blob: 58b76f6398c2333ed345de776954f69268bc026d [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 Walkinc8285712005-03-04 22:18:20 +000038static int emit_include_dependencies(arg_t *arg);
Lev Walkin21d00002005-03-04 08:48:53 +000039static int out_name_chain(arg_t *arg, int check_reserved_keywords);
Lev Walkinf15320b2004-06-03 03:38:44 +000040
Lev Walkin188ed2c2004-09-13 08:31:01 +000041enum tvm_compat {
42 _TVM_SAME = 0, /* tags and all_tags are same */
43 _TVM_SUBSET = 1, /* tags are subset of all_tags */
44 _TVM_DIFFERENT = 2, /* tags and all_tags are different */
45};
46static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc);
47
Lev Walkin59964be2004-08-25 02:03:12 +000048enum etd_spec {
49 ETD_NO_SPECIFICS,
50 ETD_HAS_SPECIFICS
51};
Lev Walkin8de2ab22004-09-26 13:11:31 +000052static 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 +000053
Lev Walkin59004fa2004-08-20 13:37:01 +000054#define C99_MODE (!(arg->flags & A1C_NO_C99))
Lev Walkinf15320b2004-06-03 03:38:44 +000055#define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS)
Lev Walkindd32b592004-09-06 08:07:29 +000056#define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL))
Lev Walkinf15320b2004-06-03 03:38:44 +000057
58#define PCTX_DEF INDENTED( \
59 OUT("\n"); \
60 OUT("/* Context for parsing across buffer boundaries */\n"); \
Lev Walkin05363a72004-09-29 13:16:40 +000061 OUT("asn_struct_ctx_t _asn_ctx;\n"));
Lev Walkinf15320b2004-06-03 03:38:44 +000062
Lev Walkinc8285712005-03-04 22:18:20 +000063
Lev Walkin3dcaafa2004-08-11 05:21:32 +000064#define DEPENDENCIES do { \
Lev Walkinc8285712005-03-04 22:18:20 +000065 emit_include_dependencies(arg); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000066 if(expr->expr_type == ASN_CONSTR_SET_OF) \
67 GEN_INCLUDE("asn_SET_OF"); \
68 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \
69 GEN_INCLUDE("asn_SEQUENCE_OF"); \
70} while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +000071
Lev Walkincaf0d5a2005-03-04 23:48:19 +000072/* MKID_safe() without checking for reserved keywords */
73#define MKID(id) asn1c_make_identifier(0, (id), 0)
74#define MKID_safe(id) asn1c_make_identifier(AMI_CHECK_RESERVED, (id), 0)
Lev Walkinf15320b2004-06-03 03:38:44 +000075
76int
Lev Walkinc78cbfb2004-09-14 12:47:45 +000077asn1c_lang_C_type_REAL(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +000078 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
79}
80
Lev Walkine0b56e02005-02-25 12:10:27 +000081struct value2enum {
82 asn1c_integer_t value;
83 const char *name;
84 int idx;
85};
86static int compar_enumMap_byName(const void *ap, const void *bp) {
87 const struct value2enum *a = (const struct value2enum *)ap;
88 const struct value2enum *b = (const struct value2enum *)bp;
89 return strcmp(a->name, b->name);
90}
91static int compar_enumMap_byValue(const void *ap, const void *bp) {
92 const struct value2enum *a = (const struct value2enum *)ap;
93 const struct value2enum *b = (const struct value2enum *)bp;
94 if(a->value < b->value)
95 return -1;
96 else if(a->value == b->value)
97 return 0;
98 return 1;
99}
100
Lev Walkinf15320b2004-06-03 03:38:44 +0000101int
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000102asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 asn1p_expr_t *expr = arg->expr;
104 asn1p_expr_t *v;
Lev Walkine0b56e02005-02-25 12:10:27 +0000105 int el_count = expr_elements_count(arg, expr);
106 struct value2enum *v2e;
107 int map_is_extensible = (expr->expr_type == ASN_BASIC_INTEGER);
Lev Walkinf15320b2004-06-03 03:38:44 +0000108
Lev Walkine0b56e02005-02-25 12:10:27 +0000109 v2e = alloca((el_count + 1) * sizeof(*v2e));
Lev Walkinf15320b2004-06-03 03:38:44 +0000110
Lev Walkine0b56e02005-02-25 12:10:27 +0000111 /*
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000112 * For all ENUMERATED types [and for those INTEGER types which
113 * have identifiers -- prohibited by X.693:8.3.4],
114 * print out an enumeration table and a mapping
Lev Walkine0b56e02005-02-25 12:10:27 +0000115 * between identifiers and associated values.
116 */
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000117 if(expr->expr_type == ASN_BASIC_ENUMERATED
118 || (0 && el_count /* -- prohibited by X.693:8.3.4 */)) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000119 int eidx = 0;
120
121 REDIR(OT_DEPS);
Lev Walkin21d00002005-03-04 08:48:53 +0000122 OUT("typedef enum ");
123 out_name_chain(arg, 1);
124 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 TQ_FOR(v, &(expr->members), next) {
126 switch(v->expr_type) {
127 case A1TC_UNIVERVAL:
Lev Walkin21d00002005-03-04 08:48:53 +0000128 OUT("\t");
129 out_name_chain(arg, 0);
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000130 OUT("_%s", MKID(v->Identifier));
Lev Walkin21d00002005-03-04 08:48:53 +0000131 OUT("\t= %" PRIdASN "%s\n",
Lev Walkine0b56e02005-02-25 12:10:27 +0000132 v->value->value.v_integer,
Lev Walkin21d00002005-03-04 08:48:53 +0000133 (eidx+1 < el_count) ? "," : "");
Lev Walkine0b56e02005-02-25 12:10:27 +0000134 v2e[eidx].name = v->Identifier;
135 v2e[eidx].value = v->value->value.v_integer;
136 eidx++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000137 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000138 case A1TC_EXTENSIBLE:
139 OUT("\t/*\n");
140 OUT("\t * Enumeration is extensible\n");
141 OUT("\t */\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000142 map_is_extensible = 1;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000143 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 default:
145 return -1;
146 }
147 }
Lev Walkin21d00002005-03-04 08:48:53 +0000148 OUT("} ");
149 out_name_chain(arg, 0);
150 OUT("_e;\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000151 assert(eidx == el_count);
152
153 /*
154 * Generate a enumerationName<->value map for XER codec.
155 */
156 REDIR(OT_STAT_DEFS);
157
Lev Walkin21d00002005-03-04 08:48:53 +0000158 OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_%d_value2enum[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000159 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000160 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
161 for(eidx = 0; eidx < el_count; eidx++) {
162 v2e[eidx].idx = eidx;
163 OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n",
164 v2e[eidx].value,
165 (long)strlen(v2e[eidx].name), v2e[eidx].name,
166 (eidx + 1 < el_count) ? "," : "");
167 }
168 if(map_is_extensible)
169 OUT("\t/* This list is extensible */\n");
170 OUT("};\n");
171
Lev Walkin21d00002005-03-04 08:48:53 +0000172 OUT("static unsigned int asn_MAP_%s_%d_enum2value[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000173 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000174 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
175 for(eidx = 0; eidx < el_count; eidx++) {
176 OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n",
177 v2e[eidx].idx,
178 (eidx + 1 < el_count) ? "," : "",
179 v2e[eidx].name, v2e[eidx].value);
180 }
181 if(map_is_extensible)
182 OUT("\t/* This list is extensible */\n");
183 OUT("};\n");
184
Lev Walkin21d00002005-03-04 08:48:53 +0000185 OUT("static asn_INTEGER_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000186 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000187 INDENT(+1);
Lev Walkin21d00002005-03-04 08:48:53 +0000188 OUT("asn_MAP_%s_%d_value2enum,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000189 "/* \"tag\" => N; sorted by tag */\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000190 MKID(expr->Identifier),
Lev Walkin21d00002005-03-04 08:48:53 +0000191 expr->_type_unique_index);
192 OUT("asn_MAP_%s_%d_enum2value,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000193 "/* N => \"tag\"; sorted by N */\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000194 MKID(expr->Identifier),
Lev Walkin21d00002005-03-04 08:48:53 +0000195 expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000196 OUT("%d,\t/* Number of elements in the maps */\n",
197 el_count);
198 OUT("%d,\t/* Enumeration is %sextensible */\n",
199 map_is_extensible, map_is_extensible ? "": "not ");
200 if(expr->expr_type == ASN_BASIC_ENUMERATED)
201 OUT("1\t/* Strict enumeration */\n");
202 else
203 OUT("0\n");
204 INDENT(-1);
205 OUT("};\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000206 }
207
208 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
209}
210
211int
212asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
213 asn1p_expr_t *expr = arg->expr;
214 asn1p_expr_t *v;
215 int comp_mode = 0; /* {root,ext=1,root,root,...} */
216
217 DEPENDENCIES;
218
219 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000220 OUT("struct ");
221 out_name_chain(arg, 1);
222 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 } else {
224 OUT("typedef struct %s {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000225 MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000226 }
227
228 TQ_FOR(v, &(expr->members), next) {
229 if(v->expr_type == A1TC_EXTENSIBLE) {
230 if(comp_mode < 3) comp_mode++;
231 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000232 if(comp_mode == 1
233 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000234 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000235 EMBED(v);
236 }
237
238 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000239 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000240 expr->_anonymous_type ? "" :
241 arg->embed
242 ? MKID_safe(expr->Identifier)
243 : MKID(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000244 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000245
246 return asn1c_lang_C_type_SEQUENCE_def(arg);
247}
248
249static int
250asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
251 asn1p_expr_t *expr = arg->expr;
252 asn1p_expr_t *v;
253 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 int ext_start = -1;
255 int ext_stop = -1;
Lev Walkinfd171ef2004-06-06 07:20:17 +0000256 tag2el_t *tag2el = NULL;
257 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000258 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000259 int all_tags_count;
260 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000261
Lev Walkinfd171ef2004-06-06 07:20:17 +0000262 /*
263 * Fetch every inner tag from the tag to elements map.
264 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000265 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinfd171ef2004-06-06 07:20:17 +0000266 if(tag2el) free(tag2el);
267 return -1;
268 }
269
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000270 GEN_INCLUDE("constr_SEQUENCE");
271 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000272 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000273
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000274 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000275
276 /*
277 * Print out the table according to which the parsing is performed.
278 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000279 if(expr_elements_count(arg, expr)) {
280 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000281
Lev Walkin21d00002005-03-04 08:48:53 +0000282 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000283 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000284
285 elements = 0;
286 INDENTED(TQ_FOR(v, &(expr->members), next) {
287 if(v->expr_type == A1TC_EXTENSIBLE) {
288 if((++comp_mode) == 1)
289 ext_start = elements - 1;
290 else
291 ext_stop = elements - 1;
292 continue;
293 }
294 elements++;
295 emit_member_table(arg, v);
296 });
297 OUT("};\n");
298 } else {
299 elements = 0;
300 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000301
Lev Walkin27ea3802004-06-28 21:13:46 +0000302 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000303 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000304 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000305 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000306
Lev Walkinfd171ef2004-06-06 07:20:17 +0000307 /*
308 * Tags to elements map.
309 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000310 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinfd171ef2004-06-06 07:20:17 +0000311
Lev Walkin21d00002005-03-04 08:48:53 +0000312 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000313 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000314 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000315 OUT("sizeof(struct "); out_name_chain(arg, 1); OUT("),\n");
316 OUT("offsetof(struct "); out_name_chain(arg, 1); OUT(", _asn_ctx),\n");
317
Lev Walkinf3b29b12005-06-02 05:21:37 +0000318 if(tag2el_count) {
319 OUT("asn_MAP_%s_%d_tag2el,\n",
320 MKID(expr->Identifier),
321 expr->_type_unique_index);
322 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
323 } else {
324 OUT("0,\t/* No top level tags */\n");
325 OUT("0,\t/* No tags in the map */\n");
326 }
Lev 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 Walkincaf0d5a2005-03-04 23:48:19 +0000370 id = MKID(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 Walkincaf0d5a2005-03-04 23:48:19 +0000385 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000386 }
387
388 TQ_FOR(v, &(expr->members), next) {
389 if(v->expr_type == A1TC_EXTENSIBLE) {
390 if(comp_mode < 3) comp_mode++;
391 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000392 if(comp_mode == 1
393 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000394 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000395 EMBED(v);
396 }
397
398 INDENTED(
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000399 id = MKID(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 OUT("\n");
401 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
402 id, id);
403 OUT("unsigned int _presence_map\n");
404 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
405 );
406
407 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000408 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000409 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000410 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000411
412 return asn1c_lang_C_type_SET_def(arg);
413}
414
Lev Walkinf15320b2004-06-03 03:38:44 +0000415static int
416asn1c_lang_C_type_SET_def(arg_t *arg) {
417 asn1p_expr_t *expr = arg->expr;
418 asn1p_expr_t *v;
419 int elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000420 tag2el_t *tag2el = NULL;
421 int tag2el_count = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000422 tag2el_t *tag2el_cxer = NULL;
423 int tag2el_cxer_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000424 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000425 int all_tags_count;
426 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000427 char *p;
428
429 /*
430 * Fetch every inner tag from the tag to elements map.
431 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000432 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000433 if(tag2el) free(tag2el);
434 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000435 }
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000436 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
437 if(tag2el) free(tag2el);
438 if(tag2el_cxer) free(tag2el_cxer);
439 return -1;
440 }
441 if(tag2el_cxer_count == tag2el_count
442 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
443 free(tag2el_cxer);
444 tag2el_cxer = 0;
445 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000446
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000447 GEN_INCLUDE("constr_SET");
448 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000449 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000450
451 REDIR(OT_STAT_DEFS);
452
Lev Walkinf15320b2004-06-03 03:38:44 +0000453 /*
454 * Print out the table according to which the parsing is performed.
455 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000456 if(expr_elements_count(arg, expr)) {
457 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000458
Lev Walkin21d00002005-03-04 08:48:53 +0000459 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000460 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000461
462 elements = 0;
463 INDENTED(TQ_FOR(v, &(expr->members), next) {
464 if(v->expr_type == A1TC_EXTENSIBLE) {
465 if(comp_mode < 3) comp_mode++;
466 } else {
467 if(comp_mode == 1
468 || expr_better_indirect(arg, v))
469 v->marker.flags |= EM_INDIRECT;
470 elements++;
471 emit_member_table(arg, v);
472 }
473 });
474 OUT("};\n");
475 } else {
476 elements = 0;
477 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000478
Lev Walkin27ea3802004-06-28 21:13:46 +0000479 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000480 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000481 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000482 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000483
484 /*
485 * Tags to elements map.
486 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000487 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
488 if(tag2el_cxer)
489 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
Lev Walkinf15320b2004-06-03 03:38:44 +0000490
491 /*
492 * Emit a map of mandatory elements.
493 */
Lev Walkin21d00002005-03-04 08:48:53 +0000494 OUT("static uint8_t asn_MAP_%s_%d_mmap",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000495 MKID(expr->Identifier), expr->_type_unique_index);
496 p = MKID_safe(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000497 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
Lev Walkin21d00002005-03-04 08:48:53 +0000498 OUT(" = {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000499 INDENTED(
500 if(elements) {
501 int delimit = 0;
502 int el = 0;
503 TQ_FOR(v, &(expr->members), next) {
504 if(v->expr_type == A1TC_EXTENSIBLE) continue;
505 if(delimit) {
506 OUT(",\n");
507 delimit = 0;
508 } else if(el) {
509 OUT(" | ");
510 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000511 OUT("(%d << %d)",
Lev Walkin0204fa62004-09-15 11:54:38 +0000512 v->marker.flags?0:1,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000513 7 - (el % 8));
Lev Walkinf15320b2004-06-03 03:38:44 +0000514 if(el && (el % 8) == 0)
515 delimit = 1;
516 el++;
517 }
518 } else {
519 OUT("0");
520 }
521 );
522 OUT("\n");
523 OUT("};\n");
524
Lev Walkin21d00002005-03-04 08:48:53 +0000525 OUT("static asn_SET_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000526 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000527 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000528 OUT("sizeof(struct ");
529 out_name_chain(arg, 1);
530 OUT("),\n");
531 OUT("offsetof(struct ");
532 out_name_chain(arg, 1);
533 OUT(", _asn_ctx),\n");
534 OUT("offsetof(struct ");
535 out_name_chain(arg, 1);
536 OUT(", _presence_map),\n");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000537 p = MKID(expr->Identifier);
Lev Walkin21d00002005-03-04 08:48:53 +0000538 OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000539 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000540 if(tag2el_cxer)
Lev Walkin21d00002005-03-04 08:48:53 +0000541 OUT("asn_MAP_%s_%d_tag2el_cxer,\n",
542 p, expr->_type_unique_index);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000543 else
Lev Walkin21d00002005-03-04 08:48:53 +0000544 OUT("asn_MAP_%s_%d_tag2el,\t/* Same as above */\n",
545 p, expr->_type_unique_index);
546 OUT("%d,\t/* Count of tags in the CXER map */\n",
547 tag2el_cxer_count);
Lev Walkin35631482004-07-01 00:52:50 +0000548 OUT("%d,\t/* Whether extensible */\n",
549 check_if_extensible(expr));
Lev Walkin21d00002005-03-04 08:48:53 +0000550 OUT("(unsigned int *)asn_MAP_%s_%d_mmap\t/* Mandatory elements map */\n",
551 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 );
553 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000554
555 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000556 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000557 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000558 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000559 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000560
Lev Walkinf15320b2004-06-03 03:38:44 +0000561 REDIR(OT_TYPE_DECLS);
562
563 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000564} /* _SET_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000565
566int
Lev Walkin08079b02004-08-22 03:25:24 +0000567asn1c_lang_C_type_SEx_OF(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000568 asn1p_expr_t *expr = arg->expr;
Lev Walkinc8285712005-03-04 22:18:20 +0000569 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
Lev Walkinf15320b2004-06-03 03:38:44 +0000570
571 DEPENDENCIES;
572
573 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000574 OUT("struct ");
575 out_name_chain(arg, 1);
576 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000578 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000579 }
580
Lev Walkin08079b02004-08-22 03:25:24 +0000581 INDENT(+1);
582 OUT("A_%s_OF(",
583 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
584 ? "SET" : "SEQUENCE");
Lev Walkine0b56e02005-02-25 12:10:27 +0000585 if(memb->expr_type & ASN_CONSTR_MASK
586 || ((memb->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000587 || (0 /* -- prohibited by X.693:8.3.4 */
588 && memb->expr_type == ASN_BASIC_INTEGER))
589 && expr_elements_count(arg, memb))) {
Lev Walkin08079b02004-08-22 03:25:24 +0000590 arg_t tmp;
591 asn1p_expr_t tmp_memb;
592 arg->embed++;
593 tmp = *arg;
594 tmp.expr = &tmp_memb;
595 tmp_memb = *memb;
596 tmp_memb._anonymous_type = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000597 if(tmp_memb.Identifier == 0) {
Lev Walkin21d00002005-03-04 08:48:53 +0000598 tmp_memb.Identifier = "Member";
599 if(0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000600 tmp_memb.Identifier = strdup(
601 asn1c_make_identifier(0,
Lev Walkin152a91e2005-02-14 20:41:29 +0000602 expr->Identifier, "Member", 0));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000603 assert(tmp_memb.Identifier);
604 }
Lev Walkin08079b02004-08-22 03:25:24 +0000605 tmp.default_cb(&tmp);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000606 if(tmp_memb.Identifier != memb->Identifier)
Lev Walkin21d00002005-03-04 08:48:53 +0000607 if(0) free(tmp_memb.Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000608 arg->embed--;
609 assert(arg->target->target == OT_TYPE_DECLS);
610 } else {
Lev Walkinfe7f6ec2005-03-04 22:38:22 +0000611 OUT("%s", asn1c_type_name(arg, memb,
612 (memb->marker.flags & EM_UNRECURSE)
613 ? TNF_RSAFE : TNF_CTYPE));
Lev Walkinf15320b2004-06-03 03:38:44 +0000614 }
Lev Walkin08079b02004-08-22 03:25:24 +0000615 OUT(") list;\n");
616 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000617
618 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000619 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000620 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000621 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000622
623 /*
Lev Walkin08079b02004-08-22 03:25:24 +0000624 * SET OF/SEQUENCE OF definition
Lev Walkinf15320b2004-06-03 03:38:44 +0000625 */
Lev Walkin08079b02004-08-22 03:25:24 +0000626 return asn1c_lang_C_type_SEx_OF_def(arg,
627 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
Lev Walkinf15320b2004-06-03 03:38:44 +0000628}
629
630static int
631asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
632 asn1p_expr_t *expr = arg->expr;
633 asn1p_expr_t *v;
Lev Walkin64399722004-08-11 07:17:22 +0000634 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000635 int all_tags_count;
636 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000637
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000638 /*
639 * Print out the table according to which the parsing is performed.
640 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000641 if(seq_of) {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000642 GEN_INCLUDE("constr_SEQUENCE_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000643 } else {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000644 GEN_INCLUDE("constr_SET_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000645 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000646 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000647 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000648
649 REDIR(OT_STAT_DEFS);
650
651 /*
652 * Print out the table according to which the parsing is performed.
653 */
Lev Walkin21d00002005-03-04 08:48:53 +0000654 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
655 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000656 INDENT(+1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000657 v = TQ_FIRST(&(expr->members));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000658 if(!v->Identifier) {
Lev Walkin152a91e2005-02-14 20:41:29 +0000659 v->Identifier = strdup("Member");
Lev Walkina9cc46e2004-09-22 16:06:28 +0000660 assert(v->Identifier);
661 }
662 v->_anonymous_type = 1;
663 arg->embed++;
Lev Walkin59964be2004-08-25 02:03:12 +0000664 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000665 arg->embed--;
666 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000667 OUT("};\n");
668
Lev Walkin27ea3802004-06-28 21:13:46 +0000669 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000670 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000671 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000672 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000673
Lev Walkin21d00002005-03-04 08:48:53 +0000674 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000675 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000676 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000677 OUT("sizeof(struct ");
678 out_name_chain(arg, 1);
679 OUT("),\n");
680 OUT("offsetof(struct ");
681 out_name_chain(arg, 1);
682 OUT(", _asn_ctx),\n");
683
Lev Walkina9cc46e2004-09-22 16:06:28 +0000684 if(expr_as_xmlvaluelist(arg, v))
685 OUT("1,\t/* XER encoding is XMLValueList */\n");
686 else
687 OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000688 );
689 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000690
691 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000692 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000693 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000694 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000695 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000696
Lev Walkinf15320b2004-06-03 03:38:44 +0000697 REDIR(OT_TYPE_DECLS);
698
699 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000700} /* _SEx_OF_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000701
702int
703asn1c_lang_C_type_CHOICE(arg_t *arg) {
704 asn1p_expr_t *expr = arg->expr;
705 asn1p_expr_t *v;
Lev Walkin08079b02004-08-22 03:25:24 +0000706 char *id;
Lev Walkinf15320b2004-06-03 03:38:44 +0000707
708 DEPENDENCIES;
709
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000710 REDIR(OT_DEPS);
711
Lev Walkin21d00002005-03-04 08:48:53 +0000712 OUT("typedef enum ");
713 out_name_chain(arg, 0);
714 OUT("_PR {\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000715 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000716 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000717 OUT("_PR_NOTHING,\t/* No components present */\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000718 TQ_FOR(v, &(expr->members), next) {
719 if(v->expr_type == A1TC_EXTENSIBLE) {
720 OUT("/* Extensions may appear below */\n");
721 continue;
722 }
Lev Walkin21d00002005-03-04 08:48:53 +0000723 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000724 OUT("_PR_");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000725 id = MKID(v->Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000726 OUT("%s,\n", id, id);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000727 }
728 );
Lev Walkin21d00002005-03-04 08:48:53 +0000729 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000730
731 REDIR(OT_TYPE_DECLS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000732
733 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000734 OUT("struct "); out_name_chain(arg, 1); OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000735 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000736 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000737 }
738
739 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000740 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000741 OUT("_PR present;\n");
742 OUT("union {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000743 TQ_FOR(v, &(expr->members), next) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000744 if(expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000745 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000746 EMBED(v);
747 }
748 if(UNNAMED_UNIONS) OUT("};\n");
749 else OUT("} choice;\n");
750 );
751
752 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000753 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000754 expr->_anonymous_type ? "" :
755 arg->embed
756 ? MKID_safe(expr->Identifier)
757 : MKID(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000758 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000759
760 return asn1c_lang_C_type_CHOICE_def(arg);
761}
762
763static int
764asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
765 asn1p_expr_t *expr = arg->expr;
766 asn1p_expr_t *v;
767 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000768 tag2el_t *tag2el = NULL;
769 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000770 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000771 int all_tags_count;
772 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000773
774 /*
775 * Fetch every inner tag from the tag to elements map.
776 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000777 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000778 if(tag2el) free(tag2el);
779 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000780 }
781
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000782 GEN_INCLUDE("constr_CHOICE");
783 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000784 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000785
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000786 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000787
788 /*
789 * Print out the table according to which the parsing is performed.
790 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000791 if(expr_elements_count(arg, expr)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000792
Lev Walkin21d00002005-03-04 08:48:53 +0000793 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
794 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000795
796 elements = 0;
797 INDENTED(TQ_FOR(v, &(expr->members), next) {
Lev Walkin659c63b2005-01-17 11:40:49 +0000798 if(v->expr_type == A1TC_EXTENSIBLE)
799 continue;
800 if(expr_better_indirect(arg, v))
801 v->marker.flags |= EM_INDIRECT;
802 elements++;
803 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000804 });
805 OUT("};\n");
806 } else {
807 elements = 0;
808 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000809
Lev Walkin64399722004-08-11 07:17:22 +0000810
Lev Walkinf15320b2004-06-03 03:38:44 +0000811 if(arg->embed) {
812 /*
813 * Our parent structure has already taken this into account.
814 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000815 tv_mode = _TVM_SAME;
816 tags_count = all_tags_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000817 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000818 tv_mode = emit_tags_vectors(arg, expr,
819 &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000820 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000821
822 /*
823 * Tags to elements map.
824 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000825 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000826
Lev Walkin21d00002005-03-04 08:48:53 +0000827 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000828 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000829 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000830 OUT("sizeof(struct ");
831 out_name_chain(arg, 1);
832 OUT("),\n");
833 OUT("offsetof(struct ");
834 out_name_chain(arg, 1);
835 OUT(", _asn_ctx),\n");
836 OUT("offsetof(struct ");
837 out_name_chain(arg, 1);
838 OUT(", present),\n");
839 OUT("sizeof(((struct ");
840 out_name_chain(arg, 1);
841 OUT(" *)0)->present),\n");
842 OUT("asn_MAP_%s_%d_tag2el,\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000843 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000844 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin35631482004-07-01 00:52:50 +0000845 OUT("%d\t/* Whether extensible */\n",
846 check_if_extensible(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000847 );
848 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000849
850 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000851 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000852 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000853 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000854 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000855
Lev Walkinf15320b2004-06-03 03:38:44 +0000856 REDIR(OT_TYPE_DECLS);
857
858 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000859} /* _CHOICE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000860
861int
862asn1c_lang_C_type_REFERENCE(arg_t *arg) {
863 asn1p_ref_t *ref;
864
865 ref = arg->expr->reference;
866 if(ref->components[ref->comp_count-1].name[0] == '&') {
Lev Walkinf15320b2004-06-03 03:38:44 +0000867 asn1p_expr_t *extract;
868 arg_t tmp;
869 int ret;
870
Lev Walkina9cc46e2004-09-22 16:06:28 +0000871 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
Lev Walkin08079b02004-08-22 03:25:24 +0000872 arg->expr, ref);
Lev Walkinf15320b2004-06-03 03:38:44 +0000873 if(extract == NULL)
874 return -1;
875
Lev Walkin08079b02004-08-22 03:25:24 +0000876 extract = asn1p_expr_clone(extract, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000877 if(extract) {
878 if(extract->Identifier)
879 free(extract->Identifier);
880 extract->Identifier = strdup(arg->expr->Identifier);
881 if(extract->Identifier == NULL) {
882 asn1p_expr_free(extract);
883 return -1;
884 }
885 } else {
886 return -1;
887 }
888
889 tmp = *arg;
890 tmp.asn = arg->asn;
Lev Walkin08079b02004-08-22 03:25:24 +0000891 tmp.mod = extract->module;
Lev Walkinf15320b2004-06-03 03:38:44 +0000892 tmp.expr = extract;
893
894 ret = arg->default_cb(&tmp);
895
896 asn1p_expr_free(extract);
897
898 return ret;
899 }
900
901
902 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
903}
904
905int
906asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
907 asn1p_expr_t *expr = arg->expr;
Lev Walkin64399722004-08-11 07:17:22 +0000908 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000909 int all_tags_count;
910 enum tvm_compat tv_mode;
Lev Walkine0b56e02005-02-25 12:10:27 +0000911 enum etd_spec etd_spec;
Lev Walkinf15320b2004-06-03 03:38:44 +0000912 char *p;
913
914 if(arg->embed) {
Lev Walkin05363a72004-09-29 13:16:40 +0000915 enum tnfmt tnfmt = TNF_CTYPE;
916
917 /*
918 * If this is an optional compound type,
919 * refer it using "struct X" convention,
920 * as it may recursively include the current structure.
921 */
Lev Walkinc8285712005-03-04 22:18:20 +0000922 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
Lev Walkin05363a72004-09-29 13:16:40 +0000923 asn1p_expr_t *terminal;
924 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
925 if(terminal
926 && (terminal->expr_type & ASN_CONSTR_MASK)) {
Lev Walkinc8285712005-03-04 22:18:20 +0000927 tnfmt = TNF_RSAFE;
928 REDIR(OT_FWD_DECLS);
Lev Walkinbf56d542005-03-04 23:50:56 +0000929 OUT("%s;\n",
Lev Walkinc8285712005-03-04 22:18:20 +0000930 asn1c_type_name(arg, arg->expr, tnfmt));
Lev Walkin05363a72004-09-29 13:16:40 +0000931 }
932 }
933
Lev Walkinf15320b2004-06-03 03:38:44 +0000934 REDIR(OT_TYPE_DECLS);
935
Lev Walkinc8285712005-03-04 22:18:20 +0000936 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
Lev Walkine0b56e02005-02-25 12:10:27 +0000937 if(!expr->_anonymous_type) {
Lev Walkinc8285712005-03-04 22:18:20 +0000938 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000939 OUT("%s", MKID_safe(expr->Identifier));
Lev Walkine0b56e02005-02-25 12:10:27 +0000940 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",
Lev Walkinc8285712005-03-04 22:18:20 +0000954 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
Lev Walkine0b56e02005-02-25 12:10:27 +0000955 OUT("%s%s_t",
Lev Walkinc8285712005-03-04 22:18:20 +0000956 (expr->marker.flags & EM_INDIRECT)?"*":" ",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000957 MKID(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000958 }
959
Lev Walkine0b56e02005-02-25 12:10:27 +0000960 if((expr->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000961 || (0 /* -- prohibited by X.693:8.3.4 */
962 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +0000963 && expr_elements_count(arg, expr)))
964 etd_spec = ETD_HAS_SPECIFICS;
965 else
966 etd_spec = ETD_NO_SPECIFICS;
Lev Walkinf15320b2004-06-03 03:38:44 +0000967
Lev Walkin8de2ab22004-09-26 13:11:31 +0000968 /*
969 * If this type just blindly refers the other type, alias it.
970 * Type1 ::= Type2
971 */
Lev Walkine0b56e02005-02-25 12:10:27 +0000972 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
973 REDIR(OT_TYPE_DECLS);
974 return 0;
975 }
Lev Walkin8de2ab22004-09-26 13:11:31 +0000976 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkine0b56e02005-02-25 12:10:27 +0000977 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
Lev Walkin6938d042005-03-04 23:23:50 +0000978 && etd_spec == ETD_NO_SPECIFICS
979 && 0 /* This shortcut is incompatible with XER */
980 ) {
Lev Walkin8de2ab22004-09-26 13:11:31 +0000981 char *type_name;
982 REDIR(OT_FUNC_DECLS);
983 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
984 OUT("/* This type is equivalent to %s */\n", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +0000985 if(HIDE_INNER_DEFS) OUT("/* ");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000986 OUT("#define\tasn_DEF_%s\t", MKID(expr->Identifier));
Lev Walkin8de2ab22004-09-26 13:11:31 +0000987 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkine0b56e02005-02-25 12:10:27 +0000988 OUT("asn_DEF_%s", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +0000989 if(HIDE_INNER_DEFS)
Lev Walkine0b56e02005-02-25 12:10:27 +0000990 OUT("\t// (Use -fall-defs-global to expose) */");
991 OUT("\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +0000992 REDIR(OT_CODE);
993 OUT("/* This type is equivalent to %s */\n", type_name);
994 OUT("\n");
995 REDIR(OT_TYPE_DECLS);
996 return 0;
997 }
998
Lev Walkinf15320b2004-06-03 03:38:44 +0000999 REDIR(OT_STAT_DEFS);
1000
Lev Walkin27ea3802004-06-28 21:13:46 +00001001 /*
Lev Walkin05363a72004-09-29 13:16:40 +00001002 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +00001003 */
Lev Walkin188ed2c2004-09-13 08:31:01 +00001004 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +00001005
Lev Walkine0b56e02005-02-25 12:10:27 +00001006 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1007 0, etd_spec);
Lev Walkinf15320b2004-06-03 03:38:44 +00001008
Lev Walkin59004fa2004-08-20 13:37:01 +00001009 REDIR(OT_CODE);
1010
Lev Walkinf15320b2004-06-03 03:38:44 +00001011 /*
1012 * Constraint checking.
1013 */
Lev Walkin8de2ab22004-09-26 13:11:31 +00001014 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1015 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001016 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001017 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001018 OUT("%s", p);
1019 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1020 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001021 INDENT(+1);
1022 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1023 OUT("\n");
1024 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1025 OUT("/* Replace with underlying type checker */\n");
1026 OUT("td->check_constraints "
Lev Walkin05363a72004-09-29 13:16:40 +00001027 "= asn_DEF_%s.check_constraints;\n",
Lev Walkin8de2ab22004-09-26 13:11:31 +00001028 asn1c_type_name(arg, expr, TNF_SAFE));
1029 OUT("return td->check_constraints"
1030 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +00001031 }
Lev Walkin8de2ab22004-09-26 13:11:31 +00001032 INDENT(-1);
1033 OUT("}\n");
1034 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001035 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001036
1037 /*
1038 * Emit suicidal functions.
1039 */
1040
Lev Walkinf15320b2004-06-03 03:38:44 +00001041 /*
1042 * This function replaces certain fields from the definition
1043 * of a type with the corresponding fields from the basic type
1044 * (from which the current type is inherited).
1045 */
Lev Walkinf15320b2004-06-03 03:38:44 +00001046 OUT("/*\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001047 OUT(" * This type is implemented using %s,\n",
1048 asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkinb65b3002004-09-23 22:20:47 +00001049 OUT(" * so here we adjust the DEF accordingly.\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001050 OUT(" */\n");
1051 OUT("static void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001052 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001053 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001054 INDENT(+1);
Lev Walkine0b56e02005-02-25 12:10:27 +00001055 {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001056 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001057 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkin05363a72004-09-29 13:16:40 +00001058 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1059 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1060 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1061 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1062 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1063 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001064 if(!terminal && !tags_count) {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001065 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
Lev Walkin05363a72004-09-29 13:16:40 +00001066 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1067 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1068 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1069 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001070 OUT("/* End of these lines */\n");
1071 }
Lev Walkin05363a72004-09-29 13:16:40 +00001072 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1073 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
Lev Walkine0b56e02005-02-25 12:10:27 +00001074 if(etd_spec != ETD_NO_SPECIFICS) {
1075 INDENT(-1);
Lev Walkin7ef83a42005-03-29 19:04:24 +00001076 OUT(" /* ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001077 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001078 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1079 if(etd_spec == ETD_NO_SPECIFICS) {
1080 INDENT(-1);
1081 OUT("\n");
1082 } else {
1083 OUT("\t// Defined explicitly */\n");
1084 }
1085 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001086 OUT("}\n");
1087 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001088
1089 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001090 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001091 OUT("void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001092 OUT("%s", p);
1093 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1094 OUT("_free(asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001095 INDENTED(
Lev Walkina9cc46e2004-09-22 16:06:28 +00001096 OUT("\tvoid *struct_ptr, int contents_only) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001097 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1098 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001099 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001100 );
1101 OUT("}\n");
1102 OUT("\n");
1103
1104 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001105 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinf15320b2004-06-03 03:38:44 +00001106 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001107 OUT("%s", p);
1108 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1109 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001110 INDENTED(
1111 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001112 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1113 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001114 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1115 );
1116 OUT("}\n");
1117 OUT("\n");
1118
1119 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001120 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkindc06f6b2004-10-20 15:50:55 +00001121 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001122 OUT("%s", p);
1123 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1124 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001125 INDENTED(
Lev Walkindd1512e2005-03-17 21:56:00 +00001126 OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001127 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1128 p, expr->_type_unique_index);
Lev Walkin05363a72004-09-29 13:16:40 +00001129 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001130 );
1131 OUT("}\n");
1132 OUT("\n");
1133
1134 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001135 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001136 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001137 OUT("%s", p);
1138 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1139 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001140 INDENTED(
1141 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1142 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001143 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1144 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001145 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1146 );
1147 OUT("}\n");
1148 OUT("\n");
1149
1150 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001151 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinca47c292004-10-23 13:34:00 +00001152 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001153 OUT("%s", p);
1154 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1155 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinca47c292004-10-23 13:34:00 +00001156 INDENTED(
Lev Walkindd1512e2005-03-17 21:56:00 +00001157 OUT("\tvoid **structure, const char *opt_mname, const void *bufptr, size_t size) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001158 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1159 p, expr->_type_unique_index);
Lev Walkinca47c292004-10-23 13:34:00 +00001160 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1161 );
1162 OUT("}\n");
1163 OUT("\n");
1164
1165 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001166 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001167 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001168 OUT("%s", p);
1169 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1170 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001171 INDENTED(
1172 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1173 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001174 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1175 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001176 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001177 );
1178 OUT("}\n");
1179 OUT("\n");
1180
1181 REDIR(OT_FUNC_DECLS);
1182
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001183 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001184 if(HIDE_INNER_DEFS) {
Lev Walkin21d00002005-03-04 08:48:53 +00001185 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1186 "\t// (Use -fall-defs-global to expose) */\n",
1187 p, expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +00001188 } else {
1189 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1190 OUT("asn_struct_free_f %s_free;\n", p);
1191 OUT("asn_struct_print_f %s_print;\n", p);
1192 OUT("asn_constr_check_f %s_constraint;\n", p);
1193 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1194 OUT("der_type_encoder_f %s_encode_der;\n", p);
1195 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1196 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1197 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001198
Lev Walkin3dcaafa2004-08-11 05:21:32 +00001199 REDIR(OT_TYPE_DECLS);
1200
Lev Walkinf15320b2004-06-03 03:38:44 +00001201 return 0;
1202}
1203
1204int
1205asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1206
1207 OUT("/*\n");
1208 OUT(" * This type is extensible,\n");
1209 OUT(" * possible extensions are below.\n");
1210 OUT(" */\n");
1211
1212 return 0;
1213}
1214
Lev Walkin35631482004-07-01 00:52:50 +00001215static int check_if_extensible(asn1p_expr_t *expr) {
1216 asn1p_expr_t *v;
1217 TQ_FOR(v, &(expr->members), next) {
1218 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1219 }
1220 return 0;
1221}
1222
Lev Walkinf15320b2004-06-03 03:38:44 +00001223static int
Lev Walkinc3e29402004-09-10 06:07:18 +00001224_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001225
1226 OUT("(");
Lev Walkinc3e29402004-09-10 06:07:18 +00001227 switch(tag->tag_class) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001228 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1229 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1230 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1231 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1232 case TC_NOCLASS:
1233 break;
1234 }
Lev Walkin33c16ba2004-09-24 21:01:43 +00001235 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
Lev Walkinf15320b2004-06-03 03:38:44 +00001236
1237 return 0;
1238}
1239
Lev Walkinfd171ef2004-06-06 07:20:17 +00001240
1241static int
1242_tag2el_cmp(const void *ap, const void *bp) {
1243 const tag2el_t *a = ap;
1244 const tag2el_t *b = bp;
1245 const struct asn1p_type_tag_s *ta = &a->el_tag;
1246 const struct asn1p_type_tag_s *tb = &b->el_tag;
1247
1248 if(ta->tag_class == tb->tag_class) {
1249 if(ta->tag_value == tb->tag_value) {
1250 /*
1251 * Sort by their respective positions.
1252 */
1253 if(a->el_no < b->el_no)
1254 return -1;
1255 else if(a->el_no > b->el_no)
1256 return 1;
1257 return 0;
1258 } else if(ta->tag_value < tb->tag_value)
1259 return -1;
1260 else
1261 return 1;
1262 } else if(ta->tag_class < tb->tag_class) {
1263 return -1;
1264 } else {
1265 return 1;
1266 }
1267}
1268
Lev Walkinf15320b2004-06-03 03:38:44 +00001269/*
1270 * For constructed types, number of external tags may be greater than
1271 * number of elements in the type because of CHOICE type.
1272 * T ::= SET { -- Three possible tags:
1273 * a INTEGER, -- One tag is here...
1274 * b Choice1 -- ... and two more tags are there.
1275 * }
1276 * Choice1 ::= CHOICE {
1277 * s1 IA5String,
1278 * s2 ObjectDescriptor
1279 * }
1280 */
1281static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001282_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001283 asn1p_expr_t *expr = arg->expr;
1284 arg_t tmparg = *arg;
1285 asn1p_expr_t *v;
1286 int element = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001287 int original_count = *count;
1288 int sort_until = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001289
1290 TQ_FOR(v, &(expr->members), next) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001291 if(v->expr_type == A1TC_EXTENSIBLE) {
1292 /*
Lev Walkine0b56e02005-02-25 12:10:27 +00001293 * CXER mandates sorting
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001294 * only for the root part.
1295 */
1296 if(flags == FTE_CANONICAL_XER
1297 && sort_until == -1)
1298 sort_until = *count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001299 continue;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001300 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001301
1302 tmparg.expr = v;
1303
1304 if(_add_tag2el_member(&tmparg, tag2el, count,
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001305 (el_no==-1)?element:el_no, flags)) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001306 return -1;
1307 }
1308
1309 element++;
1310 }
1311
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001312
1313 if(flags == FTE_CANONICAL_XER) {
1314 if(sort_until == -1) sort_until = *count;
1315 qsort((*tag2el) + original_count,
1316 sort_until - original_count,
1317 sizeof(**tag2el), _tag2el_cmp);
1318 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1319 && (sort_until - original_count) >= 1) {
1320 /* Only take in account the root component */
1321 *count = original_count + 1;
1322 }
1323 } else {
1324 /*
1325 * Sort the map according to canonical order of their
1326 * tags and element numbers.
1327 */
1328 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1329 }
Lev Walkinfd171ef2004-06-06 07:20:17 +00001330
Lev Walkin38abe792004-06-14 13:09:45 +00001331 /*
1332 * Initialize .toff_{first|last} members.
1333 */
1334 if(*count) {
1335 struct asn1p_type_tag_s *cur_tag = 0;
1336 tag2el_t *cur = *tag2el;
1337 tag2el_t *end = cur + *count;
1338 int occur, i;
1339 for(occur = 0; cur < end; cur++) {
1340 if(cur_tag == 0
1341 || cur_tag->tag_value != cur->el_tag.tag_value
1342 || cur_tag->tag_class != cur->el_tag.tag_class) {
1343 cur_tag = &cur->el_tag;
1344 occur = 0;
1345 } else {
1346 occur++;
1347 }
1348 cur->toff_first = -occur;
1349 for(i = 0; i >= -occur; i--)
1350 cur[i].toff_last = -i;
1351 }
1352 }
1353
Lev Walkinf15320b2004-06-03 03:38:44 +00001354 return 0;
1355}
1356
1357static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001358_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001359 struct asn1p_type_tag_s tag;
1360 int ret;
1361
1362 assert(el_no >= 0);
1363
Lev Walkina9cc46e2004-09-22 16:06:28 +00001364 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1365 arg->expr, &tag, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001366 if(ret == 0) {
Lev Walkin201943e2004-09-04 04:42:29 +00001367 tag2el_t *te;
1368 int new_count = (*count) + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001369 void *p;
Lev Walkin201943e2004-09-04 04:42:29 +00001370
Lev Walkinac7e2292004-09-05 10:42:33 +00001371 if(tag.tag_value == -1) {
1372 /*
1373 * This is an untagged ANY type,
1374 * proceed without adding a tag
1375 */
1376 return 0;
1377 }
1378
Lev Walkin201943e2004-09-04 04:42:29 +00001379 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
Lev Walkinf15320b2004-06-03 03:38:44 +00001380 if(p) *tag2el = p;
1381 else return -1;
1382
1383 DEBUG("Found tag for %s: %ld",
1384 arg->expr->Identifier,
1385 (long)tag.tag_value);
1386
Lev Walkin201943e2004-09-04 04:42:29 +00001387 te = &((*tag2el)[*count]);
1388 te->el_tag = tag;
1389 te->el_no = el_no;
1390 te->from_expr = arg->expr;
1391 *count = new_count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001392 return 0;
1393 }
1394
1395 DEBUG("Searching tag in complex expression %s:%x at line %d",
1396 arg->expr->Identifier,
1397 arg->expr->expr_type,
1398 arg->expr->_lineno);
1399
1400 /*
1401 * Iterate over members of CHOICE type.
1402 */
1403 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001404 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001405 }
1406
1407 if(arg->expr->expr_type == A1TC_REFERENCE) {
1408 arg_t tmp = *arg;
1409 asn1p_expr_t *expr;
Lev Walkin08079b02004-08-22 03:25:24 +00001410 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001411 arg->expr->reference);
1412 if(expr) {
Lev Walkin08079b02004-08-22 03:25:24 +00001413 tmp.mod = expr->module;
Lev Walkinf15320b2004-06-03 03:38:44 +00001414 tmp.expr = expr;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001415 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001416 } else {
1417 FATAL("Cannot dereference %s at line %d",
1418 arg->expr->Identifier,
1419 arg->expr->_lineno);
1420 return -1;
1421 }
1422 }
1423
1424 DEBUG("No tag for %s at line %d",
1425 arg->expr->Identifier,
1426 arg->expr->_lineno);
1427
1428 return -1;
1429}
1430
1431static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001432emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
Lev Walkinfd171ef2004-06-06 07:20:17 +00001433 asn1p_expr_t *expr = arg->expr;
Lev Walkinf3b29b12005-06-02 05:21:37 +00001434 int i;
1435
1436 if(!tag2el_count) return 0; /* No top level tags */
Lev Walkinfd171ef2004-06-06 07:20:17 +00001437
Lev Walkin21d00002005-03-04 08:48:53 +00001438 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001439 MKID(expr->Identifier), expr->_type_unique_index,
Lev Walkin21d00002005-03-04 08:48:53 +00001440 opt_modifier?opt_modifier:"");
Lev Walkinf3b29b12005-06-02 05:21:37 +00001441 for(i = 0; i < tag2el_count; i++) {
1442 OUT(" { ");
1443 _print_tag(arg, &tag2el[i].el_tag);
1444 OUT(", ");
1445 OUT("%d, ", tag2el[i].el_no);
1446 OUT("%d, ", tag2el[i].toff_first);
1447 OUT("%d ", tag2el[i].toff_last);
1448 OUT("}%s /* %s at %d */\n",
1449 (i + 1 < tag2el_count) ? "," : "",
1450 tag2el[i].from_expr->Identifier,
1451 tag2el[i].from_expr->_lineno
1452 );
Lev Walkinfd171ef2004-06-06 07:20:17 +00001453 }
1454 OUT("};\n");
1455
Lev Walkinf3b29b12005-06-02 05:21:37 +00001456 return 0;
Lev Walkinfd171ef2004-06-06 07:20:17 +00001457}
1458
Lev Walkin188ed2c2004-09-13 08:31:01 +00001459static enum tvm_compat
1460emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1461 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1462 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
Lev Walkin64399722004-08-11 07:17:22 +00001463 int tags_count = 0;
Lev Walkin188ed2c2004-09-13 08:31:01 +00001464 int all_tags_count = 0;
1465 enum tvm_compat tv_mode = _TVM_SAME;
Lev Walkin906654e2004-09-10 15:49:15 +00001466 int i;
Lev Walkin27ea3802004-06-28 21:13:46 +00001467
Lev Walkin6d1220a2004-09-14 14:10:10 +00001468 /* Cleanup before proceeding. */
1469 *tags_count_r = 0;
1470 *all_tags_count_r = 0;
1471
Lev Walkin906654e2004-09-10 15:49:15 +00001472 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001473 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
Lev Walkin6d1220a2004-09-14 14:10:10 +00001474 if(tags_count < 0)
1475 return -1;
Lev Walkin64399722004-08-11 07:17:22 +00001476
Lev Walkin188ed2c2004-09-13 08:31:01 +00001477 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001478 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
Lev Walkin188ed2c2004-09-13 08:31:01 +00001479 &all_tags, AFT_FULL_COLLECT);
1480 if(all_tags_count < 0) {
1481 if(tags) free(tags);
1482 return -1;
Lev Walkin5a8219a2004-09-08 00:28:57 +00001483 }
Lev Walkin906654e2004-09-10 15:49:15 +00001484
Lev Walkin188ed2c2004-09-13 08:31:01 +00001485 assert(tags_count <= all_tags_count);
1486 assert((tags_count?0:1) == (all_tags_count?0:1));
Lev Walkin27ea3802004-06-28 21:13:46 +00001487
Lev Walkin188ed2c2004-09-13 08:31:01 +00001488 if(tags_count <= all_tags_count) {
1489 for(i = 0; i < tags_count; i++) {
1490 if(tags[i].tag_value != all_tags[i].tag_value
1491 || tags[i].tag_class != all_tags[i].tag_class) {
1492 tv_mode = _TVM_DIFFERENT;
1493 break;
1494 }
1495 }
1496 if(i == tags_count && tags_count < all_tags_count)
1497 tv_mode = _TVM_SUBSET;
1498 } else {
1499 tv_mode = _TVM_DIFFERENT;
1500 }
1501
1502#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
Lev Walkin21d00002005-03-04 08:48:53 +00001503 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001504 MKID(expr->Identifier), \
Lev Walkin21d00002005-03-04 08:48:53 +00001505 expr->_type_unique_index, name); \
Lev Walkin188ed2c2004-09-13 08:31:01 +00001506 INDENT(+1); \
1507 /* Print the array of collected tags */ \
1508 for(i = 0; i < tags_count; i++) { \
1509 if(i) OUT(",\n"); \
1510 _print_tag(arg, &tags[i]); \
1511 } \
1512 OUT("\n"); \
1513 INDENT(-1); \
1514 OUT("};\n"); \
1515 } while(0)
1516
1517 if(tags_count) {
1518 if(tv_mode == _TVM_SUBSET)
1519 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1520 else
1521 EMIT_TAGS_TABLE("", tags, tags_count);
1522 }
1523
1524 if(all_tags_count) {
1525 if(tv_mode == _TVM_DIFFERENT)
1526 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1527 }
1528
1529 if(tags) free(tags);
1530 if(all_tags) free(all_tags);
1531
1532 *tags_count_r = tags_count;
1533 *all_tags_count_r = all_tags_count;
1534
1535 return tv_mode;
Lev Walkin27ea3802004-06-28 21:13:46 +00001536}
Lev Walkin59004fa2004-08-20 13:37:01 +00001537
1538static int
Lev Walkin59964be2004-08-25 02:03:12 +00001539expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001540 asn1p_expr_t *topmost_parent;
1541 asn1p_expr_t *v;
1542 int elements = 0;
1543
Lev Walkina9cc46e2004-09-22 16:06:28 +00001544 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001545 if(!topmost_parent) return 0;
1546
Lev Walkine0b56e02005-02-25 12:10:27 +00001547 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1548 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1549 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin59004fa2004-08-20 13:37:01 +00001550 return 0;
1551
1552 TQ_FOR(v, &(topmost_parent->members), next) {
1553 if(v->expr_type != A1TC_EXTENSIBLE)
1554 elements++;
1555 }
1556
1557 return elements;
1558}
1559
1560static int
Lev Walkinc8285712005-03-04 22:18:20 +00001561emit_include_dependencies(arg_t *arg) {
1562 asn1p_expr_t *expr = arg->expr;
1563 asn1p_expr_t *memb;
1564
1565 TQ_FOR(memb, &(expr->members), next) {
1566
1567 if((memb->meta_type == AMT_TYPEREF
1568 && (memb->marker.flags & EM_INDIRECT))
1569 || expr->expr_type == ASN_CONSTR_SET_OF
1570 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1571 ) {
1572 asn1p_expr_t *terminal;
1573 terminal = asn1f_find_terminal_type_ex(arg->asn, memb);
1574 if(terminal && !terminal->parent_expr
1575 && (terminal->expr_type & ASN_CONSTR_MASK)) {
1576 int saved_target = arg->target->target;
1577 REDIR(OT_FWD_DECLS);
Lev Walkinbf56d542005-03-04 23:50:56 +00001578 OUT("%s;\n",
Lev Walkinc8285712005-03-04 22:18:20 +00001579 asn1c_type_name(arg, memb, TNF_RSAFE));
1580 REDIR(saved_target);
1581 memb->marker.flags |= EM_UNRECURSE;
1582 }
1583 }
1584
1585 if((!(memb->expr_type & ASN_CONSTR_MASK)
1586 && memb->expr_type > ASN_CONSTR_MASK)
1587 || memb->meta_type == AMT_TYPEREF) {
1588 if(memb->marker.flags & EM_UNRECURSE) {
1589 GEN_POSTINCLUDE(asn1c_type_name(arg,
1590 memb, TNF_INCLUDE));
1591 } else {
1592 GEN_INCLUDE(asn1c_type_name(arg,
1593 memb, TNF_INCLUDE));
1594 }
1595 }
1596 }
1597
1598 return 0;
1599}
1600
1601static int
Lev Walkin59964be2004-08-25 02:03:12 +00001602emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001603 int save_target;
1604 arg_t tmp_arg;
Lev Walkinb9189732004-09-10 09:37:12 +00001605 struct asn1p_type_tag_s outmost_tag_s;
1606 struct asn1p_type_tag_s *outmost_tag;
Lev Walkine0b56e02005-02-25 12:10:27 +00001607 int complex_contents;
Lev Walkin59004fa2004-08-20 13:37:01 +00001608 char *p;
1609
Lev Walkinb9189732004-09-10 09:37:12 +00001610 if(asn1f_fetch_outmost_tag(arg->asn,
1611 expr->module, expr, &outmost_tag_s, 1)) {
1612 outmost_tag = 0;
1613 } else {
1614 outmost_tag = &outmost_tag_s;
1615 }
1616
Lev Walkin59004fa2004-08-20 13:37:01 +00001617 OUT("{ ");
Lev Walkinb9189732004-09-10 09:37:12 +00001618
1619 if(outmost_tag && outmost_tag->tag_value == -1)
1620 OUT("ATF_OPEN_TYPE | ");
Lev Walkinc8285712005-03-04 22:18:20 +00001621 OUT("%s, ",
1622 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
Lev Walkin0204fa62004-09-15 11:54:38 +00001623 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001624 asn1p_expr_t *tv;
1625 int opts = 0;
Lev Walkin0204fa62004-09-15 11:54:38 +00001626 for(tv = expr; tv && tv->marker.flags;
Lev Walkin59004fa2004-08-20 13:37:01 +00001627 tv = TQ_NEXT(tv, next), opts++) {
1628 if(tv->expr_type == A1TC_EXTENSIBLE)
1629 opts--;
1630 }
1631 OUT("%d, ", opts);
1632 } else {
1633 OUT("0, ");
1634 }
Lev Walkina9cc46e2004-09-22 16:06:28 +00001635 if(expr->_anonymous_type) {
1636 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1637 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1638 OUT("0,\n");
1639 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001640 OUT("offsetof(struct ");
1641 out_name_chain(arg, 1);
1642 OUT(", ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001643 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1644 && (!UNNAMED_UNIONS)) OUT("choice.");
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001645 OUT("%s),\n", MKID_safe(expr->Identifier));
Lev Walkin59004fa2004-08-20 13:37:01 +00001646 }
1647 INDENT(+1);
1648 if(C99_MODE) OUT(".tag = ");
Lev Walkinb9189732004-09-10 09:37:12 +00001649 if(outmost_tag) {
1650 if(outmost_tag->tag_value == -1)
1651 OUT("-1 /* Ambiguous tag (ANY?) */");
1652 else
1653 _print_tag(arg, outmost_tag);
Lev Walkinc3e29402004-09-10 06:07:18 +00001654 } else {
Lev Walkinb9189732004-09-10 09:37:12 +00001655 OUT("-1 /* Ambiguous tag (CHOICE?) */");
Lev Walkinc3e29402004-09-10 06:07:18 +00001656 }
Lev Walkinb9189732004-09-10 09:37:12 +00001657
Lev Walkin59004fa2004-08-20 13:37:01 +00001658 OUT(",\n");
1659 if(C99_MODE) OUT(".tag_mode = ");
1660 if(expr->tag.tag_class) {
1661 if(expr->tag.tag_mode == TM_IMPLICIT)
1662 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1663 else
1664 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1665 } else {
1666 OUT("0,\n");
1667 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001668
1669 complex_contents =
1670 (expr->expr_type & ASN_CONSTR_MASK)
1671 || expr->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +00001672 || (0 /* -- prohibited by X.693:8.3.4 */
1673 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +00001674 && expr_elements_count(arg, expr));
Lev Walkin59004fa2004-08-20 13:37:01 +00001675 if(C99_MODE) OUT(".type = ");
Lev Walkin21d00002005-03-04 08:48:53 +00001676 OUT("(void *)&asn_DEF_");
1677 if(complex_contents) {
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001678 OUT("%s", MKID(expr->Identifier));
Lev Walkin21d00002005-03-04 08:48:53 +00001679 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1680 OUT("_%d", expr->_type_unique_index);
Lev Walkin08079b02004-08-22 03:25:24 +00001681 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001682 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkin08079b02004-08-22 03:25:24 +00001683 }
Lev Walkin21d00002005-03-04 08:48:53 +00001684 OUT(",\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001685 if(C99_MODE) OUT(".memb_constraints = ");
1686 if(expr->constraints) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001687 if(arg->flags & A1C_NO_CONSTRAINTS) {
1688 OUT("0,\t/* No check because of -fno-constraints */\n");
1689 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001690 char *id = MKID(expr->Identifier);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001691 if(expr->_anonymous_type
Lev Walkin152a91e2005-02-14 20:41:29 +00001692 && !strcmp(expr->Identifier, "Member"))
Lev Walkin8de2ab22004-09-26 13:11:31 +00001693 id = asn1c_type_name(arg, expr, TNF_SAFE);
1694 OUT("memb_%s_%d_constraint,\n", id,
Lev Walkin21d00002005-03-04 08:48:53 +00001695 arg->expr->_type_unique_index);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001696 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001697 } else {
Lev Walkinb7bfd1a2005-01-17 12:16:58 +00001698 OUT("0,\t/* Defer constraints checking to the member type */\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001699 }
1700 if(C99_MODE) OUT(".name = ");
Lev Walkin152a91e2005-02-14 20:41:29 +00001701 if(1) {
1702 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1703 OUT("\"\"\n");
1704 else
1705 OUT("\"%s\"\n", expr->Identifier);
1706 } else {
1707 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1708 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001709 OUT("},\n");
1710 INDENT(-1);
1711
Lev Walkin8de2ab22004-09-26 13:11:31 +00001712 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkin59004fa2004-08-20 13:37:01 +00001713 return 0;
1714
1715 save_target = arg->target->target;
1716 REDIR(OT_CODE);
1717
Lev Walkin152a91e2005-02-14 20:41:29 +00001718 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
Lev Walkin59004fa2004-08-20 13:37:01 +00001719 p = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001720 else
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001721 p = MKID(expr->Identifier);
Lev Walkin59004fa2004-08-20 13:37:01 +00001722 OUT("static int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001723 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 +00001724 INDENT(+1);
1725 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1726 tmp_arg = *arg;
1727 tmp_arg.expr = expr;
1728 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001729 OUT("return td->check_constraints"
1730 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001731 }
1732 INDENT(-1);
1733 OUT("}\n");
1734 OUT("\n");
1735
1736 REDIR(save_target);
1737
1738 return 0;
1739}
Lev Walkin59964be2004-08-25 02:03:12 +00001740
Lev Walkindc06f6b2004-10-20 15:50:55 +00001741/*
1742 * Generate "asn_DEF_XXX" type definition.
1743 */
Lev Walkin59964be2004-08-25 02:03:12 +00001744static int
Lev Walkin8de2ab22004-09-26 13:11:31 +00001745emit_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 +00001746 int using_type_name = 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001747 char *p;
1748
Lev Walkindd32b592004-09-06 08:07:29 +00001749 if(HIDE_INNER_DEFS)
1750 OUT("static /* Use -fall-defs-global to expose */\n");
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001751 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID(expr->Identifier));
Lev Walkin21d00002005-03-04 08:48:53 +00001752 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1753 OUT(" = {\n");
Lev Walkin801fabc2005-01-28 12:18:50 +00001754 p = MKID(expr->Identifier);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001755 INDENT(+1);
Lev Walkin59964be2004-08-25 02:03:12 +00001756 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001757 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001758
1759 if(expr->expr_type & ASN_CONSTR_MASK) {
Lev Walkin21d00002005-03-04 08:48:53 +00001760 using_type_name = 1;
Lev Walkin59964be2004-08-25 02:03:12 +00001761 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1762 }
1763
Lev Walkin21d00002005-03-04 08:48:53 +00001764#define FUNCREF(foo) do { \
1765 OUT("%s", p); \
1766 if(HIDE_INNER_DEFS && !using_type_name) \
1767 OUT("_%d", expr->_type_unique_index); \
1768 OUT("_" #foo ",\n"); \
1769} while(0)
1770
1771 FUNCREF(free);
1772 FUNCREF(print);
1773 FUNCREF(constraint);
1774 FUNCREF(decode_ber);
1775 FUNCREF(encode_der);
1776 FUNCREF(decode_xer);
1777 FUNCREF(encode_xer);
Lev Walkin59964be2004-08-25 02:03:12 +00001778
Lev Walkin59964be2004-08-25 02:03:12 +00001779 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1780 OUT("CHOICE_outmost_tag,\n");
1781 } else {
1782 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1783 }
1784
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001785 p = MKID(expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001786 if(tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001787 OUT("asn_DEF_%s_%d_tags,\n",
1788 p, expr->_type_unique_index);
1789 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1790 p, expr->_type_unique_index);
1791 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1792 p, expr->_type_unique_index);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001793 if(tv_mode == _TVM_SUBSET
1794 && tags_count != all_tags_count)
1795 OUT(" - %d", all_tags_count - tags_count);
1796 OUT(", /* %d */\n", tags_count);
Lev Walkin59964be2004-08-25 02:03:12 +00001797 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001798 OUT("0,\t/* No effective tags (pointer) */\n");
1799 OUT("0,\t/* No effective tags (count) */\n");
1800 }
1801
1802 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
Lev Walkin21d00002005-03-04 08:48:53 +00001803 OUT("asn_DEF_%s_%d_all_tags,\n",
1804 p, expr->_type_unique_index);
1805 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1806 p, expr->_type_unique_index);
1807 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1808 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001809 } else if(all_tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001810 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1811 p, expr->_type_unique_index);
1812 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1813 p, expr->_type_unique_index);
1814 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1815 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001816 } else {
1817 OUT("0,\t/* No tags (pointer) */\n");
1818 OUT("0,\t/* No tags (count) */\n");
Lev Walkin59964be2004-08-25 02:03:12 +00001819 }
1820
Lev Walkin59964be2004-08-25 02:03:12 +00001821 if(elements_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001822 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001823 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1824 || expr->expr_type == ASN_CONSTR_SET_OF) {
1825 OUT("%d,\t/* Single element */\n",
1826 elements_count);
1827 assert(elements_count == 1);
1828 } else {
1829 OUT("%d,\t/* Elements count */\n",
1830 elements_count);
1831 }
1832 } else {
Lev Walkin59964be2004-08-25 02:03:12 +00001833 if(expr_elements_count(arg, expr))
1834 OUT("0, 0,\t/* Defined elsewhere */\n");
1835 else
1836 OUT("0, 0,\t/* No members */\n");
1837 }
1838
1839 switch(spec) {
1840 case ETD_NO_SPECIFICS:
1841 OUT("0\t/* No specifics */\n");
1842 break;
1843 case ETD_HAS_SPECIFICS:
Lev Walkin21d00002005-03-04 08:48:53 +00001844 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1845 p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001846 }
Lev Walkin188ed2c2004-09-13 08:31:01 +00001847 INDENT(-1);
Lev Walkin59964be2004-08-25 02:03:12 +00001848 OUT("};\n");
1849 OUT("\n");
Lev Walkin97298782004-08-26 06:20:34 +00001850
1851 return 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001852}
Lev Walkincc93b0f2004-09-10 09:18:20 +00001853
1854/*
1855 * Check if it is better to make this type indirectly accessed via
1856 * a pointer.
1857 * This may be the case for the following recursive definition:
1858 * Type ::= CHOICE { member Type };
1859 */
1860static int
1861expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) {
1862 asn1p_expr_t *top_parent;
1863 asn1p_expr_t *terminal;
1864
1865 if(expr->expr_type != A1TC_REFERENCE)
1866 return 0;
1867
Lev Walkin72a0f5a2005-07-24 08:28:39 +00001868 /* -findirect-choice compiles members of CHOICE as indirect pointers */
1869 if((arg->flags & A1C_INDIRECT_CHOICE)
1870 && arg->expr->expr_type == ASN_CONSTR_CHOICE)
1871 return 1;
1872
Lev Walkincc93b0f2004-09-10 09:18:20 +00001873 /* Rewind to the topmost parent expression */
1874 if((top_parent = expr->parent_expr)) {
1875 while(top_parent->parent_expr)
1876 top_parent = top_parent->parent_expr;
1877 } else {
1878 return 0;
1879 }
1880
Lev Walkina9cc46e2004-09-22 16:06:28 +00001881 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkincc93b0f2004-09-10 09:18:20 +00001882
1883 return (terminal == top_parent);
1884}
Lev Walkina9cc46e2004-09-22 16:06:28 +00001885
1886static int
1887expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1888 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1889 if(!expr) return 0;
1890
1891 /* X.680, 25.5, Table 5 */
1892 switch(expr->expr_type) {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001893 case ASN_BASIC_BOOLEAN:
1894 case ASN_BASIC_ENUMERATED:
1895 case ASN_BASIC_NULL:
1896 return 1;
1897 default:
1898 return 0;
1899 }
1900}
Lev Walkin801fabc2005-01-28 12:18:50 +00001901
1902static int
Lev Walkin21d00002005-03-04 08:48:53 +00001903out_name_chain(arg_t *arg, int check_reserved_keywords) {
Lev Walkin801fabc2005-01-28 12:18:50 +00001904 asn1p_expr_t *expr = arg->expr;
1905 char *id;
1906
1907 assert(expr->Identifier);
1908
Lev Walkin21d00002005-03-04 08:48:53 +00001909 if(arg->flags & A1C_COMPOUND_NAMES
1910 && ((expr->expr_type & ASN_CONSTR_MASK)
1911 || expr->expr_type == ASN_BASIC_ENUMERATED
1912 || (expr->expr_type == ASN_BASIC_INTEGER
1913 && expr_elements_count(arg, expr))
1914 )
Lev Walkin801fabc2005-01-28 12:18:50 +00001915 && expr->parent_expr
1916 && expr->parent_expr->Identifier) {
1917 arg_t tmparg = *arg;
1918
1919 tmparg.expr = expr->parent_expr;
Lev Walkin21d00002005-03-04 08:48:53 +00001920 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
1921
1922 out_name_chain(&tmparg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +00001923
Lev Walkin01633092005-03-05 00:08:41 +00001924 OUT("__"); /* a separator between id components */
Lev Walkin21d00002005-03-04 08:48:53 +00001925
Lev Walkin801fabc2005-01-28 12:18:50 +00001926 /* Fall through */
1927 }
1928
1929 if(check_reserved_keywords)
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001930 id = MKID_safe(expr->Identifier);
Lev Walkin801fabc2005-01-28 12:18:50 +00001931 else
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001932 id = MKID(expr->Identifier);
Lev Walkin801fabc2005-01-28 12:18:50 +00001933 OUT("%s", id);
1934
1935 return 0;
1936}