blob: 5615328e8572f2cce9c5eb1a81fe2fb06cefe9c4 [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
vlm1f7df782005-03-04 23:48:19 +000072/* MKID_safe() without checking for reserved keywords */
73#define MKID(id) asn1c_make_identifier(0, (id), 0)
74#define MKID_safe(id) asn1c_make_identifier(AMI_CHECK_RESERVED, (id), 0)
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);
vlm1f7df782005-03-04 23:48:19 +0000130 OUT("_%s", MKID(v->Identifier));
vlm766cf1e2005-03-04 08:48:53 +0000131 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",
vlm1f7df782005-03-04 23:48:19 +0000159 MKID(expr->Identifier), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000160 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
161 for(eidx = 0; eidx < el_count; eidx++) {
162 v2e[eidx].idx = eidx;
163 OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n",
164 v2e[eidx].value,
165 (long)strlen(v2e[eidx].name), v2e[eidx].name,
166 (eidx + 1 < el_count) ? "," : "");
167 }
168 if(map_is_extensible)
169 OUT("\t/* This list is extensible */\n");
170 OUT("};\n");
171
vlm766cf1e2005-03-04 08:48:53 +0000172 OUT("static unsigned int asn_MAP_%s_%d_enum2value[] = {\n",
vlm1f7df782005-03-04 23:48:19 +0000173 MKID(expr->Identifier), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000174 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
175 for(eidx = 0; eidx < el_count; eidx++) {
176 OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n",
177 v2e[eidx].idx,
178 (eidx + 1 < el_count) ? "," : "",
179 v2e[eidx].name, v2e[eidx].value);
180 }
181 if(map_is_extensible)
182 OUT("\t/* This list is extensible */\n");
183 OUT("};\n");
184
vlm766cf1e2005-03-04 08:48:53 +0000185 OUT("static asn_INTEGER_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000186 MKID(expr->Identifier), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000187 INDENT(+1);
vlm766cf1e2005-03-04 08:48:53 +0000188 OUT("asn_MAP_%s_%d_value2enum,\t"
vlm80a48592005-02-25 12:10:27 +0000189 "/* \"tag\" => N; sorted by tag */\n",
vlm1f7df782005-03-04 23:48:19 +0000190 MKID(expr->Identifier),
vlm766cf1e2005-03-04 08:48:53 +0000191 expr->_type_unique_index);
192 OUT("asn_MAP_%s_%d_enum2value,\t"
vlm80a48592005-02-25 12:10:27 +0000193 "/* N => \"tag\"; sorted by N */\n",
vlm1f7df782005-03-04 23:48:19 +0000194 MKID(expr->Identifier),
vlm766cf1e2005-03-04 08:48:53 +0000195 expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000196 OUT("%d,\t/* Number of elements in the maps */\n",
197 el_count);
198 OUT("%d,\t/* Enumeration is %sextensible */\n",
199 map_is_extensible, map_is_extensible ? "": "not ");
200 if(expr->expr_type == ASN_BASIC_ENUMERATED)
201 OUT("1\t/* Strict enumeration */\n");
202 else
203 OUT("0\n");
204 INDENT(-1);
205 OUT("};\n");
vlmfa67ddc2004-06-03 03:38:44 +0000206 }
207
208 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
209}
210
211int
212asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
213 asn1p_expr_t *expr = arg->expr;
214 asn1p_expr_t *v;
215 int comp_mode = 0; /* {root,ext=1,root,root,...} */
216
217 DEPENDENCIES;
218
219 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000220 OUT("struct ");
221 out_name_chain(arg, 1);
222 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000223 } else {
224 OUT("typedef struct %s {\n",
vlm1f7df782005-03-04 23:48:19 +0000225 MKID_safe(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000226 }
227
228 TQ_FOR(v, &(expr->members), next) {
229 if(v->expr_type == A1TC_EXTENSIBLE) {
230 if(comp_mode < 3) comp_mode++;
231 }
vlmddd5a7d2004-09-10 09:18:20 +0000232 if(comp_mode == 1
233 || expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000234 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000235 EMBED(v);
236 }
237
238 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000239 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000240 expr->_anonymous_type ? "" :
241 arg->embed
242 ? MKID_safe(expr->Identifier)
243 : MKID(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000244 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000245
246 return asn1c_lang_C_type_SEQUENCE_def(arg);
247}
248
249static int
250asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
251 asn1p_expr_t *expr = arg->expr;
252 asn1p_expr_t *v;
253 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000254 int ext_start = -1;
255 int ext_stop = -1;
vlm4e03ce22004-06-06 07:20:17 +0000256 tag2el_t *tag2el = NULL;
257 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000258 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000259 int all_tags_count;
260 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000261
vlm4e03ce22004-06-06 07:20:17 +0000262 /*
263 * Fetch every inner tag from the tag to elements map.
264 */
vlm940bc6b2004-10-03 09:13:30 +0000265 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlm4e03ce22004-06-06 07:20:17 +0000266 if(tag2el) free(tag2el);
267 return -1;
268 }
269
vlm33a4ff12004-08-11 05:21:32 +0000270 GEN_INCLUDE("constr_SEQUENCE");
271 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000272 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000273
vlm33a4ff12004-08-11 05:21:32 +0000274 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000275
276 /*
277 * Print out the table according to which the parsing is performed.
278 */
vlm39ba4c42004-09-22 16:06:28 +0000279 if(expr_elements_count(arg, expr)) {
280 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000281
vlm766cf1e2005-03-04 08:48:53 +0000282 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm1f7df782005-03-04 23:48:19 +0000283 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000284
285 elements = 0;
286 INDENTED(TQ_FOR(v, &(expr->members), next) {
287 if(v->expr_type == A1TC_EXTENSIBLE) {
288 if((++comp_mode) == 1)
289 ext_start = elements - 1;
290 else
291 ext_stop = elements - 1;
292 continue;
293 }
294 elements++;
295 emit_member_table(arg, v);
296 });
297 OUT("};\n");
298 } else {
299 elements = 0;
300 }
vlmfa67ddc2004-06-03 03:38:44 +0000301
vlm4a3f5822004-06-28 21:13:46 +0000302 /*
vlma5dcb912004-09-29 13:16:40 +0000303 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000304 */
vlm72425de2004-09-13 08:31:01 +0000305 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000306
vlm4e03ce22004-06-06 07:20:17 +0000307 /*
308 * Tags to elements map.
309 */
vlm940bc6b2004-10-03 09:13:30 +0000310 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlm4e03ce22004-06-06 07:20:17 +0000311
vlm766cf1e2005-03-04 08:48:53 +0000312 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000313 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000314 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000315 OUT("sizeof(struct "); out_name_chain(arg, 1); OUT("),\n");
316 OUT("offsetof(struct "); out_name_chain(arg, 1); OUT(", _asn_ctx),\n");
317
318 OUT("asn_MAP_%s_%d_tag2el,\n",
vlm1f7df782005-03-04 23:48:19 +0000319 MKID(expr->Identifier), expr->_type_unique_index);
vlm4e03ce22004-06-06 07:20:17 +0000320 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlmfa67ddc2004-06-03 03:38:44 +0000321 OUT("%d,\t/* Start extensions */\n",
322 ext_start);
323 OUT("%d\t/* Stop extensions */\n",
324 (ext_stop<ext_start)?elements+1:ext_stop, ext_stop);
325 );
326 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000327
328 /*
vlma5dcb912004-09-29 13:16:40 +0000329 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000330 */
vlm72425de2004-09-13 08:31:01 +0000331 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000332 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000333
vlmfa67ddc2004-06-03 03:38:44 +0000334 REDIR(OT_TYPE_DECLS);
335
336 return 0;
vlmedf203f2005-01-17 11:57:48 +0000337} /* _SEQUENCE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000338
339int
vlmfa67ddc2004-06-03 03:38:44 +0000340asn1c_lang_C_type_SET(arg_t *arg) {
341 asn1p_expr_t *expr = arg->expr;
342 asn1p_expr_t *v;
343 long mcount;
344 char *id;
345 int comp_mode = 0; /* {root,ext=1,root,root,...} */
346
347 DEPENDENCIES;
348
349 REDIR(OT_DEPS);
350
351 OUT("\n");
352 OUT("/*\n");
353 OUT(" * Method of determining the components presence\n");
354 OUT(" */\n");
355 mcount = 0;
vlm766cf1e2005-03-04 08:48:53 +0000356 OUT("typedef enum ");
357 out_name_chain(arg, 0);
358 OUT("_PR {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000359 TQ_FOR(v, &(expr->members), next) {
360 if(v->expr_type == A1TC_EXTENSIBLE) continue;
361 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000362 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000363 OUT("_PR_");
vlm1f7df782005-03-04 23:48:19 +0000364 id = MKID(v->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000365 OUT("%s,\t/* Member %s is present */\n",
366 id, id)
367 );
368 mcount++;
369 }
vlm766cf1e2005-03-04 08:48:53 +0000370 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
vlmfa67ddc2004-06-03 03:38:44 +0000371
372 REDIR(OT_TYPE_DECLS);
373
374 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000375 OUT("struct ");
376 out_name_chain(arg, 1);
377 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000378 } else {
vlm1f7df782005-03-04 23:48:19 +0000379 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
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(
vlm1f7df782005-03-04 23:48:19 +0000393 id = MKID(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)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000403 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000404 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",
vlm1f7df782005-03-04 23:48:19 +0000454 MKID(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",
vlm1f7df782005-03-04 23:48:19 +0000489 MKID(expr->Identifier), expr->_type_unique_index);
490 p = MKID_safe(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",
vlm1f7df782005-03-04 23:48:19 +0000520 MKID(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");
vlm1f7df782005-03-04 23:48:19 +0000531 p = MKID(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 {
vlm1f7df782005-03-04 23:48:19 +0000572 OUT("typedef struct %s {\n", MKID_safe(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 {
vlm6c5d5e52005-03-04 22:38:22 +0000605 OUT("%s", asn1c_type_name(arg, memb,
606 (memb->marker.flags & EM_UNRECURSE)
607 ? TNF_RSAFE : TNF_CTYPE));
vlmfa67ddc2004-06-03 03:38:44 +0000608 }
vlmdae7f9d2004-08-22 03:25:24 +0000609 OUT(") list;\n");
610 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000611
612 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000613 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000614 expr->_anonymous_type ? "" : MKID_safe(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000615 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000616
617 /*
vlmdae7f9d2004-08-22 03:25:24 +0000618 * SET OF/SEQUENCE OF definition
vlmfa67ddc2004-06-03 03:38:44 +0000619 */
vlmdae7f9d2004-08-22 03:25:24 +0000620 return asn1c_lang_C_type_SEx_OF_def(arg,
621 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
vlmfa67ddc2004-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;
vlm6e73a042004-08-11 07:17:22 +0000628 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000629 int all_tags_count;
630 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000631
vlm33a4ff12004-08-11 05:21:32 +0000632 /*
633 * Print out the table according to which the parsing is performed.
634 */
vlmfa67ddc2004-06-03 03:38:44 +0000635 if(seq_of) {
vlm33a4ff12004-08-11 05:21:32 +0000636 GEN_INCLUDE("constr_SEQUENCE_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000637 } else {
vlm33a4ff12004-08-11 05:21:32 +0000638 GEN_INCLUDE("constr_SET_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000639 }
vlm33a4ff12004-08-11 05:21:32 +0000640 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000641 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-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 */
vlm766cf1e2005-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);
vlm39ba4c42004-09-22 16:06:28 +0000650 INDENT(+1);
vlmfa67ddc2004-06-03 03:38:44 +0000651 v = TQ_FIRST(&(expr->members));
vlm39ba4c42004-09-22 16:06:28 +0000652 if(!v->Identifier) {
vlmbf3c5112005-02-14 20:41:29 +0000653 v->Identifier = strdup("Member");
vlm39ba4c42004-09-22 16:06:28 +0000654 assert(v->Identifier);
655 }
656 v->_anonymous_type = 1;
657 arg->embed++;
vlm4e554992004-08-25 02:03:12 +0000658 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000659 arg->embed--;
660 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000661 OUT("};\n");
662
vlm4a3f5822004-06-28 21:13:46 +0000663 /*
vlma5dcb912004-09-29 13:16:40 +0000664 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000665 */
vlm72425de2004-09-13 08:31:01 +0000666 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000667
vlm766cf1e2005-03-04 08:48:53 +0000668 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000669 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000670 INDENTED(
vlm766cf1e2005-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
vlm39ba4c42004-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");
vlmfa67ddc2004-06-03 03:38:44 +0000682 );
683 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000684
685 /*
vlma5dcb912004-09-29 13:16:40 +0000686 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000687 */
vlm72425de2004-09-13 08:31:01 +0000688 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
vlm86f5ed22004-09-26 13:11:31 +0000689 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000690
vlmfa67ddc2004-06-03 03:38:44 +0000691 REDIR(OT_TYPE_DECLS);
692
693 return 0;
vlmedf203f2005-01-17 11:57:48 +0000694} /* _SEx_OF_def() */
vlmfa67ddc2004-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;
vlmdae7f9d2004-08-22 03:25:24 +0000700 char *id;
vlmfa67ddc2004-06-03 03:38:44 +0000701
702 DEPENDENCIES;
703
vlm33a4ff12004-08-11 05:21:32 +0000704 REDIR(OT_DEPS);
705
vlm766cf1e2005-03-04 08:48:53 +0000706 OUT("typedef enum ");
707 out_name_chain(arg, 0);
708 OUT("_PR {\n");
vlm33a4ff12004-08-11 05:21:32 +0000709 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000710 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000711 OUT("_PR_NOTHING,\t/* No components present */\n");
vlm33a4ff12004-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 }
vlm766cf1e2005-03-04 08:48:53 +0000717 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000718 OUT("_PR_");
vlm1f7df782005-03-04 23:48:19 +0000719 id = MKID(v->Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000720 OUT("%s,\n", id, id);
vlm33a4ff12004-08-11 05:21:32 +0000721 }
722 );
vlm766cf1e2005-03-04 08:48:53 +0000723 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
vlm33a4ff12004-08-11 05:21:32 +0000724
725 REDIR(OT_TYPE_DECLS);
vlmfa67ddc2004-06-03 03:38:44 +0000726
727 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000728 OUT("struct "); out_name_chain(arg, 1); OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000729 } else {
vlm1f7df782005-03-04 23:48:19 +0000730 OUT("typedef struct %s {\n", MKID_safe(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000731 }
732
733 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000734 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000735 OUT("_PR present;\n");
736 OUT("union {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000737 TQ_FOR(v, &(expr->members), next) {
vlmddd5a7d2004-09-10 09:18:20 +0000738 if(expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000739 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000740 EMBED(v);
741 }
742 if(UNNAMED_UNIONS) OUT("};\n");
743 else OUT("} choice;\n");
744 );
745
746 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000747 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000748 expr->_anonymous_type ? "" :
749 arg->embed
750 ? MKID_safe(expr->Identifier)
751 : MKID(expr->Identifier),
vlmdae7f9d2004-08-22 03:25:24 +0000752 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000753
754 return asn1c_lang_C_type_CHOICE_def(arg);
755}
756
757static int
758asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
759 asn1p_expr_t *expr = arg->expr;
760 asn1p_expr_t *v;
761 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000762 tag2el_t *tag2el = NULL;
763 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000764 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000765 int all_tags_count;
766 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000767
768 /*
769 * Fetch every inner tag from the tag to elements map.
770 */
vlm940bc6b2004-10-03 09:13:30 +0000771 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000772 if(tag2el) free(tag2el);
773 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000774 }
775
vlm33a4ff12004-08-11 05:21:32 +0000776 GEN_INCLUDE("constr_CHOICE");
777 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000778 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000779
vlm33a4ff12004-08-11 05:21:32 +0000780 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000781
782 /*
783 * Print out the table according to which the parsing is performed.
784 */
vlm39ba4c42004-09-22 16:06:28 +0000785 if(expr_elements_count(arg, expr)) {
vlmfa67ddc2004-06-03 03:38:44 +0000786
vlm766cf1e2005-03-04 08:48:53 +0000787 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
788 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000789
790 elements = 0;
791 INDENTED(TQ_FOR(v, &(expr->members), next) {
vlm34dcd572005-01-17 11:40:49 +0000792 if(v->expr_type == A1TC_EXTENSIBLE)
793 continue;
794 if(expr_better_indirect(arg, v))
795 v->marker.flags |= EM_INDIRECT;
796 elements++;
797 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000798 });
799 OUT("};\n");
800 } else {
801 elements = 0;
802 }
vlmfa67ddc2004-06-03 03:38:44 +0000803
vlm6e73a042004-08-11 07:17:22 +0000804
vlmfa67ddc2004-06-03 03:38:44 +0000805 if(arg->embed) {
806 /*
807 * Our parent structure has already taken this into account.
808 */
vlm72425de2004-09-13 08:31:01 +0000809 tv_mode = _TVM_SAME;
810 tags_count = all_tags_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000811 } else {
vlm72425de2004-09-13 08:31:01 +0000812 tv_mode = emit_tags_vectors(arg, expr,
813 &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000814 }
vlmfa67ddc2004-06-03 03:38:44 +0000815
816 /*
817 * Tags to elements map.
818 */
vlm940bc6b2004-10-03 09:13:30 +0000819 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000820
vlm766cf1e2005-03-04 08:48:53 +0000821 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
vlm1f7df782005-03-04 23:48:19 +0000822 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000823 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000824 OUT("sizeof(struct ");
825 out_name_chain(arg, 1);
826 OUT("),\n");
827 OUT("offsetof(struct ");
828 out_name_chain(arg, 1);
829 OUT(", _asn_ctx),\n");
830 OUT("offsetof(struct ");
831 out_name_chain(arg, 1);
832 OUT(", present),\n");
833 OUT("sizeof(((struct ");
834 out_name_chain(arg, 1);
835 OUT(" *)0)->present),\n");
836 OUT("asn_MAP_%s_%d_tag2el,\n",
vlm1f7df782005-03-04 23:48:19 +0000837 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000838 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm79b08d52004-07-01 00:52:50 +0000839 OUT("%d\t/* Whether extensible */\n",
840 check_if_extensible(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000841 );
842 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000843
844 /*
vlma5dcb912004-09-29 13:16:40 +0000845 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000846 */
vlm72425de2004-09-13 08:31:01 +0000847 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000848 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000849
vlmfa67ddc2004-06-03 03:38:44 +0000850 REDIR(OT_TYPE_DECLS);
851
852 return 0;
vlmedf203f2005-01-17 11:57:48 +0000853} /* _CHOICE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000854
855int
856asn1c_lang_C_type_REFERENCE(arg_t *arg) {
857 asn1p_ref_t *ref;
858
859 ref = arg->expr->reference;
860 if(ref->components[ref->comp_count-1].name[0] == '&') {
vlmfa67ddc2004-06-03 03:38:44 +0000861 asn1p_expr_t *extract;
862 arg_t tmp;
863 int ret;
864
vlm39ba4c42004-09-22 16:06:28 +0000865 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
vlmdae7f9d2004-08-22 03:25:24 +0000866 arg->expr, ref);
vlmfa67ddc2004-06-03 03:38:44 +0000867 if(extract == NULL)
868 return -1;
869
vlmdae7f9d2004-08-22 03:25:24 +0000870 extract = asn1p_expr_clone(extract, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000871 if(extract) {
872 if(extract->Identifier)
873 free(extract->Identifier);
874 extract->Identifier = strdup(arg->expr->Identifier);
875 if(extract->Identifier == NULL) {
876 asn1p_expr_free(extract);
877 return -1;
878 }
879 } else {
880 return -1;
881 }
882
883 tmp = *arg;
884 tmp.asn = arg->asn;
vlmdae7f9d2004-08-22 03:25:24 +0000885 tmp.mod = extract->module;
vlmfa67ddc2004-06-03 03:38:44 +0000886 tmp.expr = extract;
887
888 ret = arg->default_cb(&tmp);
889
890 asn1p_expr_free(extract);
891
892 return ret;
893 }
894
895
896 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
897}
898
899int
900asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
901 asn1p_expr_t *expr = arg->expr;
vlm6e73a042004-08-11 07:17:22 +0000902 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000903 int all_tags_count;
904 enum tvm_compat tv_mode;
vlm80a48592005-02-25 12:10:27 +0000905 enum etd_spec etd_spec;
vlmfa67ddc2004-06-03 03:38:44 +0000906 char *p;
907
908 if(arg->embed) {
vlma5dcb912004-09-29 13:16:40 +0000909 enum tnfmt tnfmt = TNF_CTYPE;
910
911 /*
912 * If this is an optional compound type,
913 * refer it using "struct X" convention,
914 * as it may recursively include the current structure.
915 */
vlm0b567bf2005-03-04 22:18:20 +0000916 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
vlma5dcb912004-09-29 13:16:40 +0000917 asn1p_expr_t *terminal;
918 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
919 if(terminal
920 && (terminal->expr_type & ASN_CONSTR_MASK)) {
vlm0b567bf2005-03-04 22:18:20 +0000921 tnfmt = TNF_RSAFE;
922 REDIR(OT_FWD_DECLS);
vlm5feb7522005-03-04 23:50:56 +0000923 OUT("%s;\n",
vlm0b567bf2005-03-04 22:18:20 +0000924 asn1c_type_name(arg, arg->expr, tnfmt));
vlma5dcb912004-09-29 13:16:40 +0000925 }
926 }
927
vlmfa67ddc2004-06-03 03:38:44 +0000928 REDIR(OT_TYPE_DECLS);
929
vlm0b567bf2005-03-04 22:18:20 +0000930 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
vlm80a48592005-02-25 12:10:27 +0000931 if(!expr->_anonymous_type) {
vlm0b567bf2005-03-04 22:18:20 +0000932 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
vlm1f7df782005-03-04 23:48:19 +0000933 OUT("%s", MKID_safe(expr->Identifier));
vlm80a48592005-02-25 12:10:27 +0000934 if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT)
935 OUT("\t/* DEFAULT %s */",
936 asn1f_printable_value(
937 expr->marker.default_value));
938 else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL)
939 OUT("\t/* OPTIONAL */");
940 }
941
942 } else {
943 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
vlmb2839012004-08-20 13:37:01 +0000944
945 REDIR(OT_TYPE_DECLS);
vlm80a48592005-02-25 12:10:27 +0000946
947 OUT("typedef %s\t",
vlm0b567bf2005-03-04 22:18:20 +0000948 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
vlm80a48592005-02-25 12:10:27 +0000949 OUT("%s%s_t",
vlm0b567bf2005-03-04 22:18:20 +0000950 (expr->marker.flags & EM_INDIRECT)?"*":" ",
vlm1f7df782005-03-04 23:48:19 +0000951 MKID(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000952 }
953
vlm80a48592005-02-25 12:10:27 +0000954 if((expr->expr_type == ASN_BASIC_ENUMERATED)
vlmc89422d2005-03-03 21:28:12 +0000955 || (0 /* -- prohibited by X.693:8.3.4 */
956 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +0000957 && expr_elements_count(arg, expr)))
958 etd_spec = ETD_HAS_SPECIFICS;
959 else
960 etd_spec = ETD_NO_SPECIFICS;
vlmfa67ddc2004-06-03 03:38:44 +0000961
vlm86f5ed22004-09-26 13:11:31 +0000962 /*
963 * If this type just blindly refers the other type, alias it.
964 * Type1 ::= Type2
965 */
vlm80a48592005-02-25 12:10:27 +0000966 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
967 REDIR(OT_TYPE_DECLS);
968 return 0;
969 }
vlm86f5ed22004-09-26 13:11:31 +0000970 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlm80a48592005-02-25 12:10:27 +0000971 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
vlm6f74b0f2005-03-04 23:23:50 +0000972 && etd_spec == ETD_NO_SPECIFICS
973 && 0 /* This shortcut is incompatible with XER */
974 ) {
vlm86f5ed22004-09-26 13:11:31 +0000975 char *type_name;
976 REDIR(OT_FUNC_DECLS);
977 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
978 OUT("/* This type is equivalent to %s */\n", type_name);
vlm86f5ed22004-09-26 13:11:31 +0000979 if(HIDE_INNER_DEFS) OUT("/* ");
vlm1f7df782005-03-04 23:48:19 +0000980 OUT("#define\tasn_DEF_%s\t", MKID(expr->Identifier));
vlm86f5ed22004-09-26 13:11:31 +0000981 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlm80a48592005-02-25 12:10:27 +0000982 OUT("asn_DEF_%s", type_name);
vlm86f5ed22004-09-26 13:11:31 +0000983 if(HIDE_INNER_DEFS)
vlm80a48592005-02-25 12:10:27 +0000984 OUT("\t// (Use -fall-defs-global to expose) */");
985 OUT("\n");
vlm86f5ed22004-09-26 13:11:31 +0000986 REDIR(OT_CODE);
987 OUT("/* This type is equivalent to %s */\n", type_name);
988 OUT("\n");
989 REDIR(OT_TYPE_DECLS);
990 return 0;
991 }
992
vlmfa67ddc2004-06-03 03:38:44 +0000993 REDIR(OT_STAT_DEFS);
994
vlm4a3f5822004-06-28 21:13:46 +0000995 /*
vlma5dcb912004-09-29 13:16:40 +0000996 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000997 */
vlm72425de2004-09-13 08:31:01 +0000998 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000999
vlm80a48592005-02-25 12:10:27 +00001000 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1001 0, etd_spec);
vlmfa67ddc2004-06-03 03:38:44 +00001002
vlmb2839012004-08-20 13:37:01 +00001003 REDIR(OT_CODE);
1004
vlmfa67ddc2004-06-03 03:38:44 +00001005 /*
1006 * Constraint checking.
1007 */
vlm86f5ed22004-09-26 13:11:31 +00001008 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1009 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001010 if(HIDE_INNER_DEFS) OUT("static ");
vlm86f5ed22004-09-26 13:11:31 +00001011 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001012 OUT("%s", p);
1013 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1014 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
vlm86f5ed22004-09-26 13:11:31 +00001015 INDENT(+1);
1016 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1017 OUT("\n");
1018 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1019 OUT("/* Replace with underlying type checker */\n");
1020 OUT("td->check_constraints "
vlma5dcb912004-09-29 13:16:40 +00001021 "= asn_DEF_%s.check_constraints;\n",
vlm86f5ed22004-09-26 13:11:31 +00001022 asn1c_type_name(arg, expr, TNF_SAFE));
1023 OUT("return td->check_constraints"
1024 "(td, sptr, app_errlog, app_key);\n");
vlm1d036692004-08-19 13:29:46 +00001025 }
vlm86f5ed22004-09-26 13:11:31 +00001026 INDENT(-1);
1027 OUT("}\n");
1028 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001029 }
vlmfa67ddc2004-06-03 03:38:44 +00001030
1031 /*
1032 * Emit suicidal functions.
1033 */
1034
vlmfa67ddc2004-06-03 03:38:44 +00001035 /*
1036 * This function replaces certain fields from the definition
1037 * of a type with the corresponding fields from the basic type
1038 * (from which the current type is inherited).
1039 */
vlmfa67ddc2004-06-03 03:38:44 +00001040 OUT("/*\n");
vlmb2839012004-08-20 13:37:01 +00001041 OUT(" * This type is implemented using %s,\n",
1042 asn1c_type_name(arg, expr, TNF_SAFE));
vlmdcd32fe2004-09-23 22:20:47 +00001043 OUT(" * so here we adjust the DEF accordingly.\n");
vlmfa67ddc2004-06-03 03:38:44 +00001044 OUT(" */\n");
1045 OUT("static void\n");
vlm766cf1e2005-03-04 08:48:53 +00001046 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
vlm1f7df782005-03-04 23:48:19 +00001047 MKID(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001048 INDENT(+1);
vlm80a48592005-02-25 12:10:27 +00001049 {
vlm39ba4c42004-09-22 16:06:28 +00001050 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001051 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlma5dcb912004-09-29 13:16:40 +00001052 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1053 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1054 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1055 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1056 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1057 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
vlm1308d2b2004-09-10 15:49:15 +00001058 if(!terminal && !tags_count) {
vlm72425de2004-09-13 08:31:01 +00001059 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
vlma5dcb912004-09-29 13:16:40 +00001060 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1061 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1062 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1063 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
vlm1308d2b2004-09-10 15:49:15 +00001064 OUT("/* End of these lines */\n");
1065 }
vlma5dcb912004-09-29 13:16:40 +00001066 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1067 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
vlm80a48592005-02-25 12:10:27 +00001068 if(etd_spec != ETD_NO_SPECIFICS) {
1069 INDENT(-1);
1070 OUT(" /* ");
vlmb2839012004-08-20 13:37:01 +00001071 }
vlm80a48592005-02-25 12:10:27 +00001072 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1073 if(etd_spec == ETD_NO_SPECIFICS) {
1074 INDENT(-1);
1075 OUT("\n");
1076 } else {
1077 OUT("\t// Defined explicitly */\n");
1078 }
1079 }
vlmfa67ddc2004-06-03 03:38:44 +00001080 OUT("}\n");
1081 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001082
1083 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001084 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001085 OUT("void\n");
vlm766cf1e2005-03-04 08:48:53 +00001086 OUT("%s", p);
1087 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1088 OUT("_free(asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001089 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001090 OUT("\tvoid *struct_ptr, int contents_only) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001091 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1092 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001093 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001094 );
1095 OUT("}\n");
1096 OUT("\n");
1097
1098 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001099 if(HIDE_INNER_DEFS) OUT("static ");
vlmfa67ddc2004-06-03 03:38:44 +00001100 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001101 OUT("%s", p);
1102 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1103 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001104 INDENTED(
1105 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001106 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1107 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001108 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1109 );
1110 OUT("}\n");
1111 OUT("\n");
1112
1113 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001114 if(HIDE_INNER_DEFS) OUT("static ");
vlm9de248e2004-10-20 15:50:55 +00001115 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001116 OUT("%s", p);
1117 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1118 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001119 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001120 OUT("\tvoid **structure, void *bufptr, size_t size, int tag_mode) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001121 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1122 p, expr->_type_unique_index);
vlma5dcb912004-09-29 13:16:40 +00001123 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
vlm39ba4c42004-09-22 16:06:28 +00001124 );
1125 OUT("}\n");
1126 OUT("\n");
1127
1128 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001129 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001130 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001131 OUT("%s", p);
1132 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1133 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
vlm39ba4c42004-09-22 16:06:28 +00001134 INDENTED(
1135 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1136 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001137 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1138 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001139 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1140 );
1141 OUT("}\n");
1142 OUT("\n");
1143
1144 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001145 if(HIDE_INNER_DEFS) OUT("static ");
vlme03646b2004-10-23 13:34:00 +00001146 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001147 OUT("%s", p);
1148 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1149 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlme03646b2004-10-23 13:34:00 +00001150 INDENTED(
1151 OUT("\tvoid **structure, const char *opt_mname, void *bufptr, size_t size) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001152 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1153 p, expr->_type_unique_index);
vlme03646b2004-10-23 13:34:00 +00001154 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1155 );
1156 OUT("}\n");
1157 OUT("\n");
1158
1159 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001160 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001161 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001162 OUT("%s", p);
1163 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1164 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
vlm39ba4c42004-09-22 16:06:28 +00001165 INDENTED(
1166 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1167 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001168 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1169 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001170 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001171 );
1172 OUT("}\n");
1173 OUT("\n");
1174
1175 REDIR(OT_FUNC_DECLS);
1176
vlm1f7df782005-03-04 23:48:19 +00001177 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001178 if(HIDE_INNER_DEFS) {
vlm766cf1e2005-03-04 08:48:53 +00001179 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1180 "\t// (Use -fall-defs-global to expose) */\n",
1181 p, expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +00001182 } else {
1183 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1184 OUT("asn_struct_free_f %s_free;\n", p);
1185 OUT("asn_struct_print_f %s_print;\n", p);
1186 OUT("asn_constr_check_f %s_constraint;\n", p);
1187 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1188 OUT("der_type_encoder_f %s_encode_der;\n", p);
1189 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1190 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1191 }
vlmfa67ddc2004-06-03 03:38:44 +00001192
vlm33a4ff12004-08-11 05:21:32 +00001193 REDIR(OT_TYPE_DECLS);
1194
vlmfa67ddc2004-06-03 03:38:44 +00001195 return 0;
1196}
1197
1198int
1199asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1200
1201 OUT("/*\n");
1202 OUT(" * This type is extensible,\n");
1203 OUT(" * possible extensions are below.\n");
1204 OUT(" */\n");
1205
1206 return 0;
1207}
1208
vlm79b08d52004-07-01 00:52:50 +00001209static int check_if_extensible(asn1p_expr_t *expr) {
1210 asn1p_expr_t *v;
1211 TQ_FOR(v, &(expr->members), next) {
1212 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1213 }
1214 return 0;
1215}
1216
vlmfa67ddc2004-06-03 03:38:44 +00001217static int
vlma8a86cc2004-09-10 06:07:18 +00001218_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
vlmfa67ddc2004-06-03 03:38:44 +00001219
1220 OUT("(");
vlma8a86cc2004-09-10 06:07:18 +00001221 switch(tag->tag_class) {
vlmfa67ddc2004-06-03 03:38:44 +00001222 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1223 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1224 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1225 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1226 case TC_NOCLASS:
1227 break;
1228 }
vlm47ae1582004-09-24 21:01:43 +00001229 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +00001230
1231 return 0;
1232}
1233
vlm4e03ce22004-06-06 07:20:17 +00001234
1235static int
1236_tag2el_cmp(const void *ap, const void *bp) {
1237 const tag2el_t *a = ap;
1238 const tag2el_t *b = bp;
1239 const struct asn1p_type_tag_s *ta = &a->el_tag;
1240 const struct asn1p_type_tag_s *tb = &b->el_tag;
1241
1242 if(ta->tag_class == tb->tag_class) {
1243 if(ta->tag_value == tb->tag_value) {
1244 /*
1245 * Sort by their respective positions.
1246 */
1247 if(a->el_no < b->el_no)
1248 return -1;
1249 else if(a->el_no > b->el_no)
1250 return 1;
1251 return 0;
1252 } else if(ta->tag_value < tb->tag_value)
1253 return -1;
1254 else
1255 return 1;
1256 } else if(ta->tag_class < tb->tag_class) {
1257 return -1;
1258 } else {
1259 return 1;
1260 }
1261}
1262
vlmfa67ddc2004-06-03 03:38:44 +00001263/*
1264 * For constructed types, number of external tags may be greater than
1265 * number of elements in the type because of CHOICE type.
1266 * T ::= SET { -- Three possible tags:
1267 * a INTEGER, -- One tag is here...
1268 * b Choice1 -- ... and two more tags are there.
1269 * }
1270 * Choice1 ::= CHOICE {
1271 * s1 IA5String,
1272 * s2 ObjectDescriptor
1273 * }
1274 */
1275static int
vlm940bc6b2004-10-03 09:13:30 +00001276_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001277 asn1p_expr_t *expr = arg->expr;
1278 arg_t tmparg = *arg;
1279 asn1p_expr_t *v;
1280 int element = 0;
vlm940bc6b2004-10-03 09:13:30 +00001281 int original_count = *count;
1282 int sort_until = -1;
vlmfa67ddc2004-06-03 03:38:44 +00001283
1284 TQ_FOR(v, &(expr->members), next) {
vlm940bc6b2004-10-03 09:13:30 +00001285 if(v->expr_type == A1TC_EXTENSIBLE) {
1286 /*
vlm80a48592005-02-25 12:10:27 +00001287 * CXER mandates sorting
vlm940bc6b2004-10-03 09:13:30 +00001288 * only for the root part.
1289 */
1290 if(flags == FTE_CANONICAL_XER
1291 && sort_until == -1)
1292 sort_until = *count;
vlmfa67ddc2004-06-03 03:38:44 +00001293 continue;
vlm940bc6b2004-10-03 09:13:30 +00001294 }
vlmfa67ddc2004-06-03 03:38:44 +00001295
1296 tmparg.expr = v;
1297
1298 if(_add_tag2el_member(&tmparg, tag2el, count,
vlm940bc6b2004-10-03 09:13:30 +00001299 (el_no==-1)?element:el_no, flags)) {
vlmfa67ddc2004-06-03 03:38:44 +00001300 return -1;
1301 }
1302
1303 element++;
1304 }
1305
vlm940bc6b2004-10-03 09:13:30 +00001306
1307 if(flags == FTE_CANONICAL_XER) {
1308 if(sort_until == -1) sort_until = *count;
1309 qsort((*tag2el) + original_count,
1310 sort_until - original_count,
1311 sizeof(**tag2el), _tag2el_cmp);
1312 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1313 && (sort_until - original_count) >= 1) {
1314 /* Only take in account the root component */
1315 *count = original_count + 1;
1316 }
1317 } else {
1318 /*
1319 * Sort the map according to canonical order of their
1320 * tags and element numbers.
1321 */
1322 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1323 }
vlm4e03ce22004-06-06 07:20:17 +00001324
vlmc8aeab42004-06-14 13:09:45 +00001325 /*
1326 * Initialize .toff_{first|last} members.
1327 */
1328 if(*count) {
1329 struct asn1p_type_tag_s *cur_tag = 0;
1330 tag2el_t *cur = *tag2el;
1331 tag2el_t *end = cur + *count;
1332 int occur, i;
1333 for(occur = 0; cur < end; cur++) {
1334 if(cur_tag == 0
1335 || cur_tag->tag_value != cur->el_tag.tag_value
1336 || cur_tag->tag_class != cur->el_tag.tag_class) {
1337 cur_tag = &cur->el_tag;
1338 occur = 0;
1339 } else {
1340 occur++;
1341 }
1342 cur->toff_first = -occur;
1343 for(i = 0; i >= -occur; i--)
1344 cur[i].toff_last = -i;
1345 }
1346 }
1347
vlmfa67ddc2004-06-03 03:38:44 +00001348 return 0;
1349}
1350
1351static int
vlm940bc6b2004-10-03 09:13:30 +00001352_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001353 struct asn1p_type_tag_s tag;
1354 int ret;
1355
1356 assert(el_no >= 0);
1357
vlm39ba4c42004-09-22 16:06:28 +00001358 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1359 arg->expr, &tag, 1);
vlmfa67ddc2004-06-03 03:38:44 +00001360 if(ret == 0) {
vlme2070ea2004-09-04 04:42:29 +00001361 tag2el_t *te;
1362 int new_count = (*count) + 1;
vlmfa67ddc2004-06-03 03:38:44 +00001363 void *p;
vlme2070ea2004-09-04 04:42:29 +00001364
vlm2de0e792004-09-05 10:42:33 +00001365 if(tag.tag_value == -1) {
1366 /*
1367 * This is an untagged ANY type,
1368 * proceed without adding a tag
1369 */
1370 return 0;
1371 }
1372
vlme2070ea2004-09-04 04:42:29 +00001373 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
vlmfa67ddc2004-06-03 03:38:44 +00001374 if(p) *tag2el = p;
1375 else return -1;
1376
1377 DEBUG("Found tag for %s: %ld",
1378 arg->expr->Identifier,
1379 (long)tag.tag_value);
1380
vlme2070ea2004-09-04 04:42:29 +00001381 te = &((*tag2el)[*count]);
1382 te->el_tag = tag;
1383 te->el_no = el_no;
1384 te->from_expr = arg->expr;
1385 *count = new_count;
vlmfa67ddc2004-06-03 03:38:44 +00001386 return 0;
1387 }
1388
1389 DEBUG("Searching tag in complex expression %s:%x at line %d",
1390 arg->expr->Identifier,
1391 arg->expr->expr_type,
1392 arg->expr->_lineno);
1393
1394 /*
1395 * Iterate over members of CHOICE type.
1396 */
1397 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
vlm940bc6b2004-10-03 09:13:30 +00001398 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001399 }
1400
1401 if(arg->expr->expr_type == A1TC_REFERENCE) {
1402 arg_t tmp = *arg;
1403 asn1p_expr_t *expr;
vlmdae7f9d2004-08-22 03:25:24 +00001404 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
vlmfa67ddc2004-06-03 03:38:44 +00001405 arg->expr->reference);
1406 if(expr) {
vlmdae7f9d2004-08-22 03:25:24 +00001407 tmp.mod = expr->module;
vlmfa67ddc2004-06-03 03:38:44 +00001408 tmp.expr = expr;
vlm940bc6b2004-10-03 09:13:30 +00001409 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001410 } else {
1411 FATAL("Cannot dereference %s at line %d",
1412 arg->expr->Identifier,
1413 arg->expr->_lineno);
1414 return -1;
1415 }
1416 }
1417
1418 DEBUG("No tag for %s at line %d",
1419 arg->expr->Identifier,
1420 arg->expr->_lineno);
1421
1422 return -1;
1423}
1424
1425static int
vlm940bc6b2004-10-03 09:13:30 +00001426emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
vlm4e03ce22004-06-06 07:20:17 +00001427 asn1p_expr_t *expr = arg->expr;
1428
vlm766cf1e2005-03-04 08:48:53 +00001429 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
vlm1f7df782005-03-04 23:48:19 +00001430 MKID(expr->Identifier), expr->_type_unique_index,
vlm766cf1e2005-03-04 08:48:53 +00001431 opt_modifier?opt_modifier:"");
vlm4e03ce22004-06-06 07:20:17 +00001432 if(tag2el_count) {
1433 int i;
1434 for(i = 0; i < tag2el_count; i++) {
1435 OUT(" { ");
vlma8a86cc2004-09-10 06:07:18 +00001436 _print_tag(arg, &tag2el[i].el_tag);
vlm4e03ce22004-06-06 07:20:17 +00001437 OUT(", ");
vlmc8aeab42004-06-14 13:09:45 +00001438 OUT("%d, ", tag2el[i].el_no);
1439 OUT("%d, ", tag2el[i].toff_first);
1440 OUT("%d ", tag2el[i].toff_last);
vlm80a48592005-02-25 12:10:27 +00001441 OUT("}%s /* %s at %d */\n",
1442 (i + 1 < tag2el_count) ? "," : "",
vlm4e03ce22004-06-06 07:20:17 +00001443 tag2el[i].from_expr->Identifier,
1444 tag2el[i].from_expr->_lineno
1445 );
1446 }
1447 }
1448 OUT("};\n");
1449
1450 return 0;;
1451}
1452
vlm72425de2004-09-13 08:31:01 +00001453static enum tvm_compat
1454emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1455 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1456 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
vlm6e73a042004-08-11 07:17:22 +00001457 int tags_count = 0;
vlm72425de2004-09-13 08:31:01 +00001458 int all_tags_count = 0;
1459 enum tvm_compat tv_mode = _TVM_SAME;
vlm1308d2b2004-09-10 15:49:15 +00001460 int i;
vlm4a3f5822004-06-28 21:13:46 +00001461
vlmc6de2c42004-09-14 14:10:10 +00001462 /* Cleanup before proceeding. */
1463 *tags_count_r = 0;
1464 *all_tags_count_r = 0;
1465
vlm1308d2b2004-09-10 15:49:15 +00001466 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001467 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
vlmc6de2c42004-09-14 14:10:10 +00001468 if(tags_count < 0)
1469 return -1;
vlm6e73a042004-08-11 07:17:22 +00001470
vlm72425de2004-09-13 08:31:01 +00001471 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001472 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
vlm72425de2004-09-13 08:31:01 +00001473 &all_tags, AFT_FULL_COLLECT);
1474 if(all_tags_count < 0) {
1475 if(tags) free(tags);
1476 return -1;
vlmfb41dbb2004-09-08 00:28:57 +00001477 }
vlm1308d2b2004-09-10 15:49:15 +00001478
vlm72425de2004-09-13 08:31:01 +00001479 assert(tags_count <= all_tags_count);
1480 assert((tags_count?0:1) == (all_tags_count?0:1));
vlm4a3f5822004-06-28 21:13:46 +00001481
vlm72425de2004-09-13 08:31:01 +00001482 if(tags_count <= all_tags_count) {
1483 for(i = 0; i < tags_count; i++) {
1484 if(tags[i].tag_value != all_tags[i].tag_value
1485 || tags[i].tag_class != all_tags[i].tag_class) {
1486 tv_mode = _TVM_DIFFERENT;
1487 break;
1488 }
1489 }
1490 if(i == tags_count && tags_count < all_tags_count)
1491 tv_mode = _TVM_SUBSET;
1492 } else {
1493 tv_mode = _TVM_DIFFERENT;
1494 }
1495
1496#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
vlm766cf1e2005-03-04 08:48:53 +00001497 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
vlm1f7df782005-03-04 23:48:19 +00001498 MKID(expr->Identifier), \
vlm766cf1e2005-03-04 08:48:53 +00001499 expr->_type_unique_index, name); \
vlm72425de2004-09-13 08:31:01 +00001500 INDENT(+1); \
1501 /* Print the array of collected tags */ \
1502 for(i = 0; i < tags_count; i++) { \
1503 if(i) OUT(",\n"); \
1504 _print_tag(arg, &tags[i]); \
1505 } \
1506 OUT("\n"); \
1507 INDENT(-1); \
1508 OUT("};\n"); \
1509 } while(0)
1510
1511 if(tags_count) {
1512 if(tv_mode == _TVM_SUBSET)
1513 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1514 else
1515 EMIT_TAGS_TABLE("", tags, tags_count);
1516 }
1517
1518 if(all_tags_count) {
1519 if(tv_mode == _TVM_DIFFERENT)
1520 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1521 }
1522
1523 if(tags) free(tags);
1524 if(all_tags) free(all_tags);
1525
1526 *tags_count_r = tags_count;
1527 *all_tags_count_r = all_tags_count;
1528
1529 return tv_mode;
vlm4a3f5822004-06-28 21:13:46 +00001530}
vlmb2839012004-08-20 13:37:01 +00001531
1532static int
vlm4e554992004-08-25 02:03:12 +00001533expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001534 asn1p_expr_t *topmost_parent;
1535 asn1p_expr_t *v;
1536 int elements = 0;
1537
vlm39ba4c42004-09-22 16:06:28 +00001538 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001539 if(!topmost_parent) return 0;
1540
vlm80a48592005-02-25 12:10:27 +00001541 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1542 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1543 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
vlmb2839012004-08-20 13:37:01 +00001544 return 0;
1545
1546 TQ_FOR(v, &(topmost_parent->members), next) {
1547 if(v->expr_type != A1TC_EXTENSIBLE)
1548 elements++;
1549 }
1550
1551 return elements;
1552}
1553
1554static int
vlm0b567bf2005-03-04 22:18:20 +00001555emit_include_dependencies(arg_t *arg) {
1556 asn1p_expr_t *expr = arg->expr;
1557 asn1p_expr_t *memb;
1558
1559 TQ_FOR(memb, &(expr->members), next) {
1560
1561 if((memb->meta_type == AMT_TYPEREF
1562 && (memb->marker.flags & EM_INDIRECT))
1563 || expr->expr_type == ASN_CONSTR_SET_OF
1564 || expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1565 ) {
1566 asn1p_expr_t *terminal;
1567 terminal = asn1f_find_terminal_type_ex(arg->asn, memb);
1568 if(terminal && !terminal->parent_expr
1569 && (terminal->expr_type & ASN_CONSTR_MASK)) {
1570 int saved_target = arg->target->target;
1571 REDIR(OT_FWD_DECLS);
vlm5feb7522005-03-04 23:50:56 +00001572 OUT("%s;\n",
vlm0b567bf2005-03-04 22:18:20 +00001573 asn1c_type_name(arg, memb, TNF_RSAFE));
1574 REDIR(saved_target);
1575 memb->marker.flags |= EM_UNRECURSE;
1576 }
1577 }
1578
1579 if((!(memb->expr_type & ASN_CONSTR_MASK)
1580 && memb->expr_type > ASN_CONSTR_MASK)
1581 || memb->meta_type == AMT_TYPEREF) {
1582 if(memb->marker.flags & EM_UNRECURSE) {
1583 GEN_POSTINCLUDE(asn1c_type_name(arg,
1584 memb, TNF_INCLUDE));
1585 } else {
1586 GEN_INCLUDE(asn1c_type_name(arg,
1587 memb, TNF_INCLUDE));
1588 }
1589 }
1590 }
1591
1592 return 0;
1593}
1594
1595static int
vlm4e554992004-08-25 02:03:12 +00001596emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001597 int save_target;
1598 arg_t tmp_arg;
vlm060fe2a2004-09-10 09:37:12 +00001599 struct asn1p_type_tag_s outmost_tag_s;
1600 struct asn1p_type_tag_s *outmost_tag;
vlm80a48592005-02-25 12:10:27 +00001601 int complex_contents;
vlmb2839012004-08-20 13:37:01 +00001602 char *p;
1603
vlm060fe2a2004-09-10 09:37:12 +00001604 if(asn1f_fetch_outmost_tag(arg->asn,
1605 expr->module, expr, &outmost_tag_s, 1)) {
1606 outmost_tag = 0;
1607 } else {
1608 outmost_tag = &outmost_tag_s;
1609 }
1610
vlmb2839012004-08-20 13:37:01 +00001611 OUT("{ ");
vlm060fe2a2004-09-10 09:37:12 +00001612
1613 if(outmost_tag && outmost_tag->tag_value == -1)
1614 OUT("ATF_OPEN_TYPE | ");
vlm0b567bf2005-03-04 22:18:20 +00001615 OUT("%s, ",
1616 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
vlma02b74d2004-09-15 11:54:38 +00001617 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
vlmb2839012004-08-20 13:37:01 +00001618 asn1p_expr_t *tv;
1619 int opts = 0;
vlma02b74d2004-09-15 11:54:38 +00001620 for(tv = expr; tv && tv->marker.flags;
vlmb2839012004-08-20 13:37:01 +00001621 tv = TQ_NEXT(tv, next), opts++) {
1622 if(tv->expr_type == A1TC_EXTENSIBLE)
1623 opts--;
1624 }
1625 OUT("%d, ", opts);
1626 } else {
1627 OUT("0, ");
1628 }
vlm39ba4c42004-09-22 16:06:28 +00001629 if(expr->_anonymous_type) {
1630 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1631 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1632 OUT("0,\n");
1633 } else {
vlm766cf1e2005-03-04 08:48:53 +00001634 OUT("offsetof(struct ");
1635 out_name_chain(arg, 1);
1636 OUT(", ");
vlmb2839012004-08-20 13:37:01 +00001637 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1638 && (!UNNAMED_UNIONS)) OUT("choice.");
vlm1f7df782005-03-04 23:48:19 +00001639 OUT("%s),\n", MKID_safe(expr->Identifier));
vlmb2839012004-08-20 13:37:01 +00001640 }
1641 INDENT(+1);
1642 if(C99_MODE) OUT(".tag = ");
vlm060fe2a2004-09-10 09:37:12 +00001643 if(outmost_tag) {
1644 if(outmost_tag->tag_value == -1)
1645 OUT("-1 /* Ambiguous tag (ANY?) */");
1646 else
1647 _print_tag(arg, outmost_tag);
vlma8a86cc2004-09-10 06:07:18 +00001648 } else {
vlm060fe2a2004-09-10 09:37:12 +00001649 OUT("-1 /* Ambiguous tag (CHOICE?) */");
vlma8a86cc2004-09-10 06:07:18 +00001650 }
vlm060fe2a2004-09-10 09:37:12 +00001651
vlmb2839012004-08-20 13:37:01 +00001652 OUT(",\n");
1653 if(C99_MODE) OUT(".tag_mode = ");
1654 if(expr->tag.tag_class) {
1655 if(expr->tag.tag_mode == TM_IMPLICIT)
1656 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1657 else
1658 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1659 } else {
1660 OUT("0,\n");
1661 }
vlm80a48592005-02-25 12:10:27 +00001662
1663 complex_contents =
1664 (expr->expr_type & ASN_CONSTR_MASK)
1665 || expr->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +00001666 || (0 /* -- prohibited by X.693:8.3.4 */
1667 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00001668 && expr_elements_count(arg, expr));
vlmb2839012004-08-20 13:37:01 +00001669 if(C99_MODE) OUT(".type = ");
vlm766cf1e2005-03-04 08:48:53 +00001670 OUT("(void *)&asn_DEF_");
1671 if(complex_contents) {
vlm1f7df782005-03-04 23:48:19 +00001672 OUT("%s", MKID(expr->Identifier));
vlm766cf1e2005-03-04 08:48:53 +00001673 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1674 OUT("_%d", expr->_type_unique_index);
vlmdae7f9d2004-08-22 03:25:24 +00001675 } else {
vlm766cf1e2005-03-04 08:48:53 +00001676 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
vlmdae7f9d2004-08-22 03:25:24 +00001677 }
vlm766cf1e2005-03-04 08:48:53 +00001678 OUT(",\n");
vlmb2839012004-08-20 13:37:01 +00001679 if(C99_MODE) OUT(".memb_constraints = ");
1680 if(expr->constraints) {
vlm86f5ed22004-09-26 13:11:31 +00001681 if(arg->flags & A1C_NO_CONSTRAINTS) {
1682 OUT("0,\t/* No check because of -fno-constraints */\n");
1683 } else {
vlm1f7df782005-03-04 23:48:19 +00001684 char *id = MKID(expr->Identifier);
vlm86f5ed22004-09-26 13:11:31 +00001685 if(expr->_anonymous_type
vlmbf3c5112005-02-14 20:41:29 +00001686 && !strcmp(expr->Identifier, "Member"))
vlm86f5ed22004-09-26 13:11:31 +00001687 id = asn1c_type_name(arg, expr, TNF_SAFE);
1688 OUT("memb_%s_%d_constraint,\n", id,
vlm766cf1e2005-03-04 08:48:53 +00001689 arg->expr->_type_unique_index);
vlm86f5ed22004-09-26 13:11:31 +00001690 }
vlmb2839012004-08-20 13:37:01 +00001691 } else {
vlm95a476e2005-01-17 12:16:58 +00001692 OUT("0,\t/* Defer constraints checking to the member type */\n");
vlmb2839012004-08-20 13:37:01 +00001693 }
1694 if(C99_MODE) OUT(".name = ");
vlmbf3c5112005-02-14 20:41:29 +00001695 if(1) {
1696 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1697 OUT("\"\"\n");
1698 else
1699 OUT("\"%s\"\n", expr->Identifier);
1700 } else {
1701 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1702 }
vlmb2839012004-08-20 13:37:01 +00001703 OUT("},\n");
1704 INDENT(-1);
1705
vlm86f5ed22004-09-26 13:11:31 +00001706 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlmb2839012004-08-20 13:37:01 +00001707 return 0;
1708
1709 save_target = arg->target->target;
1710 REDIR(OT_CODE);
1711
vlmbf3c5112005-02-14 20:41:29 +00001712 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
vlmb2839012004-08-20 13:37:01 +00001713 p = asn1c_type_name(arg, expr, TNF_SAFE);
vlm39ba4c42004-09-22 16:06:28 +00001714 else
vlm1f7df782005-03-04 23:48:19 +00001715 p = MKID(expr->Identifier);
vlmb2839012004-08-20 13:37:01 +00001716 OUT("static int\n");
vlm766cf1e2005-03-04 08:48:53 +00001717 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 +00001718 INDENT(+1);
1719 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1720 tmp_arg = *arg;
1721 tmp_arg.expr = expr;
1722 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
vlm86f5ed22004-09-26 13:11:31 +00001723 OUT("return td->check_constraints"
1724 "(td, sptr, app_errlog, app_key);\n");
vlmb2839012004-08-20 13:37:01 +00001725 }
1726 INDENT(-1);
1727 OUT("}\n");
1728 OUT("\n");
1729
1730 REDIR(save_target);
1731
1732 return 0;
1733}
vlm4e554992004-08-25 02:03:12 +00001734
vlm9de248e2004-10-20 15:50:55 +00001735/*
1736 * Generate "asn_DEF_XXX" type definition.
1737 */
vlm4e554992004-08-25 02:03:12 +00001738static int
vlm86f5ed22004-09-26 13:11:31 +00001739emit_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 +00001740 int using_type_name = 0;
vlm4e554992004-08-25 02:03:12 +00001741 char *p;
1742
vlm12c8f692004-09-06 08:07:29 +00001743 if(HIDE_INNER_DEFS)
1744 OUT("static /* Use -fall-defs-global to expose */\n");
vlm1f7df782005-03-04 23:48:19 +00001745 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID(expr->Identifier));
vlm766cf1e2005-03-04 08:48:53 +00001746 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1747 OUT(" = {\n");
vlmaf841972005-01-28 12:18:50 +00001748 p = MKID(expr->Identifier);
vlm72425de2004-09-13 08:31:01 +00001749 INDENT(+1);
vlm4e554992004-08-25 02:03:12 +00001750 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm9de248e2004-10-20 15:50:55 +00001751 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001752
1753 if(expr->expr_type & ASN_CONSTR_MASK) {
vlm766cf1e2005-03-04 08:48:53 +00001754 using_type_name = 1;
vlm4e554992004-08-25 02:03:12 +00001755 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1756 }
1757
vlm766cf1e2005-03-04 08:48:53 +00001758#define FUNCREF(foo) do { \
1759 OUT("%s", p); \
1760 if(HIDE_INNER_DEFS && !using_type_name) \
1761 OUT("_%d", expr->_type_unique_index); \
1762 OUT("_" #foo ",\n"); \
1763} while(0)
1764
1765 FUNCREF(free);
1766 FUNCREF(print);
1767 FUNCREF(constraint);
1768 FUNCREF(decode_ber);
1769 FUNCREF(encode_der);
1770 FUNCREF(decode_xer);
1771 FUNCREF(encode_xer);
vlm4e554992004-08-25 02:03:12 +00001772
vlm4e554992004-08-25 02:03:12 +00001773 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1774 OUT("CHOICE_outmost_tag,\n");
1775 } else {
1776 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1777 }
1778
vlm1f7df782005-03-04 23:48:19 +00001779 p = MKID(expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001780 if(tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001781 OUT("asn_DEF_%s_%d_tags,\n",
1782 p, expr->_type_unique_index);
1783 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1784 p, expr->_type_unique_index);
1785 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1786 p, expr->_type_unique_index);
vlm72425de2004-09-13 08:31:01 +00001787 if(tv_mode == _TVM_SUBSET
1788 && tags_count != all_tags_count)
1789 OUT(" - %d", all_tags_count - tags_count);
1790 OUT(", /* %d */\n", tags_count);
vlm4e554992004-08-25 02:03:12 +00001791 } else {
vlm72425de2004-09-13 08:31:01 +00001792 OUT("0,\t/* No effective tags (pointer) */\n");
1793 OUT("0,\t/* No effective tags (count) */\n");
1794 }
1795
1796 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
vlm766cf1e2005-03-04 08:48:53 +00001797 OUT("asn_DEF_%s_%d_all_tags,\n",
1798 p, expr->_type_unique_index);
1799 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1800 p, expr->_type_unique_index);
1801 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1802 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001803 } else if(all_tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001804 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1805 p, expr->_type_unique_index);
1806 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1807 p, expr->_type_unique_index);
1808 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1809 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001810 } else {
1811 OUT("0,\t/* No tags (pointer) */\n");
1812 OUT("0,\t/* No tags (count) */\n");
vlm4e554992004-08-25 02:03:12 +00001813 }
1814
vlm4e554992004-08-25 02:03:12 +00001815 if(elements_count) {
vlm766cf1e2005-03-04 08:48:53 +00001816 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001817 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1818 || expr->expr_type == ASN_CONSTR_SET_OF) {
1819 OUT("%d,\t/* Single element */\n",
1820 elements_count);
1821 assert(elements_count == 1);
1822 } else {
1823 OUT("%d,\t/* Elements count */\n",
1824 elements_count);
1825 }
1826 } else {
vlm4e554992004-08-25 02:03:12 +00001827 if(expr_elements_count(arg, expr))
1828 OUT("0, 0,\t/* Defined elsewhere */\n");
1829 else
1830 OUT("0, 0,\t/* No members */\n");
1831 }
1832
1833 switch(spec) {
1834 case ETD_NO_SPECIFICS:
1835 OUT("0\t/* No specifics */\n");
1836 break;
1837 case ETD_HAS_SPECIFICS:
vlm766cf1e2005-03-04 08:48:53 +00001838 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1839 p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001840 }
vlm72425de2004-09-13 08:31:01 +00001841 INDENT(-1);
vlm4e554992004-08-25 02:03:12 +00001842 OUT("};\n");
1843 OUT("\n");
vlme2bb4432004-08-26 06:20:34 +00001844
1845 return 0;
vlm4e554992004-08-25 02:03:12 +00001846}
vlmddd5a7d2004-09-10 09:18:20 +00001847
1848/*
1849 * Check if it is better to make this type indirectly accessed via
1850 * a pointer.
1851 * This may be the case for the following recursive definition:
1852 * Type ::= CHOICE { member Type };
1853 */
1854static int
1855expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) {
1856 asn1p_expr_t *top_parent;
1857 asn1p_expr_t *terminal;
1858
1859 if(expr->expr_type != A1TC_REFERENCE)
1860 return 0;
1861
1862 /* Rewind to the topmost parent expression */
1863 if((top_parent = expr->parent_expr)) {
1864 while(top_parent->parent_expr)
1865 top_parent = top_parent->parent_expr;
1866 } else {
1867 return 0;
1868 }
1869
vlm39ba4c42004-09-22 16:06:28 +00001870 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmddd5a7d2004-09-10 09:18:20 +00001871
1872 return (terminal == top_parent);
1873}
vlm39ba4c42004-09-22 16:06:28 +00001874
1875static int
1876expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1877 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1878 if(!expr) return 0;
1879
1880 /* X.680, 25.5, Table 5 */
1881 switch(expr->expr_type) {
1882 case ASN_CONSTR_CHOICE:
1883 case ASN_BASIC_BOOLEAN:
1884 case ASN_BASIC_ENUMERATED:
1885 case ASN_BASIC_NULL:
1886 return 1;
1887 default:
1888 return 0;
1889 }
1890}
vlmaf841972005-01-28 12:18:50 +00001891
1892static int
vlm766cf1e2005-03-04 08:48:53 +00001893out_name_chain(arg_t *arg, int check_reserved_keywords) {
vlmaf841972005-01-28 12:18:50 +00001894 asn1p_expr_t *expr = arg->expr;
1895 char *id;
1896
1897 assert(expr->Identifier);
1898
vlm766cf1e2005-03-04 08:48:53 +00001899 if(arg->flags & A1C_COMPOUND_NAMES
1900 && ((expr->expr_type & ASN_CONSTR_MASK)
1901 || expr->expr_type == ASN_BASIC_ENUMERATED
1902 || (expr->expr_type == ASN_BASIC_INTEGER
1903 && expr_elements_count(arg, expr))
1904 )
vlmaf841972005-01-28 12:18:50 +00001905 && expr->parent_expr
1906 && expr->parent_expr->Identifier) {
1907 arg_t tmparg = *arg;
1908
1909 tmparg.expr = expr->parent_expr;
vlm766cf1e2005-03-04 08:48:53 +00001910 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
1911
1912 out_name_chain(&tmparg, 0);
vlmaf841972005-01-28 12:18:50 +00001913
1914 OUT("_"); /* a separator between id components */
vlm766cf1e2005-03-04 08:48:53 +00001915
vlmaf841972005-01-28 12:18:50 +00001916 /* Fall through */
1917 }
1918
1919 if(check_reserved_keywords)
vlm1f7df782005-03-04 23:48:19 +00001920 id = MKID_safe(expr->Identifier);
vlmaf841972005-01-28 12:18:50 +00001921 else
vlm1f7df782005-03-04 23:48:19 +00001922 id = MKID(expr->Identifier);
vlmaf841972005-01-28 12:18:50 +00001923 OUT("%s", id);
1924
1925 return 0;
1926}