Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
| 2 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 3 | typedef struct resolver_arg { |
| 4 | asn1p_expr_t *(*resolver)(asn1p_expr_t *, void *arg); |
| 5 | arg_t *arg; |
| 6 | asn1p_expr_t *original_expr; |
| 7 | asn1p_paramlist_t *lhs_params; |
| 8 | asn1p_expr_t *rhs_pspecs; |
| 9 | } resolver_arg_t; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 10 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 11 | static asn1p_expr_t *resolve_expr(asn1p_expr_t *, void *resolver_arg); |
Lev Walkin | f75b874 | 2017-08-23 04:44:18 -0700 | [diff] [blame] | 12 | static int compare_specializations(const asn1p_expr_t *a, const asn1p_expr_t *b); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 13 | static asn1p_expr_t *find_target_specialization_byref(resolver_arg_t *rarg, asn1p_ref_t *ref); |
| 14 | static asn1p_expr_t *find_target_specialization_bystr(resolver_arg_t *rarg, char *str); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 16 | asn1p_expr_t * |
| 17 | asn1f_parameterization_fork(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *rhs_pspecs) { |
| 18 | resolver_arg_t rarg; /* resolver argument */ |
| 19 | asn1p_expr_t *exc; /* expr clone */ |
| 20 | asn1p_expr_t *rpc; /* rhs_pspecs clone */ |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 21 | asn1p_expr_t *m; /* expr members */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | void *p; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 23 | struct asn1p_pspec_s *pspec; |
| 24 | int npspecs; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 26 | assert(rhs_pspecs); |
| 27 | assert(expr->lhs_params); |
| 28 | assert(expr->parent_expr == 0); |
| 29 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 30 | /* |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 31 | * Find if this exact specialization has been used already. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 33 | for(npspecs = 0; |
| 34 | npspecs < expr->specializations.pspecs_count; |
| 35 | npspecs++) { |
Lev Walkin | f75b874 | 2017-08-23 04:44:18 -0700 | [diff] [blame] | 36 | if(compare_specializations(rhs_pspecs, |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 37 | expr->specializations.pspec[npspecs].rhs_pspecs) == 0) { |
| 38 | DEBUG("Reused parameterization for %s", |
| 39 | expr->Identifier); |
| 40 | return expr->specializations.pspec[npspecs].my_clone; |
Lev Walkin | d594771 | 2005-11-05 12:28:14 +0000 | [diff] [blame] | 41 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | |
Lev Walkin | d8e07c5 | 2017-08-23 07:38:30 -0700 | [diff] [blame] | 44 | DEBUG("Forking parameterization at %d for %s (%d alr)", |
| 45 | rhs_pspecs->_lineno, expr->Identifier, |
| 46 | expr->specializations.pspecs_count); |
| 47 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 48 | rarg.resolver = resolve_expr; |
| 49 | rarg.arg = arg; |
| 50 | rarg.original_expr = expr; |
| 51 | rarg.lhs_params = expr->lhs_params; |
| 52 | rarg.rhs_pspecs = rhs_pspecs; |
| 53 | exc = asn1p_expr_clone_with_resolver(expr, resolve_expr, &rarg); |
Lev Walkin | c5a5d22 | 2007-06-23 17:35:56 +0000 | [diff] [blame] | 54 | if(!exc) return NULL; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 55 | rpc = asn1p_expr_clone(rhs_pspecs, 0); |
Lev Walkin | c5a5d22 | 2007-06-23 17:35:56 +0000 | [diff] [blame] | 56 | assert(rpc); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Create a new specialization. |
| 60 | */ |
| 61 | npspecs = expr->specializations.pspecs_count; |
| 62 | p = realloc(expr->specializations.pspec, |
| 63 | (npspecs + 1) * sizeof(expr->specializations.pspec[0])); |
| 64 | assert(p); |
| 65 | expr->specializations.pspec = p; |
| 66 | pspec = &expr->specializations.pspec[npspecs]; |
| 67 | memset(pspec, 0, sizeof *pspec); |
| 68 | |
| 69 | pspec->rhs_pspecs = rpc; |
| 70 | pspec->my_clone = exc; |
| 71 | exc->spec_index = npspecs; |
| 72 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 73 | /* Passing arguments to members and type references */ |
| 74 | exc->rhs_pspecs = expr->rhs_pspecs ? expr->rhs_pspecs : rhs_pspecs; |
| 75 | if(exc->rhs_pspecs) |
| 76 | exc->rhs_pspecs->ref_cnt++; |
| 77 | |
| 78 | TQ_FOR(m, &exc->members, next) { |
| 79 | m->rhs_pspecs = exc->rhs_pspecs; |
| 80 | if (exc->rhs_pspecs) |
| 81 | exc->rhs_pspecs->ref_cnt++; |
| 82 | } |
| 83 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 84 | DEBUG("Forked new parameterization for %s", expr->Identifier); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 85 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 86 | /* Commit */ |
| 87 | expr->specializations.pspecs_count = npspecs + 1; |
| 88 | return exc; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int |
Lev Walkin | f75b874 | 2017-08-23 04:44:18 -0700 | [diff] [blame] | 92 | compare_specializations(const asn1p_expr_t *a, const asn1p_expr_t *b) { |
| 93 | return asn1p_expr_compare(a, b); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static asn1p_expr_t * |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 97 | resolve_expr(asn1p_expr_t *expr_to_resolve, void *resolver_arg) { |
| 98 | resolver_arg_t *rarg = resolver_arg; |
| 99 | arg_t *arg = rarg->arg; |
| 100 | asn1p_expr_t *expr; |
| 101 | asn1p_expr_t *nex; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 103 | DEBUG("Resolving %s (meta %d)", |
| 104 | expr_to_resolve->Identifier, expr_to_resolve->meta_type); |
| 105 | |
| 106 | if(expr_to_resolve->meta_type == AMT_TYPEREF) { |
| 107 | expr = find_target_specialization_byref(rarg, |
| 108 | expr_to_resolve->reference); |
| 109 | if(!expr) return NULL; |
| 110 | } else if(expr_to_resolve->meta_type == AMT_VALUE) { |
| 111 | assert(expr_to_resolve->value); |
| 112 | expr = find_target_specialization_bystr(rarg, |
| 113 | expr_to_resolve->Identifier); |
| 114 | if(!expr) return NULL; |
| 115 | } else { |
| 116 | errno = ESRCH; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | return NULL; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 118 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | |
Lev Walkin | 5045dfa | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 120 | DEBUG("Found target %s (%d/%x)", |
| 121 | expr->Identifier, expr->meta_type, expr->expr_type); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 122 | if(expr->meta_type == AMT_TYPE |
Lev Walkin | 5045dfa | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 123 | || expr->meta_type == AMT_VALUE |
Lev Walkin | c5a5d22 | 2007-06-23 17:35:56 +0000 | [diff] [blame] | 124 | || expr->meta_type == AMT_TYPEREF |
Lev Walkin | 5045dfa | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 125 | || expr->meta_type == AMT_VALUESET) { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 126 | DEBUG("Target is a simple type %s", |
| 127 | ASN_EXPR_TYPE2STR(expr->expr_type)); |
| 128 | nex = asn1p_expr_clone(expr, 0); |
| 129 | free(nex->Identifier); |
| 130 | nex->Identifier = expr_to_resolve->Identifier |
| 131 | ? strdup(expr_to_resolve->Identifier) : 0; |
| 132 | return nex; |
| 133 | } else { |
Lev Walkin | 5045dfa | 2006-03-21 09:41:28 +0000 | [diff] [blame] | 134 | FATAL("Feature not implemented for %s (%d/%x), " |
| 135 | "please contact the asn1c author", |
| 136 | rarg->original_expr->Identifier, |
| 137 | expr->meta_type, expr->expr_type); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 138 | errno = EPERM; |
| 139 | return NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return NULL; |
| 143 | } |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 144 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 145 | static asn1p_expr_t * |
| 146 | find_target_specialization_byref(resolver_arg_t *rarg, asn1p_ref_t *ref) { |
| 147 | char *refstr; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 148 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 149 | if(!ref || ref->comp_count != 1) { |
| 150 | errno = ESRCH; |
| 151 | return NULL; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 154 | refstr = ref->components[0].name; /* T */ |
| 155 | |
| 156 | return find_target_specialization_bystr(rarg, refstr); |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 159 | static asn1p_expr_t * |
| 160 | find_target_specialization_bystr(resolver_arg_t *rarg, char *refstr) { |
| 161 | arg_t *arg = rarg->arg; |
| 162 | asn1p_expr_t *target; |
| 163 | int i; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 164 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 165 | target = TQ_FIRST(&rarg->rhs_pspecs->members); |
| 166 | for(i = 0; i < rarg->lhs_params->params_count; |
| 167 | i++, target = TQ_NEXT(target, next)) { |
| 168 | struct asn1p_param_s *param = &rarg->lhs_params->params[i]; |
| 169 | if(!target) break; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 170 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 171 | if(strcmp(param->argument, refstr)) |
| 172 | continue; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 173 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 174 | return target; |
| 175 | } |
| 176 | if(i != rarg->lhs_params->params_count) { |
| 177 | FATAL("Parameterization of %s failed: " |
| 178 | "parameters number mismatch", |
| 179 | rarg->original_expr->Identifier); |
| 180 | errno = EPERM; |
| 181 | return NULL; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 184 | errno = ESRCH; |
| 185 | return NULL; |
Lev Walkin | d541c25 | 2004-09-05 10:36:22 +0000 | [diff] [blame] | 186 | } |