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