blob: b7b66c44a4d32ebc65c4309ab4a1d6181f5018f7 [file] [log] [blame]
vlmb5be8c32004-08-18 05:42:05 +00001#include <asn1fix_internal.h>
2#include <asn1fix_constraint.h>
3#include <asn1fix_crange.h>
4
5static void _remove_exceptions(arg_t *arg, asn1p_constraint_t *ct);
6static int _constraint_value_resolve(arg_t *arg, asn1p_value_t **value);
7
8int
9asn1constraint_pullup(arg_t *arg) {
10 asn1p_expr_t *expr = arg->expr;
11 asn1p_constraint_t *ct_parent;
12 asn1p_constraint_t *ct_expr;
13 int ret;
14
15 if(expr->combined_constraints)
16 return 0; /* Operation already performed earlier */
17
18 switch(expr->meta_type) {
19 case AMT_TYPE:
20 case AMT_TYPEREF:
21 break;
22 default:
23 return 0; /* Nothing to do */
24 }
25
26 if(expr->expr_type == A1TC_REFERENCE) {
27 asn1p_ref_t *ref = expr->reference;
28 asn1p_module_t *mod_rw = arg->mod;
29 asn1p_expr_t *parent_expr;
30
31 assert(ref);
32 parent_expr = asn1f_lookup_symbol(arg, ref, &mod_rw);
33 if(!parent_expr) {
34 if(errno != EEXIST) {
35 DEBUG("\tWhile fetching parent constraints: "
36 "type \"%s\" not found: %s",
37 asn1f_printable_reference(ref),
38 strerror(errno));
39 return -1;
40 } else {
41 /*
42 * -fknown-extern-type is given.
43 * Assume there are no constraints there.
44 */
45 WARNING("External type \"%s\": "
46 "assuming no constraints",
47 asn1f_printable_reference(ref));
48 ct_parent = 0;
49 }
50 } else {
51 arg->expr = parent_expr;
52 ret = asn1constraint_pullup(arg);
53 arg->expr = expr;
54 if(ret) return ret;
55
56 ct_parent = parent_expr->combined_constraints;
57 }
58 } else {
59 ct_parent = 0;
60 }
61
62 ct_expr = expr->constraints;
63
64 if(!ct_parent && !ct_expr)
65 return 0; /* No constraints to consider */
66
67 if(ct_parent) {
68 ct_parent = asn1p_constraint_clone(ct_parent);
69 assert(ct_parent);
70 }
71
72 /*
73 * If the current type does not have constraints, it inherits
74 * the constraints of a parent.
75 */
76 if(ct_parent && !ct_expr) {
77 expr->combined_constraints = ct_parent;
78 return 0;
79 }
80
81 ct_expr = asn1p_constraint_clone(ct_expr);
82 assert(ct_expr);
83
84 /*
85 * Now we have a set of current expression's constraints,
86 * and an optional set of the parent expression's constraints.
87 */
88
89 if(ct_parent) {
90 /*
91 * If we have a parent, remove all the extensions (46.4).
92 */
93 _remove_exceptions(arg, ct_parent);
94
95 expr->combined_constraints = ct_parent;
96 if(ct_expr->type == ACT_CA_SET) {
97 int i;
98 for(i = 0; i < ct_expr->el_count; i++) {
99 if(asn1p_constraint_insert(
100 expr->combined_constraints,
101 ct_expr->elements[i])) {
102 expr->combined_constraints = 0;
103 asn1p_constraint_free(ct_expr);
104 asn1p_constraint_free(ct_parent);
105 return -1;
106 } else {
107 ct_expr->elements[i] = 0;
108 }
109 }
110 asn1p_constraint_free(ct_expr);
111 } else {
112 asn1p_constraint_insert(expr->combined_constraints,
113 ct_expr);
114 }
115 } else {
116 expr->combined_constraints = ct_expr;
117 }
118
119 return 0;
120}
121
122int
123asn1constraint_resolve(arg_t *arg, asn1p_constraint_t *ct) {
124 asn1p_expr_t *top_parent;
125 int rvalue = 0;
126 int ret;
127 int el;
128
129 if(!ct) return 0;
130
131 /* Don't touch information object classes */
132 switch(ct->type) {
133 case ACT_CT_WCOMP:
134 case ACT_CT_WCOMPS:
135 case ACT_CA_CRC:
136 return 0;
137 default:
138 break;
139 }
140
141 top_parent = asn1f_find_terminal_type(arg, arg->expr, 0);
142 if(top_parent) {
143 ret = asn1constraint_compatible(top_parent->expr_type,
144 ct->type);
145 switch(ret) {
146 case -1: /* If unknown, assume OK. */
147 case 1:
148 break;
149 case 0:
150 default:
151 FATAL("%s at line %d: "
152 "Constraint type %s is not applicable to %s",
153 arg->expr->Identifier, ct->_lineno,
154 asn1p_constraint_type2str(ct->type),
155 ASN_EXPR_TYPE2STR(top_parent->expr_type)
156 );
157 rvalue = -1;
158 break;
159 }
160 } else {
161 WARNING("%s at line %d: "
162 "Constraints ignored: Unresolved parent type",
163 arg->expr->Identifier, arg->expr->_lineno);
164 }
165
166 if(ct->value && ct->value->type == ATV_REFERENCED) {
167 ret = _constraint_value_resolve(arg, &ct->value);
168 RET2RVAL(ret, rvalue);
169 }
170 if(ct->range_start && ct->range_start->type == ATV_REFERENCED) {
171 ret = _constraint_value_resolve(arg, &ct->range_start);
172 RET2RVAL(ret, rvalue);
173 }
174 if(ct->range_stop && ct->range_stop->type == ATV_REFERENCED) {
175 ret = _constraint_value_resolve(arg, &ct->range_stop);
176 RET2RVAL(ret, rvalue);
177 }
178
179 for(el = 0; el < ct->el_count; el++) {
180 ret = asn1constraint_resolve(arg, ct->elements[el]);
181 RET2RVAL(ret, rvalue);
182 }
183
184 return rvalue;
185}
186
187static void
188_remove_exceptions(arg_t *arg, asn1p_constraint_t *ct) {
189 int i;
190
191 for(i = 0; i < ct->el_count; i++) {
192 if(ct->elements[i]->type == ACT_EL_EXT)
193 break;
194 _remove_exceptions(arg, ct->elements[i]);
195 }
196
197 /* Remove the elements at and after the extensibility mark */
198 for(; i < ct->el_count; ct->el_count--) {
199 asn1p_constraint_t *rm;
200 rm = ct->elements[ct->el_count-1];
201 asn1p_constraint_free(rm);
202 }
203
204 if(i < ct->el_size)
205 ct->elements[i] = 0;
206}
207
208
209static int
210_constraint_value_resolve(arg_t *arg, asn1p_value_t **value) {
211 asn1p_expr_t static_expr;
212 asn1p_expr_t *tmp_expr;
213 asn1p_module_t *mod_rw = arg->mod;
214 arg_t tmp_arg;
215 int rvalue = 0;
216 int ret;
217
218 tmp_expr = asn1f_lookup_symbol(arg, (*value)->value.reference, &mod_rw);
219 if(tmp_expr == NULL) {
220 FATAL("Cannot find symbol %s "
221 "used in %s subtype constraint at line %d",
222 asn1f_printable_reference((*value)->value.reference),
223 arg->expr->Identifier, arg->expr->_lineno);
224 assert((*value)->type == ATV_REFERENCED);
225 return -1;
226 }
227
228 static_expr = *tmp_expr;
229 static_expr.value = *value;
230 tmp_arg = *arg;
231 tmp_arg.mod = mod_rw;
232 tmp_arg.expr = &static_expr;
233 ret = asn1f_fix_dereference_values(&tmp_arg);
234 RET2RVAL(ret, rvalue);
235 assert(static_expr.value);
236 *value = static_expr.value;
237
238 return rvalue;
239}