blob: e64b1833c2c064c445a371f2118db9d368db788a [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/**
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001542 * @brief Check whether a compiled ext instance matches a single schema nodeid name test.
1543 *
1544 * @param[in,out] ext Compiled ext instance to consider. On a match it is zeroed to not match again.
1545 * @param[in] mod Expected module.
1546 * @param[in] name Expected name.
1547 * @param[in] name_len Length of @p name.
1548 * @return Whether it is a match or not.
1549 */
1550static ly_bool
1551lysp_schema_nodeid_match_ext(const struct lysc_ext_instance **ext, const struct lys_module *mod, const char *name,
1552 size_t name_len)
1553{
1554 /* compare with the module */
1555 if ((*ext)->module != mod) {
1556 return 0;
1557 }
1558
1559 /* compare names (argument) */
1560 if (ly_strncmp((*ext)->argument, name, name_len)) {
1561 return 0;
1562 }
1563
1564 /* zero */
1565 *ext = NULL;
1566
1567 return 1;
1568}
1569
1570/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001571 * @brief Check whether a node matches specific schema nodeid.
1572 *
1573 * @param[in] exp Parsed nodeid to match.
1574 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001575 * @param[in] exp_ext Extension instance in which @p exp is defined, it means it targets an extension instance.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001576 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1577 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1578 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1579 * @param[in] pnode_mod Compiled @p pnode to-be module.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001580 * @param[in] pnode_ext Extension instance in which @p pnode is defined.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001581 * @return Whether it is a match or not.
1582 */
1583static ly_bool
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001584lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod,
1585 const struct lysp_ext_instance *exp_ext, const struct lysc_node *ctx_node, const struct lysc_node *parent,
1586 const struct lysp_node *pnode, const struct lys_module *pnode_mod, const struct lysc_ext_instance *pnode_ext)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001587{
1588 uint32_t i;
1589 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001590 const char *name = NULL;
1591 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001592
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001593 if (exp_ext && !pnode_ext) {
1594 /* extension instance augment and standard node, will never match */
1595 return 0;
1596 } else if (!exp_ext && pnode_ext) {
1597 /* standard augment and extension instance node, will never match */
1598 return 0;
1599 }
1600
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001601 /* compare last node in the node ID */
1602 i = exp->used - 1;
1603
1604 /* get exp node ID module */
1605 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name, &name_len);
1606 assert(mod);
1607
1608 if (pnode) {
1609 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001610 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001611 return 0;
1612 }
1613 } else {
1614 /* using parent directly */
1615 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1616 return 0;
1617 }
1618 }
1619
1620 /* now compare all the compiled parents */
1621 while (i > 1) {
1622 i -= 2;
1623 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1624
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001625 if (!parent && !pnode_ext) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001626 /* no more parents but path continues */
1627 return 0;
1628 }
1629
1630 /* get exp node ID module */
1631 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1632 &name_len);
1633 assert(mod);
1634
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001635 if (parent) {
1636 /* compare with the parent */
1637 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1638 return 0;
1639 }
1640 } else {
1641 /* compare with the ext instance */
1642 if (!lysp_schema_nodeid_match_ext(&pnode_ext, mod, name, name_len)) {
1643 return 0;
1644 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001645 }
1646 }
1647
1648 if (ctx_node && (ctx_node != parent)) {
1649 /* descendant path has not finished in the context node */
1650 return 0;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001651 } else if (!ctx_node && (parent || pnode_ext)) {
1652 /* some parent/extension was not matched */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001653 return 0;
1654 }
1655
1656 return 1;
1657}
1658
1659void
1660lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1661{
1662 if (aug) {
1663 lyxp_expr_free(ctx, aug->nodeid);
1664
1665 free(aug);
1666 }
1667}
1668
1669void
1670lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1671{
1672 if (dev) {
1673 lyxp_expr_free(ctx, dev->nodeid);
1674 LY_ARRAY_FREE(dev->devs);
1675 LY_ARRAY_FREE(dev->dev_pmods);
1676
1677 free(dev);
1678 }
1679}
1680
1681void
1682lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1683{
1684 if (rfn) {
1685 lyxp_expr_free(ctx, rfn->nodeid);
1686 LY_ARRAY_FREE(rfn->rfns);
1687
1688 free(rfn);
1689 }
1690}
1691
1692void
Michal Vaskoc636ea42022-09-16 10:20:31 +02001693lysp_dev_node_free(struct lysc_ctx *cctx, struct lysp_node *dev_pnode)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001694{
1695 if (!dev_pnode) {
1696 return;
1697 }
1698
1699 switch (dev_pnode->nodetype) {
1700 case LYS_CONTAINER:
1701 ((struct lysp_node_container *)dev_pnode)->child = NULL;
Michal Vaskoec8f4272021-10-27 09:14:03 +02001702 ((struct lysp_node_container *)dev_pnode)->actions = NULL;
1703 ((struct lysp_node_container *)dev_pnode)->notifs = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001704 break;
1705 case LYS_LIST:
1706 ((struct lysp_node_list *)dev_pnode)->child = NULL;
Michal Vaskoec8f4272021-10-27 09:14:03 +02001707 ((struct lysp_node_list *)dev_pnode)->actions = NULL;
1708 ((struct lysp_node_list *)dev_pnode)->notifs = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001709 break;
1710 case LYS_CHOICE:
1711 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1712 break;
1713 case LYS_CASE:
1714 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1715 break;
1716 case LYS_LEAF:
1717 case LYS_LEAFLIST:
1718 case LYS_ANYXML:
1719 case LYS_ANYDATA:
1720 /* no children */
1721 break;
1722 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001723 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001724 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001725 case LYS_RPC:
1726 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001727 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1728 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001729 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001730 case LYS_INPUT:
1731 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001732 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001733 lysp_node_free(&cctx->free_ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001734 free(dev_pnode);
1735 return;
1736 default:
Michal Vaskoc636ea42022-09-16 10:20:31 +02001737 LOGINT(cctx->ctx);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001738 return;
1739 }
1740
Michal Vaskoc636ea42022-09-16 10:20:31 +02001741 lysp_node_free(&cctx->free_ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001742}
1743
1744LY_ERR
1745lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1746 struct lysp_node **dev_pnode, ly_bool *not_supported)
1747{
1748 LY_ERR ret = LY_SUCCESS;
1749 uint32_t i;
1750 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001751 struct lysc_refine *rfn;
1752 struct lysc_deviation *dev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001753
1754 *dev_pnode = NULL;
1755 *not_supported = 0;
1756
Michal Vaskoee057572022-05-26 08:31:52 +02001757 for (i = 0; i < ctx->uses_rfns.count; ) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001758 rfn = ctx->uses_rfns.objs[i];
1759
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001760 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, NULL, rfn->nodeid_ctx_node, parent, pnode,
1761 ctx->cur_mod, ctx->ext)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001762 /* not our target node */
Michal Vaskoee057572022-05-26 08:31:52 +02001763 ++i;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001764 continue;
1765 }
1766
1767 if (!*dev_pnode) {
1768 /* first refine on this node, create a copy first */
Michal Vasko193dacd2022-10-13 08:43:05 +02001769 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, ctx->pmod, pnode, 1, dev_pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001770 }
1771
1772 /* apply all the refines by changing (the copy of) the parsed node */
1773 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001774 LY_CHECK_GOTO(ret = lys_apply_refine(ctx, rfn->rfns[u], rfn->nodeid_pmod, *dev_pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001775 }
1776
1777 /* refine was applied, remove it */
1778 lysc_refine_free(ctx->ctx, rfn);
1779 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1780
Michal Vaskoee057572022-05-26 08:31:52 +02001781 /* refines use relative paths so more may apply to a single node */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001782 }
1783
1784 for (i = 0; i < ctx->devs.count; ++i) {
1785 dev = ctx->devs.objs[i];
1786
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001787 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, NULL, parent, pnode, ctx->cur_mod, ctx->ext)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001788 /* not our target node */
1789 continue;
1790 }
1791
1792 if (dev->not_supported) {
1793 /* it is not supported, no more deviations */
1794 *not_supported = 1;
1795 goto dev_applied;
1796 }
1797
1798 if (!*dev_pnode) {
1799 /* first deviation on this node, create a copy first */
Michal Vasko193dacd2022-10-13 08:43:05 +02001800 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, ctx->pmod, pnode, 1, dev_pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001801 }
1802
1803 /* apply all the deviates by changing (the copy of) the parsed node */
1804 LY_ARRAY_FOR(dev->devs, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001805 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 +02001806 }
1807
1808dev_applied:
1809 /* deviation was applied, remove it */
1810 lysc_deviation_free(ctx->ctx, dev);
1811 ly_set_rm_index(&ctx->devs, i, NULL);
1812
1813 /* all the deviations for one target node are in one structure, we are done */
1814 break;
1815 }
1816
1817cleanup:
1818 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001819 lysp_dev_node_free(ctx, *dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001820 *dev_pnode = NULL;
1821 *not_supported = 0;
1822 }
1823 return ret;
1824}
1825
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001826/**
1827 * @brief Compile augment children.
1828 *
1829 * @param[in] ctx Compile context.
1830 * @param[in] aug_when Parsed augment when to inherit.
1831 * @param[in] aug_flags Parsed augment flags.
1832 * @param[in] child First augment child to compile.
1833 * @param[in] target Target node of the augment.
1834 * @param[in] child_unres_disabled Whether the children are to be put into unres disabled set or not.
1835 * @return LY_SUCCESS on success.
1836 * @return LY_EVALID on failure.
1837 */
1838static LY_ERR
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001839lys_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 +02001840 struct lysc_node *target, ly_bool child_unres_disabled)
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001841{
1842 LY_ERR rc = LY_SUCCESS;
1843 struct lysp_node *pnode;
1844 struct lysc_node *node;
1845 struct lysc_when *when_shared = NULL;
1846 ly_bool enabled, allow_mand = 0;
1847 struct ly_set child_set = {0};
1848 uint32_t i, opt_prev = ctx->compile_opts;
1849
1850 /* check for mandatory nodes
1851 * - new cases augmenting some choice can have mandatory nodes
1852 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1853 */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001854 if (aug_when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001855 allow_mand = 1;
1856 }
1857
1858 LY_LIST_FOR(child, pnode) {
1859 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1860 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1861 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1862 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
1863 LOGVAL(ctx->ctx, LYVE_REFERENCE,
1864 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1865 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1866 rc = LY_EVALID;
1867 goto cleanup;
1868 }
1869
1870 /* compile the children */
1871 if (target->nodetype == LYS_CHOICE) {
1872 LY_CHECK_GOTO(rc = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
1873 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1874 if (target->nodetype == LYS_INPUT) {
1875 ctx->compile_opts |= LYS_COMPILE_RPC_INPUT;
1876 } else {
1877 ctx->compile_opts |= LYS_COMPILE_RPC_OUTPUT;
1878 }
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001879 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, target, aug_flags, &child_set), cleanup);
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001880 } else {
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001881 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, target, aug_flags, &child_set), cleanup);
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001882 }
1883
1884 /* eval if-features again for the rest of this node processing */
1885 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1886 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001887 ctx->compile_opts |= LYS_COMPILE_DISABLED;
1888 }
1889
1890 /* since the augment node is not present in the compiled tree, we need to pass some of its
1891 * statements to all its children */
1892 for (i = 0; i < child_set.count; ++i) {
1893 node = child_set.snodes[i];
1894 if (!allow_mand && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1895 node->flags &= ~LYS_MAND_TRUE;
1896 lys_compile_mandatory_parents(target, 0);
1897 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
1898 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.",
1899 node->name);
1900 rc = LY_EVALID;
1901 goto cleanup;
1902 }
1903
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001904 if (aug_when) {
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001905 /* pass augment's when to all the children */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001906 rc = lys_compile_when(ctx, aug_when, aug_flags, target, lysc_data_node(target), node, &when_shared);
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001907 LY_CHECK_GOTO(rc, cleanup);
1908 }
1909
Michal Vaskoa084fa32022-05-16 11:32:58 +02001910 if (child_unres_disabled) {
Michal Vaskoad0980a2022-05-09 11:43:47 +02001911 /* child is disabled by the augment if-features */
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001912 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
1913 }
1914 }
1915
1916 /* next iter */
1917 ly_set_erase(&child_set, NULL);
1918 ctx->compile_opts = opt_prev;
1919 }
1920
1921cleanup:
1922 ly_set_erase(&child_set, NULL);
1923 ctx->compile_opts = opt_prev;
1924 return rc;
1925}
1926
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001927/**
1928 * @brief Compile the parsed augment connecting it into its target.
1929 *
1930 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1931 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1932 * are already implemented and compiled.
1933 *
1934 * @param[in] ctx Compile context.
1935 * @param[in] aug_p Parsed augment to compile.
1936 * @param[in] target Target node of the augment.
1937 * @return LY_SUCCESS on success.
1938 * @return LY_EVALID on failure.
1939 */
1940static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001941lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001942{
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001943 LY_ERR rc = LY_SUCCESS;
Michal Vaskoa084fa32022-05-16 11:32:58 +02001944 ly_bool enabled, child_unres_disabled = 0;
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001945 uint32_t opt_prev = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001946
Michal Vasko95f736c2022-06-08 12:03:31 +02001947 assert(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF));
1948
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001949 /* nodetype checks */
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001950 if (aug_p->actions && !lysc_node_actions_p(target)) {
1951 LOGVAL(ctx->ctx, LYVE_REFERENCE,
1952 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
1953 lys_nodetype2str(target->nodetype), aug_p->actions->name);
1954 rc = LY_EVALID;
1955 goto cleanup;
1956 }
1957 if (aug_p->notifs && !lysc_node_notifs_p(target)) {
1958 LOGVAL(ctx->ctx, LYVE_REFERENCE,
1959 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
1960 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
1961 rc = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001962 goto cleanup;
1963 }
1964
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001965 /* augment if-features */
1966 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, aug_p->iffeatures, &enabled), cleanup);
1967 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001968 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vaskoa084fa32022-05-16 11:32:58 +02001969 child_unres_disabled = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001970 }
1971
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001972 /* augment children */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001973 rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, aug_p->child, target, child_unres_disabled);
1974 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001975
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001976 /* augment actions */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001977 rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, (struct lysp_node *)aug_p->actions, target,
1978 child_unres_disabled);
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001979 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001980
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001981 /* augment notifications */
Michal Vasko0b50f6b2022-10-05 15:07:55 +02001982 rc = lys_compile_augment_children(ctx, aug_p->when, aug_p->flags, (struct lysp_node *)aug_p->notifs, target,
1983 child_unres_disabled);
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001984 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001985
Michal Vasko193dacd2022-10-13 08:43:05 +02001986 /* compile extensions into the target */
1987 COMPILE_EXTS_GOTO(ctx, aug_p->exts, target->exts, target, rc, cleanup);
1988
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001989cleanup:
Michal Vasko7c565922021-06-10 14:58:27 +02001990 ctx->compile_opts = opt_prev;
Michal Vaskobbb3dee2022-05-09 10:50:28 +02001991 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001992}
1993
1994LY_ERR
1995lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1996{
1997 LY_ERR ret = LY_SUCCESS;
1998 struct lys_module *orig_mod = ctx->cur_mod;
1999 struct lysp_module *orig_pmod = ctx->pmod;
2000 uint32_t i;
2001 char orig_path[LYSC_CTX_BUFSIZE];
2002 struct lysc_augment *aug;
2003
2004 /* uses augments */
2005 for (i = 0; i < ctx->uses_augs.count; ) {
2006 aug = ctx->uses_augs.objs[i];
2007
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002008 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->ext, aug->nodeid_ctx_node, node, NULL, NULL,
2009 ctx->ext)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002010 /* not our target node */
2011 ++i;
2012 continue;
2013 }
2014
Michal Vaskob8df5762021-01-12 15:15:53 +01002015 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002016 lysc_update_path(ctx, NULL, "{augment}");
2017 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01002018 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01002019
2020 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002021 ret = lys_compile_augment(ctx, aug->aug_p, node);
2022 lysc_update_path(ctx, NULL, NULL);
2023 lysc_update_path(ctx, NULL, NULL);
2024 LY_CHECK_GOTO(ret, cleanup);
2025
Michal Vaskoc75f2042021-06-08 14:57:03 +02002026 /* augment was applied, remove it (index and the whole set may have changed because other augments
2027 * could have been applied) */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002028 ly_set_rm(&ctx->uses_augs, aug, NULL);
2029 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02002030 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002031 }
2032
2033 /* top-level augments */
2034 for (i = 0; i < ctx->augs.count; ) {
2035 aug = ctx->augs.objs[i];
2036
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002037 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, aug->ext, NULL, node, NULL, NULL, ctx->ext)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002038 /* not our target node */
2039 ++i;
2040 continue;
2041 }
2042
Michal Vaskob8df5762021-01-12 15:15:53 +01002043 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002044 strcpy(orig_path, ctx->path);
2045 ctx->path_len = 1;
Michal Vasko28fe5f62021-03-03 16:37:39 +01002046 ctx->cur_mod = aug->aug_pmod->mod;
2047 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vasko1ade6f12021-04-19 11:32:45 +02002048 lysc_update_path(ctx, NULL, "{augment}");
2049 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01002050
2051 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002052 ret = lys_compile_augment(ctx, aug->aug_p, node);
2053 strcpy(ctx->path, orig_path);
2054 ctx->path_len = strlen(ctx->path);
2055 LY_CHECK_GOTO(ret, cleanup);
2056
2057 /* augment was applied, remove it */
2058 ly_set_rm(&ctx->augs, aug, NULL);
2059 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02002060 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002061 }
2062
2063cleanup:
2064 ctx->cur_mod = orig_mod;
2065 ctx->pmod = orig_pmod;
2066 return ret;
2067}
2068
2069/**
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002070 * @brief Prepare an absolute-nodeid augment to be applied during data nodes compilation.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002071 *
2072 * @param[in] ctx Compile context.
2073 * @param[in] aug_p Parsed augment to be applied.
2074 * @param[in] pmod Both current and prefix module for @p aug_p.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002075 * @param[in] ext Extension instance in case @p aug_p is defined in one.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002076 * @return LY_ERR value.
2077 */
2078static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002079lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, const struct lysp_module *pmod,
2080 const struct lysp_ext_instance *ext)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002081{
2082 LY_ERR ret = LY_SUCCESS;
2083 struct lyxp_expr *exp = NULL;
2084 struct lysc_augment *aug;
2085 const struct lys_module *mod;
2086
2087 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2088 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
2089 LY_CHECK_GOTO(ret, cleanup);
2090
2091 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2092 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2093 if (mod != ctx->cur_mod) {
2094 /* augment for another module, ignore */
2095 goto cleanup;
2096 }
2097
2098 /* allocate new compiled augment and store it in the set */
2099 aug = calloc(1, sizeof *aug);
2100 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2101 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
2102
2103 aug->nodeid = exp;
2104 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01002105 aug->aug_pmod = pmod;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002106 aug->ext = ext;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002107 aug->aug_p = aug_p;
2108
2109cleanup:
2110 lyxp_expr_free(ctx->ctx, exp);
2111 return ret;
2112}
2113
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002114/**
2115 * @brief Prepare all top-level augments and extension instance augments to be applied during data nodes compilation.
2116 *
2117 * @param[in] ctx Compile context.
2118 * @param[in] pmod Parsed mod to use.
2119 * @return LY_ERR value.
2120 */
2121static LY_ERR
2122lys_precompile_own_augments_mod(struct lysc_ctx *ctx, const struct lysp_module *pmod)
2123{
2124 LY_ARRAY_COUNT_TYPE u, v;
2125 struct lysp_node_augment *aug_p;
2126
2127 /* module */
2128 LY_LIST_FOR(pmod->augments, aug_p) {
2129 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug_p, pmod, NULL));
2130 }
2131
2132 /* parsed extension instances */
2133 LY_ARRAY_FOR(pmod->exts, u) {
2134 aug_p = NULL;
2135 LY_ARRAY_FOR(pmod->exts[u].substmts, v) {
2136 if (pmod->exts[u].substmts[v].stmt == LY_STMT_AUGMENT) {
2137 aug_p = *(struct lysp_node_augment **)pmod->exts[u].substmts[v].storage;
2138 break;
2139 }
2140 }
2141 if (!aug_p) {
2142 continue;
2143 }
2144
2145 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug_p, pmod, &pmod->exts[u]));
2146 }
2147
2148 return LY_SUCCESS;
2149}
2150
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002151LY_ERR
2152lys_precompile_own_augments(struct lysc_ctx *ctx)
2153{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002154 LY_ARRAY_COUNT_TYPE u, v;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002155 const struct lys_module *aug_mod;
2156 const struct lysp_module *submod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002157
2158 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002159 aug_mod = ctx->cur_mod->augmented_by[u];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002160
2161 /* collect all module augments */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002162 LY_CHECK_RET(lys_precompile_own_augments_mod(ctx, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002163
2164 /* collect all submodules augments */
2165 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002166 submod = (struct lysp_module *)aug_mod->parsed->includes[v].submodule;
2167
2168 LY_CHECK_RET(lys_precompile_own_augments_mod(ctx, submod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002169 }
2170 }
2171
2172 return LY_SUCCESS;
2173}
2174
2175/**
2176 * @brief Prepare a deviation to be applied during data nodes compilation.
2177 *
2178 * @param[in] ctx Compile context.
2179 * @param[in] dev_p Parsed deviation to be applied.
2180 * @param[in] pmod Both current and prefix module for @p dev_p.
2181 * @return LY_ERR value.
2182 */
2183static LY_ERR
2184lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2185{
2186 LY_ERR ret = LY_SUCCESS;
2187 struct lysc_deviation *dev = NULL;
2188 struct lyxp_expr *exp = NULL;
2189 struct lysp_deviation **new_dev;
2190 const struct lys_module *mod;
2191 const struct lysp_module **new_dev_pmod;
2192 uint32_t i;
2193
2194 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2195 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2196 LY_CHECK_GOTO(ret, cleanup);
2197
2198 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2199 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2200 if (mod != ctx->cur_mod) {
2201 /* deviation for another module, ignore */
2202 goto cleanup;
2203 }
2204
2205 /* try to find the node in already compiled deviations */
2206 for (i = 0; i < ctx->devs.count; ++i) {
2207 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2208 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2209 dev = ctx->devs.objs[i];
2210 break;
2211 }
2212 }
2213
2214 if (!dev) {
2215 /* allocate new compiled deviation */
2216 dev = calloc(1, sizeof *dev);
2217 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2218 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2219
2220 dev->nodeid = exp;
2221 exp = NULL;
2222 }
2223
2224 /* add new parsed deviation structure */
2225 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2226 *new_dev = dev_p;
2227 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2228 *new_dev_pmod = pmod;
2229
2230cleanup:
2231 lyxp_expr_free(ctx->ctx, exp);
2232 return ret;
2233}
2234
2235LY_ERR
2236lys_precompile_own_deviations(struct lysc_ctx *ctx)
2237{
2238 LY_ARRAY_COUNT_TYPE u, v, w;
Michal Vaskoe8220db2021-06-02 15:39:05 +02002239 struct lys_module *orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002240 const struct lys_module *dev_mod;
2241 struct lysc_deviation *dev;
2242 struct lysp_deviate *d;
2243 int not_supported;
2244 uint32_t i;
2245
2246 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2247 dev_mod = ctx->cur_mod->deviated_by[u];
2248
2249 /* compile all module deviations */
2250 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2251 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2252 }
2253
2254 /* compile all submodules deviations */
2255 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2256 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2257 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2258 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2259 }
2260 }
2261 }
2262
2263 /* set not-supported flags for all the deviations */
2264 for (i = 0; i < ctx->devs.count; ++i) {
2265 dev = ctx->devs.objs[i];
2266 not_supported = 0;
2267
2268 LY_ARRAY_FOR(dev->devs, u) {
2269 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2270 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2271 not_supported = 1;
2272 break;
2273 }
2274 }
2275 if (not_supported) {
2276 break;
2277 }
2278 }
2279 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Michal Vaskoe8220db2021-06-02 15:39:05 +02002280 orig_cur_mod = ctx->cur_mod;
2281 ctx->cur_mod = dev->dev_pmods[u]->mod;
2282 lysc_update_path(ctx, NULL, "{deviation}");
2283 lysc_update_path(ctx, NULL, dev->nodeid->expr);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002284 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002285 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
Michal Vaskoe8220db2021-06-02 15:39:05 +02002286 lysc_update_path(ctx, NULL, NULL);
2287 lysc_update_path(ctx, NULL, NULL);
2288 ctx->cur_mod = orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002289 return LY_EVALID;
2290 }
2291
2292 dev->not_supported = not_supported;
2293 }
2294
2295 return LY_SUCCESS;
2296}
2297
2298/**
2299 * @brief Add a module reference into an array, checks for duplicities.
2300 *
2301 * @param[in] ctx Compile context.
2302 * @param[in] mod Module reference to add.
2303 * @param[in,out] mod_array Module sized array to add to.
2304 * @return LY_ERR value.
2305 */
2306static LY_ERR
2307lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2308{
2309 LY_ARRAY_COUNT_TYPE u;
2310 struct lys_module **new_mod;
2311
2312 LY_ARRAY_FOR(*mod_array, u) {
2313 if ((*mod_array)[u] == mod) {
2314 /* already there */
2315 return LY_EEXIST;
2316 }
2317 }
2318
2319 /* add the new module ref */
2320 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2321 *new_mod = mod;
2322
2323 return LY_SUCCESS;
2324}
2325
Michal Vasko1ccbf542021-04-19 11:35:00 +02002326/**
2327 * @brief Check whether all modules in a set are implemented.
2328 *
2329 * @param[in] mod_set Module set to check.
2330 * @return Whether all modules are implemented or not.
2331 */
2332static ly_bool
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002333lys_precompile_mod_set_is_all_implemented(const struct ly_set *mod_set)
Michal Vasko1ccbf542021-04-19 11:35:00 +02002334{
2335 uint32_t i;
2336 const struct lys_module *mod;
2337
2338 for (i = 0; i < mod_set->count; ++i) {
2339 mod = mod_set->objs[i];
2340 if (!mod->implemented) {
2341 return 0;
2342 }
2343 }
2344
2345 return 1;
2346}
2347
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002348/**
2349 * @brief Add references to target modules of top-level augments, deviations, and augments in extension instances
2350 * in a module and all its submodules.
2351 *
2352 * @param[in] pmod Module to process.
2353 * @param[in,out] mod_set Module set to add referenced modules into.
2354 * @return LY_SUCCESS on success.
2355 * @return LY_ERR on error.
2356 */
2357static LY_ERR
2358lys_precompile_mod_augments_deviations(struct lysp_module *pmod, struct ly_set *mod_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002359{
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002360 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002361 LY_ARRAY_COUNT_TYPE u, v;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002362 struct lysc_ctx ctx;
Michal Vasko65333882021-06-10 14:12:16 +02002363 struct lys_module *m;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002364 struct lysp_node_augment *aug;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002365 struct ly_set set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002366
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002367 LYSC_CTX_INIT_PMOD(ctx, pmod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002368
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002369 LY_LIST_FOR(pmod->augments, aug) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002370 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002371 lysc_update_path(&ctx, NULL, "{augment}");
2372 lysc_update_path(&ctx, NULL, aug->nodeid);
2373 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2374 lysc_update_path(&ctx, NULL, NULL);
2375 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002376 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002377
Michal Vasko1ccbf542021-04-19 11:35:00 +02002378 /* add this module into the target module augmented_by, if not there and implemented */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002379 if ((lys_array_add_mod_ref(&ctx, pmod->mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002380 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002381 LY_CHECK_GOTO(ret = ly_set_merge(mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002382 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002383 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002384 }
2385
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002386 LY_ARRAY_FOR(pmod->deviations, u) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002387 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002388 lysc_update_path(&ctx, NULL, "{deviation}");
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002389 lysc_update_path(&ctx, NULL, pmod->deviations[u].nodeid);
2390 ret = lys_nodeid_mod_check(&ctx, pmod->deviations[u].nodeid, 1, &set, NULL, &m);
Michal Vasko65333882021-06-10 14:12:16 +02002391 lysc_update_path(&ctx, NULL, NULL);
2392 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002393 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002394
Michal Vasko1ccbf542021-04-19 11:35:00 +02002395 /* add this module into the target module deviated_by, if not there and implemented */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002396 if ((lys_array_add_mod_ref(&ctx, pmod->mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002397 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002398 LY_CHECK_GOTO(ret = ly_set_merge(mod_set, &set, 0, NULL), cleanup);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002399 }
2400 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002401 }
2402
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002403 LY_ARRAY_FOR(pmod->exts, u) {
2404 aug = NULL;
2405 LY_ARRAY_FOR(pmod->exts[u].substmts, v) {
2406 if (pmod->exts[u].substmts[v].stmt == LY_STMT_AUGMENT) {
2407 aug = *(struct lysp_node_augment **)pmod->exts[u].substmts[v].storage;
2408 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002409 }
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002410 }
2411 if (!aug) {
2412 continue;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002413 }
2414
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002415 /* get target module */
2416 lysc_update_path(&ctx, NULL, "{ext-augment}");
2417 lysc_update_path(&ctx, NULL, aug->nodeid);
2418 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2419 lysc_update_path(&ctx, NULL, NULL);
2420 lysc_update_path(&ctx, NULL, NULL);
2421 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002422
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002423 /* add this module into the target module augmented_by, if not there and implemented */
2424 if ((lys_array_add_mod_ref(&ctx, pmod->mod, &m->augmented_by) != LY_EEXIST) ||
2425 !lys_precompile_mod_set_is_all_implemented(&set)) {
2426 LY_CHECK_GOTO(ret = ly_set_merge(mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002427 }
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002428 ly_set_erase(&set, NULL);
2429 }
2430
2431cleanup:
2432 ly_set_erase(&set, NULL);
2433 return ret;
2434}
2435
2436LY_ERR
2437lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres)
2438{
2439 LY_ERR ret = LY_SUCCESS, r;
2440 LY_ARRAY_COUNT_TYPE u;
2441 struct lys_module *m;
2442 struct lysp_module *submod;
2443 const char **imp_f, *all_f[] = {"*", NULL};
2444 uint32_t i;
2445 struct ly_set mod_set = {0};
2446
2447 /* module */
2448 LY_CHECK_GOTO(ret = lys_precompile_mod_augments_deviations(mod->parsed, &mod_set), cleanup);
2449
2450 /* submodules */
2451 LY_ARRAY_FOR(mod->parsed->includes, u) {
2452 submod = (struct lysp_module *)mod->parsed->includes[u].submodule;
2453 LY_CHECK_GOTO(ret = lys_precompile_mod_augments_deviations(submod, &mod_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002454 }
2455
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002456 for (i = 0; i < mod_set.count; ++i) {
2457 m = mod_set.objs[i];
Michal Vasko1ccbf542021-04-19 11:35:00 +02002458
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002459 if (m == mod) {
2460 /* will be applied normally later */
2461 continue;
2462 }
2463
2464 /* we do not actually need the target modules compiled with out amends, they just need to be implemented
2465 * not compiled yet and marked for compilation */
2466
2467 if (!m->implemented) {
2468 /* implement the target module */
Michal Vaskoc56d6372021-10-19 12:29:00 +02002469 imp_f = (mod->ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL;
2470 r = lys_implement(m, imp_f, unres);
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002471 if (r == LY_ERECOMPILE) {
2472 /* implement all the modules right away to save possible later recompilation */
2473 ret = r;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002474 continue;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002475 } else if (r) {
2476 /* error */
2477 ret = r;
Michal Vasko65333882021-06-10 14:12:16 +02002478 goto cleanup;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002479 }
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002480 } else if (m->compiled) {
2481 /* target module was already compiled without our amends (augment/deviation), we need to recompile it */
2482 m->to_compile = 1;
2483 ret = LY_ERECOMPILE;
2484 continue;
2485 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002486 }
2487
Michal Vasko1ccbf542021-04-19 11:35:00 +02002488cleanup:
Michal Vasko1ccbf542021-04-19 11:35:00 +02002489 ly_set_erase(&mod_set, NULL);
2490 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002491}
2492
2493void
2494lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2495{
2496 uint32_t i;
2497 LY_ARRAY_COUNT_TYPE u, count;
2498 struct lys_module *m;
2499
2500 for (i = 0; i < ctx->list.count; ++i) {
2501 m = ctx->list.objs[i];
2502
2503 if (m->augmented_by) {
2504 count = LY_ARRAY_COUNT(m->augmented_by);
2505 for (u = 0; u < count; ++u) {
2506 if (m->augmented_by[u] == mod) {
2507 /* keep the order */
2508 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002509 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u - 1) * sizeof *m->augmented_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002510 }
2511 LY_ARRAY_DECREMENT(m->augmented_by);
2512 break;
2513 }
2514 }
2515 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2516 LY_ARRAY_FREE(m->augmented_by);
2517 m->augmented_by = NULL;
2518 }
2519 }
2520
2521 if (m->deviated_by) {
2522 count = LY_ARRAY_COUNT(m->deviated_by);
2523 for (u = 0; u < count; ++u) {
2524 if (m->deviated_by[u] == mod) {
2525 /* keep the order */
2526 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002527 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u - 1) * sizeof *m->deviated_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002528 }
2529 LY_ARRAY_DECREMENT(m->deviated_by);
2530 break;
2531 }
2532 }
2533 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2534 LY_ARRAY_FREE(m->deviated_by);
2535 m->deviated_by = NULL;
2536 }
2537 }
2538 }
2539}