blob: 52506b8149f014c2220760c312b78fef85db15aa [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
vlm93226382005-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);
vlmfa67ddc2004-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 *
vlmf98e07a2004-09-15 11:46:47 +000031asn1f_lookup_in_imports(arg_t *arg, asn1p_module_t *mod, const char *name) {
vlmfa67ddc2004-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 */
vlmf98e07a2004-09-15 11:46:47 +000038 TQ_FOR(xp, &(mod->imports), xp_next) {
vlmfa67ddc2004-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 */
vlm931aeed2005-08-12 10:09:10 +000054 mod = asn1f_lookup_module(arg, xp->fromModuleName, xp->identifier.oid);
vlmfa67ddc2004-06-03 03:38:44 +000055 if(mod == NULL) {
vlmf1d86c92005-03-18 05:20:39 +000056 /* Conditional debug */
57 if(!(arg->expr->_mark & TM_BROKEN)) {
58 arg->expr->_mark |= TM_BROKEN;
vlmcfd23ac2005-04-12 17:46:30 +000059 FATAL("Cannot find external module \"%s\" "
60 "mentioned for "
vlmc03306f2006-03-18 05:06:57 +000061 "\"%s\" at line %d. "
62 "Obtain this module and instruct compiler to process it too.",
vlm931aeed2005-08-12 10:09:10 +000063 xp->fromModuleName, name, arg->expr->_lineno);
vlmf1d86c92005-03-18 05:20:39 +000064 }
vlmfa67ddc2004-06-03 03:38:44 +000065 /* ENOENT/ETOOMANYREFS */
66 return NULL;
67 }
68
69 /*
70 * Check that the EXPORTS section of this module contains
71 * the symbol we care about, or it is EXPORTS ALL.
72 */
73 if(asn1f_compatible_with_exports(arg, mod, name)) {
74 errno = EPERM;
75 return NULL;
76 }
77
78 return mod;
79}
80
81asn1p_module_t *
82asn1f_lookup_module(arg_t *arg, const char *module_name, asn1p_oid_t *oid) {
83 asn1p_module_t *mod;
84
85 assert(module_name);
86
87 /*
88 * If OID is given, the module_name is unused.
89 * If OID is not given, the module_name may mean
90 * either the real module's name, or the symbol which is the
91 * result of renaming. Check this first.
92 */
93 if(oid == 0) {
94 asn1p_xports_t *xp;
95 /*
96 * Check inside the IMPORTS section for possible renaming.
97 * Renaming practically means that a module_name is mentioned
98 * somewhere in the IMPORTS section AND OID is given.
99 */
100 TQ_FOR(xp, &(arg->mod->imports), xp_next) {
vlm931aeed2005-08-12 10:09:10 +0000101 if(strcmp(module_name, xp->fromModuleName))
vlmfa67ddc2004-06-03 03:38:44 +0000102 continue;
103 if(oid) {
104 FATAL("Ambiguous reference: "
105 "%s "
106 "matches several modules",
107 module_name);
108 errno = ETOOMANYREFS;
109 return NULL;
110 }
111 /*
112 * Yes, there is a renaming.
113 * Make lookup use OID instead.
114 */
vlm931aeed2005-08-12 10:09:10 +0000115 oid = xp->identifier.oid;
vlmfa67ddc2004-06-03 03:38:44 +0000116 }
117 }
118
119 /*
120 * Perform lookup using OID or module_name.
121 */
122 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
123 if(oid) {
124 if(mod->module_oid) {
125 if(asn1p_oid_compare(oid,
126 mod->module_oid)) {
127 continue;
128 } else {
129 /* Match! Even if name doesn't. */
130 return mod;
131 }
132 } else {
133 /* Not match, even if name is the same. */
134 continue;
135 }
136 }
137
vlm931aeed2005-08-12 10:09:10 +0000138 if(strcmp(module_name, mod->ModuleName) == 0)
vlmfa67ddc2004-06-03 03:38:44 +0000139 return mod;
140 }
141
142 DEBUG("\tModule \"%s\" not found", module_name);
143
144 errno = ENOENT;
145 return NULL;
146}
147
vlmc03306f2006-03-18 05:06:57 +0000148static asn1p_expr_t *
vlm0c6d3812006-03-21 03:40:38 +0000149asn1f_lookup_symbol_impl(arg_t *arg, asn1p_module_t *mod, asn1p_expr_t *rhs_pspecs, asn1p_ref_t *ref, int recursion_depth) {
vlmfa67ddc2004-06-03 03:38:44 +0000150 asn1p_expr_t *ref_tc; /* Referenced tc */
vlmf98e07a2004-09-15 11:46:47 +0000151 asn1p_module_t *imports_from;
vlmfa67ddc2004-06-03 03:38:44 +0000152 char *modulename;
153 char *identifier;
154
155 /*
156 * First of all, a reference to a symbol may be specified
157 * using several possible forms:
158 * a) simple identifier
159 * v INTEGER ::= value
160 * b) external reference
161 * v INTEGER ::= Module1.value
162 * c) class-related stuff (the most complex stuff)
163 * v ::= <[A-Z][A-Z0-9a-z-]*>.&<[A-Z0-9a-z-]+>.
164 * All other forms are not implemented at this moment.
165 */
166
vlmfd245932005-03-10 10:02:50 +0000167 DEBUG("(%s) in %s for line %d",
vlmfa67ddc2004-06-03 03:38:44 +0000168 asn1f_printable_reference(ref),
vlm931aeed2005-08-12 10:09:10 +0000169 mod->ModuleName,
vlmfa67ddc2004-06-03 03:38:44 +0000170 ref->_lineno);
171
vlmc03306f2006-03-18 05:06:57 +0000172 if(recursion_depth++ > 30 /* Arbitrary constant */) {
173 FATAL("Excessive circular referencing detected in module %s for %s at line %d",
174 mod->ModuleName,
175 asn1f_printable_reference(ref),
176 ref->_lineno);
177 errno = ETOOMANYREFS;
178 return NULL;
179 }
180
vlmfa67ddc2004-06-03 03:38:44 +0000181 if(ref->comp_count == 1) {
182 modulename = NULL;
183 identifier = ref->components[0].name;
184 } else if(ref->comp_count == 2
185 && ref->components[1].name[0] != '&') {
186 modulename = ref->components[0].name;
187 identifier = ref->components[1].name;
188 } else if(ref->comp_count > 1
189 && isupper(ref->components[0].name[0])
190 && ref->components[1].name[0] == '&') {
191 asn1p_expr_t *extract;
192 /*
193 * This is a reference to a CLASS-related stuff.
194 * Employ a separate function for that.
195 */
vlm0c6d3812006-03-21 03:40:38 +0000196 extract = asn1f_class_access(arg, mod, rhs_pspecs, ref);
vlmfa67ddc2004-06-03 03:38:44 +0000197
198 return extract;
199 } else {
200 DEBUG("\tToo many components: %d", ref->comp_count);
201 errno = EINVAL;
202 return NULL;
203 }
204
205 /*
206 * If module name is specified explicitly
207 * OR the current module's IMPORTS clause contains the identifier,
208 * fetch that module.
209 */
210 if(modulename) {
vlmf98e07a2004-09-15 11:46:47 +0000211 imports_from = asn1f_lookup_module(arg, modulename, 0);
212 if(imports_from == NULL) {
vlmfa67ddc2004-06-03 03:38:44 +0000213 FATAL("Module \"%s\" "
214 "mentioned at line %d is not found",
215 modulename, ref->_lineno);
216 return NULL;
217 }
218
219 /*
220 * Check that the EXPORTS section of this module contains
221 * the symbol we care about, or it is EXPORTS ALL.
222 */
vlmf98e07a2004-09-15 11:46:47 +0000223 if(asn1f_compatible_with_exports(arg,imports_from,identifier)) {
vlmfa67ddc2004-06-03 03:38:44 +0000224 errno = EPERM;
225 return NULL;
226 }
227 } else {
vlm79d4e632006-03-06 14:51:00 +0000228 /* Search inside the IMPORTS section of the current module */
vlmf98e07a2004-09-15 11:46:47 +0000229 imports_from = asn1f_lookup_in_imports(arg, mod, identifier);
230 if(imports_from == NULL && errno != ESRCH) {
vlmfa67ddc2004-06-03 03:38:44 +0000231 /*
232 * Return only of the name was not found.
233 * If module was not found or more serious error
234 * encountered, just return preserving the errno.
235 */
236 return NULL;
237 }
238 }
239
vlmf98e07a2004-09-15 11:46:47 +0000240 /*
241 * The symbol is being imported from another module.
242 */
vlm79d4e632006-03-06 14:51:00 +0000243 importing:
vlmf98e07a2004-09-15 11:46:47 +0000244 if(imports_from) {
245 asn1p_ref_t tmpref = *ref;
vlmf1d86c92005-03-18 05:20:39 +0000246 asn1p_expr_t *expr;
vlmf98e07a2004-09-15 11:46:47 +0000247 if(modulename) {
248 /*
249 * The modulename is specified inside this reference.
250 * To avoid recursion, reformat the reference
251 * as it were local to that module.
252 */
253 tmpref.components++; /* Hide the first element */
254 tmpref.comp_count--;
255 assert(tmpref.comp_count > 0);
256 }
vlmf1d86c92005-03-18 05:20:39 +0000257
vlm0c6d3812006-03-21 03:40:38 +0000258 expr = asn1f_lookup_symbol_impl(arg, imports_from,
259 rhs_pspecs, &tmpref, recursion_depth);
vlmd938e0b2006-03-16 11:04:55 +0000260 if(!expr && !(arg->expr->_mark & TM_BROKEN)
261 && !(imports_from->_tags & MT_STANDARD_MODULE)) {
vlmf1d86c92005-03-18 05:20:39 +0000262 arg->expr->_mark |= TM_BROKEN;
263 if(modulename) {
264 FATAL("Module %s referred by %s in module %s "
265 "does not contain the requested symbol",
vlm931aeed2005-08-12 10:09:10 +0000266 imports_from->ModuleName,
vlmf1d86c92005-03-18 05:20:39 +0000267 asn1f_printable_reference(ref),
vlm931aeed2005-08-12 10:09:10 +0000268 mod->ModuleName);
vlmf1d86c92005-03-18 05:20:39 +0000269 } else {
270 FATAL("Module %s referred in IMPORTS section "
271 "for %s of module %s does not contain "
272 "the requested symbol",
vlm931aeed2005-08-12 10:09:10 +0000273 imports_from->ModuleName,
vlmf1d86c92005-03-18 05:20:39 +0000274 asn1f_printable_reference(ref),
vlm931aeed2005-08-12 10:09:10 +0000275 mod->ModuleName);
vlmf1d86c92005-03-18 05:20:39 +0000276 }
277 }
278 return expr;
vlmf98e07a2004-09-15 11:46:47 +0000279 }
vlmfa67ddc2004-06-03 03:38:44 +0000280
281 /*
vlmf98e07a2004-09-15 11:46:47 +0000282 * Now we know where to search for a value: in the current module.
vlmfa67ddc2004-06-03 03:38:44 +0000283 */
vlmf98e07a2004-09-15 11:46:47 +0000284 TQ_FOR(ref_tc, &(mod->members), next) {
vlmfa67ddc2004-06-03 03:38:44 +0000285 if(ref_tc->Identifier)
286 if(strcmp(ref_tc->Identifier, identifier) == 0)
287 break;
288 }
vlm0c6d3812006-03-21 03:40:38 +0000289 if(ref_tc) {
290 if(rhs_pspecs && !ref_tc->lhs_params) {
291 FATAL("Parameterized type %s expected "
292 "for %s at line %d",
293 ref_tc->Identifier,
294 asn1f_printable_reference(ref),
295 ref->_lineno);
296 errno = EPERM;
297 return NULL;
298 }
299 if(!rhs_pspecs && ref_tc->lhs_params) {
300 FATAL("Type %s expects specialization "
301 "from %s at line %d",
302 ref_tc->Identifier,
303 asn1f_printable_reference(ref),
304 ref->_lineno);
305 errno = EPERM;
306 return NULL;
307 }
308 if(rhs_pspecs && ref_tc->lhs_params) {
309 /* Specialize the target */
310 ref_tc = asn1f_parameterization_fork(arg,
311 ref_tc, rhs_pspecs);
312 }
vlm79d4e632006-03-06 14:51:00 +0000313
vlm0c6d3812006-03-21 03:40:38 +0000314 return ref_tc;
315 }
316
317 /*
318 * Not found in the current module.
319 * Search in our default standard module.
320 */
vlm79d4e632006-03-06 14:51:00 +0000321 {
322 /* Search inside standard module */
323 static asn1p_oid_t *uioc_oid;
324 if(!uioc_oid) {
325 asn1p_oid_arc_t arcs[] = {
326 { 1, "iso" },
327 { 3, "org" },
328 { 6, "dod" },
329 { 1, "internet" },
330 { 4, "private" },
331 { 1, "enterprise" },
332 { 9363, "spelio" },
333 { 1, "software" },
334 { 5, "asn1c" },
335 { 3, "standard-modules" },
336 { 0, "auto-imported" },
337 { 1, 0 }
338 };
339 uioc_oid = asn1p_oid_construct(arcs,
340 sizeof(arcs)/sizeof(arcs[0]));
vlmd9cd3f92004-06-28 21:21:24 +0000341 }
vlmd0d7cf32006-03-16 11:24:47 +0000342 if(!imports_from && (!mod->module_oid
343 || asn1p_oid_compare(mod->module_oid, uioc_oid))) {
vlm79d4e632006-03-06 14:51:00 +0000344 imports_from = asn1f_lookup_module(arg,
345 "ASN1C-UsefulInformationObjectClasses",
346 uioc_oid);
347 if(imports_from) goto importing;
348 }
vlmfa67ddc2004-06-03 03:38:44 +0000349 }
350
vlm79d4e632006-03-06 14:51:00 +0000351 DEBUG("Module \"%s\" does not contain \"%s\" "
352 "mentioned at line %d: %s",
353 mod->ModuleName,
354 identifier,
355 ref->_lineno,
356 strerror(errno));
357
358 if(asn1f_check_known_external_type(identifier) == 0) {
359 errno = EEXIST; /* Exists somewhere */
360 } else {
361 errno = ESRCH;
362 }
363 return NULL;
vlmfa67ddc2004-06-03 03:38:44 +0000364}
365
366
367asn1p_expr_t *
vlm0c6d3812006-03-21 03:40:38 +0000368asn1f_lookup_symbol(arg_t *arg,
369 asn1p_module_t *mod, asn1p_expr_t *rhs_pspecs, asn1p_ref_t *ref) {
370 return asn1f_lookup_symbol_impl(arg, mod, rhs_pspecs, ref, 0);
vlmc03306f2006-03-18 05:06:57 +0000371}
372
373asn1p_expr_t *
vlm2e0c1942004-08-22 03:10:23 +0000374asn1f_find_terminal_type(arg_t *arg, asn1p_expr_t *expr) {
vlm93226382005-03-20 11:27:19 +0000375 return asn1f_find_terminal_thing(arg, expr, FTT_TYPE);
vlmfa67ddc2004-06-03 03:38:44 +0000376}
377
378asn1p_expr_t *
vlm2e0c1942004-08-22 03:10:23 +0000379asn1f_find_terminal_value(arg_t *arg, asn1p_expr_t *expr) {
vlm93226382005-03-20 11:27:19 +0000380 return asn1f_find_terminal_thing(arg, expr, FTT_VALUE);
vlmfa67ddc2004-06-03 03:38:44 +0000381}
382
383static asn1p_expr_t *
vlm93226382005-03-20 11:27:19 +0000384asn1f_find_terminal_thing(arg_t *arg, asn1p_expr_t *expr, enum ftt_what what) {
vlm2fb3f752005-03-20 13:17:48 +0000385 asn1p_ref_t *ref = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000386 asn1p_expr_t *tc;
387
vlm93226382005-03-20 11:27:19 +0000388 switch(what) {
389 case FTT_TYPE:
390 /* Expression may be a terminal type itself */
391 if(expr->expr_type != A1TC_REFERENCE)
392 return expr;
393 ref = expr->reference;
394 break;
395 case FTT_VALUE:
vlmfa67ddc2004-06-03 03:38:44 +0000396 assert(expr->meta_type == AMT_VALUE);
397 assert(expr->value);
vlm2e0c1942004-08-22 03:10:23 +0000398 /* Expression may be a terminal type itself */
399 if(expr->value->type != ATV_REFERENCED)
vlmfa67ddc2004-06-03 03:38:44 +0000400 return expr;
vlmfa67ddc2004-06-03 03:38:44 +0000401 ref = expr->value->value.reference;
vlm93226382005-03-20 11:27:19 +0000402 break;
vlmfa67ddc2004-06-03 03:38:44 +0000403 }
404
vlmfd245932005-03-10 10:02:50 +0000405 DEBUG("%s(%s->%s) for line %d",
vlm93226382005-03-20 11:27:19 +0000406 (what == FTT_VALUE)?"VALUE":"TYPE",
vlmfa67ddc2004-06-03 03:38:44 +0000407 expr->Identifier, asn1f_printable_reference(ref),
408 expr->_lineno);
409
410 assert(ref);
411
412 /*
413 * Lookup inside the type itself (ENUMERATED, INTEGER, etc).
414 */
vlm93226382005-03-20 11:27:19 +0000415 if(what == FTT_VALUE) {
vlmfa67ddc2004-06-03 03:38:44 +0000416 asn1p_expr_t *val_type_tc;
vlm2e0c1942004-08-22 03:10:23 +0000417 val_type_tc = asn1f_find_terminal_type(arg, expr);
vlmfa67ddc2004-06-03 03:38:44 +0000418 if(val_type_tc
419 && asn1f_look_value_in_type(arg, val_type_tc, expr))
420 return NULL;
421 if(expr->value->type != ATV_REFERENCED) {
vlmfa67ddc2004-06-03 03:38:44 +0000422 return expr;
423 }
424 assert(ref == expr->value->value.reference);
425 ref = expr->value->value.reference;
426 }
427
428 /*
vlmf1d86c92005-03-18 05:20:39 +0000429 * Lookup inside the default module and its IMPORTS section.
vlmfa67ddc2004-06-03 03:38:44 +0000430 */
vlm0c6d3812006-03-21 03:40:38 +0000431 tc = asn1f_lookup_symbol(arg, expr->module, expr->rhs_pspecs, ref);
vlmfa67ddc2004-06-03 03:38:44 +0000432 if(tc == NULL) {
vlmd9cd3f92004-06-28 21:21:24 +0000433 DEBUG("\tSymbol \"%s\" not found: %s",
434 asn1f_printable_reference(ref),
435 strerror(errno));
vlmfa67ddc2004-06-03 03:38:44 +0000436 return NULL;
437 }
438
439 /*
440 * Recursive loops detection.
441 */
442 if(tc->_mark & TM_RECURSION) {
443 DEBUG("Recursion loop detected for %s at line %d",
444 asn1f_printable_reference(ref), ref->_lineno);
445 errno = EPERM;
446 return NULL;
447 }
448
vlm337167e2005-11-26 11:25:14 +0000449 tc->_type_referenced = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000450 tc->_mark |= TM_RECURSION;
vlm2e0c1942004-08-22 03:10:23 +0000451 WITH_MODULE(tc->module,
vlm93226382005-03-20 11:27:19 +0000452 expr = asn1f_find_terminal_thing(arg, tc, what));
vlmfa67ddc2004-06-03 03:38:44 +0000453 tc->_mark &= ~TM_RECURSION;
454
455 return expr;
456}
457
458/*
459 * Make sure that the specified name is present or otherwise does
460 * not contradict with the EXPORTS clause of the specified module.
461 */
462static int
463asn1f_compatible_with_exports(arg_t *arg, asn1p_module_t *mod, const char *name) {
464 asn1p_xports_t *exports;
465 asn1p_expr_t *item;
466
467 assert(mod);
468 assert(name);
469
470 exports = TQ_FIRST(&(mod->exports));
471 if(exports == NULL) {
472 /* No EXPORTS section or EXPORTS ALL; */
473 return 0;
474 }
475
476 TQ_FOR(item, &(exports->members), next) {
477 if(strcmp(item->Identifier, name) == 0)
478 return 0;
479 }
480
vlmf1d86c92005-03-18 05:20:39 +0000481 /* Conditional debug */
482 if(!(arg->expr->_mark & TM_BROKEN)) {
483 arg->expr->_mark |= TM_BROKEN;
484 FATAL("EXPORTS section of module %s in %s "
485 "does not mention %s at line %d",
vlm931aeed2005-08-12 10:09:10 +0000486 mod->ModuleName, mod->source_file_name, name,
vlmf1d86c92005-03-18 05:20:39 +0000487 arg->expr->_lineno);
488 }
vlmfa67ddc2004-06-03 03:38:44 +0000489
490 errno = ESRCH;
491 return -1;
492}