Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include "common.h" |
| 16 | |
| 17 | #include <ctype.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <stdio.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 19 | |
| 20 | #include "libyang.h" |
| 21 | #include "context.h" |
| 22 | #include "tree_schema_internal.h" |
| 23 | #include "xpath.h" |
| 24 | |
| 25 | /** |
| 26 | * @brief Duplicate string into dictionary |
| 27 | * @param[in] CTX libyang context of the dictionary. |
| 28 | * @param[in] ORIG String to duplicate. |
| 29 | * @param[out] DUP Where to store the result. |
| 30 | */ |
| 31 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 32 | |
| 33 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \ |
| 34 | if (ARRAY_P) { \ |
| 35 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 36 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 37 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 38 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 39 | RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER + __array_offset]); \ |
| 40 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 41 | } \ |
| 42 | } |
| 43 | |
| 44 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \ |
| 45 | if (ARRAY_P) { \ |
| 46 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 47 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 48 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 49 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
| 50 | RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 51 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 52 | } \ |
| 53 | } |
| 54 | |
| 55 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \ |
| 56 | if (MEMBER_P) { \ |
| 57 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 58 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
| 59 | RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \ |
| 60 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 61 | } |
| 62 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 63 | #define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 64 | if (ARRAY) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 65 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 66 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 67 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 68 | return LY_EVALID; \ |
| 69 | } \ |
| 70 | } \ |
| 71 | } |
| 72 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 73 | static struct lysc_ext_instance * |
| 74 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 75 | { |
| 76 | /* TODO */ |
| 77 | (void) ctx; |
| 78 | (void) orig; |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | static struct lysc_pattern* |
| 83 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 84 | { |
| 85 | ++orig->refcount; |
| 86 | return orig; |
| 87 | } |
| 88 | |
| 89 | static struct lysc_pattern** |
| 90 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 91 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 92 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 93 | unsigned int u; |
| 94 | |
| 95 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 96 | LY_ARRAY_FOR(orig, u) { |
| 97 | dup[u] = lysc_pattern_dup(orig[u]); |
| 98 | LY_ARRAY_INCREMENT(dup); |
| 99 | } |
| 100 | return dup; |
| 101 | } |
| 102 | |
| 103 | struct lysc_range* |
| 104 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 105 | { |
| 106 | struct lysc_range *dup; |
| 107 | LY_ERR ret; |
| 108 | |
| 109 | dup = calloc(1, sizeof *dup); |
| 110 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 111 | if (orig->parts) { |
| 112 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 113 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 114 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 115 | } |
| 116 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 117 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 118 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 119 | |
| 120 | return dup; |
| 121 | cleanup: |
| 122 | free(dup); |
| 123 | (void) ret; /* set but not used due to the return type */ |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | struct iff_stack { |
| 128 | int size; |
| 129 | int index; /* first empty item */ |
| 130 | uint8_t *stack; |
| 131 | }; |
| 132 | |
| 133 | static LY_ERR |
| 134 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 135 | { |
| 136 | if (stack->index == stack->size) { |
| 137 | stack->size += 4; |
| 138 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 139 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 140 | } |
| 141 | stack->stack[stack->index++] = value; |
| 142 | return LY_SUCCESS; |
| 143 | } |
| 144 | |
| 145 | static uint8_t |
| 146 | iff_stack_pop(struct iff_stack *stack) |
| 147 | { |
| 148 | stack->index--; |
| 149 | return stack->stack[stack->index]; |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | iff_stack_clean(struct iff_stack *stack) |
| 154 | { |
| 155 | stack->size = 0; |
| 156 | free(stack->stack); |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 161 | { |
| 162 | uint8_t *item; |
| 163 | uint8_t mask = 3; |
| 164 | |
| 165 | assert(pos >= 0); |
| 166 | assert(op <= 3); /* max 2 bits */ |
| 167 | |
| 168 | item = &list[pos / 4]; |
| 169 | mask = mask << 2 * (pos % 4); |
| 170 | *item = (*item) & ~mask; |
| 171 | *item = (*item) | (op << 2 * (pos % 4)); |
| 172 | } |
| 173 | |
| 174 | #define LYS_IFF_LP 0x04 /* ( */ |
| 175 | #define LYS_IFF_RP 0x08 /* ) */ |
| 176 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 177 | /** |
| 178 | * @brief Find a feature of the given name and referenced in the given module. |
| 179 | * |
| 180 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 181 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 182 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 183 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 184 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 185 | * assigned till that time will be still valid. |
| 186 | * |
| 187 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 188 | * @param[in] name Name of the feature including possible prefix. |
| 189 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 190 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 191 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 192 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 193 | lys_feature_find(struct lys_module *mod, const char *name, size_t len) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 194 | { |
| 195 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 196 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 197 | |
| 198 | for (i = 0; i < len; ++i) { |
| 199 | if (name[i] == ':') { |
| 200 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 201 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 202 | LY_CHECK_RET(!mod, NULL); |
| 203 | |
| 204 | name = &name[i + 1]; |
| 205 | len = len - i - 1; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 210 | if (mod->implemented) { |
| 211 | /* module is implemented so there is already the compiled schema */ |
| 212 | flist = mod->compiled->features; |
| 213 | } else { |
| 214 | flist = mod->off_features; |
| 215 | } |
| 216 | LY_ARRAY_FOR(flist, i) { |
| 217 | f = &flist[i]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 218 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 219 | return f; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | static LY_ERR |
| 227 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext) |
| 228 | { |
| 229 | const char *name; |
| 230 | unsigned int u; |
| 231 | const struct lys_module *mod; |
| 232 | struct lysp_ext *edef = NULL; |
| 233 | |
| 234 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 235 | ext->insubstmt = ext_p->insubstmt; |
| 236 | ext->insubstmt_index = ext_p->insubstmt_index; |
| 237 | |
| 238 | /* get module where the extension definition should be placed */ |
| 239 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 240 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 241 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 242 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 243 | LY_EVALID); |
| 244 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 245 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 246 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 247 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 248 | LY_EVALID); |
| 249 | name = &ext_p->name[u + 1]; |
| 250 | /* find the extension definition there */ |
| 251 | for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) { |
| 252 | if (!strcmp(name, mod->parsed->extensions[u].name)) { |
| 253 | edef = &mod->parsed->extensions[u]; |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 258 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
| 259 | LY_EVALID); |
| 260 | /* TODO plugins */ |
| 261 | |
| 262 | return LY_SUCCESS; |
| 263 | } |
| 264 | |
| 265 | static LY_ERR |
| 266 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff) |
| 267 | { |
| 268 | const char *c = *value; |
| 269 | int r, rc = EXIT_FAILURE; |
| 270 | int i, j, last_not, checkversion = 0; |
| 271 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 272 | uint8_t op; |
| 273 | struct iff_stack stack = {0, 0, NULL}; |
| 274 | struct lysc_feature *f; |
| 275 | |
| 276 | assert(c); |
| 277 | |
| 278 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 279 | for (i = j = last_not = 0; c[i]; i++) { |
| 280 | if (c[i] == '(') { |
| 281 | j++; |
| 282 | checkversion = 1; |
| 283 | continue; |
| 284 | } else if (c[i] == ')') { |
| 285 | j--; |
| 286 | continue; |
| 287 | } else if (isspace(c[i])) { |
| 288 | checkversion = 1; |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 293 | if (c[i + r] == '\0') { |
| 294 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 295 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 296 | return LY_EVALID; |
| 297 | } else if (!isspace(c[i + r])) { |
| 298 | /* feature name starting with the not/and/or */ |
| 299 | last_not = 0; |
| 300 | f_size++; |
| 301 | } else if (c[i] == 'n') { /* not operation */ |
| 302 | if (last_not) { |
| 303 | /* double not */ |
| 304 | expr_size = expr_size - 2; |
| 305 | last_not = 0; |
| 306 | } else { |
| 307 | last_not = 1; |
| 308 | } |
| 309 | } else { /* and, or */ |
| 310 | f_exp++; |
| 311 | /* not a not operation */ |
| 312 | last_not = 0; |
| 313 | } |
| 314 | i += r; |
| 315 | } else { |
| 316 | f_size++; |
| 317 | last_not = 0; |
| 318 | } |
| 319 | expr_size++; |
| 320 | |
| 321 | while (!isspace(c[i])) { |
| 322 | if (!c[i] || c[i] == ')') { |
| 323 | i--; |
| 324 | break; |
| 325 | } |
| 326 | i++; |
| 327 | } |
| 328 | } |
| 329 | if (j || f_exp != f_size) { |
| 330 | /* not matching count of ( and ) */ |
| 331 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 332 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 333 | return LY_EVALID; |
| 334 | } |
| 335 | |
| 336 | if (checkversion || expr_size > 1) { |
| 337 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 338 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 339 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 340 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 341 | return LY_EVALID; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* allocate the memory */ |
| 346 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 347 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 348 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 349 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 350 | |
| 351 | stack.size = expr_size; |
| 352 | f_size--; expr_size--; /* used as indexes from now */ |
| 353 | |
| 354 | for (i--; i >= 0; i--) { |
| 355 | if (c[i] == ')') { |
| 356 | /* push it on stack */ |
| 357 | iff_stack_push(&stack, LYS_IFF_RP); |
| 358 | continue; |
| 359 | } else if (c[i] == '(') { |
| 360 | /* pop from the stack into result all operators until ) */ |
| 361 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 362 | iff_setop(iff->expr, op, expr_size--); |
| 363 | } |
| 364 | continue; |
| 365 | } else if (isspace(c[i])) { |
| 366 | continue; |
| 367 | } |
| 368 | |
| 369 | /* end of operator or operand -> find beginning and get what is it */ |
| 370 | j = i + 1; |
| 371 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 372 | i--; |
| 373 | } |
| 374 | i++; /* go back by one step */ |
| 375 | |
| 376 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 377 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 378 | /* double not */ |
| 379 | iff_stack_pop(&stack); |
| 380 | } else { |
| 381 | /* not has the highest priority, so do not pop from the stack |
| 382 | * as in case of AND and OR */ |
| 383 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 384 | } |
| 385 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 386 | /* as for OR - pop from the stack all operators with the same or higher |
| 387 | * priority and store them to the result, then push the AND to the stack */ |
| 388 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 389 | op = iff_stack_pop(&stack); |
| 390 | iff_setop(iff->expr, op, expr_size--); |
| 391 | } |
| 392 | iff_stack_push(&stack, LYS_IFF_AND); |
| 393 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 394 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 395 | op = iff_stack_pop(&stack); |
| 396 | iff_setop(iff->expr, op, expr_size--); |
| 397 | } |
| 398 | iff_stack_push(&stack, LYS_IFF_OR); |
| 399 | } else { |
| 400 | /* feature name, length is j - i */ |
| 401 | |
| 402 | /* add it to the expression */ |
| 403 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 404 | |
| 405 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 406 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 407 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 408 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 409 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 410 | iff->features[f_size] = f; |
| 411 | LY_ARRAY_INCREMENT(iff->features); |
| 412 | f_size--; |
| 413 | } |
| 414 | } |
| 415 | while (stack.index) { |
| 416 | op = iff_stack_pop(&stack); |
| 417 | iff_setop(iff->expr, op, expr_size--); |
| 418 | } |
| 419 | |
| 420 | if (++expr_size || ++f_size) { |
| 421 | /* not all expected operators and operands found */ |
| 422 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 423 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 424 | rc = LY_EINT; |
| 425 | } else { |
| 426 | rc = LY_SUCCESS; |
| 427 | } |
| 428 | |
| 429 | error: |
| 430 | /* cleanup */ |
| 431 | iff_stack_clean(&stack); |
| 432 | |
| 433 | return rc; |
| 434 | } |
| 435 | |
| 436 | static LY_ERR |
| 437 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when *when) |
| 438 | { |
| 439 | unsigned int u; |
| 440 | LY_ERR ret = LY_SUCCESS; |
| 441 | |
| 442 | when->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 443 | DUP_STRING(ctx->ctx, when_p->dsc, when->dsc); |
| 444 | DUP_STRING(ctx->ctx, when_p->ref, when->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 445 | LY_CHECK_ERR_GOTO(!when->cond, ret = ly_errcode(ctx->ctx), done); |
| 446 | COMPILE_ARRAY_GOTO(ctx, when_p->exts, when->exts, options, u, lys_compile_ext, ret, done); |
| 447 | |
| 448 | done: |
| 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | static LY_ERR |
| 453 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must) |
| 454 | { |
| 455 | unsigned int u; |
| 456 | LY_ERR ret = LY_SUCCESS; |
| 457 | |
| 458 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 459 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
| 460 | |
| 461 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 462 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 463 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 464 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 465 | COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done); |
| 466 | |
| 467 | done: |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | static LY_ERR |
| 472 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp) |
| 473 | { |
| 474 | unsigned int u; |
| 475 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 476 | LY_ERR ret = LY_SUCCESS; |
| 477 | |
| 478 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
| 479 | COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done); |
| 480 | imp->module = imp_p->module; |
| 481 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 482 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 483 | * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added, |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 484 | * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 485 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 486 | /* try to use filepath if present */ |
| 487 | if (imp->module->filepath) { |
| 488 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 489 | !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 490 | if (mod != imp->module) { |
| 491 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 492 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 493 | mod = NULL; |
| 494 | } |
| 495 | } |
| 496 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 497 | if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 498 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 499 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 500 | return LY_ENOTFOUND; |
| 501 | } |
| 502 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | done: |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | static LY_ERR |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 510 | lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *idents, struct lysc_ident *ident) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 511 | { |
| 512 | unsigned int u; |
| 513 | LY_ERR ret = LY_SUCCESS; |
| 514 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 515 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 516 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 517 | DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc); |
| 518 | DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 519 | COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 520 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
| 521 | COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done); |
| 522 | ident->flags = ident_p->flags; |
| 523 | |
| 524 | done: |
| 525 | return ret; |
| 526 | } |
| 527 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 528 | /** |
| 529 | * @brief Find and process the referenced base identities from another identity or identityref |
| 530 | * |
| 531 | * For bases in identity se backlinks to them from the base identities. For identityref, store |
| 532 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 533 | * to distinguish these two use cases. |
| 534 | * |
| 535 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 536 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 537 | * @param[in] ident Referencing identity to work with. |
| 538 | * @param[in] bases Array of bases of identityref to fill in. |
| 539 | * @return LY_ERR value. |
| 540 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 541 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 542 | lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 543 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 544 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 545 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 546 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 547 | struct lysc_ident **idref; |
| 548 | |
| 549 | assert(ident || bases); |
| 550 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 551 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 552 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 553 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 554 | return LY_EVALID; |
| 555 | } |
| 556 | |
| 557 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 558 | s = strchr(bases_p[u], ':'); |
| 559 | if (s) { |
| 560 | /* prefixed identity */ |
| 561 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 562 | mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 563 | } else { |
| 564 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 565 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 566 | } |
| 567 | if (!mod) { |
| 568 | if (ident) { |
| 569 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 570 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 571 | } else { |
| 572 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 573 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 574 | } |
| 575 | return LY_EVALID; |
| 576 | } |
| 577 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 578 | if (mod->compiled && mod->compiled->identities) { |
| 579 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 580 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 581 | if (ident) { |
| 582 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 583 | LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 584 | *idref = ident; |
| 585 | } else { |
| 586 | /* we have match! store the found identity */ |
| 587 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 588 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 589 | } |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | if (!idref || !(*idref)) { |
| 595 | if (ident) { |
| 596 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 597 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 598 | } else { |
| 599 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 600 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 601 | } |
| 602 | return LY_EVALID; |
| 603 | } |
| 604 | } |
| 605 | return LY_SUCCESS; |
| 606 | } |
| 607 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 608 | /** |
| 609 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 610 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 611 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 612 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 613 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 614 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 615 | static LY_ERR |
| 616 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 617 | { |
| 618 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 619 | |
| 620 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 621 | if (!idents_p[i].bases) { |
| 622 | continue; |
| 623 | } |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 624 | LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 625 | } |
| 626 | return LY_SUCCESS; |
| 627 | } |
| 628 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 629 | LY_ERR |
| 630 | lys_feature_precompile(struct ly_ctx *ctx, struct lysp_feature *features_p, struct lysc_feature **features) |
| 631 | { |
| 632 | unsigned int offset = 0, u; |
| 633 | struct lysc_ctx context = {0}; |
| 634 | |
| 635 | assert(ctx); |
| 636 | context.ctx = ctx; |
| 637 | |
| 638 | if (!features_p) { |
| 639 | return LY_SUCCESS; |
| 640 | } |
| 641 | if (*features) { |
| 642 | offset = LY_ARRAY_SIZE(*features); |
| 643 | } |
| 644 | |
| 645 | LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM); |
| 646 | LY_ARRAY_FOR(features_p, u) { |
| 647 | LY_ARRAY_INCREMENT(*features); |
| 648 | COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 649 | DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name); |
| 650 | DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 651 | DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref); |
| 652 | (*features)[offset + u].flags = features_p[u].flags; |
| 653 | } |
| 654 | |
| 655 | return LY_SUCCESS; |
| 656 | } |
| 657 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 658 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 659 | * @brief Create pre-compiled features array. |
| 660 | * |
| 661 | * See lys_feature_precompile() for more details. |
| 662 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 663 | * @param[in] ctx Compile context. |
| 664 | * @param[in] feature_p Parsed feature definition to compile. |
| 665 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 666 | * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 667 | * @return LY_ERR value. |
| 668 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 669 | static LY_ERR |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 670 | lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *features) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 671 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 672 | unsigned int u, v, x; |
| 673 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 674 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 675 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 676 | /* find the preprecompiled feature */ |
| 677 | LY_ARRAY_FOR(features, x) { |
| 678 | if (strcmp(features[x].name, feature_p->name)) { |
| 679 | continue; |
| 680 | } |
| 681 | feature = &features[x]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 682 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 683 | /* finish compilation started in lys_feature_precompile() */ |
| 684 | COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done); |
| 685 | COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 686 | if (feature->iffeatures) { |
| 687 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 688 | if (feature->iffeatures[u].features) { |
| 689 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
| 690 | /* add itself into the dependants list */ |
| 691 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 692 | *df = feature; |
| 693 | } |
| 694 | /* TODO check for circular dependency */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 695 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 696 | } |
| 697 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 698 | done: |
| 699 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 700 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 701 | |
| 702 | LOGINT(ctx->ctx); |
| 703 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 706 | static void |
| 707 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 708 | { |
| 709 | unsigned int u, v; |
| 710 | |
| 711 | /* keep the off_features list until the complete lys_module is freed */ |
| 712 | mod->off_features = mod->compiled->features; |
| 713 | mod->compiled->features = NULL; |
| 714 | |
| 715 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 716 | * which may points into the data being freed here */ |
| 717 | LY_ARRAY_FOR(mod->off_features, u) { |
| 718 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 719 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 720 | } |
| 721 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 722 | mod->off_features[u].iffeatures = NULL; |
| 723 | |
| 724 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 725 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 726 | } |
| 727 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 728 | mod->off_features[u].exts = NULL; |
| 729 | } |
| 730 | } |
| 731 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 732 | /** |
| 733 | * @brief Validate and normalize numeric value from a range definition. |
| 734 | * @param[in] ctx Compile context. |
| 735 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 736 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 737 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 738 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 739 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 740 | * @param[in] value String value of the range boundary. |
| 741 | * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary. |
| 742 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 743 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 744 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 745 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 746 | range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 747 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 748 | size_t fraction = 0, size; |
| 749 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 750 | *len = 0; |
| 751 | |
| 752 | assert(value); |
| 753 | /* parse value */ |
| 754 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 755 | return LY_EVALID; |
| 756 | } |
| 757 | |
| 758 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 759 | ++(*len); |
| 760 | } |
| 761 | |
| 762 | while (isdigit(value[*len])) { |
| 763 | ++(*len); |
| 764 | } |
| 765 | |
| 766 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 767 | if (basetype == LY_TYPE_DEC64) { |
| 768 | goto decimal; |
| 769 | } else { |
| 770 | *valcopy = strndup(value, *len); |
| 771 | return LY_SUCCESS; |
| 772 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 773 | } |
| 774 | fraction = *len; |
| 775 | |
| 776 | ++(*len); |
| 777 | while (isdigit(value[*len])) { |
| 778 | ++(*len); |
| 779 | } |
| 780 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 781 | if (basetype == LY_TYPE_DEC64) { |
| 782 | decimal: |
| 783 | assert(frdigits); |
| 784 | if (*len - 1 - fraction > frdigits) { |
| 785 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 786 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 787 | *len, value, frdigits); |
| 788 | return LY_EINVAL; |
| 789 | } |
| 790 | if (fraction) { |
| 791 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 792 | } else { |
| 793 | size = (*len) + frdigits + 1; |
| 794 | } |
| 795 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 796 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 797 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 798 | (*valcopy)[size - 1] = '\0'; |
| 799 | if (fraction) { |
| 800 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 801 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 802 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 803 | } else { |
| 804 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 805 | memset(&(*valcopy)[*len], '0', frdigits); |
| 806 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 807 | } |
| 808 | return LY_SUCCESS; |
| 809 | } |
| 810 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 811 | /** |
| 812 | * @brief Check that values in range are in ascendant order. |
| 813 | * @param[in] unsigned_value Flag to note that we are working with unsigned values. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 814 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 815 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 816 | * @param[in] value Current value to check. |
| 817 | * @param[in] prev_value The last seen value. |
| 818 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 819 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 820 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 821 | range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 822 | { |
| 823 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 824 | if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 825 | return LY_EEXIST; |
| 826 | } |
| 827 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 828 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 829 | return LY_EEXIST; |
| 830 | } |
| 831 | } |
| 832 | return LY_SUCCESS; |
| 833 | } |
| 834 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 835 | /** |
| 836 | * @brief Set min/max value of the range part. |
| 837 | * @param[in] ctx Compile context. |
| 838 | * @param[in] part Range part structure to fill. |
| 839 | * @param[in] max Flag to distinguish if storing min or max value. |
| 840 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 841 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 842 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 843 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 844 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 845 | * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 846 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 847 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 848 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 849 | * frdigits value), LY_EMEM. |
| 850 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 851 | static LY_ERR |
| 852 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 853 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 854 | { |
| 855 | LY_ERR ret = LY_SUCCESS; |
| 856 | char *valcopy = NULL; |
| 857 | size_t len; |
| 858 | |
| 859 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 860 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 861 | LY_CHECK_GOTO(ret, finalize); |
| 862 | } |
| 863 | if (!valcopy && base_range) { |
| 864 | if (max) { |
| 865 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 866 | } else { |
| 867 | part->min_64 = base_range->parts[0].min_64; |
| 868 | } |
| 869 | if (!first) { |
| 870 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 871 | } |
| 872 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 876 | case LY_TYPE_INT8: /* range */ |
| 877 | if (valcopy) { |
| 878 | ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
| 879 | } else if (max) { |
| 880 | part->max_64 = INT64_C(127); |
| 881 | } else { |
| 882 | part->min_64 = INT64_C(-128); |
| 883 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 884 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 885 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 886 | } |
| 887 | break; |
| 888 | case LY_TYPE_INT16: /* range */ |
| 889 | if (valcopy) { |
| 890 | ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
| 891 | } else if (max) { |
| 892 | part->max_64 = INT64_C(32767); |
| 893 | } else { |
| 894 | part->min_64 = INT64_C(-32768); |
| 895 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 896 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 897 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 898 | } |
| 899 | break; |
| 900 | case LY_TYPE_INT32: /* range */ |
| 901 | if (valcopy) { |
| 902 | ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
| 903 | } else if (max) { |
| 904 | part->max_64 = INT64_C(2147483647); |
| 905 | } else { |
| 906 | part->min_64 = INT64_C(-2147483648); |
| 907 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 908 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 909 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 910 | } |
| 911 | break; |
| 912 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 913 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 914 | if (valcopy) { |
| 915 | ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
| 916 | max ? &part->max_64 : &part->min_64); |
| 917 | } else if (max) { |
| 918 | part->max_64 = INT64_C(9223372036854775807); |
| 919 | } else { |
| 920 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 921 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 922 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 923 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 924 | } |
| 925 | break; |
| 926 | case LY_TYPE_UINT8: /* range */ |
| 927 | if (valcopy) { |
| 928 | ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
| 929 | } else if (max) { |
| 930 | part->max_u64 = UINT64_C(255); |
| 931 | } else { |
| 932 | part->min_u64 = UINT64_C(0); |
| 933 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 934 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 935 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 936 | } |
| 937 | break; |
| 938 | case LY_TYPE_UINT16: /* range */ |
| 939 | if (valcopy) { |
| 940 | ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
| 941 | } else if (max) { |
| 942 | part->max_u64 = UINT64_C(65535); |
| 943 | } else { |
| 944 | part->min_u64 = UINT64_C(0); |
| 945 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 946 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 947 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 948 | } |
| 949 | break; |
| 950 | case LY_TYPE_UINT32: /* range */ |
| 951 | if (valcopy) { |
| 952 | ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
| 953 | } else if (max) { |
| 954 | part->max_u64 = UINT64_C(4294967295); |
| 955 | } else { |
| 956 | part->min_u64 = UINT64_C(0); |
| 957 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 958 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 959 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 960 | } |
| 961 | break; |
| 962 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 963 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 964 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 965 | if (valcopy) { |
| 966 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 967 | } else if (max) { |
| 968 | part->max_u64 = UINT64_C(18446744073709551615); |
| 969 | } else { |
| 970 | part->min_u64 = UINT64_C(0); |
| 971 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 972 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 973 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 974 | } |
| 975 | break; |
| 976 | default: |
| 977 | LOGINT(ctx->ctx); |
| 978 | ret = LY_EINT; |
| 979 | } |
| 980 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 981 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 982 | if (ret == LY_EDENIED) { |
| 983 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 984 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 985 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 986 | } else if (ret == LY_EVALID) { |
| 987 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 988 | "Invalid %s restriction - invalid value \"%s\".", |
| 989 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 990 | } else if (ret == LY_EEXIST) { |
| 991 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 992 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 993 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 994 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 995 | } else if (!ret && value) { |
| 996 | *value = *value + len; |
| 997 | } |
| 998 | free(valcopy); |
| 999 | return ret; |
| 1000 | } |
| 1001 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1002 | /** |
| 1003 | * @brief Compile the parsed range restriction. |
| 1004 | * @param[in] ctx Compile context. |
| 1005 | * @param[in] range_p Parsed range structure to compile. |
| 1006 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1007 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1008 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1009 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1010 | * range restriction must be more restrictive than the base_range. |
| 1011 | * @param[in,out] range Pointer to the created current range structure. |
| 1012 | * @return LY_ERR value. |
| 1013 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1014 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1015 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1016 | struct lysc_range *base_range, struct lysc_range **range) |
| 1017 | { |
| 1018 | LY_ERR ret = LY_EVALID; |
| 1019 | const char *expr; |
| 1020 | struct lysc_range_part *parts = NULL, *part; |
| 1021 | int range_expected = 0, uns; |
| 1022 | unsigned int parts_done = 0, u, v; |
| 1023 | |
| 1024 | assert(range); |
| 1025 | assert(range_p); |
| 1026 | |
| 1027 | expr = range_p->arg; |
| 1028 | while(1) { |
| 1029 | if (isspace(*expr)) { |
| 1030 | ++expr; |
| 1031 | } else if (*expr == '\0') { |
| 1032 | if (range_expected) { |
| 1033 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1034 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1035 | length_restr ? "length" : "range", range_p->arg); |
| 1036 | goto cleanup; |
| 1037 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1038 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1039 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1040 | length_restr ? "length" : "range", range_p->arg); |
| 1041 | goto cleanup; |
| 1042 | } |
| 1043 | parts_done++; |
| 1044 | break; |
| 1045 | } else if (!strncmp(expr, "min", 3)) { |
| 1046 | if (parts) { |
| 1047 | /* min cannot be used elsewhere than in the first part */ |
| 1048 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1049 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1050 | expr - range_p->arg, range_p->arg); |
| 1051 | goto cleanup; |
| 1052 | } |
| 1053 | expr += 3; |
| 1054 | |
| 1055 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1056 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1057 | part->max_64 = part->min_64; |
| 1058 | } else if (*expr == '|') { |
| 1059 | if (!parts || range_expected) { |
| 1060 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1061 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1062 | goto cleanup; |
| 1063 | } |
| 1064 | expr++; |
| 1065 | parts_done++; |
| 1066 | /* process next part of the expression */ |
| 1067 | } else if (!strncmp(expr, "..", 2)) { |
| 1068 | expr += 2; |
| 1069 | while (isspace(*expr)) { |
| 1070 | expr++; |
| 1071 | } |
| 1072 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1073 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1074 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1075 | goto cleanup; |
| 1076 | } |
| 1077 | /* continue expecting the upper boundary */ |
| 1078 | range_expected = 1; |
| 1079 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1080 | /* number */ |
| 1081 | if (range_expected) { |
| 1082 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1083 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1084 | range_expected = 0; |
| 1085 | } else { |
| 1086 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1087 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1088 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1089 | part->max_64 = part->min_64; |
| 1090 | } |
| 1091 | |
| 1092 | /* continue with possible another expression part */ |
| 1093 | } else if (!strncmp(expr, "max", 3)) { |
| 1094 | expr += 3; |
| 1095 | while (isspace(*expr)) { |
| 1096 | expr++; |
| 1097 | } |
| 1098 | if (*expr != '\0') { |
| 1099 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1100 | length_restr ? "length" : "range", expr); |
| 1101 | goto cleanup; |
| 1102 | } |
| 1103 | if (range_expected) { |
| 1104 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1105 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1106 | range_expected = 0; |
| 1107 | } else { |
| 1108 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1109 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1110 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1111 | part->min_64 = part->max_64; |
| 1112 | } |
| 1113 | } else { |
| 1114 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1115 | length_restr ? "length" : "range", expr); |
| 1116 | goto cleanup; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | /* check with the previous range/length restriction */ |
| 1121 | if (base_range) { |
| 1122 | switch (basetype) { |
| 1123 | case LY_TYPE_BINARY: |
| 1124 | case LY_TYPE_UINT8: |
| 1125 | case LY_TYPE_UINT16: |
| 1126 | case LY_TYPE_UINT32: |
| 1127 | case LY_TYPE_UINT64: |
| 1128 | case LY_TYPE_STRING: |
| 1129 | uns = 1; |
| 1130 | break; |
| 1131 | case LY_TYPE_DEC64: |
| 1132 | case LY_TYPE_INT8: |
| 1133 | case LY_TYPE_INT16: |
| 1134 | case LY_TYPE_INT32: |
| 1135 | case LY_TYPE_INT64: |
| 1136 | uns = 0; |
| 1137 | break; |
| 1138 | default: |
| 1139 | LOGINT(ctx->ctx); |
| 1140 | ret = LY_EINT; |
| 1141 | goto cleanup; |
| 1142 | } |
| 1143 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1144 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1145 | goto baseerror; |
| 1146 | } |
| 1147 | /* current lower bound is not lower than the base */ |
| 1148 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1149 | /* base has single value */ |
| 1150 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1151 | /* both lower bounds are the same */ |
| 1152 | if (parts[u].min_64 != parts[u].max_64) { |
| 1153 | /* current continues with a range */ |
| 1154 | goto baseerror; |
| 1155 | } else { |
| 1156 | /* equal single values, move both forward */ |
| 1157 | ++v; |
| 1158 | continue; |
| 1159 | } |
| 1160 | } else { |
| 1161 | /* base is single value lower than current range, so the |
| 1162 | * value from base range is removed in the current, |
| 1163 | * move only base and repeat checking */ |
| 1164 | ++v; |
| 1165 | --u; |
| 1166 | continue; |
| 1167 | } |
| 1168 | } else { |
| 1169 | /* base is the range */ |
| 1170 | if (parts[u].min_64 == parts[u].max_64) { |
| 1171 | /* current is a single value */ |
| 1172 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1173 | /* current is behind the base range, so base range is omitted, |
| 1174 | * move the base and keep the current for further check */ |
| 1175 | ++v; |
| 1176 | --u; |
| 1177 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1178 | continue; |
| 1179 | } else { |
| 1180 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1181 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1182 | /* higher bound is higher than the current higher bound */ |
| 1183 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1184 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1185 | * continue with the same current, but move the base */ |
| 1186 | --u; |
| 1187 | ++v; |
| 1188 | continue; |
| 1189 | } |
| 1190 | /* current range starts within the base range but end behind it */ |
| 1191 | goto baseerror; |
| 1192 | } else { |
| 1193 | /* current range is smaller than the base, |
| 1194 | * move current, but stay with the base */ |
| 1195 | continue; |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | if (u != parts_done) { |
| 1201 | baseerror: |
| 1202 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1203 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1204 | length_restr ? "length" : "range", range_p->arg); |
| 1205 | goto cleanup; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | if (!(*range)) { |
| 1210 | *range = calloc(1, sizeof **range); |
| 1211 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1212 | } |
| 1213 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1214 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1215 | if (range_p->eapptag) { |
| 1216 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1217 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1218 | } |
| 1219 | if (range_p->emsg) { |
| 1220 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1221 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1222 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1223 | if (range_p->dsc) { |
| 1224 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1225 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1226 | } |
| 1227 | if (range_p->ref) { |
| 1228 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1229 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1230 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1231 | /* extensions are taken only from the last range by the caller */ |
| 1232 | |
| 1233 | (*range)->parts = parts; |
| 1234 | parts = NULL; |
| 1235 | ret = LY_SUCCESS; |
| 1236 | cleanup: |
| 1237 | /* TODO clean up */ |
| 1238 | LY_ARRAY_FREE(parts); |
| 1239 | |
| 1240 | return ret; |
| 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * @brief Checks pattern syntax. |
| 1245 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1246 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1247 | * @param[in] pattern Pattern to check. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1248 | * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed. |
| 1249 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1250 | */ |
| 1251 | static LY_ERR |
| 1252 | lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp) |
| 1253 | { |
| 1254 | int idx, idx2, start, end, err_offset, count; |
| 1255 | char *perl_regex, *ptr; |
| 1256 | const char *err_msg, *orig_ptr; |
| 1257 | pcre *precomp; |
| 1258 | #define URANGE_LEN 19 |
| 1259 | char *ublock2urange[][2] = { |
| 1260 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1261 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1262 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1263 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1264 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1265 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1266 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1267 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1268 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1269 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1270 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1271 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1272 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1273 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1274 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1275 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1276 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1277 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1278 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1279 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1280 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1281 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1282 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1283 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1284 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1285 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1286 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1287 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1288 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1289 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1290 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1291 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1292 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1293 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1294 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1295 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1296 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1297 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1298 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1299 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1300 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1301 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1302 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1303 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1304 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1305 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1306 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1307 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1308 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1309 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1310 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1311 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1312 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1313 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1314 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1315 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1316 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1317 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1318 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1319 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1320 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1321 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1322 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1323 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1324 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1325 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1326 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1327 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1328 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1329 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1330 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1331 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1332 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1333 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1334 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1335 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1336 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1337 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1338 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1339 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1340 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1341 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1342 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1343 | {NULL, NULL} |
| 1344 | }; |
| 1345 | |
| 1346 | /* adjust the expression to a Perl equivalent |
| 1347 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1348 | |
| 1349 | /* we need to replace all "$" with "\$", count them now */ |
| 1350 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1351 | |
| 1352 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1353 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1354 | perl_regex[0] = '\0'; |
| 1355 | |
| 1356 | ptr = perl_regex; |
| 1357 | |
| 1358 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1359 | /* we will add line-end anchoring */ |
| 1360 | ptr[0] = '('; |
| 1361 | ++ptr; |
| 1362 | } |
| 1363 | |
| 1364 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1365 | if (orig_ptr[0] == '$') { |
| 1366 | ptr += sprintf(ptr, "\\$"); |
| 1367 | } else if (orig_ptr[0] == '^') { |
| 1368 | ptr += sprintf(ptr, "\\^"); |
| 1369 | } else { |
| 1370 | ptr[0] = orig_ptr[0]; |
| 1371 | ++ptr; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1376 | ptr += sprintf(ptr, ")$"); |
| 1377 | } else { |
| 1378 | ptr[0] = '\0'; |
| 1379 | ++ptr; |
| 1380 | } |
| 1381 | |
| 1382 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1383 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1384 | start = ptr - perl_regex; |
| 1385 | |
| 1386 | ptr = strchr(ptr, '}'); |
| 1387 | if (!ptr) { |
| 1388 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1389 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1390 | free(perl_regex); |
| 1391 | return LY_EVALID; |
| 1392 | } |
| 1393 | end = (ptr - perl_regex) + 1; |
| 1394 | |
| 1395 | /* need more space */ |
| 1396 | if (end - start < URANGE_LEN) { |
| 1397 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1398 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1399 | } |
| 1400 | |
| 1401 | /* find our range */ |
| 1402 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1403 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1404 | break; |
| 1405 | } |
| 1406 | } |
| 1407 | if (!ublock2urange[idx][0]) { |
| 1408 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1409 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1410 | free(perl_regex); |
| 1411 | return LY_EVALID; |
| 1412 | } |
| 1413 | |
| 1414 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1415 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1416 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1417 | ++count; |
| 1418 | } |
| 1419 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1420 | --count; |
| 1421 | } |
| 1422 | } |
| 1423 | if (count) { |
| 1424 | /* skip brackets */ |
| 1425 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1426 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1427 | } else { |
| 1428 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1429 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | /* must return 0, already checked during parsing */ |
| 1434 | precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE, |
| 1435 | &err_msg, &err_offset, NULL); |
| 1436 | if (!precomp) { |
| 1437 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1438 | free(perl_regex); |
| 1439 | return LY_EVALID; |
| 1440 | } |
| 1441 | free(perl_regex); |
| 1442 | |
| 1443 | if (pcre_precomp) { |
| 1444 | *pcre_precomp = precomp; |
| 1445 | } else { |
| 1446 | free(precomp); |
| 1447 | } |
| 1448 | |
| 1449 | return LY_SUCCESS; |
| 1450 | |
| 1451 | #undef URANGE_LEN |
| 1452 | } |
| 1453 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1454 | /** |
| 1455 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1456 | * @param[in] ctx Compile context. |
| 1457 | * @param[in] patterns_p Array of parsed patterns from the current type to compile. |
| 1458 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 1459 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1460 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1461 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1462 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1463 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1464 | static LY_ERR |
| 1465 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options, |
| 1466 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1467 | { |
| 1468 | struct lysc_pattern **pattern; |
| 1469 | unsigned int u, v; |
| 1470 | const char *err_msg; |
| 1471 | LY_ERR ret = LY_SUCCESS; |
| 1472 | |
| 1473 | /* first, copy the patterns from the base type */ |
| 1474 | if (base_patterns) { |
| 1475 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1476 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1477 | } |
| 1478 | |
| 1479 | LY_ARRAY_FOR(patterns_p, u) { |
| 1480 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1481 | *pattern = calloc(1, sizeof **pattern); |
| 1482 | ++(*pattern)->refcount; |
| 1483 | |
| 1484 | ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr); |
| 1485 | LY_CHECK_RET(ret); |
| 1486 | (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg); |
| 1487 | if (err_msg) { |
| 1488 | LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg); |
| 1489 | } |
| 1490 | |
| 1491 | if (patterns_p[u].arg[0] == 0x15) { |
| 1492 | (*pattern)->inverted = 1; |
| 1493 | } |
| 1494 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1495 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1496 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1497 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1498 | COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, |
| 1499 | options, v, lys_compile_ext, ret, done); |
| 1500 | } |
| 1501 | done: |
| 1502 | return ret; |
| 1503 | } |
| 1504 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1505 | /** |
| 1506 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1507 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1508 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1509 | 0 /* LY_TYPE_UNKNOWN */, |
| 1510 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1511 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1512 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1513 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1514 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1515 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1516 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1517 | 0 /* LY_TYPE_BOOL */, |
| 1518 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1519 | 0 /* LY_TYPE_EMPTY */, |
| 1520 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1521 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1522 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1523 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1524 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1525 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1526 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1527 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1528 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 1529 | }; |
| 1530 | |
| 1531 | /** |
| 1532 | * @brief stringification of the YANG built-in data types |
| 1533 | */ |
| 1534 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 1535 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 1536 | "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1537 | }; |
| 1538 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1539 | /** |
| 1540 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 1541 | * @param[in] ctx Compile context. |
| 1542 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 1543 | * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected. |
| 1544 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 1545 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 1546 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 1547 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1548 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1549 | static LY_ERR |
| 1550 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options, |
| 1551 | struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums) |
| 1552 | { |
| 1553 | LY_ERR ret = LY_SUCCESS; |
| 1554 | unsigned int u, v, match; |
| 1555 | int32_t value = 0; |
| 1556 | uint32_t position = 0; |
| 1557 | struct lysc_type_enum_item *e, storage; |
| 1558 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1559 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1560 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 1561 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1562 | return LY_EVALID; |
| 1563 | } |
| 1564 | |
| 1565 | LY_ARRAY_FOR(enums_p, u) { |
| 1566 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 1567 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1568 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 1569 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1570 | if (base_enums) { |
| 1571 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1572 | LY_ARRAY_FOR(base_enums, v) { |
| 1573 | if (!strcmp(e->name, base_enums[v].name)) { |
| 1574 | break; |
| 1575 | } |
| 1576 | } |
| 1577 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 1578 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1579 | "Invalid %s - derived type adds new item \"%s\".", |
| 1580 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 1581 | return LY_EVALID; |
| 1582 | } |
| 1583 | match = v; |
| 1584 | } |
| 1585 | |
| 1586 | if (basetype == LY_TYPE_ENUM) { |
| 1587 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1588 | e->value = (int32_t)enums_p[u].value; |
| 1589 | if (!u || e->value >= value) { |
| 1590 | value = e->value + 1; |
| 1591 | } |
| 1592 | /* check collision with other values */ |
| 1593 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1594 | if (e->value == (*enums)[v].value) { |
| 1595 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1596 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 1597 | e->value, e->name, (*enums)[v].name); |
| 1598 | return LY_EVALID; |
| 1599 | } |
| 1600 | } |
| 1601 | } else if (base_enums) { |
| 1602 | /* inherit the assigned value */ |
| 1603 | e->value = base_enums[match].value; |
| 1604 | if (!u || e->value >= value) { |
| 1605 | value = e->value + 1; |
| 1606 | } |
| 1607 | } else { |
| 1608 | /* assign value automatically */ |
| 1609 | if (u && value == INT32_MIN) { |
| 1610 | /* counter overflow */ |
| 1611 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1612 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 1613 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 1614 | return LY_EVALID; |
| 1615 | } |
| 1616 | e->value = value++; |
| 1617 | } |
| 1618 | } else { /* LY_TYPE_BITS */ |
| 1619 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1620 | e->value = (int32_t)enums_p[u].value; |
| 1621 | if (!u || (uint32_t)e->value >= position) { |
| 1622 | position = (uint32_t)e->value + 1; |
| 1623 | } |
| 1624 | /* check collision with other values */ |
| 1625 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1626 | if (e->value == (*enums)[v].value) { |
| 1627 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1628 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 1629 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 1630 | return LY_EVALID; |
| 1631 | } |
| 1632 | } |
| 1633 | } else if (base_enums) { |
| 1634 | /* inherit the assigned value */ |
| 1635 | e->value = base_enums[match].value; |
| 1636 | if (!u || (uint32_t)e->value >= position) { |
| 1637 | position = (uint32_t)e->value + 1; |
| 1638 | } |
| 1639 | } else { |
| 1640 | /* assign value automatically */ |
| 1641 | if (u && position == 0) { |
| 1642 | /* counter overflow */ |
| 1643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1644 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 1645 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 1646 | return LY_EVALID; |
| 1647 | } |
| 1648 | e->value = position++; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | if (base_enums) { |
| 1653 | /* the assigned values must not change from the derived type */ |
| 1654 | if (e->value != base_enums[match].value) { |
| 1655 | if (basetype == LY_TYPE_ENUM) { |
| 1656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1657 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 1658 | e->name, base_enums[match].value, e->value); |
| 1659 | } else { |
| 1660 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1661 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 1662 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 1663 | } |
| 1664 | return LY_EVALID; |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done); |
| 1669 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done); |
| 1670 | |
| 1671 | if (basetype == LY_TYPE_BITS) { |
| 1672 | /* keep bits ordered by position */ |
| 1673 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 1674 | if (v != u) { |
| 1675 | memcpy(&storage, e, sizeof *e); |
| 1676 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 1677 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 1678 | } |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | done: |
| 1683 | return ret; |
| 1684 | } |
| 1685 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1686 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 1687 | for ((NODE) = (NODE)->parent; \ |
| 1688 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 1689 | (NODE) = (NODE)->parent); \ |
| 1690 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 1691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 1692 | TERM; \ |
| 1693 | } |
| 1694 | |
| 1695 | /** |
| 1696 | * @brief Validate the predicate(s) from the leafref path. |
| 1697 | * @param[in] ctx Compile context |
| 1698 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 1699 | * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated. |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1700 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1701 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1702 | * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1703 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1704 | */ |
| 1705 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1706 | lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1707 | const struct lysc_node_list *context_node, const struct lys_module *path_context) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1708 | { |
| 1709 | LY_ERR ret = LY_EVALID; |
| 1710 | const struct lys_module *mod; |
| 1711 | const struct lysc_node *src_node, *dst_node; |
| 1712 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 1713 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1714 | unsigned int dest_parent_times, c, u; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1715 | const char *start, *end, *pke_end; |
| 1716 | struct ly_set keys = {0}; |
| 1717 | int i; |
| 1718 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1719 | assert(path_context); |
| 1720 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1721 | while (**predicate == '[') { |
| 1722 | start = (*predicate)++; |
| 1723 | |
| 1724 | while (isspace(**predicate)) { |
| 1725 | ++(*predicate); |
| 1726 | } |
| 1727 | LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup); |
| 1728 | while (isspace(**predicate)) { |
| 1729 | ++(*predicate); |
| 1730 | } |
| 1731 | if (**predicate != '=') { |
| 1732 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1733 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 1734 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1735 | goto cleanup; |
| 1736 | } |
| 1737 | ++(*predicate); |
| 1738 | while (isspace(**predicate)) { |
| 1739 | ++(*predicate); |
| 1740 | } |
| 1741 | |
| 1742 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 1743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1744 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 1745 | goto cleanup; |
| 1746 | } |
| 1747 | --pke_end; |
| 1748 | while (isspace(*pke_end)) { |
| 1749 | --pke_end; |
| 1750 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1751 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1752 | /* localize path-key-expr */ |
| 1753 | pke_start = path_key_expr = *predicate; |
| 1754 | /* move after the current predicate */ |
| 1755 | *predicate = end + 1; |
| 1756 | |
| 1757 | /* source (must be leaf or leaf-list) */ |
| 1758 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1759 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1760 | if (!mod) { |
| 1761 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1762 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1763 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1764 | goto cleanup; |
| 1765 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1766 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1767 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1768 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1769 | src_node = NULL; |
| 1770 | if (context_node->keys) { |
| 1771 | for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) { |
| 1772 | if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') { |
| 1773 | src_node = (const struct lysc_node*)context_node->keys[u]; |
| 1774 | break; |
| 1775 | } |
| 1776 | } |
| 1777 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1778 | if (!src_node) { |
| 1779 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1780 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1781 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1782 | goto cleanup; |
| 1783 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1784 | |
| 1785 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1786 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1787 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 1788 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1789 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1790 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1791 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1792 | *predicate - start, start, src_node->name); |
| 1793 | goto cleanup; |
| 1794 | } |
| 1795 | |
| 1796 | /* destination */ |
| 1797 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1798 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1799 | |
| 1800 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 1801 | if (strncmp(path_key_expr, "current()", 9)) { |
| 1802 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1803 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 1804 | *predicate - start, start); |
| 1805 | goto cleanup; |
| 1806 | } |
| 1807 | path_key_expr += 9; |
| 1808 | while (isspace(*path_key_expr)) { |
| 1809 | ++path_key_expr; |
| 1810 | } |
| 1811 | |
| 1812 | if (*path_key_expr != '/') { |
| 1813 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1814 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 1815 | *predicate - start, start); |
| 1816 | goto cleanup; |
| 1817 | } |
| 1818 | ++path_key_expr; |
| 1819 | while (isspace(*path_key_expr)) { |
| 1820 | ++path_key_expr; |
| 1821 | } |
| 1822 | |
| 1823 | /* rel-path-keyexpr: |
| 1824 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 1825 | while (!strncmp(path_key_expr, "..", 2)) { |
| 1826 | ++dest_parent_times; |
| 1827 | path_key_expr += 2; |
| 1828 | while (isspace(*path_key_expr)) { |
| 1829 | ++path_key_expr; |
| 1830 | } |
| 1831 | if (*path_key_expr != '/') { |
| 1832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1833 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 1834 | *predicate - start, start); |
| 1835 | goto cleanup; |
| 1836 | } |
| 1837 | ++path_key_expr; |
| 1838 | while (isspace(*path_key_expr)) { |
| 1839 | ++path_key_expr; |
| 1840 | } |
| 1841 | |
| 1842 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 1843 | * all schema nodes that cannot be instantiated in data tree */ |
| 1844 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 1845 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 1846 | *predicate - start, start); |
| 1847 | } |
| 1848 | if (!dest_parent_times) { |
| 1849 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1850 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 1851 | *predicate - start, start); |
| 1852 | goto cleanup; |
| 1853 | } |
| 1854 | if (path_key_expr == pke_end) { |
| 1855 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1856 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 1857 | *predicate - start, start); |
| 1858 | goto cleanup; |
| 1859 | } |
| 1860 | |
| 1861 | while(path_key_expr != pke_end) { |
| 1862 | if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) { |
| 1863 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1864 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 1865 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1866 | goto cleanup; |
| 1867 | } |
| 1868 | |
| 1869 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1870 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1871 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1872 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1873 | } |
| 1874 | if (!mod) { |
| 1875 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1876 | "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.", |
| 1877 | *predicate - start, start, dst_len, dst); |
| 1878 | goto cleanup; |
| 1879 | } |
| 1880 | |
| 1881 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 1882 | if (!dst_node) { |
| 1883 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1884 | "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.", |
| 1885 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 1886 | goto cleanup; |
| 1887 | } |
| 1888 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1889 | if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1890 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1891 | "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s instead of leaf.", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1892 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 1893 | goto cleanup; |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | ret = LY_SUCCESS; |
| 1898 | cleanup: |
| 1899 | ly_set_erase(&keys, NULL); |
| 1900 | return ret; |
| 1901 | } |
| 1902 | |
| 1903 | /** |
| 1904 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 1905 | * |
| 1906 | * path-arg = absolute-path / relative-path |
| 1907 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 1908 | * relative-path = 1*(".." "/") descendant-path |
| 1909 | * |
| 1910 | * @param[in,out] path Path to parse. |
| 1911 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 1912 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 1913 | * @param[out] name Name of the token. |
| 1914 | * @param[out] nam_len Length of the name. |
| 1915 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 1916 | * must not be changed between consecutive calls. -1 if the |
| 1917 | * path is absolute. |
| 1918 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 1919 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 1920 | */ |
| 1921 | static LY_ERR |
| 1922 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 1923 | int *parent_times, int *has_predicate) |
| 1924 | { |
| 1925 | int par_times = 0; |
| 1926 | |
| 1927 | assert(path && *path); |
| 1928 | assert(parent_times); |
| 1929 | assert(prefix); |
| 1930 | assert(prefix_len); |
| 1931 | assert(name); |
| 1932 | assert(name_len); |
| 1933 | assert(has_predicate); |
| 1934 | |
| 1935 | *prefix = NULL; |
| 1936 | *prefix_len = 0; |
| 1937 | *name = NULL; |
| 1938 | *name_len = 0; |
| 1939 | *has_predicate = 0; |
| 1940 | |
| 1941 | if (!*parent_times) { |
| 1942 | if (!strncmp(*path, "..", 2)) { |
| 1943 | *path += 2; |
| 1944 | ++par_times; |
| 1945 | while (!strncmp(*path, "/..", 3)) { |
| 1946 | *path += 3; |
| 1947 | ++par_times; |
| 1948 | } |
| 1949 | } |
| 1950 | if (par_times) { |
| 1951 | *parent_times = par_times; |
| 1952 | } else { |
| 1953 | *parent_times = -1; |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | if (**path != '/') { |
| 1958 | return LY_EINVAL; |
| 1959 | } |
| 1960 | /* skip '/' */ |
| 1961 | ++(*path); |
| 1962 | |
| 1963 | /* node-identifier ([prefix:]name) */ |
| 1964 | LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len)); |
| 1965 | |
| 1966 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 1967 | /* path continues by another token or this is the last token */ |
| 1968 | return LY_SUCCESS; |
| 1969 | } else if ((*path)[0] != '[') { |
| 1970 | /* unexpected character */ |
| 1971 | return LY_EINVAL; |
| 1972 | } else { |
| 1973 | /* predicate starting with [ */ |
| 1974 | *has_predicate = 1; |
| 1975 | return LY_SUCCESS; |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1980 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 1981 | * |
| 1982 | * The set of features used for target must be a subset of features used for the leafref. |
| 1983 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 1984 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 1985 | * |
| 1986 | * @param[in] refnode The leafref node. |
| 1987 | * @param[in] target Tha target node of the leafref. |
| 1988 | * @return LY_SUCCESS or LY_EVALID; |
| 1989 | */ |
| 1990 | static LY_ERR |
| 1991 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 1992 | { |
| 1993 | LY_ERR ret = LY_EVALID; |
| 1994 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1995 | unsigned int u, v, count; |
| 1996 | struct ly_set features = {0}; |
| 1997 | |
| 1998 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1999 | if (iter->iffeatures) { |
| 2000 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2001 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2002 | LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2003 | } |
| 2004 | } |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2009 | * So when adding features applicable to the target into the features set, we should not be |
| 2010 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2011 | * to the leafref itself. */ |
| 2012 | count = features.count; |
| 2013 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2014 | if (iter->iffeatures) { |
| 2015 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2016 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2017 | if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) { |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2018 | /* new feature was added (or LY_EMEM) */ |
| 2019 | goto cleanup; |
| 2020 | } |
| 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | } |
| 2025 | ret = LY_SUCCESS; |
| 2026 | |
| 2027 | cleanup: |
| 2028 | ly_set_erase(&features, NULL); |
| 2029 | return ret; |
| 2030 | } |
| 2031 | |
| 2032 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2033 | * @brief Validate the leafref path. |
| 2034 | * @param[in] ctx Compile context |
| 2035 | * @param[in] startnode Path context node (where the leafref path begins/is placed). |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2036 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2037 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2038 | */ |
| 2039 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2040 | lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2041 | { |
| 2042 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2043 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2044 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2045 | const char *id, *prefix, *name; |
| 2046 | size_t prefix_len, name_len; |
| 2047 | int parent_times = 0, has_predicate; |
| 2048 | unsigned int iter, u; |
| 2049 | LY_ERR ret = LY_SUCCESS; |
| 2050 | |
| 2051 | assert(ctx); |
| 2052 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2053 | assert(leafref); |
| 2054 | |
| 2055 | /* TODO leafref targets may be not implemented, in such a case we actually could make (we did it in libyang1) such a models implemented */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2056 | |
| 2057 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2058 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2059 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2060 | if (!iter) { /* first iteration */ |
| 2061 | /* precess ".." in relative paths */ |
| 2062 | if (parent_times > 0) { |
| 2063 | /* move from the context node */ |
| 2064 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2065 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2066 | * all schema nodes that cannot be instantiated in data tree */ |
| 2067 | MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2068 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2074 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2075 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2076 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2077 | } |
| 2078 | if (!mod) { |
| 2079 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2080 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2081 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2082 | return LY_EVALID; |
| 2083 | } |
| 2084 | |
| 2085 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2086 | if (!node) { |
| 2087 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2088 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2089 | return LY_EVALID; |
| 2090 | } |
| 2091 | parent = node; |
| 2092 | |
| 2093 | if (has_predicate) { |
| 2094 | /* we have predicate, so the current result must be list */ |
| 2095 | if (node->nodetype != LYS_LIST) { |
| 2096 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2097 | "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.", |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2098 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2099 | return LY_EVALID; |
| 2100 | } |
| 2101 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2102 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2103 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | ++iter; |
| 2107 | } |
| 2108 | if (ret) { |
| 2109 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2110 | "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2111 | return LY_EVALID; |
| 2112 | } |
| 2113 | |
| 2114 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2115 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2116 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2117 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2118 | return LY_EVALID; |
| 2119 | } |
| 2120 | |
| 2121 | /* check status */ |
| 2122 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2123 | return LY_EVALID; |
| 2124 | } |
| 2125 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2126 | /* check config */ |
| 2127 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2128 | if (node->flags & LYS_CONFIG_R) { |
| 2129 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2130 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2131 | leafref->path); |
| 2132 | return LY_EVALID; |
| 2133 | } |
| 2134 | } |
| 2135 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2136 | /* store the target's type and check for circular chain of leafrefs */ |
| 2137 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2138 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2139 | if (type == (struct lysc_type*)leafref) { |
| 2140 | /* circular chain detected */ |
| 2141 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2142 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2143 | return LY_EVALID; |
| 2144 | } |
| 2145 | } |
| 2146 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2147 | /* check if leafref and its target are under common if-features */ |
| 2148 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2149 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2150 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2151 | leafref->path); |
| 2152 | return LY_EVALID; |
| 2153 | } |
| 2154 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2155 | return LY_SUCCESS; |
| 2156 | } |
| 2157 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2158 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name, |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2159 | struct lysp_type *type_p, int options, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2160 | /** |
| 2161 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2162 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2163 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2164 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2165 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2166 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2167 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2168 | * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2169 | * @param[in] basetype Base YANG built-in type of the type to compile. |
| 2170 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2171 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2172 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2173 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2174 | * @return LY_ERR value. |
| 2175 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2176 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2177 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name, |
| 2178 | struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname, |
| 2179 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2180 | { |
| 2181 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2182 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2183 | struct lysc_type_bin *bin; |
| 2184 | struct lysc_type_num *num; |
| 2185 | struct lysc_type_str *str; |
| 2186 | struct lysc_type_bits *bits; |
| 2187 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2188 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2189 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2190 | struct lysc_type_union *un, *un_aux; |
| 2191 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2192 | |
| 2193 | switch (basetype) { |
| 2194 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2195 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2196 | |
| 2197 | /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2198 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2199 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2200 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length); |
| 2201 | LY_CHECK_RET(ret); |
| 2202 | if (!tpdfname) { |
| 2203 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, |
| 2204 | options, u, lys_compile_ext, ret, done); |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | if (tpdfname) { |
| 2209 | type_p->compiled = *type; |
| 2210 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2211 | } |
| 2212 | break; |
| 2213 | case LY_TYPE_BITS: |
| 2214 | /* RFC 7950 9.7 - bits */ |
| 2215 | bits = (struct lysc_type_bits*)(*type); |
| 2216 | if (type_p->bits) { |
| 2217 | ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options, |
| 2218 | base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2219 | (struct lysc_type_enum_item**)&bits->bits); |
| 2220 | LY_CHECK_RET(ret); |
| 2221 | } |
| 2222 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2223 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2224 | /* type derived from bits built-in type must contain at least one bit */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2225 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2226 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2227 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2228 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2229 | free(*type); |
| 2230 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2231 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2232 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | if (tpdfname) { |
| 2236 | type_p->compiled = *type; |
| 2237 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2238 | } |
| 2239 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2240 | case LY_TYPE_DEC64: |
| 2241 | dec = (struct lysc_type_dec*)(*type); |
| 2242 | |
| 2243 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2244 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2245 | if (!type_p->fraction_digits) { |
| 2246 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2247 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2248 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2249 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", ""); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2250 | free(*type); |
| 2251 | *type = NULL; |
| 2252 | } |
| 2253 | return LY_EVALID; |
| 2254 | } |
| 2255 | } else if (type_p->fraction_digits) { |
| 2256 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2257 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2258 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2259 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2260 | tpdfname); |
| 2261 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2262 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2263 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2264 | free(*type); |
| 2265 | *type = NULL; |
| 2266 | } |
| 2267 | return LY_EVALID; |
| 2268 | } |
| 2269 | dec->fraction_digits = type_p->fraction_digits; |
| 2270 | |
| 2271 | /* RFC 7950 9.2.4 - range */ |
| 2272 | if (type_p->range) { |
| 2273 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2274 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range); |
| 2275 | LY_CHECK_RET(ret); |
| 2276 | if (!tpdfname) { |
| 2277 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, |
| 2278 | options, u, lys_compile_ext, ret, done); |
| 2279 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | if (tpdfname) { |
| 2283 | type_p->compiled = *type; |
| 2284 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2285 | } |
| 2286 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2287 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2288 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2289 | |
| 2290 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2291 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2292 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2293 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length); |
| 2294 | LY_CHECK_RET(ret); |
| 2295 | if (!tpdfname) { |
| 2296 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, |
| 2297 | options, u, lys_compile_ext, ret, done); |
| 2298 | } |
| 2299 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2300 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2301 | } |
| 2302 | |
| 2303 | /* RFC 7950 9.4.5 - pattern */ |
| 2304 | if (type_p->patterns) { |
| 2305 | ret = lys_compile_type_patterns(ctx, type_p->patterns, options, |
| 2306 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns); |
| 2307 | LY_CHECK_RET(ret); |
| 2308 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2309 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2310 | } |
| 2311 | |
| 2312 | if (tpdfname) { |
| 2313 | type_p->compiled = *type; |
| 2314 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2315 | } |
| 2316 | break; |
| 2317 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2318 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2319 | |
| 2320 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2321 | if (type_p->enums) { |
| 2322 | ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options, |
| 2323 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums); |
| 2324 | LY_CHECK_RET(ret); |
| 2325 | } |
| 2326 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2327 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2328 | /* type derived from enumerations built-in type must contain at least one enum */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2329 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2330 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2331 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2332 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2333 | free(*type); |
| 2334 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2335 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2336 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | if (tpdfname) { |
| 2340 | type_p->compiled = *type; |
| 2341 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2342 | } |
| 2343 | break; |
| 2344 | case LY_TYPE_INT8: |
| 2345 | case LY_TYPE_UINT8: |
| 2346 | case LY_TYPE_INT16: |
| 2347 | case LY_TYPE_UINT16: |
| 2348 | case LY_TYPE_INT32: |
| 2349 | case LY_TYPE_UINT32: |
| 2350 | case LY_TYPE_INT64: |
| 2351 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2352 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2353 | |
| 2354 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2355 | if (type_p->range) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2356 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2357 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range); |
| 2358 | LY_CHECK_RET(ret); |
| 2359 | if (!tpdfname) { |
| 2360 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, |
| 2361 | options, u, lys_compile_ext, ret, done); |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | if (tpdfname) { |
| 2366 | type_p->compiled = *type; |
| 2367 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2368 | } |
| 2369 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2370 | case LY_TYPE_IDENT: |
| 2371 | idref = (struct lysc_type_identityref*)(*type); |
| 2372 | |
| 2373 | /* RFC 7950 9.10.2 - base */ |
| 2374 | if (type_p->bases) { |
| 2375 | if (base) { |
| 2376 | /* only the directly derived identityrefs can contain base specification */ |
| 2377 | if (tpdfname) { |
| 2378 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2379 | "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.", |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2380 | tpdfname); |
| 2381 | } else { |
| 2382 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2383 | "Invalid base substatement for the type not directly derived from identityref built-in type."); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2384 | free(*type); |
| 2385 | *type = NULL; |
| 2386 | } |
| 2387 | return LY_EVALID; |
| 2388 | } |
| 2389 | ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases); |
| 2390 | LY_CHECK_RET(ret); |
| 2391 | } |
| 2392 | |
| 2393 | if (!base && !type_p->flags) { |
| 2394 | /* type derived from identityref built-in type must contain at least one base */ |
| 2395 | if (tpdfname) { |
| 2396 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2397 | } else { |
| 2398 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2399 | free(*type); |
| 2400 | *type = NULL; |
| 2401 | } |
| 2402 | return LY_EVALID; |
| 2403 | } |
| 2404 | |
| 2405 | if (tpdfname) { |
| 2406 | type_p->compiled = *type; |
| 2407 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2408 | } |
| 2409 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2410 | case LY_TYPE_LEAFREF: |
| 2411 | /* RFC 7950 9.9.3 - require-instance */ |
| 2412 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2413 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2414 | if (tpdfname) { |
| 2415 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2416 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2417 | } else { |
| 2418 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2419 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2420 | free(*type); |
| 2421 | *type = NULL; |
| 2422 | } |
| 2423 | return LY_EVALID; |
| 2424 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2425 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2426 | } else if (base) { |
| 2427 | /* inherit */ |
| 2428 | ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2429 | } else { |
| 2430 | /* default is true */ |
| 2431 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2432 | } |
| 2433 | if (type_p->path) { |
| 2434 | DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2435 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2436 | } else if (base) { |
| 2437 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2438 | ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2439 | } else if (tpdfname) { |
| 2440 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2441 | return LY_EVALID; |
| 2442 | } else { |
| 2443 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2444 | free(*type); |
| 2445 | *type = NULL; |
| 2446 | return LY_EVALID; |
| 2447 | } |
| 2448 | if (tpdfname) { |
| 2449 | type_p->compiled = *type; |
| 2450 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2451 | } |
| 2452 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2453 | case LY_TYPE_INST: |
| 2454 | /* RFC 7950 9.9.3 - require-instance */ |
| 2455 | if (type_p->flags & LYS_SET_REQINST) { |
| 2456 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2457 | } else { |
| 2458 | /* default is true */ |
| 2459 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2460 | } |
| 2461 | |
| 2462 | if (tpdfname) { |
| 2463 | type_p->compiled = *type; |
| 2464 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2465 | } |
| 2466 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2467 | case LY_TYPE_UNION: |
| 2468 | un = (struct lysc_type_union*)(*type); |
| 2469 | |
| 2470 | /* RFC 7950 7.4 - type */ |
| 2471 | if (type_p->types) { |
| 2472 | if (base) { |
| 2473 | /* only the directly derived union can contain types specification */ |
| 2474 | if (tpdfname) { |
| 2475 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2476 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2477 | tpdfname); |
| 2478 | } else { |
| 2479 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2480 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2481 | free(*type); |
| 2482 | *type = NULL; |
| 2483 | } |
| 2484 | return LY_EVALID; |
| 2485 | } |
| 2486 | /* compile the type */ |
| 2487 | additional = 0; |
| 2488 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2489 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2490 | ret = lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], options, &un->types[u + additional], NULL); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2491 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2492 | /* add space for additional types from the union subtype */ |
| 2493 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2494 | p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types))); |
| 2495 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2496 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2497 | |
| 2498 | /* copy subtypes of the subtype union */ |
| 2499 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2500 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2501 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2502 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2503 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2504 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2505 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 2506 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 2507 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2508 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 2509 | /* TODO extensions */ |
| 2510 | |
| 2511 | } else { |
| 2512 | un->types[u + additional] = un_aux->types[v]; |
| 2513 | ++un_aux->types[v]->refcount; |
| 2514 | } |
| 2515 | ++additional; |
| 2516 | LY_ARRAY_INCREMENT(un->types); |
| 2517 | } |
| 2518 | /* compensate u increment in main loop */ |
| 2519 | --additional; |
| 2520 | |
| 2521 | /* free the replaced union subtype */ |
| 2522 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2523 | } else { |
| 2524 | LY_ARRAY_INCREMENT(un->types); |
| 2525 | } |
| 2526 | LY_CHECK_RET(ret); |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | if (!base && !type_p->flags) { |
| 2531 | /* type derived from union built-in type must contain at least one type */ |
| 2532 | if (tpdfname) { |
| 2533 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2534 | } else { |
| 2535 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2536 | free(*type); |
| 2537 | *type = NULL; |
| 2538 | } |
| 2539 | return LY_EVALID; |
| 2540 | } |
| 2541 | |
| 2542 | if (tpdfname) { |
| 2543 | type_p->compiled = *type; |
| 2544 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2545 | } |
| 2546 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2547 | case LY_TYPE_BOOL: |
| 2548 | case LY_TYPE_EMPTY: |
| 2549 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2550 | break; |
| 2551 | } |
| 2552 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2553 | done: |
| 2554 | return ret; |
| 2555 | } |
| 2556 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2557 | /** |
| 2558 | * @brief Compile information about the leaf/leaf-list's type. |
| 2559 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2560 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2561 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2562 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2563 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2564 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2565 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2566 | * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2567 | * @param[out] units Storage for inheriting units value from the typedefs the current type derives from. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2568 | * @return LY_ERR value. |
| 2569 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2570 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2571 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name, |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2572 | struct lysp_type *type_p, int options, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2573 | { |
| 2574 | LY_ERR ret = LY_SUCCESS; |
| 2575 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2576 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2577 | struct type_context { |
| 2578 | const struct lysp_tpdf *tpdf; |
| 2579 | struct lysp_node *node; |
| 2580 | struct lysp_module *mod; |
| 2581 | } *tctx, *tctx_prev = NULL; |
| 2582 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2583 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2584 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2585 | const char *dflt = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2586 | |
| 2587 | (*type) = NULL; |
| 2588 | |
| 2589 | tctx = calloc(1, sizeof *tctx); |
| 2590 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2591 | for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2592 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2593 | ret == LY_SUCCESS; |
| 2594 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2595 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2596 | if (basetype) { |
| 2597 | break; |
| 2598 | } |
| 2599 | |
| 2600 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2601 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2602 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2603 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2604 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2605 | if (units && !*units) { |
| 2606 | /* inherit units */ |
| 2607 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2608 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2609 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2610 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2611 | dflt = tctx->tpdf->dflt; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2612 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2613 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2614 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2615 | break; |
| 2616 | } |
| 2617 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2618 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2619 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2620 | * but we still may need to inherit default and units values, so start dummy loops */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2621 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2622 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2623 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2624 | dummyloops = 1; |
| 2625 | goto preparenext; |
| 2626 | } else { |
| 2627 | tctx = NULL; |
| 2628 | break; |
| 2629 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | /* store information for the following processing */ |
| 2633 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2634 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2635 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2636 | /* prepare next loop */ |
| 2637 | tctx_prev = tctx; |
| 2638 | tctx = calloc(1, sizeof *tctx); |
| 2639 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2640 | } |
| 2641 | free(tctx); |
| 2642 | |
| 2643 | /* allocate type according to the basetype */ |
| 2644 | switch (basetype) { |
| 2645 | case LY_TYPE_BINARY: |
| 2646 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2647 | break; |
| 2648 | case LY_TYPE_BITS: |
| 2649 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2650 | break; |
| 2651 | case LY_TYPE_BOOL: |
| 2652 | case LY_TYPE_EMPTY: |
| 2653 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2654 | break; |
| 2655 | case LY_TYPE_DEC64: |
| 2656 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2657 | break; |
| 2658 | case LY_TYPE_ENUM: |
| 2659 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2660 | break; |
| 2661 | case LY_TYPE_IDENT: |
| 2662 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2663 | break; |
| 2664 | case LY_TYPE_INST: |
| 2665 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2666 | break; |
| 2667 | case LY_TYPE_LEAFREF: |
| 2668 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2669 | break; |
| 2670 | case LY_TYPE_STRING: |
| 2671 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2672 | break; |
| 2673 | case LY_TYPE_UNION: |
| 2674 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2675 | break; |
| 2676 | case LY_TYPE_INT8: |
| 2677 | case LY_TYPE_UINT8: |
| 2678 | case LY_TYPE_INT16: |
| 2679 | case LY_TYPE_UINT16: |
| 2680 | case LY_TYPE_INT32: |
| 2681 | case LY_TYPE_UINT32: |
| 2682 | case LY_TYPE_INT64: |
| 2683 | case LY_TYPE_UINT64: |
| 2684 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2685 | break; |
| 2686 | case LY_TYPE_UNKNOWN: |
| 2687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2688 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 2689 | ret = LY_EVALID; |
| 2690 | goto cleanup; |
| 2691 | } |
| 2692 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2693 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2694 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 2695 | ly_data_type2str[basetype]); |
| 2696 | free(*type); |
| 2697 | (*type) = NULL; |
| 2698 | ret = LY_EVALID; |
| 2699 | goto cleanup; |
| 2700 | } |
| 2701 | |
| 2702 | /* get restrictions from the referred typedefs */ |
| 2703 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 2704 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2705 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2706 | base = tctx->tpdf->type.compiled; |
| 2707 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2708 | } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2709 | /* no change, just use the type information from the base */ |
| 2710 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 2711 | ++base->refcount; |
| 2712 | continue; |
| 2713 | } |
| 2714 | |
| 2715 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2716 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 2717 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 2718 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 2719 | ret = LY_EVALID; |
| 2720 | goto cleanup; |
| 2721 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 2722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2723 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 2724 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 2725 | ret = LY_EVALID; |
| 2726 | goto cleanup; |
| 2727 | } |
| 2728 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2729 | (*type)->basetype = basetype; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2730 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2731 | ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type, |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2732 | basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL, |
| 2733 | basetype, options, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2734 | LY_CHECK_GOTO(ret, cleanup); |
| 2735 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2736 | } |
| 2737 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2738 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2739 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2740 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2741 | (*type)->basetype = basetype; |
| 2742 | ++(*type)->refcount; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2743 | ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, options, NULL, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2744 | LY_CHECK_GOTO(ret, cleanup); |
| 2745 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2746 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 2747 | free(*type); |
| 2748 | (*type) = base; |
| 2749 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2750 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2751 | if (!(*type)->dflt) { |
| 2752 | DUP_STRING(ctx->ctx, dflt, (*type)->dflt); |
| 2753 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2754 | |
| 2755 | COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup); |
| 2756 | |
| 2757 | cleanup: |
| 2758 | ly_set_erase(&tpdf_chain, free); |
| 2759 | return ret; |
| 2760 | } |
| 2761 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2762 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent, uint16_t uses_status); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2763 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2764 | /** |
| 2765 | * @brief Compile parsed container node information. |
| 2766 | * @param[in] ctx Compile context |
| 2767 | * @param[in] node_p Parsed container node. |
| 2768 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2769 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2770 | * is enriched with the container-specific information. |
| 2771 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2772 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2773 | static LY_ERR |
| 2774 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2775 | { |
| 2776 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 2777 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 2778 | struct lysp_node *child_p; |
| 2779 | unsigned int u; |
| 2780 | LY_ERR ret = LY_SUCCESS; |
| 2781 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2782 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2783 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done); |
| 2787 | //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done); |
| 2788 | //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done); |
| 2789 | |
| 2790 | done: |
| 2791 | return ret; |
| 2792 | } |
| 2793 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2794 | /** |
| 2795 | * @brief Compile parsed leaf node information. |
| 2796 | * @param[in] ctx Compile context |
| 2797 | * @param[in] node_p Parsed leaf node. |
| 2798 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2799 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2800 | * is enriched with the leaf-specific information. |
| 2801 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2802 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2803 | static LY_ERR |
| 2804 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2805 | { |
| 2806 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 2807 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 2808 | unsigned int u; |
| 2809 | LY_ERR ret = LY_SUCCESS; |
| 2810 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2811 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2812 | DUP_STRING(ctx->ctx, leaf_p->units, leaf->units); |
| 2813 | DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2814 | if (leaf->dflt) { |
| 2815 | leaf->flags |= LYS_SET_DFLT; |
| 2816 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2817 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2818 | ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod_def->parsed, node_p->name, &leaf_p->type, options, &leaf->type, |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2819 | leaf->units ? NULL : &leaf->units); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2820 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2821 | if (!leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
| 2822 | DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 2823 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2824 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 2825 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 2826 | ly_set_add(&ctx->unres, leaf, 0); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2827 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 2828 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 2829 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 2830 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 2831 | ly_set_add(&ctx->unres, leaf, 0); |
| 2832 | } |
| 2833 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2834 | } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) { |
| 2835 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2836 | "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt); |
| 2837 | return LY_EVALID; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2838 | } |
| 2839 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2840 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
| 2841 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2842 | done: |
| 2843 | return ret; |
| 2844 | } |
| 2845 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2846 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2847 | * @brief Compile parsed leaf-list node information. |
| 2848 | * @param[in] ctx Compile context |
| 2849 | * @param[in] node_p Parsed leaf-list node. |
| 2850 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2851 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2852 | * is enriched with the leaf-list-specific information. |
| 2853 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2854 | */ |
| 2855 | static LY_ERR |
| 2856 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2857 | { |
| 2858 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 2859 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 2860 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2861 | LY_ERR ret = LY_SUCCESS; |
| 2862 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2863 | COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done); |
| 2864 | DUP_STRING(ctx->ctx, llist_p->units, llist->units); |
| 2865 | |
| 2866 | if (llist_p->dflts) { |
| 2867 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 2868 | LY_ARRAY_FOR(llist_p->dflts, u) { |
| 2869 | DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]); |
| 2870 | LY_ARRAY_INCREMENT(llist->dflts); |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | llist->min = llist_p->min; |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 2875 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2876 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2877 | ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod_def->parsed, node_p->name, &llist_p->type, options, &llist->type, |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2878 | llist->units ? NULL : &llist->units); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2879 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2880 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2881 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, 1, ret, done); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2882 | DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2883 | LY_ARRAY_INCREMENT(llist->dflts); |
| 2884 | } |
| 2885 | |
| 2886 | if (llist->type->basetype == LY_TYPE_LEAFREF) { |
| 2887 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 2888 | ly_set_add(&ctx->unres, llist, 0); |
| 2889 | } else if (llist->type->basetype == LY_TYPE_UNION) { |
| 2890 | LY_ARRAY_FOR(((struct lysc_type_union*)llist->type)->types, u) { |
| 2891 | if (((struct lysc_type_union*)llist->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 2892 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 2893 | ly_set_add(&ctx->unres, llist, 0); |
| 2894 | } |
| 2895 | } |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 2896 | } else if (llist->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2897 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 2898 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2899 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 2900 | return LY_EVALID; |
| 2901 | } else if (llist_p->dflts) { |
| 2902 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2903 | "Leaf-list of type \"empty\" must not have a default value (%s).", llist_p->dflts[0]); |
| 2904 | return LY_EVALID; |
| 2905 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 2909 | /* configuration data values must be unique - so check the default values */ |
| 2910 | LY_ARRAY_FOR(llist->dflts, u) { |
| 2911 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 2912 | if (!strcmp(llist->dflts[u], llist->dflts[v])) { |
| 2913 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2914 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]); |
| 2915 | return LY_EVALID; |
| 2916 | } |
| 2917 | } |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
| 2922 | |
| 2923 | done: |
| 2924 | return ret; |
| 2925 | } |
| 2926 | |
| 2927 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2928 | * @brief Compile parsed list node information. |
| 2929 | * @param[in] ctx Compile context |
| 2930 | * @param[in] node_p Parsed list node. |
| 2931 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2932 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2933 | * is enriched with the list-specific information. |
| 2934 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2935 | */ |
| 2936 | static LY_ERR |
| 2937 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2938 | { |
| 2939 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 2940 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 2941 | struct lysp_node *child_p; |
| 2942 | struct lysc_node_leaf **key, ***unique; |
| 2943 | size_t len; |
| 2944 | unsigned int u, v; |
| 2945 | const char *keystr, *delim; |
| 2946 | int config; |
| 2947 | LY_ERR ret = LY_SUCCESS; |
| 2948 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2949 | list->min = list_p->min; |
| 2950 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 2951 | |
| 2952 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2953 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done); |
| 2957 | |
| 2958 | /* keys */ |
| 2959 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 2960 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 2961 | return LY_EVALID; |
| 2962 | } |
| 2963 | |
| 2964 | /* find all the keys (must be direct children) */ |
| 2965 | keystr = list_p->key; |
| 2966 | while (keystr) { |
| 2967 | delim = strpbrk(keystr, " \t\n"); |
| 2968 | if (delim) { |
| 2969 | len = delim - keystr; |
| 2970 | while (isspace(*delim)) { |
| 2971 | ++delim; |
| 2972 | } |
| 2973 | } else { |
| 2974 | len = strlen(keystr); |
| 2975 | } |
| 2976 | |
| 2977 | /* key node must be present */ |
| 2978 | LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM); |
| 2979 | *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 2980 | if (!(*key)) { |
| 2981 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2982 | "The list's key \"%.*s\" not found.", len, keystr); |
| 2983 | return LY_EVALID; |
| 2984 | } |
| 2985 | /* keys must be unique */ |
| 2986 | for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) { |
| 2987 | if (*key == list->keys[u]) { |
| 2988 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2989 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 2990 | return LY_EVALID; |
| 2991 | } |
| 2992 | } |
| 2993 | /* key must have the same config flag as the list itself */ |
| 2994 | if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) { |
| 2995 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 2996 | return LY_EVALID; |
| 2997 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2998 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2999 | /* YANG 1.0 denies key to be of empty type */ |
| 3000 | if ((*key)->type->basetype == LY_TYPE_EMPTY) { |
| 3001 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3002 | "Key of a list can be of type \"empty\" only in YANG 1.1 modules."); |
| 3003 | return LY_EVALID; |
| 3004 | } |
| 3005 | } else { |
| 3006 | /* when and if-feature are illegal on list keys */ |
| 3007 | if ((*key)->when) { |
| 3008 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3009 | "List's key \"%s\" must not have any \"when\" statement.", (*key)->name); |
| 3010 | return LY_EVALID; |
| 3011 | } |
| 3012 | if ((*key)->iffeatures) { |
| 3013 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3014 | "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name); |
| 3015 | return LY_EVALID; |
| 3016 | } |
| 3017 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3018 | |
| 3019 | /* check status */ |
| 3020 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3021 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3022 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3023 | /* ignore default values of the key */ |
| 3024 | if ((*key)->dflt) { |
| 3025 | lydict_remove(ctx->ctx, (*key)->dflt); |
| 3026 | (*key)->dflt = NULL; |
| 3027 | } |
| 3028 | /* mark leaf as key */ |
| 3029 | (*key)->flags |= LYS_KEY; |
| 3030 | |
| 3031 | /* next key value */ |
| 3032 | keystr = delim; |
| 3033 | } |
| 3034 | |
| 3035 | /* uniques */ |
| 3036 | if (list_p->uniques) { |
| 3037 | for (v = 0; v < LY_ARRAY_SIZE(list_p->uniques); ++v) { |
| 3038 | config = -1; |
| 3039 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3040 | keystr = list_p->uniques[v]; |
| 3041 | while (keystr) { |
| 3042 | delim = strpbrk(keystr, " \t\n"); |
| 3043 | if (delim) { |
| 3044 | len = delim - keystr; |
| 3045 | while (isspace(*delim)) { |
| 3046 | ++delim; |
| 3047 | } |
| 3048 | } else { |
| 3049 | len = strlen(keystr); |
| 3050 | } |
| 3051 | |
| 3052 | /* unique node must be present */ |
| 3053 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3054 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, node, LYS_LEAF, 0, (const struct lysc_node**)key); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3055 | if (ret != LY_SUCCESS) { |
| 3056 | if (ret == LY_EDENIED) { |
| 3057 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3058 | "Unique's descendant-schema-nodeid \"%.*s\" refers to a %s node instead of a leaf.", |
| 3059 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3060 | } |
| 3061 | return LY_EVALID; |
| 3062 | } |
| 3063 | |
| 3064 | /* all referenced leafs must be of the same config type */ |
| 3065 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3066 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3067 | "Unique statement \"%s\" refers to leafs with different config type.", list_p->uniques[v]); |
| 3068 | return LY_EVALID; |
| 3069 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3070 | config = 1; |
| 3071 | } else { /* LYS_CONFIG_R */ |
| 3072 | config = 0; |
| 3073 | } |
| 3074 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3075 | /* check status */ |
| 3076 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3077 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3078 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3079 | /* mark leaf as unique */ |
| 3080 | (*key)->flags |= LYS_UNIQUE; |
| 3081 | |
| 3082 | /* next unique value in line */ |
| 3083 | keystr = delim; |
| 3084 | } |
| 3085 | /* next unique definition */ |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | //COMPILE_ARRAY_GOTO(ctx, list_p->actions, list->actions, options, u, lys_compile_action, ret, done); |
| 3090 | //COMPILE_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, options, u, lys_compile_notif, ret, done); |
| 3091 | |
| 3092 | done: |
| 3093 | return ret; |
| 3094 | } |
| 3095 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3096 | static LY_ERR |
| 3097 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3098 | { |
| 3099 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3100 | const char *prefix = NULL, *name; |
| 3101 | size_t prefix_len = 0; |
| 3102 | |
| 3103 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3104 | name = strchr(dflt, ':'); |
| 3105 | if (name) { |
| 3106 | prefix = dflt; |
| 3107 | prefix_len = name - prefix; |
| 3108 | ++name; |
| 3109 | } else { |
| 3110 | name = dflt; |
| 3111 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3112 | if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3113 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3114 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3115 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3116 | prefix_len, prefix); |
| 3117 | return LY_EVALID; |
| 3118 | } |
| 3119 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3120 | if (!ch->dflt) { |
| 3121 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3122 | "Default case \"%s\" not found.", dflt); |
| 3123 | return LY_EVALID; |
| 3124 | } |
| 3125 | /* no mandatory nodes directly under the default case */ |
| 3126 | LY_LIST_FOR(ch->dflt->child, iter) { |
| 3127 | if (iter->flags & LYS_MAND_TRUE) { |
| 3128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3129 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3130 | return LY_EVALID; |
| 3131 | } |
| 3132 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3133 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3134 | return LY_SUCCESS; |
| 3135 | } |
| 3136 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3137 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3138 | * @brief Compile parsed choice node information. |
| 3139 | * @param[in] ctx Compile context |
| 3140 | * @param[in] node_p Parsed choice node. |
| 3141 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3142 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3143 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3144 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3145 | */ |
| 3146 | static LY_ERR |
| 3147 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3148 | { |
| 3149 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 3150 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 3151 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3152 | struct lys_module; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3153 | LY_ERR ret = LY_SUCCESS; |
| 3154 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3155 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3156 | if (child_p->nodetype == LYS_CASE) { |
| 3157 | LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3158 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3159 | } |
| 3160 | } else { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3161 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3166 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3167 | LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3168 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3169 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3170 | return ret; |
| 3171 | } |
| 3172 | |
| 3173 | /** |
| 3174 | * @brief Compile parsed anydata or anyxml node information. |
| 3175 | * @param[in] ctx Compile context |
| 3176 | * @param[in] node_p Parsed anydata or anyxml node. |
| 3177 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3178 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3179 | * is enriched with the any-specific information. |
| 3180 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3181 | */ |
| 3182 | static LY_ERR |
| 3183 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3184 | { |
| 3185 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 3186 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 3187 | unsigned int u; |
| 3188 | LY_ERR ret = LY_SUCCESS; |
| 3189 | |
| 3190 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done); |
| 3191 | |
| 3192 | if (any->flags & LYS_CONFIG_W) { |
| 3193 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
| 3194 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML)); |
| 3195 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3196 | done: |
| 3197 | return ret; |
| 3198 | } |
| 3199 | |
| 3200 | static LY_ERR |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3201 | lys_compile_status_check(struct lysc_ctx *ctx, uint16_t node_flags, uint16_t parent_flags) |
| 3202 | { |
| 3203 | /* check status compatibility with the parent */ |
| 3204 | if ((parent_flags & LYS_STATUS_MASK) > (node_flags & LYS_STATUS_MASK)) { |
| 3205 | if (node_flags & LYS_STATUS_CURR) { |
| 3206 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3207 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3208 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3209 | } else { /* LYS_STATUS_DEPRC */ |
| 3210 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3211 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3212 | } |
| 3213 | return LY_EVALID; |
| 3214 | } |
| 3215 | return LY_SUCCESS; |
| 3216 | } |
| 3217 | |
| 3218 | static LY_ERR |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3219 | lys_compile_status(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t parent_flags) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3220 | { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3221 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3222 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3223 | if (!(node->flags & LYS_STATUS_MASK)) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3224 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3225 | if ((parent_flags & 0x3) != 0x3) { |
| 3226 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3227 | * combination of bits (0x3) which marks the uses_status value */ |
| 3228 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3229 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3230 | } |
| 3231 | node->flags |= parent_flags & LYS_STATUS_MASK; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3232 | } else { |
| 3233 | node->flags |= LYS_STATUS_CURR; |
| 3234 | } |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3235 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3236 | return lys_compile_status_check(ctx, node->flags, parent_flags); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3237 | } |
| 3238 | return LY_SUCCESS; |
| 3239 | } |
| 3240 | |
| 3241 | static LY_ERR |
| 3242 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3243 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3244 | const char *name, void *exclude) |
| 3245 | { |
| 3246 | const struct lysc_node *iter; |
| 3247 | unsigned int u; |
| 3248 | |
| 3249 | LY_LIST_FOR(children, iter) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3250 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3251 | goto error; |
| 3252 | } |
| 3253 | } |
| 3254 | LY_ARRAY_FOR(actions, u) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3255 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3256 | goto error; |
| 3257 | } |
| 3258 | } |
| 3259 | LY_ARRAY_FOR(notifs, u) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3260 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3261 | goto error; |
| 3262 | } |
| 3263 | } |
| 3264 | return LY_SUCCESS; |
| 3265 | error: |
| 3266 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition"); |
| 3267 | return LY_EEXIST; |
| 3268 | } |
| 3269 | |
| 3270 | /** |
| 3271 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 3272 | * |
| 3273 | * @param[in] ctx Compile context |
| 3274 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 3275 | * the choice itself is expected instead of a specific case node. |
| 3276 | * @param[in] node Schema node to connect into the list. |
| 3277 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 3278 | */ |
| 3279 | static LY_ERR |
| 3280 | lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node) |
| 3281 | { |
| 3282 | struct lysc_node **children; |
| 3283 | |
| 3284 | if (node->nodetype == LYS_CASE) { |
| 3285 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 3286 | } else { |
| 3287 | children = lysc_node_children_p(parent); |
| 3288 | } |
| 3289 | if (children) { |
| 3290 | if (!(*children)) { |
| 3291 | /* first child */ |
| 3292 | *children = node; |
| 3293 | } else if (*children != node) { |
| 3294 | /* by the condition in previous branch we cover the choice/case children |
| 3295 | * - the children list is shared by the choice and the the first case, in addition |
| 3296 | * the first child of each case must be referenced from the case node. So the node is |
| 3297 | * actually always already inserted in case it is the first children - so here such |
| 3298 | * a situation actually corresponds to the first branch */ |
| 3299 | /* insert at the end of the parent's children list */ |
| 3300 | (*children)->prev->next = node; |
| 3301 | node->prev = (*children)->prev; |
| 3302 | (*children)->prev = node; |
| 3303 | |
| 3304 | /* check the name uniqueness */ |
| 3305 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 3306 | lysc_node_notifs(parent), node->name, node)) { |
| 3307 | return LY_EEXIST; |
| 3308 | } |
| 3309 | } |
| 3310 | } |
| 3311 | return LY_SUCCESS; |
| 3312 | } |
| 3313 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3314 | /** |
| 3315 | * @param[in] node_p Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case, |
| 3316 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
| 3317 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3318 | static struct lysc_node_case* |
| 3319 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child) |
| 3320 | { |
| 3321 | struct lysc_node *iter; |
| 3322 | struct lysc_node_case *cs; |
| 3323 | unsigned int u; |
| 3324 | LY_ERR ret; |
| 3325 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3326 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3327 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3328 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3329 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 3330 | return NULL; \ |
| 3331 | } \ |
| 3332 | } |
| 3333 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3334 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 3335 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3336 | |
| 3337 | /* we have to add an implicit case node into the parent choice */ |
| 3338 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3339 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 3340 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3341 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3342 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 3343 | /* the case is already present since the child is not its first children */ |
| 3344 | return (struct lysc_node_case*)ch->cases->prev; |
| 3345 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3346 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3347 | |
| 3348 | /* explicit parent case is not present (this is its first child) */ |
| 3349 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3350 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 3351 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 3352 | cs->sp = node_p; |
| 3353 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3354 | /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */ |
| 3355 | LY_CHECK_RET(lys_compile_status(ctx, (struct lysc_node*)cs, ch->flags), NULL); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3356 | COMPILE_MEMBER_GOTO(ctx, node_p->when, cs->when, options, lys_compile_when, ret, error); |
| 3357 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3358 | } else { |
| 3359 | LOGINT(ctx->ctx); |
| 3360 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3361 | } |
| 3362 | cs->module = ctx->mod; |
| 3363 | cs->prev = (struct lysc_node*)cs; |
| 3364 | cs->nodetype = LYS_CASE; |
| 3365 | lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs); |
| 3366 | cs->parent = (struct lysc_node*)ch; |
| 3367 | cs->child = child; |
| 3368 | |
| 3369 | return cs; |
| 3370 | error: |
| 3371 | return NULL; |
| 3372 | |
| 3373 | #undef UNIQUE_CHECK |
| 3374 | } |
| 3375 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3376 | static LY_ERR |
| 3377 | lys_compile_refine_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_refine *rfn, int inheriting) |
| 3378 | { |
| 3379 | struct lysc_node *child; |
| 3380 | uint16_t config = rfn->flags & LYS_CONFIG_MASK; |
| 3381 | |
| 3382 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 3383 | /* nothing to do */ |
| 3384 | return LY_SUCCESS; |
| 3385 | } |
| 3386 | |
| 3387 | if (!inheriting) { |
| 3388 | /* explicit refine */ |
| 3389 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 3390 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3391 | "Invalid refine of config in \"%s\" - configuration node cannot be child of any state data node.", |
| 3392 | rfn->nodeid); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3393 | return LY_EVALID; |
| 3394 | } |
| 3395 | } |
| 3396 | node->flags &= ~LYS_CONFIG_MASK; |
| 3397 | node->flags |= config; |
| 3398 | |
| 3399 | /* inherit the change into the children */ |
| 3400 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node), child) { |
| 3401 | LY_CHECK_RET(lys_compile_refine_config(ctx, child, rfn, 1)); |
| 3402 | } |
| 3403 | |
| 3404 | /* TODO actions and notifications */ |
| 3405 | |
| 3406 | return LY_SUCCESS; |
| 3407 | } |
| 3408 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3409 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3410 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 3411 | * If present, also apply uses's modificators. |
| 3412 | * |
| 3413 | * @param[in] ctx Compile context |
| 3414 | * @param[in] uses_p Parsed uses schema node. |
| 3415 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3416 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 3417 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 3418 | * the compile context. |
| 3419 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3420 | */ |
| 3421 | static LY_ERR |
| 3422 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent) |
| 3423 | { |
| 3424 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3425 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 3426 | /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3427 | struct lysc_node_container context_node_fake = |
| 3428 | {.nodetype = LYS_CONTAINER, |
| 3429 | .module = ctx->mod, |
| 3430 | .flags = parent ? parent->flags : 0, |
| 3431 | .child = NULL, .next = NULL, |
| 3432 | .prev = (struct lysc_node*)&context_node_fake}; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3433 | const struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3434 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3435 | int found; |
| 3436 | const char *id, *name, *prefix; |
| 3437 | size_t prefix_len, name_len; |
| 3438 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3439 | struct lysp_refine *rfn; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3440 | LY_ERR ret = LY_EVALID; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 3441 | uint32_t min, max; |
| 3442 | struct ly_set refined = {0}; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3443 | |
| 3444 | /* search for the grouping definition */ |
| 3445 | found = 0; |
| 3446 | id = uses_p->name; |
| 3447 | lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len); |
| 3448 | if (prefix) { |
| 3449 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 3450 | if (!mod) { |
| 3451 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3452 | "Invalid prefix used for grouping reference (%s).", uses_p->name); |
| 3453 | return LY_EVALID; |
| 3454 | } |
| 3455 | } else { |
| 3456 | mod = ctx->mod_def; |
| 3457 | } |
| 3458 | if (mod == ctx->mod_def) { |
| 3459 | for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) { |
| 3460 | grp = lysp_node_groupings(node_p); |
| 3461 | LY_ARRAY_FOR(grp, u) { |
| 3462 | if (!strcmp(grp[u].name, name)) { |
| 3463 | grp = &grp[u]; |
| 3464 | found = 1; |
| 3465 | break; |
| 3466 | } |
| 3467 | } |
| 3468 | } |
| 3469 | } |
| 3470 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3471 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3472 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3473 | if (grp) { |
| 3474 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 3475 | if (!strcmp(grp[u].name, name)) { |
| 3476 | grp = &grp[u]; |
| 3477 | found = 1; |
| 3478 | } |
| 3479 | } |
| 3480 | } |
| 3481 | if (!found && mod->parsed->includes) { |
| 3482 | /* ... and all the submodules */ |
| 3483 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 3484 | grp = mod->parsed->includes[u].submodule->groupings; |
| 3485 | if (grp) { |
| 3486 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 3487 | if (!strcmp(grp[v].name, name)) { |
| 3488 | grp = &grp[v]; |
| 3489 | found = 1; |
| 3490 | } |
| 3491 | } |
| 3492 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3493 | } |
| 3494 | } |
| 3495 | } |
| 3496 | if (!found) { |
| 3497 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3498 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 3499 | return LY_EVALID; |
| 3500 | } |
| 3501 | |
| 3502 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 3503 | grp_stack_count = ctx->groupings.count; |
| 3504 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 3505 | if (grp_stack_count == ctx->groupings.count) { |
| 3506 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 3507 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3508 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 3509 | return LY_EVALID; |
| 3510 | } |
| 3511 | |
| 3512 | /* switch context's mod_def */ |
| 3513 | mod_old = ctx->mod_def; |
| 3514 | ctx->mod_def = mod; |
| 3515 | |
| 3516 | /* check status */ |
| 3517 | LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), error); |
| 3518 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3519 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3520 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
| 3521 | LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), error); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3522 | child = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3523 | if (uses_p->refines) { |
| 3524 | /* some preparation for applying refines */ |
| 3525 | if (grp->data == node_p) { |
| 3526 | /* remember the first child */ |
| 3527 | context_node_fake.child = child; |
| 3528 | } |
| 3529 | } |
| 3530 | } |
| 3531 | LY_LIST_FOR(context_node_fake.child, child) { |
| 3532 | child->parent = (struct lysc_node*)&context_node_fake; |
| 3533 | } |
| 3534 | if (context_node_fake.child) { |
| 3535 | child = context_node_fake.child->prev; |
| 3536 | context_node_fake.child->prev = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3537 | } |
| 3538 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 3539 | /* TODO: apply augment */ |
| 3540 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 3541 | /* reload previous context's mod_def */ |
| 3542 | ctx->mod_def = mod_old; |
| 3543 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3544 | /* apply refine */ |
| 3545 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3546 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, 0, 0, (const struct lysc_node**)&node), |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3547 | error); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 3548 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3549 | |
| 3550 | /* default value */ |
| 3551 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3552 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3554 | "Invalid refine of default in \"%s\" - %s cannot hold %d default values.", |
| 3555 | rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
| 3556 | goto error; |
| 3557 | } |
| 3558 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 3559 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3560 | "Invalid refine of default in \"%s\" - %s cannot hold default value(s).", |
| 3561 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 3562 | goto error; |
| 3563 | } |
| 3564 | if (node->nodetype == LYS_LEAF) { |
| 3565 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 3566 | DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3567 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3568 | /* TODO check the default value according to type */ |
| 3569 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3570 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3571 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3572 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
| 3573 | goto error; |
| 3574 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3575 | LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) { |
| 3576 | lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 3577 | } |
| 3578 | LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3579 | ((struct lysc_node_leaflist*)node)->dflts = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3580 | LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, error); |
| 3581 | LY_ARRAY_FOR(rfn->dflts, u) { |
| 3582 | LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts); |
| 3583 | DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]); |
| 3584 | } |
| 3585 | /* TODO check the default values according to type */ |
| 3586 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3587 | if (((struct lysc_node_choice*)node)->dflt) { |
| 3588 | /* unset LYS_SET_DFLT from the current default case */ |
| 3589 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 3590 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3591 | LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), error); |
| 3592 | } |
| 3593 | } |
| 3594 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 3595 | /* description */ |
| 3596 | if (rfn->dsc) { |
| 3597 | FREE_STRING(ctx->ctx, node->dsc); |
| 3598 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 3599 | } |
| 3600 | |
| 3601 | /* reference */ |
| 3602 | if (rfn->ref) { |
| 3603 | FREE_STRING(ctx->ctx, node->ref); |
| 3604 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 3605 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3606 | |
| 3607 | /* config */ |
| 3608 | if (rfn->flags & LYS_CONFIG_MASK) { |
| 3609 | LY_CHECK_GOTO(lys_compile_refine_config(ctx, node, rfn, 0), error); |
| 3610 | } |
| 3611 | |
| 3612 | /* mandatory */ |
| 3613 | if (rfn->flags & LYS_MAND_MASK) { |
| 3614 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 3615 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3616 | "Invalid refine of mandatory in \"%s\" - %s cannot hold mandatory statement.", |
| 3617 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 3618 | goto error; |
| 3619 | } |
| 3620 | /* in compiled flags, only the LYS_MAND_TRUE is present */ |
| 3621 | if (rfn->flags & LYS_MAND_TRUE) { |
| 3622 | /* check if node has default value */ |
| 3623 | if (node->nodetype & LYS_LEAF) { |
| 3624 | if (node->flags & LYS_SET_DFLT) { |
| 3625 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3626 | "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", rfn->nodeid); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3627 | goto error; |
| 3628 | } else { |
| 3629 | /* remove the default value taken from the leaf's type */ |
| 3630 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt); |
| 3631 | ((struct lysc_node_leaf*)node)->dflt = NULL; |
| 3632 | } |
| 3633 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
| 3634 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3635 | "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", rfn->nodeid); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3636 | goto error; |
| 3637 | } |
| 3638 | if (node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
| 3639 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3640 | "Invalid refine of mandatory in \"%s\" - %s under the default case.", |
| 3641 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 3642 | goto error; |
| 3643 | } |
| 3644 | |
| 3645 | node->flags |= LYS_MAND_TRUE; |
| 3646 | } else { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3647 | /* make mandatory false */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3648 | node->flags &= ~LYS_MAND_TRUE; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3649 | if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) { |
| 3650 | /* get the type's default value if any */ |
| 3651 | DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt); |
| 3652 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3653 | } |
| 3654 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 3655 | |
| 3656 | /* presence */ |
| 3657 | if (rfn->presence) { |
| 3658 | if (node->nodetype != LYS_CONTAINER) { |
| 3659 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3660 | "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.", |
| 3661 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 3662 | goto error; |
| 3663 | } |
| 3664 | node->flags |= LYS_PRESENCE; |
| 3665 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 3666 | |
| 3667 | /* must */ |
| 3668 | if (rfn->musts) { |
| 3669 | switch (node->nodetype) { |
| 3670 | case LYS_LEAF: |
| 3671 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, error); |
| 3672 | break; |
| 3673 | case LYS_LEAFLIST: |
| 3674 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, error); |
| 3675 | break; |
| 3676 | case LYS_LIST: |
| 3677 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, error); |
| 3678 | break; |
| 3679 | case LYS_CONTAINER: |
| 3680 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, error); |
| 3681 | break; |
| 3682 | case LYS_ANYXML: |
| 3683 | case LYS_ANYDATA: |
| 3684 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, error); |
| 3685 | break; |
| 3686 | default: |
| 3687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3688 | "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.", |
| 3689 | rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 3690 | goto error; |
| 3691 | } |
| 3692 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 3693 | |
| 3694 | /* min/max-elements */ |
| 3695 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 3696 | switch (node->nodetype) { |
| 3697 | case LYS_LEAFLIST: |
| 3698 | if (rfn->flags & LYS_SET_MAX) { |
| 3699 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 3700 | } |
| 3701 | if (rfn->flags & LYS_SET_MIN) { |
| 3702 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
| 3703 | } |
| 3704 | break; |
| 3705 | case LYS_LIST: |
| 3706 | if (rfn->flags & LYS_SET_MAX) { |
| 3707 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 3708 | } |
| 3709 | if (rfn->flags & LYS_SET_MIN) { |
| 3710 | ((struct lysc_node_list*)node)->min = rfn->min; |
| 3711 | } |
| 3712 | break; |
| 3713 | default: |
| 3714 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3715 | "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.", |
| 3716 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype)); |
| 3717 | goto error; |
| 3718 | } |
| 3719 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 3720 | |
| 3721 | /* if-feature */ |
| 3722 | if (rfn->iffeatures) { |
| 3723 | /* any node in compiled tree can get additional if-feature, so do not check nodetype */ |
| 3724 | COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error); |
| 3725 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3726 | } |
| 3727 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 3728 | if (context_node_fake.child) { |
| 3729 | context_node_fake.child->prev = child; |
| 3730 | } |
| 3731 | LY_LIST_FOR(context_node_fake.child, child) { |
| 3732 | child->parent = parent; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3733 | } |
| 3734 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 3735 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 3736 | for (u = 0; u < refined.count; ++u) { |
| 3737 | node = (struct lysc_node*)refined.objs[u]; |
| 3738 | rfn = &uses_p->refines[u]; |
| 3739 | |
| 3740 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 3741 | if ((node->flags & LYS_MAND_TRUE) && |
| 3742 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 3743 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 3744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3745 | "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid); |
| 3746 | goto error; |
| 3747 | } |
| 3748 | |
| 3749 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 3750 | if (node->nodetype == LYS_LIST) { |
| 3751 | min = ((struct lysc_node_list*)node)->min; |
| 3752 | max = ((struct lysc_node_list*)node)->max; |
| 3753 | } else { |
| 3754 | min = ((struct lysc_node_leaflist*)node)->min; |
| 3755 | max = ((struct lysc_node_leaflist*)node)->max; |
| 3756 | } |
| 3757 | if (min > max) { |
| 3758 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3759 | "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".", |
| 3760 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid); |
| 3761 | goto error; |
| 3762 | } |
| 3763 | } |
| 3764 | } |
| 3765 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3766 | ret = LY_SUCCESS; |
| 3767 | error: |
| 3768 | /* reload previous context's mod_def */ |
| 3769 | ctx->mod_def = mod_old; |
| 3770 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 3771 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 3772 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 3773 | ly_set_erase(&refined, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3774 | |
| 3775 | return ret; |
| 3776 | } |
| 3777 | |
| 3778 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3779 | * @brief Compile parsed schema node information. |
| 3780 | * @param[in] ctx Compile context |
| 3781 | * @param[in] node_p Parsed schema node. |
| 3782 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3783 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 3784 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 3785 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3786 | * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags). |
| 3787 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3788 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3789 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3790 | static LY_ERR |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3791 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent, uint16_t uses_status) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3792 | { |
| 3793 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3794 | struct lysc_node *node; |
| 3795 | struct lysc_node_case *cs; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3796 | unsigned int u; |
| 3797 | LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*); |
| 3798 | |
| 3799 | switch (node_p->nodetype) { |
| 3800 | case LYS_CONTAINER: |
| 3801 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 3802 | node_compile_spec = lys_compile_node_container; |
| 3803 | break; |
| 3804 | case LYS_LEAF: |
| 3805 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 3806 | node_compile_spec = lys_compile_node_leaf; |
| 3807 | break; |
| 3808 | case LYS_LIST: |
| 3809 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3810 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3811 | break; |
| 3812 | case LYS_LEAFLIST: |
| 3813 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3814 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3815 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3816 | case LYS_CHOICE: |
| 3817 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3818 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3819 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3820 | case LYS_ANYXML: |
| 3821 | case LYS_ANYDATA: |
| 3822 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3823 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3824 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3825 | case LYS_USES: |
| 3826 | return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3827 | default: |
| 3828 | LOGINT(ctx->ctx); |
| 3829 | return LY_EINT; |
| 3830 | } |
| 3831 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 3832 | node->nodetype = node_p->nodetype; |
| 3833 | node->module = ctx->mod; |
| 3834 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3835 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3836 | |
| 3837 | /* config */ |
| 3838 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 3839 | /* config not explicitely set, inherit it from parent */ |
| 3840 | if (parent) { |
| 3841 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 3842 | } else { |
| 3843 | /* default is config true */ |
| 3844 | node->flags |= LYS_CONFIG_W; |
| 3845 | } |
| 3846 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3847 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 3848 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3849 | "Configuration node cannot be child of any state data node."); |
| 3850 | goto error; |
| 3851 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3852 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 3853 | /* *list ordering */ |
| 3854 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 3855 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
| 3856 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, " |
| 3857 | "RPC/action output parameters or notification content (%s).", ctx->path); |
| 3858 | node->flags &= ~LYS_ORDBY_MASK; |
| 3859 | node->flags |= LYS_ORDBY_SYSTEM; |
| 3860 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 3861 | /* default ordering is system */ |
| 3862 | node->flags |= LYS_ORDBY_SYSTEM; |
| 3863 | } |
| 3864 | } |
| 3865 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3866 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3867 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3868 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 3869 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 3870 | * the parent is choice (parent here) or some case (so we have to get its flags to check) */ |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3871 | LY_CHECK_GOTO(lys_compile_status(ctx, node, uses_status ? uses_status : (parent ? parent->flags : 0)), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | if (!(options & LYSC_OPT_FREE_SP)) { |
| 3875 | node->sp = node_p; |
| 3876 | } |
| 3877 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 3878 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 3879 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3880 | COMPILE_MEMBER_GOTO(ctx, node_p->when, node->when, options, lys_compile_when, ret, error); |
| 3881 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3882 | COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error); |
| 3883 | |
| 3884 | /* nodetype-specific part */ |
| 3885 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error); |
| 3886 | |
| 3887 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3888 | if (parent) { |
| 3889 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3890 | cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node); |
| 3891 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3892 | if (uses_status) { |
| 3893 | |
| 3894 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3895 | /* the postponed status check of the node and its real parent - in case of implicit case, |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 3896 | * it directly gets the same status flags as the choice; |
| 3897 | * uses_status cannot be applied here since uses cannot be child statement of choice */ |
| 3898 | LY_CHECK_GOTO(lys_compile_status(ctx, node, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3899 | node->parent = (struct lysc_node*)cs; |
| 3900 | } else { /* other than choice */ |
| 3901 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3902 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3903 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3904 | } else { |
| 3905 | /* top-level element */ |
| 3906 | if (!ctx->mod->compiled->data) { |
| 3907 | ctx->mod->compiled->data = node; |
| 3908 | } else { |
| 3909 | /* insert at the end of the module's top-level nodes list */ |
| 3910 | ctx->mod->compiled->data->prev->next = node; |
| 3911 | node->prev = ctx->mod->compiled->data->prev; |
| 3912 | ctx->mod->compiled->data->prev = node; |
| 3913 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3914 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 3915 | ctx->mod->compiled->notifs, node->name, node)) { |
| 3916 | return LY_EVALID; |
| 3917 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | return LY_SUCCESS; |
| 3921 | |
| 3922 | error: |
| 3923 | lysc_node_free(ctx->ctx, node); |
| 3924 | return ret; |
| 3925 | } |
| 3926 | |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 3927 | static void |
| 3928 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 3929 | { |
| 3930 | unsigned int v; |
| 3931 | size_t len; |
| 3932 | |
| 3933 | len = strlen(aug_p->nodeid); |
| 3934 | LY_ARRAY_FOR(result, v) { |
| 3935 | if (strlen(result[v]->nodeid) <= len) { |
| 3936 | continue; |
| 3937 | } |
| 3938 | if (v < LY_ARRAY_SIZE(result)) { |
| 3939 | /* move the rest of array */ |
| 3940 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 3941 | break; |
| 3942 | } |
| 3943 | } |
| 3944 | result[v] = aug_p; |
| 3945 | LY_ARRAY_INCREMENT(result); |
| 3946 | } |
| 3947 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3948 | LY_ERR |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 3949 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_module *mod_p, struct lysp_augment ***augments) |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3950 | { |
| 3951 | struct lysp_augment **result = NULL; |
| 3952 | unsigned int u, v; |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 3953 | size_t count = 0; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3954 | |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 3955 | assert(mod_p); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3956 | assert(augments); |
| 3957 | |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 3958 | /* get count of the augments in module and all its submodules */ |
| 3959 | if (mod_p->augments) { |
| 3960 | count += LY_ARRAY_SIZE(mod_p->augments); |
| 3961 | } |
| 3962 | LY_ARRAY_FOR(mod_p->includes, u) { |
| 3963 | if (mod_p->includes[u].submodule->augments) { |
| 3964 | count += LY_ARRAY_SIZE(mod_p->includes[u].submodule->augments); |
| 3965 | } |
| 3966 | } |
| 3967 | |
| 3968 | if (!count) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3969 | *augments = NULL; |
| 3970 | return LY_SUCCESS; |
| 3971 | } |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 3972 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3973 | |
| 3974 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 3975 | * together, so there can be even /z/y betwwen them. */ |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 3976 | LY_ARRAY_FOR(mod_p->augments, u) { |
| 3977 | lys_compile_augment_sort_(&mod_p->augments[u], result); |
| 3978 | } |
| 3979 | LY_ARRAY_FOR(mod_p->includes, u) { |
| 3980 | LY_ARRAY_FOR(mod_p->includes[u].submodule->augments, v) { |
| 3981 | lys_compile_augment_sort_(&mod_p->includes[u].submodule->augments[v], result); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3982 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3983 | } |
| 3984 | |
| 3985 | *augments = result; |
| 3986 | return LY_SUCCESS; |
| 3987 | } |
| 3988 | |
| 3989 | /** |
| 3990 | * @brief Compile the parsed augment connecting it into its target. |
| 3991 | * |
| 3992 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 3993 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 3994 | * are already implemented and compiled. |
| 3995 | * |
| 3996 | * @param[in] ctx Compile context. |
| 3997 | * @param[in] aug_p Parsed augment to compile. |
| 3998 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3999 | * @param[in] parent Parent node to provide the augment's context. It is NULL for the top level augments and a node holding uses's |
| 4000 | * children in case of the augmenting uses data. |
| 4001 | * @return LY_SUCCESS on success. |
| 4002 | * @return LY_EVALID on failure. |
| 4003 | */ |
| 4004 | LY_ERR |
| 4005 | lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent) |
| 4006 | { |
| 4007 | LY_ERR ret = LY_SUCCESS; |
| 4008 | struct lysp_node *node_p, *case_node_p; |
| 4009 | struct lysc_node *target; /* target target of the augment */ |
| 4010 | |
| 4011 | ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, |
| 4012 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
| 4013 | 1, (const struct lysc_node**)&target); |
| 4014 | if (ret != LY_SUCCESS) { |
| 4015 | if (ret == LY_EDENIED) { |
| 4016 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4017 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4018 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4019 | } |
| 4020 | return LY_EVALID; |
| 4021 | } |
| 4022 | |
| 4023 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4024 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4025 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4026 | && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF))) |
| 4027 | && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES))) { |
| 4028 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4029 | "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".", |
| 4030 | aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
| 4031 | return LY_EVALID; |
| 4032 | } |
| 4033 | /* compile the children */ |
| 4034 | if (node_p->nodetype != LYS_CASE) { |
| 4035 | LY_CHECK_RET(lys_compile_node(ctx, node_p, options, target, 0)); |
| 4036 | } else { |
| 4037 | LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) { |
| 4038 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options, target, 0)); |
| 4039 | } |
| 4040 | } |
| 4041 | } |
| 4042 | /* TODO actions, notifications */ |
| 4043 | |
| 4044 | return ret; |
| 4045 | } |
| 4046 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4047 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 4048 | * @brief Compile the given YANG submodule into the main module. |
| 4049 | * @param[in] ctx Compile context |
| 4050 | * @param[in] inc Include structure from the main module defining the submodule. |
| 4051 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 4052 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4053 | */ |
| 4054 | LY_ERR |
| 4055 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options) |
| 4056 | { |
| 4057 | unsigned int u; |
| 4058 | LY_ERR ret = LY_SUCCESS; |
| 4059 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4060 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 4061 | struct lysc_module *mainmod = ctx->mod->compiled; |
| 4062 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 4063 | if (!mainmod->mod->off_features) { |
| 4064 | /* features are compiled directly into the compiled module structure, |
| 4065 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 4066 | * The features compilation is finished in the main module (lys_compile()). */ |
| 4067 | ret = lys_feature_precompile(ctx->ctx, submod->features, |
| 4068 | mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features); |
| 4069 | LY_CHECK_GOTO(ret, error); |
| 4070 | } |
| 4071 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 4072 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error); |
| 4073 | |
| 4074 | error: |
| 4075 | return ret; |
| 4076 | } |
| 4077 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4078 | |
| 4079 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4080 | LY_ERR |
| 4081 | lys_compile(struct lys_module *mod, int options) |
| 4082 | { |
| 4083 | struct lysc_ctx ctx = {0}; |
| 4084 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 4085 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4086 | struct lysp_module *sp; |
| 4087 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4088 | struct lysp_augment **augments = NULL; |
| 4089 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 4090 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 4091 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4092 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4093 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 4094 | |
| 4095 | if (!mod->implemented) { |
| 4096 | /* just imported modules are not compiled */ |
| 4097 | return LY_SUCCESS; |
| 4098 | } |
| 4099 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4100 | sp = mod->parsed; |
| 4101 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4102 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4103 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4104 | ctx.mod_def = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4105 | |
| 4106 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4107 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 4108 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4109 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4110 | COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 4111 | LY_ARRAY_FOR(sp->includes, u) { |
| 4112 | ret = lys_compile_submodule(&ctx, &sp->includes[u], options); |
| 4113 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 4114 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 4115 | if (mod->off_features) { |
| 4116 | /* there is already precompiled array of features */ |
| 4117 | mod_c->features = mod->off_features; |
| 4118 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 4119 | } else { |
| 4120 | /* features are compiled directly into the compiled module structure, |
| 4121 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
| 4122 | ret = lys_feature_precompile(ctx.ctx, sp->features, &mod_c->features); |
| 4123 | LY_CHECK_GOTO(ret, error); |
| 4124 | } |
| 4125 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 4126 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 4127 | * are present. */ |
| 4128 | LY_ARRAY_FOR(sp->features, u) { |
| 4129 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features); |
| 4130 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 4131 | } |
| 4132 | LY_ARRAY_FOR(sp->includes, v) { |
| 4133 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
| 4134 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features); |
| 4135 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 4136 | } |
| 4137 | } |
| 4138 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 4139 | COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4140 | if (sp->identities) { |
| 4141 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 4142 | } |
| 4143 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4144 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4145 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4146 | ret = lys_compile_node(&ctx, node_p, options, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4147 | LY_CHECK_GOTO(ret, error); |
| 4148 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4149 | |
| 4150 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | f12a1f0 | 2019-02-11 16:42:08 +0100 | [diff] [blame] | 4151 | ret = lys_compile_augment_sort(&ctx, sp, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4152 | LY_CHECK_GOTO(ret, error); |
| 4153 | LY_ARRAY_FOR(augments, u) { |
| 4154 | ret = lys_compile_augment(&ctx, augments[u], options, NULL); |
| 4155 | LY_CHECK_GOTO(ret, error); |
| 4156 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4157 | //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error); |
| 4158 | //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error); |
| 4159 | |
| 4160 | COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error); |
| 4161 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4162 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 4163 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 4164 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 4165 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4166 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 4167 | if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4168 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 4169 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 4170 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 4171 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4172 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 4173 | } else if (type->basetype == LY_TYPE_UNION) { |
| 4174 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 4175 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 4176 | /* validate the path */ |
| 4177 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 4178 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 4179 | LY_CHECK_GOTO(ret, error); |
| 4180 | } |
| 4181 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4182 | } |
| 4183 | } |
| 4184 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 4185 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 4186 | if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 4187 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 4188 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 4189 | /* store pointer to the real type */ |
| 4190 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 4191 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 4192 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 4193 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 4194 | } else if (type->basetype == LY_TYPE_UNION) { |
| 4195 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 4196 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 4197 | /* store pointer to the real type */ |
| 4198 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 4199 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 4200 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 4201 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 4202 | } |
| 4203 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 4204 | } |
| 4205 | } |
| 4206 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4207 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4208 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4209 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4210 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4211 | if (options & LYSC_OPT_FREE_SP) { |
| 4212 | lysp_module_free(mod->parsed); |
| 4213 | ((struct lys_module*)mod)->parsed = NULL; |
| 4214 | } |
| 4215 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4216 | if (!(options & LYSC_OPT_INTERNAL)) { |
| 4217 | /* remove flag of the modules implemented by dependency */ |
| 4218 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 4219 | m = ctx.ctx->list.objs[u]; |
| 4220 | if (m->implemented == 2) { |
| 4221 | m->implemented = 1; |
| 4222 | } |
| 4223 | } |
| 4224 | } |
| 4225 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4226 | ((struct lys_module*)mod)->compiled = mod_c; |
| 4227 | return LY_SUCCESS; |
| 4228 | |
| 4229 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4230 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 4231 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4232 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4233 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4234 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4235 | mod->compiled = NULL; |
| 4236 | |
| 4237 | /* revert compilation of modules implemented by dependency */ |
| 4238 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 4239 | m = ctx.ctx->list.objs[u]; |
| 4240 | if (m->implemented == 2) { |
| 4241 | /* revert features list to the precompiled state */ |
| 4242 | lys_feature_precompile_revert(&ctx, m); |
| 4243 | /* mark module as imported-only / not-implemented */ |
| 4244 | m->implemented = 0; |
| 4245 | /* free the compiled version of the module */ |
| 4246 | lysc_module_free(m->compiled, NULL); |
| 4247 | m->compiled = NULL; |
| 4248 | } |
| 4249 | } |
| 4250 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 4251 | return ret; |
| 4252 | } |