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