vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Don't look into this file. First, because it's a mess, and second, because |
| 3 | * it's a brain of the compiler, and you don't wanna mess with brains do you? ;) |
| 4 | */ |
| 5 | #include "asn1c_internal.h" |
| 6 | #include "asn1c_C.h" |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 7 | #include "asn1c_constraint.h" |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 8 | #include "asn1c_out.h" |
| 9 | #include "asn1c_misc.h" |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 10 | #include <asn1fix_export.h> /* Stuff exported by libasn1fix */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 12 | typedef struct tag2el_s { |
| 13 | struct asn1p_type_tag_s el_tag; |
| 14 | int el_no; |
vlm | c8aeab4 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 15 | int toff_first; |
| 16 | int toff_last; |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 17 | asn1p_expr_t *from_expr; |
| 18 | } tag2el_t; |
| 19 | |
| 20 | static int _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no); |
| 21 | static int _add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no); |
| 22 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 23 | static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg); |
| 24 | static int asn1c_lang_C_type_SET_def(arg_t *arg); |
| 25 | static int asn1c_lang_C_type_CHOICE_def(arg_t *arg); |
| 26 | 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] | 27 | static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p); |
vlm | 79b08d5 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 28 | static int check_if_extensible(asn1p_expr_t *expr); |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 29 | static int expr_better_indirect(arg_t *arg, asn1p_expr_t *expr); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 30 | static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr); |
| 31 | static int emit_member_table(arg_t *arg, asn1p_expr_t *expr); |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 32 | static int emit_tags_vector(arg_t *arg, asn1p_expr_t *expr, int *tags_impl_skip, int choice_mode); |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 33 | static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 35 | enum etd_cp { |
| 36 | ETD_CP_UNKNOWN = -2, |
| 37 | ETD_CP_EITHER = -1, |
| 38 | ETD_CP_PRIMITIVE = 0, |
| 39 | ETD_CP_CONSTRUCTED = 1, |
| 40 | }; |
| 41 | enum etd_spec { |
| 42 | ETD_NO_SPECIFICS, |
| 43 | ETD_HAS_SPECIFICS |
| 44 | }; |
| 45 | static int emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, int tags_count, int tags_impl_skip, int elements_count, enum etd_cp, enum etd_spec); |
| 46 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 47 | #define C99_MODE (!(arg->flags & A1C_NO_C99)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | #define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS) |
vlm | 12c8f69 | 2004-09-06 08:07:29 +0000 | [diff] [blame] | 49 | #define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 50 | |
| 51 | #define PCTX_DEF INDENTED( \ |
| 52 | OUT("\n"); \ |
| 53 | OUT("/* Context for parsing across buffer boundaries */\n"); \ |
| 54 | OUT("ber_dec_ctx_t _ber_dec_ctx;\n")); |
| 55 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 56 | #define DEPENDENCIES do { \ |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 57 | asn1p_expr_t *__m; \ |
| 58 | TQ_FOR(__m, &(expr->members), next) { \ |
| 59 | if((!(__m->expr_type & ASN_CONSTR_MASK) \ |
| 60 | && __m->expr_type > ASN_CONSTR_MASK) \ |
| 61 | || __m->meta_type == AMT_TYPEREF) { \ |
| 62 | GEN_INCLUDE(asn1c_type_name(arg, \ |
| 63 | __m, TNF_INCLUDE)); \ |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 64 | } \ |
| 65 | } \ |
| 66 | if(expr->expr_type == ASN_CONSTR_SET_OF) \ |
| 67 | GEN_INCLUDE("asn_SET_OF"); \ |
| 68 | if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \ |
| 69 | GEN_INCLUDE("asn_SEQUENCE_OF"); \ |
| 70 | } while(0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 71 | |
| 72 | #define MKID(id) asn1c_make_identifier(0, (id), 0) |
| 73 | |
| 74 | int |
| 75 | asn1c_lang_C_type_ENUMERATED(arg_t *arg) { |
| 76 | asn1p_expr_t *expr = arg->expr; |
| 77 | asn1p_expr_t *v; |
| 78 | |
| 79 | REDIR(OT_DEPS); |
| 80 | |
| 81 | OUT("typedef enum %s {\n", MKID(expr->Identifier)); |
| 82 | TQ_FOR(v, &(expr->members), next) { |
| 83 | switch(v->expr_type) { |
| 84 | case A1TC_UNIVERVAL: |
| 85 | OUT("\t%s\t= %lld,\n", |
| 86 | asn1c_make_identifier(0, |
| 87 | expr->Identifier, |
| 88 | v->Identifier, 0), |
| 89 | v->value->value.v_integer); |
| 90 | break; |
| 91 | case A1TC_EXTENSIBLE: |
| 92 | OUT("\t/*\n"); |
| 93 | OUT("\t * Enumeration is extensible\n"); |
| 94 | OUT("\t */\n"); |
| 95 | break; |
| 96 | default: |
| 97 | return -1; |
| 98 | } |
| 99 | } |
| 100 | OUT("} %s_e;\n", MKID(expr->Identifier)); |
| 101 | |
| 102 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | int |
| 107 | asn1c_lang_C_type_INTEGER(arg_t *arg) { |
| 108 | asn1p_expr_t *expr = arg->expr; |
| 109 | asn1p_expr_t *v; |
| 110 | |
| 111 | REDIR(OT_DEPS); |
| 112 | |
| 113 | if(TQ_FIRST(&(expr->members))) { |
| 114 | OUT("typedef enum %s {\n", MKID(expr->Identifier)); |
| 115 | TQ_FOR(v, &(expr->members), next) { |
| 116 | switch(v->expr_type) { |
| 117 | case A1TC_UNIVERVAL: |
| 118 | OUT("\t%s\t= %lld,\n", |
| 119 | asn1c_make_identifier(0, |
| 120 | expr->Identifier, |
| 121 | v->Identifier, 0), |
| 122 | v->value->value.v_integer); |
| 123 | break; |
| 124 | default: |
| 125 | return -1; |
| 126 | } |
| 127 | } |
| 128 | OUT("} %s_e;\n", MKID(expr->Identifier)); |
| 129 | } |
| 130 | |
| 131 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 132 | } |
| 133 | |
| 134 | int |
| 135 | asn1c_lang_C_type_SEQUENCE(arg_t *arg) { |
| 136 | asn1p_expr_t *expr = arg->expr; |
| 137 | asn1p_expr_t *v; |
| 138 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
| 139 | |
| 140 | DEPENDENCIES; |
| 141 | |
| 142 | if(arg->embed) { |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 143 | OUT("struct %s {\n", MKID(expr->Identifier)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | } else { |
| 145 | OUT("typedef struct %s {\n", |
| 146 | MKID(expr->Identifier)); |
| 147 | } |
| 148 | |
| 149 | TQ_FOR(v, &(expr->members), next) { |
| 150 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 151 | if(comp_mode < 3) comp_mode++; |
| 152 | } |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 153 | if(comp_mode == 1 |
| 154 | || expr_better_indirect(arg, v)) |
| 155 | v->marker |= EM_INDIRECT; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 156 | EMBED(v); |
| 157 | } |
| 158 | |
| 159 | PCTX_DEF; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 160 | OUT("} %s%s%s", expr->marker?"*":"", |
| 161 | expr->_anonymous_type ? "" : MKID(expr->Identifier), |
| 162 | arg->embed ? "" : "_t"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 163 | |
| 164 | return asn1c_lang_C_type_SEQUENCE_def(arg); |
| 165 | } |
| 166 | |
| 167 | static int |
| 168 | asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) { |
| 169 | asn1p_expr_t *expr = arg->expr; |
| 170 | asn1p_expr_t *v; |
| 171 | int elements; /* Number of elements */ |
| 172 | int tags_impl_skip = 0; |
| 173 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
| 174 | int ext_start = -1; |
| 175 | int ext_stop = -1; |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 176 | tag2el_t *tag2el = NULL; |
| 177 | int tag2el_count = 0; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 178 | int tags_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 179 | char *p; |
| 180 | |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 181 | /* |
| 182 | * Fetch every inner tag from the tag to elements map. |
| 183 | */ |
| 184 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1)) { |
| 185 | if(tag2el) free(tag2el); |
| 186 | return -1; |
| 187 | } |
| 188 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 189 | GEN_INCLUDE("constr_SEQUENCE"); |
| 190 | if(!arg->embed) |
| 191 | GEN_DECLARE(expr); /* asn1_DEF_xxx */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 193 | REDIR(OT_STAT_DEFS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * Print out the table according to which the parsing is performed. |
| 197 | */ |
| 198 | p = MKID(expr->Identifier); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 199 | OUT("static asn1_TYPE_member_t asn1_MBR_%s[] = {\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | |
| 201 | elements = 0; |
| 202 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
| 203 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 204 | if((++comp_mode) == 1) |
| 205 | ext_start = elements - 1; |
| 206 | else |
| 207 | ext_stop = elements - 1; |
| 208 | continue; |
| 209 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | elements++; |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 211 | emit_member_table(arg, v); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 212 | }); |
| 213 | OUT("};\n"); |
| 214 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 215 | /* |
| 216 | * Print out asn1_DEF_<type>_tags[] vector. |
| 217 | */ |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 218 | tags_count = emit_tags_vector(arg, expr, &tags_impl_skip, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 219 | |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 220 | /* |
| 221 | * Tags to elements map. |
| 222 | */ |
| 223 | emit_tag2member_map(arg, tag2el, tag2el_count); |
| 224 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 225 | p = MKID(expr->Identifier); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 226 | OUT("static asn1_SEQUENCE_specifics_t asn1_DEF_%s_specs = {\n", p); |
| 227 | INDENTED( |
| 228 | OUT("sizeof(struct %s),\n", p); |
| 229 | OUT("offsetof(struct %s, _ber_dec_ctx),\n", p); |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 230 | OUT("asn1_DEF_%s_tag2el,\n", p); |
| 231 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 232 | OUT("%d,\t/* Start extensions */\n", |
| 233 | ext_start); |
| 234 | OUT("%d\t/* Stop extensions */\n", |
| 235 | (ext_stop<ext_start)?elements+1:ext_stop, ext_stop); |
| 236 | ); |
| 237 | OUT("};\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * Emit asn1_DEF_xxx table. |
| 241 | */ |
| 242 | emit_type_DEF(arg, expr, tags_count, tags_impl_skip, elements, |
| 243 | ETD_CP_CONSTRUCTED, ETD_HAS_SPECIFICS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 244 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 245 | REDIR(OT_TYPE_DECLS); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | int |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 251 | asn1c_lang_C_type_SET(arg_t *arg) { |
| 252 | asn1p_expr_t *expr = arg->expr; |
| 253 | asn1p_expr_t *v; |
| 254 | long mcount; |
| 255 | char *id; |
| 256 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
| 257 | |
| 258 | DEPENDENCIES; |
| 259 | |
| 260 | REDIR(OT_DEPS); |
| 261 | |
| 262 | OUT("\n"); |
| 263 | OUT("/*\n"); |
| 264 | OUT(" * Method of determining the components presence\n"); |
| 265 | OUT(" */\n"); |
| 266 | mcount = 0; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 267 | OUT("typedef enum %s_PR {\n", MKID(expr->Identifier)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 268 | TQ_FOR(v, &(expr->members), next) { |
| 269 | if(v->expr_type == A1TC_EXTENSIBLE) continue; |
| 270 | INDENTED( |
| 271 | id = MKID(expr->Identifier); |
| 272 | OUT("%s_PR_", id); |
| 273 | id = MKID(v->Identifier); |
| 274 | OUT("%s,\t/* Member %s is present */\n", |
| 275 | id, id) |
| 276 | ); |
| 277 | mcount++; |
| 278 | } |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 279 | id = MKID(expr->Identifier); |
| 280 | OUT("} %s_PR;\n", id); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 281 | |
| 282 | REDIR(OT_TYPE_DECLS); |
| 283 | |
| 284 | if(arg->embed) { |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 285 | OUT("struct %s {\n", id); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 286 | } else { |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 287 | OUT("typedef struct %s {\n", id); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | TQ_FOR(v, &(expr->members), next) { |
| 291 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 292 | if(comp_mode < 3) comp_mode++; |
| 293 | } |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 294 | if(comp_mode == 1 |
| 295 | || expr_better_indirect(arg, v)) |
| 296 | v->marker |= EM_INDIRECT; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 297 | EMBED(v); |
| 298 | } |
| 299 | |
| 300 | INDENTED( |
| 301 | id = MKID(expr->Identifier); |
| 302 | OUT("\n"); |
| 303 | OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n", |
| 304 | id, id); |
| 305 | OUT("unsigned int _presence_map\n"); |
| 306 | OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount); |
| 307 | ); |
| 308 | |
| 309 | PCTX_DEF; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 310 | OUT("} %s%s%s", expr->marker?"*":"", |
| 311 | expr->_anonymous_type ? "" : MKID(expr->Identifier), |
| 312 | arg->embed ? "" : "_t"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 313 | |
| 314 | return asn1c_lang_C_type_SET_def(arg); |
| 315 | } |
| 316 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 317 | static int |
| 318 | asn1c_lang_C_type_SET_def(arg_t *arg) { |
| 319 | asn1p_expr_t *expr = arg->expr; |
| 320 | asn1p_expr_t *v; |
| 321 | int elements; |
| 322 | int tags_impl_skip = 0; |
| 323 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 324 | tag2el_t *tag2el = NULL; |
| 325 | int tag2el_count = 0; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 326 | int tags_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 327 | char *p; |
| 328 | |
| 329 | /* |
| 330 | * Fetch every inner tag from the tag to elements map. |
| 331 | */ |
| 332 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1)) { |
| 333 | if(tag2el) free(tag2el); |
| 334 | return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 335 | } |
| 336 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 337 | GEN_INCLUDE("constr_SET"); |
| 338 | if(!arg->embed) |
| 339 | GEN_DECLARE(expr); /* asn1_DEF_xxx */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 340 | |
| 341 | REDIR(OT_STAT_DEFS); |
| 342 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 343 | /* |
| 344 | * Print out the table according to which the parsing is performed. |
| 345 | */ |
| 346 | p = MKID(expr->Identifier); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 347 | OUT("static asn1_TYPE_member_t asn1_MBR_%s[] = {\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 348 | |
| 349 | elements = 0; |
| 350 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 351 | if(v->expr_type == A1TC_EXTENSIBLE) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 352 | if(comp_mode < 3) comp_mode++; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 353 | } else { |
| 354 | if(comp_mode == 1 |
| 355 | || expr_better_indirect(arg, v)) |
| 356 | v->marker |= EM_INDIRECT; |
| 357 | elements++; |
| 358 | emit_member_table(arg, v); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 359 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 360 | }); |
| 361 | OUT("};\n"); |
| 362 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 363 | /* |
| 364 | * Print out asn1_DEF_<type>_tags[] vector. |
| 365 | */ |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 366 | tags_count = emit_tags_vector(arg, expr, &tags_impl_skip, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * Tags to elements map. |
| 370 | */ |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 371 | emit_tag2member_map(arg, tag2el, tag2el_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 372 | |
| 373 | /* |
| 374 | * Emit a map of mandatory elements. |
| 375 | */ |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 376 | p = MKID(expr->Identifier); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 377 | OUT("static uint8_t asn1_DEF_%s_mmap", p); |
| 378 | OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements); |
| 379 | OUT(" = {\n", p); |
| 380 | INDENTED( |
| 381 | if(elements) { |
| 382 | int delimit = 0; |
| 383 | int el = 0; |
| 384 | TQ_FOR(v, &(expr->members), next) { |
| 385 | if(v->expr_type == A1TC_EXTENSIBLE) continue; |
| 386 | if(delimit) { |
| 387 | OUT(",\n"); |
| 388 | delimit = 0; |
| 389 | } else if(el) { |
| 390 | OUT(" | "); |
| 391 | } |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 392 | OUT("(%d << %d)", |
| 393 | v->marker?0:1, |
| 394 | 7 - (el % 8)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 395 | if(el && (el % 8) == 0) |
| 396 | delimit = 1; |
| 397 | el++; |
| 398 | } |
| 399 | } else { |
| 400 | OUT("0"); |
| 401 | } |
| 402 | ); |
| 403 | OUT("\n"); |
| 404 | OUT("};\n"); |
| 405 | |
| 406 | OUT("static asn1_SET_specifics_t asn1_DEF_%s_specs = {\n", p); |
| 407 | INDENTED( |
| 408 | OUT("sizeof(struct %s),\n", p); |
| 409 | OUT("offsetof(struct %s, _ber_dec_ctx),\n", p); |
| 410 | OUT("offsetof(struct %s, _presence_map),\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 411 | OUT("asn1_DEF_%s_tag2el,\n", p); |
| 412 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
vlm | 79b08d5 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 413 | OUT("%d,\t/* Whether extensible */\n", |
| 414 | check_if_extensible(expr)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 415 | OUT("(unsigned int *)asn1_DEF_%s_mmap\t/* Mandatory elements map */\n", p); |
| 416 | ); |
| 417 | OUT("};\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 418 | |
| 419 | /* |
| 420 | * Emit asn1_DEF_xxx table. |
| 421 | */ |
| 422 | emit_type_DEF(arg, expr, tags_count, tags_impl_skip, elements, |
| 423 | ETD_CP_CONSTRUCTED, ETD_HAS_SPECIFICS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 424 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 425 | REDIR(OT_TYPE_DECLS); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | int |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 431 | asn1c_lang_C_type_SEx_OF(arg_t *arg) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 432 | asn1p_expr_t *expr = arg->expr; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 433 | asn1p_expr_t *memb; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 434 | |
| 435 | DEPENDENCIES; |
| 436 | |
| 437 | if(arg->embed) { |
| 438 | OUT("struct %s {\n", MKID(expr->Identifier)); |
| 439 | } else { |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 440 | OUT("typedef struct %s {\n", MKID(expr->Identifier)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 441 | } |
| 442 | |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 443 | memb = TQ_FIRST(&expr->members); |
| 444 | |
| 445 | INDENT(+1); |
| 446 | OUT("A_%s_OF(", |
| 447 | (arg->expr->expr_type == ASN_CONSTR_SET_OF) |
| 448 | ? "SET" : "SEQUENCE"); |
| 449 | if(memb->expr_type & ASN_CONSTR_MASK) { |
| 450 | arg_t tmp; |
| 451 | asn1p_expr_t tmp_memb; |
| 452 | arg->embed++; |
| 453 | tmp = *arg; |
| 454 | tmp.expr = &tmp_memb; |
| 455 | tmp_memb = *memb; |
| 456 | tmp_memb._anonymous_type = 1; |
| 457 | tmp_memb.Identifier = strdup( |
| 458 | asn1c_make_identifier(0, |
| 459 | expr->Identifier, "member", 0)); |
| 460 | assert(tmp_memb.Identifier); |
| 461 | tmp.default_cb(&tmp); |
| 462 | free(tmp_memb.Identifier); |
| 463 | arg->embed--; |
| 464 | assert(arg->target->target == OT_TYPE_DECLS); |
| 465 | } else { |
| 466 | OUT("%s", asn1c_type_name(arg, memb, TNF_RSAFE)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 467 | } |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 468 | OUT(") list;\n"); |
| 469 | INDENT(-1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 470 | |
| 471 | PCTX_DEF; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 472 | OUT("} %s%s%s", expr->marker?"*":"", |
| 473 | expr->_anonymous_type ? "" : MKID(expr->Identifier), |
| 474 | arg->embed ? "" : "_t"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 475 | |
| 476 | /* |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 477 | * SET OF/SEQUENCE OF definition |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 478 | */ |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 479 | return asn1c_lang_C_type_SEx_OF_def(arg, |
| 480 | (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static int |
| 484 | asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) { |
| 485 | asn1p_expr_t *expr = arg->expr; |
| 486 | asn1p_expr_t *v; |
| 487 | int tags_impl_skip = 0; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 488 | int tags_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 489 | char *p; |
| 490 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 491 | /* |
| 492 | * Print out the table according to which the parsing is performed. |
| 493 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 494 | if(seq_of) { |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 495 | GEN_INCLUDE("constr_SEQUENCE_OF"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 496 | } else { |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 497 | GEN_INCLUDE("constr_SET_OF"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 498 | } |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 499 | if(!arg->embed) |
| 500 | GEN_DECLARE(expr); /* asn1_DEF_xxx */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 501 | |
| 502 | REDIR(OT_STAT_DEFS); |
| 503 | |
| 504 | /* |
| 505 | * Print out the table according to which the parsing is performed. |
| 506 | */ |
| 507 | p = MKID(expr->Identifier); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 508 | OUT("static asn1_TYPE_member_t asn1_MBR_%s[] = {\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 509 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 510 | INDENTED( |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 511 | v = TQ_FIRST(&(expr->members)); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 512 | emit_member_table(arg, v); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 513 | ); |
| 514 | OUT("};\n"); |
| 515 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 516 | /* |
| 517 | * Print out asn1_DEF_<type>_tags[] vector. |
| 518 | */ |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 519 | tags_count = emit_tags_vector(arg, expr, &tags_impl_skip, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 520 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 521 | p = MKID(expr->Identifier); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 522 | OUT("static asn1_SET_OF_specifics_t asn1_DEF_%s_specs = {\n", p); |
| 523 | INDENTED( |
| 524 | OUT("sizeof(struct %s),\n", p); |
| 525 | OUT("offsetof(struct %s, _ber_dec_ctx),\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 526 | ); |
| 527 | OUT("};\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 528 | |
| 529 | /* |
| 530 | * Emit asn1_DEF_xxx table. |
| 531 | */ |
| 532 | emit_type_DEF(arg, expr, tags_count, tags_impl_skip, 1, |
| 533 | ETD_CP_CONSTRUCTED, ETD_HAS_SPECIFICS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 534 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 535 | REDIR(OT_TYPE_DECLS); |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | int |
| 541 | asn1c_lang_C_type_CHOICE(arg_t *arg) { |
| 542 | asn1p_expr_t *expr = arg->expr; |
| 543 | asn1p_expr_t *v; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 544 | char *id; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 545 | |
| 546 | DEPENDENCIES; |
| 547 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 548 | REDIR(OT_DEPS); |
| 549 | |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 550 | id = MKID(expr->Identifier); |
| 551 | OUT("typedef enum %s_PR {\n", id); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 552 | INDENTED( |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 553 | OUT("%s_PR_NOTHING,\t" |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 554 | "/* No components present */\n", id); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 555 | TQ_FOR(v, &(expr->members), next) { |
| 556 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 557 | OUT("/* Extensions may appear below */\n"); |
| 558 | continue; |
| 559 | } |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 560 | id = MKID(expr->Identifier); |
| 561 | OUT("%s_PR_", id); |
| 562 | id = MKID(v->Identifier); |
| 563 | OUT("%s,\n", id, id); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 564 | } |
| 565 | ); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 566 | id = MKID(expr->Identifier); |
| 567 | OUT("} %s_PR;\n", id); |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 568 | |
| 569 | REDIR(OT_TYPE_DECLS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 570 | |
| 571 | if(arg->embed) { |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 572 | OUT("struct %s {\n", id); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 573 | } else { |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 574 | OUT("typedef struct %s {\n", id); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | INDENTED( |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 578 | OUT("%s_PR present;\n", id); |
| 579 | OUT("union {\n", id); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 580 | TQ_FOR(v, &(expr->members), next) { |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 581 | if(expr_better_indirect(arg, v)) |
| 582 | v->marker |= EM_INDIRECT; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 583 | EMBED(v); |
| 584 | } |
| 585 | if(UNNAMED_UNIONS) OUT("};\n"); |
| 586 | else OUT("} choice;\n"); |
| 587 | ); |
| 588 | |
| 589 | PCTX_DEF; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 590 | OUT("} %s%s%s", expr->marker?"*":"", |
| 591 | expr->_anonymous_type ? "" : MKID(expr->Identifier), |
| 592 | arg->embed ? "" : "_t"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 593 | |
| 594 | return asn1c_lang_C_type_CHOICE_def(arg); |
| 595 | } |
| 596 | |
| 597 | static int |
| 598 | asn1c_lang_C_type_CHOICE_def(arg_t *arg) { |
| 599 | asn1p_expr_t *expr = arg->expr; |
| 600 | asn1p_expr_t *v; |
| 601 | int elements; /* Number of elements */ |
| 602 | int tags_impl_skip = 0; |
| 603 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 604 | tag2el_t *tag2el = NULL; |
| 605 | int tag2el_count = 0; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 606 | int tags_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 607 | char *p; |
| 608 | |
| 609 | /* |
| 610 | * Fetch every inner tag from the tag to elements map. |
| 611 | */ |
| 612 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1)) { |
| 613 | if(tag2el) free(tag2el); |
| 614 | return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 615 | } |
| 616 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 617 | GEN_INCLUDE("constr_CHOICE"); |
| 618 | if(!arg->embed) |
| 619 | GEN_DECLARE(expr); /* asn1_DEF_xxx */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 620 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 621 | REDIR(OT_STAT_DEFS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | * Print out the table according to which the parsing is performed. |
| 625 | */ |
| 626 | p = MKID(expr->Identifier); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 627 | OUT("static asn1_TYPE_member_t asn1_MBR_%s[] = {\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 628 | |
| 629 | elements = 0; |
| 630 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 631 | if(v->expr_type == A1TC_EXTENSIBLE) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 632 | if(comp_mode < 3) comp_mode++; |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 633 | } else { |
| 634 | if(comp_mode == 1 |
| 635 | || expr_better_indirect(arg, v)) |
| 636 | v->marker |= EM_INDIRECT; |
| 637 | elements++; |
| 638 | emit_member_table(arg, v); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 639 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 640 | }); |
| 641 | OUT("};\n"); |
| 642 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 643 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 644 | if(arg->embed) { |
| 645 | /* |
| 646 | * Our parent structure has already taken this into account. |
| 647 | */ |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 648 | tags_count = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 649 | } else { |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 650 | tags_count = emit_tags_vector(arg, expr, &tags_impl_skip, 1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 651 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 652 | |
| 653 | /* |
| 654 | * Tags to elements map. |
| 655 | */ |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 656 | emit_tag2member_map(arg, tag2el, tag2el_count); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 657 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 658 | p = MKID(expr->Identifier); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 659 | OUT("static asn1_CHOICE_specifics_t asn1_DEF_%s_specs = {\n", p); |
| 660 | INDENTED( |
| 661 | OUT("sizeof(struct %s),\n", p); |
| 662 | OUT("offsetof(struct %s, _ber_dec_ctx),\n", p); |
| 663 | OUT("offsetof(struct %s, present),\n", p); |
| 664 | OUT("sizeof(((struct %s *)0)->present),\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 665 | OUT("asn1_DEF_%s_tag2el,\n", p); |
| 666 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
vlm | 79b08d5 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 667 | OUT("%d\t/* Whether extensible */\n", |
| 668 | check_if_extensible(expr)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 669 | ); |
| 670 | OUT("};\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 671 | |
| 672 | /* |
| 673 | * Emit asn1_DEF_xxx table. |
| 674 | */ |
| 675 | emit_type_DEF(arg, expr, tags_count, tags_impl_skip, elements, |
| 676 | ETD_CP_CONSTRUCTED /*either?!*/, ETD_HAS_SPECIFICS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 677 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 678 | REDIR(OT_TYPE_DECLS); |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | int |
| 684 | asn1c_lang_C_type_REFERENCE(arg_t *arg) { |
| 685 | asn1p_ref_t *ref; |
| 686 | |
| 687 | ref = arg->expr->reference; |
| 688 | if(ref->components[ref->comp_count-1].name[0] == '&') { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 689 | asn1p_expr_t *extract; |
| 690 | arg_t tmp; |
| 691 | int ret; |
| 692 | |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 693 | extract = asn1f_class_access_ex(arg->asn, arg->mod, |
| 694 | arg->expr, ref); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 695 | if(extract == NULL) |
| 696 | return -1; |
| 697 | |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 698 | extract = asn1p_expr_clone(extract, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 699 | if(extract) { |
| 700 | if(extract->Identifier) |
| 701 | free(extract->Identifier); |
| 702 | extract->Identifier = strdup(arg->expr->Identifier); |
| 703 | if(extract->Identifier == NULL) { |
| 704 | asn1p_expr_free(extract); |
| 705 | return -1; |
| 706 | } |
| 707 | } else { |
| 708 | return -1; |
| 709 | } |
| 710 | |
| 711 | tmp = *arg; |
| 712 | tmp.asn = arg->asn; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 713 | tmp.mod = extract->module; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 714 | tmp.expr = extract; |
| 715 | |
| 716 | ret = arg->default_cb(&tmp); |
| 717 | |
| 718 | asn1p_expr_free(extract); |
| 719 | |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | |
| 724 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 725 | } |
| 726 | |
| 727 | int |
| 728 | asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) { |
| 729 | asn1p_expr_t *expr = arg->expr; |
| 730 | int tags_impl_skip = 0; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 731 | int tags_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 732 | char *p; |
| 733 | |
| 734 | if(arg->embed) { |
| 735 | REDIR(OT_TYPE_DECLS); |
| 736 | |
| 737 | OUT("%s\t", asn1c_type_name(arg, arg->expr, |
| 738 | expr->marker?TNF_RSAFE:TNF_CTYPE)); |
| 739 | OUT("%s", expr->marker?"*":" "); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 740 | OUT("%s", MKID(expr->Identifier)); |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 741 | if((expr->marker & EM_DEFAULT) == EM_DEFAULT) |
| 742 | OUT("\t/* DEFAULT */"); |
| 743 | else if((expr->marker & EM_OPTIONAL) == EM_OPTIONAL) |
| 744 | OUT("\t/* OPTIONAL */"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 745 | |
| 746 | REDIR(OT_TYPE_DECLS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 747 | return 0; |
| 748 | } |
| 749 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 750 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 751 | GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 752 | |
| 753 | REDIR(OT_TYPE_DECLS); |
| 754 | |
| 755 | OUT("typedef %s\t", asn1c_type_name(arg, arg->expr, TNF_CTYPE)); |
| 756 | OUT("%s", expr->marker?"*":" "); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 757 | OUT("%s_t", MKID(expr->Identifier)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 758 | |
| 759 | REDIR(OT_STAT_DEFS); |
| 760 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 761 | /* |
| 762 | * Print out asn1_DEF_<type>_tags[] vector. |
| 763 | */ |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 764 | tags_count = emit_tags_vector(arg, expr, &tags_impl_skip, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 765 | |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 766 | emit_type_DEF(arg, expr, tags_count, tags_impl_skip, 0, |
| 767 | ETD_CP_UNKNOWN, ETD_NO_SPECIFICS); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 768 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 769 | REDIR(OT_CODE); |
| 770 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 771 | /* |
| 772 | * Constraint checking. |
| 773 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 774 | p = MKID(expr->Identifier); |
| 775 | OUT("int\n"); |
| 776 | OUT("%s_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,\n", p); |
| 777 | INDENTED( |
| 778 | OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n"); |
| 779 | OUT("\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 780 | if(asn1c_emit_constraint_checking_code(arg) == 1) { |
| 781 | if(0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 782 | OUT("/* Check the constraints of the underlying type */\n"); |
| 783 | OUT("return asn1_DEF_%s.check_constraints\n", |
| 784 | asn1c_type_name(arg, expr, TNF_SAFE)); |
| 785 | OUT("\t(td, sptr, app_errlog, app_key);\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 786 | } else { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 787 | OUT("/* Make the underlying type checker permanent */\n"); |
| 788 | OUT("td->check_constraints = asn1_DEF_%s.check_constraints;\n", |
| 789 | asn1c_type_name(arg, expr, TNF_SAFE)); |
| 790 | OUT("return td->check_constraints\n"); |
| 791 | OUT("\t(td, sptr, app_errlog, app_key);\n"); |
vlm | 1d03669 | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 792 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 793 | } |
| 794 | ); |
| 795 | OUT("}\n"); |
| 796 | OUT("\n"); |
| 797 | |
| 798 | /* |
| 799 | * Emit suicidal functions. |
| 800 | */ |
| 801 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 802 | /* |
| 803 | * This function replaces certain fields from the definition |
| 804 | * of a type with the corresponding fields from the basic type |
| 805 | * (from which the current type is inherited). |
| 806 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 807 | OUT("/*\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 808 | OUT(" * This type is implemented using %s,\n", |
| 809 | asn1c_type_name(arg, expr, TNF_SAFE)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 810 | OUT(" * so adjust the DEF appropriately.\n"); |
| 811 | OUT(" */\n"); |
| 812 | OUT("static void\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 813 | p = MKID(expr->Identifier); |
| 814 | OUT("%s_inherit_TYPE_descriptor(asn1_TYPE_descriptor_t *td) {\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 815 | INDENT(+1); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 816 | { |
| 817 | char *type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
| 818 | OUT("td->ber_decoder = asn1_DEF_%s.ber_decoder;\n", type_name); |
| 819 | OUT("td->der_encoder = asn1_DEF_%s.der_encoder;\n", type_name); |
| 820 | OUT("td->free_struct = asn1_DEF_%s.free_struct;\n", type_name); |
| 821 | OUT("td->print_struct = asn1_DEF_%s.print_struct;\n", type_name); |
| 822 | OUT("td->last_tag_form = asn1_DEF_%s.last_tag_form;\n", type_name); |
| 823 | OUT("td->elements = asn1_DEF_%s.elements;\n", type_name); |
| 824 | OUT("td->elements_count = asn1_DEF_%s.elements_count;\n", type_name); |
| 825 | OUT("td->specifics = asn1_DEF_%s.specifics;\n", type_name); |
| 826 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 827 | INDENT(-1); |
| 828 | OUT("}\n"); |
| 829 | OUT("\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 830 | |
| 831 | p = MKID(expr->Identifier); |
| 832 | OUT("ber_dec_rval_t\n"); |
| 833 | OUT("%s_decode_ber(asn1_TYPE_descriptor_t *td,\n", p); |
| 834 | INDENTED( |
| 835 | OUT("\tvoid **structure, void *bufptr, size_t size, int tag_mode) {\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 836 | OUT("%s_inherit_TYPE_descriptor(td);\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 837 | OUT("return td->ber_decoder(td, structure,\n"); |
| 838 | OUT("\tbufptr, size, tag_mode);\n"); |
| 839 | ); |
| 840 | OUT("}\n"); |
| 841 | OUT("\n"); |
| 842 | |
| 843 | p = MKID(expr->Identifier); |
| 844 | OUT("der_enc_rval_t\n"); |
| 845 | OUT("%s_encode_der(asn1_TYPE_descriptor_t *td,\n", p); |
| 846 | INDENTED( |
| 847 | OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n"); |
| 848 | OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 849 | OUT("%s_inherit_TYPE_descriptor(td);\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 850 | OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n"); |
| 851 | ); |
| 852 | OUT("}\n"); |
| 853 | OUT("\n"); |
| 854 | |
| 855 | p = MKID(expr->Identifier); |
| 856 | OUT("int\n"); |
| 857 | OUT("%s_print(asn1_TYPE_descriptor_t *td, const void *struct_ptr,\n", p); |
| 858 | INDENTED( |
| 859 | OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 860 | OUT("%s_inherit_TYPE_descriptor(td);\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 861 | OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n"); |
| 862 | ); |
| 863 | OUT("}\n"); |
| 864 | OUT("\n"); |
| 865 | |
| 866 | p = MKID(expr->Identifier); |
| 867 | OUT("void\n"); |
| 868 | OUT("%s_free(asn1_TYPE_descriptor_t *td,\n", p); |
| 869 | INDENTED( |
| 870 | OUT("\tvoid *struct_ptr, int contents_only) {\n"); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 871 | OUT("%s_inherit_TYPE_descriptor(td);\n", p); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 872 | OUT("td->free_struct(td, struct_ptr, contents_only);\n"); |
| 873 | ); |
| 874 | OUT("}\n"); |
| 875 | OUT("\n"); |
| 876 | |
| 877 | REDIR(OT_FUNC_DECLS); |
| 878 | |
| 879 | p = MKID(expr->Identifier); |
vlm | 12c8f69 | 2004-09-06 08:07:29 +0000 | [diff] [blame] | 880 | if(HIDE_INNER_DEFS) OUT("/* "); |
| 881 | OUT("extern asn1_TYPE_descriptor_t asn1_DEF_%s;", p); |
| 882 | if(HIDE_INNER_DEFS) OUT(" // (Use -fall-defs-global to expose) */"); |
| 883 | OUT("\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 884 | OUT("asn_constr_check_f %s_constraint;\n", p); |
| 885 | OUT("ber_type_decoder_f %s_decode_ber;\n", p); |
| 886 | OUT("der_type_encoder_f %s_encode_der;\n", p); |
| 887 | OUT("asn_struct_print_f %s_print;\n", p); |
| 888 | OUT("asn_struct_free_f %s_free;\n", p); |
| 889 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 890 | REDIR(OT_TYPE_DECLS); |
| 891 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | int |
| 896 | asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) { |
| 897 | |
| 898 | OUT("/*\n"); |
| 899 | OUT(" * This type is extensible,\n"); |
| 900 | OUT(" * possible extensions are below.\n"); |
| 901 | OUT(" */\n"); |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
vlm | 79b08d5 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 906 | static int check_if_extensible(asn1p_expr_t *expr) { |
| 907 | asn1p_expr_t *v; |
| 908 | TQ_FOR(v, &(expr->members), next) { |
| 909 | if(v->expr_type == A1TC_EXTENSIBLE) return 1; |
| 910 | } |
| 911 | return 0; |
| 912 | } |
| 913 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 914 | static int |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 915 | _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 916 | |
| 917 | OUT("("); |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 918 | switch(tag->tag_class) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 919 | case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break; |
| 920 | case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break; |
| 921 | case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break; |
| 922 | case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break; |
| 923 | case TC_NOCLASS: |
| 924 | break; |
| 925 | } |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 926 | OUT(" | (%lld << 2))", tag->tag_value); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 931 | |
| 932 | static int |
| 933 | _tag2el_cmp(const void *ap, const void *bp) { |
| 934 | const tag2el_t *a = ap; |
| 935 | const tag2el_t *b = bp; |
| 936 | const struct asn1p_type_tag_s *ta = &a->el_tag; |
| 937 | const struct asn1p_type_tag_s *tb = &b->el_tag; |
| 938 | |
| 939 | if(ta->tag_class == tb->tag_class) { |
| 940 | if(ta->tag_value == tb->tag_value) { |
| 941 | /* |
| 942 | * Sort by their respective positions. |
| 943 | */ |
| 944 | if(a->el_no < b->el_no) |
| 945 | return -1; |
| 946 | else if(a->el_no > b->el_no) |
| 947 | return 1; |
| 948 | return 0; |
| 949 | } else if(ta->tag_value < tb->tag_value) |
| 950 | return -1; |
| 951 | else |
| 952 | return 1; |
| 953 | } else if(ta->tag_class < tb->tag_class) { |
| 954 | return -1; |
| 955 | } else { |
| 956 | return 1; |
| 957 | } |
| 958 | } |
| 959 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 960 | /* |
| 961 | * For constructed types, number of external tags may be greater than |
| 962 | * number of elements in the type because of CHOICE type. |
| 963 | * T ::= SET { -- Three possible tags: |
| 964 | * a INTEGER, -- One tag is here... |
| 965 | * b Choice1 -- ... and two more tags are there. |
| 966 | * } |
| 967 | * Choice1 ::= CHOICE { |
| 968 | * s1 IA5String, |
| 969 | * s2 ObjectDescriptor |
| 970 | * } |
| 971 | */ |
| 972 | static int |
| 973 | _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no) { |
| 974 | asn1p_expr_t *expr = arg->expr; |
| 975 | arg_t tmparg = *arg; |
| 976 | asn1p_expr_t *v; |
| 977 | int element = 0; |
| 978 | |
| 979 | TQ_FOR(v, &(expr->members), next) { |
| 980 | if(v->expr_type == A1TC_EXTENSIBLE) |
| 981 | continue; |
| 982 | |
| 983 | tmparg.expr = v; |
| 984 | |
| 985 | if(_add_tag2el_member(&tmparg, tag2el, count, |
| 986 | (el_no==-1)?element:el_no)) { |
| 987 | return -1; |
| 988 | } |
| 989 | |
| 990 | element++; |
| 991 | } |
| 992 | |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 993 | /* |
vlm | c8aeab4 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 994 | * Sort the map according to canonical order of their tags |
| 995 | * and element numbers. |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 996 | */ |
| 997 | qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp); |
| 998 | |
vlm | c8aeab4 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 999 | /* |
| 1000 | * Initialize .toff_{first|last} members. |
| 1001 | */ |
| 1002 | if(*count) { |
| 1003 | struct asn1p_type_tag_s *cur_tag = 0; |
| 1004 | tag2el_t *cur = *tag2el; |
| 1005 | tag2el_t *end = cur + *count; |
| 1006 | int occur, i; |
| 1007 | for(occur = 0; cur < end; cur++) { |
| 1008 | if(cur_tag == 0 |
| 1009 | || cur_tag->tag_value != cur->el_tag.tag_value |
| 1010 | || cur_tag->tag_class != cur->el_tag.tag_class) { |
| 1011 | cur_tag = &cur->el_tag; |
| 1012 | occur = 0; |
| 1013 | } else { |
| 1014 | occur++; |
| 1015 | } |
| 1016 | cur->toff_first = -occur; |
| 1017 | for(i = 0; i >= -occur; i--) |
| 1018 | cur[i].toff_last = -i; |
| 1019 | } |
| 1020 | } |
| 1021 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | static int |
| 1026 | _add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no) { |
| 1027 | struct asn1p_type_tag_s tag; |
| 1028 | int ret; |
| 1029 | |
| 1030 | assert(el_no >= 0); |
| 1031 | |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1032 | ret = asn1f_fetch_outmost_tag(arg->asn, arg->mod, arg->expr, &tag, 1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1033 | if(ret == 0) { |
vlm | e2070ea | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1034 | tag2el_t *te; |
| 1035 | int new_count = (*count) + 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1036 | void *p; |
vlm | e2070ea | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1037 | |
vlm | 2de0e79 | 2004-09-05 10:42:33 +0000 | [diff] [blame] | 1038 | if(tag.tag_value == -1) { |
| 1039 | /* |
| 1040 | * This is an untagged ANY type, |
| 1041 | * proceed without adding a tag |
| 1042 | */ |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
vlm | e2070ea | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1046 | p = realloc(*tag2el, new_count * sizeof(tag2el_t)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1047 | if(p) *tag2el = p; |
| 1048 | else return -1; |
| 1049 | |
| 1050 | DEBUG("Found tag for %s: %ld", |
| 1051 | arg->expr->Identifier, |
| 1052 | (long)tag.tag_value); |
| 1053 | |
vlm | e2070ea | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1054 | te = &((*tag2el)[*count]); |
| 1055 | te->el_tag = tag; |
| 1056 | te->el_no = el_no; |
| 1057 | te->from_expr = arg->expr; |
| 1058 | *count = new_count; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1059 | return 0; |
| 1060 | } |
| 1061 | |
| 1062 | DEBUG("Searching tag in complex expression %s:%x at line %d", |
| 1063 | arg->expr->Identifier, |
| 1064 | arg->expr->expr_type, |
| 1065 | arg->expr->_lineno); |
| 1066 | |
| 1067 | /* |
| 1068 | * Iterate over members of CHOICE type. |
| 1069 | */ |
| 1070 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE) { |
| 1071 | return _fill_tag2el_map(arg, tag2el, count, el_no); |
| 1072 | } |
| 1073 | |
| 1074 | if(arg->expr->expr_type == A1TC_REFERENCE) { |
| 1075 | arg_t tmp = *arg; |
| 1076 | asn1p_expr_t *expr; |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1077 | expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1078 | arg->expr->reference); |
| 1079 | if(expr) { |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1080 | tmp.mod = expr->module; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1081 | tmp.expr = expr; |
| 1082 | return _add_tag2el_member(&tmp, tag2el, count, el_no); |
| 1083 | } else { |
| 1084 | FATAL("Cannot dereference %s at line %d", |
| 1085 | arg->expr->Identifier, |
| 1086 | arg->expr->_lineno); |
| 1087 | return -1; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | DEBUG("No tag for %s at line %d", |
| 1092 | arg->expr->Identifier, |
| 1093 | arg->expr->_lineno); |
| 1094 | |
| 1095 | return -1; |
| 1096 | } |
| 1097 | |
| 1098 | static int |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1099 | emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count) { |
| 1100 | asn1p_expr_t *expr = arg->expr; |
| 1101 | |
| 1102 | OUT("static asn1_TYPE_tag2member_t asn1_DEF_%s_tag2el[] = {\n", |
| 1103 | MKID(expr->Identifier)); |
| 1104 | if(tag2el_count) { |
| 1105 | int i; |
| 1106 | for(i = 0; i < tag2el_count; i++) { |
| 1107 | OUT(" { "); |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1108 | _print_tag(arg, &tag2el[i].el_tag); |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1109 | OUT(", "); |
vlm | c8aeab4 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 1110 | OUT("%d, ", tag2el[i].el_no); |
| 1111 | OUT("%d, ", tag2el[i].toff_first); |
| 1112 | OUT("%d ", tag2el[i].toff_last); |
vlm | 4e03ce2 | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1113 | OUT("}, /* %s at %d */\n", |
| 1114 | tag2el[i].from_expr->Identifier, |
| 1115 | tag2el[i].from_expr->_lineno |
| 1116 | ); |
| 1117 | } |
| 1118 | } |
| 1119 | OUT("};\n"); |
| 1120 | |
| 1121 | return 0;; |
| 1122 | } |
| 1123 | |
| 1124 | static int |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1125 | emit_tags_vector(arg_t *arg, asn1p_expr_t *expr, int *tags_impl_skip, int choice_mode) { |
| 1126 | int tags_count = 0; |
| 1127 | int save_target = arg->target->target; |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1128 | char *p; |
| 1129 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1130 | if(save_target != OT_IGNORE) { |
| 1131 | int save_impl_skip = *tags_impl_skip; |
| 1132 | REDIR(OT_IGNORE); |
| 1133 | tags_count = emit_tags_vector(arg, expr, |
| 1134 | tags_impl_skip, choice_mode); |
| 1135 | REDIR(save_target); |
| 1136 | if(tags_count) { |
| 1137 | *tags_impl_skip = save_impl_skip; |
| 1138 | tags_count = 0; |
| 1139 | } else { |
| 1140 | return 0; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1145 | p = MKID(expr->Identifier); |
| 1146 | OUT("static ber_tlv_tag_t asn1_DEF_%s_tags[] = {\n", p); |
vlm | fb41dbb | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 1147 | INDENT(+1); |
| 1148 | if(expr->tag.tag_class) { |
| 1149 | tags_count++; |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1150 | _print_tag(arg, &expr->tag); |
vlm | fb41dbb | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 1151 | if(expr->tag.tag_mode != TM_EXPLICIT) |
| 1152 | (*tags_impl_skip)++; |
| 1153 | } else { |
| 1154 | if(!choice_mode) |
| 1155 | (*tags_impl_skip)++; |
| 1156 | } |
| 1157 | if(!choice_mode) { |
| 1158 | if(!expr->tag.tag_class |
| 1159 | || (expr->meta_type == AMT_TYPE |
| 1160 | && expr->tag.tag_mode == TM_EXPLICIT)) { |
| 1161 | struct asn1p_type_tag_s tag; |
| 1162 | if(expr->tag.tag_class) |
| 1163 | OUT(",\n"); |
| 1164 | tag.tag_class = TC_UNIVERSAL; |
| 1165 | tag.tag_mode = TM_IMPLICIT; |
| 1166 | tag.tag_value = expr_type2uclass_value[expr->expr_type]; |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1167 | _print_tag(arg, &tag); |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1168 | tags_count++; |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1169 | } |
vlm | fb41dbb | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 1170 | } |
| 1171 | OUT("\n"); |
| 1172 | INDENT(-1); |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1173 | OUT("};\n"); |
| 1174 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1175 | return tags_count; |
vlm | 4a3f582 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1176 | } |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1177 | |
| 1178 | static int |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1179 | expr_elements_count(arg_t *arg, asn1p_expr_t *expr) { |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1180 | asn1p_expr_t *topmost_parent; |
| 1181 | asn1p_expr_t *v; |
| 1182 | int elements = 0; |
| 1183 | |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1184 | topmost_parent = asn1f_find_terminal_type_ex(arg->asn, arg->mod, expr); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1185 | if(!topmost_parent) return 0; |
| 1186 | |
| 1187 | if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)) |
| 1188 | return 0; |
| 1189 | |
| 1190 | TQ_FOR(v, &(topmost_parent->members), next) { |
| 1191 | if(v->expr_type != A1TC_EXTENSIBLE) |
| 1192 | elements++; |
| 1193 | } |
| 1194 | |
| 1195 | return elements; |
| 1196 | } |
| 1197 | |
| 1198 | static int |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1199 | emit_member_table(arg_t *arg, asn1p_expr_t *expr) { |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1200 | static int global_memb_unique; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1201 | int save_target; |
| 1202 | arg_t tmp_arg; |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame^] | 1203 | struct asn1p_type_tag_s outmost_tag_s; |
| 1204 | struct asn1p_type_tag_s *outmost_tag; |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1205 | char *p; |
| 1206 | |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame^] | 1207 | if(asn1f_fetch_outmost_tag(arg->asn, |
| 1208 | expr->module, expr, &outmost_tag_s, 1)) { |
| 1209 | outmost_tag = 0; |
| 1210 | } else { |
| 1211 | outmost_tag = &outmost_tag_s; |
| 1212 | } |
| 1213 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1214 | OUT("{ "); |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame^] | 1215 | |
| 1216 | if(outmost_tag && outmost_tag->tag_value == -1) |
| 1217 | OUT("ATF_OPEN_TYPE | "); |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1218 | OUT("%s, ", expr->marker?"ATF_POINTER":"ATF_NOFLAGS"); |
| 1219 | if((expr->marker & EM_OPTIONAL) == EM_OPTIONAL) { |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1220 | asn1p_expr_t *tv; |
| 1221 | int opts = 0; |
| 1222 | for(tv = expr; tv && tv->marker; |
| 1223 | tv = TQ_NEXT(tv, next), opts++) { |
| 1224 | if(tv->expr_type == A1TC_EXTENSIBLE) |
| 1225 | opts--; |
| 1226 | } |
| 1227 | OUT("%d, ", opts); |
| 1228 | } else { |
| 1229 | OUT("0, "); |
| 1230 | } |
| 1231 | if(expr->Identifier) { |
| 1232 | OUT("offsetof(struct %s, ", MKID(arg->expr->Identifier)); |
| 1233 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE |
| 1234 | && (!UNNAMED_UNIONS)) OUT("choice."); |
| 1235 | OUT("%s),\n", MKID(expr->Identifier)); |
| 1236 | } else { |
| 1237 | assert(arg->expr->expr_type == ASN_CONSTR_SET_OF |
| 1238 | || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF); |
| 1239 | OUT("0,\n"); |
| 1240 | } |
| 1241 | INDENT(+1); |
| 1242 | if(C99_MODE) OUT(".tag = "); |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame^] | 1243 | if(outmost_tag) { |
| 1244 | if(outmost_tag->tag_value == -1) |
| 1245 | OUT("-1 /* Ambiguous tag (ANY?) */"); |
| 1246 | else |
| 1247 | _print_tag(arg, outmost_tag); |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1248 | } else { |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame^] | 1249 | OUT("-1 /* Ambiguous tag (CHOICE?) */"); |
vlm | a8a86cc | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1250 | } |
vlm | 060fe2a | 2004-09-10 09:37:12 +0000 | [diff] [blame^] | 1251 | |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1252 | OUT(",\n"); |
| 1253 | if(C99_MODE) OUT(".tag_mode = "); |
| 1254 | if(expr->tag.tag_class) { |
| 1255 | if(expr->tag.tag_mode == TM_IMPLICIT) |
| 1256 | OUT("-1,\t/* IMPLICIT tag at current level */\n"); |
| 1257 | else |
| 1258 | OUT("+1,\t/* EXPLICIT tag at current level */\n"); |
| 1259 | } else { |
| 1260 | OUT("0,\n"); |
| 1261 | } |
| 1262 | if(C99_MODE) OUT(".type = "); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1263 | if((expr->expr_type & ASN_CONSTR_MASK) |
| 1264 | && (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF |
| 1265 | || arg->expr->expr_type == ASN_CONSTR_SET_OF)) { |
| 1266 | OUT("(void *)&asn1_DEF_%s_member,\n", |
| 1267 | MKID(arg->expr->Identifier)); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1268 | } else if(expr->expr_type & ASN_CONSTR_MASK) { |
| 1269 | OUT("(void *)&asn1_DEF_%s,\n", |
| 1270 | MKID(expr->Identifier)); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1271 | } else { |
| 1272 | OUT("(void *)&asn1_DEF_%s,\n", |
| 1273 | asn1c_type_name(arg, expr, TNF_SAFE)); |
| 1274 | } |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1275 | if(C99_MODE) OUT(".memb_constraints = "); |
| 1276 | if(expr->constraints) { |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1277 | char *id = MKID(expr->Identifier); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1278 | if(!expr->Identifier) |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1279 | id = asn1c_type_name(arg, expr, TNF_SAFE); |
| 1280 | OUT("memb_%s_%d_constraint,\n", id, |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1281 | ++global_memb_unique); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1282 | } else { |
| 1283 | OUT("0,\t/* Defer to actual type */\n"); |
| 1284 | } |
| 1285 | if(C99_MODE) OUT(".name = "); |
| 1286 | OUT("\"%s\"\n", expr->Identifier ? expr->Identifier : ""); |
| 1287 | OUT("},\n"); |
| 1288 | INDENT(-1); |
| 1289 | |
| 1290 | if(!expr->constraints) |
| 1291 | return 0; |
| 1292 | |
| 1293 | save_target = arg->target->target; |
| 1294 | REDIR(OT_CODE); |
| 1295 | |
| 1296 | if(expr->Identifier) |
| 1297 | p = MKID(expr->Identifier); |
| 1298 | else |
| 1299 | p = asn1c_type_name(arg, expr, TNF_SAFE); |
| 1300 | OUT("static int\n"); |
vlm | dae7f9d | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1301 | OUT("memb_%s_%d_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,\n", p, global_memb_unique); |
vlm | b283901 | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1302 | INDENT(+1); |
| 1303 | OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n"); |
| 1304 | tmp_arg = *arg; |
| 1305 | tmp_arg.expr = expr; |
| 1306 | if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) { |
| 1307 | OUT("return td->check_constraints\n"); |
| 1308 | OUT("\t(td, sptr, app_errlog, app_key);\n"); |
| 1309 | } |
| 1310 | INDENT(-1); |
| 1311 | OUT("}\n"); |
| 1312 | OUT("\n"); |
| 1313 | |
| 1314 | REDIR(save_target); |
| 1315 | |
| 1316 | return 0; |
| 1317 | } |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1318 | |
| 1319 | static int |
| 1320 | emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, int tags_count, int tags_impl_skip, int elements_count, enum etd_cp cp, enum etd_spec spec) { |
| 1321 | char *p; |
| 1322 | |
| 1323 | p = MKID(expr->Identifier); |
vlm | 12c8f69 | 2004-09-06 08:07:29 +0000 | [diff] [blame] | 1324 | if(HIDE_INNER_DEFS) |
| 1325 | OUT("static /* Use -fall-defs-global to expose */\n"); |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1326 | OUT("asn1_TYPE_descriptor_t asn1_DEF_%s = {\n", p); |
| 1327 | INDENTED( |
| 1328 | OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier); |
| 1329 | |
| 1330 | if(expr->expr_type & ASN_CONSTR_MASK) { |
| 1331 | p = asn1c_type_name(arg, arg->expr, TNF_SAFE); |
| 1332 | } |
| 1333 | |
| 1334 | OUT("%s_constraint,\n", p); |
| 1335 | OUT("%s_decode_ber,\n", p); |
| 1336 | OUT("%s_encode_der,\n", p); |
| 1337 | OUT("%s_print,\n", p); |
| 1338 | OUT("%s_free,\n", p); |
| 1339 | |
| 1340 | p = MKID(expr->Identifier); |
| 1341 | |
| 1342 | if(expr->expr_type == ASN_CONSTR_CHOICE) { |
| 1343 | OUT("CHOICE_outmost_tag,\n"); |
| 1344 | } else { |
| 1345 | OUT("0,\t/* Use generic outmost tag fetcher */\n"); |
| 1346 | } |
| 1347 | |
| 1348 | if(tags_count) { |
| 1349 | OUT("asn1_DEF_%s_tags,\n", p); |
| 1350 | OUT("sizeof(asn1_DEF_%s_tags)\n", p); |
| 1351 | OUT("\t/sizeof(asn1_DEF_%s_tags[0]), /* %d */\n", |
| 1352 | p, tags_count); |
| 1353 | } else { |
| 1354 | OUT("0,\t/* No explicit tags (pointer) */\n"); |
| 1355 | OUT("0,\t/* No explicit tags (count) */\n"); |
| 1356 | } |
| 1357 | |
| 1358 | OUT("%d,\t/* Tags to skip */\n", tags_impl_skip); |
| 1359 | switch(cp) { |
| 1360 | case ETD_CP_UNKNOWN: |
| 1361 | OUT("-0,\t/* Unknown yet */\n"); |
| 1362 | break; |
| 1363 | case ETD_CP_EITHER: |
| 1364 | OUT("-1,\t/* Primitive or constructed */\n"); |
| 1365 | case ETD_CP_PRIMITIVE: |
| 1366 | OUT("0,\t/* Primitive */\n"); |
| 1367 | break; |
| 1368 | case ETD_CP_CONSTRUCTED: |
| 1369 | OUT("1,\t/* Whether CONSTRUCTED */\n"); |
| 1370 | break; |
| 1371 | } |
| 1372 | |
| 1373 | if(elements_count) { |
| 1374 | OUT("asn1_MBR_%s,\n", p); |
| 1375 | if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF |
| 1376 | || expr->expr_type == ASN_CONSTR_SET_OF) { |
| 1377 | OUT("%d,\t/* Single element */\n", |
| 1378 | elements_count); |
| 1379 | assert(elements_count == 1); |
| 1380 | } else { |
| 1381 | OUT("%d,\t/* Elements count */\n", |
| 1382 | elements_count); |
| 1383 | } |
| 1384 | } else { |
| 1385 | //if(expr->meta_type == AMT_TYPEREF) |
| 1386 | if(expr_elements_count(arg, expr)) |
| 1387 | OUT("0, 0,\t/* Defined elsewhere */\n"); |
| 1388 | else |
| 1389 | OUT("0, 0,\t/* No members */\n"); |
| 1390 | } |
| 1391 | |
| 1392 | switch(spec) { |
| 1393 | case ETD_NO_SPECIFICS: |
| 1394 | OUT("0\t/* No specifics */\n"); |
| 1395 | break; |
| 1396 | case ETD_HAS_SPECIFICS: |
| 1397 | OUT("&asn1_DEF_%s_specs\t/* Additional specs */\n", p); |
| 1398 | } |
| 1399 | ); |
| 1400 | OUT("};\n"); |
| 1401 | OUT("\n"); |
vlm | e2bb443 | 2004-08-26 06:20:34 +0000 | [diff] [blame] | 1402 | |
| 1403 | return 0; |
vlm | 4e55499 | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1404 | } |
vlm | ddd5a7d | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 1405 | |
| 1406 | /* |
| 1407 | * Check if it is better to make this type indirectly accessed via |
| 1408 | * a pointer. |
| 1409 | * This may be the case for the following recursive definition: |
| 1410 | * Type ::= CHOICE { member Type }; |
| 1411 | */ |
| 1412 | static int |
| 1413 | expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) { |
| 1414 | asn1p_expr_t *top_parent; |
| 1415 | asn1p_expr_t *terminal; |
| 1416 | |
| 1417 | if(expr->expr_type != A1TC_REFERENCE) |
| 1418 | return 0; |
| 1419 | |
| 1420 | /* Rewind to the topmost parent expression */ |
| 1421 | if((top_parent = expr->parent_expr)) { |
| 1422 | while(top_parent->parent_expr) |
| 1423 | top_parent = top_parent->parent_expr; |
| 1424 | } else { |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | terminal = asn1f_find_terminal_type_ex(arg->asn, arg->mod, expr); |
| 1429 | |
| 1430 | return (terminal == top_parent); |
| 1431 | } |