blob: b8a74288380c75e69509ebf7a55419dcb1cf8f41 [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
vlmfa67ddc2004-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);
vlma8a86cc2004-09-10 06:07:18 +000031static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p);
vlm79b08d52004-07-01 00:52:50 +000032static int check_if_extensible(asn1p_expr_t *expr);
vlmddd5a7d2004-09-10 09:18:20 +000033static int expr_better_indirect(arg_t *arg, asn1p_expr_t *expr);
vlm39ba4c42004-09-22 16:06:28 +000034static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr);
vlm4e554992004-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);
vlm940bc6b2004-10-03 09:13:30 +000037static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier);
vlm0b567bf2005-03-04 22:18:20 +000038static int emit_include_dependencies(arg_t *arg);
vlm766cf1e2005-03-04 08:48:53 +000039static int out_name_chain(arg_t *arg, int check_reserved_keywords);
vlmfa67ddc2004-06-03 03:38:44 +000040
vlm72425de2004-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
vlm4e554992004-08-25 02:03:12 +000048enum etd_spec {
49 ETD_NO_SPECIFICS,
50 ETD_HAS_SPECIFICS
51};
vlm86f5ed22004-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);
vlm4e554992004-08-25 02:03:12 +000053
vlmb2839012004-08-20 13:37:01 +000054#define C99_MODE (!(arg->flags & A1C_NO_C99))
vlmfa67ddc2004-06-03 03:38:44 +000055#define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS)
vlm12c8f692004-09-06 08:07:29 +000056#define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL))
vlmfa67ddc2004-06-03 03:38:44 +000057
58#define PCTX_DEF INDENTED( \
59 OUT("\n"); \
60 OUT("/* Context for parsing across buffer boundaries */\n"); \
vlma5dcb912004-09-29 13:16:40 +000061 OUT("asn_struct_ctx_t _asn_ctx;\n"));
vlmfa67ddc2004-06-03 03:38:44 +000062
vlm0b567bf2005-03-04 22:18:20 +000063
vlm33a4ff12004-08-11 05:21:32 +000064#define DEPENDENCIES do { \
vlm0b567bf2005-03-04 22:18:20 +000065 emit_include_dependencies(arg); \
vlm33a4ff12004-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)
vlmfa67ddc2004-06-03 03:38:44 +000071
vlmaf841972005-01-28 12:18:50 +000072/* MKID() without checking for reserved keywords */
vlmad970212005-03-03 21:58:28 +000073#define MKID_nc(id) asn1c_make_identifier(0, (id), 0)
vlmaf841972005-01-28 12:18:50 +000074#define MKID(id) asn1c_make_identifier(AMI_CHECK_RESERVED, (id), 0)
vlmfa67ddc2004-06-03 03:38:44 +000075
76int
vlmf9d178d2004-09-14 12:47:45 +000077asn1c_lang_C_type_REAL(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +000078 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
79}
80
vlm80a48592005-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
vlmfa67ddc2004-06-03 03:38:44 +0000101int
vlmf9d178d2004-09-14 12:47:45 +0000102asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000103 asn1p_expr_t *expr = arg->expr;
104 asn1p_expr_t *v;
vlm80a48592005-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);
vlmfa67ddc2004-06-03 03:38:44 +0000108
vlm80a48592005-02-25 12:10:27 +0000109 v2e = alloca((el_count + 1) * sizeof(*v2e));
vlmfa67ddc2004-06-03 03:38:44 +0000110
vlm80a48592005-02-25 12:10:27 +0000111 /*
vlmc89422d2005-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
vlm80a48592005-02-25 12:10:27 +0000115 * between identifiers and associated values.
116 */
vlmc89422d2005-03-03 21:28:12 +0000117 if(expr->expr_type == ASN_BASIC_ENUMERATED
118 || (0 && el_count /* -- prohibited by X.693:8.3.4 */)) {
vlm80a48592005-02-25 12:10:27 +0000119 int eidx = 0;
120
121 REDIR(OT_DEPS);
vlm766cf1e2005-03-04 08:48:53 +0000122 OUT("typedef enum ");
123 out_name_chain(arg, 1);
124 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000125 TQ_FOR(v, &(expr->members), next) {
126 switch(v->expr_type) {
127 case A1TC_UNIVERVAL:
vlm766cf1e2005-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",
vlm80a48592005-02-25 12:10:27 +0000132 v->value->value.v_integer,
vlm766cf1e2005-03-04 08:48:53 +0000133 (eidx+1 < el_count) ? "," : "");
vlm80a48592005-02-25 12:10:27 +0000134 v2e[eidx].name = v->Identifier;
135 v2e[eidx].value = v->value->value.v_integer;
136 eidx++;
vlmfa67ddc2004-06-03 03:38:44 +0000137 break;
vlmf9d178d2004-09-14 12:47:45 +0000138 case A1TC_EXTENSIBLE:
139 OUT("\t/*\n");
140 OUT("\t * Enumeration is extensible\n");
141 OUT("\t */\n");
vlm80a48592005-02-25 12:10:27 +0000142 map_is_extensible = 1;
vlmf9d178d2004-09-14 12:47:45 +0000143 break;
vlmfa67ddc2004-06-03 03:38:44 +0000144 default:
145 return -1;
146 }
147 }
vlm766cf1e2005-03-04 08:48:53 +0000148 OUT("} ");
149 out_name_chain(arg, 0);
150 OUT("_e;\n");
vlm80a48592005-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
vlm766cf1e2005-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);
vlm80a48592005-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
vlm766cf1e2005-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);
vlm80a48592005-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
vlm766cf1e2005-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);
vlm80a48592005-02-25 12:10:27 +0000189 INDENT(+1);
vlm766cf1e2005-03-04 08:48:53 +0000190 OUT("asn_MAP_%s_%d_value2enum,\t"
vlm80a48592005-02-25 12:10:27 +0000191 "/* \"tag\" => N; sorted by tag */\n",
vlm766cf1e2005-03-04 08:48:53 +0000192 MKID_nc(expr->Identifier),
193 expr->_type_unique_index);
194 OUT("asn_MAP_%s_%d_enum2value,\t"
vlm80a48592005-02-25 12:10:27 +0000195 "/* N => \"tag\"; sorted by N */\n",
vlm766cf1e2005-03-04 08:48:53 +0000196 MKID_nc(expr->Identifier),
197 expr->_type_unique_index);
vlm80a48592005-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");
vlmfa67ddc2004-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) {
vlm766cf1e2005-03-04 08:48:53 +0000222 OUT("struct ");
223 out_name_chain(arg, 1);
224 OUT(" {\n");
vlmfa67ddc2004-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 }
vlmddd5a7d2004-09-10 09:18:20 +0000234 if(comp_mode == 1
235 || expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000236 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000237 EMBED(v);
238 }
239
240 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000241 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlmdae7f9d2004-08-22 03:25:24 +0000242 expr->_anonymous_type ? "" : MKID(expr->Identifier),
243 arg->embed ? "" : "_t");
vlmfa67ddc2004-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 */
vlmfa67ddc2004-06-03 03:38:44 +0000253 int ext_start = -1;
254 int ext_stop = -1;
vlm4e03ce22004-06-06 07:20:17 +0000255 tag2el_t *tag2el = NULL;
256 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000257 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000258 int all_tags_count;
259 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000260
vlm4e03ce22004-06-06 07:20:17 +0000261 /*
262 * Fetch every inner tag from the tag to elements map.
263 */
vlm940bc6b2004-10-03 09:13:30 +0000264 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlm4e03ce22004-06-06 07:20:17 +0000265 if(tag2el) free(tag2el);
266 return -1;
267 }
268
vlm33a4ff12004-08-11 05:21:32 +0000269 GEN_INCLUDE("constr_SEQUENCE");
270 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000271 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000272
vlm33a4ff12004-08-11 05:21:32 +0000273 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000274
275 /*
276 * Print out the table according to which the parsing is performed.
277 */
vlm39ba4c42004-09-22 16:06:28 +0000278 if(expr_elements_count(arg, expr)) {
279 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000280
vlm766cf1e2005-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);
vlm39ba4c42004-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 }
vlmfa67ddc2004-06-03 03:38:44 +0000300
vlm4a3f5822004-06-28 21:13:46 +0000301 /*
vlma5dcb912004-09-29 13:16:40 +0000302 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000303 */
vlm72425de2004-09-13 08:31:01 +0000304 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000305
vlm4e03ce22004-06-06 07:20:17 +0000306 /*
307 * Tags to elements map.
308 */
vlm940bc6b2004-10-03 09:13:30 +0000309 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlm4e03ce22004-06-06 07:20:17 +0000310
vlm766cf1e2005-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);
vlmfa67ddc2004-06-03 03:38:44 +0000313 INDENTED(
vlm766cf1e2005-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);
vlm4e03ce22004-06-06 07:20:17 +0000319 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlmfa67ddc2004-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");
vlm4e554992004-08-25 02:03:12 +0000326
327 /*
vlma5dcb912004-09-29 13:16:40 +0000328 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000329 */
vlm72425de2004-09-13 08:31:01 +0000330 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000331 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000332
vlmfa67ddc2004-06-03 03:38:44 +0000333 REDIR(OT_TYPE_DECLS);
334
335 return 0;
vlmedf203f2005-01-17 11:57:48 +0000336} /* _SEQUENCE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000337
338int
vlmfa67ddc2004-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;
vlm766cf1e2005-03-04 08:48:53 +0000355 OUT("typedef enum ");
356 out_name_chain(arg, 0);
357 OUT("_PR {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000358 TQ_FOR(v, &(expr->members), next) {
359 if(v->expr_type == A1TC_EXTENSIBLE) continue;
360 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000361 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000362 OUT("_PR_");
vlmad970212005-03-03 21:58:28 +0000363 id = MKID_nc(v->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000364 OUT("%s,\t/* Member %s is present */\n",
365 id, id)
366 );
367 mcount++;
368 }
vlm766cf1e2005-03-04 08:48:53 +0000369 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
vlmfa67ddc2004-06-03 03:38:44 +0000370
371 REDIR(OT_TYPE_DECLS);
372
373 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000374 OUT("struct ");
375 out_name_chain(arg, 1);
376 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000377 } else {
vlmaf841972005-01-28 12:18:50 +0000378 id = MKID(expr->Identifier);
vlm6e73a042004-08-11 07:17:22 +0000379 OUT("typedef struct %s {\n", id);
vlmfa67ddc2004-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 }
vlmddd5a7d2004-09-10 09:18:20 +0000386 if(comp_mode == 1
387 || expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000388 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000389 EMBED(v);
390 }
391
392 INDENTED(
vlmad970212005-03-03 21:58:28 +0000393 id = MKID_nc(expr->Identifier);
vlmfa67ddc2004-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;
vlm0b567bf2005-03-04 22:18:20 +0000402 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlmdae7f9d2004-08-22 03:25:24 +0000403 expr->_anonymous_type ? "" : MKID(expr->Identifier),
404 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000405
406 return asn1c_lang_C_type_SET_def(arg);
407}
408
vlmfa67ddc2004-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;
vlmfa67ddc2004-06-03 03:38:44 +0000414 tag2el_t *tag2el = NULL;
415 int tag2el_count = 0;
vlm940bc6b2004-10-03 09:13:30 +0000416 tag2el_t *tag2el_cxer = NULL;
417 int tag2el_cxer_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000418 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000419 int all_tags_count;
420 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000421 char *p;
422
423 /*
424 * Fetch every inner tag from the tag to elements map.
425 */
vlm940bc6b2004-10-03 09:13:30 +0000426 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000427 if(tag2el) free(tag2el);
428 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000429 }
vlm940bc6b2004-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 }
vlmfa67ddc2004-06-03 03:38:44 +0000440
vlm33a4ff12004-08-11 05:21:32 +0000441 GEN_INCLUDE("constr_SET");
442 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000443 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000444
445 REDIR(OT_STAT_DEFS);
446
vlmfa67ddc2004-06-03 03:38:44 +0000447 /*
448 * Print out the table according to which the parsing is performed.
449 */
vlm39ba4c42004-09-22 16:06:28 +0000450 if(expr_elements_count(arg, expr)) {
451 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000452
vlm766cf1e2005-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);
vlm39ba4c42004-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 }
vlmfa67ddc2004-06-03 03:38:44 +0000472
vlm4a3f5822004-06-28 21:13:46 +0000473 /*
vlma5dcb912004-09-29 13:16:40 +0000474 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000475 */
vlm72425de2004-09-13 08:31:01 +0000476 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000477
478 /*
479 * Tags to elements map.
480 */
vlm940bc6b2004-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");
vlmfa67ddc2004-06-03 03:38:44 +0000484
485 /*
486 * Emit a map of mandatory elements.
487 */
vlm766cf1e2005-03-04 08:48:53 +0000488 OUT("static uint8_t asn_MAP_%s_%d_mmap",
489 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlm4a3f5822004-06-28 21:13:46 +0000490 p = MKID(expr->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000491 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
vlm766cf1e2005-03-04 08:48:53 +0000492 OUT(" = {\n");
vlmfa67ddc2004-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 }
vlmddd5a7d2004-09-10 09:18:20 +0000505 OUT("(%d << %d)",
vlma02b74d2004-09-15 11:54:38 +0000506 v->marker.flags?0:1,
vlmddd5a7d2004-09-10 09:18:20 +0000507 7 - (el % 8));
vlmfa67ddc2004-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
vlm766cf1e2005-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);
vlmfa67ddc2004-06-03 03:38:44 +0000521 INDENTED(
vlm766cf1e2005-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");
vlmad970212005-03-03 21:58:28 +0000531 p = MKID_nc(expr->Identifier);
vlm766cf1e2005-03-04 08:48:53 +0000532 OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000533 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm940bc6b2004-10-03 09:13:30 +0000534 if(tag2el_cxer)
vlm766cf1e2005-03-04 08:48:53 +0000535 OUT("asn_MAP_%s_%d_tag2el_cxer,\n",
536 p, expr->_type_unique_index);
vlm940bc6b2004-10-03 09:13:30 +0000537 else
vlm766cf1e2005-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);
vlm79b08d52004-07-01 00:52:50 +0000542 OUT("%d,\t/* Whether extensible */\n",
543 check_if_extensible(expr));
vlm766cf1e2005-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);
vlmfa67ddc2004-06-03 03:38:44 +0000546 );
547 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000548
549 /*
vlma5dcb912004-09-29 13:16:40 +0000550 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000551 */
vlm72425de2004-09-13 08:31:01 +0000552 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000553 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000554
vlmfa67ddc2004-06-03 03:38:44 +0000555 REDIR(OT_TYPE_DECLS);
556
557 return 0;
vlmedf203f2005-01-17 11:57:48 +0000558} /* _SET_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000559
560int
vlmdae7f9d2004-08-22 03:25:24 +0000561asn1c_lang_C_type_SEx_OF(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000562 asn1p_expr_t *expr = arg->expr;
vlm0b567bf2005-03-04 22:18:20 +0000563 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
vlmfa67ddc2004-06-03 03:38:44 +0000564
565 DEPENDENCIES;
566
567 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000568 OUT("struct ");
569 out_name_chain(arg, 1);
570 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000571 } else {
vlmdae7f9d2004-08-22 03:25:24 +0000572 OUT("typedef struct %s {\n", MKID(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000573 }
574
vlmdae7f9d2004-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");
vlm80a48592005-02-25 12:10:27 +0000579 if(memb->expr_type & ASN_CONSTR_MASK
580 || ((memb->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-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))) {
vlmdae7f9d2004-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;
vlm39ba4c42004-09-22 16:06:28 +0000591 if(tmp_memb.Identifier == 0) {
vlm766cf1e2005-03-04 08:48:53 +0000592 tmp_memb.Identifier = "Member";
593 if(0)
vlm39ba4c42004-09-22 16:06:28 +0000594 tmp_memb.Identifier = strdup(
595 asn1c_make_identifier(0,
vlmbf3c5112005-02-14 20:41:29 +0000596 expr->Identifier, "Member", 0));
vlm39ba4c42004-09-22 16:06:28 +0000597 assert(tmp_memb.Identifier);
598 }
vlmdae7f9d2004-08-22 03:25:24 +0000599 tmp.default_cb(&tmp);
vlm39ba4c42004-09-22 16:06:28 +0000600 if(tmp_memb.Identifier != memb->Identifier)
vlm766cf1e2005-03-04 08:48:53 +0000601 if(0) free(tmp_memb.Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000602 arg->embed--;
603 assert(arg->target->target == OT_TYPE_DECLS);
604 } else {
vlm0b567bf2005-03-04 22:18:20 +0000605 OUT("%s", asn1c_type_name(arg, memb, TNF_RSAFE));
vlmfa67ddc2004-06-03 03:38:44 +0000606 }
vlmdae7f9d2004-08-22 03:25:24 +0000607 OUT(") list;\n");
608 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000609
610 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000611 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlmdae7f9d2004-08-22 03:25:24 +0000612 expr->_anonymous_type ? "" : MKID(expr->Identifier),
613 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000614
615 /*
vlmdae7f9d2004-08-22 03:25:24 +0000616 * SET OF/SEQUENCE OF definition
vlmfa67ddc2004-06-03 03:38:44 +0000617 */
vlmdae7f9d2004-08-22 03:25:24 +0000618 return asn1c_lang_C_type_SEx_OF_def(arg,
619 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
vlmfa67ddc2004-06-03 03:38:44 +0000620}
621
622static int
623asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
624 asn1p_expr_t *expr = arg->expr;
625 asn1p_expr_t *v;
vlm6e73a042004-08-11 07:17:22 +0000626 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000627 int all_tags_count;
628 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000629
vlm33a4ff12004-08-11 05:21:32 +0000630 /*
631 * Print out the table according to which the parsing is performed.
632 */
vlmfa67ddc2004-06-03 03:38:44 +0000633 if(seq_of) {
vlm33a4ff12004-08-11 05:21:32 +0000634 GEN_INCLUDE("constr_SEQUENCE_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000635 } else {
vlm33a4ff12004-08-11 05:21:32 +0000636 GEN_INCLUDE("constr_SET_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000637 }
vlm33a4ff12004-08-11 05:21:32 +0000638 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000639 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000640
641 REDIR(OT_STAT_DEFS);
642
643 /*
644 * Print out the table according to which the parsing is performed.
645 */
vlm766cf1e2005-03-04 08:48:53 +0000646 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
647 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000648 INDENT(+1);
vlmfa67ddc2004-06-03 03:38:44 +0000649 v = TQ_FIRST(&(expr->members));
vlm39ba4c42004-09-22 16:06:28 +0000650 if(!v->Identifier) {
vlmbf3c5112005-02-14 20:41:29 +0000651 v->Identifier = strdup("Member");
vlm39ba4c42004-09-22 16:06:28 +0000652 assert(v->Identifier);
653 }
654 v->_anonymous_type = 1;
655 arg->embed++;
vlm4e554992004-08-25 02:03:12 +0000656 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000657 arg->embed--;
658 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000659 OUT("};\n");
660
vlm4a3f5822004-06-28 21:13:46 +0000661 /*
vlma5dcb912004-09-29 13:16:40 +0000662 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000663 */
vlm72425de2004-09-13 08:31:01 +0000664 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000665
vlm766cf1e2005-03-04 08:48:53 +0000666 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
667 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000668 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000669 OUT("sizeof(struct ");
670 out_name_chain(arg, 1);
671 OUT("),\n");
672 OUT("offsetof(struct ");
673 out_name_chain(arg, 1);
674 OUT(", _asn_ctx),\n");
675
vlm39ba4c42004-09-22 16:06:28 +0000676 if(expr_as_xmlvaluelist(arg, v))
677 OUT("1,\t/* XER encoding is XMLValueList */\n");
678 else
679 OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n");
vlmfa67ddc2004-06-03 03:38:44 +0000680 );
681 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000682
683 /*
vlma5dcb912004-09-29 13:16:40 +0000684 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000685 */
vlm72425de2004-09-13 08:31:01 +0000686 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
vlm86f5ed22004-09-26 13:11:31 +0000687 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000688
vlmfa67ddc2004-06-03 03:38:44 +0000689 REDIR(OT_TYPE_DECLS);
690
691 return 0;
vlmedf203f2005-01-17 11:57:48 +0000692} /* _SEx_OF_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000693
694int
695asn1c_lang_C_type_CHOICE(arg_t *arg) {
696 asn1p_expr_t *expr = arg->expr;
697 asn1p_expr_t *v;
vlmdae7f9d2004-08-22 03:25:24 +0000698 char *id;
vlmfa67ddc2004-06-03 03:38:44 +0000699
700 DEPENDENCIES;
701
vlm33a4ff12004-08-11 05:21:32 +0000702 REDIR(OT_DEPS);
703
vlm766cf1e2005-03-04 08:48:53 +0000704 OUT("typedef enum ");
705 out_name_chain(arg, 0);
706 OUT("_PR {\n");
vlm33a4ff12004-08-11 05:21:32 +0000707 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000708 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000709 OUT("_PR_NOTHING,\t/* No components present */\n");
vlm33a4ff12004-08-11 05:21:32 +0000710 TQ_FOR(v, &(expr->members), next) {
711 if(v->expr_type == A1TC_EXTENSIBLE) {
712 OUT("/* Extensions may appear below */\n");
713 continue;
714 }
vlm766cf1e2005-03-04 08:48:53 +0000715 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000716 OUT("_PR_");
vlmad970212005-03-03 21:58:28 +0000717 id = MKID_nc(v->Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000718 OUT("%s,\n", id, id);
vlm33a4ff12004-08-11 05:21:32 +0000719 }
720 );
vlm766cf1e2005-03-04 08:48:53 +0000721 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
vlm33a4ff12004-08-11 05:21:32 +0000722
723 REDIR(OT_TYPE_DECLS);
vlmfa67ddc2004-06-03 03:38:44 +0000724
725 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000726 OUT("struct "); out_name_chain(arg, 1); OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000727 } else {
vlmaf841972005-01-28 12:18:50 +0000728 id = MKID(expr->Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000729 OUT("typedef struct %s {\n", id);
vlmfa67ddc2004-06-03 03:38:44 +0000730 }
731
732 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000733 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000734 OUT("_PR present;\n");
735 OUT("union {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000736 TQ_FOR(v, &(expr->members), next) {
vlmddd5a7d2004-09-10 09:18:20 +0000737 if(expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000738 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000739 EMBED(v);
740 }
741 if(UNNAMED_UNIONS) OUT("};\n");
742 else OUT("} choice;\n");
743 );
744
745 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000746 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlmdae7f9d2004-08-22 03:25:24 +0000747 expr->_anonymous_type ? "" : MKID(expr->Identifier),
748 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000749
750 return asn1c_lang_C_type_CHOICE_def(arg);
751}
752
753static int
754asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
755 asn1p_expr_t *expr = arg->expr;
756 asn1p_expr_t *v;
757 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000758 tag2el_t *tag2el = NULL;
759 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000760 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000761 int all_tags_count;
762 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000763
764 /*
765 * Fetch every inner tag from the tag to elements map.
766 */
vlm940bc6b2004-10-03 09:13:30 +0000767 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000768 if(tag2el) free(tag2el);
769 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000770 }
771
vlm33a4ff12004-08-11 05:21:32 +0000772 GEN_INCLUDE("constr_CHOICE");
773 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000774 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000775
vlm33a4ff12004-08-11 05:21:32 +0000776 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000777
778 /*
779 * Print out the table according to which the parsing is performed.
780 */
vlm39ba4c42004-09-22 16:06:28 +0000781 if(expr_elements_count(arg, expr)) {
vlmfa67ddc2004-06-03 03:38:44 +0000782
vlm766cf1e2005-03-04 08:48:53 +0000783 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
784 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000785
786 elements = 0;
787 INDENTED(TQ_FOR(v, &(expr->members), next) {
vlm34dcd572005-01-17 11:40:49 +0000788 if(v->expr_type == A1TC_EXTENSIBLE)
789 continue;
790 if(expr_better_indirect(arg, v))
791 v->marker.flags |= EM_INDIRECT;
792 elements++;
793 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000794 });
795 OUT("};\n");
796 } else {
797 elements = 0;
798 }
vlmfa67ddc2004-06-03 03:38:44 +0000799
vlm6e73a042004-08-11 07:17:22 +0000800
vlmfa67ddc2004-06-03 03:38:44 +0000801 if(arg->embed) {
802 /*
803 * Our parent structure has already taken this into account.
804 */
vlm72425de2004-09-13 08:31:01 +0000805 tv_mode = _TVM_SAME;
806 tags_count = all_tags_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000807 } else {
vlm72425de2004-09-13 08:31:01 +0000808 tv_mode = emit_tags_vectors(arg, expr,
809 &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000810 }
vlmfa67ddc2004-06-03 03:38:44 +0000811
812 /*
813 * Tags to elements map.
814 */
vlm940bc6b2004-10-03 09:13:30 +0000815 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000816
vlm766cf1e2005-03-04 08:48:53 +0000817 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
818 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000819 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000820 OUT("sizeof(struct ");
821 out_name_chain(arg, 1);
822 OUT("),\n");
823 OUT("offsetof(struct ");
824 out_name_chain(arg, 1);
825 OUT(", _asn_ctx),\n");
826 OUT("offsetof(struct ");
827 out_name_chain(arg, 1);
828 OUT(", present),\n");
829 OUT("sizeof(((struct ");
830 out_name_chain(arg, 1);
831 OUT(" *)0)->present),\n");
832 OUT("asn_MAP_%s_%d_tag2el,\n",
833 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000834 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm79b08d52004-07-01 00:52:50 +0000835 OUT("%d\t/* Whether extensible */\n",
836 check_if_extensible(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000837 );
838 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000839
840 /*
vlma5dcb912004-09-29 13:16:40 +0000841 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000842 */
vlm72425de2004-09-13 08:31:01 +0000843 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000844 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000845
vlmfa67ddc2004-06-03 03:38:44 +0000846 REDIR(OT_TYPE_DECLS);
847
848 return 0;
vlmedf203f2005-01-17 11:57:48 +0000849} /* _CHOICE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000850
851int
852asn1c_lang_C_type_REFERENCE(arg_t *arg) {
853 asn1p_ref_t *ref;
854
855 ref = arg->expr->reference;
856 if(ref->components[ref->comp_count-1].name[0] == '&') {
vlmfa67ddc2004-06-03 03:38:44 +0000857 asn1p_expr_t *extract;
858 arg_t tmp;
859 int ret;
860
vlm39ba4c42004-09-22 16:06:28 +0000861 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
vlmdae7f9d2004-08-22 03:25:24 +0000862 arg->expr, ref);
vlmfa67ddc2004-06-03 03:38:44 +0000863 if(extract == NULL)
864 return -1;
865
vlmdae7f9d2004-08-22 03:25:24 +0000866 extract = asn1p_expr_clone(extract, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000867 if(extract) {
868 if(extract->Identifier)
869 free(extract->Identifier);
870 extract->Identifier = strdup(arg->expr->Identifier);
871 if(extract->Identifier == NULL) {
872 asn1p_expr_free(extract);
873 return -1;
874 }
875 } else {
876 return -1;
877 }
878
879 tmp = *arg;
880 tmp.asn = arg->asn;
vlmdae7f9d2004-08-22 03:25:24 +0000881 tmp.mod = extract->module;
vlmfa67ddc2004-06-03 03:38:44 +0000882 tmp.expr = extract;
883
884 ret = arg->default_cb(&tmp);
885
886 asn1p_expr_free(extract);
887
888 return ret;
889 }
890
891
892 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
893}
894
895int
896asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
897 asn1p_expr_t *expr = arg->expr;
vlm6e73a042004-08-11 07:17:22 +0000898 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000899 int all_tags_count;
900 enum tvm_compat tv_mode;
vlm80a48592005-02-25 12:10:27 +0000901 enum etd_spec etd_spec;
vlmfa67ddc2004-06-03 03:38:44 +0000902 char *p;
903
904 if(arg->embed) {
vlma5dcb912004-09-29 13:16:40 +0000905 enum tnfmt tnfmt = TNF_CTYPE;
906
907 /*
908 * If this is an optional compound type,
909 * refer it using "struct X" convention,
910 * as it may recursively include the current structure.
911 */
vlm0b567bf2005-03-04 22:18:20 +0000912 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
vlma5dcb912004-09-29 13:16:40 +0000913 asn1p_expr_t *terminal;
914 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
915 if(terminal
916 && (terminal->expr_type & ASN_CONSTR_MASK)) {
vlm0b567bf2005-03-04 22:18:20 +0000917 tnfmt = TNF_RSAFE;
918 REDIR(OT_FWD_DECLS);
vlma5dcb912004-09-29 13:16:40 +0000919 OUT("%s;\t/* Forward declaration */\n",
vlm0b567bf2005-03-04 22:18:20 +0000920 asn1c_type_name(arg, arg->expr, tnfmt));
vlma5dcb912004-09-29 13:16:40 +0000921 }
922 }
923
vlmfa67ddc2004-06-03 03:38:44 +0000924 REDIR(OT_TYPE_DECLS);
925
vlm0b567bf2005-03-04 22:18:20 +0000926 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
vlm80a48592005-02-25 12:10:27 +0000927 if(!expr->_anonymous_type) {
vlm0b567bf2005-03-04 22:18:20 +0000928 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
vlm80a48592005-02-25 12:10:27 +0000929 OUT("%s", MKID(expr->Identifier));
930 if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT)
931 OUT("\t/* DEFAULT %s */",
932 asn1f_printable_value(
933 expr->marker.default_value));
934 else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL)
935 OUT("\t/* OPTIONAL */");
936 }
937
938 } else {
939 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
vlmb2839012004-08-20 13:37:01 +0000940
941 REDIR(OT_TYPE_DECLS);
vlm80a48592005-02-25 12:10:27 +0000942
943 OUT("typedef %s\t",
vlm0b567bf2005-03-04 22:18:20 +0000944 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
vlm80a48592005-02-25 12:10:27 +0000945 OUT("%s%s_t",
vlm0b567bf2005-03-04 22:18:20 +0000946 (expr->marker.flags & EM_INDIRECT)?"*":" ",
947 MKID_nc(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000948 }
949
vlm80a48592005-02-25 12:10:27 +0000950 if((expr->expr_type == ASN_BASIC_ENUMERATED)
vlmc89422d2005-03-03 21:28:12 +0000951 || (0 /* -- prohibited by X.693:8.3.4 */
952 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +0000953 && expr_elements_count(arg, expr)))
954 etd_spec = ETD_HAS_SPECIFICS;
955 else
956 etd_spec = ETD_NO_SPECIFICS;
vlmfa67ddc2004-06-03 03:38:44 +0000957
vlm86f5ed22004-09-26 13:11:31 +0000958 /*
959 * If this type just blindly refers the other type, alias it.
960 * Type1 ::= Type2
961 */
vlm80a48592005-02-25 12:10:27 +0000962 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
963 REDIR(OT_TYPE_DECLS);
964 return 0;
965 }
vlm86f5ed22004-09-26 13:11:31 +0000966 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlm80a48592005-02-25 12:10:27 +0000967 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
968 && etd_spec == ETD_NO_SPECIFICS) {
vlm86f5ed22004-09-26 13:11:31 +0000969 char *type_name;
970 REDIR(OT_FUNC_DECLS);
971 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
972 OUT("/* This type is equivalent to %s */\n", type_name);
vlm86f5ed22004-09-26 13:11:31 +0000973 if(HIDE_INNER_DEFS) OUT("/* ");
vlmad970212005-03-03 21:58:28 +0000974 OUT("#define\tasn_DEF_%s\t", MKID_nc(expr->Identifier));
vlm86f5ed22004-09-26 13:11:31 +0000975 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlm80a48592005-02-25 12:10:27 +0000976 OUT("asn_DEF_%s", type_name);
vlm86f5ed22004-09-26 13:11:31 +0000977 if(HIDE_INNER_DEFS)
vlm80a48592005-02-25 12:10:27 +0000978 OUT("\t// (Use -fall-defs-global to expose) */");
979 OUT("\n");
vlm86f5ed22004-09-26 13:11:31 +0000980 REDIR(OT_CODE);
981 OUT("/* This type is equivalent to %s */\n", type_name);
982 OUT("\n");
983 REDIR(OT_TYPE_DECLS);
984 return 0;
985 }
986
vlmfa67ddc2004-06-03 03:38:44 +0000987 REDIR(OT_STAT_DEFS);
988
vlm4a3f5822004-06-28 21:13:46 +0000989 /*
vlma5dcb912004-09-29 13:16:40 +0000990 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000991 */
vlm72425de2004-09-13 08:31:01 +0000992 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000993
vlm80a48592005-02-25 12:10:27 +0000994 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
995 0, etd_spec);
vlmfa67ddc2004-06-03 03:38:44 +0000996
vlmb2839012004-08-20 13:37:01 +0000997 REDIR(OT_CODE);
998
vlmfa67ddc2004-06-03 03:38:44 +0000999 /*
1000 * Constraint checking.
1001 */
vlm86f5ed22004-09-26 13:11:31 +00001002 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1003 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001004 if(HIDE_INNER_DEFS) OUT("static ");
vlm86f5ed22004-09-26 13:11:31 +00001005 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001006 OUT("%s", p);
1007 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1008 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
vlm86f5ed22004-09-26 13:11:31 +00001009 INDENT(+1);
1010 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1011 OUT("\n");
1012 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1013 OUT("/* Replace with underlying type checker */\n");
1014 OUT("td->check_constraints "
vlma5dcb912004-09-29 13:16:40 +00001015 "= asn_DEF_%s.check_constraints;\n",
vlm86f5ed22004-09-26 13:11:31 +00001016 asn1c_type_name(arg, expr, TNF_SAFE));
1017 OUT("return td->check_constraints"
1018 "(td, sptr, app_errlog, app_key);\n");
vlm1d036692004-08-19 13:29:46 +00001019 }
vlm86f5ed22004-09-26 13:11:31 +00001020 INDENT(-1);
1021 OUT("}\n");
1022 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001023 }
vlmfa67ddc2004-06-03 03:38:44 +00001024
1025 /*
1026 * Emit suicidal functions.
1027 */
1028
vlmfa67ddc2004-06-03 03:38:44 +00001029 /*
1030 * This function replaces certain fields from the definition
1031 * of a type with the corresponding fields from the basic type
1032 * (from which the current type is inherited).
1033 */
vlmfa67ddc2004-06-03 03:38:44 +00001034 OUT("/*\n");
vlmb2839012004-08-20 13:37:01 +00001035 OUT(" * This type is implemented using %s,\n",
1036 asn1c_type_name(arg, expr, TNF_SAFE));
vlmdcd32fe2004-09-23 22:20:47 +00001037 OUT(" * so here we adjust the DEF accordingly.\n");
vlmfa67ddc2004-06-03 03:38:44 +00001038 OUT(" */\n");
1039 OUT("static void\n");
vlm766cf1e2005-03-04 08:48:53 +00001040 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
1041 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001042 INDENT(+1);
vlm80a48592005-02-25 12:10:27 +00001043 {
vlm39ba4c42004-09-22 16:06:28 +00001044 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001045 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlma5dcb912004-09-29 13:16:40 +00001046 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1047 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1048 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1049 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1050 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1051 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
vlm1308d2b2004-09-10 15:49:15 +00001052 if(!terminal && !tags_count) {
vlm72425de2004-09-13 08:31:01 +00001053 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
vlma5dcb912004-09-29 13:16:40 +00001054 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1055 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1056 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1057 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
vlm1308d2b2004-09-10 15:49:15 +00001058 OUT("/* End of these lines */\n");
1059 }
vlma5dcb912004-09-29 13:16:40 +00001060 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1061 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
vlm80a48592005-02-25 12:10:27 +00001062 if(etd_spec != ETD_NO_SPECIFICS) {
1063 INDENT(-1);
1064 OUT(" /* ");
vlmb2839012004-08-20 13:37:01 +00001065 }
vlm80a48592005-02-25 12:10:27 +00001066 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1067 if(etd_spec == ETD_NO_SPECIFICS) {
1068 INDENT(-1);
1069 OUT("\n");
1070 } else {
1071 OUT("\t// Defined explicitly */\n");
1072 }
1073 }
vlmfa67ddc2004-06-03 03:38:44 +00001074 OUT("}\n");
1075 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001076
1077 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001078 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001079 OUT("void\n");
vlm766cf1e2005-03-04 08:48:53 +00001080 OUT("%s", p);
1081 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1082 OUT("_free(asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001083 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001084 OUT("\tvoid *struct_ptr, int contents_only) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001085 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1086 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001087 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001088 );
1089 OUT("}\n");
1090 OUT("\n");
1091
1092 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001093 if(HIDE_INNER_DEFS) OUT("static ");
vlmfa67ddc2004-06-03 03:38:44 +00001094 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001095 OUT("%s", p);
1096 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1097 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001098 INDENTED(
1099 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001100 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1101 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001102 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1103 );
1104 OUT("}\n");
1105 OUT("\n");
1106
1107 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001108 if(HIDE_INNER_DEFS) OUT("static ");
vlm9de248e2004-10-20 15:50:55 +00001109 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001110 OUT("%s", p);
1111 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1112 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001113 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001114 OUT("\tvoid **structure, void *bufptr, size_t size, int tag_mode) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001115 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1116 p, expr->_type_unique_index);
vlma5dcb912004-09-29 13:16:40 +00001117 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
vlm39ba4c42004-09-22 16:06:28 +00001118 );
1119 OUT("}\n");
1120 OUT("\n");
1121
1122 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001123 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001124 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001125 OUT("%s", p);
1126 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1127 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
vlm39ba4c42004-09-22 16:06:28 +00001128 INDENTED(
1129 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1130 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001131 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1132 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001133 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1134 );
1135 OUT("}\n");
1136 OUT("\n");
1137
1138 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001139 if(HIDE_INNER_DEFS) OUT("static ");
vlme03646b2004-10-23 13:34:00 +00001140 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001141 OUT("%s", p);
1142 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1143 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlme03646b2004-10-23 13:34:00 +00001144 INDENTED(
1145 OUT("\tvoid **structure, const char *opt_mname, void *bufptr, size_t size) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001146 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1147 p, expr->_type_unique_index);
vlme03646b2004-10-23 13:34:00 +00001148 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1149 );
1150 OUT("}\n");
1151 OUT("\n");
1152
1153 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001154 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001155 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001156 OUT("%s", p);
1157 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1158 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
vlm39ba4c42004-09-22 16:06:28 +00001159 INDENTED(
1160 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1161 OUT("\tasn_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);
vlm39ba4c42004-09-22 16:06:28 +00001164 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001165 );
1166 OUT("}\n");
1167 OUT("\n");
1168
1169 REDIR(OT_FUNC_DECLS);
1170
vlmad970212005-03-03 21:58:28 +00001171 p = MKID_nc(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001172 if(HIDE_INNER_DEFS) {
vlm766cf1e2005-03-04 08:48:53 +00001173 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1174 "\t// (Use -fall-defs-global to expose) */\n",
1175 p, expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +00001176 } else {
1177 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1178 OUT("asn_struct_free_f %s_free;\n", p);
1179 OUT("asn_struct_print_f %s_print;\n", p);
1180 OUT("asn_constr_check_f %s_constraint;\n", p);
1181 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1182 OUT("der_type_encoder_f %s_encode_der;\n", p);
1183 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1184 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1185 }
vlmfa67ddc2004-06-03 03:38:44 +00001186
vlm33a4ff12004-08-11 05:21:32 +00001187 REDIR(OT_TYPE_DECLS);
1188
vlmfa67ddc2004-06-03 03:38:44 +00001189 return 0;
1190}
1191
1192int
1193asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1194
1195 OUT("/*\n");
1196 OUT(" * This type is extensible,\n");
1197 OUT(" * possible extensions are below.\n");
1198 OUT(" */\n");
1199
1200 return 0;
1201}
1202
vlm79b08d52004-07-01 00:52:50 +00001203static int check_if_extensible(asn1p_expr_t *expr) {
1204 asn1p_expr_t *v;
1205 TQ_FOR(v, &(expr->members), next) {
1206 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1207 }
1208 return 0;
1209}
1210
vlmfa67ddc2004-06-03 03:38:44 +00001211static int
vlma8a86cc2004-09-10 06:07:18 +00001212_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
vlmfa67ddc2004-06-03 03:38:44 +00001213
1214 OUT("(");
vlma8a86cc2004-09-10 06:07:18 +00001215 switch(tag->tag_class) {
vlmfa67ddc2004-06-03 03:38:44 +00001216 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1217 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1218 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1219 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1220 case TC_NOCLASS:
1221 break;
1222 }
vlm47ae1582004-09-24 21:01:43 +00001223 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +00001224
1225 return 0;
1226}
1227
vlm4e03ce22004-06-06 07:20:17 +00001228
1229static int
1230_tag2el_cmp(const void *ap, const void *bp) {
1231 const tag2el_t *a = ap;
1232 const tag2el_t *b = bp;
1233 const struct asn1p_type_tag_s *ta = &a->el_tag;
1234 const struct asn1p_type_tag_s *tb = &b->el_tag;
1235
1236 if(ta->tag_class == tb->tag_class) {
1237 if(ta->tag_value == tb->tag_value) {
1238 /*
1239 * Sort by their respective positions.
1240 */
1241 if(a->el_no < b->el_no)
1242 return -1;
1243 else if(a->el_no > b->el_no)
1244 return 1;
1245 return 0;
1246 } else if(ta->tag_value < tb->tag_value)
1247 return -1;
1248 else
1249 return 1;
1250 } else if(ta->tag_class < tb->tag_class) {
1251 return -1;
1252 } else {
1253 return 1;
1254 }
1255}
1256
vlmfa67ddc2004-06-03 03:38:44 +00001257/*
1258 * For constructed types, number of external tags may be greater than
1259 * number of elements in the type because of CHOICE type.
1260 * T ::= SET { -- Three possible tags:
1261 * a INTEGER, -- One tag is here...
1262 * b Choice1 -- ... and two more tags are there.
1263 * }
1264 * Choice1 ::= CHOICE {
1265 * s1 IA5String,
1266 * s2 ObjectDescriptor
1267 * }
1268 */
1269static int
vlm940bc6b2004-10-03 09:13:30 +00001270_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001271 asn1p_expr_t *expr = arg->expr;
1272 arg_t tmparg = *arg;
1273 asn1p_expr_t *v;
1274 int element = 0;
vlm940bc6b2004-10-03 09:13:30 +00001275 int original_count = *count;
1276 int sort_until = -1;
vlmfa67ddc2004-06-03 03:38:44 +00001277
1278 TQ_FOR(v, &(expr->members), next) {
vlm940bc6b2004-10-03 09:13:30 +00001279 if(v->expr_type == A1TC_EXTENSIBLE) {
1280 /*
vlm80a48592005-02-25 12:10:27 +00001281 * CXER mandates sorting
vlm940bc6b2004-10-03 09:13:30 +00001282 * only for the root part.
1283 */
1284 if(flags == FTE_CANONICAL_XER
1285 && sort_until == -1)
1286 sort_until = *count;
vlmfa67ddc2004-06-03 03:38:44 +00001287 continue;
vlm940bc6b2004-10-03 09:13:30 +00001288 }
vlmfa67ddc2004-06-03 03:38:44 +00001289
1290 tmparg.expr = v;
1291
1292 if(_add_tag2el_member(&tmparg, tag2el, count,
vlm940bc6b2004-10-03 09:13:30 +00001293 (el_no==-1)?element:el_no, flags)) {
vlmfa67ddc2004-06-03 03:38:44 +00001294 return -1;
1295 }
1296
1297 element++;
1298 }
1299
vlm940bc6b2004-10-03 09:13:30 +00001300
1301 if(flags == FTE_CANONICAL_XER) {
1302 if(sort_until == -1) sort_until = *count;
1303 qsort((*tag2el) + original_count,
1304 sort_until - original_count,
1305 sizeof(**tag2el), _tag2el_cmp);
1306 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1307 && (sort_until - original_count) >= 1) {
1308 /* Only take in account the root component */
1309 *count = original_count + 1;
1310 }
1311 } else {
1312 /*
1313 * Sort the map according to canonical order of their
1314 * tags and element numbers.
1315 */
1316 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1317 }
vlm4e03ce22004-06-06 07:20:17 +00001318
vlmc8aeab42004-06-14 13:09:45 +00001319 /*
1320 * Initialize .toff_{first|last} members.
1321 */
1322 if(*count) {
1323 struct asn1p_type_tag_s *cur_tag = 0;
1324 tag2el_t *cur = *tag2el;
1325 tag2el_t *end = cur + *count;
1326 int occur, i;
1327 for(occur = 0; cur < end; cur++) {
1328 if(cur_tag == 0
1329 || cur_tag->tag_value != cur->el_tag.tag_value
1330 || cur_tag->tag_class != cur->el_tag.tag_class) {
1331 cur_tag = &cur->el_tag;
1332 occur = 0;
1333 } else {
1334 occur++;
1335 }
1336 cur->toff_first = -occur;
1337 for(i = 0; i >= -occur; i--)
1338 cur[i].toff_last = -i;
1339 }
1340 }
1341
vlmfa67ddc2004-06-03 03:38:44 +00001342 return 0;
1343}
1344
1345static int
vlm940bc6b2004-10-03 09:13:30 +00001346_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001347 struct asn1p_type_tag_s tag;
1348 int ret;
1349
1350 assert(el_no >= 0);
1351
vlm39ba4c42004-09-22 16:06:28 +00001352 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1353 arg->expr, &tag, 1);
vlmfa67ddc2004-06-03 03:38:44 +00001354 if(ret == 0) {
vlme2070ea2004-09-04 04:42:29 +00001355 tag2el_t *te;
1356 int new_count = (*count) + 1;
vlmfa67ddc2004-06-03 03:38:44 +00001357 void *p;
vlme2070ea2004-09-04 04:42:29 +00001358
vlm2de0e792004-09-05 10:42:33 +00001359 if(tag.tag_value == -1) {
1360 /*
1361 * This is an untagged ANY type,
1362 * proceed without adding a tag
1363 */
1364 return 0;
1365 }
1366
vlme2070ea2004-09-04 04:42:29 +00001367 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
vlmfa67ddc2004-06-03 03:38:44 +00001368 if(p) *tag2el = p;
1369 else return -1;
1370
1371 DEBUG("Found tag for %s: %ld",
1372 arg->expr->Identifier,
1373 (long)tag.tag_value);
1374
vlme2070ea2004-09-04 04:42:29 +00001375 te = &((*tag2el)[*count]);
1376 te->el_tag = tag;
1377 te->el_no = el_no;
1378 te->from_expr = arg->expr;
1379 *count = new_count;
vlmfa67ddc2004-06-03 03:38:44 +00001380 return 0;
1381 }
1382
1383 DEBUG("Searching tag in complex expression %s:%x at line %d",
1384 arg->expr->Identifier,
1385 arg->expr->expr_type,
1386 arg->expr->_lineno);
1387
1388 /*
1389 * Iterate over members of CHOICE type.
1390 */
1391 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
vlm940bc6b2004-10-03 09:13:30 +00001392 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001393 }
1394
1395 if(arg->expr->expr_type == A1TC_REFERENCE) {
1396 arg_t tmp = *arg;
1397 asn1p_expr_t *expr;
vlmdae7f9d2004-08-22 03:25:24 +00001398 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
vlmfa67ddc2004-06-03 03:38:44 +00001399 arg->expr->reference);
1400 if(expr) {
vlmdae7f9d2004-08-22 03:25:24 +00001401 tmp.mod = expr->module;
vlmfa67ddc2004-06-03 03:38:44 +00001402 tmp.expr = expr;
vlm940bc6b2004-10-03 09:13:30 +00001403 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001404 } else {
1405 FATAL("Cannot dereference %s at line %d",
1406 arg->expr->Identifier,
1407 arg->expr->_lineno);
1408 return -1;
1409 }
1410 }
1411
1412 DEBUG("No tag for %s at line %d",
1413 arg->expr->Identifier,
1414 arg->expr->_lineno);
1415
1416 return -1;
1417}
1418
1419static int
vlm940bc6b2004-10-03 09:13:30 +00001420emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
vlm4e03ce22004-06-06 07:20:17 +00001421 asn1p_expr_t *expr = arg->expr;
1422
vlm766cf1e2005-03-04 08:48:53 +00001423 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
1424 MKID_nc(expr->Identifier), expr->_type_unique_index,
1425 opt_modifier?opt_modifier:"");
vlm4e03ce22004-06-06 07:20:17 +00001426 if(tag2el_count) {
1427 int i;
1428 for(i = 0; i < tag2el_count; i++) {
1429 OUT(" { ");
vlma8a86cc2004-09-10 06:07:18 +00001430 _print_tag(arg, &tag2el[i].el_tag);
vlm4e03ce22004-06-06 07:20:17 +00001431 OUT(", ");
vlmc8aeab42004-06-14 13:09:45 +00001432 OUT("%d, ", tag2el[i].el_no);
1433 OUT("%d, ", tag2el[i].toff_first);
1434 OUT("%d ", tag2el[i].toff_last);
vlm80a48592005-02-25 12:10:27 +00001435 OUT("}%s /* %s at %d */\n",
1436 (i + 1 < tag2el_count) ? "," : "",
vlm4e03ce22004-06-06 07:20:17 +00001437 tag2el[i].from_expr->Identifier,
1438 tag2el[i].from_expr->_lineno
1439 );
1440 }
1441 }
1442 OUT("};\n");
1443
1444 return 0;;
1445}
1446
vlm72425de2004-09-13 08:31:01 +00001447static enum tvm_compat
1448emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1449 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1450 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
vlm6e73a042004-08-11 07:17:22 +00001451 int tags_count = 0;
vlm72425de2004-09-13 08:31:01 +00001452 int all_tags_count = 0;
1453 enum tvm_compat tv_mode = _TVM_SAME;
vlm1308d2b2004-09-10 15:49:15 +00001454 int i;
vlm4a3f5822004-06-28 21:13:46 +00001455
vlmc6de2c42004-09-14 14:10:10 +00001456 /* Cleanup before proceeding. */
1457 *tags_count_r = 0;
1458 *all_tags_count_r = 0;
1459
vlm1308d2b2004-09-10 15:49:15 +00001460 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001461 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
vlmc6de2c42004-09-14 14:10:10 +00001462 if(tags_count < 0)
1463 return -1;
vlm6e73a042004-08-11 07:17:22 +00001464
vlm72425de2004-09-13 08:31:01 +00001465 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001466 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
vlm72425de2004-09-13 08:31:01 +00001467 &all_tags, AFT_FULL_COLLECT);
1468 if(all_tags_count < 0) {
1469 if(tags) free(tags);
1470 return -1;
vlmfb41dbb2004-09-08 00:28:57 +00001471 }
vlm1308d2b2004-09-10 15:49:15 +00001472
vlm72425de2004-09-13 08:31:01 +00001473 assert(tags_count <= all_tags_count);
1474 assert((tags_count?0:1) == (all_tags_count?0:1));
vlm4a3f5822004-06-28 21:13:46 +00001475
vlm72425de2004-09-13 08:31:01 +00001476 if(tags_count <= all_tags_count) {
1477 for(i = 0; i < tags_count; i++) {
1478 if(tags[i].tag_value != all_tags[i].tag_value
1479 || tags[i].tag_class != all_tags[i].tag_class) {
1480 tv_mode = _TVM_DIFFERENT;
1481 break;
1482 }
1483 }
1484 if(i == tags_count && tags_count < all_tags_count)
1485 tv_mode = _TVM_SUBSET;
1486 } else {
1487 tv_mode = _TVM_DIFFERENT;
1488 }
1489
1490#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
vlm766cf1e2005-03-04 08:48:53 +00001491 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
1492 MKID_nc(expr->Identifier), \
1493 expr->_type_unique_index, name); \
vlm72425de2004-09-13 08:31:01 +00001494 INDENT(+1); \
1495 /* Print the array of collected tags */ \
1496 for(i = 0; i < tags_count; i++) { \
1497 if(i) OUT(",\n"); \
1498 _print_tag(arg, &tags[i]); \
1499 } \
1500 OUT("\n"); \
1501 INDENT(-1); \
1502 OUT("};\n"); \
1503 } while(0)
1504
1505 if(tags_count) {
1506 if(tv_mode == _TVM_SUBSET)
1507 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1508 else
1509 EMIT_TAGS_TABLE("", tags, tags_count);
1510 }
1511
1512 if(all_tags_count) {
1513 if(tv_mode == _TVM_DIFFERENT)
1514 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1515 }
1516
1517 if(tags) free(tags);
1518 if(all_tags) free(all_tags);
1519
1520 *tags_count_r = tags_count;
1521 *all_tags_count_r = all_tags_count;
1522
1523 return tv_mode;
vlm4a3f5822004-06-28 21:13:46 +00001524}
vlmb2839012004-08-20 13:37:01 +00001525
1526static int
vlm4e554992004-08-25 02:03:12 +00001527expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001528 asn1p_expr_t *topmost_parent;
1529 asn1p_expr_t *v;
1530 int elements = 0;
1531
vlm39ba4c42004-09-22 16:06:28 +00001532 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001533 if(!topmost_parent) return 0;
1534
vlm80a48592005-02-25 12:10:27 +00001535 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1536 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1537 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
vlmb2839012004-08-20 13:37:01 +00001538 return 0;
1539
1540 TQ_FOR(v, &(topmost_parent->members), next) {
1541 if(v->expr_type != A1TC_EXTENSIBLE)
1542 elements++;
1543 }
1544
1545 return elements;
1546}
1547
1548static int
vlm0b567bf2005-03-04 22:18:20 +00001549emit_include_dependencies(arg_t *arg) {
1550 asn1p_expr_t *expr = arg->expr;
1551 asn1p_expr_t *memb;
1552
1553 TQ_FOR(memb, &(expr->members), next) {
1554
1555 if((memb->meta_type == AMT_TYPEREF
1556 && (memb->marker.flags & EM_INDIRECT))
1557 || expr->expr_type == ASN_CONSTR_SET_OF
1558 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1559 ) {
1560 asn1p_expr_t *terminal;
1561 terminal = asn1f_find_terminal_type_ex(arg->asn, memb);
1562 if(terminal && !terminal->parent_expr
1563 && (terminal->expr_type & ASN_CONSTR_MASK)) {
1564 int saved_target = arg->target->target;
1565 REDIR(OT_FWD_DECLS);
1566 OUT("%s;\t/* Forward declaration */\n",
1567 asn1c_type_name(arg, memb, TNF_RSAFE));
1568 REDIR(saved_target);
1569 memb->marker.flags |= EM_UNRECURSE;
1570 }
1571 }
1572
1573 if((!(memb->expr_type & ASN_CONSTR_MASK)
1574 && memb->expr_type > ASN_CONSTR_MASK)
1575 || memb->meta_type == AMT_TYPEREF) {
1576 if(memb->marker.flags & EM_UNRECURSE) {
1577 GEN_POSTINCLUDE(asn1c_type_name(arg,
1578 memb, TNF_INCLUDE));
1579 } else {
1580 GEN_INCLUDE(asn1c_type_name(arg,
1581 memb, TNF_INCLUDE));
1582 }
1583 }
1584 }
1585
1586 return 0;
1587}
1588
1589static int
vlm4e554992004-08-25 02:03:12 +00001590emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001591 int save_target;
1592 arg_t tmp_arg;
vlm060fe2a2004-09-10 09:37:12 +00001593 struct asn1p_type_tag_s outmost_tag_s;
1594 struct asn1p_type_tag_s *outmost_tag;
vlm80a48592005-02-25 12:10:27 +00001595 int complex_contents;
vlmb2839012004-08-20 13:37:01 +00001596 char *p;
1597
vlm060fe2a2004-09-10 09:37:12 +00001598 if(asn1f_fetch_outmost_tag(arg->asn,
1599 expr->module, expr, &outmost_tag_s, 1)) {
1600 outmost_tag = 0;
1601 } else {
1602 outmost_tag = &outmost_tag_s;
1603 }
1604
vlmb2839012004-08-20 13:37:01 +00001605 OUT("{ ");
vlm060fe2a2004-09-10 09:37:12 +00001606
1607 if(outmost_tag && outmost_tag->tag_value == -1)
1608 OUT("ATF_OPEN_TYPE | ");
vlm0b567bf2005-03-04 22:18:20 +00001609 OUT("%s, ",
1610 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
vlma02b74d2004-09-15 11:54:38 +00001611 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
vlmb2839012004-08-20 13:37:01 +00001612 asn1p_expr_t *tv;
1613 int opts = 0;
vlma02b74d2004-09-15 11:54:38 +00001614 for(tv = expr; tv && tv->marker.flags;
vlmb2839012004-08-20 13:37:01 +00001615 tv = TQ_NEXT(tv, next), opts++) {
1616 if(tv->expr_type == A1TC_EXTENSIBLE)
1617 opts--;
1618 }
1619 OUT("%d, ", opts);
1620 } else {
1621 OUT("0, ");
1622 }
vlm39ba4c42004-09-22 16:06:28 +00001623 if(expr->_anonymous_type) {
1624 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1625 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1626 OUT("0,\n");
1627 } else {
vlm766cf1e2005-03-04 08:48:53 +00001628 OUT("offsetof(struct ");
1629 out_name_chain(arg, 1);
1630 OUT(", ");
vlmb2839012004-08-20 13:37:01 +00001631 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1632 && (!UNNAMED_UNIONS)) OUT("choice.");
1633 OUT("%s),\n", MKID(expr->Identifier));
vlmb2839012004-08-20 13:37:01 +00001634 }
1635 INDENT(+1);
1636 if(C99_MODE) OUT(".tag = ");
vlm060fe2a2004-09-10 09:37:12 +00001637 if(outmost_tag) {
1638 if(outmost_tag->tag_value == -1)
1639 OUT("-1 /* Ambiguous tag (ANY?) */");
1640 else
1641 _print_tag(arg, outmost_tag);
vlma8a86cc2004-09-10 06:07:18 +00001642 } else {
vlm060fe2a2004-09-10 09:37:12 +00001643 OUT("-1 /* Ambiguous tag (CHOICE?) */");
vlma8a86cc2004-09-10 06:07:18 +00001644 }
vlm060fe2a2004-09-10 09:37:12 +00001645
vlmb2839012004-08-20 13:37:01 +00001646 OUT(",\n");
1647 if(C99_MODE) OUT(".tag_mode = ");
1648 if(expr->tag.tag_class) {
1649 if(expr->tag.tag_mode == TM_IMPLICIT)
1650 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1651 else
1652 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1653 } else {
1654 OUT("0,\n");
1655 }
vlm80a48592005-02-25 12:10:27 +00001656
1657 complex_contents =
1658 (expr->expr_type & ASN_CONSTR_MASK)
1659 || expr->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +00001660 || (0 /* -- prohibited by X.693:8.3.4 */
1661 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00001662 && expr_elements_count(arg, expr));
vlmb2839012004-08-20 13:37:01 +00001663 if(C99_MODE) OUT(".type = ");
vlm766cf1e2005-03-04 08:48:53 +00001664 OUT("(void *)&asn_DEF_");
1665 if(complex_contents) {
1666 OUT("%s", MKID_nc(expr->Identifier));
1667 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1668 OUT("_%d", expr->_type_unique_index);
vlmdae7f9d2004-08-22 03:25:24 +00001669 } else {
vlm766cf1e2005-03-04 08:48:53 +00001670 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
vlmdae7f9d2004-08-22 03:25:24 +00001671 }
vlm766cf1e2005-03-04 08:48:53 +00001672 OUT(",\n");
vlmb2839012004-08-20 13:37:01 +00001673 if(C99_MODE) OUT(".memb_constraints = ");
1674 if(expr->constraints) {
vlm86f5ed22004-09-26 13:11:31 +00001675 if(arg->flags & A1C_NO_CONSTRAINTS) {
1676 OUT("0,\t/* No check because of -fno-constraints */\n");
1677 } else {
vlmad970212005-03-03 21:58:28 +00001678 char *id = MKID_nc(expr->Identifier);
vlm86f5ed22004-09-26 13:11:31 +00001679 if(expr->_anonymous_type
vlmbf3c5112005-02-14 20:41:29 +00001680 && !strcmp(expr->Identifier, "Member"))
vlm86f5ed22004-09-26 13:11:31 +00001681 id = asn1c_type_name(arg, expr, TNF_SAFE);
1682 OUT("memb_%s_%d_constraint,\n", id,
vlm766cf1e2005-03-04 08:48:53 +00001683 arg->expr->_type_unique_index);
vlm86f5ed22004-09-26 13:11:31 +00001684 }
vlmb2839012004-08-20 13:37:01 +00001685 } else {
vlm95a476e2005-01-17 12:16:58 +00001686 OUT("0,\t/* Defer constraints checking to the member type */\n");
vlmb2839012004-08-20 13:37:01 +00001687 }
1688 if(C99_MODE) OUT(".name = ");
vlmbf3c5112005-02-14 20:41:29 +00001689 if(1) {
1690 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1691 OUT("\"\"\n");
1692 else
1693 OUT("\"%s\"\n", expr->Identifier);
1694 } else {
1695 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1696 }
vlmb2839012004-08-20 13:37:01 +00001697 OUT("},\n");
1698 INDENT(-1);
1699
vlm86f5ed22004-09-26 13:11:31 +00001700 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlmb2839012004-08-20 13:37:01 +00001701 return 0;
1702
1703 save_target = arg->target->target;
1704 REDIR(OT_CODE);
1705
vlmbf3c5112005-02-14 20:41:29 +00001706 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
vlmb2839012004-08-20 13:37:01 +00001707 p = asn1c_type_name(arg, expr, TNF_SAFE);
vlm39ba4c42004-09-22 16:06:28 +00001708 else
vlmad970212005-03-03 21:58:28 +00001709 p = MKID_nc(expr->Identifier);
vlmb2839012004-08-20 13:37:01 +00001710 OUT("static int\n");
vlm766cf1e2005-03-04 08:48:53 +00001711 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 +00001712 INDENT(+1);
1713 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1714 tmp_arg = *arg;
1715 tmp_arg.expr = expr;
1716 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
vlm86f5ed22004-09-26 13:11:31 +00001717 OUT("return td->check_constraints"
1718 "(td, sptr, app_errlog, app_key);\n");
vlmb2839012004-08-20 13:37:01 +00001719 }
1720 INDENT(-1);
1721 OUT("}\n");
1722 OUT("\n");
1723
1724 REDIR(save_target);
1725
1726 return 0;
1727}
vlm4e554992004-08-25 02:03:12 +00001728
vlm9de248e2004-10-20 15:50:55 +00001729/*
1730 * Generate "asn_DEF_XXX" type definition.
1731 */
vlm4e554992004-08-25 02:03:12 +00001732static int
vlm86f5ed22004-09-26 13:11:31 +00001733emit_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 +00001734 int using_type_name = 0;
vlm4e554992004-08-25 02:03:12 +00001735 char *p;
1736
vlm12c8f692004-09-06 08:07:29 +00001737 if(HIDE_INNER_DEFS)
1738 OUT("static /* Use -fall-defs-global to expose */\n");
vlm766cf1e2005-03-04 08:48:53 +00001739 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID_nc(expr->Identifier));
1740 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1741 OUT(" = {\n");
vlmaf841972005-01-28 12:18:50 +00001742 p = MKID(expr->Identifier);
vlm72425de2004-09-13 08:31:01 +00001743 INDENT(+1);
vlm4e554992004-08-25 02:03:12 +00001744 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm9de248e2004-10-20 15:50:55 +00001745 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001746
1747 if(expr->expr_type & ASN_CONSTR_MASK) {
vlm766cf1e2005-03-04 08:48:53 +00001748 using_type_name = 1;
vlm4e554992004-08-25 02:03:12 +00001749 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1750 }
1751
vlm766cf1e2005-03-04 08:48:53 +00001752#define FUNCREF(foo) do { \
1753 OUT("%s", p); \
1754 if(HIDE_INNER_DEFS && !using_type_name) \
1755 OUT("_%d", expr->_type_unique_index); \
1756 OUT("_" #foo ",\n"); \
1757} while(0)
1758
1759 FUNCREF(free);
1760 FUNCREF(print);
1761 FUNCREF(constraint);
1762 FUNCREF(decode_ber);
1763 FUNCREF(encode_der);
1764 FUNCREF(decode_xer);
1765 FUNCREF(encode_xer);
vlm4e554992004-08-25 02:03:12 +00001766
vlm4e554992004-08-25 02:03:12 +00001767 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1768 OUT("CHOICE_outmost_tag,\n");
1769 } else {
1770 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1771 }
1772
vlmad970212005-03-03 21:58:28 +00001773 p = MKID_nc(expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001774 if(tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001775 OUT("asn_DEF_%s_%d_tags,\n",
1776 p, expr->_type_unique_index);
1777 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1778 p, expr->_type_unique_index);
1779 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1780 p, expr->_type_unique_index);
vlm72425de2004-09-13 08:31:01 +00001781 if(tv_mode == _TVM_SUBSET
1782 && tags_count != all_tags_count)
1783 OUT(" - %d", all_tags_count - tags_count);
1784 OUT(", /* %d */\n", tags_count);
vlm4e554992004-08-25 02:03:12 +00001785 } else {
vlm72425de2004-09-13 08:31:01 +00001786 OUT("0,\t/* No effective tags (pointer) */\n");
1787 OUT("0,\t/* No effective tags (count) */\n");
1788 }
1789
1790 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
vlm766cf1e2005-03-04 08:48:53 +00001791 OUT("asn_DEF_%s_%d_all_tags,\n",
1792 p, expr->_type_unique_index);
1793 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1794 p, expr->_type_unique_index);
1795 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1796 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001797 } else if(all_tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001798 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1799 p, expr->_type_unique_index);
1800 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1801 p, expr->_type_unique_index);
1802 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1803 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001804 } else {
1805 OUT("0,\t/* No tags (pointer) */\n");
1806 OUT("0,\t/* No tags (count) */\n");
vlm4e554992004-08-25 02:03:12 +00001807 }
1808
vlm4e554992004-08-25 02:03:12 +00001809 if(elements_count) {
vlm766cf1e2005-03-04 08:48:53 +00001810 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001811 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1812 || expr->expr_type == ASN_CONSTR_SET_OF) {
1813 OUT("%d,\t/* Single element */\n",
1814 elements_count);
1815 assert(elements_count == 1);
1816 } else {
1817 OUT("%d,\t/* Elements count */\n",
1818 elements_count);
1819 }
1820 } else {
vlm4e554992004-08-25 02:03:12 +00001821 if(expr_elements_count(arg, expr))
1822 OUT("0, 0,\t/* Defined elsewhere */\n");
1823 else
1824 OUT("0, 0,\t/* No members */\n");
1825 }
1826
1827 switch(spec) {
1828 case ETD_NO_SPECIFICS:
1829 OUT("0\t/* No specifics */\n");
1830 break;
1831 case ETD_HAS_SPECIFICS:
vlm766cf1e2005-03-04 08:48:53 +00001832 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1833 p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001834 }
vlm72425de2004-09-13 08:31:01 +00001835 INDENT(-1);
vlm4e554992004-08-25 02:03:12 +00001836 OUT("};\n");
1837 OUT("\n");
vlme2bb4432004-08-26 06:20:34 +00001838
1839 return 0;
vlm4e554992004-08-25 02:03:12 +00001840}
vlmddd5a7d2004-09-10 09:18:20 +00001841
1842/*
1843 * Check if it is better to make this type indirectly accessed via
1844 * a pointer.
1845 * This may be the case for the following recursive definition:
1846 * Type ::= CHOICE { member Type };
1847 */
1848static int
1849expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) {
1850 asn1p_expr_t *top_parent;
1851 asn1p_expr_t *terminal;
1852
1853 if(expr->expr_type != A1TC_REFERENCE)
1854 return 0;
1855
1856 /* Rewind to the topmost parent expression */
1857 if((top_parent = expr->parent_expr)) {
1858 while(top_parent->parent_expr)
1859 top_parent = top_parent->parent_expr;
1860 } else {
1861 return 0;
1862 }
1863
vlm39ba4c42004-09-22 16:06:28 +00001864 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmddd5a7d2004-09-10 09:18:20 +00001865
1866 return (terminal == top_parent);
1867}
vlm39ba4c42004-09-22 16:06:28 +00001868
1869static int
1870expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1871 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1872 if(!expr) return 0;
1873
1874 /* X.680, 25.5, Table 5 */
1875 switch(expr->expr_type) {
1876 case ASN_CONSTR_CHOICE:
1877 case ASN_BASIC_BOOLEAN:
1878 case ASN_BASIC_ENUMERATED:
1879 case ASN_BASIC_NULL:
1880 return 1;
1881 default:
1882 return 0;
1883 }
1884}
vlmaf841972005-01-28 12:18:50 +00001885
1886static int
vlm766cf1e2005-03-04 08:48:53 +00001887out_name_chain(arg_t *arg, int check_reserved_keywords) {
vlmaf841972005-01-28 12:18:50 +00001888 asn1p_expr_t *expr = arg->expr;
1889 char *id;
1890
1891 assert(expr->Identifier);
1892
vlm766cf1e2005-03-04 08:48:53 +00001893 if(arg->flags & A1C_COMPOUND_NAMES
1894 && ((expr->expr_type & ASN_CONSTR_MASK)
1895 || expr->expr_type == ASN_BASIC_ENUMERATED
1896 || (expr->expr_type == ASN_BASIC_INTEGER
1897 && expr_elements_count(arg, expr))
1898 )
vlmaf841972005-01-28 12:18:50 +00001899 && expr->parent_expr
1900 && expr->parent_expr->Identifier) {
1901 arg_t tmparg = *arg;
1902
1903 tmparg.expr = expr->parent_expr;
vlm766cf1e2005-03-04 08:48:53 +00001904 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
1905
1906 out_name_chain(&tmparg, 0);
vlmaf841972005-01-28 12:18:50 +00001907
1908 OUT("_"); /* a separator between id components */
vlm766cf1e2005-03-04 08:48:53 +00001909
vlmaf841972005-01-28 12:18:50 +00001910 /* Fall through */
1911 }
1912
1913 if(check_reserved_keywords)
1914 id = MKID(expr->Identifier);
1915 else
vlmad970212005-03-03 21:58:28 +00001916 id = MKID_nc(expr->Identifier);
vlmaf841972005-01-28 12:18:50 +00001917 OUT("%s", id);
1918
1919 return 0;
1920}