blob: 4236858226b30649c1a2156811824133acf25c80 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
Lev Walkin33f63f82005-03-20 11:27:19 +00003enum ftt_what {
4 FTT_TYPE, /* Find the type of the given expression */
5 FTT_VALUE, /* Find the value of the given expression */
6};
7
8static asn1p_expr_t *asn1f_find_terminal_thing(arg_t *arg, asn1p_expr_t *expr, enum ftt_what);
Lev Walkinf15320b2004-06-03 03:38:44 +00009static int asn1f_compatible_with_exports(arg_t *arg, asn1p_module_t *mod, const char *name);
10
11
12/*
13 * Lookup a child by its name.
14 */
15asn1p_expr_t *
16asn1f_lookup_child(asn1p_expr_t *tc, const char *name) {
17 asn1p_expr_t *child_tc;
18
19 TQ_FOR(child_tc, &(tc->members), next) {
20 if(child_tc->Identifier
21 && strcmp(child_tc->Identifier, name) == 0) {
22 return child_tc;
23 }
24 }
25
26 errno = ESRCH;
27 return NULL;
28}
29
30asn1p_module_t *
Lev Walkin163db362004-09-15 11:46:47 +000031asn1f_lookup_in_imports(arg_t *arg, asn1p_module_t *mod, const char *name) {
Lev Walkinf15320b2004-06-03 03:38:44 +000032 asn1p_xports_t *xp;
33 asn1p_expr_t *tc;
34
35 /*
36 * Search in which exactly module this name is defined.
37 */
Lev Walkin163db362004-09-15 11:46:47 +000038 TQ_FOR(xp, &(mod->imports), xp_next) {
Lev Walkinf15320b2004-06-03 03:38:44 +000039 TQ_FOR(tc, &(xp->members), next) {
40 if(strcmp(name, tc->Identifier) == 0)
41 break;
42 }
43 if(tc) break;
44 }
45 if(xp == NULL) {
46 errno = ESRCH;
47 return NULL;
48 }
49
50 /*
51 * Okay, right now we have a module name and, hopefully, an OID.
52 * Search the arg->asn for the specified module.
53 */
Lev Walkinb36317c2005-08-12 10:09:10 +000054 mod = asn1f_lookup_module(arg, xp->fromModuleName, xp->identifier.oid);
Lev Walkinf15320b2004-06-03 03:38:44 +000055 if(mod == NULL) {
Lev Walkind8095522005-03-18 05:20:39 +000056 /* Conditional debug */
57 if(!(arg->expr->_mark & TM_BROKEN)) {
58 arg->expr->_mark |= TM_BROKEN;
Lev Walkin9e366ba2005-04-12 17:46:30 +000059 FATAL("Cannot find external module \"%s\" "
60 "mentioned for "
61 "\"%s\" at line %d",
Lev Walkinb36317c2005-08-12 10:09:10 +000062 xp->fromModuleName, name, arg->expr->_lineno);
Lev Walkind8095522005-03-18 05:20:39 +000063 }
Lev Walkinf15320b2004-06-03 03:38:44 +000064 /* ENOENT/ETOOMANYREFS */
65 return NULL;
66 }
67
68 /*
69 * Check that the EXPORTS section of this module contains
70 * the symbol we care about, or it is EXPORTS ALL.
71 */
72 if(asn1f_compatible_with_exports(arg, mod, name)) {
73 errno = EPERM;
74 return NULL;
75 }
76
77 return mod;
78}
79
80asn1p_module_t *
81asn1f_lookup_module(arg_t *arg, const char *module_name, asn1p_oid_t *oid) {
82 asn1p_module_t *mod;
83
84 assert(module_name);
85
86 /*
87 * If OID is given, the module_name is unused.
88 * If OID is not given, the module_name may mean
89 * either the real module's name, or the symbol which is the
90 * result of renaming. Check this first.
91 */
92 if(oid == 0) {
93 asn1p_xports_t *xp;
94 /*
95 * Check inside the IMPORTS section for possible renaming.
96 * Renaming practically means that a module_name is mentioned
97 * somewhere in the IMPORTS section AND OID is given.
98 */
99 TQ_FOR(xp, &(arg->mod->imports), xp_next) {
Lev Walkinb36317c2005-08-12 10:09:10 +0000100 if(strcmp(module_name, xp->fromModuleName))
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 continue;
102 if(oid) {
103 FATAL("Ambiguous reference: "
104 "%s "
105 "matches several modules",
106 module_name);
107 errno = ETOOMANYREFS;
108 return NULL;
109 }
110 /*
111 * Yes, there is a renaming.
112 * Make lookup use OID instead.
113 */
Lev Walkinb36317c2005-08-12 10:09:10 +0000114 oid = xp->identifier.oid;
Lev Walkinf15320b2004-06-03 03:38:44 +0000115 }
116 }
117
118 /*
119 * Perform lookup using OID or module_name.
120 */
121 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
122 if(oid) {
123 if(mod->module_oid) {
124 if(asn1p_oid_compare(oid,
125 mod->module_oid)) {
126 continue;
127 } else {
128 /* Match! Even if name doesn't. */
129 return mod;
130 }
131 } else {
132 /* Not match, even if name is the same. */
133 continue;
134 }
135 }
136
Lev Walkinb36317c2005-08-12 10:09:10 +0000137 if(strcmp(module_name, mod->ModuleName) == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 return mod;
139 }
140
141 DEBUG("\tModule \"%s\" not found", module_name);
142
143 errno = ENOENT;
144 return NULL;
145}
146
147
148
149asn1p_expr_t *
Lev Walkin6fec44d2004-08-22 03:10:23 +0000150asn1f_lookup_symbol(arg_t *arg, asn1p_module_t *mod, asn1p_ref_t *ref) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 asn1p_expr_t *ref_tc; /* Referenced tc */
Lev Walkin163db362004-09-15 11:46:47 +0000152 asn1p_module_t *imports_from;
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 char *modulename;
154 char *identifier;
155
156 /*
157 * First of all, a reference to a symbol may be specified
158 * using several possible forms:
159 * a) simple identifier
160 * v INTEGER ::= value
161 * b) external reference
162 * v INTEGER ::= Module1.value
163 * c) class-related stuff (the most complex stuff)
164 * v ::= <[A-Z][A-Z0-9a-z-]*>.&<[A-Z0-9a-z-]+>.
165 * All other forms are not implemented at this moment.
166 */
167
Lev Walkin03850182005-03-10 10:02:50 +0000168 DEBUG("(%s) in %s for line %d",
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 asn1f_printable_reference(ref),
Lev Walkinb36317c2005-08-12 10:09:10 +0000170 mod->ModuleName,
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 ref->_lineno);
172
173 if(ref->comp_count == 1) {
174 modulename = NULL;
175 identifier = ref->components[0].name;
176 } else if(ref->comp_count == 2
177 && ref->components[1].name[0] != '&') {
178 modulename = ref->components[0].name;
179 identifier = ref->components[1].name;
180 } else if(ref->comp_count > 1
181 && isupper(ref->components[0].name[0])
182 && ref->components[1].name[0] == '&') {
183 asn1p_expr_t *extract;
184 /*
185 * This is a reference to a CLASS-related stuff.
186 * Employ a separate function for that.
187 */
Lev Walkin6fec44d2004-08-22 03:10:23 +0000188 extract = asn1f_class_access(arg, mod, ref);
Lev Walkinf15320b2004-06-03 03:38:44 +0000189
190 return extract;
191 } else {
192 DEBUG("\tToo many components: %d", ref->comp_count);
193 errno = EINVAL;
194 return NULL;
195 }
196
197 /*
198 * If module name is specified explicitly
199 * OR the current module's IMPORTS clause contains the identifier,
200 * fetch that module.
201 */
202 if(modulename) {
Lev Walkin163db362004-09-15 11:46:47 +0000203 imports_from = asn1f_lookup_module(arg, modulename, 0);
204 if(imports_from == NULL) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000205 FATAL("Module \"%s\" "
206 "mentioned at line %d is not found",
207 modulename, ref->_lineno);
208 return NULL;
209 }
210
211 /*
212 * Check that the EXPORTS section of this module contains
213 * the symbol we care about, or it is EXPORTS ALL.
214 */
Lev Walkin163db362004-09-15 11:46:47 +0000215 if(asn1f_compatible_with_exports(arg,imports_from,identifier)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 errno = EPERM;
217 return NULL;
218 }
219 } else {
Lev Walkin163db362004-09-15 11:46:47 +0000220 imports_from = asn1f_lookup_in_imports(arg, mod, identifier);
221 if(imports_from == NULL && errno != ESRCH) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000222 /*
223 * Return only of the name was not found.
224 * If module was not found or more serious error
225 * encountered, just return preserving the errno.
226 */
227 return NULL;
228 }
229 }
230
Lev Walkin163db362004-09-15 11:46:47 +0000231 /*
232 * The symbol is being imported from another module.
233 */
234 if(imports_from) {
235 asn1p_ref_t tmpref = *ref;
Lev Walkind8095522005-03-18 05:20:39 +0000236 asn1p_expr_t *expr;
Lev Walkin163db362004-09-15 11:46:47 +0000237 if(modulename) {
238 /*
239 * The modulename is specified inside this reference.
240 * To avoid recursion, reformat the reference
241 * as it were local to that module.
242 */
243 tmpref.components++; /* Hide the first element */
244 tmpref.comp_count--;
245 assert(tmpref.comp_count > 0);
246 }
Lev Walkind8095522005-03-18 05:20:39 +0000247
248 expr = asn1f_lookup_symbol(arg, imports_from, &tmpref);
249 if(!expr && !(arg->expr->_mark & TM_BROKEN)) {
250 arg->expr->_mark |= TM_BROKEN;
251 if(modulename) {
252 FATAL("Module %s referred by %s in module %s "
253 "does not contain the requested symbol",
Lev Walkinb36317c2005-08-12 10:09:10 +0000254 imports_from->ModuleName,
Lev Walkind8095522005-03-18 05:20:39 +0000255 asn1f_printable_reference(ref),
Lev Walkinb36317c2005-08-12 10:09:10 +0000256 mod->ModuleName);
Lev Walkind8095522005-03-18 05:20:39 +0000257 } else {
258 FATAL("Module %s referred in IMPORTS section "
259 "for %s of module %s does not contain "
260 "the requested symbol",
Lev Walkinb36317c2005-08-12 10:09:10 +0000261 imports_from->ModuleName,
Lev Walkind8095522005-03-18 05:20:39 +0000262 asn1f_printable_reference(ref),
Lev Walkinb36317c2005-08-12 10:09:10 +0000263 mod->ModuleName);
Lev Walkind8095522005-03-18 05:20:39 +0000264 }
265 }
266 return expr;
Lev Walkin163db362004-09-15 11:46:47 +0000267 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000268
269 /*
Lev Walkin163db362004-09-15 11:46:47 +0000270 * Now we know where to search for a value: in the current module.
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 */
Lev Walkin163db362004-09-15 11:46:47 +0000272 TQ_FOR(ref_tc, &(mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000273 if(ref_tc->Identifier)
274 if(strcmp(ref_tc->Identifier, identifier) == 0)
275 break;
276 }
277 if(ref_tc == NULL) {
278 DEBUG("Module \"%s\" does not contain \"%s\" "
Lev Walkin97bdee22004-06-28 21:21:24 +0000279 "mentioned at line %d: %s",
Lev Walkinb36317c2005-08-12 10:09:10 +0000280 mod->ModuleName,
Lev Walkinf15320b2004-06-03 03:38:44 +0000281 identifier,
Lev Walkin97bdee22004-06-28 21:21:24 +0000282 ref->_lineno,
283 strerror(errno)
Lev Walkinf15320b2004-06-03 03:38:44 +0000284 );
Lev Walkin97bdee22004-06-28 21:21:24 +0000285 if(asn1f_check_known_external_type(identifier) == 0) {
286 errno = EEXIST; /* Exists somewhere */
287 } else {
288 errno = ESRCH;
289 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000290 return NULL;
291 }
292
Lev Walkinf15320b2004-06-03 03:38:44 +0000293 return ref_tc;
294}
295
296
297asn1p_expr_t *
Lev Walkin6fec44d2004-08-22 03:10:23 +0000298asn1f_find_terminal_type(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin33f63f82005-03-20 11:27:19 +0000299 return asn1f_find_terminal_thing(arg, expr, FTT_TYPE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000300}
301
302asn1p_expr_t *
Lev Walkin6fec44d2004-08-22 03:10:23 +0000303asn1f_find_terminal_value(arg_t *arg, asn1p_expr_t *expr) {
Lev Walkin33f63f82005-03-20 11:27:19 +0000304 return asn1f_find_terminal_thing(arg, expr, FTT_VALUE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000305}
306
307static asn1p_expr_t *
Lev Walkin33f63f82005-03-20 11:27:19 +0000308asn1f_find_terminal_thing(arg_t *arg, asn1p_expr_t *expr, enum ftt_what what) {
Lev Walkin039a09e2005-03-20 13:17:48 +0000309 asn1p_ref_t *ref = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000310 asn1p_expr_t *tc;
311
Lev Walkin33f63f82005-03-20 11:27:19 +0000312 switch(what) {
313 case FTT_TYPE:
314 /* Expression may be a terminal type itself */
315 if(expr->expr_type != A1TC_REFERENCE)
316 return expr;
317 ref = expr->reference;
318 break;
319 case FTT_VALUE:
Lev Walkinf15320b2004-06-03 03:38:44 +0000320 assert(expr->meta_type == AMT_VALUE);
321 assert(expr->value);
Lev Walkin6fec44d2004-08-22 03:10:23 +0000322 /* Expression may be a terminal type itself */
323 if(expr->value->type != ATV_REFERENCED)
Lev Walkinf15320b2004-06-03 03:38:44 +0000324 return expr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 ref = expr->value->value.reference;
Lev Walkin33f63f82005-03-20 11:27:19 +0000326 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000327 }
328
Lev Walkin03850182005-03-10 10:02:50 +0000329 DEBUG("%s(%s->%s) for line %d",
Lev Walkin33f63f82005-03-20 11:27:19 +0000330 (what == FTT_VALUE)?"VALUE":"TYPE",
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 expr->Identifier, asn1f_printable_reference(ref),
332 expr->_lineno);
333
334 assert(ref);
335
336 /*
337 * Lookup inside the type itself (ENUMERATED, INTEGER, etc).
338 */
Lev Walkin33f63f82005-03-20 11:27:19 +0000339 if(what == FTT_VALUE) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000340 asn1p_expr_t *val_type_tc;
Lev Walkin6fec44d2004-08-22 03:10:23 +0000341 val_type_tc = asn1f_find_terminal_type(arg, expr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000342 if(val_type_tc
343 && asn1f_look_value_in_type(arg, val_type_tc, expr))
344 return NULL;
345 if(expr->value->type != ATV_REFERENCED) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000346 return expr;
347 }
348 assert(ref == expr->value->value.reference);
349 ref = expr->value->value.reference;
350 }
351
352 /*
Lev Walkind8095522005-03-18 05:20:39 +0000353 * Lookup inside the default module and its IMPORTS section.
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 */
Lev Walkin6fec44d2004-08-22 03:10:23 +0000355 tc = asn1f_lookup_symbol(arg, expr->module, ref);
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 if(tc == NULL) {
Lev Walkin97bdee22004-06-28 21:21:24 +0000357 DEBUG("\tSymbol \"%s\" not found: %s",
358 asn1f_printable_reference(ref),
359 strerror(errno));
Lev Walkinf15320b2004-06-03 03:38:44 +0000360 return NULL;
361 }
362
363 /*
364 * Recursive loops detection.
365 */
366 if(tc->_mark & TM_RECURSION) {
367 DEBUG("Recursion loop detected for %s at line %d",
368 asn1f_printable_reference(ref), ref->_lineno);
369 errno = EPERM;
370 return NULL;
371 }
372
Lev Walkin59b176e2005-11-26 11:25:14 +0000373 tc->_type_referenced = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000374 tc->_mark |= TM_RECURSION;
Lev Walkin6fec44d2004-08-22 03:10:23 +0000375 WITH_MODULE(tc->module,
Lev Walkin33f63f82005-03-20 11:27:19 +0000376 expr = asn1f_find_terminal_thing(arg, tc, what));
Lev Walkinf15320b2004-06-03 03:38:44 +0000377 tc->_mark &= ~TM_RECURSION;
378
379 return expr;
380}
381
382/*
383 * Make sure that the specified name is present or otherwise does
384 * not contradict with the EXPORTS clause of the specified module.
385 */
386static int
387asn1f_compatible_with_exports(arg_t *arg, asn1p_module_t *mod, const char *name) {
388 asn1p_xports_t *exports;
389 asn1p_expr_t *item;
390
391 assert(mod);
392 assert(name);
393
394 exports = TQ_FIRST(&(mod->exports));
395 if(exports == NULL) {
396 /* No EXPORTS section or EXPORTS ALL; */
397 return 0;
398 }
399
400 TQ_FOR(item, &(exports->members), next) {
401 if(strcmp(item->Identifier, name) == 0)
402 return 0;
403 }
404
Lev Walkind8095522005-03-18 05:20:39 +0000405 /* Conditional debug */
406 if(!(arg->expr->_mark & TM_BROKEN)) {
407 arg->expr->_mark |= TM_BROKEN;
408 FATAL("EXPORTS section of module %s in %s "
409 "does not mention %s at line %d",
Lev Walkinb36317c2005-08-12 10:09:10 +0000410 mod->ModuleName, mod->source_file_name, name,
Lev Walkind8095522005-03-18 05:20:39 +0000411 arg->expr->_lineno);
412 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000413
414 errno = ESRCH;
415 return -1;
416}