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