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 | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 10 | #include <asn1fix_export.h> /* Stuff exported by libasn1fix */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 12 | typedef struct tag2el_s { |
| 13 | struct asn1p_type_tag_s el_tag; |
| 14 | int el_no; |
Lev Walkin | 38abe79 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 15 | int toff_first; |
| 16 | int toff_last; |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 17 | asn1p_expr_t *from_expr; |
| 18 | } tag2el_t; |
| 19 | |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 20 | typedef enum fte { |
| 21 | FTE_ALLTAGS, |
| 22 | FTE_CANONICAL_XER, |
| 23 | } fte_e; |
| 24 | static int _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags); |
| 25 | 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] | 26 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg); |
| 28 | static int asn1c_lang_C_type_SET_def(arg_t *arg); |
| 29 | static int asn1c_lang_C_type_CHOICE_def(arg_t *arg); |
| 30 | 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] | 31 | static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p); |
Lev Walkin | 3563148 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 32 | static int check_if_extensible(asn1p_expr_t *expr); |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 33 | static int expr_better_indirect(arg_t *arg, asn1p_expr_t *expr); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 34 | static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 35 | static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr); |
| 36 | static int emit_member_table(arg_t *arg, asn1p_expr_t *expr); |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 37 | 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] | 38 | static int emit_include_dependencies(arg_t *arg); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 39 | static int out_name_chain(arg_t *arg, int check_reserved_keywords); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 41 | enum tvm_compat { |
| 42 | _TVM_SAME = 0, /* tags and all_tags are same */ |
| 43 | _TVM_SUBSET = 1, /* tags are subset of all_tags */ |
| 44 | _TVM_DIFFERENT = 2, /* tags and all_tags are different */ |
| 45 | }; |
| 46 | static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc); |
| 47 | |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 48 | enum etd_spec { |
| 49 | ETD_NO_SPECIFICS, |
| 50 | ETD_HAS_SPECIFICS |
| 51 | }; |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 52 | 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] | 53 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 54 | #define C99_MODE (!(arg->flags & A1C_NO_C99)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | #define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS) |
Lev Walkin | dd32b59 | 2004-09-06 08:07:29 +0000 | [diff] [blame] | 56 | #define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 57 | |
| 58 | #define PCTX_DEF INDENTED( \ |
| 59 | OUT("\n"); \ |
| 60 | OUT("/* Context for parsing across buffer boundaries */\n"); \ |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 61 | OUT("asn_struct_ctx_t _asn_ctx;\n")); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 63 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 64 | #define DEPENDENCIES do { \ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 65 | emit_include_dependencies(arg); \ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 66 | if(expr->expr_type == ASN_CONSTR_SET_OF) \ |
| 67 | GEN_INCLUDE("asn_SET_OF"); \ |
| 68 | if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \ |
| 69 | GEN_INCLUDE("asn_SEQUENCE_OF"); \ |
| 70 | } while(0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 71 | |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 72 | /* MKID_safe() without checking for reserved keywords */ |
| 73 | #define MKID(id) asn1c_make_identifier(0, (id), 0) |
| 74 | #define MKID_safe(id) asn1c_make_identifier(AMI_CHECK_RESERVED, (id), 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 75 | |
| 76 | int |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 77 | asn1c_lang_C_type_REAL(arg_t *arg) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 78 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 79 | } |
| 80 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 81 | struct value2enum { |
| 82 | asn1c_integer_t value; |
| 83 | const char *name; |
| 84 | int idx; |
| 85 | }; |
| 86 | static int compar_enumMap_byName(const void *ap, const void *bp) { |
| 87 | const struct value2enum *a = (const struct value2enum *)ap; |
| 88 | const struct value2enum *b = (const struct value2enum *)bp; |
| 89 | return strcmp(a->name, b->name); |
| 90 | } |
| 91 | static int compar_enumMap_byValue(const void *ap, const void *bp) { |
| 92 | const struct value2enum *a = (const struct value2enum *)ap; |
| 93 | const struct value2enum *b = (const struct value2enum *)bp; |
| 94 | if(a->value < b->value) |
| 95 | return -1; |
| 96 | else if(a->value == b->value) |
| 97 | return 0; |
| 98 | return 1; |
| 99 | } |
| 100 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | int |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 102 | asn1c_lang_C_type_common_INTEGER(arg_t *arg) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 103 | asn1p_expr_t *expr = arg->expr; |
| 104 | asn1p_expr_t *v; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 105 | int el_count = expr_elements_count(arg, expr); |
| 106 | struct value2enum *v2e; |
| 107 | int map_is_extensible = (expr->expr_type == ASN_BASIC_INTEGER); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 108 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 109 | v2e = alloca((el_count + 1) * sizeof(*v2e)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 111 | /* |
Lev Walkin | 8ecf9db | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 112 | * For all ENUMERATED types [and for those INTEGER types which |
| 113 | * have identifiers -- prohibited by X.693:8.3.4], |
| 114 | * print out an enumeration table and a mapping |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 115 | * between identifiers and associated values. |
| 116 | */ |
Lev Walkin | 8ecf9db | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 117 | if(expr->expr_type == ASN_BASIC_ENUMERATED |
| 118 | || (0 && el_count /* -- prohibited by X.693:8.3.4 */)) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 119 | int eidx = 0; |
| 120 | |
| 121 | REDIR(OT_DEPS); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 122 | OUT("typedef enum "); |
| 123 | out_name_chain(arg, 1); |
| 124 | OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 125 | TQ_FOR(v, &(expr->members), next) { |
| 126 | switch(v->expr_type) { |
| 127 | case A1TC_UNIVERVAL: |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 128 | OUT("\t"); |
| 129 | out_name_chain(arg, 0); |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 130 | OUT("_%s", MKID(v->Identifier)); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 131 | OUT("\t= %" PRIdASN "%s\n", |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 132 | v->value->value.v_integer, |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 133 | (eidx+1 < el_count) ? "," : ""); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 134 | v2e[eidx].name = v->Identifier; |
| 135 | v2e[eidx].value = v->value->value.v_integer; |
| 136 | eidx++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 137 | break; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 138 | case A1TC_EXTENSIBLE: |
| 139 | OUT("\t/*\n"); |
| 140 | OUT("\t * Enumeration is extensible\n"); |
| 141 | OUT("\t */\n"); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 142 | map_is_extensible = 1; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 143 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | default: |
| 145 | return -1; |
| 146 | } |
| 147 | } |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 148 | OUT("} "); |
| 149 | out_name_chain(arg, 0); |
| 150 | OUT("_e;\n"); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 151 | assert(eidx == el_count); |
| 152 | |
| 153 | /* |
| 154 | * Generate a enumerationName<->value map for XER codec. |
| 155 | */ |
| 156 | REDIR(OT_STAT_DEFS); |
| 157 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 158 | OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_%d_value2enum[] = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 159 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 160 | qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue); |
| 161 | for(eidx = 0; eidx < el_count; eidx++) { |
| 162 | v2e[eidx].idx = eidx; |
| 163 | OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n", |
| 164 | v2e[eidx].value, |
| 165 | (long)strlen(v2e[eidx].name), v2e[eidx].name, |
| 166 | (eidx + 1 < el_count) ? "," : ""); |
| 167 | } |
| 168 | if(map_is_extensible) |
| 169 | OUT("\t/* This list is extensible */\n"); |
| 170 | OUT("};\n"); |
| 171 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 172 | OUT("static unsigned int asn_MAP_%s_%d_enum2value[] = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 173 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 174 | qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName); |
| 175 | for(eidx = 0; eidx < el_count; eidx++) { |
| 176 | OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n", |
| 177 | v2e[eidx].idx, |
| 178 | (eidx + 1 < el_count) ? "," : "", |
| 179 | v2e[eidx].name, v2e[eidx].value); |
| 180 | } |
| 181 | if(map_is_extensible) |
| 182 | OUT("\t/* This list is extensible */\n"); |
| 183 | OUT("};\n"); |
| 184 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 185 | OUT("static asn_INTEGER_specifics_t asn_SPC_%s_%d_specs = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 186 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 187 | INDENT(+1); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 188 | OUT("asn_MAP_%s_%d_value2enum,\t" |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 189 | "/* \"tag\" => N; sorted by tag */\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 190 | MKID(expr->Identifier), |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 191 | expr->_type_unique_index); |
| 192 | OUT("asn_MAP_%s_%d_enum2value,\t" |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 193 | "/* N => \"tag\"; sorted by N */\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 194 | MKID(expr->Identifier), |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 195 | expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 196 | OUT("%d,\t/* Number of elements in the maps */\n", |
| 197 | el_count); |
| 198 | OUT("%d,\t/* Enumeration is %sextensible */\n", |
| 199 | map_is_extensible, map_is_extensible ? "": "not "); |
| 200 | if(expr->expr_type == ASN_BASIC_ENUMERATED) |
| 201 | OUT("1\t/* Strict enumeration */\n"); |
| 202 | else |
| 203 | OUT("0\n"); |
| 204 | INDENT(-1); |
| 205 | OUT("};\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 209 | } |
| 210 | |
| 211 | int |
| 212 | asn1c_lang_C_type_SEQUENCE(arg_t *arg) { |
| 213 | asn1p_expr_t *expr = arg->expr; |
| 214 | asn1p_expr_t *v; |
| 215 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
| 216 | |
| 217 | DEPENDENCIES; |
| 218 | |
| 219 | if(arg->embed) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 220 | OUT("struct "); |
| 221 | out_name_chain(arg, 1); |
| 222 | OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 223 | } else { |
| 224 | OUT("typedef struct %s {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 225 | MKID_safe(expr->Identifier)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | TQ_FOR(v, &(expr->members), next) { |
| 229 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 230 | if(comp_mode < 3) comp_mode++; |
| 231 | } |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 232 | if(comp_mode == 1 |
| 233 | || expr_better_indirect(arg, v)) |
Lev Walkin | 0204fa6 | 2004-09-15 11:54:38 +0000 | [diff] [blame] | 234 | v->marker.flags |= EM_INDIRECT; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 235 | EMBED(v); |
| 236 | } |
| 237 | |
| 238 | PCTX_DEF; |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 239 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 240 | expr->_anonymous_type ? "" : |
| 241 | arg->embed |
| 242 | ? MKID_safe(expr->Identifier) |
| 243 | : MKID(expr->Identifier), |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 244 | arg->embed ? "" : "_t"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 245 | |
| 246 | return asn1c_lang_C_type_SEQUENCE_def(arg); |
| 247 | } |
| 248 | |
| 249 | static int |
| 250 | asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) { |
| 251 | asn1p_expr_t *expr = arg->expr; |
| 252 | asn1p_expr_t *v; |
| 253 | int elements; /* Number of elements */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 254 | int ext_start = -1; |
| 255 | int ext_stop = -1; |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 256 | tag2el_t *tag2el = NULL; |
| 257 | int tag2el_count = 0; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 258 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 259 | int all_tags_count; |
| 260 | enum tvm_compat tv_mode; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 261 | |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 262 | /* |
| 263 | * Fetch every inner tag from the tag to elements map. |
| 264 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 265 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) { |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 266 | if(tag2el) free(tag2el); |
| 267 | return -1; |
| 268 | } |
| 269 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 270 | GEN_INCLUDE("constr_SEQUENCE"); |
| 271 | if(!arg->embed) |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 272 | GEN_DECLARE(expr); /* asn_DEF_xxx */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 273 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 274 | REDIR(OT_STAT_DEFS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * Print out the table according to which the parsing is performed. |
| 278 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 279 | if(expr_elements_count(arg, expr)) { |
| 280 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 281 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 282 | OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 283 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 284 | |
| 285 | elements = 0; |
| 286 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
| 287 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 288 | if((++comp_mode) == 1) |
| 289 | ext_start = elements - 1; |
| 290 | else |
| 291 | ext_stop = elements - 1; |
| 292 | continue; |
| 293 | } |
| 294 | elements++; |
| 295 | emit_member_table(arg, v); |
| 296 | }); |
| 297 | OUT("};\n"); |
| 298 | } else { |
| 299 | elements = 0; |
| 300 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 301 | |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 302 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 303 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 304 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 305 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 306 | |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 307 | /* |
| 308 | * Tags to elements map. |
| 309 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 310 | emit_tag2member_map(arg, tag2el, tag2el_count, 0); |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 311 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 312 | OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_%d_specs = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 313 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 314 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 315 | OUT("sizeof(struct "); out_name_chain(arg, 1); OUT("),\n"); |
| 316 | OUT("offsetof(struct "); out_name_chain(arg, 1); OUT(", _asn_ctx),\n"); |
| 317 | |
| 318 | OUT("asn_MAP_%s_%d_tag2el,\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 319 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 320 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 321 | OUT("%d,\t/* Start extensions */\n", |
| 322 | ext_start); |
| 323 | OUT("%d\t/* Stop extensions */\n", |
| 324 | (ext_stop<ext_start)?elements+1:ext_stop, ext_stop); |
| 325 | ); |
| 326 | OUT("};\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 327 | |
| 328 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 329 | * Emit asn_DEF_xxx table. |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 330 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 331 | 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] | 332 | ETD_HAS_SPECIFICS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 333 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 334 | REDIR(OT_TYPE_DECLS); |
| 335 | |
| 336 | return 0; |
Lev Walkin | b0b3341 | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 337 | } /* _SEQUENCE_def() */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 338 | |
| 339 | int |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 340 | asn1c_lang_C_type_SET(arg_t *arg) { |
| 341 | asn1p_expr_t *expr = arg->expr; |
| 342 | asn1p_expr_t *v; |
| 343 | long mcount; |
| 344 | char *id; |
| 345 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
| 346 | |
| 347 | DEPENDENCIES; |
| 348 | |
| 349 | REDIR(OT_DEPS); |
| 350 | |
| 351 | OUT("\n"); |
| 352 | OUT("/*\n"); |
| 353 | OUT(" * Method of determining the components presence\n"); |
| 354 | OUT(" */\n"); |
| 355 | mcount = 0; |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 356 | OUT("typedef enum "); |
| 357 | out_name_chain(arg, 0); |
| 358 | OUT("_PR {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 359 | TQ_FOR(v, &(expr->members), next) { |
| 360 | if(v->expr_type == A1TC_EXTENSIBLE) continue; |
| 361 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 362 | out_name_chain(arg, 0); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 363 | OUT("_PR_"); |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 364 | id = MKID(v->Identifier); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 365 | OUT("%s,\t/* Member %s is present */\n", |
| 366 | id, id) |
| 367 | ); |
| 368 | mcount++; |
| 369 | } |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 370 | OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 371 | |
| 372 | REDIR(OT_TYPE_DECLS); |
| 373 | |
| 374 | if(arg->embed) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 375 | OUT("struct "); |
| 376 | out_name_chain(arg, 1); |
| 377 | OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 378 | } else { |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 379 | OUT("typedef struct %s {\n", MKID_safe(expr->Identifier)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | TQ_FOR(v, &(expr->members), next) { |
| 383 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 384 | if(comp_mode < 3) comp_mode++; |
| 385 | } |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 386 | if(comp_mode == 1 |
| 387 | || expr_better_indirect(arg, v)) |
Lev Walkin | 0204fa6 | 2004-09-15 11:54:38 +0000 | [diff] [blame] | 388 | v->marker.flags |= EM_INDIRECT; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 389 | EMBED(v); |
| 390 | } |
| 391 | |
| 392 | INDENTED( |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 393 | id = MKID(expr->Identifier); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 394 | OUT("\n"); |
| 395 | OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n", |
| 396 | id, id); |
| 397 | OUT("unsigned int _presence_map\n"); |
| 398 | OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount); |
| 399 | ); |
| 400 | |
| 401 | PCTX_DEF; |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 402 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 403 | expr->_anonymous_type ? "" : MKID_safe(expr->Identifier), |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 404 | arg->embed ? "" : "_t"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 405 | |
| 406 | return asn1c_lang_C_type_SET_def(arg); |
| 407 | } |
| 408 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 409 | static int |
| 410 | asn1c_lang_C_type_SET_def(arg_t *arg) { |
| 411 | asn1p_expr_t *expr = arg->expr; |
| 412 | asn1p_expr_t *v; |
| 413 | int elements; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 414 | tag2el_t *tag2el = NULL; |
| 415 | int tag2el_count = 0; |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 416 | tag2el_t *tag2el_cxer = NULL; |
| 417 | int tag2el_cxer_count = 0; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 418 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 419 | int all_tags_count; |
| 420 | enum tvm_compat tv_mode; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 421 | char *p; |
| 422 | |
| 423 | /* |
| 424 | * Fetch every inner tag from the tag to elements map. |
| 425 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 426 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 427 | if(tag2el) free(tag2el); |
| 428 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 429 | } |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 430 | if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) { |
| 431 | if(tag2el) free(tag2el); |
| 432 | if(tag2el_cxer) free(tag2el_cxer); |
| 433 | return -1; |
| 434 | } |
| 435 | if(tag2el_cxer_count == tag2el_count |
| 436 | && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) { |
| 437 | free(tag2el_cxer); |
| 438 | tag2el_cxer = 0; |
| 439 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 440 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 441 | GEN_INCLUDE("constr_SET"); |
| 442 | if(!arg->embed) |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 443 | GEN_DECLARE(expr); /* asn_DEF_xxx */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 444 | |
| 445 | REDIR(OT_STAT_DEFS); |
| 446 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 447 | /* |
| 448 | * Print out the table according to which the parsing is performed. |
| 449 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 450 | if(expr_elements_count(arg, expr)) { |
| 451 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 452 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 453 | OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 454 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 455 | |
| 456 | elements = 0; |
| 457 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
| 458 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 459 | if(comp_mode < 3) comp_mode++; |
| 460 | } else { |
| 461 | if(comp_mode == 1 |
| 462 | || expr_better_indirect(arg, v)) |
| 463 | v->marker.flags |= EM_INDIRECT; |
| 464 | elements++; |
| 465 | emit_member_table(arg, v); |
| 466 | } |
| 467 | }); |
| 468 | OUT("};\n"); |
| 469 | } else { |
| 470 | elements = 0; |
| 471 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 472 | |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 473 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 474 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 475 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 476 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * Tags to elements map. |
| 480 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 481 | emit_tag2member_map(arg, tag2el, tag2el_count, 0); |
| 482 | if(tag2el_cxer) |
| 483 | emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 484 | |
| 485 | /* |
| 486 | * Emit a map of mandatory elements. |
| 487 | */ |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 488 | OUT("static uint8_t asn_MAP_%s_%d_mmap", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 489 | MKID(expr->Identifier), expr->_type_unique_index); |
| 490 | p = MKID_safe(expr->Identifier); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 491 | OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 492 | OUT(" = {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 493 | INDENTED( |
| 494 | if(elements) { |
| 495 | int delimit = 0; |
| 496 | int el = 0; |
| 497 | TQ_FOR(v, &(expr->members), next) { |
| 498 | if(v->expr_type == A1TC_EXTENSIBLE) continue; |
| 499 | if(delimit) { |
| 500 | OUT(",\n"); |
| 501 | delimit = 0; |
| 502 | } else if(el) { |
| 503 | OUT(" | "); |
| 504 | } |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 505 | OUT("(%d << %d)", |
Lev Walkin | 0204fa6 | 2004-09-15 11:54:38 +0000 | [diff] [blame] | 506 | v->marker.flags?0:1, |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 507 | 7 - (el % 8)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 508 | if(el && (el % 8) == 0) |
| 509 | delimit = 1; |
| 510 | el++; |
| 511 | } |
| 512 | } else { |
| 513 | OUT("0"); |
| 514 | } |
| 515 | ); |
| 516 | OUT("\n"); |
| 517 | OUT("};\n"); |
| 518 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 519 | OUT("static asn_SET_specifics_t asn_SPC_%s_%d_specs = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 520 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 521 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 522 | OUT("sizeof(struct "); |
| 523 | out_name_chain(arg, 1); |
| 524 | OUT("),\n"); |
| 525 | OUT("offsetof(struct "); |
| 526 | out_name_chain(arg, 1); |
| 527 | OUT(", _asn_ctx),\n"); |
| 528 | OUT("offsetof(struct "); |
| 529 | out_name_chain(arg, 1); |
| 530 | OUT(", _presence_map),\n"); |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 531 | p = MKID(expr->Identifier); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 532 | OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 533 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 534 | if(tag2el_cxer) |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 535 | OUT("asn_MAP_%s_%d_tag2el_cxer,\n", |
| 536 | p, expr->_type_unique_index); |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 537 | else |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 538 | OUT("asn_MAP_%s_%d_tag2el,\t/* Same as above */\n", |
| 539 | p, expr->_type_unique_index); |
| 540 | OUT("%d,\t/* Count of tags in the CXER map */\n", |
| 541 | tag2el_cxer_count); |
Lev Walkin | 3563148 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 542 | OUT("%d,\t/* Whether extensible */\n", |
| 543 | check_if_extensible(expr)); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 544 | OUT("(unsigned int *)asn_MAP_%s_%d_mmap\t/* Mandatory elements map */\n", |
| 545 | p, expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 546 | ); |
| 547 | OUT("};\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 548 | |
| 549 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 550 | * Emit asn_DEF_xxx table. |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 551 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 552 | 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] | 553 | ETD_HAS_SPECIFICS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 554 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 555 | REDIR(OT_TYPE_DECLS); |
| 556 | |
| 557 | return 0; |
Lev Walkin | b0b3341 | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 558 | } /* _SET_def() */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 559 | |
| 560 | int |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 561 | asn1c_lang_C_type_SEx_OF(arg_t *arg) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 562 | asn1p_expr_t *expr = arg->expr; |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 563 | asn1p_expr_t *memb = TQ_FIRST(&expr->members); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 564 | |
| 565 | DEPENDENCIES; |
| 566 | |
| 567 | if(arg->embed) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 568 | OUT("struct "); |
| 569 | out_name_chain(arg, 1); |
| 570 | OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 571 | } else { |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 572 | OUT("typedef struct %s {\n", MKID_safe(expr->Identifier)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 575 | INDENT(+1); |
| 576 | OUT("A_%s_OF(", |
| 577 | (arg->expr->expr_type == ASN_CONSTR_SET_OF) |
| 578 | ? "SET" : "SEQUENCE"); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 579 | if(memb->expr_type & ASN_CONSTR_MASK |
| 580 | || ((memb->expr_type == ASN_BASIC_ENUMERATED |
Lev Walkin | 8ecf9db | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 581 | || (0 /* -- prohibited by X.693:8.3.4 */ |
| 582 | && memb->expr_type == ASN_BASIC_INTEGER)) |
| 583 | && expr_elements_count(arg, memb))) { |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 584 | arg_t tmp; |
| 585 | asn1p_expr_t tmp_memb; |
| 586 | arg->embed++; |
| 587 | tmp = *arg; |
| 588 | tmp.expr = &tmp_memb; |
| 589 | tmp_memb = *memb; |
| 590 | tmp_memb._anonymous_type = 1; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 591 | if(tmp_memb.Identifier == 0) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 592 | tmp_memb.Identifier = "Member"; |
| 593 | if(0) |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 594 | tmp_memb.Identifier = strdup( |
| 595 | asn1c_make_identifier(0, |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 596 | expr->Identifier, "Member", 0)); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 597 | assert(tmp_memb.Identifier); |
| 598 | } |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 599 | tmp.default_cb(&tmp); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 600 | if(tmp_memb.Identifier != memb->Identifier) |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 601 | if(0) free(tmp_memb.Identifier); |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 602 | arg->embed--; |
| 603 | assert(arg->target->target == OT_TYPE_DECLS); |
| 604 | } else { |
Lev Walkin | fe7f6ec | 2005-03-04 22:38:22 +0000 | [diff] [blame] | 605 | OUT("%s", asn1c_type_name(arg, memb, |
| 606 | (memb->marker.flags & EM_UNRECURSE) |
| 607 | ? TNF_RSAFE : TNF_CTYPE)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 608 | } |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 609 | OUT(") list;\n"); |
| 610 | INDENT(-1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 611 | |
| 612 | PCTX_DEF; |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 613 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 614 | expr->_anonymous_type ? "" : MKID_safe(expr->Identifier), |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 615 | arg->embed ? "" : "_t"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 616 | |
| 617 | /* |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 618 | * SET OF/SEQUENCE OF definition |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 619 | */ |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 620 | return asn1c_lang_C_type_SEx_OF_def(arg, |
| 621 | (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static int |
| 625 | asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) { |
| 626 | asn1p_expr_t *expr = arg->expr; |
| 627 | asn1p_expr_t *v; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 628 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 629 | int all_tags_count; |
| 630 | enum tvm_compat tv_mode; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 631 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 632 | /* |
| 633 | * Print out the table according to which the parsing is performed. |
| 634 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 635 | if(seq_of) { |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 636 | GEN_INCLUDE("constr_SEQUENCE_OF"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 637 | } else { |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 638 | GEN_INCLUDE("constr_SET_OF"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 639 | } |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 640 | if(!arg->embed) |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 641 | GEN_DECLARE(expr); /* asn_DEF_xxx */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 642 | |
| 643 | REDIR(OT_STAT_DEFS); |
| 644 | |
| 645 | /* |
| 646 | * Print out the table according to which the parsing is performed. |
| 647 | */ |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 648 | OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
| 649 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 650 | INDENT(+1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 651 | v = TQ_FIRST(&(expr->members)); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 652 | if(!v->Identifier) { |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 653 | v->Identifier = strdup("Member"); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 654 | assert(v->Identifier); |
| 655 | } |
| 656 | v->_anonymous_type = 1; |
| 657 | arg->embed++; |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 658 | emit_member_table(arg, v); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 659 | arg->embed--; |
| 660 | INDENT(-1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 661 | OUT("};\n"); |
| 662 | |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 663 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 664 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 665 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 666 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 667 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 668 | OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 669 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 670 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 671 | OUT("sizeof(struct "); |
| 672 | out_name_chain(arg, 1); |
| 673 | OUT("),\n"); |
| 674 | OUT("offsetof(struct "); |
| 675 | out_name_chain(arg, 1); |
| 676 | OUT(", _asn_ctx),\n"); |
| 677 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 678 | if(expr_as_xmlvaluelist(arg, v)) |
| 679 | OUT("1,\t/* XER encoding is XMLValueList */\n"); |
| 680 | else |
| 681 | OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 682 | ); |
| 683 | OUT("};\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 684 | |
| 685 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 686 | * Emit asn_DEF_xxx table. |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 687 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 688 | 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] | 689 | ETD_HAS_SPECIFICS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 690 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 691 | REDIR(OT_TYPE_DECLS); |
| 692 | |
| 693 | return 0; |
Lev Walkin | b0b3341 | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 694 | } /* _SEx_OF_def() */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 695 | |
| 696 | int |
| 697 | asn1c_lang_C_type_CHOICE(arg_t *arg) { |
| 698 | asn1p_expr_t *expr = arg->expr; |
| 699 | asn1p_expr_t *v; |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 700 | char *id; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 701 | |
| 702 | DEPENDENCIES; |
| 703 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 704 | REDIR(OT_DEPS); |
| 705 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 706 | OUT("typedef enum "); |
| 707 | out_name_chain(arg, 0); |
| 708 | OUT("_PR {\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 709 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 710 | out_name_chain(arg, 0); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 711 | OUT("_PR_NOTHING,\t/* No components present */\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 712 | TQ_FOR(v, &(expr->members), next) { |
| 713 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 714 | OUT("/* Extensions may appear below */\n"); |
| 715 | continue; |
| 716 | } |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 717 | out_name_chain(arg, 0); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 718 | OUT("_PR_"); |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 719 | id = MKID(v->Identifier); |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 720 | OUT("%s,\n", id, id); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 721 | } |
| 722 | ); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 723 | OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 724 | |
| 725 | REDIR(OT_TYPE_DECLS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 726 | |
| 727 | if(arg->embed) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 728 | OUT("struct "); out_name_chain(arg, 1); OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 729 | } else { |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 730 | OUT("typedef struct %s {\n", MKID_safe(expr->Identifier)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 734 | out_name_chain(arg, 0); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 735 | OUT("_PR present;\n"); |
| 736 | OUT("union {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 737 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 738 | if(expr_better_indirect(arg, v)) |
Lev Walkin | 0204fa6 | 2004-09-15 11:54:38 +0000 | [diff] [blame] | 739 | v->marker.flags |= EM_INDIRECT; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 740 | EMBED(v); |
| 741 | } |
| 742 | if(UNNAMED_UNIONS) OUT("};\n"); |
| 743 | else OUT("} choice;\n"); |
| 744 | ); |
| 745 | |
| 746 | PCTX_DEF; |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 747 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 748 | expr->_anonymous_type ? "" : |
| 749 | arg->embed |
| 750 | ? MKID_safe(expr->Identifier) |
| 751 | : MKID(expr->Identifier), |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 752 | arg->embed ? "" : "_t"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 753 | |
| 754 | return asn1c_lang_C_type_CHOICE_def(arg); |
| 755 | } |
| 756 | |
| 757 | static int |
| 758 | asn1c_lang_C_type_CHOICE_def(arg_t *arg) { |
| 759 | asn1p_expr_t *expr = arg->expr; |
| 760 | asn1p_expr_t *v; |
| 761 | int elements; /* Number of elements */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 762 | tag2el_t *tag2el = NULL; |
| 763 | int tag2el_count = 0; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 764 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 765 | int all_tags_count; |
| 766 | enum tvm_compat tv_mode; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 767 | |
| 768 | /* |
| 769 | * Fetch every inner tag from the tag to elements map. |
| 770 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 771 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 772 | if(tag2el) free(tag2el); |
| 773 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 776 | GEN_INCLUDE("constr_CHOICE"); |
| 777 | if(!arg->embed) |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 778 | GEN_DECLARE(expr); /* asn_DEF_xxx */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 779 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 780 | REDIR(OT_STAT_DEFS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 781 | |
| 782 | /* |
| 783 | * Print out the table according to which the parsing is performed. |
| 784 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 785 | if(expr_elements_count(arg, expr)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 786 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 787 | OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
| 788 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 789 | |
| 790 | elements = 0; |
| 791 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | 659c63b | 2005-01-17 11:40:49 +0000 | [diff] [blame] | 792 | if(v->expr_type == A1TC_EXTENSIBLE) |
| 793 | continue; |
| 794 | if(expr_better_indirect(arg, v)) |
| 795 | v->marker.flags |= EM_INDIRECT; |
| 796 | elements++; |
| 797 | emit_member_table(arg, v); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 798 | }); |
| 799 | OUT("};\n"); |
| 800 | } else { |
| 801 | elements = 0; |
| 802 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 803 | |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 804 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 805 | if(arg->embed) { |
| 806 | /* |
| 807 | * Our parent structure has already taken this into account. |
| 808 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 809 | tv_mode = _TVM_SAME; |
| 810 | tags_count = all_tags_count = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 811 | } else { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 812 | tv_mode = emit_tags_vectors(arg, expr, |
| 813 | &tags_count, &all_tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 814 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | * Tags to elements map. |
| 818 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 819 | emit_tag2member_map(arg, tag2el, tag2el_count, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 820 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 821 | OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 822 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 823 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 824 | OUT("sizeof(struct "); |
| 825 | out_name_chain(arg, 1); |
| 826 | OUT("),\n"); |
| 827 | OUT("offsetof(struct "); |
| 828 | out_name_chain(arg, 1); |
| 829 | OUT(", _asn_ctx),\n"); |
| 830 | OUT("offsetof(struct "); |
| 831 | out_name_chain(arg, 1); |
| 832 | OUT(", present),\n"); |
| 833 | OUT("sizeof(((struct "); |
| 834 | out_name_chain(arg, 1); |
| 835 | OUT(" *)0)->present),\n"); |
| 836 | OUT("asn_MAP_%s_%d_tag2el,\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 837 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 838 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
Lev Walkin | 3563148 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 839 | OUT("%d\t/* Whether extensible */\n", |
| 840 | check_if_extensible(expr)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 841 | ); |
| 842 | OUT("};\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 843 | |
| 844 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 845 | * Emit asn_DEF_xxx table. |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 846 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 847 | 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] | 848 | ETD_HAS_SPECIFICS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 849 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 850 | REDIR(OT_TYPE_DECLS); |
| 851 | |
| 852 | return 0; |
Lev Walkin | b0b3341 | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 853 | } /* _CHOICE_def() */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 854 | |
| 855 | int |
| 856 | asn1c_lang_C_type_REFERENCE(arg_t *arg) { |
| 857 | asn1p_ref_t *ref; |
| 858 | |
| 859 | ref = arg->expr->reference; |
| 860 | if(ref->components[ref->comp_count-1].name[0] == '&') { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 861 | asn1p_expr_t *extract; |
| 862 | arg_t tmp; |
| 863 | int ret; |
| 864 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 865 | extract = asn1f_class_access_ex(arg->asn, arg->expr->module, |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 866 | arg->expr, ref); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 867 | if(extract == NULL) |
| 868 | return -1; |
| 869 | |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 870 | extract = asn1p_expr_clone(extract, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 871 | if(extract) { |
| 872 | if(extract->Identifier) |
| 873 | free(extract->Identifier); |
| 874 | extract->Identifier = strdup(arg->expr->Identifier); |
| 875 | if(extract->Identifier == NULL) { |
| 876 | asn1p_expr_free(extract); |
| 877 | return -1; |
| 878 | } |
| 879 | } else { |
| 880 | return -1; |
| 881 | } |
| 882 | |
| 883 | tmp = *arg; |
| 884 | tmp.asn = arg->asn; |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 885 | tmp.mod = extract->module; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 886 | tmp.expr = extract; |
| 887 | |
| 888 | ret = arg->default_cb(&tmp); |
| 889 | |
| 890 | asn1p_expr_free(extract); |
| 891 | |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | |
| 896 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 897 | } |
| 898 | |
| 899 | int |
| 900 | asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) { |
| 901 | asn1p_expr_t *expr = arg->expr; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 902 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 903 | int all_tags_count; |
| 904 | enum tvm_compat tv_mode; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 905 | enum etd_spec etd_spec; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 906 | char *p; |
| 907 | |
| 908 | if(arg->embed) { |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 909 | enum tnfmt tnfmt = TNF_CTYPE; |
| 910 | |
| 911 | /* |
| 912 | * If this is an optional compound type, |
| 913 | * refer it using "struct X" convention, |
| 914 | * as it may recursively include the current structure. |
| 915 | */ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 916 | if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) { |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 917 | asn1p_expr_t *terminal; |
| 918 | terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 919 | if(terminal |
| 920 | && (terminal->expr_type & ASN_CONSTR_MASK)) { |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 921 | tnfmt = TNF_RSAFE; |
| 922 | REDIR(OT_FWD_DECLS); |
Lev Walkin | bf56d54 | 2005-03-04 23:50:56 +0000 | [diff] [blame] | 923 | OUT("%s;\n", |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 924 | asn1c_type_name(arg, arg->expr, tnfmt)); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 928 | REDIR(OT_TYPE_DECLS); |
| 929 | |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 930 | OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt)); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 931 | if(!expr->_anonymous_type) { |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 932 | OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t "); |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 933 | OUT("%s", MKID_safe(expr->Identifier)); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 934 | if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT) |
| 935 | OUT("\t/* DEFAULT %s */", |
| 936 | asn1f_printable_value( |
| 937 | expr->marker.default_value)); |
| 938 | else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) |
| 939 | OUT("\t/* OPTIONAL */"); |
| 940 | } |
| 941 | |
| 942 | } else { |
| 943 | GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE)); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 944 | |
| 945 | REDIR(OT_TYPE_DECLS); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 946 | |
| 947 | OUT("typedef %s\t", |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 948 | asn1c_type_name(arg, arg->expr, TNF_CTYPE)); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 949 | OUT("%s%s_t", |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 950 | (expr->marker.flags & EM_INDIRECT)?"*":" ", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 951 | MKID(expr->Identifier)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 954 | if((expr->expr_type == ASN_BASIC_ENUMERATED) |
Lev Walkin | 8ecf9db | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 955 | || (0 /* -- prohibited by X.693:8.3.4 */ |
| 956 | && expr->expr_type == ASN_BASIC_INTEGER |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 957 | && expr_elements_count(arg, expr))) |
| 958 | etd_spec = ETD_HAS_SPECIFICS; |
| 959 | else |
| 960 | etd_spec = ETD_NO_SPECIFICS; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 961 | |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 962 | /* |
| 963 | * If this type just blindly refers the other type, alias it. |
| 964 | * Type1 ::= Type2 |
| 965 | */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 966 | if(arg->embed && etd_spec == ETD_NO_SPECIFICS) { |
| 967 | REDIR(OT_TYPE_DECLS); |
| 968 | return 0; |
| 969 | } |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 970 | if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS)) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 971 | && (arg->embed || expr->tag.tag_class == TC_NOCLASS) |
Lev Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 972 | && etd_spec == ETD_NO_SPECIFICS |
| 973 | && 0 /* This shortcut is incompatible with XER */ |
| 974 | ) { |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 975 | char *type_name; |
| 976 | REDIR(OT_FUNC_DECLS); |
| 977 | type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
| 978 | OUT("/* This type is equivalent to %s */\n", type_name); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 979 | if(HIDE_INNER_DEFS) OUT("/* "); |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 980 | OUT("#define\tasn_DEF_%s\t", MKID(expr->Identifier)); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 981 | type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 982 | OUT("asn_DEF_%s", type_name); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 983 | if(HIDE_INNER_DEFS) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 984 | OUT("\t// (Use -fall-defs-global to expose) */"); |
| 985 | OUT("\n"); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 986 | REDIR(OT_CODE); |
| 987 | OUT("/* This type is equivalent to %s */\n", type_name); |
| 988 | OUT("\n"); |
| 989 | REDIR(OT_TYPE_DECLS); |
| 990 | return 0; |
| 991 | } |
| 992 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 993 | REDIR(OT_STAT_DEFS); |
| 994 | |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 995 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 996 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 997 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 998 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 999 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1000 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, |
| 1001 | 0, etd_spec); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1002 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1003 | REDIR(OT_CODE); |
| 1004 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1005 | /* |
| 1006 | * Constraint checking. |
| 1007 | */ |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1008 | if(!(arg->flags & A1C_NO_CONSTRAINTS)) { |
| 1009 | p = MKID(expr->Identifier); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1010 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1011 | OUT("int\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1012 | OUT("%s", p); |
| 1013 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1014 | OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n"); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1015 | INDENT(+1); |
| 1016 | OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {"); |
| 1017 | OUT("\n"); |
| 1018 | if(asn1c_emit_constraint_checking_code(arg) == 1) { |
| 1019 | OUT("/* Replace with underlying type checker */\n"); |
| 1020 | OUT("td->check_constraints " |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1021 | "= asn_DEF_%s.check_constraints;\n", |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1022 | asn1c_type_name(arg, expr, TNF_SAFE)); |
| 1023 | OUT("return td->check_constraints" |
| 1024 | "(td, sptr, app_errlog, app_key);\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 1025 | } |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1026 | INDENT(-1); |
| 1027 | OUT("}\n"); |
| 1028 | OUT("\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1029 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1030 | |
| 1031 | /* |
| 1032 | * Emit suicidal functions. |
| 1033 | */ |
| 1034 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1035 | /* |
| 1036 | * This function replaces certain fields from the definition |
| 1037 | * of a type with the corresponding fields from the basic type |
| 1038 | * (from which the current type is inherited). |
| 1039 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1040 | OUT("/*\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1041 | OUT(" * This type is implemented using %s,\n", |
| 1042 | asn1c_type_name(arg, expr, TNF_SAFE)); |
Lev Walkin | b65b300 | 2004-09-23 22:20:47 +0000 | [diff] [blame] | 1043 | OUT(" * so here we adjust the DEF accordingly.\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1044 | OUT(" */\n"); |
| 1045 | OUT("static void\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1046 | OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1047 | MKID(expr->Identifier), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1048 | INDENT(+1); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1049 | { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1050 | asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1051 | char *type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1052 | OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name); |
| 1053 | OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name); |
| 1054 | OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name); |
| 1055 | OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name); |
| 1056 | OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name); |
| 1057 | OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name); |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1058 | if(!terminal && !tags_count) { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1059 | 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] | 1060 | OUT("td->tags = asn_DEF_%s.tags;\n", type_name); |
| 1061 | OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name); |
| 1062 | OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name); |
| 1063 | 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] | 1064 | OUT("/* End of these lines */\n"); |
| 1065 | } |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1066 | OUT("td->elements = asn_DEF_%s.elements;\n", type_name); |
| 1067 | OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1068 | if(etd_spec != ETD_NO_SPECIFICS) { |
| 1069 | INDENT(-1); |
| 1070 | OUT(" /* "); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1071 | } |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1072 | OUT("td->specifics = asn_DEF_%s.specifics;", type_name); |
| 1073 | if(etd_spec == ETD_NO_SPECIFICS) { |
| 1074 | INDENT(-1); |
| 1075 | OUT("\n"); |
| 1076 | } else { |
| 1077 | OUT("\t// Defined explicitly */\n"); |
| 1078 | } |
| 1079 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1080 | OUT("}\n"); |
| 1081 | OUT("\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1082 | |
| 1083 | p = MKID(expr->Identifier); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1084 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1085 | OUT("void\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1086 | OUT("%s", p); |
| 1087 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1088 | OUT("_free(asn_TYPE_descriptor_t *td,\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1089 | INDENTED( |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1090 | OUT("\tvoid *struct_ptr, int contents_only) {\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1091 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1092 | p, expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1093 | OUT("td->free_struct(td, struct_ptr, contents_only);\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1094 | ); |
| 1095 | OUT("}\n"); |
| 1096 | OUT("\n"); |
| 1097 | |
| 1098 | p = MKID(expr->Identifier); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1099 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1100 | OUT("int\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1101 | OUT("%s", p); |
| 1102 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1103 | OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1104 | INDENTED( |
| 1105 | 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] | 1106 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1107 | p, expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1108 | OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n"); |
| 1109 | ); |
| 1110 | OUT("}\n"); |
| 1111 | OUT("\n"); |
| 1112 | |
| 1113 | p = MKID(expr->Identifier); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1114 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1115 | OUT("asn_dec_rval_t\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1116 | OUT("%s", p); |
| 1117 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1118 | 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] | 1119 | INDENTED( |
Lev Walkin | dd1512e | 2005-03-17 21:56:00 +0000 | [diff] [blame] | 1120 | 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] | 1121 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1122 | p, expr->_type_unique_index); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1123 | 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] | 1124 | ); |
| 1125 | OUT("}\n"); |
| 1126 | OUT("\n"); |
| 1127 | |
| 1128 | p = MKID(expr->Identifier); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1129 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1130 | OUT("asn_enc_rval_t\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1131 | OUT("%s", p); |
| 1132 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1133 | OUT("_encode_der(asn_TYPE_descriptor_t *td,\n"); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1134 | INDENTED( |
| 1135 | OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n"); |
| 1136 | OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1137 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1138 | p, expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1139 | OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n"); |
| 1140 | ); |
| 1141 | OUT("}\n"); |
| 1142 | OUT("\n"); |
| 1143 | |
| 1144 | p = MKID(expr->Identifier); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1145 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | ca47c29 | 2004-10-23 13:34:00 +0000 | [diff] [blame] | 1146 | OUT("asn_dec_rval_t\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1147 | OUT("%s", p); |
| 1148 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1149 | 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] | 1150 | INDENTED( |
Lev Walkin | dd1512e | 2005-03-17 21:56:00 +0000 | [diff] [blame] | 1151 | 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] | 1152 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1153 | p, expr->_type_unique_index); |
Lev Walkin | ca47c29 | 2004-10-23 13:34:00 +0000 | [diff] [blame] | 1154 | OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n"); |
| 1155 | ); |
| 1156 | OUT("}\n"); |
| 1157 | OUT("\n"); |
| 1158 | |
| 1159 | p = MKID(expr->Identifier); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1160 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1161 | OUT("asn_enc_rval_t\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1162 | OUT("%s", p); |
| 1163 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1164 | OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n"); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1165 | INDENTED( |
| 1166 | OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n"); |
| 1167 | OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1168 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1169 | p, expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1170 | 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] | 1171 | ); |
| 1172 | OUT("}\n"); |
| 1173 | OUT("\n"); |
| 1174 | |
| 1175 | REDIR(OT_FUNC_DECLS); |
| 1176 | |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1177 | p = MKID(expr->Identifier); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1178 | if(HIDE_INNER_DEFS) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1179 | OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;" |
| 1180 | "\t// (Use -fall-defs-global to expose) */\n", |
| 1181 | p, expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1182 | } else { |
| 1183 | OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p); |
| 1184 | OUT("asn_struct_free_f %s_free;\n", p); |
| 1185 | OUT("asn_struct_print_f %s_print;\n", p); |
| 1186 | OUT("asn_constr_check_f %s_constraint;\n", p); |
| 1187 | OUT("ber_type_decoder_f %s_decode_ber;\n", p); |
| 1188 | OUT("der_type_encoder_f %s_encode_der;\n", p); |
| 1189 | OUT("xer_type_decoder_f %s_decode_xer;\n", p); |
| 1190 | OUT("xer_type_encoder_f %s_encode_xer;\n", p); |
| 1191 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1192 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 1193 | REDIR(OT_TYPE_DECLS); |
| 1194 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | int |
| 1199 | asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) { |
| 1200 | |
| 1201 | OUT("/*\n"); |
| 1202 | OUT(" * This type is extensible,\n"); |
| 1203 | OUT(" * possible extensions are below.\n"); |
| 1204 | OUT(" */\n"); |
| 1205 | |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
Lev Walkin | 3563148 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 1209 | static int check_if_extensible(asn1p_expr_t *expr) { |
| 1210 | asn1p_expr_t *v; |
| 1211 | TQ_FOR(v, &(expr->members), next) { |
| 1212 | if(v->expr_type == A1TC_EXTENSIBLE) return 1; |
| 1213 | } |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1217 | static int |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1218 | _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1219 | |
| 1220 | OUT("("); |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1221 | switch(tag->tag_class) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1222 | case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break; |
| 1223 | case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break; |
| 1224 | case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break; |
| 1225 | case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break; |
| 1226 | case TC_NOCLASS: |
| 1227 | break; |
| 1228 | } |
Lev Walkin | 33c16ba | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 1229 | OUT(" | (%" PRIdASN " << 2))", tag->tag_value); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1230 | |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1234 | |
| 1235 | static int |
| 1236 | _tag2el_cmp(const void *ap, const void *bp) { |
| 1237 | const tag2el_t *a = ap; |
| 1238 | const tag2el_t *b = bp; |
| 1239 | const struct asn1p_type_tag_s *ta = &a->el_tag; |
| 1240 | const struct asn1p_type_tag_s *tb = &b->el_tag; |
| 1241 | |
| 1242 | if(ta->tag_class == tb->tag_class) { |
| 1243 | if(ta->tag_value == tb->tag_value) { |
| 1244 | /* |
| 1245 | * Sort by their respective positions. |
| 1246 | */ |
| 1247 | if(a->el_no < b->el_no) |
| 1248 | return -1; |
| 1249 | else if(a->el_no > b->el_no) |
| 1250 | return 1; |
| 1251 | return 0; |
| 1252 | } else if(ta->tag_value < tb->tag_value) |
| 1253 | return -1; |
| 1254 | else |
| 1255 | return 1; |
| 1256 | } else if(ta->tag_class < tb->tag_class) { |
| 1257 | return -1; |
| 1258 | } else { |
| 1259 | return 1; |
| 1260 | } |
| 1261 | } |
| 1262 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1263 | /* |
| 1264 | * For constructed types, number of external tags may be greater than |
| 1265 | * number of elements in the type because of CHOICE type. |
| 1266 | * T ::= SET { -- Three possible tags: |
| 1267 | * a INTEGER, -- One tag is here... |
| 1268 | * b Choice1 -- ... and two more tags are there. |
| 1269 | * } |
| 1270 | * Choice1 ::= CHOICE { |
| 1271 | * s1 IA5String, |
| 1272 | * s2 ObjectDescriptor |
| 1273 | * } |
| 1274 | */ |
| 1275 | static int |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1276 | _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] | 1277 | asn1p_expr_t *expr = arg->expr; |
| 1278 | arg_t tmparg = *arg; |
| 1279 | asn1p_expr_t *v; |
| 1280 | int element = 0; |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1281 | int original_count = *count; |
| 1282 | int sort_until = -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1283 | |
| 1284 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1285 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 1286 | /* |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1287 | * CXER mandates sorting |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1288 | * only for the root part. |
| 1289 | */ |
| 1290 | if(flags == FTE_CANONICAL_XER |
| 1291 | && sort_until == -1) |
| 1292 | sort_until = *count; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1293 | continue; |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1294 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1295 | |
| 1296 | tmparg.expr = v; |
| 1297 | |
| 1298 | if(_add_tag2el_member(&tmparg, tag2el, count, |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1299 | (el_no==-1)?element:el_no, flags)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1300 | return -1; |
| 1301 | } |
| 1302 | |
| 1303 | element++; |
| 1304 | } |
| 1305 | |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1306 | |
| 1307 | if(flags == FTE_CANONICAL_XER) { |
| 1308 | if(sort_until == -1) sort_until = *count; |
| 1309 | qsort((*tag2el) + original_count, |
| 1310 | sort_until - original_count, |
| 1311 | sizeof(**tag2el), _tag2el_cmp); |
| 1312 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE |
| 1313 | && (sort_until - original_count) >= 1) { |
| 1314 | /* Only take in account the root component */ |
| 1315 | *count = original_count + 1; |
| 1316 | } |
| 1317 | } else { |
| 1318 | /* |
| 1319 | * Sort the map according to canonical order of their |
| 1320 | * tags and element numbers. |
| 1321 | */ |
| 1322 | qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp); |
| 1323 | } |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1324 | |
Lev Walkin | 38abe79 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 1325 | /* |
| 1326 | * Initialize .toff_{first|last} members. |
| 1327 | */ |
| 1328 | if(*count) { |
| 1329 | struct asn1p_type_tag_s *cur_tag = 0; |
| 1330 | tag2el_t *cur = *tag2el; |
| 1331 | tag2el_t *end = cur + *count; |
| 1332 | int occur, i; |
| 1333 | for(occur = 0; cur < end; cur++) { |
| 1334 | if(cur_tag == 0 |
| 1335 | || cur_tag->tag_value != cur->el_tag.tag_value |
| 1336 | || cur_tag->tag_class != cur->el_tag.tag_class) { |
| 1337 | cur_tag = &cur->el_tag; |
| 1338 | occur = 0; |
| 1339 | } else { |
| 1340 | occur++; |
| 1341 | } |
| 1342 | cur->toff_first = -occur; |
| 1343 | for(i = 0; i >= -occur; i--) |
| 1344 | cur[i].toff_last = -i; |
| 1345 | } |
| 1346 | } |
| 1347 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | static int |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1352 | _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] | 1353 | struct asn1p_type_tag_s tag; |
| 1354 | int ret; |
| 1355 | |
| 1356 | assert(el_no >= 0); |
| 1357 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1358 | ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module, |
| 1359 | arg->expr, &tag, 1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1360 | if(ret == 0) { |
Lev Walkin | 201943e | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1361 | tag2el_t *te; |
| 1362 | int new_count = (*count) + 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1363 | void *p; |
Lev Walkin | 201943e | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1364 | |
Lev Walkin | ac7e229 | 2004-09-05 10:42:33 +0000 | [diff] [blame] | 1365 | if(tag.tag_value == -1) { |
| 1366 | /* |
| 1367 | * This is an untagged ANY type, |
| 1368 | * proceed without adding a tag |
| 1369 | */ |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
Lev Walkin | 201943e | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1373 | p = realloc(*tag2el, new_count * sizeof(tag2el_t)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1374 | if(p) *tag2el = p; |
| 1375 | else return -1; |
| 1376 | |
| 1377 | DEBUG("Found tag for %s: %ld", |
| 1378 | arg->expr->Identifier, |
| 1379 | (long)tag.tag_value); |
| 1380 | |
Lev Walkin | 201943e | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1381 | te = &((*tag2el)[*count]); |
| 1382 | te->el_tag = tag; |
| 1383 | te->el_no = el_no; |
| 1384 | te->from_expr = arg->expr; |
| 1385 | *count = new_count; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | DEBUG("Searching tag in complex expression %s:%x at line %d", |
| 1390 | arg->expr->Identifier, |
| 1391 | arg->expr->expr_type, |
| 1392 | arg->expr->_lineno); |
| 1393 | |
| 1394 | /* |
| 1395 | * Iterate over members of CHOICE type. |
| 1396 | */ |
| 1397 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE) { |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1398 | return _fill_tag2el_map(arg, tag2el, count, el_no, flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | if(arg->expr->expr_type == A1TC_REFERENCE) { |
| 1402 | arg_t tmp = *arg; |
| 1403 | asn1p_expr_t *expr; |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1404 | expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1405 | arg->expr->reference); |
| 1406 | if(expr) { |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1407 | tmp.mod = expr->module; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1408 | tmp.expr = expr; |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1409 | return _add_tag2el_member(&tmp, tag2el, count, el_no, flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1410 | } else { |
| 1411 | FATAL("Cannot dereference %s at line %d", |
| 1412 | arg->expr->Identifier, |
| 1413 | arg->expr->_lineno); |
| 1414 | return -1; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | DEBUG("No tag for %s at line %d", |
| 1419 | arg->expr->Identifier, |
| 1420 | arg->expr->_lineno); |
| 1421 | |
| 1422 | return -1; |
| 1423 | } |
| 1424 | |
| 1425 | static int |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1426 | 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] | 1427 | asn1p_expr_t *expr = arg->expr; |
| 1428 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1429 | OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1430 | MKID(expr->Identifier), expr->_type_unique_index, |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1431 | opt_modifier?opt_modifier:""); |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1432 | if(tag2el_count) { |
| 1433 | int i; |
| 1434 | for(i = 0; i < tag2el_count; i++) { |
| 1435 | OUT(" { "); |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1436 | _print_tag(arg, &tag2el[i].el_tag); |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1437 | OUT(", "); |
Lev Walkin | 38abe79 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 1438 | OUT("%d, ", tag2el[i].el_no); |
| 1439 | OUT("%d, ", tag2el[i].toff_first); |
| 1440 | OUT("%d ", tag2el[i].toff_last); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1441 | OUT("}%s /* %s at %d */\n", |
| 1442 | (i + 1 < tag2el_count) ? "," : "", |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1443 | tag2el[i].from_expr->Identifier, |
| 1444 | tag2el[i].from_expr->_lineno |
| 1445 | ); |
| 1446 | } |
| 1447 | } |
| 1448 | OUT("};\n"); |
| 1449 | |
| 1450 | return 0;; |
| 1451 | } |
| 1452 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1453 | static enum tvm_compat |
| 1454 | emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) { |
| 1455 | struct asn1p_type_tag_s *tags = 0; /* Effective tags */ |
| 1456 | struct asn1p_type_tag_s *all_tags = 0; /* The full array */ |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1457 | int tags_count = 0; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1458 | int all_tags_count = 0; |
| 1459 | enum tvm_compat tv_mode = _TVM_SAME; |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1460 | int i; |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1461 | |
Lev Walkin | 6d1220a | 2004-09-14 14:10:10 +0000 | [diff] [blame] | 1462 | /* Cleanup before proceeding. */ |
| 1463 | *tags_count_r = 0; |
| 1464 | *all_tags_count_r = 0; |
| 1465 | |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1466 | /* Fetch a chain of tags */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1467 | tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0); |
Lev Walkin | 6d1220a | 2004-09-14 14:10:10 +0000 | [diff] [blame] | 1468 | if(tags_count < 0) |
| 1469 | return -1; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1470 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1471 | /* Fetch a chain of tags */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1472 | all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1473 | &all_tags, AFT_FULL_COLLECT); |
| 1474 | if(all_tags_count < 0) { |
| 1475 | if(tags) free(tags); |
| 1476 | return -1; |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 1477 | } |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1478 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1479 | assert(tags_count <= all_tags_count); |
| 1480 | assert((tags_count?0:1) == (all_tags_count?0:1)); |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1481 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1482 | if(tags_count <= all_tags_count) { |
| 1483 | for(i = 0; i < tags_count; i++) { |
| 1484 | if(tags[i].tag_value != all_tags[i].tag_value |
| 1485 | || tags[i].tag_class != all_tags[i].tag_class) { |
| 1486 | tv_mode = _TVM_DIFFERENT; |
| 1487 | break; |
| 1488 | } |
| 1489 | } |
| 1490 | if(i == tags_count && tags_count < all_tags_count) |
| 1491 | tv_mode = _TVM_SUBSET; |
| 1492 | } else { |
| 1493 | tv_mode = _TVM_DIFFERENT; |
| 1494 | } |
| 1495 | |
| 1496 | #define EMIT_TAGS_TABLE(name, tags, tags_count) do { \ |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1497 | OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\ |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1498 | MKID(expr->Identifier), \ |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1499 | expr->_type_unique_index, name); \ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1500 | INDENT(+1); \ |
| 1501 | /* Print the array of collected tags */ \ |
| 1502 | for(i = 0; i < tags_count; i++) { \ |
| 1503 | if(i) OUT(",\n"); \ |
| 1504 | _print_tag(arg, &tags[i]); \ |
| 1505 | } \ |
| 1506 | OUT("\n"); \ |
| 1507 | INDENT(-1); \ |
| 1508 | OUT("};\n"); \ |
| 1509 | } while(0) |
| 1510 | |
| 1511 | if(tags_count) { |
| 1512 | if(tv_mode == _TVM_SUBSET) |
| 1513 | EMIT_TAGS_TABLE("", all_tags, all_tags_count); |
| 1514 | else |
| 1515 | EMIT_TAGS_TABLE("", tags, tags_count); |
| 1516 | } |
| 1517 | |
| 1518 | if(all_tags_count) { |
| 1519 | if(tv_mode == _TVM_DIFFERENT) |
| 1520 | EMIT_TAGS_TABLE("_all", all_tags, all_tags_count); |
| 1521 | } |
| 1522 | |
| 1523 | if(tags) free(tags); |
| 1524 | if(all_tags) free(all_tags); |
| 1525 | |
| 1526 | *tags_count_r = tags_count; |
| 1527 | *all_tags_count_r = all_tags_count; |
| 1528 | |
| 1529 | return tv_mode; |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1530 | } |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1531 | |
| 1532 | static int |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1533 | expr_elements_count(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1534 | asn1p_expr_t *topmost_parent; |
| 1535 | asn1p_expr_t *v; |
| 1536 | int elements = 0; |
| 1537 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1538 | topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1539 | if(!topmost_parent) return 0; |
| 1540 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1541 | if(!(topmost_parent->expr_type & ASN_CONSTR_MASK) |
| 1542 | && !topmost_parent->expr_type == ASN_BASIC_INTEGER |
| 1543 | && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED) |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1544 | return 0; |
| 1545 | |
| 1546 | TQ_FOR(v, &(topmost_parent->members), next) { |
| 1547 | if(v->expr_type != A1TC_EXTENSIBLE) |
| 1548 | elements++; |
| 1549 | } |
| 1550 | |
| 1551 | return elements; |
| 1552 | } |
| 1553 | |
| 1554 | static int |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1555 | emit_include_dependencies(arg_t *arg) { |
| 1556 | asn1p_expr_t *expr = arg->expr; |
| 1557 | asn1p_expr_t *memb; |
| 1558 | |
| 1559 | TQ_FOR(memb, &(expr->members), next) { |
| 1560 | |
| 1561 | if((memb->meta_type == AMT_TYPEREF |
| 1562 | && (memb->marker.flags & EM_INDIRECT)) |
| 1563 | || expr->expr_type == ASN_CONSTR_SET_OF |
| 1564 | || expr->expr_type == ASN_CONSTR_SEQUENCE_OF |
| 1565 | ) { |
| 1566 | asn1p_expr_t *terminal; |
| 1567 | terminal = asn1f_find_terminal_type_ex(arg->asn, memb); |
| 1568 | if(terminal && !terminal->parent_expr |
| 1569 | && (terminal->expr_type & ASN_CONSTR_MASK)) { |
| 1570 | int saved_target = arg->target->target; |
| 1571 | REDIR(OT_FWD_DECLS); |
Lev Walkin | bf56d54 | 2005-03-04 23:50:56 +0000 | [diff] [blame] | 1572 | OUT("%s;\n", |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1573 | asn1c_type_name(arg, memb, TNF_RSAFE)); |
| 1574 | REDIR(saved_target); |
| 1575 | memb->marker.flags |= EM_UNRECURSE; |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | if((!(memb->expr_type & ASN_CONSTR_MASK) |
| 1580 | && memb->expr_type > ASN_CONSTR_MASK) |
| 1581 | || memb->meta_type == AMT_TYPEREF) { |
| 1582 | if(memb->marker.flags & EM_UNRECURSE) { |
| 1583 | GEN_POSTINCLUDE(asn1c_type_name(arg, |
| 1584 | memb, TNF_INCLUDE)); |
| 1585 | } else { |
| 1586 | GEN_INCLUDE(asn1c_type_name(arg, |
| 1587 | memb, TNF_INCLUDE)); |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
| 1595 | static int |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1596 | emit_member_table(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1597 | int save_target; |
| 1598 | arg_t tmp_arg; |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 1599 | struct asn1p_type_tag_s outmost_tag_s; |
| 1600 | struct asn1p_type_tag_s *outmost_tag; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1601 | int complex_contents; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1602 | char *p; |
| 1603 | |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 1604 | if(asn1f_fetch_outmost_tag(arg->asn, |
| 1605 | expr->module, expr, &outmost_tag_s, 1)) { |
| 1606 | outmost_tag = 0; |
| 1607 | } else { |
| 1608 | outmost_tag = &outmost_tag_s; |
| 1609 | } |
| 1610 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1611 | OUT("{ "); |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 1612 | |
| 1613 | if(outmost_tag && outmost_tag->tag_value == -1) |
| 1614 | OUT("ATF_OPEN_TYPE | "); |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1615 | OUT("%s, ", |
| 1616 | (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS"); |
Lev Walkin | 0204fa6 | 2004-09-15 11:54:38 +0000 | [diff] [blame] | 1617 | if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) { |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1618 | asn1p_expr_t *tv; |
| 1619 | int opts = 0; |
Lev Walkin | 0204fa6 | 2004-09-15 11:54:38 +0000 | [diff] [blame] | 1620 | for(tv = expr; tv && tv->marker.flags; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1621 | tv = TQ_NEXT(tv, next), opts++) { |
| 1622 | if(tv->expr_type == A1TC_EXTENSIBLE) |
| 1623 | opts--; |
| 1624 | } |
| 1625 | OUT("%d, ", opts); |
| 1626 | } else { |
| 1627 | OUT("0, "); |
| 1628 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1629 | if(expr->_anonymous_type) { |
| 1630 | assert(arg->expr->expr_type == ASN_CONSTR_SET_OF |
| 1631 | || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF); |
| 1632 | OUT("0,\n"); |
| 1633 | } else { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1634 | OUT("offsetof(struct "); |
| 1635 | out_name_chain(arg, 1); |
| 1636 | OUT(", "); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1637 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE |
| 1638 | && (!UNNAMED_UNIONS)) OUT("choice."); |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1639 | OUT("%s),\n", MKID_safe(expr->Identifier)); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1640 | } |
| 1641 | INDENT(+1); |
| 1642 | if(C99_MODE) OUT(".tag = "); |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 1643 | if(outmost_tag) { |
| 1644 | if(outmost_tag->tag_value == -1) |
| 1645 | OUT("-1 /* Ambiguous tag (ANY?) */"); |
| 1646 | else |
| 1647 | _print_tag(arg, outmost_tag); |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1648 | } else { |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 1649 | OUT("-1 /* Ambiguous tag (CHOICE?) */"); |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1650 | } |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 1651 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1652 | OUT(",\n"); |
| 1653 | if(C99_MODE) OUT(".tag_mode = "); |
| 1654 | if(expr->tag.tag_class) { |
| 1655 | if(expr->tag.tag_mode == TM_IMPLICIT) |
| 1656 | OUT("-1,\t/* IMPLICIT tag at current level */\n"); |
| 1657 | else |
| 1658 | OUT("+1,\t/* EXPLICIT tag at current level */\n"); |
| 1659 | } else { |
| 1660 | OUT("0,\n"); |
| 1661 | } |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1662 | |
| 1663 | complex_contents = |
| 1664 | (expr->expr_type & ASN_CONSTR_MASK) |
| 1665 | || expr->expr_type == ASN_BASIC_ENUMERATED |
Lev Walkin | 8ecf9db | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 1666 | || (0 /* -- prohibited by X.693:8.3.4 */ |
| 1667 | && expr->expr_type == ASN_BASIC_INTEGER |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1668 | && expr_elements_count(arg, expr)); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1669 | if(C99_MODE) OUT(".type = "); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1670 | OUT("(void *)&asn_DEF_"); |
| 1671 | if(complex_contents) { |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1672 | OUT("%s", MKID(expr->Identifier)); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1673 | if(!(arg->flags & A1C_ALL_DEFS_GLOBAL)) |
| 1674 | OUT("_%d", expr->_type_unique_index); |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1675 | } else { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1676 | OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE)); |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1677 | } |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1678 | OUT(",\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1679 | if(C99_MODE) OUT(".memb_constraints = "); |
| 1680 | if(expr->constraints) { |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1681 | if(arg->flags & A1C_NO_CONSTRAINTS) { |
| 1682 | OUT("0,\t/* No check because of -fno-constraints */\n"); |
| 1683 | } else { |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1684 | char *id = MKID(expr->Identifier); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1685 | if(expr->_anonymous_type |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 1686 | && !strcmp(expr->Identifier, "Member")) |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1687 | id = asn1c_type_name(arg, expr, TNF_SAFE); |
| 1688 | OUT("memb_%s_%d_constraint,\n", id, |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1689 | arg->expr->_type_unique_index); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1690 | } |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1691 | } else { |
Lev Walkin | b7bfd1a | 2005-01-17 12:16:58 +0000 | [diff] [blame] | 1692 | OUT("0,\t/* Defer constraints checking to the member type */\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1693 | } |
| 1694 | if(C99_MODE) OUT(".name = "); |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 1695 | if(1) { |
| 1696 | if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member")) |
| 1697 | OUT("\"\"\n"); |
| 1698 | else |
| 1699 | OUT("\"%s\"\n", expr->Identifier); |
| 1700 | } else { |
| 1701 | OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier); |
| 1702 | } |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1703 | OUT("},\n"); |
| 1704 | INDENT(-1); |
| 1705 | |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1706 | if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS)) |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1707 | return 0; |
| 1708 | |
| 1709 | save_target = arg->target->target; |
| 1710 | REDIR(OT_CODE); |
| 1711 | |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 1712 | if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member")) |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1713 | p = asn1c_type_name(arg, expr, TNF_SAFE); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1714 | else |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1715 | p = MKID(expr->Identifier); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1716 | OUT("static int\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1717 | OUT("memb_%s_%d_constraint(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] | 1718 | INDENT(+1); |
| 1719 | OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n"); |
| 1720 | tmp_arg = *arg; |
| 1721 | tmp_arg.expr = expr; |
| 1722 | if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) { |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1723 | OUT("return td->check_constraints" |
| 1724 | "(td, sptr, app_errlog, app_key);\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1725 | } |
| 1726 | INDENT(-1); |
| 1727 | OUT("}\n"); |
| 1728 | OUT("\n"); |
| 1729 | |
| 1730 | REDIR(save_target); |
| 1731 | |
| 1732 | return 0; |
| 1733 | } |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1734 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1735 | /* |
| 1736 | * Generate "asn_DEF_XXX" type definition. |
| 1737 | */ |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1738 | static int |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1739 | 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 | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1740 | int using_type_name = 0; |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1741 | char *p; |
| 1742 | |
Lev Walkin | dd32b59 | 2004-09-06 08:07:29 +0000 | [diff] [blame] | 1743 | if(HIDE_INNER_DEFS) |
| 1744 | OUT("static /* Use -fall-defs-global to expose */\n"); |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1745 | OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID(expr->Identifier)); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1746 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1747 | OUT(" = {\n"); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 1748 | p = MKID(expr->Identifier); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1749 | INDENT(+1); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1750 | OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier); |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1751 | OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1752 | |
| 1753 | if(expr->expr_type & ASN_CONSTR_MASK) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1754 | using_type_name = 1; |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1755 | p = asn1c_type_name(arg, arg->expr, TNF_SAFE); |
| 1756 | } |
| 1757 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1758 | #define FUNCREF(foo) do { \ |
| 1759 | OUT("%s", p); \ |
| 1760 | if(HIDE_INNER_DEFS && !using_type_name) \ |
| 1761 | OUT("_%d", expr->_type_unique_index); \ |
| 1762 | OUT("_" #foo ",\n"); \ |
| 1763 | } while(0) |
| 1764 | |
| 1765 | FUNCREF(free); |
| 1766 | FUNCREF(print); |
| 1767 | FUNCREF(constraint); |
| 1768 | FUNCREF(decode_ber); |
| 1769 | FUNCREF(encode_der); |
| 1770 | FUNCREF(decode_xer); |
| 1771 | FUNCREF(encode_xer); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1772 | |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1773 | if(expr->expr_type == ASN_CONSTR_CHOICE) { |
| 1774 | OUT("CHOICE_outmost_tag,\n"); |
| 1775 | } else { |
| 1776 | OUT("0,\t/* Use generic outmost tag fetcher */\n"); |
| 1777 | } |
| 1778 | |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1779 | p = MKID(expr->Identifier); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1780 | if(tags_count) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1781 | OUT("asn_DEF_%s_%d_tags,\n", |
| 1782 | p, expr->_type_unique_index); |
| 1783 | OUT("sizeof(asn_DEF_%s_%d_tags)\n", |
| 1784 | p, expr->_type_unique_index); |
| 1785 | OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])", |
| 1786 | p, expr->_type_unique_index); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1787 | if(tv_mode == _TVM_SUBSET |
| 1788 | && tags_count != all_tags_count) |
| 1789 | OUT(" - %d", all_tags_count - tags_count); |
| 1790 | OUT(", /* %d */\n", tags_count); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1791 | } else { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1792 | OUT("0,\t/* No effective tags (pointer) */\n"); |
| 1793 | OUT("0,\t/* No effective tags (count) */\n"); |
| 1794 | } |
| 1795 | |
| 1796 | if(all_tags_count && tv_mode == _TVM_DIFFERENT) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1797 | OUT("asn_DEF_%s_%d_all_tags,\n", |
| 1798 | p, expr->_type_unique_index); |
| 1799 | OUT("sizeof(asn_DEF_%s_%d_all_tags)\n", |
| 1800 | p, expr->_type_unique_index); |
| 1801 | OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n", |
| 1802 | p, expr->_type_unique_index, all_tags_count); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1803 | } else if(all_tags_count) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1804 | OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n", |
| 1805 | p, expr->_type_unique_index); |
| 1806 | OUT("sizeof(asn_DEF_%s_%d_tags)\n", |
| 1807 | p, expr->_type_unique_index); |
| 1808 | OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n", |
| 1809 | p, expr->_type_unique_index, all_tags_count); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1810 | } else { |
| 1811 | OUT("0,\t/* No tags (pointer) */\n"); |
| 1812 | OUT("0,\t/* No tags (count) */\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1815 | if(elements_count) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1816 | OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1817 | if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF |
| 1818 | || expr->expr_type == ASN_CONSTR_SET_OF) { |
| 1819 | OUT("%d,\t/* Single element */\n", |
| 1820 | elements_count); |
| 1821 | assert(elements_count == 1); |
| 1822 | } else { |
| 1823 | OUT("%d,\t/* Elements count */\n", |
| 1824 | elements_count); |
| 1825 | } |
| 1826 | } else { |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1827 | if(expr_elements_count(arg, expr)) |
| 1828 | OUT("0, 0,\t/* Defined elsewhere */\n"); |
| 1829 | else |
| 1830 | OUT("0, 0,\t/* No members */\n"); |
| 1831 | } |
| 1832 | |
| 1833 | switch(spec) { |
| 1834 | case ETD_NO_SPECIFICS: |
| 1835 | OUT("0\t/* No specifics */\n"); |
| 1836 | break; |
| 1837 | case ETD_HAS_SPECIFICS: |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1838 | OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n", |
| 1839 | p, expr->_type_unique_index); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1840 | } |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1841 | INDENT(-1); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1842 | OUT("};\n"); |
| 1843 | OUT("\n"); |
Lev Walkin | 9729878 | 2004-08-26 06:20:34 +0000 | [diff] [blame] | 1844 | |
| 1845 | return 0; |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1846 | } |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1847 | |
| 1848 | /* |
| 1849 | * Check if it is better to make this type indirectly accessed via |
| 1850 | * a pointer. |
| 1851 | * This may be the case for the following recursive definition: |
| 1852 | * Type ::= CHOICE { member Type }; |
| 1853 | */ |
| 1854 | static int |
| 1855 | expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) { |
| 1856 | asn1p_expr_t *top_parent; |
| 1857 | asn1p_expr_t *terminal; |
| 1858 | |
| 1859 | if(expr->expr_type != A1TC_REFERENCE) |
| 1860 | return 0; |
| 1861 | |
| 1862 | /* Rewind to the topmost parent expression */ |
| 1863 | if((top_parent = expr->parent_expr)) { |
| 1864 | while(top_parent->parent_expr) |
| 1865 | top_parent = top_parent->parent_expr; |
| 1866 | } else { |
| 1867 | return 0; |
| 1868 | } |
| 1869 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1870 | terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1871 | |
| 1872 | return (terminal == top_parent); |
| 1873 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1874 | |
| 1875 | static int |
| 1876 | expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) { |
| 1877 | expr = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 1878 | if(!expr) return 0; |
| 1879 | |
| 1880 | /* X.680, 25.5, Table 5 */ |
| 1881 | switch(expr->expr_type) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1882 | case ASN_BASIC_BOOLEAN: |
| 1883 | case ASN_BASIC_ENUMERATED: |
| 1884 | case ASN_BASIC_NULL: |
| 1885 | return 1; |
| 1886 | default: |
| 1887 | return 0; |
| 1888 | } |
| 1889 | } |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 1890 | |
| 1891 | static int |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1892 | out_name_chain(arg_t *arg, int check_reserved_keywords) { |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 1893 | asn1p_expr_t *expr = arg->expr; |
| 1894 | char *id; |
| 1895 | |
| 1896 | assert(expr->Identifier); |
| 1897 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1898 | if(arg->flags & A1C_COMPOUND_NAMES |
| 1899 | && ((expr->expr_type & ASN_CONSTR_MASK) |
| 1900 | || expr->expr_type == ASN_BASIC_ENUMERATED |
| 1901 | || (expr->expr_type == ASN_BASIC_INTEGER |
| 1902 | && expr_elements_count(arg, expr)) |
| 1903 | ) |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 1904 | && expr->parent_expr |
| 1905 | && expr->parent_expr->Identifier) { |
| 1906 | arg_t tmparg = *arg; |
| 1907 | |
| 1908 | tmparg.expr = expr->parent_expr; |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1909 | if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES; |
| 1910 | |
| 1911 | out_name_chain(&tmparg, 0); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 1912 | |
Lev Walkin | 0163309 | 2005-03-05 00:08:41 +0000 | [diff] [blame] | 1913 | OUT("__"); /* a separator between id components */ |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1914 | |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 1915 | /* Fall through */ |
| 1916 | } |
| 1917 | |
| 1918 | if(check_reserved_keywords) |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1919 | id = MKID_safe(expr->Identifier); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 1920 | else |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1921 | id = MKID(expr->Identifier); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 1922 | OUT("%s", id); |
| 1923 | |
| 1924 | return 0; |
| 1925 | } |