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