blob: 66e6a3daac5c94bad30f1d69f73b55b830572e69 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_amend.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko19a09022021-06-15 11:54:08 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02005 * @brief Schema compilation of augments, deviations, and refines.
6 *
Michal Vasko19a09022021-06-15 11:54:08 +02007 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02008 *
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 Vasko1a7a7bd2020-10-16 14:39:15 +020021#include <stddef.h>
22#include <stdint.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020023#include <stdlib.h>
24#include <string.h>
25
26#include "common.h"
Radek Krejci77114102021-03-10 15:21:57 +010027#include "dict.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020028#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020029#include "schema_compile.h"
30#include "schema_compile_node.h"
Michal Vasko29dd11e2020-11-23 16:52:22 +010031#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020032#include "set.h"
33#include "tree.h"
Michal Vasko899c7ce2022-02-18 09:18:37 +010034#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010035#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020036#include "tree_schema.h"
37#include "tree_schema_internal.h"
38#include "xpath.h"
39
Michal Vasko0b50f6b2022-10-05 15:07:55 +020040/**
41 * @brief Get module of a single nodeid node name test.
42 *
43 * @param[in] ctx libyang context.
44 * @param[in] nametest Nametest with an optional prefix.
45 * @param[in] nametest_len Length of @p nametest.
46 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
47 * @param[out] name Optional pointer to the name test without the prefix.
48 * @param[out] name_len Length of @p name.
49 * @return Resolved module.
50 */
51static const struct lys_module *
52lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
53 const struct lysp_module *mod, const char **name, size_t *name_len)
54{
55 const struct lys_module *target_mod;
56 const char *ptr;
57
58 ptr = ly_strnchr(nametest, ':', nametest_len);
59 if (ptr) {
60 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_VALUE_SCHEMA, (void *)mod);
61 if (!target_mod) {
62 LOGVAL(ctx, LYVE_REFERENCE,
63 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
64 (int)nametest_len, nametest, (int)(ptr - nametest), nametest, LYSP_MODULE_NAME(mod));
65 return NULL;
66 }
67
68 if (name) {
69 *name = ptr + 1;
70 *name_len = nametest_len - ((ptr - nametest) + 1);
71 }
72 } else {
73 target_mod = mod->mod;
74 if (name) {
75 *name = nametest;
76 *name_len = nametest_len;
77 }
78 }
79
80 return target_mod;
81}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020082
Michal Vasko1ccbf542021-04-19 11:35:00 +020083/**
84 * @brief Check the syntax of a node-id and collect all the referenced modules.
85 *
86 * @param[in] ctx Compile context.
87 * @param[in] nodeid Node-id to check.
88 * @param[in] abs Whether @p nodeid is absolute.
89 * @param[in,out] mod_set Set to add referenced modules into.
90 * @param[out] expr Optional node-id parsed into an expression.
91 * @param[out] target_mod Optional target module of the node-id.
92 * @return LY_ERR value.
93 */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020094static LY_ERR
Michal Vasko1ccbf542021-04-19 11:35:00 +020095lys_nodeid_mod_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct ly_set *mod_set,
96 struct lyxp_expr **expr, struct lys_module **target_mod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020097{
98 LY_ERR ret = LY_SUCCESS;
99 struct lyxp_expr *e = NULL;
100 struct lys_module *tmod = NULL, *mod;
101 const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid";
102 uint32_t i;
103
104 /* parse */
105 ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e);
106 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100107 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200108 nodeid_type, nodeid);
109 ret = LY_EVALID;
110 goto cleanup;
111 }
112
113 if (abs) {
114 /* absolute schema nodeid */
115 i = 0;
116 } else {
117 /* descendant schema nodeid */
118 if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100119 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200120 nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]);
121 ret = LY_EVALID;
122 goto cleanup;
123 }
124 i = 1;
125 }
126
127 /* check all the tokens */
128 for ( ; i < e->used; i += 2) {
129 if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100130 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200131 nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]);
132 ret = LY_EVALID;
133 goto cleanup;
134 } else if (e->used == i + 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100135 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200136 "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr);
137 ret = LY_EVALID;
138 goto cleanup;
139 } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100140 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200141 nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
142 ret = LY_EVALID;
143 goto cleanup;
144 } else if (abs) {
145 mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1],
146 e->tok_len[i + 1], ctx->pmod, NULL, NULL);
147 LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup);
148
149 /* only keep the first module */
150 if (!tmod) {
151 tmod = mod;
152 }
153
Michal Vasko1ccbf542021-04-19 11:35:00 +0200154 /* store the referenced module */
155 LY_CHECK_GOTO(ret = ly_set_add(mod_set, mod, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200156 }
157 }
158
159cleanup:
160 if (ret || !expr) {
161 lyxp_expr_free(ctx->ctx, e);
162 e = NULL;
163 }
164 if (expr) {
165 *expr = ret ? NULL : e;
166 }
167 if (target_mod) {
168 *target_mod = ret ? NULL : tmod;
169 }
170 return ret;
171}
172
173/**
174 * @brief Check whether 2 schema nodeids match.
175 *
176 * @param[in] ctx libyang context.
177 * @param[in] exp1 First schema nodeid.
178 * @param[in] exp1p_mod Module of @p exp1 nodes without any prefix.
179 * @param[in] exp2 Second schema nodeid.
180 * @param[in] exp2_pmod Module of @p exp2 nodes without any prefix.
181 * @return Whether the schema nodeids match or not.
182 */
183static ly_bool
184lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lysp_module *exp1_pmod,
185 const struct lyxp_expr *exp2, const struct lysp_module *exp2_pmod)
186{
187 uint32_t i;
188 const struct lys_module *mod1, *mod2;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100189 const char *name1 = NULL, *name2 = NULL;
190 size_t name1_len = 0, name2_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200191
192 if (exp1->used != exp2->used) {
193 return 0;
194 }
195
196 for (i = 0; i < exp1->used; ++i) {
197 assert(exp1->tokens[i] == exp2->tokens[i]);
198
199 if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) {
200 /* check modules of all the nodes in the node ID */
201 mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_pmod,
202 &name1, &name1_len);
203 assert(mod1);
204 mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_pmod,
205 &name2, &name2_len);
206 assert(mod2);
207
208 /* compare modules */
209 if (mod1 != mod2) {
210 return 0;
211 }
212
213 /* compare names */
214 if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) {
215 return 0;
216 }
217 }
218 }
219
220 return 1;
221}
222
223LY_ERR
224lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node)
225{
226 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200227 struct lyxp_expr *exp = NULL;
228 struct lysc_augment *aug;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100229 struct lysp_node_augment *aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200230 struct lysc_refine *rfn;
231 struct lysp_refine **new_rfn;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100232 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200233 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +0200234 struct ly_set mod_set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200235
Radek Krejci2a9fc652021-01-22 17:44:34 +0100236 LY_LIST_FOR(uses_p->augments, aug_p) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200237 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +0100238 lysc_update_path(ctx, NULL, aug_p->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200239
240 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200241 LY_CHECK_GOTO(ret = lys_nodeid_mod_check(ctx, aug_p->nodeid, 0, &mod_set, &exp, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200242
243 /* allocate new compiled augment and store it in the set */
244 aug = calloc(1, sizeof *aug);
245 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
246 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, 1, NULL), cleanup);
247
248 aug->nodeid = exp;
249 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +0100250 aug->aug_pmod = ctx->pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200251 aug->nodeid_ctx_node = ctx_node;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100252 aug->aug_p = aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200253
254 lysc_update_path(ctx, NULL, NULL);
255 lysc_update_path(ctx, NULL, NULL);
256 }
257
258 LY_ARRAY_FOR(uses_p->refines, u) {
259 lysc_update_path(ctx, NULL, "{refine}");
260 lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid);
261
262 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200263 LY_CHECK_GOTO(ret = lys_nodeid_mod_check(ctx, uses_p->refines[u].nodeid, 0, &mod_set, &exp, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200264
265 /* try to find the node in already compiled refines */
266 rfn = NULL;
267 for (i = 0; i < ctx->uses_rfns.count; ++i) {
268 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->pmod, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid,
269 ctx->pmod)) {
270 rfn = ctx->uses_rfns.objs[i];
271 break;
272 }
273 }
274
275 if (!rfn) {
276 /* allocate new compiled refine */
277 rfn = calloc(1, sizeof *rfn);
278 LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
279 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, 1, NULL), cleanup);
280
281 rfn->nodeid = exp;
282 exp = NULL;
Michal Vasko7d3708f2021-02-03 10:50:24 +0100283 rfn->nodeid_pmod = ctx->cur_mod->parsed;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200284 rfn->nodeid_ctx_node = ctx_node;
Michal Vaskod8655722021-01-12 15:20:36 +0100285 rfn->uses_p = uses_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200286 } else {
287 /* just free exp */
288 lyxp_expr_free(ctx->ctx, exp);
289 exp = NULL;
290 }
291
292 /* add new parsed refine structure */
293 LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup);
294 *new_rfn = &uses_p->refines[u];
295
296 lysc_update_path(ctx, NULL, NULL);
297 lysc_update_path(ctx, NULL, NULL);
298 }
299
300cleanup:
Michal Vasko1ccbf542021-04-19 11:35:00 +0200301 if (ret) {
302 lysc_update_path(ctx, NULL, NULL);
303 lysc_update_path(ctx, NULL, NULL);
304 }
305 /* should include only this module, will fail later if not */
306 ly_set_erase(&mod_set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200307 lyxp_expr_free(ctx->ctx, exp);
308 return ret;
309}
310
311static LY_ERR
Michal Vasko899c7ce2022-02-18 09:18:37 +0100312lysp_ext_children_dup(const struct ly_ctx *ctx, struct lysp_stmt **child, const struct lysp_stmt *orig_child)
313{
Michal Vaskod84b8132022-03-15 10:45:44 +0100314 struct lysp_stmt *ch = NULL;
Michal Vasko4316c9d2022-02-21 10:00:10 +0100315
Michal Vasko9464f422022-02-21 10:18:28 +0100316 assert(!*child);
317
Michal Vasko899c7ce2022-02-18 09:18:37 +0100318 LY_LIST_FOR(orig_child, orig_child) {
319 /* new child */
320 if (!*child) {
Michal Vasko4316c9d2022-02-21 10:00:10 +0100321 *child = ch = calloc(1, sizeof *ch);
322 LY_CHECK_ERR_RET(!ch, LOGMEM(ctx), LY_EMEM);
Michal Vasko899c7ce2022-02-18 09:18:37 +0100323 } else {
Michal Vasko4316c9d2022-02-21 10:00:10 +0100324 ch->next = calloc(1, sizeof *ch);
325 LY_CHECK_ERR_RET(!ch->next, LOGMEM(ctx), LY_EMEM);
326 ch = ch->next;
Michal Vasko899c7ce2022-02-18 09:18:37 +0100327 }
328
329 /* fill */
Michal Vasko4316c9d2022-02-21 10:00:10 +0100330 DUP_STRING_RET(ctx, orig_child->stmt, ch->stmt);
331 ch->flags = orig_child->flags;
332 DUP_STRING_RET(ctx, orig_child->arg, ch->arg);
333 ch->format = orig_child->format;
334 LY_CHECK_RET(ly_dup_prefix_data(ctx, orig_child->format, orig_child->prefix_data, &(ch->prefix_data)));
335 ch->kw = orig_child->kw;
Michal Vasko899c7ce2022-02-18 09:18:37 +0100336
337 /* recursive children */
Michal Vasko4316c9d2022-02-21 10:00:10 +0100338 LY_CHECK_RET(lysp_ext_children_dup(ctx, &ch->child, orig_child->child));
Michal Vasko899c7ce2022-02-18 09:18:37 +0100339 }
340
341 return LY_SUCCESS;
342}
343
344static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200345lysp_ext_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_ext_instance *ext,
346 const struct lysp_ext_instance *orig_ext)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200347{
Michal Vasko193dacd2022-10-13 08:43:05 +0200348 LY_ERR ret = LY_SUCCESS;
349 struct ly_set pmods = {0};
350 struct lysp_ctx pctx = {.parsed_mods = &pmods};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200351
Michal Vasko193dacd2022-10-13 08:43:05 +0200352 DUP_STRING_GOTO(ctx, orig_ext->name, ext->name, ret, cleanup);
353 DUP_STRING_GOTO(ctx, orig_ext->argument, ext->argument, ret, cleanup);
354 ext->format = orig_ext->format;
355 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, orig_ext->format, orig_ext->prefix_data, &ext->prefix_data), cleanup);
356 ext->def = orig_ext->def;
Michal Vasko899c7ce2022-02-18 09:18:37 +0100357
Michal Vasko91bb76c2022-03-24 13:34:18 +0100358 ext->parent = orig_ext->parent;
359 ext->parent_stmt = orig_ext->parent_stmt;
360 ext->parent_stmt_index = orig_ext->parent_stmt_index;
361 ext->flags = orig_ext->flags;
Michal Vasko193dacd2022-10-13 08:43:05 +0200362 ext->record = orig_ext->record;
363
364 LY_CHECK_GOTO(ret = lysp_ext_children_dup(ctx, &ext->child, orig_ext->child), cleanup);
365 if (ext->record && ext->record->plugin.parse) {
366 /* parse again */
367 LY_CHECK_GOTO(ret = ly_set_add(&pmods, pmod, 1, NULL), cleanup);
368 LY_CHECK_GOTO(ret = ext->record->plugin.parse(&pctx, ext), cleanup);
369 }
370
371cleanup:
372 ly_set_erase(&pmods, NULL);
373 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200374}
375
376static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200377lysp_restr_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_restr *restr,
378 const struct lysp_restr *orig_restr)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200379{
380 LY_ERR ret = LY_SUCCESS;
381
382 if (orig_restr) {
383 DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret);
384 restr->arg.mod = orig_restr->arg.mod;
385 DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret);
386 DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret);
387 DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret);
388 DUP_STRING(ctx, orig_restr->ref, restr->ref, ret);
Michal Vasko193dacd2022-10-13 08:43:05 +0200389 DUP_ARRAY2(ctx, pmod, orig_restr->exts, restr->exts, lysp_ext_dup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200390 }
391
392 return ret;
393}
394
395static LY_ERR
396lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str)
397{
398 LY_ERR ret = LY_SUCCESS;
399
400 DUP_STRING(ctx, *orig_str, *str, ret);
401
402 return ret;
403}
404
405LY_ERR
406lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname)
407{
408 LY_ERR ret = LY_SUCCESS;
409
410 if (!orig_qname->str) {
411 return LY_SUCCESS;
412 }
413
414 DUP_STRING(ctx, orig_qname->str, qname->str, ret);
415 assert(orig_qname->mod);
416 qname->mod = orig_qname->mod;
417
418 return ret;
419}
420
421static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200422lysp_type_enum_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_type_enum *enm,
423 const struct lysp_type_enum *orig_enm)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200424{
425 LY_ERR ret = LY_SUCCESS;
426
427 DUP_STRING(ctx, orig_enm->name, enm->name, ret);
428 DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret);
429 DUP_STRING(ctx, orig_enm->ref, enm->ref, ret);
430 enm->value = orig_enm->value;
431 DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200432 DUP_ARRAY2(ctx, pmod, orig_enm->exts, enm->exts, lysp_ext_dup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200433 enm->flags = orig_enm->flags;
434
435 return ret;
436}
437
438static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200439lysp_type_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_type *type,
440 const struct lysp_type *orig_type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200441{
442 LY_ERR ret = LY_SUCCESS;
443
Michal Vaskoea868242021-06-21 09:28:32 +0200444 /* array macros read previous data so we must zero it */
445 memset(type, 0, sizeof *type);
446
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200447 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
448
449 if (orig_type->range) {
450 type->range = calloc(1, sizeof *type->range);
451 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
Michal Vasko193dacd2022-10-13 08:43:05 +0200452 LY_CHECK_RET(lysp_restr_dup(ctx, pmod, type->range, orig_type->range));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200453 }
454
455 if (orig_type->length) {
456 type->length = calloc(1, sizeof *type->length);
457 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
Michal Vasko193dacd2022-10-13 08:43:05 +0200458 LY_CHECK_RET(lysp_restr_dup(ctx, pmod, type->length, orig_type->length));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200459 }
460
Michal Vasko193dacd2022-10-13 08:43:05 +0200461 DUP_ARRAY2(ctx, pmod, orig_type->patterns, type->patterns, lysp_restr_dup);
462 DUP_ARRAY2(ctx, pmod, orig_type->enums, type->enums, lysp_type_enum_dup);
463 DUP_ARRAY2(ctx, pmod, orig_type->bits, type->bits, lysp_type_enum_dup);
Michal Vaskoe33134a2022-07-29 14:54:40 +0200464 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, 0, 0, &type->path), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200465 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200466 DUP_ARRAY2(ctx, pmod, orig_type->types, type->types, lysp_type_dup);
467 DUP_ARRAY2(ctx, pmod, orig_type->exts, type->exts, lysp_ext_dup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200468
469 type->pmod = orig_type->pmod;
470 type->compiled = orig_type->compiled;
471
472 type->fraction_digits = orig_type->fraction_digits;
473 type->require_instance = orig_type->require_instance;
474 type->flags = orig_type->flags;
475
476done:
477 return ret;
478}
479
480static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200481lysp_when_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_when *when,
482 const struct lysp_when *orig_when)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200483{
484 LY_ERR ret = LY_SUCCESS;
485
486 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
487 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
488 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
Michal Vasko193dacd2022-10-13 08:43:05 +0200489 DUP_ARRAY2(ctx, pmod, orig_when->exts, when->exts, lysp_ext_dup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200490
491 return ret;
492}
493
494static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200495lysp_node_common_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_node *node,
496 const struct lysp_node *orig)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200497{
498 LY_ERR ret = LY_SUCCESS;
499
500 node->parent = NULL;
501 node->nodetype = orig->nodetype;
502 node->flags = orig->flags;
503 node->next = NULL;
504 DUP_STRING(ctx, orig->name, node->name, ret);
505 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
506 DUP_STRING(ctx, orig->ref, node->ref, ret);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200507 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200508 DUP_ARRAY2(ctx, pmod, orig->exts, node->exts, lysp_ext_dup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200509
510 return ret;
511}
512
Michal Vasko193dacd2022-10-13 08:43:05 +0200513#define DUP_PWHEN(CTX, PMOD, ORIG, NEW) \
Radek Krejci9a3823e2021-01-27 20:26:46 +0100514 if (ORIG) { \
515 NEW = calloc(1, sizeof *NEW); \
516 LY_CHECK_ERR_RET(!NEW, LOGMEM(CTX), LY_EMEM); \
Michal Vasko193dacd2022-10-13 08:43:05 +0200517 LY_CHECK_RET(lysp_when_dup(CTX, PMOD, NEW, ORIG)); \
Radek Krejci9a3823e2021-01-27 20:26:46 +0100518 }
519
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200520static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200521lysp_node_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, struct lysp_node *node,
522 const struct lysp_node *orig)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200523{
524 LY_ERR ret = LY_SUCCESS;
525 struct lysp_node_container *cont;
526 const struct lysp_node_container *orig_cont;
527 struct lysp_node_leaf *leaf;
528 const struct lysp_node_leaf *orig_leaf;
529 struct lysp_node_leaflist *llist;
530 const struct lysp_node_leaflist *orig_llist;
531 struct lysp_node_list *list;
532 const struct lysp_node_list *orig_list;
533 struct lysp_node_choice *choice;
534 const struct lysp_node_choice *orig_choice;
535 struct lysp_node_case *cas;
536 const struct lysp_node_case *orig_cas;
537 struct lysp_node_anydata *any;
538 const struct lysp_node_anydata *orig_any;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100539 struct lysp_node_action *action;
540 const struct lysp_node_action *orig_action;
541 struct lysp_node_action_inout *action_inout;
542 const struct lysp_node_action_inout *orig_action_inout;
543 struct lysp_node_notif *notif;
544 const struct lysp_node_notif *orig_notif;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200545
Radek Krejci2a9fc652021-01-22 17:44:34 +0100546 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA |
547 LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200548
549 /* common part */
Michal Vasko193dacd2022-10-13 08:43:05 +0200550 LY_CHECK_RET(lysp_node_common_dup(ctx, pmod, node, orig));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200551
552 /* specific part */
553 switch (node->nodetype) {
554 case LYS_CONTAINER:
555 cont = (struct lysp_node_container *)node;
556 orig_cont = (const struct lysp_node_container *)orig;
557
Michal Vasko193dacd2022-10-13 08:43:05 +0200558 DUP_PWHEN(ctx, pmod, orig_cont->when, cont->when);
559 DUP_ARRAY2(ctx, pmod, orig_cont->musts, cont->musts, lysp_restr_dup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200560 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
561 /* we do not need the rest */
562 break;
563 case LYS_LEAF:
564 leaf = (struct lysp_node_leaf *)node;
565 orig_leaf = (const struct lysp_node_leaf *)orig;
566
Michal Vasko193dacd2022-10-13 08:43:05 +0200567 DUP_PWHEN(ctx, pmod, orig_leaf->when, leaf->when);
568 DUP_ARRAY2(ctx, pmod, orig_leaf->musts, leaf->musts, lysp_restr_dup);
569 LY_CHECK_RET(lysp_type_dup(ctx, pmod, &leaf->type, &orig_leaf->type));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200570 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
571 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
572 break;
573 case LYS_LEAFLIST:
574 llist = (struct lysp_node_leaflist *)node;
575 orig_llist = (const struct lysp_node_leaflist *)orig;
576
Michal Vasko193dacd2022-10-13 08:43:05 +0200577 DUP_PWHEN(ctx, pmod, orig_llist->when, llist->when);
578 DUP_ARRAY2(ctx, pmod, orig_llist->musts, llist->musts, lysp_restr_dup);
579 LY_CHECK_RET(lysp_type_dup(ctx, pmod, &llist->type, &orig_llist->type));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200580 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
581 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
582 llist->min = orig_llist->min;
583 llist->max = orig_llist->max;
584 break;
585 case LYS_LIST:
586 list = (struct lysp_node_list *)node;
587 orig_list = (const struct lysp_node_list *)orig;
588
Michal Vasko193dacd2022-10-13 08:43:05 +0200589 DUP_PWHEN(ctx, pmod, orig_list->when, list->when);
590 DUP_ARRAY2(ctx, pmod, orig_list->musts, list->musts, lysp_restr_dup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200591 DUP_STRING(ctx, orig_list->key, list->key, ret);
592 /* we do not need these arrays */
593 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
594 list->min = orig_list->min;
595 list->max = orig_list->max;
596 break;
597 case LYS_CHOICE:
598 choice = (struct lysp_node_choice *)node;
599 orig_choice = (const struct lysp_node_choice *)orig;
600
Michal Vasko193dacd2022-10-13 08:43:05 +0200601 DUP_PWHEN(ctx, pmod, orig_choice->when, choice->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200602 /* we do not need children */
603 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
604 break;
605 case LYS_CASE:
606 cas = (struct lysp_node_case *)node;
607 orig_cas = (const struct lysp_node_case *)orig;
608
Michal Vasko193dacd2022-10-13 08:43:05 +0200609 DUP_PWHEN(ctx, pmod, orig_cas->when, cas->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200610 /* we do not need children */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200611 break;
612 case LYS_ANYDATA:
613 case LYS_ANYXML:
614 any = (struct lysp_node_anydata *)node;
615 orig_any = (const struct lysp_node_anydata *)orig;
616
Michal Vasko193dacd2022-10-13 08:43:05 +0200617 DUP_PWHEN(ctx, pmod, orig_any->when, any->when);
618 DUP_ARRAY2(ctx, pmod, orig_any->musts, any->musts, lysp_restr_dup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200619 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100620 case LYS_RPC:
621 case LYS_ACTION:
622 action = (struct lysp_node_action *)node;
623 orig_action = (const struct lysp_node_action *)orig;
624
625 action->input.nodetype = orig_action->input.nodetype;
626 action->output.nodetype = orig_action->output.nodetype;
627 /* we do not need the rest */
628 break;
629 case LYS_INPUT:
630 case LYS_OUTPUT:
631 action_inout = (struct lysp_node_action_inout *)node;
632 orig_action_inout = (const struct lysp_node_action_inout *)orig;
633
Michal Vasko193dacd2022-10-13 08:43:05 +0200634 DUP_ARRAY2(ctx, pmod, orig_action_inout->musts, action_inout->musts, lysp_restr_dup);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100635 /* we do not need the rest */
636 break;
637 case LYS_NOTIF:
638 notif = (struct lysp_node_notif *)node;
639 orig_notif = (const struct lysp_node_notif *)orig;
640
Michal Vasko193dacd2022-10-13 08:43:05 +0200641 DUP_ARRAY2(ctx, pmod, orig_notif->musts, notif->musts, lysp_restr_dup);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100642 /* we do not need the rest */
643 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200644 default:
645 LOGINT_RET(ctx);
646 }
647
648 return ret;
649}
650
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200651/**
652 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
653 *
654 * @param[in] ctx libyang context.
Michal Vasko193dacd2022-10-13 08:43:05 +0200655 * @param[in] pmod Current parsed module.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200656 * @param[in] pnode Node to duplicate.
657 * @param[in] with_links Whether to also copy any links (child, parent pointers).
658 * @param[out] dup_p Duplicated parsed node.
659 * @return LY_ERR value.
660 */
661static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200662lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_module *pmod, const struct lysp_node *pnode,
663 ly_bool with_links, struct lysp_node **dup_p)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200664{
665 LY_ERR ret = LY_SUCCESS;
666 void *mem = NULL;
667
668 if (!pnode) {
669 *dup_p = NULL;
670 return LY_SUCCESS;
671 }
672
673 switch (pnode->nodetype) {
674 case LYS_CONTAINER:
675 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200676 break;
677 case LYS_LEAF:
678 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200679 break;
680 case LYS_LEAFLIST:
681 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200682 break;
683 case LYS_LIST:
684 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200685 break;
686 case LYS_CHOICE:
687 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200688 break;
689 case LYS_CASE:
690 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200691 break;
692 case LYS_ANYDATA:
693 case LYS_ANYXML:
694 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200695 break;
696 case LYS_INPUT:
697 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100698 mem = calloc(1, sizeof(struct lysp_node_action_inout));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200699 break;
700 case LYS_ACTION:
701 case LYS_RPC:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100702 mem = calloc(1, sizeof(struct lysp_node_action));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200703 break;
704 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100705 mem = calloc(1, sizeof(struct lysp_node_notif));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200706 break;
707 default:
708 LOGINT_RET(ctx);
709 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100710 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200711 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, pmod, mem, pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200712
713 if (with_links) {
Michal Vaskoec8f4272021-10-27 09:14:03 +0200714 /* copy also parent, child, action, and notification pointers */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200715 ((struct lysp_node *)mem)->parent = pnode->parent;
716 switch (pnode->nodetype) {
717 case LYS_CONTAINER:
718 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
Michal Vaskoec8f4272021-10-27 09:14:03 +0200719 ((struct lysp_node_container *)mem)->actions = ((struct lysp_node_container *)pnode)->actions;
720 ((struct lysp_node_container *)mem)->notifs = ((struct lysp_node_container *)pnode)->notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200721 break;
722 case LYS_LIST:
723 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
Michal Vaskoec8f4272021-10-27 09:14:03 +0200724 ((struct lysp_node_list *)mem)->actions = ((struct lysp_node_list *)pnode)->actions;
725 ((struct lysp_node_list *)mem)->notifs = ((struct lysp_node_list *)pnode)->notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200726 break;
727 case LYS_CHOICE:
728 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
729 break;
730 case LYS_CASE:
731 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
732 break;
733 default:
734 break;
735 }
736 }
737
738cleanup:
739 if (ret) {
740 free(mem);
741 } else {
742 *dup_p = mem;
743 }
744 return ret;
745}
746
747#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100748 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s of %s node - it is not possible to %s \"%s\" property.", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200749 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
750 ret = LY_EVALID; \
751 goto cleanup;
752
753#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
754 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100755 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid %s of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200756 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
757 ret = LY_EVALID; \
758 goto cleanup; \
759 }
760
761/**
762 * @brief Apply refine.
763 *
764 * @param[in] ctx Compile context.
765 * @param[in] rfn Refine to apply.
Michal Vasko193dacd2022-10-13 08:43:05 +0200766 * @param[in] rfn_pmod Local module fo the refine.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200767 * @param[in,out] target Refine target.
768 * @return LY_ERR value.
769 */
770static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200771lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, const struct lysp_module *rfn_pmod, struct lysp_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200772{
773 LY_ERR ret = LY_SUCCESS;
Michal Vasko193dacd2022-10-13 08:43:05 +0200774 struct lys_module *orig_mod = ctx->cur_mod;
775 struct lysp_module *orig_pmod = ctx->pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200776 LY_ARRAY_COUNT_TYPE u;
777 struct lysp_qname *qname;
778 struct lysp_restr **musts, *must;
779 uint32_t *num;
780
Michal Vasko193dacd2022-10-13 08:43:05 +0200781 /* use module from the refine */
782 ctx->cur_mod = rfn_pmod->mod;
783 ctx->pmod = (struct lysp_module *)rfn_pmod;
784
785 /* keep the current path and add to it */
786 lysc_update_path(ctx, NULL, "{refine}");
787 lysc_update_path(ctx, NULL, rfn->nodeid);
788
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200789 /* default value */
790 if (rfn->dflts) {
791 switch (target->nodetype) {
792 case LYS_LEAF:
793 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
794
Michal Vaskoe180ed02021-02-05 16:31:20 +0100795 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200796 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
797 break;
798 case LYS_LEAFLIST:
799 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100800 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200801 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
802 ret = LY_EVALID;
803 goto cleanup;
804 }
805
806 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
807 ((struct lysp_node_leaflist *)target)->dflts = NULL;
808 LY_ARRAY_FOR(rfn->dflts, u) {
809 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
810 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
811 }
812 break;
813 case LYS_CHOICE:
814 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
815
Michal Vaskoe180ed02021-02-05 16:31:20 +0100816 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200817 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
818 break;
819 default:
820 AMEND_WRONG_NODETYPE("refine", "replace", "default");
821 }
822 }
823
824 /* description */
825 if (rfn->dsc) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100826 lydict_remove(ctx->ctx, target->dsc);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200827 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
828 }
829
830 /* reference */
831 if (rfn->ref) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100832 lydict_remove(ctx->ctx, target->ref);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200833 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
834 }
835
836 /* config */
837 if (rfn->flags & LYS_CONFIG_MASK) {
Michal Vasko7c565922021-06-10 14:58:27 +0200838 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200839 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vasko7c565922021-06-10 14:58:27 +0200840 (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT)) ? "RPC/action" :
841 ctx->compile_opts & LYS_IS_NOTIF ? "notification" : "a subtree ignoring config", ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200842 } else {
843 target->flags &= ~LYS_CONFIG_MASK;
844 target->flags |= rfn->flags & LYS_CONFIG_MASK;
845 }
846 }
847
848 /* mandatory */
849 if (rfn->flags & LYS_MAND_MASK) {
850 switch (target->nodetype) {
851 case LYS_LEAF:
852 case LYS_CHOICE:
853 case LYS_ANYDATA:
854 case LYS_ANYXML:
855 break;
856 default:
857 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
858 }
859
860 target->flags &= ~LYS_MAND_MASK;
861 target->flags |= rfn->flags & LYS_MAND_MASK;
862 }
863
864 /* presence */
865 if (rfn->presence) {
866 switch (target->nodetype) {
867 case LYS_CONTAINER:
868 break;
869 default:
870 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
871 }
872
Michal Vaskoe180ed02021-02-05 16:31:20 +0100873 lydict_remove(ctx->ctx, ((struct lysp_node_container *)target)->presence);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200874 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
875 }
876
877 /* must */
878 if (rfn->musts) {
879 switch (target->nodetype) {
880 case LYS_CONTAINER:
881 case LYS_LIST:
882 case LYS_LEAF:
883 case LYS_LEAFLIST:
884 case LYS_ANYDATA:
885 case LYS_ANYXML:
886 musts = &((struct lysp_node_container *)target)->musts;
887 break;
888 default:
889 AMEND_WRONG_NODETYPE("refine", "add", "must");
890 }
891
892 LY_ARRAY_FOR(rfn->musts, u) {
893 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200894 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, rfn_pmod, must, &rfn->musts[u]), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200895 }
896 }
897
898 /* min-elements */
899 if (rfn->flags & LYS_SET_MIN) {
900 switch (target->nodetype) {
901 case LYS_LEAFLIST:
902 num = &((struct lysp_node_leaflist *)target)->min;
903 break;
904 case LYS_LIST:
905 num = &((struct lysp_node_list *)target)->min;
906 break;
907 default:
908 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
909 }
910
911 *num = rfn->min;
912 }
913
914 /* max-elements */
915 if (rfn->flags & LYS_SET_MAX) {
916 switch (target->nodetype) {
917 case LYS_LEAFLIST:
918 num = &((struct lysp_node_leaflist *)target)->max;
919 break;
920 case LYS_LIST:
921 num = &((struct lysp_node_list *)target)->max;
922 break;
923 default:
924 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
925 }
926
927 *num = rfn->max;
928 }
929
930 /* if-feature */
931 if (rfn->iffeatures) {
932 switch (target->nodetype) {
933 case LYS_LEAF:
934 case LYS_LEAFLIST:
935 case LYS_LIST:
936 case LYS_CONTAINER:
937 case LYS_CHOICE:
938 case LYS_CASE:
939 case LYS_ANYDATA:
940 case LYS_ANYXML:
941 break;
942 default:
943 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
944 }
945
946 LY_ARRAY_FOR(rfn->iffeatures, u) {
947 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
948 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
949 }
950 }
951
Michal Vasko193dacd2022-10-13 08:43:05 +0200952 /* extension instances */
953 DUP_ARRAY2(ctx->ctx, rfn_pmod, rfn->exts, target->exts, lysp_ext_dup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200954
955cleanup:
Michal Vasko193dacd2022-10-13 08:43:05 +0200956 ctx->cur_mod = orig_mod;
957 ctx->pmod = orig_pmod;
958
959 lysc_update_path(ctx, NULL, NULL);
960 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200961 return ret;
962}
963
964/**
965 * @brief Apply deviate add.
966 *
967 * @param[in] ctx Compile context.
968 * @param[in] d Deviate add to apply.
969 * @param[in,out] target Deviation target.
970 * @return LY_ERR value.
971 */
972static LY_ERR
973lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
974{
975 LY_ERR ret = LY_SUCCESS;
976 LY_ARRAY_COUNT_TYPE u;
977 struct lysp_qname *qname;
978 uint32_t *num;
979 struct lysp_restr **musts, *must;
980
981#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
982 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100983 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200984 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
985 ret = LY_EVALID; \
986 goto cleanup; \
987 }
988
989 /* [units-stmt] */
990 if (d->units) {
991 switch (target->nodetype) {
992 case LYS_LEAF:
993 case LYS_LEAFLIST:
994 break;
995 default:
996 AMEND_WRONG_NODETYPE("deviation", "add", "units");
997 }
998
999 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
1000 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1001 }
1002
1003 /* *must-stmt */
1004 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001005 musts = lysp_node_musts_p(target);
1006 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001007 AMEND_WRONG_NODETYPE("deviation", "add", "must");
1008 }
1009
1010 LY_ARRAY_FOR(d->musts, u) {
1011 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +02001012 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, ctx->pmod, must, &d->musts[u]), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001013 }
1014 }
1015
1016 /* *unique-stmt */
1017 if (d->uniques) {
1018 switch (target->nodetype) {
1019 case LYS_LIST:
1020 break;
1021 default:
1022 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
1023 }
1024
1025 LY_ARRAY_FOR(d->uniques, u) {
1026 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
1027 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
1028 }
1029 }
1030
1031 /* *default-stmt */
1032 if (d->dflts) {
1033 switch (target->nodetype) {
1034 case LYS_LEAF:
1035 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1036 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
1037
1038 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
1039 break;
1040 case LYS_LEAFLIST:
1041 LY_ARRAY_FOR(d->dflts, u) {
1042 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
1043 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
1044 }
1045 break;
1046 case LYS_CHOICE:
1047 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1048 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
1049
1050 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
1051 break;
1052 default:
1053 AMEND_WRONG_NODETYPE("deviation", "add", "default");
1054 }
1055 }
1056
1057 /* [config-stmt] */
1058 if (d->flags & LYS_CONFIG_MASK) {
1059 switch (target->nodetype) {
1060 case LYS_CONTAINER:
1061 case LYS_LEAF:
1062 case LYS_LEAFLIST:
1063 case LYS_LIST:
1064 case LYS_CHOICE:
1065 case LYS_ANYDATA:
1066 case LYS_ANYXML:
1067 break;
1068 default:
1069 AMEND_WRONG_NODETYPE("deviation", "add", "config");
1070 }
1071
1072 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001073 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001074 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
1075 target->flags & LYS_CONFIG_W ? "true" : "false");
1076 ret = LY_EVALID;
1077 goto cleanup;
1078 }
1079
1080 target->flags |= d->flags & LYS_CONFIG_MASK;
1081 }
1082
1083 /* [mandatory-stmt] */
1084 if (d->flags & LYS_MAND_MASK) {
1085 switch (target->nodetype) {
1086 case LYS_LEAF:
1087 case LYS_CHOICE:
1088 case LYS_ANYDATA:
1089 case LYS_ANYXML:
1090 break;
1091 default:
1092 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
1093 }
1094
1095 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001096 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001097 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
1098 target->flags & LYS_MAND_TRUE ? "true" : "false");
1099 ret = LY_EVALID;
1100 goto cleanup;
1101 }
1102
1103 target->flags |= d->flags & LYS_MAND_MASK;
1104 }
1105
1106 /* [min-elements-stmt] */
1107 if (d->flags & LYS_SET_MIN) {
1108 switch (target->nodetype) {
1109 case LYS_LEAFLIST:
1110 num = &((struct lysp_node_leaflist *)target)->min;
1111 break;
1112 case LYS_LIST:
1113 num = &((struct lysp_node_list *)target)->min;
1114 break;
1115 default:
1116 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
1117 }
1118
1119 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001120 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001121 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
1122 ret = LY_EVALID;
1123 goto cleanup;
1124 }
1125
1126 *num = d->min;
1127 }
1128
1129 /* [max-elements-stmt] */
1130 if (d->flags & LYS_SET_MAX) {
1131 switch (target->nodetype) {
1132 case LYS_LEAFLIST:
1133 num = &((struct lysp_node_leaflist *)target)->max;
1134 break;
1135 case LYS_LIST:
1136 num = &((struct lysp_node_list *)target)->max;
1137 break;
1138 default:
1139 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1140 }
1141
1142 if (target->flags & LYS_SET_MAX) {
1143 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001144 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001145 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1146 *num);
1147 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001148 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001149 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1150 }
1151 ret = LY_EVALID;
1152 goto cleanup;
1153 }
1154
1155 *num = d->max;
1156 }
1157
1158cleanup:
1159 return ret;
1160}
1161
1162/**
1163 * @brief Apply deviate delete.
1164 *
1165 * @param[in] ctx Compile context.
1166 * @param[in] d Deviate delete to apply.
1167 * @param[in,out] target Deviation target.
1168 * @return LY_ERR value.
1169 */
1170static LY_ERR
1171lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1172{
1173 LY_ERR ret = LY_SUCCESS;
1174 struct lysp_restr **musts;
1175 LY_ARRAY_COUNT_TYPE u, v;
1176 struct lysp_qname **uniques, **dflts;
1177
Michal Vaskoc636ea42022-09-16 10:20:31 +02001178#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, FREE_CTX, PROPERTY) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001179 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1180 int found = 0; \
1181 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1182 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1183 found = 1; \
1184 break; \
1185 } \
1186 } \
1187 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001188 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001189 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1190 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1191 ret = LY_EVALID; \
1192 goto cleanup; \
1193 } \
1194 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001195 FREE_FUNC(FREE_CTX, &(ORIG_ARRAY)[v]); \
Michal Vasko08e9b112021-06-11 15:41:17 +02001196 if (v < LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1197 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1198 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001199 } \
1200 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1201 LY_ARRAY_FREE(ORIG_ARRAY); \
1202 ORIG_ARRAY = NULL; \
1203 }
1204
1205#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1206 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001207 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001208 ret = LY_EVALID; \
1209 goto cleanup; \
1210 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001211 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001212 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1213 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1214 ret = LY_EVALID; \
1215 goto cleanup; \
1216 }
1217
1218 /* [units-stmt] */
1219 if (d->units) {
1220 switch (target->nodetype) {
1221 case LYS_LEAF:
1222 case LYS_LEAFLIST:
1223 break;
1224 default:
1225 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1226 }
1227
1228 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001229 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001230 ((struct lysp_node_leaf *)target)->units = NULL;
1231 }
1232
1233 /* *must-stmt */
1234 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001235 musts = lysp_node_musts_p(target);
1236 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001237 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1238 }
1239
Michal Vaskoc636ea42022-09-16 10:20:31 +02001240 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, &ctx->free_ctx, "must");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001241 }
1242
1243 /* *unique-stmt */
1244 if (d->uniques) {
1245 switch (target->nodetype) {
1246 case LYS_LIST:
1247 break;
1248 default:
1249 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1250 }
1251
1252 uniques = &((struct lysp_node_list *)target)->uniques;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001253 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, ctx->ctx, "unique");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 }
1255
1256 /* *default-stmt */
1257 if (d->dflts) {
1258 switch (target->nodetype) {
1259 case LYS_LEAF:
1260 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1261 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1262
Michal Vaskoe180ed02021-02-05 16:31:20 +01001263 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001264 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1265 break;
1266 case LYS_LEAFLIST:
1267 dflts = &((struct lysp_node_leaflist *)target)->dflts;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001268 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, ctx->ctx, "default");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001269 break;
1270 case LYS_CHOICE:
1271 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1272 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1273
Michal Vaskoe180ed02021-02-05 16:31:20 +01001274 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001275 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1276 break;
1277 default:
1278 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1279 }
1280 }
1281
1282cleanup:
1283 return ret;
1284}
1285
1286/**
1287 * @brief Apply deviate replace.
1288 *
1289 * @param[in] ctx Compile context.
1290 * @param[in] d Deviate replace to apply.
1291 * @param[in,out] target Deviation target.
1292 * @return LY_ERR value.
1293 */
1294static LY_ERR
1295lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1296{
1297 LY_ERR ret = LY_SUCCESS;
1298 uint32_t *num;
1299
1300#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1301 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001302 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001303 ret = LY_EVALID; \
1304 goto cleanup; \
1305 }
1306
1307 /* [type-stmt] */
1308 if (d->type) {
1309 switch (target->nodetype) {
1310 case LYS_LEAF:
1311 case LYS_LEAFLIST:
1312 break;
1313 default:
1314 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1315 }
1316
Michal Vaskoc636ea42022-09-16 10:20:31 +02001317 lysp_type_free(&ctx->free_ctx, &((struct lysp_node_leaf *)target)->type);
Michal Vasko193dacd2022-10-13 08:43:05 +02001318 lysp_type_dup(ctx->ctx, ctx->pmod, &((struct lysp_node_leaf *)target)->type, d->type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001319 }
1320
1321 /* [units-stmt] */
1322 if (d->units) {
1323 switch (target->nodetype) {
1324 case LYS_LEAF:
1325 case LYS_LEAFLIST:
1326 break;
1327 default:
1328 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1329 }
1330
1331 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001332 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001333 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1334 }
1335
1336 /* [default-stmt] */
1337 if (d->dflt.str) {
1338 switch (target->nodetype) {
1339 case LYS_LEAF:
1340 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1341
Michal Vaskoe180ed02021-02-05 16:31:20 +01001342 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001343 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1344 break;
1345 case LYS_CHOICE:
1346 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1347
Michal Vaskoe180ed02021-02-05 16:31:20 +01001348 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001349 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1350 break;
1351 default:
1352 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1353 }
1354 }
1355
1356 /* [config-stmt] */
1357 if (d->flags & LYS_CONFIG_MASK) {
1358 switch (target->nodetype) {
1359 case LYS_CONTAINER:
1360 case LYS_LEAF:
1361 case LYS_LEAFLIST:
1362 case LYS_LIST:
1363 case LYS_CHOICE:
1364 case LYS_ANYDATA:
1365 case LYS_ANYXML:
1366 break;
1367 default:
1368 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1369 }
1370
1371 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001372 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1373 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001374 ret = LY_EVALID;
1375 goto cleanup;
1376 }
1377
1378 target->flags &= ~LYS_CONFIG_MASK;
1379 target->flags |= d->flags & LYS_CONFIG_MASK;
1380 }
1381
1382 /* [mandatory-stmt] */
1383 if (d->flags & LYS_MAND_MASK) {
1384 switch (target->nodetype) {
1385 case LYS_LEAF:
1386 case LYS_CHOICE:
1387 case LYS_ANYDATA:
1388 case LYS_ANYXML:
1389 break;
1390 default:
1391 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1392 }
1393
1394 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001395 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1396 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001397 ret = LY_EVALID;
1398 goto cleanup;
1399 }
1400
1401 target->flags &= ~LYS_MAND_MASK;
1402 target->flags |= d->flags & LYS_MAND_MASK;
1403 }
1404
1405 /* [min-elements-stmt] */
1406 if (d->flags & LYS_SET_MIN) {
1407 switch (target->nodetype) {
1408 case LYS_LEAFLIST:
1409 num = &((struct lysp_node_leaflist *)target)->min;
1410 break;
1411 case LYS_LIST:
1412 num = &((struct lysp_node_list *)target)->min;
1413 break;
1414 default:
1415 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1416 }
1417
1418 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001419 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001420 ret = LY_EVALID;
1421 goto cleanup;
1422 }
1423
1424 *num = d->min;
1425 }
1426
1427 /* [max-elements-stmt] */
1428 if (d->flags & LYS_SET_MAX) {
1429 switch (target->nodetype) {
1430 case LYS_LEAFLIST:
1431 num = &((struct lysp_node_leaflist *)target)->max;
1432 break;
1433 case LYS_LIST:
1434 num = &((struct lysp_node_list *)target)->max;
1435 break;
1436 default:
1437 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1438 }
1439
1440 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001441 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001442 ret = LY_EVALID;
1443 goto cleanup;
1444 }
1445
1446 *num = d->max;
1447 }
1448
1449cleanup:
1450 return ret;
1451}
1452
1453/**
Michal Vasko193dacd2022-10-13 08:43:05 +02001454 * @brief Apply deviation with all its deviates.
1455 *
1456 * @param[in] ctx Compile context.
1457 * @param[in] dev Deviation to apply.
1458 * @param[in] dev_pmod Local module of the deviation.
1459 * @param[in,out] target Deviation target.
1460 * @return LY_ERR value.
1461 */
1462static LY_ERR
1463lys_apply_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev, const struct lysp_module *dev_pmod,
1464 struct lysp_node *target)
1465{
1466 LY_ERR ret = LY_SUCCESS;
1467 struct lys_module *orig_mod = ctx->cur_mod;
1468 struct lysp_module *orig_pmod = ctx->pmod;
1469 char orig_path[LYSC_CTX_BUFSIZE];
1470 struct lysp_deviate *d;
1471
1472 /* clear path and set modules */
1473 strcpy(orig_path, ctx->path);
1474 ctx->path_len = 1;
1475 ctx->cur_mod = dev_pmod->mod;
1476 ctx->pmod = (struct lysp_module *)dev_pmod;
1477
1478 /* generate correct path */
1479 lysc_update_path(ctx, NULL, "{deviation}");
1480 lysc_update_path(ctx, NULL, dev->nodeid);
1481
1482 LY_LIST_FOR(dev->deviates, d) {
1483 switch (d->mod) {
1484 case LYS_DEV_ADD:
1485 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, target);
1486 break;
1487 case LYS_DEV_DELETE:
1488 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, target);
1489 break;
1490 case LYS_DEV_REPLACE:
1491 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, target);
1492 break;
1493 default:
1494 LOGINT(ctx->ctx);
1495 ret = LY_EINT;
1496 }
1497 LY_CHECK_GOTO(ret, cleanup);
1498 }
1499
1500 /* deviation extension instances */
1501 DUP_ARRAY2(ctx->ctx, dev_pmod, dev->exts, target->exts, lysp_ext_dup);
1502
1503cleanup:
1504 ctx->cur_mod = orig_mod;
1505 ctx->pmod = orig_pmod;
1506
1507 strcpy(ctx->path, orig_path);
1508 ctx->path_len = strlen(ctx->path);
1509 return ret;
1510}
1511
1512/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001513 * @brief Check whether a compiled node matches a single schema nodeid name test.
1514 *
1515 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1516 * @param[in] mod Expected module.
1517 * @param[in] name Expected name.
1518 * @param[in] name_len Length of @p name.
1519 * @return Whether it is a match or not.
1520 */
1521static ly_bool
1522lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1523 size_t name_len)
1524{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001525 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001526 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001527 return 0;
1528 }
1529
1530 /* compare names */
Michal Vasko544e58a2021-01-28 14:33:41 +01001531 if (ly_strncmp((*node)->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001532 return 0;
1533 }
1534
Michal Vasko2a668712020-10-21 11:48:09 +02001535 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001536 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001537
1538 return 1;
1539}
1540
1541/**
1542 * @brief Check whether a node matches specific schema nodeid.
1543 *
1544 * @param[in] exp Parsed nodeid to match.
1545 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1546 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1547 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1548 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1549 * @param[in] pnode_mod Compiled @p pnode to-be module.
1550 * @return Whether it is a match or not.
1551 */
1552static ly_bool
1553lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1554 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1555{
1556 uint32_t i;
1557 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001558 const char *name = NULL;
1559 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001560
1561 /* compare last node in the node ID */
1562 i = exp->used - 1;
1563
1564 /* get exp node ID module */
1565 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name, &name_len);
1566 assert(mod);
1567
1568 if (pnode) {
1569 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001570 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001571 return 0;
1572 }
1573 } else {
1574 /* using parent directly */
1575 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1576 return 0;
1577 }
1578 }
1579
1580 /* now compare all the compiled parents */
1581 while (i > 1) {
1582 i -= 2;
1583 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1584
1585 if (!parent) {
1586 /* no more parents but path continues */
1587 return 0;
1588 }
1589
1590 /* get exp node ID module */
1591 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1592 &name_len);
1593 assert(mod);
1594
1595 /* compare with the parent */
1596 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1597 return 0;
1598 }
1599 }
1600
1601 if (ctx_node && (ctx_node != parent)) {
1602 /* descendant path has not finished in the context node */
1603 return 0;
1604 } else if (!ctx_node && parent) {
1605 /* some parent was not matched */
1606 return 0;
1607 }
1608
1609 return 1;
1610}
1611
1612void
1613lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1614{
1615 if (aug) {
1616 lyxp_expr_free(ctx, aug->nodeid);
1617
1618 free(aug);
1619 }
1620}
1621
1622void
1623lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1624{
1625 if (dev) {
1626 lyxp_expr_free(ctx, dev->nodeid);
1627 LY_ARRAY_FREE(dev->devs);
1628 LY_ARRAY_FREE(dev->dev_pmods);
1629
1630 free(dev);
1631 }
1632}
1633
1634void
1635lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1636{
1637 if (rfn) {
1638 lyxp_expr_free(ctx, rfn->nodeid);
1639 LY_ARRAY_FREE(rfn->rfns);
1640
1641 free(rfn);
1642 }
1643}
1644
1645void
Michal Vaskoc636ea42022-09-16 10:20:31 +02001646lysp_dev_node_free(struct lysc_ctx *cctx, struct lysp_node *dev_pnode)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001647{
1648 if (!dev_pnode) {
1649 return;
1650 }
1651
1652 switch (dev_pnode->nodetype) {
1653 case LYS_CONTAINER:
1654 ((struct lysp_node_container *)dev_pnode)->child = NULL;
Michal Vaskoec8f4272021-10-27 09:14:03 +02001655 ((struct lysp_node_container *)dev_pnode)->actions = NULL;
1656 ((struct lysp_node_container *)dev_pnode)->notifs = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001657 break;
1658 case LYS_LIST:
1659 ((struct lysp_node_list *)dev_pnode)->child = NULL;
Michal Vaskoec8f4272021-10-27 09:14:03 +02001660 ((struct lysp_node_list *)dev_pnode)->actions = NULL;
1661 ((struct lysp_node_list *)dev_pnode)->notifs = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001662 break;
1663 case LYS_CHOICE:
1664 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1665 break;
1666 case LYS_CASE:
1667 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1668 break;
1669 case LYS_LEAF:
1670 case LYS_LEAFLIST:
1671 case LYS_ANYXML:
1672 case LYS_ANYDATA:
1673 /* no children */
1674 break;
1675 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001676 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001677 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001678 case LYS_RPC:
1679 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001680 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1681 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001682 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001683 case LYS_INPUT:
1684 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001685 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001686 lysp_node_free(&cctx->free_ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001687 free(dev_pnode);
1688 return;
1689 default:
Michal Vaskoc636ea42022-09-16 10:20:31 +02001690 LOGINT(cctx->ctx);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001691 return;
1692 }
1693
Michal Vaskoc636ea42022-09-16 10:20:31 +02001694 lysp_node_free(&cctx->free_ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001695}
1696
1697LY_ERR
1698lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1699 struct lysp_node **dev_pnode, ly_bool *not_supported)
1700{
1701 LY_ERR ret = LY_SUCCESS;
1702 uint32_t i;
1703 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001704 struct lysc_refine *rfn;
1705 struct lysc_deviation *dev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001706
1707 *dev_pnode = NULL;
1708 *not_supported = 0;
1709
Michal Vaskoee057572022-05-26 08:31:52 +02001710 for (i = 0; i < ctx->uses_rfns.count; ) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001711 rfn = ctx->uses_rfns.objs[i];
1712
Michal Vasko193dacd2022-10-13 08:43:05 +02001713 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, ctx->cur_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001714 /* not our target node */
Michal Vaskoee057572022-05-26 08:31:52 +02001715 ++i;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001716 continue;
1717 }
1718
1719 if (!*dev_pnode) {
1720 /* first refine on this node, create a copy first */
Michal Vasko193dacd2022-10-13 08:43:05 +02001721 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, ctx->pmod, pnode, 1, dev_pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001722 }
1723
1724 /* apply all the refines by changing (the copy of) the parsed node */
1725 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001726 LY_CHECK_GOTO(ret = lys_apply_refine(ctx, rfn->rfns[u], rfn->nodeid_pmod, *dev_pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001727 }
1728
1729 /* refine was applied, remove it */
1730 lysc_refine_free(ctx->ctx, rfn);
1731 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1732
Michal Vaskoee057572022-05-26 08:31:52 +02001733 /* refines use relative paths so more may apply to a single node */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001734 }
1735
1736 for (i = 0; i < ctx->devs.count; ++i) {
1737 dev = ctx->devs.objs[i];
1738
Michal Vasko193dacd2022-10-13 08:43:05 +02001739 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, ctx->cur_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001740 /* not our target node */
1741 continue;
1742 }
1743
1744 if (dev->not_supported) {
1745 /* it is not supported, no more deviations */
1746 *not_supported = 1;
1747 goto dev_applied;
1748 }
1749
1750 if (!*dev_pnode) {
1751 /* first deviation on this node, create a copy first */
Michal Vasko193dacd2022-10-13 08:43:05 +02001752 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, ctx->pmod, pnode, 1, dev_pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001753 }
1754
1755 /* apply all the deviates by changing (the copy of) the parsed node */
1756 LY_ARRAY_FOR(dev->devs, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001757 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, dev->devs[u], dev->dev_pmods[u], *dev_pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001758 }
1759
1760dev_applied:
1761 /* deviation was applied, remove it */
1762 lysc_deviation_free(ctx->ctx, dev);
1763 ly_set_rm_index(&ctx->devs, i, NULL);
1764
1765 /* all the deviations for one target node are in one structure, we are done */
1766 break;
1767 }
1768
1769cleanup:
1770 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001771 lysp_dev_node_free(ctx, *dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001772 *dev_pnode = NULL;
1773 *not_supported = 0;
1774 }
1775 return ret;
1776}
1777
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001778LY_ERR
1779lys_compile_augment_children(struct lysc_ctx *ctx, struct lysp_when *aug_when, uint16_t aug_flags, struct lysp_node *child,
Michal Vaskoa084fa32022-05-16 11:32:58 +02001780 struct lysc_node *target, ly_bool child_unres_disabled)
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001781{
1782 LY_ERR rc = LY_SUCCESS;
1783 struct lysp_node *pnode;
1784 struct lysc_node *node;
1785 struct lysc_when *when_shared = NULL;
1786 ly_bool enabled, allow_mand = 0;
1787 struct ly_set child_set = {0};
1788 uint32_t i, opt_prev = ctx->compile_opts;
1789
1790 /* check for mandatory nodes
1791 * - new cases augmenting some choice can have mandatory nodes
1792 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1793 */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001794 if (aug_when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001795 allow_mand = 1;
1796 }
1797
1798 LY_LIST_FOR(child, pnode) {
1799 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1800 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1801 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1802 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
1803 LOGVAL(ctx->ctx, LYVE_REFERENCE,
1804 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1805 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1806 rc = LY_EVALID;
1807 goto cleanup;
1808 }
1809
1810 /* compile the children */
1811 if (target->nodetype == LYS_CHOICE) {
1812 LY_CHECK_GOTO(rc = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
1813 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1814 if (target->nodetype == LYS_INPUT) {
1815 ctx->compile_opts |= LYS_COMPILE_RPC_INPUT;
1816 } else {
1817 ctx->compile_opts |= LYS_COMPILE_RPC_OUTPUT;
1818 }
1819 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1820 } else {
1821 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1822 }
1823
1824 /* eval if-features again for the rest of this node processing */
1825 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1826 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001827 ctx->compile_opts |= LYS_COMPILE_DISABLED;
1828 }
1829
1830 /* since the augment node is not present in the compiled tree, we need to pass some of its
1831 * statements to all its children */
1832 for (i = 0; i < child_set.count; ++i) {
1833 node = child_set.snodes[i];
1834 if (!allow_mand && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1835 node->flags &= ~LYS_MAND_TRUE;
1836 lys_compile_mandatory_parents(target, 0);
1837 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
1838 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.",
1839 node->name);
1840 rc = LY_EVALID;
1841 goto cleanup;
1842 }
1843
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001844 if (aug_when) {
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001845 /* pass augment's when to all the children */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001846 rc = lys_compile_when(ctx, aug_when, aug_flags, target, lysc_data_node(target), node, &when_shared);
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001847 LY_CHECK_GOTO(rc, cleanup);
1848 }
1849
Michal Vaskoa084fa32022-05-16 11:32:58 +02001850 if (child_unres_disabled) {
Michal Vaskoad0980a2022-05-09 11:43:47 +02001851 /* child is disabled by the augment if-features */
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001852 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
1853 }
1854 }
1855
1856 /* next iter */
1857 ly_set_erase(&child_set, NULL);
1858 ctx->compile_opts = opt_prev;
1859 }
1860
1861cleanup:
1862 ly_set_erase(&child_set, NULL);
1863 ctx->compile_opts = opt_prev;
1864 return rc;
1865}
1866
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001867/**
1868 * @brief Compile the parsed augment connecting it into its target.
1869 *
1870 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1871 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1872 * are already implemented and compiled.
1873 *
1874 * @param[in] ctx Compile context.
1875 * @param[in] aug_p Parsed augment to compile.
1876 * @param[in] target Target node of the augment.
1877 * @return LY_SUCCESS on success.
1878 * @return LY_EVALID on failure.
1879 */
1880static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001881lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001882{
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001883 LY_ERR rc = LY_SUCCESS;
Michal Vaskoa084fa32022-05-16 11:32:58 +02001884 ly_bool enabled, child_unres_disabled = 0;
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001885 uint32_t opt_prev = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001886
Michal Vasko95f736c2022-06-08 12:03:31 +02001887 assert(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF));
1888
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001889 /* nodetype checks */
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001890 if (aug_p->actions && !lysc_node_actions_p(target)) {
1891 LOGVAL(ctx->ctx, LYVE_REFERENCE,
1892 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
1893 lys_nodetype2str(target->nodetype), aug_p->actions->name);
1894 rc = LY_EVALID;
1895 goto cleanup;
1896 }
1897 if (aug_p->notifs && !lysc_node_notifs_p(target)) {
1898 LOGVAL(ctx->ctx, LYVE_REFERENCE,
1899 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
1900 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
1901 rc = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001902 goto cleanup;
1903 }
1904
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001905 /* augment if-features */
1906 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, aug_p->iffeatures, &enabled), cleanup);
1907 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001908 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vaskoa084fa32022-05-16 11:32:58 +02001909 child_unres_disabled = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001910 }
1911
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001912 /* augment children */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001913 rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, aug_p->child, target, child_unres_disabled);
1914 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001915
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001916 /* augment actions */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001917 rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, (struct lysp_node *)aug_p->actions, target,
1918 child_unres_disabled);
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001919 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001920
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001921 /* augment notifications */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001922 rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, (struct lysp_node *)aug_p->notifs, target,
1923 child_unres_disabled);
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001924 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001925
Michal Vasko193dacd2022-10-13 08:43:05 +02001926 /* compile extensions into the target */
1927 COMPILE_EXTS_GOTO(ctx, aug_p->exts, target->exts, target, rc, cleanup);
1928
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001929cleanup:
Michal Vasko7c565922021-06-10 14:58:27 +02001930 ctx->compile_opts = opt_prev;
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001931 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001932}
1933
1934LY_ERR
1935lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1936{
1937 LY_ERR ret = LY_SUCCESS;
1938 struct lys_module *orig_mod = ctx->cur_mod;
1939 struct lysp_module *orig_pmod = ctx->pmod;
1940 uint32_t i;
1941 char orig_path[LYSC_CTX_BUFSIZE];
1942 struct lysc_augment *aug;
1943
1944 /* uses augments */
1945 for (i = 0; i < ctx->uses_augs.count; ) {
1946 aug = ctx->uses_augs.objs[i];
1947
Michal Vasko28fe5f62021-03-03 16:37:39 +01001948 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001949 /* not our target node */
1950 ++i;
1951 continue;
1952 }
1953
Michal Vaskob8df5762021-01-12 15:15:53 +01001954 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001955 lysc_update_path(ctx, NULL, "{augment}");
1956 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001957 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001958
1959 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001960 ret = lys_compile_augment(ctx, aug->aug_p, node);
1961 lysc_update_path(ctx, NULL, NULL);
1962 lysc_update_path(ctx, NULL, NULL);
1963 LY_CHECK_GOTO(ret, cleanup);
1964
Michal Vaskoc75f2042021-06-08 14:57:03 +02001965 /* augment was applied, remove it (index and the whole set may have changed because other augments
1966 * could have been applied) */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001967 ly_set_rm(&ctx->uses_augs, aug, NULL);
1968 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001969 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001970 }
1971
1972 /* top-level augments */
1973 for (i = 0; i < ctx->augs.count; ) {
1974 aug = ctx->augs.objs[i];
1975
Michal Vasko28fe5f62021-03-03 16:37:39 +01001976 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001977 /* not our target node */
1978 ++i;
1979 continue;
1980 }
1981
Michal Vaskob8df5762021-01-12 15:15:53 +01001982 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001983 strcpy(orig_path, ctx->path);
1984 ctx->path_len = 1;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001985 ctx->cur_mod = aug->aug_pmod->mod;
1986 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001987 lysc_update_path(ctx, NULL, "{augment}");
1988 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001989
1990 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001991 ret = lys_compile_augment(ctx, aug->aug_p, node);
1992 strcpy(ctx->path, orig_path);
1993 ctx->path_len = strlen(ctx->path);
1994 LY_CHECK_GOTO(ret, cleanup);
1995
1996 /* augment was applied, remove it */
1997 ly_set_rm(&ctx->augs, aug, NULL);
1998 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001999 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002000 }
2001
2002cleanup:
2003 ctx->cur_mod = orig_mod;
2004 ctx->pmod = orig_pmod;
2005 return ret;
2006}
2007
2008/**
2009 * @brief Prepare a top-level augment to be applied during data nodes compilation.
2010 *
2011 * @param[in] ctx Compile context.
2012 * @param[in] aug_p Parsed augment to be applied.
2013 * @param[in] pmod Both current and prefix module for @p aug_p.
2014 * @return LY_ERR value.
2015 */
2016static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002017lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, const struct lysp_module *pmod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002018{
2019 LY_ERR ret = LY_SUCCESS;
2020 struct lyxp_expr *exp = NULL;
2021 struct lysc_augment *aug;
2022 const struct lys_module *mod;
2023
2024 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2025 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
2026 LY_CHECK_GOTO(ret, cleanup);
2027
2028 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2029 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2030 if (mod != ctx->cur_mod) {
2031 /* augment for another module, ignore */
2032 goto cleanup;
2033 }
2034
2035 /* allocate new compiled augment and store it in the set */
2036 aug = calloc(1, sizeof *aug);
2037 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2038 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
2039
2040 aug->nodeid = exp;
2041 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01002042 aug->aug_pmod = pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002043 aug->aug_p = aug_p;
2044
2045cleanup:
2046 lyxp_expr_free(ctx->ctx, exp);
2047 return ret;
2048}
2049
2050LY_ERR
2051lys_precompile_own_augments(struct lysc_ctx *ctx)
2052{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002053 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002054
2055 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002056 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
2057 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002058
2059 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002060 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
2061 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002062 }
2063
2064 /* collect all submodules augments */
2065 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002066 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
2067 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, (struct lysp_module *)aug_mod->parsed->includes[v].submodule));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002068 }
2069 }
2070 }
2071
2072 return LY_SUCCESS;
2073}
2074
2075/**
2076 * @brief Prepare a deviation to be applied during data nodes compilation.
2077 *
2078 * @param[in] ctx Compile context.
2079 * @param[in] dev_p Parsed deviation to be applied.
2080 * @param[in] pmod Both current and prefix module for @p dev_p.
2081 * @return LY_ERR value.
2082 */
2083static LY_ERR
2084lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2085{
2086 LY_ERR ret = LY_SUCCESS;
2087 struct lysc_deviation *dev = NULL;
2088 struct lyxp_expr *exp = NULL;
2089 struct lysp_deviation **new_dev;
2090 const struct lys_module *mod;
2091 const struct lysp_module **new_dev_pmod;
2092 uint32_t i;
2093
2094 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2095 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2096 LY_CHECK_GOTO(ret, cleanup);
2097
2098 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2099 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2100 if (mod != ctx->cur_mod) {
2101 /* deviation for another module, ignore */
2102 goto cleanup;
2103 }
2104
2105 /* try to find the node in already compiled deviations */
2106 for (i = 0; i < ctx->devs.count; ++i) {
2107 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2108 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2109 dev = ctx->devs.objs[i];
2110 break;
2111 }
2112 }
2113
2114 if (!dev) {
2115 /* allocate new compiled deviation */
2116 dev = calloc(1, sizeof *dev);
2117 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2118 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2119
2120 dev->nodeid = exp;
2121 exp = NULL;
2122 }
2123
2124 /* add new parsed deviation structure */
2125 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2126 *new_dev = dev_p;
2127 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2128 *new_dev_pmod = pmod;
2129
2130cleanup:
2131 lyxp_expr_free(ctx->ctx, exp);
2132 return ret;
2133}
2134
2135LY_ERR
2136lys_precompile_own_deviations(struct lysc_ctx *ctx)
2137{
2138 LY_ARRAY_COUNT_TYPE u, v, w;
Michal Vaskoe8220db2021-06-02 15:39:05 +02002139 struct lys_module *orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002140 const struct lys_module *dev_mod;
2141 struct lysc_deviation *dev;
2142 struct lysp_deviate *d;
2143 int not_supported;
2144 uint32_t i;
2145
2146 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2147 dev_mod = ctx->cur_mod->deviated_by[u];
2148
2149 /* compile all module deviations */
2150 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2151 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2152 }
2153
2154 /* compile all submodules deviations */
2155 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2156 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2157 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2158 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2159 }
2160 }
2161 }
2162
2163 /* set not-supported flags for all the deviations */
2164 for (i = 0; i < ctx->devs.count; ++i) {
2165 dev = ctx->devs.objs[i];
2166 not_supported = 0;
2167
2168 LY_ARRAY_FOR(dev->devs, u) {
2169 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2170 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2171 not_supported = 1;
2172 break;
2173 }
2174 }
2175 if (not_supported) {
2176 break;
2177 }
2178 }
2179 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Michal Vaskoe8220db2021-06-02 15:39:05 +02002180 orig_cur_mod = ctx->cur_mod;
2181 ctx->cur_mod = dev->dev_pmods[u]->mod;
2182 lysc_update_path(ctx, NULL, "{deviation}");
2183 lysc_update_path(ctx, NULL, dev->nodeid->expr);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002184 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002185 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
Michal Vaskoe8220db2021-06-02 15:39:05 +02002186 lysc_update_path(ctx, NULL, NULL);
2187 lysc_update_path(ctx, NULL, NULL);
2188 ctx->cur_mod = orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002189 return LY_EVALID;
2190 }
2191
2192 dev->not_supported = not_supported;
2193 }
2194
2195 return LY_SUCCESS;
2196}
2197
2198/**
2199 * @brief Add a module reference into an array, checks for duplicities.
2200 *
2201 * @param[in] ctx Compile context.
2202 * @param[in] mod Module reference to add.
2203 * @param[in,out] mod_array Module sized array to add to.
2204 * @return LY_ERR value.
2205 */
2206static LY_ERR
2207lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2208{
2209 LY_ARRAY_COUNT_TYPE u;
2210 struct lys_module **new_mod;
2211
2212 LY_ARRAY_FOR(*mod_array, u) {
2213 if ((*mod_array)[u] == mod) {
2214 /* already there */
2215 return LY_EEXIST;
2216 }
2217 }
2218
2219 /* add the new module ref */
2220 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2221 *new_mod = mod;
2222
2223 return LY_SUCCESS;
2224}
2225
Michal Vasko1ccbf542021-04-19 11:35:00 +02002226/**
2227 * @brief Check whether all modules in a set are implemented.
2228 *
2229 * @param[in] mod_set Module set to check.
2230 * @return Whether all modules are implemented or not.
2231 */
2232static ly_bool
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002233lys_precompile_mod_set_is_all_implemented(const struct ly_set *mod_set)
Michal Vasko1ccbf542021-04-19 11:35:00 +02002234{
2235 uint32_t i;
2236 const struct lys_module *mod;
2237
2238 for (i = 0; i < mod_set->count; ++i) {
2239 mod = mod_set->objs[i];
2240 if (!mod->implemented) {
2241 return 0;
2242 }
2243 }
2244
2245 return 1;
2246}
2247
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002248LY_ERR
Michal Vasko65333882021-06-10 14:12:16 +02002249lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002250{
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002251 LY_ERR ret = LY_SUCCESS, r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002252 LY_ARRAY_COUNT_TYPE u, v;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002253 struct lysc_ctx ctx;
Michal Vasko65333882021-06-10 14:12:16 +02002254 struct lysp_module *mod_p;
2255 struct lys_module *m;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002256 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002257 struct lysp_node_augment *aug;
Michal Vaskoc56d6372021-10-19 12:29:00 +02002258 const char **imp_f, *all_f[] = {"*", NULL};
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002259 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002260 struct ly_set mod_set = {0}, set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002261
Michal Vasko65333882021-06-10 14:12:16 +02002262 mod_p = mod->parsed;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002263 LYSC_CTX_INIT_PMOD(ctx, mod_p, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002264
Radek Krejci2a9fc652021-01-22 17:44:34 +01002265 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002266 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002267 lysc_update_path(&ctx, NULL, "{augment}");
2268 lysc_update_path(&ctx, NULL, aug->nodeid);
2269 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2270 lysc_update_path(&ctx, NULL, NULL);
2271 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002272 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002273
Michal Vasko1ccbf542021-04-19 11:35:00 +02002274 /* add this module into the target module augmented_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002275 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002276 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002277 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002278 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002279 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002280 }
2281
2282 LY_ARRAY_FOR(mod_p->deviations, u) {
2283 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002284 lysc_update_path(&ctx, NULL, "{deviation}");
2285 lysc_update_path(&ctx, NULL, mod_p->deviations[u].nodeid);
2286 ret = lys_nodeid_mod_check(&ctx, mod_p->deviations[u].nodeid, 1, &set, NULL, &m);
2287 lysc_update_path(&ctx, NULL, NULL);
2288 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002289 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002290
Michal Vasko1ccbf542021-04-19 11:35:00 +02002291 /* add this module into the target module deviated_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002292 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002293 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002294 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2295 }
2296 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002297 }
2298
2299 /* the same for augments and deviations in submodules */
2300 LY_ARRAY_FOR(mod_p->includes, v) {
2301 submod = mod_p->includes[v].submodule;
Michal Vasko65333882021-06-10 14:12:16 +02002302 ctx.pmod = (struct lysp_module *)submod;
Michal Vaskoce3d6172021-06-08 14:59:49 +02002303
Radek Krejci2a9fc652021-01-22 17:44:34 +01002304 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko65333882021-06-10 14:12:16 +02002305 lysc_update_path(&ctx, NULL, "{augment}");
2306 lysc_update_path(&ctx, NULL, aug->nodeid);
2307 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2308 lysc_update_path(&ctx, NULL, NULL);
2309 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002310 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002311
Michal Vasko65333882021-06-10 14:12:16 +02002312 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002313 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002314 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002315 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002316 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002317 }
2318
2319 LY_ARRAY_FOR(submod->deviations, u) {
Michal Vasko65333882021-06-10 14:12:16 +02002320 lysc_update_path(&ctx, NULL, "{deviation}");
2321 lysc_update_path(&ctx, NULL, submod->deviations[u].nodeid);
2322 ret = lys_nodeid_mod_check(&ctx, submod->deviations[u].nodeid, 1, &set, NULL, &m);
2323 lysc_update_path(&ctx, NULL, NULL);
2324 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002325 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002326
Michal Vasko65333882021-06-10 14:12:16 +02002327 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002328 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002329 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2330 }
2331 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002332 }
2333 }
2334
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002335 for (i = 0; i < mod_set.count; ++i) {
2336 m = mod_set.objs[i];
Michal Vasko1ccbf542021-04-19 11:35:00 +02002337
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002338 if (m == mod) {
2339 /* will be applied normally later */
2340 continue;
2341 }
2342
2343 /* we do not actually need the target modules compiled with out amends, they just need to be implemented
2344 * not compiled yet and marked for compilation */
2345
2346 if (!m->implemented) {
2347 /* implement the target module */
Michal Vaskoc56d6372021-10-19 12:29:00 +02002348 imp_f = (mod->ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL;
2349 r = lys_implement(m, imp_f, unres);
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002350 if (r == LY_ERECOMPILE) {
2351 /* implement all the modules right away to save possible later recompilation */
2352 ret = r;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002353 continue;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002354 } else if (r) {
2355 /* error */
2356 ret = r;
Michal Vasko65333882021-06-10 14:12:16 +02002357 goto cleanup;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002358 }
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002359 } else if (m->compiled) {
2360 /* target module was already compiled without our amends (augment/deviation), we need to recompile it */
2361 m->to_compile = 1;
2362 ret = LY_ERECOMPILE;
2363 continue;
2364 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002365 }
2366
Michal Vasko1ccbf542021-04-19 11:35:00 +02002367cleanup:
2368 ly_set_erase(&set, NULL);
2369 ly_set_erase(&mod_set, NULL);
2370 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002371}
2372
2373void
2374lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2375{
2376 uint32_t i;
2377 LY_ARRAY_COUNT_TYPE u, count;
2378 struct lys_module *m;
2379
2380 for (i = 0; i < ctx->list.count; ++i) {
2381 m = ctx->list.objs[i];
2382
2383 if (m->augmented_by) {
2384 count = LY_ARRAY_COUNT(m->augmented_by);
2385 for (u = 0; u < count; ++u) {
2386 if (m->augmented_by[u] == mod) {
2387 /* keep the order */
2388 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002389 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u - 1) * sizeof *m->augmented_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002390 }
2391 LY_ARRAY_DECREMENT(m->augmented_by);
2392 break;
2393 }
2394 }
2395 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2396 LY_ARRAY_FREE(m->augmented_by);
2397 m->augmented_by = NULL;
2398 }
2399 }
2400
2401 if (m->deviated_by) {
2402 count = LY_ARRAY_COUNT(m->deviated_by);
2403 for (u = 0; u < count; ++u) {
2404 if (m->deviated_by[u] == mod) {
2405 /* keep the order */
2406 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002407 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u - 1) * sizeof *m->deviated_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002408 }
2409 LY_ARRAY_DECREMENT(m->deviated_by);
2410 break;
2411 }
2412 }
2413 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2414 LY_ARRAY_FREE(m->deviated_by);
2415 m->deviated_by = NULL;
2416 }
2417 }
2418 }
2419}
Michal Vasko0b50f6b2022-10-05 15:07:55 +02002420
2421LIBYANG_API_DEF LY_ERR
2422lys_compile_extension_instance_find_augment_target(struct lysc_ctx *ctx, const char *path,
2423 struct lysc_ext_instance **aug_ext, struct lysc_node **aug_target)
2424{
2425 LY_ERR rc;
2426 LY_ARRAY_COUNT_TYPE u;
2427 struct lys_module *target_mod;
2428 struct lysc_ext_instance *target_ext = NULL, *prev_ext;
2429 const struct lysc_node *target;
2430 const char *id, *id2, *name;
2431 size_t name_len;
2432 uint16_t flag;
2433
2434 LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, ctx, path, aug_target, LY_EINVAL);
2435
2436 /* skip first slash */
2437 id = path;
2438 if (id[0] != '/') {
2439 LOGVAL(ctx->ctx, LYVE_SYNTAX, "Invalid absolute-schema-nodeid \"%s\".", path);
2440 return LY_EVALID;
2441 }
2442 ++id;
2443
2444 /* find the next slash */
2445 id2 = strchr(id, '/');
2446 if (!id2) {
2447 LOGVAL(ctx->ctx, LYVE_SYNTAX, "Invalid absolute-schema-nodeid \"%s\".", path);
2448 return LY_EVALID;
2449 }
2450
2451 /* find the target module */
2452 target_mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, id, id2 - id, ctx->pmod, &name, &name_len);
2453 if (!target_mod) {
2454 return LY_ENOTFOUND;
2455 } else if (!target_mod->implemented) {
2456 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Augment target extension instance \"%*.s\" in a non-implemented module \"%s\".",
2457 (int)name_len, name, target_mod->name);
2458 return LY_ENOTFOUND;
2459 }
2460
2461 /* find the extension instance */
2462 LY_ARRAY_FOR(target_mod->compiled->exts, u) {
2463 if (!ly_strncmp(target_mod->compiled->exts[u].argument, name, name_len)) {
2464 target_ext = &target_mod->compiled->exts[u];
2465 break;
2466 }
2467 }
2468 if (!target_ext) {
2469 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Augment target extension instance \"%*.s\" not found in module \"%s\".",
2470 (int)name_len, name, target_mod->name);
2471 return LY_ENOTFOUND;
2472 }
2473
2474 /* find the augment target with the correct compile context */
2475 prev_ext = ctx->ext;
2476 ctx->ext = target_ext;
2477 rc = lysc_resolve_schema_nodeid(ctx, id2, strlen(id2), NULL, LY_VALUE_SCHEMA, ctx->pmod, 0, &target, &flag);
2478 ctx->ext = prev_ext;
2479 LY_CHECK_RET(rc);
2480
2481 /* add this module into the target module augmented_by, if not there */
2482 lys_array_add_mod_ref(ctx, ctx->cur_mod, &target_mod->augmented_by);
2483
2484 if (aug_ext) {
2485 *aug_ext = target_ext;
2486 }
2487 *aug_target = (struct lysc_node *)target;
2488 return LY_SUCCESS;
2489}