blob: a89fbfb89626a17c3c44bf8fbf9854cbe2a8c7ab [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"
vlm337167e2005-11-26 11:25:14 +000010#include <asn1fix_crange.h> /* constraint groker from libasn1fix */
11#include <asn1fix_export.h> /* other exportables from 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
vlm2e774282005-08-14 15:03:31 +000028enum onc_flags {
29 ONC_noflags = 0x00,
30 ONC_avoid_keywords = 0x01,
31 ONC_force_compound_name = 0x02,
32};
33static int out_name_chain(arg_t *arg, enum onc_flags);
vlmfa67ddc2004-06-03 03:38:44 +000034static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg);
35static int asn1c_lang_C_type_SET_def(arg_t *arg);
36static int asn1c_lang_C_type_CHOICE_def(arg_t *arg);
37static int asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of);
vlma8a86cc2004-09-10 06:07:18 +000038static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p);
vlm337167e2005-11-26 11:25:14 +000039static int compute_extensions_start(asn1p_expr_t *expr);
vlm5a6fc652005-08-16 17:00:21 +000040static int expr_break_recursion(arg_t *arg, asn1p_expr_t *expr);
vlm39ba4c42004-09-22 16:06:28 +000041static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr);
vlm4e554992004-08-25 02:03:12 +000042static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr);
vlm337167e2005-11-26 11:25:14 +000043static int emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, char *type);
44static int emit_single_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr);
45static int emit_members_PER_constraints(arg_t *arg);
vlm4e554992004-08-25 02:03:12 +000046static int emit_member_table(arg_t *arg, asn1p_expr_t *expr);
vlm940bc6b2004-10-03 09:13:30 +000047static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier);
vlm0b567bf2005-03-04 22:18:20 +000048static int emit_include_dependencies(arg_t *arg);
vlm6c4a8502005-08-18 13:38:19 +000049static asn1p_expr_t *terminal_structable(arg_t *arg, asn1p_expr_t *expr);
50static int expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr);
51static int asn1c_recurse(arg_t *arg, asn1p_expr_t *expr, int (*callback)(arg_t *arg, void *key), void *key);
vlm337167e2005-11-26 11:25:14 +000052static asn1p_expr_type_e expr_get_type(arg_t *arg, asn1p_expr_t *expr);
53static int try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out);
54static int *compute_canonical_members_order(arg_t *arg, int el_count);
vlmfa67ddc2004-06-03 03:38:44 +000055
vlm72425de2004-09-13 08:31:01 +000056enum tvm_compat {
57 _TVM_SAME = 0, /* tags and all_tags are same */
58 _TVM_SUBSET = 1, /* tags are subset of all_tags */
59 _TVM_DIFFERENT = 2, /* tags and all_tags are different */
60};
61static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc);
62
vlm4e554992004-08-25 02:03:12 +000063enum etd_spec {
64 ETD_NO_SPECIFICS,
65 ETD_HAS_SPECIFICS
66};
vlm86f5ed22004-09-26 13:11:31 +000067static 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 +000068
vlmb2839012004-08-20 13:37:01 +000069#define C99_MODE (!(arg->flags & A1C_NO_C99))
vlmfa67ddc2004-06-03 03:38:44 +000070#define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS)
vlm12c8f692004-09-06 08:07:29 +000071#define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL))
vlmfa67ddc2004-06-03 03:38:44 +000072
73#define PCTX_DEF INDENTED( \
74 OUT("\n"); \
75 OUT("/* Context for parsing across buffer boundaries */\n"); \
vlma5dcb912004-09-29 13:16:40 +000076 OUT("asn_struct_ctx_t _asn_ctx;\n"));
vlmfa67ddc2004-06-03 03:38:44 +000077
vlm0b567bf2005-03-04 22:18:20 +000078
vlm33a4ff12004-08-11 05:21:32 +000079#define DEPENDENCIES do { \
vlm0b567bf2005-03-04 22:18:20 +000080 emit_include_dependencies(arg); \
vlm33a4ff12004-08-11 05:21:32 +000081 if(expr->expr_type == ASN_CONSTR_SET_OF) \
82 GEN_INCLUDE("asn_SET_OF"); \
83 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \
84 GEN_INCLUDE("asn_SEQUENCE_OF"); \
85} while(0)
vlmfa67ddc2004-06-03 03:38:44 +000086
vlm1f7df782005-03-04 23:48:19 +000087/* MKID_safe() without checking for reserved keywords */
vlm0c6d3812006-03-21 03:40:38 +000088#define MKID(expr) (asn1c_make_identifier(0, expr, 0))
89#define MKID_safe(expr) (asn1c_make_identifier(AMI_CHECK_RESERVED, expr, 0))
vlmfa67ddc2004-06-03 03:38:44 +000090
91int
vlmf9d178d2004-09-14 12:47:45 +000092asn1c_lang_C_type_REAL(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +000093 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
94}
95
vlm80a48592005-02-25 12:10:27 +000096struct value2enum {
97 asn1c_integer_t value;
98 const char *name;
99 int idx;
100};
101static int compar_enumMap_byName(const void *ap, const void *bp) {
102 const struct value2enum *a = (const struct value2enum *)ap;
103 const struct value2enum *b = (const struct value2enum *)bp;
104 return strcmp(a->name, b->name);
105}
106static int compar_enumMap_byValue(const void *ap, const void *bp) {
107 const struct value2enum *a = (const struct value2enum *)ap;
108 const struct value2enum *b = (const struct value2enum *)bp;
109 if(a->value < b->value)
110 return -1;
111 else if(a->value == b->value)
112 return 0;
113 return 1;
114}
115
vlmfa67ddc2004-06-03 03:38:44 +0000116int
vlmf9d178d2004-09-14 12:47:45 +0000117asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000118 asn1p_expr_t *expr = arg->expr;
119 asn1p_expr_t *v;
vlm80a48592005-02-25 12:10:27 +0000120 int el_count = expr_elements_count(arg, expr);
121 struct value2enum *v2e;
vlm337167e2005-11-26 11:25:14 +0000122 int map_extensions = (expr->expr_type == ASN_BASIC_INTEGER);
vlm4573af02005-08-13 23:30:24 +0000123 int eidx;
vlmfa67ddc2004-06-03 03:38:44 +0000124
vlm80a48592005-02-25 12:10:27 +0000125 v2e = alloca((el_count + 1) * sizeof(*v2e));
vlmfa67ddc2004-06-03 03:38:44 +0000126
vlm80a48592005-02-25 12:10:27 +0000127 /*
vlm4573af02005-08-13 23:30:24 +0000128 * For all ENUMERATED types and for those INTEGER types which
129 * have identifiers, print out an enumeration table.
vlm80a48592005-02-25 12:10:27 +0000130 */
vlm4573af02005-08-13 23:30:24 +0000131 if(expr->expr_type == ASN_BASIC_ENUMERATED || el_count) {
132 eidx = 0;
vlm80a48592005-02-25 12:10:27 +0000133 REDIR(OT_DEPS);
vlm766cf1e2005-03-04 08:48:53 +0000134 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000135 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000136 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000137 TQ_FOR(v, &(expr->members), next) {
138 switch(v->expr_type) {
139 case A1TC_UNIVERVAL:
vlm766cf1e2005-03-04 08:48:53 +0000140 OUT("\t");
vlm2e774282005-08-14 15:03:31 +0000141 out_name_chain(arg, ONC_noflags);
vlm0c6d3812006-03-21 03:40:38 +0000142 OUT("_%s", MKID(v));
vlm766cf1e2005-03-04 08:48:53 +0000143 OUT("\t= %" PRIdASN "%s\n",
vlm80a48592005-02-25 12:10:27 +0000144 v->value->value.v_integer,
vlm766cf1e2005-03-04 08:48:53 +0000145 (eidx+1 < el_count) ? "," : "");
vlm80a48592005-02-25 12:10:27 +0000146 v2e[eidx].name = v->Identifier;
147 v2e[eidx].value = v->value->value.v_integer;
148 eidx++;
vlmfa67ddc2004-06-03 03:38:44 +0000149 break;
vlmf9d178d2004-09-14 12:47:45 +0000150 case A1TC_EXTENSIBLE:
151 OUT("\t/*\n");
152 OUT("\t * Enumeration is extensible\n");
153 OUT("\t */\n");
vlm337167e2005-11-26 11:25:14 +0000154 if(!map_extensions)
155 map_extensions = eidx + 1;
vlmf9d178d2004-09-14 12:47:45 +0000156 break;
vlmfa67ddc2004-06-03 03:38:44 +0000157 default:
158 return -1;
159 }
160 }
vlm7c8aa5e2006-03-21 07:25:18 +0000161 OUT("} e_");
vlm2e774282005-08-14 15:03:31 +0000162 out_name_chain(arg, ONC_noflags);
vlm7c8aa5e2006-03-21 07:25:18 +0000163 OUT(";\n");
vlm80a48592005-02-25 12:10:27 +0000164 assert(eidx == el_count);
vlm4573af02005-08-13 23:30:24 +0000165 }
166
167 /*
168 * For all ENUMERATED types print out a mapping table
169 * between identifiers and associated values.
170 * This is prohibited for INTEGER types by by X.693:8.3.4.
171 */
172 if(expr->expr_type == ASN_BASIC_ENUMERATED) {
vlm80a48592005-02-25 12:10:27 +0000173
174 /*
175 * Generate a enumerationName<->value map for XER codec.
176 */
177 REDIR(OT_STAT_DEFS);
178
vlm337167e2005-11-26 11:25:14 +0000179 OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_value2enum_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000180 MKID(expr), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000181 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
182 for(eidx = 0; eidx < el_count; eidx++) {
183 v2e[eidx].idx = eidx;
184 OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n",
185 v2e[eidx].value,
186 (long)strlen(v2e[eidx].name), v2e[eidx].name,
187 (eidx + 1 < el_count) ? "," : "");
188 }
vlm337167e2005-11-26 11:25:14 +0000189 if(map_extensions)
vlm80a48592005-02-25 12:10:27 +0000190 OUT("\t/* This list is extensible */\n");
191 OUT("};\n");
192
vlm337167e2005-11-26 11:25:14 +0000193 OUT("static unsigned int asn_MAP_%s_enum2value_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000194 MKID(expr), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000195 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
196 for(eidx = 0; eidx < el_count; eidx++) {
197 OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n",
198 v2e[eidx].idx,
199 (eidx + 1 < el_count) ? "," : "",
200 v2e[eidx].name, v2e[eidx].value);
201 }
vlm337167e2005-11-26 11:25:14 +0000202 if(map_extensions)
vlm80a48592005-02-25 12:10:27 +0000203 OUT("\t/* This list is extensible */\n");
204 OUT("};\n");
205
vlm337167e2005-11-26 11:25:14 +0000206 OUT("static asn_INTEGER_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000207 MKID(expr), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000208 INDENT(+1);
vlm337167e2005-11-26 11:25:14 +0000209 OUT("asn_MAP_%s_value2enum_%d,\t"
vlm80a48592005-02-25 12:10:27 +0000210 "/* \"tag\" => N; sorted by tag */\n",
vlm0c6d3812006-03-21 03:40:38 +0000211 MKID(expr),
vlm766cf1e2005-03-04 08:48:53 +0000212 expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +0000213 OUT("asn_MAP_%s_enum2value_%d,\t"
vlm80a48592005-02-25 12:10:27 +0000214 "/* N => \"tag\"; sorted by N */\n",
vlm0c6d3812006-03-21 03:40:38 +0000215 MKID(expr),
vlm766cf1e2005-03-04 08:48:53 +0000216 expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000217 OUT("%d,\t/* Number of elements in the maps */\n",
218 el_count);
vlm337167e2005-11-26 11:25:14 +0000219 if(map_extensions) {
220 OUT("%d,\t/* Extensions before this member */\n",
221 map_extensions);
222 } else {
223 OUT("0,\t/* Enumeration is not extensible */\n");
224 }
vlm80a48592005-02-25 12:10:27 +0000225 if(expr->expr_type == ASN_BASIC_ENUMERATED)
226 OUT("1\t/* Strict enumeration */\n");
227 else
228 OUT("0\n");
229 INDENT(-1);
230 OUT("};\n");
vlmfa67ddc2004-06-03 03:38:44 +0000231 }
232
233 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
234}
235
236int
vlm96853d82005-08-13 23:51:47 +0000237asn1c_lang_C_type_BIT_STRING(arg_t *arg) {
238 asn1p_expr_t *expr = arg->expr;
239 asn1p_expr_t *v;
240 int el_count = expr_elements_count(arg, expr);
vlm96853d82005-08-13 23:51:47 +0000241
242 if(el_count) {
vlm75b3a532005-08-14 02:18:27 +0000243 int eidx = 0;
vlm96853d82005-08-13 23:51:47 +0000244 REDIR(OT_DEPS);
245 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000246 out_name_chain(arg, ONC_avoid_keywords);
vlm96853d82005-08-13 23:51:47 +0000247 OUT(" {\n");
248 TQ_FOR(v, &(expr->members), next) {
vlm75b3a532005-08-14 02:18:27 +0000249 eidx++;
250 if(v->expr_type != A1TC_UNIVERVAL) {
vlm96853d82005-08-13 23:51:47 +0000251 OUT("/* Unexpected BIT STRING element: %s */\n",
252 v->Identifier);
vlm75b3a532005-08-14 02:18:27 +0000253 continue;
vlm96853d82005-08-13 23:51:47 +0000254 }
vlm75b3a532005-08-14 02:18:27 +0000255 OUT("\t");
vlm2e774282005-08-14 15:03:31 +0000256 out_name_chain(arg, ONC_noflags);
vlm0c6d3812006-03-21 03:40:38 +0000257 OUT("_%s", MKID(v));
vlm75b3a532005-08-14 02:18:27 +0000258 OUT("\t= %" PRIdASN "%s\n",
259 v->value->value.v_integer,
260 (eidx < el_count) ? "," : "");
vlm96853d82005-08-13 23:51:47 +0000261 }
vlm7c8aa5e2006-03-21 07:25:18 +0000262 OUT("} e_");
vlm2e774282005-08-14 15:03:31 +0000263 out_name_chain(arg, ONC_noflags);
vlm7c8aa5e2006-03-21 07:25:18 +0000264 OUT(";\n");
vlm96853d82005-08-13 23:51:47 +0000265 assert(eidx == el_count);
266 }
267
268 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
269}
270
271int
vlmfa67ddc2004-06-03 03:38:44 +0000272asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
273 asn1p_expr_t *expr = arg->expr;
274 asn1p_expr_t *v;
275 int comp_mode = 0; /* {root,ext=1,root,root,...} */
276
277 DEPENDENCIES;
278
279 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000280 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000281 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000282 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000283 } else {
284 OUT("typedef struct %s {\n",
vlm0c6d3812006-03-21 03:40:38 +0000285 MKID_safe(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000286 }
287
288 TQ_FOR(v, &(expr->members), next) {
vlm5a6fc652005-08-16 17:00:21 +0000289 if(v->expr_type == A1TC_EXTENSIBLE)
vlmfa67ddc2004-06-03 03:38:44 +0000290 if(comp_mode < 3) comp_mode++;
vlm5a6fc652005-08-16 17:00:21 +0000291 if(comp_mode == 1)
vlm337167e2005-11-26 11:25:14 +0000292 v->marker.flags |= EM_OMITABLE | EM_INDIRECT;
293 try_inline_default(arg, v, 1);
vlmfa67ddc2004-06-03 03:38:44 +0000294 EMBED(v);
295 }
296
297 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000298 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000299 expr->_anonymous_type ? "" :
300 arg->embed
vlm0c6d3812006-03-21 03:40:38 +0000301 ? MKID_safe(expr)
302 : MKID(expr),
vlmdae7f9d2004-08-22 03:25:24 +0000303 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000304
305 return asn1c_lang_C_type_SEQUENCE_def(arg);
306}
307
308static int
309asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
310 asn1p_expr_t *expr = arg->expr;
311 asn1p_expr_t *v;
312 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000313 int ext_start = -1;
314 int ext_stop = -1;
vlm4e03ce22004-06-06 07:20:17 +0000315 tag2el_t *tag2el = NULL;
316 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000317 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000318 int all_tags_count;
319 enum tvm_compat tv_mode;
vlm337167e2005-11-26 11:25:14 +0000320 int roms_count; /* Root optional members */
321 int aoms_count; /* Additions optional members */
vlmfa67ddc2004-06-03 03:38:44 +0000322
vlm4e03ce22004-06-06 07:20:17 +0000323 /*
324 * Fetch every inner tag from the tag to elements map.
325 */
vlm940bc6b2004-10-03 09:13:30 +0000326 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlm4e03ce22004-06-06 07:20:17 +0000327 if(tag2el) free(tag2el);
328 return -1;
329 }
330
vlm33a4ff12004-08-11 05:21:32 +0000331 GEN_INCLUDE("constr_SEQUENCE");
332 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000333 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000334
vlm33a4ff12004-08-11 05:21:32 +0000335 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000336
337 /*
vlm337167e2005-11-26 11:25:14 +0000338 * Print out the table according to which parsing is performed.
vlmfa67ddc2004-06-03 03:38:44 +0000339 */
vlm39ba4c42004-09-22 16:06:28 +0000340 if(expr_elements_count(arg, expr)) {
341 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000342
vlm337167e2005-11-26 11:25:14 +0000343 if(emit_members_PER_constraints(arg))
344 return -1;
vlm766cf1e2005-03-04 08:48:53 +0000345 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000346 MKID(expr), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000347
348 elements = 0;
vlm337167e2005-11-26 11:25:14 +0000349 roms_count = 0;
350 aoms_count = 0;
vlm39ba4c42004-09-22 16:06:28 +0000351 INDENTED(TQ_FOR(v, &(expr->members), next) {
352 if(v->expr_type == A1TC_EXTENSIBLE) {
353 if((++comp_mode) == 1)
354 ext_start = elements - 1;
355 else
356 ext_stop = elements - 1;
357 continue;
358 }
vlm337167e2005-11-26 11:25:14 +0000359 if(v->marker.flags & EM_OMITABLE)
360 comp_mode == 1 ? ++aoms_count : ++roms_count;
vlm39ba4c42004-09-22 16:06:28 +0000361 emit_member_table(arg, v);
vlm5a6fc652005-08-16 17:00:21 +0000362 elements++;
vlm39ba4c42004-09-22 16:06:28 +0000363 });
364 OUT("};\n");
vlm337167e2005-11-26 11:25:14 +0000365
366 if((roms_count + aoms_count) && (arg->flags & A1C_GEN_PER)) {
367 int elm = 0;
368 int comma = 0;
369 comp_mode = 0;
370 OUT("static int asn_MAP_%s_oms_%d[] = {",
vlm0c6d3812006-03-21 03:40:38 +0000371 MKID(expr),
vlm337167e2005-11-26 11:25:14 +0000372 expr->_type_unique_index);
373 TQ_FOR(v, &(expr->members), next) {
374 if(v->expr_type == A1TC_EXTENSIBLE) {
375 ++comp_mode;
376 continue;
377 }
378 if((v->marker.flags & EM_OMITABLE)
379 && comp_mode != 1) {
380 if(!comma) comma++;
381 else OUT(",");
382 OUT(" %d", elm);
383 }
384 ++elm;
385 }
386 elm = 0;
387 comp_mode = 0;
388 TQ_FOR(v, &(expr->members), next) {
389 if(v->expr_type == A1TC_EXTENSIBLE) {
390 ++comp_mode;
391 continue;
392 }
393 if((v->marker.flags & EM_OMITABLE)
394 && comp_mode == 1) {
395 if(!comma) comma++;
396 else OUT(",");
397 OUT(" %d", elm);
398 }
399 ++elm;
400 }
401 OUT(" };\n");
402 if(roms_count > 65536)
403 FATAL("Too many optional elements in %s "
404 "at line %d!",
405 arg->expr->Identifier,
406 arg->expr->_lineno);
407 } else {
408 roms_count = 0;
409 aoms_count = 0;
410 }
vlm39ba4c42004-09-22 16:06:28 +0000411 } else {
412 elements = 0;
vlm337167e2005-11-26 11:25:14 +0000413 roms_count = 0;
414 aoms_count = 0;
vlm39ba4c42004-09-22 16:06:28 +0000415 }
vlmfa67ddc2004-06-03 03:38:44 +0000416
vlm4a3f5822004-06-28 21:13:46 +0000417 /*
vlma5dcb912004-09-29 13:16:40 +0000418 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000419 */
vlm72425de2004-09-13 08:31:01 +0000420 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000421
vlm4e03ce22004-06-06 07:20:17 +0000422 /*
423 * Tags to elements map.
424 */
vlm940bc6b2004-10-03 09:13:30 +0000425 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlm4e03ce22004-06-06 07:20:17 +0000426
vlm337167e2005-11-26 11:25:14 +0000427 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000428 MKID(expr), expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +0000429 INDENT(+1);
430 OUT("sizeof(struct ");
431 out_name_chain(arg, ONC_avoid_keywords); OUT("),\n");
432 OUT("offsetof(struct ");
433 out_name_chain(arg, ONC_avoid_keywords); OUT(", _asn_ctx),\n");
vlm766cf1e2005-03-04 08:48:53 +0000434
vlm337167e2005-11-26 11:25:14 +0000435 if(tag2el_count) {
436 OUT("asn_MAP_%s_tag2el_%d,\n",
vlm0c6d3812006-03-21 03:40:38 +0000437 MKID(expr),
vlm337167e2005-11-26 11:25:14 +0000438 expr->_type_unique_index);
439 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
440 } else {
441 OUT("0,\t/* No top level tags */\n");
442 OUT("0,\t/* No tags in the map */\n");
443 }
444 if(roms_count + aoms_count) {
445 OUT("asn_MAP_%s_oms_%d,\t/* Optional members */\n",
vlm0c6d3812006-03-21 03:40:38 +0000446 MKID(expr), expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +0000447 OUT("%d, %d,\t/* Root/Additions */\n", roms_count, aoms_count);
448 } else {
449 OUT("0, 0, 0,\t/* Optional elements (not needed) */\n");
450 }
451 OUT("%d,\t/* Start extensions */\n",
vlmfa67ddc2004-06-03 03:38:44 +0000452 ext_start);
vlm337167e2005-11-26 11:25:14 +0000453 OUT("%d\t/* Stop extensions */\n",
vlmfa67ddc2004-06-03 03:38:44 +0000454 (ext_stop<ext_start)?elements+1:ext_stop, ext_stop);
vlm337167e2005-11-26 11:25:14 +0000455 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000456 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000457
458 /*
vlma5dcb912004-09-29 13:16:40 +0000459 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000460 */
vlm72425de2004-09-13 08:31:01 +0000461 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000462 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000463
vlmfa67ddc2004-06-03 03:38:44 +0000464 REDIR(OT_TYPE_DECLS);
465
466 return 0;
vlmedf203f2005-01-17 11:57:48 +0000467} /* _SEQUENCE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000468
469int
vlmfa67ddc2004-06-03 03:38:44 +0000470asn1c_lang_C_type_SET(arg_t *arg) {
471 asn1p_expr_t *expr = arg->expr;
472 asn1p_expr_t *v;
473 long mcount;
474 char *id;
475 int comp_mode = 0; /* {root,ext=1,root,root,...} */
476
477 DEPENDENCIES;
478
479 REDIR(OT_DEPS);
480
481 OUT("\n");
482 OUT("/*\n");
483 OUT(" * Method of determining the components presence\n");
484 OUT(" */\n");
485 mcount = 0;
vlm766cf1e2005-03-04 08:48:53 +0000486 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000487 out_name_chain(arg, ONC_noflags);
vlm766cf1e2005-03-04 08:48:53 +0000488 OUT("_PR {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000489 TQ_FOR(v, &(expr->members), next) {
490 if(v->expr_type == A1TC_EXTENSIBLE) continue;
491 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000492 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000493 OUT("_PR_");
vlm0c6d3812006-03-21 03:40:38 +0000494 id = MKID(v);
vlmfa67ddc2004-06-03 03:38:44 +0000495 OUT("%s,\t/* Member %s is present */\n",
496 id, id)
497 );
498 mcount++;
499 }
vlm2e774282005-08-14 15:03:31 +0000500 OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n");
vlmfa67ddc2004-06-03 03:38:44 +0000501
502 REDIR(OT_TYPE_DECLS);
503
504 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000505 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000506 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000507 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000508 } else {
vlm337167e2005-11-26 11:25:14 +0000509 OUT("typedef struct %s {\n",
vlm0c6d3812006-03-21 03:40:38 +0000510 MKID_safe(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000511 }
512
513 TQ_FOR(v, &(expr->members), next) {
vlm337167e2005-11-26 11:25:14 +0000514 if(v->expr_type == A1TC_EXTENSIBLE)
vlmfa67ddc2004-06-03 03:38:44 +0000515 if(comp_mode < 3) comp_mode++;
vlm5a6fc652005-08-16 17:00:21 +0000516 if(comp_mode == 1)
vlm337167e2005-11-26 11:25:14 +0000517 v->marker.flags |= EM_OMITABLE | EM_INDIRECT;
518 try_inline_default(arg, v, 1);
vlmfa67ddc2004-06-03 03:38:44 +0000519 EMBED(v);
520 }
521
522 INDENTED(
vlm0c6d3812006-03-21 03:40:38 +0000523 id = MKID(expr);
vlmfa67ddc2004-06-03 03:38:44 +0000524 OUT("\n");
525 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
526 id, id);
527 OUT("unsigned int _presence_map\n");
528 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
529 );
530
531 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000532 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm0c6d3812006-03-21 03:40:38 +0000533 expr->_anonymous_type ? "" : MKID_safe(expr),
vlmdae7f9d2004-08-22 03:25:24 +0000534 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000535
536 return asn1c_lang_C_type_SET_def(arg);
537}
538
vlmfa67ddc2004-06-03 03:38:44 +0000539static int
540asn1c_lang_C_type_SET_def(arg_t *arg) {
541 asn1p_expr_t *expr = arg->expr;
542 asn1p_expr_t *v;
543 int elements;
vlmfa67ddc2004-06-03 03:38:44 +0000544 tag2el_t *tag2el = NULL;
545 int tag2el_count = 0;
vlm940bc6b2004-10-03 09:13:30 +0000546 tag2el_t *tag2el_cxer = NULL;
547 int tag2el_cxer_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000548 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000549 int all_tags_count;
550 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000551 char *p;
552
553 /*
554 * Fetch every inner tag from the tag to elements map.
555 */
vlm940bc6b2004-10-03 09:13:30 +0000556 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000557 if(tag2el) free(tag2el);
558 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000559 }
vlm940bc6b2004-10-03 09:13:30 +0000560 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
561 if(tag2el) free(tag2el);
562 if(tag2el_cxer) free(tag2el_cxer);
563 return -1;
564 }
565 if(tag2el_cxer_count == tag2el_count
566 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
567 free(tag2el_cxer);
568 tag2el_cxer = 0;
569 }
vlmfa67ddc2004-06-03 03:38:44 +0000570
vlm33a4ff12004-08-11 05:21:32 +0000571 GEN_INCLUDE("constr_SET");
572 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000573 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000574
575 REDIR(OT_STAT_DEFS);
576
vlmfa67ddc2004-06-03 03:38:44 +0000577 /*
vlm337167e2005-11-26 11:25:14 +0000578 * Print out the table according to which parsing is performed.
vlmfa67ddc2004-06-03 03:38:44 +0000579 */
vlm39ba4c42004-09-22 16:06:28 +0000580 if(expr_elements_count(arg, expr)) {
581 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000582
vlm337167e2005-11-26 11:25:14 +0000583 if(emit_members_PER_constraints(arg))
584 return -1;
vlm766cf1e2005-03-04 08:48:53 +0000585 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000586 MKID(expr), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000587
588 elements = 0;
589 INDENTED(TQ_FOR(v, &(expr->members), next) {
590 if(v->expr_type == A1TC_EXTENSIBLE) {
591 if(comp_mode < 3) comp_mode++;
592 } else {
vlm39ba4c42004-09-22 16:06:28 +0000593 emit_member_table(arg, v);
vlm5a6fc652005-08-16 17:00:21 +0000594 elements++;
vlm39ba4c42004-09-22 16:06:28 +0000595 }
596 });
597 OUT("};\n");
598 } else {
599 elements = 0;
600 }
vlmfa67ddc2004-06-03 03:38:44 +0000601
vlm4a3f5822004-06-28 21:13:46 +0000602 /*
vlma5dcb912004-09-29 13:16:40 +0000603 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000604 */
vlm72425de2004-09-13 08:31:01 +0000605 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000606
607 /*
608 * Tags to elements map.
609 */
vlm940bc6b2004-10-03 09:13:30 +0000610 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
611 if(tag2el_cxer)
612 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
vlmfa67ddc2004-06-03 03:38:44 +0000613
614 /*
615 * Emit a map of mandatory elements.
616 */
vlm337167e2005-11-26 11:25:14 +0000617 OUT("static uint8_t asn_MAP_%s_mmap_%d",
vlm0c6d3812006-03-21 03:40:38 +0000618 MKID(expr), expr->_type_unique_index);
619 p = MKID_safe(expr);
vlmfa67ddc2004-06-03 03:38:44 +0000620 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
vlm766cf1e2005-03-04 08:48:53 +0000621 OUT(" = {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000622 INDENTED(
623 if(elements) {
vlmfa67ddc2004-06-03 03:38:44 +0000624 int el = 0;
625 TQ_FOR(v, &(expr->members), next) {
626 if(v->expr_type == A1TC_EXTENSIBLE) continue;
vlme2873192005-10-05 08:59:03 +0000627 if(el) {
628 if((el % 8) == 0)
629 OUT(",\n");
630 else
631 OUT(" | ");
vlmfa67ddc2004-06-03 03:38:44 +0000632 }
vlmddd5a7d2004-09-10 09:18:20 +0000633 OUT("(%d << %d)",
vlm6c4a8502005-08-18 13:38:19 +0000634 (v->marker.flags & EM_OMITABLE) != EM_OMITABLE,
vlmddd5a7d2004-09-10 09:18:20 +0000635 7 - (el % 8));
vlmfa67ddc2004-06-03 03:38:44 +0000636 el++;
637 }
638 } else {
639 OUT("0");
640 }
641 );
642 OUT("\n");
643 OUT("};\n");
644
vlm337167e2005-11-26 11:25:14 +0000645 OUT("static asn_SET_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000646 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000647 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000648 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000649 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000650 OUT("),\n");
651 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000652 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000653 OUT(", _asn_ctx),\n");
654 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000655 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000656 OUT(", _presence_map),\n");
vlm0c6d3812006-03-21 03:40:38 +0000657 p = MKID(expr);
vlm337167e2005-11-26 11:25:14 +0000658 OUT("asn_MAP_%s_tag2el_%d,\n", p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000659 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm940bc6b2004-10-03 09:13:30 +0000660 if(tag2el_cxer)
vlm337167e2005-11-26 11:25:14 +0000661 OUT("asn_MAP_%s_tag2el_cxer_%d,\n",
vlm766cf1e2005-03-04 08:48:53 +0000662 p, expr->_type_unique_index);
vlm940bc6b2004-10-03 09:13:30 +0000663 else
vlm337167e2005-11-26 11:25:14 +0000664 OUT("asn_MAP_%s_tag2el_%d,\t/* Same as above */\n",
vlm766cf1e2005-03-04 08:48:53 +0000665 p, expr->_type_unique_index);
666 OUT("%d,\t/* Count of tags in the CXER map */\n",
667 tag2el_cxer_count);
vlm79b08d52004-07-01 00:52:50 +0000668 OUT("%d,\t/* Whether extensible */\n",
vlm337167e2005-11-26 11:25:14 +0000669 compute_extensions_start(expr) == -1 ? 0 : 1);
670 OUT("(unsigned int *)asn_MAP_%s_mmap_%d\t/* Mandatory elements map */\n",
vlm766cf1e2005-03-04 08:48:53 +0000671 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000672 );
673 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000674
675 /*
vlma5dcb912004-09-29 13:16:40 +0000676 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000677 */
vlm72425de2004-09-13 08:31:01 +0000678 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000679 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000680
vlmfa67ddc2004-06-03 03:38:44 +0000681 REDIR(OT_TYPE_DECLS);
682
683 return 0;
vlmedf203f2005-01-17 11:57:48 +0000684} /* _SET_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000685
686int
vlmdae7f9d2004-08-22 03:25:24 +0000687asn1c_lang_C_type_SEx_OF(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000688 asn1p_expr_t *expr = arg->expr;
vlm0b567bf2005-03-04 22:18:20 +0000689 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
vlmfa67ddc2004-06-03 03:38:44 +0000690
691 DEPENDENCIES;
692
693 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000694 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000695 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000696 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000697 } else {
vlm0c6d3812006-03-21 03:40:38 +0000698 OUT("typedef struct %s {\n", MKID_safe(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000699 }
700
vlmdae7f9d2004-08-22 03:25:24 +0000701 INDENT(+1);
702 OUT("A_%s_OF(",
703 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
704 ? "SET" : "SEQUENCE");
vlm5a6fc652005-08-16 17:00:21 +0000705
706 /*
707 * README README
708 * The implementation of the A_SET_OF() macro is already indirect.
709 */
710 memb->marker.flags |= EM_INDIRECT;
711
vlm80a48592005-02-25 12:10:27 +0000712 if(memb->expr_type & ASN_CONSTR_MASK
713 || ((memb->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +0000714 || (0 /* -- prohibited by X.693:8.3.4 */
715 && memb->expr_type == ASN_BASIC_INTEGER))
716 && expr_elements_count(arg, memb))) {
vlmdae7f9d2004-08-22 03:25:24 +0000717 arg_t tmp;
718 asn1p_expr_t tmp_memb;
719 arg->embed++;
720 tmp = *arg;
721 tmp.expr = &tmp_memb;
722 tmp_memb = *memb;
vlm5a6fc652005-08-16 17:00:21 +0000723 tmp_memb.marker.flags &= ~EM_INDIRECT;
vlmdae7f9d2004-08-22 03:25:24 +0000724 tmp_memb._anonymous_type = 1;
vlm39ba4c42004-09-22 16:06:28 +0000725 if(tmp_memb.Identifier == 0) {
vlm766cf1e2005-03-04 08:48:53 +0000726 tmp_memb.Identifier = "Member";
727 if(0)
vlm39ba4c42004-09-22 16:06:28 +0000728 tmp_memb.Identifier = strdup(
729 asn1c_make_identifier(0,
vlm0c6d3812006-03-21 03:40:38 +0000730 expr, "Member", 0));
vlm39ba4c42004-09-22 16:06:28 +0000731 assert(tmp_memb.Identifier);
732 }
vlmdae7f9d2004-08-22 03:25:24 +0000733 tmp.default_cb(&tmp);
vlm39ba4c42004-09-22 16:06:28 +0000734 if(tmp_memb.Identifier != memb->Identifier)
vlm766cf1e2005-03-04 08:48:53 +0000735 if(0) free(tmp_memb.Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000736 arg->embed--;
737 assert(arg->target->target == OT_TYPE_DECLS);
738 } else {
vlm6c5d5e52005-03-04 22:38:22 +0000739 OUT("%s", asn1c_type_name(arg, memb,
740 (memb->marker.flags & EM_UNRECURSE)
741 ? TNF_RSAFE : TNF_CTYPE));
vlmfa67ddc2004-06-03 03:38:44 +0000742 }
vlm5a6fc652005-08-16 17:00:21 +0000743 /* README README (above) */
744 if(0 && (memb->marker.flags & EM_INDIRECT))
745 OUT(" *");
vlmdae7f9d2004-08-22 03:25:24 +0000746 OUT(") list;\n");
747 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000748
749 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000750 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm0c6d3812006-03-21 03:40:38 +0000751 expr->_anonymous_type ? "" : MKID_safe(expr),
vlmdae7f9d2004-08-22 03:25:24 +0000752 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000753
754 /*
vlmdae7f9d2004-08-22 03:25:24 +0000755 * SET OF/SEQUENCE OF definition
vlmfa67ddc2004-06-03 03:38:44 +0000756 */
vlmdae7f9d2004-08-22 03:25:24 +0000757 return asn1c_lang_C_type_SEx_OF_def(arg,
758 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
vlmfa67ddc2004-06-03 03:38:44 +0000759}
760
761static int
762asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
763 asn1p_expr_t *expr = arg->expr;
764 asn1p_expr_t *v;
vlm6e73a042004-08-11 07:17:22 +0000765 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000766 int all_tags_count;
767 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000768
vlm33a4ff12004-08-11 05:21:32 +0000769 /*
vlm337167e2005-11-26 11:25:14 +0000770 * Print out the table according to which parsing is performed.
vlm33a4ff12004-08-11 05:21:32 +0000771 */
vlmfa67ddc2004-06-03 03:38:44 +0000772 if(seq_of) {
vlm33a4ff12004-08-11 05:21:32 +0000773 GEN_INCLUDE("constr_SEQUENCE_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000774 } else {
vlm33a4ff12004-08-11 05:21:32 +0000775 GEN_INCLUDE("constr_SET_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000776 }
vlm33a4ff12004-08-11 05:21:32 +0000777 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000778 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000779
780 REDIR(OT_STAT_DEFS);
781
782 /*
vlm337167e2005-11-26 11:25:14 +0000783 * Print out the table according to which parsing is performed.
vlmfa67ddc2004-06-03 03:38:44 +0000784 */
vlm337167e2005-11-26 11:25:14 +0000785 if(emit_members_PER_constraints(arg))
786 return -1;
vlm766cf1e2005-03-04 08:48:53 +0000787 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000788 MKID(expr), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000789 INDENT(+1);
vlmfa67ddc2004-06-03 03:38:44 +0000790 v = TQ_FIRST(&(expr->members));
vlm39ba4c42004-09-22 16:06:28 +0000791 if(!v->Identifier) {
vlmbf3c5112005-02-14 20:41:29 +0000792 v->Identifier = strdup("Member");
vlm39ba4c42004-09-22 16:06:28 +0000793 assert(v->Identifier);
794 }
795 v->_anonymous_type = 1;
796 arg->embed++;
vlm4e554992004-08-25 02:03:12 +0000797 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000798 arg->embed--;
799 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000800 OUT("};\n");
801
vlm4a3f5822004-06-28 21:13:46 +0000802 /*
vlma5dcb912004-09-29 13:16:40 +0000803 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000804 */
vlm72425de2004-09-13 08:31:01 +0000805 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000806
vlm337167e2005-11-26 11:25:14 +0000807 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000808 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000809 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000810 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000811 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000812 OUT("),\n");
813 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000814 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000815 OUT(", _asn_ctx),\n");
vlm6d44a542005-11-08 03:06:16 +0000816 {
817 int as_xvl = expr_as_xmlvaluelist(arg, v);
818 OUT("%d,\t/* XER encoding is %s */\n",
819 as_xvl,
820 as_xvl ? "XMLValueList" : "XMLDelimitedItemList");
821 }
vlmfa67ddc2004-06-03 03:38:44 +0000822 );
823 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000824
825 /*
vlma5dcb912004-09-29 13:16:40 +0000826 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000827 */
vlm72425de2004-09-13 08:31:01 +0000828 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
vlm86f5ed22004-09-26 13:11:31 +0000829 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000830
vlmfa67ddc2004-06-03 03:38:44 +0000831 REDIR(OT_TYPE_DECLS);
832
833 return 0;
vlmedf203f2005-01-17 11:57:48 +0000834} /* _SEx_OF_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000835
836int
837asn1c_lang_C_type_CHOICE(arg_t *arg) {
838 asn1p_expr_t *expr = arg->expr;
839 asn1p_expr_t *v;
vlmdae7f9d2004-08-22 03:25:24 +0000840 char *id;
vlmfa67ddc2004-06-03 03:38:44 +0000841
842 DEPENDENCIES;
843
vlm33a4ff12004-08-11 05:21:32 +0000844 REDIR(OT_DEPS);
845
vlm766cf1e2005-03-04 08:48:53 +0000846 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000847 out_name_chain(arg, ONC_noflags);
vlm766cf1e2005-03-04 08:48:53 +0000848 OUT("_PR {\n");
vlm33a4ff12004-08-11 05:21:32 +0000849 INDENTED(
vlm9ea7d412006-08-18 01:32:26 +0000850 int firstTime = 1;
vlm2e774282005-08-14 15:03:31 +0000851 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000852 OUT("_PR_NOTHING,\t/* No components present */\n");
vlm33a4ff12004-08-11 05:21:32 +0000853 TQ_FOR(v, &(expr->members), next) {
vlm9ea7d412006-08-18 01:32:26 +0000854 if(firstTime) firstTime = 0;
855 else OUT(",\n");
vlm33a4ff12004-08-11 05:21:32 +0000856 if(v->expr_type == A1TC_EXTENSIBLE) {
857 OUT("/* Extensions may appear below */\n");
858 continue;
859 }
vlm2e774282005-08-14 15:03:31 +0000860 out_name_chain(arg, ONC_noflags);
vlm0c6d3812006-03-21 03:40:38 +0000861 id = MKID(v);
vlm9ea7d412006-08-18 01:32:26 +0000862 OUT("_PR_%s", id);
vlm33a4ff12004-08-11 05:21:32 +0000863 }
vlm9ea7d412006-08-18 01:32:26 +0000864 OUT("\n");
vlm33a4ff12004-08-11 05:21:32 +0000865 );
vlm2e774282005-08-14 15:03:31 +0000866 OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n");
vlm33a4ff12004-08-11 05:21:32 +0000867
868 REDIR(OT_TYPE_DECLS);
vlmfa67ddc2004-06-03 03:38:44 +0000869
870 if(arg->embed) {
vlm2e774282005-08-14 15:03:31 +0000871 OUT("struct "); out_name_chain(arg, ONC_avoid_keywords); OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000872 } else {
vlm0c6d3812006-03-21 03:40:38 +0000873 OUT("typedef struct %s {\n", MKID_safe(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000874 }
875
876 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000877 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000878 OUT("_PR present;\n");
vlm2e774282005-08-14 15:03:31 +0000879 OUT("union ");
880 if(UNNAMED_UNIONS == 0) {
881 out_name_chain(arg, ONC_force_compound_name);
882 OUT("_u ");
883 }
884 OUT("{\n");
vlmfa67ddc2004-06-03 03:38:44 +0000885 TQ_FOR(v, &(expr->members), next) {
886 EMBED(v);
887 }
888 if(UNNAMED_UNIONS) OUT("};\n");
889 else OUT("} choice;\n");
890 );
891
892 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000893 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000894 expr->_anonymous_type ? "" :
895 arg->embed
vlm0c6d3812006-03-21 03:40:38 +0000896 ? MKID_safe(expr)
897 : MKID(expr),
vlmdae7f9d2004-08-22 03:25:24 +0000898 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000899
900 return asn1c_lang_C_type_CHOICE_def(arg);
901}
902
903static int
904asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
905 asn1p_expr_t *expr = arg->expr;
906 asn1p_expr_t *v;
907 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000908 tag2el_t *tag2el = NULL;
909 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000910 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000911 int all_tags_count;
912 enum tvm_compat tv_mode;
vlm337167e2005-11-26 11:25:14 +0000913 int *cmap = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000914
915 /*
916 * Fetch every inner tag from the tag to elements map.
917 */
vlm940bc6b2004-10-03 09:13:30 +0000918 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000919 if(tag2el) free(tag2el);
920 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000921 }
922
vlm33a4ff12004-08-11 05:21:32 +0000923 GEN_INCLUDE("constr_CHOICE");
924 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000925 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000926
vlm33a4ff12004-08-11 05:21:32 +0000927 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000928
929 /*
vlm337167e2005-11-26 11:25:14 +0000930 * Print out the table according to which parsing is performed.
vlmfa67ddc2004-06-03 03:38:44 +0000931 */
vlm39ba4c42004-09-22 16:06:28 +0000932 if(expr_elements_count(arg, expr)) {
vlmfa67ddc2004-06-03 03:38:44 +0000933
vlm337167e2005-11-26 11:25:14 +0000934 if(emit_members_PER_constraints(arg))
935 return -1;
vlm766cf1e2005-03-04 08:48:53 +0000936 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000937 MKID(expr), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000938
939 elements = 0;
940 INDENTED(TQ_FOR(v, &(expr->members), next) {
vlm34dcd572005-01-17 11:40:49 +0000941 if(v->expr_type == A1TC_EXTENSIBLE)
942 continue;
vlm34dcd572005-01-17 11:40:49 +0000943 emit_member_table(arg, v);
vlm5a6fc652005-08-16 17:00:21 +0000944 elements++;
vlm39ba4c42004-09-22 16:06:28 +0000945 });
946 OUT("};\n");
947 } else {
948 elements = 0;
949 }
vlmfa67ddc2004-06-03 03:38:44 +0000950
vlm337167e2005-11-26 11:25:14 +0000951 /* Create a canonical elements map */
952 if(elements && (arg->flags & A1C_GEN_PER)) {
953 int i;
954 cmap = compute_canonical_members_order(arg, elements);
955 if(cmap) {
956 OUT("static int asn_MAP_%s_cmap_%d[] = {",
vlm0c6d3812006-03-21 03:40:38 +0000957 MKID(expr),
vlm337167e2005-11-26 11:25:14 +0000958 expr->_type_unique_index);
959 for(i = 0; i < elements; i++) {
960 if(i) OUT(",");
961 OUT(" %d", cmap[i]);
962 }
963 OUT(" };\n");
964 free(cmap);
965 }
966 }
vlm6e73a042004-08-11 07:17:22 +0000967
vlmfa67ddc2004-06-03 03:38:44 +0000968 if(arg->embed) {
969 /*
970 * Our parent structure has already taken this into account.
971 */
vlm72425de2004-09-13 08:31:01 +0000972 tv_mode = _TVM_SAME;
973 tags_count = all_tags_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000974 } else {
vlm72425de2004-09-13 08:31:01 +0000975 tv_mode = emit_tags_vectors(arg, expr,
976 &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000977 }
vlmfa67ddc2004-06-03 03:38:44 +0000978
979 /*
980 * Tags to elements map.
981 */
vlm940bc6b2004-10-03 09:13:30 +0000982 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000983
vlm337167e2005-11-26 11:25:14 +0000984 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000985 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000986 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000987 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000988 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000989 OUT("),\n");
990 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000991 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000992 OUT(", _asn_ctx),\n");
993 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000994 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000995 OUT(", present),\n");
996 OUT("sizeof(((struct ");
vlm2e774282005-08-14 15:03:31 +0000997 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000998 OUT(" *)0)->present),\n");
vlm337167e2005-11-26 11:25:14 +0000999 OUT("asn_MAP_%s_tag2el_%d,\n",
vlm0c6d3812006-03-21 03:40:38 +00001000 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001001 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm337167e2005-11-26 11:25:14 +00001002 if(C99_MODE) OUT(".canonical_order = ");
1003 if(cmap) OUT("asn_MAP_%s_cmap_%d,\t/* Canonically sorted */\n",
vlm0c6d3812006-03-21 03:40:38 +00001004 MKID(expr), expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00001005 else OUT("0,\n");
1006 if(C99_MODE) OUT(".ext_start = ");
1007 OUT("%d\t/* Extensions start */\n",
1008 compute_extensions_start(expr));
vlmfa67ddc2004-06-03 03:38:44 +00001009 );
1010 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +00001011
1012 /*
vlma5dcb912004-09-29 13:16:40 +00001013 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +00001014 */
vlm72425de2004-09-13 08:31:01 +00001015 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +00001016 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +00001017
vlmfa67ddc2004-06-03 03:38:44 +00001018 REDIR(OT_TYPE_DECLS);
1019
1020 return 0;
vlmedf203f2005-01-17 11:57:48 +00001021} /* _CHOICE_def() */
vlmfa67ddc2004-06-03 03:38:44 +00001022
1023int
1024asn1c_lang_C_type_REFERENCE(arg_t *arg) {
1025 asn1p_ref_t *ref;
1026
1027 ref = arg->expr->reference;
1028 if(ref->components[ref->comp_count-1].name[0] == '&') {
vlmfa67ddc2004-06-03 03:38:44 +00001029 asn1p_expr_t *extract;
1030 arg_t tmp;
1031 int ret;
1032
vlm39ba4c42004-09-22 16:06:28 +00001033 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
vlm0c6d3812006-03-21 03:40:38 +00001034 arg->expr, arg->expr->rhs_pspecs, ref);
vlmfa67ddc2004-06-03 03:38:44 +00001035 if(extract == NULL)
1036 return -1;
1037
vlmdae7f9d2004-08-22 03:25:24 +00001038 extract = asn1p_expr_clone(extract, 0);
vlmfa67ddc2004-06-03 03:38:44 +00001039 if(extract) {
1040 if(extract->Identifier)
1041 free(extract->Identifier);
1042 extract->Identifier = strdup(arg->expr->Identifier);
1043 if(extract->Identifier == NULL) {
1044 asn1p_expr_free(extract);
1045 return -1;
1046 }
1047 } else {
1048 return -1;
1049 }
1050
1051 tmp = *arg;
1052 tmp.asn = arg->asn;
vlmfa67ddc2004-06-03 03:38:44 +00001053 tmp.expr = extract;
1054
1055 ret = arg->default_cb(&tmp);
1056
1057 asn1p_expr_free(extract);
1058
1059 return ret;
1060 }
1061
1062
1063 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
1064}
1065
1066int
1067asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
1068 asn1p_expr_t *expr = arg->expr;
vlm6e73a042004-08-11 07:17:22 +00001069 int tags_count;
vlm72425de2004-09-13 08:31:01 +00001070 int all_tags_count;
1071 enum tvm_compat tv_mode;
vlm80a48592005-02-25 12:10:27 +00001072 enum etd_spec etd_spec;
vlmfa67ddc2004-06-03 03:38:44 +00001073 char *p;
1074
1075 if(arg->embed) {
vlma5dcb912004-09-29 13:16:40 +00001076 enum tnfmt tnfmt = TNF_CTYPE;
1077
1078 /*
1079 * If this is an optional compound type,
1080 * refer it using "struct X" convention,
1081 * as it may recursively include the current structure.
1082 */
vlm0b567bf2005-03-04 22:18:20 +00001083 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
vlm6c4a8502005-08-18 13:38:19 +00001084 if(terminal_structable(arg, expr)) {
vlm0b567bf2005-03-04 22:18:20 +00001085 tnfmt = TNF_RSAFE;
1086 REDIR(OT_FWD_DECLS);
vlm5feb7522005-03-04 23:50:56 +00001087 OUT("%s;\n",
vlm0b567bf2005-03-04 22:18:20 +00001088 asn1c_type_name(arg, arg->expr, tnfmt));
vlma5dcb912004-09-29 13:16:40 +00001089 }
1090 }
1091
vlmfa67ddc2004-06-03 03:38:44 +00001092 REDIR(OT_TYPE_DECLS);
1093
vlm0b567bf2005-03-04 22:18:20 +00001094 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
vlm80a48592005-02-25 12:10:27 +00001095 if(!expr->_anonymous_type) {
vlm0b567bf2005-03-04 22:18:20 +00001096 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
vlm0c6d3812006-03-21 03:40:38 +00001097 OUT("%s", MKID_safe(expr));
vlm337167e2005-11-26 11:25:14 +00001098 if((expr->marker.flags & (EM_DEFAULT & ~EM_INDIRECT))
1099 == (EM_DEFAULT & ~EM_INDIRECT))
vlm80a48592005-02-25 12:10:27 +00001100 OUT("\t/* DEFAULT %s */",
1101 asn1f_printable_value(
1102 expr->marker.default_value));
vlm337167e2005-11-26 11:25:14 +00001103 else if((expr->marker.flags & EM_OPTIONAL)
1104 == EM_OPTIONAL)
vlm80a48592005-02-25 12:10:27 +00001105 OUT("\t/* OPTIONAL */");
1106 }
1107
1108 } else {
1109 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
vlmb2839012004-08-20 13:37:01 +00001110
1111 REDIR(OT_TYPE_DECLS);
vlm80a48592005-02-25 12:10:27 +00001112
1113 OUT("typedef %s\t",
vlm0b567bf2005-03-04 22:18:20 +00001114 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
vlm80a48592005-02-25 12:10:27 +00001115 OUT("%s%s_t",
vlm0b567bf2005-03-04 22:18:20 +00001116 (expr->marker.flags & EM_INDIRECT)?"*":" ",
vlm0c6d3812006-03-21 03:40:38 +00001117 MKID(expr));
vlmfa67ddc2004-06-03 03:38:44 +00001118 }
1119
vlm80a48592005-02-25 12:10:27 +00001120 if((expr->expr_type == ASN_BASIC_ENUMERATED)
vlmc89422d2005-03-03 21:28:12 +00001121 || (0 /* -- prohibited by X.693:8.3.4 */
1122 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00001123 && expr_elements_count(arg, expr)))
1124 etd_spec = ETD_HAS_SPECIFICS;
1125 else
1126 etd_spec = ETD_NO_SPECIFICS;
vlmfa67ddc2004-06-03 03:38:44 +00001127
vlm86f5ed22004-09-26 13:11:31 +00001128 /*
1129 * If this type just blindly refers the other type, alias it.
1130 * Type1 ::= Type2
1131 */
vlm80a48592005-02-25 12:10:27 +00001132 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
1133 REDIR(OT_TYPE_DECLS);
1134 return 0;
1135 }
vlm86f5ed22004-09-26 13:11:31 +00001136 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlm80a48592005-02-25 12:10:27 +00001137 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
vlm6f74b0f2005-03-04 23:23:50 +00001138 && etd_spec == ETD_NO_SPECIFICS
1139 && 0 /* This shortcut is incompatible with XER */
1140 ) {
vlm86f5ed22004-09-26 13:11:31 +00001141 char *type_name;
1142 REDIR(OT_FUNC_DECLS);
1143 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
1144 OUT("/* This type is equivalent to %s */\n", type_name);
vlm86f5ed22004-09-26 13:11:31 +00001145 if(HIDE_INNER_DEFS) OUT("/* ");
vlm0c6d3812006-03-21 03:40:38 +00001146 OUT("#define\tasn_DEF_%s\t", MKID(expr));
vlm86f5ed22004-09-26 13:11:31 +00001147 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlm80a48592005-02-25 12:10:27 +00001148 OUT("asn_DEF_%s", type_name);
vlm86f5ed22004-09-26 13:11:31 +00001149 if(HIDE_INNER_DEFS)
vlm80a48592005-02-25 12:10:27 +00001150 OUT("\t// (Use -fall-defs-global to expose) */");
1151 OUT("\n");
vlm86f5ed22004-09-26 13:11:31 +00001152 REDIR(OT_CODE);
1153 OUT("/* This type is equivalent to %s */\n", type_name);
1154 OUT("\n");
1155 REDIR(OT_TYPE_DECLS);
1156 return 0;
1157 }
1158
vlmfa67ddc2004-06-03 03:38:44 +00001159 REDIR(OT_STAT_DEFS);
1160
vlm4a3f5822004-06-28 21:13:46 +00001161 /*
vlma5dcb912004-09-29 13:16:40 +00001162 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +00001163 */
vlm72425de2004-09-13 08:31:01 +00001164 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +00001165
vlm80a48592005-02-25 12:10:27 +00001166 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1167 0, etd_spec);
vlmfa67ddc2004-06-03 03:38:44 +00001168
vlmb2839012004-08-20 13:37:01 +00001169 REDIR(OT_CODE);
1170
vlmfa67ddc2004-06-03 03:38:44 +00001171 /*
1172 * Constraint checking.
1173 */
vlm86f5ed22004-09-26 13:11:31 +00001174 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
vlm0c6d3812006-03-21 03:40:38 +00001175 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001176 if(HIDE_INNER_DEFS) OUT("static ");
vlm86f5ed22004-09-26 13:11:31 +00001177 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001178 OUT("%s", p);
1179 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1180 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
vlm86f5ed22004-09-26 13:11:31 +00001181 INDENT(+1);
vlmaf68ef52006-07-13 11:19:01 +00001182 OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {");
vlm86f5ed22004-09-26 13:11:31 +00001183 OUT("\n");
1184 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1185 OUT("/* Replace with underlying type checker */\n");
1186 OUT("td->check_constraints "
vlma5dcb912004-09-29 13:16:40 +00001187 "= asn_DEF_%s.check_constraints;\n",
vlm86f5ed22004-09-26 13:11:31 +00001188 asn1c_type_name(arg, expr, TNF_SAFE));
1189 OUT("return td->check_constraints"
vlmaf68ef52006-07-13 11:19:01 +00001190 "(td, sptr, ctfailcb, app_key);\n");
vlm1d036692004-08-19 13:29:46 +00001191 }
vlm86f5ed22004-09-26 13:11:31 +00001192 INDENT(-1);
1193 OUT("}\n");
1194 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001195 }
vlmfa67ddc2004-06-03 03:38:44 +00001196
1197 /*
1198 * Emit suicidal functions.
1199 */
1200
vlmfa67ddc2004-06-03 03:38:44 +00001201 /*
1202 * This function replaces certain fields from the definition
1203 * of a type with the corresponding fields from the basic type
1204 * (from which the current type is inherited).
1205 */
vlmfa67ddc2004-06-03 03:38:44 +00001206 OUT("/*\n");
vlmb2839012004-08-20 13:37:01 +00001207 OUT(" * This type is implemented using %s,\n",
1208 asn1c_type_name(arg, expr, TNF_SAFE));
vlmdcd32fe2004-09-23 22:20:47 +00001209 OUT(" * so here we adjust the DEF accordingly.\n");
vlmfa67ddc2004-06-03 03:38:44 +00001210 OUT(" */\n");
1211 OUT("static void\n");
vlm766cf1e2005-03-04 08:48:53 +00001212 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
vlm0c6d3812006-03-21 03:40:38 +00001213 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001214 INDENT(+1);
vlm80a48592005-02-25 12:10:27 +00001215 {
vlm39ba4c42004-09-22 16:06:28 +00001216 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001217 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlma5dcb912004-09-29 13:16:40 +00001218 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1219 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1220 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1221 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1222 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1223 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
vlm337167e2005-11-26 11:25:14 +00001224 OUT("td->uper_decoder = asn_DEF_%s.uper_decoder;\n", type_name);
vlm9ea7d412006-08-18 01:32:26 +00001225 OUT("td->uper_encoder = asn_DEF_%s.uper_encoder;\n", type_name);
vlm1308d2b2004-09-10 15:49:15 +00001226 if(!terminal && !tags_count) {
vlm72425de2004-09-13 08:31:01 +00001227 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
vlma5dcb912004-09-29 13:16:40 +00001228 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1229 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1230 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1231 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
vlm1308d2b2004-09-10 15:49:15 +00001232 OUT("/* End of these lines */\n");
1233 }
vlm337167e2005-11-26 11:25:14 +00001234 OUT("if(!td->per_constraints)\n");
1235 OUT("\ttd->per_constraints = asn_DEF_%s.per_constraints;\n",
1236 type_name);
vlma5dcb912004-09-29 13:16:40 +00001237 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1238 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
vlm80a48592005-02-25 12:10:27 +00001239 if(etd_spec != ETD_NO_SPECIFICS) {
1240 INDENT(-1);
vlmc86870c2005-03-29 19:04:24 +00001241 OUT(" /* ");
vlmb2839012004-08-20 13:37:01 +00001242 }
vlm80a48592005-02-25 12:10:27 +00001243 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1244 if(etd_spec == ETD_NO_SPECIFICS) {
1245 INDENT(-1);
1246 OUT("\n");
1247 } else {
1248 OUT("\t// Defined explicitly */\n");
1249 }
1250 }
vlmfa67ddc2004-06-03 03:38:44 +00001251 OUT("}\n");
1252 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001253
vlm0c6d3812006-03-21 03:40:38 +00001254 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001255 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001256 OUT("void\n");
vlm766cf1e2005-03-04 08:48:53 +00001257 OUT("%s", p);
1258 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1259 OUT("_free(asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001260 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001261 OUT("\tvoid *struct_ptr, int contents_only) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001262 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1263 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001264 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001265 );
1266 OUT("}\n");
1267 OUT("\n");
1268
vlm0c6d3812006-03-21 03:40:38 +00001269 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001270 if(HIDE_INNER_DEFS) OUT("static ");
vlmfa67ddc2004-06-03 03:38:44 +00001271 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001272 OUT("%s", p);
1273 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1274 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001275 INDENTED(
1276 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001277 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1278 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001279 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1280 );
1281 OUT("}\n");
1282 OUT("\n");
1283
vlm0c6d3812006-03-21 03:40:38 +00001284 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001285 if(HIDE_INNER_DEFS) OUT("static ");
vlm9de248e2004-10-20 15:50:55 +00001286 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001287 OUT("%s", p);
1288 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1289 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001290 INDENTED(
vlmfcc4cbc2005-03-17 21:56:00 +00001291 OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001292 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1293 p, expr->_type_unique_index);
vlma5dcb912004-09-29 13:16:40 +00001294 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
vlm39ba4c42004-09-22 16:06:28 +00001295 );
1296 OUT("}\n");
1297 OUT("\n");
1298
vlm0c6d3812006-03-21 03:40:38 +00001299 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001300 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001301 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001302 OUT("%s", p);
1303 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1304 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
vlm39ba4c42004-09-22 16:06:28 +00001305 INDENTED(
1306 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1307 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001308 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1309 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001310 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1311 );
1312 OUT("}\n");
1313 OUT("\n");
1314
vlm0c6d3812006-03-21 03:40:38 +00001315 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001316 if(HIDE_INNER_DEFS) OUT("static ");
vlme03646b2004-10-23 13:34:00 +00001317 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001318 OUT("%s", p);
1319 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1320 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlme03646b2004-10-23 13:34:00 +00001321 INDENTED(
vlmfcc4cbc2005-03-17 21:56:00 +00001322 OUT("\tvoid **structure, const char *opt_mname, const void *bufptr, size_t size) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001323 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1324 p, expr->_type_unique_index);
vlme03646b2004-10-23 13:34:00 +00001325 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1326 );
1327 OUT("}\n");
1328 OUT("\n");
1329
vlm0c6d3812006-03-21 03:40:38 +00001330 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001331 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001332 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001333 OUT("%s", p);
1334 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1335 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
vlm39ba4c42004-09-22 16:06:28 +00001336 INDENTED(
1337 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1338 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001339 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1340 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001341 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001342 );
1343 OUT("}\n");
1344 OUT("\n");
1345
vlm337167e2005-11-26 11:25:14 +00001346 if(arg->flags & A1C_GEN_PER) {
vlm0c6d3812006-03-21 03:40:38 +00001347 p = MKID(expr);
vlm9ea7d412006-08-18 01:32:26 +00001348
vlm337167e2005-11-26 11:25:14 +00001349 if(HIDE_INNER_DEFS) OUT("static ");
1350 OUT("asn_dec_rval_t\n");
1351 OUT("%s", p);
1352 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1353 OUT("_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
1354 INDENTED(
1355 OUT("\tasn_per_constraints_t *constraints, void **structure, asn_per_data_t *per_data) {\n");
1356 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1357 p, expr->_type_unique_index);
1358 OUT("return td->uper_decoder(opt_codec_ctx, td, constraints, structure, per_data);\n");
1359 );
1360 OUT("}\n");
1361 OUT("\n");
vlm9ea7d412006-08-18 01:32:26 +00001362
1363 p = MKID(expr);
1364 if(HIDE_INNER_DEFS) OUT("static ");
1365 OUT("asn_enc_rval_t\n");
1366 OUT("%s", p);
1367 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1368 OUT("_encode_uper(asn_TYPE_descriptor_t *td,\n");
1369 INDENTED(
1370 OUT("\tasn_per_constraints_t *constraints,\n");
1371 OUT("\tvoid *structure, asn_per_outp_t *per_out) {\n");
1372 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1373 p, expr->_type_unique_index);
1374 OUT("return td->uper_encoder(td, constraints, structure, per_out);\n");
1375 );
1376 OUT("}\n");
1377 OUT("\n");
vlm337167e2005-11-26 11:25:14 +00001378 }
1379
vlmfa67ddc2004-06-03 03:38:44 +00001380 REDIR(OT_FUNC_DECLS);
1381
vlm0c6d3812006-03-21 03:40:38 +00001382 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001383 if(HIDE_INNER_DEFS) {
vlm766cf1e2005-03-04 08:48:53 +00001384 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1385 "\t// (Use -fall-defs-global to expose) */\n",
1386 p, expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +00001387 } else {
1388 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1389 OUT("asn_struct_free_f %s_free;\n", p);
1390 OUT("asn_struct_print_f %s_print;\n", p);
1391 OUT("asn_constr_check_f %s_constraint;\n", p);
1392 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1393 OUT("der_type_encoder_f %s_encode_der;\n", p);
1394 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1395 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
vlm9ea7d412006-08-18 01:32:26 +00001396 if(arg->flags & A1C_GEN_PER) {
vlm337167e2005-11-26 11:25:14 +00001397 OUT("per_type_decoder_f %s_decode_uper;\n", p);
vlm9ea7d412006-08-18 01:32:26 +00001398 OUT("per_type_encoder_f %s_encode_uper;\n", p);
1399 }
vlm80a48592005-02-25 12:10:27 +00001400 }
vlmfa67ddc2004-06-03 03:38:44 +00001401
vlm33a4ff12004-08-11 05:21:32 +00001402 REDIR(OT_TYPE_DECLS);
1403
vlmfa67ddc2004-06-03 03:38:44 +00001404 return 0;
1405}
1406
1407int
1408asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1409
1410 OUT("/*\n");
1411 OUT(" * This type is extensible,\n");
1412 OUT(" * possible extensions are below.\n");
1413 OUT(" */\n");
1414
1415 return 0;
1416}
1417
vlm337167e2005-11-26 11:25:14 +00001418static int
1419compute_extensions_start(asn1p_expr_t *expr) {
vlm79b08d52004-07-01 00:52:50 +00001420 asn1p_expr_t *v;
vlm337167e2005-11-26 11:25:14 +00001421 int eidx = 0;
vlm79b08d52004-07-01 00:52:50 +00001422 TQ_FOR(v, &(expr->members), next) {
vlm337167e2005-11-26 11:25:14 +00001423 if(v->expr_type == A1TC_EXTENSIBLE)
1424 return eidx;
1425 eidx++;
vlm79b08d52004-07-01 00:52:50 +00001426 }
vlm337167e2005-11-26 11:25:14 +00001427 return -1;
vlm79b08d52004-07-01 00:52:50 +00001428}
1429
vlmfa67ddc2004-06-03 03:38:44 +00001430static int
vlma8a86cc2004-09-10 06:07:18 +00001431_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
vlmfa67ddc2004-06-03 03:38:44 +00001432
1433 OUT("(");
vlma8a86cc2004-09-10 06:07:18 +00001434 switch(tag->tag_class) {
vlmfa67ddc2004-06-03 03:38:44 +00001435 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1436 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1437 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1438 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1439 case TC_NOCLASS:
1440 break;
1441 }
vlm47ae1582004-09-24 21:01:43 +00001442 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +00001443
1444 return 0;
1445}
1446
vlm4e03ce22004-06-06 07:20:17 +00001447
1448static int
1449_tag2el_cmp(const void *ap, const void *bp) {
1450 const tag2el_t *a = ap;
1451 const tag2el_t *b = bp;
1452 const struct asn1p_type_tag_s *ta = &a->el_tag;
1453 const struct asn1p_type_tag_s *tb = &b->el_tag;
1454
1455 if(ta->tag_class == tb->tag_class) {
1456 if(ta->tag_value == tb->tag_value) {
1457 /*
1458 * Sort by their respective positions.
1459 */
1460 if(a->el_no < b->el_no)
1461 return -1;
1462 else if(a->el_no > b->el_no)
1463 return 1;
1464 return 0;
1465 } else if(ta->tag_value < tb->tag_value)
1466 return -1;
1467 else
1468 return 1;
1469 } else if(ta->tag_class < tb->tag_class) {
1470 return -1;
1471 } else {
1472 return 1;
1473 }
1474}
1475
vlmfa67ddc2004-06-03 03:38:44 +00001476/*
1477 * For constructed types, number of external tags may be greater than
1478 * number of elements in the type because of CHOICE type.
1479 * T ::= SET { -- Three possible tags:
1480 * a INTEGER, -- One tag is here...
1481 * b Choice1 -- ... and two more tags are there.
1482 * }
1483 * Choice1 ::= CHOICE {
1484 * s1 IA5String,
1485 * s2 ObjectDescriptor
1486 * }
1487 */
1488static int
vlm940bc6b2004-10-03 09:13:30 +00001489_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001490 asn1p_expr_t *expr = arg->expr;
1491 arg_t tmparg = *arg;
1492 asn1p_expr_t *v;
1493 int element = 0;
vlm940bc6b2004-10-03 09:13:30 +00001494 int original_count = *count;
1495 int sort_until = -1;
vlmfa67ddc2004-06-03 03:38:44 +00001496
1497 TQ_FOR(v, &(expr->members), next) {
vlm940bc6b2004-10-03 09:13:30 +00001498 if(v->expr_type == A1TC_EXTENSIBLE) {
1499 /*
vlm80a48592005-02-25 12:10:27 +00001500 * CXER mandates sorting
vlm940bc6b2004-10-03 09:13:30 +00001501 * only for the root part.
1502 */
1503 if(flags == FTE_CANONICAL_XER
1504 && sort_until == -1)
1505 sort_until = *count;
vlmfa67ddc2004-06-03 03:38:44 +00001506 continue;
vlm940bc6b2004-10-03 09:13:30 +00001507 }
vlmfa67ddc2004-06-03 03:38:44 +00001508
1509 tmparg.expr = v;
1510
1511 if(_add_tag2el_member(&tmparg, tag2el, count,
vlm940bc6b2004-10-03 09:13:30 +00001512 (el_no==-1)?element:el_no, flags)) {
vlmfa67ddc2004-06-03 03:38:44 +00001513 return -1;
1514 }
1515
1516 element++;
1517 }
1518
vlm940bc6b2004-10-03 09:13:30 +00001519
1520 if(flags == FTE_CANONICAL_XER) {
1521 if(sort_until == -1) sort_until = *count;
1522 qsort((*tag2el) + original_count,
1523 sort_until - original_count,
1524 sizeof(**tag2el), _tag2el_cmp);
1525 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1526 && (sort_until - original_count) >= 1) {
1527 /* Only take in account the root component */
1528 *count = original_count + 1;
1529 }
1530 } else {
1531 /*
1532 * Sort the map according to canonical order of their
1533 * tags and element numbers.
1534 */
1535 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1536 }
vlm4e03ce22004-06-06 07:20:17 +00001537
vlmc8aeab42004-06-14 13:09:45 +00001538 /*
1539 * Initialize .toff_{first|last} members.
1540 */
1541 if(*count) {
1542 struct asn1p_type_tag_s *cur_tag = 0;
1543 tag2el_t *cur = *tag2el;
1544 tag2el_t *end = cur + *count;
1545 int occur, i;
1546 for(occur = 0; cur < end; cur++) {
1547 if(cur_tag == 0
1548 || cur_tag->tag_value != cur->el_tag.tag_value
1549 || cur_tag->tag_class != cur->el_tag.tag_class) {
1550 cur_tag = &cur->el_tag;
1551 occur = 0;
1552 } else {
1553 occur++;
1554 }
1555 cur->toff_first = -occur;
1556 for(i = 0; i >= -occur; i--)
1557 cur[i].toff_last = -i;
1558 }
1559 }
1560
vlmfa67ddc2004-06-03 03:38:44 +00001561 return 0;
1562}
1563
1564static int
vlm940bc6b2004-10-03 09:13:30 +00001565_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001566 struct asn1p_type_tag_s tag;
1567 int ret;
1568
1569 assert(el_no >= 0);
1570
vlm39ba4c42004-09-22 16:06:28 +00001571 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
vlm337167e2005-11-26 11:25:14 +00001572 arg->expr, &tag, AFT_IMAGINARY_ANY);
vlmfa67ddc2004-06-03 03:38:44 +00001573 if(ret == 0) {
vlme2070ea2004-09-04 04:42:29 +00001574 tag2el_t *te;
1575 int new_count = (*count) + 1;
vlmfa67ddc2004-06-03 03:38:44 +00001576 void *p;
vlme2070ea2004-09-04 04:42:29 +00001577
vlm2de0e792004-09-05 10:42:33 +00001578 if(tag.tag_value == -1) {
1579 /*
1580 * This is an untagged ANY type,
1581 * proceed without adding a tag
1582 */
1583 return 0;
1584 }
1585
vlme2070ea2004-09-04 04:42:29 +00001586 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
vlmfa67ddc2004-06-03 03:38:44 +00001587 if(p) *tag2el = p;
1588 else return -1;
1589
1590 DEBUG("Found tag for %s: %ld",
1591 arg->expr->Identifier,
1592 (long)tag.tag_value);
1593
vlme2070ea2004-09-04 04:42:29 +00001594 te = &((*tag2el)[*count]);
1595 te->el_tag = tag;
1596 te->el_no = el_no;
1597 te->from_expr = arg->expr;
1598 *count = new_count;
vlmfa67ddc2004-06-03 03:38:44 +00001599 return 0;
1600 }
1601
1602 DEBUG("Searching tag in complex expression %s:%x at line %d",
1603 arg->expr->Identifier,
1604 arg->expr->expr_type,
1605 arg->expr->_lineno);
1606
1607 /*
1608 * Iterate over members of CHOICE type.
1609 */
1610 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
vlm940bc6b2004-10-03 09:13:30 +00001611 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001612 }
1613
1614 if(arg->expr->expr_type == A1TC_REFERENCE) {
1615 arg_t tmp = *arg;
1616 asn1p_expr_t *expr;
vlm6c4a8502005-08-18 13:38:19 +00001617 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.expr,
vlmfa67ddc2004-06-03 03:38:44 +00001618 arg->expr->reference);
1619 if(expr) {
1620 tmp.expr = expr;
vlm940bc6b2004-10-03 09:13:30 +00001621 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001622 } else {
1623 FATAL("Cannot dereference %s at line %d",
1624 arg->expr->Identifier,
1625 arg->expr->_lineno);
1626 return -1;
1627 }
1628 }
1629
1630 DEBUG("No tag for %s at line %d",
1631 arg->expr->Identifier,
1632 arg->expr->_lineno);
1633
1634 return -1;
1635}
1636
1637static int
vlm940bc6b2004-10-03 09:13:30 +00001638emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
vlm4e03ce22004-06-06 07:20:17 +00001639 asn1p_expr_t *expr = arg->expr;
vlmfbd6d9e2005-06-02 05:21:37 +00001640 int i;
1641
1642 if(!tag2el_count) return 0; /* No top level tags */
vlm4e03ce22004-06-06 07:20:17 +00001643
vlm337167e2005-11-26 11:25:14 +00001644 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_tag2el%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +00001645 MKID(expr), opt_modifier?opt_modifier:"",
vlm337167e2005-11-26 11:25:14 +00001646 expr->_type_unique_index);
vlmfbd6d9e2005-06-02 05:21:37 +00001647 for(i = 0; i < tag2el_count; i++) {
1648 OUT(" { ");
1649 _print_tag(arg, &tag2el[i].el_tag);
1650 OUT(", ");
1651 OUT("%d, ", tag2el[i].el_no);
1652 OUT("%d, ", tag2el[i].toff_first);
1653 OUT("%d ", tag2el[i].toff_last);
1654 OUT("}%s /* %s at %d */\n",
1655 (i + 1 < tag2el_count) ? "," : "",
1656 tag2el[i].from_expr->Identifier,
1657 tag2el[i].from_expr->_lineno
1658 );
vlm4e03ce22004-06-06 07:20:17 +00001659 }
1660 OUT("};\n");
1661
vlmfbd6d9e2005-06-02 05:21:37 +00001662 return 0;
vlm4e03ce22004-06-06 07:20:17 +00001663}
1664
vlm72425de2004-09-13 08:31:01 +00001665static enum tvm_compat
1666emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1667 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1668 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
vlm6e73a042004-08-11 07:17:22 +00001669 int tags_count = 0;
vlm72425de2004-09-13 08:31:01 +00001670 int all_tags_count = 0;
1671 enum tvm_compat tv_mode = _TVM_SAME;
vlm1308d2b2004-09-10 15:49:15 +00001672 int i;
vlm4a3f5822004-06-28 21:13:46 +00001673
vlmc6de2c42004-09-14 14:10:10 +00001674 /* Cleanup before proceeding. */
1675 *tags_count_r = 0;
1676 *all_tags_count_r = 0;
1677
vlm1308d2b2004-09-10 15:49:15 +00001678 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001679 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
vlmc6de2c42004-09-14 14:10:10 +00001680 if(tags_count < 0)
1681 return -1;
vlm6e73a042004-08-11 07:17:22 +00001682
vlm72425de2004-09-13 08:31:01 +00001683 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001684 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
vlm72425de2004-09-13 08:31:01 +00001685 &all_tags, AFT_FULL_COLLECT);
1686 if(all_tags_count < 0) {
1687 if(tags) free(tags);
1688 return -1;
vlmfb41dbb2004-09-08 00:28:57 +00001689 }
vlm1308d2b2004-09-10 15:49:15 +00001690
vlm72425de2004-09-13 08:31:01 +00001691 assert(tags_count <= all_tags_count);
1692 assert((tags_count?0:1) == (all_tags_count?0:1));
vlm4a3f5822004-06-28 21:13:46 +00001693
vlm72425de2004-09-13 08:31:01 +00001694 if(tags_count <= all_tags_count) {
1695 for(i = 0; i < tags_count; i++) {
1696 if(tags[i].tag_value != all_tags[i].tag_value
1697 || tags[i].tag_class != all_tags[i].tag_class) {
1698 tv_mode = _TVM_DIFFERENT;
1699 break;
1700 }
1701 }
1702 if(i == tags_count && tags_count < all_tags_count)
1703 tv_mode = _TVM_SUBSET;
1704 } else {
1705 tv_mode = _TVM_DIFFERENT;
1706 }
1707
1708#define EMIT_TAGS_TABLE(name, tags, tags_count) do { \
vlm337167e2005-11-26 11:25:14 +00001709 OUT("static ber_tlv_tag_t asn_DEF_%s%s_tags_%d[] = {\n",\
vlm0c6d3812006-03-21 03:40:38 +00001710 MKID(expr), name, \
vlm337167e2005-11-26 11:25:14 +00001711 expr->_type_unique_index); \
vlm72425de2004-09-13 08:31:01 +00001712 INDENT(+1); \
1713 /* Print the array of collected tags */ \
1714 for(i = 0; i < tags_count; i++) { \
1715 if(i) OUT(",\n"); \
1716 _print_tag(arg, &tags[i]); \
1717 } \
1718 OUT("\n"); \
1719 INDENT(-1); \
1720 OUT("};\n"); \
1721 } while(0)
1722
1723 if(tags_count) {
1724 if(tv_mode == _TVM_SUBSET)
1725 EMIT_TAGS_TABLE("", all_tags, all_tags_count);
1726 else
1727 EMIT_TAGS_TABLE("", tags, tags_count);
1728 }
1729
1730 if(all_tags_count) {
1731 if(tv_mode == _TVM_DIFFERENT)
1732 EMIT_TAGS_TABLE("_all", all_tags, all_tags_count);
1733 }
1734
1735 if(tags) free(tags);
1736 if(all_tags) free(all_tags);
1737
1738 *tags_count_r = tags_count;
1739 *all_tags_count_r = all_tags_count;
1740
1741 return tv_mode;
vlm4a3f5822004-06-28 21:13:46 +00001742}
vlmb2839012004-08-20 13:37:01 +00001743
1744static int
vlm4e554992004-08-25 02:03:12 +00001745expr_elements_count(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00001746 asn1p_expr_t *topmost_parent;
1747 asn1p_expr_t *v;
1748 int elements = 0;
1749
vlm39ba4c42004-09-22 16:06:28 +00001750 topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001751 if(!topmost_parent) return 0;
1752
vlm80a48592005-02-25 12:10:27 +00001753 if(!(topmost_parent->expr_type & ASN_CONSTR_MASK)
1754 && !topmost_parent->expr_type == ASN_BASIC_INTEGER
1755 && !topmost_parent->expr_type == ASN_BASIC_ENUMERATED)
vlmb2839012004-08-20 13:37:01 +00001756 return 0;
1757
1758 TQ_FOR(v, &(topmost_parent->members), next) {
1759 if(v->expr_type != A1TC_EXTENSIBLE)
1760 elements++;
1761 }
1762
1763 return elements;
1764}
1765
vlm337167e2005-11-26 11:25:14 +00001766static asn1p_expr_type_e
1767expr_get_type(arg_t *arg, asn1p_expr_t *expr) {
1768 asn1p_expr_t *terminal;
1769 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
1770 if(terminal) return terminal->expr_type;
1771 return A1TC_INVALID;
1772}
1773
1774static int
1775emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, char *type) {
1776 if(!range || range->incompatible || range->not_PER_visible) {
1777 OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }");
1778 return 0;
1779 }
1780
1781 if(range->left.type == ARE_VALUE) {
1782 if(range->right.type == ARE_VALUE) {
1783 asn1c_integer_t cover = 1;
1784 asn1c_integer_t r = 1 + range->right.value
1785 - range->left.value;
1786 int rbits, ebits;
1787
1788 if(range->empty_constraint)
1789 r = 0;
1790
1791 /* Compute real constraint */
1792 for(rbits = 0; rbits < (8 * sizeof(r)); rbits++) {
1793 if(r <= cover)
1794 break;
1795 cover *= 2; /* Can't do shifting */
1796 if(cover < 0) {
1797 FATAL("Constraint at line %d too wide "
1798 "for %d-bits integer type",
1799 arg->expr->_lineno,
1800 sizeof(r) * 8);
1801 rbits = sizeof(r);
1802 break;
1803 }
1804 }
1805
1806 if(1) {
1807 /* X.691, #10.9.4.1 */
1808 for(ebits = 0; ebits <= 16; ebits++)
1809 if(r <= 1 << ebits) break;
1810 if(ebits == 17
1811 || range->right.value >= 65536)
1812 ebits = -1;
1813 } else {
1814 /* X.691, #10.5.7.1 */
1815 for(ebits = 0; ebits <= 8; ebits++)
1816 if(r <= 1 << ebits) break;
1817 if(ebits == 9) {
1818 if(r <= 65536)
1819 ebits = 16;
1820 else
1821 ebits = -1;
1822 }
1823 }
1824 OUT("{ APC_CONSTRAINED%s,%s% d, % d, ",
1825 range->extensible
1826 ? " | APC_EXTENSIBLE" : "",
1827 range->extensible ? " " : "\t", rbits, ebits);
1828 } else {
1829 if(range->extensible) {
1830 OUT("{ APC_SEMI_CONSTRAINED | APC_EXTENSIBLE, "
1831 "-1, ");
1832 } else {
1833 OUT("{ APC_SEMI_CONSTRAINED,\t-1, -1, ");
1834 }
1835 }
1836 OUT("% " PRIdASN ", % " PRIdASN " }",
1837 range->left.value, range->right.value);
1838 } else {
1839 OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }");
1840 }
1841
1842 /*
1843 * Print some courtesy debug information.
1844 */
1845 if(range->left.type == ARE_VALUE
1846 || range->right.type == ARE_VALUE) {
1847 OUT("\t/* ");
1848 if(type) OUT("(%s", type);
1849 OUT("(");
1850 if(range->left.type == ARE_VALUE)
1851 OUT("%" PRIdASN, range->left.value);
1852 else
1853 OUT("MIN");
1854 OUT("..");
1855 if(range->right.type == ARE_VALUE)
1856 OUT("%" PRIdASN, range->right.value);
1857 else
1858 OUT("MAX");
1859 if(range->extensible) OUT(",...");
1860 if(type) OUT(")");
1861 OUT(") */");
1862 }
1863
1864 return 0;
1865}
1866
1867static int
1868emit_single_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr) {
1869 asn1cnst_range_t *range;
1870 asn1p_expr_type_e etype;
1871
1872 etype = expr_get_type(arg, expr);
1873
1874 INDENT(+1);
1875
1876 /*
1877 * ENUMERATED and CHOICE are special.
1878 */
1879 if(etype == ASN_BASIC_ENUMERATED
1880 || etype == ASN_CONSTR_CHOICE) {
1881 asn1cnst_range_t tmprng;
1882 asn1p_expr_t *v;
1883 int extensible = 0;
1884 int eidx = -1;
1885
1886 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1887 assert(expr);
1888
1889 TQ_FOR(v, &(expr->members), next) {
1890 if(v->expr_type == A1TC_EXTENSIBLE) {
1891 extensible++;
1892 break;
1893 }
1894 eidx++;
1895 }
1896
1897 memset(&tmprng, 0, sizeof (tmprng));
1898 tmprng.extensible = extensible;
1899 if(eidx < 0) tmprng.empty_constraint = 1;
1900 tmprng.left.type = ARE_VALUE;
1901 tmprng.left.value = 0;
1902 tmprng.right.type = ARE_VALUE;
1903 tmprng.right.value = eidx < 0 ? 0 : eidx;
1904 if(emit_single_member_PER_constraint(arg, &tmprng, 0))
1905 return -1;
1906 } else {
1907 range = asn1constraint_compute_PER_range(etype,
1908 expr->combined_constraints, ACT_EL_RANGE,
1909 0, 0, 0);
1910 if(emit_single_member_PER_constraint(arg, range, 0))
1911 return -1;
1912 asn1constraint_range_free(range);
1913 }
1914 OUT(",\n");
1915
1916 range = asn1constraint_compute_PER_range(etype,
1917 expr->combined_constraints, ACT_CT_SIZE, 0, 0, 0);
1918 if(emit_single_member_PER_constraint(arg, range, "SIZE"))
1919 return -1;
1920 asn1constraint_range_free(range);
1921 OUT("\n");
1922
1923 INDENT(-1);
1924
1925 return 0;
1926}
1927
1928static int
1929emit_members_PER_constraints(arg_t *arg) {
1930 asn1p_expr_t *expr = arg->expr;
1931 asn1p_expr_t *v;
1932
1933 if(!(arg->flags & A1C_GEN_PER))
1934 return 0;
1935
1936 TQ_FOR(v, &(expr->members), next) {
1937 if(v->constraints
1938 || v->expr_type == ASN_BASIC_ENUMERATED
1939 || v->expr_type == ASN_CONSTR_CHOICE) {
1940 OUT("static asn_per_constraints_t "
1941 "asn_PER_memb_%s_constr_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +00001942 MKID(v), v->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00001943 if(emit_single_member_PER_constraints(arg, v))
1944 return -1;
1945 OUT("};\n");
1946 }
1947 }
1948
1949 return 0;
1950}
1951
1952static int
1953try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
1954 int save_target = arg->target->target;
1955 asn1p_expr_type_e etype = expr_get_type(arg, expr);
1956 int fits_long = 0;
1957
1958 switch(etype) {
1959 case ASN_BASIC_BOOLEAN:
1960 fits_long = 1;
1961 case ASN_BASIC_INTEGER:
1962 case ASN_BASIC_ENUMERATED:
1963 if(expr->marker.default_value == NULL
1964 || expr->marker.default_value->type != ATV_INTEGER)
1965 break;
1966 if(!fits_long)
1967 fits_long = asn1c_type_fits_long(arg, expr)!=FL_NOTFIT;
1968 if(fits_long && !expr->marker.default_value->value.v_integer)
1969 expr->marker.flags &= ~EM_INDIRECT;
1970 if(!out) return 1;
1971 REDIR(OT_STAT_DEFS);
vlm9ea7d412006-08-18 01:32:26 +00001972 OUT("static int asn_DFL_%d_set_%" PRIdASN "(int set_value, void **sptr) {\n",
vlm337167e2005-11-26 11:25:14 +00001973 expr->_type_unique_index,
1974 expr->marker.default_value->value.v_integer);
1975 INDENT(+1);
1976 OUT("%s *st = *sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE));
1977 OUT("\n");
1978 OUT("if(!st) {\n");
vlm9ea7d412006-08-18 01:32:26 +00001979 OUT("\tif(!set_value) return -1;\t/* Not a default value */\n");
vlm337167e2005-11-26 11:25:14 +00001980 OUT("\tst = (*sptr = CALLOC(1, sizeof(*st)));\n");
1981 OUT("\tif(!st) return -1;\n");
1982 OUT("}\n");
1983 OUT("\n");
vlm9ea7d412006-08-18 01:32:26 +00001984 OUT("if(set_value) {\n");
1985 INDENT(+1);
vlm337167e2005-11-26 11:25:14 +00001986 OUT("/* Install default value %" PRIdASN " */\n",
1987 expr->marker.default_value->value.v_integer);
1988 if(fits_long) {
1989 OUT("*st = %" PRIdASN ";\n",
1990 expr->marker.default_value->value.v_integer);
1991 OUT("return 0;\n");
1992 } else {
1993 OUT("return asn_long2INTEGER(st, %" PRIdASN ");\n",
1994 expr->marker.default_value->value.v_integer);
1995 }
1996 INDENT(-1);
vlm9ea7d412006-08-18 01:32:26 +00001997 OUT("} else {\n");
1998 INDENT(+1);
1999 OUT("/* Test default value %" PRIdASN " */\n",
2000 expr->marker.default_value->value.v_integer);
2001 if(fits_long) {
2002 OUT("return (*st == %" PRIdASN ");\n",
2003 expr->marker.default_value->value.v_integer);
2004 } else {
2005 OUT("long value;\n");
2006 OUT("if(asn_INTEGER2long(st, &value))\n");
2007 OUT("\treturn -1;\n");
2008 OUT("return (value == %" PRIdASN ");\n",
2009 expr->marker.default_value->value.v_integer);
2010 }
2011 INDENT(-1);
2012 OUT("}\n");
2013 INDENT(-1);
vlm337167e2005-11-26 11:25:14 +00002014 OUT("}\n");
2015 REDIR(save_target);
2016 return 1;
2017 case ASN_BASIC_NULL:
2018 //expr->marker.flags &= ~EM_INDIRECT;
2019 return 0;
2020 default:
2021 break;
2022 }
2023 return 0;
2024}
2025
vlmb2839012004-08-20 13:37:01 +00002026static int
vlm4e554992004-08-25 02:03:12 +00002027emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00002028 int save_target;
2029 arg_t tmp_arg;
vlm060fe2a2004-09-10 09:37:12 +00002030 struct asn1p_type_tag_s outmost_tag_s;
2031 struct asn1p_type_tag_s *outmost_tag;
vlm80a48592005-02-25 12:10:27 +00002032 int complex_contents;
vlmb2839012004-08-20 13:37:01 +00002033 char *p;
2034
vlm060fe2a2004-09-10 09:37:12 +00002035 if(asn1f_fetch_outmost_tag(arg->asn,
vlm337167e2005-11-26 11:25:14 +00002036 expr->module, expr, &outmost_tag_s,
2037 AFT_IMAGINARY_ANY)) {
vlm060fe2a2004-09-10 09:37:12 +00002038 outmost_tag = 0;
2039 } else {
2040 outmost_tag = &outmost_tag_s;
2041 }
2042
vlmb2839012004-08-20 13:37:01 +00002043 OUT("{ ");
vlm060fe2a2004-09-10 09:37:12 +00002044
2045 if(outmost_tag && outmost_tag->tag_value == -1)
2046 OUT("ATF_OPEN_TYPE | ");
vlm0b567bf2005-03-04 22:18:20 +00002047 OUT("%s, ",
2048 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
vlm6c4a8502005-08-18 13:38:19 +00002049 if((expr->marker.flags & EM_OMITABLE) == EM_OMITABLE) {
vlmb2839012004-08-20 13:37:01 +00002050 asn1p_expr_t *tv;
2051 int opts = 0;
vlm6c4a8502005-08-18 13:38:19 +00002052 for(tv = expr;
2053 tv && (tv->marker.flags & EM_OMITABLE) == EM_OMITABLE;
vlmb2839012004-08-20 13:37:01 +00002054 tv = TQ_NEXT(tv, next), opts++) {
2055 if(tv->expr_type == A1TC_EXTENSIBLE)
2056 opts--;
2057 }
2058 OUT("%d, ", opts);
2059 } else {
2060 OUT("0, ");
2061 }
vlm39ba4c42004-09-22 16:06:28 +00002062 if(expr->_anonymous_type) {
2063 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
2064 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
2065 OUT("0,\n");
2066 } else {
vlm766cf1e2005-03-04 08:48:53 +00002067 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +00002068 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +00002069 OUT(", ");
vlmb2839012004-08-20 13:37:01 +00002070 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
2071 && (!UNNAMED_UNIONS)) OUT("choice.");
vlm0c6d3812006-03-21 03:40:38 +00002072 OUT("%s),\n", MKID_safe(expr));
vlmb2839012004-08-20 13:37:01 +00002073 }
2074 INDENT(+1);
2075 if(C99_MODE) OUT(".tag = ");
vlm060fe2a2004-09-10 09:37:12 +00002076 if(outmost_tag) {
2077 if(outmost_tag->tag_value == -1)
2078 OUT("-1 /* Ambiguous tag (ANY?) */");
2079 else
2080 _print_tag(arg, outmost_tag);
vlma8a86cc2004-09-10 06:07:18 +00002081 } else {
vlm060fe2a2004-09-10 09:37:12 +00002082 OUT("-1 /* Ambiguous tag (CHOICE?) */");
vlma8a86cc2004-09-10 06:07:18 +00002083 }
vlm060fe2a2004-09-10 09:37:12 +00002084
vlmb2839012004-08-20 13:37:01 +00002085 OUT(",\n");
2086 if(C99_MODE) OUT(".tag_mode = ");
2087 if(expr->tag.tag_class) {
2088 if(expr->tag.tag_mode == TM_IMPLICIT)
2089 OUT("-1,\t/* IMPLICIT tag at current level */\n");
2090 else
2091 OUT("+1,\t/* EXPLICIT tag at current level */\n");
2092 } else {
2093 OUT("0,\n");
2094 }
vlm80a48592005-02-25 12:10:27 +00002095
2096 complex_contents =
2097 (expr->expr_type & ASN_CONSTR_MASK)
2098 || expr->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +00002099 || (0 /* -- prohibited by X.693:8.3.4 */
2100 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00002101 && expr_elements_count(arg, expr));
vlmb2839012004-08-20 13:37:01 +00002102 if(C99_MODE) OUT(".type = ");
vlme0fea712005-08-29 10:31:14 +00002103 OUT("&asn_DEF_");
vlm766cf1e2005-03-04 08:48:53 +00002104 if(complex_contents) {
vlm0c6d3812006-03-21 03:40:38 +00002105 OUT("%s", MKID(expr));
vlm766cf1e2005-03-04 08:48:53 +00002106 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
2107 OUT("_%d", expr->_type_unique_index);
vlmdae7f9d2004-08-22 03:25:24 +00002108 } else {
vlm766cf1e2005-03-04 08:48:53 +00002109 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
vlmdae7f9d2004-08-22 03:25:24 +00002110 }
vlm766cf1e2005-03-04 08:48:53 +00002111 OUT(",\n");
vlmb2839012004-08-20 13:37:01 +00002112 if(C99_MODE) OUT(".memb_constraints = ");
2113 if(expr->constraints) {
vlm86f5ed22004-09-26 13:11:31 +00002114 if(arg->flags & A1C_NO_CONSTRAINTS) {
2115 OUT("0,\t/* No check because of -fno-constraints */\n");
2116 } else {
vlm0c6d3812006-03-21 03:40:38 +00002117 char *id = MKID(expr);
vlm86f5ed22004-09-26 13:11:31 +00002118 if(expr->_anonymous_type
vlmbf3c5112005-02-14 20:41:29 +00002119 && !strcmp(expr->Identifier, "Member"))
vlm86f5ed22004-09-26 13:11:31 +00002120 id = asn1c_type_name(arg, expr, TNF_SAFE);
vlm337167e2005-11-26 11:25:14 +00002121 OUT("memb_%s_constraint_%d,\n", id,
vlm766cf1e2005-03-04 08:48:53 +00002122 arg->expr->_type_unique_index);
vlm86f5ed22004-09-26 13:11:31 +00002123 }
vlmb2839012004-08-20 13:37:01 +00002124 } else {
vlm95a476e2005-01-17 12:16:58 +00002125 OUT("0,\t/* Defer constraints checking to the member type */\n");
vlmb2839012004-08-20 13:37:01 +00002126 }
vlm337167e2005-11-26 11:25:14 +00002127 if(C99_MODE) OUT(".per_constraints = ");
2128 if(arg->flags & A1C_GEN_PER) {
2129 if(expr->constraints
2130 || expr->expr_type == ASN_BASIC_ENUMERATED
2131 || expr->expr_type == ASN_CONSTR_CHOICE) {
2132 OUT("&asn_PER_memb_%s_constr_%d,\n",
vlm0c6d3812006-03-21 03:40:38 +00002133 MKID(expr),
vlm337167e2005-11-26 11:25:14 +00002134 expr->_type_unique_index);
2135 } else {
2136 OUT("0,\t/* No PER visible constraints */\n");
2137 }
vlmbf3c5112005-02-14 20:41:29 +00002138 } else {
vlm337167e2005-11-26 11:25:14 +00002139 OUT("0,\t/* PER is not compiled, use -gen-PER */\n");
2140 }
2141 if(C99_MODE) OUT(".default_value = ");
2142 if(try_inline_default(arg, expr, 0)) {
2143 OUT("asn_DFL_%d_set_%" PRIdASN
2144 ",\t/* DEFAULT %" PRIdASN " */\n",
2145 expr->_type_unique_index,
2146 expr->marker.default_value->value.v_integer,
2147 expr->marker.default_value->value.v_integer);
2148 } else {
2149 OUT("0,\n");
2150 }
2151 if(C99_MODE) OUT(".name = ");
2152 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member")) {
2153 OUT("\"\"\n");
2154 } else {
2155 OUT("\"%s\"\n", expr->Identifier);
vlmbf3c5112005-02-14 20:41:29 +00002156 }
vlmb2839012004-08-20 13:37:01 +00002157 OUT("},\n");
2158 INDENT(-1);
2159
vlm86f5ed22004-09-26 13:11:31 +00002160 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlmb2839012004-08-20 13:37:01 +00002161 return 0;
2162
2163 save_target = arg->target->target;
2164 REDIR(OT_CODE);
2165
vlmbf3c5112005-02-14 20:41:29 +00002166 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
vlmb2839012004-08-20 13:37:01 +00002167 p = asn1c_type_name(arg, expr, TNF_SAFE);
vlm39ba4c42004-09-22 16:06:28 +00002168 else
vlm0c6d3812006-03-21 03:40:38 +00002169 p = MKID(expr);
vlmb2839012004-08-20 13:37:01 +00002170 OUT("static int\n");
vlm337167e2005-11-26 11:25:14 +00002171 OUT("memb_%s_constraint_%d(asn_TYPE_descriptor_t *td, const void *sptr,\n", p, arg->expr->_type_unique_index);
vlmb2839012004-08-20 13:37:01 +00002172 INDENT(+1);
vlmaf68ef52006-07-13 11:19:01 +00002173 OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {\n");
vlmb2839012004-08-20 13:37:01 +00002174 tmp_arg = *arg;
2175 tmp_arg.expr = expr;
2176 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
vlm86f5ed22004-09-26 13:11:31 +00002177 OUT("return td->check_constraints"
vlmaf68ef52006-07-13 11:19:01 +00002178 "(td, sptr, ctfailcb, app_key);\n");
vlmb2839012004-08-20 13:37:01 +00002179 }
2180 INDENT(-1);
2181 OUT("}\n");
2182 OUT("\n");
2183
2184 REDIR(save_target);
2185
2186 return 0;
2187}
vlm4e554992004-08-25 02:03:12 +00002188
vlm9de248e2004-10-20 15:50:55 +00002189/*
2190 * Generate "asn_DEF_XXX" type definition.
2191 */
vlm4e554992004-08-25 02:03:12 +00002192static int
vlm86f5ed22004-09-26 13:11:31 +00002193emit_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 +00002194 int using_type_name = 0;
vlm0c6d3812006-03-21 03:40:38 +00002195 char *p = MKID(expr);
vlm337167e2005-11-26 11:25:14 +00002196
2197 if((arg->flags & A1C_GEN_PER)
2198 && (expr->constraints
2199 || expr->expr_type == ASN_BASIC_ENUMERATED
2200 || expr->expr_type == ASN_CONSTR_CHOICE)
2201 ) {
2202 OUT("static asn_per_constraints_t asn_PER_%s_constr_%d = {\n",
2203 p, expr->_type_unique_index);
2204 if(emit_single_member_PER_constraints(arg, expr))
2205 return -1;
2206 OUT("};\n");
2207 }
vlm4e554992004-08-25 02:03:12 +00002208
vlm12c8f692004-09-06 08:07:29 +00002209 if(HIDE_INNER_DEFS)
2210 OUT("static /* Use -fall-defs-global to expose */\n");
vlm337167e2005-11-26 11:25:14 +00002211 OUT("asn_TYPE_descriptor_t asn_DEF_%s", p);
vlm766cf1e2005-03-04 08:48:53 +00002212 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
2213 OUT(" = {\n");
vlm72425de2004-09-13 08:31:01 +00002214 INDENT(+1);
vlm337167e2005-11-26 11:25:14 +00002215
2216 if(expr->_anonymous_type) {
2217 p = ASN_EXPR_TYPE2STR(expr->expr_type);
2218 OUT("\"%s\",\n", p?p:"");
vlm0c6d3812006-03-21 03:40:38 +00002219 OUT("\"%s\",\n",
2220 p ? asn1c_make_identifier(AMI_CHECK_RESERVED,
2221 0, p, 0) : "");
vlm337167e2005-11-26 11:25:14 +00002222 } else {
2223 OUT("\"%s\",\n", expr->Identifier);
2224 OUT("\"%s\",\n", expr->Identifier);
2225 }
vlm4e554992004-08-25 02:03:12 +00002226
2227 if(expr->expr_type & ASN_CONSTR_MASK) {
vlm766cf1e2005-03-04 08:48:53 +00002228 using_type_name = 1;
vlm4e554992004-08-25 02:03:12 +00002229 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
vlm337167e2005-11-26 11:25:14 +00002230 } else {
vlm0c6d3812006-03-21 03:40:38 +00002231 p = MKID(expr);
vlm4e554992004-08-25 02:03:12 +00002232 }
2233
vlm766cf1e2005-03-04 08:48:53 +00002234#define FUNCREF(foo) do { \
2235 OUT("%s", p); \
2236 if(HIDE_INNER_DEFS && !using_type_name) \
2237 OUT("_%d", expr->_type_unique_index); \
2238 OUT("_" #foo ",\n"); \
2239} while(0)
2240
2241 FUNCREF(free);
2242 FUNCREF(print);
2243 FUNCREF(constraint);
2244 FUNCREF(decode_ber);
2245 FUNCREF(encode_der);
2246 FUNCREF(decode_xer);
2247 FUNCREF(encode_xer);
vlm9ea7d412006-08-18 01:32:26 +00002248 if(arg->flags & A1C_GEN_PER) {
vlm337167e2005-11-26 11:25:14 +00002249 FUNCREF(decode_uper);
vlm9ea7d412006-08-18 01:32:26 +00002250 FUNCREF(encode_uper);
2251 } else {
2252 OUT("0, 0,\t/* No PER support, "
2253 "use \"-gen-PER\" to enable */\n");
2254 }
vlm4e554992004-08-25 02:03:12 +00002255
vlm4e554992004-08-25 02:03:12 +00002256 if(expr->expr_type == ASN_CONSTR_CHOICE) {
2257 OUT("CHOICE_outmost_tag,\n");
2258 } else {
2259 OUT("0,\t/* Use generic outmost tag fetcher */\n");
2260 }
2261
vlm0c6d3812006-03-21 03:40:38 +00002262 p = MKID(expr);
vlm4e554992004-08-25 02:03:12 +00002263 if(tags_count) {
vlm337167e2005-11-26 11:25:14 +00002264 OUT("asn_DEF_%s_tags_%d,\n",
vlm766cf1e2005-03-04 08:48:53 +00002265 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002266 OUT("sizeof(asn_DEF_%s_tags_%d)\n",
vlm766cf1e2005-03-04 08:48:53 +00002267 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002268 OUT("\t/sizeof(asn_DEF_%s_tags_%d[0])",
vlm766cf1e2005-03-04 08:48:53 +00002269 p, expr->_type_unique_index);
vlm72425de2004-09-13 08:31:01 +00002270 if(tv_mode == _TVM_SUBSET
2271 && tags_count != all_tags_count)
2272 OUT(" - %d", all_tags_count - tags_count);
2273 OUT(", /* %d */\n", tags_count);
vlm4e554992004-08-25 02:03:12 +00002274 } else {
vlm72425de2004-09-13 08:31:01 +00002275 OUT("0,\t/* No effective tags (pointer) */\n");
2276 OUT("0,\t/* No effective tags (count) */\n");
2277 }
2278
2279 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
vlm337167e2005-11-26 11:25:14 +00002280 OUT("asn_DEF_%s_all_tags_%d,\n",
vlm766cf1e2005-03-04 08:48:53 +00002281 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002282 OUT("sizeof(asn_DEF_%s_all_tags_%d)\n",
vlm766cf1e2005-03-04 08:48:53 +00002283 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002284 OUT("\t/sizeof(asn_DEF_%s_all_tags_%d[0]), /* %d */\n",
vlm766cf1e2005-03-04 08:48:53 +00002285 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00002286 } else if(all_tags_count) {
vlm337167e2005-11-26 11:25:14 +00002287 OUT("asn_DEF_%s_tags_%d,\t/* Same as above */\n",
vlm766cf1e2005-03-04 08:48:53 +00002288 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002289 OUT("sizeof(asn_DEF_%s_tags_%d)\n",
vlm766cf1e2005-03-04 08:48:53 +00002290 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002291 OUT("\t/sizeof(asn_DEF_%s_tags_%d[0]), /* %d */\n",
vlm766cf1e2005-03-04 08:48:53 +00002292 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00002293 } else {
2294 OUT("0,\t/* No tags (pointer) */\n");
2295 OUT("0,\t/* No tags (count) */\n");
vlm4e554992004-08-25 02:03:12 +00002296 }
2297
vlm337167e2005-11-26 11:25:14 +00002298 if(arg->flags & A1C_GEN_PER) {
2299 if(expr->constraints
2300 || expr->expr_type == ASN_BASIC_ENUMERATED
2301 || expr->expr_type == ASN_CONSTR_CHOICE) {
2302 OUT("&asn_PER_%s_constr_%d,\n",
2303 p, expr->_type_unique_index);
2304 } else {
2305 OUT("0,\t/* No PER visible constraints */\n");
2306 }
2307 } else {
2308 OUT("0,\t/* No PER visible constraints */\n");
2309 }
2310
vlm4e554992004-08-25 02:03:12 +00002311 if(elements_count) {
vlm766cf1e2005-03-04 08:48:53 +00002312 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00002313 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
2314 || expr->expr_type == ASN_CONSTR_SET_OF) {
2315 OUT("%d,\t/* Single element */\n",
2316 elements_count);
2317 assert(elements_count == 1);
2318 } else {
2319 OUT("%d,\t/* Elements count */\n",
2320 elements_count);
2321 }
2322 } else {
vlm4e554992004-08-25 02:03:12 +00002323 if(expr_elements_count(arg, expr))
2324 OUT("0, 0,\t/* Defined elsewhere */\n");
2325 else
2326 OUT("0, 0,\t/* No members */\n");
2327 }
2328
2329 switch(spec) {
2330 case ETD_NO_SPECIFICS:
2331 OUT("0\t/* No specifics */\n");
2332 break;
2333 case ETD_HAS_SPECIFICS:
vlm337167e2005-11-26 11:25:14 +00002334 OUT("&asn_SPC_%s_specs_%d\t/* Additional specs */\n",
vlm766cf1e2005-03-04 08:48:53 +00002335 p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00002336 }
vlm72425de2004-09-13 08:31:01 +00002337 INDENT(-1);
vlm4e554992004-08-25 02:03:12 +00002338 OUT("};\n");
2339 OUT("\n");
vlme2bb4432004-08-26 06:20:34 +00002340
2341 return 0;
vlm4e554992004-08-25 02:03:12 +00002342}
vlmddd5a7d2004-09-10 09:18:20 +00002343
vlm39ba4c42004-09-22 16:06:28 +00002344static int
2345expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
vlm6d44a542005-11-08 03:06:16 +00002346 /*
2347 * X.680, 25.5, Table 5
2348 */
vlm337167e2005-11-26 11:25:14 +00002349 switch(expr_get_type(arg, expr)) {
vlm39ba4c42004-09-22 16:06:28 +00002350 case ASN_BASIC_BOOLEAN:
2351 case ASN_BASIC_ENUMERATED:
2352 case ASN_BASIC_NULL:
2353 return 1;
vlm6d44a542005-11-08 03:06:16 +00002354 case ASN_CONSTR_CHOICE:
2355 return 2;
vlm39ba4c42004-09-22 16:06:28 +00002356 default:
2357 return 0;
2358 }
2359}
vlmaf841972005-01-28 12:18:50 +00002360
2361static int
vlm2e774282005-08-14 15:03:31 +00002362out_name_chain(arg_t *arg, enum onc_flags onc_flags) {
vlmaf841972005-01-28 12:18:50 +00002363 asn1p_expr_t *expr = arg->expr;
2364 char *id;
2365
2366 assert(expr->Identifier);
2367
vlm2e774282005-08-14 15:03:31 +00002368 if((arg->flags & A1C_COMPOUND_NAMES
2369 || onc_flags & ONC_force_compound_name)
vlm766cf1e2005-03-04 08:48:53 +00002370 && ((expr->expr_type & ASN_CONSTR_MASK)
2371 || expr->expr_type == ASN_BASIC_ENUMERATED
vlm96853d82005-08-13 23:51:47 +00002372 || ((expr->expr_type == ASN_BASIC_INTEGER
2373 || expr->expr_type == ASN_BASIC_BIT_STRING)
vlm766cf1e2005-03-04 08:48:53 +00002374 && expr_elements_count(arg, expr))
2375 )
vlmaf841972005-01-28 12:18:50 +00002376 && expr->parent_expr
2377 && expr->parent_expr->Identifier) {
2378 arg_t tmparg = *arg;
2379
2380 tmparg.expr = expr->parent_expr;
vlm766cf1e2005-03-04 08:48:53 +00002381 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
2382
vlm2e774282005-08-14 15:03:31 +00002383 out_name_chain(&tmparg, onc_flags);
vlmaf841972005-01-28 12:18:50 +00002384
vlm92d3cad2005-03-05 00:08:41 +00002385 OUT("__"); /* a separator between id components */
vlm766cf1e2005-03-04 08:48:53 +00002386
vlmaf841972005-01-28 12:18:50 +00002387 /* Fall through */
2388 }
2389
vlm2e774282005-08-14 15:03:31 +00002390 if(onc_flags & ONC_avoid_keywords)
vlm0c6d3812006-03-21 03:40:38 +00002391 id = MKID_safe(expr);
vlmaf841972005-01-28 12:18:50 +00002392 else
vlm0c6d3812006-03-21 03:40:38 +00002393 id = MKID(expr);
vlmaf841972005-01-28 12:18:50 +00002394 OUT("%s", id);
2395
2396 return 0;
2397}
vlm6c4a8502005-08-18 13:38:19 +00002398
2399static int
2400emit_include_dependencies(arg_t *arg) {
2401 asn1p_expr_t *expr = arg->expr;
2402 asn1p_expr_t *memb;
2403
2404 /* Avoid recursive definitions. */
2405 TQ_FOR(memb, &(expr->members), next) {
2406 expr_break_recursion(arg, memb);
2407 }
2408
2409 TQ_FOR(memb, &(expr->members), next) {
2410
2411 if(memb->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
2412 if(terminal_structable(arg, memb)) {
2413 int saved_target = arg->target->target;
2414 REDIR(OT_FWD_DECLS);
2415 OUT("%s;\n",
2416 asn1c_type_name(arg, memb, TNF_RSAFE));
2417 REDIR(saved_target);
2418 }
2419 }
2420
2421 if((!(memb->expr_type & ASN_CONSTR_MASK)
2422 && memb->expr_type > ASN_CONSTR_MASK)
2423 || memb->meta_type == AMT_TYPEREF) {
2424 if(memb->marker.flags & EM_UNRECURSE) {
2425 GEN_POSTINCLUDE(asn1c_type_name(arg,
2426 memb, TNF_INCLUDE));
2427 } else {
2428 GEN_INCLUDE(asn1c_type_name(arg,
2429 memb, TNF_INCLUDE));
2430 }
2431 }
2432 }
2433
2434 return 0;
2435}
2436
2437/*
2438 * Check if it is better to make this type indirectly accessed via
2439 * a pointer.
2440 * This may be the case for the following recursive definition:
2441 * Type ::= CHOICE { member Type };
2442 */
2443static int
2444expr_break_recursion(arg_t *arg, asn1p_expr_t *expr) {
vlm6c4a8502005-08-18 13:38:19 +00002445 int ret;
2446
2447 if(expr->marker.flags & EM_UNRECURSE)
2448 return 1; /* Already broken */
2449
vlm6c4a8502005-08-18 13:38:19 +00002450 /* -findirect-choice compiles members of CHOICE as indirect pointers */
2451 if((arg->flags & A1C_INDIRECT_CHOICE)
2452 && arg->expr->expr_type == ASN_CONSTR_CHOICE
vlm337167e2005-11-26 11:25:14 +00002453 && (expr_get_type(arg, expr) & ASN_CONSTR_MASK)
vlm6c4a8502005-08-18 13:38:19 +00002454 ) {
2455 /* Break cross-reference */
2456 expr->marker.flags |= EM_INDIRECT | EM_UNRECURSE;
2457 return 1;
2458 }
2459
2460 if((expr->marker.flags & EM_INDIRECT)
2461 || arg->expr->expr_type == ASN_CONSTR_SET_OF
2462 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) {
2463 if(terminal_structable(arg, expr)) {
2464 expr->marker.flags |= EM_UNRECURSE;
2465
2466 if(arg->expr->expr_type == ASN_CONSTR_SET_OF
2467 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) {
2468 /* Don't put EM_INDIRECT even if recursion */
2469 return 1;
2470 }
2471
2472 /* Fall through */
2473 }
2474 }
2475
2476 /* Look for recursive back-references */
2477 ret = expr_defined_recursively(arg, expr);
2478 switch(ret) {
2479 case 2: /* Explicitly break the recursion */
2480 case 1: /* Use safer typing */
2481 expr->marker.flags |= EM_INDIRECT;
2482 expr->marker.flags |= EM_UNRECURSE;
2483 break;
2484 }
2485
2486 return 0;
2487}
2488
2489/*
2490 * Check if the type can be represented using simple `struct TYPE`.
2491 */
2492static asn1p_expr_t *
2493terminal_structable(arg_t *arg, asn1p_expr_t *expr) {
2494 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
2495 if(terminal
2496 && !terminal->parent_expr
2497 && (terminal->expr_type & ASN_CONSTR_MASK)) {
2498 return terminal;
2499 }
2500 return 0;
2501}
2502
2503static int
2504asn1c_recurse(arg_t *arg, asn1p_expr_t *expr, int (*callback)(arg_t *arg, void *key), void *key) {
2505 arg_t tmp = *arg;
2506 int maxret = 0;
2507 int ret;
2508
2509 if(expr->_mark) return 0;
2510 expr->_mark |= TM_RECURSION;
2511
2512 /* Invoke callback for every type going into recursion */
2513 tmp.expr = expr;
2514 maxret = callback(&tmp, key);
2515 if(maxret <= 1) {
2516 /*
2517 * Recursively invoke myself and the callbacks.
2518 */
2519 TQ_FOR(tmp.expr, &(expr->members), next) {
2520 ret = asn1c_recurse(&tmp, tmp.expr, callback, key);
2521 if(ret > maxret)
2522 maxret = ret;
2523 if(maxret > 1) break;
2524 }
2525 }
2526
2527 expr->_mark &= ~TM_RECURSION;
2528 return maxret;
2529}
2530
2531static int
2532check_is_refer_to(arg_t *arg, void *key) {
2533 asn1p_expr_t *terminal = terminal_structable(arg, arg->expr);
2534 if(terminal == key) {
2535 if(arg->expr->marker.flags & EM_INDIRECT)
2536 return 1; /* This is almost safe indirection */
2537 return 2;
2538 } else if(terminal) {
2539 /* This might be N-step circular loop. Dive into it. */
2540 return asn1c_recurse(arg, terminal, check_is_refer_to, key);
2541 }
2542 return 0;
2543}
2544
2545/*
2546 * Check if the possibly inner expression defined recursively.
2547 */
2548static int
2549expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr) {
2550 asn1p_expr_t *terminal;
2551 asn1p_expr_t *topmost;
2552
2553 /* If expression is top-level, there's no way it can be recursive. */
2554 if(expr->parent_expr == 0) return 0;
2555 if(expr->expr_type != A1TC_REFERENCE)
2556 return 0; /* Basic types are never recursive */
2557
2558 terminal = terminal_structable(arg, expr);
2559 if(!terminal) return 0; /* Terminal cannot be indirected */
2560
2561 /* Search for the parent container for the given expression */
2562 topmost = expr;
2563 while(topmost->parent_expr)
2564 topmost = topmost->parent_expr;
2565
2566 /* Look inside the terminal type if it mentions the parent expression */
2567 return asn1c_recurse(arg, terminal, check_is_refer_to, topmost);
2568}
vlm337167e2005-11-26 11:25:14 +00002569
2570struct canonical_map_element {
2571 int eidx;
2572 asn1p_expr_t *expr;
2573};
2574static int compar_cameo(const void *ap, const void *bp);
2575static arg_t *cameo_arg;
2576static int *
2577compute_canonical_members_order(arg_t *arg, int el_count) {
2578 struct canonical_map_element *cmap;
2579 int *rmap;
2580 asn1p_expr_t *v;
2581 int eidx = 0;
2582 int ext_start = -1;
2583 int nextmax = -1;
2584 int already_sorted = 1;
2585
2586 cmap = calloc(el_count, sizeof *cmap);
2587 assert(cmap);
2588
2589 TQ_FOR(v, &(arg->expr->members), next) {
2590 if(v->expr_type != A1TC_EXTENSIBLE) {
2591 cmap[eidx].eidx = eidx;
2592 cmap[eidx].expr = v;
2593 eidx++;
2594 } else if(ext_start == -1)
2595 ext_start = eidx;
2596 }
2597
2598 cameo_arg = arg;
2599 if(ext_start == -1) {
2600 /* Sort the whole thing */
2601 qsort(cmap, el_count, sizeof(*cmap), compar_cameo);
2602 } else {
2603 /* Sort root and extensions independently */
2604 qsort(cmap, ext_start, sizeof(*cmap), compar_cameo);
2605 qsort(cmap + ext_start, el_count - ext_start,
2606 sizeof(*cmap), compar_cameo);
2607 }
2608
2609 /* move data back to a simpler map */
2610 rmap = calloc(el_count, sizeof *rmap);
2611 assert(rmap);
2612 for(eidx = 0; eidx < el_count; eidx++) {
2613 rmap[eidx] = cmap[eidx].eidx;
2614 if(rmap[eidx] <= nextmax)
2615 already_sorted = 0;
2616 else
2617 nextmax = rmap[eidx];
2618 }
2619 free(cmap);
2620
2621 if(already_sorted) { free(rmap); rmap = 0; }
2622 return rmap;
2623}
2624static int compar_cameo(const void *ap, const void *bp) {
2625 const struct canonical_map_element *a = (const void *)ap;
2626 const struct canonical_map_element *b = (const void *)bp;
2627 struct asn1p_type_tag_s atag, btag;
2628 arg_t *arg = cameo_arg;
2629
2630 if(asn1f_fetch_outmost_tag(arg->asn, a->expr->module, a->expr,
2631 &atag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE))
2632 return 1;
2633
2634 if(asn1f_fetch_outmost_tag(arg->asn, b->expr->module, b->expr,
2635 &btag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE))
2636 return -1;
2637
2638 if(atag.tag_class < btag.tag_class)
2639 return -1;
2640 if(atag.tag_class > btag.tag_class)
2641 return 1;
2642 if(atag.tag_value < btag.tag_value)
2643 return -1;
2644 if(atag.tag_value > btag.tag_value)
2645 return 1;
2646 return 0;
2647
2648}