blob: d8aa01fbbcbadae1ae21573f0ffe838c473e1c20 [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);
vlm86380d32006-10-09 12:07:58 +000043static int emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int juscountvalues, char *type);
vlma0da1a32006-10-09 12:27:44 +000044static int emit_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx);
vlm4e554992004-08-25 02:03:12 +000045static int emit_member_table(arg_t *arg, asn1p_expr_t *expr);
vlm940bc6b2004-10-03 09:13:30 +000046static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier);
vlm0b567bf2005-03-04 22:18:20 +000047static int emit_include_dependencies(arg_t *arg);
vlm6c4a8502005-08-18 13:38:19 +000048static asn1p_expr_t *terminal_structable(arg_t *arg, asn1p_expr_t *expr);
49static int expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr);
50static 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 +000051static asn1p_expr_type_e expr_get_type(arg_t *arg, asn1p_expr_t *expr);
52static int try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out);
53static int *compute_canonical_members_order(arg_t *arg, int el_count);
vlmfa67ddc2004-06-03 03:38:44 +000054
vlm72425de2004-09-13 08:31:01 +000055enum tvm_compat {
56 _TVM_SAME = 0, /* tags and all_tags are same */
57 _TVM_SUBSET = 1, /* tags are subset of all_tags */
58 _TVM_DIFFERENT = 2, /* tags and all_tags are different */
59};
60static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc);
61
vlm4e554992004-08-25 02:03:12 +000062enum etd_spec {
63 ETD_NO_SPECIFICS,
64 ETD_HAS_SPECIFICS
65};
vlm86f5ed22004-09-26 13:11:31 +000066static 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 +000067
vlmb2839012004-08-20 13:37:01 +000068#define C99_MODE (!(arg->flags & A1C_NO_C99))
vlmfa67ddc2004-06-03 03:38:44 +000069#define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS)
vlm12c8f692004-09-06 08:07:29 +000070#define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL))
vlmfa67ddc2004-06-03 03:38:44 +000071
72#define PCTX_DEF INDENTED( \
73 OUT("\n"); \
74 OUT("/* Context for parsing across buffer boundaries */\n"); \
vlma5dcb912004-09-29 13:16:40 +000075 OUT("asn_struct_ctx_t _asn_ctx;\n"));
vlmfa67ddc2004-06-03 03:38:44 +000076
vlm0b567bf2005-03-04 22:18:20 +000077
vlm33a4ff12004-08-11 05:21:32 +000078#define DEPENDENCIES do { \
vlm0b567bf2005-03-04 22:18:20 +000079 emit_include_dependencies(arg); \
vlm33a4ff12004-08-11 05:21:32 +000080 if(expr->expr_type == ASN_CONSTR_SET_OF) \
vlmea226772006-09-13 02:51:20 +000081 GEN_INCLUDE_STD("asn_SET_OF"); \
vlm33a4ff12004-08-11 05:21:32 +000082 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \
vlmea226772006-09-13 02:51:20 +000083 GEN_INCLUDE_STD("asn_SEQUENCE_OF"); \
vlm33a4ff12004-08-11 05:21:32 +000084} while(0)
vlmfa67ddc2004-06-03 03:38:44 +000085
vlm1f7df782005-03-04 23:48:19 +000086/* MKID_safe() without checking for reserved keywords */
vlm0c6d3812006-03-21 03:40:38 +000087#define MKID(expr) (asn1c_make_identifier(0, expr, 0))
88#define MKID_safe(expr) (asn1c_make_identifier(AMI_CHECK_RESERVED, expr, 0))
vlmfa67ddc2004-06-03 03:38:44 +000089
90int
vlmf9d178d2004-09-14 12:47:45 +000091asn1c_lang_C_type_REAL(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +000092 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
93}
94
vlm80a48592005-02-25 12:10:27 +000095struct value2enum {
96 asn1c_integer_t value;
97 const char *name;
98 int idx;
99};
100static int compar_enumMap_byName(const void *ap, const void *bp) {
101 const struct value2enum *a = (const struct value2enum *)ap;
102 const struct value2enum *b = (const struct value2enum *)bp;
103 return strcmp(a->name, b->name);
104}
105static int compar_enumMap_byValue(const void *ap, const void *bp) {
106 const struct value2enum *a = (const struct value2enum *)ap;
107 const struct value2enum *b = (const struct value2enum *)bp;
108 if(a->value < b->value)
109 return -1;
110 else if(a->value == b->value)
111 return 0;
112 return 1;
113}
114
vlmfa67ddc2004-06-03 03:38:44 +0000115int
vlmf9d178d2004-09-14 12:47:45 +0000116asn1c_lang_C_type_common_INTEGER(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000117 asn1p_expr_t *expr = arg->expr;
118 asn1p_expr_t *v;
vlm80a48592005-02-25 12:10:27 +0000119 int el_count = expr_elements_count(arg, expr);
120 struct value2enum *v2e;
vlm337167e2005-11-26 11:25:14 +0000121 int map_extensions = (expr->expr_type == ASN_BASIC_INTEGER);
vlm4573af02005-08-13 23:30:24 +0000122 int eidx;
vlmfa67ddc2004-06-03 03:38:44 +0000123
vlm80a48592005-02-25 12:10:27 +0000124 v2e = alloca((el_count + 1) * sizeof(*v2e));
vlmfa67ddc2004-06-03 03:38:44 +0000125
vlm80a48592005-02-25 12:10:27 +0000126 /*
vlm4573af02005-08-13 23:30:24 +0000127 * For all ENUMERATED types and for those INTEGER types which
128 * have identifiers, print out an enumeration table.
vlm80a48592005-02-25 12:10:27 +0000129 */
vlm4573af02005-08-13 23:30:24 +0000130 if(expr->expr_type == ASN_BASIC_ENUMERATED || el_count) {
131 eidx = 0;
vlm80a48592005-02-25 12:10:27 +0000132 REDIR(OT_DEPS);
vlm766cf1e2005-03-04 08:48:53 +0000133 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000134 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000135 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000136 TQ_FOR(v, &(expr->members), next) {
137 switch(v->expr_type) {
138 case A1TC_UNIVERVAL:
vlm766cf1e2005-03-04 08:48:53 +0000139 OUT("\t");
vlm2e774282005-08-14 15:03:31 +0000140 out_name_chain(arg, ONC_noflags);
vlm0c6d3812006-03-21 03:40:38 +0000141 OUT("_%s", MKID(v));
vlm766cf1e2005-03-04 08:48:53 +0000142 OUT("\t= %" PRIdASN "%s\n",
vlm80a48592005-02-25 12:10:27 +0000143 v->value->value.v_integer,
vlm766cf1e2005-03-04 08:48:53 +0000144 (eidx+1 < el_count) ? "," : "");
vlm80a48592005-02-25 12:10:27 +0000145 v2e[eidx].name = v->Identifier;
146 v2e[eidx].value = v->value->value.v_integer;
147 eidx++;
vlmfa67ddc2004-06-03 03:38:44 +0000148 break;
vlmf9d178d2004-09-14 12:47:45 +0000149 case A1TC_EXTENSIBLE:
150 OUT("\t/*\n");
151 OUT("\t * Enumeration is extensible\n");
152 OUT("\t */\n");
vlm337167e2005-11-26 11:25:14 +0000153 if(!map_extensions)
154 map_extensions = eidx + 1;
vlmf9d178d2004-09-14 12:47:45 +0000155 break;
vlmfa67ddc2004-06-03 03:38:44 +0000156 default:
157 return -1;
158 }
159 }
vlm7c8aa5e2006-03-21 07:25:18 +0000160 OUT("} e_");
vlm2e774282005-08-14 15:03:31 +0000161 out_name_chain(arg, ONC_noflags);
vlm7c8aa5e2006-03-21 07:25:18 +0000162 OUT(";\n");
vlm80a48592005-02-25 12:10:27 +0000163 assert(eidx == el_count);
vlm4573af02005-08-13 23:30:24 +0000164 }
165
166 /*
167 * For all ENUMERATED types print out a mapping table
168 * between identifiers and associated values.
169 * This is prohibited for INTEGER types by by X.693:8.3.4.
170 */
171 if(expr->expr_type == ASN_BASIC_ENUMERATED) {
vlm80a48592005-02-25 12:10:27 +0000172
173 /*
174 * Generate a enumerationName<->value map for XER codec.
175 */
176 REDIR(OT_STAT_DEFS);
177
vlm337167e2005-11-26 11:25:14 +0000178 OUT("static asn_INTEGER_enum_map_t asn_MAP_%s_value2enum_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000179 MKID(expr), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000180 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue);
181 for(eidx = 0; eidx < el_count; eidx++) {
182 v2e[eidx].idx = eidx;
183 OUT("\t{ %" PRIdASN ",\t%ld,\t\"%s\" }%s\n",
184 v2e[eidx].value,
185 (long)strlen(v2e[eidx].name), v2e[eidx].name,
186 (eidx + 1 < el_count) ? "," : "");
187 }
vlm337167e2005-11-26 11:25:14 +0000188 if(map_extensions)
vlm80a48592005-02-25 12:10:27 +0000189 OUT("\t/* This list is extensible */\n");
190 OUT("};\n");
191
vlm337167e2005-11-26 11:25:14 +0000192 OUT("static unsigned int asn_MAP_%s_enum2value_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000193 MKID(expr), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000194 qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName);
195 for(eidx = 0; eidx < el_count; eidx++) {
196 OUT("\t%d%s\t/* %s(%" PRIdASN ") */\n",
197 v2e[eidx].idx,
198 (eidx + 1 < el_count) ? "," : "",
199 v2e[eidx].name, v2e[eidx].value);
200 }
vlm337167e2005-11-26 11:25:14 +0000201 if(map_extensions)
vlm80a48592005-02-25 12:10:27 +0000202 OUT("\t/* This list is extensible */\n");
203 OUT("};\n");
204
vlm337167e2005-11-26 11:25:14 +0000205 OUT("static asn_INTEGER_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000206 MKID(expr), expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000207 INDENT(+1);
vlm337167e2005-11-26 11:25:14 +0000208 OUT("asn_MAP_%s_value2enum_%d,\t"
vlm80a48592005-02-25 12:10:27 +0000209 "/* \"tag\" => N; sorted by tag */\n",
vlm0c6d3812006-03-21 03:40:38 +0000210 MKID(expr),
vlm766cf1e2005-03-04 08:48:53 +0000211 expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +0000212 OUT("asn_MAP_%s_enum2value_%d,\t"
vlm80a48592005-02-25 12:10:27 +0000213 "/* N => \"tag\"; sorted by N */\n",
vlm0c6d3812006-03-21 03:40:38 +0000214 MKID(expr),
vlm766cf1e2005-03-04 08:48:53 +0000215 expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +0000216 OUT("%d,\t/* Number of elements in the maps */\n",
217 el_count);
vlm337167e2005-11-26 11:25:14 +0000218 if(map_extensions) {
219 OUT("%d,\t/* Extensions before this member */\n",
220 map_extensions);
221 } else {
222 OUT("0,\t/* Enumeration is not extensible */\n");
223 }
vlm80a48592005-02-25 12:10:27 +0000224 if(expr->expr_type == ASN_BASIC_ENUMERATED)
225 OUT("1\t/* Strict enumeration */\n");
226 else
227 OUT("0\n");
228 INDENT(-1);
229 OUT("};\n");
vlmfa67ddc2004-06-03 03:38:44 +0000230 }
231
232 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
233}
234
235int
vlm96853d82005-08-13 23:51:47 +0000236asn1c_lang_C_type_BIT_STRING(arg_t *arg) {
237 asn1p_expr_t *expr = arg->expr;
238 asn1p_expr_t *v;
239 int el_count = expr_elements_count(arg, expr);
vlm96853d82005-08-13 23:51:47 +0000240
241 if(el_count) {
vlm75b3a532005-08-14 02:18:27 +0000242 int eidx = 0;
vlm96853d82005-08-13 23:51:47 +0000243 REDIR(OT_DEPS);
244 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000245 out_name_chain(arg, ONC_avoid_keywords);
vlm96853d82005-08-13 23:51:47 +0000246 OUT(" {\n");
247 TQ_FOR(v, &(expr->members), next) {
vlm75b3a532005-08-14 02:18:27 +0000248 if(v->expr_type != A1TC_UNIVERVAL) {
vlm96853d82005-08-13 23:51:47 +0000249 OUT("/* Unexpected BIT STRING element: %s */\n",
250 v->Identifier);
vlm75b3a532005-08-14 02:18:27 +0000251 continue;
vlm96853d82005-08-13 23:51:47 +0000252 }
vlm24238062006-08-26 03:29:15 +0000253 eidx++;
vlm75b3a532005-08-14 02:18:27 +0000254 OUT("\t");
vlm2e774282005-08-14 15:03:31 +0000255 out_name_chain(arg, ONC_noflags);
vlm0c6d3812006-03-21 03:40:38 +0000256 OUT("_%s", MKID(v));
vlm75b3a532005-08-14 02:18:27 +0000257 OUT("\t= %" PRIdASN "%s\n",
258 v->value->value.v_integer,
259 (eidx < el_count) ? "," : "");
vlm96853d82005-08-13 23:51:47 +0000260 }
vlm7c8aa5e2006-03-21 07:25:18 +0000261 OUT("} e_");
vlm2e774282005-08-14 15:03:31 +0000262 out_name_chain(arg, ONC_noflags);
vlm7c8aa5e2006-03-21 07:25:18 +0000263 OUT(";\n");
vlm96853d82005-08-13 23:51:47 +0000264 assert(eidx == el_count);
265 }
266
267 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
268}
269
270int
vlmfa67ddc2004-06-03 03:38:44 +0000271asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
272 asn1p_expr_t *expr = arg->expr;
273 asn1p_expr_t *v;
274 int comp_mode = 0; /* {root,ext=1,root,root,...} */
275
276 DEPENDENCIES;
277
278 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000279 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000280 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000281 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000282 } else {
283 OUT("typedef struct %s {\n",
vlm0c6d3812006-03-21 03:40:38 +0000284 MKID_safe(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000285 }
286
287 TQ_FOR(v, &(expr->members), next) {
vlm5a6fc652005-08-16 17:00:21 +0000288 if(v->expr_type == A1TC_EXTENSIBLE)
vlmfa67ddc2004-06-03 03:38:44 +0000289 if(comp_mode < 3) comp_mode++;
vlm5a6fc652005-08-16 17:00:21 +0000290 if(comp_mode == 1)
vlm337167e2005-11-26 11:25:14 +0000291 v->marker.flags |= EM_OMITABLE | EM_INDIRECT;
292 try_inline_default(arg, v, 1);
vlmfa67ddc2004-06-03 03:38:44 +0000293 EMBED(v);
294 }
295
296 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000297 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000298 expr->_anonymous_type ? "" :
299 arg->embed
vlm0c6d3812006-03-21 03:40:38 +0000300 ? MKID_safe(expr)
301 : MKID(expr),
vlmdae7f9d2004-08-22 03:25:24 +0000302 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000303
304 return asn1c_lang_C_type_SEQUENCE_def(arg);
305}
306
307static int
308asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) {
309 asn1p_expr_t *expr = arg->expr;
310 asn1p_expr_t *v;
311 int elements; /* Number of elements */
vlm237e4ef2007-06-23 20:05:49 +0000312 int ext_start = -2;
313 int ext_stop = -2;
vlm4e03ce22004-06-06 07:20:17 +0000314 tag2el_t *tag2el = NULL;
315 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000316 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000317 int all_tags_count;
318 enum tvm_compat tv_mode;
vlm337167e2005-11-26 11:25:14 +0000319 int roms_count; /* Root optional members */
320 int aoms_count; /* Additions optional members */
vlmfa67ddc2004-06-03 03:38:44 +0000321
vlm4e03ce22004-06-06 07:20:17 +0000322 /*
323 * Fetch every inner tag from the tag to elements map.
324 */
vlm940bc6b2004-10-03 09:13:30 +0000325 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlm4e03ce22004-06-06 07:20:17 +0000326 if(tag2el) free(tag2el);
327 return -1;
328 }
329
vlmea226772006-09-13 02:51:20 +0000330 GEN_INCLUDE_STD("constr_SEQUENCE");
vlm33a4ff12004-08-11 05:21:32 +0000331 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000332 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000333
vlm33a4ff12004-08-11 05:21:32 +0000334 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000335
336 /*
vlm337167e2005-11-26 11:25:14 +0000337 * Print out the table according to which parsing is performed.
vlmfa67ddc2004-06-03 03:38:44 +0000338 */
vlm39ba4c42004-09-22 16:06:28 +0000339 if(expr_elements_count(arg, expr)) {
340 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000341
vlm766cf1e2005-03-04 08:48:53 +0000342 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000343 MKID(expr), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000344
345 elements = 0;
vlm337167e2005-11-26 11:25:14 +0000346 roms_count = 0;
347 aoms_count = 0;
vlm39ba4c42004-09-22 16:06:28 +0000348 INDENTED(TQ_FOR(v, &(expr->members), next) {
349 if(v->expr_type == A1TC_EXTENSIBLE) {
350 if((++comp_mode) == 1)
351 ext_start = elements - 1;
352 else
353 ext_stop = elements - 1;
354 continue;
355 }
vlm337167e2005-11-26 11:25:14 +0000356 if(v->marker.flags & EM_OMITABLE)
357 comp_mode == 1 ? ++aoms_count : ++roms_count;
vlm39ba4c42004-09-22 16:06:28 +0000358 emit_member_table(arg, v);
vlm5a6fc652005-08-16 17:00:21 +0000359 elements++;
vlm39ba4c42004-09-22 16:06:28 +0000360 });
361 OUT("};\n");
vlm337167e2005-11-26 11:25:14 +0000362
363 if((roms_count + aoms_count) && (arg->flags & A1C_GEN_PER)) {
364 int elm = 0;
365 int comma = 0;
366 comp_mode = 0;
367 OUT("static int asn_MAP_%s_oms_%d[] = {",
vlm0c6d3812006-03-21 03:40:38 +0000368 MKID(expr),
vlm337167e2005-11-26 11:25:14 +0000369 expr->_type_unique_index);
370 TQ_FOR(v, &(expr->members), next) {
371 if(v->expr_type == A1TC_EXTENSIBLE) {
372 ++comp_mode;
373 continue;
374 }
375 if((v->marker.flags & EM_OMITABLE)
376 && comp_mode != 1) {
377 if(!comma) comma++;
378 else OUT(",");
379 OUT(" %d", elm);
380 }
381 ++elm;
382 }
383 elm = 0;
384 comp_mode = 0;
385 TQ_FOR(v, &(expr->members), next) {
386 if(v->expr_type == A1TC_EXTENSIBLE) {
387 ++comp_mode;
388 continue;
389 }
390 if((v->marker.flags & EM_OMITABLE)
391 && comp_mode == 1) {
392 if(!comma) comma++;
393 else OUT(",");
394 OUT(" %d", elm);
395 }
396 ++elm;
397 }
398 OUT(" };\n");
399 if(roms_count > 65536)
400 FATAL("Too many optional elements in %s "
401 "at line %d!",
402 arg->expr->Identifier,
403 arg->expr->_lineno);
404 } else {
405 roms_count = 0;
406 aoms_count = 0;
407 }
vlm39ba4c42004-09-22 16:06:28 +0000408 } else {
409 elements = 0;
vlm337167e2005-11-26 11:25:14 +0000410 roms_count = 0;
411 aoms_count = 0;
vlm39ba4c42004-09-22 16:06:28 +0000412 }
vlmfa67ddc2004-06-03 03:38:44 +0000413
vlm4a3f5822004-06-28 21:13:46 +0000414 /*
vlma5dcb912004-09-29 13:16:40 +0000415 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000416 */
vlm72425de2004-09-13 08:31:01 +0000417 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000418
vlm4e03ce22004-06-06 07:20:17 +0000419 /*
420 * Tags to elements map.
421 */
vlm940bc6b2004-10-03 09:13:30 +0000422 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlm4e03ce22004-06-06 07:20:17 +0000423
vlm337167e2005-11-26 11:25:14 +0000424 OUT("static asn_SEQUENCE_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000425 MKID(expr), expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +0000426 INDENT(+1);
427 OUT("sizeof(struct ");
428 out_name_chain(arg, ONC_avoid_keywords); OUT("),\n");
429 OUT("offsetof(struct ");
430 out_name_chain(arg, ONC_avoid_keywords); OUT(", _asn_ctx),\n");
vlm766cf1e2005-03-04 08:48:53 +0000431
vlm337167e2005-11-26 11:25:14 +0000432 if(tag2el_count) {
433 OUT("asn_MAP_%s_tag2el_%d,\n",
vlm0c6d3812006-03-21 03:40:38 +0000434 MKID(expr),
vlm337167e2005-11-26 11:25:14 +0000435 expr->_type_unique_index);
436 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
437 } else {
438 OUT("0,\t/* No top level tags */\n");
439 OUT("0,\t/* No tags in the map */\n");
440 }
441 if(roms_count + aoms_count) {
442 OUT("asn_MAP_%s_oms_%d,\t/* Optional members */\n",
vlm0c6d3812006-03-21 03:40:38 +0000443 MKID(expr), expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +0000444 OUT("%d, %d,\t/* Root/Additions */\n", roms_count, aoms_count);
445 } else {
446 OUT("0, 0, 0,\t/* Optional elements (not needed) */\n");
447 }
448 OUT("%d,\t/* Start extensions */\n",
vlm237e4ef2007-06-23 20:05:49 +0000449 ext_start<0 ? -1 : ext_start);
vlm337167e2005-11-26 11:25:14 +0000450 OUT("%d\t/* Stop extensions */\n",
vlm237e4ef2007-06-23 20:05:49 +0000451 (ext_stop<ext_start)?elements+1:(ext_stop<0?-1:ext_stop));
vlm337167e2005-11-26 11:25:14 +0000452 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000453 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000454
455 /*
vlma5dcb912004-09-29 13:16:40 +0000456 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000457 */
vlm72425de2004-09-13 08:31:01 +0000458 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000459 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000460
vlmfa67ddc2004-06-03 03:38:44 +0000461 REDIR(OT_TYPE_DECLS);
462
463 return 0;
vlmedf203f2005-01-17 11:57:48 +0000464} /* _SEQUENCE_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000465
466int
vlmfa67ddc2004-06-03 03:38:44 +0000467asn1c_lang_C_type_SET(arg_t *arg) {
468 asn1p_expr_t *expr = arg->expr;
469 asn1p_expr_t *v;
470 long mcount;
471 char *id;
472 int comp_mode = 0; /* {root,ext=1,root,root,...} */
473
474 DEPENDENCIES;
475
476 REDIR(OT_DEPS);
477
478 OUT("\n");
479 OUT("/*\n");
480 OUT(" * Method of determining the components presence\n");
481 OUT(" */\n");
482 mcount = 0;
vlm766cf1e2005-03-04 08:48:53 +0000483 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000484 out_name_chain(arg, ONC_noflags);
vlm766cf1e2005-03-04 08:48:53 +0000485 OUT("_PR {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000486 TQ_FOR(v, &(expr->members), next) {
487 if(v->expr_type == A1TC_EXTENSIBLE) continue;
488 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000489 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000490 OUT("_PR_");
vlm0c6d3812006-03-21 03:40:38 +0000491 id = MKID(v);
vlmfa67ddc2004-06-03 03:38:44 +0000492 OUT("%s,\t/* Member %s is present */\n",
493 id, id)
494 );
495 mcount++;
496 }
vlm2e774282005-08-14 15:03:31 +0000497 OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n");
vlmfa67ddc2004-06-03 03:38:44 +0000498
499 REDIR(OT_TYPE_DECLS);
500
501 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000502 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000503 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000504 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000505 } else {
vlm337167e2005-11-26 11:25:14 +0000506 OUT("typedef struct %s {\n",
vlm0c6d3812006-03-21 03:40:38 +0000507 MKID_safe(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000508 }
509
510 TQ_FOR(v, &(expr->members), next) {
vlm337167e2005-11-26 11:25:14 +0000511 if(v->expr_type == A1TC_EXTENSIBLE)
vlmfa67ddc2004-06-03 03:38:44 +0000512 if(comp_mode < 3) comp_mode++;
vlm5a6fc652005-08-16 17:00:21 +0000513 if(comp_mode == 1)
vlm337167e2005-11-26 11:25:14 +0000514 v->marker.flags |= EM_OMITABLE | EM_INDIRECT;
515 try_inline_default(arg, v, 1);
vlmfa67ddc2004-06-03 03:38:44 +0000516 EMBED(v);
517 }
518
519 INDENTED(
vlm0c6d3812006-03-21 03:40:38 +0000520 id = MKID(expr);
vlmfa67ddc2004-06-03 03:38:44 +0000521 OUT("\n");
522 OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n",
523 id, id);
524 OUT("unsigned int _presence_map\n");
525 OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount);
526 );
527
528 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000529 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm0c6d3812006-03-21 03:40:38 +0000530 expr->_anonymous_type ? "" : MKID_safe(expr),
vlmdae7f9d2004-08-22 03:25:24 +0000531 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000532
533 return asn1c_lang_C_type_SET_def(arg);
534}
535
vlmfa67ddc2004-06-03 03:38:44 +0000536static int
537asn1c_lang_C_type_SET_def(arg_t *arg) {
538 asn1p_expr_t *expr = arg->expr;
539 asn1p_expr_t *v;
540 int elements;
vlmfa67ddc2004-06-03 03:38:44 +0000541 tag2el_t *tag2el = NULL;
542 int tag2el_count = 0;
vlm940bc6b2004-10-03 09:13:30 +0000543 tag2el_t *tag2el_cxer = NULL;
544 int tag2el_cxer_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000545 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000546 int all_tags_count;
547 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000548 char *p;
549
550 /*
551 * Fetch every inner tag from the tag to elements map.
552 */
vlm940bc6b2004-10-03 09:13:30 +0000553 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000554 if(tag2el) free(tag2el);
555 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000556 }
vlm940bc6b2004-10-03 09:13:30 +0000557 if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) {
558 if(tag2el) free(tag2el);
559 if(tag2el_cxer) free(tag2el_cxer);
560 return -1;
561 }
562 if(tag2el_cxer_count == tag2el_count
563 && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) {
564 free(tag2el_cxer);
565 tag2el_cxer = 0;
566 }
vlmfa67ddc2004-06-03 03:38:44 +0000567
vlmea226772006-09-13 02:51:20 +0000568 GEN_INCLUDE_STD("constr_SET");
vlm33a4ff12004-08-11 05:21:32 +0000569 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000570 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000571
572 REDIR(OT_STAT_DEFS);
573
vlmfa67ddc2004-06-03 03:38:44 +0000574 /*
vlm337167e2005-11-26 11:25:14 +0000575 * Print out the table according to which parsing is performed.
vlmfa67ddc2004-06-03 03:38:44 +0000576 */
vlm39ba4c42004-09-22 16:06:28 +0000577 if(expr_elements_count(arg, expr)) {
578 int comp_mode = 0; /* {root,ext=1,root,root,...} */
vlmfa67ddc2004-06-03 03:38:44 +0000579
vlm766cf1e2005-03-04 08:48:53 +0000580 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000581 MKID(expr), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000582
583 elements = 0;
584 INDENTED(TQ_FOR(v, &(expr->members), next) {
585 if(v->expr_type == A1TC_EXTENSIBLE) {
586 if(comp_mode < 3) comp_mode++;
587 } else {
vlm39ba4c42004-09-22 16:06:28 +0000588 emit_member_table(arg, v);
vlm5a6fc652005-08-16 17:00:21 +0000589 elements++;
vlm39ba4c42004-09-22 16:06:28 +0000590 }
591 });
592 OUT("};\n");
593 } else {
594 elements = 0;
595 }
vlmfa67ddc2004-06-03 03:38:44 +0000596
vlm4a3f5822004-06-28 21:13:46 +0000597 /*
vlma5dcb912004-09-29 13:16:40 +0000598 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000599 */
vlm72425de2004-09-13 08:31:01 +0000600 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000601
602 /*
603 * Tags to elements map.
604 */
vlm940bc6b2004-10-03 09:13:30 +0000605 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
606 if(tag2el_cxer)
607 emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer");
vlmfa67ddc2004-06-03 03:38:44 +0000608
609 /*
610 * Emit a map of mandatory elements.
611 */
vlm337167e2005-11-26 11:25:14 +0000612 OUT("static uint8_t asn_MAP_%s_mmap_%d",
vlm0c6d3812006-03-21 03:40:38 +0000613 MKID(expr), expr->_type_unique_index);
614 p = MKID_safe(expr);
vlmfa67ddc2004-06-03 03:38:44 +0000615 OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements);
vlm766cf1e2005-03-04 08:48:53 +0000616 OUT(" = {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000617 INDENTED(
618 if(elements) {
vlmfa67ddc2004-06-03 03:38:44 +0000619 int el = 0;
620 TQ_FOR(v, &(expr->members), next) {
621 if(v->expr_type == A1TC_EXTENSIBLE) continue;
vlme2873192005-10-05 08:59:03 +0000622 if(el) {
623 if((el % 8) == 0)
624 OUT(",\n");
625 else
626 OUT(" | ");
vlmfa67ddc2004-06-03 03:38:44 +0000627 }
vlmddd5a7d2004-09-10 09:18:20 +0000628 OUT("(%d << %d)",
vlm6c4a8502005-08-18 13:38:19 +0000629 (v->marker.flags & EM_OMITABLE) != EM_OMITABLE,
vlmddd5a7d2004-09-10 09:18:20 +0000630 7 - (el % 8));
vlmfa67ddc2004-06-03 03:38:44 +0000631 el++;
632 }
633 } else {
634 OUT("0");
635 }
636 );
637 OUT("\n");
638 OUT("};\n");
639
vlm337167e2005-11-26 11:25:14 +0000640 OUT("static asn_SET_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000641 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000642 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000643 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000644 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000645 OUT("),\n");
646 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000647 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000648 OUT(", _asn_ctx),\n");
649 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000650 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000651 OUT(", _presence_map),\n");
vlm0c6d3812006-03-21 03:40:38 +0000652 p = MKID(expr);
vlm337167e2005-11-26 11:25:14 +0000653 OUT("asn_MAP_%s_tag2el_%d,\n", p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000654 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm940bc6b2004-10-03 09:13:30 +0000655 if(tag2el_cxer)
vlm337167e2005-11-26 11:25:14 +0000656 OUT("asn_MAP_%s_tag2el_cxer_%d,\n",
vlm766cf1e2005-03-04 08:48:53 +0000657 p, expr->_type_unique_index);
vlm940bc6b2004-10-03 09:13:30 +0000658 else
vlm337167e2005-11-26 11:25:14 +0000659 OUT("asn_MAP_%s_tag2el_%d,\t/* Same as above */\n",
vlm766cf1e2005-03-04 08:48:53 +0000660 p, expr->_type_unique_index);
661 OUT("%d,\t/* Count of tags in the CXER map */\n",
662 tag2el_cxer_count);
vlm79b08d52004-07-01 00:52:50 +0000663 OUT("%d,\t/* Whether extensible */\n",
vlm337167e2005-11-26 11:25:14 +0000664 compute_extensions_start(expr) == -1 ? 0 : 1);
665 OUT("(unsigned int *)asn_MAP_%s_mmap_%d\t/* Mandatory elements map */\n",
vlm766cf1e2005-03-04 08:48:53 +0000666 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000667 );
668 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000669
670 /*
vlma5dcb912004-09-29 13:16:40 +0000671 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000672 */
vlm72425de2004-09-13 08:31:01 +0000673 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +0000674 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000675
vlmfa67ddc2004-06-03 03:38:44 +0000676 REDIR(OT_TYPE_DECLS);
677
678 return 0;
vlmedf203f2005-01-17 11:57:48 +0000679} /* _SET_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000680
681int
vlmdae7f9d2004-08-22 03:25:24 +0000682asn1c_lang_C_type_SEx_OF(arg_t *arg) {
vlmfa67ddc2004-06-03 03:38:44 +0000683 asn1p_expr_t *expr = arg->expr;
vlm0b567bf2005-03-04 22:18:20 +0000684 asn1p_expr_t *memb = TQ_FIRST(&expr->members);
vlmfa67ddc2004-06-03 03:38:44 +0000685
686 DEPENDENCIES;
687
688 if(arg->embed) {
vlm766cf1e2005-03-04 08:48:53 +0000689 OUT("struct ");
vlm2e774282005-08-14 15:03:31 +0000690 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000691 OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000692 } else {
vlm0c6d3812006-03-21 03:40:38 +0000693 OUT("typedef struct %s {\n", MKID_safe(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000694 }
695
vlmdae7f9d2004-08-22 03:25:24 +0000696 INDENT(+1);
697 OUT("A_%s_OF(",
698 (arg->expr->expr_type == ASN_CONSTR_SET_OF)
699 ? "SET" : "SEQUENCE");
vlm5a6fc652005-08-16 17:00:21 +0000700
701 /*
702 * README README
703 * The implementation of the A_SET_OF() macro is already indirect.
704 */
705 memb->marker.flags |= EM_INDIRECT;
706
vlm80a48592005-02-25 12:10:27 +0000707 if(memb->expr_type & ASN_CONSTR_MASK
708 || ((memb->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +0000709 || (0 /* -- prohibited by X.693:8.3.4 */
710 && memb->expr_type == ASN_BASIC_INTEGER))
711 && expr_elements_count(arg, memb))) {
vlmdae7f9d2004-08-22 03:25:24 +0000712 arg_t tmp;
713 asn1p_expr_t tmp_memb;
714 arg->embed++;
715 tmp = *arg;
716 tmp.expr = &tmp_memb;
717 tmp_memb = *memb;
vlm5a6fc652005-08-16 17:00:21 +0000718 tmp_memb.marker.flags &= ~EM_INDIRECT;
vlmdae7f9d2004-08-22 03:25:24 +0000719 tmp_memb._anonymous_type = 1;
vlm39ba4c42004-09-22 16:06:28 +0000720 if(tmp_memb.Identifier == 0) {
vlm766cf1e2005-03-04 08:48:53 +0000721 tmp_memb.Identifier = "Member";
722 if(0)
vlm39ba4c42004-09-22 16:06:28 +0000723 tmp_memb.Identifier = strdup(
724 asn1c_make_identifier(0,
vlm0c6d3812006-03-21 03:40:38 +0000725 expr, "Member", 0));
vlm39ba4c42004-09-22 16:06:28 +0000726 assert(tmp_memb.Identifier);
727 }
vlmdae7f9d2004-08-22 03:25:24 +0000728 tmp.default_cb(&tmp);
vlm39ba4c42004-09-22 16:06:28 +0000729 if(tmp_memb.Identifier != memb->Identifier)
vlm766cf1e2005-03-04 08:48:53 +0000730 if(0) free(tmp_memb.Identifier);
vlmdae7f9d2004-08-22 03:25:24 +0000731 arg->embed--;
732 assert(arg->target->target == OT_TYPE_DECLS);
733 } else {
vlm6c5d5e52005-03-04 22:38:22 +0000734 OUT("%s", asn1c_type_name(arg, memb,
735 (memb->marker.flags & EM_UNRECURSE)
736 ? TNF_RSAFE : TNF_CTYPE));
vlmfa67ddc2004-06-03 03:38:44 +0000737 }
vlm5a6fc652005-08-16 17:00:21 +0000738 /* README README (above) */
739 if(0 && (memb->marker.flags & EM_INDIRECT))
740 OUT(" *");
vlmdae7f9d2004-08-22 03:25:24 +0000741 OUT(") list;\n");
742 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000743
744 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000745 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm0c6d3812006-03-21 03:40:38 +0000746 expr->_anonymous_type ? "" : MKID_safe(expr),
vlmdae7f9d2004-08-22 03:25:24 +0000747 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000748
749 /*
vlmdae7f9d2004-08-22 03:25:24 +0000750 * SET OF/SEQUENCE OF definition
vlmfa67ddc2004-06-03 03:38:44 +0000751 */
vlmdae7f9d2004-08-22 03:25:24 +0000752 return asn1c_lang_C_type_SEx_OF_def(arg,
753 (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF));
vlmfa67ddc2004-06-03 03:38:44 +0000754}
755
756static int
757asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) {
758 asn1p_expr_t *expr = arg->expr;
759 asn1p_expr_t *v;
vlm6e73a042004-08-11 07:17:22 +0000760 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000761 int all_tags_count;
762 enum tvm_compat tv_mode;
vlmfa67ddc2004-06-03 03:38:44 +0000763
vlm33a4ff12004-08-11 05:21:32 +0000764 /*
vlm337167e2005-11-26 11:25:14 +0000765 * Print out the table according to which parsing is performed.
vlm33a4ff12004-08-11 05:21:32 +0000766 */
vlmfa67ddc2004-06-03 03:38:44 +0000767 if(seq_of) {
vlmea226772006-09-13 02:51:20 +0000768 GEN_INCLUDE_STD("constr_SEQUENCE_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000769 } else {
vlmea226772006-09-13 02:51:20 +0000770 GEN_INCLUDE_STD("constr_SET_OF");
vlmfa67ddc2004-06-03 03:38:44 +0000771 }
vlm33a4ff12004-08-11 05:21:32 +0000772 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000773 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000774
775 REDIR(OT_STAT_DEFS);
776
777 /*
vlm337167e2005-11-26 11:25:14 +0000778 * Print out the table according to which parsing is performed.
vlmfa67ddc2004-06-03 03:38:44 +0000779 */
vlm766cf1e2005-03-04 08:48:53 +0000780 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000781 MKID(expr), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000782 INDENT(+1);
vlmfa67ddc2004-06-03 03:38:44 +0000783 v = TQ_FIRST(&(expr->members));
vlm39ba4c42004-09-22 16:06:28 +0000784 if(!v->Identifier) {
vlmbf3c5112005-02-14 20:41:29 +0000785 v->Identifier = strdup("Member");
vlm39ba4c42004-09-22 16:06:28 +0000786 assert(v->Identifier);
787 }
788 v->_anonymous_type = 1;
789 arg->embed++;
vlm4e554992004-08-25 02:03:12 +0000790 emit_member_table(arg, v);
vlm39ba4c42004-09-22 16:06:28 +0000791 arg->embed--;
792 INDENT(-1);
vlmfa67ddc2004-06-03 03:38:44 +0000793 OUT("};\n");
794
vlm4a3f5822004-06-28 21:13:46 +0000795 /*
vlma5dcb912004-09-29 13:16:40 +0000796 * Print out asn_DEF_<type>_[all_]tags[] vectors.
vlm4a3f5822004-06-28 21:13:46 +0000797 */
vlm72425de2004-09-13 08:31:01 +0000798 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000799
vlm337167e2005-11-26 11:25:14 +0000800 OUT("static asn_SET_OF_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000801 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000802 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000803 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000804 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000805 OUT("),\n");
806 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000807 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000808 OUT(", _asn_ctx),\n");
vlm6d44a542005-11-08 03:06:16 +0000809 {
810 int as_xvl = expr_as_xmlvaluelist(arg, v);
811 OUT("%d,\t/* XER encoding is %s */\n",
812 as_xvl,
813 as_xvl ? "XMLValueList" : "XMLDelimitedItemList");
814 }
vlmfa67ddc2004-06-03 03:38:44 +0000815 );
816 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +0000817
818 /*
vlma5dcb912004-09-29 13:16:40 +0000819 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +0000820 */
vlm72425de2004-09-13 08:31:01 +0000821 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1,
vlm86f5ed22004-09-26 13:11:31 +0000822 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +0000823
vlmfa67ddc2004-06-03 03:38:44 +0000824 REDIR(OT_TYPE_DECLS);
825
826 return 0;
vlmedf203f2005-01-17 11:57:48 +0000827} /* _SEx_OF_def() */
vlmfa67ddc2004-06-03 03:38:44 +0000828
829int
830asn1c_lang_C_type_CHOICE(arg_t *arg) {
831 asn1p_expr_t *expr = arg->expr;
832 asn1p_expr_t *v;
vlmdae7f9d2004-08-22 03:25:24 +0000833 char *id;
vlmfa67ddc2004-06-03 03:38:44 +0000834
835 DEPENDENCIES;
836
vlm33a4ff12004-08-11 05:21:32 +0000837 REDIR(OT_DEPS);
838
vlm766cf1e2005-03-04 08:48:53 +0000839 OUT("typedef enum ");
vlm2e774282005-08-14 15:03:31 +0000840 out_name_chain(arg, ONC_noflags);
vlm766cf1e2005-03-04 08:48:53 +0000841 OUT("_PR {\n");
vlm33a4ff12004-08-11 05:21:32 +0000842 INDENTED(
vlm6aa50982006-08-18 01:38:40 +0000843 int skipComma = 1;
vlm2e774282005-08-14 15:03:31 +0000844 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000845 OUT("_PR_NOTHING,\t/* No components present */\n");
vlm33a4ff12004-08-11 05:21:32 +0000846 TQ_FOR(v, &(expr->members), next) {
vlm6aa50982006-08-18 01:38:40 +0000847 if(skipComma) skipComma = 0;
vlm9ea7d412006-08-18 01:32:26 +0000848 else OUT(",\n");
vlm33a4ff12004-08-11 05:21:32 +0000849 if(v->expr_type == A1TC_EXTENSIBLE) {
850 OUT("/* Extensions may appear below */\n");
vlm6aa50982006-08-18 01:38:40 +0000851 skipComma = 1;
vlm33a4ff12004-08-11 05:21:32 +0000852 continue;
853 }
vlm2e774282005-08-14 15:03:31 +0000854 out_name_chain(arg, ONC_noflags);
vlm0c6d3812006-03-21 03:40:38 +0000855 id = MKID(v);
vlm9ea7d412006-08-18 01:32:26 +0000856 OUT("_PR_%s", id);
vlm33a4ff12004-08-11 05:21:32 +0000857 }
vlm9ea7d412006-08-18 01:32:26 +0000858 OUT("\n");
vlm33a4ff12004-08-11 05:21:32 +0000859 );
vlm2e774282005-08-14 15:03:31 +0000860 OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n");
vlm33a4ff12004-08-11 05:21:32 +0000861
862 REDIR(OT_TYPE_DECLS);
vlmfa67ddc2004-06-03 03:38:44 +0000863
864 if(arg->embed) {
vlm2e774282005-08-14 15:03:31 +0000865 OUT("struct "); out_name_chain(arg, ONC_avoid_keywords); OUT(" {\n");
vlmfa67ddc2004-06-03 03:38:44 +0000866 } else {
vlm0c6d3812006-03-21 03:40:38 +0000867 OUT("typedef struct %s {\n", MKID_safe(expr));
vlmfa67ddc2004-06-03 03:38:44 +0000868 }
869
870 INDENTED(
vlm2e774282005-08-14 15:03:31 +0000871 out_name_chain(arg, ONC_noflags);
vlmaf841972005-01-28 12:18:50 +0000872 OUT("_PR present;\n");
vlm2e774282005-08-14 15:03:31 +0000873 OUT("union ");
874 if(UNNAMED_UNIONS == 0) {
875 out_name_chain(arg, ONC_force_compound_name);
876 OUT("_u ");
877 }
878 OUT("{\n");
vlmfa67ddc2004-06-03 03:38:44 +0000879 TQ_FOR(v, &(expr->members), next) {
880 EMBED(v);
881 }
882 if(UNNAMED_UNIONS) OUT("};\n");
883 else OUT("} choice;\n");
884 );
885
886 PCTX_DEF;
vlm0b567bf2005-03-04 22:18:20 +0000887 OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
vlm1f7df782005-03-04 23:48:19 +0000888 expr->_anonymous_type ? "" :
889 arg->embed
vlm0c6d3812006-03-21 03:40:38 +0000890 ? MKID_safe(expr)
891 : MKID(expr),
vlmdae7f9d2004-08-22 03:25:24 +0000892 arg->embed ? "" : "_t");
vlmfa67ddc2004-06-03 03:38:44 +0000893
894 return asn1c_lang_C_type_CHOICE_def(arg);
895}
896
897static int
898asn1c_lang_C_type_CHOICE_def(arg_t *arg) {
899 asn1p_expr_t *expr = arg->expr;
900 asn1p_expr_t *v;
901 int elements; /* Number of elements */
vlmfa67ddc2004-06-03 03:38:44 +0000902 tag2el_t *tag2el = NULL;
903 int tag2el_count = 0;
vlm6e73a042004-08-11 07:17:22 +0000904 int tags_count;
vlm72425de2004-09-13 08:31:01 +0000905 int all_tags_count;
906 enum tvm_compat tv_mode;
vlm337167e2005-11-26 11:25:14 +0000907 int *cmap = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000908
909 /*
910 * Fetch every inner tag from the tag to elements map.
911 */
vlm940bc6b2004-10-03 09:13:30 +0000912 if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) {
vlmfa67ddc2004-06-03 03:38:44 +0000913 if(tag2el) free(tag2el);
914 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000915 }
916
vlmea226772006-09-13 02:51:20 +0000917 GEN_INCLUDE_STD("constr_CHOICE");
vlm33a4ff12004-08-11 05:21:32 +0000918 if(!arg->embed)
vlma5dcb912004-09-29 13:16:40 +0000919 GEN_DECLARE(expr); /* asn_DEF_xxx */
vlmfa67ddc2004-06-03 03:38:44 +0000920
vlm33a4ff12004-08-11 05:21:32 +0000921 REDIR(OT_STAT_DEFS);
vlmfa67ddc2004-06-03 03:38:44 +0000922
923 /*
vlm337167e2005-11-26 11:25:14 +0000924 * Print out the table according to which parsing is performed.
vlmfa67ddc2004-06-03 03:38:44 +0000925 */
vlm39ba4c42004-09-22 16:06:28 +0000926 if(expr_elements_count(arg, expr)) {
vlmfa67ddc2004-06-03 03:38:44 +0000927
vlm766cf1e2005-03-04 08:48:53 +0000928 OUT("static asn_TYPE_member_t asn_MBR_%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000929 MKID(expr), expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +0000930
931 elements = 0;
932 INDENTED(TQ_FOR(v, &(expr->members), next) {
vlm34dcd572005-01-17 11:40:49 +0000933 if(v->expr_type == A1TC_EXTENSIBLE)
934 continue;
vlm34dcd572005-01-17 11:40:49 +0000935 emit_member_table(arg, v);
vlm5a6fc652005-08-16 17:00:21 +0000936 elements++;
vlm39ba4c42004-09-22 16:06:28 +0000937 });
938 OUT("};\n");
939 } else {
940 elements = 0;
941 }
vlmfa67ddc2004-06-03 03:38:44 +0000942
vlm337167e2005-11-26 11:25:14 +0000943 /* Create a canonical elements map */
944 if(elements && (arg->flags & A1C_GEN_PER)) {
945 int i;
946 cmap = compute_canonical_members_order(arg, elements);
947 if(cmap) {
948 OUT("static int asn_MAP_%s_cmap_%d[] = {",
vlm0c6d3812006-03-21 03:40:38 +0000949 MKID(expr),
vlm337167e2005-11-26 11:25:14 +0000950 expr->_type_unique_index);
951 for(i = 0; i < elements; i++) {
952 if(i) OUT(",");
953 OUT(" %d", cmap[i]);
954 }
955 OUT(" };\n");
956 free(cmap);
957 }
958 }
vlm6e73a042004-08-11 07:17:22 +0000959
vlmfa67ddc2004-06-03 03:38:44 +0000960 if(arg->embed) {
961 /*
962 * Our parent structure has already taken this into account.
963 */
vlm72425de2004-09-13 08:31:01 +0000964 tv_mode = _TVM_SAME;
965 tags_count = all_tags_count = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000966 } else {
vlm72425de2004-09-13 08:31:01 +0000967 tv_mode = emit_tags_vectors(arg, expr,
968 &tags_count, &all_tags_count);
vlmfa67ddc2004-06-03 03:38:44 +0000969 }
vlmfa67ddc2004-06-03 03:38:44 +0000970
971 /*
972 * Tags to elements map.
973 */
vlm940bc6b2004-10-03 09:13:30 +0000974 emit_tag2member_map(arg, tag2el, tag2el_count, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000975
vlm337167e2005-11-26 11:25:14 +0000976 OUT("static asn_CHOICE_specifics_t asn_SPC_%s_specs_%d = {\n",
vlm0c6d3812006-03-21 03:40:38 +0000977 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000978 INDENTED(
vlm766cf1e2005-03-04 08:48:53 +0000979 OUT("sizeof(struct ");
vlm2e774282005-08-14 15:03:31 +0000980 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000981 OUT("),\n");
982 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000983 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000984 OUT(", _asn_ctx),\n");
985 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +0000986 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000987 OUT(", present),\n");
988 OUT("sizeof(((struct ");
vlm2e774282005-08-14 15:03:31 +0000989 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +0000990 OUT(" *)0)->present),\n");
vlm337167e2005-11-26 11:25:14 +0000991 OUT("asn_MAP_%s_tag2el_%d,\n",
vlm0c6d3812006-03-21 03:40:38 +0000992 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +0000993 OUT("%d,\t/* Count of tags in the map */\n", tag2el_count);
vlm337167e2005-11-26 11:25:14 +0000994 if(C99_MODE) OUT(".canonical_order = ");
995 if(cmap) OUT("asn_MAP_%s_cmap_%d,\t/* Canonically sorted */\n",
vlm0c6d3812006-03-21 03:40:38 +0000996 MKID(expr), expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +0000997 else OUT("0,\n");
998 if(C99_MODE) OUT(".ext_start = ");
999 OUT("%d\t/* Extensions start */\n",
1000 compute_extensions_start(expr));
vlmfa67ddc2004-06-03 03:38:44 +00001001 );
1002 OUT("};\n");
vlm4e554992004-08-25 02:03:12 +00001003
1004 /*
vlma5dcb912004-09-29 13:16:40 +00001005 * Emit asn_DEF_xxx table.
vlm4e554992004-08-25 02:03:12 +00001006 */
vlm72425de2004-09-13 08:31:01 +00001007 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements,
vlm86f5ed22004-09-26 13:11:31 +00001008 ETD_HAS_SPECIFICS);
vlmfa67ddc2004-06-03 03:38:44 +00001009
vlmfa67ddc2004-06-03 03:38:44 +00001010 REDIR(OT_TYPE_DECLS);
1011
1012 return 0;
vlmedf203f2005-01-17 11:57:48 +00001013} /* _CHOICE_def() */
vlmfa67ddc2004-06-03 03:38:44 +00001014
1015int
1016asn1c_lang_C_type_REFERENCE(arg_t *arg) {
1017 asn1p_ref_t *ref;
1018
1019 ref = arg->expr->reference;
1020 if(ref->components[ref->comp_count-1].name[0] == '&') {
vlmfa67ddc2004-06-03 03:38:44 +00001021 asn1p_expr_t *extract;
1022 arg_t tmp;
1023 int ret;
1024
vlm39ba4c42004-09-22 16:06:28 +00001025 extract = asn1f_class_access_ex(arg->asn, arg->expr->module,
vlm0c6d3812006-03-21 03:40:38 +00001026 arg->expr, arg->expr->rhs_pspecs, ref);
vlmfa67ddc2004-06-03 03:38:44 +00001027 if(extract == NULL)
1028 return -1;
1029
vlmdae7f9d2004-08-22 03:25:24 +00001030 extract = asn1p_expr_clone(extract, 0);
vlmfa67ddc2004-06-03 03:38:44 +00001031 if(extract) {
1032 if(extract->Identifier)
1033 free(extract->Identifier);
1034 extract->Identifier = strdup(arg->expr->Identifier);
1035 if(extract->Identifier == NULL) {
1036 asn1p_expr_free(extract);
1037 return -1;
1038 }
1039 } else {
1040 return -1;
1041 }
1042
1043 tmp = *arg;
1044 tmp.asn = arg->asn;
vlmfa67ddc2004-06-03 03:38:44 +00001045 tmp.expr = extract;
1046
1047 ret = arg->default_cb(&tmp);
1048
1049 asn1p_expr_free(extract);
1050
1051 return ret;
1052 }
1053
1054
1055 return asn1c_lang_C_type_SIMPLE_TYPE(arg);
1056}
1057
1058int
1059asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
1060 asn1p_expr_t *expr = arg->expr;
vlm6e73a042004-08-11 07:17:22 +00001061 int tags_count;
vlm72425de2004-09-13 08:31:01 +00001062 int all_tags_count;
1063 enum tvm_compat tv_mode;
vlm80a48592005-02-25 12:10:27 +00001064 enum etd_spec etd_spec;
vlmfa67ddc2004-06-03 03:38:44 +00001065 char *p;
1066
1067 if(arg->embed) {
vlma5dcb912004-09-29 13:16:40 +00001068 enum tnfmt tnfmt = TNF_CTYPE;
1069
1070 /*
1071 * If this is an optional compound type,
1072 * refer it using "struct X" convention,
1073 * as it may recursively include the current structure.
1074 */
vlm0b567bf2005-03-04 22:18:20 +00001075 if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
vlm6c4a8502005-08-18 13:38:19 +00001076 if(terminal_structable(arg, expr)) {
vlm0b567bf2005-03-04 22:18:20 +00001077 tnfmt = TNF_RSAFE;
1078 REDIR(OT_FWD_DECLS);
vlm5feb7522005-03-04 23:50:56 +00001079 OUT("%s;\n",
vlm0b567bf2005-03-04 22:18:20 +00001080 asn1c_type_name(arg, arg->expr, tnfmt));
vlma5dcb912004-09-29 13:16:40 +00001081 }
1082 }
1083
vlmfa67ddc2004-06-03 03:38:44 +00001084 REDIR(OT_TYPE_DECLS);
1085
vlm0b567bf2005-03-04 22:18:20 +00001086 OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
vlm80a48592005-02-25 12:10:27 +00001087 if(!expr->_anonymous_type) {
vlm0b567bf2005-03-04 22:18:20 +00001088 OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
vlm0c6d3812006-03-21 03:40:38 +00001089 OUT("%s", MKID_safe(expr));
vlm337167e2005-11-26 11:25:14 +00001090 if((expr->marker.flags & (EM_DEFAULT & ~EM_INDIRECT))
1091 == (EM_DEFAULT & ~EM_INDIRECT))
vlm80a48592005-02-25 12:10:27 +00001092 OUT("\t/* DEFAULT %s */",
1093 asn1f_printable_value(
1094 expr->marker.default_value));
vlm337167e2005-11-26 11:25:14 +00001095 else if((expr->marker.flags & EM_OPTIONAL)
1096 == EM_OPTIONAL)
vlm80a48592005-02-25 12:10:27 +00001097 OUT("\t/* OPTIONAL */");
1098 }
1099
1100 } else {
1101 GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
vlmb2839012004-08-20 13:37:01 +00001102
1103 REDIR(OT_TYPE_DECLS);
vlm80a48592005-02-25 12:10:27 +00001104
1105 OUT("typedef %s\t",
vlm0b567bf2005-03-04 22:18:20 +00001106 asn1c_type_name(arg, arg->expr, TNF_CTYPE));
vlm80a48592005-02-25 12:10:27 +00001107 OUT("%s%s_t",
vlm0b567bf2005-03-04 22:18:20 +00001108 (expr->marker.flags & EM_INDIRECT)?"*":" ",
vlm0c6d3812006-03-21 03:40:38 +00001109 MKID(expr));
vlmfa67ddc2004-06-03 03:38:44 +00001110 }
1111
vlm80a48592005-02-25 12:10:27 +00001112 if((expr->expr_type == ASN_BASIC_ENUMERATED)
vlmc89422d2005-03-03 21:28:12 +00001113 || (0 /* -- prohibited by X.693:8.3.4 */
1114 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00001115 && expr_elements_count(arg, expr)))
1116 etd_spec = ETD_HAS_SPECIFICS;
1117 else
1118 etd_spec = ETD_NO_SPECIFICS;
vlmfa67ddc2004-06-03 03:38:44 +00001119
vlm86f5ed22004-09-26 13:11:31 +00001120 /*
1121 * If this type just blindly refers the other type, alias it.
1122 * Type1 ::= Type2
1123 */
vlm80a48592005-02-25 12:10:27 +00001124 if(arg->embed && etd_spec == ETD_NO_SPECIFICS) {
1125 REDIR(OT_TYPE_DECLS);
1126 return 0;
1127 }
vlm86f5ed22004-09-26 13:11:31 +00001128 if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlm80a48592005-02-25 12:10:27 +00001129 && (arg->embed || expr->tag.tag_class == TC_NOCLASS)
vlm6f74b0f2005-03-04 23:23:50 +00001130 && etd_spec == ETD_NO_SPECIFICS
1131 && 0 /* This shortcut is incompatible with XER */
1132 ) {
vlm86f5ed22004-09-26 13:11:31 +00001133 char *type_name;
1134 REDIR(OT_FUNC_DECLS);
1135 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
1136 OUT("/* This type is equivalent to %s */\n", type_name);
vlm86f5ed22004-09-26 13:11:31 +00001137 if(HIDE_INNER_DEFS) OUT("/* ");
vlm0c6d3812006-03-21 03:40:38 +00001138 OUT("#define\tasn_DEF_%s\t", MKID(expr));
vlm86f5ed22004-09-26 13:11:31 +00001139 type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlm80a48592005-02-25 12:10:27 +00001140 OUT("asn_DEF_%s", type_name);
vlm86f5ed22004-09-26 13:11:31 +00001141 if(HIDE_INNER_DEFS)
vlm80a48592005-02-25 12:10:27 +00001142 OUT("\t// (Use -fall-defs-global to expose) */");
1143 OUT("\n");
vlm86f5ed22004-09-26 13:11:31 +00001144 REDIR(OT_CODE);
1145 OUT("/* This type is equivalent to %s */\n", type_name);
1146 OUT("\n");
1147 REDIR(OT_TYPE_DECLS);
1148 return 0;
1149 }
1150
vlmb2839012004-08-20 13:37:01 +00001151 REDIR(OT_CODE);
1152
vlmfa67ddc2004-06-03 03:38:44 +00001153 /*
1154 * Constraint checking.
1155 */
vlm86f5ed22004-09-26 13:11:31 +00001156 if(!(arg->flags & A1C_NO_CONSTRAINTS)) {
vlm0c6d3812006-03-21 03:40:38 +00001157 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001158 if(HIDE_INNER_DEFS) OUT("static ");
vlm86f5ed22004-09-26 13:11:31 +00001159 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001160 OUT("%s", p);
1161 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1162 OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n");
vlm86f5ed22004-09-26 13:11:31 +00001163 INDENT(+1);
vlmaf68ef52006-07-13 11:19:01 +00001164 OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {");
vlm86f5ed22004-09-26 13:11:31 +00001165 OUT("\n");
vlm86380d32006-10-09 12:07:58 +00001166 DEBUG("expr constraint checking code for %s", p);
vlm86f5ed22004-09-26 13:11:31 +00001167 if(asn1c_emit_constraint_checking_code(arg) == 1) {
1168 OUT("/* Replace with underlying type checker */\n");
1169 OUT("td->check_constraints "
vlma5dcb912004-09-29 13:16:40 +00001170 "= asn_DEF_%s.check_constraints;\n",
vlm86f5ed22004-09-26 13:11:31 +00001171 asn1c_type_name(arg, expr, TNF_SAFE));
1172 OUT("return td->check_constraints"
vlmaf68ef52006-07-13 11:19:01 +00001173 "(td, sptr, ctfailcb, app_key);\n");
vlm1d036692004-08-19 13:29:46 +00001174 }
vlm86f5ed22004-09-26 13:11:31 +00001175 INDENT(-1);
1176 OUT("}\n");
1177 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001178 }
vlmfa67ddc2004-06-03 03:38:44 +00001179
vlm86380d32006-10-09 12:07:58 +00001180 REDIR(OT_STAT_DEFS);
1181
1182 /*
1183 * Print out asn_DEF_<type>_[all_]tags[] vectors.
1184 */
1185 tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count);
1186 DEBUG("emit tag vectors for %s %d, %d, %d", expr->Identifier,
1187 tv_mode, tags_count, all_tags_count);
1188
1189 emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count,
1190 0, etd_spec);
1191
1192 REDIR(OT_CODE);
1193
vlmfa67ddc2004-06-03 03:38:44 +00001194 /*
1195 * Emit suicidal functions.
1196 */
1197
vlmfa67ddc2004-06-03 03:38:44 +00001198 /*
1199 * This function replaces certain fields from the definition
1200 * of a type with the corresponding fields from the basic type
1201 * (from which the current type is inherited).
1202 */
vlmfa67ddc2004-06-03 03:38:44 +00001203 OUT("/*\n");
vlmb2839012004-08-20 13:37:01 +00001204 OUT(" * This type is implemented using %s,\n",
1205 asn1c_type_name(arg, expr, TNF_SAFE));
vlmdcd32fe2004-09-23 22:20:47 +00001206 OUT(" * so here we adjust the DEF accordingly.\n");
vlmfa67ddc2004-06-03 03:38:44 +00001207 OUT(" */\n");
1208 OUT("static void\n");
vlm766cf1e2005-03-04 08:48:53 +00001209 OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n",
vlm0c6d3812006-03-21 03:40:38 +00001210 MKID(expr), expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001211 INDENT(+1);
vlm80a48592005-02-25 12:10:27 +00001212 {
vlm39ba4c42004-09-22 16:06:28 +00001213 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
vlmb2839012004-08-20 13:37:01 +00001214 char *type_name = asn1c_type_name(arg, expr, TNF_SAFE);
vlma5dcb912004-09-29 13:16:40 +00001215 OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name);
1216 OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name);
1217 OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name);
1218 OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name);
1219 OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name);
1220 OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name);
vlm337167e2005-11-26 11:25:14 +00001221 OUT("td->uper_decoder = asn_DEF_%s.uper_decoder;\n", type_name);
vlm9ea7d412006-08-18 01:32:26 +00001222 OUT("td->uper_encoder = asn_DEF_%s.uper_encoder;\n", type_name);
vlm1308d2b2004-09-10 15:49:15 +00001223 if(!terminal && !tags_count) {
vlm72425de2004-09-13 08:31:01 +00001224 OUT("/* The next four lines are here because of -fknown-extern-type */\n");
vlma5dcb912004-09-29 13:16:40 +00001225 OUT("td->tags = asn_DEF_%s.tags;\n", type_name);
1226 OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name);
1227 OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name);
1228 OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name);
vlm1308d2b2004-09-10 15:49:15 +00001229 OUT("/* End of these lines */\n");
1230 }
vlm337167e2005-11-26 11:25:14 +00001231 OUT("if(!td->per_constraints)\n");
1232 OUT("\ttd->per_constraints = asn_DEF_%s.per_constraints;\n",
1233 type_name);
vlma5dcb912004-09-29 13:16:40 +00001234 OUT("td->elements = asn_DEF_%s.elements;\n", type_name);
1235 OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name);
vlm80a48592005-02-25 12:10:27 +00001236 if(etd_spec != ETD_NO_SPECIFICS) {
1237 INDENT(-1);
vlmc86870c2005-03-29 19:04:24 +00001238 OUT(" /* ");
vlmb2839012004-08-20 13:37:01 +00001239 }
vlm80a48592005-02-25 12:10:27 +00001240 OUT("td->specifics = asn_DEF_%s.specifics;", type_name);
1241 if(etd_spec == ETD_NO_SPECIFICS) {
1242 INDENT(-1);
1243 OUT("\n");
1244 } else {
1245 OUT("\t// Defined explicitly */\n");
1246 }
1247 }
vlmfa67ddc2004-06-03 03:38:44 +00001248 OUT("}\n");
1249 OUT("\n");
vlmfa67ddc2004-06-03 03:38:44 +00001250
vlm0c6d3812006-03-21 03:40:38 +00001251 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001252 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001253 OUT("void\n");
vlm766cf1e2005-03-04 08:48:53 +00001254 OUT("%s", p);
1255 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1256 OUT("_free(asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001257 INDENTED(
vlm39ba4c42004-09-22 16:06:28 +00001258 OUT("\tvoid *struct_ptr, int contents_only) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001259 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1260 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001261 OUT("td->free_struct(td, struct_ptr, contents_only);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001262 );
1263 OUT("}\n");
1264 OUT("\n");
1265
vlm0c6d3812006-03-21 03:40:38 +00001266 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001267 if(HIDE_INNER_DEFS) OUT("static ");
vlmfa67ddc2004-06-03 03:38:44 +00001268 OUT("int\n");
vlm766cf1e2005-03-04 08:48:53 +00001269 OUT("%s", p);
1270 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1271 OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001272 INDENTED(
1273 OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001274 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1275 p, expr->_type_unique_index);
vlmfa67ddc2004-06-03 03:38:44 +00001276 OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n");
1277 );
1278 OUT("}\n");
1279 OUT("\n");
1280
vlm0c6d3812006-03-21 03:40:38 +00001281 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001282 if(HIDE_INNER_DEFS) OUT("static ");
vlm9de248e2004-10-20 15:50:55 +00001283 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001284 OUT("%s", p);
1285 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1286 OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlmfa67ddc2004-06-03 03:38:44 +00001287 INDENTED(
vlmfcc4cbc2005-03-17 21:56:00 +00001288 OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001289 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1290 p, expr->_type_unique_index);
vlma5dcb912004-09-29 13:16:40 +00001291 OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n");
vlm39ba4c42004-09-22 16:06:28 +00001292 );
1293 OUT("}\n");
1294 OUT("\n");
1295
vlm0c6d3812006-03-21 03:40:38 +00001296 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001297 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001298 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001299 OUT("%s", p);
1300 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1301 OUT("_encode_der(asn_TYPE_descriptor_t *td,\n");
vlm39ba4c42004-09-22 16:06:28 +00001302 INDENTED(
1303 OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n");
1304 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001305 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1306 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001307 OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n");
1308 );
1309 OUT("}\n");
1310 OUT("\n");
1311
vlm0c6d3812006-03-21 03:40:38 +00001312 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001313 if(HIDE_INNER_DEFS) OUT("static ");
vlme03646b2004-10-23 13:34:00 +00001314 OUT("asn_dec_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001315 OUT("%s", p);
1316 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1317 OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
vlme03646b2004-10-23 13:34:00 +00001318 INDENTED(
vlmfcc4cbc2005-03-17 21:56:00 +00001319 OUT("\tvoid **structure, const char *opt_mname, const void *bufptr, size_t size) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001320 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1321 p, expr->_type_unique_index);
vlme03646b2004-10-23 13:34:00 +00001322 OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n");
1323 );
1324 OUT("}\n");
1325 OUT("\n");
1326
vlm0c6d3812006-03-21 03:40:38 +00001327 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001328 if(HIDE_INNER_DEFS) OUT("static ");
vlm39ba4c42004-09-22 16:06:28 +00001329 OUT("asn_enc_rval_t\n");
vlm766cf1e2005-03-04 08:48:53 +00001330 OUT("%s", p);
1331 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1332 OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n");
vlm39ba4c42004-09-22 16:06:28 +00001333 INDENTED(
1334 OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n");
1335 OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n");
vlm766cf1e2005-03-04 08:48:53 +00001336 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1337 p, expr->_type_unique_index);
vlm39ba4c42004-09-22 16:06:28 +00001338 OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n");
vlmfa67ddc2004-06-03 03:38:44 +00001339 );
1340 OUT("}\n");
1341 OUT("\n");
1342
vlm337167e2005-11-26 11:25:14 +00001343 if(arg->flags & A1C_GEN_PER) {
vlm0c6d3812006-03-21 03:40:38 +00001344 p = MKID(expr);
vlm9ea7d412006-08-18 01:32:26 +00001345
vlm337167e2005-11-26 11:25:14 +00001346 if(HIDE_INNER_DEFS) OUT("static ");
1347 OUT("asn_dec_rval_t\n");
1348 OUT("%s", p);
1349 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1350 OUT("_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n");
1351 INDENTED(
1352 OUT("\tasn_per_constraints_t *constraints, void **structure, asn_per_data_t *per_data) {\n");
1353 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1354 p, expr->_type_unique_index);
1355 OUT("return td->uper_decoder(opt_codec_ctx, td, constraints, structure, per_data);\n");
1356 );
1357 OUT("}\n");
1358 OUT("\n");
vlm9ea7d412006-08-18 01:32:26 +00001359
1360 p = MKID(expr);
1361 if(HIDE_INNER_DEFS) OUT("static ");
1362 OUT("asn_enc_rval_t\n");
1363 OUT("%s", p);
1364 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
1365 OUT("_encode_uper(asn_TYPE_descriptor_t *td,\n");
1366 INDENTED(
1367 OUT("\tasn_per_constraints_t *constraints,\n");
1368 OUT("\tvoid *structure, asn_per_outp_t *per_out) {\n");
1369 OUT("%s_%d_inherit_TYPE_descriptor(td);\n",
1370 p, expr->_type_unique_index);
1371 OUT("return td->uper_encoder(td, constraints, structure, per_out);\n");
1372 );
1373 OUT("}\n");
1374 OUT("\n");
vlm337167e2005-11-26 11:25:14 +00001375 }
1376
vlmfa67ddc2004-06-03 03:38:44 +00001377 REDIR(OT_FUNC_DECLS);
1378
vlm0c6d3812006-03-21 03:40:38 +00001379 p = MKID(expr);
vlm80a48592005-02-25 12:10:27 +00001380 if(HIDE_INNER_DEFS) {
vlm766cf1e2005-03-04 08:48:53 +00001381 OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;"
1382 "\t// (Use -fall-defs-global to expose) */\n",
1383 p, expr->_type_unique_index);
vlm80a48592005-02-25 12:10:27 +00001384 } else {
1385 OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p);
1386 OUT("asn_struct_free_f %s_free;\n", p);
1387 OUT("asn_struct_print_f %s_print;\n", p);
1388 OUT("asn_constr_check_f %s_constraint;\n", p);
1389 OUT("ber_type_decoder_f %s_decode_ber;\n", p);
1390 OUT("der_type_encoder_f %s_encode_der;\n", p);
1391 OUT("xer_type_decoder_f %s_decode_xer;\n", p);
1392 OUT("xer_type_encoder_f %s_encode_xer;\n", p);
vlm9ea7d412006-08-18 01:32:26 +00001393 if(arg->flags & A1C_GEN_PER) {
vlm337167e2005-11-26 11:25:14 +00001394 OUT("per_type_decoder_f %s_decode_uper;\n", p);
vlm9ea7d412006-08-18 01:32:26 +00001395 OUT("per_type_encoder_f %s_encode_uper;\n", p);
1396 }
vlm80a48592005-02-25 12:10:27 +00001397 }
vlmfa67ddc2004-06-03 03:38:44 +00001398
vlm33a4ff12004-08-11 05:21:32 +00001399 REDIR(OT_TYPE_DECLS);
1400
vlmfa67ddc2004-06-03 03:38:44 +00001401 return 0;
1402}
1403
1404int
1405asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) {
1406
1407 OUT("/*\n");
1408 OUT(" * This type is extensible,\n");
1409 OUT(" * possible extensions are below.\n");
1410 OUT(" */\n");
1411
1412 return 0;
1413}
1414
vlm337167e2005-11-26 11:25:14 +00001415static int
1416compute_extensions_start(asn1p_expr_t *expr) {
vlm79b08d52004-07-01 00:52:50 +00001417 asn1p_expr_t *v;
vlm337167e2005-11-26 11:25:14 +00001418 int eidx = 0;
vlm79b08d52004-07-01 00:52:50 +00001419 TQ_FOR(v, &(expr->members), next) {
vlm337167e2005-11-26 11:25:14 +00001420 if(v->expr_type == A1TC_EXTENSIBLE)
1421 return eidx;
1422 eidx++;
vlm79b08d52004-07-01 00:52:50 +00001423 }
vlm337167e2005-11-26 11:25:14 +00001424 return -1;
vlm79b08d52004-07-01 00:52:50 +00001425}
1426
vlmfa67ddc2004-06-03 03:38:44 +00001427static int
vlma8a86cc2004-09-10 06:07:18 +00001428_print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) {
vlmfa67ddc2004-06-03 03:38:44 +00001429
1430 OUT("(");
vlma8a86cc2004-09-10 06:07:18 +00001431 switch(tag->tag_class) {
vlmfa67ddc2004-06-03 03:38:44 +00001432 case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break;
1433 case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break;
1434 case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break;
1435 case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break;
1436 case TC_NOCLASS:
1437 break;
1438 }
vlm47ae1582004-09-24 21:01:43 +00001439 OUT(" | (%" PRIdASN " << 2))", tag->tag_value);
vlmfa67ddc2004-06-03 03:38:44 +00001440
1441 return 0;
1442}
1443
vlm4e03ce22004-06-06 07:20:17 +00001444
1445static int
1446_tag2el_cmp(const void *ap, const void *bp) {
1447 const tag2el_t *a = ap;
1448 const tag2el_t *b = bp;
1449 const struct asn1p_type_tag_s *ta = &a->el_tag;
1450 const struct asn1p_type_tag_s *tb = &b->el_tag;
1451
1452 if(ta->tag_class == tb->tag_class) {
1453 if(ta->tag_value == tb->tag_value) {
1454 /*
1455 * Sort by their respective positions.
1456 */
1457 if(a->el_no < b->el_no)
1458 return -1;
1459 else if(a->el_no > b->el_no)
1460 return 1;
1461 return 0;
1462 } else if(ta->tag_value < tb->tag_value)
1463 return -1;
1464 else
1465 return 1;
1466 } else if(ta->tag_class < tb->tag_class) {
1467 return -1;
1468 } else {
1469 return 1;
1470 }
1471}
1472
vlmfa67ddc2004-06-03 03:38:44 +00001473/*
1474 * For constructed types, number of external tags may be greater than
1475 * number of elements in the type because of CHOICE type.
1476 * T ::= SET { -- Three possible tags:
1477 * a INTEGER, -- One tag is here...
1478 * b Choice1 -- ... and two more tags are there.
1479 * }
1480 * Choice1 ::= CHOICE {
1481 * s1 IA5String,
1482 * s2 ObjectDescriptor
1483 * }
1484 */
1485static int
vlm940bc6b2004-10-03 09:13:30 +00001486_fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001487 asn1p_expr_t *expr = arg->expr;
1488 arg_t tmparg = *arg;
1489 asn1p_expr_t *v;
1490 int element = 0;
vlm940bc6b2004-10-03 09:13:30 +00001491 int original_count = *count;
1492 int sort_until = -1;
vlmfa67ddc2004-06-03 03:38:44 +00001493
1494 TQ_FOR(v, &(expr->members), next) {
vlm940bc6b2004-10-03 09:13:30 +00001495 if(v->expr_type == A1TC_EXTENSIBLE) {
1496 /*
vlm80a48592005-02-25 12:10:27 +00001497 * CXER mandates sorting
vlm940bc6b2004-10-03 09:13:30 +00001498 * only for the root part.
1499 */
1500 if(flags == FTE_CANONICAL_XER
1501 && sort_until == -1)
1502 sort_until = *count;
vlmfa67ddc2004-06-03 03:38:44 +00001503 continue;
vlm940bc6b2004-10-03 09:13:30 +00001504 }
vlmfa67ddc2004-06-03 03:38:44 +00001505
1506 tmparg.expr = v;
1507
1508 if(_add_tag2el_member(&tmparg, tag2el, count,
vlm940bc6b2004-10-03 09:13:30 +00001509 (el_no==-1)?element:el_no, flags)) {
vlmfa67ddc2004-06-03 03:38:44 +00001510 return -1;
1511 }
1512
1513 element++;
1514 }
1515
vlm940bc6b2004-10-03 09:13:30 +00001516
1517 if(flags == FTE_CANONICAL_XER) {
1518 if(sort_until == -1) sort_until = *count;
1519 qsort((*tag2el) + original_count,
1520 sort_until - original_count,
1521 sizeof(**tag2el), _tag2el_cmp);
1522 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
1523 && (sort_until - original_count) >= 1) {
1524 /* Only take in account the root component */
1525 *count = original_count + 1;
1526 }
1527 } else {
1528 /*
1529 * Sort the map according to canonical order of their
1530 * tags and element numbers.
1531 */
1532 qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp);
1533 }
vlm4e03ce22004-06-06 07:20:17 +00001534
vlmc8aeab42004-06-14 13:09:45 +00001535 /*
1536 * Initialize .toff_{first|last} members.
1537 */
1538 if(*count) {
1539 struct asn1p_type_tag_s *cur_tag = 0;
1540 tag2el_t *cur = *tag2el;
1541 tag2el_t *end = cur + *count;
1542 int occur, i;
1543 for(occur = 0; cur < end; cur++) {
1544 if(cur_tag == 0
1545 || cur_tag->tag_value != cur->el_tag.tag_value
1546 || cur_tag->tag_class != cur->el_tag.tag_class) {
1547 cur_tag = &cur->el_tag;
1548 occur = 0;
1549 } else {
1550 occur++;
1551 }
1552 cur->toff_first = -occur;
1553 for(i = 0; i >= -occur; i--)
1554 cur[i].toff_last = -i;
1555 }
1556 }
1557
vlmfa67ddc2004-06-03 03:38:44 +00001558 return 0;
1559}
1560
1561static int
vlm940bc6b2004-10-03 09:13:30 +00001562_add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) {
vlmfa67ddc2004-06-03 03:38:44 +00001563 struct asn1p_type_tag_s tag;
1564 int ret;
1565
1566 assert(el_no >= 0);
1567
vlm39ba4c42004-09-22 16:06:28 +00001568 ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module,
vlm337167e2005-11-26 11:25:14 +00001569 arg->expr, &tag, AFT_IMAGINARY_ANY);
vlmfa67ddc2004-06-03 03:38:44 +00001570 if(ret == 0) {
vlme2070ea2004-09-04 04:42:29 +00001571 tag2el_t *te;
1572 int new_count = (*count) + 1;
vlmfa67ddc2004-06-03 03:38:44 +00001573 void *p;
vlme2070ea2004-09-04 04:42:29 +00001574
vlm2de0e792004-09-05 10:42:33 +00001575 if(tag.tag_value == -1) {
1576 /*
1577 * This is an untagged ANY type,
1578 * proceed without adding a tag
1579 */
1580 return 0;
1581 }
1582
vlme2070ea2004-09-04 04:42:29 +00001583 p = realloc(*tag2el, new_count * sizeof(tag2el_t));
vlmfa67ddc2004-06-03 03:38:44 +00001584 if(p) *tag2el = p;
1585 else return -1;
1586
vlm86380d32006-10-09 12:07:58 +00001587 if(0) DEBUG("Found tag for %s: %ld",
vlmfa67ddc2004-06-03 03:38:44 +00001588 arg->expr->Identifier,
1589 (long)tag.tag_value);
1590
vlme2070ea2004-09-04 04:42:29 +00001591 te = &((*tag2el)[*count]);
1592 te->el_tag = tag;
1593 te->el_no = el_no;
1594 te->from_expr = arg->expr;
1595 *count = new_count;
vlmfa67ddc2004-06-03 03:38:44 +00001596 return 0;
1597 }
1598
1599 DEBUG("Searching tag in complex expression %s:%x at line %d",
1600 arg->expr->Identifier,
1601 arg->expr->expr_type,
1602 arg->expr->_lineno);
1603
1604 /*
1605 * Iterate over members of CHOICE type.
1606 */
1607 if(arg->expr->expr_type == ASN_CONSTR_CHOICE) {
vlm940bc6b2004-10-03 09:13:30 +00001608 return _fill_tag2el_map(arg, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001609 }
1610
1611 if(arg->expr->expr_type == A1TC_REFERENCE) {
1612 arg_t tmp = *arg;
1613 asn1p_expr_t *expr;
vlm6c4a8502005-08-18 13:38:19 +00001614 expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.expr,
vlmfa67ddc2004-06-03 03:38:44 +00001615 arg->expr->reference);
1616 if(expr) {
1617 tmp.expr = expr;
vlm940bc6b2004-10-03 09:13:30 +00001618 return _add_tag2el_member(&tmp, tag2el, count, el_no, flags);
vlmfa67ddc2004-06-03 03:38:44 +00001619 } else {
1620 FATAL("Cannot dereference %s at line %d",
1621 arg->expr->Identifier,
1622 arg->expr->_lineno);
1623 return -1;
1624 }
1625 }
1626
1627 DEBUG("No tag for %s at line %d",
1628 arg->expr->Identifier,
1629 arg->expr->_lineno);
1630
1631 return -1;
1632}
1633
1634static int
vlm940bc6b2004-10-03 09:13:30 +00001635emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) {
vlm4e03ce22004-06-06 07:20:17 +00001636 asn1p_expr_t *expr = arg->expr;
vlmfbd6d9e2005-06-02 05:21:37 +00001637 int i;
1638
1639 if(!tag2el_count) return 0; /* No top level tags */
vlm4e03ce22004-06-06 07:20:17 +00001640
vlm337167e2005-11-26 11:25:14 +00001641 OUT("static asn_TYPE_tag2member_t asn_MAP_%s_tag2el%s_%d[] = {\n",
vlm0c6d3812006-03-21 03:40:38 +00001642 MKID(expr), opt_modifier?opt_modifier:"",
vlm337167e2005-11-26 11:25:14 +00001643 expr->_type_unique_index);
vlmfbd6d9e2005-06-02 05:21:37 +00001644 for(i = 0; i < tag2el_count; i++) {
1645 OUT(" { ");
1646 _print_tag(arg, &tag2el[i].el_tag);
1647 OUT(", ");
1648 OUT("%d, ", tag2el[i].el_no);
1649 OUT("%d, ", tag2el[i].toff_first);
1650 OUT("%d ", tag2el[i].toff_last);
1651 OUT("}%s /* %s at %d */\n",
1652 (i + 1 < tag2el_count) ? "," : "",
1653 tag2el[i].from_expr->Identifier,
1654 tag2el[i].from_expr->_lineno
1655 );
vlm4e03ce22004-06-06 07:20:17 +00001656 }
1657 OUT("};\n");
1658
vlmfbd6d9e2005-06-02 05:21:37 +00001659 return 0;
vlm4e03ce22004-06-06 07:20:17 +00001660}
1661
vlm72425de2004-09-13 08:31:01 +00001662static enum tvm_compat
1663emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) {
1664 struct asn1p_type_tag_s *tags = 0; /* Effective tags */
1665 struct asn1p_type_tag_s *all_tags = 0; /* The full array */
vlm6e73a042004-08-11 07:17:22 +00001666 int tags_count = 0;
vlm72425de2004-09-13 08:31:01 +00001667 int all_tags_count = 0;
1668 enum tvm_compat tv_mode = _TVM_SAME;
vlm1308d2b2004-09-10 15:49:15 +00001669 int i;
vlm4a3f5822004-06-28 21:13:46 +00001670
vlmc6de2c42004-09-14 14:10:10 +00001671 /* Cleanup before proceeding. */
1672 *tags_count_r = 0;
1673 *all_tags_count_r = 0;
1674
vlm1308d2b2004-09-10 15:49:15 +00001675 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001676 tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0);
vlm86380d32006-10-09 12:07:58 +00001677 if(tags_count < 0) {
1678 DEBUG("fail to fetch tags for %s", expr->Identifier);
vlmc6de2c42004-09-14 14:10:10 +00001679 return -1;
vlm86380d32006-10-09 12:07:58 +00001680 }
vlm6e73a042004-08-11 07:17:22 +00001681
vlm72425de2004-09-13 08:31:01 +00001682 /* Fetch a chain of tags */
vlm39ba4c42004-09-22 16:06:28 +00001683 all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr,
vlm72425de2004-09-13 08:31:01 +00001684 &all_tags, AFT_FULL_COLLECT);
1685 if(all_tags_count < 0) {
1686 if(tags) free(tags);
vlm86380d32006-10-09 12:07:58 +00001687 DEBUG("fail to fetch tags chain for %s", expr->Identifier);
vlm72425de2004-09-13 08:31:01 +00001688 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
vlm86380d32006-10-09 12:07:58 +00001774static asn1c_integer_t
1775PER_FROM_alphabet_characters(asn1cnst_range_t *range) {
1776 asn1c_integer_t numchars = 0;
1777 if(range->el_count) {
1778 int i;
1779 for(i = 0; i < range->el_count; i++)
1780 numchars
1781 += PER_FROM_alphabet_characters(range->elements[i]);
1782 } else {
1783 assert(range->left.type == ARE_VALUE);
1784 assert(range->right.type == ARE_VALUE);
1785 numchars = 1 + (range->right.value - range->left.value);
1786 }
1787 return numchars;
1788}
1789
vlm337167e2005-11-26 11:25:14 +00001790static int
vlm86380d32006-10-09 12:07:58 +00001791emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int alphabetsize, char *type) {
vlm337167e2005-11-26 11:25:14 +00001792 if(!range || range->incompatible || range->not_PER_visible) {
1793 OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }");
1794 return 0;
1795 }
1796
1797 if(range->left.type == ARE_VALUE) {
1798 if(range->right.type == ARE_VALUE) {
1799 asn1c_integer_t cover = 1;
1800 asn1c_integer_t r = 1 + range->right.value
1801 - range->left.value;
vlm1fcf7592006-08-18 02:27:55 +00001802 size_t rbits; /* Value range bits */
1803 ssize_t ebits; /* Value effective range bits */
vlm337167e2005-11-26 11:25:14 +00001804
1805 if(range->empty_constraint)
1806 r = 0;
1807
vlm86380d32006-10-09 12:07:58 +00001808 if(alphabetsize) {
1809 /* X.691: 27.5.2 */
1810 r = PER_FROM_alphabet_characters(range);
1811 }
1812
vlm337167e2005-11-26 11:25:14 +00001813 /* Compute real constraint */
1814 for(rbits = 0; rbits < (8 * sizeof(r)); rbits++) {
1815 if(r <= cover)
1816 break;
1817 cover *= 2; /* Can't do shifting */
1818 if(cover < 0) {
1819 FATAL("Constraint at line %d too wide "
1820 "for %d-bits integer type",
1821 arg->expr->_lineno,
1822 sizeof(r) * 8);
1823 rbits = sizeof(r);
1824 break;
1825 }
1826 }
1827
vlm1daeca02006-11-24 11:20:27 +00001828 if(alphabetsize) {
1829 ebits = rbits;
1830 } else {
vlm337167e2005-11-26 11:25:14 +00001831 /* X.691, #10.9.4.1 */
1832 for(ebits = 0; ebits <= 16; ebits++)
1833 if(r <= 1 << ebits) break;
1834 if(ebits == 17
1835 || range->right.value >= 65536)
1836 ebits = -1;
vlm1daeca02006-11-24 11:20:27 +00001837 if(0) {
vlm337167e2005-11-26 11:25:14 +00001838 /* X.691, #10.5.7.1 */
1839 for(ebits = 0; ebits <= 8; ebits++)
1840 if(r <= 1 << ebits) break;
1841 if(ebits == 9) {
1842 if(r <= 65536)
1843 ebits = 16;
1844 else
1845 ebits = -1;
1846 }
1847 }
vlm1daeca02006-11-24 11:20:27 +00001848 }
vlm86380d32006-10-09 12:07:58 +00001849
vlm337167e2005-11-26 11:25:14 +00001850 OUT("{ APC_CONSTRAINED%s,%s% d, % d, ",
1851 range->extensible
1852 ? " | APC_EXTENSIBLE" : "",
1853 range->extensible ? " " : "\t", rbits, ebits);
vlm1daeca02006-11-24 11:20:27 +00001854
1855 if(alphabetsize) {
1856 asn1c_integer_t lv = range->left.value;
1857 asn1c_integer_t rv = range->right.value;
1858 int gcmt = 0;
1859 if(lv > 0x7fffffff) { lv = 0x7fffffff; gcmt++; }
1860 if(rv > 0x7fffffff) { rv = 0x7fffffff; gcmt++; }
1861 if(gcmt) {
1862 OUT("% " PRIdASN ", % " PRIdASN " }",
1863 lv, rv);
1864 goto pcmt;
1865 }
1866 }
vlm337167e2005-11-26 11:25:14 +00001867 } else {
1868 if(range->extensible) {
1869 OUT("{ APC_SEMI_CONSTRAINED | APC_EXTENSIBLE, "
1870 "-1, ");
1871 } else {
1872 OUT("{ APC_SEMI_CONSTRAINED,\t-1, -1, ");
1873 }
1874 }
1875 OUT("% " PRIdASN ", % " PRIdASN " }",
1876 range->left.value, range->right.value);
1877 } else {
1878 OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }");
1879 }
1880
vlm1daeca02006-11-24 11:20:27 +00001881 pcmt:
1882
vlm337167e2005-11-26 11:25:14 +00001883 /*
1884 * Print some courtesy debug information.
1885 */
1886 if(range->left.type == ARE_VALUE
1887 || range->right.type == ARE_VALUE) {
1888 OUT("\t/* ");
1889 if(type) OUT("(%s", type);
1890 OUT("(");
1891 if(range->left.type == ARE_VALUE)
1892 OUT("%" PRIdASN, range->left.value);
1893 else
1894 OUT("MIN");
1895 OUT("..");
1896 if(range->right.type == ARE_VALUE)
1897 OUT("%" PRIdASN, range->right.value);
1898 else
1899 OUT("MAX");
1900 if(range->extensible) OUT(",...");
1901 if(type) OUT(")");
1902 OUT(") */");
1903 }
1904
1905 return 0;
1906}
1907
1908static int
vlma0da1a32006-10-09 12:27:44 +00001909emit_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx) {
vlm86380d32006-10-09 12:07:58 +00001910 int save_target = arg->target->target;
vlm337167e2005-11-26 11:25:14 +00001911 asn1cnst_range_t *range;
1912 asn1p_expr_type_e etype;
1913
vlmb0267572006-10-09 12:52:15 +00001914 etype = expr_get_type(arg, expr);
1915
vlm86380d32006-10-09 12:07:58 +00001916 if((arg->flags & A1C_GEN_PER)
1917 && (expr->constraints
vlmb0267572006-10-09 12:52:15 +00001918 || etype == ASN_BASIC_ENUMERATED
1919 || etype == ASN_CONSTR_CHOICE)
vlm86380d32006-10-09 12:07:58 +00001920 ) {
1921 /* Fall through */
1922 } else {
1923 return 0;
1924 }
1925
1926 REDIR(OT_CTDEFS);
1927
1928 OUT("static asn_per_constraints_t "
vlma0da1a32006-10-09 12:27:44 +00001929 "asn_PER_%s_%s_constr_%d = {\n",
1930 pfx, MKID(expr), expr->_type_unique_index);
vlm86380d32006-10-09 12:07:58 +00001931
vlm337167e2005-11-26 11:25:14 +00001932 INDENT(+1);
1933
1934 /*
1935 * ENUMERATED and CHOICE are special.
1936 */
1937 if(etype == ASN_BASIC_ENUMERATED
1938 || etype == ASN_CONSTR_CHOICE) {
1939 asn1cnst_range_t tmprng;
1940 asn1p_expr_t *v;
1941 int extensible = 0;
1942 int eidx = -1;
1943
1944 expr = asn1f_find_terminal_type_ex(arg->asn, expr);
1945 assert(expr);
1946
1947 TQ_FOR(v, &(expr->members), next) {
1948 if(v->expr_type == A1TC_EXTENSIBLE) {
1949 extensible++;
1950 break;
1951 }
1952 eidx++;
1953 }
1954
1955 memset(&tmprng, 0, sizeof (tmprng));
1956 tmprng.extensible = extensible;
1957 if(eidx < 0) tmprng.empty_constraint = 1;
1958 tmprng.left.type = ARE_VALUE;
1959 tmprng.left.value = 0;
1960 tmprng.right.type = ARE_VALUE;
1961 tmprng.right.value = eidx < 0 ? 0 : eidx;
vlm86380d32006-10-09 12:07:58 +00001962 if(emit_single_member_PER_constraint(arg, &tmprng, 0, 0))
vlm337167e2005-11-26 11:25:14 +00001963 return -1;
vlme54d16a2006-09-21 01:50:13 +00001964 } else if(etype & ASN_STRING_KM_MASK) {
1965 range = asn1constraint_compute_PER_range(etype,
1966 expr->combined_constraints, ACT_CT_FROM,
1967 0, 0, 0);
vlm86380d32006-10-09 12:07:58 +00001968 DEBUG("Emitting FROM constraint for %s", expr->Identifier);
vlm1daeca02006-11-24 11:20:27 +00001969
1970 if((range->left.type == ARE_MIN && range->right.type == ARE_MAX)
1971 || range->not_PER_visible) {
1972 switch(etype) {
1973 case ASN_STRING_BMPString:
1974 range->left.type = ARE_VALUE;
1975 range->left.value = 0;
1976 range->right.type = ARE_VALUE;
1977 range->right.value = 65535;
1978 range->not_PER_visible = 0;
1979 range->extensible = 0;
1980 break;
1981 case ASN_STRING_UniversalString:
1982 OUT("{ APC_CONSTRAINED,\t32, 32,"
1983 " 0, 2147483647 }"
1984 " /* special case 1 */\n");
1985 goto avoid;
1986 default:
1987 break;
1988 }
1989 }
vlm86380d32006-10-09 12:07:58 +00001990 if(emit_single_member_PER_constraint(arg, range, 1, 0))
vlme54d16a2006-09-21 01:50:13 +00001991 return -1;
vlm1daeca02006-11-24 11:20:27 +00001992 avoid:
vlme54d16a2006-09-21 01:50:13 +00001993 asn1constraint_range_free(range);
vlm337167e2005-11-26 11:25:14 +00001994 } else {
1995 range = asn1constraint_compute_PER_range(etype,
1996 expr->combined_constraints, ACT_EL_RANGE,
1997 0, 0, 0);
vlm86380d32006-10-09 12:07:58 +00001998 if(emit_single_member_PER_constraint(arg, range, 0, 0))
vlm337167e2005-11-26 11:25:14 +00001999 return -1;
2000 asn1constraint_range_free(range);
2001 }
2002 OUT(",\n");
2003
2004 range = asn1constraint_compute_PER_range(etype,
2005 expr->combined_constraints, ACT_CT_SIZE, 0, 0, 0);
vlm86380d32006-10-09 12:07:58 +00002006 if(emit_single_member_PER_constraint(arg, range, 0, "SIZE"))
vlm337167e2005-11-26 11:25:14 +00002007 return -1;
2008 asn1constraint_range_free(range);
vlm86380d32006-10-09 12:07:58 +00002009 OUT(",\n");
2010
2011 if((etype & ASN_STRING_KM_MASK) && (expr->_mark & TM_PERFROMCT)) {
2012 int old_target = arg->target->target;
2013 REDIR(OT_CODE);
2014
2015 OUT("static int asn_PER_MAP_%s_%d_v2c(unsigned int value) {\n",
2016 MKID(expr), expr->_type_unique_index);
2017 OUT("\tif(value >= sizeof(permitted_alphabet_table_%d)/"
2018 "sizeof(permitted_alphabet_table_%d[0]))\n",
2019 expr->_type_unique_index,
2020 expr->_type_unique_index);
2021 OUT("\t\treturn -1;\n");
2022 OUT("\treturn permitted_alphabet_table_%d[value] - 1;\n",
2023 expr->_type_unique_index);
2024 OUT("}\n");
2025
2026 OUT("static int asn_PER_MAP_%s_%d_c2v(unsigned int code) {\n",
2027 MKID(expr), expr->_type_unique_index);
2028 OUT("\tif(code >= sizeof(permitted_alphabet_code2value_%d)/"
2029 "sizeof(permitted_alphabet_code2value_%d[0]))\n",
2030 expr->_type_unique_index,
2031 expr->_type_unique_index);
2032 OUT("\t\treturn -1;\n");
2033 OUT("\treturn permitted_alphabet_code2value_%d[code];\n",
2034 expr->_type_unique_index);
2035 OUT("}\n");
2036
2037 REDIR(old_target);
2038
2039 OUT("asn_PER_MAP_%s_%d_v2c,\t/* Value to PER code map */\n",
2040 MKID(expr), expr->_type_unique_index);
2041 OUT("asn_PER_MAP_%s_%d_c2v\t/* PER code to value map */\n",
2042 MKID(expr), expr->_type_unique_index);
2043 } else if(etype & ASN_STRING_KM_MASK) {
2044 DEBUG("No PER value map necessary for %s", MKID(expr));
2045 OUT("0, 0\t/* No PER character map necessary */\n");
2046 } else {
2047 OUT("0, 0\t/* No PER value map */\n");
2048 }
vlm337167e2005-11-26 11:25:14 +00002049
2050 INDENT(-1);
2051
vlm86380d32006-10-09 12:07:58 +00002052 OUT("};\n");
vlm337167e2005-11-26 11:25:14 +00002053
vlm86380d32006-10-09 12:07:58 +00002054 REDIR(save_target);
vlm337167e2005-11-26 11:25:14 +00002055
2056 return 0;
2057}
2058
2059static int
2060try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) {
2061 int save_target = arg->target->target;
2062 asn1p_expr_type_e etype = expr_get_type(arg, expr);
2063 int fits_long = 0;
2064
2065 switch(etype) {
2066 case ASN_BASIC_BOOLEAN:
2067 fits_long = 1;
2068 case ASN_BASIC_INTEGER:
2069 case ASN_BASIC_ENUMERATED:
2070 if(expr->marker.default_value == NULL
2071 || expr->marker.default_value->type != ATV_INTEGER)
2072 break;
2073 if(!fits_long)
2074 fits_long = asn1c_type_fits_long(arg, expr)!=FL_NOTFIT;
2075 if(fits_long && !expr->marker.default_value->value.v_integer)
2076 expr->marker.flags &= ~EM_INDIRECT;
2077 if(!out) return 1;
2078 REDIR(OT_STAT_DEFS);
vlm9ea7d412006-08-18 01:32:26 +00002079 OUT("static int asn_DFL_%d_set_%" PRIdASN "(int set_value, void **sptr) {\n",
vlm337167e2005-11-26 11:25:14 +00002080 expr->_type_unique_index,
2081 expr->marker.default_value->value.v_integer);
2082 INDENT(+1);
2083 OUT("%s *st = *sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE));
2084 OUT("\n");
2085 OUT("if(!st) {\n");
vlm9ea7d412006-08-18 01:32:26 +00002086 OUT("\tif(!set_value) return -1;\t/* Not a default value */\n");
vlm337167e2005-11-26 11:25:14 +00002087 OUT("\tst = (*sptr = CALLOC(1, sizeof(*st)));\n");
2088 OUT("\tif(!st) return -1;\n");
2089 OUT("}\n");
2090 OUT("\n");
vlm9ea7d412006-08-18 01:32:26 +00002091 OUT("if(set_value) {\n");
2092 INDENT(+1);
vlm337167e2005-11-26 11:25:14 +00002093 OUT("/* Install default value %" PRIdASN " */\n",
2094 expr->marker.default_value->value.v_integer);
2095 if(fits_long) {
2096 OUT("*st = %" PRIdASN ";\n",
2097 expr->marker.default_value->value.v_integer);
2098 OUT("return 0;\n");
2099 } else {
2100 OUT("return asn_long2INTEGER(st, %" PRIdASN ");\n",
2101 expr->marker.default_value->value.v_integer);
2102 }
2103 INDENT(-1);
vlm9ea7d412006-08-18 01:32:26 +00002104 OUT("} else {\n");
2105 INDENT(+1);
2106 OUT("/* Test default value %" PRIdASN " */\n",
2107 expr->marker.default_value->value.v_integer);
2108 if(fits_long) {
2109 OUT("return (*st == %" PRIdASN ");\n",
2110 expr->marker.default_value->value.v_integer);
2111 } else {
2112 OUT("long value;\n");
2113 OUT("if(asn_INTEGER2long(st, &value))\n");
2114 OUT("\treturn -1;\n");
2115 OUT("return (value == %" PRIdASN ");\n",
2116 expr->marker.default_value->value.v_integer);
2117 }
2118 INDENT(-1);
2119 OUT("}\n");
2120 INDENT(-1);
vlm337167e2005-11-26 11:25:14 +00002121 OUT("}\n");
2122 REDIR(save_target);
2123 return 1;
2124 case ASN_BASIC_NULL:
2125 //expr->marker.flags &= ~EM_INDIRECT;
2126 return 0;
2127 default:
2128 break;
2129 }
2130 return 0;
2131}
2132
vlmb2839012004-08-20 13:37:01 +00002133static int
vlm4e554992004-08-25 02:03:12 +00002134emit_member_table(arg_t *arg, asn1p_expr_t *expr) {
vlmb2839012004-08-20 13:37:01 +00002135 int save_target;
2136 arg_t tmp_arg;
vlm060fe2a2004-09-10 09:37:12 +00002137 struct asn1p_type_tag_s outmost_tag_s;
2138 struct asn1p_type_tag_s *outmost_tag;
vlm80a48592005-02-25 12:10:27 +00002139 int complex_contents;
vlmb2839012004-08-20 13:37:01 +00002140 char *p;
2141
vlm060fe2a2004-09-10 09:37:12 +00002142 if(asn1f_fetch_outmost_tag(arg->asn,
vlm337167e2005-11-26 11:25:14 +00002143 expr->module, expr, &outmost_tag_s,
2144 AFT_IMAGINARY_ANY)) {
vlm060fe2a2004-09-10 09:37:12 +00002145 outmost_tag = 0;
2146 } else {
2147 outmost_tag = &outmost_tag_s;
2148 }
2149
vlmb2839012004-08-20 13:37:01 +00002150 OUT("{ ");
vlm060fe2a2004-09-10 09:37:12 +00002151
2152 if(outmost_tag && outmost_tag->tag_value == -1)
2153 OUT("ATF_OPEN_TYPE | ");
vlm0b567bf2005-03-04 22:18:20 +00002154 OUT("%s, ",
2155 (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS");
vlm6c4a8502005-08-18 13:38:19 +00002156 if((expr->marker.flags & EM_OMITABLE) == EM_OMITABLE) {
vlmb2839012004-08-20 13:37:01 +00002157 asn1p_expr_t *tv;
2158 int opts = 0;
vlm6c4a8502005-08-18 13:38:19 +00002159 for(tv = expr;
2160 tv && (tv->marker.flags & EM_OMITABLE) == EM_OMITABLE;
vlmb2839012004-08-20 13:37:01 +00002161 tv = TQ_NEXT(tv, next), opts++) {
2162 if(tv->expr_type == A1TC_EXTENSIBLE)
2163 opts--;
2164 }
2165 OUT("%d, ", opts);
2166 } else {
2167 OUT("0, ");
2168 }
vlm39ba4c42004-09-22 16:06:28 +00002169 if(expr->_anonymous_type) {
2170 assert(arg->expr->expr_type == ASN_CONSTR_SET_OF
2171 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF);
2172 OUT("0,\n");
2173 } else {
vlm766cf1e2005-03-04 08:48:53 +00002174 OUT("offsetof(struct ");
vlm2e774282005-08-14 15:03:31 +00002175 out_name_chain(arg, ONC_avoid_keywords);
vlm766cf1e2005-03-04 08:48:53 +00002176 OUT(", ");
vlmb2839012004-08-20 13:37:01 +00002177 if(arg->expr->expr_type == ASN_CONSTR_CHOICE
2178 && (!UNNAMED_UNIONS)) OUT("choice.");
vlm0c6d3812006-03-21 03:40:38 +00002179 OUT("%s),\n", MKID_safe(expr));
vlmb2839012004-08-20 13:37:01 +00002180 }
2181 INDENT(+1);
2182 if(C99_MODE) OUT(".tag = ");
vlm060fe2a2004-09-10 09:37:12 +00002183 if(outmost_tag) {
2184 if(outmost_tag->tag_value == -1)
2185 OUT("-1 /* Ambiguous tag (ANY?) */");
2186 else
2187 _print_tag(arg, outmost_tag);
vlma8a86cc2004-09-10 06:07:18 +00002188 } else {
vlm060fe2a2004-09-10 09:37:12 +00002189 OUT("-1 /* Ambiguous tag (CHOICE?) */");
vlma8a86cc2004-09-10 06:07:18 +00002190 }
vlm060fe2a2004-09-10 09:37:12 +00002191
vlmb2839012004-08-20 13:37:01 +00002192 OUT(",\n");
2193 if(C99_MODE) OUT(".tag_mode = ");
vlmc154db62006-10-19 02:46:01 +00002194 if((!(expr->expr_type & ASN_CONSTR_MASK)
2195 || expr->expr_type == ASN_CONSTR_CHOICE)
2196 && expr->tag.tag_class) {
vlmb2839012004-08-20 13:37:01 +00002197 if(expr->tag.tag_mode == TM_IMPLICIT)
2198 OUT("-1,\t/* IMPLICIT tag at current level */\n");
2199 else
2200 OUT("+1,\t/* EXPLICIT tag at current level */\n");
2201 } else {
2202 OUT("0,\n");
2203 }
vlm80a48592005-02-25 12:10:27 +00002204
2205 complex_contents =
2206 (expr->expr_type & ASN_CONSTR_MASK)
2207 || expr->expr_type == ASN_BASIC_ENUMERATED
vlmc89422d2005-03-03 21:28:12 +00002208 || (0 /* -- prohibited by X.693:8.3.4 */
2209 && expr->expr_type == ASN_BASIC_INTEGER
vlm80a48592005-02-25 12:10:27 +00002210 && expr_elements_count(arg, expr));
vlmb2839012004-08-20 13:37:01 +00002211 if(C99_MODE) OUT(".type = ");
vlme0fea712005-08-29 10:31:14 +00002212 OUT("&asn_DEF_");
vlm766cf1e2005-03-04 08:48:53 +00002213 if(complex_contents) {
vlm0c6d3812006-03-21 03:40:38 +00002214 OUT("%s", MKID(expr));
vlm766cf1e2005-03-04 08:48:53 +00002215 if(!(arg->flags & A1C_ALL_DEFS_GLOBAL))
2216 OUT("_%d", expr->_type_unique_index);
vlmdae7f9d2004-08-22 03:25:24 +00002217 } else {
vlm766cf1e2005-03-04 08:48:53 +00002218 OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE));
vlmdae7f9d2004-08-22 03:25:24 +00002219 }
vlm766cf1e2005-03-04 08:48:53 +00002220 OUT(",\n");
vlmb2839012004-08-20 13:37:01 +00002221 if(C99_MODE) OUT(".memb_constraints = ");
2222 if(expr->constraints) {
vlm86f5ed22004-09-26 13:11:31 +00002223 if(arg->flags & A1C_NO_CONSTRAINTS) {
2224 OUT("0,\t/* No check because of -fno-constraints */\n");
2225 } else {
vlm0c6d3812006-03-21 03:40:38 +00002226 char *id = MKID(expr);
vlm86f5ed22004-09-26 13:11:31 +00002227 if(expr->_anonymous_type
vlmbf3c5112005-02-14 20:41:29 +00002228 && !strcmp(expr->Identifier, "Member"))
vlm86f5ed22004-09-26 13:11:31 +00002229 id = asn1c_type_name(arg, expr, TNF_SAFE);
vlm337167e2005-11-26 11:25:14 +00002230 OUT("memb_%s_constraint_%d,\n", id,
vlm766cf1e2005-03-04 08:48:53 +00002231 arg->expr->_type_unique_index);
vlm86f5ed22004-09-26 13:11:31 +00002232 }
vlmb2839012004-08-20 13:37:01 +00002233 } else {
vlm95a476e2005-01-17 12:16:58 +00002234 OUT("0,\t/* Defer constraints checking to the member type */\n");
vlmb2839012004-08-20 13:37:01 +00002235 }
vlm337167e2005-11-26 11:25:14 +00002236 if(C99_MODE) OUT(".per_constraints = ");
2237 if(arg->flags & A1C_GEN_PER) {
vlmb0267572006-10-09 12:52:15 +00002238 if(expr->constraints) {
vlma0da1a32006-10-09 12:27:44 +00002239 OUT("&asn_PER_memb_%s_constr_%d,\n",
vlm0c6d3812006-03-21 03:40:38 +00002240 MKID(expr),
vlm337167e2005-11-26 11:25:14 +00002241 expr->_type_unique_index);
2242 } else {
2243 OUT("0,\t/* No PER visible constraints */\n");
2244 }
vlmbf3c5112005-02-14 20:41:29 +00002245 } else {
vlm337167e2005-11-26 11:25:14 +00002246 OUT("0,\t/* PER is not compiled, use -gen-PER */\n");
2247 }
2248 if(C99_MODE) OUT(".default_value = ");
2249 if(try_inline_default(arg, expr, 0)) {
2250 OUT("asn_DFL_%d_set_%" PRIdASN
2251 ",\t/* DEFAULT %" PRIdASN " */\n",
2252 expr->_type_unique_index,
2253 expr->marker.default_value->value.v_integer,
2254 expr->marker.default_value->value.v_integer);
2255 } else {
2256 OUT("0,\n");
2257 }
2258 if(C99_MODE) OUT(".name = ");
2259 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member")) {
2260 OUT("\"\"\n");
2261 } else {
2262 OUT("\"%s\"\n", expr->Identifier);
vlmbf3c5112005-02-14 20:41:29 +00002263 }
vlmb2839012004-08-20 13:37:01 +00002264 OUT("},\n");
2265 INDENT(-1);
2266
vlm86f5ed22004-09-26 13:11:31 +00002267 if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS))
vlmb2839012004-08-20 13:37:01 +00002268 return 0;
2269
2270 save_target = arg->target->target;
2271 REDIR(OT_CODE);
2272
vlmbf3c5112005-02-14 20:41:29 +00002273 if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member"))
vlmb2839012004-08-20 13:37:01 +00002274 p = asn1c_type_name(arg, expr, TNF_SAFE);
vlm39ba4c42004-09-22 16:06:28 +00002275 else
vlm0c6d3812006-03-21 03:40:38 +00002276 p = MKID(expr);
vlmb2839012004-08-20 13:37:01 +00002277 OUT("static int\n");
vlm337167e2005-11-26 11:25:14 +00002278 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 +00002279 INDENT(+1);
vlmaf68ef52006-07-13 11:19:01 +00002280 OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {\n");
vlmb2839012004-08-20 13:37:01 +00002281 tmp_arg = *arg;
2282 tmp_arg.expr = expr;
vlm86380d32006-10-09 12:07:58 +00002283 DEBUG("member constraint checking code for %s", p);
vlmb2839012004-08-20 13:37:01 +00002284 if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) {
vlm86f5ed22004-09-26 13:11:31 +00002285 OUT("return td->check_constraints"
vlmaf68ef52006-07-13 11:19:01 +00002286 "(td, sptr, ctfailcb, app_key);\n");
vlmb2839012004-08-20 13:37:01 +00002287 }
2288 INDENT(-1);
2289 OUT("}\n");
2290 OUT("\n");
2291
vlma0da1a32006-10-09 12:27:44 +00002292 if(emit_member_PER_constraints(arg, expr, "memb"))
vlm86380d32006-10-09 12:07:58 +00002293 return -1;
2294
vlmb2839012004-08-20 13:37:01 +00002295 REDIR(save_target);
2296
2297 return 0;
2298}
vlm4e554992004-08-25 02:03:12 +00002299
vlm9de248e2004-10-20 15:50:55 +00002300/*
2301 * Generate "asn_DEF_XXX" type definition.
2302 */
vlm4e554992004-08-25 02:03:12 +00002303static int
vlm86f5ed22004-09-26 13:11:31 +00002304emit_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) {
vlmfbceb502006-09-17 06:31:08 +00002305 asn1p_expr_t *terminal;
vlm766cf1e2005-03-04 08:48:53 +00002306 int using_type_name = 0;
vlm0c6d3812006-03-21 03:40:38 +00002307 char *p = MKID(expr);
vlm337167e2005-11-26 11:25:14 +00002308
vlmfbceb502006-09-17 06:31:08 +00002309 terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
2310
vlma0da1a32006-10-09 12:27:44 +00002311 if(emit_member_PER_constraints(arg, expr, "type"))
vlm86380d32006-10-09 12:07:58 +00002312 return -1;
vlm4e554992004-08-25 02:03:12 +00002313
vlm12c8f692004-09-06 08:07:29 +00002314 if(HIDE_INNER_DEFS)
2315 OUT("static /* Use -fall-defs-global to expose */\n");
vlm337167e2005-11-26 11:25:14 +00002316 OUT("asn_TYPE_descriptor_t asn_DEF_%s", p);
vlm766cf1e2005-03-04 08:48:53 +00002317 if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index);
2318 OUT(" = {\n");
vlm72425de2004-09-13 08:31:01 +00002319 INDENT(+1);
vlm337167e2005-11-26 11:25:14 +00002320
2321 if(expr->_anonymous_type) {
2322 p = ASN_EXPR_TYPE2STR(expr->expr_type);
2323 OUT("\"%s\",\n", p?p:"");
vlm0c6d3812006-03-21 03:40:38 +00002324 OUT("\"%s\",\n",
2325 p ? asn1c_make_identifier(AMI_CHECK_RESERVED,
2326 0, p, 0) : "");
vlm337167e2005-11-26 11:25:14 +00002327 } else {
2328 OUT("\"%s\",\n", expr->Identifier);
2329 OUT("\"%s\",\n", expr->Identifier);
2330 }
vlm4e554992004-08-25 02:03:12 +00002331
2332 if(expr->expr_type & ASN_CONSTR_MASK) {
vlm766cf1e2005-03-04 08:48:53 +00002333 using_type_name = 1;
vlm4e554992004-08-25 02:03:12 +00002334 p = asn1c_type_name(arg, arg->expr, TNF_SAFE);
vlm337167e2005-11-26 11:25:14 +00002335 } else {
vlm0c6d3812006-03-21 03:40:38 +00002336 p = MKID(expr);
vlm4e554992004-08-25 02:03:12 +00002337 }
2338
vlm766cf1e2005-03-04 08:48:53 +00002339#define FUNCREF(foo) do { \
2340 OUT("%s", p); \
2341 if(HIDE_INNER_DEFS && !using_type_name) \
2342 OUT("_%d", expr->_type_unique_index); \
2343 OUT("_" #foo ",\n"); \
2344} while(0)
2345
2346 FUNCREF(free);
2347 FUNCREF(print);
2348 FUNCREF(constraint);
2349 FUNCREF(decode_ber);
2350 FUNCREF(encode_der);
2351 FUNCREF(decode_xer);
2352 FUNCREF(encode_xer);
vlm9ea7d412006-08-18 01:32:26 +00002353 if(arg->flags & A1C_GEN_PER) {
vlm337167e2005-11-26 11:25:14 +00002354 FUNCREF(decode_uper);
vlm9ea7d412006-08-18 01:32:26 +00002355 FUNCREF(encode_uper);
2356 } else {
2357 OUT("0, 0,\t/* No PER support, "
2358 "use \"-gen-PER\" to enable */\n");
2359 }
vlm4e554992004-08-25 02:03:12 +00002360
vlmfbceb502006-09-17 06:31:08 +00002361 if(!terminal || terminal->expr_type == ASN_CONSTR_CHOICE) {
2362 //if(expr->expr_type == ASN_CONSTR_CHOICE) {
vlm4e554992004-08-25 02:03:12 +00002363 OUT("CHOICE_outmost_tag,\n");
2364 } else {
2365 OUT("0,\t/* Use generic outmost tag fetcher */\n");
2366 }
2367
vlm0c6d3812006-03-21 03:40:38 +00002368 p = MKID(expr);
vlm4e554992004-08-25 02:03:12 +00002369 if(tags_count) {
vlm337167e2005-11-26 11:25:14 +00002370 OUT("asn_DEF_%s_tags_%d,\n",
vlm766cf1e2005-03-04 08:48:53 +00002371 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002372 OUT("sizeof(asn_DEF_%s_tags_%d)\n",
vlm766cf1e2005-03-04 08:48:53 +00002373 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002374 OUT("\t/sizeof(asn_DEF_%s_tags_%d[0])",
vlm766cf1e2005-03-04 08:48:53 +00002375 p, expr->_type_unique_index);
vlm72425de2004-09-13 08:31:01 +00002376 if(tv_mode == _TVM_SUBSET
2377 && tags_count != all_tags_count)
2378 OUT(" - %d", all_tags_count - tags_count);
2379 OUT(", /* %d */\n", tags_count);
vlm4e554992004-08-25 02:03:12 +00002380 } else {
vlm72425de2004-09-13 08:31:01 +00002381 OUT("0,\t/* No effective tags (pointer) */\n");
2382 OUT("0,\t/* No effective tags (count) */\n");
2383 }
2384
2385 if(all_tags_count && tv_mode == _TVM_DIFFERENT) {
vlm337167e2005-11-26 11:25:14 +00002386 OUT("asn_DEF_%s_all_tags_%d,\n",
vlm766cf1e2005-03-04 08:48:53 +00002387 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002388 OUT("sizeof(asn_DEF_%s_all_tags_%d)\n",
vlm766cf1e2005-03-04 08:48:53 +00002389 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002390 OUT("\t/sizeof(asn_DEF_%s_all_tags_%d[0]), /* %d */\n",
vlm766cf1e2005-03-04 08:48:53 +00002391 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00002392 } else if(all_tags_count) {
vlm337167e2005-11-26 11:25:14 +00002393 OUT("asn_DEF_%s_tags_%d,\t/* Same as above */\n",
vlm766cf1e2005-03-04 08:48:53 +00002394 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002395 OUT("sizeof(asn_DEF_%s_tags_%d)\n",
vlm766cf1e2005-03-04 08:48:53 +00002396 p, expr->_type_unique_index);
vlm337167e2005-11-26 11:25:14 +00002397 OUT("\t/sizeof(asn_DEF_%s_tags_%d[0]), /* %d */\n",
vlm766cf1e2005-03-04 08:48:53 +00002398 p, expr->_type_unique_index, all_tags_count);
vlm72425de2004-09-13 08:31:01 +00002399 } else {
2400 OUT("0,\t/* No tags (pointer) */\n");
2401 OUT("0,\t/* No tags (count) */\n");
vlm4e554992004-08-25 02:03:12 +00002402 }
2403
vlm337167e2005-11-26 11:25:14 +00002404 if(arg->flags & A1C_GEN_PER) {
2405 if(expr->constraints
2406 || expr->expr_type == ASN_BASIC_ENUMERATED
2407 || expr->expr_type == ASN_CONSTR_CHOICE) {
vlma0da1a32006-10-09 12:27:44 +00002408 OUT("&asn_PER_type_%s_constr_%d,\n",
vlm337167e2005-11-26 11:25:14 +00002409 p, expr->_type_unique_index);
2410 } else {
2411 OUT("0,\t/* No PER visible constraints */\n");
2412 }
2413 } else {
2414 OUT("0,\t/* No PER visible constraints */\n");
2415 }
2416
vlm4e554992004-08-25 02:03:12 +00002417 if(elements_count) {
vlm766cf1e2005-03-04 08:48:53 +00002418 OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00002419 if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF
2420 || expr->expr_type == ASN_CONSTR_SET_OF) {
2421 OUT("%d,\t/* Single element */\n",
2422 elements_count);
2423 assert(elements_count == 1);
2424 } else {
2425 OUT("%d,\t/* Elements count */\n",
2426 elements_count);
2427 }
2428 } else {
vlm4e554992004-08-25 02:03:12 +00002429 if(expr_elements_count(arg, expr))
2430 OUT("0, 0,\t/* Defined elsewhere */\n");
2431 else
2432 OUT("0, 0,\t/* No members */\n");
2433 }
2434
2435 switch(spec) {
2436 case ETD_NO_SPECIFICS:
2437 OUT("0\t/* No specifics */\n");
2438 break;
2439 case ETD_HAS_SPECIFICS:
vlm337167e2005-11-26 11:25:14 +00002440 OUT("&asn_SPC_%s_specs_%d\t/* Additional specs */\n",
vlm766cf1e2005-03-04 08:48:53 +00002441 p, expr->_type_unique_index);
vlm4e554992004-08-25 02:03:12 +00002442 }
vlm72425de2004-09-13 08:31:01 +00002443 INDENT(-1);
vlm4e554992004-08-25 02:03:12 +00002444 OUT("};\n");
2445 OUT("\n");
vlme2bb4432004-08-26 06:20:34 +00002446
2447 return 0;
vlm4e554992004-08-25 02:03:12 +00002448}
vlmddd5a7d2004-09-10 09:18:20 +00002449
vlm39ba4c42004-09-22 16:06:28 +00002450static int
2451expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) {
vlm6d44a542005-11-08 03:06:16 +00002452 /*
2453 * X.680, 25.5, Table 5
2454 */
vlm337167e2005-11-26 11:25:14 +00002455 switch(expr_get_type(arg, expr)) {
vlm39ba4c42004-09-22 16:06:28 +00002456 case ASN_BASIC_BOOLEAN:
2457 case ASN_BASIC_ENUMERATED:
2458 case ASN_BASIC_NULL:
2459 return 1;
vlm6d44a542005-11-08 03:06:16 +00002460 case ASN_CONSTR_CHOICE:
2461 return 2;
vlm39ba4c42004-09-22 16:06:28 +00002462 default:
2463 return 0;
2464 }
2465}
vlmaf841972005-01-28 12:18:50 +00002466
2467static int
vlm2e774282005-08-14 15:03:31 +00002468out_name_chain(arg_t *arg, enum onc_flags onc_flags) {
vlmaf841972005-01-28 12:18:50 +00002469 asn1p_expr_t *expr = arg->expr;
2470 char *id;
2471
2472 assert(expr->Identifier);
2473
vlm2e774282005-08-14 15:03:31 +00002474 if((arg->flags & A1C_COMPOUND_NAMES
2475 || onc_flags & ONC_force_compound_name)
vlm766cf1e2005-03-04 08:48:53 +00002476 && ((expr->expr_type & ASN_CONSTR_MASK)
2477 || expr->expr_type == ASN_BASIC_ENUMERATED
vlm96853d82005-08-13 23:51:47 +00002478 || ((expr->expr_type == ASN_BASIC_INTEGER
2479 || expr->expr_type == ASN_BASIC_BIT_STRING)
vlm766cf1e2005-03-04 08:48:53 +00002480 && expr_elements_count(arg, expr))
2481 )
vlmaf841972005-01-28 12:18:50 +00002482 && expr->parent_expr
2483 && expr->parent_expr->Identifier) {
2484 arg_t tmparg = *arg;
2485
2486 tmparg.expr = expr->parent_expr;
vlm766cf1e2005-03-04 08:48:53 +00002487 if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES;
2488
vlm2e774282005-08-14 15:03:31 +00002489 out_name_chain(&tmparg, onc_flags);
vlmaf841972005-01-28 12:18:50 +00002490
vlm92d3cad2005-03-05 00:08:41 +00002491 OUT("__"); /* a separator between id components */
vlm766cf1e2005-03-04 08:48:53 +00002492
vlmaf841972005-01-28 12:18:50 +00002493 /* Fall through */
2494 }
2495
vlm2e774282005-08-14 15:03:31 +00002496 if(onc_flags & ONC_avoid_keywords)
vlm0c6d3812006-03-21 03:40:38 +00002497 id = MKID_safe(expr);
vlmaf841972005-01-28 12:18:50 +00002498 else
vlm0c6d3812006-03-21 03:40:38 +00002499 id = MKID(expr);
vlmaf841972005-01-28 12:18:50 +00002500 OUT("%s", id);
2501
2502 return 0;
2503}
vlm6c4a8502005-08-18 13:38:19 +00002504
2505static int
2506emit_include_dependencies(arg_t *arg) {
2507 asn1p_expr_t *expr = arg->expr;
2508 asn1p_expr_t *memb;
2509
2510 /* Avoid recursive definitions. */
2511 TQ_FOR(memb, &(expr->members), next) {
2512 expr_break_recursion(arg, memb);
2513 }
2514
2515 TQ_FOR(memb, &(expr->members), next) {
2516
2517 if(memb->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) {
2518 if(terminal_structable(arg, memb)) {
2519 int saved_target = arg->target->target;
2520 REDIR(OT_FWD_DECLS);
2521 OUT("%s;\n",
2522 asn1c_type_name(arg, memb, TNF_RSAFE));
2523 REDIR(saved_target);
2524 }
2525 }
2526
2527 if((!(memb->expr_type & ASN_CONSTR_MASK)
2528 && memb->expr_type > ASN_CONSTR_MASK)
2529 || memb->meta_type == AMT_TYPEREF) {
2530 if(memb->marker.flags & EM_UNRECURSE) {
2531 GEN_POSTINCLUDE(asn1c_type_name(arg,
2532 memb, TNF_INCLUDE));
2533 } else {
2534 GEN_INCLUDE(asn1c_type_name(arg,
2535 memb, TNF_INCLUDE));
2536 }
2537 }
2538 }
2539
2540 return 0;
2541}
2542
2543/*
2544 * Check if it is better to make this type indirectly accessed via
2545 * a pointer.
2546 * This may be the case for the following recursive definition:
2547 * Type ::= CHOICE { member Type };
2548 */
2549static int
2550expr_break_recursion(arg_t *arg, asn1p_expr_t *expr) {
vlm6c4a8502005-08-18 13:38:19 +00002551 int ret;
2552
2553 if(expr->marker.flags & EM_UNRECURSE)
2554 return 1; /* Already broken */
2555
vlm6c4a8502005-08-18 13:38:19 +00002556 /* -findirect-choice compiles members of CHOICE as indirect pointers */
2557 if((arg->flags & A1C_INDIRECT_CHOICE)
2558 && arg->expr->expr_type == ASN_CONSTR_CHOICE
vlm337167e2005-11-26 11:25:14 +00002559 && (expr_get_type(arg, expr) & ASN_CONSTR_MASK)
vlm6c4a8502005-08-18 13:38:19 +00002560 ) {
2561 /* Break cross-reference */
2562 expr->marker.flags |= EM_INDIRECT | EM_UNRECURSE;
2563 return 1;
2564 }
2565
2566 if((expr->marker.flags & EM_INDIRECT)
2567 || arg->expr->expr_type == ASN_CONSTR_SET_OF
2568 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) {
2569 if(terminal_structable(arg, expr)) {
2570 expr->marker.flags |= EM_UNRECURSE;
2571
2572 if(arg->expr->expr_type == ASN_CONSTR_SET_OF
2573 || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) {
2574 /* Don't put EM_INDIRECT even if recursion */
2575 return 1;
2576 }
2577
2578 /* Fall through */
2579 }
2580 }
2581
2582 /* Look for recursive back-references */
2583 ret = expr_defined_recursively(arg, expr);
2584 switch(ret) {
2585 case 2: /* Explicitly break the recursion */
2586 case 1: /* Use safer typing */
2587 expr->marker.flags |= EM_INDIRECT;
2588 expr->marker.flags |= EM_UNRECURSE;
2589 break;
2590 }
2591
2592 return 0;
2593}
2594
2595/*
2596 * Check if the type can be represented using simple `struct TYPE`.
2597 */
2598static asn1p_expr_t *
2599terminal_structable(arg_t *arg, asn1p_expr_t *expr) {
2600 asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
2601 if(terminal
2602 && !terminal->parent_expr
2603 && (terminal->expr_type & ASN_CONSTR_MASK)) {
2604 return terminal;
2605 }
2606 return 0;
2607}
2608
2609static int
2610asn1c_recurse(arg_t *arg, asn1p_expr_t *expr, int (*callback)(arg_t *arg, void *key), void *key) {
2611 arg_t tmp = *arg;
2612 int maxret = 0;
2613 int ret;
2614
2615 if(expr->_mark) return 0;
2616 expr->_mark |= TM_RECURSION;
2617
2618 /* Invoke callback for every type going into recursion */
2619 tmp.expr = expr;
2620 maxret = callback(&tmp, key);
2621 if(maxret <= 1) {
2622 /*
2623 * Recursively invoke myself and the callbacks.
2624 */
2625 TQ_FOR(tmp.expr, &(expr->members), next) {
2626 ret = asn1c_recurse(&tmp, tmp.expr, callback, key);
2627 if(ret > maxret)
2628 maxret = ret;
2629 if(maxret > 1) break;
2630 }
2631 }
2632
2633 expr->_mark &= ~TM_RECURSION;
2634 return maxret;
2635}
2636
2637static int
2638check_is_refer_to(arg_t *arg, void *key) {
2639 asn1p_expr_t *terminal = terminal_structable(arg, arg->expr);
2640 if(terminal == key) {
2641 if(arg->expr->marker.flags & EM_INDIRECT)
2642 return 1; /* This is almost safe indirection */
2643 return 2;
2644 } else if(terminal) {
2645 /* This might be N-step circular loop. Dive into it. */
2646 return asn1c_recurse(arg, terminal, check_is_refer_to, key);
2647 }
2648 return 0;
2649}
2650
2651/*
2652 * Check if the possibly inner expression defined recursively.
2653 */
2654static int
2655expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr) {
2656 asn1p_expr_t *terminal;
2657 asn1p_expr_t *topmost;
2658
2659 /* If expression is top-level, there's no way it can be recursive. */
2660 if(expr->parent_expr == 0) return 0;
2661 if(expr->expr_type != A1TC_REFERENCE)
2662 return 0; /* Basic types are never recursive */
2663
2664 terminal = terminal_structable(arg, expr);
2665 if(!terminal) return 0; /* Terminal cannot be indirected */
2666
2667 /* Search for the parent container for the given expression */
2668 topmost = expr;
2669 while(topmost->parent_expr)
2670 topmost = topmost->parent_expr;
2671
2672 /* Look inside the terminal type if it mentions the parent expression */
2673 return asn1c_recurse(arg, terminal, check_is_refer_to, topmost);
2674}
vlm337167e2005-11-26 11:25:14 +00002675
2676struct canonical_map_element {
2677 int eidx;
2678 asn1p_expr_t *expr;
2679};
2680static int compar_cameo(const void *ap, const void *bp);
2681static arg_t *cameo_arg;
2682static int *
2683compute_canonical_members_order(arg_t *arg, int el_count) {
2684 struct canonical_map_element *cmap;
2685 int *rmap;
2686 asn1p_expr_t *v;
2687 int eidx = 0;
2688 int ext_start = -1;
2689 int nextmax = -1;
2690 int already_sorted = 1;
2691
2692 cmap = calloc(el_count, sizeof *cmap);
2693 assert(cmap);
2694
2695 TQ_FOR(v, &(arg->expr->members), next) {
2696 if(v->expr_type != A1TC_EXTENSIBLE) {
2697 cmap[eidx].eidx = eidx;
2698 cmap[eidx].expr = v;
2699 eidx++;
2700 } else if(ext_start == -1)
2701 ext_start = eidx;
2702 }
2703
2704 cameo_arg = arg;
2705 if(ext_start == -1) {
2706 /* Sort the whole thing */
2707 qsort(cmap, el_count, sizeof(*cmap), compar_cameo);
2708 } else {
2709 /* Sort root and extensions independently */
2710 qsort(cmap, ext_start, sizeof(*cmap), compar_cameo);
2711 qsort(cmap + ext_start, el_count - ext_start,
2712 sizeof(*cmap), compar_cameo);
2713 }
2714
2715 /* move data back to a simpler map */
2716 rmap = calloc(el_count, sizeof *rmap);
2717 assert(rmap);
2718 for(eidx = 0; eidx < el_count; eidx++) {
2719 rmap[eidx] = cmap[eidx].eidx;
2720 if(rmap[eidx] <= nextmax)
2721 already_sorted = 0;
2722 else
2723 nextmax = rmap[eidx];
2724 }
2725 free(cmap);
2726
2727 if(already_sorted) { free(rmap); rmap = 0; }
2728 return rmap;
2729}
2730static int compar_cameo(const void *ap, const void *bp) {
2731 const struct canonical_map_element *a = (const void *)ap;
2732 const struct canonical_map_element *b = (const void *)bp;
2733 struct asn1p_type_tag_s atag, btag;
2734 arg_t *arg = cameo_arg;
2735
2736 if(asn1f_fetch_outmost_tag(arg->asn, a->expr->module, a->expr,
2737 &atag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE))
2738 return 1;
2739
2740 if(asn1f_fetch_outmost_tag(arg->asn, b->expr->module, b->expr,
2741 &btag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE))
2742 return -1;
2743
2744 if(atag.tag_class < btag.tag_class)
2745 return -1;
2746 if(atag.tag_class > btag.tag_class)
2747 return 1;
2748 if(atag.tag_value < btag.tag_value)
2749 return -1;
2750 if(atag.tag_value > btag.tag_value)
2751 return 1;
2752 return 0;
2753
2754}