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 | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 10 | #include <asn1fix_crange.h> /* constraint groker from libasn1fix */ |
| 11 | #include <asn1fix_export.h> /* other exportables from libasn1fix */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 12 | |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 13 | typedef struct tag2el_s { |
| 14 | struct asn1p_type_tag_s el_tag; |
| 15 | int el_no; |
vlm | c8aeab4 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 16 | int toff_first; |
| 17 | int toff_last; |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 18 | asn1p_expr_t *from_expr; |
| 19 | } tag2el_t; |
| 20 | |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 21 | typedef enum fte { |
| 22 | FTE_ALLTAGS, |
| 23 | FTE_CANONICAL_XER, |
| 24 | } fte_e; |
| 25 | static int _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags); |
| 26 | 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] | 27 | |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 28 | enum onc_flags { |
| 29 | ONC_noflags = 0x00, |
| 30 | ONC_avoid_keywords = 0x01, |
| 31 | ONC_force_compound_name = 0x02, |
| 32 | }; |
| 33 | static int out_name_chain(arg_t *arg, enum onc_flags); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg); |
| 35 | static int asn1c_lang_C_type_SET_def(arg_t *arg); |
| 36 | static int asn1c_lang_C_type_CHOICE_def(arg_t *arg); |
| 37 | 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] | 38 | static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 39 | static int compute_extensions_start(asn1p_expr_t *expr); |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 40 | static int expr_break_recursion(arg_t *arg, asn1p_expr_t *expr); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 41 | static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 42 | static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 43 | static int emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, char *type); |
| 44 | static int emit_single_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr); |
| 45 | static int emit_members_PER_constraints(arg_t *arg); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 46 | static int emit_member_table(arg_t *arg, asn1p_expr_t *expr); |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 47 | 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] | 48 | static int emit_include_dependencies(arg_t *arg); |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 49 | static asn1p_expr_t *terminal_structable(arg_t *arg, asn1p_expr_t *expr); |
| 50 | static int expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr); |
| 51 | static int asn1c_recurse(arg_t *arg, asn1p_expr_t *expr, int (*callback)(arg_t *arg, void *key), void *key); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 52 | static asn1p_expr_type_e expr_get_type(arg_t *arg, asn1p_expr_t *expr); |
| 53 | static int try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out); |
| 54 | static int *compute_canonical_members_order(arg_t *arg, int el_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 56 | enum tvm_compat { |
| 57 | _TVM_SAME = 0, /* tags and all_tags are same */ |
| 58 | _TVM_SUBSET = 1, /* tags are subset of all_tags */ |
| 59 | _TVM_DIFFERENT = 2, /* tags and all_tags are different */ |
| 60 | }; |
| 61 | static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc); |
| 62 | |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 63 | enum etd_spec { |
| 64 | ETD_NO_SPECIFICS, |
| 65 | ETD_HAS_SPECIFICS |
| 66 | }; |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 67 | 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] | 68 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 69 | #define C99_MODE (!(arg->flags & A1C_NO_C99)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | #define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS) |
vlm | 12c8f69 | 2004-09-06 08:07:29 +0000 | [diff] [blame] | 71 | #define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | |
| 73 | #define PCTX_DEF INDENTED( \ |
| 74 | OUT("\n"); \ |
| 75 | OUT("/* Context for parsing across buffer boundaries */\n"); \ |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 76 | OUT("asn_struct_ctx_t _asn_ctx;\n")); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 77 | |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 78 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 79 | #define DEPENDENCIES do { \ |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 80 | emit_include_dependencies(arg); \ |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 81 | if(expr->expr_type == ASN_CONSTR_SET_OF) \ |
| 82 | GEN_INCLUDE("asn_SET_OF"); \ |
| 83 | if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \ |
| 84 | GEN_INCLUDE("asn_SEQUENCE_OF"); \ |
| 85 | } while(0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 87 | /* MKID_safe() without checking for reserved keywords */ |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 88 | #define MKID(id) ((id)?asn1c_make_identifier(0, (id), 0):"Member") |
| 89 | #define MKID_safe(id) ((id)?asn1c_make_identifier(AMI_CHECK_RESERVED, \ |
| 90 | (id), 0):"Member") |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 91 | |
| 92 | int |
vlm | f9d178d | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 93 | asn1c_lang_C_type_REAL(arg_t *arg) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 95 | } |
| 96 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 97 | struct value2enum { |
| 98 | asn1c_integer_t value; |
| 99 | const char *name; |
| 100 | int idx; |
| 101 | }; |
| 102 | static int compar_enumMap_byName(const void *ap, const void *bp) { |
| 103 | const struct value2enum *a = (const struct value2enum *)ap; |
| 104 | const struct value2enum *b = (const struct value2enum *)bp; |
| 105 | return strcmp(a->name, b->name); |
| 106 | } |
| 107 | static int compar_enumMap_byValue(const void *ap, const void *bp) { |
| 108 | const struct value2enum *a = (const struct value2enum *)ap; |
| 109 | const struct value2enum *b = (const struct value2enum *)bp; |
| 110 | if(a->value < b->value) |
| 111 | return -1; |
| 112 | else if(a->value == b->value) |
| 113 | return 0; |
| 114 | return 1; |
| 115 | } |
| 116 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | int |
vlm | f9d178d | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 118 | asn1c_lang_C_type_common_INTEGER(arg_t *arg) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | asn1p_expr_t *expr = arg->expr; |
| 120 | asn1p_expr_t *v; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 121 | int el_count = expr_elements_count(arg, expr); |
| 122 | struct value2enum *v2e; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 123 | int map_extensions = (expr->expr_type == ASN_BASIC_INTEGER); |
vlm | 4573af0 | 2005-08-13 23:30:24 +0000 | [diff] [blame] | 124 | int eidx; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 125 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 126 | v2e = alloca((el_count + 1) * sizeof(*v2e)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 128 | /* |
vlm | 4573af0 | 2005-08-13 23:30:24 +0000 | [diff] [blame] | 129 | * For all ENUMERATED types and for those INTEGER types which |
| 130 | * have identifiers, print out an enumeration table. |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 131 | */ |
vlm | 4573af0 | 2005-08-13 23:30:24 +0000 | [diff] [blame] | 132 | if(expr->expr_type == ASN_BASIC_ENUMERATED || el_count) { |
| 133 | eidx = 0; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 134 | REDIR(OT_DEPS); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 135 | OUT("typedef enum "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 136 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 137 | OUT(" {\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 138 | TQ_FOR(v, &(expr->members), next) { |
| 139 | switch(v->expr_type) { |
| 140 | case A1TC_UNIVERVAL: |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 141 | OUT("\t"); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 142 | out_name_chain(arg, ONC_noflags); |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 143 | OUT("_%s", MKID(v->Identifier)); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 144 | OUT("\t= %" PRIdASN "%s\n", |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 145 | v->value->value.v_integer, |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 146 | (eidx+1 < el_count) ? "," : ""); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 147 | v2e[eidx].name = v->Identifier; |
| 148 | v2e[eidx].value = v->value->value.v_integer; |
| 149 | eidx++; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 150 | break; |
vlm | f9d178d | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 151 | case A1TC_EXTENSIBLE: |
| 152 | OUT("\t/*\n"); |
| 153 | OUT("\t * Enumeration is extensible\n"); |
| 154 | OUT("\t */\n"); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 155 | if(!map_extensions) |
| 156 | map_extensions = eidx + 1; |
vlm | f9d178d | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 157 | break; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 158 | default: |
| 159 | return -1; |
| 160 | } |
| 161 | } |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 162 | OUT("} "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 163 | out_name_chain(arg, ONC_noflags); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 164 | OUT("_e;\n"); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 165 | assert(eidx == el_count); |
vlm | 4573af0 | 2005-08-13 23:30:24 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * For all ENUMERATED types print out a mapping table |
| 170 | * between identifiers and associated values. |
| 171 | * This is prohibited for INTEGER types by by X.693:8.3.4. |
| 172 | */ |
| 173 | if(expr->expr_type == ASN_BASIC_ENUMERATED) { |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * Generate a enumerationName<->value map for XER codec. |
| 177 | */ |
| 178 | REDIR(OT_STAT_DEFS); |
| 179 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 180 | OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_value2enum_%d[] = {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 181 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 182 | qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue); |
| 183 | for(eidx = 0; eidx < el_count; eidx++) { |
| 184 | v2e[eidx].idx = eidx; |
| 185 | OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n", |
| 186 | v2e[eidx].value, |
| 187 | (long)strlen(v2e[eidx].name), v2e[eidx].name, |
| 188 | (eidx + 1 < el_count) ? "," : ""); |
| 189 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 190 | if(map_extensions) |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 191 | OUT("\t/* This list is extensible */\n"); |
| 192 | OUT("};\n"); |
| 193 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 194 | OUT("static unsigned int asn_MAP_%s_enum2value_%d[] = {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 195 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 196 | qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName); |
| 197 | for(eidx = 0; eidx < el_count; eidx++) { |
| 198 | OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n", |
| 199 | v2e[eidx].idx, |
| 200 | (eidx + 1 < el_count) ? "," : "", |
| 201 | v2e[eidx].name, v2e[eidx].value); |
| 202 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 203 | if(map_extensions) |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 204 | OUT("\t/* This list is extensible */\n"); |
| 205 | OUT("};\n"); |
| 206 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 207 | OUT("static asn_INTEGER_specifics_t asn_SPC_%s_specs_%d = {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 208 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 209 | INDENT(+1); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 210 | OUT("asn_MAP_%s_value2enum_%d,\t" |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 211 | "/* \"tag\" => N; sorted by tag */\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 212 | MKID(expr->Identifier), |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 213 | expr->_type_unique_index); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 214 | OUT("asn_MAP_%s_enum2value_%d,\t" |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 215 | "/* N => \"tag\"; sorted by N */\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 216 | MKID(expr->Identifier), |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 217 | expr->_type_unique_index); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 218 | OUT("%d,\t/* Number of elements in the maps */\n", |
| 219 | el_count); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 220 | if(map_extensions) { |
| 221 | OUT("%d,\t/* Extensions before this member */\n", |
| 222 | map_extensions); |
| 223 | } else { |
| 224 | OUT("0,\t/* Enumeration is not extensible */\n"); |
| 225 | } |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 226 | if(expr->expr_type == ASN_BASIC_ENUMERATED) |
| 227 | OUT("1\t/* Strict enumeration */\n"); |
| 228 | else |
| 229 | OUT("0\n"); |
| 230 | INDENT(-1); |
| 231 | OUT("};\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 235 | } |
| 236 | |
| 237 | int |
vlm | 96853d8 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 238 | asn1c_lang_C_type_BIT_STRING(arg_t *arg) { |
| 239 | asn1p_expr_t *expr = arg->expr; |
| 240 | asn1p_expr_t *v; |
| 241 | int el_count = expr_elements_count(arg, expr); |
vlm | 96853d8 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 242 | |
| 243 | if(el_count) { |
vlm | 75b3a53 | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 244 | int eidx = 0; |
vlm | 96853d8 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 245 | REDIR(OT_DEPS); |
| 246 | OUT("typedef enum "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 247 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 96853d8 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 248 | OUT(" {\n"); |
| 249 | TQ_FOR(v, &(expr->members), next) { |
vlm | 75b3a53 | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 250 | eidx++; |
| 251 | if(v->expr_type != A1TC_UNIVERVAL) { |
vlm | 96853d8 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 252 | OUT("/* Unexpected BIT STRING element: %s */\n", |
| 253 | v->Identifier); |
vlm | 75b3a53 | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 254 | continue; |
vlm | 96853d8 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 255 | } |
vlm | 75b3a53 | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 256 | OUT("\t"); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 257 | out_name_chain(arg, ONC_noflags); |
vlm | 75b3a53 | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 258 | OUT("_%s", MKID(v->Identifier)); |
| 259 | OUT("\t= %" PRIdASN "%s\n", |
| 260 | v->value->value.v_integer, |
| 261 | (eidx < el_count) ? "," : ""); |
vlm | 96853d8 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 262 | } |
| 263 | OUT("} "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 264 | out_name_chain(arg, ONC_noflags); |
vlm | 96853d8 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 265 | OUT("_e;\n"); |
| 266 | assert(eidx == el_count); |
| 267 | } |
| 268 | |
| 269 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 270 | } |
| 271 | |
| 272 | int |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 273 | asn1c_lang_C_type_SEQUENCE(arg_t *arg) { |
| 274 | asn1p_expr_t *expr = arg->expr; |
| 275 | asn1p_expr_t *v; |
| 276 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
| 277 | |
| 278 | DEPENDENCIES; |
| 279 | |
| 280 | if(arg->embed) { |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 281 | OUT("struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 282 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 283 | OUT(" {\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 284 | } else { |
| 285 | OUT("typedef struct %s {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 286 | MKID_safe(expr->Identifier)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | TQ_FOR(v, &(expr->members), next) { |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 290 | if(v->expr_type == A1TC_EXTENSIBLE) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 291 | if(comp_mode < 3) comp_mode++; |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 292 | if(comp_mode == 1) |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 293 | v->marker.flags |= EM_OMITABLE | EM_INDIRECT; |
| 294 | try_inline_default(arg, v, 1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 295 | EMBED(v); |
| 296 | } |
| 297 | |
| 298 | PCTX_DEF; |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 299 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 300 | expr->_anonymous_type ? "" : |
| 301 | arg->embed |
| 302 | ? MKID_safe(expr->Identifier) |
| 303 | : MKID(expr->Identifier), |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 304 | arg->embed ? "" : "_t"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 305 | |
| 306 | return asn1c_lang_C_type_SEQUENCE_def(arg); |
| 307 | } |
| 308 | |
| 309 | static int |
| 310 | asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) { |
| 311 | asn1p_expr_t *expr = arg->expr; |
| 312 | asn1p_expr_t *v; |
| 313 | int elements; /* Number of elements */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 314 | int ext_start = -1; |
| 315 | int ext_stop = -1; |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 316 | tag2el_t *tag2el = NULL; |
| 317 | int tag2el_count = 0; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 318 | int tags_count; |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 319 | int all_tags_count; |
| 320 | enum tvm_compat tv_mode; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 321 | int roms_count; /* Root optional members */ |
| 322 | int aoms_count; /* Additions optional members */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 323 | |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 324 | /* |
| 325 | * Fetch every inner tag from the tag to elements map. |
| 326 | */ |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 327 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) { |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 328 | if(tag2el) free(tag2el); |
| 329 | return -1; |
| 330 | } |
| 331 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 332 | GEN_INCLUDE("constr_SEQUENCE"); |
| 333 | if(!arg->embed) |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 334 | GEN_DECLARE(expr); /* asn_DEF_xxx */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 335 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 336 | REDIR(OT_STAT_DEFS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 337 | |
| 338 | /* |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 339 | * Print out the table according to which parsing is performed. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 340 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 341 | if(expr_elements_count(arg, expr)) { |
| 342 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 343 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 344 | if(emit_members_PER_constraints(arg)) |
| 345 | return -1; |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 346 | OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 347 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 348 | |
| 349 | elements = 0; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 350 | roms_count = 0; |
| 351 | aoms_count = 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 352 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
| 353 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 354 | if((++comp_mode) == 1) |
| 355 | ext_start = elements - 1; |
| 356 | else |
| 357 | ext_stop = elements - 1; |
| 358 | continue; |
| 359 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 360 | if(v->marker.flags & EM_OMITABLE) |
| 361 | comp_mode == 1 ? ++aoms_count : ++roms_count; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 362 | emit_member_table(arg, v); |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 363 | elements++; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 364 | }); |
| 365 | OUT("};\n"); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 366 | |
| 367 | if((roms_count + aoms_count) && (arg->flags & A1C_GEN_PER)) { |
| 368 | int elm = 0; |
| 369 | int comma = 0; |
| 370 | comp_mode = 0; |
| 371 | OUT("static int asn_MAP_%s_oms_%d[] = {", |
| 372 | MKID(expr->Identifier), |
| 373 | expr->_type_unique_index); |
| 374 | TQ_FOR(v, &(expr->members), next) { |
| 375 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 376 | ++comp_mode; |
| 377 | continue; |
| 378 | } |
| 379 | if((v->marker.flags & EM_OMITABLE) |
| 380 | && comp_mode != 1) { |
| 381 | if(!comma) comma++; |
| 382 | else OUT(","); |
| 383 | OUT(" %d", elm); |
| 384 | } |
| 385 | ++elm; |
| 386 | } |
| 387 | elm = 0; |
| 388 | comp_mode = 0; |
| 389 | TQ_FOR(v, &(expr->members), next) { |
| 390 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 391 | ++comp_mode; |
| 392 | continue; |
| 393 | } |
| 394 | if((v->marker.flags & EM_OMITABLE) |
| 395 | && comp_mode == 1) { |
| 396 | if(!comma) comma++; |
| 397 | else OUT(","); |
| 398 | OUT(" %d", elm); |
| 399 | } |
| 400 | ++elm; |
| 401 | } |
| 402 | OUT(" };\n"); |
| 403 | if(roms_count > 65536) |
| 404 | FATAL("Too many optional elements in %s " |
| 405 | "at line %d!", |
| 406 | arg->expr->Identifier, |
| 407 | arg->expr->_lineno); |
| 408 | } else { |
| 409 | roms_count = 0; |
| 410 | aoms_count = 0; |
| 411 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 412 | } else { |
| 413 | elements = 0; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 414 | roms_count = 0; |
| 415 | aoms_count = 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 416 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 417 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 418 | /* |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 419 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 420 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 421 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 422 | |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 423 | /* |
| 424 | * Tags to elements map. |
| 425 | */ |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 426 | emit_tag2member_map(arg, tag2el, tag2el_count, 0); |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 427 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 428 | OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_specs_%d = {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 429 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 430 | INDENT(+1); |
| 431 | OUT("sizeof(struct "); |
| 432 | out_name_chain(arg, ONC_avoid_keywords); OUT("),\n"); |
| 433 | OUT("offsetof(struct "); |
| 434 | out_name_chain(arg, ONC_avoid_keywords); OUT(", _asn_ctx),\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 435 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 436 | if(tag2el_count) { |
| 437 | OUT("asn_MAP_%s_tag2el_%d,\n", |
| 438 | MKID(expr->Identifier), |
| 439 | expr->_type_unique_index); |
| 440 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
| 441 | } else { |
| 442 | OUT("0,\t/* No top level tags */\n"); |
| 443 | OUT("0,\t/* No tags in the map */\n"); |
| 444 | } |
| 445 | if(roms_count + aoms_count) { |
| 446 | OUT("asn_MAP_%s_oms_%d,\t/* Optional members */\n", |
| 447 | MKID(expr->Identifier), expr->_type_unique_index); |
| 448 | OUT("%d, %d,\t/* Root/Additions */\n", roms_count, aoms_count); |
| 449 | } else { |
| 450 | OUT("0, 0, 0,\t/* Optional elements (not needed) */\n"); |
| 451 | } |
| 452 | OUT("%d,\t/* Start extensions */\n", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 453 | ext_start); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 454 | OUT("%d\t/* Stop extensions */\n", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 455 | (ext_stop<ext_start)?elements+1:ext_stop, ext_stop); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 456 | INDENT(-1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 457 | OUT("};\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 458 | |
| 459 | /* |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 460 | * Emit asn_DEF_xxx table. |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 461 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 462 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements, |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 463 | ETD_HAS_SPECIFICS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 464 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 465 | REDIR(OT_TYPE_DECLS); |
| 466 | |
| 467 | return 0; |
vlm | edf203f | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 468 | } /* _SEQUENCE_def() */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 469 | |
| 470 | int |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 471 | asn1c_lang_C_type_SET(arg_t *arg) { |
| 472 | asn1p_expr_t *expr = arg->expr; |
| 473 | asn1p_expr_t *v; |
| 474 | long mcount; |
| 475 | char *id; |
| 476 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
| 477 | |
| 478 | DEPENDENCIES; |
| 479 | |
| 480 | REDIR(OT_DEPS); |
| 481 | |
| 482 | OUT("\n"); |
| 483 | OUT("/*\n"); |
| 484 | OUT(" * Method of determining the components presence\n"); |
| 485 | OUT(" */\n"); |
| 486 | mcount = 0; |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 487 | OUT("typedef enum "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 488 | out_name_chain(arg, ONC_noflags); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 489 | OUT("_PR {\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 490 | TQ_FOR(v, &(expr->members), next) { |
| 491 | if(v->expr_type == A1TC_EXTENSIBLE) continue; |
| 492 | INDENTED( |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 493 | out_name_chain(arg, ONC_noflags); |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 494 | OUT("_PR_"); |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 495 | id = MKID(v->Identifier); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 496 | OUT("%s,\t/* Member %s is present */\n", |
| 497 | id, id) |
| 498 | ); |
| 499 | mcount++; |
| 500 | } |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 501 | OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 502 | |
| 503 | REDIR(OT_TYPE_DECLS); |
| 504 | |
| 505 | if(arg->embed) { |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 506 | OUT("struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 507 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 508 | OUT(" {\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 509 | } else { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 510 | OUT("typedef struct %s {\n", |
| 511 | MKID_safe(expr->Identifier)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | TQ_FOR(v, &(expr->members), next) { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 515 | if(v->expr_type == A1TC_EXTENSIBLE) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 516 | if(comp_mode < 3) comp_mode++; |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 517 | if(comp_mode == 1) |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 518 | v->marker.flags |= EM_OMITABLE | EM_INDIRECT; |
| 519 | try_inline_default(arg, v, 1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 520 | EMBED(v); |
| 521 | } |
| 522 | |
| 523 | INDENTED( |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 524 | id = MKID(expr->Identifier); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 525 | OUT("\n"); |
| 526 | OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n", |
| 527 | id, id); |
| 528 | OUT("unsigned int _presence_map\n"); |
| 529 | OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount); |
| 530 | ); |
| 531 | |
| 532 | PCTX_DEF; |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 533 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 534 | expr->_anonymous_type ? "" : MKID_safe(expr->Identifier), |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 535 | arg->embed ? "" : "_t"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 536 | |
| 537 | return asn1c_lang_C_type_SET_def(arg); |
| 538 | } |
| 539 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 540 | static int |
| 541 | asn1c_lang_C_type_SET_def(arg_t *arg) { |
| 542 | asn1p_expr_t *expr = arg->expr; |
| 543 | asn1p_expr_t *v; |
| 544 | int elements; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 545 | tag2el_t *tag2el = NULL; |
| 546 | int tag2el_count = 0; |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 547 | tag2el_t *tag2el_cxer = NULL; |
| 548 | int tag2el_cxer_count = 0; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 549 | int tags_count; |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 550 | int all_tags_count; |
| 551 | enum tvm_compat tv_mode; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 552 | char *p; |
| 553 | |
| 554 | /* |
| 555 | * Fetch every inner tag from the tag to elements map. |
| 556 | */ |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 557 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 558 | if(tag2el) free(tag2el); |
| 559 | return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 560 | } |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 561 | if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) { |
| 562 | if(tag2el) free(tag2el); |
| 563 | if(tag2el_cxer) free(tag2el_cxer); |
| 564 | return -1; |
| 565 | } |
| 566 | if(tag2el_cxer_count == tag2el_count |
| 567 | && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) { |
| 568 | free(tag2el_cxer); |
| 569 | tag2el_cxer = 0; |
| 570 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 571 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 572 | GEN_INCLUDE("constr_SET"); |
| 573 | if(!arg->embed) |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 574 | GEN_DECLARE(expr); /* asn_DEF_xxx */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 575 | |
| 576 | REDIR(OT_STAT_DEFS); |
| 577 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 578 | /* |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 579 | * Print out the table according to which parsing is performed. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 580 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 581 | if(expr_elements_count(arg, expr)) { |
| 582 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 583 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 584 | if(emit_members_PER_constraints(arg)) |
| 585 | return -1; |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 586 | OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 587 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 588 | |
| 589 | elements = 0; |
| 590 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
| 591 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 592 | if(comp_mode < 3) comp_mode++; |
| 593 | } else { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 594 | emit_member_table(arg, v); |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 595 | elements++; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 596 | } |
| 597 | }); |
| 598 | OUT("};\n"); |
| 599 | } else { |
| 600 | elements = 0; |
| 601 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 602 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 603 | /* |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 604 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 605 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 606 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 607 | |
| 608 | /* |
| 609 | * Tags to elements map. |
| 610 | */ |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 611 | emit_tag2member_map(arg, tag2el, tag2el_count, 0); |
| 612 | if(tag2el_cxer) |
| 613 | emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * Emit a map of mandatory elements. |
| 617 | */ |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 618 | OUT("static uint8_t asn_MAP_%s_mmap_%d", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 619 | MKID(expr->Identifier), expr->_type_unique_index); |
| 620 | p = MKID_safe(expr->Identifier); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 621 | OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 622 | OUT(" = {\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 623 | INDENTED( |
| 624 | if(elements) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 625 | int el = 0; |
| 626 | TQ_FOR(v, &(expr->members), next) { |
| 627 | if(v->expr_type == A1TC_EXTENSIBLE) continue; |
vlm | e287319 | 2005-10-05 08:59:03 +0000 | [diff] [blame] | 628 | if(el) { |
| 629 | if((el % 8) == 0) |
| 630 | OUT(",\n"); |
| 631 | else |
| 632 | OUT(" | "); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 633 | } |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 634 | OUT("(%d << %d)", |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 635 | (v->marker.flags & EM_OMITABLE) != EM_OMITABLE, |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 636 | 7 - (el % 8)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 637 | el++; |
| 638 | } |
| 639 | } else { |
| 640 | OUT("0"); |
| 641 | } |
| 642 | ); |
| 643 | OUT("\n"); |
| 644 | OUT("};\n"); |
| 645 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 646 | OUT("static asn_SET_specifics_t asn_SPC_%s_specs_%d = {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 647 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 648 | INDENTED( |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 649 | OUT("sizeof(struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 650 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 651 | OUT("),\n"); |
| 652 | OUT("offsetof(struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 653 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 654 | OUT(", _asn_ctx),\n"); |
| 655 | OUT("offsetof(struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 656 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 657 | OUT(", _presence_map),\n"); |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 658 | p = MKID(expr->Identifier); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 659 | OUT("asn_MAP_%s_tag2el_%d,\n", p, expr->_type_unique_index); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 660 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 661 | if(tag2el_cxer) |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 662 | OUT("asn_MAP_%s_tag2el_cxer_%d,\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 663 | p, expr->_type_unique_index); |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 664 | else |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 665 | OUT("asn_MAP_%s_tag2el_%d,\t/* Same as above */\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 666 | p, expr->_type_unique_index); |
| 667 | OUT("%d,\t/* Count of tags in the CXER map */\n", |
| 668 | tag2el_cxer_count); |
vlm | 79b08d5 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 669 | OUT("%d,\t/* Whether extensible */\n", |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 670 | compute_extensions_start(expr) == -1 ? 0 : 1); |
| 671 | OUT("(unsigned int *)asn_MAP_%s_mmap_%d\t/* Mandatory elements map */\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 672 | p, expr->_type_unique_index); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 673 | ); |
| 674 | OUT("};\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 675 | |
| 676 | /* |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 677 | * Emit asn_DEF_xxx table. |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 678 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 679 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements, |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 680 | ETD_HAS_SPECIFICS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 681 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 682 | REDIR(OT_TYPE_DECLS); |
| 683 | |
| 684 | return 0; |
vlm | edf203f | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 685 | } /* _SET_def() */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 686 | |
| 687 | int |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 688 | asn1c_lang_C_type_SEx_OF(arg_t *arg) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 689 | asn1p_expr_t *expr = arg->expr; |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 690 | asn1p_expr_t *memb = TQ_FIRST(&expr->members); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 691 | |
| 692 | DEPENDENCIES; |
| 693 | |
| 694 | if(arg->embed) { |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 695 | OUT("struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 696 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 697 | OUT(" {\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 698 | } else { |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 699 | OUT("typedef struct %s {\n", MKID_safe(expr->Identifier)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 700 | } |
| 701 | |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 702 | INDENT(+1); |
| 703 | OUT("A_%s_OF(", |
| 704 | (arg->expr->expr_type == ASN_CONSTR_SET_OF) |
| 705 | ? "SET" : "SEQUENCE"); |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 706 | |
| 707 | /* |
| 708 | * README README |
| 709 | * The implementation of the A_SET_OF() macro is already indirect. |
| 710 | */ |
| 711 | memb->marker.flags |= EM_INDIRECT; |
| 712 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 713 | if(memb->expr_type & ASN_CONSTR_MASK |
| 714 | || ((memb->expr_type == ASN_BASIC_ENUMERATED |
vlm | c89422d | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 715 | || (0 /* -- prohibited by X.693:8.3.4 */ |
| 716 | && memb->expr_type == ASN_BASIC_INTEGER)) |
| 717 | && expr_elements_count(arg, memb))) { |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 718 | arg_t tmp; |
| 719 | asn1p_expr_t tmp_memb; |
| 720 | arg->embed++; |
| 721 | tmp = *arg; |
| 722 | tmp.expr = &tmp_memb; |
| 723 | tmp_memb = *memb; |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 724 | tmp_memb.marker.flags &= ~EM_INDIRECT; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 725 | tmp_memb._anonymous_type = 1; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 726 | if(tmp_memb.Identifier == 0) { |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 727 | tmp_memb.Identifier = "Member"; |
| 728 | if(0) |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 729 | tmp_memb.Identifier = strdup( |
| 730 | asn1c_make_identifier(0, |
vlm | bf3c511 | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 731 | expr->Identifier, "Member", 0)); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 732 | assert(tmp_memb.Identifier); |
| 733 | } |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 734 | tmp.default_cb(&tmp); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 735 | if(tmp_memb.Identifier != memb->Identifier) |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 736 | if(0) free(tmp_memb.Identifier); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 737 | arg->embed--; |
| 738 | assert(arg->target->target == OT_TYPE_DECLS); |
| 739 | } else { |
vlm | 6c5d5e5 | 2005-03-04 22:38:22 +0000 | [diff] [blame] | 740 | OUT("%s", asn1c_type_name(arg, memb, |
| 741 | (memb->marker.flags & EM_UNRECURSE) |
| 742 | ? TNF_RSAFE : TNF_CTYPE)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 743 | } |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 744 | /* README README (above) */ |
| 745 | if(0 && (memb->marker.flags & EM_INDIRECT)) |
| 746 | OUT(" *"); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 747 | OUT(") list;\n"); |
| 748 | INDENT(-1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 749 | |
| 750 | PCTX_DEF; |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 751 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 752 | expr->_anonymous_type ? "" : MKID_safe(expr->Identifier), |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 753 | arg->embed ? "" : "_t"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 754 | |
| 755 | /* |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 756 | * SET OF/SEQUENCE OF definition |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 757 | */ |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 758 | return asn1c_lang_C_type_SEx_OF_def(arg, |
| 759 | (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static int |
| 763 | asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) { |
| 764 | asn1p_expr_t *expr = arg->expr; |
| 765 | asn1p_expr_t *v; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 766 | int tags_count; |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 767 | int all_tags_count; |
| 768 | enum tvm_compat tv_mode; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 769 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 770 | /* |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 771 | * Print out the table according to which parsing is performed. |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 772 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 773 | if(seq_of) { |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 774 | GEN_INCLUDE("constr_SEQUENCE_OF"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 775 | } else { |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 776 | GEN_INCLUDE("constr_SET_OF"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 777 | } |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 778 | if(!arg->embed) |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 779 | GEN_DECLARE(expr); /* asn_DEF_xxx */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 780 | |
| 781 | REDIR(OT_STAT_DEFS); |
| 782 | |
| 783 | /* |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 784 | * Print out the table according to which parsing is performed. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 785 | */ |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 786 | if(emit_members_PER_constraints(arg)) |
| 787 | return -1; |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 788 | OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
| 789 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 790 | INDENT(+1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 791 | v = TQ_FIRST(&(expr->members)); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 792 | if(!v->Identifier) { |
vlm | bf3c511 | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 793 | v->Identifier = strdup("Member"); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 794 | assert(v->Identifier); |
| 795 | } |
| 796 | v->_anonymous_type = 1; |
| 797 | arg->embed++; |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 798 | emit_member_table(arg, v); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 799 | arg->embed--; |
| 800 | INDENT(-1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 801 | OUT("};\n"); |
| 802 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 803 | /* |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 804 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 805 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 806 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 807 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 808 | OUT("static asn_SET_OF_specifics_t asn_SPC_%s_specs_%d = {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 809 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 810 | INDENTED( |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 811 | OUT("sizeof(struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 812 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 813 | OUT("),\n"); |
| 814 | OUT("offsetof(struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 815 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 816 | OUT(", _asn_ctx),\n"); |
vlm | 6d44a54 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 817 | { |
| 818 | int as_xvl = expr_as_xmlvaluelist(arg, v); |
| 819 | OUT("%d,\t/* XER encoding is %s */\n", |
| 820 | as_xvl, |
| 821 | as_xvl ? "XMLValueList" : "XMLDelimitedItemList"); |
| 822 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 823 | ); |
| 824 | OUT("};\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 825 | |
| 826 | /* |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 827 | * Emit asn_DEF_xxx table. |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 828 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 829 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1, |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 830 | ETD_HAS_SPECIFICS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 831 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 832 | REDIR(OT_TYPE_DECLS); |
| 833 | |
| 834 | return 0; |
vlm | edf203f | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 835 | } /* _SEx_OF_def() */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 836 | |
| 837 | int |
| 838 | asn1c_lang_C_type_CHOICE(arg_t *arg) { |
| 839 | asn1p_expr_t *expr = arg->expr; |
| 840 | asn1p_expr_t *v; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 841 | char *id; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 842 | |
| 843 | DEPENDENCIES; |
| 844 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 845 | REDIR(OT_DEPS); |
| 846 | |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 847 | OUT("typedef enum "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 848 | out_name_chain(arg, ONC_noflags); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 849 | OUT("_PR {\n"); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 850 | INDENTED( |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 851 | out_name_chain(arg, ONC_noflags); |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 852 | OUT("_PR_NOTHING,\t/* No components present */\n"); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 853 | TQ_FOR(v, &(expr->members), next) { |
| 854 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 855 | OUT("/* Extensions may appear below */\n"); |
| 856 | continue; |
| 857 | } |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 858 | out_name_chain(arg, ONC_noflags); |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 859 | OUT("_PR_"); |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 860 | id = MKID(v->Identifier); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 861 | OUT("%s,\n", id, id); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 862 | } |
| 863 | ); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 864 | OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n"); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 865 | |
| 866 | REDIR(OT_TYPE_DECLS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 867 | |
| 868 | if(arg->embed) { |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 869 | OUT("struct "); out_name_chain(arg, ONC_avoid_keywords); OUT(" {\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 870 | } else { |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 871 | OUT("typedef struct %s {\n", MKID_safe(expr->Identifier)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | INDENTED( |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 875 | out_name_chain(arg, ONC_noflags); |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 876 | OUT("_PR present;\n"); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 877 | OUT("union "); |
| 878 | if(UNNAMED_UNIONS == 0) { |
| 879 | out_name_chain(arg, ONC_force_compound_name); |
| 880 | OUT("_u "); |
| 881 | } |
| 882 | OUT("{\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 883 | TQ_FOR(v, &(expr->members), next) { |
| 884 | EMBED(v); |
| 885 | } |
| 886 | if(UNNAMED_UNIONS) OUT("};\n"); |
| 887 | else OUT("} choice;\n"); |
| 888 | ); |
| 889 | |
| 890 | PCTX_DEF; |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 891 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 892 | expr->_anonymous_type ? "" : |
| 893 | arg->embed |
| 894 | ? MKID_safe(expr->Identifier) |
| 895 | : MKID(expr->Identifier), |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 896 | arg->embed ? "" : "_t"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 897 | |
| 898 | return asn1c_lang_C_type_CHOICE_def(arg); |
| 899 | } |
| 900 | |
| 901 | static int |
| 902 | asn1c_lang_C_type_CHOICE_def(arg_t *arg) { |
| 903 | asn1p_expr_t *expr = arg->expr; |
| 904 | asn1p_expr_t *v; |
| 905 | int elements; /* Number of elements */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 906 | tag2el_t *tag2el = NULL; |
| 907 | int tag2el_count = 0; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 908 | int tags_count; |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 909 | int all_tags_count; |
| 910 | enum tvm_compat tv_mode; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 911 | int *cmap = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 912 | |
| 913 | /* |
| 914 | * Fetch every inner tag from the tag to elements map. |
| 915 | */ |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 916 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 917 | if(tag2el) free(tag2el); |
| 918 | return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 919 | } |
| 920 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 921 | GEN_INCLUDE("constr_CHOICE"); |
| 922 | if(!arg->embed) |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 923 | GEN_DECLARE(expr); /* asn_DEF_xxx */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 924 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 925 | REDIR(OT_STAT_DEFS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 926 | |
| 927 | /* |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 928 | * Print out the table according to which parsing is performed. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 929 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 930 | if(expr_elements_count(arg, expr)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 931 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 932 | if(emit_members_PER_constraints(arg)) |
| 933 | return -1; |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 934 | OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
| 935 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 936 | |
| 937 | elements = 0; |
| 938 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
vlm | 34dcd57 | 2005-01-17 11:40:49 +0000 | [diff] [blame] | 939 | if(v->expr_type == A1TC_EXTENSIBLE) |
| 940 | continue; |
vlm | 34dcd57 | 2005-01-17 11:40:49 +0000 | [diff] [blame] | 941 | emit_member_table(arg, v); |
vlm | 5a6fc65 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 942 | elements++; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 943 | }); |
| 944 | OUT("};\n"); |
| 945 | } else { |
| 946 | elements = 0; |
| 947 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 948 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 949 | /* Create a canonical elements map */ |
| 950 | if(elements && (arg->flags & A1C_GEN_PER)) { |
| 951 | int i; |
| 952 | cmap = compute_canonical_members_order(arg, elements); |
| 953 | if(cmap) { |
| 954 | OUT("static int asn_MAP_%s_cmap_%d[] = {", |
| 955 | MKID(expr->Identifier), |
| 956 | expr->_type_unique_index); |
| 957 | for(i = 0; i < elements; i++) { |
| 958 | if(i) OUT(","); |
| 959 | OUT(" %d", cmap[i]); |
| 960 | } |
| 961 | OUT(" };\n"); |
| 962 | free(cmap); |
| 963 | } |
| 964 | } |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 965 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 966 | if(arg->embed) { |
| 967 | /* |
| 968 | * Our parent structure has already taken this into account. |
| 969 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 970 | tv_mode = _TVM_SAME; |
| 971 | tags_count = all_tags_count = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 972 | } else { |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 973 | tv_mode = emit_tags_vectors(arg, expr, |
| 974 | &tags_count, &all_tags_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 975 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 976 | |
| 977 | /* |
| 978 | * Tags to elements map. |
| 979 | */ |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 980 | emit_tag2member_map(arg, tag2el, tag2el_count, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 981 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 982 | OUT("static asn_CHOICE_specifics_t asn_SPC_%s_specs_%d = {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 983 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 984 | INDENTED( |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 985 | OUT("sizeof(struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 986 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 987 | OUT("),\n"); |
| 988 | OUT("offsetof(struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 989 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 990 | OUT(", _asn_ctx),\n"); |
| 991 | OUT("offsetof(struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 992 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 993 | OUT(", present),\n"); |
| 994 | OUT("sizeof(((struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 995 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 996 | OUT(" *)0)->present),\n"); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 997 | OUT("asn_MAP_%s_tag2el_%d,\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 998 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 999 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1000 | if(C99_MODE) OUT(".canonical_order = "); |
| 1001 | if(cmap) OUT("asn_MAP_%s_cmap_%d,\t/* Canonically sorted */\n", |
| 1002 | MKID(expr->Identifier), expr->_type_unique_index); |
| 1003 | else OUT("0,\n"); |
| 1004 | if(C99_MODE) OUT(".ext_start = "); |
| 1005 | OUT("%d\t/* Extensions start */\n", |
| 1006 | compute_extensions_start(expr)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1007 | ); |
| 1008 | OUT("};\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1009 | |
| 1010 | /* |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1011 | * Emit asn_DEF_xxx table. |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1012 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1013 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements, |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1014 | ETD_HAS_SPECIFICS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1015 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1016 | REDIR(OT_TYPE_DECLS); |
| 1017 | |
| 1018 | return 0; |
vlm | edf203f | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 1019 | } /* _CHOICE_def() */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1020 | |
| 1021 | int |
| 1022 | asn1c_lang_C_type_REFERENCE(arg_t *arg) { |
| 1023 | asn1p_ref_t *ref; |
| 1024 | |
| 1025 | ref = arg->expr->reference; |
| 1026 | if(ref->components[ref->comp_count-1].name[0] == '&') { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1027 | asn1p_expr_t *extract; |
| 1028 | arg_t tmp; |
| 1029 | int ret; |
| 1030 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1031 | extract = asn1f_class_access_ex(arg->asn, arg->expr->module, |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1032 | arg->expr, ref); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1033 | if(extract == NULL) |
| 1034 | return -1; |
| 1035 | |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1036 | extract = asn1p_expr_clone(extract, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1037 | if(extract) { |
| 1038 | if(extract->Identifier) |
| 1039 | free(extract->Identifier); |
| 1040 | extract->Identifier = strdup(arg->expr->Identifier); |
| 1041 | if(extract->Identifier == NULL) { |
| 1042 | asn1p_expr_free(extract); |
| 1043 | return -1; |
| 1044 | } |
| 1045 | } else { |
| 1046 | return -1; |
| 1047 | } |
| 1048 | |
| 1049 | tmp = *arg; |
| 1050 | tmp.asn = arg->asn; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1051 | tmp.expr = extract; |
| 1052 | |
| 1053 | ret = arg->default_cb(&tmp); |
| 1054 | |
| 1055 | asn1p_expr_free(extract); |
| 1056 | |
| 1057 | return ret; |
| 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 1062 | } |
| 1063 | |
| 1064 | int |
| 1065 | asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) { |
| 1066 | asn1p_expr_t *expr = arg->expr; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1067 | int tags_count; |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1068 | int all_tags_count; |
| 1069 | enum tvm_compat tv_mode; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1070 | enum etd_spec etd_spec; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1071 | char *p; |
| 1072 | |
| 1073 | if(arg->embed) { |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1074 | enum tnfmt tnfmt = TNF_CTYPE; |
| 1075 | |
| 1076 | /* |
| 1077 | * If this is an optional compound type, |
| 1078 | * refer it using "struct X" convention, |
| 1079 | * as it may recursively include the current structure. |
| 1080 | */ |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1081 | if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) { |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 1082 | if(terminal_structable(arg, expr)) { |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1083 | tnfmt = TNF_RSAFE; |
| 1084 | REDIR(OT_FWD_DECLS); |
vlm | 5feb752 | 2005-03-04 23:50:56 +0000 | [diff] [blame] | 1085 | OUT("%s;\n", |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1086 | asn1c_type_name(arg, arg->expr, tnfmt)); |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1090 | REDIR(OT_TYPE_DECLS); |
| 1091 | |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1092 | OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt)); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1093 | if(!expr->_anonymous_type) { |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1094 | OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t "); |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1095 | OUT("%s", MKID_safe(expr->Identifier)); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1096 | if((expr->marker.flags & (EM_DEFAULT & ~EM_INDIRECT)) |
| 1097 | == (EM_DEFAULT & ~EM_INDIRECT)) |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1098 | OUT("\t/* DEFAULT %s */", |
| 1099 | asn1f_printable_value( |
| 1100 | expr->marker.default_value)); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1101 | else if((expr->marker.flags & EM_OPTIONAL) |
| 1102 | == EM_OPTIONAL) |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1103 | OUT("\t/* OPTIONAL */"); |
| 1104 | } |
| 1105 | |
| 1106 | } else { |
| 1107 | GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE)); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1108 | |
| 1109 | REDIR(OT_TYPE_DECLS); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1110 | |
| 1111 | OUT("typedef %s\t", |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1112 | asn1c_type_name(arg, arg->expr, TNF_CTYPE)); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1113 | OUT("%s%s_t", |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1114 | (expr->marker.flags & EM_INDIRECT)?"*":" ", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1115 | MKID(expr->Identifier)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1118 | if((expr->expr_type == ASN_BASIC_ENUMERATED) |
vlm | c89422d | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 1119 | || (0 /* -- prohibited by X.693:8.3.4 */ |
| 1120 | && expr->expr_type == ASN_BASIC_INTEGER |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1121 | && expr_elements_count(arg, expr))) |
| 1122 | etd_spec = ETD_HAS_SPECIFICS; |
| 1123 | else |
| 1124 | etd_spec = ETD_NO_SPECIFICS; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1125 | |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1126 | /* |
| 1127 | * If this type just blindly refers the other type, alias it. |
| 1128 | * Type1 ::= Type2 |
| 1129 | */ |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1130 | if(arg->embed && etd_spec == ETD_NO_SPECIFICS) { |
| 1131 | REDIR(OT_TYPE_DECLS); |
| 1132 | return 0; |
| 1133 | } |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1134 | if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS)) |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1135 | && (arg->embed || expr->tag.tag_class == TC_NOCLASS) |
vlm | 6f74b0f | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 1136 | && etd_spec == ETD_NO_SPECIFICS |
| 1137 | && 0 /* This shortcut is incompatible with XER */ |
| 1138 | ) { |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1139 | char *type_name; |
| 1140 | REDIR(OT_FUNC_DECLS); |
| 1141 | type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
| 1142 | OUT("/* This type is equivalent to %s */\n", type_name); |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1143 | if(HIDE_INNER_DEFS) OUT("/* "); |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1144 | OUT("#define\tasn_DEF_%s\t", MKID(expr->Identifier)); |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1145 | type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1146 | OUT("asn_DEF_%s", type_name); |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1147 | if(HIDE_INNER_DEFS) |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1148 | OUT("\t// (Use -fall-defs-global to expose) */"); |
| 1149 | OUT("\n"); |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1150 | REDIR(OT_CODE); |
| 1151 | OUT("/* This type is equivalent to %s */\n", type_name); |
| 1152 | OUT("\n"); |
| 1153 | REDIR(OT_TYPE_DECLS); |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1157 | REDIR(OT_STAT_DEFS); |
| 1158 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1159 | /* |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1160 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1161 | */ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1162 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1163 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1164 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, |
| 1165 | 0, etd_spec); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1166 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1167 | REDIR(OT_CODE); |
| 1168 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1169 | /* |
| 1170 | * Constraint checking. |
| 1171 | */ |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1172 | if(!(arg->flags & A1C_NO_CONSTRAINTS)) { |
| 1173 | p = MKID(expr->Identifier); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1174 | if(HIDE_INNER_DEFS) OUT("static "); |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1175 | OUT("int\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1176 | OUT("%s", p); |
| 1177 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1178 | OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n"); |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1179 | INDENT(+1); |
| 1180 | OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {"); |
| 1181 | OUT("\n"); |
| 1182 | if(asn1c_emit_constraint_checking_code(arg) == 1) { |
| 1183 | OUT("/* Replace with underlying type checker */\n"); |
| 1184 | OUT("td->check_constraints " |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1185 | "= asn_DEF_%s.check_constraints;\n", |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1186 | asn1c_type_name(arg, expr, TNF_SAFE)); |
| 1187 | OUT("return td->check_constraints" |
| 1188 | "(td, sptr, app_errlog, app_key);\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 1189 | } |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1190 | INDENT(-1); |
| 1191 | OUT("}\n"); |
| 1192 | OUT("\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1193 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1194 | |
| 1195 | /* |
| 1196 | * Emit suicidal functions. |
| 1197 | */ |
| 1198 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1199 | /* |
| 1200 | * This function replaces certain fields from the definition |
| 1201 | * of a type with the corresponding fields from the basic type |
| 1202 | * (from which the current type is inherited). |
| 1203 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1204 | OUT("/*\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1205 | OUT(" * This type is implemented using %s,\n", |
| 1206 | asn1c_type_name(arg, expr, TNF_SAFE)); |
vlm | dcd32fe | 2004-09-23 22:20:47 +0000 | [diff] [blame] | 1207 | OUT(" * so here we adjust the DEF accordingly.\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1208 | OUT(" */\n"); |
| 1209 | OUT("static void\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1210 | OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n", |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1211 | MKID(expr->Identifier), expr->_type_unique_index); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1212 | INDENT(+1); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1213 | { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1214 | asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1215 | char *type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1216 | OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name); |
| 1217 | OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name); |
| 1218 | OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name); |
| 1219 | OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name); |
| 1220 | OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name); |
| 1221 | OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1222 | OUT("td->uper_decoder = asn_DEF_%s.uper_decoder;\n", type_name); |
vlm | 1308d2b | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1223 | if(!terminal && !tags_count) { |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1224 | OUT("/* The next four lines are here because of -fknown-extern-type */\n"); |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1225 | OUT("td->tags = asn_DEF_%s.tags;\n", type_name); |
| 1226 | OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name); |
| 1227 | OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name); |
| 1228 | 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] | 1229 | OUT("/* End of these lines */\n"); |
| 1230 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1231 | OUT("if(!td->per_constraints)\n"); |
| 1232 | OUT("\ttd->per_constraints = asn_DEF_%s.per_constraints;\n", |
| 1233 | type_name); |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1234 | OUT("td->elements = asn_DEF_%s.elements;\n", type_name); |
| 1235 | OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1236 | if(etd_spec != ETD_NO_SPECIFICS) { |
| 1237 | INDENT(-1); |
vlm | c86870c | 2005-03-29 19:04:24 +0000 | [diff] [blame] | 1238 | OUT(" /* "); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1239 | } |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1240 | OUT("td->specifics = asn_DEF_%s.specifics;", type_name); |
| 1241 | if(etd_spec == ETD_NO_SPECIFICS) { |
| 1242 | INDENT(-1); |
| 1243 | OUT("\n"); |
| 1244 | } else { |
| 1245 | OUT("\t// Defined explicitly */\n"); |
| 1246 | } |
| 1247 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1248 | OUT("}\n"); |
| 1249 | OUT("\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1250 | |
| 1251 | p = MKID(expr->Identifier); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1252 | if(HIDE_INNER_DEFS) OUT("static "); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1253 | OUT("void\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1254 | OUT("%s", p); |
| 1255 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1256 | OUT("_free(asn_TYPE_descriptor_t *td,\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1257 | INDENTED( |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1258 | OUT("\tvoid *struct_ptr, int contents_only) {\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1259 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1260 | p, expr->_type_unique_index); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1261 | OUT("td->free_struct(td, struct_ptr, contents_only);\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1262 | ); |
| 1263 | OUT("}\n"); |
| 1264 | OUT("\n"); |
| 1265 | |
| 1266 | p = MKID(expr->Identifier); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1267 | if(HIDE_INNER_DEFS) OUT("static "); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1268 | OUT("int\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1269 | OUT("%s", p); |
| 1270 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1271 | OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1272 | INDENTED( |
| 1273 | OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1274 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1275 | p, expr->_type_unique_index); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1276 | OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n"); |
| 1277 | ); |
| 1278 | OUT("}\n"); |
| 1279 | OUT("\n"); |
| 1280 | |
| 1281 | p = MKID(expr->Identifier); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1282 | if(HIDE_INNER_DEFS) OUT("static "); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1283 | OUT("asn_dec_rval_t\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1284 | OUT("%s", p); |
| 1285 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1286 | 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] | 1287 | INDENTED( |
vlm | fcc4cbc | 2005-03-17 21:56:00 +0000 | [diff] [blame] | 1288 | OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1289 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1290 | p, expr->_type_unique_index); |
vlm | a5dcb91 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1291 | 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] | 1292 | ); |
| 1293 | OUT("}\n"); |
| 1294 | OUT("\n"); |
| 1295 | |
| 1296 | p = MKID(expr->Identifier); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1297 | if(HIDE_INNER_DEFS) OUT("static "); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1298 | OUT("asn_enc_rval_t\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1299 | OUT("%s", p); |
| 1300 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1301 | OUT("_encode_der(asn_TYPE_descriptor_t *td,\n"); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1302 | INDENTED( |
| 1303 | OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n"); |
| 1304 | OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1305 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1306 | p, expr->_type_unique_index); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1307 | OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n"); |
| 1308 | ); |
| 1309 | OUT("}\n"); |
| 1310 | OUT("\n"); |
| 1311 | |
| 1312 | p = MKID(expr->Identifier); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1313 | if(HIDE_INNER_DEFS) OUT("static "); |
vlm | e03646b | 2004-10-23 13:34:00 +0000 | [diff] [blame] | 1314 | OUT("asn_dec_rval_t\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1315 | OUT("%s", p); |
| 1316 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1317 | 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] | 1318 | INDENTED( |
vlm | fcc4cbc | 2005-03-17 21:56:00 +0000 | [diff] [blame] | 1319 | 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] | 1320 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1321 | p, expr->_type_unique_index); |
vlm | e03646b | 2004-10-23 13:34:00 +0000 | [diff] [blame] | 1322 | OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n"); |
| 1323 | ); |
| 1324 | OUT("}\n"); |
| 1325 | OUT("\n"); |
| 1326 | |
| 1327 | p = MKID(expr->Identifier); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1328 | if(HIDE_INNER_DEFS) OUT("static "); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1329 | OUT("asn_enc_rval_t\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1330 | OUT("%s", p); |
| 1331 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1332 | OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n"); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1333 | INDENTED( |
| 1334 | OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n"); |
| 1335 | OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1336 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1337 | p, expr->_type_unique_index); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1338 | OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1339 | ); |
| 1340 | OUT("}\n"); |
| 1341 | OUT("\n"); |
| 1342 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1343 | if(arg->flags & A1C_GEN_PER) { |
| 1344 | p = MKID(expr->Identifier); |
| 1345 | if(HIDE_INNER_DEFS) OUT("static "); |
| 1346 | OUT("asn_dec_rval_t\n"); |
| 1347 | OUT("%s", p); |
| 1348 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1349 | OUT("_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n"); |
| 1350 | INDENTED( |
| 1351 | OUT("\tasn_per_constraints_t *constraints, void **structure, asn_per_data_t *per_data) {\n"); |
| 1352 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1353 | p, expr->_type_unique_index); |
| 1354 | OUT("return td->uper_decoder(opt_codec_ctx, td, constraints, structure, per_data);\n"); |
| 1355 | ); |
| 1356 | OUT("}\n"); |
| 1357 | OUT("\n"); |
| 1358 | } |
| 1359 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1360 | REDIR(OT_FUNC_DECLS); |
| 1361 | |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 1362 | p = MKID(expr->Identifier); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1363 | if(HIDE_INNER_DEFS) { |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1364 | OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;" |
| 1365 | "\t// (Use -fall-defs-global to expose) */\n", |
| 1366 | p, expr->_type_unique_index); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1367 | } else { |
| 1368 | OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p); |
| 1369 | OUT("asn_struct_free_f %s_free;\n", p); |
| 1370 | OUT("asn_struct_print_f %s_print;\n", p); |
| 1371 | OUT("asn_constr_check_f %s_constraint;\n", p); |
| 1372 | OUT("ber_type_decoder_f %s_decode_ber;\n", p); |
| 1373 | OUT("der_type_encoder_f %s_encode_der;\n", p); |
| 1374 | OUT("xer_type_decoder_f %s_decode_xer;\n", p); |
| 1375 | OUT("xer_type_encoder_f %s_encode_xer;\n", p); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1376 | if(arg->flags & A1C_GEN_PER) |
| 1377 | OUT("per_type_decoder_f %s_decode_uper;\n", p); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1378 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1379 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 1380 | REDIR(OT_TYPE_DECLS); |
| 1381 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | int |
| 1386 | asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) { |
| 1387 | |
| 1388 | OUT("/*\n"); |
| 1389 | OUT(" * This type is extensible,\n"); |
| 1390 | OUT(" * possible extensions are below.\n"); |
| 1391 | OUT(" */\n"); |
| 1392 | |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1396 | static int |
| 1397 | compute_extensions_start(asn1p_expr_t *expr) { |
vlm | 79b08d5 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 1398 | asn1p_expr_t *v; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1399 | int eidx = 0; |
vlm | 79b08d5 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 1400 | TQ_FOR(v, &(expr->members), next) { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1401 | if(v->expr_type == A1TC_EXTENSIBLE) |
| 1402 | return eidx; |
| 1403 | eidx++; |
vlm | 79b08d5 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 1404 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1405 | return -1; |
vlm | 79b08d5 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1408 | static int |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1409 | _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1410 | |
| 1411 | OUT("("); |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1412 | switch(tag->tag_class) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1413 | case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break; |
| 1414 | case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break; |
| 1415 | case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break; |
| 1416 | case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break; |
| 1417 | case TC_NOCLASS: |
| 1418 | break; |
| 1419 | } |
vlm | 47ae158 | 2004-09-24 21:01:43 +0000 | [diff] [blame] | 1420 | OUT(" | (%" PRIdASN " << 2))", tag->tag_value); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1421 | |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1425 | |
| 1426 | static int |
| 1427 | _tag2el_cmp(const void *ap, const void *bp) { |
| 1428 | const tag2el_t *a = ap; |
| 1429 | const tag2el_t *b = bp; |
| 1430 | const struct asn1p_type_tag_s *ta = &a->el_tag; |
| 1431 | const struct asn1p_type_tag_s *tb = &b->el_tag; |
| 1432 | |
| 1433 | if(ta->tag_class == tb->tag_class) { |
| 1434 | if(ta->tag_value == tb->tag_value) { |
| 1435 | /* |
| 1436 | * Sort by their respective positions. |
| 1437 | */ |
| 1438 | if(a->el_no < b->el_no) |
| 1439 | return -1; |
| 1440 | else if(a->el_no > b->el_no) |
| 1441 | return 1; |
| 1442 | return 0; |
| 1443 | } else if(ta->tag_value < tb->tag_value) |
| 1444 | return -1; |
| 1445 | else |
| 1446 | return 1; |
| 1447 | } else if(ta->tag_class < tb->tag_class) { |
| 1448 | return -1; |
| 1449 | } else { |
| 1450 | return 1; |
| 1451 | } |
| 1452 | } |
| 1453 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1454 | /* |
| 1455 | * For constructed types, number of external tags may be greater than |
| 1456 | * number of elements in the type because of CHOICE type. |
| 1457 | * T ::= SET { -- Three possible tags: |
| 1458 | * a INTEGER, -- One tag is here... |
| 1459 | * b Choice1 -- ... and two more tags are there. |
| 1460 | * } |
| 1461 | * Choice1 ::= CHOICE { |
| 1462 | * s1 IA5String, |
| 1463 | * s2 ObjectDescriptor |
| 1464 | * } |
| 1465 | */ |
| 1466 | static int |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1467 | _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] | 1468 | asn1p_expr_t *expr = arg->expr; |
| 1469 | arg_t tmparg = *arg; |
| 1470 | asn1p_expr_t *v; |
| 1471 | int element = 0; |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1472 | int original_count = *count; |
| 1473 | int sort_until = -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1474 | |
| 1475 | TQ_FOR(v, &(expr->members), next) { |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1476 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 1477 | /* |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1478 | * CXER mandates sorting |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1479 | * only for the root part. |
| 1480 | */ |
| 1481 | if(flags == FTE_CANONICAL_XER |
| 1482 | && sort_until == -1) |
| 1483 | sort_until = *count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1484 | continue; |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1485 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1486 | |
| 1487 | tmparg.expr = v; |
| 1488 | |
| 1489 | if(_add_tag2el_member(&tmparg, tag2el, count, |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1490 | (el_no==-1)?element:el_no, flags)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1491 | return -1; |
| 1492 | } |
| 1493 | |
| 1494 | element++; |
| 1495 | } |
| 1496 | |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1497 | |
| 1498 | if(flags == FTE_CANONICAL_XER) { |
| 1499 | if(sort_until == -1) sort_until = *count; |
| 1500 | qsort((*tag2el) + original_count, |
| 1501 | sort_until - original_count, |
| 1502 | sizeof(**tag2el), _tag2el_cmp); |
| 1503 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE |
| 1504 | && (sort_until - original_count) >= 1) { |
| 1505 | /* Only take in account the root component */ |
| 1506 | *count = original_count + 1; |
| 1507 | } |
| 1508 | } else { |
| 1509 | /* |
| 1510 | * Sort the map according to canonical order of their |
| 1511 | * tags and element numbers. |
| 1512 | */ |
| 1513 | qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp); |
| 1514 | } |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1515 | |
vlm | c8aeab4 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 1516 | /* |
| 1517 | * Initialize .toff_{first|last} members. |
| 1518 | */ |
| 1519 | if(*count) { |
| 1520 | struct asn1p_type_tag_s *cur_tag = 0; |
| 1521 | tag2el_t *cur = *tag2el; |
| 1522 | tag2el_t *end = cur + *count; |
| 1523 | int occur, i; |
| 1524 | for(occur = 0; cur < end; cur++) { |
| 1525 | if(cur_tag == 0 |
| 1526 | || cur_tag->tag_value != cur->el_tag.tag_value |
| 1527 | || cur_tag->tag_class != cur->el_tag.tag_class) { |
| 1528 | cur_tag = &cur->el_tag; |
| 1529 | occur = 0; |
| 1530 | } else { |
| 1531 | occur++; |
| 1532 | } |
| 1533 | cur->toff_first = -occur; |
| 1534 | for(i = 0; i >= -occur; i--) |
| 1535 | cur[i].toff_last = -i; |
| 1536 | } |
| 1537 | } |
| 1538 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | static int |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1543 | _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] | 1544 | struct asn1p_type_tag_s tag; |
| 1545 | int ret; |
| 1546 | |
| 1547 | assert(el_no >= 0); |
| 1548 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1549 | ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module, |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1550 | arg->expr, &tag, AFT_IMAGINARY_ANY); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1551 | if(ret == 0) { |
vlm | e2070ea | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1552 | tag2el_t *te; |
| 1553 | int new_count = (*count) + 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1554 | void *p; |
vlm | e2070ea | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1555 | |
vlm | 2de0e79 | 2004-09-05 10:42:33 +0000 | [diff] [blame] | 1556 | if(tag.tag_value == -1) { |
| 1557 | /* |
| 1558 | * This is an untagged ANY type, |
| 1559 | * proceed without adding a tag |
| 1560 | */ |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
vlm | e2070ea | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1564 | p = realloc(*tag2el, new_count * sizeof(tag2el_t)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1565 | if(p) *tag2el = p; |
| 1566 | else return -1; |
| 1567 | |
| 1568 | DEBUG("Found tag for %s: %ld", |
| 1569 | arg->expr->Identifier, |
| 1570 | (long)tag.tag_value); |
| 1571 | |
vlm | e2070ea | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1572 | te = &((*tag2el)[*count]); |
| 1573 | te->el_tag = tag; |
| 1574 | te->el_no = el_no; |
| 1575 | te->from_expr = arg->expr; |
| 1576 | *count = new_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1577 | return 0; |
| 1578 | } |
| 1579 | |
| 1580 | DEBUG("Searching tag in complex expression %s:%x at line %d", |
| 1581 | arg->expr->Identifier, |
| 1582 | arg->expr->expr_type, |
| 1583 | arg->expr->_lineno); |
| 1584 | |
| 1585 | /* |
| 1586 | * Iterate over members of CHOICE type. |
| 1587 | */ |
| 1588 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE) { |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1589 | return _fill_tag2el_map(arg, tag2el, count, el_no, flags); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | if(arg->expr->expr_type == A1TC_REFERENCE) { |
| 1593 | arg_t tmp = *arg; |
| 1594 | asn1p_expr_t *expr; |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 1595 | expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.expr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1596 | arg->expr->reference); |
| 1597 | if(expr) { |
| 1598 | tmp.expr = expr; |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1599 | return _add_tag2el_member(&tmp, tag2el, count, el_no, flags); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1600 | } else { |
| 1601 | FATAL("Cannot dereference %s at line %d", |
| 1602 | arg->expr->Identifier, |
| 1603 | arg->expr->_lineno); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | DEBUG("No tag for %s at line %d", |
| 1609 | arg->expr->Identifier, |
| 1610 | arg->expr->_lineno); |
| 1611 | |
| 1612 | return -1; |
| 1613 | } |
| 1614 | |
| 1615 | static int |
vlm | 940bc6b | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1616 | 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] | 1617 | asn1p_expr_t *expr = arg->expr; |
vlm | fbd6d9e | 2005-06-02 05:21:37 +0000 | [diff] [blame] | 1618 | int i; |
| 1619 | |
| 1620 | if(!tag2el_count) return 0; /* No top level tags */ |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1621 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1622 | OUT("static asn_TYPE_tag2member_t asn_MAP_%s_tag2el%s_%d[] = {\n", |
| 1623 | MKID(expr->Identifier), opt_modifier?opt_modifier:"", |
| 1624 | expr->_type_unique_index); |
vlm | fbd6d9e | 2005-06-02 05:21:37 +0000 | [diff] [blame] | 1625 | for(i = 0; i < tag2el_count; i++) { |
| 1626 | OUT(" { "); |
| 1627 | _print_tag(arg, &tag2el[i].el_tag); |
| 1628 | OUT(", "); |
| 1629 | OUT("%d, ", tag2el[i].el_no); |
| 1630 | OUT("%d, ", tag2el[i].toff_first); |
| 1631 | OUT("%d ", tag2el[i].toff_last); |
| 1632 | OUT("}%s /* %s at %d */\n", |
| 1633 | (i + 1 < tag2el_count) ? "," : "", |
| 1634 | tag2el[i].from_expr->Identifier, |
| 1635 | tag2el[i].from_expr->_lineno |
| 1636 | ); |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1637 | } |
| 1638 | OUT("};\n"); |
| 1639 | |
vlm | fbd6d9e | 2005-06-02 05:21:37 +0000 | [diff] [blame] | 1640 | return 0; |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1643 | static enum tvm_compat |
| 1644 | emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) { |
| 1645 | struct asn1p_type_tag_s *tags = 0; /* Effective tags */ |
| 1646 | struct asn1p_type_tag_s *all_tags = 0; /* The full array */ |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1647 | int tags_count = 0; |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1648 | int all_tags_count = 0; |
| 1649 | enum tvm_compat tv_mode = _TVM_SAME; |
vlm | 1308d2b | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1650 | int i; |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1651 | |
vlm | c6de2c4 | 2004-09-14 14:10:10 +0000 | [diff] [blame] | 1652 | /* Cleanup before proceeding. */ |
| 1653 | *tags_count_r = 0; |
| 1654 | *all_tags_count_r = 0; |
| 1655 | |
vlm | 1308d2b | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1656 | /* Fetch a chain of tags */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1657 | tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0); |
vlm | c6de2c4 | 2004-09-14 14:10:10 +0000 | [diff] [blame] | 1658 | if(tags_count < 0) |
| 1659 | return -1; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1660 | |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1661 | /* Fetch a chain of tags */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1662 | all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1663 | &all_tags, AFT_FULL_COLLECT); |
| 1664 | if(all_tags_count < 0) { |
| 1665 | if(tags) free(tags); |
| 1666 | return -1; |
vlm | fb41dbb | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 1667 | } |
vlm | 1308d2b | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1668 | |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1669 | assert(tags_count <= all_tags_count); |
| 1670 | assert((tags_count?0:1) == (all_tags_count?0:1)); |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1671 | |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1672 | if(tags_count <= all_tags_count) { |
| 1673 | for(i = 0; i < tags_count; i++) { |
| 1674 | if(tags[i].tag_value != all_tags[i].tag_value |
| 1675 | || tags[i].tag_class != all_tags[i].tag_class) { |
| 1676 | tv_mode = _TVM_DIFFERENT; |
| 1677 | break; |
| 1678 | } |
| 1679 | } |
| 1680 | if(i == tags_count && tags_count < all_tags_count) |
| 1681 | tv_mode = _TVM_SUBSET; |
| 1682 | } else { |
| 1683 | tv_mode = _TVM_DIFFERENT; |
| 1684 | } |
| 1685 | |
| 1686 | #define EMIT_TAGS_TABLE(name, tags, tags_count) do { \ |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1687 | OUT("static ber_tlv_tag_t asn_DEF_%s%s_tags_%d[] = {\n",\ |
| 1688 | MKID(expr->Identifier), name, \ |
| 1689 | expr->_type_unique_index); \ |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1690 | INDENT(+1); \ |
| 1691 | /* Print the array of collected tags */ \ |
| 1692 | for(i = 0; i < tags_count; i++) { \ |
| 1693 | if(i) OUT(",\n"); \ |
| 1694 | _print_tag(arg, &tags[i]); \ |
| 1695 | } \ |
| 1696 | OUT("\n"); \ |
| 1697 | INDENT(-1); \ |
| 1698 | OUT("};\n"); \ |
| 1699 | } while(0) |
| 1700 | |
| 1701 | if(tags_count) { |
| 1702 | if(tv_mode == _TVM_SUBSET) |
| 1703 | EMIT_TAGS_TABLE("", all_tags, all_tags_count); |
| 1704 | else |
| 1705 | EMIT_TAGS_TABLE("", tags, tags_count); |
| 1706 | } |
| 1707 | |
| 1708 | if(all_tags_count) { |
| 1709 | if(tv_mode == _TVM_DIFFERENT) |
| 1710 | EMIT_TAGS_TABLE("_all", all_tags, all_tags_count); |
| 1711 | } |
| 1712 | |
| 1713 | if(tags) free(tags); |
| 1714 | if(all_tags) free(all_tags); |
| 1715 | |
| 1716 | *tags_count_r = tags_count; |
| 1717 | *all_tags_count_r = all_tags_count; |
| 1718 | |
| 1719 | return tv_mode; |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1720 | } |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1721 | |
| 1722 | static int |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1723 | expr_elements_count(arg_t *arg, asn1p_expr_t *expr) { |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1724 | asn1p_expr_t *topmost_parent; |
| 1725 | asn1p_expr_t *v; |
| 1726 | int elements = 0; |
| 1727 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1728 | topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1729 | if(!topmost_parent) return 0; |
| 1730 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1731 | if(!(topmost_parent->expr_type & ASN_CONSTR_MASK) |
| 1732 | && !topmost_parent->expr_type == ASN_BASIC_INTEGER |
| 1733 | && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED) |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1734 | return 0; |
| 1735 | |
| 1736 | TQ_FOR(v, &(topmost_parent->members), next) { |
| 1737 | if(v->expr_type != A1TC_EXTENSIBLE) |
| 1738 | elements++; |
| 1739 | } |
| 1740 | |
| 1741 | return elements; |
| 1742 | } |
| 1743 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1744 | static asn1p_expr_type_e |
| 1745 | expr_get_type(arg_t *arg, asn1p_expr_t *expr) { |
| 1746 | asn1p_expr_t *terminal; |
| 1747 | terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 1748 | if(terminal) return terminal->expr_type; |
| 1749 | return A1TC_INVALID; |
| 1750 | } |
| 1751 | |
| 1752 | static int |
| 1753 | emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, char *type) { |
| 1754 | if(!range || range->incompatible || range->not_PER_visible) { |
| 1755 | OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }"); |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | if(range->left.type == ARE_VALUE) { |
| 1760 | if(range->right.type == ARE_VALUE) { |
| 1761 | asn1c_integer_t cover = 1; |
| 1762 | asn1c_integer_t r = 1 + range->right.value |
| 1763 | - range->left.value; |
| 1764 | int rbits, ebits; |
| 1765 | |
| 1766 | if(range->empty_constraint) |
| 1767 | r = 0; |
| 1768 | |
| 1769 | /* Compute real constraint */ |
| 1770 | for(rbits = 0; rbits < (8 * sizeof(r)); rbits++) { |
| 1771 | if(r <= cover) |
| 1772 | break; |
| 1773 | cover *= 2; /* Can't do shifting */ |
| 1774 | if(cover < 0) { |
| 1775 | FATAL("Constraint at line %d too wide " |
| 1776 | "for %d-bits integer type", |
| 1777 | arg->expr->_lineno, |
| 1778 | sizeof(r) * 8); |
| 1779 | rbits = sizeof(r); |
| 1780 | break; |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | if(1) { |
| 1785 | /* X.691, #10.9.4.1 */ |
| 1786 | for(ebits = 0; ebits <= 16; ebits++) |
| 1787 | if(r <= 1 << ebits) break; |
| 1788 | if(ebits == 17 |
| 1789 | || range->right.value >= 65536) |
| 1790 | ebits = -1; |
| 1791 | } else { |
| 1792 | /* X.691, #10.5.7.1 */ |
| 1793 | for(ebits = 0; ebits <= 8; ebits++) |
| 1794 | if(r <= 1 << ebits) break; |
| 1795 | if(ebits == 9) { |
| 1796 | if(r <= 65536) |
| 1797 | ebits = 16; |
| 1798 | else |
| 1799 | ebits = -1; |
| 1800 | } |
| 1801 | } |
| 1802 | OUT("{ APC_CONSTRAINED%s,%s% d, % d, ", |
| 1803 | range->extensible |
| 1804 | ? " | APC_EXTENSIBLE" : "", |
| 1805 | range->extensible ? " " : "\t", rbits, ebits); |
| 1806 | } else { |
| 1807 | if(range->extensible) { |
| 1808 | OUT("{ APC_SEMI_CONSTRAINED | APC_EXTENSIBLE, " |
| 1809 | "-1, "); |
| 1810 | } else { |
| 1811 | OUT("{ APC_SEMI_CONSTRAINED,\t-1, -1, "); |
| 1812 | } |
| 1813 | } |
| 1814 | OUT("% " PRIdASN ", % " PRIdASN " }", |
| 1815 | range->left.value, range->right.value); |
| 1816 | } else { |
| 1817 | OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }"); |
| 1818 | } |
| 1819 | |
| 1820 | /* |
| 1821 | * Print some courtesy debug information. |
| 1822 | */ |
| 1823 | if(range->left.type == ARE_VALUE |
| 1824 | || range->right.type == ARE_VALUE) { |
| 1825 | OUT("\t/* "); |
| 1826 | if(type) OUT("(%s", type); |
| 1827 | OUT("("); |
| 1828 | if(range->left.type == ARE_VALUE) |
| 1829 | OUT("%" PRIdASN, range->left.value); |
| 1830 | else |
| 1831 | OUT("MIN"); |
| 1832 | OUT(".."); |
| 1833 | if(range->right.type == ARE_VALUE) |
| 1834 | OUT("%" PRIdASN, range->right.value); |
| 1835 | else |
| 1836 | OUT("MAX"); |
| 1837 | if(range->extensible) OUT(",..."); |
| 1838 | if(type) OUT(")"); |
| 1839 | OUT(") */"); |
| 1840 | } |
| 1841 | |
| 1842 | return 0; |
| 1843 | } |
| 1844 | |
| 1845 | static int |
| 1846 | emit_single_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr) { |
| 1847 | asn1cnst_range_t *range; |
| 1848 | asn1p_expr_type_e etype; |
| 1849 | |
| 1850 | etype = expr_get_type(arg, expr); |
| 1851 | |
| 1852 | INDENT(+1); |
| 1853 | |
| 1854 | /* |
| 1855 | * ENUMERATED and CHOICE are special. |
| 1856 | */ |
| 1857 | if(etype == ASN_BASIC_ENUMERATED |
| 1858 | || etype == ASN_CONSTR_CHOICE) { |
| 1859 | asn1cnst_range_t tmprng; |
| 1860 | asn1p_expr_t *v; |
| 1861 | int extensible = 0; |
| 1862 | int eidx = -1; |
| 1863 | |
| 1864 | expr = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 1865 | assert(expr); |
| 1866 | |
| 1867 | TQ_FOR(v, &(expr->members), next) { |
| 1868 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 1869 | extensible++; |
| 1870 | break; |
| 1871 | } |
| 1872 | eidx++; |
| 1873 | } |
| 1874 | |
| 1875 | memset(&tmprng, 0, sizeof (tmprng)); |
| 1876 | tmprng.extensible = extensible; |
| 1877 | if(eidx < 0) tmprng.empty_constraint = 1; |
| 1878 | tmprng.left.type = ARE_VALUE; |
| 1879 | tmprng.left.value = 0; |
| 1880 | tmprng.right.type = ARE_VALUE; |
| 1881 | tmprng.right.value = eidx < 0 ? 0 : eidx; |
| 1882 | if(emit_single_member_PER_constraint(arg, &tmprng, 0)) |
| 1883 | return -1; |
| 1884 | } else { |
| 1885 | range = asn1constraint_compute_PER_range(etype, |
| 1886 | expr->combined_constraints, ACT_EL_RANGE, |
| 1887 | 0, 0, 0); |
| 1888 | if(emit_single_member_PER_constraint(arg, range, 0)) |
| 1889 | return -1; |
| 1890 | asn1constraint_range_free(range); |
| 1891 | } |
| 1892 | OUT(",\n"); |
| 1893 | |
| 1894 | range = asn1constraint_compute_PER_range(etype, |
| 1895 | expr->combined_constraints, ACT_CT_SIZE, 0, 0, 0); |
| 1896 | if(emit_single_member_PER_constraint(arg, range, "SIZE")) |
| 1897 | return -1; |
| 1898 | asn1constraint_range_free(range); |
| 1899 | OUT("\n"); |
| 1900 | |
| 1901 | INDENT(-1); |
| 1902 | |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | static int |
| 1907 | emit_members_PER_constraints(arg_t *arg) { |
| 1908 | asn1p_expr_t *expr = arg->expr; |
| 1909 | asn1p_expr_t *v; |
| 1910 | |
| 1911 | if(!(arg->flags & A1C_GEN_PER)) |
| 1912 | return 0; |
| 1913 | |
| 1914 | TQ_FOR(v, &(expr->members), next) { |
| 1915 | if(v->constraints |
| 1916 | || v->expr_type == ASN_BASIC_ENUMERATED |
| 1917 | || v->expr_type == ASN_CONSTR_CHOICE) { |
| 1918 | OUT("static asn_per_constraints_t " |
| 1919 | "asn_PER_memb_%s_constr_%d = {\n", |
| 1920 | MKID(v->Identifier), v->_type_unique_index); |
| 1921 | if(emit_single_member_PER_constraints(arg, v)) |
| 1922 | return -1; |
| 1923 | OUT("};\n"); |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | static int |
| 1931 | try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) { |
| 1932 | int save_target = arg->target->target; |
| 1933 | asn1p_expr_type_e etype = expr_get_type(arg, expr); |
| 1934 | int fits_long = 0; |
| 1935 | |
| 1936 | switch(etype) { |
| 1937 | case ASN_BASIC_BOOLEAN: |
| 1938 | fits_long = 1; |
| 1939 | case ASN_BASIC_INTEGER: |
| 1940 | case ASN_BASIC_ENUMERATED: |
| 1941 | if(expr->marker.default_value == NULL |
| 1942 | || expr->marker.default_value->type != ATV_INTEGER) |
| 1943 | break; |
| 1944 | if(!fits_long) |
| 1945 | fits_long = asn1c_type_fits_long(arg, expr)!=FL_NOTFIT; |
| 1946 | if(fits_long && !expr->marker.default_value->value.v_integer) |
| 1947 | expr->marker.flags &= ~EM_INDIRECT; |
| 1948 | if(!out) return 1; |
| 1949 | REDIR(OT_STAT_DEFS); |
| 1950 | OUT("static int asn_DFL_%d_set_%" PRIdASN "(void **sptr) {\n", |
| 1951 | expr->_type_unique_index, |
| 1952 | expr->marker.default_value->value.v_integer); |
| 1953 | INDENT(+1); |
| 1954 | OUT("%s *st = *sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE)); |
| 1955 | OUT("\n"); |
| 1956 | OUT("if(!st) {\n"); |
| 1957 | OUT("\tst = (*sptr = CALLOC(1, sizeof(*st)));\n"); |
| 1958 | OUT("\tif(!st) return -1;\n"); |
| 1959 | OUT("}\n"); |
| 1960 | OUT("\n"); |
| 1961 | OUT("/* Install default value %" PRIdASN " */\n", |
| 1962 | expr->marker.default_value->value.v_integer); |
| 1963 | if(fits_long) { |
| 1964 | OUT("*st = %" PRIdASN ";\n", |
| 1965 | expr->marker.default_value->value.v_integer); |
| 1966 | OUT("return 0;\n"); |
| 1967 | } else { |
| 1968 | OUT("return asn_long2INTEGER(st, %" PRIdASN ");\n", |
| 1969 | expr->marker.default_value->value.v_integer); |
| 1970 | } |
| 1971 | INDENT(-1); |
| 1972 | OUT("}\n"); |
| 1973 | REDIR(save_target); |
| 1974 | return 1; |
| 1975 | case ASN_BASIC_NULL: |
| 1976 | //expr->marker.flags &= ~EM_INDIRECT; |
| 1977 | return 0; |
| 1978 | default: |
| 1979 | break; |
| 1980 | } |
| 1981 | return 0; |
| 1982 | } |
| 1983 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1984 | static int |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1985 | emit_member_table(arg_t *arg, asn1p_expr_t *expr) { |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1986 | int save_target; |
| 1987 | arg_t tmp_arg; |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 1988 | struct asn1p_type_tag_s outmost_tag_s; |
| 1989 | struct asn1p_type_tag_s *outmost_tag; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1990 | int complex_contents; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1991 | char *p; |
| 1992 | |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 1993 | if(asn1f_fetch_outmost_tag(arg->asn, |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1994 | expr->module, expr, &outmost_tag_s, |
| 1995 | AFT_IMAGINARY_ANY)) { |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 1996 | outmost_tag = 0; |
| 1997 | } else { |
| 1998 | outmost_tag = &outmost_tag_s; |
| 1999 | } |
| 2000 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2001 | OUT("{ "); |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2002 | |
| 2003 | if(outmost_tag && outmost_tag->tag_value == -1) |
| 2004 | OUT("ATF_OPEN_TYPE | "); |
vlm | 0b567bf | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 2005 | OUT("%s, ", |
| 2006 | (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS"); |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 2007 | if((expr->marker.flags & EM_OMITABLE) == EM_OMITABLE) { |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2008 | asn1p_expr_t *tv; |
| 2009 | int opts = 0; |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 2010 | for(tv = expr; |
| 2011 | tv && (tv->marker.flags & EM_OMITABLE) == EM_OMITABLE; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2012 | tv = TQ_NEXT(tv, next), opts++) { |
| 2013 | if(tv->expr_type == A1TC_EXTENSIBLE) |
| 2014 | opts--; |
| 2015 | } |
| 2016 | OUT("%d, ", opts); |
| 2017 | } else { |
| 2018 | OUT("0, "); |
| 2019 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 2020 | if(expr->_anonymous_type) { |
| 2021 | assert(arg->expr->expr_type == ASN_CONSTR_SET_OF |
| 2022 | || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF); |
| 2023 | OUT("0,\n"); |
| 2024 | } else { |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2025 | OUT("offsetof(struct "); |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 2026 | out_name_chain(arg, ONC_avoid_keywords); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2027 | OUT(", "); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2028 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE |
| 2029 | && (!UNNAMED_UNIONS)) OUT("choice."); |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 2030 | OUT("%s),\n", MKID_safe(expr->Identifier)); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2031 | } |
| 2032 | INDENT(+1); |
| 2033 | if(C99_MODE) OUT(".tag = "); |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2034 | if(outmost_tag) { |
| 2035 | if(outmost_tag->tag_value == -1) |
| 2036 | OUT("-1 /* Ambiguous tag (ANY?) */"); |
| 2037 | else |
| 2038 | _print_tag(arg, outmost_tag); |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 2039 | } else { |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2040 | OUT("-1 /* Ambiguous tag (CHOICE?) */"); |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 2041 | } |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2042 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2043 | OUT(",\n"); |
| 2044 | if(C99_MODE) OUT(".tag_mode = "); |
| 2045 | if(expr->tag.tag_class) { |
| 2046 | if(expr->tag.tag_mode == TM_IMPLICIT) |
| 2047 | OUT("-1,\t/* IMPLICIT tag at current level */\n"); |
| 2048 | else |
| 2049 | OUT("+1,\t/* EXPLICIT tag at current level */\n"); |
| 2050 | } else { |
| 2051 | OUT("0,\n"); |
| 2052 | } |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 2053 | |
| 2054 | complex_contents = |
| 2055 | (expr->expr_type & ASN_CONSTR_MASK) |
| 2056 | || expr->expr_type == ASN_BASIC_ENUMERATED |
vlm | c89422d | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 2057 | || (0 /* -- prohibited by X.693:8.3.4 */ |
| 2058 | && expr->expr_type == ASN_BASIC_INTEGER |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 2059 | && expr_elements_count(arg, expr)); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2060 | if(C99_MODE) OUT(".type = "); |
vlm | e0fea71 | 2005-08-29 10:31:14 +0000 | [diff] [blame] | 2061 | OUT("&asn_DEF_"); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2062 | if(complex_contents) { |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 2063 | OUT("%s", MKID(expr->Identifier)); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2064 | if(!(arg->flags & A1C_ALL_DEFS_GLOBAL)) |
| 2065 | OUT("_%d", expr->_type_unique_index); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 2066 | } else { |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2067 | OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE)); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 2068 | } |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2069 | OUT(",\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2070 | if(C99_MODE) OUT(".memb_constraints = "); |
| 2071 | if(expr->constraints) { |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2072 | if(arg->flags & A1C_NO_CONSTRAINTS) { |
| 2073 | OUT("0,\t/* No check because of -fno-constraints */\n"); |
| 2074 | } else { |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 2075 | char *id = MKID(expr->Identifier); |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2076 | if(expr->_anonymous_type |
vlm | bf3c511 | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 2077 | && !strcmp(expr->Identifier, "Member")) |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2078 | id = asn1c_type_name(arg, expr, TNF_SAFE); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2079 | OUT("memb_%s_constraint_%d,\n", id, |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2080 | arg->expr->_type_unique_index); |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2081 | } |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2082 | } else { |
vlm | 95a476e | 2005-01-17 12:16:58 +0000 | [diff] [blame] | 2083 | OUT("0,\t/* Defer constraints checking to the member type */\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2084 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2085 | if(C99_MODE) OUT(".per_constraints = "); |
| 2086 | if(arg->flags & A1C_GEN_PER) { |
| 2087 | if(expr->constraints |
| 2088 | || expr->expr_type == ASN_BASIC_ENUMERATED |
| 2089 | || expr->expr_type == ASN_CONSTR_CHOICE) { |
| 2090 | OUT("&asn_PER_memb_%s_constr_%d,\n", |
| 2091 | MKID(expr->Identifier), |
| 2092 | expr->_type_unique_index); |
| 2093 | } else { |
| 2094 | OUT("0,\t/* No PER visible constraints */\n"); |
| 2095 | } |
vlm | bf3c511 | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 2096 | } else { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2097 | OUT("0,\t/* PER is not compiled, use -gen-PER */\n"); |
| 2098 | } |
| 2099 | if(C99_MODE) OUT(".default_value = "); |
| 2100 | if(try_inline_default(arg, expr, 0)) { |
| 2101 | OUT("asn_DFL_%d_set_%" PRIdASN |
| 2102 | ",\t/* DEFAULT %" PRIdASN " */\n", |
| 2103 | expr->_type_unique_index, |
| 2104 | expr->marker.default_value->value.v_integer, |
| 2105 | expr->marker.default_value->value.v_integer); |
| 2106 | } else { |
| 2107 | OUT("0,\n"); |
| 2108 | } |
| 2109 | if(C99_MODE) OUT(".name = "); |
| 2110 | if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member")) { |
| 2111 | OUT("\"\"\n"); |
| 2112 | } else { |
| 2113 | OUT("\"%s\"\n", expr->Identifier); |
vlm | bf3c511 | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 2114 | } |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2115 | OUT("},\n"); |
| 2116 | INDENT(-1); |
| 2117 | |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2118 | if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS)) |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2119 | return 0; |
| 2120 | |
| 2121 | save_target = arg->target->target; |
| 2122 | REDIR(OT_CODE); |
| 2123 | |
vlm | bf3c511 | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 2124 | if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member")) |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2125 | p = asn1c_type_name(arg, expr, TNF_SAFE); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 2126 | else |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 2127 | p = MKID(expr->Identifier); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2128 | OUT("static int\n"); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2129 | OUT("memb_%s_constraint_%d(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] | 2130 | INDENT(+1); |
| 2131 | OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n"); |
| 2132 | tmp_arg = *arg; |
| 2133 | tmp_arg.expr = expr; |
| 2134 | if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) { |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2135 | OUT("return td->check_constraints" |
| 2136 | "(td, sptr, app_errlog, app_key);\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2137 | } |
| 2138 | INDENT(-1); |
| 2139 | OUT("}\n"); |
| 2140 | OUT("\n"); |
| 2141 | |
| 2142 | REDIR(save_target); |
| 2143 | |
| 2144 | return 0; |
| 2145 | } |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2146 | |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 2147 | /* |
| 2148 | * Generate "asn_DEF_XXX" type definition. |
| 2149 | */ |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2150 | static int |
vlm | 86f5ed2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2151 | 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] | 2152 | int using_type_name = 0; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2153 | char *p = MKID(expr->Identifier); |
| 2154 | |
| 2155 | if((arg->flags & A1C_GEN_PER) |
| 2156 | && (expr->constraints |
| 2157 | || expr->expr_type == ASN_BASIC_ENUMERATED |
| 2158 | || expr->expr_type == ASN_CONSTR_CHOICE) |
| 2159 | ) { |
| 2160 | OUT("static asn_per_constraints_t asn_PER_%s_constr_%d = {\n", |
| 2161 | p, expr->_type_unique_index); |
| 2162 | if(emit_single_member_PER_constraints(arg, expr)) |
| 2163 | return -1; |
| 2164 | OUT("};\n"); |
| 2165 | } |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2166 | |
vlm | 12c8f69 | 2004-09-06 08:07:29 +0000 | [diff] [blame] | 2167 | if(HIDE_INNER_DEFS) |
| 2168 | OUT("static /* Use -fall-defs-global to expose */\n"); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2169 | OUT("asn_TYPE_descriptor_t asn_DEF_%s", p); |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2170 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 2171 | OUT(" = {\n"); |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2172 | INDENT(+1); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2173 | |
| 2174 | if(expr->_anonymous_type) { |
| 2175 | p = ASN_EXPR_TYPE2STR(expr->expr_type); |
| 2176 | OUT("\"%s\",\n", p?p:""); |
| 2177 | OUT("\"%s\",\n", MKID(p?p:"")); |
| 2178 | } else { |
| 2179 | OUT("\"%s\",\n", expr->Identifier); |
| 2180 | OUT("\"%s\",\n", expr->Identifier); |
| 2181 | } |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2182 | |
| 2183 | if(expr->expr_type & ASN_CONSTR_MASK) { |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2184 | using_type_name = 1; |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2185 | p = asn1c_type_name(arg, arg->expr, TNF_SAFE); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2186 | } else { |
| 2187 | p = MKID(expr->Identifier); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2190 | #define FUNCREF(foo) do { \ |
| 2191 | OUT("%s", p); \ |
| 2192 | if(HIDE_INNER_DEFS && !using_type_name) \ |
| 2193 | OUT("_%d", expr->_type_unique_index); \ |
| 2194 | OUT("_" #foo ",\n"); \ |
| 2195 | } while(0) |
| 2196 | |
| 2197 | FUNCREF(free); |
| 2198 | FUNCREF(print); |
| 2199 | FUNCREF(constraint); |
| 2200 | FUNCREF(decode_ber); |
| 2201 | FUNCREF(encode_der); |
| 2202 | FUNCREF(decode_xer); |
| 2203 | FUNCREF(encode_xer); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2204 | if(arg->flags & A1C_GEN_PER) |
| 2205 | FUNCREF(decode_uper); |
| 2206 | else |
| 2207 | OUT("0,\t/* No PER decoder, -gen-PER to enable */\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2208 | |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2209 | if(expr->expr_type == ASN_CONSTR_CHOICE) { |
| 2210 | OUT("CHOICE_outmost_tag,\n"); |
| 2211 | } else { |
| 2212 | OUT("0,\t/* Use generic outmost tag fetcher */\n"); |
| 2213 | } |
| 2214 | |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 2215 | p = MKID(expr->Identifier); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2216 | if(tags_count) { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2217 | OUT("asn_DEF_%s_tags_%d,\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2218 | p, expr->_type_unique_index); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2219 | OUT("sizeof(asn_DEF_%s_tags_%d)\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2220 | p, expr->_type_unique_index); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2221 | OUT("\t/sizeof(asn_DEF_%s_tags_%d[0])", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2222 | p, expr->_type_unique_index); |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2223 | if(tv_mode == _TVM_SUBSET |
| 2224 | && tags_count != all_tags_count) |
| 2225 | OUT(" - %d", all_tags_count - tags_count); |
| 2226 | OUT(", /* %d */\n", tags_count); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2227 | } else { |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2228 | OUT("0,\t/* No effective tags (pointer) */\n"); |
| 2229 | OUT("0,\t/* No effective tags (count) */\n"); |
| 2230 | } |
| 2231 | |
| 2232 | if(all_tags_count && tv_mode == _TVM_DIFFERENT) { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2233 | OUT("asn_DEF_%s_all_tags_%d,\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2234 | p, expr->_type_unique_index); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2235 | OUT("sizeof(asn_DEF_%s_all_tags_%d)\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2236 | p, expr->_type_unique_index); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2237 | OUT("\t/sizeof(asn_DEF_%s_all_tags_%d[0]), /* %d */\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2238 | p, expr->_type_unique_index, all_tags_count); |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2239 | } else if(all_tags_count) { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2240 | OUT("asn_DEF_%s_tags_%d,\t/* Same as above */\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2241 | p, expr->_type_unique_index); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2242 | OUT("sizeof(asn_DEF_%s_tags_%d)\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2243 | p, expr->_type_unique_index); |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2244 | OUT("\t/sizeof(asn_DEF_%s_tags_%d[0]), /* %d */\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2245 | p, expr->_type_unique_index, all_tags_count); |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2246 | } else { |
| 2247 | OUT("0,\t/* No tags (pointer) */\n"); |
| 2248 | OUT("0,\t/* No tags (count) */\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2251 | if(arg->flags & A1C_GEN_PER) { |
| 2252 | if(expr->constraints |
| 2253 | || expr->expr_type == ASN_BASIC_ENUMERATED |
| 2254 | || expr->expr_type == ASN_CONSTR_CHOICE) { |
| 2255 | OUT("&asn_PER_%s_constr_%d,\n", |
| 2256 | p, expr->_type_unique_index); |
| 2257 | } else { |
| 2258 | OUT("0,\t/* No PER visible constraints */\n"); |
| 2259 | } |
| 2260 | } else { |
| 2261 | OUT("0,\t/* No PER visible constraints */\n"); |
| 2262 | } |
| 2263 | |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2264 | if(elements_count) { |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2265 | OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2266 | if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF |
| 2267 | || expr->expr_type == ASN_CONSTR_SET_OF) { |
| 2268 | OUT("%d,\t/* Single element */\n", |
| 2269 | elements_count); |
| 2270 | assert(elements_count == 1); |
| 2271 | } else { |
| 2272 | OUT("%d,\t/* Elements count */\n", |
| 2273 | elements_count); |
| 2274 | } |
| 2275 | } else { |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2276 | if(expr_elements_count(arg, expr)) |
| 2277 | OUT("0, 0,\t/* Defined elsewhere */\n"); |
| 2278 | else |
| 2279 | OUT("0, 0,\t/* No members */\n"); |
| 2280 | } |
| 2281 | |
| 2282 | switch(spec) { |
| 2283 | case ETD_NO_SPECIFICS: |
| 2284 | OUT("0\t/* No specifics */\n"); |
| 2285 | break; |
| 2286 | case ETD_HAS_SPECIFICS: |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2287 | OUT("&asn_SPC_%s_specs_%d\t/* Additional specs */\n", |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2288 | p, expr->_type_unique_index); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2289 | } |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2290 | INDENT(-1); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2291 | OUT("};\n"); |
| 2292 | OUT("\n"); |
vlm | e2bb443 | 2004-08-26 06:20:34 +0000 | [diff] [blame] | 2293 | |
| 2294 | return 0; |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2295 | } |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 2296 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 2297 | static int |
| 2298 | expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) { |
vlm | 6d44a54 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 2299 | /* |
| 2300 | * X.680, 25.5, Table 5 |
| 2301 | */ |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2302 | switch(expr_get_type(arg, expr)) { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 2303 | case ASN_BASIC_BOOLEAN: |
| 2304 | case ASN_BASIC_ENUMERATED: |
| 2305 | case ASN_BASIC_NULL: |
| 2306 | return 1; |
vlm | 6d44a54 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 2307 | case ASN_CONSTR_CHOICE: |
| 2308 | return 2; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 2309 | default: |
| 2310 | return 0; |
| 2311 | } |
| 2312 | } |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 2313 | |
| 2314 | static int |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 2315 | out_name_chain(arg_t *arg, enum onc_flags onc_flags) { |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 2316 | asn1p_expr_t *expr = arg->expr; |
| 2317 | char *id; |
| 2318 | |
| 2319 | assert(expr->Identifier); |
| 2320 | |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 2321 | if((arg->flags & A1C_COMPOUND_NAMES |
| 2322 | || onc_flags & ONC_force_compound_name) |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2323 | && ((expr->expr_type & ASN_CONSTR_MASK) |
| 2324 | || expr->expr_type == ASN_BASIC_ENUMERATED |
vlm | 96853d8 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 2325 | || ((expr->expr_type == ASN_BASIC_INTEGER |
| 2326 | || expr->expr_type == ASN_BASIC_BIT_STRING) |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2327 | && expr_elements_count(arg, expr)) |
| 2328 | ) |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 2329 | && expr->parent_expr |
| 2330 | && expr->parent_expr->Identifier) { |
| 2331 | arg_t tmparg = *arg; |
| 2332 | |
| 2333 | tmparg.expr = expr->parent_expr; |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2334 | if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES; |
| 2335 | |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 2336 | out_name_chain(&tmparg, onc_flags); |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 2337 | |
vlm | 92d3cad | 2005-03-05 00:08:41 +0000 | [diff] [blame] | 2338 | OUT("__"); /* a separator between id components */ |
vlm | 766cf1e | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2339 | |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 2340 | /* Fall through */ |
| 2341 | } |
| 2342 | |
vlm | 2e77428 | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 2343 | if(onc_flags & ONC_avoid_keywords) |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 2344 | id = MKID_safe(expr->Identifier); |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 2345 | else |
vlm | 1f7df78 | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 2346 | id = MKID(expr->Identifier); |
vlm | af84197 | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 2347 | OUT("%s", id); |
| 2348 | |
| 2349 | return 0; |
| 2350 | } |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 2351 | |
| 2352 | static int |
| 2353 | emit_include_dependencies(arg_t *arg) { |
| 2354 | asn1p_expr_t *expr = arg->expr; |
| 2355 | asn1p_expr_t *memb; |
| 2356 | |
| 2357 | /* Avoid recursive definitions. */ |
| 2358 | TQ_FOR(memb, &(expr->members), next) { |
| 2359 | expr_break_recursion(arg, memb); |
| 2360 | } |
| 2361 | |
| 2362 | TQ_FOR(memb, &(expr->members), next) { |
| 2363 | |
| 2364 | if(memb->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) { |
| 2365 | if(terminal_structable(arg, memb)) { |
| 2366 | int saved_target = arg->target->target; |
| 2367 | REDIR(OT_FWD_DECLS); |
| 2368 | OUT("%s;\n", |
| 2369 | asn1c_type_name(arg, memb, TNF_RSAFE)); |
| 2370 | REDIR(saved_target); |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | if((!(memb->expr_type & ASN_CONSTR_MASK) |
| 2375 | && memb->expr_type > ASN_CONSTR_MASK) |
| 2376 | || memb->meta_type == AMT_TYPEREF) { |
| 2377 | if(memb->marker.flags & EM_UNRECURSE) { |
| 2378 | GEN_POSTINCLUDE(asn1c_type_name(arg, |
| 2379 | memb, TNF_INCLUDE)); |
| 2380 | } else { |
| 2381 | GEN_INCLUDE(asn1c_type_name(arg, |
| 2382 | memb, TNF_INCLUDE)); |
| 2383 | } |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | return 0; |
| 2388 | } |
| 2389 | |
| 2390 | /* |
| 2391 | * Check if it is better to make this type indirectly accessed via |
| 2392 | * a pointer. |
| 2393 | * This may be the case for the following recursive definition: |
| 2394 | * Type ::= CHOICE { member Type }; |
| 2395 | */ |
| 2396 | static int |
| 2397 | expr_break_recursion(arg_t *arg, asn1p_expr_t *expr) { |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 2398 | int ret; |
| 2399 | |
| 2400 | if(expr->marker.flags & EM_UNRECURSE) |
| 2401 | return 1; /* Already broken */ |
| 2402 | |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 2403 | /* -findirect-choice compiles members of CHOICE as indirect pointers */ |
| 2404 | if((arg->flags & A1C_INDIRECT_CHOICE) |
| 2405 | && arg->expr->expr_type == ASN_CONSTR_CHOICE |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2406 | && (expr_get_type(arg, expr) & ASN_CONSTR_MASK) |
vlm | 6c4a850 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 2407 | ) { |
| 2408 | /* Break cross-reference */ |
| 2409 | expr->marker.flags |= EM_INDIRECT | EM_UNRECURSE; |
| 2410 | return 1; |
| 2411 | } |
| 2412 | |
| 2413 | if((expr->marker.flags & EM_INDIRECT) |
| 2414 | || arg->expr->expr_type == ASN_CONSTR_SET_OF |
| 2415 | || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) { |
| 2416 | if(terminal_structable(arg, expr)) { |
| 2417 | expr->marker.flags |= EM_UNRECURSE; |
| 2418 | |
| 2419 | if(arg->expr->expr_type == ASN_CONSTR_SET_OF |
| 2420 | || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) { |
| 2421 | /* Don't put EM_INDIRECT even if recursion */ |
| 2422 | return 1; |
| 2423 | } |
| 2424 | |
| 2425 | /* Fall through */ |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | /* Look for recursive back-references */ |
| 2430 | ret = expr_defined_recursively(arg, expr); |
| 2431 | switch(ret) { |
| 2432 | case 2: /* Explicitly break the recursion */ |
| 2433 | case 1: /* Use safer typing */ |
| 2434 | expr->marker.flags |= EM_INDIRECT; |
| 2435 | expr->marker.flags |= EM_UNRECURSE; |
| 2436 | break; |
| 2437 | } |
| 2438 | |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | /* |
| 2443 | * Check if the type can be represented using simple `struct TYPE`. |
| 2444 | */ |
| 2445 | static asn1p_expr_t * |
| 2446 | terminal_structable(arg_t *arg, asn1p_expr_t *expr) { |
| 2447 | asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 2448 | if(terminal |
| 2449 | && !terminal->parent_expr |
| 2450 | && (terminal->expr_type & ASN_CONSTR_MASK)) { |
| 2451 | return terminal; |
| 2452 | } |
| 2453 | return 0; |
| 2454 | } |
| 2455 | |
| 2456 | static int |
| 2457 | asn1c_recurse(arg_t *arg, asn1p_expr_t *expr, int (*callback)(arg_t *arg, void *key), void *key) { |
| 2458 | arg_t tmp = *arg; |
| 2459 | int maxret = 0; |
| 2460 | int ret; |
| 2461 | |
| 2462 | if(expr->_mark) return 0; |
| 2463 | expr->_mark |= TM_RECURSION; |
| 2464 | |
| 2465 | /* Invoke callback for every type going into recursion */ |
| 2466 | tmp.expr = expr; |
| 2467 | maxret = callback(&tmp, key); |
| 2468 | if(maxret <= 1) { |
| 2469 | /* |
| 2470 | * Recursively invoke myself and the callbacks. |
| 2471 | */ |
| 2472 | TQ_FOR(tmp.expr, &(expr->members), next) { |
| 2473 | ret = asn1c_recurse(&tmp, tmp.expr, callback, key); |
| 2474 | if(ret > maxret) |
| 2475 | maxret = ret; |
| 2476 | if(maxret > 1) break; |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | expr->_mark &= ~TM_RECURSION; |
| 2481 | return maxret; |
| 2482 | } |
| 2483 | |
| 2484 | static int |
| 2485 | check_is_refer_to(arg_t *arg, void *key) { |
| 2486 | asn1p_expr_t *terminal = terminal_structable(arg, arg->expr); |
| 2487 | if(terminal == key) { |
| 2488 | if(arg->expr->marker.flags & EM_INDIRECT) |
| 2489 | return 1; /* This is almost safe indirection */ |
| 2490 | return 2; |
| 2491 | } else if(terminal) { |
| 2492 | /* This might be N-step circular loop. Dive into it. */ |
| 2493 | return asn1c_recurse(arg, terminal, check_is_refer_to, key); |
| 2494 | } |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
| 2498 | /* |
| 2499 | * Check if the possibly inner expression defined recursively. |
| 2500 | */ |
| 2501 | static int |
| 2502 | expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr) { |
| 2503 | asn1p_expr_t *terminal; |
| 2504 | asn1p_expr_t *topmost; |
| 2505 | |
| 2506 | /* If expression is top-level, there's no way it can be recursive. */ |
| 2507 | if(expr->parent_expr == 0) return 0; |
| 2508 | if(expr->expr_type != A1TC_REFERENCE) |
| 2509 | return 0; /* Basic types are never recursive */ |
| 2510 | |
| 2511 | terminal = terminal_structable(arg, expr); |
| 2512 | if(!terminal) return 0; /* Terminal cannot be indirected */ |
| 2513 | |
| 2514 | /* Search for the parent container for the given expression */ |
| 2515 | topmost = expr; |
| 2516 | while(topmost->parent_expr) |
| 2517 | topmost = topmost->parent_expr; |
| 2518 | |
| 2519 | /* Look inside the terminal type if it mentions the parent expression */ |
| 2520 | return asn1c_recurse(arg, terminal, check_is_refer_to, topmost); |
| 2521 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2522 | |
| 2523 | struct canonical_map_element { |
| 2524 | int eidx; |
| 2525 | asn1p_expr_t *expr; |
| 2526 | }; |
| 2527 | static int compar_cameo(const void *ap, const void *bp); |
| 2528 | static arg_t *cameo_arg; |
| 2529 | static int * |
| 2530 | compute_canonical_members_order(arg_t *arg, int el_count) { |
| 2531 | struct canonical_map_element *cmap; |
| 2532 | int *rmap; |
| 2533 | asn1p_expr_t *v; |
| 2534 | int eidx = 0; |
| 2535 | int ext_start = -1; |
| 2536 | int nextmax = -1; |
| 2537 | int already_sorted = 1; |
| 2538 | |
| 2539 | cmap = calloc(el_count, sizeof *cmap); |
| 2540 | assert(cmap); |
| 2541 | |
| 2542 | TQ_FOR(v, &(arg->expr->members), next) { |
| 2543 | if(v->expr_type != A1TC_EXTENSIBLE) { |
| 2544 | cmap[eidx].eidx = eidx; |
| 2545 | cmap[eidx].expr = v; |
| 2546 | eidx++; |
| 2547 | } else if(ext_start == -1) |
| 2548 | ext_start = eidx; |
| 2549 | } |
| 2550 | |
| 2551 | cameo_arg = arg; |
| 2552 | if(ext_start == -1) { |
| 2553 | /* Sort the whole thing */ |
| 2554 | qsort(cmap, el_count, sizeof(*cmap), compar_cameo); |
| 2555 | } else { |
| 2556 | /* Sort root and extensions independently */ |
| 2557 | qsort(cmap, ext_start, sizeof(*cmap), compar_cameo); |
| 2558 | qsort(cmap + ext_start, el_count - ext_start, |
| 2559 | sizeof(*cmap), compar_cameo); |
| 2560 | } |
| 2561 | |
| 2562 | /* move data back to a simpler map */ |
| 2563 | rmap = calloc(el_count, sizeof *rmap); |
| 2564 | assert(rmap); |
| 2565 | for(eidx = 0; eidx < el_count; eidx++) { |
| 2566 | rmap[eidx] = cmap[eidx].eidx; |
| 2567 | if(rmap[eidx] <= nextmax) |
| 2568 | already_sorted = 0; |
| 2569 | else |
| 2570 | nextmax = rmap[eidx]; |
| 2571 | } |
| 2572 | free(cmap); |
| 2573 | |
| 2574 | if(already_sorted) { free(rmap); rmap = 0; } |
| 2575 | return rmap; |
| 2576 | } |
| 2577 | static int compar_cameo(const void *ap, const void *bp) { |
| 2578 | const struct canonical_map_element *a = (const void *)ap; |
| 2579 | const struct canonical_map_element *b = (const void *)bp; |
| 2580 | struct asn1p_type_tag_s atag, btag; |
| 2581 | arg_t *arg = cameo_arg; |
| 2582 | |
| 2583 | if(asn1f_fetch_outmost_tag(arg->asn, a->expr->module, a->expr, |
| 2584 | &atag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE)) |
| 2585 | return 1; |
| 2586 | |
| 2587 | if(asn1f_fetch_outmost_tag(arg->asn, b->expr->module, b->expr, |
| 2588 | &btag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE)) |
| 2589 | return -1; |
| 2590 | |
| 2591 | if(atag.tag_class < btag.tag_class) |
| 2592 | return -1; |
| 2593 | if(atag.tag_class > btag.tag_class) |
| 2594 | return 1; |
| 2595 | if(atag.tag_value < btag.tag_value) |
| 2596 | return -1; |
| 2597 | if(atag.tag_value > btag.tag_value) |
| 2598 | return 1; |
| 2599 | return 0; |
| 2600 | |
| 2601 | } |