blob: 0e60ef7bec103cab2020d7f6fd9bb436bad9dddc [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
3static int _asn1f_copy_value(arg_t *arg, asn1p_expr_t *to,asn1p_expr_t *from);
4
5int
vlm5ba49f52005-03-10 16:15:57 +00006asn1f_value_resolve(arg_t *arg, asn1p_expr_t *expr, const enum asn1p_constraint_type_e *opt_constr_type) {
vlmfa67ddc2004-06-03 03:38:44 +00007 asn1p_expr_t *val_type_expr;
8 asn1p_expr_t *value_expr;
9 asn1p_expr_t *type_expr;
10 int ret;
11
12 /* Make sure this IS a value assignment */
13 assert(expr->meta_type == AMT_VALUE);
14 assert(expr->value);
15
vlm71ad4962004-09-15 11:47:02 +000016 if(expr->value->type != ATV_REFERENCED)
17 return 0;
18
vlm8fba2572005-03-10 11:27:13 +000019 DEBUG("(=\"%s\", %x%s%s)",
20 asn1f_printable_value(expr->value), expr->expr_type,
21 opt_constr_type ? ", " : "",
22 opt_constr_type
23 ? asn1p_constraint_type2str(*opt_constr_type) : ""
24 );
vlmfa67ddc2004-06-03 03:38:44 +000025
26 /*
27 * 1. Find the terminal type for this assignment.
28 */
vlm2e0c1942004-08-22 03:10:23 +000029 type_expr = asn1f_find_terminal_type(arg, expr);
vlmfa67ddc2004-06-03 03:38:44 +000030 if(type_expr == 0) {
vlm14a1e892005-06-04 01:08:32 +000031 if(errno == EEXIST) {
32 DEBUG("External type for %s at line %d",
33 expr->Identifier, expr->_lineno);
34 return 0;
35 } else {
36 FATAL("Terminal type for %s at line %d not found",
37 expr->Identifier, expr->_lineno);
38 return -1;
39 }
vlmfa67ddc2004-06-03 03:38:44 +000040 }
41
vlm71ad4962004-09-15 11:47:02 +000042 if(asn1f_look_value_in_type(arg, type_expr, expr) == -1) {
vlm20e60cc2005-03-24 16:35:24 +000043 FATAL("Value not found in type for %s at line %d",
44 expr->Identifier, expr->_lineno);
vlmfa67ddc2004-06-03 03:38:44 +000045 return -1;
vlm71ad4962004-09-15 11:47:02 +000046 }
vlmfa67ddc2004-06-03 03:38:44 +000047
48 /*
49 * 2. Find the terminal value also.
50 */
vlm2e0c1942004-08-22 03:10:23 +000051 value_expr = asn1f_find_terminal_value(arg, expr);
vlmfa67ddc2004-06-03 03:38:44 +000052 if(value_expr) {
vlm8fba2572005-03-10 11:27:13 +000053 DEBUG("Terminal value for %s->%s is %s at line %d",
vlmfa67ddc2004-06-03 03:38:44 +000054 expr->Identifier, asn1f_printable_value(expr->value),
55 value_expr->Identifier, value_expr->_lineno);
56 } else {
vlm71ad4962004-09-15 11:47:02 +000057 FATAL("Terminal value for %s->%s not found",
vlmfa67ddc2004-06-03 03:38:44 +000058 expr->Identifier, asn1f_printable_value(expr->value));
59 return -1;
60 }
61
62 /*
63 * 3. Find the _type_ of a _terminal value_.
64 */
vlm2e0c1942004-08-22 03:10:23 +000065 WITH_MODULE(value_expr->module,
66 val_type_expr = asn1f_find_terminal_type(arg, value_expr));
vlmfa67ddc2004-06-03 03:38:44 +000067 if(val_type_expr) {
vlm8fba2572005-03-10 11:27:13 +000068 DEBUG("Terminal type of value %s->%s is %s at line %d",
vlmfa67ddc2004-06-03 03:38:44 +000069 expr->Identifier, asn1f_printable_value(expr->value),
70 val_type_expr->Identifier, val_type_expr->_lineno);
71 } else {
vlm8fba2572005-03-10 11:27:13 +000072 FATAL("Terminal type of value %s->%s not found",
vlmfa67ddc2004-06-03 03:38:44 +000073 expr->Identifier, asn1f_printable_value(expr->value));
74 return -1;
75 }
76
77 /*
78 * 4. Check compatibility between the type of the current expression
79 * and the type of the discovered value.
80 */
vlm8fba2572005-03-10 11:27:13 +000081 if(opt_constr_type)
82 ret = asn1constraint_compatible(val_type_expr->expr_type,
vlm27028582005-08-14 14:45:44 +000083 *opt_constr_type, 0 /* must not matter here */);
vlm8fba2572005-03-10 11:27:13 +000084 else
85 ret = asn1f_check_type_compatibility(arg,
86 type_expr, val_type_expr);
vlmfa67ddc2004-06-03 03:38:44 +000087 if(ret == -1) {
vlm59f89da2004-09-06 08:06:37 +000088 switch(type_expr->expr_type) {
vlmc8bb2b12005-03-20 11:07:34 +000089 default:
90 if(!(type_expr->expr_type & ASN_STRING_MASK))
91 break;
92 /* Compatibility rules are not defined */
93 /* Fall through */
vlm59f89da2004-09-06 08:06:37 +000094 case ASN_BASIC_INTEGER:
95 case ASN_BASIC_ENUMERATED:
vlm8fba2572005-03-10 11:27:13 +000096 FATAL("Incompatible type of \"%s\" (%s) at line %d "
97 "with \"%s\" (%s) at line %d",
98 type_expr->Identifier,
99 ASN_EXPR_TYPE2STR(type_expr->expr_type),
100 type_expr->_lineno,
101 val_type_expr->Identifier,
102 ASN_EXPR_TYPE2STR(val_type_expr->expr_type),
103 val_type_expr->_lineno);
vlm59f89da2004-09-06 08:06:37 +0000104 return -1;
105 case ASN_BASIC_OBJECT_IDENTIFIER:
106 /*
107 * Ignore this for now.
108 * We can't deal with OIDs inheritance properly yet.
109 */
110 return 0;
vlm59f89da2004-09-06 08:06:37 +0000111 }
vlmc8bb2b12005-03-20 11:07:34 +0000112 WARNING("Possibly incompatible type of \"%s\" (%s) at line %d "
vlm8fba2572005-03-10 11:27:13 +0000113 "with \"%s\" (%s) at line %d",
114 type_expr->Identifier,
115 ASN_EXPR_TYPE2STR(type_expr->expr_type),
116 type_expr->_lineno,
117 val_type_expr->Identifier,
118 ASN_EXPR_TYPE2STR(val_type_expr->expr_type),
119 val_type_expr->_lineno);
vlm59f89da2004-09-06 08:06:37 +0000120 return 1;
vlmfa67ddc2004-06-03 03:38:44 +0000121 }
122
123 if(asn1f_look_value_in_type(arg, val_type_expr, expr) == -1)
124 return -1;
125
126 /*
127 * 5. Copy value from the terminal value into the current expression.
128 */
129 ret = _asn1f_copy_value(arg, expr, value_expr);
130 if(ret == -1) {
vlm71ad4962004-09-15 11:47:02 +0000131 FATAL("Value %s cannot be copied from line %d to line %d",
vlmfa67ddc2004-06-03 03:38:44 +0000132 asn1f_printable_value(value_expr->value),
133 value_expr->_lineno, expr->_lineno);
134 return -1;
135 }
136
vlm8fba2572005-03-10 11:27:13 +0000137 DEBUG("Final value for \"%s\" at line %d is %s",
vlmfa67ddc2004-06-03 03:38:44 +0000138 expr->Identifier, expr->_lineno,
139 asn1f_printable_value(expr->value));
140
141 return 0;
142}
143
144static int
145_asn1f_copy_value(arg_t *arg, asn1p_expr_t *to, asn1p_expr_t *from) {
146 asn1p_value_t *v;
147
148 v = asn1p_value_clone(from->value);
149 if(v) {
150 asn1p_value_free(to->value);
151 to->value = v;
152 DEBUG("Copied value %s from \"%s\" on line %d "
153 "to \"%s\" on line %d",
154 asn1f_printable_value(v),
155 from->Identifier,
156 from->_lineno,
157 to->Identifier,
158 to->_lineno
159 );
160 return 0;
161 } else {
162 return -1;
163 }
164}
165
166int
167asn1f_look_value_in_type(arg_t *arg,
168 asn1p_expr_t *type_expr,
169 asn1p_expr_t *value_expr) {
170 asn1p_expr_t *child_expr;
171 char *identifier;
172
173 if(value_expr->value->type != ATV_REFERENCED
174 || value_expr->value->value.reference->comp_count != 1)
175 return 0;
176 if(type_expr->expr_type != ASN_BASIC_INTEGER
177 && type_expr->expr_type != ASN_BASIC_ENUMERATED)
178 return 0;
179
vlmfd245932005-03-10 10:02:50 +0000180 DEBUG("(for %s in %s %x) for line %d",
vlmfa67ddc2004-06-03 03:38:44 +0000181 asn1f_printable_value(value_expr->value),
182 type_expr->Identifier,
183 type_expr->expr_type,
184 value_expr->_lineno);
185
186 /*
187 * Look into the definitions of the type itself:
188 * Type1 ::= INTEGER { a(1), b(2) }
189 * value Type1 = b -- will assign 2
190 */
191 identifier = value_expr->value->value.reference->components[0].name;
192
193 child_expr = asn1f_lookup_child(type_expr, identifier);
vlm8fba2572005-03-10 11:27:13 +0000194 DEBUG("Looking into a type %s at line %d for %s at line %d: %s",
vlmfa67ddc2004-06-03 03:38:44 +0000195 type_expr->Identifier, type_expr->_lineno,
196 identifier, value_expr->_lineno,
197 child_expr
198 ? asn1f_printable_value(child_expr->value)
199 : "<not found>"
200 );
201
202 if(child_expr && child_expr->value) {
203 if(_asn1f_copy_value(arg, value_expr, child_expr))
204 return -1;
205 /* Fall through */
206 }
207
208 return 0;
209}