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