blob: ba624cb84e75a75593e4bee6f3b72bf3ed9352fb [file] [log] [blame]
Lev Walkinf15320b2004-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);
Lev Walkind541c252004-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
Lev Walkinf15320b2004-06-03 03:38:44 +00007static asn1p_expr_t *_referenced_argument(asn1p_ref_t *ref, asn1p_expr_t *ptype, asn1p_expr_t *actargs);
Lev Walkind541c252004-09-05 10:36:22 +00008static int _process_constraints(arg_t *arg, asn1p_constraint_t *ct, asn1p_expr_t *ptype, asn1p_expr_t *actargs);
Lev Walkinf15320b2004-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
Lev Walkin03850182005-03-10 10:02:50 +000018 DEBUG("(\"%s\" ::= \"%s\" { %s }) for line %d",
19 expr->Identifier,
Lev Walkinf15320b2004-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));
Lev Walkin6fec44d2004-08-22 03:10:23 +000029 ptype = asn1f_lookup_symbol(arg, expr->module, expr->reference);
Lev Walkinf15320b2004-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
Lev Walkin1004aa92004-09-08 00:28:11 +000061#define SUBSTITUTE(to, from) do { \
Lev Walkin0320d872005-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, " \
67 "contact asn1c author for assistance with"); \
68 return -1; \
69 } \
70 /* This code shall not be invoked too early */ \
71 assert((to)->combined_constraints == NULL); \
72 assert((from)->combined_constraints == NULL); \
73 /* Copy stuff, and merge some parameters */ \
74 tmp = *(to); \
75 *(to) = *(from); \
76 TQ_MOVE(&(to)->members, &(from)->members); \
77 *(from) = tmp; \
78 (to)->next = tmp.next; \
79 (to)->parent_expr = tmp.parent_expr; \
80 assert((to)->marker.flags == EM_NOMARK); \
81 (to)->marker = tmp.marker; \
82 if(tmp.tag.tag_class) \
83 (to)->tag = tmp.tag; \
84 if(tmp.constraints) { \
85 if((to)->constraints) { \
86 asn1p_constraint_t *ct; \
87 ct = asn1p_constraint_new( \
88 (to)->constraints->_lineno); \
89 ct->type = ACT_CA_SET; \
90 asn1p_constraint_insert(ct, \
91 (to)->constraints); \
92 asn1p_constraint_insert(ct, \
93 tmp.constraints); \
94 (to)->constraints = ct; \
95 } else { \
96 (to)->constraints = tmp.constraints; \
Lev Walkin71160962005-06-02 05:21:53 +000097 } \
Lev Walkin0320d872005-06-02 17:53:17 +000098 } \
99 (from)->constraints = 0; \
100 (from)->marker.default_value = 0; \
101 memset(&((from)->next), 0, sizeof((from)->next)); \
102 memset(&((from)->members), 0, sizeof((from)->members)); \
103 asn1p_expr_free(from); \
104 TQ_FOR(__v, &((to)->members), next) { \
105 assert(__v->parent_expr == (from)); \
106 __v->parent_expr = (to); \
107 } \
108} while(0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000109
110static int
111asn1f_parametrize(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype) {
112 asn1p_expr_t *nex;
113 void *p;
114 int ret;
115
Lev Walkinf593f102005-02-22 07:59:59 +0000116 DEBUG("asn1f_parametrize(%s <= %s)",
117 expr->Identifier, ptype->Identifier);
118
Lev Walkinf15320b2004-06-03 03:38:44 +0000119 /*
120 * The algorithm goes like that:
121 * 1. Replace the expression's type with parametrized type.
122 * 2. For every child in the parametrized type, import it
123 * as a child of the expression, replacing all occurences of
124 * symbols which are defined as parametrized type arguments
125 * with the actual values.
Lev Walkind541c252004-09-05 10:36:22 +0000126 * 3. Don't forget to parametrize the subtype constraints.
Lev Walkinf15320b2004-06-03 03:38:44 +0000127 */
128
Lev Walkin6fec44d2004-08-22 03:10:23 +0000129 nex = asn1p_expr_clone(ptype, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 if(nex == NULL) return -1;
131
132 /*
133 * Cleanup the new expression so there is no ptype-related
134 * stuff hanging around.
135 */
136 p = strdup(expr->Identifier);
137 if(p) {
138 free(nex->Identifier);
139 nex->Identifier = p;
140 } else {
141 asn1p_expr_free(nex);
142 return -1;
143 }
144 asn1p_paramlist_free(nex->params);
145 nex->params = NULL;
146 nex->meta_type = expr->meta_type;
147
148 ret = asn1f_param_process_recursive(arg, nex, ptype, expr);
149 if(ret != 0) {
150 asn1p_expr_free(nex);
151 return ret;
152 }
153
154 SUBSTITUTE(expr, nex);
155
156 return ret;
157}
158
159static int
160asn1f_param_process_recursive(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
161 asn1p_expr_t *child;
162
Lev Walkinf593f102005-02-22 07:59:59 +0000163
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 TQ_FOR(child, &(expr->members), next) {
165 asn1p_expr_t *ra;
Lev Walkind541c252004-09-05 10:36:22 +0000166 asn1p_expr_t *ne; /* new expression (clone) */
167
168 if(asn1f_param_process_constraints(arg, child, ptype, actargs))
169 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000170
171 ra = _referenced_argument(child->reference, ptype, actargs);
Lev Walkind541c252004-09-05 10:36:22 +0000172 if(ra) {
173 DEBUG("Substituting parameter for %s %s at line %d",
174 child->Identifier,
175 asn1f_printable_reference(child->reference),
176 child->_lineno
177 );
178
179 assert(child->meta_type == AMT_TYPEREF);
180 assert(child->expr_type == A1TC_REFERENCE);
181
182 ne = asn1p_expr_clone(ra, 0);
183 if(ne == NULL) return -1;
184 assert(ne->Identifier == 0);
185 ne->Identifier = strdup(child->Identifier);
186 if(ne->Identifier == 0) {
187 asn1p_expr_free(ne);
188 return -1;
189 }
190 SUBSTITUTE(child, ne);
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 }
193
194 return 0;
195}
196
Lev Walkind541c252004-09-05 10:36:22 +0000197/*
198 * Check that the given ref looks like an argument of a parametrized type.
199 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000200static asn1p_expr_t *
201_referenced_argument(asn1p_ref_t *ref, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
202 asn1p_expr_t *aa;
203 int i;
204
205 if(ref == NULL || ref->comp_count != 1)
206 return NULL;
207
208 aa = TQ_FIRST(&(actargs->members));
209 for(i = 0; i < ptype->params->params_count;
210 i++, aa = TQ_NEXT(aa, next)) {
211 if(strcmp(ref->components[0].name,
212 ptype->params->params[i].argument) == 0)
213 return aa;
214 }
215
216 return NULL;
217}
Lev Walkind541c252004-09-05 10:36:22 +0000218
219/*
220 * Search for parameters inside constraints.
221 */
222static int
223asn1f_param_process_constraints(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
224 asn1p_constraint_t *cts;
225 int ret;
226
227 if(!expr->constraints) return 0;
228
229 cts = asn1p_constraint_clone(expr->constraints);
230 assert(cts);
231
232 ret = _process_constraints(arg, cts, ptype, actargs);
233 if(ret == 1) {
234 asn1p_constraint_free(expr->constraints);
235 expr->constraints = cts;
236 ret = 0;
237 } else {
238 asn1p_constraint_free(cts);
239 }
240
241 return ret;
242}
243
244static int
245_process_constraints(arg_t *arg, asn1p_constraint_t *ct, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
246 asn1p_value_t *values[3];
247 int rvalue = 0;
248 size_t i;
249
250 values[0] = ct->value;
251 values[1] = ct->range_start;
252 values[2] = ct->range_stop;
253
254 for(i = 0; i < sizeof(values)/sizeof(values[0]); i++) {
255 asn1p_value_t *v = values[i];
256 asn1p_expr_t *ra;
257 asn1p_ref_t *ref;
258 char *str;
259
260 if(!v || v->type != ATV_REFERENCED) continue;
261
262 ref = v->value.reference;
263 ra = _referenced_argument(ref, ptype, actargs);
264 if(!ra) continue;
265
266 DEBUG("_process_constraints(%s), ra=%s",
267 asn1f_printable_reference(ref), ra->Identifier);
268
Lev Walkinf593f102005-02-22 07:59:59 +0000269 if(ra->expr_type == A1TC_PARAMETRIZED) {
Lev Walkin2c65b122005-02-22 08:05:06 +0000270 DEBUG("Double %s", "parametrization");
Lev Walkinf593f102005-02-22 07:59:59 +0000271 }
272
273 assert(ra->Identifier);
Lev Walkind541c252004-09-05 10:36:22 +0000274 str = strdup(ra->Identifier);
275 if(!str) return -1;
276
277 assert(ref->comp_count == 1);
278 ref = asn1p_ref_new(ref->_lineno);
279 if(!ref) { free(str); return -1; }
280
281 if(asn1p_ref_add_component(ref, str, 0)) {
282 free(str);
283 return -1;
284 }
285
286 asn1p_ref_free(v->value.reference);
287 v->value.reference = ref;
288 rvalue = 1;
289 }
290
291 /* Process the rest of constraints recursively */
292 for(i = 0; i < ct->el_count; i++) {
293 int ret = _process_constraints(arg, ct->elements[i],
294 ptype, actargs);
295 if(ret == -1)
296 rvalue = -1;
297 else if(ret == 1 && rvalue != -1)
298 rvalue = 1;
299 }
300
301 return rvalue;
302}
303