Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1fix_internal.h" |
| 2 | |
Lev Walkin | 33f63f8 | 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); |
Lev Walkin | f15320b | 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 * |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 31 | asn1f_lookup_in_imports(arg_t *arg, asn1p_module_t *mod, const char *name) { |
Lev Walkin | f15320b | 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 | */ |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 38 | TQ_FOR(xp, &(mod->imports), xp_next) { |
Lev Walkin | f15320b | 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 | */ |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 54 | mod = asn1f_lookup_module(arg, xp->fromModuleName, xp->identifier.oid); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | if(mod == NULL) { |
Lev Walkin | d809552 | 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; |
Lev Walkin | 9e366ba | 2005-04-12 17:46:30 +0000 | [diff] [blame] | 59 | FATAL("Cannot find external module \"%s\" " |
| 60 | "mentioned for " |
Lev Walkin | 41635d3 | 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.", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 63 | xp->fromModuleName, name, arg->expr->_lineno); |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 64 | } |
Lev Walkin | f15320b | 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) { |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 101 | if(strcmp(module_name, xp->fromModuleName)) |
Lev Walkin | f15320b | 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 | */ |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 115 | oid = xp->identifier.oid; |
Lev Walkin | f15320b | 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 | |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 138 | if(strcmp(module_name, mod->ModuleName) == 0) |
Lev Walkin | f15320b | 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 | |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 148 | static asn1p_expr_t * |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 149 | asn1f_lookup_symbol_impl(arg_t *arg, asn1p_module_t *mod, asn1p_expr_t *rhs_pspecs, asn1p_ref_t *ref, int recursion_depth) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 150 | asn1p_expr_t *ref_tc; /* Referenced tc */ |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 151 | asn1p_module_t *imports_from; |
Lev Walkin | f15320b | 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 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 167 | DEBUG("(%s) in %s for line %d", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 168 | asn1f_printable_reference(ref), |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 169 | mod->ModuleName, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 170 | ref->_lineno); |
| 171 | |
Lev Walkin | 41635d3 | 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 | |
Lev Walkin | f15320b | 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 | */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 196 | extract = asn1f_class_access(arg, mod, rhs_pspecs, ref); |
Lev Walkin | f15320b | 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) { |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 211 | imports_from = asn1f_lookup_module(arg, modulename, 0); |
| 212 | if(imports_from == NULL) { |
Lev Walkin | f15320b | 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 | */ |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 223 | if(asn1f_compatible_with_exports(arg,imports_from,identifier)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 224 | errno = EPERM; |
| 225 | return NULL; |
| 226 | } |
| 227 | } else { |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 228 | /* Search inside the IMPORTS section of the current module */ |
Lev Walkin | 163db36 | 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) { |
Lev Walkin | f15320b | 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 | |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 240 | /* |
| 241 | * The symbol is being imported from another module. |
| 242 | */ |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 243 | importing: |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 244 | if(imports_from) { |
| 245 | asn1p_ref_t tmpref = *ref; |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 246 | asn1p_expr_t *expr; |
Lev Walkin | 163db36 | 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 | } |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 257 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 258 | expr = asn1f_lookup_symbol_impl(arg, imports_from, |
| 259 | rhs_pspecs, &tmpref, recursion_depth); |
Lev Walkin | dc4376d | 2006-03-16 11:04:55 +0000 | [diff] [blame] | 260 | if(!expr && !(arg->expr->_mark & TM_BROKEN) |
| 261 | && !(imports_from->_tags & MT_STANDARD_MODULE)) { |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 262 | 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", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 266 | imports_from->ModuleName, |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 267 | asn1f_printable_reference(ref), |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 268 | mod->ModuleName); |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 269 | } else { |
| 270 | FATAL("Module %s referred in IMPORTS section " |
| 271 | "for %s of module %s does not contain " |
| 272 | "the requested symbol", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 273 | imports_from->ModuleName, |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 274 | asn1f_printable_reference(ref), |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 275 | mod->ModuleName); |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | return expr; |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 279 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 280 | |
| 281 | /* |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 282 | * Now we know where to search for a value: in the current module. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 283 | */ |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 284 | TQ_FOR(ref_tc, &(mod->members), next) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 285 | if(ref_tc->Identifier) |
| 286 | if(strcmp(ref_tc->Identifier, identifier) == 0) |
| 287 | break; |
| 288 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 289 | 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 | } |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 313 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 314 | return ref_tc; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Not found in the current module. |
| 319 | * Search in our default standard module. |
| 320 | */ |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 321 | { |
| 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])); |
Lev Walkin | 97bdee2 | 2004-06-28 21:21:24 +0000 | [diff] [blame] | 341 | } |
Lev Walkin | 0c2619f | 2006-03-16 11:24:47 +0000 | [diff] [blame] | 342 | if(!imports_from && (!mod->module_oid |
| 343 | || asn1p_oid_compare(mod->module_oid, uioc_oid))) { |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 344 | imports_from = asn1f_lookup_module(arg, |
| 345 | "ASN1C-UsefulInformationObjectClasses", |
| 346 | uioc_oid); |
| 347 | if(imports_from) goto importing; |
| 348 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 351 | 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; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
| 367 | asn1p_expr_t * |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 368 | asn1f_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); |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | asn1p_expr_t * |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 374 | asn1f_find_terminal_type(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 375 | return asn1f_find_terminal_thing(arg, expr, FTT_TYPE); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | asn1p_expr_t * |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 379 | asn1f_find_terminal_value(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 380 | return asn1f_find_terminal_thing(arg, expr, FTT_VALUE); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static asn1p_expr_t * |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 384 | asn1f_find_terminal_thing(arg_t *arg, asn1p_expr_t *expr, enum ftt_what what) { |
Lev Walkin | 039a09e | 2005-03-20 13:17:48 +0000 | [diff] [blame] | 385 | asn1p_ref_t *ref = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 386 | asn1p_expr_t *tc; |
| 387 | |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 388 | 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: |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 396 | assert(expr->meta_type == AMT_VALUE); |
| 397 | assert(expr->value); |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 398 | /* Expression may be a terminal type itself */ |
| 399 | if(expr->value->type != ATV_REFERENCED) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 400 | return expr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 401 | ref = expr->value->value.reference; |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 402 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 405 | DEBUG("%s(%s->%s) for line %d", |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 406 | (what == FTT_VALUE)?"VALUE":"TYPE", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 407 | 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 | */ |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 415 | if(what == FTT_VALUE) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 416 | asn1p_expr_t *val_type_tc; |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 417 | val_type_tc = asn1f_find_terminal_type(arg, expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 418 | 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) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 422 | return expr; |
| 423 | } |
| 424 | assert(ref == expr->value->value.reference); |
| 425 | ref = expr->value->value.reference; |
| 426 | } |
| 427 | |
| 428 | /* |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 429 | * Lookup inside the default module and its IMPORTS section. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 430 | */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 431 | tc = asn1f_lookup_symbol(arg, expr->module, expr->rhs_pspecs, ref); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 432 | if(tc == NULL) { |
Lev Walkin | 97bdee2 | 2004-06-28 21:21:24 +0000 | [diff] [blame] | 433 | DEBUG("\tSymbol \"%s\" not found: %s", |
| 434 | asn1f_printable_reference(ref), |
| 435 | strerror(errno)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 436 | 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 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 449 | tc->_type_referenced = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 450 | tc->_mark |= TM_RECURSION; |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 451 | WITH_MODULE(tc->module, |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 452 | expr = asn1f_find_terminal_thing(arg, tc, what)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 453 | 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 | */ |
| 462 | static int |
| 463 | asn1f_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 | |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 481 | /* 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", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 486 | mod->ModuleName, mod->source_file_name, name, |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 487 | arg->expr->_lineno); |
| 488 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 489 | |
| 490 | errno = ESRCH; |
| 491 | return -1; |
| 492 | } |