blob: d39018ee71e573e0601e3d35c07e0f52a356ba4c [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 Walkin414b0782005-08-13 23:30:24 +0000108 int eidx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000109
Lev Walkine0b56e02005-02-25 12:10:27 +0000110 v2e = alloca((el_count + 1) * sizeof(*v2e));
Lev Walkinf15320b2004-06-03 03:38:44 +0000111
Lev Walkine0b56e02005-02-25 12:10:27 +0000112 /*
Lev Walkin414b0782005-08-13 23:30:24 +0000113 * For all ENUMERATED types and for those INTEGER types which
114 * have identifiers, print out an enumeration table.
Lev Walkine0b56e02005-02-25 12:10:27 +0000115 */
Lev Walkin414b0782005-08-13 23:30:24 +0000116 if(expr->expr_type == ASN_BASIC_ENUMERATED || el_count) {
117 eidx = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000118 REDIR(OT_DEPS);
Lev Walkin21d00002005-03-04 08:48:53 +0000119 OUT("typedef enum ");
120 out_name_chain(arg, 1);
121 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 TQ_FOR(v, &(expr->members), next) {
123 switch(v->expr_type) {
124 case A1TC_UNIVERVAL:
Lev Walkin21d00002005-03-04 08:48:53 +0000125 OUT("\t");
126 out_name_chain(arg, 0);
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000127 OUT("_%s", MKID(v->Identifier));
Lev Walkin21d00002005-03-04 08:48:53 +0000128 OUT("\t= %" PRIdASN "%s\n",
Lev Walkine0b56e02005-02-25 12:10:27 +0000129 v->value->value.v_integer,
Lev Walkin21d00002005-03-04 08:48:53 +0000130 (eidx+1 < el_count) ? "," : "");
Lev Walkine0b56e02005-02-25 12:10:27 +0000131 v2e[eidx].name = v->Identifier;
132 v2e[eidx].value = v->value->value.v_integer;
133 eidx++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000135 case A1TC_EXTENSIBLE:
136 OUT("\t/*\n");
137 OUT("\t * Enumeration is extensible\n");
138 OUT("\t */\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000139 map_is_extensible = 1;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000140 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000141 default:
142 return -1;
143 }
144 }
Lev Walkin21d00002005-03-04 08:48:53 +0000145 OUT("} ");
146 out_name_chain(arg, 0);
147 OUT("_e;\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000148 assert(eidx == el_count);
Lev Walkin414b0782005-08-13 23:30:24 +0000149 }
150
151 /*
152 * For all ENUMERATED types print out a mapping table
153 * between identifiers and associated values.
154 * This is prohibited for INTEGER types by by X.693:8.3.4.
155 */
156 if(expr->expr_type == ASN_BASIC_ENUMERATED) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000157
158 /*
159 * Generate a enumerationName<->value map for XER codec.
160 */
161 REDIR(OT_STAT_DEFS);
162
Lev Walkin21d00002005-03-04 08:48:53 +0000163 OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_%d_value2enum[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000164 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000165 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
166 for(eidx = 0; eidx < el_count; eidx++) {
167 v2e[eidx].idx = eidx;
168 OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n",
169 v2e[eidx].value,
170 (long)strlen(v2e[eidx].name), v2e[eidx].name,
171 (eidx + 1 < el_count) ? "," : "");
172 }
173 if(map_is_extensible)
174 OUT("\t/* This list is extensible */\n");
175 OUT("};\n");
176
Lev Walkin21d00002005-03-04 08:48:53 +0000177 OUT("static unsigned int asn_MAP_%s_%d_enum2value[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000178 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000179 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
180 for(eidx = 0; eidx < el_count; eidx++) {
181 OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n",
182 v2e[eidx].idx,
183 (eidx + 1 < el_count) ? "," : "",
184 v2e[eidx].name, v2e[eidx].value);
185 }
186 if(map_is_extensible)
187 OUT("\t/* This list is extensible */\n");
188 OUT("};\n");
189
Lev Walkin21d00002005-03-04 08:48:53 +0000190 OUT("static asn_INTEGER_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000191 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000192 INDENT(+1);
Lev Walkin21d00002005-03-04 08:48:53 +0000193 OUT("asn_MAP_%s_%d_value2enum,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000194 "/* \"tag\" => N; sorted by tag */\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000195 MKID(expr->Identifier),
Lev Walkin21d00002005-03-04 08:48:53 +0000196 expr->_type_unique_index);
197 OUT("asn_MAP_%s_%d_enum2value,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000198 "/* N => \"tag\"; sorted by N */\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000199 MKID(expr->Identifier),
Lev Walkin21d00002005-03-04 08:48:53 +0000200 expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000201 OUT("%d,\t/* Number of elements in the maps */\n",
202 el_count);
203 OUT("%d,\t/* Enumeration is %sextensible */\n",
204 map_is_extensible, map_is_extensible ? "": "not ");
205 if(expr->expr_type == ASN_BASIC_ENUMERATED)
206 OUT("1\t/* Strict enumeration */\n");
207 else
208 OUT("0\n");
209 INDENT(-1);
210 OUT("};\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 }
212
213 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
214}
215
216int
Lev Walkinb02a8832005-08-13 23:51:47 +0000217asn1c_lang_C_type_BIT_STRING(arg_t *arg) {
218 asn1p_expr_t *expr = arg->expr;
219 asn1p_expr_t *v;
220 int el_count = expr_elements_count(arg, expr);
Lev Walkinb02a8832005-08-13 23:51:47 +0000221
222 if(el_count) {
Lev Walkin082cadc2005-08-14 02:18:27 +0000223 int eidx = 0;
Lev Walkinb02a8832005-08-13 23:51:47 +0000224 REDIR(OT_DEPS);
225 OUT("typedef enum ");
226 out_name_chain(arg, 1);
227 OUT(" {\n");
228 TQ_FOR(v, &(expr->members), next) {
Lev Walkin082cadc2005-08-14 02:18:27 +0000229 eidx++;
230 if(v->expr_type != A1TC_UNIVERVAL) {
Lev Walkinb02a8832005-08-13 23:51:47 +0000231 OUT("/* Unexpected BIT STRING element: %s */\n",
232 v->Identifier);
Lev Walkin082cadc2005-08-14 02:18:27 +0000233 continue;
Lev Walkinb02a8832005-08-13 23:51:47 +0000234 }
Lev Walkin082cadc2005-08-14 02:18:27 +0000235 OUT("\t");
236 out_name_chain(arg, 0);
237 OUT("_%s", MKID(v->Identifier));
238 OUT("\t= %" PRIdASN "%s\n",
239 v->value->value.v_integer,
240 (eidx < el_count) ? "," : "");
Lev Walkinb02a8832005-08-13 23:51:47 +0000241 }
242 OUT("} ");
243 out_name_chain(arg, 0);
244 OUT("_e;\n");
245 assert(eidx == el_count);
246 }
247
248 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
249}
250
251int
Lev Walkinf15320b2004-06-03 03:38:44 +0000252asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
253 asn1p_expr_t *expr = arg->expr;
254 asn1p_expr_t *v;
255 int comp_mode = 0; /* {root,ext=1,root,root,...} */
256
257 DEPENDENCIES;
258
259 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000260 OUT("struct ");
261 out_name_chain(arg, 1);
262 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 } else {
264 OUT("typedef struct %s {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000265 MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 }
267
268 TQ_FOR(v, &(expr->members), next) {
269 if(v->expr_type == A1TC_EXTENSIBLE) {
270 if(comp_mode < 3) comp_mode++;
271 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000272 if(comp_mode == 1
273 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000274 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000275 EMBED(v);
276 }
277
278 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000279 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000280 expr->_anonymous_type ? "" :
281 arg->embed
282 ? MKID_safe(expr->Identifier)
283 : MKID(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000284 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000285
286 return asn1c_lang_C_type_SEQUENCE_def(arg);
287}
288
289static int
290asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
291 asn1p_expr_t *expr = arg->expr;
292 asn1p_expr_t *v;
293 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000294 int ext_start = -1;
295 int ext_stop = -1;
Lev Walkinfd171ef2004-06-06 07:20:17 +0000296 tag2el_t *tag2el = NULL;
297 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000298 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000299 int all_tags_count;
300 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000301
Lev Walkinfd171ef2004-06-06 07:20:17 +0000302 /*
303 * Fetch every inner tag from the tag to elements map.
304 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000305 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinfd171ef2004-06-06 07:20:17 +0000306 if(tag2el) free(tag2el);
307 return -1;
308 }
309
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000310 GEN_INCLUDE("constr_SEQUENCE");
311 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000312 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000313
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000314 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000315
316 /*
317 * Print out the table according to which the parsing is performed.
318 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000319 if(expr_elements_count(arg, expr)) {
320 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000321
Lev Walkin21d00002005-03-04 08:48:53 +0000322 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000323 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000324
325 elements = 0;
326 INDENTED(TQ_FOR(v, &(expr->members), next) {
327 if(v->expr_type == A1TC_EXTENSIBLE) {
328 if((++comp_mode) == 1)
329 ext_start = elements - 1;
330 else
331 ext_stop = elements - 1;
332 continue;
333 }
334 elements++;
335 emit_member_table(arg, v);
336 });
337 OUT("};\n");
338 } else {
339 elements = 0;
340 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000341
Lev Walkin27ea3802004-06-28 21:13:46 +0000342 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000343 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000344 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000345 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000346
Lev Walkinfd171ef2004-06-06 07:20:17 +0000347 /*
348 * Tags to elements map.
349 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000350 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinfd171ef2004-06-06 07:20:17 +0000351
Lev Walkin21d00002005-03-04 08:48:53 +0000352 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000353 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000355 OUT("sizeof(struct "); out_name_chain(arg, 1); OUT("),\n");
356 OUT("offsetof(struct "); out_name_chain(arg, 1); OUT(", _asn_ctx),\n");
357
Lev Walkinf3b29b12005-06-02 05:21:37 +0000358 if(tag2el_count) {
359 OUT("asn_MAP_%s_%d_tag2el,\n",
360 MKID(expr->Identifier),
361 expr->_type_unique_index);
362 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
363 } else {
364 OUT("0,\t/* No top level tags */\n");
365 OUT("0,\t/* No tags in the map */\n");
366 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 OUT("%d,\t/* Start extensions */\n",
368 ext_start);
369 OUT("%d\t/* Stop extensions */\n",
370 (ext_stop<ext_start)?elements+1:ext_stop, ext_stop);
371 );
372 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000373
374 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000375 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000376 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000377 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000378 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000379
Lev Walkinf15320b2004-06-03 03:38:44 +0000380 REDIR(OT_TYPE_DECLS);
381
382 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000383} /* _SEQUENCE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000384
385int
Lev Walkinf15320b2004-06-03 03:38:44 +0000386asn1c_lang_C_type_SET(arg_t *arg) {
387 asn1p_expr_t *expr = arg->expr;
388 asn1p_expr_t *v;
389 long mcount;
390 char *id;
391 int comp_mode = 0; /* {root,ext=1,root,root,...} */
392
393 DEPENDENCIES;
394
395 REDIR(OT_DEPS);
396
397 OUT("\n");
398 OUT("/*\n");
399 OUT(" * Method of determining the components presence\n");
400 OUT(" */\n");
401 mcount = 0;
Lev Walkin21d00002005-03-04 08:48:53 +0000402 OUT("typedef enum ");
403 out_name_chain(arg, 0);
404 OUT("_PR {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000405 TQ_FOR(v, &(expr->members), next) {
406 if(v->expr_type == A1TC_EXTENSIBLE) continue;
407 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000408 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000409 OUT("_PR_");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000410 id = MKID(v->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000411 OUT("%s,\t/* Member %s is present */\n",
412 id, id)
413 );
414 mcount++;
415 }
Lev Walkin21d00002005-03-04 08:48:53 +0000416 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000417
418 REDIR(OT_TYPE_DECLS);
419
420 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000421 OUT("struct ");
422 out_name_chain(arg, 1);
423 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000424 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000425 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000426 }
427
428 TQ_FOR(v, &(expr->members), next) {
429 if(v->expr_type == A1TC_EXTENSIBLE) {
430 if(comp_mode < 3) comp_mode++;
431 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000432 if(comp_mode == 1
433 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000434 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000435 EMBED(v);
436 }
437
438 INDENTED(
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000439 id = MKID(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000440 OUT("\n");
441 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
442 id, id);
443 OUT("unsigned int _presence_map\n");
444 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
445 );
446
447 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000448 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000449 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000450 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000451
452 return asn1c_lang_C_type_SET_def(arg);
453}
454
Lev Walkinf15320b2004-06-03 03:38:44 +0000455static int
456asn1c_lang_C_type_SET_def(arg_t *arg) {
457 asn1p_expr_t *expr = arg->expr;
458 asn1p_expr_t *v;
459 int elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 tag2el_t *tag2el = NULL;
461 int tag2el_count = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000462 tag2el_t *tag2el_cxer = NULL;
463 int tag2el_cxer_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000464 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000465 int all_tags_count;
466 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000467 char *p;
468
469 /*
470 * Fetch every inner tag from the tag to elements map.
471 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000472 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 if(tag2el) free(tag2el);
474 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000475 }
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000476 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
477 if(tag2el) free(tag2el);
478 if(tag2el_cxer) free(tag2el_cxer);
479 return -1;
480 }
481 if(tag2el_cxer_count == tag2el_count
482 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
483 free(tag2el_cxer);
484 tag2el_cxer = 0;
485 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000486
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000487 GEN_INCLUDE("constr_SET");
488 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000489 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000490
491 REDIR(OT_STAT_DEFS);
492
Lev Walkinf15320b2004-06-03 03:38:44 +0000493 /*
494 * Print out the table according to which the parsing is performed.
495 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000496 if(expr_elements_count(arg, expr)) {
497 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000498
Lev Walkin21d00002005-03-04 08:48:53 +0000499 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000500 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000501
502 elements = 0;
503 INDENTED(TQ_FOR(v, &(expr->members), next) {
504 if(v->expr_type == A1TC_EXTENSIBLE) {
505 if(comp_mode < 3) comp_mode++;
506 } else {
507 if(comp_mode == 1
508 || expr_better_indirect(arg, v))
509 v->marker.flags |= EM_INDIRECT;
510 elements++;
511 emit_member_table(arg, v);
512 }
513 });
514 OUT("};\n");
515 } else {
516 elements = 0;
517 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000518
Lev Walkin27ea3802004-06-28 21:13:46 +0000519 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000520 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000521 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000522 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000523
524 /*
525 * Tags to elements map.
526 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000527 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
528 if(tag2el_cxer)
529 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
Lev Walkinf15320b2004-06-03 03:38:44 +0000530
531 /*
532 * Emit a map of mandatory elements.
533 */
Lev Walkin21d00002005-03-04 08:48:53 +0000534 OUT("static uint8_t asn_MAP_%s_%d_mmap",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000535 MKID(expr->Identifier), expr->_type_unique_index);
536 p = MKID_safe(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000537 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
Lev Walkin21d00002005-03-04 08:48:53 +0000538 OUT(" = {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000539 INDENTED(
540 if(elements) {
541 int delimit = 0;
542 int el = 0;
543 TQ_FOR(v, &(expr->members), next) {
544 if(v->expr_type == A1TC_EXTENSIBLE) continue;
545 if(delimit) {
546 OUT(",\n");
547 delimit = 0;
548 } else if(el) {
549 OUT(" | ");
550 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000551 OUT("(%d << %d)",
Lev Walkin0204fa62004-09-15 11:54:38 +0000552 v->marker.flags?0:1,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000553 7 - (el % 8));
Lev Walkinf15320b2004-06-03 03:38:44 +0000554 if(el && (el % 8) == 0)
555 delimit = 1;
556 el++;
557 }
558 } else {
559 OUT("0");
560 }
561 );
562 OUT("\n");
563 OUT("};\n");
564
Lev Walkin21d00002005-03-04 08:48:53 +0000565 OUT("static asn_SET_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000566 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000568 OUT("sizeof(struct ");
569 out_name_chain(arg, 1);
570 OUT("),\n");
571 OUT("offsetof(struct ");
572 out_name_chain(arg, 1);
573 OUT(", _asn_ctx),\n");
574 OUT("offsetof(struct ");
575 out_name_chain(arg, 1);
576 OUT(", _presence_map),\n");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000577 p = MKID(expr->Identifier);
Lev Walkin21d00002005-03-04 08:48:53 +0000578 OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000579 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000580 if(tag2el_cxer)
Lev Walkin21d00002005-03-04 08:48:53 +0000581 OUT("asn_MAP_%s_%d_tag2el_cxer,\n",
582 p, expr->_type_unique_index);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000583 else
Lev Walkin21d00002005-03-04 08:48:53 +0000584 OUT("asn_MAP_%s_%d_tag2el,\t/* Same as above */\n",
585 p, expr->_type_unique_index);
586 OUT("%d,\t/* Count of tags in the CXER map */\n",
587 tag2el_cxer_count);
Lev Walkin35631482004-07-01 00:52:50 +0000588 OUT("%d,\t/* Whether extensible */\n",
589 check_if_extensible(expr));
Lev Walkin21d00002005-03-04 08:48:53 +0000590 OUT("(unsigned int *)asn_MAP_%s_%d_mmap\t/* Mandatory elements map */\n",
591 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000592 );
593 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000594
595 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000596 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000597 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000598 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000599 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000600
Lev Walkinf15320b2004-06-03 03:38:44 +0000601 REDIR(OT_TYPE_DECLS);
602
603 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000604} /* _SET_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000605
606int
Lev Walkin08079b02004-08-22 03:25:24 +0000607asn1c_lang_C_type_SEx_OF(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000608 asn1p_expr_t *expr = arg->expr;
Lev Walkinc8285712005-03-04 22:18:20 +0000609 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
Lev Walkinf15320b2004-06-03 03:38:44 +0000610
611 DEPENDENCIES;
612
613 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000614 OUT("struct ");
615 out_name_chain(arg, 1);
616 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000617 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000618 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000619 }
620
Lev Walkin08079b02004-08-22 03:25:24 +0000621 INDENT(+1);
622 OUT("A_%s_OF(",
623 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
624 ? "SET" : "SEQUENCE");
Lev Walkine0b56e02005-02-25 12:10:27 +0000625 if(memb->expr_type & ASN_CONSTR_MASK
626 || ((memb->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000627 || (0 /* -- prohibited by X.693:8.3.4 */
628 && memb->expr_type == ASN_BASIC_INTEGER))
629 && expr_elements_count(arg, memb))) {
Lev Walkin08079b02004-08-22 03:25:24 +0000630 arg_t tmp;
631 asn1p_expr_t tmp_memb;
632 arg->embed++;
633 tmp = *arg;
634 tmp.expr = &tmp_memb;
635 tmp_memb = *memb;
636 tmp_memb._anonymous_type = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000637 if(tmp_memb.Identifier == 0) {
Lev Walkin21d00002005-03-04 08:48:53 +0000638 tmp_memb.Identifier = "Member";
639 if(0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000640 tmp_memb.Identifier = strdup(
641 asn1c_make_identifier(0,
Lev Walkin152a91e2005-02-14 20:41:29 +0000642 expr->Identifier, "Member", 0));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000643 assert(tmp_memb.Identifier);
644 }
Lev Walkin08079b02004-08-22 03:25:24 +0000645 tmp.default_cb(&tmp);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000646 if(tmp_memb.Identifier != memb->Identifier)
Lev Walkin21d00002005-03-04 08:48:53 +0000647 if(0) free(tmp_memb.Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000648 arg->embed--;
649 assert(arg->target->target == OT_TYPE_DECLS);
650 } else {
Lev Walkinfe7f6ec2005-03-04 22:38:22 +0000651 OUT("%s", asn1c_type_name(arg, memb,
652 (memb->marker.flags & EM_UNRECURSE)
653 ? TNF_RSAFE : TNF_CTYPE));
Lev Walkinf15320b2004-06-03 03:38:44 +0000654 }
Lev Walkin08079b02004-08-22 03:25:24 +0000655 OUT(") list;\n");
656 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000657
658 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000659 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000660 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000661 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000662
663 /*
Lev Walkin08079b02004-08-22 03:25:24 +0000664 * SET OF/SEQUENCE OF definition
Lev Walkinf15320b2004-06-03 03:38:44 +0000665 */
Lev Walkin08079b02004-08-22 03:25:24 +0000666 return asn1c_lang_C_type_SEx_OF_def(arg,
667 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
Lev Walkinf15320b2004-06-03 03:38:44 +0000668}
669
670static int
671asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
672 asn1p_expr_t *expr = arg->expr;
673 asn1p_expr_t *v;
Lev Walkin64399722004-08-11 07:17:22 +0000674 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000675 int all_tags_count;
676 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000677
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000678 /*
679 * Print out the table according to which the parsing is performed.
680 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000681 if(seq_of) {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000682 GEN_INCLUDE("constr_SEQUENCE_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000683 } else {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000684 GEN_INCLUDE("constr_SET_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000685 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000686 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000687 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000688
689 REDIR(OT_STAT_DEFS);
690
691 /*
692 * Print out the table according to which the parsing is performed.
693 */
Lev Walkin21d00002005-03-04 08:48:53 +0000694 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
695 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000696 INDENT(+1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000697 v = TQ_FIRST(&(expr->members));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000698 if(!v->Identifier) {
Lev Walkin152a91e2005-02-14 20:41:29 +0000699 v->Identifier = strdup("Member");
Lev Walkina9cc46e2004-09-22 16:06:28 +0000700 assert(v->Identifier);
701 }
702 v->_anonymous_type = 1;
703 arg->embed++;
Lev Walkin59964be2004-08-25 02:03:12 +0000704 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000705 arg->embed--;
706 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000707 OUT("};\n");
708
Lev Walkin27ea3802004-06-28 21:13:46 +0000709 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000710 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000711 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000712 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000713
Lev Walkin21d00002005-03-04 08:48:53 +0000714 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000715 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000716 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000717 OUT("sizeof(struct ");
718 out_name_chain(arg, 1);
719 OUT("),\n");
720 OUT("offsetof(struct ");
721 out_name_chain(arg, 1);
722 OUT(", _asn_ctx),\n");
723
Lev Walkina9cc46e2004-09-22 16:06:28 +0000724 if(expr_as_xmlvaluelist(arg, v))
725 OUT("1,\t/* XER encoding is XMLValueList */\n");
726 else
727 OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000728 );
729 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000730
731 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000732 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000733 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000734 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000735 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000736
Lev Walkinf15320b2004-06-03 03:38:44 +0000737 REDIR(OT_TYPE_DECLS);
738
739 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000740} /* _SEx_OF_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000741
742int
743asn1c_lang_C_type_CHOICE(arg_t *arg) {
744 asn1p_expr_t *expr = arg->expr;
745 asn1p_expr_t *v;
Lev Walkin08079b02004-08-22 03:25:24 +0000746 char *id;
Lev Walkinf15320b2004-06-03 03:38:44 +0000747
748 DEPENDENCIES;
749
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000750 REDIR(OT_DEPS);
751
Lev Walkin21d00002005-03-04 08:48:53 +0000752 OUT("typedef enum ");
753 out_name_chain(arg, 0);
754 OUT("_PR {\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000755 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000756 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000757 OUT("_PR_NOTHING,\t/* No components present */\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000758 TQ_FOR(v, &(expr->members), next) {
759 if(v->expr_type == A1TC_EXTENSIBLE) {
760 OUT("/* Extensions may appear below */\n");
761 continue;
762 }
Lev Walkin21d00002005-03-04 08:48:53 +0000763 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000764 OUT("_PR_");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000765 id = MKID(v->Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000766 OUT("%s,\n", id, id);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000767 }
768 );
Lev Walkin21d00002005-03-04 08:48:53 +0000769 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000770
771 REDIR(OT_TYPE_DECLS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000772
773 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000774 OUT("struct "); out_name_chain(arg, 1); OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000775 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000776 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000777 }
778
779 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000780 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000781 OUT("_PR present;\n");
782 OUT("union {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000783 TQ_FOR(v, &(expr->members), next) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000784 if(expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000785 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000786 EMBED(v);
787 }
788 if(UNNAMED_UNIONS) OUT("};\n");
789 else OUT("} choice;\n");
790 );
791
792 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000793 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000794 expr->_anonymous_type ? "" :
795 arg->embed
796 ? MKID_safe(expr->Identifier)
797 : MKID(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000798 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000799
800 return asn1c_lang_C_type_CHOICE_def(arg);
801}
802
803static int
804asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
805 asn1p_expr_t *expr = arg->expr;
806 asn1p_expr_t *v;
807 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000808 tag2el_t *tag2el = NULL;
809 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000810 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000811 int all_tags_count;
812 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000813
814 /*
815 * Fetch every inner tag from the tag to elements map.
816 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000817 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000818 if(tag2el) free(tag2el);
819 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000820 }
821
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000822 GEN_INCLUDE("constr_CHOICE");
823 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000824 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000825
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000826 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000827
828 /*
829 * Print out the table according to which the parsing is performed.
830 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000831 if(expr_elements_count(arg, expr)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000832
Lev Walkin21d00002005-03-04 08:48:53 +0000833 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
834 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000835
836 elements = 0;
837 INDENTED(TQ_FOR(v, &(expr->members), next) {
Lev Walkin659c63b2005-01-17 11:40:49 +0000838 if(v->expr_type == A1TC_EXTENSIBLE)
839 continue;
840 if(expr_better_indirect(arg, v))
841 v->marker.flags |= EM_INDIRECT;
842 elements++;
843 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000844 });
845 OUT("};\n");
846 } else {
847 elements = 0;
848 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000849
Lev Walkin64399722004-08-11 07:17:22 +0000850
Lev Walkinf15320b2004-06-03 03:38:44 +0000851 if(arg->embed) {
852 /*
853 * Our parent structure has already taken this into account.
854 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000855 tv_mode = _TVM_SAME;
856 tags_count = all_tags_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000857 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000858 tv_mode = emit_tags_vectors(arg, expr,
859 &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000860 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000861
862 /*
863 * Tags to elements map.
864 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000865 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000866
Lev Walkin21d00002005-03-04 08:48:53 +0000867 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000868 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000869 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000870 OUT("sizeof(struct ");
871 out_name_chain(arg, 1);
872 OUT("),\n");
873 OUT("offsetof(struct ");
874 out_name_chain(arg, 1);
875 OUT(", _asn_ctx),\n");
876 OUT("offsetof(struct ");
877 out_name_chain(arg, 1);
878 OUT(", present),\n");
879 OUT("sizeof(((struct ");
880 out_name_chain(arg, 1);
881 OUT(" *)0)->present),\n");
882 OUT("asn_MAP_%s_%d_tag2el,\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000883 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000884 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin35631482004-07-01 00:52:50 +0000885 OUT("%d\t/* Whether extensible */\n",
886 check_if_extensible(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000887 );
888 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000889
890 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000891 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000892 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000893 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000894 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000895
Lev Walkinf15320b2004-06-03 03:38:44 +0000896 REDIR(OT_TYPE_DECLS);
897
898 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000899} /* _CHOICE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000900
901int
902asn1c_lang_C_type_REFERENCE(arg_t *arg) {
903 asn1p_ref_t *ref;
904
905 ref = arg->expr->reference;
906 if(ref->components[ref->comp_count-1].name[0] == '&') {
Lev Walkinf15320b2004-06-03 03:38:44 +0000907 asn1p_expr_t *extract;
908 arg_t tmp;
909 int ret;
910
Lev Walkina9cc46e2004-09-22 16:06:28 +0000911 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
Lev Walkin08079b02004-08-22 03:25:24 +0000912 arg->expr, ref);
Lev Walkinf15320b2004-06-03 03:38:44 +0000913 if(extract == NULL)
914 return -1;
915
Lev Walkin08079b02004-08-22 03:25:24 +0000916 extract = asn1p_expr_clone(extract, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000917 if(extract) {
918 if(extract->Identifier)
919 free(extract->Identifier);
920 extract->Identifier = strdup(arg->expr->Identifier);
921 if(extract->Identifier == NULL) {
922 asn1p_expr_free(extract);
923 return -1;
924 }
925 } else {
926 return -1;
927 }
928
929 tmp = *arg;
930 tmp.asn = arg->asn;
Lev Walkin08079b02004-08-22 03:25:24 +0000931 tmp.mod = extract->module;
Lev Walkinf15320b2004-06-03 03:38:44 +0000932 tmp.expr = extract;
933
934 ret = arg->default_cb(&tmp);
935
936 asn1p_expr_free(extract);
937
938 return ret;
939 }
940
941
942 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
943}
944
945int
946asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
947 asn1p_expr_t *expr = arg->expr;
Lev Walkin64399722004-08-11 07:17:22 +0000948 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000949 int all_tags_count;
950 enum tvm_compat tv_mode;
Lev Walkine0b56e02005-02-25 12:10:27 +0000951 enum etd_spec etd_spec;
Lev Walkinf15320b2004-06-03 03:38:44 +0000952 char *p;
953
954 if(arg->embed) {
Lev Walkin05363a72004-09-29 13:16:40 +0000955 enum tnfmt tnfmt = TNF_CTYPE;
956
957 /*
958 * If this is an optional compound type,
959 * refer it using "struct X" convention,
960 * as it may recursively include the current structure.
961 */
Lev Walkinc8285712005-03-04 22:18:20 +0000962 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
Lev Walkin05363a72004-09-29 13:16:40 +0000963 asn1p_expr_t *terminal;
964 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
965 if(terminal
966 && (terminal->expr_type & ASN_CONSTR_MASK)) {
Lev Walkinc8285712005-03-04 22:18:20 +0000967 tnfmt = TNF_RSAFE;
968 REDIR(OT_FWD_DECLS);
Lev Walkinbf56d542005-03-04 23:50:56 +0000969 OUT("%s;\n",
Lev Walkinc8285712005-03-04 22:18:20 +0000970 asn1c_type_name(arg, arg->expr, tnfmt));
Lev Walkin05363a72004-09-29 13:16:40 +0000971 }
972 }
973
Lev Walkinf15320b2004-06-03 03:38:44 +0000974 REDIR(OT_TYPE_DECLS);
975
Lev Walkinc8285712005-03-04 22:18:20 +0000976 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
Lev Walkine0b56e02005-02-25 12:10:27 +0000977 if(!expr->_anonymous_type) {
Lev Walkinc8285712005-03-04 22:18:20 +0000978 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000979 OUT("%s", MKID_safe(expr->Identifier));
Lev Walkine0b56e02005-02-25 12:10:27 +0000980 if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT)
981 OUT("\t/* DEFAULT %s */",
982 asn1f_printable_value(
983 expr->marker.default_value));
984 else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL)
985 OUT("\t/* OPTIONAL */");
986 }
987
988 } else {
989 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
Lev Walkin59004fa2004-08-20 13:37:01 +0000990
991 REDIR(OT_TYPE_DECLS);
Lev Walkine0b56e02005-02-25 12:10:27 +0000992
993 OUT("typedef %s\t",
Lev Walkinc8285712005-03-04 22:18:20 +0000994 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
Lev Walkine0b56e02005-02-25 12:10:27 +0000995 OUT("%s%s_t",
Lev Walkinc8285712005-03-04 22:18:20 +0000996 (expr->marker.flags & EM_INDIRECT)?"*":" ",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000997 MKID(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000998 }
999
Lev Walkine0b56e02005-02-25 12:10:27 +00001000 if((expr->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin8ecf9db2005-03-03 21:28:12 +00001001 || (0 /* -- prohibited by X.693:8.3.4 */
1002 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +00001003 && expr_elements_count(arg, expr)))
1004 etd_spec = ETD_HAS_SPECIFICS;
1005 else
1006 etd_spec = ETD_NO_SPECIFICS;
Lev Walkinf15320b2004-06-03 03:38:44 +00001007
Lev Walkin8de2ab22004-09-26 13:11:31 +00001008 /*
1009 * If this type just blindly refers the other type, alias it.
1010 * Type1 ::= Type2
1011 */
Lev Walkine0b56e02005-02-25 12:10:27 +00001012 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
1013 REDIR(OT_TYPE_DECLS);
1014 return 0;
1015 }
Lev Walkin8de2ab22004-09-26 13:11:31 +00001016 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkine0b56e02005-02-25 12:10:27 +00001017 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
Lev Walkin6938d042005-03-04 23:23:50 +00001018 && etd_spec == ETD_NO_SPECIFICS
1019 && 0 /* This shortcut is incompatible with XER */
1020 ) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001021 char *type_name;
1022 REDIR(OT_FUNC_DECLS);
1023 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
1024 OUT("/* This type is equivalent to %s */\n", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001025 if(HIDE_INNER_DEFS) OUT("/* ");
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001026 OUT("#define\tasn_DEF_%s\t", MKID(expr->Identifier));
Lev Walkin8de2ab22004-09-26 13:11:31 +00001027 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkine0b56e02005-02-25 12:10:27 +00001028 OUT("asn_DEF_%s", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001029 if(HIDE_INNER_DEFS)
Lev Walkine0b56e02005-02-25 12:10:27 +00001030 OUT("\t// (Use -fall-defs-global to expose) */");
1031 OUT("\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001032 REDIR(OT_CODE);
1033 OUT("/* This type is equivalent to %s */\n", type_name);
1034 OUT("\n");
1035 REDIR(OT_TYPE_DECLS);
1036 return 0;
1037 }
1038
Lev Walkinf15320b2004-06-03 03:38:44 +00001039 REDIR(OT_STAT_DEFS);
1040
Lev Walkin27ea3802004-06-28 21:13:46 +00001041 /*
Lev Walkin05363a72004-09-29 13:16:40 +00001042 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +00001043 */
Lev Walkin188ed2c2004-09-13 08:31:01 +00001044 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +00001045
Lev Walkine0b56e02005-02-25 12:10:27 +00001046 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1047 0, etd_spec);
Lev Walkinf15320b2004-06-03 03:38:44 +00001048
Lev Walkin59004fa2004-08-20 13:37:01 +00001049 REDIR(OT_CODE);
1050
Lev Walkinf15320b2004-06-03 03:38:44 +00001051 /*
1052 * Constraint checking.
1053 */
Lev Walkin8de2ab22004-09-26 13:11:31 +00001054 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1055 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001056 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001057 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001058 OUT("%s", p);
1059 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1060 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001061 INDENT(+1);
1062 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1063 OUT("\n");
1064 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1065 OUT("/* Replace with underlying type checker */\n");
1066 OUT("td->check_constraints "
Lev Walkin05363a72004-09-29 13:16:40 +00001067 "= asn_DEF_%s.check_constraints;\n",
Lev Walkin8de2ab22004-09-26 13:11:31 +00001068 asn1c_type_name(arg, expr, TNF_SAFE));
1069 OUT("return td->check_constraints"
1070 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +00001071 }
Lev Walkin8de2ab22004-09-26 13:11:31 +00001072 INDENT(-1);
1073 OUT("}\n");
1074 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001075 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001076
1077 /*
1078 * Emit suicidal functions.
1079 */
1080
Lev Walkinf15320b2004-06-03 03:38:44 +00001081 /*
1082 * This function replaces certain fields from the definition
1083 * of a type with the corresponding fields from the basic type
1084 * (from which the current type is inherited).
1085 */
Lev Walkinf15320b2004-06-03 03:38:44 +00001086 OUT("/*\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001087 OUT(" * This type is implemented using %s,\n",
1088 asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkinb65b3002004-09-23 22:20:47 +00001089 OUT(" * so here we adjust the DEF accordingly.\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001090 OUT(" */\n");
1091 OUT("static void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001092 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001093 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001094 INDENT(+1);
Lev Walkine0b56e02005-02-25 12:10:27 +00001095 {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001096 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001097 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkin05363a72004-09-29 13:16:40 +00001098 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1099 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1100 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1101 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1102 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1103 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001104 if(!terminal && !tags_count) {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001105 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
Lev Walkin05363a72004-09-29 13:16:40 +00001106 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1107 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1108 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1109 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001110 OUT("/* End of these lines */\n");
1111 }
Lev Walkin05363a72004-09-29 13:16:40 +00001112 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1113 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
Lev Walkine0b56e02005-02-25 12:10:27 +00001114 if(etd_spec != ETD_NO_SPECIFICS) {
1115 INDENT(-1);
Lev Walkin7ef83a42005-03-29 19:04:24 +00001116 OUT(" /* ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001117 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001118 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1119 if(etd_spec == ETD_NO_SPECIFICS) {
1120 INDENT(-1);
1121 OUT("\n");
1122 } else {
1123 OUT("\t// Defined explicitly */\n");
1124 }
1125 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001126 OUT("}\n");
1127 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001128
1129 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001130 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001131 OUT("void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001132 OUT("%s", p);
1133 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1134 OUT("_free(asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001135 INDENTED(
Lev Walkina9cc46e2004-09-22 16:06:28 +00001136 OUT("\tvoid *struct_ptr, int contents_only) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001137 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1138 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001139 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001140 );
1141 OUT("}\n");
1142 OUT("\n");
1143
1144 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001145 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinf15320b2004-06-03 03:38:44 +00001146 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001147 OUT("%s", p);
1148 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1149 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001150 INDENTED(
1151 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001152 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1153 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001154 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1155 );
1156 OUT("}\n");
1157 OUT("\n");
1158
1159 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001160 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkindc06f6b2004-10-20 15:50:55 +00001161 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001162 OUT("%s", p);
1163 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1164 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001165 INDENTED(
Lev Walkindd1512e2005-03-17 21:56:00 +00001166 OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001167 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1168 p, expr->_type_unique_index);
Lev Walkin05363a72004-09-29 13:16:40 +00001169 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001170 );
1171 OUT("}\n");
1172 OUT("\n");
1173
1174 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001175 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001176 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001177 OUT("%s", p);
1178 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1179 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001180 INDENTED(
1181 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1182 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001183 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1184 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001185 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1186 );
1187 OUT("}\n");
1188 OUT("\n");
1189
1190 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001191 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinca47c292004-10-23 13:34:00 +00001192 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001193 OUT("%s", p);
1194 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1195 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinca47c292004-10-23 13:34:00 +00001196 INDENTED(
Lev Walkindd1512e2005-03-17 21:56:00 +00001197 OUT("\tvoid **structure, const char *opt_mname, const void *bufptr, size_t size) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001198 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1199 p, expr->_type_unique_index);
Lev Walkinca47c292004-10-23 13:34:00 +00001200 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1201 );
1202 OUT("}\n");
1203 OUT("\n");
1204
1205 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001206 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001207 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001208 OUT("%s", p);
1209 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1210 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001211 INDENTED(
1212 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1213 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001214 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1215 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001216 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001217 );
1218 OUT("}\n");
1219 OUT("\n");
1220
1221 REDIR(OT_FUNC_DECLS);
1222
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001223 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001224 if(HIDE_INNER_DEFS) {
Lev Walkin21d00002005-03-04 08:48:53 +00001225 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1226 "\t// (Use -fall-defs-global to expose) */\n",
1227 p, expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +00001228 } else {
1229 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1230 OUT("asn_struct_free_f %s_free;\n", p);
1231 OUT("asn_struct_print_f %s_print;\n", p);
1232 OUT("asn_constr_check_f %s_constraint;\n", p);
1233 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1234 OUT("der_type_encoder_f %s_encode_der;\n", p);
1235 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1236 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1237 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001238
Lev Walkin3dcaafa2004-08-11 05:21:32 +00001239 REDIR(OT_TYPE_DECLS);
1240
Lev Walkinf15320b2004-06-03 03:38:44 +00001241 return 0;
1242}
1243
1244int
1245asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1246
1247 OUT("/*\n");
1248 OUT(" * This type is extensible,\n");
1249 OUT(" * possible extensions are below.\n");
1250 OUT(" */\n");
1251
1252 return 0;
1253}
1254
Lev Walkin35631482004-07-01 00:52:50 +00001255static int check_if_extensible(asn1p_expr_t *expr) {
1256 asn1p_expr_t *v;
1257 TQ_FOR(v, &(expr->members), next) {
1258 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1259 }
1260 return 0;
1261}
1262
Lev Walkinf15320b2004-06-03 03:38:44 +00001263static int
Lev Walkinc3e29402004-09-10 06:07:18 +00001264_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001265
1266 OUT("(");
Lev Walkinc3e29402004-09-10 06:07:18 +00001267 switch(tag->tag_class) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001268 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1269 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1270 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1271 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1272 case TC_NOCLASS:
1273 break;
1274 }
Lev Walkin33c16ba2004-09-24 21:01:43 +00001275 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
Lev Walkinf15320b2004-06-03 03:38:44 +00001276
1277 return 0;
1278}
1279
Lev Walkinfd171ef2004-06-06 07:20:17 +00001280
1281static int
1282_tag2el_cmp(const void *ap, const void *bp) {
1283 const tag2el_t *a = ap;
1284 const tag2el_t *b = bp;
1285 const struct asn1p_type_tag_s *ta = &a->el_tag;
1286 const struct asn1p_type_tag_s *tb = &b->el_tag;
1287
1288 if(ta->tag_class == tb->tag_class) {
1289 if(ta->tag_value == tb->tag_value) {
1290 /*
1291 * Sort by their respective positions.
1292 */
1293 if(a->el_no < b->el_no)
1294 return -1;
1295 else if(a->el_no > b->el_no)
1296 return 1;
1297 return 0;
1298 } else if(ta->tag_value < tb->tag_value)
1299 return -1;
1300 else
1301 return 1;
1302 } else if(ta->tag_class < tb->tag_class) {
1303 return -1;
1304 } else {
1305 return 1;
1306 }
1307}
1308
Lev Walkinf15320b2004-06-03 03:38:44 +00001309/*
1310 * For constructed types, number of external tags may be greater than
1311 * number of elements in the type because of CHOICE type.
1312 * T ::= SET { -- Three possible tags:
1313 * a INTEGER, -- One tag is here...
1314 * b Choice1 -- ... and two more tags are there.
1315 * }
1316 * Choice1 ::= CHOICE {
1317 * s1 IA5String,
1318 * s2 ObjectDescriptor
1319 * }
1320 */
1321static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001322_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001323 asn1p_expr_t *expr = arg->expr;
1324 arg_t tmparg = *arg;
1325 asn1p_expr_t *v;
1326 int element = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001327 int original_count = *count;
1328 int sort_until = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001329
1330 TQ_FOR(v, &(expr->members), next) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001331 if(v->expr_type == A1TC_EXTENSIBLE) {
1332 /*
Lev Walkine0b56e02005-02-25 12:10:27 +00001333 * CXER mandates sorting
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001334 * only for the root part.
1335 */
1336 if(flags == FTE_CANONICAL_XER
1337 && sort_until == -1)
1338 sort_until = *count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001339 continue;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001340 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001341
1342 tmparg.expr = v;
1343
1344 if(_add_tag2el_member(&tmparg, tag2el, count,
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001345 (el_no==-1)?element:el_no, flags)) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001346 return -1;
1347 }
1348
1349 element++;
1350 }
1351
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001352
1353 if(flags == FTE_CANONICAL_XER) {
1354 if(sort_until == -1) sort_until = *count;
1355 qsort((*tag2el) + original_count,
1356 sort_until - original_count,
1357 sizeof(**tag2el), _tag2el_cmp);
1358 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1359 && (sort_until - original_count) >= 1) {
1360 /* Only take in account the root component */
1361 *count = original_count + 1;
1362 }
1363 } else {
1364 /*
1365 * Sort the map according to canonical order of their
1366 * tags and element numbers.
1367 */
1368 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1369 }
Lev Walkinfd171ef2004-06-06 07:20:17 +00001370
Lev Walkin38abe792004-06-14 13:09:45 +00001371 /*
1372 * Initialize .toff_{first|last} members.
1373 */
1374 if(*count) {
1375 struct asn1p_type_tag_s *cur_tag = 0;
1376 tag2el_t *cur = *tag2el;
1377 tag2el_t *end = cur + *count;
1378 int occur, i;
1379 for(occur = 0; cur < end; cur++) {
1380 if(cur_tag == 0
1381 || cur_tag->tag_value != cur->el_tag.tag_value
1382 || cur_tag->tag_class != cur->el_tag.tag_class) {
1383 cur_tag = &cur->el_tag;
1384 occur = 0;
1385 } else {
1386 occur++;
1387 }
1388 cur->toff_first = -occur;
1389 for(i = 0; i >= -occur; i--)
1390 cur[i].toff_last = -i;
1391 }
1392 }
1393
Lev Walkinf15320b2004-06-03 03:38:44 +00001394 return 0;
1395}
1396
1397static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001398_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001399 struct asn1p_type_tag_s tag;
1400 int ret;
1401
1402 assert(el_no >= 0);
1403
Lev Walkina9cc46e2004-09-22 16:06:28 +00001404 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1405 arg->expr, &tag, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001406 if(ret == 0) {
Lev Walkin201943e2004-09-04 04:42:29 +00001407 tag2el_t *te;
1408 int new_count = (*count) + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001409 void *p;
Lev Walkin201943e2004-09-04 04:42:29 +00001410
Lev Walkinac7e2292004-09-05 10:42:33 +00001411 if(tag.tag_value == -1) {
1412 /*
1413 * This is an untagged ANY type,
1414 * proceed without adding a tag
1415 */
1416 return 0;
1417 }
1418
Lev Walkin201943e2004-09-04 04:42:29 +00001419 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
Lev Walkinf15320b2004-06-03 03:38:44 +00001420 if(p) *tag2el = p;
1421 else return -1;
1422
1423 DEBUG("Found tag for %s: %ld",
1424 arg->expr->Identifier,
1425 (long)tag.tag_value);
1426
Lev Walkin201943e2004-09-04 04:42:29 +00001427 te = &((*tag2el)[*count]);
1428 te->el_tag = tag;
1429 te->el_no = el_no;
1430 te->from_expr = arg->expr;
1431 *count = new_count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001432 return 0;
1433 }
1434
1435 DEBUG("Searching tag in complex expression %s:%x at line %d",
1436 arg->expr->Identifier,
1437 arg->expr->expr_type,
1438 arg->expr->_lineno);
1439
1440 /*
1441 * Iterate over members of CHOICE type.
1442 */
1443 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001444 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001445 }
1446
1447 if(arg->expr->expr_type == A1TC_REFERENCE) {
1448 arg_t tmp = *arg;
1449 asn1p_expr_t *expr;
Lev Walkin08079b02004-08-22 03:25:24 +00001450 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001451 arg->expr->reference);
1452 if(expr) {
Lev Walkin08079b02004-08-22 03:25:24 +00001453 tmp.mod = expr->module;
Lev Walkinf15320b2004-06-03 03:38:44 +00001454 tmp.expr = expr;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001455 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001456 } else {
1457 FATAL("Cannot dereference %s at line %d",
1458 arg->expr->Identifier,
1459 arg->expr->_lineno);
1460 return -1;
1461 }
1462 }
1463
1464 DEBUG("No tag for %s at line %d",
1465 arg->expr->Identifier,
1466 arg->expr->_lineno);
1467
1468 return -1;
1469}
1470
1471static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001472emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
Lev Walkinfd171ef2004-06-06 07:20:17 +00001473 asn1p_expr_t *expr = arg->expr;
Lev Walkinf3b29b12005-06-02 05:21:37 +00001474 int i;
1475
1476 if(!tag2el_count) return 0; /* No top level tags */
Lev Walkinfd171ef2004-06-06 07:20:17 +00001477
Lev Walkin21d00002005-03-04 08:48:53 +00001478 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001479 MKID(expr->Identifier), expr->_type_unique_index,
Lev Walkin21d00002005-03-04 08:48:53 +00001480 opt_modifier?opt_modifier:"");
Lev Walkinf3b29b12005-06-02 05:21:37 +00001481 for(i = 0; i < tag2el_count; i++) {
1482 OUT(" { ");
1483 _print_tag(arg, &tag2el[i].el_tag);
1484 OUT(", ");
1485 OUT("%d, ", tag2el[i].el_no);
1486 OUT("%d, ", tag2el[i].toff_first);
1487 OUT("%d ", tag2el[i].toff_last);
1488 OUT("}%s /* %s at %d */\n",
1489 (i + 1 < tag2el_count) ? "," : "",
1490 tag2el[i].from_expr->Identifier,
1491 tag2el[i].from_expr->_lineno
1492 );
Lev Walkinfd171ef2004-06-06 07:20:17 +00001493 }
1494 OUT("};\n");
1495
Lev Walkinf3b29b12005-06-02 05:21:37 +00001496 return 0;
Lev Walkinfd171ef2004-06-06 07:20:17 +00001497}
1498
Lev Walkin188ed2c2004-09-13 08:31:01 +00001499static enum tvm_compat
1500emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1501 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1502 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
Lev Walkin64399722004-08-11 07:17:22 +00001503 int tags_count = 0;
Lev Walkin188ed2c2004-09-13 08:31:01 +00001504 int all_tags_count = 0;
1505 enum tvm_compat tv_mode = _TVM_SAME;
Lev Walkin906654e2004-09-10 15:49:15 +00001506 int i;
Lev Walkin27ea3802004-06-28 21:13:46 +00001507
Lev Walkin6d1220a2004-09-14 14:10:10 +00001508 /* Cleanup before proceeding. */
1509 *tags_count_r = 0;
1510 *all_tags_count_r = 0;
1511
Lev Walkin906654e2004-09-10 15:49:15 +00001512 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001513 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
Lev Walkin6d1220a2004-09-14 14:10:10 +00001514 if(tags_count < 0)
1515 return -1;
Lev Walkin64399722004-08-11 07:17:22 +00001516
Lev Walkin188ed2c2004-09-13 08:31:01 +00001517 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001518 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
Lev Walkin188ed2c2004-09-13 08:31:01 +00001519 &all_tags, AFT_FULL_COLLECT);
1520 if(all_tags_count < 0) {
1521 if(tags) free(tags);
1522 return -1;
Lev Walkin5a8219a2004-09-08 00:28:57 +00001523 }
Lev Walkin906654e2004-09-10 15:49:15 +00001524
Lev Walkin188ed2c2004-09-13 08:31:01 +00001525 assert(tags_count <= all_tags_count);
1526 assert((tags_count?0:1) == (all_tags_count?0:1));
Lev Walkin27ea3802004-06-28 21:13:46 +00001527
Lev Walkin188ed2c2004-09-13 08:31:01 +00001528 if(tags_count <= all_tags_count) {
1529 for(i = 0; i < tags_count; i++) {
1530 if(tags[i].tag_value != all_tags[i].tag_value
1531 || tags[i].tag_class != all_tags[i].tag_class) {
1532 tv_mode = _TVM_DIFFERENT;
1533 break;
1534 }
1535 }
1536 if(i == tags_count && tags_count < all_tags_count)
1537 tv_mode = _TVM_SUBSET;
1538 } else {
1539 tv_mode = _TVM_DIFFERENT;
1540 }
1541
1542#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
Lev Walkin21d00002005-03-04 08:48:53 +00001543 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001544 MKID(expr->Identifier), \
Lev Walkin21d00002005-03-04 08:48:53 +00001545 expr->_type_unique_index, name); \
Lev Walkin188ed2c2004-09-13 08:31:01 +00001546 INDENT(+1); \
1547 /* Print the array of collected tags */ \
1548 for(i = 0; i < tags_count; i++) { \
1549 if(i) OUT(",\n"); \
1550 _print_tag(arg, &tags[i]); \
1551 } \
1552 OUT("\n"); \
1553 INDENT(-1); \
1554 OUT("};\n"); \
1555 } while(0)
1556
1557 if(tags_count) {
1558 if(tv_mode == _TVM_SUBSET)
1559 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1560 else
1561 EMIT_TAGS_TABLE("", tags, tags_count);
1562 }
1563
1564 if(all_tags_count) {
1565 if(tv_mode == _TVM_DIFFERENT)
1566 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1567 }
1568
1569 if(tags) free(tags);
1570 if(all_tags) free(all_tags);
1571
1572 *tags_count_r = tags_count;
1573 *all_tags_count_r = all_tags_count;
1574
1575 return tv_mode;
Lev Walkin27ea3802004-06-28 21:13:46 +00001576}
Lev Walkin59004fa2004-08-20 13:37:01 +00001577
1578static int
Lev Walkin59964be2004-08-25 02:03:12 +00001579expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001580 asn1p_expr_t *topmost_parent;
1581 asn1p_expr_t *v;
1582 int elements = 0;
1583
Lev Walkina9cc46e2004-09-22 16:06:28 +00001584 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001585 if(!topmost_parent) return 0;
1586
Lev Walkine0b56e02005-02-25 12:10:27 +00001587 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1588 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1589 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin59004fa2004-08-20 13:37:01 +00001590 return 0;
1591
1592 TQ_FOR(v, &(topmost_parent->members), next) {
1593 if(v->expr_type != A1TC_EXTENSIBLE)
1594 elements++;
1595 }
1596
1597 return elements;
1598}
1599
1600static int
Lev Walkinc8285712005-03-04 22:18:20 +00001601emit_include_dependencies(arg_t *arg) {
1602 asn1p_expr_t *expr = arg->expr;
1603 asn1p_expr_t *memb;
1604
1605 TQ_FOR(memb, &(expr->members), next) {
1606
1607 if((memb->meta_type == AMT_TYPEREF
1608 && (memb->marker.flags & EM_INDIRECT))
1609 || expr->expr_type == ASN_CONSTR_SET_OF
1610 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1611 ) {
1612 asn1p_expr_t *terminal;
1613 terminal = asn1f_find_terminal_type_ex(arg->asn, memb);
1614 if(terminal && !terminal->parent_expr
1615 && (terminal->expr_type & ASN_CONSTR_MASK)) {
1616 int saved_target = arg->target->target;
1617 REDIR(OT_FWD_DECLS);
Lev Walkinbf56d542005-03-04 23:50:56 +00001618 OUT("%s;\n",
Lev Walkinc8285712005-03-04 22:18:20 +00001619 asn1c_type_name(arg, memb, TNF_RSAFE));
1620 REDIR(saved_target);
1621 memb->marker.flags |= EM_UNRECURSE;
1622 }
1623 }
1624
1625 if((!(memb->expr_type & ASN_CONSTR_MASK)
1626 && memb->expr_type > ASN_CONSTR_MASK)
1627 || memb->meta_type == AMT_TYPEREF) {
1628 if(memb->marker.flags & EM_UNRECURSE) {
1629 GEN_POSTINCLUDE(asn1c_type_name(arg,
1630 memb, TNF_INCLUDE));
1631 } else {
1632 GEN_INCLUDE(asn1c_type_name(arg,
1633 memb, TNF_INCLUDE));
1634 }
1635 }
1636 }
1637
1638 return 0;
1639}
1640
1641static int
Lev Walkin59964be2004-08-25 02:03:12 +00001642emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001643 int save_target;
1644 arg_t tmp_arg;
Lev Walkinb9189732004-09-10 09:37:12 +00001645 struct asn1p_type_tag_s outmost_tag_s;
1646 struct asn1p_type_tag_s *outmost_tag;
Lev Walkine0b56e02005-02-25 12:10:27 +00001647 int complex_contents;
Lev Walkin59004fa2004-08-20 13:37:01 +00001648 char *p;
1649
Lev Walkinb9189732004-09-10 09:37:12 +00001650 if(asn1f_fetch_outmost_tag(arg->asn,
1651 expr->module, expr, &outmost_tag_s, 1)) {
1652 outmost_tag = 0;
1653 } else {
1654 outmost_tag = &outmost_tag_s;
1655 }
1656
Lev Walkin59004fa2004-08-20 13:37:01 +00001657 OUT("{ ");
Lev Walkinb9189732004-09-10 09:37:12 +00001658
1659 if(outmost_tag && outmost_tag->tag_value == -1)
1660 OUT("ATF_OPEN_TYPE | ");
Lev Walkinc8285712005-03-04 22:18:20 +00001661 OUT("%s, ",
1662 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
Lev Walkin0204fa62004-09-15 11:54:38 +00001663 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001664 asn1p_expr_t *tv;
1665 int opts = 0;
Lev Walkin0204fa62004-09-15 11:54:38 +00001666 for(tv = expr; tv && tv->marker.flags;
Lev Walkin59004fa2004-08-20 13:37:01 +00001667 tv = TQ_NEXT(tv, next), opts++) {
1668 if(tv->expr_type == A1TC_EXTENSIBLE)
1669 opts--;
1670 }
1671 OUT("%d, ", opts);
1672 } else {
1673 OUT("0, ");
1674 }
Lev Walkina9cc46e2004-09-22 16:06:28 +00001675 if(expr->_anonymous_type) {
1676 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1677 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1678 OUT("0,\n");
1679 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001680 OUT("offsetof(struct ");
1681 out_name_chain(arg, 1);
1682 OUT(", ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001683 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1684 && (!UNNAMED_UNIONS)) OUT("choice.");
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001685 OUT("%s),\n", MKID_safe(expr->Identifier));
Lev Walkin59004fa2004-08-20 13:37:01 +00001686 }
1687 INDENT(+1);
1688 if(C99_MODE) OUT(".tag = ");
Lev Walkinb9189732004-09-10 09:37:12 +00001689 if(outmost_tag) {
1690 if(outmost_tag->tag_value == -1)
1691 OUT("-1 /* Ambiguous tag (ANY?) */");
1692 else
1693 _print_tag(arg, outmost_tag);
Lev Walkinc3e29402004-09-10 06:07:18 +00001694 } else {
Lev Walkinb9189732004-09-10 09:37:12 +00001695 OUT("-1 /* Ambiguous tag (CHOICE?) */");
Lev Walkinc3e29402004-09-10 06:07:18 +00001696 }
Lev Walkinb9189732004-09-10 09:37:12 +00001697
Lev Walkin59004fa2004-08-20 13:37:01 +00001698 OUT(",\n");
1699 if(C99_MODE) OUT(".tag_mode = ");
1700 if(expr->tag.tag_class) {
1701 if(expr->tag.tag_mode == TM_IMPLICIT)
1702 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1703 else
1704 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1705 } else {
1706 OUT("0,\n");
1707 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001708
1709 complex_contents =
1710 (expr->expr_type & ASN_CONSTR_MASK)
1711 || expr->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +00001712 || (0 /* -- prohibited by X.693:8.3.4 */
1713 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +00001714 && expr_elements_count(arg, expr));
Lev Walkin59004fa2004-08-20 13:37:01 +00001715 if(C99_MODE) OUT(".type = ");
Lev Walkin21d00002005-03-04 08:48:53 +00001716 OUT("(void *)&asn_DEF_");
1717 if(complex_contents) {
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001718 OUT("%s", MKID(expr->Identifier));
Lev Walkin21d00002005-03-04 08:48:53 +00001719 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1720 OUT("_%d", expr->_type_unique_index);
Lev Walkin08079b02004-08-22 03:25:24 +00001721 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001722 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkin08079b02004-08-22 03:25:24 +00001723 }
Lev Walkin21d00002005-03-04 08:48:53 +00001724 OUT(",\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001725 if(C99_MODE) OUT(".memb_constraints = ");
1726 if(expr->constraints) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001727 if(arg->flags & A1C_NO_CONSTRAINTS) {
1728 OUT("0,\t/* No check because of -fno-constraints */\n");
1729 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001730 char *id = MKID(expr->Identifier);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001731 if(expr->_anonymous_type
Lev Walkin152a91e2005-02-14 20:41:29 +00001732 && !strcmp(expr->Identifier, "Member"))
Lev Walkin8de2ab22004-09-26 13:11:31 +00001733 id = asn1c_type_name(arg, expr, TNF_SAFE);
1734 OUT("memb_%s_%d_constraint,\n", id,
Lev Walkin21d00002005-03-04 08:48:53 +00001735 arg->expr->_type_unique_index);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001736 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001737 } else {
Lev Walkinb7bfd1a2005-01-17 12:16:58 +00001738 OUT("0,\t/* Defer constraints checking to the member type */\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001739 }
1740 if(C99_MODE) OUT(".name = ");
Lev Walkin152a91e2005-02-14 20:41:29 +00001741 if(1) {
1742 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1743 OUT("\"\"\n");
1744 else
1745 OUT("\"%s\"\n", expr->Identifier);
1746 } else {
1747 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1748 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001749 OUT("},\n");
1750 INDENT(-1);
1751
Lev Walkin8de2ab22004-09-26 13:11:31 +00001752 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkin59004fa2004-08-20 13:37:01 +00001753 return 0;
1754
1755 save_target = arg->target->target;
1756 REDIR(OT_CODE);
1757
Lev Walkin152a91e2005-02-14 20:41:29 +00001758 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
Lev Walkin59004fa2004-08-20 13:37:01 +00001759 p = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001760 else
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001761 p = MKID(expr->Identifier);
Lev Walkin59004fa2004-08-20 13:37:01 +00001762 OUT("static int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001763 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 +00001764 INDENT(+1);
1765 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1766 tmp_arg = *arg;
1767 tmp_arg.expr = expr;
1768 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001769 OUT("return td->check_constraints"
1770 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001771 }
1772 INDENT(-1);
1773 OUT("}\n");
1774 OUT("\n");
1775
1776 REDIR(save_target);
1777
1778 return 0;
1779}
Lev Walkin59964be2004-08-25 02:03:12 +00001780
Lev Walkindc06f6b2004-10-20 15:50:55 +00001781/*
1782 * Generate "asn_DEF_XXX" type definition.
1783 */
Lev Walkin59964be2004-08-25 02:03:12 +00001784static int
Lev Walkin8de2ab22004-09-26 13:11:31 +00001785emit_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 +00001786 int using_type_name = 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001787 char *p;
1788
Lev Walkindd32b592004-09-06 08:07:29 +00001789 if(HIDE_INNER_DEFS)
1790 OUT("static /* Use -fall-defs-global to expose */\n");
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001791 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID(expr->Identifier));
Lev Walkin21d00002005-03-04 08:48:53 +00001792 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1793 OUT(" = {\n");
Lev Walkin801fabc2005-01-28 12:18:50 +00001794 p = MKID(expr->Identifier);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001795 INDENT(+1);
Lev Walkin59964be2004-08-25 02:03:12 +00001796 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001797 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001798
1799 if(expr->expr_type & ASN_CONSTR_MASK) {
Lev Walkin21d00002005-03-04 08:48:53 +00001800 using_type_name = 1;
Lev Walkin59964be2004-08-25 02:03:12 +00001801 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1802 }
1803
Lev Walkin21d00002005-03-04 08:48:53 +00001804#define FUNCREF(foo) do { \
1805 OUT("%s", p); \
1806 if(HIDE_INNER_DEFS && !using_type_name) \
1807 OUT("_%d", expr->_type_unique_index); \
1808 OUT("_" #foo ",\n"); \
1809} while(0)
1810
1811 FUNCREF(free);
1812 FUNCREF(print);
1813 FUNCREF(constraint);
1814 FUNCREF(decode_ber);
1815 FUNCREF(encode_der);
1816 FUNCREF(decode_xer);
1817 FUNCREF(encode_xer);
Lev Walkin59964be2004-08-25 02:03:12 +00001818
Lev Walkin59964be2004-08-25 02:03:12 +00001819 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1820 OUT("CHOICE_outmost_tag,\n");
1821 } else {
1822 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1823 }
1824
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001825 p = MKID(expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001826 if(tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001827 OUT("asn_DEF_%s_%d_tags,\n",
1828 p, expr->_type_unique_index);
1829 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1830 p, expr->_type_unique_index);
1831 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1832 p, expr->_type_unique_index);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001833 if(tv_mode == _TVM_SUBSET
1834 && tags_count != all_tags_count)
1835 OUT(" - %d", all_tags_count - tags_count);
1836 OUT(", /* %d */\n", tags_count);
Lev Walkin59964be2004-08-25 02:03:12 +00001837 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001838 OUT("0,\t/* No effective tags (pointer) */\n");
1839 OUT("0,\t/* No effective tags (count) */\n");
1840 }
1841
1842 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
Lev Walkin21d00002005-03-04 08:48:53 +00001843 OUT("asn_DEF_%s_%d_all_tags,\n",
1844 p, expr->_type_unique_index);
1845 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1846 p, expr->_type_unique_index);
1847 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1848 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001849 } else if(all_tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001850 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1851 p, expr->_type_unique_index);
1852 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1853 p, expr->_type_unique_index);
1854 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1855 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001856 } else {
1857 OUT("0,\t/* No tags (pointer) */\n");
1858 OUT("0,\t/* No tags (count) */\n");
Lev Walkin59964be2004-08-25 02:03:12 +00001859 }
1860
Lev Walkin59964be2004-08-25 02:03:12 +00001861 if(elements_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001862 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001863 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1864 || expr->expr_type == ASN_CONSTR_SET_OF) {
1865 OUT("%d,\t/* Single element */\n",
1866 elements_count);
1867 assert(elements_count == 1);
1868 } else {
1869 OUT("%d,\t/* Elements count */\n",
1870 elements_count);
1871 }
1872 } else {
Lev Walkin59964be2004-08-25 02:03:12 +00001873 if(expr_elements_count(arg, expr))
1874 OUT("0, 0,\t/* Defined elsewhere */\n");
1875 else
1876 OUT("0, 0,\t/* No members */\n");
1877 }
1878
1879 switch(spec) {
1880 case ETD_NO_SPECIFICS:
1881 OUT("0\t/* No specifics */\n");
1882 break;
1883 case ETD_HAS_SPECIFICS:
Lev Walkin21d00002005-03-04 08:48:53 +00001884 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1885 p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001886 }
Lev Walkin188ed2c2004-09-13 08:31:01 +00001887 INDENT(-1);
Lev Walkin59964be2004-08-25 02:03:12 +00001888 OUT("};\n");
1889 OUT("\n");
Lev Walkin97298782004-08-26 06:20:34 +00001890
1891 return 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001892}
Lev Walkincc93b0f2004-09-10 09:18:20 +00001893
1894/*
1895 * Check if it is better to make this type indirectly accessed via
1896 * a pointer.
1897 * This may be the case for the following recursive definition:
1898 * Type ::= CHOICE { member Type };
1899 */
1900static int
1901expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) {
1902 asn1p_expr_t *top_parent;
1903 asn1p_expr_t *terminal;
1904
1905 if(expr->expr_type != A1TC_REFERENCE)
1906 return 0;
1907
Lev Walkin72a0f5a2005-07-24 08:28:39 +00001908 /* -findirect-choice compiles members of CHOICE as indirect pointers */
1909 if((arg->flags & A1C_INDIRECT_CHOICE)
1910 && arg->expr->expr_type == ASN_CONSTR_CHOICE)
1911 return 1;
1912
Lev Walkincc93b0f2004-09-10 09:18:20 +00001913 /* Rewind to the topmost parent expression */
1914 if((top_parent = expr->parent_expr)) {
1915 while(top_parent->parent_expr)
1916 top_parent = top_parent->parent_expr;
1917 } else {
1918 return 0;
1919 }
1920
Lev Walkina9cc46e2004-09-22 16:06:28 +00001921 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkincc93b0f2004-09-10 09:18:20 +00001922
1923 return (terminal == top_parent);
1924}
Lev Walkina9cc46e2004-09-22 16:06:28 +00001925
1926static int
1927expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1928 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1929 if(!expr) return 0;
1930
1931 /* X.680, 25.5, Table 5 */
1932 switch(expr->expr_type) {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001933 case ASN_BASIC_BOOLEAN:
1934 case ASN_BASIC_ENUMERATED:
1935 case ASN_BASIC_NULL:
1936 return 1;
1937 default:
1938 return 0;
1939 }
1940}
Lev Walkin801fabc2005-01-28 12:18:50 +00001941
1942static int
Lev Walkin21d00002005-03-04 08:48:53 +00001943out_name_chain(arg_t *arg, int check_reserved_keywords) {
Lev Walkin801fabc2005-01-28 12:18:50 +00001944 asn1p_expr_t *expr = arg->expr;
1945 char *id;
1946
1947 assert(expr->Identifier);
1948
Lev Walkin21d00002005-03-04 08:48:53 +00001949 if(arg->flags & A1C_COMPOUND_NAMES
1950 && ((expr->expr_type & ASN_CONSTR_MASK)
1951 || expr->expr_type == ASN_BASIC_ENUMERATED
Lev Walkinb02a8832005-08-13 23:51:47 +00001952 || ((expr->expr_type == ASN_BASIC_INTEGER
1953 || expr->expr_type == ASN_BASIC_BIT_STRING)
Lev Walkin21d00002005-03-04 08:48:53 +00001954 && expr_elements_count(arg, expr))
1955 )
Lev Walkin801fabc2005-01-28 12:18:50 +00001956 && expr->parent_expr
1957 && expr->parent_expr->Identifier) {
1958 arg_t tmparg = *arg;
1959
1960 tmparg.expr = expr->parent_expr;
Lev Walkin21d00002005-03-04 08:48:53 +00001961 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
1962
1963 out_name_chain(&tmparg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +00001964
Lev Walkin01633092005-03-05 00:08:41 +00001965 OUT("__"); /* a separator between id components */
Lev Walkin21d00002005-03-04 08:48:53 +00001966
Lev Walkin801fabc2005-01-28 12:18:50 +00001967 /* Fall through */
1968 }
1969
1970 if(check_reserved_keywords)
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001971 id = MKID_safe(expr->Identifier);
Lev Walkin801fabc2005-01-28 12:18:50 +00001972 else
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001973 id = MKID(expr->Identifier);
Lev Walkin801fabc2005-01-28 12:18:50 +00001974 OUT("%s", id);
1975
1976 return 0;
1977}