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