blob: e35f534fb15c070b933a1f32d9853a293b731223 [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"
vlm98218602005-03-04 08:52:50 +000010#include "asn1c_compat.h"
vlm1d036692004-08-19 13:29:46 +000011#include <asn1fix_export.h> /* Stuff exported by libasn1fix */
vlmfa67ddc2004-06-03 03:38:44 +000012
vlm4e03ce22004-06-06 07:20:17 +000013typedef struct tag2el_s {
14 struct asn1p_type_tag_s el_tag;
15 int el_no;
vlmc8aeab42004-06-14 13:09:45 +000016 int toff_first;
17 int toff_last;
vlm4e03ce22004-06-06 07:20:17 +000018 asn1p_expr_t *from_expr;
19} tag2el_t;
20
vlm940bc6b2004-10-03 09:13:30 +000021typedef enum fte {
22 FTE_ALLTAGS,
23 FTE_CANONICAL_XER,
24} fte_e;
25static int _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags);
26static int _add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags);
vlm4e03ce22004-06-06 07:20:17 +000027
vlmfa67ddc2004-06-03 03:38:44 +000028static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg);
29static int asn1c_lang_C_type_SET_def(arg_t *arg);
30static int asn1c_lang_C_type_CHOICE_def(arg_t *arg);
31static int asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of);
vlma8a86cc2004-09-10 06:07:18 +000032static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p);
vlm79b08d52004-07-01 00:52:50 +000033static int check_if_extensible(asn1p_expr_t *expr);
vlmddd5a7d2004-09-10 09:18:20 +000034static int expr_better_indirect(arg_t *arg, asn1p_expr_t *expr);
vlm39ba4c42004-09-22 16:06:28 +000035static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr);
vlm4e554992004-08-25 02:03:12 +000036static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr);
37static int emit_member_table(arg_t *arg, asn1p_expr_t *expr);
vlm940bc6b2004-10-03 09:13:30 +000038static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier);
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
vlm33a4ff12004-08-11 05:21:32 +000063#define DEPENDENCIES do { \
vlmdae7f9d2004-08-22 03:25:24 +000064 asn1p_expr_t *__m; \
65 TQ_FOR(__m, &(expr->members), next) { \
66 if((!(__m->expr_type & ASN_CONSTR_MASK) \
67 && __m->expr_type > ASN_CONSTR_MASK) \
68 || __m->meta_type == AMT_TYPEREF) { \
69 GEN_INCLUDE(asn1c_type_name(arg, \
vlmaf841972005-01-28 12:18:50 +000070 __m, TNF_INCLUDE)); \
vlm33a4ff12004-08-11 05:21:32 +000071 } \
72 } \
73 if(expr->expr_type == ASN_CONSTR_SET_OF) \
74 GEN_INCLUDE("asn_SET_OF"); \
75 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \
76 GEN_INCLUDE("asn_SEQUENCE_OF"); \
77} while(0)
vlmfa67ddc2004-06-03 03:38:44 +000078
vlmaf841972005-01-28 12:18:50 +000079/* MKID() without checking for reserved keywords */
vlmad970212005-03-03 21:58:28 +000080#define MKID_nc(id) asn1c_make_identifier(0, (id), 0)
vlmaf841972005-01-28 12:18:50 +000081#define MKID(id) asn1c_make_identifier(AMI_CHECK_RESERVED, (id), 0)
vlmfa67ddc2004-06-03 03:38:44 +000082
83int
vlmf9d178d2004-09-14 12:47:45 +000084asn1c_lang_C_type_REAL(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +000085 REDIR(OT_DEPS);
vlmfa67ddc2004-06-03 03:38:44 +000086 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
87}
88
vlm80a48592005-02-25 12:10:27 +000089struct value2enum {
90 asn1c_integer_t value;
91 const char *name;
92 int idx;
93};
94static int compar_enumMap_byName(const void *ap, const void *bp) {
95 const struct value2enum *a = (const struct value2enum *)ap;
96 const struct value2enum *b = (const struct value2enum *)bp;
97 return strcmp(a->name, b->name);
98}
99static int compar_enumMap_byValue(const void *ap, const void *bp) {
100 const struct value2enum *a = (const struct value2enum *)ap;
101 const struct value2enum *b = (const struct value2enum *)bp;
102 if(a->value < b->value)
103 return -1;
104 else if(a->value == b->value)
105 return 0;
106 return 1;
107}
108
vlmfa67ddc2004-06-03 03:38:44 +0000109int
vlmf9d178d2004-09-14 12:47:45 +0000110asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000111 asn1p_expr_t *expr = arg->expr;
112 asn1p_expr_t *v;
vlm80a48592005-02-25 12:10:27 +0000113 int el_count = expr_elements_count(arg, expr);
114 struct value2enum *v2e;
115 int map_is_extensible = (expr->expr_type == ASN_BASIC_INTEGER);
vlmfa67ddc2004-06-03 03:38:44 +0000116
vlm80a48592005-02-25 12:10:27 +0000117 v2e = alloca((el_count + 1) * sizeof(*v2e));
vlmfa67ddc2004-06-03 03:38:44 +0000118
vlm80a48592005-02-25 12:10:27 +0000119 /*
vlmc89422d2005-03-03 21:28:12 +0000120 * For all ENUMERATED types [and for those INTEGER types which
121 * have identifiers -- prohibited by X.693:8.3.4],
122 * print out an enumeration table and a mapping
vlm80a48592005-02-25 12:10:27 +0000123 * between identifiers and associated values.
124 */
vlmc89422d2005-03-03 21:28:12 +0000125 if(expr->expr_type == ASN_BASIC_ENUMERATED
126 || (0 && el_count /* -- prohibited by X.693:8.3.4 */)) {
vlm80a48592005-02-25 12:10:27 +0000127 int eidx = 0;
128
129 REDIR(OT_DEPS);
vlm766cf1e2005-03-04 08:48:53 +0000130 OUT("typedef enum ");
131 out_name_chain(arg, 1);
132 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000133 TQ_FOR(v, &(expr->members), next) {
134 switch(v->expr_type) {
135 case A1TC_UNIVERVAL:
vlm766cf1e2005-03-04 08:48:53 +0000136 OUT("\t");
137 out_name_chain(arg, 0);
138 OUT("_%s", MKID_nc(v->Identifier));
139 OUT("\t= %" PRIdASN "%s\n",
vlm80a48592005-02-25 12:10:27 +0000140 v->value->value.v_integer,
vlm766cf1e2005-03-04 08:48:53 +0000141 (eidx+1 < el_count) ? "," : "");
vlm80a48592005-02-25 12:10:27 +0000142 v2e[eidx].name = v->Identifier;
143 v2e[eidx].value = v->value->value.v_integer;
144 eidx++;
vlmfa67ddc2004-06-03 03:38:44 +0000145 break;
vlmf9d178d2004-09-14 12:47:45 +0000146 case A1TC_EXTENSIBLE:
147 OUT("\t/*\n");
148 OUT("\t * Enumeration is extensible\n");
149 OUT("\t */\n");
vlm80a48592005-02-25 12:10:27 +0000150 map_is_extensible = 1;
vlmf9d178d2004-09-14 12:47:45 +0000151 break;
vlmfa67ddc2004-06-03 03:38:44 +0000152 default:
153 return -1;
154 }
155 }
vlm766cf1e2005-03-04 08:48:53 +0000156 OUT("} ");
157 out_name_chain(arg, 0);
158 OUT("_e;\n");
vlm80a48592005-02-25 12:10:27 +0000159 assert(eidx == el_count);
160
161 /*
162 * Generate a enumerationName<->value map for XER codec.
163 */
164 REDIR(OT_STAT_DEFS);
165
vlm766cf1e2005-03-04 08:48:53 +0000166 OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_%d_value2enum[] = {\n",
167 MKID_nc(expr->Identifier),
168 expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000169 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
170 for(eidx = 0; eidx < el_count; eidx++) {
171 v2e[eidx].idx = eidx;
172 OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n",
173 v2e[eidx].value,
174 (long)strlen(v2e[eidx].name), v2e[eidx].name,
175 (eidx + 1 < el_count) ? "," : "");
176 }
177 if(map_is_extensible)
178 OUT("\t/* This list is extensible */\n");
179 OUT("};\n");
180
vlm766cf1e2005-03-04 08:48:53 +0000181 OUT("static unsigned int asn_MAP_%s_%d_enum2value[] = {\n",
182 MKID_nc(expr->Identifier),
183 expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000184 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
185 for(eidx = 0; eidx < el_count; eidx++) {
186 OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n",
187 v2e[eidx].idx,
188 (eidx + 1 < el_count) ? "," : "",
189 v2e[eidx].name, v2e[eidx].value);
190 }
191 if(map_is_extensible)
192 OUT("\t/* This list is extensible */\n");
193 OUT("};\n");
194
vlm766cf1e2005-03-04 08:48:53 +0000195 OUT("static asn_INTEGER_specifics_t asn_SPC_%s_%d_specs = {\n",
196 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000197 INDENT(+1);
vlm766cf1e2005-03-04 08:48:53 +0000198 OUT("asn_MAP_%s_%d_value2enum,\t"
vlm80a48592005-02-25 12:10:27 +0000199 "/* \"tag\" => N; sorted by tag */\n",
vlm766cf1e2005-03-04 08:48:53 +0000200 MKID_nc(expr->Identifier),
201 expr->_type_unique_index);
202 OUT("asn_MAP_%s_%d_enum2value,\t"
vlm80a48592005-02-25 12:10:27 +0000203 "/* N => \"tag\"; sorted by N */\n",
vlm766cf1e2005-03-04 08:48:53 +0000204 MKID_nc(expr->Identifier),
205 expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000206 OUT("%d,\t/* Number of elements in the maps */\n",
207 el_count);
208 OUT("%d,\t/* Enumeration is %sextensible */\n",
209 map_is_extensible, map_is_extensible ? "": "not ");
210 if(expr->expr_type == ASN_BASIC_ENUMERATED)
211 OUT("1\t/* Strict enumeration */\n");
212 else
213 OUT("0\n");
214 INDENT(-1);
215 OUT("};\n");
vlmfa67ddc2004-06-03 03:38:44 +0000216 }
217
218 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
219}
220
221int
222asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
223 asn1p_expr_t *expr = arg->expr;
224 asn1p_expr_t *v;
225 int comp_mode = 0; /* {root,ext=1,root,root,...} */
226
227 DEPENDENCIES;
228
229 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000230 OUT("struct ");
231 out_name_chain(arg, 1);
232 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000233 } else {
234 OUT("typedef struct %s {\n",
235 MKID(expr->Identifier));
236 }
237
238 TQ_FOR(v, &(expr->members), next) {
239 if(v->expr_type == A1TC_EXTENSIBLE) {
240 if(comp_mode < 3) comp_mode++;
241 }
vlmddd5a7d2004-09-10 09:18:20 +0000242 if(comp_mode == 1
243 || expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000244 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000245 EMBED(v);
246 }
247
248 PCTX_DEF;
vlma02b74d2004-09-15 11:54:38 +0000249 OUT("} %s%s%s", expr->marker.flags?"*":"",
vlmdae7f9d2004-08-22 03:25:24 +0000250 expr->_anonymous_type ? "" : MKID(expr->Identifier),
251 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000252
253 return asn1c_lang_C_type_SEQUENCE_def(arg);
254}
255
256static int
257asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
258 asn1p_expr_t *expr = arg->expr;
259 asn1p_expr_t *v;
260 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000261 int ext_start = -1;
262 int ext_stop = -1;
vlm4e03ce22004-06-06 07:20:17 +0000263 tag2el_t *tag2el = NULL;
264 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000265 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000266 int all_tags_count;
267 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000268
vlm4e03ce22004-06-06 07:20:17 +0000269 /*
270 * Fetch every inner tag from the tag to elements map.
271 */
vlm940bc6b2004-10-03 09:13:30 +0000272 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlm4e03ce22004-06-06 07:20:17 +0000273 if(tag2el) free(tag2el);
274 return -1;
275 }
276
vlm33a4ff12004-08-11 05:21:32 +0000277 GEN_INCLUDE("constr_SEQUENCE");
278 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000279 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000280
vlm33a4ff12004-08-11 05:21:32 +0000281 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000282
283 /*
284 * Print out the table according to which the parsing is performed.
285 */
vlm39ba4c42004-09-22 16:06:28 +0000286 if(expr_elements_count(arg, expr)) {
287 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000288
vlm766cf1e2005-03-04 08:48:53 +0000289 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
290 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000291
292 elements = 0;
293 INDENTED(TQ_FOR(v, &(expr->members), next) {
294 if(v->expr_type == A1TC_EXTENSIBLE) {
295 if((++comp_mode) == 1)
296 ext_start = elements - 1;
297 else
298 ext_stop = elements - 1;
299 continue;
300 }
301 elements++;
302 emit_member_table(arg, v);
303 });
304 OUT("};\n");
305 } else {
306 elements = 0;
307 }
vlmfa67ddc2004-06-03 03:38:44 +0000308
vlm4a3f5822004-06-28 21:13:46 +0000309 /*
vlma5dcb912004-09-29 13:16:40 +0000310 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000311 */
vlm72425de2004-09-13 08:31:01 +0000312 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000313
vlm4e03ce22004-06-06 07:20:17 +0000314 /*
315 * Tags to elements map.
316 */
vlm940bc6b2004-10-03 09:13:30 +0000317 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlm4e03ce22004-06-06 07:20:17 +0000318
vlm766cf1e2005-03-04 08:48:53 +0000319 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_%d_specs = {\n",
320 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000321 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000322 OUT("sizeof(struct "); out_name_chain(arg, 1); OUT("),\n");
323 OUT("offsetof(struct "); out_name_chain(arg, 1); OUT(", _asn_ctx),\n");
324
325 OUT("asn_MAP_%s_%d_tag2el,\n",
326 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlm4e03ce22004-06-06 07:20:17 +0000327 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlmfa67ddc2004-06-03 03:38:44 +0000328 OUT("%d,\t/* Start extensions */\n",
329 ext_start);
330 OUT("%d\t/* Stop extensions */\n",
331 (ext_stop<ext_start)?elements+1:ext_stop, ext_stop);
332 );
333 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000334
335 /*
vlma5dcb912004-09-29 13:16:40 +0000336 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000337 */
vlm72425de2004-09-13 08:31:01 +0000338 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000339 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000340
vlmfa67ddc2004-06-03 03:38:44 +0000341 REDIR(OT_TYPE_DECLS);
342
343 return 0;
vlmedf203f2005-01-17 11:57:48 +0000344} /* _SEQUENCE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000345
346int
vlmfa67ddc2004-06-03 03:38:44 +0000347asn1c_lang_C_type_SET(arg_t *arg) {
348 asn1p_expr_t *expr = arg->expr;
349 asn1p_expr_t *v;
350 long mcount;
351 char *id;
352 int comp_mode = 0; /* {root,ext=1,root,root,...} */
353
354 DEPENDENCIES;
355
356 REDIR(OT_DEPS);
357
358 OUT("\n");
359 OUT("/*\n");
360 OUT(" * Method of determining the components presence\n");
361 OUT(" */\n");
362 mcount = 0;
vlm766cf1e2005-03-04 08:48:53 +0000363 OUT("typedef enum ");
364 out_name_chain(arg, 0);
365 OUT("_PR {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000366 TQ_FOR(v, &(expr->members), next) {
367 if(v->expr_type == A1TC_EXTENSIBLE) continue;
368 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000369 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000370 OUT("_PR_");
vlmad970212005-03-03 21:58:28 +0000371 id = MKID_nc(v->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000372 OUT("%s,\t/* Member %s is present */\n",
373 id, id)
374 );
375 mcount++;
376 }
vlm766cf1e2005-03-04 08:48:53 +0000377 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
vlmfa67ddc2004-06-03 03:38:44 +0000378
379 REDIR(OT_TYPE_DECLS);
380
381 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000382 OUT("struct ");
383 out_name_chain(arg, 1);
384 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000385 } else {
vlmaf841972005-01-28 12:18:50 +0000386 id = MKID(expr->Identifier);
vlm6e73a042004-08-11 07:17:22 +0000387 OUT("typedef struct %s {\n", id);
vlmfa67ddc2004-06-03 03:38:44 +0000388 }
389
390 TQ_FOR(v, &(expr->members), next) {
391 if(v->expr_type == A1TC_EXTENSIBLE) {
392 if(comp_mode < 3) comp_mode++;
393 }
vlmddd5a7d2004-09-10 09:18:20 +0000394 if(comp_mode == 1
395 || expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000396 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000397 EMBED(v);
398 }
399
400 INDENTED(
vlmad970212005-03-03 21:58:28 +0000401 id = MKID_nc(expr->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000402 OUT("\n");
403 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
404 id, id);
405 OUT("unsigned int _presence_map\n");
406 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
407 );
408
409 PCTX_DEF;
vlma02b74d2004-09-15 11:54:38 +0000410 OUT("} %s%s%s", expr->marker.flags?"*":"",
vlmdae7f9d2004-08-22 03:25:24 +0000411 expr->_anonymous_type ? "" : MKID(expr->Identifier),
412 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000413
414 return asn1c_lang_C_type_SET_def(arg);
415}
416
vlmfa67ddc2004-06-03 03:38:44 +0000417static int
418asn1c_lang_C_type_SET_def(arg_t *arg) {
419 asn1p_expr_t *expr = arg->expr;
420 asn1p_expr_t *v;
421 int elements;
vlmfa67ddc2004-06-03 03:38:44 +0000422 tag2el_t *tag2el = NULL;
423 int tag2el_count = 0;
vlm940bc6b2004-10-03 09:13:30 +0000424 tag2el_t *tag2el_cxer = NULL;
425 int tag2el_cxer_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000426 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000427 int all_tags_count;
428 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000429 char *p;
430
431 /*
432 * Fetch every inner tag from the tag to elements map.
433 */
vlm940bc6b2004-10-03 09:13:30 +0000434 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000435 if(tag2el) free(tag2el);
436 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000437 }
vlm940bc6b2004-10-03 09:13:30 +0000438 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
439 if(tag2el) free(tag2el);
440 if(tag2el_cxer) free(tag2el_cxer);
441 return -1;
442 }
443 if(tag2el_cxer_count == tag2el_count
444 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
445 free(tag2el_cxer);
446 tag2el_cxer = 0;
447 }
vlmfa67ddc2004-06-03 03:38:44 +0000448
vlm33a4ff12004-08-11 05:21:32 +0000449 GEN_INCLUDE("constr_SET");
450 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000451 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000452
453 REDIR(OT_STAT_DEFS);
454
vlmfa67ddc2004-06-03 03:38:44 +0000455 /*
456 * Print out the table according to which the parsing is performed.
457 */
vlm39ba4c42004-09-22 16:06:28 +0000458 if(expr_elements_count(arg, expr)) {
459 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000460
vlm766cf1e2005-03-04 08:48:53 +0000461 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
462 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000463
464 elements = 0;
465 INDENTED(TQ_FOR(v, &(expr->members), next) {
466 if(v->expr_type == A1TC_EXTENSIBLE) {
467 if(comp_mode < 3) comp_mode++;
468 } else {
469 if(comp_mode == 1
470 || expr_better_indirect(arg, v))
471 v->marker.flags |= EM_INDIRECT;
472 elements++;
473 emit_member_table(arg, v);
474 }
475 });
476 OUT("};\n");
477 } else {
478 elements = 0;
479 }
vlmfa67ddc2004-06-03 03:38:44 +0000480
vlm4a3f5822004-06-28 21:13:46 +0000481 /*
vlma5dcb912004-09-29 13:16:40 +0000482 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000483 */
vlm72425de2004-09-13 08:31:01 +0000484 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000485
486 /*
487 * Tags to elements map.
488 */
vlm940bc6b2004-10-03 09:13:30 +0000489 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
490 if(tag2el_cxer)
491 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
vlmfa67ddc2004-06-03 03:38:44 +0000492
493 /*
494 * Emit a map of mandatory elements.
495 */
vlm766cf1e2005-03-04 08:48:53 +0000496 OUT("static uint8_t asn_MAP_%s_%d_mmap",
497 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlm4a3f5822004-06-28 21:13:46 +0000498 p = MKID(expr->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +0000499 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
vlm766cf1e2005-03-04 08:48:53 +0000500 OUT(" = {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000501 INDENTED(
502 if(elements) {
503 int delimit = 0;
504 int el = 0;
505 TQ_FOR(v, &(expr->members), next) {
506 if(v->expr_type == A1TC_EXTENSIBLE) continue;
507 if(delimit) {
508 OUT(",\n");
509 delimit = 0;
510 } else if(el) {
511 OUT(" | ");
512 }
vlmddd5a7d2004-09-10 09:18:20 +0000513 OUT("(%d << %d)",
vlma02b74d2004-09-15 11:54:38 +0000514 v->marker.flags?0:1,
vlmddd5a7d2004-09-10 09:18:20 +0000515 7 - (el % 8));
vlmfa67ddc2004-06-03 03:38:44 +0000516 if(el && (el % 8) == 0)
517 delimit = 1;
518 el++;
519 }
520 } else {
521 OUT("0");
522 }
523 );
524 OUT("\n");
525 OUT("};\n");
526
vlm766cf1e2005-03-04 08:48:53 +0000527 OUT("static asn_SET_specifics_t asn_SPC_%s_%d_specs = {\n",
528 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000529 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000530 OUT("sizeof(struct ");
531 out_name_chain(arg, 1);
532 OUT("),\n");
533 OUT("offsetof(struct ");
534 out_name_chain(arg, 1);
535 OUT(", _asn_ctx),\n");
536 OUT("offsetof(struct ");
537 out_name_chain(arg, 1);
538 OUT(", _presence_map),\n");
vlmad970212005-03-03 21:58:28 +0000539 p = MKID_nc(expr->Identifier);
vlm766cf1e2005-03-04 08:48:53 +0000540 OUT("asn_MAP_%s_%d_tag2el,\n", p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000541 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm940bc6b2004-10-03 09:13:30 +0000542 if(tag2el_cxer)
vlm766cf1e2005-03-04 08:48:53 +0000543 OUT("asn_MAP_%s_%d_tag2el_cxer,\n",
544 p, expr->_type_unique_index);
vlm940bc6b2004-10-03 09:13:30 +0000545 else
vlm766cf1e2005-03-04 08:48:53 +0000546 OUT("asn_MAP_%s_%d_tag2el,\t/* Same as above */\n",
547 p, expr->_type_unique_index);
548 OUT("%d,\t/* Count of tags in the CXER map */\n",
549 tag2el_cxer_count);
vlm79b08d52004-07-01 00:52:50 +0000550 OUT("%d,\t/* Whether extensible */\n",
551 check_if_extensible(expr));
vlm766cf1e2005-03-04 08:48:53 +0000552 OUT("(unsigned int *)asn_MAP_%s_%d_mmap\t/* Mandatory elements map */\n",
553 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000554 );
555 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000556
557 /*
vlma5dcb912004-09-29 13:16:40 +0000558 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000559 */
vlm72425de2004-09-13 08:31:01 +0000560 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000561 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000562
vlmfa67ddc2004-06-03 03:38:44 +0000563 REDIR(OT_TYPE_DECLS);
564
565 return 0;
vlmedf203f2005-01-17 11:57:48 +0000566} /* _SET_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000567
568int
vlmdae7f9d2004-08-22 03:25:24 +0000569asn1c_lang_C_type_SEx_OF(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000570 asn1p_expr_t *expr = arg->expr;
vlmdae7f9d2004-08-22 03:25:24 +0000571 asn1p_expr_t *memb;
vlmfa67ddc2004-06-03 03:38:44 +0000572
573 DEPENDENCIES;
574
575 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000576 OUT("struct ");
577 out_name_chain(arg, 1);
578 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000579 } else {
vlmdae7f9d2004-08-22 03:25:24 +0000580 OUT("typedef struct %s {\n", MKID(expr->Identifier));
vlmfa67ddc2004-06-03 03:38:44 +0000581 }
582
vlmdae7f9d2004-08-22 03:25:24 +0000583 memb = TQ_FIRST(&expr->members);
584
585 INDENT(+1);
586 OUT("A_%s_OF(",
587 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
588 ? "SET" : "SEQUENCE");
vlm80a48592005-02-25 12:10:27 +0000589 if(memb->expr_type & ASN_CONSTR_MASK
590 || ((memb->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +0000591 || (0 /* -- prohibited by X.693:8.3.4 */
592 && memb->expr_type == ASN_BASIC_INTEGER))
593 && expr_elements_count(arg, memb))) {
vlmdae7f9d2004-08-22 03:25:24 +0000594 arg_t tmp;
595 asn1p_expr_t tmp_memb;
596 arg->embed++;
597 tmp = *arg;
598 tmp.expr = &tmp_memb;
599 tmp_memb = *memb;
600 tmp_memb._anonymous_type = 1;
vlm39ba4c42004-09-22 16:06:28 +0000601 if(tmp_memb.Identifier == 0) {
vlm766cf1e2005-03-04 08:48:53 +0000602 tmp_memb.Identifier = "Member";
603 if(0)
vlm39ba4c42004-09-22 16:06:28 +0000604 tmp_memb.Identifier = strdup(
605 asn1c_make_identifier(0,
vlmbf3c5112005-02-14 20:41:29 +0000606 expr->Identifier, "Member", 0));
vlm39ba4c42004-09-22 16:06:28 +0000607 assert(tmp_memb.Identifier);
608 }
vlmdae7f9d2004-08-22 03:25:24 +0000609 tmp.default_cb(&tmp);
vlm39ba4c42004-09-22 16:06:28 +0000610 if(tmp_memb.Identifier != memb->Identifier)
vlm766cf1e2005-03-04 08:48:53 +0000611 if(0) free(tmp_memb.Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000612 arg->embed--;
613 assert(arg->target->target == OT_TYPE_DECLS);
614 } else {
vlmaf841972005-01-28 12:18:50 +0000615 OUT("%s", asn1c_type_name(arg, memb, TNF_CTYPE | TNF_CHECK));
vlmfa67ddc2004-06-03 03:38:44 +0000616 }
vlmdae7f9d2004-08-22 03:25:24 +0000617 OUT(") list;\n");
618 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000619
620 PCTX_DEF;
vlma02b74d2004-09-15 11:54:38 +0000621 OUT("} %s%s%s", expr->marker.flags?"*":"",
vlmdae7f9d2004-08-22 03:25:24 +0000622 expr->_anonymous_type ? "" : MKID(expr->Identifier),
623 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000624
625 /*
vlmdae7f9d2004-08-22 03:25:24 +0000626 * SET OF/SEQUENCE OF definition
vlmfa67ddc2004-06-03 03:38:44 +0000627 */
vlmdae7f9d2004-08-22 03:25:24 +0000628 return asn1c_lang_C_type_SEx_OF_def(arg,
629 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
vlmfa67ddc2004-06-03 03:38:44 +0000630}
631
632static int
633asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
634 asn1p_expr_t *expr = arg->expr;
635 asn1p_expr_t *v;
vlm6e73a042004-08-11 07:17:22 +0000636 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000637 int all_tags_count;
638 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000639
vlm33a4ff12004-08-11 05:21:32 +0000640 /*
641 * Print out the table according to which the parsing is performed.
642 */
vlmfa67ddc2004-06-03 03:38:44 +0000643 if(seq_of) {
vlm33a4ff12004-08-11 05:21:32 +0000644 GEN_INCLUDE("constr_SEQUENCE_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000645 } else {
vlm33a4ff12004-08-11 05:21:32 +0000646 GEN_INCLUDE("constr_SET_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000647 }
vlm33a4ff12004-08-11 05:21:32 +0000648 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000649 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000650
651 REDIR(OT_STAT_DEFS);
652
653 /*
654 * Print out the table according to which the parsing is performed.
655 */
vlm766cf1e2005-03-04 08:48:53 +0000656 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
657 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000658 INDENT(+1);
vlmfa67ddc2004-06-03 03:38:44 +0000659 v = TQ_FIRST(&(expr->members));
vlm39ba4c42004-09-22 16:06:28 +0000660 if(!v->Identifier) {
vlmbf3c5112005-02-14 20:41:29 +0000661 v->Identifier = strdup("Member");
vlm39ba4c42004-09-22 16:06:28 +0000662 assert(v->Identifier);
663 }
664 v->_anonymous_type = 1;
665 arg->embed++;
vlm4e554992004-08-25 02:03:12 +0000666 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000667 arg->embed--;
668 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000669 OUT("};\n");
670
vlm4a3f5822004-06-28 21:13:46 +0000671 /*
vlma5dcb912004-09-29 13:16:40 +0000672 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000673 */
vlm72425de2004-09-13 08:31:01 +0000674 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000675
vlm766cf1e2005-03-04 08:48:53 +0000676 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_%d_specs = {\n",
677 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000678 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000679 OUT("sizeof(struct ");
680 out_name_chain(arg, 1);
681 OUT("),\n");
682 OUT("offsetof(struct ");
683 out_name_chain(arg, 1);
684 OUT(", _asn_ctx),\n");
685
vlm39ba4c42004-09-22 16:06:28 +0000686 if(expr_as_xmlvaluelist(arg, v))
687 OUT("1,\t/* XER encoding is XMLValueList */\n");
688 else
689 OUT("0,\t/* XER encoding is XMLDelimitedItemList */\n");
vlmfa67ddc2004-06-03 03:38:44 +0000690 );
691 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000692
693 /*
vlma5dcb912004-09-29 13:16:40 +0000694 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000695 */
vlm72425de2004-09-13 08:31:01 +0000696 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
vlm86f5ed22004-09-26 13:11:31 +0000697 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000698
vlmfa67ddc2004-06-03 03:38:44 +0000699 REDIR(OT_TYPE_DECLS);
700
701 return 0;
vlmedf203f2005-01-17 11:57:48 +0000702} /* _SEx_OF_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000703
704int
705asn1c_lang_C_type_CHOICE(arg_t *arg) {
706 asn1p_expr_t *expr = arg->expr;
707 asn1p_expr_t *v;
vlmdae7f9d2004-08-22 03:25:24 +0000708 char *id;
vlmfa67ddc2004-06-03 03:38:44 +0000709
710 DEPENDENCIES;
711
vlm33a4ff12004-08-11 05:21:32 +0000712 REDIR(OT_DEPS);
713
vlm766cf1e2005-03-04 08:48:53 +0000714 OUT("typedef enum ");
715 out_name_chain(arg, 0);
716 OUT("_PR {\n");
vlm33a4ff12004-08-11 05:21:32 +0000717 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000718 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000719 OUT("_PR_NOTHING,\t/* No components present */\n");
vlm33a4ff12004-08-11 05:21:32 +0000720 TQ_FOR(v, &(expr->members), next) {
721 if(v->expr_type == A1TC_EXTENSIBLE) {
722 OUT("/* Extensions may appear below */\n");
723 continue;
724 }
vlm766cf1e2005-03-04 08:48:53 +0000725 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000726 OUT("_PR_");
vlmad970212005-03-03 21:58:28 +0000727 id = MKID_nc(v->Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000728 OUT("%s,\n", id, id);
vlm33a4ff12004-08-11 05:21:32 +0000729 }
730 );
vlm766cf1e2005-03-04 08:48:53 +0000731 OUT("} "); out_name_chain(arg, 0); OUT("_PR;\n");
vlm33a4ff12004-08-11 05:21:32 +0000732
733 REDIR(OT_TYPE_DECLS);
vlmfa67ddc2004-06-03 03:38:44 +0000734
735 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000736 OUT("struct "); out_name_chain(arg, 1); OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000737 } else {
vlmaf841972005-01-28 12:18:50 +0000738 id = MKID(expr->Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000739 OUT("typedef struct %s {\n", id);
vlmfa67ddc2004-06-03 03:38:44 +0000740 }
741
742 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000743 out_name_chain(arg, 0);
vlmaf841972005-01-28 12:18:50 +0000744 OUT("_PR present;\n");
745 OUT("union {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000746 TQ_FOR(v, &(expr->members), next) {
vlmddd5a7d2004-09-10 09:18:20 +0000747 if(expr_better_indirect(arg, v))
vlma02b74d2004-09-15 11:54:38 +0000748 v->marker.flags |= EM_INDIRECT;
vlmfa67ddc2004-06-03 03:38:44 +0000749 EMBED(v);
750 }
751 if(UNNAMED_UNIONS) OUT("};\n");
752 else OUT("} choice;\n");
753 );
754
755 PCTX_DEF;
vlma02b74d2004-09-15 11:54:38 +0000756 OUT("} %s%s%s", expr->marker.flags?"*":"",
vlmdae7f9d2004-08-22 03:25:24 +0000757 expr->_anonymous_type ? "" : MKID(expr->Identifier),
758 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000759
760 return asn1c_lang_C_type_CHOICE_def(arg);
761}
762
763static int
764asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
765 asn1p_expr_t *expr = arg->expr;
766 asn1p_expr_t *v;
767 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000768 tag2el_t *tag2el = NULL;
769 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000770 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000771 int all_tags_count;
772 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000773
774 /*
775 * Fetch every inner tag from the tag to elements map.
776 */
vlm940bc6b2004-10-03 09:13:30 +0000777 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000778 if(tag2el) free(tag2el);
779 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000780 }
781
vlm33a4ff12004-08-11 05:21:32 +0000782 GEN_INCLUDE("constr_CHOICE");
783 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000784 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000785
vlm33a4ff12004-08-11 05:21:32 +0000786 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000787
788 /*
789 * Print out the table according to which the parsing is performed.
790 */
vlm39ba4c42004-09-22 16:06:28 +0000791 if(expr_elements_count(arg, expr)) {
vlmfa67ddc2004-06-03 03:38:44 +0000792
vlm766cf1e2005-03-04 08:48:53 +0000793 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
794 MKID(expr->Identifier), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000795
796 elements = 0;
797 INDENTED(TQ_FOR(v, &(expr->members), next) {
vlm34dcd572005-01-17 11:40:49 +0000798 if(v->expr_type == A1TC_EXTENSIBLE)
799 continue;
800 if(expr_better_indirect(arg, v))
801 v->marker.flags |= EM_INDIRECT;
802 elements++;
803 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000804 });
805 OUT("};\n");
806 } else {
807 elements = 0;
808 }
vlmfa67ddc2004-06-03 03:38:44 +0000809
vlm6e73a042004-08-11 07:17:22 +0000810
vlmfa67ddc2004-06-03 03:38:44 +0000811 if(arg->embed) {
812 /*
813 * Our parent structure has already taken this into account.
814 */
vlm72425de2004-09-13 08:31:01 +0000815 tv_mode = _TVM_SAME;
816 tags_count = all_tags_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000817 } else {
vlm72425de2004-09-13 08:31:01 +0000818 tv_mode = emit_tags_vectors(arg, expr,
819 &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000820 }
vlmfa67ddc2004-06-03 03:38:44 +0000821
822 /*
823 * Tags to elements map.
824 */
vlm940bc6b2004-10-03 09:13:30 +0000825 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000826
vlm766cf1e2005-03-04 08:48:53 +0000827 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_%d_specs = {\n",
828 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000829 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000830 OUT("sizeof(struct ");
831 out_name_chain(arg, 1);
832 OUT("),\n");
833 OUT("offsetof(struct ");
834 out_name_chain(arg, 1);
835 OUT(", _asn_ctx),\n");
836 OUT("offsetof(struct ");
837 out_name_chain(arg, 1);
838 OUT(", present),\n");
839 OUT("sizeof(((struct ");
840 out_name_chain(arg, 1);
841 OUT(" *)0)->present),\n");
842 OUT("asn_MAP_%s_%d_tag2el,\n",
843 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000844 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm79b08d52004-07-01 00:52:50 +0000845 OUT("%d\t/* Whether extensible */\n",
846 check_if_extensible(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000847 );
848 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000849
850 /*
vlma5dcb912004-09-29 13:16:40 +0000851 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000852 */
vlm72425de2004-09-13 08:31:01 +0000853 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000854 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000855
vlmfa67ddc2004-06-03 03:38:44 +0000856 REDIR(OT_TYPE_DECLS);
857
858 return 0;
vlmedf203f2005-01-17 11:57:48 +0000859} /* _CHOICE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000860
861int
862asn1c_lang_C_type_REFERENCE(arg_t *arg) {
863 asn1p_ref_t *ref;
864
865 ref = arg->expr->reference;
866 if(ref->components[ref->comp_count-1].name[0] == '&') {
vlmfa67ddc2004-06-03 03:38:44 +0000867 asn1p_expr_t *extract;
868 arg_t tmp;
869 int ret;
870
vlm39ba4c42004-09-22 16:06:28 +0000871 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
vlmdae7f9d2004-08-22 03:25:24 +0000872 arg->expr, ref);
vlmfa67ddc2004-06-03 03:38:44 +0000873 if(extract == NULL)
874 return -1;
875
vlmdae7f9d2004-08-22 03:25:24 +0000876 extract = asn1p_expr_clone(extract, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000877 if(extract) {
878 if(extract->Identifier)
879 free(extract->Identifier);
880 extract->Identifier = strdup(arg->expr->Identifier);
881 if(extract->Identifier == NULL) {
882 asn1p_expr_free(extract);
883 return -1;
884 }
885 } else {
886 return -1;
887 }
888
889 tmp = *arg;
890 tmp.asn = arg->asn;
vlmdae7f9d2004-08-22 03:25:24 +0000891 tmp.mod = extract->module;
vlmfa67ddc2004-06-03 03:38:44 +0000892 tmp.expr = extract;
893
894 ret = arg->default_cb(&tmp);
895
896 asn1p_expr_free(extract);
897
898 return ret;
899 }
900
901
902 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
903}
904
905int
906asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
907 asn1p_expr_t *expr = arg->expr;
vlm6e73a042004-08-11 07:17:22 +0000908 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000909 int all_tags_count;
910 enum tvm_compat tv_mode;
vlm80a48592005-02-25 12:10:27 +0000911 enum etd_spec etd_spec;
vlmfa67ddc2004-06-03 03:38:44 +0000912 char *p;
913
914 if(arg->embed) {
vlma5dcb912004-09-29 13:16:40 +0000915 enum tnfmt tnfmt = TNF_CTYPE;
916
917 /*
918 * If this is an optional compound type,
919 * refer it using "struct X" convention,
920 * as it may recursively include the current structure.
921 */
922 if(expr->marker.flags) {
923 asn1p_expr_t *terminal;
924 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
925 if(terminal
926 && (terminal->expr_type & ASN_CONSTR_MASK)) {
927 REDIR(OT_DEPS);
928 tnfmt = TNF_RSAFE;
929 OUT("\n");
930 OUT("%s;\t/* Forward declaration */\n",
vlmaf841972005-01-28 12:18:50 +0000931 asn1c_type_name(arg, arg->expr, tnfmt | TNF_CHECK));
vlma5dcb912004-09-29 13:16:40 +0000932 }
933 }
934
vlmfa67ddc2004-06-03 03:38:44 +0000935 REDIR(OT_TYPE_DECLS);
936
vlm80a48592005-02-25 12:10:27 +0000937 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt | TNF_CHECK));
938 if(!expr->_anonymous_type) {
939 OUT("%s", expr->marker.flags?"\t*":"\t ");
940 OUT("%s", MKID(expr->Identifier));
941 if((expr->marker.flags & EM_DEFAULT) == EM_DEFAULT)
942 OUT("\t/* DEFAULT %s */",
943 asn1f_printable_value(
944 expr->marker.default_value));
945 else if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL)
946 OUT("\t/* OPTIONAL */");
947 }
948
949 } else {
950 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
vlmb2839012004-08-20 13:37:01 +0000951
952 REDIR(OT_TYPE_DECLS);
vlm80a48592005-02-25 12:10:27 +0000953
954 OUT("typedef %s\t",
955 asn1c_type_name(arg, arg->expr, TNF_CTYPE | TNF_CHECK));
956 OUT("%s%s_t",
957 expr->marker.flags?"*":" ",
958 MKID(expr->Identifier));
959 OUT_DEBUG("\t/* %s:%d */", __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000960 }
961
vlm80a48592005-02-25 12:10:27 +0000962 if((expr->expr_type == ASN_BASIC_ENUMERATED)
vlmc89422d2005-03-03 21:28:12 +0000963 || (0 /* -- prohibited by X.693:8.3.4 */
964 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +0000965 && expr_elements_count(arg, expr)))
966 etd_spec = ETD_HAS_SPECIFICS;
967 else
968 etd_spec = ETD_NO_SPECIFICS;
vlmfa67ddc2004-06-03 03:38:44 +0000969
vlm86f5ed22004-09-26 13:11:31 +0000970 /*
971 * If this type just blindly refers the other type, alias it.
972 * Type1 ::= Type2
973 */
vlm80a48592005-02-25 12:10:27 +0000974 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
975 REDIR(OT_TYPE_DECLS);
976 return 0;
977 }
vlm86f5ed22004-09-26 13:11:31 +0000978 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlm80a48592005-02-25 12:10:27 +0000979 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
980 && etd_spec == ETD_NO_SPECIFICS) {
vlm86f5ed22004-09-26 13:11:31 +0000981 char *type_name;
982 REDIR(OT_FUNC_DECLS);
983 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
984 OUT("/* This type is equivalent to %s */\n", type_name);
vlm86f5ed22004-09-26 13:11:31 +0000985 if(HIDE_INNER_DEFS) OUT("/* ");
vlmad970212005-03-03 21:58:28 +0000986 OUT("#define\tasn_DEF_%s\t", MKID_nc(expr->Identifier));
vlm86f5ed22004-09-26 13:11:31 +0000987 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlm80a48592005-02-25 12:10:27 +0000988 OUT("asn_DEF_%s", type_name);
vlm86f5ed22004-09-26 13:11:31 +0000989 if(HIDE_INNER_DEFS)
vlm80a48592005-02-25 12:10:27 +0000990 OUT("\t// (Use -fall-defs-global to expose) */");
991 OUT("\n");
vlm86f5ed22004-09-26 13:11:31 +0000992 REDIR(OT_CODE);
993 OUT("/* This type is equivalent to %s */\n", type_name);
994 OUT("\n");
995 REDIR(OT_TYPE_DECLS);
996 return 0;
997 }
998
vlmfa67ddc2004-06-03 03:38:44 +0000999 REDIR(OT_STAT_DEFS);
1000
vlm4a3f5822004-06-28 21:13:46 +00001001 /*
vlma5dcb912004-09-29 13:16:40 +00001002 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +00001003 */
vlm72425de2004-09-13 08:31:01 +00001004 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +00001005
vlm80a48592005-02-25 12:10:27 +00001006 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1007 0, etd_spec);
vlmfa67ddc2004-06-03 03:38:44 +00001008
vlmb2839012004-08-20 13:37:01 +00001009 REDIR(OT_CODE);
1010
vlmfa67ddc2004-06-03 03:38:44 +00001011 /*
1012 * Constraint checking.
1013 */
vlm86f5ed22004-09-26 13:11:31 +00001014 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
1015 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001016 if(HIDE_INNER_DEFS) OUT("static ");
vlm86f5ed22004-09-26 13:11:31 +00001017 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001018 OUT("%s", p);
1019 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1020 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
vlm86f5ed22004-09-26 13:11:31 +00001021 INDENT(+1);
1022 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {");
1023 OUT("\n");
1024 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1025 OUT("/* Replace with underlying type checker */\n");
1026 OUT("td->check_constraints "
vlma5dcb912004-09-29 13:16:40 +00001027 "= asn_DEF_%s.check_constraints;\n",
vlm86f5ed22004-09-26 13:11:31 +00001028 asn1c_type_name(arg, expr, TNF_SAFE));
1029 OUT("return td->check_constraints"
1030 "(td, sptr, app_errlog, app_key);\n");
vlm1d036692004-08-19 13:29:46 +00001031 }
vlm86f5ed22004-09-26 13:11:31 +00001032 INDENT(-1);
1033 OUT("}\n");
1034 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001035 }
vlmfa67ddc2004-06-03 03:38:44 +00001036
1037 /*
1038 * Emit suicidal functions.
1039 */
1040
vlmfa67ddc2004-06-03 03:38:44 +00001041 /*
1042 * This function replaces certain fields from the definition
1043 * of a type with the corresponding fields from the basic type
1044 * (from which the current type is inherited).
1045 */
vlmfa67ddc2004-06-03 03:38:44 +00001046 OUT("/*\n");
vlmb2839012004-08-20 13:37:01 +00001047 OUT(" * This type is implemented using %s,\n",
1048 asn1c_type_name(arg, expr, TNF_SAFE));
vlmdcd32fe2004-09-23 22:20:47 +00001049 OUT(" * so here we adjust the DEF accordingly.\n");
vlmfa67ddc2004-06-03 03:38:44 +00001050 OUT(" */\n");
1051 OUT("static void\n");
vlm766cf1e2005-03-04 08:48:53 +00001052 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
1053 MKID_nc(expr->Identifier), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001054 INDENT(+1);
vlm80a48592005-02-25 12:10:27 +00001055 {
vlm39ba4c42004-09-22 16:06:28 +00001056 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001057 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlma5dcb912004-09-29 13:16:40 +00001058 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1059 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1060 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1061 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1062 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1063 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
vlm1308d2b2004-09-10 15:49:15 +00001064 if(!terminal && !tags_count) {
vlm72425de2004-09-13 08:31:01 +00001065 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
vlma5dcb912004-09-29 13:16:40 +00001066 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1067 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1068 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1069 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
vlm1308d2b2004-09-10 15:49:15 +00001070 OUT("/* End of these lines */\n");
1071 }
vlma5dcb912004-09-29 13:16:40 +00001072 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1073 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
vlm80a48592005-02-25 12:10:27 +00001074 if(etd_spec != ETD_NO_SPECIFICS) {
1075 INDENT(-1);
1076 OUT(" /* ");
vlmb2839012004-08-20 13:37:01 +00001077 }
vlm80a48592005-02-25 12:10:27 +00001078 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1079 if(etd_spec == ETD_NO_SPECIFICS) {
1080 INDENT(-1);
1081 OUT("\n");
1082 } else {
1083 OUT("\t// Defined explicitly */\n");
1084 }
1085 }
vlmfa67ddc2004-06-03 03:38:44 +00001086 OUT("}\n");
1087 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001088
1089 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001090 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001091 OUT("void\n");
vlm766cf1e2005-03-04 08:48:53 +00001092 OUT("%s", p);
1093 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1094 OUT("_free(asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001095 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001096 OUT("\tvoid *struct_ptr, int contents_only) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001097 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1098 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001099 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001100 );
1101 OUT("}\n");
1102 OUT("\n");
1103
1104 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001105 if(HIDE_INNER_DEFS) OUT("static ");
vlmfa67ddc2004-06-03 03:38:44 +00001106 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001107 OUT("%s", p);
1108 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1109 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001110 INDENTED(
1111 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001112 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1113 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001114 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1115 );
1116 OUT("}\n");
1117 OUT("\n");
1118
1119 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001120 if(HIDE_INNER_DEFS) OUT("static ");
vlm9de248e2004-10-20 15:50:55 +00001121 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001122 OUT("%s", p);
1123 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1124 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001125 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001126 OUT("\tvoid **structure, void *bufptr, size_t size, int tag_mode) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001127 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1128 p, expr->_type_unique_index);
vlma5dcb912004-09-29 13:16:40 +00001129 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
vlm39ba4c42004-09-22 16:06:28 +00001130 );
1131 OUT("}\n");
1132 OUT("\n");
1133
1134 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001135 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001136 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001137 OUT("%s", p);
1138 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1139 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
vlm39ba4c42004-09-22 16:06:28 +00001140 INDENTED(
1141 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1142 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001143 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1144 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001145 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1146 );
1147 OUT("}\n");
1148 OUT("\n");
1149
1150 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001151 if(HIDE_INNER_DEFS) OUT("static ");
vlme03646b2004-10-23 13:34:00 +00001152 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001153 OUT("%s", p);
1154 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1155 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlme03646b2004-10-23 13:34:00 +00001156 INDENTED(
1157 OUT("\tvoid **structure, const char *opt_mname, void *bufptr, size_t size) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001158 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1159 p, expr->_type_unique_index);
vlme03646b2004-10-23 13:34:00 +00001160 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1161 );
1162 OUT("}\n");
1163 OUT("\n");
1164
1165 p = MKID(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001166 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001167 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001168 OUT("%s", p);
1169 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1170 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
vlm39ba4c42004-09-22 16:06:28 +00001171 INDENTED(
1172 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1173 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001174 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1175 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001176 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001177 );
1178 OUT("}\n");
1179 OUT("\n");
1180
1181 REDIR(OT_FUNC_DECLS);
1182
vlmad970212005-03-03 21:58:28 +00001183 p = MKID_nc(expr->Identifier);
vlm80a48592005-02-25 12:10:27 +00001184 if(HIDE_INNER_DEFS) {
vlm766cf1e2005-03-04 08:48:53 +00001185 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1186 "\t// (Use -fall-defs-global to expose) */\n",
1187 p, expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +00001188 } else {
1189 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1190 OUT("asn_struct_free_f %s_free;\n", p);
1191 OUT("asn_struct_print_f %s_print;\n", p);
1192 OUT("asn_constr_check_f %s_constraint;\n", p);
1193 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1194 OUT("der_type_encoder_f %s_encode_der;\n", p);
1195 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1196 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
1197 }
vlmfa67ddc2004-06-03 03:38:44 +00001198
vlm33a4ff12004-08-11 05:21:32 +00001199 REDIR(OT_TYPE_DECLS);
1200
vlmfa67ddc2004-06-03 03:38:44 +00001201 return 0;
1202}
1203
1204int
1205asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1206
1207 OUT("/*\n");
1208 OUT(" * This type is extensible,\n");
1209 OUT(" * possible extensions are below.\n");
1210 OUT(" */\n");
1211
1212 return 0;
1213}
1214
vlm79b08d52004-07-01 00:52:50 +00001215static int check_if_extensible(asn1p_expr_t *expr) {
1216 asn1p_expr_t *v;
1217 TQ_FOR(v, &(expr->members), next) {
1218 if(v->expr_type == A1TC_EXTENSIBLE) return 1;
1219 }
1220 return 0;
1221}
1222
vlmfa67ddc2004-06-03 03:38:44 +00001223static int
vlma8a86cc2004-09-10 06:07:18 +00001224_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
vlmfa67ddc2004-06-03 03:38:44 +00001225
1226 OUT("(");
vlma8a86cc2004-09-10 06:07:18 +00001227 switch(tag->tag_class) {
vlmfa67ddc2004-06-03 03:38:44 +00001228 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1229 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1230 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1231 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1232 case TC_NOCLASS:
1233 break;
1234 }
vlm47ae1582004-09-24 21:01:43 +00001235 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +00001236
1237 return 0;
1238}
1239
vlm4e03ce22004-06-06 07:20:17 +00001240
1241static int
1242_tag2el_cmp(const void *ap, const void *bp) {
1243 const tag2el_t *a = ap;
1244 const tag2el_t *b = bp;
1245 const struct asn1p_type_tag_s *ta = &a->el_tag;
1246 const struct asn1p_type_tag_s *tb = &b->el_tag;
1247
1248 if(ta->tag_class == tb->tag_class) {
1249 if(ta->tag_value == tb->tag_value) {
1250 /*
1251 * Sort by their respective positions.
1252 */
1253 if(a->el_no < b->el_no)
1254 return -1;
1255 else if(a->el_no > b->el_no)
1256 return 1;
1257 return 0;
1258 } else if(ta->tag_value < tb->tag_value)
1259 return -1;
1260 else
1261 return 1;
1262 } else if(ta->tag_class < tb->tag_class) {
1263 return -1;
1264 } else {
1265 return 1;
1266 }
1267}
1268
vlmfa67ddc2004-06-03 03:38:44 +00001269/*
1270 * For constructed types, number of external tags may be greater than
1271 * number of elements in the type because of CHOICE type.
1272 * T ::= SET { -- Three possible tags:
1273 * a INTEGER, -- One tag is here...
1274 * b Choice1 -- ... and two more tags are there.
1275 * }
1276 * Choice1 ::= CHOICE {
1277 * s1 IA5String,
1278 * s2 ObjectDescriptor
1279 * }
1280 */
1281static int
vlm940bc6b2004-10-03 09:13:30 +00001282_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001283 asn1p_expr_t *expr = arg->expr;
1284 arg_t tmparg = *arg;
1285 asn1p_expr_t *v;
1286 int element = 0;
vlm940bc6b2004-10-03 09:13:30 +00001287 int original_count = *count;
1288 int sort_until = -1;
vlmfa67ddc2004-06-03 03:38:44 +00001289
1290 TQ_FOR(v, &(expr->members), next) {
vlm940bc6b2004-10-03 09:13:30 +00001291 if(v->expr_type == A1TC_EXTENSIBLE) {
1292 /*
vlm80a48592005-02-25 12:10:27 +00001293 * CXER mandates sorting
vlm940bc6b2004-10-03 09:13:30 +00001294 * only for the root part.
1295 */
1296 if(flags == FTE_CANONICAL_XER
1297 && sort_until == -1)
1298 sort_until = *count;
vlmfa67ddc2004-06-03 03:38:44 +00001299 continue;
vlm940bc6b2004-10-03 09:13:30 +00001300 }
vlmfa67ddc2004-06-03 03:38:44 +00001301
1302 tmparg.expr = v;
1303
1304 if(_add_tag2el_member(&tmparg, tag2el, count,
vlm940bc6b2004-10-03 09:13:30 +00001305 (el_no==-1)?element:el_no, flags)) {
vlmfa67ddc2004-06-03 03:38:44 +00001306 return -1;
1307 }
1308
1309 element++;
1310 }
1311
vlm940bc6b2004-10-03 09:13:30 +00001312
1313 if(flags == FTE_CANONICAL_XER) {
1314 if(sort_until == -1) sort_until = *count;
1315 qsort((*tag2el) + original_count,
1316 sort_until - original_count,
1317 sizeof(**tag2el), _tag2el_cmp);
1318 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1319 && (sort_until - original_count) >= 1) {
1320 /* Only take in account the root component */
1321 *count = original_count + 1;
1322 }
1323 } else {
1324 /*
1325 * Sort the map according to canonical order of their
1326 * tags and element numbers.
1327 */
1328 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1329 }
vlm4e03ce22004-06-06 07:20:17 +00001330
vlmc8aeab42004-06-14 13:09:45 +00001331 /*
1332 * Initialize .toff_{first|last} members.
1333 */
1334 if(*count) {
1335 struct asn1p_type_tag_s *cur_tag = 0;
1336 tag2el_t *cur = *tag2el;
1337 tag2el_t *end = cur + *count;
1338 int occur, i;
1339 for(occur = 0; cur < end; cur++) {
1340 if(cur_tag == 0
1341 || cur_tag->tag_value != cur->el_tag.tag_value
1342 || cur_tag->tag_class != cur->el_tag.tag_class) {
1343 cur_tag = &cur->el_tag;
1344 occur = 0;
1345 } else {
1346 occur++;
1347 }
1348 cur->toff_first = -occur;
1349 for(i = 0; i >= -occur; i--)
1350 cur[i].toff_last = -i;
1351 }
1352 }
1353
vlmfa67ddc2004-06-03 03:38:44 +00001354 return 0;
1355}
1356
1357static int
vlm940bc6b2004-10-03 09:13:30 +00001358_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001359 struct asn1p_type_tag_s tag;
1360 int ret;
1361
1362 assert(el_no >= 0);
1363
vlm39ba4c42004-09-22 16:06:28 +00001364 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
1365 arg->expr, &tag, 1);
vlmfa67ddc2004-06-03 03:38:44 +00001366 if(ret == 0) {
vlme2070ea2004-09-04 04:42:29 +00001367 tag2el_t *te;
1368 int new_count = (*count) + 1;
vlmfa67ddc2004-06-03 03:38:44 +00001369 void *p;
vlme2070ea2004-09-04 04:42:29 +00001370
vlm2de0e792004-09-05 10:42:33 +00001371 if(tag.tag_value == -1) {
1372 /*
1373 * This is an untagged ANY type,
1374 * proceed without adding a tag
1375 */
1376 return 0;
1377 }
1378
vlme2070ea2004-09-04 04:42:29 +00001379 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
vlmfa67ddc2004-06-03 03:38:44 +00001380 if(p) *tag2el = p;
1381 else return -1;
1382
1383 DEBUG("Found tag for %s: %ld",
1384 arg->expr->Identifier,
1385 (long)tag.tag_value);
1386
vlme2070ea2004-09-04 04:42:29 +00001387 te = &((*tag2el)[*count]);
1388 te->el_tag = tag;
1389 te->el_no = el_no;
1390 te->from_expr = arg->expr;
1391 *count = new_count;
vlmfa67ddc2004-06-03 03:38:44 +00001392 return 0;
1393 }
1394
1395 DEBUG("Searching tag in complex expression %s:%x at line %d",
1396 arg->expr->Identifier,
1397 arg->expr->expr_type,
1398 arg->expr->_lineno);
1399
1400 /*
1401 * Iterate over members of CHOICE type.
1402 */
1403 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
vlm940bc6b2004-10-03 09:13:30 +00001404 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001405 }
1406
1407 if(arg->expr->expr_type == A1TC_REFERENCE) {
1408 arg_t tmp = *arg;
1409 asn1p_expr_t *expr;
vlmdae7f9d2004-08-22 03:25:24 +00001410 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.mod, tmp.expr,
vlmfa67ddc2004-06-03 03:38:44 +00001411 arg->expr->reference);
1412 if(expr) {
vlmdae7f9d2004-08-22 03:25:24 +00001413 tmp.mod = expr->module;
vlmfa67ddc2004-06-03 03:38:44 +00001414 tmp.expr = expr;
vlm940bc6b2004-10-03 09:13:30 +00001415 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001416 } else {
1417 FATAL("Cannot dereference %s at line %d",
1418 arg->expr->Identifier,
1419 arg->expr->_lineno);
1420 return -1;
1421 }
1422 }
1423
1424 DEBUG("No tag for %s at line %d",
1425 arg->expr->Identifier,
1426 arg->expr->_lineno);
1427
1428 return -1;
1429}
1430
1431static int
vlm940bc6b2004-10-03 09:13:30 +00001432emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
vlm4e03ce22004-06-06 07:20:17 +00001433 asn1p_expr_t *expr = arg->expr;
1434
vlm766cf1e2005-03-04 08:48:53 +00001435 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_%d_tag2el%s[] = {\n",
1436 MKID_nc(expr->Identifier), expr->_type_unique_index,
1437 opt_modifier?opt_modifier:"");
vlm4e03ce22004-06-06 07:20:17 +00001438 if(tag2el_count) {
1439 int i;
1440 for(i = 0; i < tag2el_count; i++) {
1441 OUT(" { ");
vlma8a86cc2004-09-10 06:07:18 +00001442 _print_tag(arg, &tag2el[i].el_tag);
vlm4e03ce22004-06-06 07:20:17 +00001443 OUT(", ");
vlmc8aeab42004-06-14 13:09:45 +00001444 OUT("%d, ", tag2el[i].el_no);
1445 OUT("%d, ", tag2el[i].toff_first);
1446 OUT("%d ", tag2el[i].toff_last);
vlm80a48592005-02-25 12:10:27 +00001447 OUT("}%s /* %s at %d */\n",
1448 (i + 1 < tag2el_count) ? "," : "",
vlm4e03ce22004-06-06 07:20:17 +00001449 tag2el[i].from_expr->Identifier,
1450 tag2el[i].from_expr->_lineno
1451 );
1452 }
1453 }
1454 OUT("};\n");
1455
1456 return 0;;
1457}
1458
vlm72425de2004-09-13 08:31:01 +00001459static enum tvm_compat
1460emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1461 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1462 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
vlm6e73a042004-08-11 07:17:22 +00001463 int tags_count = 0;
vlm72425de2004-09-13 08:31:01 +00001464 int all_tags_count = 0;
1465 enum tvm_compat tv_mode = _TVM_SAME;
vlm1308d2b2004-09-10 15:49:15 +00001466 int i;
vlm4a3f5822004-06-28 21:13:46 +00001467
vlmc6de2c42004-09-14 14:10:10 +00001468 /* Cleanup before proceeding. */
1469 *tags_count_r = 0;
1470 *all_tags_count_r = 0;
1471
vlm1308d2b2004-09-10 15:49:15 +00001472 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001473 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
vlmc6de2c42004-09-14 14:10:10 +00001474 if(tags_count < 0)
1475 return -1;
vlm6e73a042004-08-11 07:17:22 +00001476
vlm72425de2004-09-13 08:31:01 +00001477 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001478 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
vlm72425de2004-09-13 08:31:01 +00001479 &all_tags, AFT_FULL_COLLECT);
1480 if(all_tags_count < 0) {
1481 if(tags) free(tags);
1482 return -1;
vlmfb41dbb2004-09-08 00:28:57 +00001483 }
vlm1308d2b2004-09-10 15:49:15 +00001484
vlm72425de2004-09-13 08:31:01 +00001485 assert(tags_count <= all_tags_count);
1486 assert((tags_count?0:1) == (all_tags_count?0:1));
vlm4a3f5822004-06-28 21:13:46 +00001487
vlm72425de2004-09-13 08:31:01 +00001488 if(tags_count <= all_tags_count) {
1489 for(i = 0; i < tags_count; i++) {
1490 if(tags[i].tag_value != all_tags[i].tag_value
1491 || tags[i].tag_class != all_tags[i].tag_class) {
1492 tv_mode = _TVM_DIFFERENT;
1493 break;
1494 }
1495 }
1496 if(i == tags_count && tags_count < all_tags_count)
1497 tv_mode = _TVM_SUBSET;
1498 } else {
1499 tv_mode = _TVM_DIFFERENT;
1500 }
1501
1502#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
vlm766cf1e2005-03-04 08:48:53 +00001503 OUT("static ber_tlv_tag_t asn_DEF_%s_%d%s_tags[] = {\n",\
1504 MKID_nc(expr->Identifier), \
1505 expr->_type_unique_index, name); \
vlm72425de2004-09-13 08:31:01 +00001506 INDENT(+1); \
1507 /* Print the array of collected tags */ \
1508 for(i = 0; i < tags_count; i++) { \
1509 if(i) OUT(",\n"); \
1510 _print_tag(arg, &tags[i]); \
1511 } \
1512 OUT("\n"); \
1513 INDENT(-1); \
1514 OUT("};\n"); \
1515 } while(0)
1516
1517 if(tags_count) {
1518 if(tv_mode == _TVM_SUBSET)
1519 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1520 else
1521 EMIT_TAGS_TABLE("", tags, tags_count);
1522 }
1523
1524 if(all_tags_count) {
1525 if(tv_mode == _TVM_DIFFERENT)
1526 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1527 }
1528
1529 if(tags) free(tags);
1530 if(all_tags) free(all_tags);
1531
1532 *tags_count_r = tags_count;
1533 *all_tags_count_r = all_tags_count;
1534
1535 return tv_mode;
vlm4a3f5822004-06-28 21:13:46 +00001536}
vlmb2839012004-08-20 13:37:01 +00001537
1538static int
vlm4e554992004-08-25 02:03:12 +00001539expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001540 asn1p_expr_t *topmost_parent;
1541 asn1p_expr_t *v;
1542 int elements = 0;
1543
vlm39ba4c42004-09-22 16:06:28 +00001544 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001545 if(!topmost_parent) return 0;
1546
vlm80a48592005-02-25 12:10:27 +00001547 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1548 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1549 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
vlmb2839012004-08-20 13:37:01 +00001550 return 0;
1551
1552 TQ_FOR(v, &(topmost_parent->members), next) {
1553 if(v->expr_type != A1TC_EXTENSIBLE)
1554 elements++;
1555 }
1556
1557 return elements;
1558}
1559
1560static int
vlm4e554992004-08-25 02:03:12 +00001561emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001562 int save_target;
1563 arg_t tmp_arg;
vlm060fe2a2004-09-10 09:37:12 +00001564 struct asn1p_type_tag_s outmost_tag_s;
1565 struct asn1p_type_tag_s *outmost_tag;
vlm80a48592005-02-25 12:10:27 +00001566 int complex_contents;
vlmb2839012004-08-20 13:37:01 +00001567 char *p;
1568
vlm060fe2a2004-09-10 09:37:12 +00001569 if(asn1f_fetch_outmost_tag(arg->asn,
1570 expr->module, expr, &outmost_tag_s, 1)) {
1571 outmost_tag = 0;
1572 } else {
1573 outmost_tag = &outmost_tag_s;
1574 }
1575
vlmb2839012004-08-20 13:37:01 +00001576 OUT("{ ");
vlm060fe2a2004-09-10 09:37:12 +00001577
1578 if(outmost_tag && outmost_tag->tag_value == -1)
1579 OUT("ATF_OPEN_TYPE | ");
vlma02b74d2004-09-15 11:54:38 +00001580 OUT("%s, ", expr->marker.flags?"ATF_POINTER":"ATF_NOFLAGS");
1581 if((expr->marker.flags & EM_OPTIONAL) == EM_OPTIONAL) {
vlmb2839012004-08-20 13:37:01 +00001582 asn1p_expr_t *tv;
1583 int opts = 0;
vlma02b74d2004-09-15 11:54:38 +00001584 for(tv = expr; tv && tv->marker.flags;
vlmb2839012004-08-20 13:37:01 +00001585 tv = TQ_NEXT(tv, next), opts++) {
1586 if(tv->expr_type == A1TC_EXTENSIBLE)
1587 opts--;
1588 }
1589 OUT("%d, ", opts);
1590 } else {
1591 OUT("0, ");
1592 }
vlm39ba4c42004-09-22 16:06:28 +00001593 if(expr->_anonymous_type) {
1594 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
1595 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
1596 OUT("0,\n");
1597 } else {
vlm766cf1e2005-03-04 08:48:53 +00001598 OUT("offsetof(struct ");
1599 out_name_chain(arg, 1);
1600 OUT(", ");
vlmb2839012004-08-20 13:37:01 +00001601 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1602 && (!UNNAMED_UNIONS)) OUT("choice.");
1603 OUT("%s),\n", MKID(expr->Identifier));
vlmb2839012004-08-20 13:37:01 +00001604 }
1605 INDENT(+1);
1606 if(C99_MODE) OUT(".tag = ");
vlm060fe2a2004-09-10 09:37:12 +00001607 if(outmost_tag) {
1608 if(outmost_tag->tag_value == -1)
1609 OUT("-1 /* Ambiguous tag (ANY?) */");
1610 else
1611 _print_tag(arg, outmost_tag);
vlma8a86cc2004-09-10 06:07:18 +00001612 } else {
vlm060fe2a2004-09-10 09:37:12 +00001613 OUT("-1 /* Ambiguous tag (CHOICE?) */");
vlma8a86cc2004-09-10 06:07:18 +00001614 }
vlm060fe2a2004-09-10 09:37:12 +00001615
vlmb2839012004-08-20 13:37:01 +00001616 OUT(",\n");
1617 if(C99_MODE) OUT(".tag_mode = ");
1618 if(expr->tag.tag_class) {
1619 if(expr->tag.tag_mode == TM_IMPLICIT)
1620 OUT("-1,\t/* IMPLICIT tag at current level */\n");
1621 else
1622 OUT("+1,\t/* EXPLICIT tag at current level */\n");
1623 } else {
1624 OUT("0,\n");
1625 }
vlm80a48592005-02-25 12:10:27 +00001626
1627 complex_contents =
1628 (expr->expr_type & ASN_CONSTR_MASK)
1629 || expr->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +00001630 || (0 /* -- prohibited by X.693:8.3.4 */
1631 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00001632 && expr_elements_count(arg, expr));
vlmb2839012004-08-20 13:37:01 +00001633 if(C99_MODE) OUT(".type = ");
vlm766cf1e2005-03-04 08:48:53 +00001634 OUT("(void *)&asn_DEF_");
1635 if(complex_contents) {
1636 OUT("%s", MKID_nc(expr->Identifier));
1637 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
1638 OUT("_%d", expr->_type_unique_index);
vlmdae7f9d2004-08-22 03:25:24 +00001639 } else {
vlm766cf1e2005-03-04 08:48:53 +00001640 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
vlmdae7f9d2004-08-22 03:25:24 +00001641 }
vlm766cf1e2005-03-04 08:48:53 +00001642 OUT(",\n");
vlmb2839012004-08-20 13:37:01 +00001643 if(C99_MODE) OUT(".memb_constraints = ");
1644 if(expr->constraints) {
vlm86f5ed22004-09-26 13:11:31 +00001645 if(arg->flags & A1C_NO_CONSTRAINTS) {
1646 OUT("0,\t/* No check because of -fno-constraints */\n");
1647 } else {
vlmad970212005-03-03 21:58:28 +00001648 char *id = MKID_nc(expr->Identifier);
vlm86f5ed22004-09-26 13:11:31 +00001649 if(expr->_anonymous_type
vlmbf3c5112005-02-14 20:41:29 +00001650 && !strcmp(expr->Identifier, "Member"))
vlm86f5ed22004-09-26 13:11:31 +00001651 id = asn1c_type_name(arg, expr, TNF_SAFE);
1652 OUT("memb_%s_%d_constraint,\n", id,
vlm766cf1e2005-03-04 08:48:53 +00001653 arg->expr->_type_unique_index);
vlm86f5ed22004-09-26 13:11:31 +00001654 }
vlmb2839012004-08-20 13:37:01 +00001655 } else {
vlm95a476e2005-01-17 12:16:58 +00001656 OUT("0,\t/* Defer constraints checking to the member type */\n");
vlmb2839012004-08-20 13:37:01 +00001657 }
1658 if(C99_MODE) OUT(".name = ");
vlmbf3c5112005-02-14 20:41:29 +00001659 if(1) {
1660 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
1661 OUT("\"\"\n");
1662 else
1663 OUT("\"%s\"\n", expr->Identifier);
1664 } else {
1665 OUT("\"%s\"\n", expr->_anonymous_type ? "" : expr->Identifier);
1666 }
vlmb2839012004-08-20 13:37:01 +00001667 OUT("},\n");
1668 INDENT(-1);
1669
vlm86f5ed22004-09-26 13:11:31 +00001670 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlmb2839012004-08-20 13:37:01 +00001671 return 0;
1672
1673 save_target = arg->target->target;
1674 REDIR(OT_CODE);
1675
vlmbf3c5112005-02-14 20:41:29 +00001676 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
vlmb2839012004-08-20 13:37:01 +00001677 p = asn1c_type_name(arg, expr, TNF_SAFE);
vlm39ba4c42004-09-22 16:06:28 +00001678 else
vlmad970212005-03-03 21:58:28 +00001679 p = MKID_nc(expr->Identifier);
vlmb2839012004-08-20 13:37:01 +00001680 OUT("static int\n");
vlm766cf1e2005-03-04 08:48:53 +00001681 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 +00001682 INDENT(+1);
1683 OUT("\t\tasn_app_consume_bytes_f *app_errlog, void *app_key) {\n");
1684 tmp_arg = *arg;
1685 tmp_arg.expr = expr;
1686 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
vlm86f5ed22004-09-26 13:11:31 +00001687 OUT("return td->check_constraints"
1688 "(td, sptr, app_errlog, app_key);\n");
vlmb2839012004-08-20 13:37:01 +00001689 }
1690 INDENT(-1);
1691 OUT("}\n");
1692 OUT("\n");
1693
1694 REDIR(save_target);
1695
1696 return 0;
1697}
vlm4e554992004-08-25 02:03:12 +00001698
vlm9de248e2004-10-20 15:50:55 +00001699/*
1700 * Generate "asn_DEF_XXX" type definition.
1701 */
vlm4e554992004-08-25 02:03:12 +00001702static int
vlm86f5ed22004-09-26 13:11:31 +00001703emit_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 +00001704 int using_type_name = 0;
vlm4e554992004-08-25 02:03:12 +00001705 char *p;
1706
vlm98218602005-03-04 08:52:50 +00001707 OUT("/* %s defined in %s near line %d */\n",
vlm766cf1e2005-03-04 08:48:53 +00001708 MKID_nc(expr->Identifier),
vlm98218602005-03-04 08:52:50 +00001709 a1c_basename(arg->mod->source_file_name), expr->_lineno);
vlm12c8f692004-09-06 08:07:29 +00001710 if(HIDE_INNER_DEFS)
1711 OUT("static /* Use -fall-defs-global to expose */\n");
vlm766cf1e2005-03-04 08:48:53 +00001712 OUT("asn_TYPE_descriptor_t asn_DEF_%s", MKID_nc(expr->Identifier));
1713 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1714 OUT(" = {\n");
vlmaf841972005-01-28 12:18:50 +00001715 p = MKID(expr->Identifier);
vlm72425de2004-09-13 08:31:01 +00001716 INDENT(+1);
vlm4e554992004-08-25 02:03:12 +00001717 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm9de248e2004-10-20 15:50:55 +00001718 OUT("\"%s\",\n", expr->_anonymous_type?"":expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001719
1720 if(expr->expr_type & ASN_CONSTR_MASK) {
vlm766cf1e2005-03-04 08:48:53 +00001721 using_type_name = 1;
vlm4e554992004-08-25 02:03:12 +00001722 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
1723 }
1724
vlm766cf1e2005-03-04 08:48:53 +00001725#define FUNCREF(foo) do { \
1726 OUT("%s", p); \
1727 if(HIDE_INNER_DEFS && !using_type_name) \
1728 OUT("_%d", expr->_type_unique_index); \
1729 OUT("_" #foo ",\n"); \
1730} while(0)
1731
1732 FUNCREF(free);
1733 FUNCREF(print);
1734 FUNCREF(constraint);
1735 FUNCREF(decode_ber);
1736 FUNCREF(encode_der);
1737 FUNCREF(decode_xer);
1738 FUNCREF(encode_xer);
vlm4e554992004-08-25 02:03:12 +00001739
vlm4e554992004-08-25 02:03:12 +00001740 if(expr->expr_type == ASN_CONSTR_CHOICE) {
1741 OUT("CHOICE_outmost_tag,\n");
1742 } else {
1743 OUT("0,\t/* Use generic outmost tag fetcher */\n");
1744 }
1745
vlmad970212005-03-03 21:58:28 +00001746 p = MKID_nc(expr->Identifier);
vlm4e554992004-08-25 02:03:12 +00001747 if(tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001748 OUT("asn_DEF_%s_%d_tags,\n",
1749 p, expr->_type_unique_index);
1750 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1751 p, expr->_type_unique_index);
1752 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0])",
1753 p, expr->_type_unique_index);
vlm72425de2004-09-13 08:31:01 +00001754 if(tv_mode == _TVM_SUBSET
1755 && tags_count != all_tags_count)
1756 OUT(" - %d", all_tags_count - tags_count);
1757 OUT(", /* %d */\n", tags_count);
vlm4e554992004-08-25 02:03:12 +00001758 } else {
vlm72425de2004-09-13 08:31:01 +00001759 OUT("0,\t/* No effective tags (pointer) */\n");
1760 OUT("0,\t/* No effective tags (count) */\n");
1761 }
1762
1763 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
vlm766cf1e2005-03-04 08:48:53 +00001764 OUT("asn_DEF_%s_%d_all_tags,\n",
1765 p, expr->_type_unique_index);
1766 OUT("sizeof(asn_DEF_%s_%d_all_tags)\n",
1767 p, expr->_type_unique_index);
1768 OUT("\t/sizeof(asn_DEF_%s_%d_all_tags[0]), /* %d */\n",
1769 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001770 } else if(all_tags_count) {
vlm766cf1e2005-03-04 08:48:53 +00001771 OUT("asn_DEF_%s_%d_tags,\t/* Same as above */\n",
1772 p, expr->_type_unique_index);
1773 OUT("sizeof(asn_DEF_%s_%d_tags)\n",
1774 p, expr->_type_unique_index);
1775 OUT("\t/sizeof(asn_DEF_%s_%d_tags[0]), /* %d */\n",
1776 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00001777 } else {
1778 OUT("0,\t/* No tags (pointer) */\n");
1779 OUT("0,\t/* No tags (count) */\n");
vlm4e554992004-08-25 02:03:12 +00001780 }
1781
vlm4e554992004-08-25 02:03:12 +00001782 if(elements_count) {
vlm766cf1e2005-03-04 08:48:53 +00001783 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001784 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
1785 || expr->expr_type == ASN_CONSTR_SET_OF) {
1786 OUT("%d,\t/* Single element */\n",
1787 elements_count);
1788 assert(elements_count == 1);
1789 } else {
1790 OUT("%d,\t/* Elements count */\n",
1791 elements_count);
1792 }
1793 } else {
vlm4e554992004-08-25 02:03:12 +00001794 if(expr_elements_count(arg, expr))
1795 OUT("0, 0,\t/* Defined elsewhere */\n");
1796 else
1797 OUT("0, 0,\t/* No members */\n");
1798 }
1799
1800 switch(spec) {
1801 case ETD_NO_SPECIFICS:
1802 OUT("0\t/* No specifics */\n");
1803 break;
1804 case ETD_HAS_SPECIFICS:
vlm766cf1e2005-03-04 08:48:53 +00001805 OUT("&asn_SPC_%s_%d_specs\t/* Additional specs */\n",
1806 p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00001807 }
vlm72425de2004-09-13 08:31:01 +00001808 INDENT(-1);
vlm4e554992004-08-25 02:03:12 +00001809 OUT("};\n");
1810 OUT("\n");
vlme2bb4432004-08-26 06:20:34 +00001811
1812 return 0;
vlm4e554992004-08-25 02:03:12 +00001813}
vlmddd5a7d2004-09-10 09:18:20 +00001814
1815/*
1816 * Check if it is better to make this type indirectly accessed via
1817 * a pointer.
1818 * This may be the case for the following recursive definition:
1819 * Type ::= CHOICE { member Type };
1820 */
1821static int
1822expr_better_indirect(arg_t *arg, asn1p_expr_t *expr) {
1823 asn1p_expr_t *top_parent;
1824 asn1p_expr_t *terminal;
1825
1826 if(expr->expr_type != A1TC_REFERENCE)
1827 return 0;
1828
1829 /* Rewind to the topmost parent expression */
1830 if((top_parent = expr->parent_expr)) {
1831 while(top_parent->parent_expr)
1832 top_parent = top_parent->parent_expr;
1833 } else {
1834 return 0;
1835 }
1836
vlm39ba4c42004-09-22 16:06:28 +00001837 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmddd5a7d2004-09-10 09:18:20 +00001838
1839 return (terminal == top_parent);
1840}
vlm39ba4c42004-09-22 16:06:28 +00001841
1842static int
1843expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
1844 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1845 if(!expr) return 0;
1846
1847 /* X.680, 25.5, Table 5 */
1848 switch(expr->expr_type) {
1849 case ASN_CONSTR_CHOICE:
1850 case ASN_BASIC_BOOLEAN:
1851 case ASN_BASIC_ENUMERATED:
1852 case ASN_BASIC_NULL:
1853 return 1;
1854 default:
1855 return 0;
1856 }
1857}
vlmaf841972005-01-28 12:18:50 +00001858
1859static int
vlm766cf1e2005-03-04 08:48:53 +00001860out_name_chain(arg_t *arg, int check_reserved_keywords) {
vlmaf841972005-01-28 12:18:50 +00001861 asn1p_expr_t *expr = arg->expr;
1862 char *id;
1863
1864 assert(expr->Identifier);
1865
vlm766cf1e2005-03-04 08:48:53 +00001866 if(arg->flags & A1C_COMPOUND_NAMES
1867 && ((expr->expr_type & ASN_CONSTR_MASK)
1868 || expr->expr_type == ASN_BASIC_ENUMERATED
1869 || (expr->expr_type == ASN_BASIC_INTEGER
1870 && expr_elements_count(arg, expr))
1871 )
vlmaf841972005-01-28 12:18:50 +00001872 && expr->parent_expr
1873 && expr->parent_expr->Identifier) {
1874 arg_t tmparg = *arg;
1875
1876 tmparg.expr = expr->parent_expr;
vlm766cf1e2005-03-04 08:48:53 +00001877 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
1878
1879 out_name_chain(&tmparg, 0);
vlmaf841972005-01-28 12:18:50 +00001880
1881 OUT("_"); /* a separator between id components */
vlm766cf1e2005-03-04 08:48:53 +00001882
vlmaf841972005-01-28 12:18:50 +00001883 /* Fall through */
1884 }
1885
1886 if(check_reserved_keywords)
1887 id = MKID(expr->Identifier);
1888 else
vlmad970212005-03-03 21:58:28 +00001889 id = MKID_nc(expr->Identifier);
vlmaf841972005-01-28 12:18:50 +00001890 OUT("%s", id);
1891
1892 return 0;
1893}