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