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