vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
| 2 | |
vlm | 9322638 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 3 | enum ftt_what { |
| 4 | FTT_TYPE, /* Find the type of the given expression */ |
| 5 | FTT_VALUE, /* Find the value of the given expression */ |
| 6 | }; |
| 7 | |
| 8 | static asn1p_expr_t *asn1f_find_terminal_thing(arg_t *arg, asn1p_expr_t *expr, enum ftt_what); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 9 | static 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 | */ |
| 15 | asn1p_expr_t * |
| 16 | asn1f_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 | |
| 30 | asn1p_module_t * |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 31 | asn1f_lookup_in_imports(arg_t *arg, asn1p_module_t *mod, const char *name) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | asn1p_xports_t *xp; |
| 33 | asn1p_expr_t *tc; |
| 34 | |
| 35 | /* |
| 36 | * Search in which exactly module this name is defined. |
| 37 | */ |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 38 | TQ_FOR(xp, &(mod->imports), xp_next) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | 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 | */ |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 54 | mod = asn1f_lookup_module(arg, xp->fromModuleName, xp->identifier.oid); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | if(mod == NULL) { |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 56 | /* Conditional debug */ |
| 57 | if(!(arg->expr->_mark & TM_BROKEN)) { |
| 58 | arg->expr->_mark |= TM_BROKEN; |
vlm | cfd23ac | 2005-04-12 17:46:30 +0000 | [diff] [blame] | 59 | FATAL("Cannot find external module \"%s\" " |
| 60 | "mentioned for " |
vlm | c03306f | 2006-03-18 05:06:57 +0000 | [diff] [blame^] | 61 | "\"%s\" at line %d. " |
| 62 | "Obtain this module and instruct compiler to process it too.", |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 63 | xp->fromModuleName, name, arg->expr->_lineno); |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 64 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 65 | /* 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 | |
| 81 | asn1p_module_t * |
| 82 | asn1f_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) { |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 101 | if(strcmp(module_name, xp->fromModuleName)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | 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 | */ |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 115 | oid = xp->identifier.oid; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | } |
| 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 | |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 138 | if(strcmp(module_name, mod->ModuleName) == 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | return mod; |
| 140 | } |
| 141 | |
| 142 | DEBUG("\tModule \"%s\" not found", module_name); |
| 143 | |
| 144 | errno = ENOENT; |
| 145 | return NULL; |
| 146 | } |
| 147 | |
vlm | c03306f | 2006-03-18 05:06:57 +0000 | [diff] [blame^] | 148 | static asn1p_expr_t * |
| 149 | asn1f_lookup_symbol_impl(arg_t *arg, asn1p_module_t *mod, asn1p_ref_t *ref, int recursion_depth) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 150 | asn1p_expr_t *ref_tc; /* Referenced tc */ |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 151 | asn1p_module_t *imports_from; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 152 | 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 | |
vlm | fd24593 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 167 | DEBUG("(%s) in %s for line %d", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 168 | asn1f_printable_reference(ref), |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 169 | mod->ModuleName, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 170 | ref->_lineno); |
| 171 | |
vlm | c03306f | 2006-03-18 05:06:57 +0000 | [diff] [blame^] | 172 | 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 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 181 | 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 | */ |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 196 | extract = asn1f_class_access(arg, mod, ref); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 197 | |
| 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) { |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 211 | imports_from = asn1f_lookup_module(arg, modulename, 0); |
| 212 | if(imports_from == NULL) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 213 | 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 | */ |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 223 | if(asn1f_compatible_with_exports(arg,imports_from,identifier)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 224 | errno = EPERM; |
| 225 | return NULL; |
| 226 | } |
| 227 | } else { |
vlm | 79d4e63 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 228 | /* Search inside the IMPORTS section of the current module */ |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 229 | imports_from = asn1f_lookup_in_imports(arg, mod, identifier); |
| 230 | if(imports_from == NULL && errno != ESRCH) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 231 | /* |
| 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 | |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 240 | /* |
| 241 | * The symbol is being imported from another module. |
| 242 | */ |
vlm | 79d4e63 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 243 | importing: |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 244 | if(imports_from) { |
| 245 | asn1p_ref_t tmpref = *ref; |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 246 | asn1p_expr_t *expr; |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 247 | 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 | } |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 257 | |
vlm | c03306f | 2006-03-18 05:06:57 +0000 | [diff] [blame^] | 258 | expr = asn1f_lookup_symbol_impl(arg, imports_from, &tmpref, recursion_depth); |
vlm | d938e0b | 2006-03-16 11:04:55 +0000 | [diff] [blame] | 259 | if(!expr && !(arg->expr->_mark & TM_BROKEN) |
| 260 | && !(imports_from->_tags & MT_STANDARD_MODULE)) { |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 261 | arg->expr->_mark |= TM_BROKEN; |
| 262 | if(modulename) { |
| 263 | FATAL("Module %s referred by %s in module %s " |
| 264 | "does not contain the requested symbol", |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 265 | imports_from->ModuleName, |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 266 | asn1f_printable_reference(ref), |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 267 | mod->ModuleName); |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 268 | } else { |
| 269 | FATAL("Module %s referred in IMPORTS section " |
| 270 | "for %s of module %s does not contain " |
| 271 | "the requested symbol", |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 272 | imports_from->ModuleName, |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 273 | asn1f_printable_reference(ref), |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 274 | mod->ModuleName); |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | return expr; |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 278 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 279 | |
| 280 | /* |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 281 | * Now we know where to search for a value: in the current module. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 282 | */ |
vlm | f98e07a | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 283 | TQ_FOR(ref_tc, &(mod->members), next) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 284 | if(ref_tc->Identifier) |
| 285 | if(strcmp(ref_tc->Identifier, identifier) == 0) |
| 286 | break; |
| 287 | } |
vlm | 79d4e63 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 288 | if(ref_tc) |
| 289 | return ref_tc; |
| 290 | |
| 291 | { |
| 292 | /* Search inside standard module */ |
| 293 | static asn1p_oid_t *uioc_oid; |
| 294 | if(!uioc_oid) { |
| 295 | asn1p_oid_arc_t arcs[] = { |
| 296 | { 1, "iso" }, |
| 297 | { 3, "org" }, |
| 298 | { 6, "dod" }, |
| 299 | { 1, "internet" }, |
| 300 | { 4, "private" }, |
| 301 | { 1, "enterprise" }, |
| 302 | { 9363, "spelio" }, |
| 303 | { 1, "software" }, |
| 304 | { 5, "asn1c" }, |
| 305 | { 3, "standard-modules" }, |
| 306 | { 0, "auto-imported" }, |
| 307 | { 1, 0 } |
| 308 | }; |
| 309 | uioc_oid = asn1p_oid_construct(arcs, |
| 310 | sizeof(arcs)/sizeof(arcs[0])); |
vlm | d9cd3f9 | 2004-06-28 21:21:24 +0000 | [diff] [blame] | 311 | } |
vlm | d0d7cf3 | 2006-03-16 11:24:47 +0000 | [diff] [blame] | 312 | if(!imports_from && (!mod->module_oid |
| 313 | || asn1p_oid_compare(mod->module_oid, uioc_oid))) { |
vlm | 79d4e63 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 314 | imports_from = asn1f_lookup_module(arg, |
| 315 | "ASN1C-UsefulInformationObjectClasses", |
| 316 | uioc_oid); |
| 317 | if(imports_from) goto importing; |
| 318 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 319 | } |
| 320 | |
vlm | 79d4e63 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 321 | DEBUG("Module \"%s\" does not contain \"%s\" " |
| 322 | "mentioned at line %d: %s", |
| 323 | mod->ModuleName, |
| 324 | identifier, |
| 325 | ref->_lineno, |
| 326 | strerror(errno)); |
| 327 | |
| 328 | if(asn1f_check_known_external_type(identifier) == 0) { |
| 329 | errno = EEXIST; /* Exists somewhere */ |
| 330 | } else { |
| 331 | errno = ESRCH; |
| 332 | } |
| 333 | return NULL; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
| 337 | asn1p_expr_t * |
vlm | c03306f | 2006-03-18 05:06:57 +0000 | [diff] [blame^] | 338 | asn1f_lookup_symbol(arg_t *arg, asn1p_module_t *mod, asn1p_ref_t *ref) { |
| 339 | return asn1f_lookup_symbol_impl(arg, mod, ref, 0); |
| 340 | } |
| 341 | |
| 342 | asn1p_expr_t * |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 343 | asn1f_find_terminal_type(arg_t *arg, asn1p_expr_t *expr) { |
vlm | 9322638 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 344 | return asn1f_find_terminal_thing(arg, expr, FTT_TYPE); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | asn1p_expr_t * |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 348 | asn1f_find_terminal_value(arg_t *arg, asn1p_expr_t *expr) { |
vlm | 9322638 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 349 | return asn1f_find_terminal_thing(arg, expr, FTT_VALUE); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static asn1p_expr_t * |
vlm | 9322638 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 353 | asn1f_find_terminal_thing(arg_t *arg, asn1p_expr_t *expr, enum ftt_what what) { |
vlm | 2fb3f75 | 2005-03-20 13:17:48 +0000 | [diff] [blame] | 354 | asn1p_ref_t *ref = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 355 | asn1p_expr_t *tc; |
| 356 | |
vlm | 9322638 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 357 | switch(what) { |
| 358 | case FTT_TYPE: |
| 359 | /* Expression may be a terminal type itself */ |
| 360 | if(expr->expr_type != A1TC_REFERENCE) |
| 361 | return expr; |
| 362 | ref = expr->reference; |
| 363 | break; |
| 364 | case FTT_VALUE: |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 365 | assert(expr->meta_type == AMT_VALUE); |
| 366 | assert(expr->value); |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 367 | /* Expression may be a terminal type itself */ |
| 368 | if(expr->value->type != ATV_REFERENCED) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 369 | return expr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 370 | ref = expr->value->value.reference; |
vlm | 9322638 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 371 | break; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 372 | } |
| 373 | |
vlm | fd24593 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 374 | DEBUG("%s(%s->%s) for line %d", |
vlm | 9322638 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 375 | (what == FTT_VALUE)?"VALUE":"TYPE", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 376 | expr->Identifier, asn1f_printable_reference(ref), |
| 377 | expr->_lineno); |
| 378 | |
| 379 | assert(ref); |
| 380 | |
| 381 | /* |
| 382 | * Lookup inside the type itself (ENUMERATED, INTEGER, etc). |
| 383 | */ |
vlm | 9322638 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 384 | if(what == FTT_VALUE) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 385 | asn1p_expr_t *val_type_tc; |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 386 | val_type_tc = asn1f_find_terminal_type(arg, expr); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 387 | if(val_type_tc |
| 388 | && asn1f_look_value_in_type(arg, val_type_tc, expr)) |
| 389 | return NULL; |
| 390 | if(expr->value->type != ATV_REFERENCED) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 391 | return expr; |
| 392 | } |
| 393 | assert(ref == expr->value->value.reference); |
| 394 | ref = expr->value->value.reference; |
| 395 | } |
| 396 | |
| 397 | /* |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 398 | * Lookup inside the default module and its IMPORTS section. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 399 | */ |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 400 | tc = asn1f_lookup_symbol(arg, expr->module, ref); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 401 | if(tc == NULL) { |
vlm | d9cd3f9 | 2004-06-28 21:21:24 +0000 | [diff] [blame] | 402 | DEBUG("\tSymbol \"%s\" not found: %s", |
| 403 | asn1f_printable_reference(ref), |
| 404 | strerror(errno)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Recursive loops detection. |
| 410 | */ |
| 411 | if(tc->_mark & TM_RECURSION) { |
| 412 | DEBUG("Recursion loop detected for %s at line %d", |
| 413 | asn1f_printable_reference(ref), ref->_lineno); |
| 414 | errno = EPERM; |
| 415 | return NULL; |
| 416 | } |
| 417 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 418 | tc->_type_referenced = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 419 | tc->_mark |= TM_RECURSION; |
vlm | 2e0c194 | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 420 | WITH_MODULE(tc->module, |
vlm | 9322638 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 421 | expr = asn1f_find_terminal_thing(arg, tc, what)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 422 | tc->_mark &= ~TM_RECURSION; |
| 423 | |
| 424 | return expr; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Make sure that the specified name is present or otherwise does |
| 429 | * not contradict with the EXPORTS clause of the specified module. |
| 430 | */ |
| 431 | static int |
| 432 | asn1f_compatible_with_exports(arg_t *arg, asn1p_module_t *mod, const char *name) { |
| 433 | asn1p_xports_t *exports; |
| 434 | asn1p_expr_t *item; |
| 435 | |
| 436 | assert(mod); |
| 437 | assert(name); |
| 438 | |
| 439 | exports = TQ_FIRST(&(mod->exports)); |
| 440 | if(exports == NULL) { |
| 441 | /* No EXPORTS section or EXPORTS ALL; */ |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | TQ_FOR(item, &(exports->members), next) { |
| 446 | if(strcmp(item->Identifier, name) == 0) |
| 447 | return 0; |
| 448 | } |
| 449 | |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 450 | /* Conditional debug */ |
| 451 | if(!(arg->expr->_mark & TM_BROKEN)) { |
| 452 | arg->expr->_mark |= TM_BROKEN; |
| 453 | FATAL("EXPORTS section of module %s in %s " |
| 454 | "does not mention %s at line %d", |
vlm | 931aeed | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 455 | mod->ModuleName, mod->source_file_name, name, |
vlm | f1d86c9 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 456 | arg->expr->_lineno); |
| 457 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 458 | |
| 459 | errno = ESRCH; |
| 460 | return -1; |
| 461 | } |