Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file schema_compile_amend.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 19a0902 | 2021-06-15 11:54:08 +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 augments, deviations, and refines. |
| 6 | * |
Michal Vasko | 19a0902 | 2021-06-15 11:54:08 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2021 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 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include "schema_compile_amend.h" |
| 19 | |
| 20 | #include <assert.h> |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 21 | #include <stddef.h> |
| 22 | #include <stdint.h> |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "common.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 27 | #include "dict.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 28 | #include "log.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 29 | #include "schema_compile.h" |
| 30 | #include "schema_compile_node.h" |
Michal Vasko | 29dd11e | 2020-11-23 16:52:22 +0100 | [diff] [blame] | 31 | #include "schema_features.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 32 | #include "set.h" |
| 33 | #include "tree.h" |
Michal Vasko | 899c7ce | 2022-02-18 09:18:37 +0100 | [diff] [blame] | 34 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 35 | #include "tree_edit.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 36 | #include "tree_schema.h" |
| 37 | #include "tree_schema_internal.h" |
| 38 | #include "xpath.h" |
| 39 | |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 40 | /** |
| 41 | * @brief Get module of a single nodeid node name test. |
| 42 | * |
| 43 | * @param[in] ctx libyang context. |
| 44 | * @param[in] nametest Nametest with an optional prefix. |
| 45 | * @param[in] nametest_len Length of @p nametest. |
| 46 | * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix. |
| 47 | * @param[out] name Optional pointer to the name test without the prefix. |
| 48 | * @param[out] name_len Length of @p name. |
| 49 | * @return Resolved module. |
| 50 | */ |
| 51 | static const struct lys_module * |
| 52 | lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len, |
| 53 | const struct lysp_module *mod, const char **name, size_t *name_len) |
| 54 | { |
| 55 | const struct lys_module *target_mod; |
| 56 | const char *ptr; |
| 57 | |
| 58 | ptr = ly_strnchr(nametest, ':', nametest_len); |
| 59 | if (ptr) { |
| 60 | target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_VALUE_SCHEMA, (void *)mod); |
| 61 | if (!target_mod) { |
| 62 | LOGVAL(ctx, LYVE_REFERENCE, |
| 63 | "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
| 64 | (int)nametest_len, nametest, (int)(ptr - nametest), nametest, LYSP_MODULE_NAME(mod)); |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | if (name) { |
| 69 | *name = ptr + 1; |
| 70 | *name_len = nametest_len - ((ptr - nametest) + 1); |
| 71 | } |
| 72 | } else { |
| 73 | target_mod = mod->mod; |
| 74 | if (name) { |
| 75 | *name = nametest; |
| 76 | *name_len = nametest_len; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return target_mod; |
| 81 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 82 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 83 | /** |
| 84 | * @brief Check the syntax of a node-id and collect all the referenced modules. |
| 85 | * |
| 86 | * @param[in] ctx Compile context. |
| 87 | * @param[in] nodeid Node-id to check. |
| 88 | * @param[in] abs Whether @p nodeid is absolute. |
| 89 | * @param[in,out] mod_set Set to add referenced modules into. |
| 90 | * @param[out] expr Optional node-id parsed into an expression. |
| 91 | * @param[out] target_mod Optional target module of the node-id. |
| 92 | * @return LY_ERR value. |
| 93 | */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 94 | static LY_ERR |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 95 | lys_nodeid_mod_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct ly_set *mod_set, |
| 96 | struct lyxp_expr **expr, struct lys_module **target_mod) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 97 | { |
| 98 | LY_ERR ret = LY_SUCCESS; |
| 99 | struct lyxp_expr *e = NULL; |
| 100 | struct lys_module *tmod = NULL, *mod; |
| 101 | const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid"; |
| 102 | uint32_t i; |
| 103 | |
| 104 | /* parse */ |
| 105 | ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e); |
| 106 | if (ret) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 107 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 108 | nodeid_type, nodeid); |
| 109 | ret = LY_EVALID; |
| 110 | goto cleanup; |
| 111 | } |
| 112 | |
| 113 | if (abs) { |
| 114 | /* absolute schema nodeid */ |
| 115 | i = 0; |
| 116 | } else { |
| 117 | /* descendant schema nodeid */ |
| 118 | if (e->tokens[0] != LYXP_TOKEN_NAMETEST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 119 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 120 | nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]); |
| 121 | ret = LY_EVALID; |
| 122 | goto cleanup; |
| 123 | } |
| 124 | i = 1; |
| 125 | } |
| 126 | |
| 127 | /* check all the tokens */ |
| 128 | for ( ; i < e->used; i += 2) { |
| 129 | if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 130 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 131 | nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]); |
| 132 | ret = LY_EVALID; |
| 133 | goto cleanup; |
| 134 | } else if (e->used == i + 1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 135 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 136 | "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr); |
| 137 | ret = LY_EVALID; |
| 138 | goto cleanup; |
| 139 | } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 140 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 141 | nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]); |
| 142 | ret = LY_EVALID; |
| 143 | goto cleanup; |
| 144 | } else if (abs) { |
| 145 | mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1], |
| 146 | e->tok_len[i + 1], ctx->pmod, NULL, NULL); |
| 147 | LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup); |
| 148 | |
| 149 | /* only keep the first module */ |
| 150 | if (!tmod) { |
| 151 | tmod = mod; |
| 152 | } |
| 153 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 154 | /* store the referenced module */ |
| 155 | LY_CHECK_GOTO(ret = ly_set_add(mod_set, mod, 0, NULL), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | cleanup: |
| 160 | if (ret || !expr) { |
| 161 | lyxp_expr_free(ctx->ctx, e); |
| 162 | e = NULL; |
| 163 | } |
| 164 | if (expr) { |
| 165 | *expr = ret ? NULL : e; |
| 166 | } |
| 167 | if (target_mod) { |
| 168 | *target_mod = ret ? NULL : tmod; |
| 169 | } |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @brief Check whether 2 schema nodeids match. |
| 175 | * |
| 176 | * @param[in] ctx libyang context. |
| 177 | * @param[in] exp1 First schema nodeid. |
| 178 | * @param[in] exp1p_mod Module of @p exp1 nodes without any prefix. |
| 179 | * @param[in] exp2 Second schema nodeid. |
| 180 | * @param[in] exp2_pmod Module of @p exp2 nodes without any prefix. |
| 181 | * @return Whether the schema nodeids match or not. |
| 182 | */ |
| 183 | static ly_bool |
| 184 | lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lysp_module *exp1_pmod, |
| 185 | const struct lyxp_expr *exp2, const struct lysp_module *exp2_pmod) |
| 186 | { |
| 187 | uint32_t i; |
| 188 | const struct lys_module *mod1, *mod2; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 189 | const char *name1 = NULL, *name2 = NULL; |
| 190 | size_t name1_len = 0, name2_len = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 191 | |
| 192 | if (exp1->used != exp2->used) { |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | for (i = 0; i < exp1->used; ++i) { |
| 197 | assert(exp1->tokens[i] == exp2->tokens[i]); |
| 198 | |
| 199 | if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) { |
| 200 | /* check modules of all the nodes in the node ID */ |
| 201 | mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_pmod, |
| 202 | &name1, &name1_len); |
| 203 | assert(mod1); |
| 204 | mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_pmod, |
| 205 | &name2, &name2_len); |
| 206 | assert(mod2); |
| 207 | |
| 208 | /* compare modules */ |
| 209 | if (mod1 != mod2) { |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /* compare names */ |
| 214 | if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) { |
| 215 | return 0; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | LY_ERR |
| 224 | lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node) |
| 225 | { |
| 226 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 227 | struct lyxp_expr *exp = NULL; |
| 228 | struct lysc_augment *aug; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 229 | struct lysp_node_augment *aug_p; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 230 | struct lysc_refine *rfn; |
| 231 | struct lysp_refine **new_rfn; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 232 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 233 | uint32_t i; |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 234 | struct ly_set mod_set = {0}; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 235 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 236 | LY_LIST_FOR(uses_p->augments, aug_p) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 237 | lysc_update_path(ctx, NULL, "{augment}"); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 238 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 239 | |
| 240 | /* parse the nodeid */ |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 241 | LY_CHECK_GOTO(ret = lys_nodeid_mod_check(ctx, aug_p->nodeid, 0, &mod_set, &exp, NULL), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 242 | |
| 243 | /* allocate new compiled augment and store it in the set */ |
| 244 | aug = calloc(1, sizeof *aug); |
| 245 | LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 246 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, 1, NULL), cleanup); |
| 247 | |
| 248 | aug->nodeid = exp; |
| 249 | exp = NULL; |
Michal Vasko | 28fe5f6 | 2021-03-03 16:37:39 +0100 | [diff] [blame] | 250 | aug->aug_pmod = ctx->pmod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 251 | aug->nodeid_ctx_node = ctx_node; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 252 | aug->aug_p = aug_p; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 253 | |
| 254 | lysc_update_path(ctx, NULL, NULL); |
| 255 | lysc_update_path(ctx, NULL, NULL); |
| 256 | } |
| 257 | |
| 258 | LY_ARRAY_FOR(uses_p->refines, u) { |
| 259 | lysc_update_path(ctx, NULL, "{refine}"); |
| 260 | lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid); |
| 261 | |
| 262 | /* parse the nodeid */ |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 263 | LY_CHECK_GOTO(ret = lys_nodeid_mod_check(ctx, uses_p->refines[u].nodeid, 0, &mod_set, &exp, NULL), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 264 | |
| 265 | /* try to find the node in already compiled refines */ |
| 266 | rfn = NULL; |
| 267 | for (i = 0; i < ctx->uses_rfns.count; ++i) { |
| 268 | if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->pmod, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid, |
| 269 | ctx->pmod)) { |
| 270 | rfn = ctx->uses_rfns.objs[i]; |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (!rfn) { |
| 276 | /* allocate new compiled refine */ |
| 277 | rfn = calloc(1, sizeof *rfn); |
| 278 | LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 279 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, 1, NULL), cleanup); |
| 280 | |
| 281 | rfn->nodeid = exp; |
| 282 | exp = NULL; |
Michal Vasko | 7d3708f | 2021-02-03 10:50:24 +0100 | [diff] [blame] | 283 | rfn->nodeid_pmod = ctx->cur_mod->parsed; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 284 | rfn->nodeid_ctx_node = ctx_node; |
Michal Vasko | d865572 | 2021-01-12 15:20:36 +0100 | [diff] [blame] | 285 | rfn->uses_p = uses_p; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 286 | } else { |
| 287 | /* just free exp */ |
| 288 | lyxp_expr_free(ctx->ctx, exp); |
| 289 | exp = NULL; |
| 290 | } |
| 291 | |
| 292 | /* add new parsed refine structure */ |
| 293 | LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup); |
| 294 | *new_rfn = &uses_p->refines[u]; |
| 295 | |
| 296 | lysc_update_path(ctx, NULL, NULL); |
| 297 | lysc_update_path(ctx, NULL, NULL); |
| 298 | } |
| 299 | |
| 300 | cleanup: |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 301 | if (ret) { |
| 302 | lysc_update_path(ctx, NULL, NULL); |
| 303 | lysc_update_path(ctx, NULL, NULL); |
| 304 | } |
| 305 | /* should include only this module, will fail later if not */ |
| 306 | ly_set_erase(&mod_set, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 307 | lyxp_expr_free(ctx->ctx, exp); |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static LY_ERR |
Michal Vasko | 899c7ce | 2022-02-18 09:18:37 +0100 | [diff] [blame] | 312 | lysp_ext_children_dup(const struct ly_ctx *ctx, struct lysp_stmt **child, const struct lysp_stmt *orig_child) |
| 313 | { |
Michal Vasko | d84b813 | 2022-03-15 10:45:44 +0100 | [diff] [blame] | 314 | struct lysp_stmt *ch = NULL; |
Michal Vasko | 4316c9d | 2022-02-21 10:00:10 +0100 | [diff] [blame] | 315 | |
Michal Vasko | 9464f42 | 2022-02-21 10:18:28 +0100 | [diff] [blame] | 316 | assert(!*child); |
| 317 | |
Michal Vasko | 899c7ce | 2022-02-18 09:18:37 +0100 | [diff] [blame] | 318 | LY_LIST_FOR(orig_child, orig_child) { |
| 319 | /* new child */ |
| 320 | if (!*child) { |
Michal Vasko | 4316c9d | 2022-02-21 10:00:10 +0100 | [diff] [blame] | 321 | *child = ch = calloc(1, sizeof *ch); |
| 322 | LY_CHECK_ERR_RET(!ch, LOGMEM(ctx), LY_EMEM); |
Michal Vasko | 899c7ce | 2022-02-18 09:18:37 +0100 | [diff] [blame] | 323 | } else { |
Michal Vasko | 4316c9d | 2022-02-21 10:00:10 +0100 | [diff] [blame] | 324 | ch->next = calloc(1, sizeof *ch); |
| 325 | LY_CHECK_ERR_RET(!ch->next, LOGMEM(ctx), LY_EMEM); |
| 326 | ch = ch->next; |
Michal Vasko | 899c7ce | 2022-02-18 09:18:37 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* fill */ |
Michal Vasko | 4316c9d | 2022-02-21 10:00:10 +0100 | [diff] [blame] | 330 | DUP_STRING_RET(ctx, orig_child->stmt, ch->stmt); |
| 331 | ch->flags = orig_child->flags; |
| 332 | DUP_STRING_RET(ctx, orig_child->arg, ch->arg); |
| 333 | ch->format = orig_child->format; |
| 334 | LY_CHECK_RET(ly_dup_prefix_data(ctx, orig_child->format, orig_child->prefix_data, &(ch->prefix_data))); |
| 335 | ch->kw = orig_child->kw; |
Michal Vasko | 899c7ce | 2022-02-18 09:18:37 +0100 | [diff] [blame] | 336 | |
| 337 | /* recursive children */ |
Michal Vasko | 4316c9d | 2022-02-21 10:00:10 +0100 | [diff] [blame] | 338 | LY_CHECK_RET(lysp_ext_children_dup(ctx, &ch->child, orig_child->child)); |
Michal Vasko | 899c7ce | 2022-02-18 09:18:37 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return LY_SUCCESS; |
| 342 | } |
| 343 | |
| 344 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 345 | lysp_ext_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_ext_instance *ext, |
| 346 | const struct lysp_ext_instance *orig_ext) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 347 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 348 | LY_ERR ret = LY_SUCCESS; |
| 349 | struct ly_set pmods = {0}; |
| 350 | struct lysp_ctx pctx = {.parsed_mods = &pmods}; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 351 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 352 | DUP_STRING_GOTO(ctx, orig_ext->name, ext->name, ret, cleanup); |
| 353 | DUP_STRING_GOTO(ctx, orig_ext->argument, ext->argument, ret, cleanup); |
| 354 | ext->format = orig_ext->format; |
| 355 | LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, orig_ext->format, orig_ext->prefix_data, &ext->prefix_data), cleanup); |
| 356 | ext->def = orig_ext->def; |
Michal Vasko | 899c7ce | 2022-02-18 09:18:37 +0100 | [diff] [blame] | 357 | |
Michal Vasko | 91bb76c | 2022-03-24 13:34:18 +0100 | [diff] [blame] | 358 | ext->parent = orig_ext->parent; |
| 359 | ext->parent_stmt = orig_ext->parent_stmt; |
| 360 | ext->parent_stmt_index = orig_ext->parent_stmt_index; |
| 361 | ext->flags = orig_ext->flags; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 362 | ext->record = orig_ext->record; |
| 363 | |
| 364 | LY_CHECK_GOTO(ret = lysp_ext_children_dup(ctx, &ext->child, orig_ext->child), cleanup); |
| 365 | if (ext->record && ext->record->plugin.parse) { |
| 366 | /* parse again */ |
| 367 | LY_CHECK_GOTO(ret = ly_set_add(&pmods, pmod, 1, NULL), cleanup); |
| 368 | LY_CHECK_GOTO(ret = ext->record->plugin.parse(&pctx, ext), cleanup); |
| 369 | } |
| 370 | |
| 371 | cleanup: |
| 372 | ly_set_erase(&pmods, NULL); |
| 373 | return ret; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 377 | lysp_restr_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_restr *restr, |
| 378 | const struct lysp_restr *orig_restr) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 379 | { |
| 380 | LY_ERR ret = LY_SUCCESS; |
| 381 | |
| 382 | if (orig_restr) { |
| 383 | DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret); |
| 384 | restr->arg.mod = orig_restr->arg.mod; |
| 385 | DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret); |
| 386 | DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret); |
| 387 | DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret); |
| 388 | DUP_STRING(ctx, orig_restr->ref, restr->ref, ret); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 389 | DUP_ARRAY2(ctx, pmod, orig_restr->exts, restr->exts, lysp_ext_dup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | static LY_ERR |
| 396 | lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str) |
| 397 | { |
| 398 | LY_ERR ret = LY_SUCCESS; |
| 399 | |
| 400 | DUP_STRING(ctx, *orig_str, *str, ret); |
| 401 | |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | LY_ERR |
| 406 | lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname) |
| 407 | { |
| 408 | LY_ERR ret = LY_SUCCESS; |
| 409 | |
| 410 | if (!orig_qname->str) { |
| 411 | return LY_SUCCESS; |
| 412 | } |
| 413 | |
| 414 | DUP_STRING(ctx, orig_qname->str, qname->str, ret); |
| 415 | assert(orig_qname->mod); |
| 416 | qname->mod = orig_qname->mod; |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 422 | lysp_type_enum_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_type_enum *enm, |
| 423 | const struct lysp_type_enum *orig_enm) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 424 | { |
| 425 | LY_ERR ret = LY_SUCCESS; |
| 426 | |
| 427 | DUP_STRING(ctx, orig_enm->name, enm->name, ret); |
| 428 | DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret); |
| 429 | DUP_STRING(ctx, orig_enm->ref, enm->ref, ret); |
| 430 | enm->value = orig_enm->value; |
| 431 | DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 432 | DUP_ARRAY2(ctx, pmod, orig_enm->exts, enm->exts, lysp_ext_dup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 433 | enm->flags = orig_enm->flags; |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 439 | lysp_type_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_type *type, |
| 440 | const struct lysp_type *orig_type) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 441 | { |
| 442 | LY_ERR ret = LY_SUCCESS; |
| 443 | |
Michal Vasko | ea86824 | 2021-06-21 09:28:32 +0200 | [diff] [blame] | 444 | /* array macros read previous data so we must zero it */ |
| 445 | memset(type, 0, sizeof *type); |
| 446 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 447 | DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done); |
| 448 | |
| 449 | if (orig_type->range) { |
| 450 | type->range = calloc(1, sizeof *type->range); |
| 451 | LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 452 | LY_CHECK_RET(lysp_restr_dup(ctx, pmod, type->range, orig_type->range)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | if (orig_type->length) { |
| 456 | type->length = calloc(1, sizeof *type->length); |
| 457 | LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 458 | LY_CHECK_RET(lysp_restr_dup(ctx, pmod, type->length, orig_type->length)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 461 | DUP_ARRAY2(ctx, pmod, orig_type->patterns, type->patterns, lysp_restr_dup); |
| 462 | DUP_ARRAY2(ctx, pmod, orig_type->enums, type->enums, lysp_type_enum_dup); |
| 463 | DUP_ARRAY2(ctx, pmod, orig_type->bits, type->bits, lysp_type_enum_dup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 464 | LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, 0, 0, &type->path), done); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 465 | DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 466 | DUP_ARRAY2(ctx, pmod, orig_type->types, type->types, lysp_type_dup); |
| 467 | DUP_ARRAY2(ctx, pmod, orig_type->exts, type->exts, lysp_ext_dup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 468 | |
| 469 | type->pmod = orig_type->pmod; |
| 470 | type->compiled = orig_type->compiled; |
| 471 | |
| 472 | type->fraction_digits = orig_type->fraction_digits; |
| 473 | type->require_instance = orig_type->require_instance; |
| 474 | type->flags = orig_type->flags; |
| 475 | |
| 476 | done: |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 481 | lysp_when_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_when *when, |
| 482 | const struct lysp_when *orig_when) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 483 | { |
| 484 | LY_ERR ret = LY_SUCCESS; |
| 485 | |
| 486 | DUP_STRING(ctx, orig_when->cond, when->cond, ret); |
| 487 | DUP_STRING(ctx, orig_when->dsc, when->dsc, ret); |
| 488 | DUP_STRING(ctx, orig_when->ref, when->ref, ret); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 489 | DUP_ARRAY2(ctx, pmod, orig_when->exts, when->exts, lysp_ext_dup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 490 | |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 495 | lysp_node_common_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_node *node, |
| 496 | const struct lysp_node *orig) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 497 | { |
| 498 | LY_ERR ret = LY_SUCCESS; |
| 499 | |
| 500 | node->parent = NULL; |
| 501 | node->nodetype = orig->nodetype; |
| 502 | node->flags = orig->flags; |
| 503 | node->next = NULL; |
| 504 | DUP_STRING(ctx, orig->name, node->name, ret); |
| 505 | DUP_STRING(ctx, orig->dsc, node->dsc, ret); |
| 506 | DUP_STRING(ctx, orig->ref, node->ref, ret); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 507 | DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 508 | DUP_ARRAY2(ctx, pmod, orig->exts, node->exts, lysp_ext_dup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 509 | |
| 510 | return ret; |
| 511 | } |
| 512 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 513 | #define DUP_PWHEN(CTX, PMOD, ORIG, NEW) \ |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 514 | if (ORIG) { \ |
| 515 | NEW = calloc(1, sizeof *NEW); \ |
| 516 | LY_CHECK_ERR_RET(!NEW, LOGMEM(CTX), LY_EMEM); \ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 517 | LY_CHECK_RET(lysp_when_dup(CTX, PMOD, NEW, ORIG)); \ |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 520 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 521 | lysp_node_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_node *node, |
| 522 | const struct lysp_node *orig) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 523 | { |
| 524 | LY_ERR ret = LY_SUCCESS; |
| 525 | struct lysp_node_container *cont; |
| 526 | const struct lysp_node_container *orig_cont; |
| 527 | struct lysp_node_leaf *leaf; |
| 528 | const struct lysp_node_leaf *orig_leaf; |
| 529 | struct lysp_node_leaflist *llist; |
| 530 | const struct lysp_node_leaflist *orig_llist; |
| 531 | struct lysp_node_list *list; |
| 532 | const struct lysp_node_list *orig_list; |
| 533 | struct lysp_node_choice *choice; |
| 534 | const struct lysp_node_choice *orig_choice; |
| 535 | struct lysp_node_case *cas; |
| 536 | const struct lysp_node_case *orig_cas; |
| 537 | struct lysp_node_anydata *any; |
| 538 | const struct lysp_node_anydata *orig_any; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 539 | struct lysp_node_action *action; |
| 540 | const struct lysp_node_action *orig_action; |
| 541 | struct lysp_node_action_inout *action_inout; |
| 542 | const struct lysp_node_action_inout *orig_action_inout; |
| 543 | struct lysp_node_notif *notif; |
| 544 | const struct lysp_node_notif *orig_notif; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 545 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 546 | assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA | |
| 547 | LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 548 | |
| 549 | /* common part */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 550 | LY_CHECK_RET(lysp_node_common_dup(ctx, pmod, node, orig)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 551 | |
| 552 | /* specific part */ |
| 553 | switch (node->nodetype) { |
| 554 | case LYS_CONTAINER: |
| 555 | cont = (struct lysp_node_container *)node; |
| 556 | orig_cont = (const struct lysp_node_container *)orig; |
| 557 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 558 | DUP_PWHEN(ctx, pmod, orig_cont->when, cont->when); |
| 559 | DUP_ARRAY2(ctx, pmod, orig_cont->musts, cont->musts, lysp_restr_dup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 560 | DUP_STRING(ctx, orig_cont->presence, cont->presence, ret); |
| 561 | /* we do not need the rest */ |
| 562 | break; |
| 563 | case LYS_LEAF: |
| 564 | leaf = (struct lysp_node_leaf *)node; |
| 565 | orig_leaf = (const struct lysp_node_leaf *)orig; |
| 566 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 567 | DUP_PWHEN(ctx, pmod, orig_leaf->when, leaf->when); |
| 568 | DUP_ARRAY2(ctx, pmod, orig_leaf->musts, leaf->musts, lysp_restr_dup); |
| 569 | LY_CHECK_RET(lysp_type_dup(ctx, pmod, &leaf->type, &orig_leaf->type)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 570 | DUP_STRING(ctx, orig_leaf->units, leaf->units, ret); |
| 571 | LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt)); |
| 572 | break; |
| 573 | case LYS_LEAFLIST: |
| 574 | llist = (struct lysp_node_leaflist *)node; |
| 575 | orig_llist = (const struct lysp_node_leaflist *)orig; |
| 576 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 577 | DUP_PWHEN(ctx, pmod, orig_llist->when, llist->when); |
| 578 | DUP_ARRAY2(ctx, pmod, orig_llist->musts, llist->musts, lysp_restr_dup); |
| 579 | LY_CHECK_RET(lysp_type_dup(ctx, pmod, &llist->type, &orig_llist->type)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 580 | DUP_STRING(ctx, orig_llist->units, llist->units, ret); |
| 581 | DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup); |
| 582 | llist->min = orig_llist->min; |
| 583 | llist->max = orig_llist->max; |
| 584 | break; |
| 585 | case LYS_LIST: |
| 586 | list = (struct lysp_node_list *)node; |
| 587 | orig_list = (const struct lysp_node_list *)orig; |
| 588 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 589 | DUP_PWHEN(ctx, pmod, orig_list->when, list->when); |
| 590 | DUP_ARRAY2(ctx, pmod, orig_list->musts, list->musts, lysp_restr_dup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 591 | DUP_STRING(ctx, orig_list->key, list->key, ret); |
| 592 | /* we do not need these arrays */ |
| 593 | DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup); |
| 594 | list->min = orig_list->min; |
| 595 | list->max = orig_list->max; |
| 596 | break; |
| 597 | case LYS_CHOICE: |
| 598 | choice = (struct lysp_node_choice *)node; |
| 599 | orig_choice = (const struct lysp_node_choice *)orig; |
| 600 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 601 | DUP_PWHEN(ctx, pmod, orig_choice->when, choice->when); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 602 | /* we do not need children */ |
| 603 | LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt)); |
| 604 | break; |
| 605 | case LYS_CASE: |
| 606 | cas = (struct lysp_node_case *)node; |
| 607 | orig_cas = (const struct lysp_node_case *)orig; |
| 608 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 609 | DUP_PWHEN(ctx, pmod, orig_cas->when, cas->when); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 610 | /* we do not need children */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 611 | break; |
| 612 | case LYS_ANYDATA: |
| 613 | case LYS_ANYXML: |
| 614 | any = (struct lysp_node_anydata *)node; |
| 615 | orig_any = (const struct lysp_node_anydata *)orig; |
| 616 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 617 | DUP_PWHEN(ctx, pmod, orig_any->when, any->when); |
| 618 | DUP_ARRAY2(ctx, pmod, orig_any->musts, any->musts, lysp_restr_dup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 619 | break; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 620 | case LYS_RPC: |
| 621 | case LYS_ACTION: |
| 622 | action = (struct lysp_node_action *)node; |
| 623 | orig_action = (const struct lysp_node_action *)orig; |
| 624 | |
| 625 | action->input.nodetype = orig_action->input.nodetype; |
| 626 | action->output.nodetype = orig_action->output.nodetype; |
| 627 | /* we do not need the rest */ |
| 628 | break; |
| 629 | case LYS_INPUT: |
| 630 | case LYS_OUTPUT: |
| 631 | action_inout = (struct lysp_node_action_inout *)node; |
| 632 | orig_action_inout = (const struct lysp_node_action_inout *)orig; |
| 633 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 634 | DUP_ARRAY2(ctx, pmod, orig_action_inout->musts, action_inout->musts, lysp_restr_dup); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 635 | /* we do not need the rest */ |
| 636 | break; |
| 637 | case LYS_NOTIF: |
| 638 | notif = (struct lysp_node_notif *)node; |
| 639 | orig_notif = (const struct lysp_node_notif *)orig; |
| 640 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 641 | DUP_ARRAY2(ctx, pmod, orig_notif->musts, notif->musts, lysp_restr_dup); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 642 | /* we do not need the rest */ |
| 643 | break; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 644 | default: |
| 645 | LOGINT_RET(ctx); |
| 646 | } |
| 647 | |
| 648 | return ret; |
| 649 | } |
| 650 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 651 | /** |
| 652 | * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied. |
| 653 | * |
| 654 | * @param[in] ctx libyang context. |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 655 | * @param[in] pmod Current parsed module. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 656 | * @param[in] pnode Node to duplicate. |
| 657 | * @param[in] with_links Whether to also copy any links (child, parent pointers). |
| 658 | * @param[out] dup_p Duplicated parsed node. |
| 659 | * @return LY_ERR value. |
| 660 | */ |
| 661 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 662 | lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_module *pmod, const struct lysp_node *pnode, |
| 663 | ly_bool with_links, struct lysp_node **dup_p) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 664 | { |
| 665 | LY_ERR ret = LY_SUCCESS; |
| 666 | void *mem = NULL; |
| 667 | |
| 668 | if (!pnode) { |
| 669 | *dup_p = NULL; |
| 670 | return LY_SUCCESS; |
| 671 | } |
| 672 | |
| 673 | switch (pnode->nodetype) { |
| 674 | case LYS_CONTAINER: |
| 675 | mem = calloc(1, sizeof(struct lysp_node_container)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 676 | break; |
| 677 | case LYS_LEAF: |
| 678 | mem = calloc(1, sizeof(struct lysp_node_leaf)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 679 | break; |
| 680 | case LYS_LEAFLIST: |
| 681 | mem = calloc(1, sizeof(struct lysp_node_leaflist)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 682 | break; |
| 683 | case LYS_LIST: |
| 684 | mem = calloc(1, sizeof(struct lysp_node_list)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 685 | break; |
| 686 | case LYS_CHOICE: |
| 687 | mem = calloc(1, sizeof(struct lysp_node_choice)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 688 | break; |
| 689 | case LYS_CASE: |
| 690 | mem = calloc(1, sizeof(struct lysp_node_case)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 691 | break; |
| 692 | case LYS_ANYDATA: |
| 693 | case LYS_ANYXML: |
| 694 | mem = calloc(1, sizeof(struct lysp_node_anydata)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 695 | break; |
| 696 | case LYS_INPUT: |
| 697 | case LYS_OUTPUT: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 698 | mem = calloc(1, sizeof(struct lysp_node_action_inout)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 699 | break; |
| 700 | case LYS_ACTION: |
| 701 | case LYS_RPC: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 702 | mem = calloc(1, sizeof(struct lysp_node_action)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 703 | break; |
| 704 | case LYS_NOTIF: |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 705 | mem = calloc(1, sizeof(struct lysp_node_notif)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 706 | break; |
| 707 | default: |
| 708 | LOGINT_RET(ctx); |
| 709 | } |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 710 | LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 711 | LY_CHECK_GOTO(ret = lysp_node_dup(ctx, pmod, mem, pnode), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 712 | |
| 713 | if (with_links) { |
Michal Vasko | ec8f427 | 2021-10-27 09:14:03 +0200 | [diff] [blame] | 714 | /* copy also parent, child, action, and notification pointers */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 715 | ((struct lysp_node *)mem)->parent = pnode->parent; |
| 716 | switch (pnode->nodetype) { |
| 717 | case LYS_CONTAINER: |
| 718 | ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child; |
Michal Vasko | ec8f427 | 2021-10-27 09:14:03 +0200 | [diff] [blame] | 719 | ((struct lysp_node_container *)mem)->actions = ((struct lysp_node_container *)pnode)->actions; |
| 720 | ((struct lysp_node_container *)mem)->notifs = ((struct lysp_node_container *)pnode)->notifs; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 721 | break; |
| 722 | case LYS_LIST: |
| 723 | ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child; |
Michal Vasko | ec8f427 | 2021-10-27 09:14:03 +0200 | [diff] [blame] | 724 | ((struct lysp_node_list *)mem)->actions = ((struct lysp_node_list *)pnode)->actions; |
| 725 | ((struct lysp_node_list *)mem)->notifs = ((struct lysp_node_list *)pnode)->notifs; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 726 | break; |
| 727 | case LYS_CHOICE: |
| 728 | ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child; |
| 729 | break; |
| 730 | case LYS_CASE: |
| 731 | ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child; |
| 732 | break; |
| 733 | default: |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | cleanup: |
| 739 | if (ret) { |
| 740 | free(mem); |
| 741 | } else { |
| 742 | *dup_p = mem; |
| 743 | } |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | #define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 748 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s of %s node - it is not possible to %s \"%s\" property.", \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 749 | AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\ |
| 750 | ret = LY_EVALID; \ |
| 751 | goto cleanup; |
| 752 | |
| 753 | #define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \ |
| 754 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 755 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid %s of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 756 | AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 757 | ret = LY_EVALID; \ |
| 758 | goto cleanup; \ |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * @brief Apply refine. |
| 763 | * |
| 764 | * @param[in] ctx Compile context. |
| 765 | * @param[in] rfn Refine to apply. |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 766 | * @param[in] rfn_pmod Local module fo the refine. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 767 | * @param[in,out] target Refine target. |
| 768 | * @return LY_ERR value. |
| 769 | */ |
| 770 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 771 | lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, const struct lysp_module *rfn_pmod, struct lysp_node *target) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 772 | { |
| 773 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 774 | struct lys_module *orig_mod = ctx->cur_mod; |
| 775 | struct lysp_module *orig_pmod = ctx->pmod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 776 | LY_ARRAY_COUNT_TYPE u; |
| 777 | struct lysp_qname *qname; |
| 778 | struct lysp_restr **musts, *must; |
| 779 | uint32_t *num; |
| 780 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 781 | /* use module from the refine */ |
| 782 | ctx->cur_mod = rfn_pmod->mod; |
| 783 | ctx->pmod = (struct lysp_module *)rfn_pmod; |
| 784 | |
| 785 | /* keep the current path and add to it */ |
| 786 | lysc_update_path(ctx, NULL, "{refine}"); |
| 787 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 788 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 789 | /* default value */ |
| 790 | if (rfn->dflts) { |
| 791 | switch (target->nodetype) { |
| 792 | case LYS_LEAF: |
| 793 | AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default"); |
| 794 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 795 | lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 796 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup); |
| 797 | break; |
| 798 | case LYS_LEAFLIST: |
| 799 | if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 800 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 801 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
| 802 | ret = LY_EVALID; |
| 803 | goto cleanup; |
| 804 | } |
| 805 | |
| 806 | FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free); |
| 807 | ((struct lysp_node_leaflist *)target)->dflts = NULL; |
| 808 | LY_ARRAY_FOR(rfn->dflts, u) { |
| 809 | LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup); |
| 810 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup); |
| 811 | } |
| 812 | break; |
| 813 | case LYS_CHOICE: |
| 814 | AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default"); |
| 815 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 816 | lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 817 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup); |
| 818 | break; |
| 819 | default: |
| 820 | AMEND_WRONG_NODETYPE("refine", "replace", "default"); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* description */ |
| 825 | if (rfn->dsc) { |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 826 | lydict_remove(ctx->ctx, target->dsc); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 827 | DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup); |
| 828 | } |
| 829 | |
| 830 | /* reference */ |
| 831 | if (rfn->ref) { |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 832 | lydict_remove(ctx->ctx, target->ref); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 833 | DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup); |
| 834 | } |
| 835 | |
| 836 | /* config */ |
| 837 | if (rfn->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 838 | if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 839 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 840 | (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT)) ? "RPC/action" : |
| 841 | ctx->compile_opts & LYS_IS_NOTIF ? "notification" : "a subtree ignoring config", ctx->path); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 842 | } else { |
| 843 | target->flags &= ~LYS_CONFIG_MASK; |
| 844 | target->flags |= rfn->flags & LYS_CONFIG_MASK; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* mandatory */ |
| 849 | if (rfn->flags & LYS_MAND_MASK) { |
| 850 | switch (target->nodetype) { |
| 851 | case LYS_LEAF: |
| 852 | case LYS_CHOICE: |
| 853 | case LYS_ANYDATA: |
| 854 | case LYS_ANYXML: |
| 855 | break; |
| 856 | default: |
| 857 | AMEND_WRONG_NODETYPE("refine", "replace", "mandatory"); |
| 858 | } |
| 859 | |
| 860 | target->flags &= ~LYS_MAND_MASK; |
| 861 | target->flags |= rfn->flags & LYS_MAND_MASK; |
| 862 | } |
| 863 | |
| 864 | /* presence */ |
| 865 | if (rfn->presence) { |
| 866 | switch (target->nodetype) { |
| 867 | case LYS_CONTAINER: |
| 868 | break; |
| 869 | default: |
| 870 | AMEND_WRONG_NODETYPE("refine", "replace", "presence"); |
| 871 | } |
| 872 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 873 | lydict_remove(ctx->ctx, ((struct lysp_node_container *)target)->presence); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 874 | DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup); |
| 875 | } |
| 876 | |
| 877 | /* must */ |
| 878 | if (rfn->musts) { |
| 879 | switch (target->nodetype) { |
| 880 | case LYS_CONTAINER: |
| 881 | case LYS_LIST: |
| 882 | case LYS_LEAF: |
| 883 | case LYS_LEAFLIST: |
| 884 | case LYS_ANYDATA: |
| 885 | case LYS_ANYXML: |
| 886 | musts = &((struct lysp_node_container *)target)->musts; |
| 887 | break; |
| 888 | default: |
| 889 | AMEND_WRONG_NODETYPE("refine", "add", "must"); |
| 890 | } |
| 891 | |
| 892 | LY_ARRAY_FOR(rfn->musts, u) { |
| 893 | LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 894 | LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, rfn_pmod, must, &rfn->musts[u]), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | /* min-elements */ |
| 899 | if (rfn->flags & LYS_SET_MIN) { |
| 900 | switch (target->nodetype) { |
| 901 | case LYS_LEAFLIST: |
| 902 | num = &((struct lysp_node_leaflist *)target)->min; |
| 903 | break; |
| 904 | case LYS_LIST: |
| 905 | num = &((struct lysp_node_list *)target)->min; |
| 906 | break; |
| 907 | default: |
| 908 | AMEND_WRONG_NODETYPE("refine", "replace", "min-elements"); |
| 909 | } |
| 910 | |
| 911 | *num = rfn->min; |
| 912 | } |
| 913 | |
| 914 | /* max-elements */ |
| 915 | if (rfn->flags & LYS_SET_MAX) { |
| 916 | switch (target->nodetype) { |
| 917 | case LYS_LEAFLIST: |
| 918 | num = &((struct lysp_node_leaflist *)target)->max; |
| 919 | break; |
| 920 | case LYS_LIST: |
| 921 | num = &((struct lysp_node_list *)target)->max; |
| 922 | break; |
| 923 | default: |
| 924 | AMEND_WRONG_NODETYPE("refine", "replace", "max-elements"); |
| 925 | } |
| 926 | |
| 927 | *num = rfn->max; |
| 928 | } |
| 929 | |
| 930 | /* if-feature */ |
| 931 | if (rfn->iffeatures) { |
| 932 | switch (target->nodetype) { |
| 933 | case LYS_LEAF: |
| 934 | case LYS_LEAFLIST: |
| 935 | case LYS_LIST: |
| 936 | case LYS_CONTAINER: |
| 937 | case LYS_CHOICE: |
| 938 | case LYS_CASE: |
| 939 | case LYS_ANYDATA: |
| 940 | case LYS_ANYXML: |
| 941 | break; |
| 942 | default: |
| 943 | AMEND_WRONG_NODETYPE("refine", "add", "if-feature"); |
| 944 | } |
| 945 | |
| 946 | LY_ARRAY_FOR(rfn->iffeatures, u) { |
| 947 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup); |
| 948 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup); |
| 949 | } |
| 950 | } |
| 951 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 952 | /* extension instances */ |
| 953 | DUP_ARRAY2(ctx->ctx, rfn_pmod, rfn->exts, target->exts, lysp_ext_dup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 954 | |
| 955 | cleanup: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 956 | ctx->cur_mod = orig_mod; |
| 957 | ctx->pmod = orig_pmod; |
| 958 | |
| 959 | lysc_update_path(ctx, NULL, NULL); |
| 960 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 961 | return ret; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * @brief Apply deviate add. |
| 966 | * |
| 967 | * @param[in] ctx Compile context. |
| 968 | * @param[in] d Deviate add to apply. |
| 969 | * @param[in,out] target Deviation target. |
| 970 | * @return LY_ERR value. |
| 971 | */ |
| 972 | static LY_ERR |
| 973 | lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target) |
| 974 | { |
| 975 | LY_ERR ret = LY_SUCCESS; |
| 976 | LY_ARRAY_COUNT_TYPE u; |
| 977 | struct lysp_qname *qname; |
| 978 | uint32_t *num; |
| 979 | struct lysp_restr **musts, *must; |
| 980 | |
| 981 | #define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 982 | if (((TYPE)target)->MEMBER) { \ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 983 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 984 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 985 | ret = LY_EVALID; \ |
| 986 | goto cleanup; \ |
| 987 | } |
| 988 | |
| 989 | /* [units-stmt] */ |
| 990 | if (d->units) { |
| 991 | switch (target->nodetype) { |
| 992 | case LYS_LEAF: |
| 993 | case LYS_LEAFLIST: |
| 994 | break; |
| 995 | default: |
| 996 | AMEND_WRONG_NODETYPE("deviation", "add", "units"); |
| 997 | } |
| 998 | |
| 999 | DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units); |
| 1000 | DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup); |
| 1001 | } |
| 1002 | |
| 1003 | /* *must-stmt */ |
| 1004 | if (d->musts) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1005 | musts = lysp_node_musts_p(target); |
| 1006 | if (!musts) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1007 | AMEND_WRONG_NODETYPE("deviation", "add", "must"); |
| 1008 | } |
| 1009 | |
| 1010 | LY_ARRAY_FOR(d->musts, u) { |
| 1011 | LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1012 | LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, ctx->pmod, must, &d->musts[u]), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | /* *unique-stmt */ |
| 1017 | if (d->uniques) { |
| 1018 | switch (target->nodetype) { |
| 1019 | case LYS_LIST: |
| 1020 | break; |
| 1021 | default: |
| 1022 | AMEND_WRONG_NODETYPE("deviation", "add", "unique"); |
| 1023 | } |
| 1024 | |
| 1025 | LY_ARRAY_FOR(d->uniques, u) { |
| 1026 | LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup); |
| 1027 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /* *default-stmt */ |
| 1032 | if (d->dflts) { |
| 1033 | switch (target->nodetype) { |
| 1034 | case LYS_LEAF: |
| 1035 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 1036 | DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str); |
| 1037 | |
| 1038 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup); |
| 1039 | break; |
| 1040 | case LYS_LEAFLIST: |
| 1041 | LY_ARRAY_FOR(d->dflts, u) { |
| 1042 | LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup); |
| 1043 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup); |
| 1044 | } |
| 1045 | break; |
| 1046 | case LYS_CHOICE: |
| 1047 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 1048 | DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str); |
| 1049 | |
| 1050 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup); |
| 1051 | break; |
| 1052 | default: |
| 1053 | AMEND_WRONG_NODETYPE("deviation", "add", "default"); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | /* [config-stmt] */ |
| 1058 | if (d->flags & LYS_CONFIG_MASK) { |
| 1059 | switch (target->nodetype) { |
| 1060 | case LYS_CONTAINER: |
| 1061 | case LYS_LEAF: |
| 1062 | case LYS_LEAFLIST: |
| 1063 | case LYS_LIST: |
| 1064 | case LYS_CHOICE: |
| 1065 | case LYS_ANYDATA: |
| 1066 | case LYS_ANYXML: |
| 1067 | break; |
| 1068 | default: |
| 1069 | AMEND_WRONG_NODETYPE("deviation", "add", "config"); |
| 1070 | } |
| 1071 | |
| 1072 | if (target->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1073 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1074 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 1075 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 1076 | ret = LY_EVALID; |
| 1077 | goto cleanup; |
| 1078 | } |
| 1079 | |
| 1080 | target->flags |= d->flags & LYS_CONFIG_MASK; |
| 1081 | } |
| 1082 | |
| 1083 | /* [mandatory-stmt] */ |
| 1084 | if (d->flags & LYS_MAND_MASK) { |
| 1085 | switch (target->nodetype) { |
| 1086 | case LYS_LEAF: |
| 1087 | case LYS_CHOICE: |
| 1088 | case LYS_ANYDATA: |
| 1089 | case LYS_ANYXML: |
| 1090 | break; |
| 1091 | default: |
| 1092 | AMEND_WRONG_NODETYPE("deviation", "add", "mandatory"); |
| 1093 | } |
| 1094 | |
| 1095 | if (target->flags & LYS_MAND_MASK) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1096 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1097 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 1098 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 1099 | ret = LY_EVALID; |
| 1100 | goto cleanup; |
| 1101 | } |
| 1102 | |
| 1103 | target->flags |= d->flags & LYS_MAND_MASK; |
| 1104 | } |
| 1105 | |
| 1106 | /* [min-elements-stmt] */ |
| 1107 | if (d->flags & LYS_SET_MIN) { |
| 1108 | switch (target->nodetype) { |
| 1109 | case LYS_LEAFLIST: |
| 1110 | num = &((struct lysp_node_leaflist *)target)->min; |
| 1111 | break; |
| 1112 | case LYS_LIST: |
| 1113 | num = &((struct lysp_node_list *)target)->min; |
| 1114 | break; |
| 1115 | default: |
| 1116 | AMEND_WRONG_NODETYPE("deviation", "add", "min-elements"); |
| 1117 | } |
| 1118 | |
| 1119 | if (target->flags & LYS_SET_MIN) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1120 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1121 | "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num); |
| 1122 | ret = LY_EVALID; |
| 1123 | goto cleanup; |
| 1124 | } |
| 1125 | |
| 1126 | *num = d->min; |
| 1127 | } |
| 1128 | |
| 1129 | /* [max-elements-stmt] */ |
| 1130 | if (d->flags & LYS_SET_MAX) { |
| 1131 | switch (target->nodetype) { |
| 1132 | case LYS_LEAFLIST: |
| 1133 | num = &((struct lysp_node_leaflist *)target)->max; |
| 1134 | break; |
| 1135 | case LYS_LIST: |
| 1136 | num = &((struct lysp_node_list *)target)->max; |
| 1137 | break; |
| 1138 | default: |
| 1139 | AMEND_WRONG_NODETYPE("deviation", "add", "max-elements"); |
| 1140 | } |
| 1141 | |
| 1142 | if (target->flags & LYS_SET_MAX) { |
| 1143 | if (*num) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1144 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1145 | "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").", |
| 1146 | *num); |
| 1147 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1148 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1149 | "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\")."); |
| 1150 | } |
| 1151 | ret = LY_EVALID; |
| 1152 | goto cleanup; |
| 1153 | } |
| 1154 | |
| 1155 | *num = d->max; |
| 1156 | } |
| 1157 | |
| 1158 | cleanup: |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * @brief Apply deviate delete. |
| 1164 | * |
| 1165 | * @param[in] ctx Compile context. |
| 1166 | * @param[in] d Deviate delete to apply. |
| 1167 | * @param[in,out] target Deviation target. |
| 1168 | * @return LY_ERR value. |
| 1169 | */ |
| 1170 | static LY_ERR |
| 1171 | lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target) |
| 1172 | { |
| 1173 | LY_ERR ret = LY_SUCCESS; |
| 1174 | struct lysp_restr **musts; |
| 1175 | LY_ARRAY_COUNT_TYPE u, v; |
| 1176 | struct lysp_qname **uniques, **dflts; |
| 1177 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1178 | #define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, FREE_CTX, PROPERTY) \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1179 | LY_ARRAY_FOR(d->DEV_ARRAY, u) { \ |
| 1180 | int found = 0; \ |
| 1181 | LY_ARRAY_FOR(ORIG_ARRAY, v) { \ |
| 1182 | if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \ |
| 1183 | found = 1; \ |
| 1184 | break; \ |
| 1185 | } \ |
| 1186 | } \ |
| 1187 | if (!found) { \ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1188 | LOGVAL(ctx->ctx, LYVE_REFERENCE, \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1189 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 1190 | PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \ |
| 1191 | ret = LY_EVALID; \ |
| 1192 | goto cleanup; \ |
| 1193 | } \ |
| 1194 | LY_ARRAY_DECREMENT(ORIG_ARRAY); \ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1195 | FREE_FUNC(FREE_CTX, &(ORIG_ARRAY)[v]); \ |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1196 | if (v < LY_ARRAY_COUNT(ORIG_ARRAY)) { \ |
| 1197 | memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \ |
| 1198 | } \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1199 | } \ |
| 1200 | if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \ |
| 1201 | LY_ARRAY_FREE(ORIG_ARRAY); \ |
| 1202 | ORIG_ARRAY = NULL; \ |
| 1203 | } |
| 1204 | |
| 1205 | #define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 1206 | if (!((TYPE)target)->MEMBER) { \ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1207 | LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1208 | ret = LY_EVALID; \ |
| 1209 | goto cleanup; \ |
| 1210 | } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1211 | LOGVAL(ctx->ctx, LYVE_REFERENCE, \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1212 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \ |
| 1213 | PROPERTY, VALUE, ((TYPE)target)->MEMBER); \ |
| 1214 | ret = LY_EVALID; \ |
| 1215 | goto cleanup; \ |
| 1216 | } |
| 1217 | |
| 1218 | /* [units-stmt] */ |
| 1219 | if (d->units) { |
| 1220 | switch (target->nodetype) { |
| 1221 | case LYS_LEAF: |
| 1222 | case LYS_LEAFLIST: |
| 1223 | break; |
| 1224 | default: |
| 1225 | AMEND_WRONG_NODETYPE("deviation", "delete", "units"); |
| 1226 | } |
| 1227 | |
| 1228 | DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units); |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 1229 | lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1230 | ((struct lysp_node_leaf *)target)->units = NULL; |
| 1231 | } |
| 1232 | |
| 1233 | /* *must-stmt */ |
| 1234 | if (d->musts) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1235 | musts = lysp_node_musts_p(target); |
| 1236 | if (!musts) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1237 | AMEND_WRONG_NODETYPE("deviation", "delete", "must"); |
| 1238 | } |
| 1239 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1240 | DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, &ctx->free_ctx, "must"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /* *unique-stmt */ |
| 1244 | if (d->uniques) { |
| 1245 | switch (target->nodetype) { |
| 1246 | case LYS_LIST: |
| 1247 | break; |
| 1248 | default: |
| 1249 | AMEND_WRONG_NODETYPE("deviation", "delete", "unique"); |
| 1250 | } |
| 1251 | |
| 1252 | uniques = &((struct lysp_node_list *)target)->uniques; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1253 | DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, ctx->ctx, "unique"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /* *default-stmt */ |
| 1257 | if (d->dflts) { |
| 1258 | switch (target->nodetype) { |
| 1259 | case LYS_LEAF: |
| 1260 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 1261 | DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str); |
| 1262 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 1263 | lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1264 | ((struct lysp_node_leaf *)target)->dflt.str = NULL; |
| 1265 | break; |
| 1266 | case LYS_LEAFLIST: |
| 1267 | dflts = &((struct lysp_node_leaflist *)target)->dflts; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1268 | DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, ctx->ctx, "default"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1269 | break; |
| 1270 | case LYS_CHOICE: |
| 1271 | AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default"); |
| 1272 | DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str); |
| 1273 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 1274 | lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1275 | ((struct lysp_node_choice *)target)->dflt.str = NULL; |
| 1276 | break; |
| 1277 | default: |
| 1278 | AMEND_WRONG_NODETYPE("deviation", "delete", "default"); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | cleanup: |
| 1283 | return ret; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * @brief Apply deviate replace. |
| 1288 | * |
| 1289 | * @param[in] ctx Compile context. |
| 1290 | * @param[in] d Deviate replace to apply. |
| 1291 | * @param[in,out] target Deviation target. |
| 1292 | * @return LY_ERR value. |
| 1293 | */ |
| 1294 | static LY_ERR |
| 1295 | lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target) |
| 1296 | { |
| 1297 | LY_ERR ret = LY_SUCCESS; |
| 1298 | uint32_t *num; |
| 1299 | |
| 1300 | #define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 1301 | if (!((TYPE)target)->MEMBER) { \ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1302 | LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1303 | ret = LY_EVALID; \ |
| 1304 | goto cleanup; \ |
| 1305 | } |
| 1306 | |
| 1307 | /* [type-stmt] */ |
| 1308 | if (d->type) { |
| 1309 | switch (target->nodetype) { |
| 1310 | case LYS_LEAF: |
| 1311 | case LYS_LEAFLIST: |
| 1312 | break; |
| 1313 | default: |
| 1314 | AMEND_WRONG_NODETYPE("deviation", "replace", "type"); |
| 1315 | } |
| 1316 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1317 | lysp_type_free(&ctx->free_ctx, &((struct lysp_node_leaf *)target)->type); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1318 | lysp_type_dup(ctx->ctx, ctx->pmod, &((struct lysp_node_leaf *)target)->type, d->type); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /* [units-stmt] */ |
| 1322 | if (d->units) { |
| 1323 | switch (target->nodetype) { |
| 1324 | case LYS_LEAF: |
| 1325 | case LYS_LEAFLIST: |
| 1326 | break; |
| 1327 | default: |
| 1328 | AMEND_WRONG_NODETYPE("deviation", "replace", "units"); |
| 1329 | } |
| 1330 | |
| 1331 | DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units); |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 1332 | lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1333 | DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup); |
| 1334 | } |
| 1335 | |
| 1336 | /* [default-stmt] */ |
| 1337 | if (d->dflt.str) { |
| 1338 | switch (target->nodetype) { |
| 1339 | case LYS_LEAF: |
| 1340 | DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str); |
| 1341 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 1342 | lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1343 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup); |
| 1344 | break; |
| 1345 | case LYS_CHOICE: |
| 1346 | DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt); |
| 1347 | |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 1348 | lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1349 | LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup); |
| 1350 | break; |
| 1351 | default: |
| 1352 | AMEND_WRONG_NODETYPE("deviation", "replace", "default"); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | /* [config-stmt] */ |
| 1357 | if (d->flags & LYS_CONFIG_MASK) { |
| 1358 | switch (target->nodetype) { |
| 1359 | case LYS_CONTAINER: |
| 1360 | case LYS_LEAF: |
| 1361 | case LYS_LEAFLIST: |
| 1362 | case LYS_LIST: |
| 1363 | case LYS_CHOICE: |
| 1364 | case LYS_ANYDATA: |
| 1365 | case LYS_ANYXML: |
| 1366 | break; |
| 1367 | default: |
| 1368 | AMEND_WRONG_NODETYPE("deviation", "replace", "config"); |
| 1369 | } |
| 1370 | |
| 1371 | if (!(target->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1372 | LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config", |
| 1373 | d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1374 | ret = LY_EVALID; |
| 1375 | goto cleanup; |
| 1376 | } |
| 1377 | |
| 1378 | target->flags &= ~LYS_CONFIG_MASK; |
| 1379 | target->flags |= d->flags & LYS_CONFIG_MASK; |
| 1380 | } |
| 1381 | |
| 1382 | /* [mandatory-stmt] */ |
| 1383 | if (d->flags & LYS_MAND_MASK) { |
| 1384 | switch (target->nodetype) { |
| 1385 | case LYS_LEAF: |
| 1386 | case LYS_CHOICE: |
| 1387 | case LYS_ANYDATA: |
| 1388 | case LYS_ANYXML: |
| 1389 | break; |
| 1390 | default: |
| 1391 | AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory"); |
| 1392 | } |
| 1393 | |
| 1394 | if (!(target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1395 | LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory", |
| 1396 | d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1397 | ret = LY_EVALID; |
| 1398 | goto cleanup; |
| 1399 | } |
| 1400 | |
| 1401 | target->flags &= ~LYS_MAND_MASK; |
| 1402 | target->flags |= d->flags & LYS_MAND_MASK; |
| 1403 | } |
| 1404 | |
| 1405 | /* [min-elements-stmt] */ |
| 1406 | if (d->flags & LYS_SET_MIN) { |
| 1407 | switch (target->nodetype) { |
| 1408 | case LYS_LEAFLIST: |
| 1409 | num = &((struct lysp_node_leaflist *)target)->min; |
| 1410 | break; |
| 1411 | case LYS_LIST: |
| 1412 | num = &((struct lysp_node_list *)target)->min; |
| 1413 | break; |
| 1414 | default: |
| 1415 | AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements"); |
| 1416 | } |
| 1417 | |
| 1418 | if (!(target->flags & LYS_SET_MIN)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1419 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present."); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1420 | ret = LY_EVALID; |
| 1421 | goto cleanup; |
| 1422 | } |
| 1423 | |
| 1424 | *num = d->min; |
| 1425 | } |
| 1426 | |
| 1427 | /* [max-elements-stmt] */ |
| 1428 | if (d->flags & LYS_SET_MAX) { |
| 1429 | switch (target->nodetype) { |
| 1430 | case LYS_LEAFLIST: |
| 1431 | num = &((struct lysp_node_leaflist *)target)->max; |
| 1432 | break; |
| 1433 | case LYS_LIST: |
| 1434 | num = &((struct lysp_node_list *)target)->max; |
| 1435 | break; |
| 1436 | default: |
| 1437 | AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements"); |
| 1438 | } |
| 1439 | |
| 1440 | if (!(target->flags & LYS_SET_MAX)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1441 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present."); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1442 | ret = LY_EVALID; |
| 1443 | goto cleanup; |
| 1444 | } |
| 1445 | |
| 1446 | *num = d->max; |
| 1447 | } |
| 1448 | |
| 1449 | cleanup: |
| 1450 | return ret; |
| 1451 | } |
| 1452 | |
| 1453 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1454 | * @brief Apply deviation with all its deviates. |
| 1455 | * |
| 1456 | * @param[in] ctx Compile context. |
| 1457 | * @param[in] dev Deviation to apply. |
| 1458 | * @param[in] dev_pmod Local module of the deviation. |
| 1459 | * @param[in,out] target Deviation target. |
| 1460 | * @return LY_ERR value. |
| 1461 | */ |
| 1462 | static LY_ERR |
| 1463 | lys_apply_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev, const struct lysp_module *dev_pmod, |
| 1464 | struct lysp_node *target) |
| 1465 | { |
| 1466 | LY_ERR ret = LY_SUCCESS; |
| 1467 | struct lys_module *orig_mod = ctx->cur_mod; |
| 1468 | struct lysp_module *orig_pmod = ctx->pmod; |
| 1469 | char orig_path[LYSC_CTX_BUFSIZE]; |
| 1470 | struct lysp_deviate *d; |
| 1471 | |
| 1472 | /* clear path and set modules */ |
| 1473 | strcpy(orig_path, ctx->path); |
| 1474 | ctx->path_len = 1; |
| 1475 | ctx->cur_mod = dev_pmod->mod; |
| 1476 | ctx->pmod = (struct lysp_module *)dev_pmod; |
| 1477 | |
| 1478 | /* generate correct path */ |
| 1479 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 1480 | lysc_update_path(ctx, NULL, dev->nodeid); |
| 1481 | |
| 1482 | LY_LIST_FOR(dev->deviates, d) { |
| 1483 | switch (d->mod) { |
| 1484 | case LYS_DEV_ADD: |
| 1485 | ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, target); |
| 1486 | break; |
| 1487 | case LYS_DEV_DELETE: |
| 1488 | ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, target); |
| 1489 | break; |
| 1490 | case LYS_DEV_REPLACE: |
| 1491 | ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, target); |
| 1492 | break; |
| 1493 | default: |
| 1494 | LOGINT(ctx->ctx); |
| 1495 | ret = LY_EINT; |
| 1496 | } |
| 1497 | LY_CHECK_GOTO(ret, cleanup); |
| 1498 | } |
| 1499 | |
| 1500 | /* deviation extension instances */ |
| 1501 | DUP_ARRAY2(ctx->ctx, dev_pmod, dev->exts, target->exts, lysp_ext_dup); |
| 1502 | |
| 1503 | cleanup: |
| 1504 | ctx->cur_mod = orig_mod; |
| 1505 | ctx->pmod = orig_pmod; |
| 1506 | |
| 1507 | strcpy(ctx->path, orig_path); |
| 1508 | ctx->path_len = strlen(ctx->path); |
| 1509 | return ret; |
| 1510 | } |
| 1511 | |
| 1512 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1513 | * @brief Check whether a compiled node matches a single schema nodeid name test. |
| 1514 | * |
| 1515 | * @param[in,out] node Compiled node to consider. On a match it is moved to its parent. |
| 1516 | * @param[in] mod Expected module. |
| 1517 | * @param[in] name Expected name. |
| 1518 | * @param[in] name_len Length of @p name. |
| 1519 | * @return Whether it is a match or not. |
| 1520 | */ |
| 1521 | static ly_bool |
| 1522 | lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name, |
| 1523 | size_t name_len) |
| 1524 | { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1525 | /* compare with the module of the node */ |
Radek Krejci | 7d95fbb | 2021-01-26 17:33:13 +0100 | [diff] [blame] | 1526 | if ((*node)->module != mod) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1527 | return 0; |
| 1528 | } |
| 1529 | |
| 1530 | /* compare names */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1531 | if (ly_strncmp((*node)->name, name, name_len)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1532 | return 0; |
| 1533 | } |
| 1534 | |
Michal Vasko | 2a66871 | 2020-10-21 11:48:09 +0200 | [diff] [blame] | 1535 | /* move to next parent */ |
Radek Krejci | 7d95fbb | 2021-01-26 17:33:13 +0100 | [diff] [blame] | 1536 | *node = (*node)->parent; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1537 | |
| 1538 | return 1; |
| 1539 | } |
| 1540 | |
| 1541 | /** |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1542 | * @brief Check whether a compiled ext instance matches a single schema nodeid name test. |
| 1543 | * |
| 1544 | * @param[in,out] ext Compiled ext instance to consider. On a match it is zeroed to not match again. |
| 1545 | * @param[in] mod Expected module. |
| 1546 | * @param[in] name Expected name. |
| 1547 | * @param[in] name_len Length of @p name. |
| 1548 | * @return Whether it is a match or not. |
| 1549 | */ |
| 1550 | static ly_bool |
| 1551 | lysp_schema_nodeid_match_ext(const struct lysc_ext_instance **ext, const struct lys_module *mod, const char *name, |
| 1552 | size_t name_len) |
| 1553 | { |
| 1554 | /* compare with the module */ |
| 1555 | if ((*ext)->module != mod) { |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | /* compare names (argument) */ |
| 1560 | if (ly_strncmp((*ext)->argument, name, name_len)) { |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | /* zero */ |
| 1565 | *ext = NULL; |
| 1566 | |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
| 1570 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1571 | * @brief Check whether a node matches specific schema nodeid. |
| 1572 | * |
| 1573 | * @param[in] exp Parsed nodeid to match. |
| 1574 | * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix. |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1575 | * @param[in] exp_ext Extension instance in which @p exp is defined, it means it targets an extension instance. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1576 | * @param[in] ctx_node Initial context node that should match, only for descendant paths. |
| 1577 | * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched. |
| 1578 | * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled. |
| 1579 | * @param[in] pnode_mod Compiled @p pnode to-be module. |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1580 | * @param[in] pnode_ext Extension instance in which @p pnode is defined. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1581 | * @return Whether it is a match or not. |
| 1582 | */ |
| 1583 | static ly_bool |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1584 | lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, |
| 1585 | const struct lysp_ext_instance *exp_ext, const struct lysc_node *ctx_node, const struct lysc_node *parent, |
| 1586 | const struct lysp_node *pnode, const struct lys_module *pnode_mod, const struct lysc_ext_instance *pnode_ext) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1587 | { |
| 1588 | uint32_t i; |
| 1589 | const struct lys_module *mod; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 1590 | const char *name = NULL; |
| 1591 | size_t name_len = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1592 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1593 | if (exp_ext && !pnode_ext) { |
| 1594 | /* extension instance augment and standard node, will never match */ |
| 1595 | return 0; |
| 1596 | } else if (!exp_ext && pnode_ext) { |
| 1597 | /* standard augment and extension instance node, will never match */ |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1601 | /* compare last node in the node ID */ |
| 1602 | i = exp->used - 1; |
| 1603 | |
| 1604 | /* get exp node ID module */ |
| 1605 | mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name, &name_len); |
| 1606 | assert(mod); |
| 1607 | |
| 1608 | if (pnode) { |
| 1609 | /* compare on the last parsed-only node */ |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1610 | if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1611 | return 0; |
| 1612 | } |
| 1613 | } else { |
| 1614 | /* using parent directly */ |
| 1615 | if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) { |
| 1616 | return 0; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | /* now compare all the compiled parents */ |
| 1621 | while (i > 1) { |
| 1622 | i -= 2; |
| 1623 | assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST); |
| 1624 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1625 | if (!parent && !pnode_ext) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1626 | /* no more parents but path continues */ |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | /* get exp node ID module */ |
| 1631 | mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name, |
| 1632 | &name_len); |
| 1633 | assert(mod); |
| 1634 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1635 | if (parent) { |
| 1636 | /* compare with the parent */ |
| 1637 | if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) { |
| 1638 | return 0; |
| 1639 | } |
| 1640 | } else { |
| 1641 | /* compare with the ext instance */ |
| 1642 | if (!lysp_schema_nodeid_match_ext(&pnode_ext, mod, name, name_len)) { |
| 1643 | return 0; |
| 1644 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | if (ctx_node && (ctx_node != parent)) { |
| 1649 | /* descendant path has not finished in the context node */ |
| 1650 | return 0; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1651 | } else if (!ctx_node && (parent || pnode_ext)) { |
| 1652 | /* some parent/extension was not matched */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | return 1; |
| 1657 | } |
| 1658 | |
| 1659 | void |
| 1660 | lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug) |
| 1661 | { |
| 1662 | if (aug) { |
| 1663 | lyxp_expr_free(ctx, aug->nodeid); |
| 1664 | |
| 1665 | free(aug); |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | void |
| 1670 | lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev) |
| 1671 | { |
| 1672 | if (dev) { |
| 1673 | lyxp_expr_free(ctx, dev->nodeid); |
| 1674 | LY_ARRAY_FREE(dev->devs); |
| 1675 | LY_ARRAY_FREE(dev->dev_pmods); |
| 1676 | |
| 1677 | free(dev); |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | void |
| 1682 | lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn) |
| 1683 | { |
| 1684 | if (rfn) { |
| 1685 | lyxp_expr_free(ctx, rfn->nodeid); |
| 1686 | LY_ARRAY_FREE(rfn->rfns); |
| 1687 | |
| 1688 | free(rfn); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | void |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1693 | lysp_dev_node_free(struct lysc_ctx *cctx, struct lysp_node *dev_pnode) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1694 | { |
| 1695 | if (!dev_pnode) { |
| 1696 | return; |
| 1697 | } |
| 1698 | |
| 1699 | switch (dev_pnode->nodetype) { |
| 1700 | case LYS_CONTAINER: |
| 1701 | ((struct lysp_node_container *)dev_pnode)->child = NULL; |
Michal Vasko | ec8f427 | 2021-10-27 09:14:03 +0200 | [diff] [blame] | 1702 | ((struct lysp_node_container *)dev_pnode)->actions = NULL; |
| 1703 | ((struct lysp_node_container *)dev_pnode)->notifs = NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1704 | break; |
| 1705 | case LYS_LIST: |
| 1706 | ((struct lysp_node_list *)dev_pnode)->child = NULL; |
Michal Vasko | ec8f427 | 2021-10-27 09:14:03 +0200 | [diff] [blame] | 1707 | ((struct lysp_node_list *)dev_pnode)->actions = NULL; |
| 1708 | ((struct lysp_node_list *)dev_pnode)->notifs = NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1709 | break; |
| 1710 | case LYS_CHOICE: |
| 1711 | ((struct lysp_node_choice *)dev_pnode)->child = NULL; |
| 1712 | break; |
| 1713 | case LYS_CASE: |
| 1714 | ((struct lysp_node_case *)dev_pnode)->child = NULL; |
| 1715 | break; |
| 1716 | case LYS_LEAF: |
| 1717 | case LYS_LEAFLIST: |
| 1718 | case LYS_ANYXML: |
| 1719 | case LYS_ANYDATA: |
| 1720 | /* no children */ |
| 1721 | break; |
| 1722 | case LYS_NOTIF: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1723 | ((struct lysp_node_notif *)dev_pnode)->child = NULL; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1724 | break; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1725 | case LYS_RPC: |
| 1726 | case LYS_ACTION: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1727 | ((struct lysp_node_action *)dev_pnode)->input.child = NULL; |
| 1728 | ((struct lysp_node_action *)dev_pnode)->output.child = NULL; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1729 | break; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1730 | case LYS_INPUT: |
| 1731 | case LYS_OUTPUT: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1732 | ((struct lysp_node_action_inout *)dev_pnode)->child = NULL; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1733 | lysp_node_free(&cctx->free_ctx, dev_pnode); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1734 | free(dev_pnode); |
| 1735 | return; |
| 1736 | default: |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1737 | LOGINT(cctx->ctx); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1738 | return; |
| 1739 | } |
| 1740 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1741 | lysp_node_free(&cctx->free_ctx, dev_pnode); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | LY_ERR |
| 1745 | lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent, |
| 1746 | struct lysp_node **dev_pnode, ly_bool *not_supported) |
| 1747 | { |
| 1748 | LY_ERR ret = LY_SUCCESS; |
| 1749 | uint32_t i; |
| 1750 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1751 | struct lysc_refine *rfn; |
| 1752 | struct lysc_deviation *dev; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1753 | |
| 1754 | *dev_pnode = NULL; |
| 1755 | *not_supported = 0; |
| 1756 | |
Michal Vasko | ee05757 | 2022-05-26 08:31:52 +0200 | [diff] [blame] | 1757 | for (i = 0; i < ctx->uses_rfns.count; ) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1758 | rfn = ctx->uses_rfns.objs[i]; |
| 1759 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1760 | if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, NULL, rfn->nodeid_ctx_node, parent, pnode, |
| 1761 | ctx->cur_mod, ctx->ext)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1762 | /* not our target node */ |
Michal Vasko | ee05757 | 2022-05-26 08:31:52 +0200 | [diff] [blame] | 1763 | ++i; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1764 | continue; |
| 1765 | } |
| 1766 | |
| 1767 | if (!*dev_pnode) { |
| 1768 | /* first refine on this node, create a copy first */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1769 | LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, ctx->pmod, pnode, 1, dev_pnode), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | /* apply all the refines by changing (the copy of) the parsed node */ |
| 1773 | LY_ARRAY_FOR(rfn->rfns, u) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1774 | LY_CHECK_GOTO(ret = lys_apply_refine(ctx, rfn->rfns[u], rfn->nodeid_pmod, *dev_pnode), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | /* refine was applied, remove it */ |
| 1778 | lysc_refine_free(ctx->ctx, rfn); |
| 1779 | ly_set_rm_index(&ctx->uses_rfns, i, NULL); |
| 1780 | |
Michal Vasko | ee05757 | 2022-05-26 08:31:52 +0200 | [diff] [blame] | 1781 | /* refines use relative paths so more may apply to a single node */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | for (i = 0; i < ctx->devs.count; ++i) { |
| 1785 | dev = ctx->devs.objs[i]; |
| 1786 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1787 | if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, NULL, parent, pnode, ctx->cur_mod, ctx->ext)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1788 | /* not our target node */ |
| 1789 | continue; |
| 1790 | } |
| 1791 | |
| 1792 | if (dev->not_supported) { |
| 1793 | /* it is not supported, no more deviations */ |
| 1794 | *not_supported = 1; |
| 1795 | goto dev_applied; |
| 1796 | } |
| 1797 | |
| 1798 | if (!*dev_pnode) { |
| 1799 | /* first deviation on this node, create a copy first */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1800 | LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, ctx->pmod, pnode, 1, dev_pnode), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | /* apply all the deviates by changing (the copy of) the parsed node */ |
| 1804 | LY_ARRAY_FOR(dev->devs, u) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1805 | LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, dev->devs[u], dev->dev_pmods[u], *dev_pnode), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | dev_applied: |
| 1809 | /* deviation was applied, remove it */ |
| 1810 | lysc_deviation_free(ctx->ctx, dev); |
| 1811 | ly_set_rm_index(&ctx->devs, i, NULL); |
| 1812 | |
| 1813 | /* all the deviations for one target node are in one structure, we are done */ |
| 1814 | break; |
| 1815 | } |
| 1816 | |
| 1817 | cleanup: |
| 1818 | if (ret) { |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1819 | lysp_dev_node_free(ctx, *dev_pnode); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1820 | *dev_pnode = NULL; |
| 1821 | *not_supported = 0; |
| 1822 | } |
| 1823 | return ret; |
| 1824 | } |
| 1825 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1826 | /** |
| 1827 | * @brief Compile augment children. |
| 1828 | * |
| 1829 | * @param[in] ctx Compile context. |
| 1830 | * @param[in] aug_when Parsed augment when to inherit. |
| 1831 | * @param[in] aug_flags Parsed augment flags. |
| 1832 | * @param[in] child First augment child to compile. |
| 1833 | * @param[in] target Target node of the augment. |
| 1834 | * @param[in] child_unres_disabled Whether the children are to be put into unres disabled set or not. |
| 1835 | * @return LY_SUCCESS on success. |
| 1836 | * @return LY_EVALID on failure. |
| 1837 | */ |
| 1838 | static LY_ERR |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1839 | lys_compile_augment_children(struct lysc_ctx *ctx, struct lysp_when *aug_when, uint16_t aug_flags, struct lysp_node *child, |
Michal Vasko | a084fa3 | 2022-05-16 11:32:58 +0200 | [diff] [blame] | 1840 | struct lysc_node *target, ly_bool child_unres_disabled) |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1841 | { |
| 1842 | LY_ERR rc = LY_SUCCESS; |
| 1843 | struct lysp_node *pnode; |
| 1844 | struct lysc_node *node; |
| 1845 | struct lysc_when *when_shared = NULL; |
| 1846 | ly_bool enabled, allow_mand = 0; |
| 1847 | struct ly_set child_set = {0}; |
| 1848 | uint32_t i, opt_prev = ctx->compile_opts; |
| 1849 | |
| 1850 | /* check for mandatory nodes |
| 1851 | * - new cases augmenting some choice can have mandatory nodes |
| 1852 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 1853 | */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1854 | if (aug_when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) { |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1855 | allow_mand = 1; |
| 1856 | } |
| 1857 | |
| 1858 | LY_LIST_FOR(child, pnode) { |
| 1859 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 1860 | if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) || |
| 1861 | ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) || |
| 1862 | ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) { |
| 1863 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
| 1864 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 1865 | lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name); |
| 1866 | rc = LY_EVALID; |
| 1867 | goto cleanup; |
| 1868 | } |
| 1869 | |
| 1870 | /* compile the children */ |
| 1871 | if (target->nodetype == LYS_CHOICE) { |
| 1872 | LY_CHECK_GOTO(rc = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup); |
| 1873 | } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 1874 | if (target->nodetype == LYS_INPUT) { |
| 1875 | ctx->compile_opts |= LYS_COMPILE_RPC_INPUT; |
| 1876 | } else { |
| 1877 | ctx->compile_opts |= LYS_COMPILE_RPC_OUTPUT; |
| 1878 | } |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1879 | LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, target, aug_flags, &child_set), cleanup); |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1880 | } else { |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 1881 | LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, target, aug_flags, &child_set), cleanup); |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | /* eval if-features again for the rest of this node processing */ |
| 1885 | LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup); |
| 1886 | if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) { |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1887 | ctx->compile_opts |= LYS_COMPILE_DISABLED; |
| 1888 | } |
| 1889 | |
| 1890 | /* since the augment node is not present in the compiled tree, we need to pass some of its |
| 1891 | * statements to all its children */ |
| 1892 | for (i = 0; i < child_set.count; ++i) { |
| 1893 | node = child_set.snodes[i]; |
| 1894 | if (!allow_mand && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) { |
| 1895 | node->flags &= ~LYS_MAND_TRUE; |
| 1896 | lys_compile_mandatory_parents(target, 0); |
| 1897 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
| 1898 | "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", |
| 1899 | node->name); |
| 1900 | rc = LY_EVALID; |
| 1901 | goto cleanup; |
| 1902 | } |
| 1903 | |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1904 | if (aug_when) { |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1905 | /* pass augment's when to all the children */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1906 | rc = lys_compile_when(ctx, aug_when, aug_flags, target, lysc_data_node(target), node, &when_shared); |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1907 | LY_CHECK_GOTO(rc, cleanup); |
| 1908 | } |
| 1909 | |
Michal Vasko | a084fa3 | 2022-05-16 11:32:58 +0200 | [diff] [blame] | 1910 | if (child_unres_disabled) { |
Michal Vasko | ad0980a | 2022-05-09 11:43:47 +0200 | [diff] [blame] | 1911 | /* child is disabled by the augment if-features */ |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1912 | ly_set_add(&ctx->unres->disabled, node, 1, NULL); |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | /* next iter */ |
| 1917 | ly_set_erase(&child_set, NULL); |
| 1918 | ctx->compile_opts = opt_prev; |
| 1919 | } |
| 1920 | |
| 1921 | cleanup: |
| 1922 | ly_set_erase(&child_set, NULL); |
| 1923 | ctx->compile_opts = opt_prev; |
| 1924 | return rc; |
| 1925 | } |
| 1926 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1927 | /** |
| 1928 | * @brief Compile the parsed augment connecting it into its target. |
| 1929 | * |
| 1930 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 1931 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 1932 | * are already implemented and compiled. |
| 1933 | * |
| 1934 | * @param[in] ctx Compile context. |
| 1935 | * @param[in] aug_p Parsed augment to compile. |
| 1936 | * @param[in] target Target node of the augment. |
| 1937 | * @return LY_SUCCESS on success. |
| 1938 | * @return LY_EVALID on failure. |
| 1939 | */ |
| 1940 | static LY_ERR |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1941 | lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1942 | { |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1943 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | a084fa3 | 2022-05-16 11:32:58 +0200 | [diff] [blame] | 1944 | ly_bool enabled, child_unres_disabled = 0; |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1945 | uint32_t opt_prev = ctx->compile_opts; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1946 | |
Michal Vasko | 95f736c | 2022-06-08 12:03:31 +0200 | [diff] [blame] | 1947 | assert(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)); |
| 1948 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1949 | /* nodetype checks */ |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1950 | if (aug_p->actions && !lysc_node_actions_p(target)) { |
| 1951 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
| 1952 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 1953 | lys_nodetype2str(target->nodetype), aug_p->actions->name); |
| 1954 | rc = LY_EVALID; |
| 1955 | goto cleanup; |
| 1956 | } |
| 1957 | if (aug_p->notifs && !lysc_node_notifs_p(target)) { |
| 1958 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
| 1959 | "Invalid augment of %s node which is not allowed to contain notification node \"%s\".", |
| 1960 | lys_nodetype2str(target->nodetype), aug_p->notifs->name); |
| 1961 | rc = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1962 | goto cleanup; |
| 1963 | } |
| 1964 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1965 | /* augment if-features */ |
| 1966 | LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, aug_p->iffeatures, &enabled), cleanup); |
| 1967 | if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) { |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1968 | ctx->compile_opts |= LYS_COMPILE_DISABLED; |
Michal Vasko | a084fa3 | 2022-05-16 11:32:58 +0200 | [diff] [blame] | 1969 | child_unres_disabled = 1; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1970 | } |
| 1971 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1972 | /* augment children */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1973 | rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, aug_p->child, target, child_unres_disabled); |
| 1974 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1975 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1976 | /* augment actions */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1977 | rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, (struct lysp_node *)aug_p->actions, target, |
| 1978 | child_unres_disabled); |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1979 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1980 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1981 | /* augment notifications */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1982 | rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, (struct lysp_node *)aug_p->notifs, target, |
| 1983 | child_unres_disabled); |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1984 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1985 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1986 | /* compile extensions into the target */ |
| 1987 | COMPILE_EXTS_GOTO(ctx, aug_p->exts, target->exts, target, rc, cleanup); |
| 1988 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1989 | cleanup: |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 1990 | ctx->compile_opts = opt_prev; |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1991 | return rc; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | LY_ERR |
| 1995 | lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node) |
| 1996 | { |
| 1997 | LY_ERR ret = LY_SUCCESS; |
| 1998 | struct lys_module *orig_mod = ctx->cur_mod; |
| 1999 | struct lysp_module *orig_pmod = ctx->pmod; |
| 2000 | uint32_t i; |
| 2001 | char orig_path[LYSC_CTX_BUFSIZE]; |
| 2002 | struct lysc_augment *aug; |
| 2003 | |
| 2004 | /* uses augments */ |
| 2005 | for (i = 0; i < ctx->uses_augs.count; ) { |
| 2006 | aug = ctx->uses_augs.objs[i]; |
| 2007 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2008 | if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->ext, aug->nodeid_ctx_node, node, NULL, NULL, |
| 2009 | ctx->ext)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2010 | /* not our target node */ |
| 2011 | ++i; |
| 2012 | continue; |
| 2013 | } |
| 2014 | |
Michal Vasko | b8df576 | 2021-01-12 15:15:53 +0100 | [diff] [blame] | 2015 | /* use the path and modules from the augment */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2016 | lysc_update_path(ctx, NULL, "{augment}"); |
| 2017 | lysc_update_path(ctx, NULL, aug->aug_p->nodeid); |
Michal Vasko | 28fe5f6 | 2021-03-03 16:37:39 +0100 | [diff] [blame] | 2018 | ctx->pmod = (struct lysp_module *)aug->aug_pmod; |
Michal Vasko | b8df576 | 2021-01-12 15:15:53 +0100 | [diff] [blame] | 2019 | |
| 2020 | /* apply augment, restore the path */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2021 | ret = lys_compile_augment(ctx, aug->aug_p, node); |
| 2022 | lysc_update_path(ctx, NULL, NULL); |
| 2023 | lysc_update_path(ctx, NULL, NULL); |
| 2024 | LY_CHECK_GOTO(ret, cleanup); |
| 2025 | |
Michal Vasko | c75f204 | 2021-06-08 14:57:03 +0200 | [diff] [blame] | 2026 | /* augment was applied, remove it (index and the whole set may have changed because other augments |
| 2027 | * could have been applied) */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2028 | ly_set_rm(&ctx->uses_augs, aug, NULL); |
| 2029 | lysc_augment_free(ctx->ctx, aug); |
Michal Vasko | c75f204 | 2021-06-08 14:57:03 +0200 | [diff] [blame] | 2030 | i = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | /* top-level augments */ |
| 2034 | for (i = 0; i < ctx->augs.count; ) { |
| 2035 | aug = ctx->augs.objs[i]; |
| 2036 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2037 | if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, aug->ext, NULL, node, NULL, NULL, ctx->ext)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2038 | /* not our target node */ |
| 2039 | ++i; |
| 2040 | continue; |
| 2041 | } |
| 2042 | |
Michal Vasko | b8df576 | 2021-01-12 15:15:53 +0100 | [diff] [blame] | 2043 | /* use the path and modules from the augment */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2044 | strcpy(orig_path, ctx->path); |
| 2045 | ctx->path_len = 1; |
Michal Vasko | 28fe5f6 | 2021-03-03 16:37:39 +0100 | [diff] [blame] | 2046 | ctx->cur_mod = aug->aug_pmod->mod; |
| 2047 | ctx->pmod = (struct lysp_module *)aug->aug_pmod; |
Michal Vasko | 1ade6f1 | 2021-04-19 11:32:45 +0200 | [diff] [blame] | 2048 | lysc_update_path(ctx, NULL, "{augment}"); |
| 2049 | lysc_update_path(ctx, NULL, aug->aug_p->nodeid); |
Michal Vasko | b8df576 | 2021-01-12 15:15:53 +0100 | [diff] [blame] | 2050 | |
| 2051 | /* apply augment, restore the path */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2052 | ret = lys_compile_augment(ctx, aug->aug_p, node); |
| 2053 | strcpy(ctx->path, orig_path); |
| 2054 | ctx->path_len = strlen(ctx->path); |
| 2055 | LY_CHECK_GOTO(ret, cleanup); |
| 2056 | |
| 2057 | /* augment was applied, remove it */ |
| 2058 | ly_set_rm(&ctx->augs, aug, NULL); |
| 2059 | lysc_augment_free(ctx->ctx, aug); |
Michal Vasko | c75f204 | 2021-06-08 14:57:03 +0200 | [diff] [blame] | 2060 | i = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | cleanup: |
| 2064 | ctx->cur_mod = orig_mod; |
| 2065 | ctx->pmod = orig_pmod; |
| 2066 | return ret; |
| 2067 | } |
| 2068 | |
| 2069 | /** |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2070 | * @brief Prepare an absolute-nodeid augment to be applied during data nodes compilation. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2071 | * |
| 2072 | * @param[in] ctx Compile context. |
| 2073 | * @param[in] aug_p Parsed augment to be applied. |
| 2074 | * @param[in] pmod Both current and prefix module for @p aug_p. |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2075 | * @param[in] ext Extension instance in case @p aug_p is defined in one. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2076 | * @return LY_ERR value. |
| 2077 | */ |
| 2078 | static LY_ERR |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2079 | lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, const struct lysp_module *pmod, |
| 2080 | const struct lysp_ext_instance *ext) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2081 | { |
| 2082 | LY_ERR ret = LY_SUCCESS; |
| 2083 | struct lyxp_expr *exp = NULL; |
| 2084 | struct lysc_augment *aug; |
| 2085 | const struct lys_module *mod; |
| 2086 | |
| 2087 | /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */ |
| 2088 | ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp); |
| 2089 | LY_CHECK_GOTO(ret, cleanup); |
| 2090 | |
| 2091 | mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL); |
| 2092 | LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup); |
| 2093 | if (mod != ctx->cur_mod) { |
| 2094 | /* augment for another module, ignore */ |
| 2095 | goto cleanup; |
| 2096 | } |
| 2097 | |
| 2098 | /* allocate new compiled augment and store it in the set */ |
| 2099 | aug = calloc(1, sizeof *aug); |
| 2100 | LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 2101 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup); |
| 2102 | |
| 2103 | aug->nodeid = exp; |
| 2104 | exp = NULL; |
Michal Vasko | 28fe5f6 | 2021-03-03 16:37:39 +0100 | [diff] [blame] | 2105 | aug->aug_pmod = pmod; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2106 | aug->ext = ext; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2107 | aug->aug_p = aug_p; |
| 2108 | |
| 2109 | cleanup: |
| 2110 | lyxp_expr_free(ctx->ctx, exp); |
| 2111 | return ret; |
| 2112 | } |
| 2113 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2114 | /** |
| 2115 | * @brief Prepare all top-level augments and extension instance augments to be applied during data nodes compilation. |
| 2116 | * |
| 2117 | * @param[in] ctx Compile context. |
| 2118 | * @param[in] pmod Parsed mod to use. |
| 2119 | * @return LY_ERR value. |
| 2120 | */ |
| 2121 | static LY_ERR |
| 2122 | lys_precompile_own_augments_mod(struct lysc_ctx *ctx, const struct lysp_module *pmod) |
| 2123 | { |
| 2124 | LY_ARRAY_COUNT_TYPE u, v; |
| 2125 | struct lysp_node_augment *aug_p; |
| 2126 | |
| 2127 | /* module */ |
| 2128 | LY_LIST_FOR(pmod->augments, aug_p) { |
| 2129 | LY_CHECK_RET(lys_precompile_own_augment(ctx, aug_p, pmod, NULL)); |
| 2130 | } |
| 2131 | |
| 2132 | /* parsed extension instances */ |
| 2133 | LY_ARRAY_FOR(pmod->exts, u) { |
| 2134 | aug_p = NULL; |
| 2135 | LY_ARRAY_FOR(pmod->exts[u].substmts, v) { |
| 2136 | if (pmod->exts[u].substmts[v].stmt == LY_STMT_AUGMENT) { |
| 2137 | aug_p = *(struct lysp_node_augment **)pmod->exts[u].substmts[v].storage; |
| 2138 | break; |
| 2139 | } |
| 2140 | } |
| 2141 | if (!aug_p) { |
| 2142 | continue; |
| 2143 | } |
| 2144 | |
| 2145 | LY_CHECK_RET(lys_precompile_own_augment(ctx, aug_p, pmod, &pmod->exts[u])); |
| 2146 | } |
| 2147 | |
| 2148 | return LY_SUCCESS; |
| 2149 | } |
| 2150 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2151 | LY_ERR |
| 2152 | lys_precompile_own_augments(struct lysc_ctx *ctx) |
| 2153 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2154 | LY_ARRAY_COUNT_TYPE u, v; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2155 | const struct lys_module *aug_mod; |
| 2156 | const struct lysp_module *submod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2157 | |
| 2158 | LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) { |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2159 | aug_mod = ctx->cur_mod->augmented_by[u]; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2160 | |
| 2161 | /* collect all module augments */ |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2162 | LY_CHECK_RET(lys_precompile_own_augments_mod(ctx, aug_mod->parsed)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2163 | |
| 2164 | /* collect all submodules augments */ |
| 2165 | LY_ARRAY_FOR(aug_mod->parsed->includes, v) { |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2166 | submod = (struct lysp_module *)aug_mod->parsed->includes[v].submodule; |
| 2167 | |
| 2168 | LY_CHECK_RET(lys_precompile_own_augments_mod(ctx, submod)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | return LY_SUCCESS; |
| 2173 | } |
| 2174 | |
| 2175 | /** |
| 2176 | * @brief Prepare a deviation to be applied during data nodes compilation. |
| 2177 | * |
| 2178 | * @param[in] ctx Compile context. |
| 2179 | * @param[in] dev_p Parsed deviation to be applied. |
| 2180 | * @param[in] pmod Both current and prefix module for @p dev_p. |
| 2181 | * @return LY_ERR value. |
| 2182 | */ |
| 2183 | static LY_ERR |
| 2184 | lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod) |
| 2185 | { |
| 2186 | LY_ERR ret = LY_SUCCESS; |
| 2187 | struct lysc_deviation *dev = NULL; |
| 2188 | struct lyxp_expr *exp = NULL; |
| 2189 | struct lysp_deviation **new_dev; |
| 2190 | const struct lys_module *mod; |
| 2191 | const struct lysp_module **new_dev_pmod; |
| 2192 | uint32_t i; |
| 2193 | |
| 2194 | /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */ |
| 2195 | ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp); |
| 2196 | LY_CHECK_GOTO(ret, cleanup); |
| 2197 | |
| 2198 | mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL); |
| 2199 | LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup); |
| 2200 | if (mod != ctx->cur_mod) { |
| 2201 | /* deviation for another module, ignore */ |
| 2202 | goto cleanup; |
| 2203 | } |
| 2204 | |
| 2205 | /* try to find the node in already compiled deviations */ |
| 2206 | for (i = 0; i < ctx->devs.count; ++i) { |
| 2207 | if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid, |
| 2208 | ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) { |
| 2209 | dev = ctx->devs.objs[i]; |
| 2210 | break; |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | if (!dev) { |
| 2215 | /* allocate new compiled deviation */ |
| 2216 | dev = calloc(1, sizeof *dev); |
| 2217 | LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 2218 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup); |
| 2219 | |
| 2220 | dev->nodeid = exp; |
| 2221 | exp = NULL; |
| 2222 | } |
| 2223 | |
| 2224 | /* add new parsed deviation structure */ |
| 2225 | LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup); |
| 2226 | *new_dev = dev_p; |
| 2227 | LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup); |
| 2228 | *new_dev_pmod = pmod; |
| 2229 | |
| 2230 | cleanup: |
| 2231 | lyxp_expr_free(ctx->ctx, exp); |
| 2232 | return ret; |
| 2233 | } |
| 2234 | |
| 2235 | LY_ERR |
| 2236 | lys_precompile_own_deviations(struct lysc_ctx *ctx) |
| 2237 | { |
| 2238 | LY_ARRAY_COUNT_TYPE u, v, w; |
Michal Vasko | e8220db | 2021-06-02 15:39:05 +0200 | [diff] [blame] | 2239 | struct lys_module *orig_cur_mod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2240 | const struct lys_module *dev_mod; |
| 2241 | struct lysc_deviation *dev; |
| 2242 | struct lysp_deviate *d; |
| 2243 | int not_supported; |
| 2244 | uint32_t i; |
| 2245 | |
| 2246 | LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) { |
| 2247 | dev_mod = ctx->cur_mod->deviated_by[u]; |
| 2248 | |
| 2249 | /* compile all module deviations */ |
| 2250 | LY_ARRAY_FOR(dev_mod->parsed->deviations, v) { |
| 2251 | LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed)); |
| 2252 | } |
| 2253 | |
| 2254 | /* compile all submodules deviations */ |
| 2255 | LY_ARRAY_FOR(dev_mod->parsed->includes, v) { |
| 2256 | LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) { |
| 2257 | LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w], |
| 2258 | (struct lysp_module *)dev_mod->parsed->includes[v].submodule)); |
| 2259 | } |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | /* set not-supported flags for all the deviations */ |
| 2264 | for (i = 0; i < ctx->devs.count; ++i) { |
| 2265 | dev = ctx->devs.objs[i]; |
| 2266 | not_supported = 0; |
| 2267 | |
| 2268 | LY_ARRAY_FOR(dev->devs, u) { |
| 2269 | LY_LIST_FOR(dev->devs[u]->deviates, d) { |
| 2270 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 2271 | not_supported = 1; |
| 2272 | break; |
| 2273 | } |
| 2274 | } |
| 2275 | if (not_supported) { |
| 2276 | break; |
| 2277 | } |
| 2278 | } |
| 2279 | if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) { |
Michal Vasko | e8220db | 2021-06-02 15:39:05 +0200 | [diff] [blame] | 2280 | orig_cur_mod = ctx->cur_mod; |
| 2281 | ctx->cur_mod = dev->dev_pmods[u]->mod; |
| 2282 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 2283 | lysc_update_path(ctx, NULL, dev->nodeid->expr); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2284 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2285 | "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr); |
Michal Vasko | e8220db | 2021-06-02 15:39:05 +0200 | [diff] [blame] | 2286 | lysc_update_path(ctx, NULL, NULL); |
| 2287 | lysc_update_path(ctx, NULL, NULL); |
| 2288 | ctx->cur_mod = orig_cur_mod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2289 | return LY_EVALID; |
| 2290 | } |
| 2291 | |
| 2292 | dev->not_supported = not_supported; |
| 2293 | } |
| 2294 | |
| 2295 | return LY_SUCCESS; |
| 2296 | } |
| 2297 | |
| 2298 | /** |
| 2299 | * @brief Add a module reference into an array, checks for duplicities. |
| 2300 | * |
| 2301 | * @param[in] ctx Compile context. |
| 2302 | * @param[in] mod Module reference to add. |
| 2303 | * @param[in,out] mod_array Module sized array to add to. |
| 2304 | * @return LY_ERR value. |
| 2305 | */ |
| 2306 | static LY_ERR |
| 2307 | lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array) |
| 2308 | { |
| 2309 | LY_ARRAY_COUNT_TYPE u; |
| 2310 | struct lys_module **new_mod; |
| 2311 | |
| 2312 | LY_ARRAY_FOR(*mod_array, u) { |
| 2313 | if ((*mod_array)[u] == mod) { |
| 2314 | /* already there */ |
| 2315 | return LY_EEXIST; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | /* add the new module ref */ |
| 2320 | LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM); |
| 2321 | *new_mod = mod; |
| 2322 | |
| 2323 | return LY_SUCCESS; |
| 2324 | } |
| 2325 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2326 | /** |
| 2327 | * @brief Check whether all modules in a set are implemented. |
| 2328 | * |
| 2329 | * @param[in] mod_set Module set to check. |
| 2330 | * @return Whether all modules are implemented or not. |
| 2331 | */ |
| 2332 | static ly_bool |
Michal Vasko | ffe7dfe | 2021-06-15 11:58:38 +0200 | [diff] [blame] | 2333 | lys_precompile_mod_set_is_all_implemented(const struct ly_set *mod_set) |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2334 | { |
| 2335 | uint32_t i; |
| 2336 | const struct lys_module *mod; |
| 2337 | |
| 2338 | for (i = 0; i < mod_set->count; ++i) { |
| 2339 | mod = mod_set->objs[i]; |
| 2340 | if (!mod->implemented) { |
| 2341 | return 0; |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | return 1; |
| 2346 | } |
| 2347 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2348 | /** |
| 2349 | * @brief Add references to target modules of top-level augments, deviations, and augments in extension instances |
| 2350 | * in a module and all its submodules. |
| 2351 | * |
| 2352 | * @param[in] pmod Module to process. |
| 2353 | * @param[in,out] mod_set Module set to add referenced modules into. |
| 2354 | * @return LY_SUCCESS on success. |
| 2355 | * @return LY_ERR on error. |
| 2356 | */ |
| 2357 | static LY_ERR |
| 2358 | lys_precompile_mod_augments_deviations(struct lysp_module *pmod, struct ly_set *mod_set) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2359 | { |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2360 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2361 | LY_ARRAY_COUNT_TYPE u, v; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 2362 | struct lysc_ctx ctx; |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2363 | struct lys_module *m; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2364 | struct lysp_node_augment *aug; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2365 | struct ly_set set = {0}; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2366 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2367 | LYSC_CTX_INIT_PMOD(ctx, pmod, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2368 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2369 | LY_LIST_FOR(pmod->augments, aug) { |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2370 | /* get target module */ |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2371 | lysc_update_path(&ctx, NULL, "{augment}"); |
| 2372 | lysc_update_path(&ctx, NULL, aug->nodeid); |
| 2373 | ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m); |
| 2374 | lysc_update_path(&ctx, NULL, NULL); |
| 2375 | lysc_update_path(&ctx, NULL, NULL); |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2376 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2377 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2378 | /* add this module into the target module augmented_by, if not there and implemented */ |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2379 | if ((lys_array_add_mod_ref(&ctx, pmod->mod, &m->augmented_by) != LY_EEXIST) || |
Michal Vasko | ffe7dfe | 2021-06-15 11:58:38 +0200 | [diff] [blame] | 2380 | !lys_precompile_mod_set_is_all_implemented(&set)) { |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2381 | LY_CHECK_GOTO(ret = ly_set_merge(mod_set, &set, 0, NULL), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2382 | } |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2383 | ly_set_erase(&set, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2386 | LY_ARRAY_FOR(pmod->deviations, u) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2387 | /* get target module */ |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2388 | lysc_update_path(&ctx, NULL, "{deviation}"); |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2389 | lysc_update_path(&ctx, NULL, pmod->deviations[u].nodeid); |
| 2390 | ret = lys_nodeid_mod_check(&ctx, pmod->deviations[u].nodeid, 1, &set, NULL, &m); |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2391 | lysc_update_path(&ctx, NULL, NULL); |
| 2392 | lysc_update_path(&ctx, NULL, NULL); |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2393 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2394 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2395 | /* add this module into the target module deviated_by, if not there and implemented */ |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2396 | if ((lys_array_add_mod_ref(&ctx, pmod->mod, &m->deviated_by) != LY_EEXIST) || |
Michal Vasko | ffe7dfe | 2021-06-15 11:58:38 +0200 | [diff] [blame] | 2397 | !lys_precompile_mod_set_is_all_implemented(&set)) { |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2398 | LY_CHECK_GOTO(ret = ly_set_merge(mod_set, &set, 0, NULL), cleanup); |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2399 | } |
| 2400 | ly_set_erase(&set, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2401 | } |
| 2402 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2403 | LY_ARRAY_FOR(pmod->exts, u) { |
| 2404 | aug = NULL; |
| 2405 | LY_ARRAY_FOR(pmod->exts[u].substmts, v) { |
| 2406 | if (pmod->exts[u].substmts[v].stmt == LY_STMT_AUGMENT) { |
| 2407 | aug = *(struct lysp_node_augment **)pmod->exts[u].substmts[v].storage; |
| 2408 | break; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2409 | } |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2410 | } |
| 2411 | if (!aug) { |
| 2412 | continue; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2413 | } |
| 2414 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2415 | /* get target module */ |
| 2416 | lysc_update_path(&ctx, NULL, "{ext-augment}"); |
| 2417 | lysc_update_path(&ctx, NULL, aug->nodeid); |
| 2418 | ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m); |
| 2419 | lysc_update_path(&ctx, NULL, NULL); |
| 2420 | lysc_update_path(&ctx, NULL, NULL); |
| 2421 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2422 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2423 | /* add this module into the target module augmented_by, if not there and implemented */ |
| 2424 | if ((lys_array_add_mod_ref(&ctx, pmod->mod, &m->augmented_by) != LY_EEXIST) || |
| 2425 | !lys_precompile_mod_set_is_all_implemented(&set)) { |
| 2426 | LY_CHECK_GOTO(ret = ly_set_merge(mod_set, &set, 0, NULL), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2427 | } |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 2428 | ly_set_erase(&set, NULL); |
| 2429 | } |
| 2430 | |
| 2431 | cleanup: |
| 2432 | ly_set_erase(&set, NULL); |
| 2433 | return ret; |
| 2434 | } |
| 2435 | |
| 2436 | LY_ERR |
| 2437 | lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres) |
| 2438 | { |
| 2439 | LY_ERR ret = LY_SUCCESS, r; |
| 2440 | LY_ARRAY_COUNT_TYPE u; |
| 2441 | struct lys_module *m; |
| 2442 | struct lysp_module *submod; |
| 2443 | const char **imp_f, *all_f[] = {"*", NULL}; |
| 2444 | uint32_t i; |
| 2445 | struct ly_set mod_set = {0}; |
| 2446 | |
| 2447 | /* module */ |
| 2448 | LY_CHECK_GOTO(ret = lys_precompile_mod_augments_deviations(mod->parsed, &mod_set), cleanup); |
| 2449 | |
| 2450 | /* submodules */ |
| 2451 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 2452 | submod = (struct lysp_module *)mod->parsed->includes[u].submodule; |
| 2453 | LY_CHECK_GOTO(ret = lys_precompile_mod_augments_deviations(submod, &mod_set), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2454 | } |
| 2455 | |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2456 | for (i = 0; i < mod_set.count; ++i) { |
| 2457 | m = mod_set.objs[i]; |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2458 | |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2459 | if (m == mod) { |
| 2460 | /* will be applied normally later */ |
| 2461 | continue; |
| 2462 | } |
| 2463 | |
| 2464 | /* we do not actually need the target modules compiled with out amends, they just need to be implemented |
| 2465 | * not compiled yet and marked for compilation */ |
| 2466 | |
| 2467 | if (!m->implemented) { |
| 2468 | /* implement the target module */ |
Michal Vasko | c56d637 | 2021-10-19 12:29:00 +0200 | [diff] [blame] | 2469 | imp_f = (mod->ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL; |
| 2470 | r = lys_implement(m, imp_f, unres); |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2471 | if (r == LY_ERECOMPILE) { |
| 2472 | /* implement all the modules right away to save possible later recompilation */ |
| 2473 | ret = r; |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2474 | continue; |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2475 | } else if (r) { |
| 2476 | /* error */ |
| 2477 | ret = r; |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2478 | goto cleanup; |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2479 | } |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2480 | } else if (m->compiled) { |
| 2481 | /* target module was already compiled without our amends (augment/deviation), we need to recompile it */ |
| 2482 | m->to_compile = 1; |
| 2483 | ret = LY_ERECOMPILE; |
| 2484 | continue; |
| 2485 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2486 | } |
| 2487 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2488 | cleanup: |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2489 | ly_set_erase(&mod_set, NULL); |
| 2490 | return ret; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | void |
| 2494 | lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod) |
| 2495 | { |
| 2496 | uint32_t i; |
| 2497 | LY_ARRAY_COUNT_TYPE u, count; |
| 2498 | struct lys_module *m; |
| 2499 | |
| 2500 | for (i = 0; i < ctx->list.count; ++i) { |
| 2501 | m = ctx->list.objs[i]; |
| 2502 | |
| 2503 | if (m->augmented_by) { |
| 2504 | count = LY_ARRAY_COUNT(m->augmented_by); |
| 2505 | for (u = 0; u < count; ++u) { |
| 2506 | if (m->augmented_by[u] == mod) { |
| 2507 | /* keep the order */ |
| 2508 | if (u < count - 1) { |
Michal Vasko | 69bd24a | 2021-06-29 08:12:06 +0200 | [diff] [blame] | 2509 | memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u - 1) * sizeof *m->augmented_by); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2510 | } |
| 2511 | LY_ARRAY_DECREMENT(m->augmented_by); |
| 2512 | break; |
| 2513 | } |
| 2514 | } |
| 2515 | if (!LY_ARRAY_COUNT(m->augmented_by)) { |
| 2516 | LY_ARRAY_FREE(m->augmented_by); |
| 2517 | m->augmented_by = NULL; |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | if (m->deviated_by) { |
| 2522 | count = LY_ARRAY_COUNT(m->deviated_by); |
| 2523 | for (u = 0; u < count; ++u) { |
| 2524 | if (m->deviated_by[u] == mod) { |
| 2525 | /* keep the order */ |
| 2526 | if (u < count - 1) { |
Michal Vasko | 69bd24a | 2021-06-29 08:12:06 +0200 | [diff] [blame] | 2527 | memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u - 1) * sizeof *m->deviated_by); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2528 | } |
| 2529 | LY_ARRAY_DECREMENT(m->deviated_by); |
| 2530 | break; |
| 2531 | } |
| 2532 | } |
| 2533 | if (!LY_ARRAY_COUNT(m->deviated_by)) { |
| 2534 | LY_ARRAY_FREE(m->deviated_by); |
| 2535 | m->deviated_by = NULL; |
| 2536 | } |
| 2537 | } |
| 2538 | } |
| 2539 | } |