blob: 904da4342959fba1ebe95a93df470c3fc6ce69b9 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
Lev Walkina00d6b32006-03-21 03:40:38 +00003typedef 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 Walkind541c252004-09-05 10:36:22 +000010
Lev Walkina00d6b32006-03-21 03:40:38 +000011static asn1p_expr_t *resolve_expr(asn1p_expr_t *, void *resolver_arg);
Lev Walkinf75b8742017-08-23 04:44:18 -070012static int compare_specializations(const asn1p_expr_t *a, const asn1p_expr_t *b);
Lev Walkina00d6b32006-03-21 03:40:38 +000013static asn1p_expr_t *find_target_specialization_byref(resolver_arg_t *rarg, asn1p_ref_t *ref);
14static asn1p_expr_t *find_target_specialization_bystr(resolver_arg_t *rarg, char *str);
Lev Walkinf15320b2004-06-03 03:38:44 +000015
Lev Walkina00d6b32006-03-21 03:40:38 +000016asn1p_expr_t *
17asn1f_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, Chiu80fd3062017-05-07 21:00:51 +080021 asn1p_expr_t *m; /* expr members */
Lev Walkinf15320b2004-06-03 03:38:44 +000022 void *p;
Lev Walkina00d6b32006-03-21 03:40:38 +000023 struct asn1p_pspec_s *pspec;
24 int npspecs;
Lev Walkinf15320b2004-06-03 03:38:44 +000025
Lev Walkina00d6b32006-03-21 03:40:38 +000026 assert(rhs_pspecs);
27 assert(expr->lhs_params);
28 assert(expr->parent_expr == 0);
29
30 DEBUG("Forking parameterization at %d for %s (%d alr)",
31 rhs_pspecs->_lineno, expr->Identifier,
32 expr->specializations.pspecs_count);
Lev Walkinf593f102005-02-22 07:59:59 +000033
Lev Walkinf15320b2004-06-03 03:38:44 +000034 /*
Lev Walkina00d6b32006-03-21 03:40:38 +000035 * Find if this exact specialization has been used already.
Lev Walkinf15320b2004-06-03 03:38:44 +000036 */
Lev Walkina00d6b32006-03-21 03:40:38 +000037 for(npspecs = 0;
38 npspecs < expr->specializations.pspecs_count;
39 npspecs++) {
Lev Walkinf75b8742017-08-23 04:44:18 -070040 if(compare_specializations(rhs_pspecs,
Lev Walkina00d6b32006-03-21 03:40:38 +000041 expr->specializations.pspec[npspecs].rhs_pspecs) == 0) {
42 DEBUG("Reused parameterization for %s",
43 expr->Identifier);
44 return expr->specializations.pspec[npspecs].my_clone;
Lev Walkind5947712005-11-05 12:28:14 +000045 }
Lev Walkinf15320b2004-06-03 03:38:44 +000046 }
Lev Walkinf15320b2004-06-03 03:38:44 +000047
Lev Walkina00d6b32006-03-21 03:40:38 +000048 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 Walkinc5a5d222007-06-23 17:35:56 +000054 if(!exc) return NULL;
Lev Walkina00d6b32006-03-21 03:40:38 +000055 rpc = asn1p_expr_clone(rhs_pspecs, 0);
Lev Walkinc5a5d222007-06-23 17:35:56 +000056 assert(rpc);
Lev Walkina00d6b32006-03-21 03:40:38 +000057
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, Chiu80fd3062017-05-07 21:00:51 +080073 /* 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 Walkina00d6b32006-03-21 03:40:38 +000084 DEBUG("Forked new parameterization for %s", expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000085
Lev Walkina00d6b32006-03-21 03:40:38 +000086 /* Commit */
87 expr->specializations.pspecs_count = npspecs + 1;
88 return exc;
Lev Walkinf15320b2004-06-03 03:38:44 +000089}
90
91static int
Lev Walkinf75b8742017-08-23 04:44:18 -070092compare_specializations(const asn1p_expr_t *a, const asn1p_expr_t *b) {
93 return asn1p_expr_compare(a, b);
Lev Walkinf15320b2004-06-03 03:38:44 +000094}
95
96static asn1p_expr_t *
Lev Walkina00d6b32006-03-21 03:40:38 +000097resolve_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 Walkinf15320b2004-06-03 03:38:44 +0000102
Lev Walkina00d6b32006-03-21 03:40:38 +0000103 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 Walkinf15320b2004-06-03 03:38:44 +0000117 return NULL;
Lev Walkina00d6b32006-03-21 03:40:38 +0000118 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000119
Lev Walkin5045dfa2006-03-21 09:41:28 +0000120 DEBUG("Found target %s (%d/%x)",
121 expr->Identifier, expr->meta_type, expr->expr_type);
Lev Walkina00d6b32006-03-21 03:40:38 +0000122 if(expr->meta_type == AMT_TYPE
Lev Walkin5045dfa2006-03-21 09:41:28 +0000123 || expr->meta_type == AMT_VALUE
Lev Walkinc5a5d222007-06-23 17:35:56 +0000124 || expr->meta_type == AMT_TYPEREF
Lev Walkin5045dfa2006-03-21 09:41:28 +0000125 || expr->meta_type == AMT_VALUESET) {
Lev Walkina00d6b32006-03-21 03:40:38 +0000126 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 Walkin5045dfa2006-03-21 09:41:28 +0000134 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 Walkina00d6b32006-03-21 03:40:38 +0000138 errno = EPERM;
139 return NULL;
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 }
141
142 return NULL;
143}
Lev Walkind541c252004-09-05 10:36:22 +0000144
Lev Walkina00d6b32006-03-21 03:40:38 +0000145static asn1p_expr_t *
146find_target_specialization_byref(resolver_arg_t *rarg, asn1p_ref_t *ref) {
147 char *refstr;
Lev Walkind541c252004-09-05 10:36:22 +0000148
Lev Walkina00d6b32006-03-21 03:40:38 +0000149 if(!ref || ref->comp_count != 1) {
150 errno = ESRCH;
151 return NULL;
Lev Walkind541c252004-09-05 10:36:22 +0000152 }
153
Lev Walkina00d6b32006-03-21 03:40:38 +0000154 refstr = ref->components[0].name; /* T */
155
156 return find_target_specialization_bystr(rarg, refstr);
Lev Walkind541c252004-09-05 10:36:22 +0000157}
158
Lev Walkina00d6b32006-03-21 03:40:38 +0000159static asn1p_expr_t *
160find_target_specialization_bystr(resolver_arg_t *rarg, char *refstr) {
161 arg_t *arg = rarg->arg;
162 asn1p_expr_t *target;
163 int i;
Lev Walkind541c252004-09-05 10:36:22 +0000164
Lev Walkina00d6b32006-03-21 03:40:38 +0000165 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 Walkind541c252004-09-05 10:36:22 +0000170
Lev Walkina00d6b32006-03-21 03:40:38 +0000171 if(strcmp(param->argument, refstr))
172 continue;
Lev Walkind541c252004-09-05 10:36:22 +0000173
Lev Walkina00d6b32006-03-21 03:40:38 +0000174 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 Walkind541c252004-09-05 10:36:22 +0000182 }
183
Lev Walkina00d6b32006-03-21 03:40:38 +0000184 errno = ESRCH;
185 return NULL;
Lev Walkind541c252004-09-05 10:36:22 +0000186}