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