blob: 1440ef5a92b9453e84da675bf445fa6bc8300a9a [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*
2 * Don't look into this file. First, because it's a mess, and second, because
3 * it's a brain of the compiler, and you don't wanna mess with brains do you? ;)
4 */
5#include "asn1c_internal.h"
6#include "asn1c_C.h"
Lev Walkin84cd58e2004-08-19 13:29:46 +00007#include "asn1c_constraint.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00008#include "asn1c_out.h"
9#include "asn1c_misc.h"
Lev Walkin84cd58e2004-08-19 13:29:46 +000010#include <asn1fix_export.h> /* Stuff exported by libasn1fix */
Lev Walkinf15320b2004-06-03 03:38:44 +000011
Lev Walkinfd171ef2004-06-06 07:20:17 +000012typedef struct tag2el_s {
13 struct asn1p_type_tag_s el_tag;
14 int el_no;
Lev Walkin38abe792004-06-14 13:09:45 +000015 int toff_first;
16 int toff_last;
Lev Walkinfd171ef2004-06-06 07:20:17 +000017 asn1p_expr_t *from_expr;
18} tag2el_t;
19
Lev Walkin33d5d3c2004-10-03 09:13:30 +000020typedef enum fte {
21 FTE_ALLTAGS,
22 FTE_CANONICAL_XER,
23} fte_e;
24static int _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags);
25static int _add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags);
Lev Walkinfd171ef2004-06-06 07:20:17 +000026
Lev Walkinf15320b2004-06-03 03:38:44 +000027static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg);
28static int asn1c_lang_C_type_SET_def(arg_t *arg);
29static int asn1c_lang_C_type_CHOICE_def(arg_t *arg);
30static int asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of);
Lev Walkinc3e29402004-09-10 06:07:18 +000031static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p);
Lev Walkin35631482004-07-01 00:52:50 +000032static int check_if_extensible(asn1p_expr_t *expr);
Lev Walkincc93b0f2004-09-10 09:18:20 +000033static int expr_better_indirect(arg_t *arg, asn1p_expr_t *expr);
Lev Walkina9cc46e2004-09-22 16:06:28 +000034static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr);
Lev Walkin59964be2004-08-25 02:03:12 +000035static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr);
36static int emit_member_table(arg_t *arg, asn1p_expr_t *expr);
Lev Walkin33d5d3c2004-10-03 09:13:30 +000037static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier);
Lev Walkinc8285712005-03-04 22:18:20 +000038static int emit_include_dependencies(arg_t *arg);
Lev Walkin21d00002005-03-04 08:48:53 +000039static int out_name_chain(arg_t *arg, int check_reserved_keywords);
Lev Walkinf15320b2004-06-03 03:38:44 +000040
Lev Walkin188ed2c2004-09-13 08:31:01 +000041enum tvm_compat {
42 _TVM_SAME = 0, /* tags and all_tags are same */
43 _TVM_SUBSET = 1, /* tags are subset of all_tags */
44 _TVM_DIFFERENT = 2, /* tags and all_tags are different */
45};
46static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc);
47
Lev Walkin59964be2004-08-25 02:03:12 +000048enum etd_spec {
49 ETD_NO_SPECIFICS,
50 ETD_HAS_SPECIFICS
51};
Lev Walkin8de2ab22004-09-26 13:11:31 +000052static int emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_count, int all_tags_count, int elements_count, enum etd_spec);
Lev Walkin59964be2004-08-25 02:03:12 +000053
Lev Walkin59004fa2004-08-20 13:37:01 +000054#define C99_MODE (!(arg->flags & A1C_NO_C99))
Lev Walkinf15320b2004-06-03 03:38:44 +000055#define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS)
Lev Walkindd32b592004-09-06 08:07:29 +000056#define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL))
Lev Walkinf15320b2004-06-03 03:38:44 +000057
58#define PCTX_DEF INDENTED( \
59 OUT("\n"); \
60 OUT("/* Context for parsing across buffer boundaries */\n"); \
Lev Walkin05363a72004-09-29 13:16:40 +000061 OUT("asn_struct_ctx_t _asn_ctx;\n"));
Lev Walkinf15320b2004-06-03 03:38:44 +000062
Lev Walkinc8285712005-03-04 22:18:20 +000063
Lev Walkin3dcaafa2004-08-11 05:21:32 +000064#define DEPENDENCIES do { \
Lev Walkinc8285712005-03-04 22:18:20 +000065 emit_include_dependencies(arg); \
Lev Walkin3dcaafa2004-08-11 05:21:32 +000066 if(expr->expr_type == ASN_CONSTR_SET_OF) \
67 GEN_INCLUDE("asn_SET_OF"); \
68 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \
69 GEN_INCLUDE("asn_SEQUENCE_OF"); \
70} while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +000071
Lev Walkin801fabc2005-01-28 12:18:50 +000072/* MKID() without checking for reserved keywords */
Lev Walkin4e319f02005-03-03 21:58:28 +000073#define MKID_nc(id) asn1c_make_identifier(0, (id), 0)
Lev Walkin801fabc2005-01-28 12:18:50 +000074#define MKID(id) asn1c_make_identifier(AMI_CHECK_RESERVED, (id), 0)
Lev Walkinf15320b2004-06-03 03:38:44 +000075
76int
Lev Walkinc78cbfb2004-09-14 12:47:45 +000077asn1c_lang_C_type_REAL(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +000078 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
79}
80
Lev Walkine0b56e02005-02-25 12:10:27 +000081struct value2enum {
82 asn1c_integer_t value;
83 const char *name;
84 int idx;
85};
86static int compar_enumMap_byName(const void *ap, const void *bp) {
87 const struct value2enum *a = (const struct value2enum *)ap;
88 const struct value2enum *b = (const struct value2enum *)bp;
89 return strcmp(a->name, b->name);
90}
91static int compar_enumMap_byValue(const void *ap, const void *bp) {
92 const struct value2enum *a = (const struct value2enum *)ap;
93 const struct value2enum *b = (const struct value2enum *)bp;
94 if(a->value < b->value)
95 return -1;
96 else if(a->value == b->value)
97 return 0;
98 return 1;
99}
100
Lev Walkinf15320b2004-06-03 03:38:44 +0000101int
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000102asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 asn1p_expr_t *expr = arg->expr;
104 asn1p_expr_t *v;
Lev Walkine0b56e02005-02-25 12:10:27 +0000105 int el_count = expr_elements_count(arg, expr);
106 struct value2enum *v2e;
107 int map_is_extensible = (expr->expr_type == ASN_BASIC_INTEGER);
Lev Walkinf15320b2004-06-03 03:38:44 +0000108
Lev Walkine0b56e02005-02-25 12:10:27 +0000109 v2e = alloca((el_count + 1) * sizeof(*v2e));
Lev Walkinf15320b2004-06-03 03:38:44 +0000110
Lev Walkine0b56e02005-02-25 12:10:27 +0000111 /*
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000112 * For all ENUMERATED types [and for those INTEGER types which
113 * have identifiers -- prohibited by X.693:8.3.4],
114 * print out an enumeration table and a mapping
Lev Walkine0b56e02005-02-25 12:10:27 +0000115 * between identifiers and associated values.
116 */
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000117 if(expr->expr_type == ASN_BASIC_ENUMERATED
118 || (0 && el_count /* -- prohibited by X.693:8.3.4 */)) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000119 int eidx = 0;
120
121 REDIR(OT_DEPS);
Lev Walkin21d00002005-03-04 08:48:53 +0000122 OUT("typedef enum ");
123 out_name_chain(arg, 1);
124 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 TQ_FOR(v, &(expr->members), next) {
126 switch(v->expr_type) {
127 case A1TC_UNIVERVAL:
Lev Walkin21d00002005-03-04 08:48:53 +0000128 OUT("\t");
129 out_name_chain(arg, 0);
130 OUT("_%s", MKID_nc(v->Identifier));
131 OUT("\t= %" PRIdASN "%s\n",
Lev Walkine0b56e02005-02-25 12:10:27 +0000132 v->value->value.v_integer,
Lev Walkin21d00002005-03-04 08:48:53 +0000133 (eidx+1 < el_count) ? "," : "");
Lev Walkine0b56e02005-02-25 12:10:27 +0000134 v2e[eidx].name = v->Identifier;
135 v2e[eidx].value = v->value->value.v_integer;
136 eidx++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000137 break;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000138 case A1TC_EXTENSIBLE:
139 OUT("\t/*\n");
140 OUT("\t * Enumeration is extensible\n");
141 OUT("\t */\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000142 map_is_extensible = 1;
Lev Walkinc78cbfb2004-09-14 12:47:45 +0000143 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 default:
145 return -1;
146 }
147 }
Lev Walkin21d00002005-03-04 08:48:53 +0000148 OUT("} ");
149 out_name_chain(arg, 0);
150 OUT("_e;\n");
Lev Walkine0b56e02005-02-25 12:10:27 +0000151 assert(eidx == el_count);
152
153 /*
154 * Generate a enumerationName<->value map for XER codec.
155 */
156 REDIR(OT_STAT_DEFS);
157
Lev Walkin21d00002005-03-04 08:48:53 +0000158 OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_%d_value2enum[] = {\n",
159 MKID_nc(expr->Identifier),
160 expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000161 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
162 for(eidx = 0; eidx < el_count; eidx++) {
163 v2e[eidx].idx = eidx;
164 OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n",
165 v2e[eidx].value,
166 (long)strlen(v2e[eidx].name), v2e[eidx].name,
167 (eidx + 1 < el_count) ? "," : "");
168 }
169 if(map_is_extensible)
170 OUT("\t/* This list is extensible */\n");
171 OUT("};\n");
172
Lev Walkin21d00002005-03-04 08:48:53 +0000173 OUT("static unsigned int asn_MAP_%s_%d_enum2value[] = {\n",
174 MKID_nc(expr->Identifier),
175 expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000176 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
177 for(eidx = 0; eidx < el_count; eidx++) {
178 OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n",
179 v2e[eidx].idx,
180 (eidx + 1 < el_count) ? "," : "",
181 v2e[eidx].name, v2e[eidx].value);
182 }
183 if(map_is_extensible)
184 OUT("\t/* This list is extensible */\n");
185 OUT("};\n");
186
Lev Walkin21d00002005-03-04 08:48:53 +0000187 OUT("static asn_INTEGER_specifics_t asn_SPC_%s_%d_specs = {\n",
188 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000189 INDENT(+1);
Lev Walkin21d00002005-03-04 08:48:53 +0000190 OUT("asn_MAP_%s_%d_value2enum,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000191 "/* \"tag\" => N; sorted by tag */\n",
Lev Walkin21d00002005-03-04 08:48:53 +0000192 MKID_nc(expr->Identifier),
193 expr->_type_unique_index);
194 OUT("asn_MAP_%s_%d_enum2value,\t"
Lev Walkine0b56e02005-02-25 12:10:27 +0000195 "/* N => \"tag\"; sorted by N */\n",
Lev Walkin21d00002005-03-04 08:48:53 +0000196 MKID_nc(expr->Identifier),
197 expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +0000198 OUT("%d,\t/* Number of elements in the maps */\n",
199 el_count);
200 OUT("%d,\t/* Enumeration is %sextensible */\n",
201 map_is_extensible, map_is_extensible ? "": "not ");
202 if(expr->expr_type == ASN_BASIC_ENUMERATED)
203 OUT("1\t/* Strict enumeration */\n");
204 else
205 OUT("0\n");
206 INDENT(-1);
207 OUT("};\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 }
209
210 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
211}
212
213int
214asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
215 asn1p_expr_t *expr = arg->expr;
216 asn1p_expr_t *v;
217 int comp_mode = 0; /* {root,ext=1,root,root,...} */
218
219 DEPENDENCIES;
220
221 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000222 OUT("struct ");
223 out_name_chain(arg, 1);
224 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000225 } else {
226 OUT("typedef struct %s {\n",
227 MKID(expr->Identifier));
228 }
229
230 TQ_FOR(v, &(expr->members), next) {
231 if(v->expr_type == A1TC_EXTENSIBLE) {
232 if(comp_mode < 3) comp_mode++;
233 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000234 if(comp_mode == 1
235 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000236 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000237 EMBED(v);
238 }
239
240 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000241 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkin08079b02004-08-22 03:25:24 +0000242 expr->_anonymous_type ? "" : MKID(expr->Identifier),
243 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000244
245 return asn1c_lang_C_type_SEQUENCE_def(arg);
246}
247
248static int
249asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
250 asn1p_expr_t *expr = arg->expr;
251 asn1p_expr_t *v;
252 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000253 int ext_start = -1;
254 int ext_stop = -1;
Lev Walkinfd171ef2004-06-06 07:20:17 +0000255 tag2el_t *tag2el = NULL;
256 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000257 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000258 int all_tags_count;
259 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000260
Lev Walkinfd171ef2004-06-06 07:20:17 +0000261 /*
262 * Fetch every inner tag from the tag to elements map.
263 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000264 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinfd171ef2004-06-06 07:20:17 +0000265 if(tag2el) free(tag2el);
266 return -1;
267 }
268
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000269 GEN_INCLUDE("constr_SEQUENCE");
270 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000271 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000272
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000273 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000274
275 /*
276 * Print out the table according to which the parsing is performed.
277 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000278 if(expr_elements_count(arg, expr)) {
279 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000280
Lev Walkin21d00002005-03-04 08:48:53 +0000281 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
282 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000283
284 elements = 0;
285 INDENTED(TQ_FOR(v, &(expr->members), next) {
286 if(v->expr_type == A1TC_EXTENSIBLE) {
287 if((++comp_mode) == 1)
288 ext_start = elements - 1;
289 else
290 ext_stop = elements - 1;
291 continue;
292 }
293 elements++;
294 emit_member_table(arg, v);
295 });
296 OUT("};\n");
297 } else {
298 elements = 0;
299 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000300
Lev Walkin27ea3802004-06-28 21:13:46 +0000301 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000302 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000303 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000304 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000305
Lev Walkinfd171ef2004-06-06 07:20:17 +0000306 /*
307 * Tags to elements map.
308 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000309 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinfd171ef2004-06-06 07:20:17 +0000310
Lev Walkin21d00002005-03-04 08:48:53 +0000311 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_%d_specs = {\n",
312 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000313 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000314 OUT("sizeof(struct "); out_name_chain(arg, 1); OUT("),\n");
315 OUT("offsetof(struct "); out_name_chain(arg, 1); OUT(", _asn_ctx),\n");
316
317 OUT("asn_MAP_%s_%d_tag2el,\n",
318 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinfd171ef2004-06-06 07:20:17 +0000319 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000320 OUT("%d,\t/* Start extensions */\n",
321 ext_start);
322 OUT("%d\t/* Stop extensions */\n",
323 (ext_stop<ext_start)?elements+1:ext_stop, ext_stop);
324 );
325 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000326
327 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000328 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000329 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000330 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000331 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000332
Lev Walkinf15320b2004-06-03 03:38:44 +0000333 REDIR(OT_TYPE_DECLS);
334
335 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000336} /* _SEQUENCE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000337
338int
Lev Walkinf15320b2004-06-03 03:38:44 +0000339asn1c_lang_C_type_SET(arg_t *arg) {
340 asn1p_expr_t *expr = arg->expr;
341 asn1p_expr_t *v;
342 long mcount;
343 char *id;
344 int comp_mode = 0; /* {root,ext=1,root,root,...} */
345
346 DEPENDENCIES;
347
348 REDIR(OT_DEPS);
349
350 OUT("\n");
351 OUT("/*\n");
352 OUT(" * Method of determining the components presence\n");
353 OUT(" */\n");
354 mcount = 0;
Lev Walkin21d00002005-03-04 08:48:53 +0000355 OUT("typedef enum ");
356 out_name_chain(arg, 0);
357 OUT("_PR {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000358 TQ_FOR(v, &(expr->members), next) {
359 if(v->expr_type == A1TC_EXTENSIBLE) continue;
360 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000361 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000362 OUT("_PR_");
Lev Walkin4e319f02005-03-03 21:58:28 +0000363 id = MKID_nc(v->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000364 OUT("%s,\t/* Member %s is present */\n",
365 id, id)
366 );
367 mcount++;
368 }
Lev Walkin21d00002005-03-04 08:48:53 +0000369 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000370
371 REDIR(OT_TYPE_DECLS);
372
373 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000374 OUT("struct ");
375 out_name_chain(arg, 1);
376 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000377 } else {
Lev Walkin801fabc2005-01-28 12:18:50 +0000378 id = MKID(expr->Identifier);
Lev Walkin64399722004-08-11 07:17:22 +0000379 OUT("typedef struct %s {\n", id);
Lev Walkinf15320b2004-06-03 03:38:44 +0000380 }
381
382 TQ_FOR(v, &(expr->members), next) {
383 if(v->expr_type == A1TC_EXTENSIBLE) {
384 if(comp_mode < 3) comp_mode++;
385 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000386 if(comp_mode == 1
387 || expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000388 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000389 EMBED(v);
390 }
391
392 INDENTED(
Lev Walkin4e319f02005-03-03 21:58:28 +0000393 id = MKID_nc(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000394 OUT("\n");
395 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
396 id, id);
397 OUT("unsigned int _presence_map\n");
398 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
399 );
400
401 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000402 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkin08079b02004-08-22 03:25:24 +0000403 expr->_anonymous_type ? "" : MKID(expr->Identifier),
404 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000405
406 return asn1c_lang_C_type_SET_def(arg);
407}
408
Lev Walkinf15320b2004-06-03 03:38:44 +0000409static int
410asn1c_lang_C_type_SET_def(arg_t *arg) {
411 asn1p_expr_t *expr = arg->expr;
412 asn1p_expr_t *v;
413 int elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 tag2el_t *tag2el = NULL;
415 int tag2el_count = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000416 tag2el_t *tag2el_cxer = NULL;
417 int tag2el_cxer_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000418 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000419 int all_tags_count;
420 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000421 char *p;
422
423 /*
424 * Fetch every inner tag from the tag to elements map.
425 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000426 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000427 if(tag2el) free(tag2el);
428 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 }
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000430 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
431 if(tag2el) free(tag2el);
432 if(tag2el_cxer) free(tag2el_cxer);
433 return -1;
434 }
435 if(tag2el_cxer_count == tag2el_count
436 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
437 free(tag2el_cxer);
438 tag2el_cxer = 0;
439 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000440
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000441 GEN_INCLUDE("constr_SET");
442 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000443 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000444
445 REDIR(OT_STAT_DEFS);
446
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 /*
448 * Print out the table according to which the parsing is performed.
449 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000450 if(expr_elements_count(arg, expr)) {
451 int comp_mode = 0; /* {root,ext=1,root,root,...} */
Lev Walkinf15320b2004-06-03 03:38:44 +0000452
Lev Walkin21d00002005-03-04 08:48:53 +0000453 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
454 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000455
456 elements = 0;
457 INDENTED(TQ_FOR(v, &(expr->members), next) {
458 if(v->expr_type == A1TC_EXTENSIBLE) {
459 if(comp_mode < 3) comp_mode++;
460 } else {
461 if(comp_mode == 1
462 || expr_better_indirect(arg, v))
463 v->marker.flags |= EM_INDIRECT;
464 elements++;
465 emit_member_table(arg, v);
466 }
467 });
468 OUT("};\n");
469 } else {
470 elements = 0;
471 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000472
Lev Walkin27ea3802004-06-28 21:13:46 +0000473 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000474 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000475 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000476 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000477
478 /*
479 * Tags to elements map.
480 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000481 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
482 if(tag2el_cxer)
483 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
Lev Walkinf15320b2004-06-03 03:38:44 +0000484
485 /*
486 * Emit a map of mandatory elements.
487 */
Lev Walkin21d00002005-03-04 08:48:53 +0000488 OUT("static uint8_t asn_MAP_%s_%d_mmap",
489 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkin27ea3802004-06-28 21:13:46 +0000490 p = MKID(expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +0000491 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
Lev Walkin21d00002005-03-04 08:48:53 +0000492 OUT(" = {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000493 INDENTED(
494 if(elements) {
495 int delimit = 0;
496 int el = 0;
497 TQ_FOR(v, &(expr->members), next) {
498 if(v->expr_type == A1TC_EXTENSIBLE) continue;
499 if(delimit) {
500 OUT(",\n");
501 delimit = 0;
502 } else if(el) {
503 OUT(" | ");
504 }
Lev Walkincc93b0f2004-09-10 09:18:20 +0000505 OUT("(%d << %d)",
Lev Walkin0204fa62004-09-15 11:54:38 +0000506 v->marker.flags?0:1,
Lev Walkincc93b0f2004-09-10 09:18:20 +0000507 7 - (el % 8));
Lev Walkinf15320b2004-06-03 03:38:44 +0000508 if(el && (el % 8) == 0)
509 delimit = 1;
510 el++;
511 }
512 } else {
513 OUT("0");
514 }
515 );
516 OUT("\n");
517 OUT("};\n");
518
Lev Walkin21d00002005-03-04 08:48:53 +0000519 OUT("static asn_SET_specifics_t asn_SPC_%s_%d_specs = {\n",
520 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000522 OUT("sizeof(struct ");
523 out_name_chain(arg, 1);
524 OUT("),\n");
525 OUT("offsetof(struct ");
526 out_name_chain(arg, 1);
527 OUT(", _asn_ctx),\n");
528 OUT("offsetof(struct ");
529 out_name_chain(arg, 1);
530 OUT(", _presence_map),\n");
Lev Walkin4e319f02005-03-03 21:58:28 +0000531 p = MKID_nc(expr->Identifier);
Lev Walkin21d00002005-03-04 08:48:53 +0000532 OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000533 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000534 if(tag2el_cxer)
Lev Walkin21d00002005-03-04 08:48:53 +0000535 OUT("asn_MAP_%s_%d_tag2el_cxer,\n",
536 p, expr->_type_unique_index);
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000537 else
Lev Walkin21d00002005-03-04 08:48:53 +0000538 OUT("asn_MAP_%s_%d_tag2el,\t/* Same as above */\n",
539 p, expr->_type_unique_index);
540 OUT("%d,\t/* Count of tags in the CXER map */\n",
541 tag2el_cxer_count);
Lev Walkin35631482004-07-01 00:52:50 +0000542 OUT("%d,\t/* Whether extensible */\n",
543 check_if_extensible(expr));
Lev Walkin21d00002005-03-04 08:48:53 +0000544 OUT("(unsigned int *)asn_MAP_%s_%d_mmap\t/* Mandatory elements map */\n",
545 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000546 );
547 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000548
549 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000550 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000551 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000552 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000553 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000554
Lev Walkinf15320b2004-06-03 03:38:44 +0000555 REDIR(OT_TYPE_DECLS);
556
557 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000558} /* _SET_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000559
560int
Lev Walkin08079b02004-08-22 03:25:24 +0000561asn1c_lang_C_type_SEx_OF(arg_t *arg) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 asn1p_expr_t *expr = arg->expr;
Lev Walkinc8285712005-03-04 22:18:20 +0000563 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
Lev Walkinf15320b2004-06-03 03:38:44 +0000564
565 DEPENDENCIES;
566
567 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000568 OUT("struct ");
569 out_name_chain(arg, 1);
570 OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 } else {
Lev Walkin08079b02004-08-22 03:25:24 +0000572 OUT("typedef struct %s {\n", MKID(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000573 }
574
Lev Walkin08079b02004-08-22 03:25:24 +0000575 INDENT(+1);
576 OUT("A_%s_OF(",
577 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
578 ? "SET" : "SEQUENCE");
Lev Walkine0b56e02005-02-25 12:10:27 +0000579 if(memb->expr_type & ASN_CONSTR_MASK
580 || ((memb->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000581 || (0 /* -- prohibited by X.693:8.3.4 */
582 && memb->expr_type == ASN_BASIC_INTEGER))
583 && expr_elements_count(arg, memb))) {
Lev Walkin08079b02004-08-22 03:25:24 +0000584 arg_t tmp;
585 asn1p_expr_t tmp_memb;
586 arg->embed++;
587 tmp = *arg;
588 tmp.expr = &tmp_memb;
589 tmp_memb = *memb;
590 tmp_memb._anonymous_type = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000591 if(tmp_memb.Identifier == 0) {
Lev Walkin21d00002005-03-04 08:48:53 +0000592 tmp_memb.Identifier = "Member";
593 if(0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000594 tmp_memb.Identifier = strdup(
595 asn1c_make_identifier(0,
Lev Walkin152a91e2005-02-14 20:41:29 +0000596 expr->Identifier, "Member", 0));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000597 assert(tmp_memb.Identifier);
598 }
Lev Walkin08079b02004-08-22 03:25:24 +0000599 tmp.default_cb(&tmp);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000600 if(tmp_memb.Identifier != memb->Identifier)
Lev Walkin21d00002005-03-04 08:48:53 +0000601 if(0) free(tmp_memb.Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000602 arg->embed--;
603 assert(arg->target->target == OT_TYPE_DECLS);
604 } else {
Lev Walkinfe7f6ec2005-03-04 22:38:22 +0000605 OUT("%s", asn1c_type_name(arg, memb,
606 (memb->marker.flags & EM_UNRECURSE)
607 ? TNF_RSAFE : TNF_CTYPE));
Lev Walkinf15320b2004-06-03 03:38:44 +0000608 }
Lev Walkin08079b02004-08-22 03:25:24 +0000609 OUT(") list;\n");
610 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000611
612 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000613 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkin08079b02004-08-22 03:25:24 +0000614 expr->_anonymous_type ? "" : MKID(expr->Identifier),
615 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000616
617 /*
Lev Walkin08079b02004-08-22 03:25:24 +0000618 * SET OF/SEQUENCE OF definition
Lev Walkinf15320b2004-06-03 03:38:44 +0000619 */
Lev Walkin08079b02004-08-22 03:25:24 +0000620 return asn1c_lang_C_type_SEx_OF_def(arg,
621 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
Lev Walkinf15320b2004-06-03 03:38:44 +0000622}
623
624static int
625asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
626 asn1p_expr_t *expr = arg->expr;
627 asn1p_expr_t *v;
Lev Walkin64399722004-08-11 07:17:22 +0000628 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000629 int all_tags_count;
630 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000631
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000632 /*
633 * Print out the table according to which the parsing is performed.
634 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000635 if(seq_of) {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000636 GEN_INCLUDE("constr_SEQUENCE_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000637 } else {
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000638 GEN_INCLUDE("constr_SET_OF");
Lev Walkinf15320b2004-06-03 03:38:44 +0000639 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000640 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000641 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000642
643 REDIR(OT_STAT_DEFS);
644
645 /*
646 * Print out the table according to which the parsing is performed.
647 */
Lev Walkin21d00002005-03-04 08:48:53 +0000648 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
649 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000650 INDENT(+1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000651 v = TQ_FIRST(&(expr->members));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000652 if(!v->Identifier) {
Lev Walkin152a91e2005-02-14 20:41:29 +0000653 v->Identifier = strdup("Member");
Lev Walkina9cc46e2004-09-22 16:06:28 +0000654 assert(v->Identifier);
655 }
656 v->_anonymous_type = 1;
657 arg->embed++;
Lev Walkin59964be2004-08-25 02:03:12 +0000658 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000659 arg->embed--;
660 INDENT(-1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000661 OUT("};\n");
662
Lev Walkin27ea3802004-06-28 21:13:46 +0000663 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000664 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000665 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000666 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000667
Lev Walkin21d00002005-03-04 08:48:53 +0000668 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
669 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000670 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000671 OUT("sizeof(struct ");
672 out_name_chain(arg, 1);
673 OUT("),\n");
674 OUT("offsetof(struct ");
675 out_name_chain(arg, 1);
676 OUT(", _asn_ctx),\n");
677
Lev Walkina9cc46e2004-09-22 16:06:28 +0000678 if(expr_as_xmlvaluelist(arg, v))
679 OUT("1,\t/* XER encoding is XMLValueList */\n");
680 else
681 OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000682 );
683 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000684
685 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000686 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000687 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000688 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000689 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000690
Lev Walkinf15320b2004-06-03 03:38:44 +0000691 REDIR(OT_TYPE_DECLS);
692
693 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000694} /* _SEx_OF_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000695
696int
697asn1c_lang_C_type_CHOICE(arg_t *arg) {
698 asn1p_expr_t *expr = arg->expr;
699 asn1p_expr_t *v;
Lev Walkin08079b02004-08-22 03:25:24 +0000700 char *id;
Lev Walkinf15320b2004-06-03 03:38:44 +0000701
702 DEPENDENCIES;
703
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000704 REDIR(OT_DEPS);
705
Lev Walkin21d00002005-03-04 08:48:53 +0000706 OUT("typedef enum ");
707 out_name_chain(arg, 0);
708 OUT("_PR {\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000709 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000710 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000711 OUT("_PR_NOTHING,\t/* No components present */\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000712 TQ_FOR(v, &(expr->members), next) {
713 if(v->expr_type == A1TC_EXTENSIBLE) {
714 OUT("/* Extensions may appear below */\n");
715 continue;
716 }
Lev Walkin21d00002005-03-04 08:48:53 +0000717 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000718 OUT("_PR_");
Lev Walkin4e319f02005-03-03 21:58:28 +0000719 id = MKID_nc(v->Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000720 OUT("%s,\n", id, id);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000721 }
722 );
Lev Walkin21d00002005-03-04 08:48:53 +0000723 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000724
725 REDIR(OT_TYPE_DECLS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000726
727 if(arg->embed) {
Lev Walkin21d00002005-03-04 08:48:53 +0000728 OUT("struct "); out_name_chain(arg, 1); OUT(" {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000729 } else {
Lev Walkin801fabc2005-01-28 12:18:50 +0000730 id = MKID(expr->Identifier);
Lev Walkin08079b02004-08-22 03:25:24 +0000731 OUT("typedef struct %s {\n", id);
Lev Walkinf15320b2004-06-03 03:38:44 +0000732 }
733
734 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000735 out_name_chain(arg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +0000736 OUT("_PR present;\n");
737 OUT("union {\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000738 TQ_FOR(v, &(expr->members), next) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000739 if(expr_better_indirect(arg, v))
Lev Walkin0204fa62004-09-15 11:54:38 +0000740 v->marker.flags |= EM_INDIRECT;
Lev Walkinf15320b2004-06-03 03:38:44 +0000741 EMBED(v);
742 }
743 if(UNNAMED_UNIONS) OUT("};\n");
744 else OUT("} choice;\n");
745 );
746
747 PCTX_DEF;
Lev Walkinc8285712005-03-04 22:18:20 +0000748 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
Lev Walkin08079b02004-08-22 03:25:24 +0000749 expr->_anonymous_type ? "" : MKID(expr->Identifier),
750 arg->embed ? "" : "_t");
Lev Walkinf15320b2004-06-03 03:38:44 +0000751
752 return asn1c_lang_C_type_CHOICE_def(arg);
753}
754
755static int
756asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
757 asn1p_expr_t *expr = arg->expr;
758 asn1p_expr_t *v;
759 int elements; /* Number of elements */
Lev Walkinf15320b2004-06-03 03:38:44 +0000760 tag2el_t *tag2el = NULL;
761 int tag2el_count = 0;
Lev Walkin64399722004-08-11 07:17:22 +0000762 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000763 int all_tags_count;
764 enum tvm_compat tv_mode;
Lev Walkinf15320b2004-06-03 03:38:44 +0000765
766 /*
767 * Fetch every inner tag from the tag to elements map.
768 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000769 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000770 if(tag2el) free(tag2el);
771 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000772 }
773
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000774 GEN_INCLUDE("constr_CHOICE");
775 if(!arg->embed)
Lev Walkin05363a72004-09-29 13:16:40 +0000776 GEN_DECLARE(expr); /* asn_DEF_xxx */
Lev Walkinf15320b2004-06-03 03:38:44 +0000777
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000778 REDIR(OT_STAT_DEFS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000779
780 /*
781 * Print out the table according to which the parsing is performed.
782 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000783 if(expr_elements_count(arg, expr)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000784
Lev Walkin21d00002005-03-04 08:48:53 +0000785 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
786 MKID(expr->Identifier), expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000787
788 elements = 0;
789 INDENTED(TQ_FOR(v, &(expr->members), next) {
Lev Walkin659c63b2005-01-17 11:40:49 +0000790 if(v->expr_type == A1TC_EXTENSIBLE)
791 continue;
792 if(expr_better_indirect(arg, v))
793 v->marker.flags |= EM_INDIRECT;
794 elements++;
795 emit_member_table(arg, v);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000796 });
797 OUT("};\n");
798 } else {
799 elements = 0;
800 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000801
Lev Walkin64399722004-08-11 07:17:22 +0000802
Lev Walkinf15320b2004-06-03 03:38:44 +0000803 if(arg->embed) {
804 /*
805 * Our parent structure has already taken this into account.
806 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000807 tv_mode = _TVM_SAME;
808 tags_count = all_tags_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000809 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000810 tv_mode = emit_tags_vectors(arg, expr,
811 &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000812 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000813
814 /*
815 * Tags to elements map.
816 */
Lev Walkin33d5d3c2004-10-03 09:13:30 +0000817 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000818
Lev Walkin21d00002005-03-04 08:48:53 +0000819 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
820 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000821 INDENTED(
Lev Walkin21d00002005-03-04 08:48:53 +0000822 OUT("sizeof(struct ");
823 out_name_chain(arg, 1);
824 OUT("),\n");
825 OUT("offsetof(struct ");
826 out_name_chain(arg, 1);
827 OUT(", _asn_ctx),\n");
828 OUT("offsetof(struct ");
829 out_name_chain(arg, 1);
830 OUT(", present),\n");
831 OUT("sizeof(((struct ");
832 out_name_chain(arg, 1);
833 OUT(" *)0)->present),\n");
834 OUT("asn_MAP_%s_%d_tag2el,\n",
835 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +0000836 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
Lev Walkin35631482004-07-01 00:52:50 +0000837 OUT("%d\t/* Whether extensible */\n",
838 check_if_extensible(expr));
Lev Walkinf15320b2004-06-03 03:38:44 +0000839 );
840 OUT("};\n");
Lev Walkin59964be2004-08-25 02:03:12 +0000841
842 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000843 * Emit asn_DEF_xxx table.
Lev Walkin59964be2004-08-25 02:03:12 +0000844 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000845 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
Lev Walkin8de2ab22004-09-26 13:11:31 +0000846 ETD_HAS_SPECIFICS);
Lev Walkinf15320b2004-06-03 03:38:44 +0000847
Lev Walkinf15320b2004-06-03 03:38:44 +0000848 REDIR(OT_TYPE_DECLS);
849
850 return 0;
Lev Walkinb0b33412005-01-17 11:57:48 +0000851} /* _CHOICE_def() */
Lev Walkinf15320b2004-06-03 03:38:44 +0000852
853int
854asn1c_lang_C_type_REFERENCE(arg_t *arg) {
855 asn1p_ref_t *ref;
856
857 ref = arg->expr->reference;
858 if(ref->components[ref->comp_count-1].name[0] == '&') {
Lev Walkinf15320b2004-06-03 03:38:44 +0000859 asn1p_expr_t *extract;
860 arg_t tmp;
861 int ret;
862
Lev Walkina9cc46e2004-09-22 16:06:28 +0000863 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
Lev Walkin08079b02004-08-22 03:25:24 +0000864 arg->expr, ref);
Lev Walkinf15320b2004-06-03 03:38:44 +0000865 if(extract == NULL)
866 return -1;
867
Lev Walkin08079b02004-08-22 03:25:24 +0000868 extract = asn1p_expr_clone(extract, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000869 if(extract) {
870 if(extract->Identifier)
871 free(extract->Identifier);
872 extract->Identifier = strdup(arg->expr->Identifier);
873 if(extract->Identifier == NULL) {
874 asn1p_expr_free(extract);
875 return -1;
876 }
877 } else {
878 return -1;
879 }
880
881 tmp = *arg;
882 tmp.asn = arg->asn;
Lev Walkin08079b02004-08-22 03:25:24 +0000883 tmp.mod = extract->module;
Lev Walkinf15320b2004-06-03 03:38:44 +0000884 tmp.expr = extract;
885
886 ret = arg->default_cb(&tmp);
887
888 asn1p_expr_free(extract);
889
890 return ret;
891 }
892
893
894 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
895}
896
897int
898asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
899 asn1p_expr_t *expr = arg->expr;
Lev Walkin64399722004-08-11 07:17:22 +0000900 int tags_count;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000901 int all_tags_count;
902 enum tvm_compat tv_mode;
Lev Walkine0b56e02005-02-25 12:10:27 +0000903 enum etd_spec etd_spec;
Lev Walkinf15320b2004-06-03 03:38:44 +0000904 char *p;
905
906 if(arg->embed) {
Lev Walkin05363a72004-09-29 13:16:40 +0000907 enum tnfmt tnfmt = TNF_CTYPE;
908
909 /*
910 * If this is an optional compound type,
911 * refer it using "struct X" convention,
912 * as it may recursively include the current structure.
913 */
Lev Walkinc8285712005-03-04 22:18:20 +0000914 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
Lev Walkin05363a72004-09-29 13:16:40 +0000915 asn1p_expr_t *terminal;
916 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
917 if(terminal
918 && (terminal->expr_type & ASN_CONSTR_MASK)) {
Lev Walkinc8285712005-03-04 22:18:20 +0000919 tnfmt = TNF_RSAFE;
920 REDIR(OT_FWD_DECLS);
Lev Walkin05363a72004-09-29 13:16:40 +0000921 OUT("%s;\t/* Forward declaration */\n",
Lev Walkinc8285712005-03-04 22:18:20 +0000922 asn1c_type_name(arg, arg->expr, tnfmt));
Lev Walkin05363a72004-09-29 13:16:40 +0000923 }
924 }
925
Lev Walkinf15320b2004-06-03 03:38:44 +0000926 REDIR(OT_TYPE_DECLS);
927
Lev Walkinc8285712005-03-04 22:18:20 +0000928 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
Lev Walkine0b56e02005-02-25 12:10:27 +0000929 if(!expr->_anonymous_type) {
Lev Walkinc8285712005-03-04 22:18:20 +0000930 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
Lev Walkine0b56e02005-02-25 12:10:27 +0000931 OUT("%s", MKID(expr->Identifier));
932 if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT)
933 OUT("\t/* DEFAULT %s */",
934 asn1f_printable_value(
935 expr->marker.default_value));
936 else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL)
937 OUT("\t/* OPTIONAL */");
938 }
939
940 } else {
941 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
Lev Walkin59004fa2004-08-20 13:37:01 +0000942
943 REDIR(OT_TYPE_DECLS);
Lev Walkine0b56e02005-02-25 12:10:27 +0000944
945 OUT("typedef %s\t",
Lev Walkinc8285712005-03-04 22:18:20 +0000946 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
Lev Walkine0b56e02005-02-25 12:10:27 +0000947 OUT("%s%s_t",
Lev Walkinc8285712005-03-04 22:18:20 +0000948 (expr->marker.flags & EM_INDIRECT)?"*":" ",
949 MKID_nc(expr->Identifier));
Lev Walkinf15320b2004-06-03 03:38:44 +0000950 }
951
Lev Walkine0b56e02005-02-25 12:10:27 +0000952 if((expr->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin8ecf9db2005-03-03 21:28:12 +0000953 || (0 /* -- prohibited by X.693:8.3.4 */
954 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +0000955 && expr_elements_count(arg, expr)))
956 etd_spec = ETD_HAS_SPECIFICS;
957 else
958 etd_spec = ETD_NO_SPECIFICS;
Lev Walkinf15320b2004-06-03 03:38:44 +0000959
Lev Walkin8de2ab22004-09-26 13:11:31 +0000960 /*
961 * If this type just blindly refers the other type, alias it.
962 * Type1 ::= Type2
963 */
Lev Walkine0b56e02005-02-25 12:10:27 +0000964 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
965 REDIR(OT_TYPE_DECLS);
966 return 0;
967 }
Lev Walkin8de2ab22004-09-26 13:11:31 +0000968 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkine0b56e02005-02-25 12:10:27 +0000969 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
Lev Walkin6938d042005-03-04 23:23:50 +0000970 && etd_spec == ETD_NO_SPECIFICS
971 && 0 /* This shortcut is incompatible with XER */
972 ) {
Lev Walkin8de2ab22004-09-26 13:11:31 +0000973 char *type_name;
974 REDIR(OT_FUNC_DECLS);
975 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
976 OUT("/* This type is equivalent to %s */\n", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +0000977 if(HIDE_INNER_DEFS) OUT("/* ");
Lev Walkin4e319f02005-03-03 21:58:28 +0000978 OUT("#define\tasn_DEF_%s\t", MKID_nc(expr->Identifier));
Lev Walkin8de2ab22004-09-26 13:11:31 +0000979 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkine0b56e02005-02-25 12:10:27 +0000980 OUT("asn_DEF_%s", type_name);
Lev Walkin8de2ab22004-09-26 13:11:31 +0000981 if(HIDE_INNER_DEFS)
Lev Walkine0b56e02005-02-25 12:10:27 +0000982 OUT("\t// (Use -fall-defs-global to expose) */");
983 OUT("\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +0000984 REDIR(OT_CODE);
985 OUT("/* This type is equivalent to %s */\n", type_name);
986 OUT("\n");
987 REDIR(OT_TYPE_DECLS);
988 return 0;
989 }
990
Lev Walkinf15320b2004-06-03 03:38:44 +0000991 REDIR(OT_STAT_DEFS);
992
Lev Walkin27ea3802004-06-28 21:13:46 +0000993 /*
Lev Walkin05363a72004-09-29 13:16:40 +0000994 * Print out asn_DEF_<type>_[all_]tags[] vectors.
Lev Walkin27ea3802004-06-28 21:13:46 +0000995 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000996 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000997
Lev Walkine0b56e02005-02-25 12:10:27 +0000998 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
999 0, etd_spec);
Lev Walkinf15320b2004-06-03 03:38:44 +00001000
Lev Walkin59004fa2004-08-20 13:37:01 +00001001 REDIR(OT_CODE);
1002
Lev Walkinf15320b2004-06-03 03:38:44 +00001003 /*
1004 * Constraint checking.
1005 */
Lev Walkin8de2ab22004-09-26 13:11:31 +00001006 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1007 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001008 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001009 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001010 OUT("%s", p);
1011 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1012 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
Lev Walkin8de2ab22004-09-26 13:11:31 +00001013 INDENT(+1);
1014 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1015 OUT("\n");
1016 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1017 OUT("/* Replace with underlying type checker */\n");
1018 OUT("td->check_constraints "
Lev Walkin05363a72004-09-29 13:16:40 +00001019 "= asn_DEF_%s.check_constraints;\n",
Lev Walkin8de2ab22004-09-26 13:11:31 +00001020 asn1c_type_name(arg, expr, TNF_SAFE));
1021 OUT("return td->check_constraints"
1022 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin84cd58e2004-08-19 13:29:46 +00001023 }
Lev Walkin8de2ab22004-09-26 13:11:31 +00001024 INDENT(-1);
1025 OUT("}\n");
1026 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001027 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001028
1029 /*
1030 * Emit suicidal functions.
1031 */
1032
Lev Walkinf15320b2004-06-03 03:38:44 +00001033 /*
1034 * This function replaces certain fields from the definition
1035 * of a type with the corresponding fields from the basic type
1036 * (from which the current type is inherited).
1037 */
Lev Walkinf15320b2004-06-03 03:38:44 +00001038 OUT("/*\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001039 OUT(" * This type is implemented using %s,\n",
1040 asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkinb65b3002004-09-23 22:20:47 +00001041 OUT(" * so here we adjust the DEF accordingly.\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001042 OUT(" */\n");
1043 OUT("static void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001044 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
1045 MKID_nc(expr->Identifier), expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001046 INDENT(+1);
Lev Walkine0b56e02005-02-25 12:10:27 +00001047 {
Lev Walkina9cc46e2004-09-22 16:06:28 +00001048 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001049 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkin05363a72004-09-29 13:16:40 +00001050 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1051 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1052 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1053 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1054 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1055 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001056 if(!terminal && !tags_count) {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001057 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
Lev Walkin05363a72004-09-29 13:16:40 +00001058 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1059 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1060 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1061 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
Lev Walkin906654e2004-09-10 15:49:15 +00001062 OUT("/* End of these lines */\n");
1063 }
Lev Walkin05363a72004-09-29 13:16:40 +00001064 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1065 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
Lev Walkine0b56e02005-02-25 12:10:27 +00001066 if(etd_spec != ETD_NO_SPECIFICS) {
1067 INDENT(-1);
1068 OUT(" /* ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001069 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001070 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1071 if(etd_spec == ETD_NO_SPECIFICS) {
1072 INDENT(-1);
1073 OUT("\n");
1074 } else {
1075 OUT("\t// Defined explicitly */\n");
1076 }
1077 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001078 OUT("}\n");
1079 OUT("\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001080
1081 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001082 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001083 OUT("void\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001084 OUT("%s", p);
1085 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1086 OUT("_free(asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001087 INDENTED(
Lev Walkina9cc46e2004-09-22 16:06:28 +00001088 OUT("\tvoid *struct_ptr, int contents_only) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001089 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1090 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001091 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001092 );
1093 OUT("}\n");
1094 OUT("\n");
1095
1096 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001097 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinf15320b2004-06-03 03:38:44 +00001098 OUT("int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001099 OUT("%s", p);
1100 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1101 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001102 INDENTED(
1103 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001104 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1105 p, expr->_type_unique_index);
Lev Walkinf15320b2004-06-03 03:38:44 +00001106 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1107 );
1108 OUT("}\n");
1109 OUT("\n");
1110
1111 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001112 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkindc06f6b2004-10-20 15:50:55 +00001113 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001114 OUT("%s", p);
1115 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1116 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001117 INDENTED(
Lev Walkina9cc46e2004-09-22 16:06:28 +00001118 OUT("\tvoid **structure, void *bufptr, size_t size, int tag_mode) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001119 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1120 p, expr->_type_unique_index);
Lev Walkin05363a72004-09-29 13:16:40 +00001121 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001122 );
1123 OUT("}\n");
1124 OUT("\n");
1125
1126 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001127 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001128 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001129 OUT("%s", p);
1130 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1131 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001132 INDENTED(
1133 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1134 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001135 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1136 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001137 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1138 );
1139 OUT("}\n");
1140 OUT("\n");
1141
1142 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001143 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkinca47c292004-10-23 13:34:00 +00001144 OUT("asn_dec_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001145 OUT("%s", p);
1146 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1147 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
Lev Walkinca47c292004-10-23 13:34:00 +00001148 INDENTED(
1149 OUT("\tvoid **structure, const char *opt_mname, void *bufptr, size_t size) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001150 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1151 p, expr->_type_unique_index);
Lev Walkinca47c292004-10-23 13:34:00 +00001152 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1153 );
1154 OUT("}\n");
1155 OUT("\n");
1156
1157 p = MKID(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001158 if(HIDE_INNER_DEFS) OUT("static ");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001159 OUT("asn_enc_rval_t\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001160 OUT("%s", p);
1161 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1162 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
Lev Walkina9cc46e2004-09-22 16:06:28 +00001163 INDENTED(
1164 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1165 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001166 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1167 p, expr->_type_unique_index);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001168 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
Lev Walkinf15320b2004-06-03 03:38:44 +00001169 );
1170 OUT("}\n");
1171 OUT("\n");
1172
1173 REDIR(OT_FUNC_DECLS);
1174
Lev Walkin4e319f02005-03-03 21:58:28 +00001175 p = MKID_nc(expr->Identifier);
Lev Walkine0b56e02005-02-25 12:10:27 +00001176 if(HIDE_INNER_DEFS) {
Lev Walkin21d00002005-03-04 08:48:53 +00001177 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1178 "\t// (Use -fall-defs-global to expose) */\n",
1179 p, expr->_type_unique_index);
Lev Walkine0b56e02005-02-25 12:10:27 +00001180 } else {
1181 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1182 OUT("asn_struct_free_f %s_free;\n", p);
1183 OUT("asn_struct_print_f %s_print;\n", p);
1184 OUT("asn_constr_check_f %s_constraint;\n", p);
1185 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1186 OUT("der_type_encoder_f %s_encode_der;\n", p);
1187 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1188 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1189 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001190
Lev Walkin3dcaafa2004-08-11 05:21:32 +00001191 REDIR(OT_TYPE_DECLS);
1192
Lev Walkinf15320b2004-06-03 03:38:44 +00001193 return 0;
1194}
1195
1196int
1197asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1198
1199 OUT("/*\n");
1200 OUT(" * This type is extensible,\n");
1201 OUT(" * possible extensions are below.\n");
1202 OUT(" */\n");
1203
1204 return 0;
1205}
1206
Lev Walkin35631482004-07-01 00:52:50 +00001207static int check_if_extensible(asn1p_expr_t *expr) {
1208 asn1p_expr_t *v;
1209 TQ_FOR(v, &(expr->members), next) {
1210 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1211 }
1212 return 0;
1213}
1214
Lev Walkinf15320b2004-06-03 03:38:44 +00001215static int
Lev Walkinc3e29402004-09-10 06:07:18 +00001216_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001217
1218 OUT("(");
Lev Walkinc3e29402004-09-10 06:07:18 +00001219 switch(tag->tag_class) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001220 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1221 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1222 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1223 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1224 case TC_NOCLASS:
1225 break;
1226 }
Lev Walkin33c16ba2004-09-24 21:01:43 +00001227 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
Lev Walkinf15320b2004-06-03 03:38:44 +00001228
1229 return 0;
1230}
1231
Lev Walkinfd171ef2004-06-06 07:20:17 +00001232
1233static int
1234_tag2el_cmp(const void *ap, const void *bp) {
1235 const tag2el_t *a = ap;
1236 const tag2el_t *b = bp;
1237 const struct asn1p_type_tag_s *ta = &a->el_tag;
1238 const struct asn1p_type_tag_s *tb = &b->el_tag;
1239
1240 if(ta->tag_class == tb->tag_class) {
1241 if(ta->tag_value == tb->tag_value) {
1242 /*
1243 * Sort by their respective positions.
1244 */
1245 if(a->el_no < b->el_no)
1246 return -1;
1247 else if(a->el_no > b->el_no)
1248 return 1;
1249 return 0;
1250 } else if(ta->tag_value < tb->tag_value)
1251 return -1;
1252 else
1253 return 1;
1254 } else if(ta->tag_class < tb->tag_class) {
1255 return -1;
1256 } else {
1257 return 1;
1258 }
1259}
1260
Lev Walkinf15320b2004-06-03 03:38:44 +00001261/*
1262 * For constructed types, number of external tags may be greater than
1263 * number of elements in the type because of CHOICE type.
1264 * T ::= SET { -- Three possible tags:
1265 * a INTEGER, -- One tag is here...
1266 * b Choice1 -- ... and two more tags are there.
1267 * }
1268 * Choice1 ::= CHOICE {
1269 * s1 IA5String,
1270 * s2 ObjectDescriptor
1271 * }
1272 */
1273static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001274_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001275 asn1p_expr_t *expr = arg->expr;
1276 arg_t tmparg = *arg;
1277 asn1p_expr_t *v;
1278 int element = 0;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001279 int original_count = *count;
1280 int sort_until = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001281
1282 TQ_FOR(v, &(expr->members), next) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001283 if(v->expr_type == A1TC_EXTENSIBLE) {
1284 /*
Lev Walkine0b56e02005-02-25 12:10:27 +00001285 * CXER mandates sorting
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001286 * only for the root part.
1287 */
1288 if(flags == FTE_CANONICAL_XER
1289 && sort_until == -1)
1290 sort_until = *count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001291 continue;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001292 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001293
1294 tmparg.expr = v;
1295
1296 if(_add_tag2el_member(&tmparg, tag2el, count,
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001297 (el_no==-1)?element:el_no, flags)) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001298 return -1;
1299 }
1300
1301 element++;
1302 }
1303
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001304
1305 if(flags == FTE_CANONICAL_XER) {
1306 if(sort_until == -1) sort_until = *count;
1307 qsort((*tag2el) + original_count,
1308 sort_until - original_count,
1309 sizeof(**tag2el), _tag2el_cmp);
1310 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1311 && (sort_until - original_count) >= 1) {
1312 /* Only take in account the root component */
1313 *count = original_count + 1;
1314 }
1315 } else {
1316 /*
1317 * Sort the map according to canonical order of their
1318 * tags and element numbers.
1319 */
1320 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1321 }
Lev Walkinfd171ef2004-06-06 07:20:17 +00001322
Lev Walkin38abe792004-06-14 13:09:45 +00001323 /*
1324 * Initialize .toff_{first|last} members.
1325 */
1326 if(*count) {
1327 struct asn1p_type_tag_s *cur_tag = 0;
1328 tag2el_t *cur = *tag2el;
1329 tag2el_t *end = cur + *count;
1330 int occur, i;
1331 for(occur = 0; cur < end; cur++) {
1332 if(cur_tag == 0
1333 || cur_tag->tag_value != cur->el_tag.tag_value
1334 || cur_tag->tag_class != cur->el_tag.tag_class) {
1335 cur_tag = &cur->el_tag;
1336 occur = 0;
1337 } else {
1338 occur++;
1339 }
1340 cur->toff_first = -occur;
1341 for(i = 0; i >= -occur; i--)
1342 cur[i].toff_last = -i;
1343 }
1344 }
1345
Lev Walkinf15320b2004-06-03 03:38:44 +00001346 return 0;
1347}
1348
1349static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001350_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001351 struct asn1p_type_tag_s tag;
1352 int ret;
1353
1354 assert(el_no >= 0);
1355
Lev Walkina9cc46e2004-09-22 16:06:28 +00001356 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1357 arg->expr, &tag, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001358 if(ret == 0) {
Lev Walkin201943e2004-09-04 04:42:29 +00001359 tag2el_t *te;
1360 int new_count = (*count) + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001361 void *p;
Lev Walkin201943e2004-09-04 04:42:29 +00001362
Lev Walkinac7e2292004-09-05 10:42:33 +00001363 if(tag.tag_value == -1) {
1364 /*
1365 * This is an untagged ANY type,
1366 * proceed without adding a tag
1367 */
1368 return 0;
1369 }
1370
Lev Walkin201943e2004-09-04 04:42:29 +00001371 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
Lev Walkinf15320b2004-06-03 03:38:44 +00001372 if(p) *tag2el = p;
1373 else return -1;
1374
1375 DEBUG("Found tag for %s: %ld",
1376 arg->expr->Identifier,
1377 (long)tag.tag_value);
1378
Lev Walkin201943e2004-09-04 04:42:29 +00001379 te = &((*tag2el)[*count]);
1380 te->el_tag = tag;
1381 te->el_no = el_no;
1382 te->from_expr = arg->expr;
1383 *count = new_count;
Lev Walkinf15320b2004-06-03 03:38:44 +00001384 return 0;
1385 }
1386
1387 DEBUG("Searching tag in complex expression %s:%x at line %d",
1388 arg->expr->Identifier,
1389 arg->expr->expr_type,
1390 arg->expr->_lineno);
1391
1392 /*
1393 * Iterate over members of CHOICE type.
1394 */
1395 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001396 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001397 }
1398
1399 if(arg->expr->expr_type == A1TC_REFERENCE) {
1400 arg_t tmp = *arg;
1401 asn1p_expr_t *expr;
Lev Walkin08079b02004-08-22 03:25:24 +00001402 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001403 arg->expr->reference);
1404 if(expr) {
Lev Walkin08079b02004-08-22 03:25:24 +00001405 tmp.mod = expr->module;
Lev Walkinf15320b2004-06-03 03:38:44 +00001406 tmp.expr = expr;
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001407 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
Lev Walkinf15320b2004-06-03 03:38:44 +00001408 } else {
1409 FATAL("Cannot dereference %s at line %d",
1410 arg->expr->Identifier,
1411 arg->expr->_lineno);
1412 return -1;
1413 }
1414 }
1415
1416 DEBUG("No tag for %s at line %d",
1417 arg->expr->Identifier,
1418 arg->expr->_lineno);
1419
1420 return -1;
1421}
1422
1423static int
Lev Walkin33d5d3c2004-10-03 09:13:30 +00001424emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
Lev Walkinfd171ef2004-06-06 07:20:17 +00001425 asn1p_expr_t *expr = arg->expr;
1426
Lev Walkin21d00002005-03-04 08:48:53 +00001427 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
1428 MKID_nc(expr->Identifier), expr->_type_unique_index,
1429 opt_modifier?opt_modifier:"");
Lev Walkinfd171ef2004-06-06 07:20:17 +00001430 if(tag2el_count) {
1431 int i;
1432 for(i = 0; i < tag2el_count; i++) {
1433 OUT(" { ");
Lev Walkinc3e29402004-09-10 06:07:18 +00001434 _print_tag(arg, &tag2el[i].el_tag);
Lev Walkinfd171ef2004-06-06 07:20:17 +00001435 OUT(", ");
Lev Walkin38abe792004-06-14 13:09:45 +00001436 OUT("%d, ", tag2el[i].el_no);
1437 OUT("%d, ", tag2el[i].toff_first);
1438 OUT("%d ", tag2el[i].toff_last);
Lev Walkine0b56e02005-02-25 12:10:27 +00001439 OUT("}%s /* %s at %d */\n",
1440 (i + 1 < tag2el_count) ? "," : "",
Lev Walkinfd171ef2004-06-06 07:20:17 +00001441 tag2el[i].from_expr->Identifier,
1442 tag2el[i].from_expr->_lineno
1443 );
1444 }
1445 }
1446 OUT("};\n");
1447
1448 return 0;;
1449}
1450
Lev Walkin188ed2c2004-09-13 08:31:01 +00001451static enum tvm_compat
1452emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1453 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1454 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
Lev Walkin64399722004-08-11 07:17:22 +00001455 int tags_count = 0;
Lev Walkin188ed2c2004-09-13 08:31:01 +00001456 int all_tags_count = 0;
1457 enum tvm_compat tv_mode = _TVM_SAME;
Lev Walkin906654e2004-09-10 15:49:15 +00001458 int i;
Lev Walkin27ea3802004-06-28 21:13:46 +00001459
Lev Walkin6d1220a2004-09-14 14:10:10 +00001460 /* Cleanup before proceeding. */
1461 *tags_count_r = 0;
1462 *all_tags_count_r = 0;
1463
Lev Walkin906654e2004-09-10 15:49:15 +00001464 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001465 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
Lev Walkin6d1220a2004-09-14 14:10:10 +00001466 if(tags_count < 0)
1467 return -1;
Lev Walkin64399722004-08-11 07:17:22 +00001468
Lev Walkin188ed2c2004-09-13 08:31:01 +00001469 /* Fetch a chain of tags */
Lev Walkina9cc46e2004-09-22 16:06:28 +00001470 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
Lev Walkin188ed2c2004-09-13 08:31:01 +00001471 &all_tags, AFT_FULL_COLLECT);
1472 if(all_tags_count < 0) {
1473 if(tags) free(tags);
1474 return -1;
Lev Walkin5a8219a2004-09-08 00:28:57 +00001475 }
Lev Walkin906654e2004-09-10 15:49:15 +00001476
Lev Walkin188ed2c2004-09-13 08:31:01 +00001477 assert(tags_count <= all_tags_count);
1478 assert((tags_count?0:1) == (all_tags_count?0:1));
Lev Walkin27ea3802004-06-28 21:13:46 +00001479
Lev Walkin188ed2c2004-09-13 08:31:01 +00001480 if(tags_count <= all_tags_count) {
1481 for(i = 0; i < tags_count; i++) {
1482 if(tags[i].tag_value != all_tags[i].tag_value
1483 || tags[i].tag_class != all_tags[i].tag_class) {
1484 tv_mode = _TVM_DIFFERENT;
1485 break;
1486 }
1487 }
1488 if(i == tags_count && tags_count < all_tags_count)
1489 tv_mode = _TVM_SUBSET;
1490 } else {
1491 tv_mode = _TVM_DIFFERENT;
1492 }
1493
1494#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
Lev Walkin21d00002005-03-04 08:48:53 +00001495 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
1496 MKID_nc(expr->Identifier), \
1497 expr->_type_unique_index, name); \
Lev Walkin188ed2c2004-09-13 08:31:01 +00001498 INDENT(+1); \
1499 /* Print the array of collected tags */ \
1500 for(i = 0; i < tags_count; i++) { \
1501 if(i) OUT(",\n"); \
1502 _print_tag(arg, &tags[i]); \
1503 } \
1504 OUT("\n"); \
1505 INDENT(-1); \
1506 OUT("};\n"); \
1507 } while(0)
1508
1509 if(tags_count) {
1510 if(tv_mode == _TVM_SUBSET)
1511 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1512 else
1513 EMIT_TAGS_TABLE("", tags, tags_count);
1514 }
1515
1516 if(all_tags_count) {
1517 if(tv_mode == _TVM_DIFFERENT)
1518 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1519 }
1520
1521 if(tags) free(tags);
1522 if(all_tags) free(all_tags);
1523
1524 *tags_count_r = tags_count;
1525 *all_tags_count_r = all_tags_count;
1526
1527 return tv_mode;
Lev Walkin27ea3802004-06-28 21:13:46 +00001528}
Lev Walkin59004fa2004-08-20 13:37:01 +00001529
1530static int
Lev Walkin59964be2004-08-25 02:03:12 +00001531expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001532 asn1p_expr_t *topmost_parent;
1533 asn1p_expr_t *v;
1534 int elements = 0;
1535
Lev Walkina9cc46e2004-09-22 16:06:28 +00001536 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkin59004fa2004-08-20 13:37:01 +00001537 if(!topmost_parent) return 0;
1538
Lev Walkine0b56e02005-02-25 12:10:27 +00001539 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1540 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1541 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
Lev Walkin59004fa2004-08-20 13:37:01 +00001542 return 0;
1543
1544 TQ_FOR(v, &(topmost_parent->members), next) {
1545 if(v->expr_type != A1TC_EXTENSIBLE)
1546 elements++;
1547 }
1548
1549 return elements;
1550}
1551
1552static int
Lev Walkinc8285712005-03-04 22:18:20 +00001553emit_include_dependencies(arg_t *arg) {
1554 asn1p_expr_t *expr = arg->expr;
1555 asn1p_expr_t *memb;
1556
1557 TQ_FOR(memb, &(expr->members), next) {
1558
1559 if((memb->meta_type == AMT_TYPEREF
1560 && (memb->marker.flags & EM_INDIRECT))
1561 || expr->expr_type == ASN_CONSTR_SET_OF
1562 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1563 ) {
1564 asn1p_expr_t *terminal;
1565 terminal = asn1f_find_terminal_type_ex(arg->asn, memb);
1566 if(terminal && !terminal->parent_expr
1567 && (terminal->expr_type & ASN_CONSTR_MASK)) {
1568 int saved_target = arg->target->target;
1569 REDIR(OT_FWD_DECLS);
1570 OUT("%s;\t/* Forward declaration */\n",
1571 asn1c_type_name(arg, memb, TNF_RSAFE));
1572 REDIR(saved_target);
1573 memb->marker.flags |= EM_UNRECURSE;
1574 }
1575 }
1576
1577 if((!(memb->expr_type & ASN_CONSTR_MASK)
1578 && memb->expr_type > ASN_CONSTR_MASK)
1579 || memb->meta_type == AMT_TYPEREF) {
1580 if(memb->marker.flags & EM_UNRECURSE) {
1581 GEN_POSTINCLUDE(asn1c_type_name(arg,
1582 memb, TNF_INCLUDE));
1583 } else {
1584 GEN_INCLUDE(asn1c_type_name(arg,
1585 memb, TNF_INCLUDE));
1586 }
1587 }
1588 }
1589
1590 return 0;
1591}
1592
1593static int
Lev Walkin59964be2004-08-25 02:03:12 +00001594emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001595 int save_target;
1596 arg_t tmp_arg;
Lev Walkinb9189732004-09-10 09:37:12 +00001597 struct asn1p_type_tag_s outmost_tag_s;
1598 struct asn1p_type_tag_s *outmost_tag;
Lev Walkine0b56e02005-02-25 12:10:27 +00001599 int complex_contents;
Lev Walkin59004fa2004-08-20 13:37:01 +00001600 char *p;
1601
Lev Walkinb9189732004-09-10 09:37:12 +00001602 if(asn1f_fetch_outmost_tag(arg->asn,
1603 expr->module, expr, &outmost_tag_s, 1)) {
1604 outmost_tag = 0;
1605 } else {
1606 outmost_tag = &outmost_tag_s;
1607 }
1608
Lev Walkin59004fa2004-08-20 13:37:01 +00001609 OUT("{ ");
Lev Walkinb9189732004-09-10 09:37:12 +00001610
1611 if(outmost_tag && outmost_tag->tag_value == -1)
1612 OUT("ATF_OPEN_TYPE | ");
Lev Walkinc8285712005-03-04 22:18:20 +00001613 OUT("%s, ",
1614 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
Lev Walkin0204fa62004-09-15 11:54:38 +00001615 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
Lev Walkin59004fa2004-08-20 13:37:01 +00001616 asn1p_expr_t *tv;
1617 int opts = 0;
Lev Walkin0204fa62004-09-15 11:54:38 +00001618 for(tv = expr; tv && tv->marker.flags;
Lev Walkin59004fa2004-08-20 13:37:01 +00001619 tv = TQ_NEXT(tv, next), opts++) {
1620 if(tv->expr_type == A1TC_EXTENSIBLE)
1621 opts--;
1622 }
1623 OUT("%d, ", opts);
1624 } else {
1625 OUT("0, ");
1626 }
Lev Walkina9cc46e2004-09-22 16:06:28 +00001627 if(expr->_anonymous_type) {
1628 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1629 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1630 OUT("0,\n");
1631 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001632 OUT("offsetof(struct ");
1633 out_name_chain(arg, 1);
1634 OUT(", ");
Lev Walkin59004fa2004-08-20 13:37:01 +00001635 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1636 && (!UNNAMED_UNIONS)) OUT("choice.");
1637 OUT("%s),\n", MKID(expr->Identifier));
Lev Walkin59004fa2004-08-20 13:37:01 +00001638 }
1639 INDENT(+1);
1640 if(C99_MODE) OUT(".tag = ");
Lev Walkinb9189732004-09-10 09:37:12 +00001641 if(outmost_tag) {
1642 if(outmost_tag->tag_value == -1)
1643 OUT("-1 /* Ambiguous tag (ANY?) */");
1644 else
1645 _print_tag(arg, outmost_tag);
Lev Walkinc3e29402004-09-10 06:07:18 +00001646 } else {
Lev Walkinb9189732004-09-10 09:37:12 +00001647 OUT("-1 /* Ambiguous tag (CHOICE?) */");
Lev Walkinc3e29402004-09-10 06:07:18 +00001648 }
Lev Walkinb9189732004-09-10 09:37:12 +00001649
Lev Walkin59004fa2004-08-20 13:37:01 +00001650 OUT(",\n");
1651 if(C99_MODE) OUT(".tag_mode = ");
1652 if(expr->tag.tag_class) {
1653 if(expr->tag.tag_mode == TM_IMPLICIT)
1654 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1655 else
1656 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1657 } else {
1658 OUT("0,\n");
1659 }
Lev Walkine0b56e02005-02-25 12:10:27 +00001660
1661 complex_contents =
1662 (expr->expr_type & ASN_CONSTR_MASK)
1663 || expr->expr_type == ASN_BASIC_ENUMERATED
Lev Walkin8ecf9db2005-03-03 21:28:12 +00001664 || (0 /* -- prohibited by X.693:8.3.4 */
1665 && expr->expr_type == ASN_BASIC_INTEGER
Lev Walkine0b56e02005-02-25 12:10:27 +00001666 && expr_elements_count(arg, expr));
Lev Walkin59004fa2004-08-20 13:37:01 +00001667 if(C99_MODE) OUT(".type = ");
Lev Walkin21d00002005-03-04 08:48:53 +00001668 OUT("(void *)&asn_DEF_");
1669 if(complex_contents) {
1670 OUT("%s", MKID_nc(expr->Identifier));
1671 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1672 OUT("_%d", expr->_type_unique_index);
Lev Walkin08079b02004-08-22 03:25:24 +00001673 } else {
Lev Walkin21d00002005-03-04 08:48:53 +00001674 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
Lev Walkin08079b02004-08-22 03:25:24 +00001675 }
Lev Walkin21d00002005-03-04 08:48:53 +00001676 OUT(",\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001677 if(C99_MODE) OUT(".memb_constraints = ");
1678 if(expr->constraints) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001679 if(arg->flags & A1C_NO_CONSTRAINTS) {
1680 OUT("0,\t/* No check because of -fno-constraints */\n");
1681 } else {
Lev Walkin4e319f02005-03-03 21:58:28 +00001682 char *id = MKID_nc(expr->Identifier);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001683 if(expr->_anonymous_type
Lev Walkin152a91e2005-02-14 20:41:29 +00001684 && !strcmp(expr->Identifier, "Member"))
Lev Walkin8de2ab22004-09-26 13:11:31 +00001685 id = asn1c_type_name(arg, expr, TNF_SAFE);
1686 OUT("memb_%s_%d_constraint,\n", id,
Lev Walkin21d00002005-03-04 08:48:53 +00001687 arg->expr->_type_unique_index);
Lev Walkin8de2ab22004-09-26 13:11:31 +00001688 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001689 } else {
Lev Walkinb7bfd1a2005-01-17 12:16:58 +00001690 OUT("0,\t/* Defer constraints checking to the member type */\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001691 }
1692 if(C99_MODE) OUT(".name = ");
Lev Walkin152a91e2005-02-14 20:41:29 +00001693 if(1) {
1694 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1695 OUT("\"\"\n");
1696 else
1697 OUT("\"%s\"\n", expr->Identifier);
1698 } else {
1699 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1700 }
Lev Walkin59004fa2004-08-20 13:37:01 +00001701 OUT("},\n");
1702 INDENT(-1);
1703
Lev Walkin8de2ab22004-09-26 13:11:31 +00001704 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
Lev Walkin59004fa2004-08-20 13:37:01 +00001705 return 0;
1706
1707 save_target = arg->target->target;
1708 REDIR(OT_CODE);
1709
Lev Walkin152a91e2005-02-14 20:41:29 +00001710 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
Lev Walkin59004fa2004-08-20 13:37:01 +00001711 p = asn1c_type_name(arg, expr, TNF_SAFE);
Lev Walkina9cc46e2004-09-22 16:06:28 +00001712 else
Lev Walkin4e319f02005-03-03 21:58:28 +00001713 p = MKID_nc(expr->Identifier);
Lev Walkin59004fa2004-08-20 13:37:01 +00001714 OUT("static int\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001715 OUT("memb_%s_%d_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n", p, arg->expr->_type_unique_index);
Lev Walkin59004fa2004-08-20 13:37:01 +00001716 INDENT(+1);
1717 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1718 tmp_arg = *arg;
1719 tmp_arg.expr = expr;
1720 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
Lev Walkin8de2ab22004-09-26 13:11:31 +00001721 OUT("return td->check_constraints"
1722 "(td, sptr, app_errlog, app_key);\n");
Lev Walkin59004fa2004-08-20 13:37:01 +00001723 }
1724 INDENT(-1);
1725 OUT("}\n");
1726 OUT("\n");
1727
1728 REDIR(save_target);
1729
1730 return 0;
1731}
Lev Walkin59964be2004-08-25 02:03:12 +00001732
Lev Walkindc06f6b2004-10-20 15:50:55 +00001733/*
1734 * Generate "asn_DEF_XXX" type definition.
1735 */
Lev Walkin59964be2004-08-25 02:03:12 +00001736static int
Lev Walkin8de2ab22004-09-26 13:11:31 +00001737emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_count, int all_tags_count, int elements_count, enum etd_spec spec) {
Lev Walkin21d00002005-03-04 08:48:53 +00001738 int using_type_name = 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001739 char *p;
1740
Lev Walkindd32b592004-09-06 08:07:29 +00001741 if(HIDE_INNER_DEFS)
1742 OUT("static /* Use -fall-defs-global to expose */\n");
Lev Walkin21d00002005-03-04 08:48:53 +00001743 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID_nc(expr->Identifier));
1744 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1745 OUT(" = {\n");
Lev Walkin801fabc2005-01-28 12:18:50 +00001746 p = MKID(expr->Identifier);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001747 INDENT(+1);
Lev Walkin59964be2004-08-25 02:03:12 +00001748 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001749 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001750
1751 if(expr->expr_type & ASN_CONSTR_MASK) {
Lev Walkin21d00002005-03-04 08:48:53 +00001752 using_type_name = 1;
Lev Walkin59964be2004-08-25 02:03:12 +00001753 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1754 }
1755
Lev Walkin21d00002005-03-04 08:48:53 +00001756#define FUNCREF(foo) do { \
1757 OUT("%s", p); \
1758 if(HIDE_INNER_DEFS && !using_type_name) \
1759 OUT("_%d", expr->_type_unique_index); \
1760 OUT("_" #foo ",\n"); \
1761} while(0)
1762
1763 FUNCREF(free);
1764 FUNCREF(print);
1765 FUNCREF(constraint);
1766 FUNCREF(decode_ber);
1767 FUNCREF(encode_der);
1768 FUNCREF(decode_xer);
1769 FUNCREF(encode_xer);
Lev Walkin59964be2004-08-25 02:03:12 +00001770
Lev Walkin59964be2004-08-25 02:03:12 +00001771 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1772 OUT("CHOICE_outmost_tag,\n");
1773 } else {
1774 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1775 }
1776
Lev Walkin4e319f02005-03-03 21:58:28 +00001777 p = MKID_nc(expr->Identifier);
Lev Walkin59964be2004-08-25 02:03:12 +00001778 if(tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001779 OUT("asn_DEF_%s_%d_tags,\n",
1780 p, expr->_type_unique_index);
1781 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1782 p, expr->_type_unique_index);
1783 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1784 p, expr->_type_unique_index);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001785 if(tv_mode == _TVM_SUBSET
1786 && tags_count != all_tags_count)
1787 OUT(" - %d", all_tags_count - tags_count);
1788 OUT(", /* %d */\n", tags_count);
Lev Walkin59964be2004-08-25 02:03:12 +00001789 } else {
Lev Walkin188ed2c2004-09-13 08:31:01 +00001790 OUT("0,\t/* No effective tags (pointer) */\n");
1791 OUT("0,\t/* No effective tags (count) */\n");
1792 }
1793
1794 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
Lev Walkin21d00002005-03-04 08:48:53 +00001795 OUT("asn_DEF_%s_%d_all_tags,\n",
1796 p, expr->_type_unique_index);
1797 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1798 p, expr->_type_unique_index);
1799 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1800 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001801 } else if(all_tags_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001802 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1803 p, expr->_type_unique_index);
1804 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1805 p, expr->_type_unique_index);
1806 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1807 p, expr->_type_unique_index, all_tags_count);
Lev Walkin188ed2c2004-09-13 08:31:01 +00001808 } else {
1809 OUT("0,\t/* No tags (pointer) */\n");
1810 OUT("0,\t/* No tags (count) */\n");
Lev Walkin59964be2004-08-25 02:03:12 +00001811 }
1812
Lev Walkin59964be2004-08-25 02:03:12 +00001813 if(elements_count) {
Lev Walkin21d00002005-03-04 08:48:53 +00001814 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001815 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1816 || expr->expr_type == ASN_CONSTR_SET_OF) {
1817 OUT("%d,\t/* Single element */\n",
1818 elements_count);
1819 assert(elements_count == 1);
1820 } else {
1821 OUT("%d,\t/* Elements count */\n",
1822 elements_count);
1823 }
1824 } else {
Lev Walkin59964be2004-08-25 02:03:12 +00001825 if(expr_elements_count(arg, expr))
1826 OUT("0, 0,\t/* Defined elsewhere */\n");
1827 else
1828 OUT("0, 0,\t/* No members */\n");
1829 }
1830
1831 switch(spec) {
1832 case ETD_NO_SPECIFICS:
1833 OUT("0\t/* No specifics */\n");
1834 break;
1835 case ETD_HAS_SPECIFICS:
Lev Walkin21d00002005-03-04 08:48:53 +00001836 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1837 p, expr->_type_unique_index);
Lev Walkin59964be2004-08-25 02:03:12 +00001838 }
Lev Walkin188ed2c2004-09-13 08:31:01 +00001839 INDENT(-1);
Lev Walkin59964be2004-08-25 02:03:12 +00001840 OUT("};\n");
1841 OUT("\n");
Lev Walkin97298782004-08-26 06:20:34 +00001842
1843 return 0;
Lev Walkin59964be2004-08-25 02:03:12 +00001844}
Lev Walkincc93b0f2004-09-10 09:18:20 +00001845
1846/*
1847 * Check if it is better to make this type indirectly accessed via
1848 * a pointer.
1849 * This may be the case for the following recursive definition:
1850 * Type ::= CHOICE { member Type };
1851 */
1852static int
1853expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) {
1854 asn1p_expr_t *top_parent;
1855 asn1p_expr_t *terminal;
1856
1857 if(expr->expr_type != A1TC_REFERENCE)
1858 return 0;
1859
1860 /* Rewind to the topmost parent expression */
1861 if((top_parent = expr->parent_expr)) {
1862 while(top_parent->parent_expr)
1863 top_parent = top_parent->parent_expr;
1864 } else {
1865 return 0;
1866 }
1867
Lev Walkina9cc46e2004-09-22 16:06:28 +00001868 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
Lev Walkincc93b0f2004-09-10 09:18:20 +00001869
1870 return (terminal == top_parent);
1871}
Lev Walkina9cc46e2004-09-22 16:06:28 +00001872
1873static int
1874expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1875 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1876 if(!expr) return 0;
1877
1878 /* X.680, 25.5, Table 5 */
1879 switch(expr->expr_type) {
1880 case ASN_CONSTR_CHOICE:
1881 case ASN_BASIC_BOOLEAN:
1882 case ASN_BASIC_ENUMERATED:
1883 case ASN_BASIC_NULL:
1884 return 1;
1885 default:
1886 return 0;
1887 }
1888}
Lev Walkin801fabc2005-01-28 12:18:50 +00001889
1890static int
Lev Walkin21d00002005-03-04 08:48:53 +00001891out_name_chain(arg_t *arg, int check_reserved_keywords) {
Lev Walkin801fabc2005-01-28 12:18:50 +00001892 asn1p_expr_t *expr = arg->expr;
1893 char *id;
1894
1895 assert(expr->Identifier);
1896
Lev Walkin21d00002005-03-04 08:48:53 +00001897 if(arg->flags & A1C_COMPOUND_NAMES
1898 && ((expr->expr_type & ASN_CONSTR_MASK)
1899 || expr->expr_type == ASN_BASIC_ENUMERATED
1900 || (expr->expr_type == ASN_BASIC_INTEGER
1901 && expr_elements_count(arg, expr))
1902 )
Lev Walkin801fabc2005-01-28 12:18:50 +00001903 && expr->parent_expr
1904 && expr->parent_expr->Identifier) {
1905 arg_t tmparg = *arg;
1906
1907 tmparg.expr = expr->parent_expr;
Lev Walkin21d00002005-03-04 08:48:53 +00001908 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
1909
1910 out_name_chain(&tmparg, 0);
Lev Walkin801fabc2005-01-28 12:18:50 +00001911
1912 OUT("_"); /* a separator between id components */
Lev Walkin21d00002005-03-04 08:48:53 +00001913
Lev Walkin801fabc2005-01-28 12:18:50 +00001914 /* Fall through */
1915 }
1916
1917 if(check_reserved_keywords)
1918 id = MKID(expr->Identifier);
1919 else
Lev Walkin4e319f02005-03-03 21:58:28 +00001920 id = MKID_nc(expr->Identifier);
Lev Walkin801fabc2005-01-28 12:18:50 +00001921 OUT("%s", id);
1922
1923 return 0;
1924}