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