blob: 161d9a38d8c98985c2815117e1fe31bd91819622 [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);
vlmfd245932005-03-10 10:02:50 +000030 DEBUG("terminal type %p", type_expr);
vlmfa67ddc2004-06-03 03:38:44 +000031 if(type_expr == 0) {
vlm59f89da2004-09-06 08:06:37 +000032 FATAL("Terminal type for is %s not found", expr->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +000033 return -1;
34 }
35
vlm71ad4962004-09-15 11:47:02 +000036 if(asn1f_look_value_in_type(arg, type_expr, expr) == -1) {
37 FATAL("Value not found in type for %s", expr->Identifier);
vlmfa67ddc2004-06-03 03:38:44 +000038 return -1;
vlm71ad4962004-09-15 11:47:02 +000039 }
vlmfa67ddc2004-06-03 03:38:44 +000040
41 /*
42 * 2. Find the terminal value also.
43 */
vlm2e0c1942004-08-22 03:10:23 +000044 value_expr = asn1f_find_terminal_value(arg, expr);
vlmfa67ddc2004-06-03 03:38:44 +000045 if(value_expr) {
vlm8fba2572005-03-10 11:27:13 +000046 DEBUG("Terminal value for %s->%s is %s at line %d",
vlmfa67ddc2004-06-03 03:38:44 +000047 expr->Identifier, asn1f_printable_value(expr->value),
48 value_expr->Identifier, value_expr->_lineno);
49 } else {
vlm71ad4962004-09-15 11:47:02 +000050 FATAL("Terminal value for %s->%s not found",
vlmfa67ddc2004-06-03 03:38:44 +000051 expr->Identifier, asn1f_printable_value(expr->value));
52 return -1;
53 }
54
55 /*
56 * 3. Find the _type_ of a _terminal value_.
57 */
vlm2e0c1942004-08-22 03:10:23 +000058 WITH_MODULE(value_expr->module,
59 val_type_expr = asn1f_find_terminal_type(arg, value_expr));
vlmfa67ddc2004-06-03 03:38:44 +000060 if(val_type_expr) {
vlm8fba2572005-03-10 11:27:13 +000061 DEBUG("Terminal type of value %s->%s is %s at line %d",
vlmfa67ddc2004-06-03 03:38:44 +000062 expr->Identifier, asn1f_printable_value(expr->value),
63 val_type_expr->Identifier, val_type_expr->_lineno);
64 } else {
vlm8fba2572005-03-10 11:27:13 +000065 FATAL("Terminal type of value %s->%s not found",
vlmfa67ddc2004-06-03 03:38:44 +000066 expr->Identifier, asn1f_printable_value(expr->value));
67 return -1;
68 }
69
70 /*
71 * 4. Check compatibility between the type of the current expression
72 * and the type of the discovered value.
73 */
vlm8fba2572005-03-10 11:27:13 +000074 if(opt_constr_type)
75 ret = asn1constraint_compatible(val_type_expr->expr_type,
76 *opt_constr_type);
77 else
78 ret = asn1f_check_type_compatibility(arg,
79 type_expr, val_type_expr);
vlmfa67ddc2004-06-03 03:38:44 +000080 if(ret == -1) {
vlm59f89da2004-09-06 08:06:37 +000081 switch(type_expr->expr_type) {
82 case ASN_BASIC_INTEGER:
83 case ASN_BASIC_ENUMERATED:
vlm8fba2572005-03-10 11:27:13 +000084 FATAL("Incompatible type of \"%s\" (%s) at line %d "
85 "with \"%s\" (%s) at line %d",
86 type_expr->Identifier,
87 ASN_EXPR_TYPE2STR(type_expr->expr_type),
88 type_expr->_lineno,
89 val_type_expr->Identifier,
90 ASN_EXPR_TYPE2STR(val_type_expr->expr_type),
91 val_type_expr->_lineno);
vlm59f89da2004-09-06 08:06:37 +000092 return -1;
93 case ASN_BASIC_OBJECT_IDENTIFIER:
94 /*
95 * Ignore this for now.
96 * We can't deal with OIDs inheritance properly yet.
97 */
98 return 0;
99 default:
100 break;
101 }
vlm8fba2572005-03-10 11:27:13 +0000102 WARNING("Incompatible type of \"%s\" (%s) at line %d "
103 "with \"%s\" (%s) at line %d",
104 type_expr->Identifier,
105 ASN_EXPR_TYPE2STR(type_expr->expr_type),
106 type_expr->_lineno,
107 val_type_expr->Identifier,
108 ASN_EXPR_TYPE2STR(val_type_expr->expr_type),
109 val_type_expr->_lineno);
vlm59f89da2004-09-06 08:06:37 +0000110 return 1;
vlmfa67ddc2004-06-03 03:38:44 +0000111 }
112
113 if(asn1f_look_value_in_type(arg, val_type_expr, expr) == -1)
114 return -1;
115
116 /*
117 * 5. Copy value from the terminal value into the current expression.
118 */
119 ret = _asn1f_copy_value(arg, expr, value_expr);
120 if(ret == -1) {
vlm71ad4962004-09-15 11:47:02 +0000121 FATAL("Value %s cannot be copied from line %d to line %d",
vlmfa67ddc2004-06-03 03:38:44 +0000122 asn1f_printable_value(value_expr->value),
123 value_expr->_lineno, expr->_lineno);
124 return -1;
125 }
126
vlm8fba2572005-03-10 11:27:13 +0000127 DEBUG("Final value for \"%s\" at line %d is %s",
vlmfa67ddc2004-06-03 03:38:44 +0000128 expr->Identifier, expr->_lineno,
129 asn1f_printable_value(expr->value));
130
131 return 0;
132}
133
134static int
135_asn1f_copy_value(arg_t *arg, asn1p_expr_t *to, asn1p_expr_t *from) {
136 asn1p_value_t *v;
137
138 v = asn1p_value_clone(from->value);
139 if(v) {
140 asn1p_value_free(to->value);
141 to->value = v;
142 DEBUG("Copied value %s from \"%s\" on line %d "
143 "to \"%s\" on line %d",
144 asn1f_printable_value(v),
145 from->Identifier,
146 from->_lineno,
147 to->Identifier,
148 to->_lineno
149 );
150 return 0;
151 } else {
152 return -1;
153 }
154}
155
156int
157asn1f_look_value_in_type(arg_t *arg,
158 asn1p_expr_t *type_expr,
159 asn1p_expr_t *value_expr) {
160 asn1p_expr_t *child_expr;
161 char *identifier;
162
163 if(value_expr->value->type != ATV_REFERENCED
164 || value_expr->value->value.reference->comp_count != 1)
165 return 0;
166 if(type_expr->expr_type != ASN_BASIC_INTEGER
167 && type_expr->expr_type != ASN_BASIC_ENUMERATED)
168 return 0;
169
vlmfd245932005-03-10 10:02:50 +0000170 DEBUG("(for %s in %s %x) for line %d",
vlmfa67ddc2004-06-03 03:38:44 +0000171 asn1f_printable_value(value_expr->value),
172 type_expr->Identifier,
173 type_expr->expr_type,
174 value_expr->_lineno);
175
176 /*
177 * Look into the definitions of the type itself:
178 * Type1 ::= INTEGER { a(1), b(2) }
179 * value Type1 = b -- will assign 2
180 */
181 identifier = value_expr->value->value.reference->components[0].name;
182
183 child_expr = asn1f_lookup_child(type_expr, identifier);
vlm8fba2572005-03-10 11:27:13 +0000184 DEBUG("Looking into a type %s at line %d for %s at line %d: %s",
vlmfa67ddc2004-06-03 03:38:44 +0000185 type_expr->Identifier, type_expr->_lineno,
186 identifier, value_expr->_lineno,
187 child_expr
188 ? asn1f_printable_value(child_expr->value)
189 : "<not found>"
190 );
191
192 if(child_expr && child_expr->value) {
193 if(_asn1f_copy_value(arg, value_expr, child_expr))
194 return -1;
195 /* Fall through */
196 }
197
198 return 0;
199}