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