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); |
| 219 | mod = lys_module_find_prefix(ctx->mod, ext_p->name, u); |
| 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 */ |
| 317 | if (ctx->mod->compiled->version != LYS_VERSION_1_1) { |
| 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; |
| 523 | struct lysc_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 | |
| 528 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod->compiled->version < 2) { |
| 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]; |
| 539 | mod = lysc_module_find_prefix(ctx->mod->compiled, bases_p[u], s - bases_p[u]); |
| 540 | } else { |
| 541 | name = bases_p[u]; |
| 542 | mod = ctx->mod->compiled; |
| 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; |
| 555 | if (mod->identities) { |
| 556 | for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) { |
| 557 | if (!strcmp(name, mod->identities[v].name)) { |
| 558 | if (ident) { |
| 559 | /* we have match! store the backlink */ |
| 560 | LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM); |
| 561 | *idref = ident; |
| 562 | } else { |
| 563 | /* we have match! store the found identity */ |
| 564 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 565 | *idref = &mod->identities[v]; |
| 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 | |
| 1460 | if (base_enums && ctx->mod->compiled->version < 2) { |
| 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, |
| 2058 | struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt); |
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) { |
| 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, NULL); |
| 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. |
| 2467 | * @param[out] dflt Storage for inheriting default value from the typedefs the current type derives from. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2468 | * @return LY_ERR value. |
| 2469 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2470 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2471 | 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, |
| 2472 | struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2473 | { |
| 2474 | LY_ERR ret = LY_SUCCESS; |
| 2475 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2476 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2477 | struct type_context { |
| 2478 | const struct lysp_tpdf *tpdf; |
| 2479 | struct lysp_node *node; |
| 2480 | struct lysp_module *mod; |
| 2481 | } *tctx, *tctx_prev = NULL; |
| 2482 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2483 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2484 | struct ly_set tpdf_chain = {0}; |
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 | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2490 | for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod->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 | } |
| 2508 | if (dflt && !*dflt) { |
| 2509 | /* inherit default */ |
| 2510 | DUP_STRING(ctx->ctx, tctx->tpdf->dflt, *dflt); |
| 2511 | } |
| 2512 | if (dummyloops && (!units || *units) && (!dflt || *dflt)) { |
| 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 | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2522 | if ((units && !*units) || (dflt && !*dflt)) { |
| 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 | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2642 | ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod, 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 | } |
| 2650 | |
| 2651 | COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup); |
| 2652 | |
| 2653 | cleanup: |
| 2654 | ly_set_erase(&tpdf_chain, free); |
| 2655 | return ret; |
| 2656 | } |
| 2657 | |
| 2658 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent); |
| 2659 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2660 | /** |
| 2661 | * @brief Compile parsed container node information. |
| 2662 | * @param[in] ctx Compile context |
| 2663 | * @param[in] node_p Parsed container node. |
| 2664 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2665 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2666 | * is enriched with the container-specific information. |
| 2667 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2668 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2669 | static LY_ERR |
| 2670 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2671 | { |
| 2672 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 2673 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 2674 | struct lysp_node *child_p; |
| 2675 | unsigned int u; |
| 2676 | LY_ERR ret = LY_SUCCESS; |
| 2677 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2678 | LY_LIST_FOR(cont_p->child, child_p) { |
| 2679 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node)); |
| 2680 | } |
| 2681 | |
| 2682 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done); |
| 2683 | //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done); |
| 2684 | //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done); |
| 2685 | |
| 2686 | done: |
| 2687 | return ret; |
| 2688 | } |
| 2689 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2690 | /** |
| 2691 | * @brief Compile parsed leaf node information. |
| 2692 | * @param[in] ctx Compile context |
| 2693 | * @param[in] node_p Parsed leaf node. |
| 2694 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2695 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2696 | * is enriched with the leaf-specific information. |
| 2697 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2698 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2699 | static LY_ERR |
| 2700 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2701 | { |
| 2702 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 2703 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 2704 | unsigned int u; |
| 2705 | LY_ERR ret = LY_SUCCESS; |
| 2706 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2707 | 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] | 2708 | DUP_STRING(ctx->ctx, leaf_p->units, leaf->units); |
| 2709 | DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt); |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2710 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2711 | ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod->parsed, node_p->name, &leaf_p->type, options, &leaf->type, |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2712 | leaf->units ? NULL : &leaf->units, leaf->dflt || (leaf->flags & LYS_MAND_TRUE) ? NULL : &leaf->dflt); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2713 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2714 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 2715 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 2716 | ly_set_add(&ctx->unres, leaf, 0); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2717 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 2718 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 2719 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 2720 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 2721 | ly_set_add(&ctx->unres, leaf, 0); |
| 2722 | } |
| 2723 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2724 | } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) { |
| 2725 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2726 | "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt); |
| 2727 | return LY_EVALID; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2728 | } |
| 2729 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2730 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
| 2731 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2732 | done: |
| 2733 | return ret; |
| 2734 | } |
| 2735 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2736 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2737 | * @brief Compile parsed leaf-list node information. |
| 2738 | * @param[in] ctx Compile context |
| 2739 | * @param[in] node_p Parsed leaf-list node. |
| 2740 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2741 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2742 | * is enriched with the leaf-list-specific information. |
| 2743 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2744 | */ |
| 2745 | static LY_ERR |
| 2746 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2747 | { |
| 2748 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 2749 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 2750 | unsigned int u, v; |
| 2751 | const char *dflt = NULL; |
| 2752 | LY_ERR ret = LY_SUCCESS; |
| 2753 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2754 | COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done); |
| 2755 | DUP_STRING(ctx->ctx, llist_p->units, llist->units); |
| 2756 | |
| 2757 | if (llist_p->dflts) { |
| 2758 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 2759 | LY_ARRAY_FOR(llist_p->dflts, u) { |
| 2760 | DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]); |
| 2761 | LY_ARRAY_INCREMENT(llist->dflts); |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | llist->min = llist_p->min; |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 2766 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2767 | |
| 2768 | ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod->parsed, node_p->name, &llist_p->type, options, &llist->type, |
| 2769 | llist->units ? NULL : &llist->units, (llist->dflts || llist->min) ? NULL : &dflt); |
| 2770 | LY_CHECK_GOTO(ret, done); |
| 2771 | if (dflt) { |
| 2772 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, 1, ret, done); |
| 2773 | llist->dflts[0] = dflt; |
| 2774 | LY_ARRAY_INCREMENT(llist->dflts); |
| 2775 | } |
| 2776 | |
| 2777 | if (llist->type->basetype == LY_TYPE_LEAFREF) { |
| 2778 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 2779 | ly_set_add(&ctx->unres, llist, 0); |
| 2780 | } else if (llist->type->basetype == LY_TYPE_UNION) { |
| 2781 | LY_ARRAY_FOR(((struct lysc_type_union*)llist->type)->types, u) { |
| 2782 | if (((struct lysc_type_union*)llist->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 2783 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 2784 | ly_set_add(&ctx->unres, llist, 0); |
| 2785 | } |
| 2786 | } |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 2787 | } else if (llist->type->basetype == LY_TYPE_EMPTY) { |
| 2788 | if (ctx->mod->compiled->version < LYS_VERSION_1_1) { |
| 2789 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2790 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 2791 | return LY_EVALID; |
| 2792 | } else if (llist_p->dflts) { |
| 2793 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2794 | "Leaf-list of type \"empty\" must not have a default value (%s).", llist_p->dflts[0]); |
| 2795 | return LY_EVALID; |
| 2796 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 2800 | /* configuration data values must be unique - so check the default values */ |
| 2801 | LY_ARRAY_FOR(llist->dflts, u) { |
| 2802 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 2803 | if (!strcmp(llist->dflts[u], llist->dflts[v])) { |
| 2804 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2805 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]); |
| 2806 | return LY_EVALID; |
| 2807 | } |
| 2808 | } |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
| 2813 | |
| 2814 | done: |
| 2815 | return ret; |
| 2816 | } |
| 2817 | |
| 2818 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2819 | * @brief Compile parsed list node information. |
| 2820 | * @param[in] ctx Compile context |
| 2821 | * @param[in] node_p Parsed list node. |
| 2822 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2823 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2824 | * is enriched with the list-specific information. |
| 2825 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2826 | */ |
| 2827 | static LY_ERR |
| 2828 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2829 | { |
| 2830 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 2831 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 2832 | struct lysp_node *child_p; |
| 2833 | struct lysc_node_leaf **key, ***unique; |
| 2834 | size_t len; |
| 2835 | unsigned int u, v; |
| 2836 | const char *keystr, *delim; |
| 2837 | int config; |
| 2838 | LY_ERR ret = LY_SUCCESS; |
| 2839 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2840 | list->min = list_p->min; |
| 2841 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 2842 | |
| 2843 | LY_LIST_FOR(list_p->child, child_p) { |
| 2844 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node)); |
| 2845 | } |
| 2846 | |
| 2847 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done); |
| 2848 | |
| 2849 | /* keys */ |
| 2850 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 2851 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 2852 | return LY_EVALID; |
| 2853 | } |
| 2854 | |
| 2855 | /* find all the keys (must be direct children) */ |
| 2856 | keystr = list_p->key; |
| 2857 | while (keystr) { |
| 2858 | delim = strpbrk(keystr, " \t\n"); |
| 2859 | if (delim) { |
| 2860 | len = delim - keystr; |
| 2861 | while (isspace(*delim)) { |
| 2862 | ++delim; |
| 2863 | } |
| 2864 | } else { |
| 2865 | len = strlen(keystr); |
| 2866 | } |
| 2867 | |
| 2868 | /* key node must be present */ |
| 2869 | LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM); |
| 2870 | *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 2871 | if (!(*key)) { |
| 2872 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2873 | "The list's key \"%.*s\" not found.", len, keystr); |
| 2874 | return LY_EVALID; |
| 2875 | } |
| 2876 | /* keys must be unique */ |
| 2877 | for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) { |
| 2878 | if (*key == list->keys[u]) { |
| 2879 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2880 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 2881 | return LY_EVALID; |
| 2882 | } |
| 2883 | } |
| 2884 | /* key must have the same config flag as the list itself */ |
| 2885 | if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) { |
| 2886 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 2887 | return LY_EVALID; |
| 2888 | } |
| 2889 | if (ctx->mod->compiled->version < LYS_VERSION_1_1) { |
| 2890 | /* YANG 1.0 denies key to be of empty type */ |
| 2891 | if ((*key)->type->basetype == LY_TYPE_EMPTY) { |
| 2892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2893 | "Key of a list can be of type \"empty\" only in YANG 1.1 modules."); |
| 2894 | return LY_EVALID; |
| 2895 | } |
| 2896 | } else { |
| 2897 | /* when and if-feature are illegal on list keys */ |
| 2898 | if ((*key)->when) { |
| 2899 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2900 | "List's key \"%s\" must not have any \"when\" statement.", (*key)->name); |
| 2901 | return LY_EVALID; |
| 2902 | } |
| 2903 | if ((*key)->iffeatures) { |
| 2904 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2905 | "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name); |
| 2906 | return LY_EVALID; |
| 2907 | } |
| 2908 | } |
| 2909 | /* ignore default values of the key */ |
| 2910 | if ((*key)->dflt) { |
| 2911 | lydict_remove(ctx->ctx, (*key)->dflt); |
| 2912 | (*key)->dflt = NULL; |
| 2913 | } |
| 2914 | /* mark leaf as key */ |
| 2915 | (*key)->flags |= LYS_KEY; |
| 2916 | |
| 2917 | /* next key value */ |
| 2918 | keystr = delim; |
| 2919 | } |
| 2920 | |
| 2921 | /* uniques */ |
| 2922 | if (list_p->uniques) { |
| 2923 | for (v = 0; v < LY_ARRAY_SIZE(list_p->uniques); ++v) { |
| 2924 | config = -1; |
| 2925 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 2926 | keystr = list_p->uniques[v]; |
| 2927 | while (keystr) { |
| 2928 | delim = strpbrk(keystr, " \t\n"); |
| 2929 | if (delim) { |
| 2930 | len = delim - keystr; |
| 2931 | while (isspace(*delim)) { |
| 2932 | ++delim; |
| 2933 | } |
| 2934 | } else { |
| 2935 | len = strlen(keystr); |
| 2936 | } |
| 2937 | |
| 2938 | /* unique node must be present */ |
| 2939 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
| 2940 | ret = lys_resolve_descendant_schema_nodeid(ctx, keystr, len, node, LYS_LEAF, (const struct lysc_node**)key); |
| 2941 | if (ret != LY_SUCCESS) { |
| 2942 | if (ret == LY_EDENIED) { |
| 2943 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2944 | "Unique's descendant-schema-nodeid \"%.*s\" refers to a %s node instead of a leaf.", |
| 2945 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 2946 | } |
| 2947 | return LY_EVALID; |
| 2948 | } |
| 2949 | |
| 2950 | /* all referenced leafs must be of the same config type */ |
| 2951 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 2952 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2953 | "Unique statement \"%s\" refers to leafs with different config type.", list_p->uniques[v]); |
| 2954 | return LY_EVALID; |
| 2955 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 2956 | config = 1; |
| 2957 | } else { /* LYS_CONFIG_R */ |
| 2958 | config = 0; |
| 2959 | } |
| 2960 | |
| 2961 | /* mark leaf as unique */ |
| 2962 | (*key)->flags |= LYS_UNIQUE; |
| 2963 | |
| 2964 | /* next unique value in line */ |
| 2965 | keystr = delim; |
| 2966 | } |
| 2967 | /* next unique definition */ |
| 2968 | } |
| 2969 | } |
| 2970 | |
| 2971 | //COMPILE_ARRAY_GOTO(ctx, list_p->actions, list->actions, options, u, lys_compile_action, ret, done); |
| 2972 | //COMPILE_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, options, u, lys_compile_notif, ret, done); |
| 2973 | |
| 2974 | done: |
| 2975 | return ret; |
| 2976 | } |
| 2977 | |
| 2978 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2979 | * @brief Compile parsed choice node information. |
| 2980 | * @param[in] ctx Compile context |
| 2981 | * @param[in] node_p Parsed choice node. |
| 2982 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2983 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2984 | * is enriched with the container-specific information. |
| 2985 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2986 | */ |
| 2987 | static LY_ERR |
| 2988 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2989 | { |
| 2990 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 2991 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 2992 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 2993 | struct lysc_node *iter; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 2994 | const char *prefix = NULL, *name; |
| 2995 | size_t prefix_len = 0; |
| 2996 | struct lys_module; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2997 | LY_ERR ret = LY_SUCCESS; |
| 2998 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2999 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3000 | if (child_p->nodetype == LYS_CASE) { |
| 3001 | LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) { |
| 3002 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node)); |
| 3003 | } |
| 3004 | } else { |
| 3005 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node)); |
| 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3010 | if (ch_p->dflt) { |
| 3011 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3012 | name = strchr(ch_p->dflt, ':'); |
| 3013 | if (name) { |
| 3014 | prefix = ch_p->dflt; |
| 3015 | prefix_len = name - prefix; |
| 3016 | ++name; |
| 3017 | } else { |
| 3018 | name = ch_p->dflt; |
| 3019 | } |
| 3020 | if (prefix && (strncmp(prefix, node->module->compiled->prefix, prefix_len) || node->module->compiled->prefix[prefix_len] != '\0')) { |
| 3021 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3022 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3023 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3024 | prefix_len, prefix); |
| 3025 | return LY_EVALID; |
| 3026 | } |
| 3027 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 3028 | if (!ch->dflt) { |
| 3029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3030 | "Default case \"%s\" not found.", ch_p->dflt); |
| 3031 | return LY_EVALID; |
| 3032 | } |
| 3033 | /* no mandatory nodes directly under the default case */ |
| 3034 | LY_LIST_FOR(ch->dflt->child, iter) { |
| 3035 | if (iter->flags & LYS_MAND_TRUE) { |
| 3036 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3037 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, ch_p->dflt); |
| 3038 | return LY_EVALID; |
| 3039 | } |
| 3040 | } |
| 3041 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3042 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3043 | return ret; |
| 3044 | } |
| 3045 | |
| 3046 | /** |
| 3047 | * @brief Compile parsed anydata or anyxml node information. |
| 3048 | * @param[in] ctx Compile context |
| 3049 | * @param[in] node_p Parsed anydata or anyxml node. |
| 3050 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3051 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3052 | * is enriched with the any-specific information. |
| 3053 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3054 | */ |
| 3055 | static LY_ERR |
| 3056 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 3057 | { |
| 3058 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 3059 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 3060 | unsigned int u; |
| 3061 | LY_ERR ret = LY_SUCCESS; |
| 3062 | |
| 3063 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done); |
| 3064 | |
| 3065 | if (any->flags & LYS_CONFIG_W) { |
| 3066 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
| 3067 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML)); |
| 3068 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3069 | done: |
| 3070 | return ret; |
| 3071 | } |
| 3072 | |
| 3073 | static LY_ERR |
| 3074 | lys_compile_status(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent) |
| 3075 | { |
| 3076 | |
| 3077 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3078 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3079 | if (!(node->flags & LYS_STATUS_MASK)) { |
| 3080 | if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) { |
| 3081 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3082 | (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3083 | node->flags |= parent->flags & LYS_STATUS_MASK; |
| 3084 | } else { |
| 3085 | node->flags |= LYS_STATUS_CURR; |
| 3086 | } |
| 3087 | } else if (parent) { |
| 3088 | /* check status compatibility with the parent */ |
| 3089 | if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) { |
| 3090 | if (node->flags & LYS_STATUS_CURR) { |
| 3091 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3092 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3093 | (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3094 | } else { /* LYS_STATUS_DEPRC */ |
| 3095 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3096 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3097 | } |
| 3098 | return LY_EVALID; |
| 3099 | } |
| 3100 | } |
| 3101 | return LY_SUCCESS; |
| 3102 | } |
| 3103 | |
| 3104 | static LY_ERR |
| 3105 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3106 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3107 | const char *name, void *exclude) |
| 3108 | { |
| 3109 | const struct lysc_node *iter; |
| 3110 | unsigned int u; |
| 3111 | |
| 3112 | LY_LIST_FOR(children, iter) { |
| 3113 | if (iter != exclude && !strcmp(name, iter->name)) { |
| 3114 | goto error; |
| 3115 | } |
| 3116 | } |
| 3117 | LY_ARRAY_FOR(actions, u) { |
| 3118 | if (&actions[u] != exclude && !strcmp(name, actions[u].name)) { |
| 3119 | goto error; |
| 3120 | } |
| 3121 | } |
| 3122 | LY_ARRAY_FOR(notifs, u) { |
| 3123 | if (¬ifs[u] != exclude && !strcmp(name, notifs[u].name)) { |
| 3124 | goto error; |
| 3125 | } |
| 3126 | } |
| 3127 | return LY_SUCCESS; |
| 3128 | error: |
| 3129 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition"); |
| 3130 | return LY_EEXIST; |
| 3131 | } |
| 3132 | |
| 3133 | /** |
| 3134 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 3135 | * |
| 3136 | * @param[in] ctx Compile context |
| 3137 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 3138 | * the choice itself is expected instead of a specific case node. |
| 3139 | * @param[in] node Schema node to connect into the list. |
| 3140 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 3141 | */ |
| 3142 | static LY_ERR |
| 3143 | lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node) |
| 3144 | { |
| 3145 | struct lysc_node **children; |
| 3146 | |
| 3147 | if (node->nodetype == LYS_CASE) { |
| 3148 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 3149 | } else { |
| 3150 | children = lysc_node_children_p(parent); |
| 3151 | } |
| 3152 | if (children) { |
| 3153 | if (!(*children)) { |
| 3154 | /* first child */ |
| 3155 | *children = node; |
| 3156 | } else if (*children != node) { |
| 3157 | /* by the condition in previous branch we cover the choice/case children |
| 3158 | * - the children list is shared by the choice and the the first case, in addition |
| 3159 | * the first child of each case must be referenced from the case node. So the node is |
| 3160 | * actually always already inserted in case it is the first children - so here such |
| 3161 | * a situation actually corresponds to the first branch */ |
| 3162 | /* insert at the end of the parent's children list */ |
| 3163 | (*children)->prev->next = node; |
| 3164 | node->prev = (*children)->prev; |
| 3165 | (*children)->prev = node; |
| 3166 | |
| 3167 | /* check the name uniqueness */ |
| 3168 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 3169 | lysc_node_notifs(parent), node->name, node)) { |
| 3170 | return LY_EEXIST; |
| 3171 | } |
| 3172 | } |
| 3173 | } |
| 3174 | return LY_SUCCESS; |
| 3175 | } |
| 3176 | |
| 3177 | static struct lysc_node_case* |
| 3178 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child) |
| 3179 | { |
| 3180 | struct lysc_node *iter; |
| 3181 | struct lysc_node_case *cs; |
| 3182 | unsigned int u; |
| 3183 | LY_ERR ret; |
| 3184 | |
| 3185 | #define UNIQUE_CHECK(NAME) \ |
| 3186 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
| 3187 | if (!strcmp(iter->name, NAME)) { \ |
| 3188 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 3189 | return NULL; \ |
| 3190 | } \ |
| 3191 | } |
| 3192 | |
| 3193 | if (node_p->nodetype == LYS_CHOICE) { |
| 3194 | UNIQUE_CHECK(child->name); |
| 3195 | |
| 3196 | /* we have to add an implicit case node into the parent choice */ |
| 3197 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3198 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 3199 | cs->flags = ch->flags & LYS_STATUS_MASK; |
| 3200 | } else { /* node_p->nodetype == LYS_CASE */ |
| 3201 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 3202 | /* the case is already present since the child is not its first children */ |
| 3203 | return (struct lysc_node_case*)ch->cases->prev; |
| 3204 | } |
| 3205 | UNIQUE_CHECK(node_p->name); |
| 3206 | |
| 3207 | /* explicit parent case is not present (this is its first child) */ |
| 3208 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 3209 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 3210 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 3211 | cs->sp = node_p; |
| 3212 | |
| 3213 | /* check the case's status */ |
| 3214 | LY_CHECK_RET(lys_compile_status(ctx, (struct lysc_node*)cs, (struct lysc_node* )ch), NULL); |
| 3215 | COMPILE_MEMBER_GOTO(ctx, node_p->when, cs->when, options, lys_compile_when, ret, error); |
| 3216 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error); |
| 3217 | } |
| 3218 | cs->module = ctx->mod; |
| 3219 | cs->prev = (struct lysc_node*)cs; |
| 3220 | cs->nodetype = LYS_CASE; |
| 3221 | lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs); |
| 3222 | cs->parent = (struct lysc_node*)ch; |
| 3223 | cs->child = child; |
| 3224 | |
| 3225 | return cs; |
| 3226 | error: |
| 3227 | return NULL; |
| 3228 | |
| 3229 | #undef UNIQUE_CHECK |
| 3230 | } |
| 3231 | |
| 3232 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3233 | * @brief Compile parsed schema node information. |
| 3234 | * @param[in] ctx Compile context |
| 3235 | * @param[in] node_p Parsed schema node. |
| 3236 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3237 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 3238 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 3239 | * the compile context. |
| 3240 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3241 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3242 | static LY_ERR |
| 3243 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent) |
| 3244 | { |
| 3245 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3246 | struct lysc_node *node; |
| 3247 | struct lysc_node_case *cs; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3248 | unsigned int u; |
| 3249 | LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*); |
| 3250 | |
| 3251 | switch (node_p->nodetype) { |
| 3252 | case LYS_CONTAINER: |
| 3253 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 3254 | node_compile_spec = lys_compile_node_container; |
| 3255 | break; |
| 3256 | case LYS_LEAF: |
| 3257 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 3258 | node_compile_spec = lys_compile_node_leaf; |
| 3259 | break; |
| 3260 | case LYS_LIST: |
| 3261 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3262 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3263 | break; |
| 3264 | case LYS_LEAFLIST: |
| 3265 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3266 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3267 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3268 | case LYS_CHOICE: |
| 3269 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3270 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3271 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3272 | case LYS_ANYXML: |
| 3273 | case LYS_ANYDATA: |
| 3274 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3275 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3276 | break; |
| 3277 | default: |
| 3278 | LOGINT(ctx->ctx); |
| 3279 | return LY_EINT; |
| 3280 | } |
| 3281 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 3282 | node->nodetype = node_p->nodetype; |
| 3283 | node->module = ctx->mod; |
| 3284 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3285 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3286 | |
| 3287 | /* config */ |
| 3288 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 3289 | /* config not explicitely set, inherit it from parent */ |
| 3290 | if (parent) { |
| 3291 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 3292 | } else { |
| 3293 | /* default is config true */ |
| 3294 | node->flags |= LYS_CONFIG_W; |
| 3295 | } |
| 3296 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3297 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 3298 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3299 | "Configuration node cannot be child of any state data node."); |
| 3300 | goto error; |
| 3301 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3302 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 3303 | /* *list ordering */ |
| 3304 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 3305 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
| 3306 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, " |
| 3307 | "RPC/action output parameters or notification content (%s).", ctx->path); |
| 3308 | node->flags &= ~LYS_ORDBY_MASK; |
| 3309 | node->flags |= LYS_ORDBY_SYSTEM; |
| 3310 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 3311 | /* default ordering is system */ |
| 3312 | node->flags |= LYS_ORDBY_SYSTEM; |
| 3313 | } |
| 3314 | } |
| 3315 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3316 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3317 | * 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] | 3318 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 3319 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 3320 | * the parent is choice (parent here) or some case (so we have to get its flags to check) */ |
| 3321 | LY_CHECK_GOTO(lys_compile_status(ctx, node, parent), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | if (!(options & LYSC_OPT_FREE_SP)) { |
| 3325 | node->sp = node_p; |
| 3326 | } |
| 3327 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3328 | COMPILE_MEMBER_GOTO(ctx, node_p->when, node->when, options, lys_compile_when, ret, error); |
| 3329 | 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] | 3330 | COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error); |
| 3331 | |
| 3332 | /* nodetype-specific part */ |
| 3333 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error); |
| 3334 | |
| 3335 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3336 | if (parent) { |
| 3337 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3338 | cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node); |
| 3339 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
| 3340 | /* the postponed status check of the node and its real parent - in case of implicit case, |
| 3341 | * it directly gets the same status flags as the choice */ |
| 3342 | LY_CHECK_GOTO(lys_compile_status(ctx, node, (struct lysc_node*)cs), error); |
| 3343 | node->parent = (struct lysc_node*)cs; |
| 3344 | } else { /* other than choice */ |
| 3345 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3346 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3347 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node), LY_EVALID); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3348 | } else { |
| 3349 | /* top-level element */ |
| 3350 | if (!ctx->mod->compiled->data) { |
| 3351 | ctx->mod->compiled->data = node; |
| 3352 | } else { |
| 3353 | /* insert at the end of the module's top-level nodes list */ |
| 3354 | ctx->mod->compiled->data->prev->next = node; |
| 3355 | node->prev = ctx->mod->compiled->data->prev; |
| 3356 | ctx->mod->compiled->data->prev = node; |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | return LY_SUCCESS; |
| 3361 | |
| 3362 | error: |
| 3363 | lysc_node_free(ctx->ctx, node); |
| 3364 | return ret; |
| 3365 | } |
| 3366 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3367 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 3368 | * @brief Compile the given YANG submodule into the main module. |
| 3369 | * @param[in] ctx Compile context |
| 3370 | * @param[in] inc Include structure from the main module defining the submodule. |
| 3371 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3372 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3373 | */ |
| 3374 | LY_ERR |
| 3375 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options) |
| 3376 | { |
| 3377 | unsigned int u; |
| 3378 | LY_ERR ret = LY_SUCCESS; |
| 3379 | /* shortcuts */ |
| 3380 | struct lysp_module *submod = inc->submodule; |
| 3381 | struct lysc_module *mainmod = ctx->mod->compiled; |
| 3382 | |
| 3383 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->features, mainmod->features, options, u, lys_compile_feature, ret, error); |
| 3384 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error); |
| 3385 | |
| 3386 | error: |
| 3387 | return ret; |
| 3388 | } |
| 3389 | |
| 3390 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3391 | * @brief Compile the given YANG module. |
| 3392 | * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed. |
| 3393 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 3394 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3395 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3396 | LY_ERR |
| 3397 | lys_compile(struct lys_module *mod, int options) |
| 3398 | { |
| 3399 | struct lysc_ctx ctx = {0}; |
| 3400 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3401 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3402 | struct lysp_module *sp; |
| 3403 | struct lysp_node *node_p; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3404 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 3405 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3406 | |
| 3407 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL); |
| 3408 | sp = mod->parsed; |
| 3409 | |
| 3410 | if (sp->submodule) { |
| 3411 | LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name); |
| 3412 | return LY_EINVAL; |
| 3413 | } |
| 3414 | |
| 3415 | ctx.ctx = sp->ctx; |
| 3416 | ctx.mod = mod; |
| 3417 | |
| 3418 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 3419 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM); |
| 3420 | mod_c->ctx = sp->ctx; |
| 3421 | mod_c->implemented = sp->implemented; |
| 3422 | mod_c->latest_revision = sp->latest_revision; |
| 3423 | mod_c->version = sp->version; |
| 3424 | |
| 3425 | DUP_STRING(sp->ctx, sp->name, mod_c->name); |
| 3426 | DUP_STRING(sp->ctx, sp->ns, mod_c->ns); |
| 3427 | DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix); |
| 3428 | if (sp->revs) { |
| 3429 | DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision); |
| 3430 | } |
| 3431 | 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] | 3432 | LY_ARRAY_FOR(sp->includes, u) { |
| 3433 | ret = lys_compile_submodule(&ctx, &sp->includes[u], options); |
| 3434 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 3435 | } |
| 3436 | COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error); |
| 3437 | 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] | 3438 | if (sp->identities) { |
| 3439 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 3440 | } |
| 3441 | |
| 3442 | LY_LIST_FOR(sp->data, node_p) { |
| 3443 | ret = lys_compile_node(&ctx, node_p, options, NULL); |
| 3444 | LY_CHECK_GOTO(ret, error); |
| 3445 | } |
| 3446 | //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error); |
| 3447 | //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error); |
| 3448 | |
| 3449 | COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error); |
| 3450 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3451 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3452 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 3453 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 3454 | * 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] | 3455 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3456 | 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] | 3457 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 3458 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 3459 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3460 | 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] | 3461 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3462 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3463 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 3464 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 3465 | /* validate the path */ |
| 3466 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 3467 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 3468 | LY_CHECK_GOTO(ret, error); |
| 3469 | } |
| 3470 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3471 | } |
| 3472 | } |
| 3473 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3474 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3475 | 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] | 3476 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 3477 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 3478 | /* store pointer to the real type */ |
| 3479 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 3480 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 3481 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 3482 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3483 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3484 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 3485 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 3486 | /* store pointer to the real type */ |
| 3487 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 3488 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 3489 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 3490 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 3491 | } |
| 3492 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 3493 | } |
| 3494 | } |
| 3495 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3496 | ly_set_erase(&ctx.unres, NULL); |
| 3497 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3498 | if (options & LYSC_OPT_FREE_SP) { |
| 3499 | lysp_module_free(mod->parsed); |
| 3500 | ((struct lys_module*)mod)->parsed = NULL; |
| 3501 | } |
| 3502 | |
| 3503 | ((struct lys_module*)mod)->compiled = mod_c; |
| 3504 | return LY_SUCCESS; |
| 3505 | |
| 3506 | error: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3507 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3508 | lysc_module_free(mod_c, NULL); |
| 3509 | ((struct lys_module*)mod)->compiled = NULL; |
| 3510 | return ret; |
| 3511 | } |