blob: 159dbbc294026e4b7bf9daa9f7752413609e0d5c [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
3static int asn1f_parametrize(arg_t *arg, asn1p_expr_t *ex, asn1p_expr_t *ptype);
4static int asn1f_param_process_recursive(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype, asn1p_expr_t *actargs);
vlm76142452004-09-05 10:36:22 +00005static int asn1f_param_process_constraints(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype, asn1p_expr_t *actargs);
6
vlmfa67ddc2004-06-03 03:38:44 +00007static asn1p_expr_t *_referenced_argument(asn1p_ref_t *ref, asn1p_expr_t *ptype, asn1p_expr_t *actargs);
vlm76142452004-09-05 10:36:22 +00008static int _process_constraints(arg_t *arg, asn1p_constraint_t *ct, asn1p_expr_t *ptype, asn1p_expr_t *actargs);
vlmfa67ddc2004-06-03 03:38:44 +00009
10int
11asn1f_fix_parametrized_assignment(arg_t *arg) {
12 asn1p_expr_t *expr = arg->expr;
13 asn1p_expr_t *ptype;
14
15 assert(expr->expr_type == A1TC_PARAMETRIZED);
16 assert(expr->reference);
17
vlmfd245932005-03-10 10:02:50 +000018 DEBUG("(\"%s\" ::= \"%s\" { %s }) for line %d",
19 expr->Identifier,
vlmfa67ddc2004-06-03 03:38:44 +000020 asn1f_printable_reference(expr->reference),
21 asn1f_printable_value(expr->value),
22 expr->_lineno);
23
24 /*
25 * Find the corresponding parametrized type definition.
26 */
27 DEBUG("Looking for parametrized type definition \"%s\"",
28 asn1f_printable_reference(expr->reference));
vlm2e0c1942004-08-22 03:10:23 +000029 ptype = asn1f_lookup_symbol(arg, expr->module, expr->reference);
vlmfa67ddc2004-06-03 03:38:44 +000030 if(ptype == NULL) {
31 DEBUG("%s: missing parametrized type declaration",
32 asn1f_printable_reference(expr->reference));
33 return -1;
34 }
35
36 /*
37 * Check that the number of arguments which are expected by
38 * the parametrized type declaration is consistent with the
39 * number of arguments supplied by the parametrized assignment.
40 */
41 if(asn1f_count_children(expr) != ptype->params->params_count) {
42 FATAL("Number of actual arguments %d in %s at line %d "
43 "is not equal to number of expected arguments "
44 "%d in %s at line %d",
45 asn1f_count_children(expr),
46 asn1f_printable_reference(expr->reference),
47 expr->_lineno,
48 ptype->params->params_count,
49 ptype->Identifier,
50 ptype->_lineno
51 );
52 return -1;
53 }
54
55 /*
56 * Perform an expansion of a parametrized assignment.
57 */
58 return asn1f_parametrize(arg, expr, ptype);
59}
60
vlm6a02a8a2004-09-08 00:28:11 +000061#define SUBSTITUTE(to, from) do { \
vlm1457c0d2005-06-02 17:53:17 +000062 asn1p_expr_t tmp, *__v; \
63 if((to)->tag.tag_class \
64 && (from)->tag.tag_class) { \
65 FATAL("Layered tagging in parametrization " \
66 "is not yet supported, " \
vlma5b977d2005-06-06 08:28:58 +000067 "contact asn1c author for assistance " \
68 "with line %d", (to)->_lineno); \
vlm1457c0d2005-06-02 17:53:17 +000069 return -1; \
70 } \
71 /* This code shall not be invoked too early */ \
72 assert((to)->combined_constraints == NULL); \
73 assert((from)->combined_constraints == NULL); \
74 /* Copy stuff, and merge some parameters */ \
75 tmp = *(to); \
76 *(to) = *(from); \
77 TQ_MOVE(&(to)->members, &(from)->members); \
78 *(from) = tmp; \
79 (to)->next = tmp.next; \
80 (to)->parent_expr = tmp.parent_expr; \
81 assert((to)->marker.flags == EM_NOMARK); \
82 (to)->marker = tmp.marker; \
83 if(tmp.tag.tag_class) \
84 (to)->tag = tmp.tag; \
85 if(tmp.constraints) { \
86 if((to)->constraints) { \
87 asn1p_constraint_t *ct; \
88 ct = asn1p_constraint_new( \
89 (to)->constraints->_lineno); \
90 ct->type = ACT_CA_SET; \
91 asn1p_constraint_insert(ct, \
92 (to)->constraints); \
93 asn1p_constraint_insert(ct, \
94 tmp.constraints); \
95 (to)->constraints = ct; \
96 } else { \
97 (to)->constraints = tmp.constraints; \
vlm5b103032005-06-02 05:21:53 +000098 } \
vlm1457c0d2005-06-02 17:53:17 +000099 } \
100 (from)->constraints = 0; \
101 (from)->marker.default_value = 0; \
102 memset(&((from)->next), 0, sizeof((from)->next)); \
103 memset(&((from)->members), 0, sizeof((from)->members)); \
104 asn1p_expr_free(from); \
105 TQ_FOR(__v, &((to)->members), next) { \
106 assert(__v->parent_expr == (from)); \
107 __v->parent_expr = (to); \
108 } \
109} while(0)
vlmfa67ddc2004-06-03 03:38:44 +0000110
111static int
112asn1f_parametrize(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype) {
113 asn1p_expr_t *nex;
114 void *p;
115 int ret;
116
vlm5a7280d2005-02-22 07:59:59 +0000117 DEBUG("asn1f_parametrize(%s <= %s)",
118 expr->Identifier, ptype->Identifier);
119
vlmfa67ddc2004-06-03 03:38:44 +0000120 /*
121 * The algorithm goes like that:
122 * 1. Replace the expression's type with parametrized type.
123 * 2. For every child in the parametrized type, import it
124 * as a child of the expression, replacing all occurences of
125 * symbols which are defined as parametrized type arguments
126 * with the actual values.
vlm76142452004-09-05 10:36:22 +0000127 * 3. Don't forget to parametrize the subtype constraints.
vlmfa67ddc2004-06-03 03:38:44 +0000128 */
129
vlm2e0c1942004-08-22 03:10:23 +0000130 nex = asn1p_expr_clone(ptype, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000131 if(nex == NULL) return -1;
132
133 /*
134 * Cleanup the new expression so there is no ptype-related
135 * stuff hanging around.
136 */
137 p = strdup(expr->Identifier);
138 if(p) {
139 free(nex->Identifier);
140 nex->Identifier = p;
141 } else {
142 asn1p_expr_free(nex);
143 return -1;
144 }
145 asn1p_paramlist_free(nex->params);
146 nex->params = NULL;
147 nex->meta_type = expr->meta_type;
148
149 ret = asn1f_param_process_recursive(arg, nex, ptype, expr);
150 if(ret != 0) {
151 asn1p_expr_free(nex);
152 return ret;
153 }
154
155 SUBSTITUTE(expr, nex);
156
157 return ret;
158}
159
160static int
161asn1f_param_process_recursive(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
162 asn1p_expr_t *child;
163
vlm5a7280d2005-02-22 07:59:59 +0000164
vlmfa67ddc2004-06-03 03:38:44 +0000165 TQ_FOR(child, &(expr->members), next) {
166 asn1p_expr_t *ra;
vlm76142452004-09-05 10:36:22 +0000167 asn1p_expr_t *ne; /* new expression (clone) */
168
169 if(asn1f_param_process_constraints(arg, child, ptype, actargs))
170 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000171
172 ra = _referenced_argument(child->reference, ptype, actargs);
vlm76142452004-09-05 10:36:22 +0000173 if(ra) {
174 DEBUG("Substituting parameter for %s %s at line %d",
175 child->Identifier,
176 asn1f_printable_reference(child->reference),
177 child->_lineno
178 );
179
180 assert(child->meta_type == AMT_TYPEREF);
181 assert(child->expr_type == A1TC_REFERENCE);
182
183 ne = asn1p_expr_clone(ra, 0);
184 if(ne == NULL) return -1;
185 assert(ne->Identifier == 0);
186 ne->Identifier = strdup(child->Identifier);
187 if(ne->Identifier == 0) {
188 asn1p_expr_free(ne);
189 return -1;
190 }
191 SUBSTITUTE(child, ne);
vlmfa67ddc2004-06-03 03:38:44 +0000192 }
vlmfa67ddc2004-06-03 03:38:44 +0000193 }
194
195 return 0;
196}
197
vlm76142452004-09-05 10:36:22 +0000198/*
199 * Check that the given ref looks like an argument of a parametrized type.
200 */
vlmfa67ddc2004-06-03 03:38:44 +0000201static asn1p_expr_t *
202_referenced_argument(asn1p_ref_t *ref, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
203 asn1p_expr_t *aa;
204 int i;
205
206 if(ref == NULL || ref->comp_count != 1)
207 return NULL;
208
209 aa = TQ_FIRST(&(actargs->members));
210 for(i = 0; i < ptype->params->params_count;
211 i++, aa = TQ_NEXT(aa, next)) {
212 if(strcmp(ref->components[0].name,
213 ptype->params->params[i].argument) == 0)
214 return aa;
215 }
216
217 return NULL;
218}
vlm76142452004-09-05 10:36:22 +0000219
220/*
221 * Search for parameters inside constraints.
222 */
223static int
224asn1f_param_process_constraints(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
225 asn1p_constraint_t *cts;
226 int ret;
227
228 if(!expr->constraints) return 0;
229
230 cts = asn1p_constraint_clone(expr->constraints);
231 assert(cts);
232
233 ret = _process_constraints(arg, cts, ptype, actargs);
234 if(ret == 1) {
235 asn1p_constraint_free(expr->constraints);
236 expr->constraints = cts;
237 ret = 0;
238 } else {
239 asn1p_constraint_free(cts);
240 }
241
242 return ret;
243}
244
245static int
246_process_constraints(arg_t *arg, asn1p_constraint_t *ct, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
247 asn1p_value_t *values[3];
248 int rvalue = 0;
249 size_t i;
250
251 values[0] = ct->value;
252 values[1] = ct->range_start;
253 values[2] = ct->range_stop;
254
255 for(i = 0; i < sizeof(values)/sizeof(values[0]); i++) {
256 asn1p_value_t *v = values[i];
257 asn1p_expr_t *ra;
258 asn1p_ref_t *ref;
259 char *str;
260
261 if(!v || v->type != ATV_REFERENCED) continue;
262
263 ref = v->value.reference;
264 ra = _referenced_argument(ref, ptype, actargs);
265 if(!ra) continue;
266
267 DEBUG("_process_constraints(%s), ra=%s",
268 asn1f_printable_reference(ref), ra->Identifier);
269
vlm5a7280d2005-02-22 07:59:59 +0000270 if(ra->expr_type == A1TC_PARAMETRIZED) {
vlm8aacca22005-02-22 08:05:06 +0000271 DEBUG("Double %s", "parametrization");
vlm5a7280d2005-02-22 07:59:59 +0000272 }
273
274 assert(ra->Identifier);
vlm76142452004-09-05 10:36:22 +0000275 str = strdup(ra->Identifier);
276 if(!str) return -1;
277
278 assert(ref->comp_count == 1);
279 ref = asn1p_ref_new(ref->_lineno);
280 if(!ref) { free(str); return -1; }
281
282 if(asn1p_ref_add_component(ref, str, 0)) {
283 free(str);
284 return -1;
285 }
286
287 asn1p_ref_free(v->value.reference);
288 v->value.reference = ref;
289 rvalue = 1;
290 }
291
292 /* Process the rest of constraints recursively */
293 for(i = 0; i < ct->el_count; i++) {
294 int ret = _process_constraints(arg, ct->elements[i],
295 ptype, actargs);
296 if(ret == -1)
297 rvalue = -1;
298 else if(ret == 1 && rvalue != -1)
299 rvalue = 1;
300 }
301
302 return rvalue;
303}
304