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