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 | /** |
| 1542 | * @brief Check whether a node matches specific schema nodeid. |
| 1543 | * |
| 1544 | * @param[in] exp Parsed nodeid to match. |
| 1545 | * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix. |
| 1546 | * @param[in] ctx_node Initial context node that should match, only for descendant paths. |
| 1547 | * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched. |
| 1548 | * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled. |
| 1549 | * @param[in] pnode_mod Compiled @p pnode to-be module. |
| 1550 | * @return Whether it is a match or not. |
| 1551 | */ |
| 1552 | static ly_bool |
| 1553 | lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node, |
| 1554 | const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod) |
| 1555 | { |
| 1556 | uint32_t i; |
| 1557 | const struct lys_module *mod; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 1558 | const char *name = NULL; |
| 1559 | size_t name_len = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1560 | |
| 1561 | /* compare last node in the node ID */ |
| 1562 | i = exp->used - 1; |
| 1563 | |
| 1564 | /* get exp node ID module */ |
| 1565 | 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); |
| 1566 | assert(mod); |
| 1567 | |
| 1568 | if (pnode) { |
| 1569 | /* compare on the last parsed-only node */ |
Radek Krejci | 2d5f6df | 2021-01-28 14:00:13 +0100 | [diff] [blame] | 1570 | if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1571 | return 0; |
| 1572 | } |
| 1573 | } else { |
| 1574 | /* using parent directly */ |
| 1575 | if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) { |
| 1576 | return 0; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | /* now compare all the compiled parents */ |
| 1581 | while (i > 1) { |
| 1582 | i -= 2; |
| 1583 | assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST); |
| 1584 | |
| 1585 | if (!parent) { |
| 1586 | /* no more parents but path continues */ |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
| 1590 | /* get exp node ID module */ |
| 1591 | mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name, |
| 1592 | &name_len); |
| 1593 | assert(mod); |
| 1594 | |
| 1595 | /* compare with the parent */ |
| 1596 | if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) { |
| 1597 | return 0; |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | if (ctx_node && (ctx_node != parent)) { |
| 1602 | /* descendant path has not finished in the context node */ |
| 1603 | return 0; |
| 1604 | } else if (!ctx_node && parent) { |
| 1605 | /* some parent was not matched */ |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
| 1609 | return 1; |
| 1610 | } |
| 1611 | |
| 1612 | void |
| 1613 | lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug) |
| 1614 | { |
| 1615 | if (aug) { |
| 1616 | lyxp_expr_free(ctx, aug->nodeid); |
| 1617 | |
| 1618 | free(aug); |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | void |
| 1623 | lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev) |
| 1624 | { |
| 1625 | if (dev) { |
| 1626 | lyxp_expr_free(ctx, dev->nodeid); |
| 1627 | LY_ARRAY_FREE(dev->devs); |
| 1628 | LY_ARRAY_FREE(dev->dev_pmods); |
| 1629 | |
| 1630 | free(dev); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | void |
| 1635 | lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn) |
| 1636 | { |
| 1637 | if (rfn) { |
| 1638 | lyxp_expr_free(ctx, rfn->nodeid); |
| 1639 | LY_ARRAY_FREE(rfn->rfns); |
| 1640 | |
| 1641 | free(rfn); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | void |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1646 | 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] | 1647 | { |
| 1648 | if (!dev_pnode) { |
| 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | switch (dev_pnode->nodetype) { |
| 1653 | case LYS_CONTAINER: |
| 1654 | ((struct lysp_node_container *)dev_pnode)->child = NULL; |
Michal Vasko | ec8f427 | 2021-10-27 09:14:03 +0200 | [diff] [blame] | 1655 | ((struct lysp_node_container *)dev_pnode)->actions = NULL; |
| 1656 | ((struct lysp_node_container *)dev_pnode)->notifs = NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1657 | break; |
| 1658 | case LYS_LIST: |
| 1659 | ((struct lysp_node_list *)dev_pnode)->child = NULL; |
Michal Vasko | ec8f427 | 2021-10-27 09:14:03 +0200 | [diff] [blame] | 1660 | ((struct lysp_node_list *)dev_pnode)->actions = NULL; |
| 1661 | ((struct lysp_node_list *)dev_pnode)->notifs = NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1662 | break; |
| 1663 | case LYS_CHOICE: |
| 1664 | ((struct lysp_node_choice *)dev_pnode)->child = NULL; |
| 1665 | break; |
| 1666 | case LYS_CASE: |
| 1667 | ((struct lysp_node_case *)dev_pnode)->child = NULL; |
| 1668 | break; |
| 1669 | case LYS_LEAF: |
| 1670 | case LYS_LEAFLIST: |
| 1671 | case LYS_ANYXML: |
| 1672 | case LYS_ANYDATA: |
| 1673 | /* no children */ |
| 1674 | break; |
| 1675 | case LYS_NOTIF: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1676 | ((struct lysp_node_notif *)dev_pnode)->child = NULL; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1677 | break; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1678 | case LYS_RPC: |
| 1679 | case LYS_ACTION: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1680 | ((struct lysp_node_action *)dev_pnode)->input.child = NULL; |
| 1681 | ((struct lysp_node_action *)dev_pnode)->output.child = NULL; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1682 | break; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1683 | case LYS_INPUT: |
| 1684 | case LYS_OUTPUT: |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1685 | ((struct lysp_node_action_inout *)dev_pnode)->child = NULL; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1686 | lysp_node_free(&cctx->free_ctx, dev_pnode); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1687 | free(dev_pnode); |
| 1688 | return; |
| 1689 | default: |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1690 | LOGINT(cctx->ctx); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1691 | return; |
| 1692 | } |
| 1693 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1694 | lysp_node_free(&cctx->free_ctx, dev_pnode); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | LY_ERR |
| 1698 | lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent, |
| 1699 | struct lysp_node **dev_pnode, ly_bool *not_supported) |
| 1700 | { |
| 1701 | LY_ERR ret = LY_SUCCESS; |
| 1702 | uint32_t i; |
| 1703 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1704 | struct lysc_refine *rfn; |
| 1705 | struct lysc_deviation *dev; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1706 | |
| 1707 | *dev_pnode = NULL; |
| 1708 | *not_supported = 0; |
| 1709 | |
Michal Vasko | ee05757 | 2022-05-26 08:31:52 +0200 | [diff] [blame] | 1710 | for (i = 0; i < ctx->uses_rfns.count; ) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1711 | rfn = ctx->uses_rfns.objs[i]; |
| 1712 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame^] | 1713 | if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, ctx->cur_mod)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1714 | /* not our target node */ |
Michal Vasko | ee05757 | 2022-05-26 08:31:52 +0200 | [diff] [blame] | 1715 | ++i; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1716 | continue; |
| 1717 | } |
| 1718 | |
| 1719 | if (!*dev_pnode) { |
| 1720 | /* first refine on this node, create a copy first */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame^] | 1721 | 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] | 1722 | } |
| 1723 | |
| 1724 | /* apply all the refines by changing (the copy of) the parsed node */ |
| 1725 | LY_ARRAY_FOR(rfn->rfns, u) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame^] | 1726 | 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] | 1727 | } |
| 1728 | |
| 1729 | /* refine was applied, remove it */ |
| 1730 | lysc_refine_free(ctx->ctx, rfn); |
| 1731 | ly_set_rm_index(&ctx->uses_rfns, i, NULL); |
| 1732 | |
Michal Vasko | ee05757 | 2022-05-26 08:31:52 +0200 | [diff] [blame] | 1733 | /* refines use relative paths so more may apply to a single node */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | for (i = 0; i < ctx->devs.count; ++i) { |
| 1737 | dev = ctx->devs.objs[i]; |
| 1738 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame^] | 1739 | if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, ctx->cur_mod)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1740 | /* not our target node */ |
| 1741 | continue; |
| 1742 | } |
| 1743 | |
| 1744 | if (dev->not_supported) { |
| 1745 | /* it is not supported, no more deviations */ |
| 1746 | *not_supported = 1; |
| 1747 | goto dev_applied; |
| 1748 | } |
| 1749 | |
| 1750 | if (!*dev_pnode) { |
| 1751 | /* first deviation on this node, create a copy first */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame^] | 1752 | 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] | 1753 | } |
| 1754 | |
| 1755 | /* apply all the deviates by changing (the copy of) the parsed node */ |
| 1756 | LY_ARRAY_FOR(dev->devs, u) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame^] | 1757 | 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] | 1758 | } |
| 1759 | |
| 1760 | dev_applied: |
| 1761 | /* deviation was applied, remove it */ |
| 1762 | lysc_deviation_free(ctx->ctx, dev); |
| 1763 | ly_set_rm_index(&ctx->devs, i, NULL); |
| 1764 | |
| 1765 | /* all the deviations for one target node are in one structure, we are done */ |
| 1766 | break; |
| 1767 | } |
| 1768 | |
| 1769 | cleanup: |
| 1770 | if (ret) { |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1771 | lysp_dev_node_free(ctx, *dev_pnode); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1772 | *dev_pnode = NULL; |
| 1773 | *not_supported = 0; |
| 1774 | } |
| 1775 | return ret; |
| 1776 | } |
| 1777 | |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1778 | LY_ERR |
| 1779 | 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] | 1780 | struct lysc_node *target, ly_bool child_unres_disabled) |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1781 | { |
| 1782 | LY_ERR rc = LY_SUCCESS; |
| 1783 | struct lysp_node *pnode; |
| 1784 | struct lysc_node *node; |
| 1785 | struct lysc_when *when_shared = NULL; |
| 1786 | ly_bool enabled, allow_mand = 0; |
| 1787 | struct ly_set child_set = {0}; |
| 1788 | uint32_t i, opt_prev = ctx->compile_opts; |
| 1789 | |
| 1790 | /* check for mandatory nodes |
| 1791 | * - new cases augmenting some choice can have mandatory nodes |
| 1792 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 1793 | */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1794 | if (aug_when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) { |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1795 | allow_mand = 1; |
| 1796 | } |
| 1797 | |
| 1798 | LY_LIST_FOR(child, pnode) { |
| 1799 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 1800 | if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) || |
| 1801 | ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) || |
| 1802 | ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) { |
| 1803 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
| 1804 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 1805 | lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name); |
| 1806 | rc = LY_EVALID; |
| 1807 | goto cleanup; |
| 1808 | } |
| 1809 | |
| 1810 | /* compile the children */ |
| 1811 | if (target->nodetype == LYS_CHOICE) { |
| 1812 | LY_CHECK_GOTO(rc = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup); |
| 1813 | } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 1814 | if (target->nodetype == LYS_INPUT) { |
| 1815 | ctx->compile_opts |= LYS_COMPILE_RPC_INPUT; |
| 1816 | } else { |
| 1817 | ctx->compile_opts |= LYS_COMPILE_RPC_OUTPUT; |
| 1818 | } |
| 1819 | LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup); |
| 1820 | } else { |
| 1821 | LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup); |
| 1822 | } |
| 1823 | |
| 1824 | /* eval if-features again for the rest of this node processing */ |
| 1825 | LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup); |
| 1826 | 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] | 1827 | ctx->compile_opts |= LYS_COMPILE_DISABLED; |
| 1828 | } |
| 1829 | |
| 1830 | /* since the augment node is not present in the compiled tree, we need to pass some of its |
| 1831 | * statements to all its children */ |
| 1832 | for (i = 0; i < child_set.count; ++i) { |
| 1833 | node = child_set.snodes[i]; |
| 1834 | if (!allow_mand && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) { |
| 1835 | node->flags &= ~LYS_MAND_TRUE; |
| 1836 | lys_compile_mandatory_parents(target, 0); |
| 1837 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
| 1838 | "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", |
| 1839 | node->name); |
| 1840 | rc = LY_EVALID; |
| 1841 | goto cleanup; |
| 1842 | } |
| 1843 | |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1844 | if (aug_when) { |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1845 | /* pass augment's when to all the children */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1846 | 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] | 1847 | LY_CHECK_GOTO(rc, cleanup); |
| 1848 | } |
| 1849 | |
Michal Vasko | a084fa3 | 2022-05-16 11:32:58 +0200 | [diff] [blame] | 1850 | if (child_unres_disabled) { |
Michal Vasko | ad0980a | 2022-05-09 11:43:47 +0200 | [diff] [blame] | 1851 | /* child is disabled by the augment if-features */ |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1852 | ly_set_add(&ctx->unres->disabled, node, 1, NULL); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | /* next iter */ |
| 1857 | ly_set_erase(&child_set, NULL); |
| 1858 | ctx->compile_opts = opt_prev; |
| 1859 | } |
| 1860 | |
| 1861 | cleanup: |
| 1862 | ly_set_erase(&child_set, NULL); |
| 1863 | ctx->compile_opts = opt_prev; |
| 1864 | return rc; |
| 1865 | } |
| 1866 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1867 | /** |
| 1868 | * @brief Compile the parsed augment connecting it into its target. |
| 1869 | * |
| 1870 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 1871 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 1872 | * are already implemented and compiled. |
| 1873 | * |
| 1874 | * @param[in] ctx Compile context. |
| 1875 | * @param[in] aug_p Parsed augment to compile. |
| 1876 | * @param[in] target Target node of the augment. |
| 1877 | * @return LY_SUCCESS on success. |
| 1878 | * @return LY_EVALID on failure. |
| 1879 | */ |
| 1880 | static LY_ERR |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1881 | 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] | 1882 | { |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1883 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | a084fa3 | 2022-05-16 11:32:58 +0200 | [diff] [blame] | 1884 | ly_bool enabled, child_unres_disabled = 0; |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1885 | uint32_t opt_prev = ctx->compile_opts; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1886 | |
Michal Vasko | 95f736c | 2022-06-08 12:03:31 +0200 | [diff] [blame] | 1887 | assert(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)); |
| 1888 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1889 | /* nodetype checks */ |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1890 | if (aug_p->actions && !lysc_node_actions_p(target)) { |
| 1891 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
| 1892 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 1893 | lys_nodetype2str(target->nodetype), aug_p->actions->name); |
| 1894 | rc = LY_EVALID; |
| 1895 | goto cleanup; |
| 1896 | } |
| 1897 | if (aug_p->notifs && !lysc_node_notifs_p(target)) { |
| 1898 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
| 1899 | "Invalid augment of %s node which is not allowed to contain notification node \"%s\".", |
| 1900 | lys_nodetype2str(target->nodetype), aug_p->notifs->name); |
| 1901 | rc = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1902 | goto cleanup; |
| 1903 | } |
| 1904 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1905 | /* augment if-features */ |
| 1906 | LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, aug_p->iffeatures, &enabled), cleanup); |
| 1907 | 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] | 1908 | ctx->compile_opts |= LYS_COMPILE_DISABLED; |
Michal Vasko | a084fa3 | 2022-05-16 11:32:58 +0200 | [diff] [blame] | 1909 | child_unres_disabled = 1; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1910 | } |
| 1911 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1912 | /* augment children */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1913 | rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, aug_p->child, target, child_unres_disabled); |
| 1914 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1915 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1916 | /* augment actions */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1917 | rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, (struct lysp_node *)aug_p->actions, target, |
| 1918 | child_unres_disabled); |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1919 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1920 | |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1921 | /* augment notifications */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 1922 | rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, (struct lysp_node *)aug_p->notifs, target, |
| 1923 | child_unres_disabled); |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1924 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1925 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame^] | 1926 | /* compile extensions into the target */ |
| 1927 | COMPILE_EXTS_GOTO(ctx, aug_p->exts, target->exts, target, rc, cleanup); |
| 1928 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1929 | cleanup: |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 1930 | ctx->compile_opts = opt_prev; |
Michal Vasko | bbb3dee | 2022-05-09 10:50:28 +0200 | [diff] [blame] | 1931 | return rc; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | LY_ERR |
| 1935 | lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node) |
| 1936 | { |
| 1937 | LY_ERR ret = LY_SUCCESS; |
| 1938 | struct lys_module *orig_mod = ctx->cur_mod; |
| 1939 | struct lysp_module *orig_pmod = ctx->pmod; |
| 1940 | uint32_t i; |
| 1941 | char orig_path[LYSC_CTX_BUFSIZE]; |
| 1942 | struct lysc_augment *aug; |
| 1943 | |
| 1944 | /* uses augments */ |
| 1945 | for (i = 0; i < ctx->uses_augs.count; ) { |
| 1946 | aug = ctx->uses_augs.objs[i]; |
| 1947 | |
Michal Vasko | 28fe5f6 | 2021-03-03 16:37:39 +0100 | [diff] [blame] | 1948 | if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1949 | /* not our target node */ |
| 1950 | ++i; |
| 1951 | continue; |
| 1952 | } |
| 1953 | |
Michal Vasko | b8df576 | 2021-01-12 15:15:53 +0100 | [diff] [blame] | 1954 | /* use the path and modules from the augment */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1955 | lysc_update_path(ctx, NULL, "{augment}"); |
| 1956 | lysc_update_path(ctx, NULL, aug->aug_p->nodeid); |
Michal Vasko | 28fe5f6 | 2021-03-03 16:37:39 +0100 | [diff] [blame] | 1957 | ctx->pmod = (struct lysp_module *)aug->aug_pmod; |
Michal Vasko | b8df576 | 2021-01-12 15:15:53 +0100 | [diff] [blame] | 1958 | |
| 1959 | /* apply augment, restore the path */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1960 | ret = lys_compile_augment(ctx, aug->aug_p, node); |
| 1961 | lysc_update_path(ctx, NULL, NULL); |
| 1962 | lysc_update_path(ctx, NULL, NULL); |
| 1963 | LY_CHECK_GOTO(ret, cleanup); |
| 1964 | |
Michal Vasko | c75f204 | 2021-06-08 14:57:03 +0200 | [diff] [blame] | 1965 | /* augment was applied, remove it (index and the whole set may have changed because other augments |
| 1966 | * could have been applied) */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1967 | ly_set_rm(&ctx->uses_augs, aug, NULL); |
| 1968 | lysc_augment_free(ctx->ctx, aug); |
Michal Vasko | c75f204 | 2021-06-08 14:57:03 +0200 | [diff] [blame] | 1969 | i = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | /* top-level augments */ |
| 1973 | for (i = 0; i < ctx->augs.count; ) { |
| 1974 | aug = ctx->augs.objs[i]; |
| 1975 | |
Michal Vasko | 28fe5f6 | 2021-03-03 16:37:39 +0100 | [diff] [blame] | 1976 | if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1977 | /* not our target node */ |
| 1978 | ++i; |
| 1979 | continue; |
| 1980 | } |
| 1981 | |
Michal Vasko | b8df576 | 2021-01-12 15:15:53 +0100 | [diff] [blame] | 1982 | /* use the path and modules from the augment */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1983 | strcpy(orig_path, ctx->path); |
| 1984 | ctx->path_len = 1; |
Michal Vasko | 28fe5f6 | 2021-03-03 16:37:39 +0100 | [diff] [blame] | 1985 | ctx->cur_mod = aug->aug_pmod->mod; |
| 1986 | ctx->pmod = (struct lysp_module *)aug->aug_pmod; |
Michal Vasko | 1ade6f1 | 2021-04-19 11:32:45 +0200 | [diff] [blame] | 1987 | lysc_update_path(ctx, NULL, "{augment}"); |
| 1988 | lysc_update_path(ctx, NULL, aug->aug_p->nodeid); |
Michal Vasko | b8df576 | 2021-01-12 15:15:53 +0100 | [diff] [blame] | 1989 | |
| 1990 | /* apply augment, restore the path */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1991 | ret = lys_compile_augment(ctx, aug->aug_p, node); |
| 1992 | strcpy(ctx->path, orig_path); |
| 1993 | ctx->path_len = strlen(ctx->path); |
| 1994 | LY_CHECK_GOTO(ret, cleanup); |
| 1995 | |
| 1996 | /* augment was applied, remove it */ |
| 1997 | ly_set_rm(&ctx->augs, aug, NULL); |
| 1998 | lysc_augment_free(ctx->ctx, aug); |
Michal Vasko | c75f204 | 2021-06-08 14:57:03 +0200 | [diff] [blame] | 1999 | i = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | cleanup: |
| 2003 | ctx->cur_mod = orig_mod; |
| 2004 | ctx->pmod = orig_pmod; |
| 2005 | return ret; |
| 2006 | } |
| 2007 | |
| 2008 | /** |
| 2009 | * @brief Prepare a top-level augment to be applied during data nodes compilation. |
| 2010 | * |
| 2011 | * @param[in] ctx Compile context. |
| 2012 | * @param[in] aug_p Parsed augment to be applied. |
| 2013 | * @param[in] pmod Both current and prefix module for @p aug_p. |
| 2014 | * @return LY_ERR value. |
| 2015 | */ |
| 2016 | static LY_ERR |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2017 | lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, const struct lysp_module *pmod) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2018 | { |
| 2019 | LY_ERR ret = LY_SUCCESS; |
| 2020 | struct lyxp_expr *exp = NULL; |
| 2021 | struct lysc_augment *aug; |
| 2022 | const struct lys_module *mod; |
| 2023 | |
| 2024 | /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */ |
| 2025 | ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp); |
| 2026 | LY_CHECK_GOTO(ret, cleanup); |
| 2027 | |
| 2028 | mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL); |
| 2029 | LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup); |
| 2030 | if (mod != ctx->cur_mod) { |
| 2031 | /* augment for another module, ignore */ |
| 2032 | goto cleanup; |
| 2033 | } |
| 2034 | |
| 2035 | /* allocate new compiled augment and store it in the set */ |
| 2036 | aug = calloc(1, sizeof *aug); |
| 2037 | LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 2038 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup); |
| 2039 | |
| 2040 | aug->nodeid = exp; |
| 2041 | exp = NULL; |
Michal Vasko | 28fe5f6 | 2021-03-03 16:37:39 +0100 | [diff] [blame] | 2042 | aug->aug_pmod = pmod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2043 | aug->aug_p = aug_p; |
| 2044 | |
| 2045 | cleanup: |
| 2046 | lyxp_expr_free(ctx->ctx, exp); |
| 2047 | return ret; |
| 2048 | } |
| 2049 | |
| 2050 | LY_ERR |
| 2051 | lys_precompile_own_augments(struct lysc_ctx *ctx) |
| 2052 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2053 | LY_ARRAY_COUNT_TYPE u, v; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2054 | |
| 2055 | LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2056 | const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u]; |
| 2057 | struct lysp_node_augment *aug; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2058 | |
| 2059 | /* collect all module augments */ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2060 | LY_LIST_FOR(aug_mod->parsed->augments, aug) { |
| 2061 | LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | /* collect all submodules augments */ |
| 2065 | LY_ARRAY_FOR(aug_mod->parsed->includes, v) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2066 | LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) { |
| 2067 | LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, (struct lysp_module *)aug_mod->parsed->includes[v].submodule)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2068 | } |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | return LY_SUCCESS; |
| 2073 | } |
| 2074 | |
| 2075 | /** |
| 2076 | * @brief Prepare a deviation to be applied during data nodes compilation. |
| 2077 | * |
| 2078 | * @param[in] ctx Compile context. |
| 2079 | * @param[in] dev_p Parsed deviation to be applied. |
| 2080 | * @param[in] pmod Both current and prefix module for @p dev_p. |
| 2081 | * @return LY_ERR value. |
| 2082 | */ |
| 2083 | static LY_ERR |
| 2084 | lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod) |
| 2085 | { |
| 2086 | LY_ERR ret = LY_SUCCESS; |
| 2087 | struct lysc_deviation *dev = NULL; |
| 2088 | struct lyxp_expr *exp = NULL; |
| 2089 | struct lysp_deviation **new_dev; |
| 2090 | const struct lys_module *mod; |
| 2091 | const struct lysp_module **new_dev_pmod; |
| 2092 | uint32_t i; |
| 2093 | |
| 2094 | /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */ |
| 2095 | ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp); |
| 2096 | LY_CHECK_GOTO(ret, cleanup); |
| 2097 | |
| 2098 | mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL); |
| 2099 | LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup); |
| 2100 | if (mod != ctx->cur_mod) { |
| 2101 | /* deviation for another module, ignore */ |
| 2102 | goto cleanup; |
| 2103 | } |
| 2104 | |
| 2105 | /* try to find the node in already compiled deviations */ |
| 2106 | for (i = 0; i < ctx->devs.count; ++i) { |
| 2107 | if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid, |
| 2108 | ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) { |
| 2109 | dev = ctx->devs.objs[i]; |
| 2110 | break; |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | if (!dev) { |
| 2115 | /* allocate new compiled deviation */ |
| 2116 | dev = calloc(1, sizeof *dev); |
| 2117 | LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 2118 | LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup); |
| 2119 | |
| 2120 | dev->nodeid = exp; |
| 2121 | exp = NULL; |
| 2122 | } |
| 2123 | |
| 2124 | /* add new parsed deviation structure */ |
| 2125 | LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup); |
| 2126 | *new_dev = dev_p; |
| 2127 | LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup); |
| 2128 | *new_dev_pmod = pmod; |
| 2129 | |
| 2130 | cleanup: |
| 2131 | lyxp_expr_free(ctx->ctx, exp); |
| 2132 | return ret; |
| 2133 | } |
| 2134 | |
| 2135 | LY_ERR |
| 2136 | lys_precompile_own_deviations(struct lysc_ctx *ctx) |
| 2137 | { |
| 2138 | LY_ARRAY_COUNT_TYPE u, v, w; |
Michal Vasko | e8220db | 2021-06-02 15:39:05 +0200 | [diff] [blame] | 2139 | struct lys_module *orig_cur_mod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2140 | const struct lys_module *dev_mod; |
| 2141 | struct lysc_deviation *dev; |
| 2142 | struct lysp_deviate *d; |
| 2143 | int not_supported; |
| 2144 | uint32_t i; |
| 2145 | |
| 2146 | LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) { |
| 2147 | dev_mod = ctx->cur_mod->deviated_by[u]; |
| 2148 | |
| 2149 | /* compile all module deviations */ |
| 2150 | LY_ARRAY_FOR(dev_mod->parsed->deviations, v) { |
| 2151 | LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed)); |
| 2152 | } |
| 2153 | |
| 2154 | /* compile all submodules deviations */ |
| 2155 | LY_ARRAY_FOR(dev_mod->parsed->includes, v) { |
| 2156 | LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) { |
| 2157 | LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w], |
| 2158 | (struct lysp_module *)dev_mod->parsed->includes[v].submodule)); |
| 2159 | } |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | /* set not-supported flags for all the deviations */ |
| 2164 | for (i = 0; i < ctx->devs.count; ++i) { |
| 2165 | dev = ctx->devs.objs[i]; |
| 2166 | not_supported = 0; |
| 2167 | |
| 2168 | LY_ARRAY_FOR(dev->devs, u) { |
| 2169 | LY_LIST_FOR(dev->devs[u]->deviates, d) { |
| 2170 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 2171 | not_supported = 1; |
| 2172 | break; |
| 2173 | } |
| 2174 | } |
| 2175 | if (not_supported) { |
| 2176 | break; |
| 2177 | } |
| 2178 | } |
| 2179 | if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) { |
Michal Vasko | e8220db | 2021-06-02 15:39:05 +0200 | [diff] [blame] | 2180 | orig_cur_mod = ctx->cur_mod; |
| 2181 | ctx->cur_mod = dev->dev_pmods[u]->mod; |
| 2182 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 2183 | lysc_update_path(ctx, NULL, dev->nodeid->expr); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2184 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2185 | "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] | 2186 | lysc_update_path(ctx, NULL, NULL); |
| 2187 | lysc_update_path(ctx, NULL, NULL); |
| 2188 | ctx->cur_mod = orig_cur_mod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2189 | return LY_EVALID; |
| 2190 | } |
| 2191 | |
| 2192 | dev->not_supported = not_supported; |
| 2193 | } |
| 2194 | |
| 2195 | return LY_SUCCESS; |
| 2196 | } |
| 2197 | |
| 2198 | /** |
| 2199 | * @brief Add a module reference into an array, checks for duplicities. |
| 2200 | * |
| 2201 | * @param[in] ctx Compile context. |
| 2202 | * @param[in] mod Module reference to add. |
| 2203 | * @param[in,out] mod_array Module sized array to add to. |
| 2204 | * @return LY_ERR value. |
| 2205 | */ |
| 2206 | static LY_ERR |
| 2207 | lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array) |
| 2208 | { |
| 2209 | LY_ARRAY_COUNT_TYPE u; |
| 2210 | struct lys_module **new_mod; |
| 2211 | |
| 2212 | LY_ARRAY_FOR(*mod_array, u) { |
| 2213 | if ((*mod_array)[u] == mod) { |
| 2214 | /* already there */ |
| 2215 | return LY_EEXIST; |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | /* add the new module ref */ |
| 2220 | LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM); |
| 2221 | *new_mod = mod; |
| 2222 | |
| 2223 | return LY_SUCCESS; |
| 2224 | } |
| 2225 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2226 | /** |
| 2227 | * @brief Check whether all modules in a set are implemented. |
| 2228 | * |
| 2229 | * @param[in] mod_set Module set to check. |
| 2230 | * @return Whether all modules are implemented or not. |
| 2231 | */ |
| 2232 | static ly_bool |
Michal Vasko | ffe7dfe | 2021-06-15 11:58:38 +0200 | [diff] [blame] | 2233 | 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] | 2234 | { |
| 2235 | uint32_t i; |
| 2236 | const struct lys_module *mod; |
| 2237 | |
| 2238 | for (i = 0; i < mod_set->count; ++i) { |
| 2239 | mod = mod_set->objs[i]; |
| 2240 | if (!mod->implemented) { |
| 2241 | return 0; |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | return 1; |
| 2246 | } |
| 2247 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2248 | LY_ERR |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2249 | lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2250 | { |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2251 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2252 | LY_ARRAY_COUNT_TYPE u, v; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 2253 | struct lysc_ctx ctx; |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2254 | struct lysp_module *mod_p; |
| 2255 | struct lys_module *m; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2256 | struct lysp_submodule *submod; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2257 | struct lysp_node_augment *aug; |
Michal Vasko | c56d637 | 2021-10-19 12:29:00 +0200 | [diff] [blame] | 2258 | const char **imp_f, *all_f[] = {"*", NULL}; |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2259 | uint32_t i; |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2260 | struct ly_set mod_set = {0}, set = {0}; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2261 | |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2262 | mod_p = mod->parsed; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 2263 | LYSC_CTX_INIT_PMOD(ctx, mod_p, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2264 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2265 | LY_LIST_FOR(mod_p->augments, aug) { |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2266 | /* get target module */ |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2267 | lysc_update_path(&ctx, NULL, "{augment}"); |
| 2268 | lysc_update_path(&ctx, NULL, aug->nodeid); |
| 2269 | ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m); |
| 2270 | lysc_update_path(&ctx, NULL, NULL); |
| 2271 | lysc_update_path(&ctx, NULL, NULL); |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2272 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2273 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2274 | /* add this module into the target module augmented_by, if not there and implemented */ |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2275 | if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) || |
Michal Vasko | ffe7dfe | 2021-06-15 11:58:38 +0200 | [diff] [blame] | 2276 | !lys_precompile_mod_set_is_all_implemented(&set)) { |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2277 | 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] | 2278 | } |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2279 | ly_set_erase(&set, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 2283 | /* get target module */ |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2284 | lysc_update_path(&ctx, NULL, "{deviation}"); |
| 2285 | lysc_update_path(&ctx, NULL, mod_p->deviations[u].nodeid); |
| 2286 | ret = lys_nodeid_mod_check(&ctx, mod_p->deviations[u].nodeid, 1, &set, NULL, &m); |
| 2287 | lysc_update_path(&ctx, NULL, NULL); |
| 2288 | lysc_update_path(&ctx, NULL, NULL); |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2289 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2290 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2291 | /* add this module into the target module deviated_by, if not there and implemented */ |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2292 | if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) || |
Michal Vasko | ffe7dfe | 2021-06-15 11:58:38 +0200 | [diff] [blame] | 2293 | !lys_precompile_mod_set_is_all_implemented(&set)) { |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2294 | LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup); |
| 2295 | } |
| 2296 | ly_set_erase(&set, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | /* the same for augments and deviations in submodules */ |
| 2300 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 2301 | submod = mod_p->includes[v].submodule; |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2302 | ctx.pmod = (struct lysp_module *)submod; |
Michal Vasko | ce3d617 | 2021-06-08 14:59:49 +0200 | [diff] [blame] | 2303 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2304 | LY_LIST_FOR(submod->augments, aug) { |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2305 | lysc_update_path(&ctx, NULL, "{augment}"); |
| 2306 | lysc_update_path(&ctx, NULL, aug->nodeid); |
| 2307 | ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m); |
| 2308 | lysc_update_path(&ctx, NULL, NULL); |
| 2309 | lysc_update_path(&ctx, NULL, NULL); |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2310 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2311 | |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2312 | if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) || |
Michal Vasko | ffe7dfe | 2021-06-15 11:58:38 +0200 | [diff] [blame] | 2313 | !lys_precompile_mod_set_is_all_implemented(&set)) { |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2314 | 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] | 2315 | } |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2316 | ly_set_erase(&set, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | LY_ARRAY_FOR(submod->deviations, u) { |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2320 | lysc_update_path(&ctx, NULL, "{deviation}"); |
| 2321 | lysc_update_path(&ctx, NULL, submod->deviations[u].nodeid); |
| 2322 | ret = lys_nodeid_mod_check(&ctx, submod->deviations[u].nodeid, 1, &set, NULL, &m); |
| 2323 | lysc_update_path(&ctx, NULL, NULL); |
| 2324 | lysc_update_path(&ctx, NULL, NULL); |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2325 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2326 | |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2327 | if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) || |
Michal Vasko | ffe7dfe | 2021-06-15 11:58:38 +0200 | [diff] [blame] | 2328 | !lys_precompile_mod_set_is_all_implemented(&set)) { |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2329 | LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup); |
| 2330 | } |
| 2331 | ly_set_erase(&set, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2335 | for (i = 0; i < mod_set.count; ++i) { |
| 2336 | m = mod_set.objs[i]; |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2337 | |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2338 | if (m == mod) { |
| 2339 | /* will be applied normally later */ |
| 2340 | continue; |
| 2341 | } |
| 2342 | |
| 2343 | /* we do not actually need the target modules compiled with out amends, they just need to be implemented |
| 2344 | * not compiled yet and marked for compilation */ |
| 2345 | |
| 2346 | if (!m->implemented) { |
| 2347 | /* implement the target module */ |
Michal Vasko | c56d637 | 2021-10-19 12:29:00 +0200 | [diff] [blame] | 2348 | imp_f = (mod->ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL; |
| 2349 | r = lys_implement(m, imp_f, unres); |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2350 | if (r == LY_ERECOMPILE) { |
| 2351 | /* implement all the modules right away to save possible later recompilation */ |
| 2352 | ret = r; |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2353 | continue; |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2354 | } else if (r) { |
| 2355 | /* error */ |
| 2356 | ret = r; |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 2357 | goto cleanup; |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2358 | } |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 2359 | } else if (m->compiled) { |
| 2360 | /* target module was already compiled without our amends (augment/deviation), we need to recompile it */ |
| 2361 | m->to_compile = 1; |
| 2362 | ret = LY_ERECOMPILE; |
| 2363 | continue; |
| 2364 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2365 | } |
| 2366 | |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 2367 | cleanup: |
| 2368 | ly_set_erase(&set, NULL); |
| 2369 | ly_set_erase(&mod_set, NULL); |
| 2370 | return ret; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | void |
| 2374 | lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod) |
| 2375 | { |
| 2376 | uint32_t i; |
| 2377 | LY_ARRAY_COUNT_TYPE u, count; |
| 2378 | struct lys_module *m; |
| 2379 | |
| 2380 | for (i = 0; i < ctx->list.count; ++i) { |
| 2381 | m = ctx->list.objs[i]; |
| 2382 | |
| 2383 | if (m->augmented_by) { |
| 2384 | count = LY_ARRAY_COUNT(m->augmented_by); |
| 2385 | for (u = 0; u < count; ++u) { |
| 2386 | if (m->augmented_by[u] == mod) { |
| 2387 | /* keep the order */ |
| 2388 | if (u < count - 1) { |
Michal Vasko | 69bd24a | 2021-06-29 08:12:06 +0200 | [diff] [blame] | 2389 | 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] | 2390 | } |
| 2391 | LY_ARRAY_DECREMENT(m->augmented_by); |
| 2392 | break; |
| 2393 | } |
| 2394 | } |
| 2395 | if (!LY_ARRAY_COUNT(m->augmented_by)) { |
| 2396 | LY_ARRAY_FREE(m->augmented_by); |
| 2397 | m->augmented_by = NULL; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | if (m->deviated_by) { |
| 2402 | count = LY_ARRAY_COUNT(m->deviated_by); |
| 2403 | for (u = 0; u < count; ++u) { |
| 2404 | if (m->deviated_by[u] == mod) { |
| 2405 | /* keep the order */ |
| 2406 | if (u < count - 1) { |
Michal Vasko | 69bd24a | 2021-06-29 08:12:06 +0200 | [diff] [blame] | 2407 | 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] | 2408 | } |
| 2409 | LY_ARRAY_DECREMENT(m->deviated_by); |
| 2410 | break; |
| 2411 | } |
| 2412 | } |
| 2413 | if (!LY_ARRAY_COUNT(m->deviated_by)) { |
| 2414 | LY_ARRAY_FREE(m->deviated_by); |
| 2415 | m->deviated_by = NULL; |
| 2416 | } |
| 2417 | } |
| 2418 | } |
| 2419 | } |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 2420 | |
| 2421 | LIBYANG_API_DEF LY_ERR |
| 2422 | lys_compile_extension_instance_find_augment_target(struct lysc_ctx *ctx, const char *path, |
| 2423 | struct lysc_ext_instance **aug_ext, struct lysc_node **aug_target) |
| 2424 | { |
| 2425 | LY_ERR rc; |
| 2426 | LY_ARRAY_COUNT_TYPE u; |
| 2427 | struct lys_module *target_mod; |
| 2428 | struct lysc_ext_instance *target_ext = NULL, *prev_ext; |
| 2429 | const struct lysc_node *target; |
| 2430 | const char *id, *id2, *name; |
| 2431 | size_t name_len; |
| 2432 | uint16_t flag; |
| 2433 | |
| 2434 | LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, ctx, path, aug_target, LY_EINVAL); |
| 2435 | |
| 2436 | /* skip first slash */ |
| 2437 | id = path; |
| 2438 | if (id[0] != '/') { |
| 2439 | LOGVAL(ctx->ctx, LYVE_SYNTAX, "Invalid absolute-schema-nodeid \"%s\".", path); |
| 2440 | return LY_EVALID; |
| 2441 | } |
| 2442 | ++id; |
| 2443 | |
| 2444 | /* find the next slash */ |
| 2445 | id2 = strchr(id, '/'); |
| 2446 | if (!id2) { |
| 2447 | LOGVAL(ctx->ctx, LYVE_SYNTAX, "Invalid absolute-schema-nodeid \"%s\".", path); |
| 2448 | return LY_EVALID; |
| 2449 | } |
| 2450 | |
| 2451 | /* find the target module */ |
| 2452 | target_mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, id, id2 - id, ctx->pmod, &name, &name_len); |
| 2453 | if (!target_mod) { |
| 2454 | return LY_ENOTFOUND; |
| 2455 | } else if (!target_mod->implemented) { |
| 2456 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Augment target extension instance \"%*.s\" in a non-implemented module \"%s\".", |
| 2457 | (int)name_len, name, target_mod->name); |
| 2458 | return LY_ENOTFOUND; |
| 2459 | } |
| 2460 | |
| 2461 | /* find the extension instance */ |
| 2462 | LY_ARRAY_FOR(target_mod->compiled->exts, u) { |
| 2463 | if (!ly_strncmp(target_mod->compiled->exts[u].argument, name, name_len)) { |
| 2464 | target_ext = &target_mod->compiled->exts[u]; |
| 2465 | break; |
| 2466 | } |
| 2467 | } |
| 2468 | if (!target_ext) { |
| 2469 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Augment target extension instance \"%*.s\" not found in module \"%s\".", |
| 2470 | (int)name_len, name, target_mod->name); |
| 2471 | return LY_ENOTFOUND; |
| 2472 | } |
| 2473 | |
| 2474 | /* find the augment target with the correct compile context */ |
| 2475 | prev_ext = ctx->ext; |
| 2476 | ctx->ext = target_ext; |
| 2477 | rc = lysc_resolve_schema_nodeid(ctx, id2, strlen(id2), NULL, LY_VALUE_SCHEMA, ctx->pmod, 0, &target, &flag); |
| 2478 | ctx->ext = prev_ext; |
| 2479 | LY_CHECK_RET(rc); |
| 2480 | |
| 2481 | /* add this module into the target module augmented_by, if not there */ |
| 2482 | lys_array_add_mod_ref(ctx, ctx->cur_mod, &target_mod->augmented_by); |
| 2483 | |
| 2484 | if (aug_ext) { |
| 2485 | *aug_ext = target_ext; |
| 2486 | } |
| 2487 | *aug_target = (struct lysc_node *)target; |
| 2488 | return LY_SUCCESS; |
| 2489 | } |