Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
| 2 | |
| 3 | static int _asn1f_copy_value(arg_t *arg, asn1p_expr_t *to,asn1p_expr_t *from); |
| 4 | |
| 5 | int |
Lev Walkin | fb61770 | 2005-03-10 16:15:57 +0000 | [diff] [blame] | 6 | asn1f_value_resolve(arg_t *arg, asn1p_expr_t *expr, const enum asn1p_constraint_type_e *opt_constr_type) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | 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 | |
Lev Walkin | a13e73f | 2004-09-15 11:47:02 +0000 | [diff] [blame] | 16 | if(expr->value->type != ATV_REFERENCED) |
| 17 | return 0; |
| 18 | |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 19 | 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 | ); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * 1. Find the terminal type for this assignment. |
| 28 | */ |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 29 | type_expr = asn1f_find_terminal_type(arg, expr); |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 30 | DEBUG("terminal type %p", type_expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 31 | if(type_expr == 0) { |
Lev Walkin | cae1cfd | 2004-09-06 08:06:37 +0000 | [diff] [blame] | 32 | FATAL("Terminal type for is %s not found", expr->Identifier); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 33 | return -1; |
| 34 | } |
| 35 | |
Lev Walkin | a13e73f | 2004-09-15 11:47:02 +0000 | [diff] [blame] | 36 | if(asn1f_look_value_in_type(arg, type_expr, expr) == -1) { |
| 37 | FATAL("Value not found in type for %s", expr->Identifier); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | return -1; |
Lev Walkin | a13e73f | 2004-09-15 11:47:02 +0000 | [diff] [blame] | 39 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * 2. Find the terminal value also. |
| 43 | */ |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 44 | value_expr = asn1f_find_terminal_value(arg, expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | if(value_expr) { |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 46 | DEBUG("Terminal value for %s->%s is %s at line %d", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | expr->Identifier, asn1f_printable_value(expr->value), |
| 48 | value_expr->Identifier, value_expr->_lineno); |
| 49 | } else { |
Lev Walkin | a13e73f | 2004-09-15 11:47:02 +0000 | [diff] [blame] | 50 | FATAL("Terminal value for %s->%s not found", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | expr->Identifier, asn1f_printable_value(expr->value)); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * 3. Find the _type_ of a _terminal value_. |
| 57 | */ |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 58 | WITH_MODULE(value_expr->module, |
| 59 | val_type_expr = asn1f_find_terminal_type(arg, value_expr)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | if(val_type_expr) { |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 61 | DEBUG("Terminal type of value %s->%s is %s at line %d", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | expr->Identifier, asn1f_printable_value(expr->value), |
| 63 | val_type_expr->Identifier, val_type_expr->_lineno); |
| 64 | } else { |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 65 | FATAL("Terminal type of value %s->%s not found", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 66 | 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 | */ |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 74 | 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); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | if(ret == -1) { |
Lev Walkin | cae1cfd | 2004-09-06 08:06:37 +0000 | [diff] [blame] | 81 | switch(type_expr->expr_type) { |
| 82 | case ASN_BASIC_INTEGER: |
| 83 | case ASN_BASIC_ENUMERATED: |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 84 | 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); |
Lev Walkin | cae1cfd | 2004-09-06 08:06:37 +0000 | [diff] [blame] | 92 | 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 | } |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 102 | 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); |
Lev Walkin | cae1cfd | 2004-09-06 08:06:37 +0000 | [diff] [blame] | 110 | return 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | } |
| 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) { |
Lev Walkin | a13e73f | 2004-09-15 11:47:02 +0000 | [diff] [blame] | 121 | FATAL("Value %s cannot be copied from line %d to line %d", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 122 | asn1f_printable_value(value_expr->value), |
| 123 | value_expr->_lineno, expr->_lineno); |
| 124 | return -1; |
| 125 | } |
| 126 | |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 127 | DEBUG("Final value for \"%s\" at line %d is %s", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 128 | expr->Identifier, expr->_lineno, |
| 129 | asn1f_printable_value(expr->value)); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static 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 | |
| 156 | int |
| 157 | asn1f_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 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 170 | DEBUG("(for %s in %s %x) for line %d", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 171 | 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); |
Lev Walkin | 9288d1c | 2005-03-10 11:27:13 +0000 | [diff] [blame] | 184 | DEBUG("Looking into a type %s at line %d for %s at line %d: %s", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 185 | 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 | } |