blob: 8c9ce41af1103ed5f00fe419dcf05ed6f7dd6a58 [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 { \
62 asn1p_expr_t tmp, *__v; \
vlm5b103032005-06-02 05:21:53 +000063 if((to)->tag.tag_class \
64 && (from)->tag.tag_class) { \
65 FATAL("Layered tagging " \
66 "in parametrization " \
67 "is not yet supported, " \
68 "contact asn1c author for"); \
69 return -1; \
70 } \
vlm6a02a8a2004-09-08 00:28:11 +000071 tmp = *(to); \
72 *(to) = *(from); \
73 TQ_MOVE(&(to)->members, &(from)->members); \
74 *(from) = tmp; \
75 (to)->next = tmp.next; \
76 (to)->parent_expr = tmp.parent_expr; \
vlm5b103032005-06-02 05:21:53 +000077 if(tmp.tag.tag_class) \
78 (to)->tag = tmp.tag; \
vlm6a02a8a2004-09-08 00:28:11 +000079 memset(&((from)->next), 0, \
80 sizeof((from)->next)); \
81 memset(&((from)->members), 0, \
82 sizeof((from)->members)); \
83 asn1p_expr_free(from); \
84 TQ_FOR(__v, &((to)->members), next) { \
85 assert(__v->parent_expr == (from)); \
86 __v->parent_expr = (to); \
87 } \
vlmfa67ddc2004-06-03 03:38:44 +000088 } while(0)
89
90static int
91asn1f_parametrize(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype) {
92 asn1p_expr_t *nex;
93 void *p;
94 int ret;
95
vlm5a7280d2005-02-22 07:59:59 +000096 DEBUG("asn1f_parametrize(%s <= %s)",
97 expr->Identifier, ptype->Identifier);
98
vlmfa67ddc2004-06-03 03:38:44 +000099 /*
100 * The algorithm goes like that:
101 * 1. Replace the expression's type with parametrized type.
102 * 2. For every child in the parametrized type, import it
103 * as a child of the expression, replacing all occurences of
104 * symbols which are defined as parametrized type arguments
105 * with the actual values.
vlm76142452004-09-05 10:36:22 +0000106 * 3. Don't forget to parametrize the subtype constraints.
vlmfa67ddc2004-06-03 03:38:44 +0000107 */
108
vlm2e0c1942004-08-22 03:10:23 +0000109 nex = asn1p_expr_clone(ptype, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000110 if(nex == NULL) return -1;
111
112 /*
113 * Cleanup the new expression so there is no ptype-related
114 * stuff hanging around.
115 */
116 p = strdup(expr->Identifier);
117 if(p) {
118 free(nex->Identifier);
119 nex->Identifier = p;
120 } else {
121 asn1p_expr_free(nex);
122 return -1;
123 }
124 asn1p_paramlist_free(nex->params);
125 nex->params = NULL;
126 nex->meta_type = expr->meta_type;
127
128 ret = asn1f_param_process_recursive(arg, nex, ptype, expr);
129 if(ret != 0) {
130 asn1p_expr_free(nex);
131 return ret;
132 }
133
134 SUBSTITUTE(expr, nex);
135
136 return ret;
137}
138
139static int
140asn1f_param_process_recursive(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
141 asn1p_expr_t *child;
142
vlm5a7280d2005-02-22 07:59:59 +0000143
vlmfa67ddc2004-06-03 03:38:44 +0000144 TQ_FOR(child, &(expr->members), next) {
145 asn1p_expr_t *ra;
vlm76142452004-09-05 10:36:22 +0000146 asn1p_expr_t *ne; /* new expression (clone) */
147
148 if(asn1f_param_process_constraints(arg, child, ptype, actargs))
149 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000150
151 ra = _referenced_argument(child->reference, ptype, actargs);
vlm76142452004-09-05 10:36:22 +0000152 if(ra) {
153 DEBUG("Substituting parameter for %s %s at line %d",
154 child->Identifier,
155 asn1f_printable_reference(child->reference),
156 child->_lineno
157 );
158
159 assert(child->meta_type == AMT_TYPEREF);
160 assert(child->expr_type == A1TC_REFERENCE);
161
162 ne = asn1p_expr_clone(ra, 0);
163 if(ne == NULL) return -1;
164 assert(ne->Identifier == 0);
165 ne->Identifier = strdup(child->Identifier);
166 if(ne->Identifier == 0) {
167 asn1p_expr_free(ne);
168 return -1;
169 }
170 SUBSTITUTE(child, ne);
vlmfa67ddc2004-06-03 03:38:44 +0000171 }
vlmfa67ddc2004-06-03 03:38:44 +0000172 }
173
174 return 0;
175}
176
vlm76142452004-09-05 10:36:22 +0000177/*
178 * Check that the given ref looks like an argument of a parametrized type.
179 */
vlmfa67ddc2004-06-03 03:38:44 +0000180static asn1p_expr_t *
181_referenced_argument(asn1p_ref_t *ref, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
182 asn1p_expr_t *aa;
183 int i;
184
185 if(ref == NULL || ref->comp_count != 1)
186 return NULL;
187
188 aa = TQ_FIRST(&(actargs->members));
189 for(i = 0; i < ptype->params->params_count;
190 i++, aa = TQ_NEXT(aa, next)) {
191 if(strcmp(ref->components[0].name,
192 ptype->params->params[i].argument) == 0)
193 return aa;
194 }
195
196 return NULL;
197}
vlm76142452004-09-05 10:36:22 +0000198
199/*
200 * Search for parameters inside constraints.
201 */
202static int
203asn1f_param_process_constraints(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
204 asn1p_constraint_t *cts;
205 int ret;
206
207 if(!expr->constraints) return 0;
208
209 cts = asn1p_constraint_clone(expr->constraints);
210 assert(cts);
211
212 ret = _process_constraints(arg, cts, ptype, actargs);
213 if(ret == 1) {
214 asn1p_constraint_free(expr->constraints);
215 expr->constraints = cts;
216 ret = 0;
217 } else {
218 asn1p_constraint_free(cts);
219 }
220
221 return ret;
222}
223
224static int
225_process_constraints(arg_t *arg, asn1p_constraint_t *ct, asn1p_expr_t *ptype, asn1p_expr_t *actargs) {
226 asn1p_value_t *values[3];
227 int rvalue = 0;
228 size_t i;
229
230 values[0] = ct->value;
231 values[1] = ct->range_start;
232 values[2] = ct->range_stop;
233
234 for(i = 0; i < sizeof(values)/sizeof(values[0]); i++) {
235 asn1p_value_t *v = values[i];
236 asn1p_expr_t *ra;
237 asn1p_ref_t *ref;
238 char *str;
239
240 if(!v || v->type != ATV_REFERENCED) continue;
241
242 ref = v->value.reference;
243 ra = _referenced_argument(ref, ptype, actargs);
244 if(!ra) continue;
245
246 DEBUG("_process_constraints(%s), ra=%s",
247 asn1f_printable_reference(ref), ra->Identifier);
248
vlm5a7280d2005-02-22 07:59:59 +0000249 if(ra->expr_type == A1TC_PARAMETRIZED) {
vlm8aacca22005-02-22 08:05:06 +0000250 DEBUG("Double %s", "parametrization");
vlm5a7280d2005-02-22 07:59:59 +0000251 }
252
253 assert(ra->Identifier);
vlm76142452004-09-05 10:36:22 +0000254 str = strdup(ra->Identifier);
255 if(!str) return -1;
256
257 assert(ref->comp_count == 1);
258 ref = asn1p_ref_new(ref->_lineno);
259 if(!ref) { free(str); return -1; }
260
261 if(asn1p_ref_add_component(ref, str, 0)) {
262 free(str);
263 return -1;
264 }
265
266 asn1p_ref_free(v->value.reference);
267 v->value.reference = ref;
268 rvalue = 1;
269 }
270
271 /* Process the rest of constraints recursively */
272 for(i = 0; i < ct->el_count; i++) {
273 int ret = _process_constraints(arg, ct->elements[i],
274 ptype, actargs);
275 if(ret == -1)
276 rvalue = -1;
277 else if(ret == 1 && rvalue != -1)
278 rvalue = 1;
279 }
280
281 return rvalue;
282}
283