blob: 6725f6700180b84e60544f79aff452a3b84ccf90 [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);
vlmddd5a7d2004-09-10 09:18:20 +000039static int expr_better_indirect(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) {
274 if(v->expr_type == A1TC_EXTENSIBLE) {
275 if(comp_mode < 3) comp_mode++;
276 }
vlmddd5a7d2004-09-10 09:18:20 +0000277 if(comp_mode == 1
278 || expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000279 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000280 EMBED(v);
281 }
282
283 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000284 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000285 expr->_anonymous_type ? "" :
286 arg->embed
287 ? MKID_safe(expr->Identifier)
288 : MKID(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000289 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000290
291 return asn1c_lang_C_type_SEQUENCE_def(arg);
292}
293
294static int
295asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
296 asn1p_expr_t *expr = arg->expr;
297 asn1p_expr_t *v;
298 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000299 int ext_start = -1;
300 int ext_stop = -1;
vlm4e03ce22004-06-06 07:20:17 +0000301 tag2el_t *tag2el = NULL;
302 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000303 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000304 int all_tags_count;
305 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000306
vlm4e03ce22004-06-06 07:20:17 +0000307 /*
308 * Fetch every inner tag from the tag to elements map.
309 */
vlm940bc6b2004-10-03 09:13:30 +0000310 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlm4e03ce22004-06-06 07:20:17 +0000311 if(tag2el) free(tag2el);
312 return -1;
313 }
314
vlm33a4ff12004-08-11 05:21:32 +0000315 GEN_INCLUDE("constr_SEQUENCE");
316 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000317 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000318
vlm33a4ff12004-08-11 05:21:32 +0000319 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000320
321 /*
322 * Print out the table according to which the parsing is performed.
323 */
vlm39ba4c42004-09-22 16:06:28 +0000324 if(expr_elements_count(arg, expr)) {
325 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000326
vlm766cf1e2005-03-04 08:48:53 +0000327 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm1f7df782005-03-04 23:48:19 +0000328 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000329
330 elements = 0;
331 INDENTED(TQ_FOR(v, &(expr->members), next) {
332 if(v->expr_type == A1TC_EXTENSIBLE) {
333 if((++comp_mode) == 1)
334 ext_start = elements - 1;
335 else
336 ext_stop = elements - 1;
337 continue;
338 }
339 elements++;
340 emit_member_table(arg, v);
341 });
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 }
vlmddd5a7d2004-09-10 09:18:20 +0000437 if(comp_mode == 1
438 || expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000439 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000440 EMBED(v);
441 }
442
443 INDENTED(
vlm1f7df782005-03-04 23:48:19 +0000444 id = MKID(expr->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000445 OUT("\n");
446 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
447 id, id);
448 OUT("unsigned int _presence_map\n");
449 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
450 );
451
452 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000453 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000454 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000455 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000456
457 return asn1c_lang_C_type_SET_def(arg);
458}
459
vlmfa67ddc2004-06-03 03:38:44 +0000460static int
461asn1c_lang_C_type_SET_def(arg_t *arg) {
462 asn1p_expr_t *expr = arg->expr;
463 asn1p_expr_t *v;
464 int elements;
vlmfa67ddc2004-06-03 03:38:44 +0000465 tag2el_t *tag2el = NULL;
466 int tag2el_count = 0;
vlm940bc6b2004-10-03 09:13:30 +0000467 tag2el_t *tag2el_cxer = NULL;
468 int tag2el_cxer_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000469 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000470 int all_tags_count;
471 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000472 char *p;
473
474 /*
475 * Fetch every inner tag from the tag to elements map.
476 */
vlm940bc6b2004-10-03 09:13:30 +0000477 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000478 if(tag2el) free(tag2el);
479 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000480 }
vlm940bc6b2004-10-03 09:13:30 +0000481 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
482 if(tag2el) free(tag2el);
483 if(tag2el_cxer) free(tag2el_cxer);
484 return -1;
485 }
486 if(tag2el_cxer_count == tag2el_count
487 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
488 free(tag2el_cxer);
489 tag2el_cxer = 0;
490 }
vlmfa67ddc2004-06-03 03:38:44 +0000491
vlm33a4ff12004-08-11 05:21:32 +0000492 GEN_INCLUDE("constr_SET");
493 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000494 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000495
496 REDIR(OT_STAT_DEFS);
497
vlmfa67ddc2004-06-03 03:38:44 +0000498 /*
499 * Print out the table according to which the parsing is performed.
500 */
vlm39ba4c42004-09-22 16:06:28 +0000501 if(expr_elements_count(arg, expr)) {
502 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000503
vlm766cf1e2005-03-04 08:48:53 +0000504 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm1f7df782005-03-04 23:48:19 +0000505 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000506
507 elements = 0;
508 INDENTED(TQ_FOR(v, &(expr->members), next) {
509 if(v->expr_type == A1TC_EXTENSIBLE) {
510 if(comp_mode < 3) comp_mode++;
511 } else {
512 if(comp_mode == 1
513 || expr_better_indirect(arg, v))
514 v->marker.flags |= EM_INDIRECT;
515 elements++;
516 emit_member_table(arg, v);
517 }
518 });
519 OUT("};\n");
520 } else {
521 elements = 0;
522 }
vlmfa67ddc2004-06-03 03:38:44 +0000523
vlm4a3f5822004-06-28 21:13:46 +0000524 /*
vlma5dcb912004-09-29 13:16:40 +0000525 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000526 */
vlm72425de2004-09-13 08:31:01 +0000527 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000528
529 /*
530 * Tags to elements map.
531 */
vlm940bc6b2004-10-03 09:13:30 +0000532 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
533 if(tag2el_cxer)
534 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
vlmfa67ddc2004-06-03 03:38:44 +0000535
536 /*
537 * Emit a map of mandatory elements.
538 */
vlm766cf1e2005-03-04 08:48:53 +0000539 OUT("static uint8_t asn_MAP_%s_%d_mmap",
vlm1f7df782005-03-04 23:48:19 +0000540 MKID(expr->Identifier), expr->_type_unique_index);
541 p = MKID_safe(expr->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000542 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
vlm766cf1e2005-03-04 08:48:53 +0000543 OUT(" = {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000544 INDENTED(
545 if(elements) {
546 int delimit = 0;
547 int el = 0;
548 TQ_FOR(v, &(expr->members), next) {
549 if(v->expr_type == A1TC_EXTENSIBLE) continue;
550 if(delimit) {
551 OUT(",\n");
552 delimit = 0;
553 } else if(el) {
554 OUT(" | ");
555 }
vlmddd5a7d2004-09-10 09:18:20 +0000556 OUT("(%d << %d)",
vlma02b74d2004-09-15 11:54:38 +0000557 v->marker.flags?0:1,
vlmddd5a7d2004-09-10 09:18:20 +0000558 7 - (el % 8));
vlmfa67ddc2004-06-03 03:38:44 +0000559 if(el && (el % 8) == 0)
560 delimit = 1;
561 el++;
562 }
563 } else {
564 OUT("0");
565 }
566 );
567 OUT("\n");
568 OUT("};\n");
569
vlm766cf1e2005-03-04 08:48:53 +0000570 OUT("static asn_SET_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000571 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000572 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000573 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000574 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000575 OUT("),\n");
576 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000577 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000578 OUT(", _asn_ctx),\n");
579 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000580 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000581 OUT(", _presence_map),\n");
vlm1f7df782005-03-04 23:48:19 +0000582 p = MKID(expr->Identifier);
vlm766cf1e2005-03-04 08:48:53 +0000583 OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000584 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm940bc6b2004-10-03 09:13:30 +0000585 if(tag2el_cxer)
vlm766cf1e2005-03-04 08:48:53 +0000586 OUT("asn_MAP_%s_%d_tag2el_cxer,\n",
587 p, expr->_type_unique_index);
vlm940bc6b2004-10-03 09:13:30 +0000588 else
vlm766cf1e2005-03-04 08:48:53 +0000589 OUT("asn_MAP_%s_%d_tag2el,\t/* Same as above */\n",
590 p, expr->_type_unique_index);
591 OUT("%d,\t/* Count of tags in the CXER map */\n",
592 tag2el_cxer_count);
vlm79b08d52004-07-01 00:52:50 +0000593 OUT("%d,\t/* Whether extensible */\n",
594 check_if_extensible(expr));
vlm766cf1e2005-03-04 08:48:53 +0000595 OUT("(unsigned int *)asn_MAP_%s_%d_mmap\t/* Mandatory elements map */\n",
596 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000597 );
598 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000599
600 /*
vlma5dcb912004-09-29 13:16:40 +0000601 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000602 */
vlm72425de2004-09-13 08:31:01 +0000603 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000604 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000605
vlmfa67ddc2004-06-03 03:38:44 +0000606 REDIR(OT_TYPE_DECLS);
607
608 return 0;
vlmedf203f2005-01-17 11:57:48 +0000609} /* _SET_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000610
611int
vlmdae7f9d2004-08-22 03:25:24 +0000612asn1c_lang_C_type_SEx_OF(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000613 asn1p_expr_t *expr = arg->expr;
vlm0b567bf2005-03-04 22:18:20 +0000614 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
vlmfa67ddc2004-06-03 03:38:44 +0000615
616 DEPENDENCIES;
617
618 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000619 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000620 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000621 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000622 } else {
vlm1f7df782005-03-04 23:48:19 +0000623 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000624 }
625
vlmdae7f9d2004-08-22 03:25:24 +0000626 INDENT(+1);
627 OUT("A_%s_OF(",
628 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
629 ? "SET" : "SEQUENCE");
vlm80a48592005-02-25 12:10:27 +0000630 if(memb->expr_type & ASN_CONSTR_MASK
631 || ((memb->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +0000632 || (0 /* -- prohibited by X.693:8.3.4 */
633 && memb->expr_type == ASN_BASIC_INTEGER))
634 && expr_elements_count(arg, memb))) {
vlmdae7f9d2004-08-22 03:25:24 +0000635 arg_t tmp;
636 asn1p_expr_t tmp_memb;
637 arg->embed++;
638 tmp = *arg;
639 tmp.expr = &tmp_memb;
640 tmp_memb = *memb;
641 tmp_memb._anonymous_type = 1;
vlm39ba4c42004-09-22 16:06:28 +0000642 if(tmp_memb.Identifier == 0) {
vlm766cf1e2005-03-04 08:48:53 +0000643 tmp_memb.Identifier = "Member";
644 if(0)
vlm39ba4c42004-09-22 16:06:28 +0000645 tmp_memb.Identifier = strdup(
646 asn1c_make_identifier(0,
vlmbf3c5112005-02-14 20:41:29 +0000647 expr->Identifier, "Member", 0));
vlm39ba4c42004-09-22 16:06:28 +0000648 assert(tmp_memb.Identifier);
649 }
vlmdae7f9d2004-08-22 03:25:24 +0000650 tmp.default_cb(&tmp);
vlm39ba4c42004-09-22 16:06:28 +0000651 if(tmp_memb.Identifier != memb->Identifier)
vlm766cf1e2005-03-04 08:48:53 +0000652 if(0) free(tmp_memb.Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000653 arg->embed--;
654 assert(arg->target->target == OT_TYPE_DECLS);
655 } else {
vlm6c5d5e52005-03-04 22:38:22 +0000656 OUT("%s", asn1c_type_name(arg, memb,
657 (memb->marker.flags & EM_UNRECURSE)
658 ? TNF_RSAFE : TNF_CTYPE));
vlmfa67ddc2004-06-03 03:38:44 +0000659 }
vlmdae7f9d2004-08-22 03:25:24 +0000660 OUT(") list;\n");
661 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000662
663 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000664 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000665 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000666 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000667
668 /*
vlmdae7f9d2004-08-22 03:25:24 +0000669 * SET OF/SEQUENCE OF definition
vlmfa67ddc2004-06-03 03:38:44 +0000670 */
vlmdae7f9d2004-08-22 03:25:24 +0000671 return asn1c_lang_C_type_SEx_OF_def(arg,
672 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
vlmfa67ddc2004-06-03 03:38:44 +0000673}
674
675static int
676asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
677 asn1p_expr_t *expr = arg->expr;
678 asn1p_expr_t *v;
vlm6e73a042004-08-11 07:17:22 +0000679 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000680 int all_tags_count;
681 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000682
vlm33a4ff12004-08-11 05:21:32 +0000683 /*
684 * Print out the table according to which the parsing is performed.
685 */
vlmfa67ddc2004-06-03 03:38:44 +0000686 if(seq_of) {
vlm33a4ff12004-08-11 05:21:32 +0000687 GEN_INCLUDE("constr_SEQUENCE_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000688 } else {
vlm33a4ff12004-08-11 05:21:32 +0000689 GEN_INCLUDE("constr_SET_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000690 }
vlm33a4ff12004-08-11 05:21:32 +0000691 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000692 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000693
694 REDIR(OT_STAT_DEFS);
695
696 /*
697 * Print out the table according to which the parsing is performed.
698 */
vlm766cf1e2005-03-04 08:48:53 +0000699 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
700 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000701 INDENT(+1);
vlmfa67ddc2004-06-03 03:38:44 +0000702 v = TQ_FIRST(&(expr->members));
vlm39ba4c42004-09-22 16:06:28 +0000703 if(!v->Identifier) {
vlmbf3c5112005-02-14 20:41:29 +0000704 v->Identifier = strdup("Member");
vlm39ba4c42004-09-22 16:06:28 +0000705 assert(v->Identifier);
706 }
707 v->_anonymous_type = 1;
708 arg->embed++;
vlm4e554992004-08-25 02:03:12 +0000709 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000710 arg->embed--;
711 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000712 OUT("};\n");
713
vlm4a3f5822004-06-28 21:13:46 +0000714 /*
vlma5dcb912004-09-29 13:16:40 +0000715 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000716 */
vlm72425de2004-09-13 08:31:01 +0000717 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000718
vlm766cf1e2005-03-04 08:48:53 +0000719 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000720 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000721 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000722 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000723 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000724 OUT("),\n");
725 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000726 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000727 OUT(", _asn_ctx),\n");
728
vlm39ba4c42004-09-22 16:06:28 +0000729 if(expr_as_xmlvaluelist(arg, v))
730 OUT("1,\t/* XER encoding is XMLValueList */\n");
731 else
732 OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n");
vlmfa67ddc2004-06-03 03:38:44 +0000733 );
734 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000735
736 /*
vlma5dcb912004-09-29 13:16:40 +0000737 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000738 */
vlm72425de2004-09-13 08:31:01 +0000739 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
vlm86f5ed22004-09-26 13:11:31 +0000740 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000741
vlmfa67ddc2004-06-03 03:38:44 +0000742 REDIR(OT_TYPE_DECLS);
743
744 return 0;
vlmedf203f2005-01-17 11:57:48 +0000745} /* _SEx_OF_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000746
747int
748asn1c_lang_C_type_CHOICE(arg_t *arg) {
749 asn1p_expr_t *expr = arg->expr;
750 asn1p_expr_t *v;
vlmdae7f9d2004-08-22 03:25:24 +0000751 char *id;
vlmfa67ddc2004-06-03 03:38:44 +0000752
753 DEPENDENCIES;
754
vlm33a4ff12004-08-11 05:21:32 +0000755 REDIR(OT_DEPS);
756
vlm766cf1e2005-03-04 08:48:53 +0000757 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000758 out_name_chain(arg, ONC_noflags);
vlm766cf1e2005-03-04 08:48:53 +0000759 OUT("_PR {\n");
vlm33a4ff12004-08-11 05:21:32 +0000760 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000761 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000762 OUT("_PR_NOTHING,\t/* No components present */\n");
vlm33a4ff12004-08-11 05:21:32 +0000763 TQ_FOR(v, &(expr->members), next) {
764 if(v->expr_type == A1TC_EXTENSIBLE) {
765 OUT("/* Extensions may appear below */\n");
766 continue;
767 }
vlm2e774282005-08-14 15:03:31 +0000768 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000769 OUT("_PR_");
vlm1f7df782005-03-04 23:48:19 +0000770 id = MKID(v->Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000771 OUT("%s,\n", id, id);
vlm33a4ff12004-08-11 05:21:32 +0000772 }
773 );
vlm2e774282005-08-14 15:03:31 +0000774 OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n");
vlm33a4ff12004-08-11 05:21:32 +0000775
776 REDIR(OT_TYPE_DECLS);
vlmfa67ddc2004-06-03 03:38:44 +0000777
778 if(arg->embed) {
vlm2e774282005-08-14 15:03:31 +0000779 OUT("struct "); out_name_chain(arg, ONC_avoid_keywords); OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000780 } else {
vlm1f7df782005-03-04 23:48:19 +0000781 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000782 }
783
784 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000785 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000786 OUT("_PR present;\n");
vlm2e774282005-08-14 15:03:31 +0000787 OUT("union ");
788 if(UNNAMED_UNIONS == 0) {
789 out_name_chain(arg, ONC_force_compound_name);
790 OUT("_u ");
791 }
792 OUT("{\n");
vlmfa67ddc2004-06-03 03:38:44 +0000793 TQ_FOR(v, &(expr->members), next) {
vlmddd5a7d2004-09-10 09:18:20 +0000794 if(expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000795 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000796 EMBED(v);
797 }
798 if(UNNAMED_UNIONS) OUT("};\n");
799 else OUT("} choice;\n");
800 );
801
802 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000803 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000804 expr->_anonymous_type ? "" :
805 arg->embed
806 ? MKID_safe(expr->Identifier)
807 : MKID(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000808 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000809
810 return asn1c_lang_C_type_CHOICE_def(arg);
811}
812
813static int
814asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
815 asn1p_expr_t *expr = arg->expr;
816 asn1p_expr_t *v;
817 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000818 tag2el_t *tag2el = NULL;
819 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000820 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000821 int all_tags_count;
822 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000823
824 /*
825 * Fetch every inner tag from the tag to elements map.
826 */
vlm940bc6b2004-10-03 09:13:30 +0000827 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000828 if(tag2el) free(tag2el);
829 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000830 }
831
vlm33a4ff12004-08-11 05:21:32 +0000832 GEN_INCLUDE("constr_CHOICE");
833 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000834 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000835
vlm33a4ff12004-08-11 05:21:32 +0000836 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000837
838 /*
839 * Print out the table according to which the parsing is performed.
840 */
vlm39ba4c42004-09-22 16:06:28 +0000841 if(expr_elements_count(arg, expr)) {
vlmfa67ddc2004-06-03 03:38:44 +0000842
vlm766cf1e2005-03-04 08:48:53 +0000843 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
844 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000845
846 elements = 0;
847 INDENTED(TQ_FOR(v, &(expr->members), next) {
vlm34dcd572005-01-17 11:40:49 +0000848 if(v->expr_type == A1TC_EXTENSIBLE)
849 continue;
850 if(expr_better_indirect(arg, v))
851 v->marker.flags |= EM_INDIRECT;
852 elements++;
853 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000854 });
855 OUT("};\n");
856 } else {
857 elements = 0;
858 }
vlmfa67ddc2004-06-03 03:38:44 +0000859
vlm6e73a042004-08-11 07:17:22 +0000860
vlmfa67ddc2004-06-03 03:38:44 +0000861 if(arg->embed) {
862 /*
863 * Our parent structure has already taken this into account.
864 */
vlm72425de2004-09-13 08:31:01 +0000865 tv_mode = _TVM_SAME;
866 tags_count = all_tags_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000867 } else {
vlm72425de2004-09-13 08:31:01 +0000868 tv_mode = emit_tags_vectors(arg, expr,
869 &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000870 }
vlmfa67ddc2004-06-03 03:38:44 +0000871
872 /*
873 * Tags to elements map.
874 */
vlm940bc6b2004-10-03 09:13:30 +0000875 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000876
vlm766cf1e2005-03-04 08:48:53 +0000877 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000878 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000879 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000880 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000881 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000882 OUT("),\n");
883 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000884 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000885 OUT(", _asn_ctx),\n");
886 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000887 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000888 OUT(", present),\n");
889 OUT("sizeof(((struct ");
vlm2e774282005-08-14 15:03:31 +0000890 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000891 OUT(" *)0)->present),\n");
892 OUT("asn_MAP_%s_%d_tag2el,\n",
vlm1f7df782005-03-04 23:48:19 +0000893 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000894 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm79b08d52004-07-01 00:52:50 +0000895 OUT("%d\t/* Whether extensible */\n",
896 check_if_extensible(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000897 );
898 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000899
900 /*
vlma5dcb912004-09-29 13:16:40 +0000901 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000902 */
vlm72425de2004-09-13 08:31:01 +0000903 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000904 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000905
vlmfa67ddc2004-06-03 03:38:44 +0000906 REDIR(OT_TYPE_DECLS);
907
908 return 0;
vlmedf203f2005-01-17 11:57:48 +0000909} /* _CHOICE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000910
911int
912asn1c_lang_C_type_REFERENCE(arg_t *arg) {
913 asn1p_ref_t *ref;
914
915 ref = arg->expr->reference;
916 if(ref->components[ref->comp_count-1].name[0] == '&') {
vlmfa67ddc2004-06-03 03:38:44 +0000917 asn1p_expr_t *extract;
918 arg_t tmp;
919 int ret;
920
vlm39ba4c42004-09-22 16:06:28 +0000921 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
vlmdae7f9d2004-08-22 03:25:24 +0000922 arg->expr, ref);
vlmfa67ddc2004-06-03 03:38:44 +0000923 if(extract == NULL)
924 return -1;
925
vlmdae7f9d2004-08-22 03:25:24 +0000926 extract = asn1p_expr_clone(extract, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000927 if(extract) {
928 if(extract->Identifier)
929 free(extract->Identifier);
930 extract->Identifier = strdup(arg->expr->Identifier);
931 if(extract->Identifier == NULL) {
932 asn1p_expr_free(extract);
933 return -1;
934 }
935 } else {
936 return -1;
937 }
938
939 tmp = *arg;
940 tmp.asn = arg->asn;
vlmdae7f9d2004-08-22 03:25:24 +0000941 tmp.mod = extract->module;
vlmfa67ddc2004-06-03 03:38:44 +0000942 tmp.expr = extract;
943
944 ret = arg->default_cb(&tmp);
945
946 asn1p_expr_free(extract);
947
948 return ret;
949 }
950
951
952 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
953}
954
955int
956asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
957 asn1p_expr_t *expr = arg->expr;
vlm6e73a042004-08-11 07:17:22 +0000958 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000959 int all_tags_count;
960 enum tvm_compat tv_mode;
vlm80a48592005-02-25 12:10:27 +0000961 enum etd_spec etd_spec;
vlmfa67ddc2004-06-03 03:38:44 +0000962 char *p;
963
964 if(arg->embed) {
vlma5dcb912004-09-29 13:16:40 +0000965 enum tnfmt tnfmt = TNF_CTYPE;
966
967 /*
968 * If this is an optional compound type,
969 * refer it using "struct X" convention,
970 * as it may recursively include the current structure.
971 */
vlm0b567bf2005-03-04 22:18:20 +0000972 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
vlma5dcb912004-09-29 13:16:40 +0000973 asn1p_expr_t *terminal;
974 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
975 if(terminal
976 && (terminal->expr_type & ASN_CONSTR_MASK)) {
vlm0b567bf2005-03-04 22:18:20 +0000977 tnfmt = TNF_RSAFE;
978 REDIR(OT_FWD_DECLS);
vlm5feb7522005-03-04 23:50:56 +0000979 OUT("%s;\n",
vlm0b567bf2005-03-04 22:18:20 +0000980 asn1c_type_name(arg, arg->expr, tnfmt));
vlma5dcb912004-09-29 13:16:40 +0000981 }
982 }
983
vlmfa67ddc2004-06-03 03:38:44 +0000984 REDIR(OT_TYPE_DECLS);
985
vlm0b567bf2005-03-04 22:18:20 +0000986 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
vlm80a48592005-02-25 12:10:27 +0000987 if(!expr->_anonymous_type) {
vlm0b567bf2005-03-04 22:18:20 +0000988 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
vlm1f7df782005-03-04 23:48:19 +0000989 OUT("%s", MKID_safe(expr->Identifier));
vlm80a48592005-02-25 12:10:27 +0000990 if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT)
991 OUT("\t/* DEFAULT %s */",
992 asn1f_printable_value(
993 expr->marker.default_value));
994 else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL)
995 OUT("\t/* OPTIONAL */");
996 }
997
998 } else {
999 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
vlmb2839012004-08-20 13:37:01 +00001000
1001 REDIR(OT_TYPE_DECLS);
vlm80a48592005-02-25 12:10:27 +00001002
1003 OUT("typedef %s\t",
vlm0b567bf2005-03-04 22:18:20 +00001004 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
vlm80a48592005-02-25 12:10:27 +00001005 OUT("%s%s_t",
vlm0b567bf2005-03-04 22:18:20 +00001006 (expr->marker.flags & EM_INDIRECT)?"*":" ",
vlm1f7df782005-03-04 23:48:19 +00001007 MKID(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +00001008 }
1009
vlm80a48592005-02-25 12:10:27 +00001010 if((expr->expr_type == ASN_BASIC_ENUMERATED)
vlmc89422d2005-03-03 21:28:12 +00001011 || (0 /* -- prohibited by X.693:8.3.4 */
1012 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00001013 && expr_elements_count(arg, expr)))
1014 etd_spec = ETD_HAS_SPECIFICS;
1015 else
1016 etd_spec = ETD_NO_SPECIFICS;
vlmfa67ddc2004-06-03 03:38:44 +00001017
vlm86f5ed22004-09-26 13:11:31 +00001018 /*
1019 * If this type just blindly refers the other type, alias it.
1020 * Type1 ::= Type2
1021 */
vlm80a48592005-02-25 12:10:27 +00001022 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
1023 REDIR(OT_TYPE_DECLS);
1024 return 0;
1025 }
vlm86f5ed22004-09-26 13:11:31 +00001026 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlm80a48592005-02-25 12:10:27 +00001027 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
vlm6f74b0f2005-03-04 23:23:50 +00001028 && etd_spec == ETD_NO_SPECIFICS
1029 && 0 /* This shortcut is incompatible with XER */
1030 ) {
vlm86f5ed22004-09-26 13:11:31 +00001031 char *type_name;
1032 REDIR(OT_FUNC_DECLS);
1033 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
1034 OUT("/* This type is equivalent to %s */\n", type_name);
vlm86f5ed22004-09-26 13:11:31 +00001035 if(HIDE_INNER_DEFS) OUT("/* ");
vlm1f7df782005-03-04 23:48:19 +00001036 OUT("#define\tasn_DEF_%s\t", MKID(expr->Identifier));
vlm86f5ed22004-09-26 13:11:31 +00001037 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlm80a48592005-02-25 12:10:27 +00001038 OUT("asn_DEF_%s", type_name);
vlm86f5ed22004-09-26 13:11:31 +00001039 if(HIDE_INNER_DEFS)
vlm80a48592005-02-25 12:10:27 +00001040 OUT("\t// (Use -fall-defs-global to expose) */");
1041 OUT("\n");
vlm86f5ed22004-09-26 13:11:31 +00001042 REDIR(OT_CODE);
1043 OUT("/* This type is equivalent to %s */\n", type_name);
1044 OUT("\n");
1045 REDIR(OT_TYPE_DECLS);
1046 return 0;
1047 }
1048
vlmfa67ddc2004-06-03 03:38:44 +00001049 REDIR(OT_STAT_DEFS);
1050
vlm4a3f5822004-06-28 21:13:46 +00001051 /*
vlma5dcb912004-09-29 13:16:40 +00001052 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +00001053 */
vlm72425de2004-09-13 08:31:01 +00001054 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +00001055
vlm80a48592005-02-25 12:10:27 +00001056 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1057 0, etd_spec);
vlmfa67ddc2004-06-03 03:38:44 +00001058
vlmb2839012004-08-20 13:37:01 +00001059 REDIR(OT_CODE);
1060
vlmfa67ddc2004-06-03 03:38:44 +00001061 /*
1062 * Constraint checking.
1063 */
vlm86f5ed22004-09-26 13:11:31 +00001064 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1065 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001066 if(HIDE_INNER_DEFS) OUT("static ");
vlm86f5ed22004-09-26 13:11:31 +00001067 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001068 OUT("%s", p);
1069 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1070 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
vlm86f5ed22004-09-26 13:11:31 +00001071 INDENT(+1);
1072 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1073 OUT("\n");
1074 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1075 OUT("/* Replace with underlying type checker */\n");
1076 OUT("td->check_constraints "
vlma5dcb912004-09-29 13:16:40 +00001077 "= asn_DEF_%s.check_constraints;\n",
vlm86f5ed22004-09-26 13:11:31 +00001078 asn1c_type_name(arg, expr, TNF_SAFE));
1079 OUT("return td->check_constraints"
1080 "(td, sptr, app_errlog, app_key);\n");
vlm1d036692004-08-19 13:29:46 +00001081 }
vlm86f5ed22004-09-26 13:11:31 +00001082 INDENT(-1);
1083 OUT("}\n");
1084 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001085 }
vlmfa67ddc2004-06-03 03:38:44 +00001086
1087 /*
1088 * Emit suicidal functions.
1089 */
1090
vlmfa67ddc2004-06-03 03:38:44 +00001091 /*
1092 * This function replaces certain fields from the definition
1093 * of a type with the corresponding fields from the basic type
1094 * (from which the current type is inherited).
1095 */
vlmfa67ddc2004-06-03 03:38:44 +00001096 OUT("/*\n");
vlmb2839012004-08-20 13:37:01 +00001097 OUT(" * This type is implemented using %s,\n",
1098 asn1c_type_name(arg, expr, TNF_SAFE));
vlmdcd32fe2004-09-23 22:20:47 +00001099 OUT(" * so here we adjust the DEF accordingly.\n");
vlmfa67ddc2004-06-03 03:38:44 +00001100 OUT(" */\n");
1101 OUT("static void\n");
vlm766cf1e2005-03-04 08:48:53 +00001102 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
vlm1f7df782005-03-04 23:48:19 +00001103 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001104 INDENT(+1);
vlm80a48592005-02-25 12:10:27 +00001105 {
vlm39ba4c42004-09-22 16:06:28 +00001106 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001107 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlma5dcb912004-09-29 13:16:40 +00001108 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1109 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1110 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1111 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1112 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1113 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
vlm1308d2b2004-09-10 15:49:15 +00001114 if(!terminal && !tags_count) {
vlm72425de2004-09-13 08:31:01 +00001115 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
vlma5dcb912004-09-29 13:16:40 +00001116 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1117 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1118 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1119 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
vlm1308d2b2004-09-10 15:49:15 +00001120 OUT("/* End of these lines */\n");
1121 }
vlma5dcb912004-09-29 13:16:40 +00001122 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1123 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
vlm80a48592005-02-25 12:10:27 +00001124 if(etd_spec != ETD_NO_SPECIFICS) {
1125 INDENT(-1);
vlmc86870c2005-03-29 19:04:24 +00001126 OUT(" /* ");
vlmb2839012004-08-20 13:37:01 +00001127 }
vlm80a48592005-02-25 12:10:27 +00001128 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1129 if(etd_spec == ETD_NO_SPECIFICS) {
1130 INDENT(-1);
1131 OUT("\n");
1132 } else {
1133 OUT("\t// Defined explicitly */\n");
1134 }
1135 }
vlmfa67ddc2004-06-03 03:38:44 +00001136 OUT("}\n");
1137 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001138
1139 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001140 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001141 OUT("void\n");
vlm766cf1e2005-03-04 08:48:53 +00001142 OUT("%s", p);
1143 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1144 OUT("_free(asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001145 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001146 OUT("\tvoid *struct_ptr, int contents_only) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001147 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1148 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001149 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001150 );
1151 OUT("}\n");
1152 OUT("\n");
1153
1154 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001155 if(HIDE_INNER_DEFS) OUT("static ");
vlmfa67ddc2004-06-03 03:38:44 +00001156 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001157 OUT("%s", p);
1158 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1159 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001160 INDENTED(
1161 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001162 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1163 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001164 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1165 );
1166 OUT("}\n");
1167 OUT("\n");
1168
1169 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001170 if(HIDE_INNER_DEFS) OUT("static ");
vlm9de248e2004-10-20 15:50:55 +00001171 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001172 OUT("%s", p);
1173 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1174 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001175 INDENTED(
vlmfcc4cbc2005-03-17 21:56:00 +00001176 OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001177 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1178 p, expr->_type_unique_index);
vlma5dcb912004-09-29 13:16:40 +00001179 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
vlm39ba4c42004-09-22 16:06:28 +00001180 );
1181 OUT("}\n");
1182 OUT("\n");
1183
1184 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001185 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001186 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001187 OUT("%s", p);
1188 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1189 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
vlm39ba4c42004-09-22 16:06:28 +00001190 INDENTED(
1191 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1192 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001193 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1194 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001195 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1196 );
1197 OUT("}\n");
1198 OUT("\n");
1199
1200 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001201 if(HIDE_INNER_DEFS) OUT("static ");
vlme03646b2004-10-23 13:34:00 +00001202 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001203 OUT("%s", p);
1204 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1205 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlme03646b2004-10-23 13:34:00 +00001206 INDENTED(
vlmfcc4cbc2005-03-17 21:56:00 +00001207 OUT("\tvoid **structure, const char *opt_mname, const void *bufptr, size_t size) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001208 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1209 p, expr->_type_unique_index);
vlme03646b2004-10-23 13:34:00 +00001210 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1211 );
1212 OUT("}\n");
1213 OUT("\n");
1214
1215 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001216 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001217 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001218 OUT("%s", p);
1219 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1220 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
vlm39ba4c42004-09-22 16:06:28 +00001221 INDENTED(
1222 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1223 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001224 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1225 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001226 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001227 );
1228 OUT("}\n");
1229 OUT("\n");
1230
1231 REDIR(OT_FUNC_DECLS);
1232
vlm1f7df782005-03-04 23:48:19 +00001233 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001234 if(HIDE_INNER_DEFS) {
vlm766cf1e2005-03-04 08:48:53 +00001235 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1236 "\t// (Use -fall-defs-global to expose) */\n",
1237 p, expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +00001238 } else {
1239 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1240 OUT("asn_struct_free_f %s_free;\n", p);
1241 OUT("asn_struct_print_f %s_print;\n", p);
1242 OUT("asn_constr_check_f %s_constraint;\n", p);
1243 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1244 OUT("der_type_encoder_f %s_encode_der;\n", p);
1245 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1246 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1247 }
vlmfa67ddc2004-06-03 03:38:44 +00001248
vlm33a4ff12004-08-11 05:21:32 +00001249 REDIR(OT_TYPE_DECLS);
1250
vlmfa67ddc2004-06-03 03:38:44 +00001251 return 0;
1252}
1253
1254int
1255asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1256
1257 OUT("/*\n");
1258 OUT(" * This type is extensible,\n");
1259 OUT(" * possible extensions are below.\n");
1260 OUT(" */\n");
1261
1262 return 0;
1263}
1264
vlm79b08d52004-07-01 00:52:50 +00001265static int check_if_extensible(asn1p_expr_t *expr) {
1266 asn1p_expr_t *v;
1267 TQ_FOR(v, &(expr->members), next) {
1268 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1269 }
1270 return 0;
1271}
1272
vlmfa67ddc2004-06-03 03:38:44 +00001273static int
vlma8a86cc2004-09-10 06:07:18 +00001274_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
vlmfa67ddc2004-06-03 03:38:44 +00001275
1276 OUT("(");
vlma8a86cc2004-09-10 06:07:18 +00001277 switch(tag->tag_class) {
vlmfa67ddc2004-06-03 03:38:44 +00001278 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1279 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1280 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1281 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1282 case TC_NOCLASS:
1283 break;
1284 }
vlm47ae1582004-09-24 21:01:43 +00001285 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +00001286
1287 return 0;
1288}
1289
vlm4e03ce22004-06-06 07:20:17 +00001290
1291static int
1292_tag2el_cmp(const void *ap, const void *bp) {
1293 const tag2el_t *a = ap;
1294 const tag2el_t *b = bp;
1295 const struct asn1p_type_tag_s *ta = &a->el_tag;
1296 const struct asn1p_type_tag_s *tb = &b->el_tag;
1297
1298 if(ta->tag_class == tb->tag_class) {
1299 if(ta->tag_value == tb->tag_value) {
1300 /*
1301 * Sort by their respective positions.
1302 */
1303 if(a->el_no < b->el_no)
1304 return -1;
1305 else if(a->el_no > b->el_no)
1306 return 1;
1307 return 0;
1308 } else if(ta->tag_value < tb->tag_value)
1309 return -1;
1310 else
1311 return 1;
1312 } else if(ta->tag_class < tb->tag_class) {
1313 return -1;
1314 } else {
1315 return 1;
1316 }
1317}
1318
vlmfa67ddc2004-06-03 03:38:44 +00001319/*
1320 * For constructed types, number of external tags may be greater than
1321 * number of elements in the type because of CHOICE type.
1322 * T ::= SET { -- Three possible tags:
1323 * a INTEGER, -- One tag is here...
1324 * b Choice1 -- ... and two more tags are there.
1325 * }
1326 * Choice1 ::= CHOICE {
1327 * s1 IA5String,
1328 * s2 ObjectDescriptor
1329 * }
1330 */
1331static int
vlm940bc6b2004-10-03 09:13:30 +00001332_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001333 asn1p_expr_t *expr = arg->expr;
1334 arg_t tmparg = *arg;
1335 asn1p_expr_t *v;
1336 int element = 0;
vlm940bc6b2004-10-03 09:13:30 +00001337 int original_count = *count;
1338 int sort_until = -1;
vlmfa67ddc2004-06-03 03:38:44 +00001339
1340 TQ_FOR(v, &(expr->members), next) {
vlm940bc6b2004-10-03 09:13:30 +00001341 if(v->expr_type == A1TC_EXTENSIBLE) {
1342 /*
vlm80a48592005-02-25 12:10:27 +00001343 * CXER mandates sorting
vlm940bc6b2004-10-03 09:13:30 +00001344 * only for the root part.
1345 */
1346 if(flags == FTE_CANONICAL_XER
1347 && sort_until == -1)
1348 sort_until = *count;
vlmfa67ddc2004-06-03 03:38:44 +00001349 continue;
vlm940bc6b2004-10-03 09:13:30 +00001350 }
vlmfa67ddc2004-06-03 03:38:44 +00001351
1352 tmparg.expr = v;
1353
1354 if(_add_tag2el_member(&tmparg, tag2el, count,
vlm940bc6b2004-10-03 09:13:30 +00001355 (el_no==-1)?element:el_no, flags)) {
vlmfa67ddc2004-06-03 03:38:44 +00001356 return -1;
1357 }
1358
1359 element++;
1360 }
1361
vlm940bc6b2004-10-03 09:13:30 +00001362
1363 if(flags == FTE_CANONICAL_XER) {
1364 if(sort_until == -1) sort_until = *count;
1365 qsort((*tag2el) + original_count,
1366 sort_until - original_count,
1367 sizeof(**tag2el), _tag2el_cmp);
1368 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1369 && (sort_until - original_count) >= 1) {
1370 /* Only take in account the root component */
1371 *count = original_count + 1;
1372 }
1373 } else {
1374 /*
1375 * Sort the map according to canonical order of their
1376 * tags and element numbers.
1377 */
1378 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1379 }
vlm4e03ce22004-06-06 07:20:17 +00001380
vlmc8aeab42004-06-14 13:09:45 +00001381 /*
1382 * Initialize .toff_{first|last} members.
1383 */
1384 if(*count) {
1385 struct asn1p_type_tag_s *cur_tag = 0;
1386 tag2el_t *cur = *tag2el;
1387 tag2el_t *end = cur + *count;
1388 int occur, i;
1389 for(occur = 0; cur < end; cur++) {
1390 if(cur_tag == 0
1391 || cur_tag->tag_value != cur->el_tag.tag_value
1392 || cur_tag->tag_class != cur->el_tag.tag_class) {
1393 cur_tag = &cur->el_tag;
1394 occur = 0;
1395 } else {
1396 occur++;
1397 }
1398 cur->toff_first = -occur;
1399 for(i = 0; i >= -occur; i--)
1400 cur[i].toff_last = -i;
1401 }
1402 }
1403
vlmfa67ddc2004-06-03 03:38:44 +00001404 return 0;
1405}
1406
1407static int
vlm940bc6b2004-10-03 09:13:30 +00001408_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001409 struct asn1p_type_tag_s tag;
1410 int ret;
1411
1412 assert(el_no >= 0);
1413
vlm39ba4c42004-09-22 16:06:28 +00001414 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1415 arg->expr, &tag, 1);
vlmfa67ddc2004-06-03 03:38:44 +00001416 if(ret == 0) {
vlme2070ea2004-09-04 04:42:29 +00001417 tag2el_t *te;
1418 int new_count = (*count) + 1;
vlmfa67ddc2004-06-03 03:38:44 +00001419 void *p;
vlme2070ea2004-09-04 04:42:29 +00001420
vlm2de0e792004-09-05 10:42:33 +00001421 if(tag.tag_value == -1) {
1422 /*
1423 * This is an untagged ANY type,
1424 * proceed without adding a tag
1425 */
1426 return 0;
1427 }
1428
vlme2070ea2004-09-04 04:42:29 +00001429 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
vlmfa67ddc2004-06-03 03:38:44 +00001430 if(p) *tag2el = p;
1431 else return -1;
1432
1433 DEBUG("Found tag for %s: %ld",
1434 arg->expr->Identifier,
1435 (long)tag.tag_value);
1436
vlme2070ea2004-09-04 04:42:29 +00001437 te = &((*tag2el)[*count]);
1438 te->el_tag = tag;
1439 te->el_no = el_no;
1440 te->from_expr = arg->expr;
1441 *count = new_count;
vlmfa67ddc2004-06-03 03:38:44 +00001442 return 0;
1443 }
1444
1445 DEBUG("Searching tag in complex expression %s:%x at line %d",
1446 arg->expr->Identifier,
1447 arg->expr->expr_type,
1448 arg->expr->_lineno);
1449
1450 /*
1451 * Iterate over members of CHOICE type.
1452 */
1453 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
vlm940bc6b2004-10-03 09:13:30 +00001454 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001455 }
1456
1457 if(arg->expr->expr_type == A1TC_REFERENCE) {
1458 arg_t tmp = *arg;
1459 asn1p_expr_t *expr;
vlmdae7f9d2004-08-22 03:25:24 +00001460 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
vlmfa67ddc2004-06-03 03:38:44 +00001461 arg->expr->reference);
1462 if(expr) {
vlmdae7f9d2004-08-22 03:25:24 +00001463 tmp.mod = expr->module;
vlmfa67ddc2004-06-03 03:38:44 +00001464 tmp.expr = expr;
vlm940bc6b2004-10-03 09:13:30 +00001465 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001466 } else {
1467 FATAL("Cannot dereference %s at line %d",
1468 arg->expr->Identifier,
1469 arg->expr->_lineno);
1470 return -1;
1471 }
1472 }
1473
1474 DEBUG("No tag for %s at line %d",
1475 arg->expr->Identifier,
1476 arg->expr->_lineno);
1477
1478 return -1;
1479}
1480
1481static int
vlm940bc6b2004-10-03 09:13:30 +00001482emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
vlm4e03ce22004-06-06 07:20:17 +00001483 asn1p_expr_t *expr = arg->expr;
vlmfbd6d9e2005-06-02 05:21:37 +00001484 int i;
1485
1486 if(!tag2el_count) return 0; /* No top level tags */
vlm4e03ce22004-06-06 07:20:17 +00001487
vlm766cf1e2005-03-04 08:48:53 +00001488 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
vlm1f7df782005-03-04 23:48:19 +00001489 MKID(expr->Identifier), expr->_type_unique_index,
vlm766cf1e2005-03-04 08:48:53 +00001490 opt_modifier?opt_modifier:"");
vlmfbd6d9e2005-06-02 05:21:37 +00001491 for(i = 0; i < tag2el_count; i++) {
1492 OUT(" { ");
1493 _print_tag(arg, &tag2el[i].el_tag);
1494 OUT(", ");
1495 OUT("%d, ", tag2el[i].el_no);
1496 OUT("%d, ", tag2el[i].toff_first);
1497 OUT("%d ", tag2el[i].toff_last);
1498 OUT("}%s /* %s at %d */\n",
1499 (i + 1 < tag2el_count) ? "," : "",
1500 tag2el[i].from_expr->Identifier,
1501 tag2el[i].from_expr->_lineno
1502 );
vlm4e03ce22004-06-06 07:20:17 +00001503 }
1504 OUT("};\n");
1505
vlmfbd6d9e2005-06-02 05:21:37 +00001506 return 0;
vlm4e03ce22004-06-06 07:20:17 +00001507}
1508
vlm72425de2004-09-13 08:31:01 +00001509static enum tvm_compat
1510emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1511 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1512 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
vlm6e73a042004-08-11 07:17:22 +00001513 int tags_count = 0;
vlm72425de2004-09-13 08:31:01 +00001514 int all_tags_count = 0;
1515 enum tvm_compat tv_mode = _TVM_SAME;
vlm1308d2b2004-09-10 15:49:15 +00001516 int i;
vlm4a3f5822004-06-28 21:13:46 +00001517
vlmc6de2c42004-09-14 14:10:10 +00001518 /* Cleanup before proceeding. */
1519 *tags_count_r = 0;
1520 *all_tags_count_r = 0;
1521
vlm1308d2b2004-09-10 15:49:15 +00001522 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001523 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
vlmc6de2c42004-09-14 14:10:10 +00001524 if(tags_count < 0)
1525 return -1;
vlm6e73a042004-08-11 07:17:22 +00001526
vlm72425de2004-09-13 08:31:01 +00001527 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001528 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
vlm72425de2004-09-13 08:31:01 +00001529 &all_tags, AFT_FULL_COLLECT);
1530 if(all_tags_count < 0) {
1531 if(tags) free(tags);
1532 return -1;
vlmfb41dbb2004-09-08 00:28:57 +00001533 }
vlm1308d2b2004-09-10 15:49:15 +00001534
vlm72425de2004-09-13 08:31:01 +00001535 assert(tags_count <= all_tags_count);
1536 assert((tags_count?0:1) == (all_tags_count?0:1));
vlm4a3f5822004-06-28 21:13:46 +00001537
vlm72425de2004-09-13 08:31:01 +00001538 if(tags_count <= all_tags_count) {
1539 for(i = 0; i < tags_count; i++) {
1540 if(tags[i].tag_value != all_tags[i].tag_value
1541 || tags[i].tag_class != all_tags[i].tag_class) {
1542 tv_mode = _TVM_DIFFERENT;
1543 break;
1544 }
1545 }
1546 if(i == tags_count && tags_count < all_tags_count)
1547 tv_mode = _TVM_SUBSET;
1548 } else {
1549 tv_mode = _TVM_DIFFERENT;
1550 }
1551
1552#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
vlm766cf1e2005-03-04 08:48:53 +00001553 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
vlm1f7df782005-03-04 23:48:19 +00001554 MKID(expr->Identifier), \
vlm766cf1e2005-03-04 08:48:53 +00001555 expr->_type_unique_index, name); \
vlm72425de2004-09-13 08:31:01 +00001556 INDENT(+1); \
1557 /* Print the array of collected tags */ \
1558 for(i = 0; i < tags_count; i++) { \
1559 if(i) OUT(",\n"); \
1560 _print_tag(arg, &tags[i]); \
1561 } \
1562 OUT("\n"); \
1563 INDENT(-1); \
1564 OUT("};\n"); \
1565 } while(0)
1566
1567 if(tags_count) {
1568 if(tv_mode == _TVM_SUBSET)
1569 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1570 else
1571 EMIT_TAGS_TABLE("", tags, tags_count);
1572 }
1573
1574 if(all_tags_count) {
1575 if(tv_mode == _TVM_DIFFERENT)
1576 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1577 }
1578
1579 if(tags) free(tags);
1580 if(all_tags) free(all_tags);
1581
1582 *tags_count_r = tags_count;
1583 *all_tags_count_r = all_tags_count;
1584
1585 return tv_mode;
vlm4a3f5822004-06-28 21:13:46 +00001586}
vlmb2839012004-08-20 13:37:01 +00001587
1588static int
vlm4e554992004-08-25 02:03:12 +00001589expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001590 asn1p_expr_t *topmost_parent;
1591 asn1p_expr_t *v;
1592 int elements = 0;
1593
vlm39ba4c42004-09-22 16:06:28 +00001594 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001595 if(!topmost_parent) return 0;
1596
vlm80a48592005-02-25 12:10:27 +00001597 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1598 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1599 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
vlmb2839012004-08-20 13:37:01 +00001600 return 0;
1601
1602 TQ_FOR(v, &(topmost_parent->members), next) {
1603 if(v->expr_type != A1TC_EXTENSIBLE)
1604 elements++;
1605 }
1606
1607 return elements;
1608}
1609
1610static int
vlm0b567bf2005-03-04 22:18:20 +00001611emit_include_dependencies(arg_t *arg) {
1612 asn1p_expr_t *expr = arg->expr;
1613 asn1p_expr_t *memb;
1614
1615 TQ_FOR(memb, &(expr->members), next) {
1616
1617 if((memb->meta_type == AMT_TYPEREF
1618 && (memb->marker.flags & EM_INDIRECT))
1619 || expr->expr_type == ASN_CONSTR_SET_OF
1620 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1621 ) {
1622 asn1p_expr_t *terminal;
1623 terminal = asn1f_find_terminal_type_ex(arg->asn, memb);
1624 if(terminal && !terminal->parent_expr
1625 && (terminal->expr_type & ASN_CONSTR_MASK)) {
1626 int saved_target = arg->target->target;
1627 REDIR(OT_FWD_DECLS);
vlm5feb7522005-03-04 23:50:56 +00001628 OUT("%s;\n",
vlm0b567bf2005-03-04 22:18:20 +00001629 asn1c_type_name(arg, memb, TNF_RSAFE));
1630 REDIR(saved_target);
1631 memb->marker.flags |= EM_UNRECURSE;
1632 }
1633 }
1634
1635 if((!(memb->expr_type & ASN_CONSTR_MASK)
1636 && memb->expr_type > ASN_CONSTR_MASK)
1637 || memb->meta_type == AMT_TYPEREF) {
1638 if(memb->marker.flags & EM_UNRECURSE) {
1639 GEN_POSTINCLUDE(asn1c_type_name(arg,
1640 memb, TNF_INCLUDE));
1641 } else {
1642 GEN_INCLUDE(asn1c_type_name(arg,
1643 memb, TNF_INCLUDE));
1644 }
1645 }
1646 }
1647
1648 return 0;
1649}
1650
1651static int
vlm4e554992004-08-25 02:03:12 +00001652emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001653 int save_target;
1654 arg_t tmp_arg;
vlm060fe2a2004-09-10 09:37:12 +00001655 struct asn1p_type_tag_s outmost_tag_s;
1656 struct asn1p_type_tag_s *outmost_tag;
vlm80a48592005-02-25 12:10:27 +00001657 int complex_contents;
vlmb2839012004-08-20 13:37:01 +00001658 char *p;
1659
vlm060fe2a2004-09-10 09:37:12 +00001660 if(asn1f_fetch_outmost_tag(arg->asn,
1661 expr->module, expr, &outmost_tag_s, 1)) {
1662 outmost_tag = 0;
1663 } else {
1664 outmost_tag = &outmost_tag_s;
1665 }
1666
vlmb2839012004-08-20 13:37:01 +00001667 OUT("{ ");
vlm060fe2a2004-09-10 09:37:12 +00001668
1669 if(outmost_tag && outmost_tag->tag_value == -1)
1670 OUT("ATF_OPEN_TYPE | ");
vlm0b567bf2005-03-04 22:18:20 +00001671 OUT("%s, ",
1672 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
vlma02b74d2004-09-15 11:54:38 +00001673 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
vlmb2839012004-08-20 13:37:01 +00001674 asn1p_expr_t *tv;
1675 int opts = 0;
vlma02b74d2004-09-15 11:54:38 +00001676 for(tv = expr; tv && tv->marker.flags;
vlmb2839012004-08-20 13:37:01 +00001677 tv = TQ_NEXT(tv, next), opts++) {
1678 if(tv->expr_type == A1TC_EXTENSIBLE)
1679 opts--;
1680 }
1681 OUT("%d, ", opts);
1682 } else {
1683 OUT("0, ");
1684 }
vlm39ba4c42004-09-22 16:06:28 +00001685 if(expr->_anonymous_type) {
1686 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1687 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1688 OUT("0,\n");
1689 } else {
vlm766cf1e2005-03-04 08:48:53 +00001690 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +00001691 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +00001692 OUT(", ");
vlmb2839012004-08-20 13:37:01 +00001693 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1694 && (!UNNAMED_UNIONS)) OUT("choice.");
vlm1f7df782005-03-04 23:48:19 +00001695 OUT("%s),\n", MKID_safe(expr->Identifier));
vlmb2839012004-08-20 13:37:01 +00001696 }
1697 INDENT(+1);
1698 if(C99_MODE) OUT(".tag = ");
vlm060fe2a2004-09-10 09:37:12 +00001699 if(outmost_tag) {
1700 if(outmost_tag->tag_value == -1)
1701 OUT("-1 /* Ambiguous tag (ANY?) */");
1702 else
1703 _print_tag(arg, outmost_tag);
vlma8a86cc2004-09-10 06:07:18 +00001704 } else {
vlm060fe2a2004-09-10 09:37:12 +00001705 OUT("-1 /* Ambiguous tag (CHOICE?) */");
vlma8a86cc2004-09-10 06:07:18 +00001706 }
vlm060fe2a2004-09-10 09:37:12 +00001707
vlmb2839012004-08-20 13:37:01 +00001708 OUT(",\n");
1709 if(C99_MODE) OUT(".tag_mode = ");
1710 if(expr->tag.tag_class) {
1711 if(expr->tag.tag_mode == TM_IMPLICIT)
1712 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1713 else
1714 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1715 } else {
1716 OUT("0,\n");
1717 }
vlm80a48592005-02-25 12:10:27 +00001718
1719 complex_contents =
1720 (expr->expr_type & ASN_CONSTR_MASK)
1721 || expr->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +00001722 || (0 /* -- prohibited by X.693:8.3.4 */
1723 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00001724 && expr_elements_count(arg, expr));
vlmb2839012004-08-20 13:37:01 +00001725 if(C99_MODE) OUT(".type = ");
vlm766cf1e2005-03-04 08:48:53 +00001726 OUT("(void *)&asn_DEF_");
1727 if(complex_contents) {
vlm1f7df782005-03-04 23:48:19 +00001728 OUT("%s", MKID(expr->Identifier));
vlm766cf1e2005-03-04 08:48:53 +00001729 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1730 OUT("_%d", expr->_type_unique_index);
vlmdae7f9d2004-08-22 03:25:24 +00001731 } else {
vlm766cf1e2005-03-04 08:48:53 +00001732 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
vlmdae7f9d2004-08-22 03:25:24 +00001733 }
vlm766cf1e2005-03-04 08:48:53 +00001734 OUT(",\n");
vlmb2839012004-08-20 13:37:01 +00001735 if(C99_MODE) OUT(".memb_constraints = ");
1736 if(expr->constraints) {
vlm86f5ed22004-09-26 13:11:31 +00001737 if(arg->flags & A1C_NO_CONSTRAINTS) {
1738 OUT("0,\t/* No check because of -fno-constraints */\n");
1739 } else {
vlm1f7df782005-03-04 23:48:19 +00001740 char *id = MKID(expr->Identifier);
vlm86f5ed22004-09-26 13:11:31 +00001741 if(expr->_anonymous_type
vlmbf3c5112005-02-14 20:41:29 +00001742 && !strcmp(expr->Identifier, "Member"))
vlm86f5ed22004-09-26 13:11:31 +00001743 id = asn1c_type_name(arg, expr, TNF_SAFE);
1744 OUT("memb_%s_%d_constraint,\n", id,
vlm766cf1e2005-03-04 08:48:53 +00001745 arg->expr->_type_unique_index);
vlm86f5ed22004-09-26 13:11:31 +00001746 }
vlmb2839012004-08-20 13:37:01 +00001747 } else {
vlm95a476e2005-01-17 12:16:58 +00001748 OUT("0,\t/* Defer constraints checking to the member type */\n");
vlmb2839012004-08-20 13:37:01 +00001749 }
1750 if(C99_MODE) OUT(".name = ");
vlmbf3c5112005-02-14 20:41:29 +00001751 if(1) {
1752 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1753 OUT("\"\"\n");
1754 else
1755 OUT("\"%s\"\n", expr->Identifier);
1756 } else {
1757 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1758 }
vlmb2839012004-08-20 13:37:01 +00001759 OUT("},\n");
1760 INDENT(-1);
1761
vlm86f5ed22004-09-26 13:11:31 +00001762 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlmb2839012004-08-20 13:37:01 +00001763 return 0;
1764
1765 save_target = arg->target->target;
1766 REDIR(OT_CODE);
1767
vlmbf3c5112005-02-14 20:41:29 +00001768 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
vlmb2839012004-08-20 13:37:01 +00001769 p = asn1c_type_name(arg, expr, TNF_SAFE);
vlm39ba4c42004-09-22 16:06:28 +00001770 else
vlm1f7df782005-03-04 23:48:19 +00001771 p = MKID(expr->Identifier);
vlmb2839012004-08-20 13:37:01 +00001772 OUT("static int\n");
vlm766cf1e2005-03-04 08:48:53 +00001773 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 +00001774 INDENT(+1);
1775 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1776 tmp_arg = *arg;
1777 tmp_arg.expr = expr;
1778 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
vlm86f5ed22004-09-26 13:11:31 +00001779 OUT("return td->check_constraints"
1780 "(td, sptr, app_errlog, app_key);\n");
vlmb2839012004-08-20 13:37:01 +00001781 }
1782 INDENT(-1);
1783 OUT("}\n");
1784 OUT("\n");
1785
1786 REDIR(save_target);
1787
1788 return 0;
1789}
vlm4e554992004-08-25 02:03:12 +00001790
vlm9de248e2004-10-20 15:50:55 +00001791/*
1792 * Generate "asn_DEF_XXX" type definition.
1793 */
vlm4e554992004-08-25 02:03:12 +00001794static int
vlm86f5ed22004-09-26 13:11:31 +00001795emit_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 +00001796 int using_type_name = 0;
vlm4e554992004-08-25 02:03:12 +00001797 char *p;
1798
vlm12c8f692004-09-06 08:07:29 +00001799 if(HIDE_INNER_DEFS)
1800 OUT("static /* Use -fall-defs-global to expose */\n");
vlm1f7df782005-03-04 23:48:19 +00001801 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID(expr->Identifier));
vlm766cf1e2005-03-04 08:48:53 +00001802 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1803 OUT(" = {\n");
vlmaf841972005-01-28 12:18:50 +00001804 p = MKID(expr->Identifier);
vlm72425de2004-09-13 08:31:01 +00001805 INDENT(+1);
vlm4e554992004-08-25 02:03:12 +00001806 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm9de248e2004-10-20 15:50:55 +00001807 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001808
1809 if(expr->expr_type & ASN_CONSTR_MASK) {
vlm766cf1e2005-03-04 08:48:53 +00001810 using_type_name = 1;
vlm4e554992004-08-25 02:03:12 +00001811 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1812 }
1813
vlm766cf1e2005-03-04 08:48:53 +00001814#define FUNCREF(foo) do { \
1815 OUT("%s", p); \
1816 if(HIDE_INNER_DEFS && !using_type_name) \
1817 OUT("_%d", expr->_type_unique_index); \
1818 OUT("_" #foo ",\n"); \
1819} while(0)
1820
1821 FUNCREF(free);
1822 FUNCREF(print);
1823 FUNCREF(constraint);
1824 FUNCREF(decode_ber);
1825 FUNCREF(encode_der);
1826 FUNCREF(decode_xer);
1827 FUNCREF(encode_xer);
vlm4e554992004-08-25 02:03:12 +00001828
vlm4e554992004-08-25 02:03:12 +00001829 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1830 OUT("CHOICE_outmost_tag,\n");
1831 } else {
1832 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1833 }
1834
vlm1f7df782005-03-04 23:48:19 +00001835 p = MKID(expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001836 if(tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001837 OUT("asn_DEF_%s_%d_tags,\n",
1838 p, expr->_type_unique_index);
1839 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1840 p, expr->_type_unique_index);
1841 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1842 p, expr->_type_unique_index);
vlm72425de2004-09-13 08:31:01 +00001843 if(tv_mode == _TVM_SUBSET
1844 && tags_count != all_tags_count)
1845 OUT(" - %d", all_tags_count - tags_count);
1846 OUT(", /* %d */\n", tags_count);
vlm4e554992004-08-25 02:03:12 +00001847 } else {
vlm72425de2004-09-13 08:31:01 +00001848 OUT("0,\t/* No effective tags (pointer) */\n");
1849 OUT("0,\t/* No effective tags (count) */\n");
1850 }
1851
1852 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
vlm766cf1e2005-03-04 08:48:53 +00001853 OUT("asn_DEF_%s_%d_all_tags,\n",
1854 p, expr->_type_unique_index);
1855 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1856 p, expr->_type_unique_index);
1857 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1858 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001859 } else if(all_tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001860 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1861 p, expr->_type_unique_index);
1862 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1863 p, expr->_type_unique_index);
1864 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1865 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001866 } else {
1867 OUT("0,\t/* No tags (pointer) */\n");
1868 OUT("0,\t/* No tags (count) */\n");
vlm4e554992004-08-25 02:03:12 +00001869 }
1870
vlm4e554992004-08-25 02:03:12 +00001871 if(elements_count) {
vlm766cf1e2005-03-04 08:48:53 +00001872 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001873 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1874 || expr->expr_type == ASN_CONSTR_SET_OF) {
1875 OUT("%d,\t/* Single element */\n",
1876 elements_count);
1877 assert(elements_count == 1);
1878 } else {
1879 OUT("%d,\t/* Elements count */\n",
1880 elements_count);
1881 }
1882 } else {
vlm4e554992004-08-25 02:03:12 +00001883 if(expr_elements_count(arg, expr))
1884 OUT("0, 0,\t/* Defined elsewhere */\n");
1885 else
1886 OUT("0, 0,\t/* No members */\n");
1887 }
1888
1889 switch(spec) {
1890 case ETD_NO_SPECIFICS:
1891 OUT("0\t/* No specifics */\n");
1892 break;
1893 case ETD_HAS_SPECIFICS:
vlm766cf1e2005-03-04 08:48:53 +00001894 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1895 p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001896 }
vlm72425de2004-09-13 08:31:01 +00001897 INDENT(-1);
vlm4e554992004-08-25 02:03:12 +00001898 OUT("};\n");
1899 OUT("\n");
vlme2bb4432004-08-26 06:20:34 +00001900
1901 return 0;
vlm4e554992004-08-25 02:03:12 +00001902}
vlmddd5a7d2004-09-10 09:18:20 +00001903
1904/*
1905 * Check if it is better to make this type indirectly accessed via
1906 * a pointer.
1907 * This may be the case for the following recursive definition:
1908 * Type ::= CHOICE { member Type };
1909 */
1910static int
1911expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) {
1912 asn1p_expr_t *top_parent;
1913 asn1p_expr_t *terminal;
1914
1915 if(expr->expr_type != A1TC_REFERENCE)
1916 return 0;
1917
vlm4cc45722005-07-24 08:28:39 +00001918 /* -findirect-choice compiles members of CHOICE as indirect pointers */
1919 if((arg->flags & A1C_INDIRECT_CHOICE)
1920 && arg->expr->expr_type == ASN_CONSTR_CHOICE)
1921 return 1;
1922
vlmddd5a7d2004-09-10 09:18:20 +00001923 /* Rewind to the topmost parent expression */
1924 if((top_parent = expr->parent_expr)) {
1925 while(top_parent->parent_expr)
1926 top_parent = top_parent->parent_expr;
1927 } else {
1928 return 0;
1929 }
1930
vlm39ba4c42004-09-22 16:06:28 +00001931 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmddd5a7d2004-09-10 09:18:20 +00001932
1933 return (terminal == top_parent);
1934}
vlm39ba4c42004-09-22 16:06:28 +00001935
1936static int
1937expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1938 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1939 if(!expr) return 0;
1940
1941 /* X.680, 25.5, Table 5 */
1942 switch(expr->expr_type) {
vlm39ba4c42004-09-22 16:06:28 +00001943 case ASN_BASIC_BOOLEAN:
1944 case ASN_BASIC_ENUMERATED:
1945 case ASN_BASIC_NULL:
1946 return 1;
1947 default:
1948 return 0;
1949 }
1950}
vlmaf841972005-01-28 12:18:50 +00001951
1952static int
vlm2e774282005-08-14 15:03:31 +00001953out_name_chain(arg_t *arg, enum onc_flags onc_flags) {
vlmaf841972005-01-28 12:18:50 +00001954 asn1p_expr_t *expr = arg->expr;
1955 char *id;
1956
1957 assert(expr->Identifier);
1958
vlm2e774282005-08-14 15:03:31 +00001959 if((arg->flags & A1C_COMPOUND_NAMES
1960 || onc_flags & ONC_force_compound_name)
vlm766cf1e2005-03-04 08:48:53 +00001961 && ((expr->expr_type & ASN_CONSTR_MASK)
1962 || expr->expr_type == ASN_BASIC_ENUMERATED
vlm96853d82005-08-13 23:51:47 +00001963 || ((expr->expr_type == ASN_BASIC_INTEGER
1964 || expr->expr_type == ASN_BASIC_BIT_STRING)
vlm766cf1e2005-03-04 08:48:53 +00001965 && expr_elements_count(arg, expr))
1966 )
vlmaf841972005-01-28 12:18:50 +00001967 && expr->parent_expr
1968 && expr->parent_expr->Identifier) {
1969 arg_t tmparg = *arg;
1970
1971 tmparg.expr = expr->parent_expr;
vlm766cf1e2005-03-04 08:48:53 +00001972 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
1973
vlm2e774282005-08-14 15:03:31 +00001974 out_name_chain(&tmparg, onc_flags);
vlmaf841972005-01-28 12:18:50 +00001975
vlm92d3cad2005-03-05 00:08:41 +00001976 OUT("__"); /* a separator between id components */
vlm766cf1e2005-03-04 08:48:53 +00001977
vlmaf841972005-01-28 12:18:50 +00001978 /* Fall through */
1979 }
1980
vlm2e774282005-08-14 15:03:31 +00001981 if(onc_flags & ONC_avoid_keywords)
vlm1f7df782005-03-04 23:48:19 +00001982 id = MKID_safe(expr->Identifier);
vlmaf841972005-01-28 12:18:50 +00001983 else
vlm1f7df782005-03-04 23:48:19 +00001984 id = MKID(expr->Identifier);
vlmaf841972005-01-28 12:18:50 +00001985 OUT("%s", id);
1986
1987 return 0;
1988}