blob: f91da76fb7c66a6adde7bf5e96072f5a9a840ad4 [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);
221 int eidx = 0;
222
223 if(el_count) {
224 REDIR(OT_DEPS);
225 OUT("typedef enum ");
226 out_name_chain(arg, 1);
227 OUT(" {\n");
228 TQ_FOR(v, &(expr->members), next) {
229 switch(v->expr_type) {
230 case A1TC_UNIVERVAL:
231 OUT("\t");
232 out_name_chain(arg, 0);
233 OUT("_%s", MKID(v->Identifier));
234 OUT("\t= %" PRIdASN "%s\n",
235 v->value->value.v_integer,
236 (eidx+1 < el_count) ? "," : "");
237 eidx++;
238 break;
239 default:
240 OUT("/* Unexpected BIT STRING element: %s */\n",
241 v->Identifier);
242 break;
243 }
244 }
245 OUT("} ");
246 out_name_chain(arg, 0);
247 OUT("_e;\n");
248 assert(eidx == el_count);
249 }
250
251 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
252}
253
254int
Lev Walkinf15320b2004-06-03 03:38:44 +0000255asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
256 asn1p_expr_t *expr = arg->expr;
257 asn1p_expr_t *v;
258 int comp_mode = 0; /* {root,ext=1,root,root,...} */
259
260 DEPENDENCIES;
261
262 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000263 OUT("struct ");
264 out_name_chain(arg, 1);
265 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 } else {
267 OUT("typedef struct %s {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000268 MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 }
270
271 TQ_FOR(v, &(expr->members), next) {
272 if(v->expr_type == A1TC_EXTENSIBLE) {
273 if(comp_mode < 3) comp_mode++;
274 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000275 if(comp_mode == 1
276 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000277 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000278 EMBED(v);
279 }
280
281 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000282 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000283 expr->_anonymous_type ? "" :
284 arg->embed
285 ? MKID_safe(expr->Identifier)
286 : MKID(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000287 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000288
289 return asn1c_lang_C_type_SEQUENCE_def(arg);
290}
291
292static int
293asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
294 asn1p_expr_t *expr = arg->expr;
295 asn1p_expr_t *v;
296 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000297 int ext_start = -1;
298 int ext_stop = -1;
Lev Walkinfd171ef2004-06-06 07:20:17 +0000299 tag2el_t *tag2el = NULL;
300 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000301 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000302 int all_tags_count;
303 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000304
Lev Walkinfd171ef2004-06-06 07:20:17 +0000305 /*
306 * Fetch every inner tag from the tag to elements map.
307 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000308 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinfd171ef2004-06-06 07:20:17 +0000309 if(tag2el) free(tag2el);
310 return -1;
311 }
312
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000313 GEN_INCLUDE("constr_SEQUENCE");
314 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000315 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000316
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000317 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000318
319 /*
320 * Print out the table according to which the parsing is performed.
321 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000322 if(expr_elements_count(arg, expr)) {
323 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000324
Lev Walkin21d00002005-03-04 08:48:53 +0000325 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000326 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000327
328 elements = 0;
329 INDENTED(TQ_FOR(v, &(expr->members), next) {
330 if(v->expr_type == A1TC_EXTENSIBLE) {
331 if((++comp_mode) == 1)
332 ext_start = elements - 1;
333 else
334 ext_stop = elements - 1;
335 continue;
336 }
337 elements++;
338 emit_member_table(arg, v);
339 });
340 OUT("};\n");
341 } else {
342 elements = 0;
343 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000344
Lev Walkin27ea3802004-06-28 21:13:46 +0000345 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000346 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000347 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000348 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000349
Lev Walkinfd171ef2004-06-06 07:20:17 +0000350 /*
351 * Tags to elements map.
352 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000353 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinfd171ef2004-06-06 07:20:17 +0000354
Lev Walkin21d00002005-03-04 08:48:53 +0000355 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000356 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000357 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000358 OUT("sizeof(struct "); out_name_chain(arg, 1); OUT("),\n");
359 OUT("offsetof(struct "); out_name_chain(arg, 1); OUT(", _asn_ctx),\n");
360
Lev Walkinf3b29b12005-06-02 05:21:37 +0000361 if(tag2el_count) {
362 OUT("asn_MAP_%s_%d_tag2el,\n",
363 MKID(expr->Identifier),
364 expr->_type_unique_index);
365 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
366 } else {
367 OUT("0,\t/* No top level tags */\n");
368 OUT("0,\t/* No tags in the map */\n");
369 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 OUT("%d,\t/* Start extensions */\n",
371 ext_start);
372 OUT("%d\t/* Stop extensions */\n",
373 (ext_stop<ext_start)?elements+1:ext_stop, ext_stop);
374 );
375 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000376
377 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000378 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000379 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000380 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000381 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000382
Lev Walkinf15320b2004-06-03 03:38:44 +0000383 REDIR(OT_TYPE_DECLS);
384
385 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000386} /* _SEQUENCE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000387
388int
Lev Walkinf15320b2004-06-03 03:38:44 +0000389asn1c_lang_C_type_SET(arg_t *arg) {
390 asn1p_expr_t *expr = arg->expr;
391 asn1p_expr_t *v;
392 long mcount;
393 char *id;
394 int comp_mode = 0; /* {root,ext=1,root,root,...} */
395
396 DEPENDENCIES;
397
398 REDIR(OT_DEPS);
399
400 OUT("\n");
401 OUT("/*\n");
402 OUT(" * Method of determining the components presence\n");
403 OUT(" */\n");
404 mcount = 0;
Lev Walkin21d00002005-03-04 08:48:53 +0000405 OUT("typedef enum ");
406 out_name_chain(arg, 0);
407 OUT("_PR {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 TQ_FOR(v, &(expr->members), next) {
409 if(v->expr_type == A1TC_EXTENSIBLE) continue;
410 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000411 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000412 OUT("_PR_");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000413 id = MKID(v->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 OUT("%s,\t/* Member %s is present */\n",
415 id, id)
416 );
417 mcount++;
418 }
Lev Walkin21d00002005-03-04 08:48:53 +0000419 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000420
421 REDIR(OT_TYPE_DECLS);
422
423 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000424 OUT("struct ");
425 out_name_chain(arg, 1);
426 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000427 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000428 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 }
430
431 TQ_FOR(v, &(expr->members), next) {
432 if(v->expr_type == A1TC_EXTENSIBLE) {
433 if(comp_mode < 3) comp_mode++;
434 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000435 if(comp_mode == 1
436 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000437 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 EMBED(v);
439 }
440
441 INDENTED(
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000442 id = MKID(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000443 OUT("\n");
444 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
445 id, id);
446 OUT("unsigned int _presence_map\n");
447 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
448 );
449
450 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000451 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000452 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000453 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000454
455 return asn1c_lang_C_type_SET_def(arg);
456}
457
Lev Walkinf15320b2004-06-03 03:38:44 +0000458static int
459asn1c_lang_C_type_SET_def(arg_t *arg) {
460 asn1p_expr_t *expr = arg->expr;
461 asn1p_expr_t *v;
462 int elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000463 tag2el_t *tag2el = NULL;
464 int tag2el_count = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000465 tag2el_t *tag2el_cxer = NULL;
466 int tag2el_cxer_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000467 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000468 int all_tags_count;
469 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 char *p;
471
472 /*
473 * Fetch every inner tag from the tag to elements map.
474 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000475 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 if(tag2el) free(tag2el);
477 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000478 }
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000479 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
480 if(tag2el) free(tag2el);
481 if(tag2el_cxer) free(tag2el_cxer);
482 return -1;
483 }
484 if(tag2el_cxer_count == tag2el_count
485 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
486 free(tag2el_cxer);
487 tag2el_cxer = 0;
488 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000489
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000490 GEN_INCLUDE("constr_SET");
491 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000492 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000493
494 REDIR(OT_STAT_DEFS);
495
Lev Walkinf15320b2004-06-03 03:38:44 +0000496 /*
497 * Print out the table according to which the parsing is performed.
498 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000499 if(expr_elements_count(arg, expr)) {
500 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000501
Lev Walkin21d00002005-03-04 08:48:53 +0000502 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000503 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000504
505 elements = 0;
506 INDENTED(TQ_FOR(v, &(expr->members), next) {
507 if(v->expr_type == A1TC_EXTENSIBLE) {
508 if(comp_mode < 3) comp_mode++;
509 } else {
510 if(comp_mode == 1
511 || expr_better_indirect(arg, v))
512 v->marker.flags |= EM_INDIRECT;
513 elements++;
514 emit_member_table(arg, v);
515 }
516 });
517 OUT("};\n");
518 } else {
519 elements = 0;
520 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000521
Lev Walkin27ea3802004-06-28 21:13:46 +0000522 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000523 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000524 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000525 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000526
527 /*
528 * Tags to elements map.
529 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000530 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
531 if(tag2el_cxer)
532 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
Lev Walkinf15320b2004-06-03 03:38:44 +0000533
534 /*
535 * Emit a map of mandatory elements.
536 */
Lev Walkin21d00002005-03-04 08:48:53 +0000537 OUT("static uint8_t asn_MAP_%s_%d_mmap",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000538 MKID(expr->Identifier), expr->_type_unique_index);
539 p = MKID_safe(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000540 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
Lev Walkin21d00002005-03-04 08:48:53 +0000541 OUT(" = {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000542 INDENTED(
543 if(elements) {
544 int delimit = 0;
545 int el = 0;
546 TQ_FOR(v, &(expr->members), next) {
547 if(v->expr_type == A1TC_EXTENSIBLE) continue;
548 if(delimit) {
549 OUT(",\n");
550 delimit = 0;
551 } else if(el) {
552 OUT(" | ");
553 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000554 OUT("(%d << %d)",
Lev Walkin0204fa62004-09-15 11:54:38 +0000555 v->marker.flags?0:1,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000556 7 - (el % 8));
Lev Walkinf15320b2004-06-03 03:38:44 +0000557 if(el && (el % 8) == 0)
558 delimit = 1;
559 el++;
560 }
561 } else {
562 OUT("0");
563 }
564 );
565 OUT("\n");
566 OUT("};\n");
567
Lev Walkin21d00002005-03-04 08:48:53 +0000568 OUT("static asn_SET_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000569 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000570 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000571 OUT("sizeof(struct ");
572 out_name_chain(arg, 1);
573 OUT("),\n");
574 OUT("offsetof(struct ");
575 out_name_chain(arg, 1);
576 OUT(", _asn_ctx),\n");
577 OUT("offsetof(struct ");
578 out_name_chain(arg, 1);
579 OUT(", _presence_map),\n");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000580 p = MKID(expr->Identifier);
Lev Walkin21d00002005-03-04 08:48:53 +0000581 OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000582 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000583 if(tag2el_cxer)
Lev Walkin21d00002005-03-04 08:48:53 +0000584 OUT("asn_MAP_%s_%d_tag2el_cxer,\n",
585 p, expr->_type_unique_index);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000586 else
Lev Walkin21d00002005-03-04 08:48:53 +0000587 OUT("asn_MAP_%s_%d_tag2el,\t/* Same as above */\n",
588 p, expr->_type_unique_index);
589 OUT("%d,\t/* Count of tags in the CXER map */\n",
590 tag2el_cxer_count);
Lev Walkin35631482004-07-01 00:52:50 +0000591 OUT("%d,\t/* Whether extensible */\n",
592 check_if_extensible(expr));
Lev Walkin21d00002005-03-04 08:48:53 +0000593 OUT("(unsigned int *)asn_MAP_%s_%d_mmap\t/* Mandatory elements map */\n",
594 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000595 );
596 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000597
598 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000599 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000600 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000601 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000602 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000603
Lev Walkinf15320b2004-06-03 03:38:44 +0000604 REDIR(OT_TYPE_DECLS);
605
606 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000607} /* _SET_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000608
609int
Lev Walkin08079b02004-08-22 03:25:24 +0000610asn1c_lang_C_type_SEx_OF(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000611 asn1p_expr_t *expr = arg->expr;
Lev Walkinc8285712005-03-04 22:18:20 +0000612 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
Lev Walkinf15320b2004-06-03 03:38:44 +0000613
614 DEPENDENCIES;
615
616 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000617 OUT("struct ");
618 out_name_chain(arg, 1);
619 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000620 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000621 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000622 }
623
Lev Walkin08079b02004-08-22 03:25:24 +0000624 INDENT(+1);
625 OUT("A_%s_OF(",
626 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
627 ? "SET" : "SEQUENCE");
Lev Walkine0b56e02005-02-25 12:10:27 +0000628 if(memb->expr_type & ASN_CONSTR_MASK
629 || ((memb->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000630 || (0 /* -- prohibited by X.693:8.3.4 */
631 && memb->expr_type == ASN_BASIC_INTEGER))
632 && expr_elements_count(arg, memb))) {
Lev Walkin08079b02004-08-22 03:25:24 +0000633 arg_t tmp;
634 asn1p_expr_t tmp_memb;
635 arg->embed++;
636 tmp = *arg;
637 tmp.expr = &tmp_memb;
638 tmp_memb = *memb;
639 tmp_memb._anonymous_type = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000640 if(tmp_memb.Identifier == 0) {
Lev Walkin21d00002005-03-04 08:48:53 +0000641 tmp_memb.Identifier = "Member";
642 if(0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000643 tmp_memb.Identifier = strdup(
644 asn1c_make_identifier(0,
Lev Walkin152a91e2005-02-14 20:41:29 +0000645 expr->Identifier, "Member", 0));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000646 assert(tmp_memb.Identifier);
647 }
Lev Walkin08079b02004-08-22 03:25:24 +0000648 tmp.default_cb(&tmp);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000649 if(tmp_memb.Identifier != memb->Identifier)
Lev Walkin21d00002005-03-04 08:48:53 +0000650 if(0) free(tmp_memb.Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000651 arg->embed--;
652 assert(arg->target->target == OT_TYPE_DECLS);
653 } else {
Lev Walkinfe7f6ec2005-03-04 22:38:22 +0000654 OUT("%s", asn1c_type_name(arg, memb,
655 (memb->marker.flags & EM_UNRECURSE)
656 ? TNF_RSAFE : TNF_CTYPE));
Lev Walkinf15320b2004-06-03 03:38:44 +0000657 }
Lev Walkin08079b02004-08-22 03:25:24 +0000658 OUT(") list;\n");
659 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000660
661 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000662 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000663 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000664 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000665
666 /*
Lev Walkin08079b02004-08-22 03:25:24 +0000667 * SET OF/SEQUENCE OF definition
Lev Walkinf15320b2004-06-03 03:38:44 +0000668 */
Lev Walkin08079b02004-08-22 03:25:24 +0000669 return asn1c_lang_C_type_SEx_OF_def(arg,
670 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
Lev Walkinf15320b2004-06-03 03:38:44 +0000671}
672
673static int
674asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
675 asn1p_expr_t *expr = arg->expr;
676 asn1p_expr_t *v;
Lev Walkin64399722004-08-11 07:17:22 +0000677 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000678 int all_tags_count;
679 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000680
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000681 /*
682 * Print out the table according to which the parsing is performed.
683 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000684 if(seq_of) {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000685 GEN_INCLUDE("constr_SEQUENCE_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000686 } else {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000687 GEN_INCLUDE("constr_SET_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000688 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000689 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000690 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000691
692 REDIR(OT_STAT_DEFS);
693
694 /*
695 * Print out the table according to which the parsing is performed.
696 */
Lev Walkin21d00002005-03-04 08:48:53 +0000697 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
698 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000699 INDENT(+1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000700 v = TQ_FIRST(&(expr->members));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000701 if(!v->Identifier) {
Lev Walkin152a91e2005-02-14 20:41:29 +0000702 v->Identifier = strdup("Member");
Lev Walkina9cc46e2004-09-22 16:06:28 +0000703 assert(v->Identifier);
704 }
705 v->_anonymous_type = 1;
706 arg->embed++;
Lev Walkin59964be2004-08-25 02:03:12 +0000707 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000708 arg->embed--;
709 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000710 OUT("};\n");
711
Lev Walkin27ea3802004-06-28 21:13:46 +0000712 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000713 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000714 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000715 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000716
Lev Walkin21d00002005-03-04 08:48:53 +0000717 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000718 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000719 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000720 OUT("sizeof(struct ");
721 out_name_chain(arg, 1);
722 OUT("),\n");
723 OUT("offsetof(struct ");
724 out_name_chain(arg, 1);
725 OUT(", _asn_ctx),\n");
726
Lev Walkina9cc46e2004-09-22 16:06:28 +0000727 if(expr_as_xmlvaluelist(arg, v))
728 OUT("1,\t/* XER encoding is XMLValueList */\n");
729 else
730 OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000731 );
732 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000733
734 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000735 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000736 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000737 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000738 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000739
Lev Walkinf15320b2004-06-03 03:38:44 +0000740 REDIR(OT_TYPE_DECLS);
741
742 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000743} /* _SEx_OF_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000744
745int
746asn1c_lang_C_type_CHOICE(arg_t *arg) {
747 asn1p_expr_t *expr = arg->expr;
748 asn1p_expr_t *v;
Lev Walkin08079b02004-08-22 03:25:24 +0000749 char *id;
Lev Walkinf15320b2004-06-03 03:38:44 +0000750
751 DEPENDENCIES;
752
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000753 REDIR(OT_DEPS);
754
Lev Walkin21d00002005-03-04 08:48:53 +0000755 OUT("typedef enum ");
756 out_name_chain(arg, 0);
757 OUT("_PR {\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000758 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000759 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000760 OUT("_PR_NOTHING,\t/* No components present */\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000761 TQ_FOR(v, &(expr->members), next) {
762 if(v->expr_type == A1TC_EXTENSIBLE) {
763 OUT("/* Extensions may appear below */\n");
764 continue;
765 }
Lev Walkin21d00002005-03-04 08:48:53 +0000766 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000767 OUT("_PR_");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000768 id = MKID(v->Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000769 OUT("%s,\n", id, id);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000770 }
771 );
Lev Walkin21d00002005-03-04 08:48:53 +0000772 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000773
774 REDIR(OT_TYPE_DECLS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000775
776 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000777 OUT("struct "); out_name_chain(arg, 1); OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000778 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000779 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000780 }
781
782 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000783 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000784 OUT("_PR present;\n");
785 OUT("union {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000786 TQ_FOR(v, &(expr->members), next) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000787 if(expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000788 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000789 EMBED(v);
790 }
791 if(UNNAMED_UNIONS) OUT("};\n");
792 else OUT("} choice;\n");
793 );
794
795 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000796 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000797 expr->_anonymous_type ? "" :
798 arg->embed
799 ? MKID_safe(expr->Identifier)
800 : MKID(expr->Identifier),
Lev Walkin08079b02004-08-22 03:25:24 +0000801 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000802
803 return asn1c_lang_C_type_CHOICE_def(arg);
804}
805
806static int
807asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
808 asn1p_expr_t *expr = arg->expr;
809 asn1p_expr_t *v;
810 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000811 tag2el_t *tag2el = NULL;
812 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000813 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000814 int all_tags_count;
815 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000816
817 /*
818 * Fetch every inner tag from the tag to elements map.
819 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000820 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000821 if(tag2el) free(tag2el);
822 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000823 }
824
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000825 GEN_INCLUDE("constr_CHOICE");
826 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000827 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000828
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000829 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000830
831 /*
832 * Print out the table according to which the parsing is performed.
833 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000834 if(expr_elements_count(arg, expr)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000835
Lev Walkin21d00002005-03-04 08:48:53 +0000836 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
837 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000838
839 elements = 0;
840 INDENTED(TQ_FOR(v, &(expr->members), next) {
Lev Walkin659c63b2005-01-17 11:40:49 +0000841 if(v->expr_type == A1TC_EXTENSIBLE)
842 continue;
843 if(expr_better_indirect(arg, v))
844 v->marker.flags |= EM_INDIRECT;
845 elements++;
846 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000847 });
848 OUT("};\n");
849 } else {
850 elements = 0;
851 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000852
Lev Walkin64399722004-08-11 07:17:22 +0000853
Lev Walkinf15320b2004-06-03 03:38:44 +0000854 if(arg->embed) {
855 /*
856 * Our parent structure has already taken this into account.
857 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000858 tv_mode = _TVM_SAME;
859 tags_count = all_tags_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000860 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000861 tv_mode = emit_tags_vectors(arg, expr,
862 &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000863 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000864
865 /*
866 * Tags to elements map.
867 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000868 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000869
Lev Walkin21d00002005-03-04 08:48:53 +0000870 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000871 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000872 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000873 OUT("sizeof(struct ");
874 out_name_chain(arg, 1);
875 OUT("),\n");
876 OUT("offsetof(struct ");
877 out_name_chain(arg, 1);
878 OUT(", _asn_ctx),\n");
879 OUT("offsetof(struct ");
880 out_name_chain(arg, 1);
881 OUT(", present),\n");
882 OUT("sizeof(((struct ");
883 out_name_chain(arg, 1);
884 OUT(" *)0)->present),\n");
885 OUT("asn_MAP_%s_%d_tag2el,\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000886 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000887 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin35631482004-07-01 00:52:50 +0000888 OUT("%d\t/* Whether extensible */\n",
889 check_if_extensible(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000890 );
891 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000892
893 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000894 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000895 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000896 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000897 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000898
Lev Walkinf15320b2004-06-03 03:38:44 +0000899 REDIR(OT_TYPE_DECLS);
900
901 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000902} /* _CHOICE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000903
904int
905asn1c_lang_C_type_REFERENCE(arg_t *arg) {
906 asn1p_ref_t *ref;
907
908 ref = arg->expr->reference;
909 if(ref->components[ref->comp_count-1].name[0] == '&') {
Lev Walkinf15320b2004-06-03 03:38:44 +0000910 asn1p_expr_t *extract;
911 arg_t tmp;
912 int ret;
913
Lev Walkina9cc46e2004-09-22 16:06:28 +0000914 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
Lev Walkin08079b02004-08-22 03:25:24 +0000915 arg->expr, ref);
Lev Walkinf15320b2004-06-03 03:38:44 +0000916 if(extract == NULL)
917 return -1;
918
Lev Walkin08079b02004-08-22 03:25:24 +0000919 extract = asn1p_expr_clone(extract, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000920 if(extract) {
921 if(extract->Identifier)
922 free(extract->Identifier);
923 extract->Identifier = strdup(arg->expr->Identifier);
924 if(extract->Identifier == NULL) {
925 asn1p_expr_free(extract);
926 return -1;
927 }
928 } else {
929 return -1;
930 }
931
932 tmp = *arg;
933 tmp.asn = arg->asn;
Lev Walkin08079b02004-08-22 03:25:24 +0000934 tmp.mod = extract->module;
Lev Walkinf15320b2004-06-03 03:38:44 +0000935 tmp.expr = extract;
936
937 ret = arg->default_cb(&tmp);
938
939 asn1p_expr_free(extract);
940
941 return ret;
942 }
943
944
945 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
946}
947
948int
949asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
950 asn1p_expr_t *expr = arg->expr;
Lev Walkin64399722004-08-11 07:17:22 +0000951 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000952 int all_tags_count;
953 enum tvm_compat tv_mode;
Lev Walkine0b56e02005-02-25 12:10:27 +0000954 enum etd_spec etd_spec;
Lev Walkinf15320b2004-06-03 03:38:44 +0000955 char *p;
956
957 if(arg->embed) {
Lev Walkin05363a72004-09-29 13:16:40 +0000958 enum tnfmt tnfmt = TNF_CTYPE;
959
960 /*
961 * If this is an optional compound type,
962 * refer it using "struct X" convention,
963 * as it may recursively include the current structure.
964 */
Lev Walkinc8285712005-03-04 22:18:20 +0000965 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
Lev Walkin05363a72004-09-29 13:16:40 +0000966 asn1p_expr_t *terminal;
967 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
968 if(terminal
969 && (terminal->expr_type & ASN_CONSTR_MASK)) {
Lev Walkinc8285712005-03-04 22:18:20 +0000970 tnfmt = TNF_RSAFE;
971 REDIR(OT_FWD_DECLS);
Lev Walkinbf56d542005-03-04 23:50:56 +0000972 OUT("%s;\n",
Lev Walkinc8285712005-03-04 22:18:20 +0000973 asn1c_type_name(arg, arg->expr, tnfmt));
Lev Walkin05363a72004-09-29 13:16:40 +0000974 }
975 }
976
Lev Walkinf15320b2004-06-03 03:38:44 +0000977 REDIR(OT_TYPE_DECLS);
978
Lev Walkinc8285712005-03-04 22:18:20 +0000979 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
Lev Walkine0b56e02005-02-25 12:10:27 +0000980 if(!expr->_anonymous_type) {
Lev Walkinc8285712005-03-04 22:18:20 +0000981 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
Lev Walkincaf0d5a2005-03-04 23:48:19 +0000982 OUT("%s", MKID_safe(expr->Identifier));
Lev Walkine0b56e02005-02-25 12:10:27 +0000983 if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT)
984 OUT("\t/* DEFAULT %s */",
985 asn1f_printable_value(
986 expr->marker.default_value));
987 else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL)
988 OUT("\t/* OPTIONAL */");
989 }
990
991 } else {
992 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
Lev Walkin59004fa2004-08-20 13:37:01 +0000993
994 REDIR(OT_TYPE_DECLS);
Lev Walkine0b56e02005-02-25 12:10:27 +0000995
996 OUT("typedef %s\t",
Lev Walkinc8285712005-03-04 22:18:20 +0000997 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
Lev Walkine0b56e02005-02-25 12:10:27 +0000998 OUT("%s%s_t",
Lev Walkinc8285712005-03-04 22:18:20 +0000999 (expr->marker.flags & EM_INDIRECT)?"*":" ",
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001000 MKID(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +00001001 }
1002
Lev Walkine0b56e02005-02-25 12:10:27 +00001003 if((expr->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin8ecf9db2005-03-03 21:28:12 +00001004 || (0 /* -- prohibited by X.693:8.3.4 */
1005 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +00001006 && expr_elements_count(arg, expr)))
1007 etd_spec = ETD_HAS_SPECIFICS;
1008 else
1009 etd_spec = ETD_NO_SPECIFICS;
Lev Walkinf15320b2004-06-03 03:38:44 +00001010
Lev Walkin8de2ab22004-09-26 13:11:31 +00001011 /*
1012 * If this type just blindly refers the other type, alias it.
1013 * Type1 ::= Type2
1014 */
Lev Walkine0b56e02005-02-25 12:10:27 +00001015 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
1016 REDIR(OT_TYPE_DECLS);
1017 return 0;
1018 }
Lev Walkin8de2ab22004-09-26 13:11:31 +00001019 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkine0b56e02005-02-25 12:10:27 +00001020 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
Lev Walkin6938d042005-03-04 23:23:50 +00001021 && etd_spec == ETD_NO_SPECIFICS
1022 && 0 /* This shortcut is incompatible with XER */
1023 ) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001024 char *type_name;
1025 REDIR(OT_FUNC_DECLS);
1026 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
1027 OUT("/* This type is equivalent to %s */\n", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001028 if(HIDE_INNER_DEFS) OUT("/* ");
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001029 OUT("#define\tasn_DEF_%s\t", MKID(expr->Identifier));
Lev Walkin8de2ab22004-09-26 13:11:31 +00001030 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkine0b56e02005-02-25 12:10:27 +00001031 OUT("asn_DEF_%s", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001032 if(HIDE_INNER_DEFS)
Lev Walkine0b56e02005-02-25 12:10:27 +00001033 OUT("\t// (Use -fall-defs-global to expose) */");
1034 OUT("\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001035 REDIR(OT_CODE);
1036 OUT("/* This type is equivalent to %s */\n", type_name);
1037 OUT("\n");
1038 REDIR(OT_TYPE_DECLS);
1039 return 0;
1040 }
1041
Lev Walkinf15320b2004-06-03 03:38:44 +00001042 REDIR(OT_STAT_DEFS);
1043
Lev Walkin27ea3802004-06-28 21:13:46 +00001044 /*
Lev Walkin05363a72004-09-29 13:16:40 +00001045 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +00001046 */
Lev Walkin188ed2c2004-09-13 08:31:01 +00001047 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +00001048
Lev Walkine0b56e02005-02-25 12:10:27 +00001049 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1050 0, etd_spec);
Lev Walkinf15320b2004-06-03 03:38:44 +00001051
Lev Walkin59004fa2004-08-20 13:37:01 +00001052 REDIR(OT_CODE);
1053
Lev Walkinf15320b2004-06-03 03:38:44 +00001054 /*
1055 * Constraint checking.
1056 */
Lev Walkin8de2ab22004-09-26 13:11:31 +00001057 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1058 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001059 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001060 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001061 OUT("%s", p);
1062 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1063 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001064 INDENT(+1);
1065 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1066 OUT("\n");
1067 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1068 OUT("/* Replace with underlying type checker */\n");
1069 OUT("td->check_constraints "
Lev Walkin05363a72004-09-29 13:16:40 +00001070 "= asn_DEF_%s.check_constraints;\n",
Lev Walkin8de2ab22004-09-26 13:11:31 +00001071 asn1c_type_name(arg, expr, TNF_SAFE));
1072 OUT("return td->check_constraints"
1073 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +00001074 }
Lev Walkin8de2ab22004-09-26 13:11:31 +00001075 INDENT(-1);
1076 OUT("}\n");
1077 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001078 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001079
1080 /*
1081 * Emit suicidal functions.
1082 */
1083
Lev Walkinf15320b2004-06-03 03:38:44 +00001084 /*
1085 * This function replaces certain fields from the definition
1086 * of a type with the corresponding fields from the basic type
1087 * (from which the current type is inherited).
1088 */
Lev Walkinf15320b2004-06-03 03:38:44 +00001089 OUT("/*\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001090 OUT(" * This type is implemented using %s,\n",
1091 asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkinb65b3002004-09-23 22:20:47 +00001092 OUT(" * so here we adjust the DEF accordingly.\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001093 OUT(" */\n");
1094 OUT("static void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001095 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001096 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001097 INDENT(+1);
Lev Walkine0b56e02005-02-25 12:10:27 +00001098 {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001099 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001100 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkin05363a72004-09-29 13:16:40 +00001101 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1102 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1103 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1104 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1105 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1106 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001107 if(!terminal && !tags_count) {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001108 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
Lev Walkin05363a72004-09-29 13:16:40 +00001109 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1110 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1111 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1112 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001113 OUT("/* End of these lines */\n");
1114 }
Lev Walkin05363a72004-09-29 13:16:40 +00001115 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1116 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
Lev Walkine0b56e02005-02-25 12:10:27 +00001117 if(etd_spec != ETD_NO_SPECIFICS) {
1118 INDENT(-1);
Lev Walkin7ef83a42005-03-29 19:04:24 +00001119 OUT(" /* ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001120 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001121 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1122 if(etd_spec == ETD_NO_SPECIFICS) {
1123 INDENT(-1);
1124 OUT("\n");
1125 } else {
1126 OUT("\t// Defined explicitly */\n");
1127 }
1128 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001129 OUT("}\n");
1130 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001131
1132 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001133 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001134 OUT("void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001135 OUT("%s", p);
1136 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1137 OUT("_free(asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001138 INDENTED(
Lev Walkina9cc46e2004-09-22 16:06:28 +00001139 OUT("\tvoid *struct_ptr, int contents_only) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001140 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1141 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001142 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001143 );
1144 OUT("}\n");
1145 OUT("\n");
1146
1147 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001148 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinf15320b2004-06-03 03:38:44 +00001149 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001150 OUT("%s", p);
1151 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1152 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001153 INDENTED(
1154 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001155 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1156 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001157 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1158 );
1159 OUT("}\n");
1160 OUT("\n");
1161
1162 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001163 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkindc06f6b2004-10-20 15:50:55 +00001164 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001165 OUT("%s", p);
1166 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1167 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001168 INDENTED(
Lev Walkindd1512e2005-03-17 21:56:00 +00001169 OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001170 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1171 p, expr->_type_unique_index);
Lev Walkin05363a72004-09-29 13:16:40 +00001172 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001173 );
1174 OUT("}\n");
1175 OUT("\n");
1176
1177 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001178 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001179 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001180 OUT("%s", p);
1181 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1182 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001183 INDENTED(
1184 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1185 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001186 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1187 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001188 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1189 );
1190 OUT("}\n");
1191 OUT("\n");
1192
1193 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001194 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinca47c292004-10-23 13:34:00 +00001195 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001196 OUT("%s", p);
1197 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1198 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinca47c292004-10-23 13:34:00 +00001199 INDENTED(
Lev Walkindd1512e2005-03-17 21:56:00 +00001200 OUT("\tvoid **structure, const char *opt_mname, const void *bufptr, size_t size) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001201 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1202 p, expr->_type_unique_index);
Lev Walkinca47c292004-10-23 13:34:00 +00001203 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1204 );
1205 OUT("}\n");
1206 OUT("\n");
1207
1208 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001209 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001210 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001211 OUT("%s", p);
1212 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1213 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001214 INDENTED(
1215 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1216 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001217 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1218 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001219 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001220 );
1221 OUT("}\n");
1222 OUT("\n");
1223
1224 REDIR(OT_FUNC_DECLS);
1225
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001226 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001227 if(HIDE_INNER_DEFS) {
Lev Walkin21d00002005-03-04 08:48:53 +00001228 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1229 "\t// (Use -fall-defs-global to expose) */\n",
1230 p, expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +00001231 } else {
1232 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1233 OUT("asn_struct_free_f %s_free;\n", p);
1234 OUT("asn_struct_print_f %s_print;\n", p);
1235 OUT("asn_constr_check_f %s_constraint;\n", p);
1236 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1237 OUT("der_type_encoder_f %s_encode_der;\n", p);
1238 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1239 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1240 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001241
Lev Walkin3dcaafa2004-08-11 05:21:32 +00001242 REDIR(OT_TYPE_DECLS);
1243
Lev Walkinf15320b2004-06-03 03:38:44 +00001244 return 0;
1245}
1246
1247int
1248asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1249
1250 OUT("/*\n");
1251 OUT(" * This type is extensible,\n");
1252 OUT(" * possible extensions are below.\n");
1253 OUT(" */\n");
1254
1255 return 0;
1256}
1257
Lev Walkin35631482004-07-01 00:52:50 +00001258static int check_if_extensible(asn1p_expr_t *expr) {
1259 asn1p_expr_t *v;
1260 TQ_FOR(v, &(expr->members), next) {
1261 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1262 }
1263 return 0;
1264}
1265
Lev Walkinf15320b2004-06-03 03:38:44 +00001266static int
Lev Walkinc3e29402004-09-10 06:07:18 +00001267_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001268
1269 OUT("(");
Lev Walkinc3e29402004-09-10 06:07:18 +00001270 switch(tag->tag_class) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001271 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1272 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1273 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1274 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1275 case TC_NOCLASS:
1276 break;
1277 }
Lev Walkin33c16ba2004-09-24 21:01:43 +00001278 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
Lev Walkinf15320b2004-06-03 03:38:44 +00001279
1280 return 0;
1281}
1282
Lev Walkinfd171ef2004-06-06 07:20:17 +00001283
1284static int
1285_tag2el_cmp(const void *ap, const void *bp) {
1286 const tag2el_t *a = ap;
1287 const tag2el_t *b = bp;
1288 const struct asn1p_type_tag_s *ta = &a->el_tag;
1289 const struct asn1p_type_tag_s *tb = &b->el_tag;
1290
1291 if(ta->tag_class == tb->tag_class) {
1292 if(ta->tag_value == tb->tag_value) {
1293 /*
1294 * Sort by their respective positions.
1295 */
1296 if(a->el_no < b->el_no)
1297 return -1;
1298 else if(a->el_no > b->el_no)
1299 return 1;
1300 return 0;
1301 } else if(ta->tag_value < tb->tag_value)
1302 return -1;
1303 else
1304 return 1;
1305 } else if(ta->tag_class < tb->tag_class) {
1306 return -1;
1307 } else {
1308 return 1;
1309 }
1310}
1311
Lev Walkinf15320b2004-06-03 03:38:44 +00001312/*
1313 * For constructed types, number of external tags may be greater than
1314 * number of elements in the type because of CHOICE type.
1315 * T ::= SET { -- Three possible tags:
1316 * a INTEGER, -- One tag is here...
1317 * b Choice1 -- ... and two more tags are there.
1318 * }
1319 * Choice1 ::= CHOICE {
1320 * s1 IA5String,
1321 * s2 ObjectDescriptor
1322 * }
1323 */
1324static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001325_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001326 asn1p_expr_t *expr = arg->expr;
1327 arg_t tmparg = *arg;
1328 asn1p_expr_t *v;
1329 int element = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001330 int original_count = *count;
1331 int sort_until = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001332
1333 TQ_FOR(v, &(expr->members), next) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001334 if(v->expr_type == A1TC_EXTENSIBLE) {
1335 /*
Lev Walkine0b56e02005-02-25 12:10:27 +00001336 * CXER mandates sorting
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001337 * only for the root part.
1338 */
1339 if(flags == FTE_CANONICAL_XER
1340 && sort_until == -1)
1341 sort_until = *count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001342 continue;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001343 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001344
1345 tmparg.expr = v;
1346
1347 if(_add_tag2el_member(&tmparg, tag2el, count,
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001348 (el_no==-1)?element:el_no, flags)) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001349 return -1;
1350 }
1351
1352 element++;
1353 }
1354
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001355
1356 if(flags == FTE_CANONICAL_XER) {
1357 if(sort_until == -1) sort_until = *count;
1358 qsort((*tag2el) + original_count,
1359 sort_until - original_count,
1360 sizeof(**tag2el), _tag2el_cmp);
1361 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1362 && (sort_until - original_count) >= 1) {
1363 /* Only take in account the root component */
1364 *count = original_count + 1;
1365 }
1366 } else {
1367 /*
1368 * Sort the map according to canonical order of their
1369 * tags and element numbers.
1370 */
1371 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1372 }
Lev Walkinfd171ef2004-06-06 07:20:17 +00001373
Lev Walkin38abe792004-06-14 13:09:45 +00001374 /*
1375 * Initialize .toff_{first|last} members.
1376 */
1377 if(*count) {
1378 struct asn1p_type_tag_s *cur_tag = 0;
1379 tag2el_t *cur = *tag2el;
1380 tag2el_t *end = cur + *count;
1381 int occur, i;
1382 for(occur = 0; cur < end; cur++) {
1383 if(cur_tag == 0
1384 || cur_tag->tag_value != cur->el_tag.tag_value
1385 || cur_tag->tag_class != cur->el_tag.tag_class) {
1386 cur_tag = &cur->el_tag;
1387 occur = 0;
1388 } else {
1389 occur++;
1390 }
1391 cur->toff_first = -occur;
1392 for(i = 0; i >= -occur; i--)
1393 cur[i].toff_last = -i;
1394 }
1395 }
1396
Lev Walkinf15320b2004-06-03 03:38:44 +00001397 return 0;
1398}
1399
1400static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001401_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001402 struct asn1p_type_tag_s tag;
1403 int ret;
1404
1405 assert(el_no >= 0);
1406
Lev Walkina9cc46e2004-09-22 16:06:28 +00001407 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1408 arg->expr, &tag, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001409 if(ret == 0) {
Lev Walkin201943e2004-09-04 04:42:29 +00001410 tag2el_t *te;
1411 int new_count = (*count) + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001412 void *p;
Lev Walkin201943e2004-09-04 04:42:29 +00001413
Lev Walkinac7e2292004-09-05 10:42:33 +00001414 if(tag.tag_value == -1) {
1415 /*
1416 * This is an untagged ANY type,
1417 * proceed without adding a tag
1418 */
1419 return 0;
1420 }
1421
Lev Walkin201943e2004-09-04 04:42:29 +00001422 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
Lev Walkinf15320b2004-06-03 03:38:44 +00001423 if(p) *tag2el = p;
1424 else return -1;
1425
1426 DEBUG("Found tag for %s: %ld",
1427 arg->expr->Identifier,
1428 (long)tag.tag_value);
1429
Lev Walkin201943e2004-09-04 04:42:29 +00001430 te = &((*tag2el)[*count]);
1431 te->el_tag = tag;
1432 te->el_no = el_no;
1433 te->from_expr = arg->expr;
1434 *count = new_count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001435 return 0;
1436 }
1437
1438 DEBUG("Searching tag in complex expression %s:%x at line %d",
1439 arg->expr->Identifier,
1440 arg->expr->expr_type,
1441 arg->expr->_lineno);
1442
1443 /*
1444 * Iterate over members of CHOICE type.
1445 */
1446 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001447 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001448 }
1449
1450 if(arg->expr->expr_type == A1TC_REFERENCE) {
1451 arg_t tmp = *arg;
1452 asn1p_expr_t *expr;
Lev Walkin08079b02004-08-22 03:25:24 +00001453 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001454 arg->expr->reference);
1455 if(expr) {
Lev Walkin08079b02004-08-22 03:25:24 +00001456 tmp.mod = expr->module;
Lev Walkinf15320b2004-06-03 03:38:44 +00001457 tmp.expr = expr;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001458 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001459 } else {
1460 FATAL("Cannot dereference %s at line %d",
1461 arg->expr->Identifier,
1462 arg->expr->_lineno);
1463 return -1;
1464 }
1465 }
1466
1467 DEBUG("No tag for %s at line %d",
1468 arg->expr->Identifier,
1469 arg->expr->_lineno);
1470
1471 return -1;
1472}
1473
1474static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001475emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
Lev Walkinfd171ef2004-06-06 07:20:17 +00001476 asn1p_expr_t *expr = arg->expr;
Lev Walkinf3b29b12005-06-02 05:21:37 +00001477 int i;
1478
1479 if(!tag2el_count) return 0; /* No top level tags */
Lev Walkinfd171ef2004-06-06 07:20:17 +00001480
Lev Walkin21d00002005-03-04 08:48:53 +00001481 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001482 MKID(expr->Identifier), expr->_type_unique_index,
Lev Walkin21d00002005-03-04 08:48:53 +00001483 opt_modifier?opt_modifier:"");
Lev Walkinf3b29b12005-06-02 05:21:37 +00001484 for(i = 0; i < tag2el_count; i++) {
1485 OUT(" { ");
1486 _print_tag(arg, &tag2el[i].el_tag);
1487 OUT(", ");
1488 OUT("%d, ", tag2el[i].el_no);
1489 OUT("%d, ", tag2el[i].toff_first);
1490 OUT("%d ", tag2el[i].toff_last);
1491 OUT("}%s /* %s at %d */\n",
1492 (i + 1 < tag2el_count) ? "," : "",
1493 tag2el[i].from_expr->Identifier,
1494 tag2el[i].from_expr->_lineno
1495 );
Lev Walkinfd171ef2004-06-06 07:20:17 +00001496 }
1497 OUT("};\n");
1498
Lev Walkinf3b29b12005-06-02 05:21:37 +00001499 return 0;
Lev Walkinfd171ef2004-06-06 07:20:17 +00001500}
1501
Lev Walkin188ed2c2004-09-13 08:31:01 +00001502static enum tvm_compat
1503emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1504 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1505 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
Lev Walkin64399722004-08-11 07:17:22 +00001506 int tags_count = 0;
Lev Walkin188ed2c2004-09-13 08:31:01 +00001507 int all_tags_count = 0;
1508 enum tvm_compat tv_mode = _TVM_SAME;
Lev Walkin906654e2004-09-10 15:49:15 +00001509 int i;
Lev Walkin27ea3802004-06-28 21:13:46 +00001510
Lev Walkin6d1220a2004-09-14 14:10:10 +00001511 /* Cleanup before proceeding. */
1512 *tags_count_r = 0;
1513 *all_tags_count_r = 0;
1514
Lev Walkin906654e2004-09-10 15:49:15 +00001515 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001516 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
Lev Walkin6d1220a2004-09-14 14:10:10 +00001517 if(tags_count < 0)
1518 return -1;
Lev Walkin64399722004-08-11 07:17:22 +00001519
Lev Walkin188ed2c2004-09-13 08:31:01 +00001520 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001521 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
Lev Walkin188ed2c2004-09-13 08:31:01 +00001522 &all_tags, AFT_FULL_COLLECT);
1523 if(all_tags_count < 0) {
1524 if(tags) free(tags);
1525 return -1;
Lev Walkin5a8219a2004-09-08 00:28:57 +00001526 }
Lev Walkin906654e2004-09-10 15:49:15 +00001527
Lev Walkin188ed2c2004-09-13 08:31:01 +00001528 assert(tags_count <= all_tags_count);
1529 assert((tags_count?0:1) == (all_tags_count?0:1));
Lev Walkin27ea3802004-06-28 21:13:46 +00001530
Lev Walkin188ed2c2004-09-13 08:31:01 +00001531 if(tags_count <= all_tags_count) {
1532 for(i = 0; i < tags_count; i++) {
1533 if(tags[i].tag_value != all_tags[i].tag_value
1534 || tags[i].tag_class != all_tags[i].tag_class) {
1535 tv_mode = _TVM_DIFFERENT;
1536 break;
1537 }
1538 }
1539 if(i == tags_count && tags_count < all_tags_count)
1540 tv_mode = _TVM_SUBSET;
1541 } else {
1542 tv_mode = _TVM_DIFFERENT;
1543 }
1544
1545#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
Lev Walkin21d00002005-03-04 08:48:53 +00001546 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001547 MKID(expr->Identifier), \
Lev Walkin21d00002005-03-04 08:48:53 +00001548 expr->_type_unique_index, name); \
Lev Walkin188ed2c2004-09-13 08:31:01 +00001549 INDENT(+1); \
1550 /* Print the array of collected tags */ \
1551 for(i = 0; i < tags_count; i++) { \
1552 if(i) OUT(",\n"); \
1553 _print_tag(arg, &tags[i]); \
1554 } \
1555 OUT("\n"); \
1556 INDENT(-1); \
1557 OUT("};\n"); \
1558 } while(0)
1559
1560 if(tags_count) {
1561 if(tv_mode == _TVM_SUBSET)
1562 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1563 else
1564 EMIT_TAGS_TABLE("", tags, tags_count);
1565 }
1566
1567 if(all_tags_count) {
1568 if(tv_mode == _TVM_DIFFERENT)
1569 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1570 }
1571
1572 if(tags) free(tags);
1573 if(all_tags) free(all_tags);
1574
1575 *tags_count_r = tags_count;
1576 *all_tags_count_r = all_tags_count;
1577
1578 return tv_mode;
Lev Walkin27ea3802004-06-28 21:13:46 +00001579}
Lev Walkin59004fa2004-08-20 13:37:01 +00001580
1581static int
Lev Walkin59964be2004-08-25 02:03:12 +00001582expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001583 asn1p_expr_t *topmost_parent;
1584 asn1p_expr_t *v;
1585 int elements = 0;
1586
Lev Walkina9cc46e2004-09-22 16:06:28 +00001587 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001588 if(!topmost_parent) return 0;
1589
Lev Walkine0b56e02005-02-25 12:10:27 +00001590 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1591 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1592 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin59004fa2004-08-20 13:37:01 +00001593 return 0;
1594
1595 TQ_FOR(v, &(topmost_parent->members), next) {
1596 if(v->expr_type != A1TC_EXTENSIBLE)
1597 elements++;
1598 }
1599
1600 return elements;
1601}
1602
1603static int
Lev Walkinc8285712005-03-04 22:18:20 +00001604emit_include_dependencies(arg_t *arg) {
1605 asn1p_expr_t *expr = arg->expr;
1606 asn1p_expr_t *memb;
1607
1608 TQ_FOR(memb, &(expr->members), next) {
1609
1610 if((memb->meta_type == AMT_TYPEREF
1611 && (memb->marker.flags & EM_INDIRECT))
1612 || expr->expr_type == ASN_CONSTR_SET_OF
1613 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1614 ) {
1615 asn1p_expr_t *terminal;
1616 terminal = asn1f_find_terminal_type_ex(arg->asn, memb);
1617 if(terminal && !terminal->parent_expr
1618 && (terminal->expr_type & ASN_CONSTR_MASK)) {
1619 int saved_target = arg->target->target;
1620 REDIR(OT_FWD_DECLS);
Lev Walkinbf56d542005-03-04 23:50:56 +00001621 OUT("%s;\n",
Lev Walkinc8285712005-03-04 22:18:20 +00001622 asn1c_type_name(arg, memb, TNF_RSAFE));
1623 REDIR(saved_target);
1624 memb->marker.flags |= EM_UNRECURSE;
1625 }
1626 }
1627
1628 if((!(memb->expr_type & ASN_CONSTR_MASK)
1629 && memb->expr_type > ASN_CONSTR_MASK)
1630 || memb->meta_type == AMT_TYPEREF) {
1631 if(memb->marker.flags & EM_UNRECURSE) {
1632 GEN_POSTINCLUDE(asn1c_type_name(arg,
1633 memb, TNF_INCLUDE));
1634 } else {
1635 GEN_INCLUDE(asn1c_type_name(arg,
1636 memb, TNF_INCLUDE));
1637 }
1638 }
1639 }
1640
1641 return 0;
1642}
1643
1644static int
Lev Walkin59964be2004-08-25 02:03:12 +00001645emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001646 int save_target;
1647 arg_t tmp_arg;
Lev Walkinb9189732004-09-10 09:37:12 +00001648 struct asn1p_type_tag_s outmost_tag_s;
1649 struct asn1p_type_tag_s *outmost_tag;
Lev Walkine0b56e02005-02-25 12:10:27 +00001650 int complex_contents;
Lev Walkin59004fa2004-08-20 13:37:01 +00001651 char *p;
1652
Lev Walkinb9189732004-09-10 09:37:12 +00001653 if(asn1f_fetch_outmost_tag(arg->asn,
1654 expr->module, expr, &outmost_tag_s, 1)) {
1655 outmost_tag = 0;
1656 } else {
1657 outmost_tag = &outmost_tag_s;
1658 }
1659
Lev Walkin59004fa2004-08-20 13:37:01 +00001660 OUT("{ ");
Lev Walkinb9189732004-09-10 09:37:12 +00001661
1662 if(outmost_tag && outmost_tag->tag_value == -1)
1663 OUT("ATF_OPEN_TYPE | ");
Lev Walkinc8285712005-03-04 22:18:20 +00001664 OUT("%s, ",
1665 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
Lev Walkin0204fa62004-09-15 11:54:38 +00001666 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001667 asn1p_expr_t *tv;
1668 int opts = 0;
Lev Walkin0204fa62004-09-15 11:54:38 +00001669 for(tv = expr; tv && tv->marker.flags;
Lev Walkin59004fa2004-08-20 13:37:01 +00001670 tv = TQ_NEXT(tv, next), opts++) {
1671 if(tv->expr_type == A1TC_EXTENSIBLE)
1672 opts--;
1673 }
1674 OUT("%d, ", opts);
1675 } else {
1676 OUT("0, ");
1677 }
Lev Walkina9cc46e2004-09-22 16:06:28 +00001678 if(expr->_anonymous_type) {
1679 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1680 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1681 OUT("0,\n");
1682 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001683 OUT("offsetof(struct ");
1684 out_name_chain(arg, 1);
1685 OUT(", ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001686 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1687 && (!UNNAMED_UNIONS)) OUT("choice.");
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001688 OUT("%s),\n", MKID_safe(expr->Identifier));
Lev Walkin59004fa2004-08-20 13:37:01 +00001689 }
1690 INDENT(+1);
1691 if(C99_MODE) OUT(".tag = ");
Lev Walkinb9189732004-09-10 09:37:12 +00001692 if(outmost_tag) {
1693 if(outmost_tag->tag_value == -1)
1694 OUT("-1 /* Ambiguous tag (ANY?) */");
1695 else
1696 _print_tag(arg, outmost_tag);
Lev Walkinc3e29402004-09-10 06:07:18 +00001697 } else {
Lev Walkinb9189732004-09-10 09:37:12 +00001698 OUT("-1 /* Ambiguous tag (CHOICE?) */");
Lev Walkinc3e29402004-09-10 06:07:18 +00001699 }
Lev Walkinb9189732004-09-10 09:37:12 +00001700
Lev Walkin59004fa2004-08-20 13:37:01 +00001701 OUT(",\n");
1702 if(C99_MODE) OUT(".tag_mode = ");
1703 if(expr->tag.tag_class) {
1704 if(expr->tag.tag_mode == TM_IMPLICIT)
1705 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1706 else
1707 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1708 } else {
1709 OUT("0,\n");
1710 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001711
1712 complex_contents =
1713 (expr->expr_type & ASN_CONSTR_MASK)
1714 || expr->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +00001715 || (0 /* -- prohibited by X.693:8.3.4 */
1716 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +00001717 && expr_elements_count(arg, expr));
Lev Walkin59004fa2004-08-20 13:37:01 +00001718 if(C99_MODE) OUT(".type = ");
Lev Walkin21d00002005-03-04 08:48:53 +00001719 OUT("(void *)&asn_DEF_");
1720 if(complex_contents) {
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001721 OUT("%s", MKID(expr->Identifier));
Lev Walkin21d00002005-03-04 08:48:53 +00001722 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1723 OUT("_%d", expr->_type_unique_index);
Lev Walkin08079b02004-08-22 03:25:24 +00001724 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001725 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkin08079b02004-08-22 03:25:24 +00001726 }
Lev Walkin21d00002005-03-04 08:48:53 +00001727 OUT(",\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001728 if(C99_MODE) OUT(".memb_constraints = ");
1729 if(expr->constraints) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001730 if(arg->flags & A1C_NO_CONSTRAINTS) {
1731 OUT("0,\t/* No check because of -fno-constraints */\n");
1732 } else {
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001733 char *id = MKID(expr->Identifier);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001734 if(expr->_anonymous_type
Lev Walkin152a91e2005-02-14 20:41:29 +00001735 && !strcmp(expr->Identifier, "Member"))
Lev Walkin8de2ab22004-09-26 13:11:31 +00001736 id = asn1c_type_name(arg, expr, TNF_SAFE);
1737 OUT("memb_%s_%d_constraint,\n", id,
Lev Walkin21d00002005-03-04 08:48:53 +00001738 arg->expr->_type_unique_index);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001739 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001740 } else {
Lev Walkinb7bfd1a2005-01-17 12:16:58 +00001741 OUT("0,\t/* Defer constraints checking to the member type */\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001742 }
1743 if(C99_MODE) OUT(".name = ");
Lev Walkin152a91e2005-02-14 20:41:29 +00001744 if(1) {
1745 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1746 OUT("\"\"\n");
1747 else
1748 OUT("\"%s\"\n", expr->Identifier);
1749 } else {
1750 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1751 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001752 OUT("},\n");
1753 INDENT(-1);
1754
Lev Walkin8de2ab22004-09-26 13:11:31 +00001755 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkin59004fa2004-08-20 13:37:01 +00001756 return 0;
1757
1758 save_target = arg->target->target;
1759 REDIR(OT_CODE);
1760
Lev Walkin152a91e2005-02-14 20:41:29 +00001761 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
Lev Walkin59004fa2004-08-20 13:37:01 +00001762 p = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001763 else
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001764 p = MKID(expr->Identifier);
Lev Walkin59004fa2004-08-20 13:37:01 +00001765 OUT("static int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001766 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 +00001767 INDENT(+1);
1768 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1769 tmp_arg = *arg;
1770 tmp_arg.expr = expr;
1771 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001772 OUT("return td->check_constraints"
1773 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001774 }
1775 INDENT(-1);
1776 OUT("}\n");
1777 OUT("\n");
1778
1779 REDIR(save_target);
1780
1781 return 0;
1782}
Lev Walkin59964be2004-08-25 02:03:12 +00001783
Lev Walkindc06f6b2004-10-20 15:50:55 +00001784/*
1785 * Generate "asn_DEF_XXX" type definition.
1786 */
Lev Walkin59964be2004-08-25 02:03:12 +00001787static int
Lev Walkin8de2ab22004-09-26 13:11:31 +00001788emit_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 +00001789 int using_type_name = 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001790 char *p;
1791
Lev Walkindd32b592004-09-06 08:07:29 +00001792 if(HIDE_INNER_DEFS)
1793 OUT("static /* Use -fall-defs-global to expose */\n");
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001794 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID(expr->Identifier));
Lev Walkin21d00002005-03-04 08:48:53 +00001795 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1796 OUT(" = {\n");
Lev Walkin801fabc2005-01-28 12:18:50 +00001797 p = MKID(expr->Identifier);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001798 INDENT(+1);
Lev Walkin59964be2004-08-25 02:03:12 +00001799 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001800 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001801
1802 if(expr->expr_type & ASN_CONSTR_MASK) {
Lev Walkin21d00002005-03-04 08:48:53 +00001803 using_type_name = 1;
Lev Walkin59964be2004-08-25 02:03:12 +00001804 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1805 }
1806
Lev Walkin21d00002005-03-04 08:48:53 +00001807#define FUNCREF(foo) do { \
1808 OUT("%s", p); \
1809 if(HIDE_INNER_DEFS && !using_type_name) \
1810 OUT("_%d", expr->_type_unique_index); \
1811 OUT("_" #foo ",\n"); \
1812} while(0)
1813
1814 FUNCREF(free);
1815 FUNCREF(print);
1816 FUNCREF(constraint);
1817 FUNCREF(decode_ber);
1818 FUNCREF(encode_der);
1819 FUNCREF(decode_xer);
1820 FUNCREF(encode_xer);
Lev Walkin59964be2004-08-25 02:03:12 +00001821
Lev Walkin59964be2004-08-25 02:03:12 +00001822 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1823 OUT("CHOICE_outmost_tag,\n");
1824 } else {
1825 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1826 }
1827
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001828 p = MKID(expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001829 if(tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001830 OUT("asn_DEF_%s_%d_tags,\n",
1831 p, expr->_type_unique_index);
1832 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1833 p, expr->_type_unique_index);
1834 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1835 p, expr->_type_unique_index);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001836 if(tv_mode == _TVM_SUBSET
1837 && tags_count != all_tags_count)
1838 OUT(" - %d", all_tags_count - tags_count);
1839 OUT(", /* %d */\n", tags_count);
Lev Walkin59964be2004-08-25 02:03:12 +00001840 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001841 OUT("0,\t/* No effective tags (pointer) */\n");
1842 OUT("0,\t/* No effective tags (count) */\n");
1843 }
1844
1845 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
Lev Walkin21d00002005-03-04 08:48:53 +00001846 OUT("asn_DEF_%s_%d_all_tags,\n",
1847 p, expr->_type_unique_index);
1848 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1849 p, expr->_type_unique_index);
1850 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1851 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001852 } else if(all_tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001853 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1854 p, expr->_type_unique_index);
1855 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1856 p, expr->_type_unique_index);
1857 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1858 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001859 } else {
1860 OUT("0,\t/* No tags (pointer) */\n");
1861 OUT("0,\t/* No tags (count) */\n");
Lev Walkin59964be2004-08-25 02:03:12 +00001862 }
1863
Lev Walkin59964be2004-08-25 02:03:12 +00001864 if(elements_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001865 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001866 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1867 || expr->expr_type == ASN_CONSTR_SET_OF) {
1868 OUT("%d,\t/* Single element */\n",
1869 elements_count);
1870 assert(elements_count == 1);
1871 } else {
1872 OUT("%d,\t/* Elements count */\n",
1873 elements_count);
1874 }
1875 } else {
Lev Walkin59964be2004-08-25 02:03:12 +00001876 if(expr_elements_count(arg, expr))
1877 OUT("0, 0,\t/* Defined elsewhere */\n");
1878 else
1879 OUT("0, 0,\t/* No members */\n");
1880 }
1881
1882 switch(spec) {
1883 case ETD_NO_SPECIFICS:
1884 OUT("0\t/* No specifics */\n");
1885 break;
1886 case ETD_HAS_SPECIFICS:
Lev Walkin21d00002005-03-04 08:48:53 +00001887 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1888 p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001889 }
Lev Walkin188ed2c2004-09-13 08:31:01 +00001890 INDENT(-1);
Lev Walkin59964be2004-08-25 02:03:12 +00001891 OUT("};\n");
1892 OUT("\n");
Lev Walkin97298782004-08-26 06:20:34 +00001893
1894 return 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001895}
Lev Walkincc93b0f2004-09-10 09:18:20 +00001896
1897/*
1898 * Check if it is better to make this type indirectly accessed via
1899 * a pointer.
1900 * This may be the case for the following recursive definition:
1901 * Type ::= CHOICE { member Type };
1902 */
1903static int
1904expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) {
1905 asn1p_expr_t *top_parent;
1906 asn1p_expr_t *terminal;
1907
1908 if(expr->expr_type != A1TC_REFERENCE)
1909 return 0;
1910
Lev Walkin72a0f5a2005-07-24 08:28:39 +00001911 /* -findirect-choice compiles members of CHOICE as indirect pointers */
1912 if((arg->flags & A1C_INDIRECT_CHOICE)
1913 && arg->expr->expr_type == ASN_CONSTR_CHOICE)
1914 return 1;
1915
Lev Walkincc93b0f2004-09-10 09:18:20 +00001916 /* Rewind to the topmost parent expression */
1917 if((top_parent = expr->parent_expr)) {
1918 while(top_parent->parent_expr)
1919 top_parent = top_parent->parent_expr;
1920 } else {
1921 return 0;
1922 }
1923
Lev Walkina9cc46e2004-09-22 16:06:28 +00001924 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkincc93b0f2004-09-10 09:18:20 +00001925
1926 return (terminal == top_parent);
1927}
Lev Walkina9cc46e2004-09-22 16:06:28 +00001928
1929static int
1930expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1931 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1932 if(!expr) return 0;
1933
1934 /* X.680, 25.5, Table 5 */
1935 switch(expr->expr_type) {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001936 case ASN_BASIC_BOOLEAN:
1937 case ASN_BASIC_ENUMERATED:
1938 case ASN_BASIC_NULL:
1939 return 1;
1940 default:
1941 return 0;
1942 }
1943}
Lev Walkin801fabc2005-01-28 12:18:50 +00001944
1945static int
Lev Walkin21d00002005-03-04 08:48:53 +00001946out_name_chain(arg_t *arg, int check_reserved_keywords) {
Lev Walkin801fabc2005-01-28 12:18:50 +00001947 asn1p_expr_t *expr = arg->expr;
1948 char *id;
1949
1950 assert(expr->Identifier);
1951
Lev Walkin21d00002005-03-04 08:48:53 +00001952 if(arg->flags & A1C_COMPOUND_NAMES
1953 && ((expr->expr_type & ASN_CONSTR_MASK)
1954 || expr->expr_type == ASN_BASIC_ENUMERATED
Lev Walkinb02a8832005-08-13 23:51:47 +00001955 || ((expr->expr_type == ASN_BASIC_INTEGER
1956 || expr->expr_type == ASN_BASIC_BIT_STRING)
Lev Walkin21d00002005-03-04 08:48:53 +00001957 && expr_elements_count(arg, expr))
1958 )
Lev Walkin801fabc2005-01-28 12:18:50 +00001959 && expr->parent_expr
1960 && expr->parent_expr->Identifier) {
1961 arg_t tmparg = *arg;
1962
1963 tmparg.expr = expr->parent_expr;
Lev Walkin21d00002005-03-04 08:48:53 +00001964 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
1965
1966 out_name_chain(&tmparg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +00001967
Lev Walkin01633092005-03-05 00:08:41 +00001968 OUT("__"); /* a separator between id components */
Lev Walkin21d00002005-03-04 08:48:53 +00001969
Lev Walkin801fabc2005-01-28 12:18:50 +00001970 /* Fall through */
1971 }
1972
1973 if(check_reserved_keywords)
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001974 id = MKID_safe(expr->Identifier);
Lev Walkin801fabc2005-01-28 12:18:50 +00001975 else
Lev Walkincaf0d5a2005-03-04 23:48:19 +00001976 id = MKID(expr->Identifier);
Lev Walkin801fabc2005-01-28 12:18:50 +00001977 OUT("%s", id);
1978
1979 return 0;
1980}