blob: 41ad21ea2a8d80f714d4fa6d6168fd57c9e5e6d6 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*
2 * Don't look into this file. First, because it's a mess, and second, because
3 * it's a brain of the compiler, and you don't wanna mess with brains do you? ;)
4 */
5#include "asn1c_internal.h"
6#include "asn1c_C.h"
Lev Walkin84cd58e2004-08-19 13:29:46 +00007#include "asn1c_constraint.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00008#include "asn1c_out.h"
9#include "asn1c_misc.h"
Lev Walkin84cd58e2004-08-19 13:29:46 +000010#include <asn1fix_export.h> /* Stuff exported by libasn1fix */
Lev Walkinf15320b2004-06-03 03:38:44 +000011
Lev Walkinfd171ef2004-06-06 07:20:17 +000012typedef struct tag2el_s {
13 struct asn1p_type_tag_s el_tag;
14 int el_no;
Lev Walkin38abe792004-06-14 13:09:45 +000015 int toff_first;
16 int toff_last;
Lev Walkinfd171ef2004-06-06 07:20:17 +000017 asn1p_expr_t *from_expr;
18} tag2el_t;
19
Lev Walkin33d5d3c2004-10-03 09:13:30 +000020typedef enum fte {
21 FTE_ALLTAGS,
22 FTE_CANONICAL_XER,
23} fte_e;
24static int _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags);
25static int _add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags);
Lev Walkinfd171ef2004-06-06 07:20:17 +000026
Lev Walkinf15320b2004-06-03 03:38:44 +000027static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg);
28static int asn1c_lang_C_type_SET_def(arg_t *arg);
29static int asn1c_lang_C_type_CHOICE_def(arg_t *arg);
30static int asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of);
Lev Walkinc3e29402004-09-10 06:07:18 +000031static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p);
Lev Walkin35631482004-07-01 00:52:50 +000032static int check_if_extensible(asn1p_expr_t *expr);
Lev Walkincc93b0f2004-09-10 09:18:20 +000033static int expr_better_indirect(arg_t *arg, asn1p_expr_t *expr);
Lev Walkina9cc46e2004-09-22 16:06:28 +000034static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr);
Lev Walkin59964be2004-08-25 02:03:12 +000035static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr);
36static int emit_member_table(arg_t *arg, asn1p_expr_t *expr);
Lev Walkin33d5d3c2004-10-03 09:13:30 +000037static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier);
Lev Walkin21d00002005-03-04 08:48:53 +000038static int out_name_chain(arg_t *arg, int check_reserved_keywords);
Lev Walkinf15320b2004-06-03 03:38:44 +000039
Lev Walkin188ed2c2004-09-13 08:31:01 +000040enum tvm_compat {
41 _TVM_SAME = 0, /* tags and all_tags are same */
42 _TVM_SUBSET = 1, /* tags are subset of all_tags */
43 _TVM_DIFFERENT = 2, /* tags and all_tags are different */
44};
45static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc);
46
Lev Walkin59964be2004-08-25 02:03:12 +000047enum etd_spec {
48 ETD_NO_SPECIFICS,
49 ETD_HAS_SPECIFICS
50};
Lev Walkin8de2ab22004-09-26 13:11:31 +000051static int emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_count, int all_tags_count, int elements_count, enum etd_spec);
Lev Walkin59964be2004-08-25 02:03:12 +000052
Lev Walkin59004fa2004-08-20 13:37:01 +000053#define C99_MODE (!(arg->flags & A1C_NO_C99))
Lev Walkinf15320b2004-06-03 03:38:44 +000054#define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS)
Lev Walkindd32b592004-09-06 08:07:29 +000055#define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL))
Lev Walkinf15320b2004-06-03 03:38:44 +000056
57#define PCTX_DEF INDENTED( \
58 OUT("\n"); \
59 OUT("/* Context for parsing across buffer boundaries */\n"); \
Lev Walkin05363a72004-09-29 13:16:40 +000060 OUT("asn_struct_ctx_t _asn_ctx;\n"));
Lev Walkinf15320b2004-06-03 03:38:44 +000061
Lev Walkin3dcaafa2004-08-11 05:21:32 +000062#define DEPENDENCIES do { \
Lev Walkin08079b02004-08-22 03:25:24 +000063 asn1p_expr_t *__m; \
64 TQ_FOR(__m, &(expr->members), next) { \
65 if((!(__m->expr_type & ASN_CONSTR_MASK) \
66 && __m->expr_type > ASN_CONSTR_MASK) \
67 || __m->meta_type == AMT_TYPEREF) { \
68 GEN_INCLUDE(asn1c_type_name(arg, \
Lev Walkin801fabc2005-01-28 12:18:50 +000069 __m, TNF_INCLUDE)); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000070 } \
71 } \
72 if(expr->expr_type == ASN_CONSTR_SET_OF) \
73 GEN_INCLUDE("asn_SET_OF"); \
74 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \
75 GEN_INCLUDE("asn_SEQUENCE_OF"); \
76} while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +000077
Lev Walkin801fabc2005-01-28 12:18:50 +000078/* MKID() without checking for reserved keywords */
Lev Walkin4e319f02005-03-03 21:58:28 +000079#define MKID_nc(id) asn1c_make_identifier(0, (id), 0)
Lev Walkin801fabc2005-01-28 12:18:50 +000080#define MKID(id) asn1c_make_identifier(AMI_CHECK_RESERVED, (id), 0)
Lev Walkinf15320b2004-06-03 03:38:44 +000081
82int
Lev Walkinc78cbfb2004-09-14 12:47:45 +000083asn1c_lang_C_type_REAL(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +000084 REDIR(OT_DEPS);
Lev Walkinf15320b2004-06-03 03:38:44 +000085 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
86}
87
Lev Walkine0b56e02005-02-25 12:10:27 +000088struct value2enum {
89 asn1c_integer_t value;
90 const char *name;
91 int idx;
92};
93static int compar_enumMap_byName(const void *ap, const void *bp) {
94 const struct value2enum *a = (const struct value2enum *)ap;
95 const struct value2enum *b = (const struct value2enum *)bp;
96 return strcmp(a->name, b->name);
97}
98static int compar_enumMap_byValue(const void *ap, const void *bp) {
99 const struct value2enum *a = (const struct value2enum *)ap;
100 const struct value2enum *b = (const struct value2enum *)bp;
101 if(a->value < b->value)
102 return -1;
103 else if(a->value == b->value)
104 return 0;
105 return 1;
106}
107
Lev Walkinf15320b2004-06-03 03:38:44 +0000108int
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000109asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 asn1p_expr_t *expr = arg->expr;
111 asn1p_expr_t *v;
Lev Walkine0b56e02005-02-25 12:10:27 +0000112 int el_count = expr_elements_count(arg, expr);
113 struct value2enum *v2e;
114 int map_is_extensible = (expr->expr_type == ASN_BASIC_INTEGER);
Lev Walkinf15320b2004-06-03 03:38:44 +0000115
Lev Walkine0b56e02005-02-25 12:10:27 +0000116 v2e = alloca((el_count + 1) * sizeof(*v2e));
Lev Walkinf15320b2004-06-03 03:38:44 +0000117
Lev Walkine0b56e02005-02-25 12:10:27 +0000118 /*
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000119 * For all ENUMERATED types [and for those INTEGER types which
120 * have identifiers -- prohibited by X.693:8.3.4],
121 * print out an enumeration table and a mapping
Lev Walkine0b56e02005-02-25 12:10:27 +0000122 * between identifiers and associated values.
123 */
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000124 if(expr->expr_type == ASN_BASIC_ENUMERATED
125 || (0 && el_count /* -- prohibited by X.693:8.3.4 */)) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000126 int eidx = 0;
127
128 REDIR(OT_DEPS);
Lev Walkin21d00002005-03-04 08:48:53 +0000129 OUT("typedef enum ");
130 out_name_chain(arg, 1);
131 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 TQ_FOR(v, &(expr->members), next) {
133 switch(v->expr_type) {
134 case A1TC_UNIVERVAL:
Lev Walkin21d00002005-03-04 08:48:53 +0000135 OUT("\t");
136 out_name_chain(arg, 0);
137 OUT("_%s", MKID_nc(v->Identifier));
138 OUT("\t= %" PRIdASN "%s\n",
Lev Walkine0b56e02005-02-25 12:10:27 +0000139 v->value->value.v_integer,
Lev Walkin21d00002005-03-04 08:48:53 +0000140 (eidx+1 < el_count) ? "," : "");
Lev Walkine0b56e02005-02-25 12:10:27 +0000141 v2e[eidx].name = v->Identifier;
142 v2e[eidx].value = v->value->value.v_integer;
143 eidx++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000145 case A1TC_EXTENSIBLE:
146 OUT("\t/*\n");
147 OUT("\t * Enumeration is extensible\n");
148 OUT("\t */\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000149 map_is_extensible = 1;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000150 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 default:
152 return -1;
153 }
154 }
Lev Walkin21d00002005-03-04 08:48:53 +0000155 OUT("} ");
156 out_name_chain(arg, 0);
157 OUT("_e;\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000158 assert(eidx == el_count);
159
160 /*
161 * Generate a enumerationName<->value map for XER codec.
162 */
163 REDIR(OT_STAT_DEFS);
164
Lev Walkin21d00002005-03-04 08:48:53 +0000165 OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_%d_value2enum[] = {\n",
166 MKID_nc(expr->Identifier),
167 expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000168 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
169 for(eidx = 0; eidx < el_count; eidx++) {
170 v2e[eidx].idx = eidx;
171 OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n",
172 v2e[eidx].value,
173 (long)strlen(v2e[eidx].name), v2e[eidx].name,
174 (eidx + 1 < el_count) ? "," : "");
175 }
176 if(map_is_extensible)
177 OUT("\t/* This list is extensible */\n");
178 OUT("};\n");
179
Lev Walkin21d00002005-03-04 08:48:53 +0000180 OUT("static unsigned int asn_MAP_%s_%d_enum2value[] = {\n",
181 MKID_nc(expr->Identifier),
182 expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000183 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
184 for(eidx = 0; eidx < el_count; eidx++) {
185 OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n",
186 v2e[eidx].idx,
187 (eidx + 1 < el_count) ? "," : "",
188 v2e[eidx].name, v2e[eidx].value);
189 }
190 if(map_is_extensible)
191 OUT("\t/* This list is extensible */\n");
192 OUT("};\n");
193
Lev Walkin21d00002005-03-04 08:48:53 +0000194 OUT("static asn_INTEGER_specifics_t asn_SPC_%s_%d_specs = {\n",
195 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000196 INDENT(+1);
Lev Walkin21d00002005-03-04 08:48:53 +0000197 OUT("asn_MAP_%s_%d_value2enum,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000198 "/* \"tag\" => N; sorted by tag */\n",
Lev Walkin21d00002005-03-04 08:48:53 +0000199 MKID_nc(expr->Identifier),
200 expr->_type_unique_index);
201 OUT("asn_MAP_%s_%d_enum2value,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000202 "/* N => \"tag\"; sorted by N */\n",
Lev Walkin21d00002005-03-04 08:48:53 +0000203 MKID_nc(expr->Identifier),
204 expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000205 OUT("%d,\t/* Number of elements in the maps */\n",
206 el_count);
207 OUT("%d,\t/* Enumeration is %sextensible */\n",
208 map_is_extensible, map_is_extensible ? "": "not ");
209 if(expr->expr_type == ASN_BASIC_ENUMERATED)
210 OUT("1\t/* Strict enumeration */\n");
211 else
212 OUT("0\n");
213 INDENT(-1);
214 OUT("};\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000215 }
216
217 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
218}
219
220int
221asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
222 asn1p_expr_t *expr = arg->expr;
223 asn1p_expr_t *v;
224 int comp_mode = 0; /* {root,ext=1,root,root,...} */
225
226 DEPENDENCIES;
227
228 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000229 OUT("struct ");
230 out_name_chain(arg, 1);
231 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000232 } else {
233 OUT("typedef struct %s {\n",
234 MKID(expr->Identifier));
235 }
236
237 TQ_FOR(v, &(expr->members), next) {
238 if(v->expr_type == A1TC_EXTENSIBLE) {
239 if(comp_mode < 3) comp_mode++;
240 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000241 if(comp_mode == 1
242 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000243 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 EMBED(v);
245 }
246
247 PCTX_DEF;
Lev Walkin0204fa62004-09-15 11:54:38 +0000248 OUT("} %s%s%s", expr->marker.flags?"*":"",
Lev Walkin08079b02004-08-22 03:25:24 +0000249 expr->_anonymous_type ? "" : MKID(expr->Identifier),
250 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000251
252 return asn1c_lang_C_type_SEQUENCE_def(arg);
253}
254
255static int
256asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
257 asn1p_expr_t *expr = arg->expr;
258 asn1p_expr_t *v;
259 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 int ext_start = -1;
261 int ext_stop = -1;
Lev Walkinfd171ef2004-06-06 07:20:17 +0000262 tag2el_t *tag2el = NULL;
263 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000264 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000265 int all_tags_count;
266 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000267
Lev Walkinfd171ef2004-06-06 07:20:17 +0000268 /*
269 * Fetch every inner tag from the tag to elements map.
270 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000271 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinfd171ef2004-06-06 07:20:17 +0000272 if(tag2el) free(tag2el);
273 return -1;
274 }
275
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000276 GEN_INCLUDE("constr_SEQUENCE");
277 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000278 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000279
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000280 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000281
282 /*
283 * Print out the table according to which the parsing is performed.
284 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000285 if(expr_elements_count(arg, expr)) {
286 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000287
Lev Walkin21d00002005-03-04 08:48:53 +0000288 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
289 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000290
291 elements = 0;
292 INDENTED(TQ_FOR(v, &(expr->members), next) {
293 if(v->expr_type == A1TC_EXTENSIBLE) {
294 if((++comp_mode) == 1)
295 ext_start = elements - 1;
296 else
297 ext_stop = elements - 1;
298 continue;
299 }
300 elements++;
301 emit_member_table(arg, v);
302 });
303 OUT("};\n");
304 } else {
305 elements = 0;
306 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000307
Lev Walkin27ea3802004-06-28 21:13:46 +0000308 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000309 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000310 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000311 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000312
Lev Walkinfd171ef2004-06-06 07:20:17 +0000313 /*
314 * Tags to elements map.
315 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000316 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinfd171ef2004-06-06 07:20:17 +0000317
Lev Walkin21d00002005-03-04 08:48:53 +0000318 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_%d_specs = {\n",
319 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000320 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000321 OUT("sizeof(struct "); out_name_chain(arg, 1); OUT("),\n");
322 OUT("offsetof(struct "); out_name_chain(arg, 1); OUT(", _asn_ctx),\n");
323
324 OUT("asn_MAP_%s_%d_tag2el,\n",
325 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinfd171ef2004-06-06 07:20:17 +0000326 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000327 OUT("%d,\t/* Start extensions */\n",
328 ext_start);
329 OUT("%d\t/* Stop extensions */\n",
330 (ext_stop<ext_start)?elements+1:ext_stop, ext_stop);
331 );
332 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000333
334 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000335 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000336 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000337 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000338 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000339
Lev Walkinf15320b2004-06-03 03:38:44 +0000340 REDIR(OT_TYPE_DECLS);
341
342 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000343} /* _SEQUENCE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000344
345int
Lev Walkinf15320b2004-06-03 03:38:44 +0000346asn1c_lang_C_type_SET(arg_t *arg) {
347 asn1p_expr_t *expr = arg->expr;
348 asn1p_expr_t *v;
349 long mcount;
350 char *id;
351 int comp_mode = 0; /* {root,ext=1,root,root,...} */
352
353 DEPENDENCIES;
354
355 REDIR(OT_DEPS);
356
357 OUT("\n");
358 OUT("/*\n");
359 OUT(" * Method of determining the components presence\n");
360 OUT(" */\n");
361 mcount = 0;
Lev Walkin21d00002005-03-04 08:48:53 +0000362 OUT("typedef enum ");
363 out_name_chain(arg, 0);
364 OUT("_PR {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000365 TQ_FOR(v, &(expr->members), next) {
366 if(v->expr_type == A1TC_EXTENSIBLE) continue;
367 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000368 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000369 OUT("_PR_");
Lev Walkin4e319f02005-03-03 21:58:28 +0000370 id = MKID_nc(v->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000371 OUT("%s,\t/* Member %s is present */\n",
372 id, id)
373 );
374 mcount++;
375 }
Lev Walkin21d00002005-03-04 08:48:53 +0000376 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000377
378 REDIR(OT_TYPE_DECLS);
379
380 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000381 OUT("struct ");
382 out_name_chain(arg, 1);
383 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 } else {
Lev Walkin801fabc2005-01-28 12:18:50 +0000385 id = MKID(expr->Identifier);
Lev Walkin64399722004-08-11 07:17:22 +0000386 OUT("typedef struct %s {\n", id);
Lev Walkinf15320b2004-06-03 03:38:44 +0000387 }
388
389 TQ_FOR(v, &(expr->members), next) {
390 if(v->expr_type == A1TC_EXTENSIBLE) {
391 if(comp_mode < 3) comp_mode++;
392 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000393 if(comp_mode == 1
394 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000395 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000396 EMBED(v);
397 }
398
399 INDENTED(
Lev Walkin4e319f02005-03-03 21:58:28 +0000400 id = MKID_nc(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 OUT("\n");
402 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
403 id, id);
404 OUT("unsigned int _presence_map\n");
405 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
406 );
407
408 PCTX_DEF;
Lev Walkin0204fa62004-09-15 11:54:38 +0000409 OUT("} %s%s%s", expr->marker.flags?"*":"",
Lev Walkin08079b02004-08-22 03:25:24 +0000410 expr->_anonymous_type ? "" : MKID(expr->Identifier),
411 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000412
413 return asn1c_lang_C_type_SET_def(arg);
414}
415
Lev Walkinf15320b2004-06-03 03:38:44 +0000416static int
417asn1c_lang_C_type_SET_def(arg_t *arg) {
418 asn1p_expr_t *expr = arg->expr;
419 asn1p_expr_t *v;
420 int elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000421 tag2el_t *tag2el = NULL;
422 int tag2el_count = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000423 tag2el_t *tag2el_cxer = NULL;
424 int tag2el_cxer_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000425 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000426 int all_tags_count;
427 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 char *p;
429
430 /*
431 * Fetch every inner tag from the tag to elements map.
432 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000433 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 if(tag2el) free(tag2el);
435 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000436 }
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000437 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
438 if(tag2el) free(tag2el);
439 if(tag2el_cxer) free(tag2el_cxer);
440 return -1;
441 }
442 if(tag2el_cxer_count == tag2el_count
443 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
444 free(tag2el_cxer);
445 tag2el_cxer = 0;
446 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000447
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000448 GEN_INCLUDE("constr_SET");
449 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000450 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000451
452 REDIR(OT_STAT_DEFS);
453
Lev Walkinf15320b2004-06-03 03:38:44 +0000454 /*
455 * Print out the table according to which the parsing is performed.
456 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000457 if(expr_elements_count(arg, expr)) {
458 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000459
Lev Walkin21d00002005-03-04 08:48:53 +0000460 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
461 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000462
463 elements = 0;
464 INDENTED(TQ_FOR(v, &(expr->members), next) {
465 if(v->expr_type == A1TC_EXTENSIBLE) {
466 if(comp_mode < 3) comp_mode++;
467 } else {
468 if(comp_mode == 1
469 || expr_better_indirect(arg, v))
470 v->marker.flags |= EM_INDIRECT;
471 elements++;
472 emit_member_table(arg, v);
473 }
474 });
475 OUT("};\n");
476 } else {
477 elements = 0;
478 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000479
Lev Walkin27ea3802004-06-28 21:13:46 +0000480 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000481 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000482 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000483 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000484
485 /*
486 * Tags to elements map.
487 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000488 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
489 if(tag2el_cxer)
490 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
Lev Walkinf15320b2004-06-03 03:38:44 +0000491
492 /*
493 * Emit a map of mandatory elements.
494 */
Lev Walkin21d00002005-03-04 08:48:53 +0000495 OUT("static uint8_t asn_MAP_%s_%d_mmap",
496 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkin27ea3802004-06-28 21:13:46 +0000497 p = MKID(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
Lev Walkin21d00002005-03-04 08:48:53 +0000499 OUT(" = {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000500 INDENTED(
501 if(elements) {
502 int delimit = 0;
503 int el = 0;
504 TQ_FOR(v, &(expr->members), next) {
505 if(v->expr_type == A1TC_EXTENSIBLE) continue;
506 if(delimit) {
507 OUT(",\n");
508 delimit = 0;
509 } else if(el) {
510 OUT(" | ");
511 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000512 OUT("(%d << %d)",
Lev Walkin0204fa62004-09-15 11:54:38 +0000513 v->marker.flags?0:1,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000514 7 - (el % 8));
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 if(el && (el % 8) == 0)
516 delimit = 1;
517 el++;
518 }
519 } else {
520 OUT("0");
521 }
522 );
523 OUT("\n");
524 OUT("};\n");
525
Lev Walkin21d00002005-03-04 08:48:53 +0000526 OUT("static asn_SET_specifics_t asn_SPC_%s_%d_specs = {\n",
527 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000528 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000529 OUT("sizeof(struct ");
530 out_name_chain(arg, 1);
531 OUT("),\n");
532 OUT("offsetof(struct ");
533 out_name_chain(arg, 1);
534 OUT(", _asn_ctx),\n");
535 OUT("offsetof(struct ");
536 out_name_chain(arg, 1);
537 OUT(", _presence_map),\n");
Lev Walkin4e319f02005-03-03 21:58:28 +0000538 p = MKID_nc(expr->Identifier);
Lev Walkin21d00002005-03-04 08:48:53 +0000539 OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000540 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000541 if(tag2el_cxer)
Lev Walkin21d00002005-03-04 08:48:53 +0000542 OUT("asn_MAP_%s_%d_tag2el_cxer,\n",
543 p, expr->_type_unique_index);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000544 else
Lev Walkin21d00002005-03-04 08:48:53 +0000545 OUT("asn_MAP_%s_%d_tag2el,\t/* Same as above */\n",
546 p, expr->_type_unique_index);
547 OUT("%d,\t/* Count of tags in the CXER map */\n",
548 tag2el_cxer_count);
Lev Walkin35631482004-07-01 00:52:50 +0000549 OUT("%d,\t/* Whether extensible */\n",
550 check_if_extensible(expr));
Lev Walkin21d00002005-03-04 08:48:53 +0000551 OUT("(unsigned int *)asn_MAP_%s_%d_mmap\t/* Mandatory elements map */\n",
552 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 );
554 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000555
556 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000557 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000558 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000559 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000560 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000561
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 REDIR(OT_TYPE_DECLS);
563
564 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000565} /* _SET_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000566
567int
Lev Walkin08079b02004-08-22 03:25:24 +0000568asn1c_lang_C_type_SEx_OF(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000569 asn1p_expr_t *expr = arg->expr;
Lev Walkin08079b02004-08-22 03:25:24 +0000570 asn1p_expr_t *memb;
Lev Walkinf15320b2004-06-03 03:38:44 +0000571
572 DEPENDENCIES;
573
574 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000575 OUT("struct ");
576 out_name_chain(arg, 1);
577 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 } else {
Lev Walkin08079b02004-08-22 03:25:24 +0000579 OUT("typedef struct %s {\n", MKID(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000580 }
581
Lev Walkin08079b02004-08-22 03:25:24 +0000582 memb = TQ_FIRST(&expr->members);
583
584 INDENT(+1);
585 OUT("A_%s_OF(",
586 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
587 ? "SET" : "SEQUENCE");
Lev Walkine0b56e02005-02-25 12:10:27 +0000588 if(memb->expr_type & ASN_CONSTR_MASK
589 || ((memb->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000590 || (0 /* -- prohibited by X.693:8.3.4 */
591 && memb->expr_type == ASN_BASIC_INTEGER))
592 && expr_elements_count(arg, memb))) {
Lev Walkin08079b02004-08-22 03:25:24 +0000593 arg_t tmp;
594 asn1p_expr_t tmp_memb;
595 arg->embed++;
596 tmp = *arg;
597 tmp.expr = &tmp_memb;
598 tmp_memb = *memb;
599 tmp_memb._anonymous_type = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000600 if(tmp_memb.Identifier == 0) {
Lev Walkin21d00002005-03-04 08:48:53 +0000601 tmp_memb.Identifier = "Member";
602 if(0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000603 tmp_memb.Identifier = strdup(
604 asn1c_make_identifier(0,
Lev Walkin152a91e2005-02-14 20:41:29 +0000605 expr->Identifier, "Member", 0));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000606 assert(tmp_memb.Identifier);
607 }
Lev Walkin08079b02004-08-22 03:25:24 +0000608 tmp.default_cb(&tmp);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000609 if(tmp_memb.Identifier != memb->Identifier)
Lev Walkin21d00002005-03-04 08:48:53 +0000610 if(0) free(tmp_memb.Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000611 arg->embed--;
612 assert(arg->target->target == OT_TYPE_DECLS);
613 } else {
Lev Walkin801fabc2005-01-28 12:18:50 +0000614 OUT("%s", asn1c_type_name(arg, memb, TNF_CTYPE | TNF_CHECK));
Lev Walkinf15320b2004-06-03 03:38:44 +0000615 }
Lev Walkin08079b02004-08-22 03:25:24 +0000616 OUT(") list;\n");
617 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000618
619 PCTX_DEF;
Lev Walkin0204fa62004-09-15 11:54:38 +0000620 OUT("} %s%s%s", expr->marker.flags?"*":"",
Lev Walkin08079b02004-08-22 03:25:24 +0000621 expr->_anonymous_type ? "" : MKID(expr->Identifier),
622 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000623
624 /*
Lev Walkin08079b02004-08-22 03:25:24 +0000625 * SET OF/SEQUENCE OF definition
Lev Walkinf15320b2004-06-03 03:38:44 +0000626 */
Lev Walkin08079b02004-08-22 03:25:24 +0000627 return asn1c_lang_C_type_SEx_OF_def(arg,
628 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
Lev Walkinf15320b2004-06-03 03:38:44 +0000629}
630
631static int
632asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
633 asn1p_expr_t *expr = arg->expr;
634 asn1p_expr_t *v;
Lev Walkin64399722004-08-11 07:17:22 +0000635 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000636 int all_tags_count;
637 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000638
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000639 /*
640 * Print out the table according to which the parsing is performed.
641 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000642 if(seq_of) {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000643 GEN_INCLUDE("constr_SEQUENCE_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000644 } else {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000645 GEN_INCLUDE("constr_SET_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000646 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000647 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000648 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000649
650 REDIR(OT_STAT_DEFS);
651
652 /*
653 * Print out the table according to which the parsing is performed.
654 */
Lev Walkin21d00002005-03-04 08:48:53 +0000655 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
656 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000657 INDENT(+1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000658 v = TQ_FIRST(&(expr->members));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000659 if(!v->Identifier) {
Lev Walkin152a91e2005-02-14 20:41:29 +0000660 v->Identifier = strdup("Member");
Lev Walkina9cc46e2004-09-22 16:06:28 +0000661 assert(v->Identifier);
662 }
663 v->_anonymous_type = 1;
664 arg->embed++;
Lev Walkin59964be2004-08-25 02:03:12 +0000665 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000666 arg->embed--;
667 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000668 OUT("};\n");
669
Lev Walkin27ea3802004-06-28 21:13:46 +0000670 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000671 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000672 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000673 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000674
Lev Walkin21d00002005-03-04 08:48:53 +0000675 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
676 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000677 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000678 OUT("sizeof(struct ");
679 out_name_chain(arg, 1);
680 OUT("),\n");
681 OUT("offsetof(struct ");
682 out_name_chain(arg, 1);
683 OUT(", _asn_ctx),\n");
684
Lev Walkina9cc46e2004-09-22 16:06:28 +0000685 if(expr_as_xmlvaluelist(arg, v))
686 OUT("1,\t/* XER encoding is XMLValueList */\n");
687 else
688 OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000689 );
690 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000691
692 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000693 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000694 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000695 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000696 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000697
Lev Walkinf15320b2004-06-03 03:38:44 +0000698 REDIR(OT_TYPE_DECLS);
699
700 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000701} /* _SEx_OF_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000702
703int
704asn1c_lang_C_type_CHOICE(arg_t *arg) {
705 asn1p_expr_t *expr = arg->expr;
706 asn1p_expr_t *v;
Lev Walkin08079b02004-08-22 03:25:24 +0000707 char *id;
Lev Walkinf15320b2004-06-03 03:38:44 +0000708
709 DEPENDENCIES;
710
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000711 REDIR(OT_DEPS);
712
Lev Walkin21d00002005-03-04 08:48:53 +0000713 OUT("typedef enum ");
714 out_name_chain(arg, 0);
715 OUT("_PR {\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000716 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000717 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000718 OUT("_PR_NOTHING,\t/* No components present */\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000719 TQ_FOR(v, &(expr->members), next) {
720 if(v->expr_type == A1TC_EXTENSIBLE) {
721 OUT("/* Extensions may appear below */\n");
722 continue;
723 }
Lev Walkin21d00002005-03-04 08:48:53 +0000724 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000725 OUT("_PR_");
Lev Walkin4e319f02005-03-03 21:58:28 +0000726 id = MKID_nc(v->Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000727 OUT("%s,\n", id, id);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000728 }
729 );
Lev Walkin21d00002005-03-04 08:48:53 +0000730 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000731
732 REDIR(OT_TYPE_DECLS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000733
734 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000735 OUT("struct "); out_name_chain(arg, 1); OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000736 } else {
Lev Walkin801fabc2005-01-28 12:18:50 +0000737 id = MKID(expr->Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000738 OUT("typedef struct %s {\n", id);
Lev Walkinf15320b2004-06-03 03:38:44 +0000739 }
740
741 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000742 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000743 OUT("_PR present;\n");
744 OUT("union {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000745 TQ_FOR(v, &(expr->members), next) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000746 if(expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000747 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000748 EMBED(v);
749 }
750 if(UNNAMED_UNIONS) OUT("};\n");
751 else OUT("} choice;\n");
752 );
753
754 PCTX_DEF;
Lev Walkin0204fa62004-09-15 11:54:38 +0000755 OUT("} %s%s%s", expr->marker.flags?"*":"",
Lev Walkin08079b02004-08-22 03:25:24 +0000756 expr->_anonymous_type ? "" : MKID(expr->Identifier),
757 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000758
759 return asn1c_lang_C_type_CHOICE_def(arg);
760}
761
762static int
763asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
764 asn1p_expr_t *expr = arg->expr;
765 asn1p_expr_t *v;
766 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000767 tag2el_t *tag2el = NULL;
768 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000769 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000770 int all_tags_count;
771 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000772
773 /*
774 * Fetch every inner tag from the tag to elements map.
775 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000776 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000777 if(tag2el) free(tag2el);
778 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000779 }
780
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000781 GEN_INCLUDE("constr_CHOICE");
782 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000783 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000784
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000785 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000786
787 /*
788 * Print out the table according to which the parsing is performed.
789 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000790 if(expr_elements_count(arg, expr)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000791
Lev Walkin21d00002005-03-04 08:48:53 +0000792 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
793 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000794
795 elements = 0;
796 INDENTED(TQ_FOR(v, &(expr->members), next) {
Lev Walkin659c63b2005-01-17 11:40:49 +0000797 if(v->expr_type == A1TC_EXTENSIBLE)
798 continue;
799 if(expr_better_indirect(arg, v))
800 v->marker.flags |= EM_INDIRECT;
801 elements++;
802 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000803 });
804 OUT("};\n");
805 } else {
806 elements = 0;
807 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000808
Lev Walkin64399722004-08-11 07:17:22 +0000809
Lev Walkinf15320b2004-06-03 03:38:44 +0000810 if(arg->embed) {
811 /*
812 * Our parent structure has already taken this into account.
813 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000814 tv_mode = _TVM_SAME;
815 tags_count = all_tags_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000816 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000817 tv_mode = emit_tags_vectors(arg, expr,
818 &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000819 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000820
821 /*
822 * Tags to elements map.
823 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000824 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000825
Lev Walkin21d00002005-03-04 08:48:53 +0000826 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
827 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000828 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000829 OUT("sizeof(struct ");
830 out_name_chain(arg, 1);
831 OUT("),\n");
832 OUT("offsetof(struct ");
833 out_name_chain(arg, 1);
834 OUT(", _asn_ctx),\n");
835 OUT("offsetof(struct ");
836 out_name_chain(arg, 1);
837 OUT(", present),\n");
838 OUT("sizeof(((struct ");
839 out_name_chain(arg, 1);
840 OUT(" *)0)->present),\n");
841 OUT("asn_MAP_%s_%d_tag2el,\n",
842 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000843 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin35631482004-07-01 00:52:50 +0000844 OUT("%d\t/* Whether extensible */\n",
845 check_if_extensible(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000846 );
847 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000848
849 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000850 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000851 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000852 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000853 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000854
Lev Walkinf15320b2004-06-03 03:38:44 +0000855 REDIR(OT_TYPE_DECLS);
856
857 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000858} /* _CHOICE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000859
860int
861asn1c_lang_C_type_REFERENCE(arg_t *arg) {
862 asn1p_ref_t *ref;
863
864 ref = arg->expr->reference;
865 if(ref->components[ref->comp_count-1].name[0] == '&') {
Lev Walkinf15320b2004-06-03 03:38:44 +0000866 asn1p_expr_t *extract;
867 arg_t tmp;
868 int ret;
869
Lev Walkina9cc46e2004-09-22 16:06:28 +0000870 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
Lev Walkin08079b02004-08-22 03:25:24 +0000871 arg->expr, ref);
Lev Walkinf15320b2004-06-03 03:38:44 +0000872 if(extract == NULL)
873 return -1;
874
Lev Walkin08079b02004-08-22 03:25:24 +0000875 extract = asn1p_expr_clone(extract, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000876 if(extract) {
877 if(extract->Identifier)
878 free(extract->Identifier);
879 extract->Identifier = strdup(arg->expr->Identifier);
880 if(extract->Identifier == NULL) {
881 asn1p_expr_free(extract);
882 return -1;
883 }
884 } else {
885 return -1;
886 }
887
888 tmp = *arg;
889 tmp.asn = arg->asn;
Lev Walkin08079b02004-08-22 03:25:24 +0000890 tmp.mod = extract->module;
Lev Walkinf15320b2004-06-03 03:38:44 +0000891 tmp.expr = extract;
892
893 ret = arg->default_cb(&tmp);
894
895 asn1p_expr_free(extract);
896
897 return ret;
898 }
899
900
901 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
902}
903
904int
905asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
906 asn1p_expr_t *expr = arg->expr;
Lev Walkin64399722004-08-11 07:17:22 +0000907 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000908 int all_tags_count;
909 enum tvm_compat tv_mode;
Lev Walkine0b56e02005-02-25 12:10:27 +0000910 enum etd_spec etd_spec;
Lev Walkinf15320b2004-06-03 03:38:44 +0000911 char *p;
912
913 if(arg->embed) {
Lev Walkin05363a72004-09-29 13:16:40 +0000914 enum tnfmt tnfmt = TNF_CTYPE;
915
916 /*
917 * If this is an optional compound type,
918 * refer it using "struct X" convention,
919 * as it may recursively include the current structure.
920 */
921 if(expr->marker.flags) {
922 asn1p_expr_t *terminal;
923 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
924 if(terminal
925 && (terminal->expr_type & ASN_CONSTR_MASK)) {
926 REDIR(OT_DEPS);
927 tnfmt = TNF_RSAFE;
928 OUT("\n");
929 OUT("%s;\t/* Forward declaration */\n",
Lev Walkin801fabc2005-01-28 12:18:50 +0000930 asn1c_type_name(arg, arg->expr, tnfmt | TNF_CHECK));
Lev Walkin05363a72004-09-29 13:16:40 +0000931 }
932 }
933
Lev Walkinf15320b2004-06-03 03:38:44 +0000934 REDIR(OT_TYPE_DECLS);
935
Lev Walkine0b56e02005-02-25 12:10:27 +0000936 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt | TNF_CHECK));
937 if(!expr->_anonymous_type) {
938 OUT("%s", expr->marker.flags?"\t*":"\t ");
939 OUT("%s", MKID(expr->Identifier));
940 if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT)
941 OUT("\t/* DEFAULT %s */",
942 asn1f_printable_value(
943 expr->marker.default_value));
944 else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL)
945 OUT("\t/* OPTIONAL */");
946 }
947
948 } else {
949 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
Lev Walkin59004fa2004-08-20 13:37:01 +0000950
951 REDIR(OT_TYPE_DECLS);
Lev Walkine0b56e02005-02-25 12:10:27 +0000952
953 OUT("typedef %s\t",
954 asn1c_type_name(arg, arg->expr, TNF_CTYPE | TNF_CHECK));
955 OUT("%s%s_t",
956 expr->marker.flags?"*":" ",
957 MKID(expr->Identifier));
958 OUT_DEBUG("\t/* %s:%d */", __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000959 }
960
Lev Walkine0b56e02005-02-25 12:10:27 +0000961 if((expr->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000962 || (0 /* -- prohibited by X.693:8.3.4 */
963 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +0000964 && expr_elements_count(arg, expr)))
965 etd_spec = ETD_HAS_SPECIFICS;
966 else
967 etd_spec = ETD_NO_SPECIFICS;
Lev Walkinf15320b2004-06-03 03:38:44 +0000968
Lev Walkin8de2ab22004-09-26 13:11:31 +0000969 /*
970 * If this type just blindly refers the other type, alias it.
971 * Type1 ::= Type2
972 */
Lev Walkine0b56e02005-02-25 12:10:27 +0000973 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
974 REDIR(OT_TYPE_DECLS);
975 return 0;
976 }
Lev Walkin8de2ab22004-09-26 13:11:31 +0000977 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkine0b56e02005-02-25 12:10:27 +0000978 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
979 && etd_spec == ETD_NO_SPECIFICS) {
Lev Walkin8de2ab22004-09-26 13:11:31 +0000980 char *type_name;
981 REDIR(OT_FUNC_DECLS);
982 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
983 OUT("/* This type is equivalent to %s */\n", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +0000984 if(HIDE_INNER_DEFS) OUT("/* ");
Lev Walkin4e319f02005-03-03 21:58:28 +0000985 OUT("#define\tasn_DEF_%s\t", MKID_nc(expr->Identifier));
Lev Walkin8de2ab22004-09-26 13:11:31 +0000986 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkine0b56e02005-02-25 12:10:27 +0000987 OUT("asn_DEF_%s", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +0000988 if(HIDE_INNER_DEFS)
Lev Walkine0b56e02005-02-25 12:10:27 +0000989 OUT("\t// (Use -fall-defs-global to expose) */");
990 OUT("\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +0000991 REDIR(OT_CODE);
992 OUT("/* This type is equivalent to %s */\n", type_name);
993 OUT("\n");
994 REDIR(OT_TYPE_DECLS);
995 return 0;
996 }
997
Lev Walkinf15320b2004-06-03 03:38:44 +0000998 REDIR(OT_STAT_DEFS);
999
Lev Walkin27ea3802004-06-28 21:13:46 +00001000 /*
Lev Walkin05363a72004-09-29 13:16:40 +00001001 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +00001002 */
Lev Walkin188ed2c2004-09-13 08:31:01 +00001003 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +00001004
Lev Walkine0b56e02005-02-25 12:10:27 +00001005 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1006 0, etd_spec);
Lev Walkinf15320b2004-06-03 03:38:44 +00001007
Lev Walkin59004fa2004-08-20 13:37:01 +00001008 REDIR(OT_CODE);
1009
Lev Walkinf15320b2004-06-03 03:38:44 +00001010 /*
1011 * Constraint checking.
1012 */
Lev Walkin8de2ab22004-09-26 13:11:31 +00001013 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1014 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001015 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001016 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001017 OUT("%s", p);
1018 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1019 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001020 INDENT(+1);
1021 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1022 OUT("\n");
1023 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1024 OUT("/* Replace with underlying type checker */\n");
1025 OUT("td->check_constraints "
Lev Walkin05363a72004-09-29 13:16:40 +00001026 "= asn_DEF_%s.check_constraints;\n",
Lev Walkin8de2ab22004-09-26 13:11:31 +00001027 asn1c_type_name(arg, expr, TNF_SAFE));
1028 OUT("return td->check_constraints"
1029 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +00001030 }
Lev Walkin8de2ab22004-09-26 13:11:31 +00001031 INDENT(-1);
1032 OUT("}\n");
1033 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001034 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001035
1036 /*
1037 * Emit suicidal functions.
1038 */
1039
Lev Walkinf15320b2004-06-03 03:38:44 +00001040 /*
1041 * This function replaces certain fields from the definition
1042 * of a type with the corresponding fields from the basic type
1043 * (from which the current type is inherited).
1044 */
Lev Walkinf15320b2004-06-03 03:38:44 +00001045 OUT("/*\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001046 OUT(" * This type is implemented using %s,\n",
1047 asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkinb65b3002004-09-23 22:20:47 +00001048 OUT(" * so here we adjust the DEF accordingly.\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001049 OUT(" */\n");
1050 OUT("static void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001051 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
1052 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001053 INDENT(+1);
Lev Walkine0b56e02005-02-25 12:10:27 +00001054 {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001055 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001056 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkin05363a72004-09-29 13:16:40 +00001057 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1058 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1059 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1060 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1061 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1062 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001063 if(!terminal && !tags_count) {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001064 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
Lev Walkin05363a72004-09-29 13:16:40 +00001065 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1066 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1067 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1068 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001069 OUT("/* End of these lines */\n");
1070 }
Lev Walkin05363a72004-09-29 13:16:40 +00001071 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1072 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
Lev Walkine0b56e02005-02-25 12:10:27 +00001073 if(etd_spec != ETD_NO_SPECIFICS) {
1074 INDENT(-1);
1075 OUT(" /* ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001076 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001077 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1078 if(etd_spec == ETD_NO_SPECIFICS) {
1079 INDENT(-1);
1080 OUT("\n");
1081 } else {
1082 OUT("\t// Defined explicitly */\n");
1083 }
1084 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001085 OUT("}\n");
1086 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001087
1088 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001089 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001090 OUT("void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001091 OUT("%s", p);
1092 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1093 OUT("_free(asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001094 INDENTED(
Lev Walkina9cc46e2004-09-22 16:06:28 +00001095 OUT("\tvoid *struct_ptr, int contents_only) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001096 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1097 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001098 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001099 );
1100 OUT("}\n");
1101 OUT("\n");
1102
1103 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001104 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinf15320b2004-06-03 03:38:44 +00001105 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001106 OUT("%s", p);
1107 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1108 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001109 INDENTED(
1110 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001111 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1112 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001113 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1114 );
1115 OUT("}\n");
1116 OUT("\n");
1117
1118 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001119 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkindc06f6b2004-10-20 15:50:55 +00001120 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001121 OUT("%s", p);
1122 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1123 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001124 INDENTED(
Lev Walkina9cc46e2004-09-22 16:06:28 +00001125 OUT("\tvoid **structure, void *bufptr, size_t size, int tag_mode) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001126 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1127 p, expr->_type_unique_index);
Lev Walkin05363a72004-09-29 13:16:40 +00001128 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001129 );
1130 OUT("}\n");
1131 OUT("\n");
1132
1133 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001134 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001135 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001136 OUT("%s", p);
1137 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1138 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001139 INDENTED(
1140 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1141 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001142 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1143 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001144 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1145 );
1146 OUT("}\n");
1147 OUT("\n");
1148
1149 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001150 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinca47c292004-10-23 13:34:00 +00001151 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001152 OUT("%s", p);
1153 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1154 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinca47c292004-10-23 13:34:00 +00001155 INDENTED(
1156 OUT("\tvoid **structure, const char *opt_mname, void *bufptr, size_t size) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001157 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1158 p, expr->_type_unique_index);
Lev Walkinca47c292004-10-23 13:34:00 +00001159 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1160 );
1161 OUT("}\n");
1162 OUT("\n");
1163
1164 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001165 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001166 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001167 OUT("%s", p);
1168 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1169 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001170 INDENTED(
1171 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1172 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001173 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1174 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001175 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001176 );
1177 OUT("}\n");
1178 OUT("\n");
1179
1180 REDIR(OT_FUNC_DECLS);
1181
Lev Walkin4e319f02005-03-03 21:58:28 +00001182 p = MKID_nc(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001183 if(HIDE_INNER_DEFS) {
Lev Walkin21d00002005-03-04 08:48:53 +00001184 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1185 "\t// (Use -fall-defs-global to expose) */\n",
1186 p, expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +00001187 } else {
1188 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1189 OUT("asn_struct_free_f %s_free;\n", p);
1190 OUT("asn_struct_print_f %s_print;\n", p);
1191 OUT("asn_constr_check_f %s_constraint;\n", p);
1192 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1193 OUT("der_type_encoder_f %s_encode_der;\n", p);
1194 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1195 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1196 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001197
Lev Walkin3dcaafa2004-08-11 05:21:32 +00001198 REDIR(OT_TYPE_DECLS);
1199
Lev Walkinf15320b2004-06-03 03:38:44 +00001200 return 0;
1201}
1202
1203int
1204asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1205
1206 OUT("/*\n");
1207 OUT(" * This type is extensible,\n");
1208 OUT(" * possible extensions are below.\n");
1209 OUT(" */\n");
1210
1211 return 0;
1212}
1213
Lev Walkin35631482004-07-01 00:52:50 +00001214static int check_if_extensible(asn1p_expr_t *expr) {
1215 asn1p_expr_t *v;
1216 TQ_FOR(v, &(expr->members), next) {
1217 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1218 }
1219 return 0;
1220}
1221
Lev Walkinf15320b2004-06-03 03:38:44 +00001222static int
Lev Walkinc3e29402004-09-10 06:07:18 +00001223_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001224
1225 OUT("(");
Lev Walkinc3e29402004-09-10 06:07:18 +00001226 switch(tag->tag_class) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001227 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1228 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1229 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1230 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1231 case TC_NOCLASS:
1232 break;
1233 }
Lev Walkin33c16ba2004-09-24 21:01:43 +00001234 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
Lev Walkinf15320b2004-06-03 03:38:44 +00001235
1236 return 0;
1237}
1238
Lev Walkinfd171ef2004-06-06 07:20:17 +00001239
1240static int
1241_tag2el_cmp(const void *ap, const void *bp) {
1242 const tag2el_t *a = ap;
1243 const tag2el_t *b = bp;
1244 const struct asn1p_type_tag_s *ta = &a->el_tag;
1245 const struct asn1p_type_tag_s *tb = &b->el_tag;
1246
1247 if(ta->tag_class == tb->tag_class) {
1248 if(ta->tag_value == tb->tag_value) {
1249 /*
1250 * Sort by their respective positions.
1251 */
1252 if(a->el_no < b->el_no)
1253 return -1;
1254 else if(a->el_no > b->el_no)
1255 return 1;
1256 return 0;
1257 } else if(ta->tag_value < tb->tag_value)
1258 return -1;
1259 else
1260 return 1;
1261 } else if(ta->tag_class < tb->tag_class) {
1262 return -1;
1263 } else {
1264 return 1;
1265 }
1266}
1267
Lev Walkinf15320b2004-06-03 03:38:44 +00001268/*
1269 * For constructed types, number of external tags may be greater than
1270 * number of elements in the type because of CHOICE type.
1271 * T ::= SET { -- Three possible tags:
1272 * a INTEGER, -- One tag is here...
1273 * b Choice1 -- ... and two more tags are there.
1274 * }
1275 * Choice1 ::= CHOICE {
1276 * s1 IA5String,
1277 * s2 ObjectDescriptor
1278 * }
1279 */
1280static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001281_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001282 asn1p_expr_t *expr = arg->expr;
1283 arg_t tmparg = *arg;
1284 asn1p_expr_t *v;
1285 int element = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001286 int original_count = *count;
1287 int sort_until = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001288
1289 TQ_FOR(v, &(expr->members), next) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001290 if(v->expr_type == A1TC_EXTENSIBLE) {
1291 /*
Lev Walkine0b56e02005-02-25 12:10:27 +00001292 * CXER mandates sorting
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001293 * only for the root part.
1294 */
1295 if(flags == FTE_CANONICAL_XER
1296 && sort_until == -1)
1297 sort_until = *count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001298 continue;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001299 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001300
1301 tmparg.expr = v;
1302
1303 if(_add_tag2el_member(&tmparg, tag2el, count,
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001304 (el_no==-1)?element:el_no, flags)) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001305 return -1;
1306 }
1307
1308 element++;
1309 }
1310
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001311
1312 if(flags == FTE_CANONICAL_XER) {
1313 if(sort_until == -1) sort_until = *count;
1314 qsort((*tag2el) + original_count,
1315 sort_until - original_count,
1316 sizeof(**tag2el), _tag2el_cmp);
1317 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1318 && (sort_until - original_count) >= 1) {
1319 /* Only take in account the root component */
1320 *count = original_count + 1;
1321 }
1322 } else {
1323 /*
1324 * Sort the map according to canonical order of their
1325 * tags and element numbers.
1326 */
1327 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1328 }
Lev Walkinfd171ef2004-06-06 07:20:17 +00001329
Lev Walkin38abe792004-06-14 13:09:45 +00001330 /*
1331 * Initialize .toff_{first|last} members.
1332 */
1333 if(*count) {
1334 struct asn1p_type_tag_s *cur_tag = 0;
1335 tag2el_t *cur = *tag2el;
1336 tag2el_t *end = cur + *count;
1337 int occur, i;
1338 for(occur = 0; cur < end; cur++) {
1339 if(cur_tag == 0
1340 || cur_tag->tag_value != cur->el_tag.tag_value
1341 || cur_tag->tag_class != cur->el_tag.tag_class) {
1342 cur_tag = &cur->el_tag;
1343 occur = 0;
1344 } else {
1345 occur++;
1346 }
1347 cur->toff_first = -occur;
1348 for(i = 0; i >= -occur; i--)
1349 cur[i].toff_last = -i;
1350 }
1351 }
1352
Lev Walkinf15320b2004-06-03 03:38:44 +00001353 return 0;
1354}
1355
1356static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001357_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001358 struct asn1p_type_tag_s tag;
1359 int ret;
1360
1361 assert(el_no >= 0);
1362
Lev Walkina9cc46e2004-09-22 16:06:28 +00001363 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1364 arg->expr, &tag, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001365 if(ret == 0) {
Lev Walkin201943e2004-09-04 04:42:29 +00001366 tag2el_t *te;
1367 int new_count = (*count) + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001368 void *p;
Lev Walkin201943e2004-09-04 04:42:29 +00001369
Lev Walkinac7e2292004-09-05 10:42:33 +00001370 if(tag.tag_value == -1) {
1371 /*
1372 * This is an untagged ANY type,
1373 * proceed without adding a tag
1374 */
1375 return 0;
1376 }
1377
Lev Walkin201943e2004-09-04 04:42:29 +00001378 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
Lev Walkinf15320b2004-06-03 03:38:44 +00001379 if(p) *tag2el = p;
1380 else return -1;
1381
1382 DEBUG("Found tag for %s: %ld",
1383 arg->expr->Identifier,
1384 (long)tag.tag_value);
1385
Lev Walkin201943e2004-09-04 04:42:29 +00001386 te = &((*tag2el)[*count]);
1387 te->el_tag = tag;
1388 te->el_no = el_no;
1389 te->from_expr = arg->expr;
1390 *count = new_count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001391 return 0;
1392 }
1393
1394 DEBUG("Searching tag in complex expression %s:%x at line %d",
1395 arg->expr->Identifier,
1396 arg->expr->expr_type,
1397 arg->expr->_lineno);
1398
1399 /*
1400 * Iterate over members of CHOICE type.
1401 */
1402 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001403 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001404 }
1405
1406 if(arg->expr->expr_type == A1TC_REFERENCE) {
1407 arg_t tmp = *arg;
1408 asn1p_expr_t *expr;
Lev Walkin08079b02004-08-22 03:25:24 +00001409 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001410 arg->expr->reference);
1411 if(expr) {
Lev Walkin08079b02004-08-22 03:25:24 +00001412 tmp.mod = expr->module;
Lev Walkinf15320b2004-06-03 03:38:44 +00001413 tmp.expr = expr;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001414 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001415 } else {
1416 FATAL("Cannot dereference %s at line %d",
1417 arg->expr->Identifier,
1418 arg->expr->_lineno);
1419 return -1;
1420 }
1421 }
1422
1423 DEBUG("No tag for %s at line %d",
1424 arg->expr->Identifier,
1425 arg->expr->_lineno);
1426
1427 return -1;
1428}
1429
1430static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001431emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
Lev Walkinfd171ef2004-06-06 07:20:17 +00001432 asn1p_expr_t *expr = arg->expr;
1433
Lev Walkin21d00002005-03-04 08:48:53 +00001434 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
1435 MKID_nc(expr->Identifier), expr->_type_unique_index,
1436 opt_modifier?opt_modifier:"");
Lev Walkinfd171ef2004-06-06 07:20:17 +00001437 if(tag2el_count) {
1438 int i;
1439 for(i = 0; i < tag2el_count; i++) {
1440 OUT(" { ");
Lev Walkinc3e29402004-09-10 06:07:18 +00001441 _print_tag(arg, &tag2el[i].el_tag);
Lev Walkinfd171ef2004-06-06 07:20:17 +00001442 OUT(", ");
Lev Walkin38abe792004-06-14 13:09:45 +00001443 OUT("%d, ", tag2el[i].el_no);
1444 OUT("%d, ", tag2el[i].toff_first);
1445 OUT("%d ", tag2el[i].toff_last);
Lev Walkine0b56e02005-02-25 12:10:27 +00001446 OUT("}%s /* %s at %d */\n",
1447 (i + 1 < tag2el_count) ? "," : "",
Lev Walkinfd171ef2004-06-06 07:20:17 +00001448 tag2el[i].from_expr->Identifier,
1449 tag2el[i].from_expr->_lineno
1450 );
1451 }
1452 }
1453 OUT("};\n");
1454
1455 return 0;;
1456}
1457
Lev Walkin188ed2c2004-09-13 08:31:01 +00001458static enum tvm_compat
1459emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1460 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1461 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
Lev Walkin64399722004-08-11 07:17:22 +00001462 int tags_count = 0;
Lev Walkin188ed2c2004-09-13 08:31:01 +00001463 int all_tags_count = 0;
1464 enum tvm_compat tv_mode = _TVM_SAME;
Lev Walkin906654e2004-09-10 15:49:15 +00001465 int i;
Lev Walkin27ea3802004-06-28 21:13:46 +00001466
Lev Walkin6d1220a2004-09-14 14:10:10 +00001467 /* Cleanup before proceeding. */
1468 *tags_count_r = 0;
1469 *all_tags_count_r = 0;
1470
Lev Walkin906654e2004-09-10 15:49:15 +00001471 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001472 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
Lev Walkin6d1220a2004-09-14 14:10:10 +00001473 if(tags_count < 0)
1474 return -1;
Lev Walkin64399722004-08-11 07:17:22 +00001475
Lev Walkin188ed2c2004-09-13 08:31:01 +00001476 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001477 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
Lev Walkin188ed2c2004-09-13 08:31:01 +00001478 &all_tags, AFT_FULL_COLLECT);
1479 if(all_tags_count < 0) {
1480 if(tags) free(tags);
1481 return -1;
Lev Walkin5a8219a2004-09-08 00:28:57 +00001482 }
Lev Walkin906654e2004-09-10 15:49:15 +00001483
Lev Walkin188ed2c2004-09-13 08:31:01 +00001484 assert(tags_count <= all_tags_count);
1485 assert((tags_count?0:1) == (all_tags_count?0:1));
Lev Walkin27ea3802004-06-28 21:13:46 +00001486
Lev Walkin188ed2c2004-09-13 08:31:01 +00001487 if(tags_count <= all_tags_count) {
1488 for(i = 0; i < tags_count; i++) {
1489 if(tags[i].tag_value != all_tags[i].tag_value
1490 || tags[i].tag_class != all_tags[i].tag_class) {
1491 tv_mode = _TVM_DIFFERENT;
1492 break;
1493 }
1494 }
1495 if(i == tags_count && tags_count < all_tags_count)
1496 tv_mode = _TVM_SUBSET;
1497 } else {
1498 tv_mode = _TVM_DIFFERENT;
1499 }
1500
1501#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
Lev Walkin21d00002005-03-04 08:48:53 +00001502 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
1503 MKID_nc(expr->Identifier), \
1504 expr->_type_unique_index, name); \
Lev Walkin188ed2c2004-09-13 08:31:01 +00001505 INDENT(+1); \
1506 /* Print the array of collected tags */ \
1507 for(i = 0; i < tags_count; i++) { \
1508 if(i) OUT(",\n"); \
1509 _print_tag(arg, &tags[i]); \
1510 } \
1511 OUT("\n"); \
1512 INDENT(-1); \
1513 OUT("};\n"); \
1514 } while(0)
1515
1516 if(tags_count) {
1517 if(tv_mode == _TVM_SUBSET)
1518 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1519 else
1520 EMIT_TAGS_TABLE("", tags, tags_count);
1521 }
1522
1523 if(all_tags_count) {
1524 if(tv_mode == _TVM_DIFFERENT)
1525 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1526 }
1527
1528 if(tags) free(tags);
1529 if(all_tags) free(all_tags);
1530
1531 *tags_count_r = tags_count;
1532 *all_tags_count_r = all_tags_count;
1533
1534 return tv_mode;
Lev Walkin27ea3802004-06-28 21:13:46 +00001535}
Lev Walkin59004fa2004-08-20 13:37:01 +00001536
1537static int
Lev Walkin59964be2004-08-25 02:03:12 +00001538expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001539 asn1p_expr_t *topmost_parent;
1540 asn1p_expr_t *v;
1541 int elements = 0;
1542
Lev Walkina9cc46e2004-09-22 16:06:28 +00001543 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001544 if(!topmost_parent) return 0;
1545
Lev Walkine0b56e02005-02-25 12:10:27 +00001546 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1547 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1548 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin59004fa2004-08-20 13:37:01 +00001549 return 0;
1550
1551 TQ_FOR(v, &(topmost_parent->members), next) {
1552 if(v->expr_type != A1TC_EXTENSIBLE)
1553 elements++;
1554 }
1555
1556 return elements;
1557}
1558
1559static int
Lev Walkin59964be2004-08-25 02:03:12 +00001560emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001561 int save_target;
1562 arg_t tmp_arg;
Lev Walkinb9189732004-09-10 09:37:12 +00001563 struct asn1p_type_tag_s outmost_tag_s;
1564 struct asn1p_type_tag_s *outmost_tag;
Lev Walkine0b56e02005-02-25 12:10:27 +00001565 int complex_contents;
Lev Walkin59004fa2004-08-20 13:37:01 +00001566 char *p;
1567
Lev Walkinb9189732004-09-10 09:37:12 +00001568 if(asn1f_fetch_outmost_tag(arg->asn,
1569 expr->module, expr, &outmost_tag_s, 1)) {
1570 outmost_tag = 0;
1571 } else {
1572 outmost_tag = &outmost_tag_s;
1573 }
1574
Lev Walkin59004fa2004-08-20 13:37:01 +00001575 OUT("{ ");
Lev Walkinb9189732004-09-10 09:37:12 +00001576
1577 if(outmost_tag && outmost_tag->tag_value == -1)
1578 OUT("ATF_OPEN_TYPE | ");
Lev Walkin0204fa62004-09-15 11:54:38 +00001579 OUT("%s, ", expr->marker.flags?"ATF_POINTER":"ATF_NOFLAGS");
1580 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001581 asn1p_expr_t *tv;
1582 int opts = 0;
Lev Walkin0204fa62004-09-15 11:54:38 +00001583 for(tv = expr; tv && tv->marker.flags;
Lev Walkin59004fa2004-08-20 13:37:01 +00001584 tv = TQ_NEXT(tv, next), opts++) {
1585 if(tv->expr_type == A1TC_EXTENSIBLE)
1586 opts--;
1587 }
1588 OUT("%d, ", opts);
1589 } else {
1590 OUT("0, ");
1591 }
Lev Walkina9cc46e2004-09-22 16:06:28 +00001592 if(expr->_anonymous_type) {
1593 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1594 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1595 OUT("0,\n");
1596 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001597 OUT("offsetof(struct ");
1598 out_name_chain(arg, 1);
1599 OUT(", ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001600 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1601 && (!UNNAMED_UNIONS)) OUT("choice.");
1602 OUT("%s),\n", MKID(expr->Identifier));
Lev Walkin59004fa2004-08-20 13:37:01 +00001603 }
1604 INDENT(+1);
1605 if(C99_MODE) OUT(".tag = ");
Lev Walkinb9189732004-09-10 09:37:12 +00001606 if(outmost_tag) {
1607 if(outmost_tag->tag_value == -1)
1608 OUT("-1 /* Ambiguous tag (ANY?) */");
1609 else
1610 _print_tag(arg, outmost_tag);
Lev Walkinc3e29402004-09-10 06:07:18 +00001611 } else {
Lev Walkinb9189732004-09-10 09:37:12 +00001612 OUT("-1 /* Ambiguous tag (CHOICE?) */");
Lev Walkinc3e29402004-09-10 06:07:18 +00001613 }
Lev Walkinb9189732004-09-10 09:37:12 +00001614
Lev Walkin59004fa2004-08-20 13:37:01 +00001615 OUT(",\n");
1616 if(C99_MODE) OUT(".tag_mode = ");
1617 if(expr->tag.tag_class) {
1618 if(expr->tag.tag_mode == TM_IMPLICIT)
1619 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1620 else
1621 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1622 } else {
1623 OUT("0,\n");
1624 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001625
1626 complex_contents =
1627 (expr->expr_type & ASN_CONSTR_MASK)
1628 || expr->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +00001629 || (0 /* -- prohibited by X.693:8.3.4 */
1630 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +00001631 && expr_elements_count(arg, expr));
Lev Walkin59004fa2004-08-20 13:37:01 +00001632 if(C99_MODE) OUT(".type = ");
Lev Walkin21d00002005-03-04 08:48:53 +00001633 OUT("(void *)&asn_DEF_");
1634 if(complex_contents) {
1635 OUT("%s", MKID_nc(expr->Identifier));
1636 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1637 OUT("_%d", expr->_type_unique_index);
Lev Walkin08079b02004-08-22 03:25:24 +00001638 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001639 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkin08079b02004-08-22 03:25:24 +00001640 }
Lev Walkin21d00002005-03-04 08:48:53 +00001641 OUT(",\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001642 if(C99_MODE) OUT(".memb_constraints = ");
1643 if(expr->constraints) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001644 if(arg->flags & A1C_NO_CONSTRAINTS) {
1645 OUT("0,\t/* No check because of -fno-constraints */\n");
1646 } else {
Lev Walkin4e319f02005-03-03 21:58:28 +00001647 char *id = MKID_nc(expr->Identifier);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001648 if(expr->_anonymous_type
Lev Walkin152a91e2005-02-14 20:41:29 +00001649 && !strcmp(expr->Identifier, "Member"))
Lev Walkin8de2ab22004-09-26 13:11:31 +00001650 id = asn1c_type_name(arg, expr, TNF_SAFE);
1651 OUT("memb_%s_%d_constraint,\n", id,
Lev Walkin21d00002005-03-04 08:48:53 +00001652 arg->expr->_type_unique_index);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001653 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001654 } else {
Lev Walkinb7bfd1a2005-01-17 12:16:58 +00001655 OUT("0,\t/* Defer constraints checking to the member type */\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001656 }
1657 if(C99_MODE) OUT(".name = ");
Lev Walkin152a91e2005-02-14 20:41:29 +00001658 if(1) {
1659 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1660 OUT("\"\"\n");
1661 else
1662 OUT("\"%s\"\n", expr->Identifier);
1663 } else {
1664 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1665 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001666 OUT("},\n");
1667 INDENT(-1);
1668
Lev Walkin8de2ab22004-09-26 13:11:31 +00001669 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkin59004fa2004-08-20 13:37:01 +00001670 return 0;
1671
1672 save_target = arg->target->target;
1673 REDIR(OT_CODE);
1674
Lev Walkin152a91e2005-02-14 20:41:29 +00001675 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
Lev Walkin59004fa2004-08-20 13:37:01 +00001676 p = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001677 else
Lev Walkin4e319f02005-03-03 21:58:28 +00001678 p = MKID_nc(expr->Identifier);
Lev Walkin59004fa2004-08-20 13:37:01 +00001679 OUT("static int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001680 OUT("memb_%s_%d_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n", p, arg->expr->_type_unique_index);
Lev Walkin59004fa2004-08-20 13:37:01 +00001681 INDENT(+1);
1682 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1683 tmp_arg = *arg;
1684 tmp_arg.expr = expr;
1685 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001686 OUT("return td->check_constraints"
1687 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001688 }
1689 INDENT(-1);
1690 OUT("}\n");
1691 OUT("\n");
1692
1693 REDIR(save_target);
1694
1695 return 0;
1696}
Lev Walkin59964be2004-08-25 02:03:12 +00001697
Lev Walkindc06f6b2004-10-20 15:50:55 +00001698/*
1699 * Generate "asn_DEF_XXX" type definition.
1700 */
Lev Walkin59964be2004-08-25 02:03:12 +00001701static int
Lev Walkin8de2ab22004-09-26 13:11:31 +00001702emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_count, int all_tags_count, int elements_count, enum etd_spec spec) {
Lev Walkin21d00002005-03-04 08:48:53 +00001703 int using_type_name = 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001704 char *p;
1705
Lev Walkindd32b592004-09-06 08:07:29 +00001706 if(HIDE_INNER_DEFS)
1707 OUT("static /* Use -fall-defs-global to expose */\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001708 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID_nc(expr->Identifier));
1709 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1710 OUT(" = {\n");
Lev Walkin801fabc2005-01-28 12:18:50 +00001711 p = MKID(expr->Identifier);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001712 INDENT(+1);
Lev Walkin59964be2004-08-25 02:03:12 +00001713 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001714 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001715
1716 if(expr->expr_type & ASN_CONSTR_MASK) {
Lev Walkin21d00002005-03-04 08:48:53 +00001717 using_type_name = 1;
Lev Walkin59964be2004-08-25 02:03:12 +00001718 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1719 }
1720
Lev Walkin21d00002005-03-04 08:48:53 +00001721#define FUNCREF(foo) do { \
1722 OUT("%s", p); \
1723 if(HIDE_INNER_DEFS && !using_type_name) \
1724 OUT("_%d", expr->_type_unique_index); \
1725 OUT("_" #foo ",\n"); \
1726} while(0)
1727
1728 FUNCREF(free);
1729 FUNCREF(print);
1730 FUNCREF(constraint);
1731 FUNCREF(decode_ber);
1732 FUNCREF(encode_der);
1733 FUNCREF(decode_xer);
1734 FUNCREF(encode_xer);
Lev Walkin59964be2004-08-25 02:03:12 +00001735
Lev Walkin59964be2004-08-25 02:03:12 +00001736 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1737 OUT("CHOICE_outmost_tag,\n");
1738 } else {
1739 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1740 }
1741
Lev Walkin4e319f02005-03-03 21:58:28 +00001742 p = MKID_nc(expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001743 if(tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001744 OUT("asn_DEF_%s_%d_tags,\n",
1745 p, expr->_type_unique_index);
1746 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1747 p, expr->_type_unique_index);
1748 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1749 p, expr->_type_unique_index);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001750 if(tv_mode == _TVM_SUBSET
1751 && tags_count != all_tags_count)
1752 OUT(" - %d", all_tags_count - tags_count);
1753 OUT(", /* %d */\n", tags_count);
Lev Walkin59964be2004-08-25 02:03:12 +00001754 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001755 OUT("0,\t/* No effective tags (pointer) */\n");
1756 OUT("0,\t/* No effective tags (count) */\n");
1757 }
1758
1759 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
Lev Walkin21d00002005-03-04 08:48:53 +00001760 OUT("asn_DEF_%s_%d_all_tags,\n",
1761 p, expr->_type_unique_index);
1762 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1763 p, expr->_type_unique_index);
1764 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1765 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001766 } else if(all_tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001767 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1768 p, expr->_type_unique_index);
1769 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1770 p, expr->_type_unique_index);
1771 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1772 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001773 } else {
1774 OUT("0,\t/* No tags (pointer) */\n");
1775 OUT("0,\t/* No tags (count) */\n");
Lev Walkin59964be2004-08-25 02:03:12 +00001776 }
1777
Lev Walkin59964be2004-08-25 02:03:12 +00001778 if(elements_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001779 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001780 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1781 || expr->expr_type == ASN_CONSTR_SET_OF) {
1782 OUT("%d,\t/* Single element */\n",
1783 elements_count);
1784 assert(elements_count == 1);
1785 } else {
1786 OUT("%d,\t/* Elements count */\n",
1787 elements_count);
1788 }
1789 } else {
Lev Walkin59964be2004-08-25 02:03:12 +00001790 if(expr_elements_count(arg, expr))
1791 OUT("0, 0,\t/* Defined elsewhere */\n");
1792 else
1793 OUT("0, 0,\t/* No members */\n");
1794 }
1795
1796 switch(spec) {
1797 case ETD_NO_SPECIFICS:
1798 OUT("0\t/* No specifics */\n");
1799 break;
1800 case ETD_HAS_SPECIFICS:
Lev Walkin21d00002005-03-04 08:48:53 +00001801 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1802 p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001803 }
Lev Walkin188ed2c2004-09-13 08:31:01 +00001804 INDENT(-1);
Lev Walkin59964be2004-08-25 02:03:12 +00001805 OUT("};\n");
1806 OUT("\n");
Lev Walkin97298782004-08-26 06:20:34 +00001807
1808 return 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001809}
Lev Walkincc93b0f2004-09-10 09:18:20 +00001810
1811/*
1812 * Check if it is better to make this type indirectly accessed via
1813 * a pointer.
1814 * This may be the case for the following recursive definition:
1815 * Type ::= CHOICE { member Type };
1816 */
1817static int
1818expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) {
1819 asn1p_expr_t *top_parent;
1820 asn1p_expr_t *terminal;
1821
1822 if(expr->expr_type != A1TC_REFERENCE)
1823 return 0;
1824
1825 /* Rewind to the topmost parent expression */
1826 if((top_parent = expr->parent_expr)) {
1827 while(top_parent->parent_expr)
1828 top_parent = top_parent->parent_expr;
1829 } else {
1830 return 0;
1831 }
1832
Lev Walkina9cc46e2004-09-22 16:06:28 +00001833 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkincc93b0f2004-09-10 09:18:20 +00001834
1835 return (terminal == top_parent);
1836}
Lev Walkina9cc46e2004-09-22 16:06:28 +00001837
1838static int
1839expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1840 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1841 if(!expr) return 0;
1842
1843 /* X.680, 25.5, Table 5 */
1844 switch(expr->expr_type) {
1845 case ASN_CONSTR_CHOICE:
1846 case ASN_BASIC_BOOLEAN:
1847 case ASN_BASIC_ENUMERATED:
1848 case ASN_BASIC_NULL:
1849 return 1;
1850 default:
1851 return 0;
1852 }
1853}
Lev Walkin801fabc2005-01-28 12:18:50 +00001854
1855static int
Lev Walkin21d00002005-03-04 08:48:53 +00001856out_name_chain(arg_t *arg, int check_reserved_keywords) {
Lev Walkin801fabc2005-01-28 12:18:50 +00001857 asn1p_expr_t *expr = arg->expr;
1858 char *id;
1859
1860 assert(expr->Identifier);
1861
Lev Walkin21d00002005-03-04 08:48:53 +00001862 if(arg->flags & A1C_COMPOUND_NAMES
1863 && ((expr->expr_type & ASN_CONSTR_MASK)
1864 || expr->expr_type == ASN_BASIC_ENUMERATED
1865 || (expr->expr_type == ASN_BASIC_INTEGER
1866 && expr_elements_count(arg, expr))
1867 )
Lev Walkin801fabc2005-01-28 12:18:50 +00001868 && expr->parent_expr
1869 && expr->parent_expr->Identifier) {
1870 arg_t tmparg = *arg;
1871
1872 tmparg.expr = expr->parent_expr;
Lev Walkin21d00002005-03-04 08:48:53 +00001873 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
1874
1875 out_name_chain(&tmparg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +00001876
1877 OUT("_"); /* a separator between id components */
Lev Walkin21d00002005-03-04 08:48:53 +00001878
Lev Walkin801fabc2005-01-28 12:18:50 +00001879 /* Fall through */
1880 }
1881
1882 if(check_reserved_keywords)
1883 id = MKID(expr->Identifier);
1884 else
Lev Walkin4e319f02005-03-03 21:58:28 +00001885 id = MKID_nc(expr->Identifier);
Lev Walkin801fabc2005-01-28 12:18:50 +00001886 OUT("%s", id);
1887
1888 return 0;
1889}