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 */ |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 6 | FTT_CONSTR_TYPE /* Find the type of the given expression having constraint */ |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 7 | }; |
| 8 | |
| 9 | 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] | 10 | static int asn1f_compatible_with_exports(arg_t *arg, asn1p_module_t *mod, const char *name); |
| 11 | |
| 12 | |
| 13 | /* |
| 14 | * Lookup a child by its name. |
| 15 | */ |
| 16 | asn1p_expr_t * |
| 17 | asn1f_lookup_child(asn1p_expr_t *tc, const char *name) { |
| 18 | asn1p_expr_t *child_tc; |
| 19 | |
| 20 | TQ_FOR(child_tc, &(tc->members), next) { |
| 21 | if(child_tc->Identifier |
| 22 | && strcmp(child_tc->Identifier, name) == 0) { |
| 23 | return child_tc; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | errno = ESRCH; |
| 28 | return NULL; |
| 29 | } |
| 30 | |
| 31 | asn1p_module_t * |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 32 | 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] | 33 | asn1p_xports_t *xp; |
| 34 | asn1p_expr_t *tc; |
| 35 | |
| 36 | /* |
| 37 | * Search in which exactly module this name is defined. |
| 38 | */ |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 39 | TQ_FOR(xp, &(mod->imports), xp_next) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | TQ_FOR(tc, &(xp->members), next) { |
| 41 | if(strcmp(name, tc->Identifier) == 0) |
| 42 | break; |
| 43 | } |
| 44 | if(tc) break; |
| 45 | } |
| 46 | if(xp == NULL) { |
| 47 | errno = ESRCH; |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Okay, right now we have a module name and, hopefully, an OID. |
| 53 | * Search the arg->asn for the specified module. |
| 54 | */ |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 55 | mod = asn1f_lookup_module(arg, xp->fromModuleName, xp->identifier.oid); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | if(mod == NULL) { |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 57 | /* Conditional debug */ |
| 58 | if(!(arg->expr->_mark & TM_BROKEN)) { |
| 59 | arg->expr->_mark |= TM_BROKEN; |
Lev Walkin | 9e366ba | 2005-04-12 17:46:30 +0000 | [diff] [blame] | 60 | FATAL("Cannot find external module \"%s\" " |
| 61 | "mentioned for " |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 62 | "\"%s\" at line %d. " |
| 63 | "Obtain this module and instruct compiler to process it too.", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 64 | xp->fromModuleName, name, arg->expr->_lineno); |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 65 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 66 | /* ENOENT/ETOOMANYREFS */ |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Check that the EXPORTS section of this module contains |
| 72 | * the symbol we care about, or it is EXPORTS ALL. |
| 73 | */ |
| 74 | if(asn1f_compatible_with_exports(arg, mod, name)) { |
| 75 | errno = EPERM; |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | return mod; |
| 80 | } |
| 81 | |
| 82 | asn1p_module_t * |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 83 | asn1f_lookup_module(arg_t *arg, const char *module_name, const asn1p_oid_t *oid) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | asn1p_module_t *mod; |
| 85 | |
| 86 | assert(module_name); |
| 87 | |
| 88 | /* |
| 89 | * If OID is given, the module_name is unused. |
| 90 | * If OID is not given, the module_name may mean |
| 91 | * either the real module's name, or the symbol which is the |
| 92 | * result of renaming. Check this first. |
| 93 | */ |
| 94 | if(oid == 0) { |
| 95 | asn1p_xports_t *xp; |
| 96 | /* |
| 97 | * Check inside the IMPORTS section for possible renaming. |
| 98 | * Renaming practically means that a module_name is mentioned |
| 99 | * somewhere in the IMPORTS section AND OID is given. |
| 100 | */ |
| 101 | TQ_FOR(xp, &(arg->mod->imports), xp_next) { |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 102 | if(strcmp(module_name, xp->fromModuleName)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 103 | continue; |
| 104 | if(oid) { |
| 105 | FATAL("Ambiguous reference: " |
| 106 | "%s " |
| 107 | "matches several modules", |
| 108 | module_name); |
| 109 | errno = ETOOMANYREFS; |
| 110 | return NULL; |
| 111 | } |
| 112 | /* |
| 113 | * Yes, there is a renaming. |
| 114 | * Make lookup use OID instead. |
| 115 | */ |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 116 | oid = xp->identifier.oid; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Perform lookup using OID or module_name. |
| 122 | */ |
| 123 | TQ_FOR(mod, &(arg->asn->modules), mod_next) { |
| 124 | if(oid) { |
| 125 | if(mod->module_oid) { |
| 126 | if(asn1p_oid_compare(oid, |
| 127 | mod->module_oid)) { |
| 128 | continue; |
| 129 | } else { |
| 130 | /* Match! Even if name doesn't. */ |
| 131 | return mod; |
| 132 | } |
| 133 | } else { |
| 134 | /* Not match, even if name is the same. */ |
| 135 | continue; |
| 136 | } |
| 137 | } |
| 138 | |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 139 | if(strcmp(module_name, mod->ModuleName) == 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 140 | return mod; |
| 141 | } |
| 142 | |
| 143 | DEBUG("\tModule \"%s\" not found", module_name); |
| 144 | |
| 145 | errno = ENOENT; |
| 146 | return NULL; |
| 147 | } |
| 148 | |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 149 | static asn1p_expr_t * |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 150 | asn1f_lookup_symbol_impl(arg_t *arg, asn1p_expr_t *rhs_pspecs, const asn1p_ref_t *ref, int recursion_depth) { |
| 151 | asn1_namespace_t *initial_namespace = arg->ns; |
| 152 | asn1p_ref_t tmp_ref; |
Lev Walkin | 163db36 | 2004-09-15 11:46:47 +0000 | [diff] [blame] | 153 | asn1p_module_t *imports_from; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 154 | char *modulename; |
| 155 | char *identifier; |
| 156 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 157 | if(ref->module && arg->mod != ref->module) { |
| 158 | return WITH_MODULE( |
| 159 | ref->module, |
| 160 | asn1f_lookup_symbol_impl(arg, rhs_pspecs, ref, recursion_depth)); |
| 161 | } |
| 162 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 163 | /* |
| 164 | * First of all, a reference to a symbol may be specified |
| 165 | * using several possible forms: |
| 166 | * a) simple identifier |
| 167 | * v INTEGER ::= value |
| 168 | * b) external reference |
| 169 | * v INTEGER ::= Module1.value |
| 170 | * c) class-related stuff (the most complex stuff) |
| 171 | * v ::= <[A-Z][A-Z0-9a-z-]*>.&<[A-Z0-9a-z-]+>. |
| 172 | * All other forms are not implemented at this moment. |
| 173 | */ |
| 174 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 175 | DEBUG("Lookup (%s) in %s for line %d", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | asn1f_printable_reference(ref), |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 177 | asn1_namespace_string(initial_namespace), |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | ref->_lineno); |
| 179 | |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 180 | if(recursion_depth++ > 30 /* Arbitrary constant */) { |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 181 | FATAL( |
| 182 | "Excessive circular referencing detected in namespace %s for %s at " |
| 183 | "line %d", |
| 184 | asn1_namespace_string(initial_namespace), |
| 185 | asn1f_printable_reference(ref), ref->_lineno); |
| 186 | errno = ETOOMANYREFS; |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
| 189 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 190 | if(ref->comp_count == 1) { |
| 191 | modulename = NULL; |
| 192 | identifier = ref->components[0].name; |
| 193 | } else if(ref->comp_count == 2 |
| 194 | && ref->components[1].name[0] != '&') { |
| 195 | modulename = ref->components[0].name; |
| 196 | identifier = ref->components[1].name; |
| 197 | } else if(ref->comp_count > 1 |
| 198 | && isupper(ref->components[0].name[0]) |
| 199 | && ref->components[1].name[0] == '&') { |
| 200 | asn1p_expr_t *extract; |
| 201 | /* |
| 202 | * This is a reference to a CLASS-related stuff. |
| 203 | * Employ a separate function for that. |
| 204 | */ |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 205 | extract = asn1f_class_access(arg, rhs_pspecs, ref); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | |
| 207 | return extract; |
| 208 | } else { |
| 209 | DEBUG("\tToo many components: %d", ref->comp_count); |
| 210 | errno = EINVAL; |
| 211 | return NULL; |
| 212 | } |
| 213 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 214 | if(modulename) { |
| 215 | /* |
| 216 | * The modulename is specified inside this reference. |
| 217 | * To avoid recursion, reformat the reference |
| 218 | * as it were local to that module. |
| 219 | */ |
| 220 | tmp_ref = *ref; |
| 221 | tmp_ref.components++; /* Hide the first element */ |
| 222 | tmp_ref.comp_count--; |
| 223 | assert(tmp_ref.comp_count > 0); |
| 224 | ref = &tmp_ref; |
| 225 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 226 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 227 | asn1_namespace_t *my_namespace = initial_namespace; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 228 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 229 | #define DISPOSE_OF_MY_NAMESPACE() \ |
| 230 | do { \ |
| 231 | int tmp_errno = errno; \ |
| 232 | if(my_namespace != initial_namespace) { \ |
| 233 | asn1_namespace_free(my_namespace); \ |
| 234 | my_namespace = NULL; \ |
| 235 | } \ |
| 236 | errno = tmp_errno; \ |
| 237 | } while(0) |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 238 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 239 | /* |
| 240 | * If module name is specified explicitly |
| 241 | * OR the current module's IMPORTS clause contains the identifier, |
| 242 | * switch namespace to that module. |
| 243 | */ |
| 244 | if(modulename) { |
| 245 | imports_from = asn1f_lookup_module(arg, modulename, 0); |
| 246 | if(imports_from == NULL) { |
| 247 | FATAL( |
| 248 | "Module \"%s\" " |
| 249 | "mentioned at line %d is not found", |
| 250 | modulename, ref->_lineno); |
| 251 | return NULL; |
| 252 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 253 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 254 | /* |
| 255 | * Check that the EXPORTS section of this module contains |
| 256 | * the symbol we care about, or it is EXPORTS ALL. |
| 257 | */ |
| 258 | if(asn1f_compatible_with_exports(arg, imports_from, identifier)) { |
| 259 | errno = EPERM; |
| 260 | return NULL; |
| 261 | } |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 262 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 263 | my_namespace = asn1_namespace_new_from_module(imports_from, 1); |
| 264 | DEBUG("Lookup (%s) in %s for line %d", asn1f_printable_reference(ref), |
| 265 | asn1_namespace_string(my_namespace), ref->_lineno); |
| 266 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 267 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 268 | size_t namespace_switches = 0; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 269 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 270 | /* |
| 271 | * Search through all layers of the namespace. |
| 272 | */ |
| 273 | for(ssize_t ns_item = my_namespace->elements_count - 1; ns_item >= 0; |
| 274 | ns_item--) { |
| 275 | struct asn1_namespace_element_s *ns_el = |
| 276 | &my_namespace->elements[ns_item]; |
| 277 | asn1p_expr_t *ref_tc; /* Referenced tc */ |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 278 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 279 | switch(ns_el->selector) { |
| 280 | case NAM_SYMBOL: |
| 281 | if(modulename) { |
| 282 | /* |
| 283 | * Trying to match a fully specified "Module.Symbol" |
| 284 | * against the "Symbol" parameter. Doesn't match. |
| 285 | */ |
| 286 | continue; |
| 287 | } |
| 288 | if(strcmp(ns_el->u.symbol.identifier, identifier) != 0) { |
| 289 | continue; |
| 290 | } else { |
| 291 | DEBUG("Lookup (%s) in %s for line %d => found as parameter", |
| 292 | asn1f_printable_reference(ref), |
| 293 | asn1_namespace_string(my_namespace), ref->_lineno); |
| 294 | DISPOSE_OF_MY_NAMESPACE(); |
| 295 | return ns_el->u.symbol.resolution; |
| 296 | } |
| 297 | case NAM_SPACE: |
| 298 | /* |
| 299 | * Do a direct symbol search in the given module. |
| 300 | */ |
| 301 | TQ_FOR(ref_tc, &(ns_el->u.space.module->members), next) { |
| 302 | if(ref_tc->Identifier) |
| 303 | if(strcmp(ref_tc->Identifier, identifier) == 0) break; |
| 304 | } |
| 305 | if(ref_tc) { |
| 306 | /* It is acceptable that we don't use input parameters */ |
| 307 | if(rhs_pspecs && !ref_tc->lhs_params) { |
| 308 | WARNING( |
| 309 | "Parameterized type %s expected " |
| 310 | "for %s at line %d", |
| 311 | ref_tc->Identifier, asn1f_printable_reference(ref), |
| 312 | ref->_lineno); |
| 313 | } |
| 314 | if(!rhs_pspecs && ref_tc->lhs_params) { |
| 315 | FATAL( |
| 316 | "Type %s expects specialization " |
| 317 | "from %s at line %d", |
| 318 | ref_tc->Identifier, asn1f_printable_reference(ref), |
| 319 | ref->_lineno); |
| 320 | errno = EPERM; |
| 321 | return NULL; |
| 322 | } |
| 323 | if(rhs_pspecs && ref_tc->lhs_params) { |
| 324 | /* Specialize the target */ |
| 325 | ref_tc = |
| 326 | asn1f_parameterization_fork(arg, ref_tc, rhs_pspecs); |
| 327 | } |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 328 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 329 | DISPOSE_OF_MY_NAMESPACE(); |
| 330 | return ref_tc; |
| 331 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 332 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 333 | /* |
| 334 | if(!expr && !(arg->expr->_mark & TM_BROKEN) |
| 335 | && !(imports_from->_tags & MT_STANDARD_MODULE)) { |
| 336 | arg->expr->_mark |= TM_BROKEN; |
| 337 | if(modulename) { |
| 338 | } else { |
| 339 | FATAL( |
| 340 | "Module %s referred in IMPORTS section " |
| 341 | "for %s of module %s does not contain " |
| 342 | "the requested symbol", |
| 343 | imports_from->ModuleName, |
| 344 | asn1f_printable_reference(ref), mod->ModuleName); |
| 345 | } |
| 346 | } |
| 347 | */ |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 348 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 349 | /* Search inside the IMPORTS section of the given module */ |
| 350 | imports_from = |
| 351 | asn1f_lookup_in_imports(arg, ns_el->u.space.module, identifier); |
| 352 | if(imports_from) { |
| 353 | |
| 354 | if(namespace_switches++ > 10 /* Arbitrary constant */) { |
| 355 | FATAL( |
| 356 | "Excessive circular referencing detected in namespace " |
| 357 | "%s for %s at " |
| 358 | "line %d", |
| 359 | asn1_namespace_string(arg->ns), |
| 360 | asn1f_printable_reference(ref), ref->_lineno); |
| 361 | errno = ETOOMANYREFS; |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | /* Switch namespace */ |
| 366 | DISPOSE_OF_MY_NAMESPACE(); |
| 367 | my_namespace = asn1_namespace_new_from_module(imports_from, 1); |
| 368 | DEBUG("Lookup (%s) in %s for line %d", |
| 369 | asn1f_printable_reference(ref), |
| 370 | asn1_namespace_string(my_namespace), ref->_lineno); |
| 371 | ns_item = my_namespace->elements_count; |
| 372 | |
| 373 | continue; |
| 374 | } else if(errno != ESRCH) { |
| 375 | /* |
| 376 | * Return only if the name was not found. |
| 377 | * If module was not found or more serious error |
| 378 | * encountered, just return preserving the errno. |
| 379 | */ |
| 380 | DISPOSE_OF_MY_NAMESPACE(); |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | if(modulename) { |
| 385 | FATAL( |
| 386 | "Module %s referred by %s in module %s " |
| 387 | "does not contain the requested symbol", |
| 388 | ns_el->u.space.module->ModuleName, |
| 389 | asn1f_printable_reference(ref), modulename); |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | /* Search failed in the given namespace */ |
| 394 | continue; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | DEBUG( |
| 399 | "Namespace %s does not contain \"%s\" " |
| 400 | "mentioned at line %d: %s", |
| 401 | asn1_namespace_string(my_namespace), identifier, ref->_lineno, |
| 402 | strerror(errno)); |
| 403 | |
| 404 | DISPOSE_OF_MY_NAMESPACE(); |
| 405 | |
| 406 | if(asn1f_check_known_external_type(identifier) == 0) { |
Lev Walkin | 6b3ff54 | 2006-03-06 14:51:00 +0000 | [diff] [blame] | 407 | errno = EEXIST; /* Exists somewhere */ |
| 408 | } else { |
| 409 | errno = ESRCH; |
| 410 | } |
| 411 | return NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
| 415 | asn1p_expr_t * |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 416 | asn1f_lookup_symbol(arg_t *arg, asn1p_expr_t *rhs_pspecs, |
| 417 | const asn1p_ref_t *ref) { |
| 418 | return asn1f_lookup_symbol_impl(arg, rhs_pspecs, ref, 0); |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | asn1p_expr_t * |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 422 | asn1f_find_terminal_type(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 423 | return asn1f_find_terminal_thing(arg, expr, FTT_TYPE); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | asn1p_expr_t * |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 427 | asn1f_find_terminal_value(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 428 | return asn1f_find_terminal_thing(arg, expr, FTT_VALUE); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 431 | asn1p_expr_t * |
| 432 | asn1f_find_ancestor_type_with_PER_constraint(arg_t *arg, asn1p_expr_t *expr) { |
| 433 | return asn1f_find_terminal_thing(arg, expr, FTT_CONSTR_TYPE); |
| 434 | } |
| 435 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 436 | static asn1p_expr_t * |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 437 | 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] | 438 | asn1p_ref_t *ref = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 439 | asn1p_expr_t *tc; |
| 440 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 441 | if(arg->mod != expr->module) { |
| 442 | return WITH_MODULE(expr->module, |
| 443 | asn1f_find_terminal_thing(arg, expr, what)); |
| 444 | } |
| 445 | |
| 446 | switch(what) { |
| 447 | case FTT_TYPE: |
| 448 | case FTT_CONSTR_TYPE: |
| 449 | /* Expression may be a terminal type itself */ |
| 450 | if(expr->expr_type != A1TC_REFERENCE) { |
| 451 | return expr; |
| 452 | } |
| 453 | ref = expr->reference; |
| 454 | break; |
| 455 | case FTT_VALUE: |
| 456 | assert(expr->meta_type == AMT_VALUE); |
| 457 | assert(expr->value); |
| 458 | /* Expression may be a terminal type itself */ |
| 459 | if(expr->value->type != ATV_REFERENCED) { |
| 460 | return expr; |
| 461 | } |
| 462 | ref = expr->value->value.reference; |
| 463 | break; |
| 464 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 465 | |
Lev Walkin | 0385018 | 2005-03-10 10:02:50 +0000 | [diff] [blame] | 466 | DEBUG("%s(%s->%s) for line %d", |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 467 | (what == FTT_VALUE)?"VALUE":"TYPE", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 468 | expr->Identifier, asn1f_printable_reference(ref), |
| 469 | expr->_lineno); |
| 470 | |
| 471 | assert(ref); |
| 472 | |
| 473 | /* |
| 474 | * Lookup inside the type itself (ENUMERATED, INTEGER, etc). |
| 475 | */ |
Lev Walkin | 33f63f8 | 2005-03-20 11:27:19 +0000 | [diff] [blame] | 476 | if(what == FTT_VALUE) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 477 | asn1p_expr_t *val_type_tc; |
Lev Walkin | 6fec44d | 2004-08-22 03:10:23 +0000 | [diff] [blame] | 478 | val_type_tc = asn1f_find_terminal_type(arg, expr); |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 479 | if(val_type_tc |
| 480 | && WITH_MODULE(val_type_tc->module, |
| 481 | asn1f_look_value_in_type(arg, val_type_tc, expr))) { |
| 482 | return expr; |
| 483 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 484 | if(expr->value->type != ATV_REFERENCED) { |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 485 | return expr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 486 | } |
| 487 | assert(ref == expr->value->value.reference); |
| 488 | ref = expr->value->value.reference; |
| 489 | } |
| 490 | |
| 491 | /* |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 492 | * Lookup inside the default module and its IMPORTS section. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 493 | */ |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 494 | tc = asn1f_lookup_symbol(arg, expr->rhs_pspecs, ref); |
| 495 | if(tc == NULL) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 496 | /* |
| 497 | * Lookup inside the ref's module and its IMPORTS section. |
| 498 | */ |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 499 | tc = WITH_MODULE(ref->module, |
| 500 | asn1f_lookup_symbol(arg, expr->rhs_pspecs, ref)); |
| 501 | if(tc == NULL) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 502 | DEBUG("\tSymbol \"%s\" not found: %s", |
| 503 | asn1f_printable_reference(ref), |
| 504 | strerror(errno)); |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 505 | return NULL; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 506 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Recursive loops detection. |
| 511 | */ |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 512 | if(tc->_mark & TM_RECURSION) { |
| 513 | DEBUG("Recursion loop detected for %s at line %d", |
| 514 | asn1f_printable_reference(ref), ref->_lineno); |
| 515 | errno = EPERM; |
| 516 | return NULL; |
| 517 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 518 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 519 | tc->_type_referenced = 1; |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 520 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 521 | if((what == FTT_CONSTR_TYPE) && (tc->constraints)) { |
| 522 | return tc; |
| 523 | } |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 524 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 525 | tc->_mark |= TM_RECURSION; |
| 526 | expr = WITH_MODULE(tc->module, asn1f_find_terminal_thing(arg, tc, what)); |
| 527 | tc->_mark &= ~TM_RECURSION; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 528 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 529 | return expr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 532 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 533 | /* |
| 534 | * Make sure that the specified name is present or otherwise does |
| 535 | * not contradict with the EXPORTS clause of the specified module. |
| 536 | */ |
| 537 | static int |
| 538 | asn1f_compatible_with_exports(arg_t *arg, asn1p_module_t *mod, const char *name) { |
| 539 | asn1p_xports_t *exports; |
| 540 | asn1p_expr_t *item; |
| 541 | |
| 542 | assert(mod); |
| 543 | assert(name); |
| 544 | |
| 545 | exports = TQ_FIRST(&(mod->exports)); |
| 546 | if(exports == NULL) { |
| 547 | /* No EXPORTS section or EXPORTS ALL; */ |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | TQ_FOR(item, &(exports->members), next) { |
| 552 | if(strcmp(item->Identifier, name) == 0) |
| 553 | return 0; |
| 554 | } |
| 555 | |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 556 | /* Conditional debug */ |
| 557 | if(!(arg->expr->_mark & TM_BROKEN)) { |
| 558 | arg->expr->_mark |= TM_BROKEN; |
| 559 | FATAL("EXPORTS section of module %s in %s " |
| 560 | "does not mention %s at line %d", |
Lev Walkin | b36317c | 2005-08-12 10:09:10 +0000 | [diff] [blame] | 561 | mod->ModuleName, mod->source_file_name, name, |
Lev Walkin | d809552 | 2005-03-18 05:20:39 +0000 | [diff] [blame] | 562 | arg->expr->_lineno); |
| 563 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 564 | |
| 565 | errno = ESRCH; |
| 566 | return -1; |
| 567 | } |