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