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