blob: a2bdcb70620600ff86ae6336f89932585c7e7528 [file] [log] [blame]
vlmfa67ddc2004-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"
vlm1d036692004-08-19 13:29:46 +00007#include "asn1c_constraint.h"
vlmb2839012004-08-20 13:37:01 +00008#include "asn1c_out.h"
9#include "asn1c_misc.h"
vlm1d036692004-08-19 13:29:46 +000010#include <asn1fix_export.h> /* Stuff exported by libasn1fix */
vlmfa67ddc2004-06-03 03:38:44 +000011
vlm4e03ce22004-06-06 07:20:17 +000012typedef struct tag2el_s {
13 struct asn1p_type_tag_s el_tag;
14 int el_no;
vlmc8aeab42004-06-14 13:09:45 +000015 int toff_first;
16 int toff_last;
vlm4e03ce22004-06-06 07:20:17 +000017 asn1p_expr_t *from_expr;
18} tag2el_t;
19
vlm940bc6b2004-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);
vlm4e03ce22004-06-06 07:20:17 +000026
vlm2e774282005-08-14 15:03:31 +000027enum onc_flags {
28 ONC_noflags = 0x00,
29 ONC_avoid_keywords = 0x01,
30 ONC_force_compound_name = 0x02,
31};
32static int out_name_chain(arg_t *arg, enum onc_flags);
vlmfa67ddc2004-06-03 03:38:44 +000033static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg);
34static int asn1c_lang_C_type_SET_def(arg_t *arg);
35static int asn1c_lang_C_type_CHOICE_def(arg_t *arg);
36static int asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of);
vlma8a86cc2004-09-10 06:07:18 +000037static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p);
vlm79b08d52004-07-01 00:52:50 +000038static int check_if_extensible(asn1p_expr_t *expr);
vlm5a6fc652005-08-16 17:00:21 +000039static int expr_break_recursion(arg_t *arg, asn1p_expr_t *expr);
vlm39ba4c42004-09-22 16:06:28 +000040static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr);
vlm4e554992004-08-25 02:03:12 +000041static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr);
42static int emit_member_table(arg_t *arg, asn1p_expr_t *expr);
vlm940bc6b2004-10-03 09:13:30 +000043static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier);
vlm0b567bf2005-03-04 22:18:20 +000044static int emit_include_dependencies(arg_t *arg);
vlmfa67ddc2004-06-03 03:38:44 +000045
vlm72425de2004-09-13 08:31:01 +000046enum tvm_compat {
47 _TVM_SAME = 0, /* tags and all_tags are same */
48 _TVM_SUBSET = 1, /* tags are subset of all_tags */
49 _TVM_DIFFERENT = 2, /* tags and all_tags are different */
50};
51static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc);
52
vlm4e554992004-08-25 02:03:12 +000053enum etd_spec {
54 ETD_NO_SPECIFICS,
55 ETD_HAS_SPECIFICS
56};
vlm86f5ed22004-09-26 13:11:31 +000057static 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);
vlm4e554992004-08-25 02:03:12 +000058
vlmb2839012004-08-20 13:37:01 +000059#define C99_MODE (!(arg->flags & A1C_NO_C99))
vlmfa67ddc2004-06-03 03:38:44 +000060#define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS)
vlm12c8f692004-09-06 08:07:29 +000061#define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL))
vlmfa67ddc2004-06-03 03:38:44 +000062
63#define PCTX_DEF INDENTED( \
64 OUT("\n"); \
65 OUT("/* Context for parsing across buffer boundaries */\n"); \
vlma5dcb912004-09-29 13:16:40 +000066 OUT("asn_struct_ctx_t _asn_ctx;\n"));
vlmfa67ddc2004-06-03 03:38:44 +000067
vlm0b567bf2005-03-04 22:18:20 +000068
vlm33a4ff12004-08-11 05:21:32 +000069#define DEPENDENCIES do { \
vlm0b567bf2005-03-04 22:18:20 +000070 emit_include_dependencies(arg); \
vlm33a4ff12004-08-11 05:21:32 +000071 if(expr->expr_type == ASN_CONSTR_SET_OF) \
72 GEN_INCLUDE("asn_SET_OF"); \
73 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \
74 GEN_INCLUDE("asn_SEQUENCE_OF"); \
75} while(0)
vlmfa67ddc2004-06-03 03:38:44 +000076
vlm1f7df782005-03-04 23:48:19 +000077/* MKID_safe() without checking for reserved keywords */
78#define MKID(id) asn1c_make_identifier(0, (id), 0)
79#define MKID_safe(id) asn1c_make_identifier(AMI_CHECK_RESERVED, (id), 0)
vlmfa67ddc2004-06-03 03:38:44 +000080
81int
vlmf9d178d2004-09-14 12:47:45 +000082asn1c_lang_C_type_REAL(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +000083 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
84}
85
vlm80a48592005-02-25 12:10:27 +000086struct value2enum {
87 asn1c_integer_t value;
88 const char *name;
89 int idx;
90};
91static int compar_enumMap_byName(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 return strcmp(a->name, b->name);
95}
96static int compar_enumMap_byValue(const void *ap, const void *bp) {
97 const struct value2enum *a = (const struct value2enum *)ap;
98 const struct value2enum *b = (const struct value2enum *)bp;
99 if(a->value < b->value)
100 return -1;
101 else if(a->value == b->value)
102 return 0;
103 return 1;
104}
105
vlmfa67ddc2004-06-03 03:38:44 +0000106int
vlmf9d178d2004-09-14 12:47:45 +0000107asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000108 asn1p_expr_t *expr = arg->expr;
109 asn1p_expr_t *v;
vlm80a48592005-02-25 12:10:27 +0000110 int el_count = expr_elements_count(arg, expr);
111 struct value2enum *v2e;
112 int map_is_extensible = (expr->expr_type == ASN_BASIC_INTEGER);
vlm4573af02005-08-13 23:30:24 +0000113 int eidx;
vlmfa67ddc2004-06-03 03:38:44 +0000114
vlm80a48592005-02-25 12:10:27 +0000115 v2e = alloca((el_count + 1) * sizeof(*v2e));
vlmfa67ddc2004-06-03 03:38:44 +0000116
vlm80a48592005-02-25 12:10:27 +0000117 /*
vlm4573af02005-08-13 23:30:24 +0000118 * For all ENUMERATED types and for those INTEGER types which
119 * have identifiers, print out an enumeration table.
vlm80a48592005-02-25 12:10:27 +0000120 */
vlm4573af02005-08-13 23:30:24 +0000121 if(expr->expr_type == ASN_BASIC_ENUMERATED || el_count) {
122 eidx = 0;
vlm80a48592005-02-25 12:10:27 +0000123 REDIR(OT_DEPS);
vlm766cf1e2005-03-04 08:48:53 +0000124 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000125 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000126 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000127 TQ_FOR(v, &(expr->members), next) {
128 switch(v->expr_type) {
129 case A1TC_UNIVERVAL:
vlm766cf1e2005-03-04 08:48:53 +0000130 OUT("\t");
vlm2e774282005-08-14 15:03:31 +0000131 out_name_chain(arg, ONC_noflags);
vlm1f7df782005-03-04 23:48:19 +0000132 OUT("_%s", MKID(v->Identifier));
vlm766cf1e2005-03-04 08:48:53 +0000133 OUT("\t= %" PRIdASN "%s\n",
vlm80a48592005-02-25 12:10:27 +0000134 v->value->value.v_integer,
vlm766cf1e2005-03-04 08:48:53 +0000135 (eidx+1 < el_count) ? "," : "");
vlm80a48592005-02-25 12:10:27 +0000136 v2e[eidx].name = v->Identifier;
137 v2e[eidx].value = v->value->value.v_integer;
138 eidx++;
vlmfa67ddc2004-06-03 03:38:44 +0000139 break;
vlmf9d178d2004-09-14 12:47:45 +0000140 case A1TC_EXTENSIBLE:
141 OUT("\t/*\n");
142 OUT("\t * Enumeration is extensible\n");
143 OUT("\t */\n");
vlm80a48592005-02-25 12:10:27 +0000144 map_is_extensible = 1;
vlmf9d178d2004-09-14 12:47:45 +0000145 break;
vlmfa67ddc2004-06-03 03:38:44 +0000146 default:
147 return -1;
148 }
149 }
vlm766cf1e2005-03-04 08:48:53 +0000150 OUT("} ");
vlm2e774282005-08-14 15:03:31 +0000151 out_name_chain(arg, ONC_noflags);
vlm766cf1e2005-03-04 08:48:53 +0000152 OUT("_e;\n");
vlm80a48592005-02-25 12:10:27 +0000153 assert(eidx == el_count);
vlm4573af02005-08-13 23:30:24 +0000154 }
155
156 /*
157 * For all ENUMERATED types print out a mapping table
158 * between identifiers and associated values.
159 * This is prohibited for INTEGER types by by X.693:8.3.4.
160 */
161 if(expr->expr_type == ASN_BASIC_ENUMERATED) {
vlm80a48592005-02-25 12:10:27 +0000162
163 /*
164 * Generate a enumerationName<->value map for XER codec.
165 */
166 REDIR(OT_STAT_DEFS);
167
vlm766cf1e2005-03-04 08:48:53 +0000168 OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_%d_value2enum[] = {\n",
vlm1f7df782005-03-04 23:48:19 +0000169 MKID(expr->Identifier), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000170 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
171 for(eidx = 0; eidx < el_count; eidx++) {
172 v2e[eidx].idx = eidx;
173 OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n",
174 v2e[eidx].value,
175 (long)strlen(v2e[eidx].name), v2e[eidx].name,
176 (eidx + 1 < el_count) ? "," : "");
177 }
178 if(map_is_extensible)
179 OUT("\t/* This list is extensible */\n");
180 OUT("};\n");
181
vlm766cf1e2005-03-04 08:48:53 +0000182 OUT("static unsigned int asn_MAP_%s_%d_enum2value[] = {\n",
vlm1f7df782005-03-04 23:48:19 +0000183 MKID(expr->Identifier), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000184 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
185 for(eidx = 0; eidx < el_count; eidx++) {
186 OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n",
187 v2e[eidx].idx,
188 (eidx + 1 < el_count) ? "," : "",
189 v2e[eidx].name, v2e[eidx].value);
190 }
191 if(map_is_extensible)
192 OUT("\t/* This list is extensible */\n");
193 OUT("};\n");
194
vlm766cf1e2005-03-04 08:48:53 +0000195 OUT("static asn_INTEGER_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000196 MKID(expr->Identifier), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000197 INDENT(+1);
vlm766cf1e2005-03-04 08:48:53 +0000198 OUT("asn_MAP_%s_%d_value2enum,\t"
vlm80a48592005-02-25 12:10:27 +0000199 "/* \"tag\" => N; sorted by tag */\n",
vlm1f7df782005-03-04 23:48:19 +0000200 MKID(expr->Identifier),
vlm766cf1e2005-03-04 08:48:53 +0000201 expr->_type_unique_index);
202 OUT("asn_MAP_%s_%d_enum2value,\t"
vlm80a48592005-02-25 12:10:27 +0000203 "/* N => \"tag\"; sorted by N */\n",
vlm1f7df782005-03-04 23:48:19 +0000204 MKID(expr->Identifier),
vlm766cf1e2005-03-04 08:48:53 +0000205 expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000206 OUT("%d,\t/* Number of elements in the maps */\n",
207 el_count);
208 OUT("%d,\t/* Enumeration is %sextensible */\n",
209 map_is_extensible, map_is_extensible ? "": "not ");
210 if(expr->expr_type == ASN_BASIC_ENUMERATED)
211 OUT("1\t/* Strict enumeration */\n");
212 else
213 OUT("0\n");
214 INDENT(-1);
215 OUT("};\n");
vlmfa67ddc2004-06-03 03:38:44 +0000216 }
217
218 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
219}
220
221int
vlm96853d82005-08-13 23:51:47 +0000222asn1c_lang_C_type_BIT_STRING(arg_t *arg) {
223 asn1p_expr_t *expr = arg->expr;
224 asn1p_expr_t *v;
225 int el_count = expr_elements_count(arg, expr);
vlm96853d82005-08-13 23:51:47 +0000226
227 if(el_count) {
vlm75b3a532005-08-14 02:18:27 +0000228 int eidx = 0;
vlm96853d82005-08-13 23:51:47 +0000229 REDIR(OT_DEPS);
230 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000231 out_name_chain(arg, ONC_avoid_keywords);
vlm96853d82005-08-13 23:51:47 +0000232 OUT(" {\n");
233 TQ_FOR(v, &(expr->members), next) {
vlm75b3a532005-08-14 02:18:27 +0000234 eidx++;
235 if(v->expr_type != A1TC_UNIVERVAL) {
vlm96853d82005-08-13 23:51:47 +0000236 OUT("/* Unexpected BIT STRING element: %s */\n",
237 v->Identifier);
vlm75b3a532005-08-14 02:18:27 +0000238 continue;
vlm96853d82005-08-13 23:51:47 +0000239 }
vlm75b3a532005-08-14 02:18:27 +0000240 OUT("\t");
vlm2e774282005-08-14 15:03:31 +0000241 out_name_chain(arg, ONC_noflags);
vlm75b3a532005-08-14 02:18:27 +0000242 OUT("_%s", MKID(v->Identifier));
243 OUT("\t= %" PRIdASN "%s\n",
244 v->value->value.v_integer,
245 (eidx < el_count) ? "," : "");
vlm96853d82005-08-13 23:51:47 +0000246 }
247 OUT("} ");
vlm2e774282005-08-14 15:03:31 +0000248 out_name_chain(arg, ONC_noflags);
vlm96853d82005-08-13 23:51:47 +0000249 OUT("_e;\n");
250 assert(eidx == el_count);
251 }
252
253 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
254}
255
256int
vlmfa67ddc2004-06-03 03:38:44 +0000257asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
258 asn1p_expr_t *expr = arg->expr;
259 asn1p_expr_t *v;
260 int comp_mode = 0; /* {root,ext=1,root,root,...} */
261
262 DEPENDENCIES;
263
264 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000265 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000266 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000267 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000268 } else {
269 OUT("typedef struct %s {\n",
vlm1f7df782005-03-04 23:48:19 +0000270 MKID_safe(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000271 }
272
273 TQ_FOR(v, &(expr->members), next) {
vlm5a6fc652005-08-16 17:00:21 +0000274 if(v->expr_type == A1TC_EXTENSIBLE)
vlmfa67ddc2004-06-03 03:38:44 +0000275 if(comp_mode < 3) comp_mode++;
vlm5a6fc652005-08-16 17:00:21 +0000276 if(comp_mode == 1)
vlma02b74d2004-09-15 11:54:38 +0000277 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000278 EMBED(v);
279 }
280
281 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000282 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000283 expr->_anonymous_type ? "" :
284 arg->embed
285 ? MKID_safe(expr->Identifier)
286 : MKID(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000287 arg->embed ? "" : "_t");
vlmfa67ddc2004-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 */
vlmfa67ddc2004-06-03 03:38:44 +0000297 int ext_start = -1;
298 int ext_stop = -1;
vlm4e03ce22004-06-06 07:20:17 +0000299 tag2el_t *tag2el = NULL;
300 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000301 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000302 int all_tags_count;
303 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000304
vlm4e03ce22004-06-06 07:20:17 +0000305 /*
306 * Fetch every inner tag from the tag to elements map.
307 */
vlm940bc6b2004-10-03 09:13:30 +0000308 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlm4e03ce22004-06-06 07:20:17 +0000309 if(tag2el) free(tag2el);
310 return -1;
311 }
312
vlm33a4ff12004-08-11 05:21:32 +0000313 GEN_INCLUDE("constr_SEQUENCE");
314 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000315 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000316
vlm33a4ff12004-08-11 05:21:32 +0000317 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000318
319 /*
320 * Print out the table according to which the parsing is performed.
321 */
vlm39ba4c42004-09-22 16:06:28 +0000322 if(expr_elements_count(arg, expr)) {
323 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000324
vlm766cf1e2005-03-04 08:48:53 +0000325 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm1f7df782005-03-04 23:48:19 +0000326 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-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 }
vlm5a6fc652005-08-16 17:00:21 +0000337 if(comp_mode == 1)
338 v->marker.flags |= EM_INDIRECT;
vlm39ba4c42004-09-22 16:06:28 +0000339 emit_member_table(arg, v);
vlm5a6fc652005-08-16 17:00:21 +0000340 elements++;
vlm39ba4c42004-09-22 16:06:28 +0000341 });
342 OUT("};\n");
343 } else {
344 elements = 0;
345 }
vlmfa67ddc2004-06-03 03:38:44 +0000346
vlm4a3f5822004-06-28 21:13:46 +0000347 /*
vlma5dcb912004-09-29 13:16:40 +0000348 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000349 */
vlm72425de2004-09-13 08:31:01 +0000350 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000351
vlm4e03ce22004-06-06 07:20:17 +0000352 /*
353 * Tags to elements map.
354 */
vlm940bc6b2004-10-03 09:13:30 +0000355 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlm4e03ce22004-06-06 07:20:17 +0000356
vlm766cf1e2005-03-04 08:48:53 +0000357 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000358 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000359 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000360 OUT("sizeof(struct "); out_name_chain(arg, ONC_avoid_keywords); OUT("),\n");
361 OUT("offsetof(struct "); out_name_chain(arg, ONC_avoid_keywords); OUT(", _asn_ctx),\n");
vlm766cf1e2005-03-04 08:48:53 +0000362
vlmfbd6d9e2005-06-02 05:21:37 +0000363 if(tag2el_count) {
364 OUT("asn_MAP_%s_%d_tag2el,\n",
365 MKID(expr->Identifier),
366 expr->_type_unique_index);
367 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
368 } else {
369 OUT("0,\t/* No top level tags */\n");
370 OUT("0,\t/* No tags in the map */\n");
371 }
vlmfa67ddc2004-06-03 03:38:44 +0000372 OUT("%d,\t/* Start extensions */\n",
373 ext_start);
374 OUT("%d\t/* Stop extensions */\n",
375 (ext_stop<ext_start)?elements+1:ext_stop, ext_stop);
376 );
377 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000378
379 /*
vlma5dcb912004-09-29 13:16:40 +0000380 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000381 */
vlm72425de2004-09-13 08:31:01 +0000382 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000383 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000384
vlmfa67ddc2004-06-03 03:38:44 +0000385 REDIR(OT_TYPE_DECLS);
386
387 return 0;
vlmedf203f2005-01-17 11:57:48 +0000388} /* _SEQUENCE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000389
390int
vlmfa67ddc2004-06-03 03:38:44 +0000391asn1c_lang_C_type_SET(arg_t *arg) {
392 asn1p_expr_t *expr = arg->expr;
393 asn1p_expr_t *v;
394 long mcount;
395 char *id;
396 int comp_mode = 0; /* {root,ext=1,root,root,...} */
397
398 DEPENDENCIES;
399
400 REDIR(OT_DEPS);
401
402 OUT("\n");
403 OUT("/*\n");
404 OUT(" * Method of determining the components presence\n");
405 OUT(" */\n");
406 mcount = 0;
vlm766cf1e2005-03-04 08:48:53 +0000407 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000408 out_name_chain(arg, ONC_noflags);
vlm766cf1e2005-03-04 08:48:53 +0000409 OUT("_PR {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000410 TQ_FOR(v, &(expr->members), next) {
411 if(v->expr_type == A1TC_EXTENSIBLE) continue;
412 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000413 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000414 OUT("_PR_");
vlm1f7df782005-03-04 23:48:19 +0000415 id = MKID(v->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000416 OUT("%s,\t/* Member %s is present */\n",
417 id, id)
418 );
419 mcount++;
420 }
vlm2e774282005-08-14 15:03:31 +0000421 OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n");
vlmfa67ddc2004-06-03 03:38:44 +0000422
423 REDIR(OT_TYPE_DECLS);
424
425 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000426 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000427 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000428 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000429 } else {
vlm1f7df782005-03-04 23:48:19 +0000430 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000431 }
432
433 TQ_FOR(v, &(expr->members), next) {
434 if(v->expr_type == A1TC_EXTENSIBLE) {
435 if(comp_mode < 3) comp_mode++;
436 }
vlm5a6fc652005-08-16 17:00:21 +0000437 if(comp_mode == 1)
vlma02b74d2004-09-15 11:54:38 +0000438 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000439 EMBED(v);
440 }
441
442 INDENTED(
vlm1f7df782005-03-04 23:48:19 +0000443 id = MKID(expr->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000444 OUT("\n");
445 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
446 id, id);
447 OUT("unsigned int _presence_map\n");
448 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
449 );
450
451 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000452 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000453 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000454 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000455
456 return asn1c_lang_C_type_SET_def(arg);
457}
458
vlmfa67ddc2004-06-03 03:38:44 +0000459static int
460asn1c_lang_C_type_SET_def(arg_t *arg) {
461 asn1p_expr_t *expr = arg->expr;
462 asn1p_expr_t *v;
463 int elements;
vlmfa67ddc2004-06-03 03:38:44 +0000464 tag2el_t *tag2el = NULL;
465 int tag2el_count = 0;
vlm940bc6b2004-10-03 09:13:30 +0000466 tag2el_t *tag2el_cxer = NULL;
467 int tag2el_cxer_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000468 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000469 int all_tags_count;
470 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000471 char *p;
472
473 /*
474 * Fetch every inner tag from the tag to elements map.
475 */
vlm940bc6b2004-10-03 09:13:30 +0000476 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000477 if(tag2el) free(tag2el);
478 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000479 }
vlm940bc6b2004-10-03 09:13:30 +0000480 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
481 if(tag2el) free(tag2el);
482 if(tag2el_cxer) free(tag2el_cxer);
483 return -1;
484 }
485 if(tag2el_cxer_count == tag2el_count
486 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
487 free(tag2el_cxer);
488 tag2el_cxer = 0;
489 }
vlmfa67ddc2004-06-03 03:38:44 +0000490
vlm33a4ff12004-08-11 05:21:32 +0000491 GEN_INCLUDE("constr_SET");
492 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000493 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000494
495 REDIR(OT_STAT_DEFS);
496
vlmfa67ddc2004-06-03 03:38:44 +0000497 /*
498 * Print out the table according to which the parsing is performed.
499 */
vlm39ba4c42004-09-22 16:06:28 +0000500 if(expr_elements_count(arg, expr)) {
501 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000502
vlm766cf1e2005-03-04 08:48:53 +0000503 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm1f7df782005-03-04 23:48:19 +0000504 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000505
506 elements = 0;
507 INDENTED(TQ_FOR(v, &(expr->members), next) {
508 if(v->expr_type == A1TC_EXTENSIBLE) {
509 if(comp_mode < 3) comp_mode++;
510 } else {
vlm5a6fc652005-08-16 17:00:21 +0000511 if(comp_mode == 1)
vlm39ba4c42004-09-22 16:06:28 +0000512 v->marker.flags |= EM_INDIRECT;
vlm39ba4c42004-09-22 16:06:28 +0000513 emit_member_table(arg, v);
vlm5a6fc652005-08-16 17:00:21 +0000514 elements++;
vlm39ba4c42004-09-22 16:06:28 +0000515 }
516 });
517 OUT("};\n");
518 } else {
519 elements = 0;
520 }
vlmfa67ddc2004-06-03 03:38:44 +0000521
vlm4a3f5822004-06-28 21:13:46 +0000522 /*
vlma5dcb912004-09-29 13:16:40 +0000523 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000524 */
vlm72425de2004-09-13 08:31:01 +0000525 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000526
527 /*
528 * Tags to elements map.
529 */
vlm940bc6b2004-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");
vlmfa67ddc2004-06-03 03:38:44 +0000533
534 /*
535 * Emit a map of mandatory elements.
536 */
vlm766cf1e2005-03-04 08:48:53 +0000537 OUT("static uint8_t asn_MAP_%s_%d_mmap",
vlm1f7df782005-03-04 23:48:19 +0000538 MKID(expr->Identifier), expr->_type_unique_index);
539 p = MKID_safe(expr->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000540 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
vlm766cf1e2005-03-04 08:48:53 +0000541 OUT(" = {\n");
vlmfa67ddc2004-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 }
vlmddd5a7d2004-09-10 09:18:20 +0000554 OUT("(%d << %d)",
vlma02b74d2004-09-15 11:54:38 +0000555 v->marker.flags?0:1,
vlmddd5a7d2004-09-10 09:18:20 +0000556 7 - (el % 8));
vlmfa67ddc2004-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
vlm766cf1e2005-03-04 08:48:53 +0000568 OUT("static asn_SET_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000569 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000570 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000571 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000572 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000573 OUT("),\n");
574 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000575 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000576 OUT(", _asn_ctx),\n");
577 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000578 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000579 OUT(", _presence_map),\n");
vlm1f7df782005-03-04 23:48:19 +0000580 p = MKID(expr->Identifier);
vlm766cf1e2005-03-04 08:48:53 +0000581 OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000582 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm940bc6b2004-10-03 09:13:30 +0000583 if(tag2el_cxer)
vlm766cf1e2005-03-04 08:48:53 +0000584 OUT("asn_MAP_%s_%d_tag2el_cxer,\n",
585 p, expr->_type_unique_index);
vlm940bc6b2004-10-03 09:13:30 +0000586 else
vlm766cf1e2005-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);
vlm79b08d52004-07-01 00:52:50 +0000591 OUT("%d,\t/* Whether extensible */\n",
592 check_if_extensible(expr));
vlm766cf1e2005-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);
vlmfa67ddc2004-06-03 03:38:44 +0000595 );
596 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000597
598 /*
vlma5dcb912004-09-29 13:16:40 +0000599 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000600 */
vlm72425de2004-09-13 08:31:01 +0000601 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000602 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000603
vlmfa67ddc2004-06-03 03:38:44 +0000604 REDIR(OT_TYPE_DECLS);
605
606 return 0;
vlmedf203f2005-01-17 11:57:48 +0000607} /* _SET_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000608
609int
vlmdae7f9d2004-08-22 03:25:24 +0000610asn1c_lang_C_type_SEx_OF(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000611 asn1p_expr_t *expr = arg->expr;
vlm0b567bf2005-03-04 22:18:20 +0000612 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
vlmfa67ddc2004-06-03 03:38:44 +0000613
614 DEPENDENCIES;
615
616 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000617 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000618 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000619 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000620 } else {
vlm1f7df782005-03-04 23:48:19 +0000621 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000622 }
623
vlmdae7f9d2004-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");
vlm5a6fc652005-08-16 17:00:21 +0000628
629 /*
630 * README README
631 * The implementation of the A_SET_OF() macro is already indirect.
632 */
633 memb->marker.flags |= EM_INDIRECT;
634
vlm80a48592005-02-25 12:10:27 +0000635 if(memb->expr_type & ASN_CONSTR_MASK
636 || ((memb->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +0000637 || (0 /* -- prohibited by X.693:8.3.4 */
638 && memb->expr_type == ASN_BASIC_INTEGER))
639 && expr_elements_count(arg, memb))) {
vlmdae7f9d2004-08-22 03:25:24 +0000640 arg_t tmp;
641 asn1p_expr_t tmp_memb;
642 arg->embed++;
643 tmp = *arg;
644 tmp.expr = &tmp_memb;
645 tmp_memb = *memb;
vlm5a6fc652005-08-16 17:00:21 +0000646 tmp_memb.marker.flags &= ~EM_INDIRECT;
vlmdae7f9d2004-08-22 03:25:24 +0000647 tmp_memb._anonymous_type = 1;
vlm39ba4c42004-09-22 16:06:28 +0000648 if(tmp_memb.Identifier == 0) {
vlm766cf1e2005-03-04 08:48:53 +0000649 tmp_memb.Identifier = "Member";
650 if(0)
vlm39ba4c42004-09-22 16:06:28 +0000651 tmp_memb.Identifier = strdup(
652 asn1c_make_identifier(0,
vlmbf3c5112005-02-14 20:41:29 +0000653 expr->Identifier, "Member", 0));
vlm39ba4c42004-09-22 16:06:28 +0000654 assert(tmp_memb.Identifier);
655 }
vlmdae7f9d2004-08-22 03:25:24 +0000656 tmp.default_cb(&tmp);
vlm39ba4c42004-09-22 16:06:28 +0000657 if(tmp_memb.Identifier != memb->Identifier)
vlm766cf1e2005-03-04 08:48:53 +0000658 if(0) free(tmp_memb.Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000659 arg->embed--;
660 assert(arg->target->target == OT_TYPE_DECLS);
661 } else {
vlm6c5d5e52005-03-04 22:38:22 +0000662 OUT("%s", asn1c_type_name(arg, memb,
663 (memb->marker.flags & EM_UNRECURSE)
664 ? TNF_RSAFE : TNF_CTYPE));
vlmfa67ddc2004-06-03 03:38:44 +0000665 }
vlm5a6fc652005-08-16 17:00:21 +0000666 /* README README (above) */
667 if(0 && (memb->marker.flags & EM_INDIRECT))
668 OUT(" *");
vlmdae7f9d2004-08-22 03:25:24 +0000669 OUT(") list;\n");
670 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000671
672 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000673 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000674 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000675 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000676
677 /*
vlmdae7f9d2004-08-22 03:25:24 +0000678 * SET OF/SEQUENCE OF definition
vlmfa67ddc2004-06-03 03:38:44 +0000679 */
vlmdae7f9d2004-08-22 03:25:24 +0000680 return asn1c_lang_C_type_SEx_OF_def(arg,
681 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
vlmfa67ddc2004-06-03 03:38:44 +0000682}
683
684static int
685asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
686 asn1p_expr_t *expr = arg->expr;
687 asn1p_expr_t *v;
vlm6e73a042004-08-11 07:17:22 +0000688 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000689 int all_tags_count;
690 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000691
vlm33a4ff12004-08-11 05:21:32 +0000692 /*
693 * Print out the table according to which the parsing is performed.
694 */
vlmfa67ddc2004-06-03 03:38:44 +0000695 if(seq_of) {
vlm33a4ff12004-08-11 05:21:32 +0000696 GEN_INCLUDE("constr_SEQUENCE_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000697 } else {
vlm33a4ff12004-08-11 05:21:32 +0000698 GEN_INCLUDE("constr_SET_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000699 }
vlm33a4ff12004-08-11 05:21:32 +0000700 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000701 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000702
703 REDIR(OT_STAT_DEFS);
704
705 /*
706 * Print out the table according to which the parsing is performed.
707 */
vlm766cf1e2005-03-04 08:48:53 +0000708 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
709 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000710 INDENT(+1);
vlmfa67ddc2004-06-03 03:38:44 +0000711 v = TQ_FIRST(&(expr->members));
vlm39ba4c42004-09-22 16:06:28 +0000712 if(!v->Identifier) {
vlmbf3c5112005-02-14 20:41:29 +0000713 v->Identifier = strdup("Member");
vlm39ba4c42004-09-22 16:06:28 +0000714 assert(v->Identifier);
715 }
716 v->_anonymous_type = 1;
717 arg->embed++;
vlm4e554992004-08-25 02:03:12 +0000718 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000719 arg->embed--;
720 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000721 OUT("};\n");
722
vlm4a3f5822004-06-28 21:13:46 +0000723 /*
vlma5dcb912004-09-29 13:16:40 +0000724 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000725 */
vlm72425de2004-09-13 08:31:01 +0000726 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000727
vlm766cf1e2005-03-04 08:48:53 +0000728 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000729 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000730 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000731 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000732 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000733 OUT("),\n");
734 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000735 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000736 OUT(", _asn_ctx),\n");
737
vlm39ba4c42004-09-22 16:06:28 +0000738 if(expr_as_xmlvaluelist(arg, v))
739 OUT("1,\t/* XER encoding is XMLValueList */\n");
740 else
741 OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n");
vlmfa67ddc2004-06-03 03:38:44 +0000742 );
743 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000744
745 /*
vlma5dcb912004-09-29 13:16:40 +0000746 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000747 */
vlm72425de2004-09-13 08:31:01 +0000748 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
vlm86f5ed22004-09-26 13:11:31 +0000749 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000750
vlmfa67ddc2004-06-03 03:38:44 +0000751 REDIR(OT_TYPE_DECLS);
752
753 return 0;
vlmedf203f2005-01-17 11:57:48 +0000754} /* _SEx_OF_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000755
756int
757asn1c_lang_C_type_CHOICE(arg_t *arg) {
758 asn1p_expr_t *expr = arg->expr;
759 asn1p_expr_t *v;
vlmdae7f9d2004-08-22 03:25:24 +0000760 char *id;
vlmfa67ddc2004-06-03 03:38:44 +0000761
762 DEPENDENCIES;
763
vlm33a4ff12004-08-11 05:21:32 +0000764 REDIR(OT_DEPS);
765
vlm766cf1e2005-03-04 08:48:53 +0000766 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000767 out_name_chain(arg, ONC_noflags);
vlm766cf1e2005-03-04 08:48:53 +0000768 OUT("_PR {\n");
vlm33a4ff12004-08-11 05:21:32 +0000769 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000770 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000771 OUT("_PR_NOTHING,\t/* No components present */\n");
vlm33a4ff12004-08-11 05:21:32 +0000772 TQ_FOR(v, &(expr->members), next) {
773 if(v->expr_type == A1TC_EXTENSIBLE) {
774 OUT("/* Extensions may appear below */\n");
775 continue;
776 }
vlm2e774282005-08-14 15:03:31 +0000777 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000778 OUT("_PR_");
vlm1f7df782005-03-04 23:48:19 +0000779 id = MKID(v->Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000780 OUT("%s,\n", id, id);
vlm33a4ff12004-08-11 05:21:32 +0000781 }
782 );
vlm2e774282005-08-14 15:03:31 +0000783 OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n");
vlm33a4ff12004-08-11 05:21:32 +0000784
785 REDIR(OT_TYPE_DECLS);
vlmfa67ddc2004-06-03 03:38:44 +0000786
787 if(arg->embed) {
vlm2e774282005-08-14 15:03:31 +0000788 OUT("struct "); out_name_chain(arg, ONC_avoid_keywords); OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000789 } else {
vlm1f7df782005-03-04 23:48:19 +0000790 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000791 }
792
793 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000794 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000795 OUT("_PR present;\n");
vlm2e774282005-08-14 15:03:31 +0000796 OUT("union ");
797 if(UNNAMED_UNIONS == 0) {
798 out_name_chain(arg, ONC_force_compound_name);
799 OUT("_u ");
800 }
801 OUT("{\n");
vlmfa67ddc2004-06-03 03:38:44 +0000802 TQ_FOR(v, &(expr->members), next) {
803 EMBED(v);
804 }
805 if(UNNAMED_UNIONS) OUT("};\n");
806 else OUT("} choice;\n");
807 );
808
809 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000810 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000811 expr->_anonymous_type ? "" :
812 arg->embed
813 ? MKID_safe(expr->Identifier)
814 : MKID(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000815 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000816
817 return asn1c_lang_C_type_CHOICE_def(arg);
818}
819
820static int
821asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
822 asn1p_expr_t *expr = arg->expr;
823 asn1p_expr_t *v;
824 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000825 tag2el_t *tag2el = NULL;
826 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000827 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000828 int all_tags_count;
829 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000830
831 /*
832 * Fetch every inner tag from the tag to elements map.
833 */
vlm940bc6b2004-10-03 09:13:30 +0000834 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000835 if(tag2el) free(tag2el);
836 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000837 }
838
vlm33a4ff12004-08-11 05:21:32 +0000839 GEN_INCLUDE("constr_CHOICE");
840 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000841 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000842
vlm33a4ff12004-08-11 05:21:32 +0000843 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000844
845 /*
846 * Print out the table according to which the parsing is performed.
847 */
vlm39ba4c42004-09-22 16:06:28 +0000848 if(expr_elements_count(arg, expr)) {
vlmfa67ddc2004-06-03 03:38:44 +0000849
vlm766cf1e2005-03-04 08:48:53 +0000850 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
851 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000852
853 elements = 0;
854 INDENTED(TQ_FOR(v, &(expr->members), next) {
vlm34dcd572005-01-17 11:40:49 +0000855 if(v->expr_type == A1TC_EXTENSIBLE)
856 continue;
vlm34dcd572005-01-17 11:40:49 +0000857 emit_member_table(arg, v);
vlm5a6fc652005-08-16 17:00:21 +0000858 elements++;
vlm39ba4c42004-09-22 16:06:28 +0000859 });
860 OUT("};\n");
861 } else {
862 elements = 0;
863 }
vlmfa67ddc2004-06-03 03:38:44 +0000864
vlm6e73a042004-08-11 07:17:22 +0000865
vlmfa67ddc2004-06-03 03:38:44 +0000866 if(arg->embed) {
867 /*
868 * Our parent structure has already taken this into account.
869 */
vlm72425de2004-09-13 08:31:01 +0000870 tv_mode = _TVM_SAME;
871 tags_count = all_tags_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000872 } else {
vlm72425de2004-09-13 08:31:01 +0000873 tv_mode = emit_tags_vectors(arg, expr,
874 &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000875 }
vlmfa67ddc2004-06-03 03:38:44 +0000876
877 /*
878 * Tags to elements map.
879 */
vlm940bc6b2004-10-03 09:13:30 +0000880 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000881
vlm766cf1e2005-03-04 08:48:53 +0000882 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000883 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000884 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000885 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000886 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000887 OUT("),\n");
888 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000889 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000890 OUT(", _asn_ctx),\n");
891 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000892 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000893 OUT(", present),\n");
894 OUT("sizeof(((struct ");
vlm2e774282005-08-14 15:03:31 +0000895 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000896 OUT(" *)0)->present),\n");
897 OUT("asn_MAP_%s_%d_tag2el,\n",
vlm1f7df782005-03-04 23:48:19 +0000898 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000899 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm79b08d52004-07-01 00:52:50 +0000900 OUT("%d\t/* Whether extensible */\n",
901 check_if_extensible(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000902 );
903 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000904
905 /*
vlma5dcb912004-09-29 13:16:40 +0000906 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000907 */
vlm72425de2004-09-13 08:31:01 +0000908 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000909 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000910
vlmfa67ddc2004-06-03 03:38:44 +0000911 REDIR(OT_TYPE_DECLS);
912
913 return 0;
vlmedf203f2005-01-17 11:57:48 +0000914} /* _CHOICE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000915
916int
917asn1c_lang_C_type_REFERENCE(arg_t *arg) {
918 asn1p_ref_t *ref;
919
920 ref = arg->expr->reference;
921 if(ref->components[ref->comp_count-1].name[0] == '&') {
vlmfa67ddc2004-06-03 03:38:44 +0000922 asn1p_expr_t *extract;
923 arg_t tmp;
924 int ret;
925
vlm39ba4c42004-09-22 16:06:28 +0000926 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
vlmdae7f9d2004-08-22 03:25:24 +0000927 arg->expr, ref);
vlmfa67ddc2004-06-03 03:38:44 +0000928 if(extract == NULL)
929 return -1;
930
vlmdae7f9d2004-08-22 03:25:24 +0000931 extract = asn1p_expr_clone(extract, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000932 if(extract) {
933 if(extract->Identifier)
934 free(extract->Identifier);
935 extract->Identifier = strdup(arg->expr->Identifier);
936 if(extract->Identifier == NULL) {
937 asn1p_expr_free(extract);
938 return -1;
939 }
940 } else {
941 return -1;
942 }
943
944 tmp = *arg;
945 tmp.asn = arg->asn;
vlmdae7f9d2004-08-22 03:25:24 +0000946 tmp.mod = extract->module;
vlmfa67ddc2004-06-03 03:38:44 +0000947 tmp.expr = extract;
948
949 ret = arg->default_cb(&tmp);
950
951 asn1p_expr_free(extract);
952
953 return ret;
954 }
955
956
957 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
958}
959
960int
961asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
962 asn1p_expr_t *expr = arg->expr;
vlm6e73a042004-08-11 07:17:22 +0000963 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000964 int all_tags_count;
965 enum tvm_compat tv_mode;
vlm80a48592005-02-25 12:10:27 +0000966 enum etd_spec etd_spec;
vlmfa67ddc2004-06-03 03:38:44 +0000967 char *p;
968
969 if(arg->embed) {
vlma5dcb912004-09-29 13:16:40 +0000970 enum tnfmt tnfmt = TNF_CTYPE;
971
972 /*
973 * If this is an optional compound type,
974 * refer it using "struct X" convention,
975 * as it may recursively include the current structure.
976 */
vlm0b567bf2005-03-04 22:18:20 +0000977 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
vlma5dcb912004-09-29 13:16:40 +0000978 asn1p_expr_t *terminal;
979 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
980 if(terminal
981 && (terminal->expr_type & ASN_CONSTR_MASK)) {
vlm0b567bf2005-03-04 22:18:20 +0000982 tnfmt = TNF_RSAFE;
983 REDIR(OT_FWD_DECLS);
vlm5feb7522005-03-04 23:50:56 +0000984 OUT("%s;\n",
vlm0b567bf2005-03-04 22:18:20 +0000985 asn1c_type_name(arg, arg->expr, tnfmt));
vlma5dcb912004-09-29 13:16:40 +0000986 }
987 }
988
vlmfa67ddc2004-06-03 03:38:44 +0000989 REDIR(OT_TYPE_DECLS);
990
vlm0b567bf2005-03-04 22:18:20 +0000991 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
vlm80a48592005-02-25 12:10:27 +0000992 if(!expr->_anonymous_type) {
vlm0b567bf2005-03-04 22:18:20 +0000993 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
vlm1f7df782005-03-04 23:48:19 +0000994 OUT("%s", MKID_safe(expr->Identifier));
vlm80a48592005-02-25 12:10:27 +0000995 if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT)
996 OUT("\t/* DEFAULT %s */",
997 asn1f_printable_value(
998 expr->marker.default_value));
999 else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL)
1000 OUT("\t/* OPTIONAL */");
1001 }
1002
1003 } else {
1004 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
vlmb2839012004-08-20 13:37:01 +00001005
1006 REDIR(OT_TYPE_DECLS);
vlm80a48592005-02-25 12:10:27 +00001007
1008 OUT("typedef %s\t",
vlm0b567bf2005-03-04 22:18:20 +00001009 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
vlm80a48592005-02-25 12:10:27 +00001010 OUT("%s%s_t",
vlm0b567bf2005-03-04 22:18:20 +00001011 (expr->marker.flags & EM_INDIRECT)?"*":" ",
vlm1f7df782005-03-04 23:48:19 +00001012 MKID(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +00001013 }
1014
vlm80a48592005-02-25 12:10:27 +00001015 if((expr->expr_type == ASN_BASIC_ENUMERATED)
vlmc89422d2005-03-03 21:28:12 +00001016 || (0 /* -- prohibited by X.693:8.3.4 */
1017 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00001018 && expr_elements_count(arg, expr)))
1019 etd_spec = ETD_HAS_SPECIFICS;
1020 else
1021 etd_spec = ETD_NO_SPECIFICS;
vlmfa67ddc2004-06-03 03:38:44 +00001022
vlm86f5ed22004-09-26 13:11:31 +00001023 /*
1024 * If this type just blindly refers the other type, alias it.
1025 * Type1 ::= Type2
1026 */
vlm80a48592005-02-25 12:10:27 +00001027 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
1028 REDIR(OT_TYPE_DECLS);
1029 return 0;
1030 }
vlm86f5ed22004-09-26 13:11:31 +00001031 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlm80a48592005-02-25 12:10:27 +00001032 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
vlm6f74b0f2005-03-04 23:23:50 +00001033 && etd_spec == ETD_NO_SPECIFICS
1034 && 0 /* This shortcut is incompatible with XER */
1035 ) {
vlm86f5ed22004-09-26 13:11:31 +00001036 char *type_name;
1037 REDIR(OT_FUNC_DECLS);
1038 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
1039 OUT("/* This type is equivalent to %s */\n", type_name);
vlm86f5ed22004-09-26 13:11:31 +00001040 if(HIDE_INNER_DEFS) OUT("/* ");
vlm1f7df782005-03-04 23:48:19 +00001041 OUT("#define\tasn_DEF_%s\t", MKID(expr->Identifier));
vlm86f5ed22004-09-26 13:11:31 +00001042 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlm80a48592005-02-25 12:10:27 +00001043 OUT("asn_DEF_%s", type_name);
vlm86f5ed22004-09-26 13:11:31 +00001044 if(HIDE_INNER_DEFS)
vlm80a48592005-02-25 12:10:27 +00001045 OUT("\t// (Use -fall-defs-global to expose) */");
1046 OUT("\n");
vlm86f5ed22004-09-26 13:11:31 +00001047 REDIR(OT_CODE);
1048 OUT("/* This type is equivalent to %s */\n", type_name);
1049 OUT("\n");
1050 REDIR(OT_TYPE_DECLS);
1051 return 0;
1052 }
1053
vlmfa67ddc2004-06-03 03:38:44 +00001054 REDIR(OT_STAT_DEFS);
1055
vlm4a3f5822004-06-28 21:13:46 +00001056 /*
vlma5dcb912004-09-29 13:16:40 +00001057 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +00001058 */
vlm72425de2004-09-13 08:31:01 +00001059 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +00001060
vlm80a48592005-02-25 12:10:27 +00001061 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1062 0, etd_spec);
vlmfa67ddc2004-06-03 03:38:44 +00001063
vlmb2839012004-08-20 13:37:01 +00001064 REDIR(OT_CODE);
1065
vlmfa67ddc2004-06-03 03:38:44 +00001066 /*
1067 * Constraint checking.
1068 */
vlm86f5ed22004-09-26 13:11:31 +00001069 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1070 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001071 if(HIDE_INNER_DEFS) OUT("static ");
vlm86f5ed22004-09-26 13:11:31 +00001072 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001073 OUT("%s", p);
1074 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1075 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
vlm86f5ed22004-09-26 13:11:31 +00001076 INDENT(+1);
1077 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1078 OUT("\n");
1079 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1080 OUT("/* Replace with underlying type checker */\n");
1081 OUT("td->check_constraints "
vlma5dcb912004-09-29 13:16:40 +00001082 "= asn_DEF_%s.check_constraints;\n",
vlm86f5ed22004-09-26 13:11:31 +00001083 asn1c_type_name(arg, expr, TNF_SAFE));
1084 OUT("return td->check_constraints"
1085 "(td, sptr, app_errlog, app_key);\n");
vlm1d036692004-08-19 13:29:46 +00001086 }
vlm86f5ed22004-09-26 13:11:31 +00001087 INDENT(-1);
1088 OUT("}\n");
1089 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001090 }
vlmfa67ddc2004-06-03 03:38:44 +00001091
1092 /*
1093 * Emit suicidal functions.
1094 */
1095
vlmfa67ddc2004-06-03 03:38:44 +00001096 /*
1097 * This function replaces certain fields from the definition
1098 * of a type with the corresponding fields from the basic type
1099 * (from which the current type is inherited).
1100 */
vlmfa67ddc2004-06-03 03:38:44 +00001101 OUT("/*\n");
vlmb2839012004-08-20 13:37:01 +00001102 OUT(" * This type is implemented using %s,\n",
1103 asn1c_type_name(arg, expr, TNF_SAFE));
vlmdcd32fe2004-09-23 22:20:47 +00001104 OUT(" * so here we adjust the DEF accordingly.\n");
vlmfa67ddc2004-06-03 03:38:44 +00001105 OUT(" */\n");
1106 OUT("static void\n");
vlm766cf1e2005-03-04 08:48:53 +00001107 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
vlm1f7df782005-03-04 23:48:19 +00001108 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001109 INDENT(+1);
vlm80a48592005-02-25 12:10:27 +00001110 {
vlm39ba4c42004-09-22 16:06:28 +00001111 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001112 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlma5dcb912004-09-29 13:16:40 +00001113 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1114 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1115 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1116 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1117 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1118 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
vlm1308d2b2004-09-10 15:49:15 +00001119 if(!terminal && !tags_count) {
vlm72425de2004-09-13 08:31:01 +00001120 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
vlma5dcb912004-09-29 13:16:40 +00001121 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1122 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1123 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1124 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
vlm1308d2b2004-09-10 15:49:15 +00001125 OUT("/* End of these lines */\n");
1126 }
vlma5dcb912004-09-29 13:16:40 +00001127 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1128 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
vlm80a48592005-02-25 12:10:27 +00001129 if(etd_spec != ETD_NO_SPECIFICS) {
1130 INDENT(-1);
vlmc86870c2005-03-29 19:04:24 +00001131 OUT(" /* ");
vlmb2839012004-08-20 13:37:01 +00001132 }
vlm80a48592005-02-25 12:10:27 +00001133 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1134 if(etd_spec == ETD_NO_SPECIFICS) {
1135 INDENT(-1);
1136 OUT("\n");
1137 } else {
1138 OUT("\t// Defined explicitly */\n");
1139 }
1140 }
vlmfa67ddc2004-06-03 03:38:44 +00001141 OUT("}\n");
1142 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001143
1144 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001145 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001146 OUT("void\n");
vlm766cf1e2005-03-04 08:48:53 +00001147 OUT("%s", p);
1148 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1149 OUT("_free(asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001150 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001151 OUT("\tvoid *struct_ptr, int contents_only) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001152 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1153 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001154 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001155 );
1156 OUT("}\n");
1157 OUT("\n");
1158
1159 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001160 if(HIDE_INNER_DEFS) OUT("static ");
vlmfa67ddc2004-06-03 03:38:44 +00001161 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001162 OUT("%s", p);
1163 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1164 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001165 INDENTED(
1166 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001167 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1168 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001169 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1170 );
1171 OUT("}\n");
1172 OUT("\n");
1173
1174 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001175 if(HIDE_INNER_DEFS) OUT("static ");
vlm9de248e2004-10-20 15:50:55 +00001176 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001177 OUT("%s", p);
1178 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1179 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001180 INDENTED(
vlmfcc4cbc2005-03-17 21:56:00 +00001181 OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001182 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1183 p, expr->_type_unique_index);
vlma5dcb912004-09-29 13:16:40 +00001184 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
vlm39ba4c42004-09-22 16:06:28 +00001185 );
1186 OUT("}\n");
1187 OUT("\n");
1188
1189 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001190 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001191 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001192 OUT("%s", p);
1193 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1194 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
vlm39ba4c42004-09-22 16:06:28 +00001195 INDENTED(
1196 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1197 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001198 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1199 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001200 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1201 );
1202 OUT("}\n");
1203 OUT("\n");
1204
1205 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001206 if(HIDE_INNER_DEFS) OUT("static ");
vlme03646b2004-10-23 13:34:00 +00001207 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001208 OUT("%s", p);
1209 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1210 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlme03646b2004-10-23 13:34:00 +00001211 INDENTED(
vlmfcc4cbc2005-03-17 21:56:00 +00001212 OUT("\tvoid **structure, const char *opt_mname, const void *bufptr, size_t size) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001213 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1214 p, expr->_type_unique_index);
vlme03646b2004-10-23 13:34:00 +00001215 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1216 );
1217 OUT("}\n");
1218 OUT("\n");
1219
1220 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001221 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001222 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001223 OUT("%s", p);
1224 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1225 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
vlm39ba4c42004-09-22 16:06:28 +00001226 INDENTED(
1227 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1228 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001229 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1230 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001231 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001232 );
1233 OUT("}\n");
1234 OUT("\n");
1235
1236 REDIR(OT_FUNC_DECLS);
1237
vlm1f7df782005-03-04 23:48:19 +00001238 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001239 if(HIDE_INNER_DEFS) {
vlm766cf1e2005-03-04 08:48:53 +00001240 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1241 "\t// (Use -fall-defs-global to expose) */\n",
1242 p, expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +00001243 } else {
1244 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1245 OUT("asn_struct_free_f %s_free;\n", p);
1246 OUT("asn_struct_print_f %s_print;\n", p);
1247 OUT("asn_constr_check_f %s_constraint;\n", p);
1248 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1249 OUT("der_type_encoder_f %s_encode_der;\n", p);
1250 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1251 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1252 }
vlmfa67ddc2004-06-03 03:38:44 +00001253
vlm33a4ff12004-08-11 05:21:32 +00001254 REDIR(OT_TYPE_DECLS);
1255
vlmfa67ddc2004-06-03 03:38:44 +00001256 return 0;
1257}
1258
1259int
1260asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1261
1262 OUT("/*\n");
1263 OUT(" * This type is extensible,\n");
1264 OUT(" * possible extensions are below.\n");
1265 OUT(" */\n");
1266
1267 return 0;
1268}
1269
vlm79b08d52004-07-01 00:52:50 +00001270static int check_if_extensible(asn1p_expr_t *expr) {
1271 asn1p_expr_t *v;
1272 TQ_FOR(v, &(expr->members), next) {
1273 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1274 }
1275 return 0;
1276}
1277
vlmfa67ddc2004-06-03 03:38:44 +00001278static int
vlma8a86cc2004-09-10 06:07:18 +00001279_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
vlmfa67ddc2004-06-03 03:38:44 +00001280
1281 OUT("(");
vlma8a86cc2004-09-10 06:07:18 +00001282 switch(tag->tag_class) {
vlmfa67ddc2004-06-03 03:38:44 +00001283 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1284 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1285 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1286 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1287 case TC_NOCLASS:
1288 break;
1289 }
vlm47ae1582004-09-24 21:01:43 +00001290 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +00001291
1292 return 0;
1293}
1294
vlm4e03ce22004-06-06 07:20:17 +00001295
1296static int
1297_tag2el_cmp(const void *ap, const void *bp) {
1298 const tag2el_t *a = ap;
1299 const tag2el_t *b = bp;
1300 const struct asn1p_type_tag_s *ta = &a->el_tag;
1301 const struct asn1p_type_tag_s *tb = &b->el_tag;
1302
1303 if(ta->tag_class == tb->tag_class) {
1304 if(ta->tag_value == tb->tag_value) {
1305 /*
1306 * Sort by their respective positions.
1307 */
1308 if(a->el_no < b->el_no)
1309 return -1;
1310 else if(a->el_no > b->el_no)
1311 return 1;
1312 return 0;
1313 } else if(ta->tag_value < tb->tag_value)
1314 return -1;
1315 else
1316 return 1;
1317 } else if(ta->tag_class < tb->tag_class) {
1318 return -1;
1319 } else {
1320 return 1;
1321 }
1322}
1323
vlmfa67ddc2004-06-03 03:38:44 +00001324/*
1325 * For constructed types, number of external tags may be greater than
1326 * number of elements in the type because of CHOICE type.
1327 * T ::= SET { -- Three possible tags:
1328 * a INTEGER, -- One tag is here...
1329 * b Choice1 -- ... and two more tags are there.
1330 * }
1331 * Choice1 ::= CHOICE {
1332 * s1 IA5String,
1333 * s2 ObjectDescriptor
1334 * }
1335 */
1336static int
vlm940bc6b2004-10-03 09:13:30 +00001337_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001338 asn1p_expr_t *expr = arg->expr;
1339 arg_t tmparg = *arg;
1340 asn1p_expr_t *v;
1341 int element = 0;
vlm940bc6b2004-10-03 09:13:30 +00001342 int original_count = *count;
1343 int sort_until = -1;
vlmfa67ddc2004-06-03 03:38:44 +00001344
1345 TQ_FOR(v, &(expr->members), next) {
vlm940bc6b2004-10-03 09:13:30 +00001346 if(v->expr_type == A1TC_EXTENSIBLE) {
1347 /*
vlm80a48592005-02-25 12:10:27 +00001348 * CXER mandates sorting
vlm940bc6b2004-10-03 09:13:30 +00001349 * only for the root part.
1350 */
1351 if(flags == FTE_CANONICAL_XER
1352 && sort_until == -1)
1353 sort_until = *count;
vlmfa67ddc2004-06-03 03:38:44 +00001354 continue;
vlm940bc6b2004-10-03 09:13:30 +00001355 }
vlmfa67ddc2004-06-03 03:38:44 +00001356
1357 tmparg.expr = v;
1358
1359 if(_add_tag2el_member(&tmparg, tag2el, count,
vlm940bc6b2004-10-03 09:13:30 +00001360 (el_no==-1)?element:el_no, flags)) {
vlmfa67ddc2004-06-03 03:38:44 +00001361 return -1;
1362 }
1363
1364 element++;
1365 }
1366
vlm940bc6b2004-10-03 09:13:30 +00001367
1368 if(flags == FTE_CANONICAL_XER) {
1369 if(sort_until == -1) sort_until = *count;
1370 qsort((*tag2el) + original_count,
1371 sort_until - original_count,
1372 sizeof(**tag2el), _tag2el_cmp);
1373 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1374 && (sort_until - original_count) >= 1) {
1375 /* Only take in account the root component */
1376 *count = original_count + 1;
1377 }
1378 } else {
1379 /*
1380 * Sort the map according to canonical order of their
1381 * tags and element numbers.
1382 */
1383 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1384 }
vlm4e03ce22004-06-06 07:20:17 +00001385
vlmc8aeab42004-06-14 13:09:45 +00001386 /*
1387 * Initialize .toff_{first|last} members.
1388 */
1389 if(*count) {
1390 struct asn1p_type_tag_s *cur_tag = 0;
1391 tag2el_t *cur = *tag2el;
1392 tag2el_t *end = cur + *count;
1393 int occur, i;
1394 for(occur = 0; cur < end; cur++) {
1395 if(cur_tag == 0
1396 || cur_tag->tag_value != cur->el_tag.tag_value
1397 || cur_tag->tag_class != cur->el_tag.tag_class) {
1398 cur_tag = &cur->el_tag;
1399 occur = 0;
1400 } else {
1401 occur++;
1402 }
1403 cur->toff_first = -occur;
1404 for(i = 0; i >= -occur; i--)
1405 cur[i].toff_last = -i;
1406 }
1407 }
1408
vlmfa67ddc2004-06-03 03:38:44 +00001409 return 0;
1410}
1411
1412static int
vlm940bc6b2004-10-03 09:13:30 +00001413_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001414 struct asn1p_type_tag_s tag;
1415 int ret;
1416
1417 assert(el_no >= 0);
1418
vlm39ba4c42004-09-22 16:06:28 +00001419 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1420 arg->expr, &tag, 1);
vlmfa67ddc2004-06-03 03:38:44 +00001421 if(ret == 0) {
vlme2070ea2004-09-04 04:42:29 +00001422 tag2el_t *te;
1423 int new_count = (*count) + 1;
vlmfa67ddc2004-06-03 03:38:44 +00001424 void *p;
vlme2070ea2004-09-04 04:42:29 +00001425
vlm2de0e792004-09-05 10:42:33 +00001426 if(tag.tag_value == -1) {
1427 /*
1428 * This is an untagged ANY type,
1429 * proceed without adding a tag
1430 */
1431 return 0;
1432 }
1433
vlme2070ea2004-09-04 04:42:29 +00001434 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
vlmfa67ddc2004-06-03 03:38:44 +00001435 if(p) *tag2el = p;
1436 else return -1;
1437
1438 DEBUG("Found tag for %s: %ld",
1439 arg->expr->Identifier,
1440 (long)tag.tag_value);
1441
vlme2070ea2004-09-04 04:42:29 +00001442 te = &((*tag2el)[*count]);
1443 te->el_tag = tag;
1444 te->el_no = el_no;
1445 te->from_expr = arg->expr;
1446 *count = new_count;
vlmfa67ddc2004-06-03 03:38:44 +00001447 return 0;
1448 }
1449
1450 DEBUG("Searching tag in complex expression %s:%x at line %d",
1451 arg->expr->Identifier,
1452 arg->expr->expr_type,
1453 arg->expr->_lineno);
1454
1455 /*
1456 * Iterate over members of CHOICE type.
1457 */
1458 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
vlm940bc6b2004-10-03 09:13:30 +00001459 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001460 }
1461
1462 if(arg->expr->expr_type == A1TC_REFERENCE) {
1463 arg_t tmp = *arg;
1464 asn1p_expr_t *expr;
vlmdae7f9d2004-08-22 03:25:24 +00001465 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
vlmfa67ddc2004-06-03 03:38:44 +00001466 arg->expr->reference);
1467 if(expr) {
vlmdae7f9d2004-08-22 03:25:24 +00001468 tmp.mod = expr->module;
vlmfa67ddc2004-06-03 03:38:44 +00001469 tmp.expr = expr;
vlm940bc6b2004-10-03 09:13:30 +00001470 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001471 } else {
1472 FATAL("Cannot dereference %s at line %d",
1473 arg->expr->Identifier,
1474 arg->expr->_lineno);
1475 return -1;
1476 }
1477 }
1478
1479 DEBUG("No tag for %s at line %d",
1480 arg->expr->Identifier,
1481 arg->expr->_lineno);
1482
1483 return -1;
1484}
1485
1486static int
vlm940bc6b2004-10-03 09:13:30 +00001487emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
vlm4e03ce22004-06-06 07:20:17 +00001488 asn1p_expr_t *expr = arg->expr;
vlmfbd6d9e2005-06-02 05:21:37 +00001489 int i;
1490
1491 if(!tag2el_count) return 0; /* No top level tags */
vlm4e03ce22004-06-06 07:20:17 +00001492
vlm766cf1e2005-03-04 08:48:53 +00001493 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
vlm1f7df782005-03-04 23:48:19 +00001494 MKID(expr->Identifier), expr->_type_unique_index,
vlm766cf1e2005-03-04 08:48:53 +00001495 opt_modifier?opt_modifier:"");
vlmfbd6d9e2005-06-02 05:21:37 +00001496 for(i = 0; i < tag2el_count; i++) {
1497 OUT(" { ");
1498 _print_tag(arg, &tag2el[i].el_tag);
1499 OUT(", ");
1500 OUT("%d, ", tag2el[i].el_no);
1501 OUT("%d, ", tag2el[i].toff_first);
1502 OUT("%d ", tag2el[i].toff_last);
1503 OUT("}%s /* %s at %d */\n",
1504 (i + 1 < tag2el_count) ? "," : "",
1505 tag2el[i].from_expr->Identifier,
1506 tag2el[i].from_expr->_lineno
1507 );
vlm4e03ce22004-06-06 07:20:17 +00001508 }
1509 OUT("};\n");
1510
vlmfbd6d9e2005-06-02 05:21:37 +00001511 return 0;
vlm4e03ce22004-06-06 07:20:17 +00001512}
1513
vlm72425de2004-09-13 08:31:01 +00001514static enum tvm_compat
1515emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1516 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1517 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
vlm6e73a042004-08-11 07:17:22 +00001518 int tags_count = 0;
vlm72425de2004-09-13 08:31:01 +00001519 int all_tags_count = 0;
1520 enum tvm_compat tv_mode = _TVM_SAME;
vlm1308d2b2004-09-10 15:49:15 +00001521 int i;
vlm4a3f5822004-06-28 21:13:46 +00001522
vlmc6de2c42004-09-14 14:10:10 +00001523 /* Cleanup before proceeding. */
1524 *tags_count_r = 0;
1525 *all_tags_count_r = 0;
1526
vlm1308d2b2004-09-10 15:49:15 +00001527 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001528 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
vlmc6de2c42004-09-14 14:10:10 +00001529 if(tags_count < 0)
1530 return -1;
vlm6e73a042004-08-11 07:17:22 +00001531
vlm72425de2004-09-13 08:31:01 +00001532 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001533 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
vlm72425de2004-09-13 08:31:01 +00001534 &all_tags, AFT_FULL_COLLECT);
1535 if(all_tags_count < 0) {
1536 if(tags) free(tags);
1537 return -1;
vlmfb41dbb2004-09-08 00:28:57 +00001538 }
vlm1308d2b2004-09-10 15:49:15 +00001539
vlm72425de2004-09-13 08:31:01 +00001540 assert(tags_count <= all_tags_count);
1541 assert((tags_count?0:1) == (all_tags_count?0:1));
vlm4a3f5822004-06-28 21:13:46 +00001542
vlm72425de2004-09-13 08:31:01 +00001543 if(tags_count <= all_tags_count) {
1544 for(i = 0; i < tags_count; i++) {
1545 if(tags[i].tag_value != all_tags[i].tag_value
1546 || tags[i].tag_class != all_tags[i].tag_class) {
1547 tv_mode = _TVM_DIFFERENT;
1548 break;
1549 }
1550 }
1551 if(i == tags_count && tags_count < all_tags_count)
1552 tv_mode = _TVM_SUBSET;
1553 } else {
1554 tv_mode = _TVM_DIFFERENT;
1555 }
1556
1557#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
vlm766cf1e2005-03-04 08:48:53 +00001558 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
vlm1f7df782005-03-04 23:48:19 +00001559 MKID(expr->Identifier), \
vlm766cf1e2005-03-04 08:48:53 +00001560 expr->_type_unique_index, name); \
vlm72425de2004-09-13 08:31:01 +00001561 INDENT(+1); \
1562 /* Print the array of collected tags */ \
1563 for(i = 0; i < tags_count; i++) { \
1564 if(i) OUT(",\n"); \
1565 _print_tag(arg, &tags[i]); \
1566 } \
1567 OUT("\n"); \
1568 INDENT(-1); \
1569 OUT("};\n"); \
1570 } while(0)
1571
1572 if(tags_count) {
1573 if(tv_mode == _TVM_SUBSET)
1574 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1575 else
1576 EMIT_TAGS_TABLE("", tags, tags_count);
1577 }
1578
1579 if(all_tags_count) {
1580 if(tv_mode == _TVM_DIFFERENT)
1581 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1582 }
1583
1584 if(tags) free(tags);
1585 if(all_tags) free(all_tags);
1586
1587 *tags_count_r = tags_count;
1588 *all_tags_count_r = all_tags_count;
1589
1590 return tv_mode;
vlm4a3f5822004-06-28 21:13:46 +00001591}
vlmb2839012004-08-20 13:37:01 +00001592
1593static int
vlm4e554992004-08-25 02:03:12 +00001594expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001595 asn1p_expr_t *topmost_parent;
1596 asn1p_expr_t *v;
1597 int elements = 0;
1598
vlm39ba4c42004-09-22 16:06:28 +00001599 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001600 if(!topmost_parent) return 0;
1601
vlm80a48592005-02-25 12:10:27 +00001602 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1603 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1604 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
vlmb2839012004-08-20 13:37:01 +00001605 return 0;
1606
1607 TQ_FOR(v, &(topmost_parent->members), next) {
1608 if(v->expr_type != A1TC_EXTENSIBLE)
1609 elements++;
1610 }
1611
1612 return elements;
1613}
1614
1615static int
vlm0b567bf2005-03-04 22:18:20 +00001616emit_include_dependencies(arg_t *arg) {
1617 asn1p_expr_t *expr = arg->expr;
1618 asn1p_expr_t *memb;
1619
1620 TQ_FOR(memb, &(expr->members), next) {
1621
vlm5a6fc652005-08-16 17:00:21 +00001622 /* Avoid recursive definitions. */
1623 expr_break_recursion(arg, memb);
1624
1625 if(memb->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
vlm0b567bf2005-03-04 22:18:20 +00001626 asn1p_expr_t *terminal;
1627 terminal = asn1f_find_terminal_type_ex(arg->asn, memb);
vlm5a6fc652005-08-16 17:00:21 +00001628 if(terminal
1629 && !terminal->parent_expr
1630 && (terminal->expr_type & ASN_CONSTR_MASK)) {
vlm0b567bf2005-03-04 22:18:20 +00001631 int saved_target = arg->target->target;
1632 REDIR(OT_FWD_DECLS);
vlm5feb7522005-03-04 23:50:56 +00001633 OUT("%s;\n",
vlm0b567bf2005-03-04 22:18:20 +00001634 asn1c_type_name(arg, memb, TNF_RSAFE));
1635 REDIR(saved_target);
vlm0b567bf2005-03-04 22:18:20 +00001636 }
1637 }
1638
1639 if((!(memb->expr_type & ASN_CONSTR_MASK)
1640 && memb->expr_type > ASN_CONSTR_MASK)
1641 || memb->meta_type == AMT_TYPEREF) {
1642 if(memb->marker.flags & EM_UNRECURSE) {
1643 GEN_POSTINCLUDE(asn1c_type_name(arg,
1644 memb, TNF_INCLUDE));
1645 } else {
1646 GEN_INCLUDE(asn1c_type_name(arg,
1647 memb, TNF_INCLUDE));
1648 }
1649 }
1650 }
1651
1652 return 0;
1653}
1654
vlm5a6fc652005-08-16 17:00:21 +00001655/*
1656 * Check if it is better to make this type indirectly accessed via
1657 * a pointer.
1658 * This may be the case for the following recursive definition:
1659 * Type ::= CHOICE { member Type };
1660 */
1661static int
1662expr_break_recursion(arg_t *arg, asn1p_expr_t *expr) {
1663 asn1p_expr_t *top_parent;
1664 asn1p_expr_t *terminal;
1665
1666 if(expr->marker.flags & EM_UNRECURSE)
1667 return 1; /* Already broken */
1668
1669 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
1670
1671 /* -findirect-choice compiles members of CHOICE as indirect pointers */
1672 if((arg->flags & A1C_INDIRECT_CHOICE)
1673 && arg->expr->expr_type == ASN_CONSTR_CHOICE
1674 && terminal
1675 && (terminal->expr_type & ASN_CONSTR_MASK)
1676 ) {
1677 /* Break cross-reference */
1678 expr->marker.flags |= EM_INDIRECT | EM_UNRECURSE;
1679 return 1;
1680 }
1681
1682 if((expr->marker.flags & EM_INDIRECT)
1683 || arg->expr->expr_type == ASN_CONSTR_SET_OF
1684 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) {
1685 if(terminal
1686 && !terminal->parent_expr
1687 && (terminal->expr_type & ASN_CONSTR_MASK)) {
1688 expr->marker.flags |= EM_UNRECURSE;
1689
1690 if(arg->expr->expr_type == ASN_CONSTR_SET_OF
1691 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) {
1692 /* Don't put EM_INDIRECT even if recursion */
1693 return 1;
1694 }
1695
1696 /* Fall through */
1697 }
1698 }
1699
1700 /* Look for recursive back-references */
1701 top_parent = expr->parent_expr;
1702 if(top_parent) {
1703 while(top_parent->parent_expr)
1704 top_parent = top_parent->parent_expr;
1705 if(top_parent == terminal) {
1706 /* Explicitly break the recursion */
1707 expr->marker.flags |= EM_INDIRECT | EM_UNRECURSE;
1708 return 1;
1709 }
1710 }
1711
1712 return 0;
1713}
1714
vlm0b567bf2005-03-04 22:18:20 +00001715static int
vlm4e554992004-08-25 02:03:12 +00001716emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001717 int save_target;
1718 arg_t tmp_arg;
vlm060fe2a2004-09-10 09:37:12 +00001719 struct asn1p_type_tag_s outmost_tag_s;
1720 struct asn1p_type_tag_s *outmost_tag;
vlm80a48592005-02-25 12:10:27 +00001721 int complex_contents;
vlmb2839012004-08-20 13:37:01 +00001722 char *p;
1723
vlm060fe2a2004-09-10 09:37:12 +00001724 if(asn1f_fetch_outmost_tag(arg->asn,
1725 expr->module, expr, &outmost_tag_s, 1)) {
1726 outmost_tag = 0;
1727 } else {
1728 outmost_tag = &outmost_tag_s;
1729 }
1730
vlmb2839012004-08-20 13:37:01 +00001731 OUT("{ ");
vlm060fe2a2004-09-10 09:37:12 +00001732
1733 if(outmost_tag && outmost_tag->tag_value == -1)
1734 OUT("ATF_OPEN_TYPE | ");
vlm0b567bf2005-03-04 22:18:20 +00001735 OUT("%s, ",
1736 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
vlma02b74d2004-09-15 11:54:38 +00001737 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
vlmb2839012004-08-20 13:37:01 +00001738 asn1p_expr_t *tv;
1739 int opts = 0;
vlma02b74d2004-09-15 11:54:38 +00001740 for(tv = expr; tv && tv->marker.flags;
vlmb2839012004-08-20 13:37:01 +00001741 tv = TQ_NEXT(tv, next), opts++) {
1742 if(tv->expr_type == A1TC_EXTENSIBLE)
1743 opts--;
1744 }
1745 OUT("%d, ", opts);
1746 } else {
1747 OUT("0, ");
1748 }
vlm39ba4c42004-09-22 16:06:28 +00001749 if(expr->_anonymous_type) {
1750 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1751 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1752 OUT("0,\n");
1753 } else {
vlm766cf1e2005-03-04 08:48:53 +00001754 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +00001755 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +00001756 OUT(", ");
vlmb2839012004-08-20 13:37:01 +00001757 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1758 && (!UNNAMED_UNIONS)) OUT("choice.");
vlm1f7df782005-03-04 23:48:19 +00001759 OUT("%s),\n", MKID_safe(expr->Identifier));
vlmb2839012004-08-20 13:37:01 +00001760 }
1761 INDENT(+1);
1762 if(C99_MODE) OUT(".tag = ");
vlm060fe2a2004-09-10 09:37:12 +00001763 if(outmost_tag) {
1764 if(outmost_tag->tag_value == -1)
1765 OUT("-1 /* Ambiguous tag (ANY?) */");
1766 else
1767 _print_tag(arg, outmost_tag);
vlma8a86cc2004-09-10 06:07:18 +00001768 } else {
vlm060fe2a2004-09-10 09:37:12 +00001769 OUT("-1 /* Ambiguous tag (CHOICE?) */");
vlma8a86cc2004-09-10 06:07:18 +00001770 }
vlm060fe2a2004-09-10 09:37:12 +00001771
vlmb2839012004-08-20 13:37:01 +00001772 OUT(",\n");
1773 if(C99_MODE) OUT(".tag_mode = ");
1774 if(expr->tag.tag_class) {
1775 if(expr->tag.tag_mode == TM_IMPLICIT)
1776 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1777 else
1778 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1779 } else {
1780 OUT("0,\n");
1781 }
vlm80a48592005-02-25 12:10:27 +00001782
1783 complex_contents =
1784 (expr->expr_type & ASN_CONSTR_MASK)
1785 || expr->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +00001786 || (0 /* -- prohibited by X.693:8.3.4 */
1787 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00001788 && expr_elements_count(arg, expr));
vlmb2839012004-08-20 13:37:01 +00001789 if(C99_MODE) OUT(".type = ");
vlm766cf1e2005-03-04 08:48:53 +00001790 OUT("(void *)&asn_DEF_");
1791 if(complex_contents) {
vlm1f7df782005-03-04 23:48:19 +00001792 OUT("%s", MKID(expr->Identifier));
vlm766cf1e2005-03-04 08:48:53 +00001793 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1794 OUT("_%d", expr->_type_unique_index);
vlmdae7f9d2004-08-22 03:25:24 +00001795 } else {
vlm766cf1e2005-03-04 08:48:53 +00001796 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
vlmdae7f9d2004-08-22 03:25:24 +00001797 }
vlm766cf1e2005-03-04 08:48:53 +00001798 OUT(",\n");
vlmb2839012004-08-20 13:37:01 +00001799 if(C99_MODE) OUT(".memb_constraints = ");
1800 if(expr->constraints) {
vlm86f5ed22004-09-26 13:11:31 +00001801 if(arg->flags & A1C_NO_CONSTRAINTS) {
1802 OUT("0,\t/* No check because of -fno-constraints */\n");
1803 } else {
vlm1f7df782005-03-04 23:48:19 +00001804 char *id = MKID(expr->Identifier);
vlm86f5ed22004-09-26 13:11:31 +00001805 if(expr->_anonymous_type
vlmbf3c5112005-02-14 20:41:29 +00001806 && !strcmp(expr->Identifier, "Member"))
vlm86f5ed22004-09-26 13:11:31 +00001807 id = asn1c_type_name(arg, expr, TNF_SAFE);
1808 OUT("memb_%s_%d_constraint,\n", id,
vlm766cf1e2005-03-04 08:48:53 +00001809 arg->expr->_type_unique_index);
vlm86f5ed22004-09-26 13:11:31 +00001810 }
vlmb2839012004-08-20 13:37:01 +00001811 } else {
vlm95a476e2005-01-17 12:16:58 +00001812 OUT("0,\t/* Defer constraints checking to the member type */\n");
vlmb2839012004-08-20 13:37:01 +00001813 }
1814 if(C99_MODE) OUT(".name = ");
vlmbf3c5112005-02-14 20:41:29 +00001815 if(1) {
1816 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1817 OUT("\"\"\n");
1818 else
1819 OUT("\"%s\"\n", expr->Identifier);
1820 } else {
1821 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1822 }
vlmb2839012004-08-20 13:37:01 +00001823 OUT("},\n");
1824 INDENT(-1);
1825
vlm86f5ed22004-09-26 13:11:31 +00001826 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlmb2839012004-08-20 13:37:01 +00001827 return 0;
1828
1829 save_target = arg->target->target;
1830 REDIR(OT_CODE);
1831
vlmbf3c5112005-02-14 20:41:29 +00001832 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
vlmb2839012004-08-20 13:37:01 +00001833 p = asn1c_type_name(arg, expr, TNF_SAFE);
vlm39ba4c42004-09-22 16:06:28 +00001834 else
vlm1f7df782005-03-04 23:48:19 +00001835 p = MKID(expr->Identifier);
vlmb2839012004-08-20 13:37:01 +00001836 OUT("static int\n");
vlm766cf1e2005-03-04 08:48:53 +00001837 OUT("memb_%s_%d_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n", p, arg->expr->_type_unique_index);
vlmb2839012004-08-20 13:37:01 +00001838 INDENT(+1);
1839 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1840 tmp_arg = *arg;
1841 tmp_arg.expr = expr;
1842 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
vlm86f5ed22004-09-26 13:11:31 +00001843 OUT("return td->check_constraints"
1844 "(td, sptr, app_errlog, app_key);\n");
vlmb2839012004-08-20 13:37:01 +00001845 }
1846 INDENT(-1);
1847 OUT("}\n");
1848 OUT("\n");
1849
1850 REDIR(save_target);
1851
1852 return 0;
1853}
vlm4e554992004-08-25 02:03:12 +00001854
vlm9de248e2004-10-20 15:50:55 +00001855/*
1856 * Generate "asn_DEF_XXX" type definition.
1857 */
vlm4e554992004-08-25 02:03:12 +00001858static int
vlm86f5ed22004-09-26 13:11:31 +00001859emit_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) {
vlm766cf1e2005-03-04 08:48:53 +00001860 int using_type_name = 0;
vlm4e554992004-08-25 02:03:12 +00001861 char *p;
1862
vlm12c8f692004-09-06 08:07:29 +00001863 if(HIDE_INNER_DEFS)
1864 OUT("static /* Use -fall-defs-global to expose */\n");
vlm1f7df782005-03-04 23:48:19 +00001865 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID(expr->Identifier));
vlm766cf1e2005-03-04 08:48:53 +00001866 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1867 OUT(" = {\n");
vlmaf841972005-01-28 12:18:50 +00001868 p = MKID(expr->Identifier);
vlm72425de2004-09-13 08:31:01 +00001869 INDENT(+1);
vlm4e554992004-08-25 02:03:12 +00001870 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm9de248e2004-10-20 15:50:55 +00001871 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001872
1873 if(expr->expr_type & ASN_CONSTR_MASK) {
vlm766cf1e2005-03-04 08:48:53 +00001874 using_type_name = 1;
vlm4e554992004-08-25 02:03:12 +00001875 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1876 }
1877
vlm766cf1e2005-03-04 08:48:53 +00001878#define FUNCREF(foo) do { \
1879 OUT("%s", p); \
1880 if(HIDE_INNER_DEFS && !using_type_name) \
1881 OUT("_%d", expr->_type_unique_index); \
1882 OUT("_" #foo ",\n"); \
1883} while(0)
1884
1885 FUNCREF(free);
1886 FUNCREF(print);
1887 FUNCREF(constraint);
1888 FUNCREF(decode_ber);
1889 FUNCREF(encode_der);
1890 FUNCREF(decode_xer);
1891 FUNCREF(encode_xer);
vlm4e554992004-08-25 02:03:12 +00001892
vlm4e554992004-08-25 02:03:12 +00001893 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1894 OUT("CHOICE_outmost_tag,\n");
1895 } else {
1896 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1897 }
1898
vlm1f7df782005-03-04 23:48:19 +00001899 p = MKID(expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001900 if(tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001901 OUT("asn_DEF_%s_%d_tags,\n",
1902 p, expr->_type_unique_index);
1903 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1904 p, expr->_type_unique_index);
1905 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1906 p, expr->_type_unique_index);
vlm72425de2004-09-13 08:31:01 +00001907 if(tv_mode == _TVM_SUBSET
1908 && tags_count != all_tags_count)
1909 OUT(" - %d", all_tags_count - tags_count);
1910 OUT(", /* %d */\n", tags_count);
vlm4e554992004-08-25 02:03:12 +00001911 } else {
vlm72425de2004-09-13 08:31:01 +00001912 OUT("0,\t/* No effective tags (pointer) */\n");
1913 OUT("0,\t/* No effective tags (count) */\n");
1914 }
1915
1916 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
vlm766cf1e2005-03-04 08:48:53 +00001917 OUT("asn_DEF_%s_%d_all_tags,\n",
1918 p, expr->_type_unique_index);
1919 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1920 p, expr->_type_unique_index);
1921 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1922 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001923 } else if(all_tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001924 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1925 p, expr->_type_unique_index);
1926 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1927 p, expr->_type_unique_index);
1928 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1929 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001930 } else {
1931 OUT("0,\t/* No tags (pointer) */\n");
1932 OUT("0,\t/* No tags (count) */\n");
vlm4e554992004-08-25 02:03:12 +00001933 }
1934
vlm4e554992004-08-25 02:03:12 +00001935 if(elements_count) {
vlm766cf1e2005-03-04 08:48:53 +00001936 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001937 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1938 || expr->expr_type == ASN_CONSTR_SET_OF) {
1939 OUT("%d,\t/* Single element */\n",
1940 elements_count);
1941 assert(elements_count == 1);
1942 } else {
1943 OUT("%d,\t/* Elements count */\n",
1944 elements_count);
1945 }
1946 } else {
vlm4e554992004-08-25 02:03:12 +00001947 if(expr_elements_count(arg, expr))
1948 OUT("0, 0,\t/* Defined elsewhere */\n");
1949 else
1950 OUT("0, 0,\t/* No members */\n");
1951 }
1952
1953 switch(spec) {
1954 case ETD_NO_SPECIFICS:
1955 OUT("0\t/* No specifics */\n");
1956 break;
1957 case ETD_HAS_SPECIFICS:
vlm766cf1e2005-03-04 08:48:53 +00001958 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1959 p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001960 }
vlm72425de2004-09-13 08:31:01 +00001961 INDENT(-1);
vlm4e554992004-08-25 02:03:12 +00001962 OUT("};\n");
1963 OUT("\n");
vlme2bb4432004-08-26 06:20:34 +00001964
1965 return 0;
vlm4e554992004-08-25 02:03:12 +00001966}
vlmddd5a7d2004-09-10 09:18:20 +00001967
vlm39ba4c42004-09-22 16:06:28 +00001968static int
1969expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1970 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1971 if(!expr) return 0;
1972
1973 /* X.680, 25.5, Table 5 */
1974 switch(expr->expr_type) {
vlm39ba4c42004-09-22 16:06:28 +00001975 case ASN_BASIC_BOOLEAN:
1976 case ASN_BASIC_ENUMERATED:
1977 case ASN_BASIC_NULL:
1978 return 1;
1979 default:
1980 return 0;
1981 }
1982}
vlmaf841972005-01-28 12:18:50 +00001983
1984static int
vlm2e774282005-08-14 15:03:31 +00001985out_name_chain(arg_t *arg, enum onc_flags onc_flags) {
vlmaf841972005-01-28 12:18:50 +00001986 asn1p_expr_t *expr = arg->expr;
1987 char *id;
1988
1989 assert(expr->Identifier);
1990
vlm2e774282005-08-14 15:03:31 +00001991 if((arg->flags & A1C_COMPOUND_NAMES
1992 || onc_flags & ONC_force_compound_name)
vlm766cf1e2005-03-04 08:48:53 +00001993 && ((expr->expr_type & ASN_CONSTR_MASK)
1994 || expr->expr_type == ASN_BASIC_ENUMERATED
vlm96853d82005-08-13 23:51:47 +00001995 || ((expr->expr_type == ASN_BASIC_INTEGER
1996 || expr->expr_type == ASN_BASIC_BIT_STRING)
vlm766cf1e2005-03-04 08:48:53 +00001997 && expr_elements_count(arg, expr))
1998 )
vlmaf841972005-01-28 12:18:50 +00001999 && expr->parent_expr
2000 && expr->parent_expr->Identifier) {
2001 arg_t tmparg = *arg;
2002
2003 tmparg.expr = expr->parent_expr;
vlm766cf1e2005-03-04 08:48:53 +00002004 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
2005
vlm2e774282005-08-14 15:03:31 +00002006 out_name_chain(&tmparg, onc_flags);
vlmaf841972005-01-28 12:18:50 +00002007
vlm92d3cad2005-03-05 00:08:41 +00002008 OUT("__"); /* a separator between id components */
vlm766cf1e2005-03-04 08:48:53 +00002009
vlmaf841972005-01-28 12:18:50 +00002010 /* Fall through */
2011 }
2012
vlm2e774282005-08-14 15:03:31 +00002013 if(onc_flags & ONC_avoid_keywords)
vlm1f7df782005-03-04 23:48:19 +00002014 id = MKID_safe(expr->Identifier);
vlmaf841972005-01-28 12:18:50 +00002015 else
vlm1f7df782005-03-04 23:48:19 +00002016 id = MKID(expr->Identifier);
vlmaf841972005-01-28 12:18:50 +00002017 OUT("%s", id);
2018
2019 return 0;
2020}