blob: cf509c776933cef0685fd2467758ec880a00994a [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"
Radek Krejci77114102021-03-10 15:21:57 +010029#include "plugins_exts_compile.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020030#include "schema_compile.h"
31#include "schema_compile_node.h"
Michal Vasko29dd11e2020-11-23 16:52:22 +010032#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020033#include "set.h"
34#include "tree.h"
Michal Vasko899c7ce2022-02-18 09:18:37 +010035#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010036#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "tree_schema.h"
38#include "tree_schema_internal.h"
39#include "xpath.h"
40
41static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest,
42 size_t nametest_len, const struct lysp_module *mod, const char **name, size_t *name_len);
43
Michal Vasko1ccbf542021-04-19 11:35:00 +020044/**
45 * @brief Check the syntax of a node-id and collect all the referenced modules.
46 *
47 * @param[in] ctx Compile context.
48 * @param[in] nodeid Node-id to check.
49 * @param[in] abs Whether @p nodeid is absolute.
50 * @param[in,out] mod_set Set to add referenced modules into.
51 * @param[out] expr Optional node-id parsed into an expression.
52 * @param[out] target_mod Optional target module of the node-id.
53 * @return LY_ERR value.
54 */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020055static LY_ERR
Michal Vasko1ccbf542021-04-19 11:35:00 +020056lys_nodeid_mod_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct ly_set *mod_set,
57 struct lyxp_expr **expr, struct lys_module **target_mod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020058{
59 LY_ERR ret = LY_SUCCESS;
60 struct lyxp_expr *e = NULL;
61 struct lys_module *tmod = NULL, *mod;
62 const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid";
63 uint32_t i;
64
65 /* parse */
66 ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e);
67 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010068 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020069 nodeid_type, nodeid);
70 ret = LY_EVALID;
71 goto cleanup;
72 }
73
74 if (abs) {
75 /* absolute schema nodeid */
76 i = 0;
77 } else {
78 /* descendant schema nodeid */
79 if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010080 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020081 nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]);
82 ret = LY_EVALID;
83 goto cleanup;
84 }
85 i = 1;
86 }
87
88 /* check all the tokens */
89 for ( ; i < e->used; i += 2) {
90 if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010091 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020092 nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]);
93 ret = LY_EVALID;
94 goto cleanup;
95 } else if (e->used == i + 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010096 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020097 "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr);
98 ret = LY_EVALID;
99 goto cleanup;
100 } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100101 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200102 nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
103 ret = LY_EVALID;
104 goto cleanup;
105 } else if (abs) {
106 mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1],
107 e->tok_len[i + 1], ctx->pmod, NULL, NULL);
108 LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup);
109
110 /* only keep the first module */
111 if (!tmod) {
112 tmod = mod;
113 }
114
Michal Vasko1ccbf542021-04-19 11:35:00 +0200115 /* store the referenced module */
116 LY_CHECK_GOTO(ret = ly_set_add(mod_set, mod, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200117 }
118 }
119
120cleanup:
121 if (ret || !expr) {
122 lyxp_expr_free(ctx->ctx, e);
123 e = NULL;
124 }
125 if (expr) {
126 *expr = ret ? NULL : e;
127 }
128 if (target_mod) {
129 *target_mod = ret ? NULL : tmod;
130 }
131 return ret;
132}
133
134/**
135 * @brief Check whether 2 schema nodeids match.
136 *
137 * @param[in] ctx libyang context.
138 * @param[in] exp1 First schema nodeid.
139 * @param[in] exp1p_mod Module of @p exp1 nodes without any prefix.
140 * @param[in] exp2 Second schema nodeid.
141 * @param[in] exp2_pmod Module of @p exp2 nodes without any prefix.
142 * @return Whether the schema nodeids match or not.
143 */
144static ly_bool
145lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lysp_module *exp1_pmod,
146 const struct lyxp_expr *exp2, const struct lysp_module *exp2_pmod)
147{
148 uint32_t i;
149 const struct lys_module *mod1, *mod2;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100150 const char *name1 = NULL, *name2 = NULL;
151 size_t name1_len = 0, name2_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200152
153 if (exp1->used != exp2->used) {
154 return 0;
155 }
156
157 for (i = 0; i < exp1->used; ++i) {
158 assert(exp1->tokens[i] == exp2->tokens[i]);
159
160 if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) {
161 /* check modules of all the nodes in the node ID */
162 mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_pmod,
163 &name1, &name1_len);
164 assert(mod1);
165 mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_pmod,
166 &name2, &name2_len);
167 assert(mod2);
168
169 /* compare modules */
170 if (mod1 != mod2) {
171 return 0;
172 }
173
174 /* compare names */
175 if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) {
176 return 0;
177 }
178 }
179 }
180
181 return 1;
182}
183
184LY_ERR
185lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node)
186{
187 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200188 struct lyxp_expr *exp = NULL;
189 struct lysc_augment *aug;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100190 struct lysp_node_augment *aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200191 struct lysc_refine *rfn;
192 struct lysp_refine **new_rfn;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100193 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200194 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +0200195 struct ly_set mod_set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200196
Radek Krejci2a9fc652021-01-22 17:44:34 +0100197 LY_LIST_FOR(uses_p->augments, aug_p) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200198 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +0100199 lysc_update_path(ctx, NULL, aug_p->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200200
201 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200202 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 +0200203
204 /* allocate new compiled augment and store it in the set */
205 aug = calloc(1, sizeof *aug);
206 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
207 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, 1, NULL), cleanup);
208
209 aug->nodeid = exp;
210 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +0100211 aug->aug_pmod = ctx->pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200212 aug->nodeid_ctx_node = ctx_node;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100213 aug->aug_p = aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200214
215 lysc_update_path(ctx, NULL, NULL);
216 lysc_update_path(ctx, NULL, NULL);
217 }
218
219 LY_ARRAY_FOR(uses_p->refines, u) {
220 lysc_update_path(ctx, NULL, "{refine}");
221 lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid);
222
223 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200224 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 +0200225
226 /* try to find the node in already compiled refines */
227 rfn = NULL;
228 for (i = 0; i < ctx->uses_rfns.count; ++i) {
229 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->pmod, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid,
230 ctx->pmod)) {
231 rfn = ctx->uses_rfns.objs[i];
232 break;
233 }
234 }
235
236 if (!rfn) {
237 /* allocate new compiled refine */
238 rfn = calloc(1, sizeof *rfn);
239 LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
240 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, 1, NULL), cleanup);
241
242 rfn->nodeid = exp;
243 exp = NULL;
Michal Vasko7d3708f2021-02-03 10:50:24 +0100244 rfn->nodeid_pmod = ctx->cur_mod->parsed;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200245 rfn->nodeid_ctx_node = ctx_node;
Michal Vaskod8655722021-01-12 15:20:36 +0100246 rfn->uses_p = uses_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200247 } else {
248 /* just free exp */
249 lyxp_expr_free(ctx->ctx, exp);
250 exp = NULL;
251 }
252
253 /* add new parsed refine structure */
254 LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup);
255 *new_rfn = &uses_p->refines[u];
256
257 lysc_update_path(ctx, NULL, NULL);
258 lysc_update_path(ctx, NULL, NULL);
259 }
260
261cleanup:
Michal Vasko1ccbf542021-04-19 11:35:00 +0200262 if (ret) {
263 lysc_update_path(ctx, NULL, NULL);
264 lysc_update_path(ctx, NULL, NULL);
265 }
266 /* should include only this module, will fail later if not */
267 ly_set_erase(&mod_set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200268 lyxp_expr_free(ctx->ctx, exp);
269 return ret;
270}
271
272static LY_ERR
Michal Vasko899c7ce2022-02-18 09:18:37 +0100273lysp_ext_children_dup(const struct ly_ctx *ctx, struct lysp_stmt **child, const struct lysp_stmt *orig_child)
274{
Michal Vasko4316c9d2022-02-21 10:00:10 +0100275 struct lysp_stmt *ch;
276
Michal Vasko899c7ce2022-02-18 09:18:37 +0100277 LY_LIST_FOR(orig_child, orig_child) {
278 /* new child */
279 if (!*child) {
Michal Vasko4316c9d2022-02-21 10:00:10 +0100280 *child = ch = calloc(1, sizeof *ch);
281 LY_CHECK_ERR_RET(!ch, LOGMEM(ctx), LY_EMEM);
Michal Vasko899c7ce2022-02-18 09:18:37 +0100282 } else {
Michal Vasko4316c9d2022-02-21 10:00:10 +0100283 ch->next = calloc(1, sizeof *ch);
284 LY_CHECK_ERR_RET(!ch->next, LOGMEM(ctx), LY_EMEM);
285 ch = ch->next;
Michal Vasko899c7ce2022-02-18 09:18:37 +0100286 }
287
288 /* fill */
Michal Vasko4316c9d2022-02-21 10:00:10 +0100289 DUP_STRING_RET(ctx, orig_child->stmt, ch->stmt);
290 ch->flags = orig_child->flags;
291 DUP_STRING_RET(ctx, orig_child->arg, ch->arg);
292 ch->format = orig_child->format;
293 LY_CHECK_RET(ly_dup_prefix_data(ctx, orig_child->format, orig_child->prefix_data, &(ch->prefix_data)));
294 ch->kw = orig_child->kw;
Michal Vasko899c7ce2022-02-18 09:18:37 +0100295
296 /* recursive children */
Michal Vasko4316c9d2022-02-21 10:00:10 +0100297 LY_CHECK_RET(lysp_ext_children_dup(ctx, &ch->child, orig_child->child));
Michal Vasko899c7ce2022-02-18 09:18:37 +0100298 }
299
300 return LY_SUCCESS;
301}
302
303static LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200304lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext)
305{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200306 *ext = *orig_ext;
Michal Vasko899c7ce2022-02-18 09:18:37 +0100307 DUP_STRING_RET(ctx, orig_ext->name, ext->name);
308 DUP_STRING_RET(ctx, orig_ext->argument, ext->argument);
309 ext->parsed = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200310
Michal Vasko899c7ce2022-02-18 09:18:37 +0100311 ext->child = NULL;
312 LY_CHECK_RET(lysp_ext_children_dup(ctx, &ext->child, orig_ext->child));
313
314 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200315}
316
317static LY_ERR
318lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr)
319{
320 LY_ERR ret = LY_SUCCESS;
321
322 if (orig_restr) {
323 DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret);
324 restr->arg.mod = orig_restr->arg.mod;
325 DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret);
326 DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret);
327 DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret);
328 DUP_STRING(ctx, orig_restr->ref, restr->ref, ret);
329 DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup);
330 }
331
332 return ret;
333}
334
335static LY_ERR
336lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str)
337{
338 LY_ERR ret = LY_SUCCESS;
339
340 DUP_STRING(ctx, *orig_str, *str, ret);
341
342 return ret;
343}
344
345LY_ERR
346lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname)
347{
348 LY_ERR ret = LY_SUCCESS;
349
350 if (!orig_qname->str) {
351 return LY_SUCCESS;
352 }
353
354 DUP_STRING(ctx, orig_qname->str, qname->str, ret);
355 assert(orig_qname->mod);
356 qname->mod = orig_qname->mod;
357
358 return ret;
359}
360
361static LY_ERR
362lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm)
363{
364 LY_ERR ret = LY_SUCCESS;
365
366 DUP_STRING(ctx, orig_enm->name, enm->name, ret);
367 DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret);
368 DUP_STRING(ctx, orig_enm->ref, enm->ref, ret);
369 enm->value = orig_enm->value;
370 DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup);
371 DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup);
372 enm->flags = orig_enm->flags;
373
374 return ret;
375}
376
377static LY_ERR
378lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type)
379{
380 LY_ERR ret = LY_SUCCESS;
381
Michal Vaskoea868242021-06-21 09:28:32 +0200382 /* array macros read previous data so we must zero it */
383 memset(type, 0, sizeof *type);
384
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200385 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
386
387 if (orig_type->range) {
388 type->range = calloc(1, sizeof *type->range);
389 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
390 LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range));
391 }
392
393 if (orig_type->length) {
394 type->length = calloc(1, sizeof *type->length);
395 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
396 LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length));
397 }
398
399 DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup);
400 DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup);
401 DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup);
402 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done);
403 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
404 DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup);
405 DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup);
406
407 type->pmod = orig_type->pmod;
408 type->compiled = orig_type->compiled;
409
410 type->fraction_digits = orig_type->fraction_digits;
411 type->require_instance = orig_type->require_instance;
412 type->flags = orig_type->flags;
413
414done:
415 return ret;
416}
417
418static LY_ERR
419lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when)
420{
421 LY_ERR ret = LY_SUCCESS;
422
423 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
424 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
425 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
426 DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup);
427
428 return ret;
429}
430
431static LY_ERR
432lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
433{
434 LY_ERR ret = LY_SUCCESS;
435
436 node->parent = NULL;
437 node->nodetype = orig->nodetype;
438 node->flags = orig->flags;
439 node->next = NULL;
440 DUP_STRING(ctx, orig->name, node->name, ret);
441 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
442 DUP_STRING(ctx, orig->ref, node->ref, ret);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200443 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
444 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
445
446 return ret;
447}
448
Radek Krejci9a3823e2021-01-27 20:26:46 +0100449#define DUP_PWHEN(CTX, ORIG, NEW) \
450 if (ORIG) { \
451 NEW = calloc(1, sizeof *NEW); \
452 LY_CHECK_ERR_RET(!NEW, LOGMEM(CTX), LY_EMEM); \
453 LY_CHECK_RET(lysp_when_dup(CTX, NEW, ORIG)); \
454 }
455
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200456static LY_ERR
457lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
458{
459 LY_ERR ret = LY_SUCCESS;
460 struct lysp_node_container *cont;
461 const struct lysp_node_container *orig_cont;
462 struct lysp_node_leaf *leaf;
463 const struct lysp_node_leaf *orig_leaf;
464 struct lysp_node_leaflist *llist;
465 const struct lysp_node_leaflist *orig_llist;
466 struct lysp_node_list *list;
467 const struct lysp_node_list *orig_list;
468 struct lysp_node_choice *choice;
469 const struct lysp_node_choice *orig_choice;
470 struct lysp_node_case *cas;
471 const struct lysp_node_case *orig_cas;
472 struct lysp_node_anydata *any;
473 const struct lysp_node_anydata *orig_any;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100474 struct lysp_node_action *action;
475 const struct lysp_node_action *orig_action;
476 struct lysp_node_action_inout *action_inout;
477 const struct lysp_node_action_inout *orig_action_inout;
478 struct lysp_node_notif *notif;
479 const struct lysp_node_notif *orig_notif;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200480
Radek Krejci2a9fc652021-01-22 17:44:34 +0100481 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA |
482 LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200483
484 /* common part */
485 LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig));
486
487 /* specific part */
488 switch (node->nodetype) {
489 case LYS_CONTAINER:
490 cont = (struct lysp_node_container *)node;
491 orig_cont = (const struct lysp_node_container *)orig;
492
Radek Krejci9a3823e2021-01-27 20:26:46 +0100493 DUP_PWHEN(ctx, orig_cont->when, cont->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200494 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
495 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
496 /* we do not need the rest */
497 break;
498 case LYS_LEAF:
499 leaf = (struct lysp_node_leaf *)node;
500 orig_leaf = (const struct lysp_node_leaf *)orig;
501
Radek Krejci9a3823e2021-01-27 20:26:46 +0100502 DUP_PWHEN(ctx, orig_leaf->when, leaf->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200503 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
504 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
505 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
506 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
507 break;
508 case LYS_LEAFLIST:
509 llist = (struct lysp_node_leaflist *)node;
510 orig_llist = (const struct lysp_node_leaflist *)orig;
511
Radek Krejci9a3823e2021-01-27 20:26:46 +0100512 DUP_PWHEN(ctx, orig_llist->when, llist->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200513 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
514 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
515 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
516 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
517 llist->min = orig_llist->min;
518 llist->max = orig_llist->max;
519 break;
520 case LYS_LIST:
521 list = (struct lysp_node_list *)node;
522 orig_list = (const struct lysp_node_list *)orig;
523
Radek Krejci9a3823e2021-01-27 20:26:46 +0100524 DUP_PWHEN(ctx, orig_list->when, list->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200525 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
526 DUP_STRING(ctx, orig_list->key, list->key, ret);
527 /* we do not need these arrays */
528 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
529 list->min = orig_list->min;
530 list->max = orig_list->max;
531 break;
532 case LYS_CHOICE:
533 choice = (struct lysp_node_choice *)node;
534 orig_choice = (const struct lysp_node_choice *)orig;
535
Radek Krejci9a3823e2021-01-27 20:26:46 +0100536 DUP_PWHEN(ctx, orig_choice->when, choice->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200537 /* we do not need children */
538 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
539 break;
540 case LYS_CASE:
541 cas = (struct lysp_node_case *)node;
542 orig_cas = (const struct lysp_node_case *)orig;
543
Radek Krejci9a3823e2021-01-27 20:26:46 +0100544 DUP_PWHEN(ctx, orig_cas->when, cas->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200545 /* we do not need children */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200546 break;
547 case LYS_ANYDATA:
548 case LYS_ANYXML:
549 any = (struct lysp_node_anydata *)node;
550 orig_any = (const struct lysp_node_anydata *)orig;
551
Radek Krejci9a3823e2021-01-27 20:26:46 +0100552 DUP_PWHEN(ctx, orig_any->when, any->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200553 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
554 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100555 case LYS_RPC:
556 case LYS_ACTION:
557 action = (struct lysp_node_action *)node;
558 orig_action = (const struct lysp_node_action *)orig;
559
560 action->input.nodetype = orig_action->input.nodetype;
561 action->output.nodetype = orig_action->output.nodetype;
562 /* we do not need the rest */
563 break;
564 case LYS_INPUT:
565 case LYS_OUTPUT:
566 action_inout = (struct lysp_node_action_inout *)node;
567 orig_action_inout = (const struct lysp_node_action_inout *)orig;
568
569 DUP_ARRAY(ctx, orig_action_inout->musts, action_inout->musts, lysp_restr_dup);
570 /* we do not need the rest */
571 break;
572 case LYS_NOTIF:
573 notif = (struct lysp_node_notif *)node;
574 orig_notif = (const struct lysp_node_notif *)orig;
575
576 DUP_ARRAY(ctx, orig_notif->musts, notif->musts, lysp_restr_dup);
577 /* we do not need the rest */
578 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200579 default:
580 LOGINT_RET(ctx);
581 }
582
583 return ret;
584}
585
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200586/**
587 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
588 *
589 * @param[in] ctx libyang context.
590 * @param[in] pnode Node to duplicate.
591 * @param[in] with_links Whether to also copy any links (child, parent pointers).
592 * @param[out] dup_p Duplicated parsed node.
593 * @return LY_ERR value.
594 */
595static LY_ERR
596lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
597{
598 LY_ERR ret = LY_SUCCESS;
599 void *mem = NULL;
600
601 if (!pnode) {
602 *dup_p = NULL;
603 return LY_SUCCESS;
604 }
605
606 switch (pnode->nodetype) {
607 case LYS_CONTAINER:
608 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200609 break;
610 case LYS_LEAF:
611 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200612 break;
613 case LYS_LEAFLIST:
614 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200615 break;
616 case LYS_LIST:
617 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200618 break;
619 case LYS_CHOICE:
620 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200621 break;
622 case LYS_CASE:
623 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200624 break;
625 case LYS_ANYDATA:
626 case LYS_ANYXML:
627 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200628 break;
629 case LYS_INPUT:
630 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100631 mem = calloc(1, sizeof(struct lysp_node_action_inout));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200632 break;
633 case LYS_ACTION:
634 case LYS_RPC:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100635 mem = calloc(1, sizeof(struct lysp_node_action));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200636 break;
637 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100638 mem = calloc(1, sizeof(struct lysp_node_notif));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200639 break;
640 default:
641 LOGINT_RET(ctx);
642 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100643 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
644 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200645
646 if (with_links) {
Michal Vaskoec8f4272021-10-27 09:14:03 +0200647 /* copy also parent, child, action, and notification pointers */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200648 ((struct lysp_node *)mem)->parent = pnode->parent;
649 switch (pnode->nodetype) {
650 case LYS_CONTAINER:
651 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
Michal Vaskoec8f4272021-10-27 09:14:03 +0200652 ((struct lysp_node_container *)mem)->actions = ((struct lysp_node_container *)pnode)->actions;
653 ((struct lysp_node_container *)mem)->notifs = ((struct lysp_node_container *)pnode)->notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200654 break;
655 case LYS_LIST:
656 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
Michal Vaskoec8f4272021-10-27 09:14:03 +0200657 ((struct lysp_node_list *)mem)->actions = ((struct lysp_node_list *)pnode)->actions;
658 ((struct lysp_node_list *)mem)->notifs = ((struct lysp_node_list *)pnode)->notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200659 break;
660 case LYS_CHOICE:
661 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
662 break;
663 case LYS_CASE:
664 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
665 break;
666 default:
667 break;
668 }
669 }
670
671cleanup:
672 if (ret) {
673 free(mem);
674 } else {
675 *dup_p = mem;
676 }
677 return ret;
678}
679
680#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100681 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 +0200682 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
683 ret = LY_EVALID; \
684 goto cleanup;
685
686#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
687 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100688 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 +0200689 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
690 ret = LY_EVALID; \
691 goto cleanup; \
692 }
693
694/**
695 * @brief Apply refine.
696 *
697 * @param[in] ctx Compile context.
698 * @param[in] rfn Refine to apply.
699 * @param[in,out] target Refine target.
700 * @return LY_ERR value.
701 */
702static LY_ERR
703lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
704{
705 LY_ERR ret = LY_SUCCESS;
706 LY_ARRAY_COUNT_TYPE u;
707 struct lysp_qname *qname;
708 struct lysp_restr **musts, *must;
709 uint32_t *num;
710
711 /* default value */
712 if (rfn->dflts) {
713 switch (target->nodetype) {
714 case LYS_LEAF:
715 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
716
Michal Vaskoe180ed02021-02-05 16:31:20 +0100717 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200718 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
719 break;
720 case LYS_LEAFLIST:
721 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100722 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200723 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
724 ret = LY_EVALID;
725 goto cleanup;
726 }
727
728 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
729 ((struct lysp_node_leaflist *)target)->dflts = NULL;
730 LY_ARRAY_FOR(rfn->dflts, u) {
731 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
732 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
733 }
734 break;
735 case LYS_CHOICE:
736 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
737
Michal Vaskoe180ed02021-02-05 16:31:20 +0100738 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200739 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
740 break;
741 default:
742 AMEND_WRONG_NODETYPE("refine", "replace", "default");
743 }
744 }
745
746 /* description */
747 if (rfn->dsc) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100748 lydict_remove(ctx->ctx, target->dsc);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200749 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
750 }
751
752 /* reference */
753 if (rfn->ref) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100754 lydict_remove(ctx->ctx, target->ref);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200755 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
756 }
757
758 /* config */
759 if (rfn->flags & LYS_CONFIG_MASK) {
Michal Vasko7c565922021-06-10 14:58:27 +0200760 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200761 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vasko7c565922021-06-10 14:58:27 +0200762 (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT)) ? "RPC/action" :
763 ctx->compile_opts & LYS_IS_NOTIF ? "notification" : "a subtree ignoring config", ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200764 } else {
765 target->flags &= ~LYS_CONFIG_MASK;
766 target->flags |= rfn->flags & LYS_CONFIG_MASK;
767 }
768 }
769
770 /* mandatory */
771 if (rfn->flags & LYS_MAND_MASK) {
772 switch (target->nodetype) {
773 case LYS_LEAF:
774 case LYS_CHOICE:
775 case LYS_ANYDATA:
776 case LYS_ANYXML:
777 break;
778 default:
779 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
780 }
781
782 target->flags &= ~LYS_MAND_MASK;
783 target->flags |= rfn->flags & LYS_MAND_MASK;
784 }
785
786 /* presence */
787 if (rfn->presence) {
788 switch (target->nodetype) {
789 case LYS_CONTAINER:
790 break;
791 default:
792 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
793 }
794
Michal Vaskoe180ed02021-02-05 16:31:20 +0100795 lydict_remove(ctx->ctx, ((struct lysp_node_container *)target)->presence);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200796 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
797 }
798
799 /* must */
800 if (rfn->musts) {
801 switch (target->nodetype) {
802 case LYS_CONTAINER:
803 case LYS_LIST:
804 case LYS_LEAF:
805 case LYS_LEAFLIST:
806 case LYS_ANYDATA:
807 case LYS_ANYXML:
808 musts = &((struct lysp_node_container *)target)->musts;
809 break;
810 default:
811 AMEND_WRONG_NODETYPE("refine", "add", "must");
812 }
813
814 LY_ARRAY_FOR(rfn->musts, u) {
815 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
816 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
817 }
818 }
819
820 /* min-elements */
821 if (rfn->flags & LYS_SET_MIN) {
822 switch (target->nodetype) {
823 case LYS_LEAFLIST:
824 num = &((struct lysp_node_leaflist *)target)->min;
825 break;
826 case LYS_LIST:
827 num = &((struct lysp_node_list *)target)->min;
828 break;
829 default:
830 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
831 }
832
833 *num = rfn->min;
834 }
835
836 /* max-elements */
837 if (rfn->flags & LYS_SET_MAX) {
838 switch (target->nodetype) {
839 case LYS_LEAFLIST:
840 num = &((struct lysp_node_leaflist *)target)->max;
841 break;
842 case LYS_LIST:
843 num = &((struct lysp_node_list *)target)->max;
844 break;
845 default:
846 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
847 }
848
849 *num = rfn->max;
850 }
851
852 /* if-feature */
853 if (rfn->iffeatures) {
854 switch (target->nodetype) {
855 case LYS_LEAF:
856 case LYS_LEAFLIST:
857 case LYS_LIST:
858 case LYS_CONTAINER:
859 case LYS_CHOICE:
860 case LYS_CASE:
861 case LYS_ANYDATA:
862 case LYS_ANYXML:
863 break;
864 default:
865 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
866 }
867
868 LY_ARRAY_FOR(rfn->iffeatures, u) {
869 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
870 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
871 }
872 }
873
874 /* extension */
875 /* TODO refine extensions */
876
877cleanup:
878 return ret;
879}
880
881/**
882 * @brief Apply deviate add.
883 *
884 * @param[in] ctx Compile context.
885 * @param[in] d Deviate add to apply.
886 * @param[in,out] target Deviation target.
887 * @return LY_ERR value.
888 */
889static LY_ERR
890lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
891{
892 LY_ERR ret = LY_SUCCESS;
893 LY_ARRAY_COUNT_TYPE u;
894 struct lysp_qname *qname;
895 uint32_t *num;
896 struct lysp_restr **musts, *must;
897
898#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
899 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100900 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200901 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
902 ret = LY_EVALID; \
903 goto cleanup; \
904 }
905
906 /* [units-stmt] */
907 if (d->units) {
908 switch (target->nodetype) {
909 case LYS_LEAF:
910 case LYS_LEAFLIST:
911 break;
912 default:
913 AMEND_WRONG_NODETYPE("deviation", "add", "units");
914 }
915
916 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
917 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
918 }
919
920 /* *must-stmt */
921 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100922 musts = lysp_node_musts_p(target);
923 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200924 AMEND_WRONG_NODETYPE("deviation", "add", "must");
925 }
926
927 LY_ARRAY_FOR(d->musts, u) {
928 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
929 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
930 }
931 }
932
933 /* *unique-stmt */
934 if (d->uniques) {
935 switch (target->nodetype) {
936 case LYS_LIST:
937 break;
938 default:
939 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
940 }
941
942 LY_ARRAY_FOR(d->uniques, u) {
943 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
944 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
945 }
946 }
947
948 /* *default-stmt */
949 if (d->dflts) {
950 switch (target->nodetype) {
951 case LYS_LEAF:
952 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
953 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
954
955 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
956 break;
957 case LYS_LEAFLIST:
958 LY_ARRAY_FOR(d->dflts, u) {
959 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
960 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
961 }
962 break;
963 case LYS_CHOICE:
964 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
965 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
966
967 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
968 break;
969 default:
970 AMEND_WRONG_NODETYPE("deviation", "add", "default");
971 }
972 }
973
974 /* [config-stmt] */
975 if (d->flags & LYS_CONFIG_MASK) {
976 switch (target->nodetype) {
977 case LYS_CONTAINER:
978 case LYS_LEAF:
979 case LYS_LEAFLIST:
980 case LYS_LIST:
981 case LYS_CHOICE:
982 case LYS_ANYDATA:
983 case LYS_ANYXML:
984 break;
985 default:
986 AMEND_WRONG_NODETYPE("deviation", "add", "config");
987 }
988
989 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100990 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200991 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
992 target->flags & LYS_CONFIG_W ? "true" : "false");
993 ret = LY_EVALID;
994 goto cleanup;
995 }
996
997 target->flags |= d->flags & LYS_CONFIG_MASK;
998 }
999
1000 /* [mandatory-stmt] */
1001 if (d->flags & LYS_MAND_MASK) {
1002 switch (target->nodetype) {
1003 case LYS_LEAF:
1004 case LYS_CHOICE:
1005 case LYS_ANYDATA:
1006 case LYS_ANYXML:
1007 break;
1008 default:
1009 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
1010 }
1011
1012 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001013 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001014 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
1015 target->flags & LYS_MAND_TRUE ? "true" : "false");
1016 ret = LY_EVALID;
1017 goto cleanup;
1018 }
1019
1020 target->flags |= d->flags & LYS_MAND_MASK;
1021 }
1022
1023 /* [min-elements-stmt] */
1024 if (d->flags & LYS_SET_MIN) {
1025 switch (target->nodetype) {
1026 case LYS_LEAFLIST:
1027 num = &((struct lysp_node_leaflist *)target)->min;
1028 break;
1029 case LYS_LIST:
1030 num = &((struct lysp_node_list *)target)->min;
1031 break;
1032 default:
1033 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
1034 }
1035
1036 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001037 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001038 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
1039 ret = LY_EVALID;
1040 goto cleanup;
1041 }
1042
1043 *num = d->min;
1044 }
1045
1046 /* [max-elements-stmt] */
1047 if (d->flags & LYS_SET_MAX) {
1048 switch (target->nodetype) {
1049 case LYS_LEAFLIST:
1050 num = &((struct lysp_node_leaflist *)target)->max;
1051 break;
1052 case LYS_LIST:
1053 num = &((struct lysp_node_list *)target)->max;
1054 break;
1055 default:
1056 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1057 }
1058
1059 if (target->flags & LYS_SET_MAX) {
1060 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001061 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001062 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1063 *num);
1064 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001065 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001066 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1067 }
1068 ret = LY_EVALID;
1069 goto cleanup;
1070 }
1071
1072 *num = d->max;
1073 }
1074
1075cleanup:
1076 return ret;
1077}
1078
1079/**
1080 * @brief Apply deviate delete.
1081 *
1082 * @param[in] ctx Compile context.
1083 * @param[in] d Deviate delete to apply.
1084 * @param[in,out] target Deviation target.
1085 * @return LY_ERR value.
1086 */
1087static LY_ERR
1088lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1089{
1090 LY_ERR ret = LY_SUCCESS;
1091 struct lysp_restr **musts;
1092 LY_ARRAY_COUNT_TYPE u, v;
1093 struct lysp_qname **uniques, **dflts;
1094
1095#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1096 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1097 int found = 0; \
1098 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1099 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1100 found = 1; \
1101 break; \
1102 } \
1103 } \
1104 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001105 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001106 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1107 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1108 ret = LY_EVALID; \
1109 goto cleanup; \
1110 } \
1111 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1112 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
Michal Vasko08e9b112021-06-11 15:41:17 +02001113 if (v < LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1114 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1115 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001116 } \
1117 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1118 LY_ARRAY_FREE(ORIG_ARRAY); \
1119 ORIG_ARRAY = NULL; \
1120 }
1121
1122#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1123 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001124 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001125 ret = LY_EVALID; \
1126 goto cleanup; \
1127 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001128 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001129 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1130 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1131 ret = LY_EVALID; \
1132 goto cleanup; \
1133 }
1134
1135 /* [units-stmt] */
1136 if (d->units) {
1137 switch (target->nodetype) {
1138 case LYS_LEAF:
1139 case LYS_LEAFLIST:
1140 break;
1141 default:
1142 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1143 }
1144
1145 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001146 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001147 ((struct lysp_node_leaf *)target)->units = NULL;
1148 }
1149
1150 /* *must-stmt */
1151 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001152 musts = lysp_node_musts_p(target);
1153 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001154 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1155 }
1156
1157 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1158 }
1159
1160 /* *unique-stmt */
1161 if (d->uniques) {
1162 switch (target->nodetype) {
1163 case LYS_LIST:
1164 break;
1165 default:
1166 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1167 }
1168
1169 uniques = &((struct lysp_node_list *)target)->uniques;
1170 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1171 }
1172
1173 /* *default-stmt */
1174 if (d->dflts) {
1175 switch (target->nodetype) {
1176 case LYS_LEAF:
1177 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1178 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1179
Michal Vaskoe180ed02021-02-05 16:31:20 +01001180 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001181 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1182 break;
1183 case LYS_LEAFLIST:
1184 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1185 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1186 break;
1187 case LYS_CHOICE:
1188 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1189 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1190
Michal Vaskoe180ed02021-02-05 16:31:20 +01001191 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001192 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1193 break;
1194 default:
1195 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1196 }
1197 }
1198
1199cleanup:
1200 return ret;
1201}
1202
1203/**
1204 * @brief Apply deviate replace.
1205 *
1206 * @param[in] ctx Compile context.
1207 * @param[in] d Deviate replace to apply.
1208 * @param[in,out] target Deviation target.
1209 * @return LY_ERR value.
1210 */
1211static LY_ERR
1212lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1213{
1214 LY_ERR ret = LY_SUCCESS;
1215 uint32_t *num;
1216
1217#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1218 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001219 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 ret = LY_EVALID; \
1221 goto cleanup; \
1222 }
1223
1224 /* [type-stmt] */
1225 if (d->type) {
1226 switch (target->nodetype) {
1227 case LYS_LEAF:
1228 case LYS_LEAFLIST:
1229 break;
1230 default:
1231 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1232 }
1233
1234 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1235 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1236 }
1237
1238 /* [units-stmt] */
1239 if (d->units) {
1240 switch (target->nodetype) {
1241 case LYS_LEAF:
1242 case LYS_LEAFLIST:
1243 break;
1244 default:
1245 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1246 }
1247
1248 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001249 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001250 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1251 }
1252
1253 /* [default-stmt] */
1254 if (d->dflt.str) {
1255 switch (target->nodetype) {
1256 case LYS_LEAF:
1257 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1258
Michal Vaskoe180ed02021-02-05 16:31:20 +01001259 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1261 break;
1262 case LYS_CHOICE:
1263 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1264
Michal Vaskoe180ed02021-02-05 16:31:20 +01001265 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001266 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1267 break;
1268 default:
1269 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1270 }
1271 }
1272
1273 /* [config-stmt] */
1274 if (d->flags & LYS_CONFIG_MASK) {
1275 switch (target->nodetype) {
1276 case LYS_CONTAINER:
1277 case LYS_LEAF:
1278 case LYS_LEAFLIST:
1279 case LYS_LIST:
1280 case LYS_CHOICE:
1281 case LYS_ANYDATA:
1282 case LYS_ANYXML:
1283 break;
1284 default:
1285 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1286 }
1287
1288 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001289 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1290 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001291 ret = LY_EVALID;
1292 goto cleanup;
1293 }
1294
1295 target->flags &= ~LYS_CONFIG_MASK;
1296 target->flags |= d->flags & LYS_CONFIG_MASK;
1297 }
1298
1299 /* [mandatory-stmt] */
1300 if (d->flags & LYS_MAND_MASK) {
1301 switch (target->nodetype) {
1302 case LYS_LEAF:
1303 case LYS_CHOICE:
1304 case LYS_ANYDATA:
1305 case LYS_ANYXML:
1306 break;
1307 default:
1308 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1309 }
1310
1311 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001312 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1313 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001314 ret = LY_EVALID;
1315 goto cleanup;
1316 }
1317
1318 target->flags &= ~LYS_MAND_MASK;
1319 target->flags |= d->flags & LYS_MAND_MASK;
1320 }
1321
1322 /* [min-elements-stmt] */
1323 if (d->flags & LYS_SET_MIN) {
1324 switch (target->nodetype) {
1325 case LYS_LEAFLIST:
1326 num = &((struct lysp_node_leaflist *)target)->min;
1327 break;
1328 case LYS_LIST:
1329 num = &((struct lysp_node_list *)target)->min;
1330 break;
1331 default:
1332 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1333 }
1334
1335 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001336 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001337 ret = LY_EVALID;
1338 goto cleanup;
1339 }
1340
1341 *num = d->min;
1342 }
1343
1344 /* [max-elements-stmt] */
1345 if (d->flags & LYS_SET_MAX) {
1346 switch (target->nodetype) {
1347 case LYS_LEAFLIST:
1348 num = &((struct lysp_node_leaflist *)target)->max;
1349 break;
1350 case LYS_LIST:
1351 num = &((struct lysp_node_list *)target)->max;
1352 break;
1353 default:
1354 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1355 }
1356
1357 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001358 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001359 ret = LY_EVALID;
1360 goto cleanup;
1361 }
1362
1363 *num = d->max;
1364 }
1365
1366cleanup:
1367 return ret;
1368}
1369
1370/**
1371 * @brief Get module of a single nodeid node name test.
1372 *
1373 * @param[in] ctx libyang context.
1374 * @param[in] nametest Nametest with an optional prefix.
1375 * @param[in] nametest_len Length of @p nametest.
1376 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1377 * @param[out] name Optional pointer to the name test without the prefix.
1378 * @param[out] name_len Length of @p name.
1379 * @return Resolved module.
1380 */
1381static const struct lys_module *
1382lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1383 const struct lysp_module *mod, const char **name, size_t *name_len)
1384{
1385 const struct lys_module *target_mod;
1386 const char *ptr;
1387
1388 ptr = ly_strnchr(nametest, ':', nametest_len);
1389 if (ptr) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001390 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_VALUE_SCHEMA, (void *)mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001391 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001392 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001393 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001394 (int)nametest_len, nametest, (int)(ptr - nametest), nametest, LYSP_MODULE_NAME(mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001395 return NULL;
1396 }
1397
1398 if (name) {
1399 *name = ptr + 1;
1400 *name_len = nametest_len - ((ptr - nametest) + 1);
1401 }
1402 } else {
1403 target_mod = mod->mod;
1404 if (name) {
1405 *name = nametest;
1406 *name_len = nametest_len;
1407 }
1408 }
1409
1410 return target_mod;
1411}
1412
1413/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001414 * @brief Check whether a compiled node matches a single schema nodeid name test.
1415 *
1416 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1417 * @param[in] mod Expected module.
1418 * @param[in] name Expected name.
1419 * @param[in] name_len Length of @p name.
1420 * @return Whether it is a match or not.
1421 */
1422static ly_bool
1423lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1424 size_t name_len)
1425{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001426 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001427 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001428 return 0;
1429 }
1430
1431 /* compare names */
Michal Vasko544e58a2021-01-28 14:33:41 +01001432 if (ly_strncmp((*node)->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001433 return 0;
1434 }
1435
Michal Vasko2a668712020-10-21 11:48:09 +02001436 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001437 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001438
1439 return 1;
1440}
1441
1442/**
1443 * @brief Check whether a node matches specific schema nodeid.
1444 *
1445 * @param[in] exp Parsed nodeid to match.
1446 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1447 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1448 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1449 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1450 * @param[in] pnode_mod Compiled @p pnode to-be module.
1451 * @return Whether it is a match or not.
1452 */
1453static ly_bool
1454lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1455 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1456{
1457 uint32_t i;
1458 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001459 const char *name = NULL;
1460 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001461
1462 /* compare last node in the node ID */
1463 i = exp->used - 1;
1464
1465 /* get exp node ID module */
1466 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);
1467 assert(mod);
1468
1469 if (pnode) {
1470 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001471 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001472 return 0;
1473 }
1474 } else {
1475 /* using parent directly */
1476 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1477 return 0;
1478 }
1479 }
1480
1481 /* now compare all the compiled parents */
1482 while (i > 1) {
1483 i -= 2;
1484 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1485
1486 if (!parent) {
1487 /* no more parents but path continues */
1488 return 0;
1489 }
1490
1491 /* get exp node ID module */
1492 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1493 &name_len);
1494 assert(mod);
1495
1496 /* compare with the parent */
1497 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1498 return 0;
1499 }
1500 }
1501
1502 if (ctx_node && (ctx_node != parent)) {
1503 /* descendant path has not finished in the context node */
1504 return 0;
1505 } else if (!ctx_node && parent) {
1506 /* some parent was not matched */
1507 return 0;
1508 }
1509
1510 return 1;
1511}
1512
1513void
1514lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1515{
1516 if (aug) {
1517 lyxp_expr_free(ctx, aug->nodeid);
1518
1519 free(aug);
1520 }
1521}
1522
1523void
1524lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1525{
1526 if (dev) {
1527 lyxp_expr_free(ctx, dev->nodeid);
1528 LY_ARRAY_FREE(dev->devs);
1529 LY_ARRAY_FREE(dev->dev_pmods);
1530
1531 free(dev);
1532 }
1533}
1534
1535void
1536lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1537{
1538 if (rfn) {
1539 lyxp_expr_free(ctx, rfn->nodeid);
1540 LY_ARRAY_FREE(rfn->rfns);
1541
1542 free(rfn);
1543 }
1544}
1545
1546void
1547lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1548{
1549 if (!dev_pnode) {
1550 return;
1551 }
1552
1553 switch (dev_pnode->nodetype) {
1554 case LYS_CONTAINER:
1555 ((struct lysp_node_container *)dev_pnode)->child = NULL;
Michal Vaskoec8f4272021-10-27 09:14:03 +02001556 ((struct lysp_node_container *)dev_pnode)->actions = NULL;
1557 ((struct lysp_node_container *)dev_pnode)->notifs = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001558 break;
1559 case LYS_LIST:
1560 ((struct lysp_node_list *)dev_pnode)->child = NULL;
Michal Vaskoec8f4272021-10-27 09:14:03 +02001561 ((struct lysp_node_list *)dev_pnode)->actions = NULL;
1562 ((struct lysp_node_list *)dev_pnode)->notifs = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001563 break;
1564 case LYS_CHOICE:
1565 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1566 break;
1567 case LYS_CASE:
1568 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1569 break;
1570 case LYS_LEAF:
1571 case LYS_LEAFLIST:
1572 case LYS_ANYXML:
1573 case LYS_ANYDATA:
1574 /* no children */
1575 break;
1576 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001577 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001578 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001579 case LYS_RPC:
1580 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001581 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1582 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001583 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001584 case LYS_INPUT:
1585 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001586 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001587 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001588 free(dev_pnode);
1589 return;
1590 default:
1591 LOGINT(ctx);
1592 return;
1593 }
1594
1595 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1596}
1597
1598LY_ERR
1599lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1600 struct lysp_node **dev_pnode, ly_bool *not_supported)
1601{
1602 LY_ERR ret = LY_SUCCESS;
1603 uint32_t i;
1604 LY_ARRAY_COUNT_TYPE u;
1605 struct lys_module *orig_mod = ctx->cur_mod;
1606 struct lysp_module *orig_pmod = ctx->pmod;
1607 char orig_path[LYSC_CTX_BUFSIZE];
1608 struct lysc_refine *rfn;
1609 struct lysc_deviation *dev;
1610 struct lysp_deviation *dev_p;
1611 struct lysp_deviate *d;
1612
1613 *dev_pnode = NULL;
1614 *not_supported = 0;
1615
1616 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1617 rfn = ctx->uses_rfns.objs[i];
1618
Michal Vasko28fe5f62021-03-03 16:37:39 +01001619 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001620 /* not our target node */
1621 continue;
1622 }
1623
1624 if (!*dev_pnode) {
1625 /* first refine on this node, create a copy first */
1626 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1627 }
1628
Michal Vaskob8df5762021-01-12 15:15:53 +01001629 /* use modules from the refine */
1630 ctx->cur_mod = rfn->nodeid_pmod->mod;
1631 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1632
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001633 /* apply all the refines by changing (the copy of) the parsed node */
1634 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001635 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001636 lysc_update_path(ctx, NULL, "{refine}");
1637 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001638
1639 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001640 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1641 lysc_update_path(ctx, NULL, NULL);
1642 lysc_update_path(ctx, NULL, NULL);
1643 LY_CHECK_GOTO(ret, cleanup);
1644 }
1645
1646 /* refine was applied, remove it */
1647 lysc_refine_free(ctx->ctx, rfn);
1648 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1649
1650 /* all the refines for one target node are in one structure, we are done */
1651 break;
1652 }
1653
1654 for (i = 0; i < ctx->devs.count; ++i) {
1655 dev = ctx->devs.objs[i];
1656
Michal Vasko28fe5f62021-03-03 16:37:39 +01001657 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001658 /* not our target node */
1659 continue;
1660 }
1661
1662 if (dev->not_supported) {
1663 /* it is not supported, no more deviations */
1664 *not_supported = 1;
1665 goto dev_applied;
1666 }
1667
1668 if (!*dev_pnode) {
1669 /* first deviation on this node, create a copy first */
1670 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1671 }
1672
1673 /* apply all the deviates by changing (the copy of) the parsed node */
1674 LY_ARRAY_FOR(dev->devs, u) {
1675 dev_p = dev->devs[u];
1676 LY_LIST_FOR(dev_p->deviates, d) {
1677 /* generate correct path */
1678 strcpy(orig_path, ctx->path);
1679 ctx->path_len = 1;
1680 ctx->cur_mod = dev->dev_pmods[u]->mod;
1681 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1682 lysc_update_path(ctx, NULL, "{deviation}");
1683 lysc_update_path(ctx, NULL, dev_p->nodeid);
1684
1685 switch (d->mod) {
1686 case LYS_DEV_ADD:
1687 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1688 break;
1689 case LYS_DEV_DELETE:
1690 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1691 break;
1692 case LYS_DEV_REPLACE:
1693 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1694 break;
1695 default:
1696 LOGINT(ctx->ctx);
1697 ret = LY_EINT;
1698 }
1699
1700 /* restore previous path */
1701 strcpy(ctx->path, orig_path);
1702 ctx->path_len = strlen(ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001703
1704 LY_CHECK_GOTO(ret, cleanup);
1705 }
1706 }
1707
1708dev_applied:
1709 /* deviation was applied, remove it */
1710 lysc_deviation_free(ctx->ctx, dev);
1711 ly_set_rm_index(&ctx->devs, i, NULL);
1712
1713 /* all the deviations for one target node are in one structure, we are done */
1714 break;
1715 }
1716
1717cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001718 ctx->cur_mod = orig_mod;
1719 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001720 if (ret) {
1721 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1722 *dev_pnode = NULL;
1723 *not_supported = 0;
1724 }
1725 return ret;
1726}
1727
1728/**
1729 * @brief Compile the parsed augment connecting it into its target.
1730 *
1731 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1732 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1733 * are already implemented and compiled.
1734 *
1735 * @param[in] ctx Compile context.
1736 * @param[in] aug_p Parsed augment to compile.
1737 * @param[in] target Target node of the augment.
1738 * @return LY_SUCCESS on success.
1739 * @return LY_EVALID on failure.
1740 */
1741static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001742lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001743{
1744 LY_ERR ret = LY_SUCCESS;
1745 struct lysp_node *pnode;
1746 struct lysc_node *node;
1747 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001748 struct lysc_node_action **actions;
1749 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001750 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001751 struct ly_set child_set = {0};
Michal Vasko7c565922021-06-10 14:58:27 +02001752 uint32_t i, opt_prev = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001753
1754 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001755 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001756 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1757 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1758 ret = LY_EVALID;
1759 goto cleanup;
1760 }
1761
1762 /* check for mandatory nodes
1763 * - new cases augmenting some choice can have mandatory nodes
1764 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1765 */
1766 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1767 allow_mandatory = 1;
1768 }
1769
1770 LY_LIST_FOR(aug_p->child, pnode) {
1771 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1772 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1773 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1774 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001775 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001776 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1777 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1778 ret = LY_EVALID;
1779 goto cleanup;
1780 }
1781
1782 /* compile the children */
1783 if (target->nodetype == LYS_CHOICE) {
1784 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001785 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1786 if (target->nodetype == LYS_INPUT) {
Michal Vasko7c565922021-06-10 14:58:27 +02001787 ctx->compile_opts |= LYS_COMPILE_RPC_INPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001788 } else {
Michal Vasko7c565922021-06-10 14:58:27 +02001789 ctx->compile_opts |= LYS_COMPILE_RPC_OUTPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001790 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001791 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001792 } else {
1793 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1794 }
1795
Michal Vasko29dd11e2020-11-23 16:52:22 +01001796 /* eval if-features again for the rest of this node processing */
1797 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1798 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001799 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001800 }
1801
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001802 /* since the augment node is not present in the compiled tree, we need to pass some of its
1803 * statements to all its children */
1804 for (i = 0; i < child_set.count; ++i) {
1805 node = child_set.snodes[i];
1806 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1807 node->flags &= ~LYS_MAND_TRUE;
1808 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001809 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko23864e02021-06-24 09:23:58 +02001810 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.",
1811 node->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001812 ret = LY_EVALID;
1813 goto cleanup;
1814 }
1815
1816 if (aug_p->when) {
1817 /* pass augment's when to all the children */
Michal Vaskodfd254d2021-06-24 09:24:59 +02001818 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, lysc_data_node(target), node, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001819 LY_CHECK_GOTO(ret, cleanup);
1820 }
1821 }
1822 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001823
1824 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001825 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826 }
1827
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001828 actions = lysc_node_actions_p(target);
1829 notifs = lysc_node_notifs_p(target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001830
1831 if (aug_p->actions) {
1832 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001833 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001834 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001835 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001836 ret = LY_EVALID;
1837 goto cleanup;
1838 }
1839
1840 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001841 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001842 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001843
Michal Vasko012807e2021-02-03 09:52:18 +01001844 /* eval if-features again for the rest of this node processing */
1845 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1846 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001847 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001848 }
Michal Vasko012807e2021-02-03 09:52:18 +01001849
1850 /* since the augment node is not present in the compiled tree, we need to pass some of its
1851 * statements to all its children */
1852 for (i = 0; i < child_set.count; ++i) {
1853 node = child_set.snodes[i];
1854 if (aug_p->when) {
1855 /* pass augment's when to all the actions */
Michal Vaskodfd254d2021-06-24 09:24:59 +02001856 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, lysc_data_node(target), node, &when_shared);
Michal Vasko012807e2021-02-03 09:52:18 +01001857 LY_CHECK_GOTO(ret, cleanup);
1858 }
1859 }
1860 ly_set_erase(&child_set, NULL);
1861
1862 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001863 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001864 }
1865 }
1866 if (aug_p->notifs) {
1867 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001868 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001869 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001870 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001871 ret = LY_EVALID;
1872 goto cleanup;
1873 }
1874
1875 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001876 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001877 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001878
Michal Vasko012807e2021-02-03 09:52:18 +01001879 /* eval if-features again for the rest of this node processing */
1880 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1881 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001882 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001883 }
Michal Vasko012807e2021-02-03 09:52:18 +01001884
1885 /* since the augment node is not present in the compiled tree, we need to pass some of its
1886 * statements to all its children */
1887 for (i = 0; i < child_set.count; ++i) {
1888 node = child_set.snodes[i];
1889 if (aug_p->when) {
1890 /* pass augment's when to all the actions */
Michal Vaskodfd254d2021-06-24 09:24:59 +02001891 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, lysc_data_node(target), node, &when_shared);
Michal Vasko012807e2021-02-03 09:52:18 +01001892 LY_CHECK_GOTO(ret, cleanup);
1893 }
1894 }
1895 ly_set_erase(&child_set, NULL);
1896
1897 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001898 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001899 }
1900 }
1901
1902cleanup:
1903 ly_set_erase(&child_set, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02001904 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001905 return ret;
1906}
1907
1908LY_ERR
1909lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1910{
1911 LY_ERR ret = LY_SUCCESS;
1912 struct lys_module *orig_mod = ctx->cur_mod;
1913 struct lysp_module *orig_pmod = ctx->pmod;
1914 uint32_t i;
1915 char orig_path[LYSC_CTX_BUFSIZE];
1916 struct lysc_augment *aug;
1917
1918 /* uses augments */
1919 for (i = 0; i < ctx->uses_augs.count; ) {
1920 aug = ctx->uses_augs.objs[i];
1921
Michal Vasko28fe5f62021-03-03 16:37:39 +01001922 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001923 /* not our target node */
1924 ++i;
1925 continue;
1926 }
1927
Michal Vaskob8df5762021-01-12 15:15:53 +01001928 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001929 lysc_update_path(ctx, NULL, "{augment}");
1930 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001931 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001932
1933 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001934 ret = lys_compile_augment(ctx, aug->aug_p, node);
1935 lysc_update_path(ctx, NULL, NULL);
1936 lysc_update_path(ctx, NULL, NULL);
1937 LY_CHECK_GOTO(ret, cleanup);
1938
Michal Vaskoc75f2042021-06-08 14:57:03 +02001939 /* augment was applied, remove it (index and the whole set may have changed because other augments
1940 * could have been applied) */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001941 ly_set_rm(&ctx->uses_augs, aug, NULL);
1942 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001943 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001944 }
1945
1946 /* top-level augments */
1947 for (i = 0; i < ctx->augs.count; ) {
1948 aug = ctx->augs.objs[i];
1949
Michal Vasko28fe5f62021-03-03 16:37:39 +01001950 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001951 /* not our target node */
1952 ++i;
1953 continue;
1954 }
1955
Michal Vaskob8df5762021-01-12 15:15:53 +01001956 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001957 strcpy(orig_path, ctx->path);
1958 ctx->path_len = 1;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001959 ctx->cur_mod = aug->aug_pmod->mod;
1960 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001961 lysc_update_path(ctx, NULL, "{augment}");
1962 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001963
1964 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001965 ret = lys_compile_augment(ctx, aug->aug_p, node);
1966 strcpy(ctx->path, orig_path);
1967 ctx->path_len = strlen(ctx->path);
1968 LY_CHECK_GOTO(ret, cleanup);
1969
1970 /* augment was applied, remove it */
1971 ly_set_rm(&ctx->augs, aug, NULL);
1972 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001973 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001974 }
1975
1976cleanup:
1977 ctx->cur_mod = orig_mod;
1978 ctx->pmod = orig_pmod;
1979 return ret;
1980}
1981
1982/**
1983 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1984 *
1985 * @param[in] ctx Compile context.
1986 * @param[in] aug_p Parsed augment to be applied.
1987 * @param[in] pmod Both current and prefix module for @p aug_p.
1988 * @return LY_ERR value.
1989 */
1990static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001991lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, const struct lysp_module *pmod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001992{
1993 LY_ERR ret = LY_SUCCESS;
1994 struct lyxp_expr *exp = NULL;
1995 struct lysc_augment *aug;
1996 const struct lys_module *mod;
1997
1998 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1999 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
2000 LY_CHECK_GOTO(ret, cleanup);
2001
2002 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2003 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2004 if (mod != ctx->cur_mod) {
2005 /* augment for another module, ignore */
2006 goto cleanup;
2007 }
2008
2009 /* allocate new compiled augment and store it in the set */
2010 aug = calloc(1, sizeof *aug);
2011 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2012 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
2013
2014 aug->nodeid = exp;
2015 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01002016 aug->aug_pmod = pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002017 aug->aug_p = aug_p;
2018
2019cleanup:
2020 lyxp_expr_free(ctx->ctx, exp);
2021 return ret;
2022}
2023
2024LY_ERR
2025lys_precompile_own_augments(struct lysc_ctx *ctx)
2026{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002027 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002028
2029 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002030 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
2031 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002032
2033 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002034 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
2035 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002036 }
2037
2038 /* collect all submodules augments */
2039 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002040 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
2041 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, (struct lysp_module *)aug_mod->parsed->includes[v].submodule));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002042 }
2043 }
2044 }
2045
2046 return LY_SUCCESS;
2047}
2048
2049/**
2050 * @brief Prepare a deviation to be applied during data nodes compilation.
2051 *
2052 * @param[in] ctx Compile context.
2053 * @param[in] dev_p Parsed deviation to be applied.
2054 * @param[in] pmod Both current and prefix module for @p dev_p.
2055 * @return LY_ERR value.
2056 */
2057static LY_ERR
2058lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2059{
2060 LY_ERR ret = LY_SUCCESS;
2061 struct lysc_deviation *dev = NULL;
2062 struct lyxp_expr *exp = NULL;
2063 struct lysp_deviation **new_dev;
2064 const struct lys_module *mod;
2065 const struct lysp_module **new_dev_pmod;
2066 uint32_t i;
2067
2068 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2069 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2070 LY_CHECK_GOTO(ret, cleanup);
2071
2072 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2073 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2074 if (mod != ctx->cur_mod) {
2075 /* deviation for another module, ignore */
2076 goto cleanup;
2077 }
2078
2079 /* try to find the node in already compiled deviations */
2080 for (i = 0; i < ctx->devs.count; ++i) {
2081 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2082 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2083 dev = ctx->devs.objs[i];
2084 break;
2085 }
2086 }
2087
2088 if (!dev) {
2089 /* allocate new compiled deviation */
2090 dev = calloc(1, sizeof *dev);
2091 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2092 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2093
2094 dev->nodeid = exp;
2095 exp = NULL;
2096 }
2097
2098 /* add new parsed deviation structure */
2099 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2100 *new_dev = dev_p;
2101 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2102 *new_dev_pmod = pmod;
2103
2104cleanup:
2105 lyxp_expr_free(ctx->ctx, exp);
2106 return ret;
2107}
2108
2109LY_ERR
2110lys_precompile_own_deviations(struct lysc_ctx *ctx)
2111{
2112 LY_ARRAY_COUNT_TYPE u, v, w;
Michal Vaskoe8220db2021-06-02 15:39:05 +02002113 struct lys_module *orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002114 const struct lys_module *dev_mod;
2115 struct lysc_deviation *dev;
2116 struct lysp_deviate *d;
2117 int not_supported;
2118 uint32_t i;
2119
2120 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2121 dev_mod = ctx->cur_mod->deviated_by[u];
2122
2123 /* compile all module deviations */
2124 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2125 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2126 }
2127
2128 /* compile all submodules deviations */
2129 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2130 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2131 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2132 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2133 }
2134 }
2135 }
2136
2137 /* set not-supported flags for all the deviations */
2138 for (i = 0; i < ctx->devs.count; ++i) {
2139 dev = ctx->devs.objs[i];
2140 not_supported = 0;
2141
2142 LY_ARRAY_FOR(dev->devs, u) {
2143 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2144 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2145 not_supported = 1;
2146 break;
2147 }
2148 }
2149 if (not_supported) {
2150 break;
2151 }
2152 }
2153 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Michal Vaskoe8220db2021-06-02 15:39:05 +02002154 orig_cur_mod = ctx->cur_mod;
2155 ctx->cur_mod = dev->dev_pmods[u]->mod;
2156 lysc_update_path(ctx, NULL, "{deviation}");
2157 lysc_update_path(ctx, NULL, dev->nodeid->expr);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002158 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002159 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
Michal Vaskoe8220db2021-06-02 15:39:05 +02002160 lysc_update_path(ctx, NULL, NULL);
2161 lysc_update_path(ctx, NULL, NULL);
2162 ctx->cur_mod = orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002163 return LY_EVALID;
2164 }
2165
2166 dev->not_supported = not_supported;
2167 }
2168
2169 return LY_SUCCESS;
2170}
2171
2172/**
2173 * @brief Add a module reference into an array, checks for duplicities.
2174 *
2175 * @param[in] ctx Compile context.
2176 * @param[in] mod Module reference to add.
2177 * @param[in,out] mod_array Module sized array to add to.
2178 * @return LY_ERR value.
2179 */
2180static LY_ERR
2181lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2182{
2183 LY_ARRAY_COUNT_TYPE u;
2184 struct lys_module **new_mod;
2185
2186 LY_ARRAY_FOR(*mod_array, u) {
2187 if ((*mod_array)[u] == mod) {
2188 /* already there */
2189 return LY_EEXIST;
2190 }
2191 }
2192
2193 /* add the new module ref */
2194 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2195 *new_mod = mod;
2196
2197 return LY_SUCCESS;
2198}
2199
Michal Vasko1ccbf542021-04-19 11:35:00 +02002200/**
2201 * @brief Check whether all modules in a set are implemented.
2202 *
2203 * @param[in] mod_set Module set to check.
2204 * @return Whether all modules are implemented or not.
2205 */
2206static ly_bool
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002207lys_precompile_mod_set_is_all_implemented(const struct ly_set *mod_set)
Michal Vasko1ccbf542021-04-19 11:35:00 +02002208{
2209 uint32_t i;
2210 const struct lys_module *mod;
2211
2212 for (i = 0; i < mod_set->count; ++i) {
2213 mod = mod_set->objs[i];
2214 if (!mod->implemented) {
2215 return 0;
2216 }
2217 }
2218
2219 return 1;
2220}
2221
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002222LY_ERR
Michal Vasko65333882021-06-10 14:12:16 +02002223lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002224{
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002225 LY_ERR ret = LY_SUCCESS, r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002226 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko65333882021-06-10 14:12:16 +02002227 struct lysc_ctx ctx = {0};
2228 struct lysp_module *mod_p;
2229 struct lys_module *m;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002230 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002231 struct lysp_node_augment *aug;
Michal Vaskoc56d6372021-10-19 12:29:00 +02002232 const char **imp_f, *all_f[] = {"*", NULL};
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002233 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002234 struct ly_set mod_set = {0}, set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002235
Michal Vasko65333882021-06-10 14:12:16 +02002236 mod_p = mod->parsed;
2237
2238 /* prepare context */
2239 ctx.ctx = mod->ctx;
2240 ctx.cur_mod = mod;
2241 ctx.pmod = mod_p;
2242 ctx.path_len = 1;
2243 ctx.path[0] = '/';
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002244
Radek Krejci2a9fc652021-01-22 17:44:34 +01002245 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002246 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002247 lysc_update_path(&ctx, NULL, "{augment}");
2248 lysc_update_path(&ctx, NULL, aug->nodeid);
2249 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2250 lysc_update_path(&ctx, NULL, NULL);
2251 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002252 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002253
Michal Vasko1ccbf542021-04-19 11:35:00 +02002254 /* add this module into the target module augmented_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002255 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002256 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002257 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002258 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002259 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002260 }
2261
2262 LY_ARRAY_FOR(mod_p->deviations, u) {
2263 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002264 lysc_update_path(&ctx, NULL, "{deviation}");
2265 lysc_update_path(&ctx, NULL, mod_p->deviations[u].nodeid);
2266 ret = lys_nodeid_mod_check(&ctx, mod_p->deviations[u].nodeid, 1, &set, NULL, &m);
2267 lysc_update_path(&ctx, NULL, NULL);
2268 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002269 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002270
Michal Vasko1ccbf542021-04-19 11:35:00 +02002271 /* add this module into the target module deviated_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002272 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002273 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002274 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2275 }
2276 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002277 }
2278
2279 /* the same for augments and deviations in submodules */
2280 LY_ARRAY_FOR(mod_p->includes, v) {
2281 submod = mod_p->includes[v].submodule;
Michal Vasko65333882021-06-10 14:12:16 +02002282 ctx.pmod = (struct lysp_module *)submod;
Michal Vaskoce3d6172021-06-08 14:59:49 +02002283
Radek Krejci2a9fc652021-01-22 17:44:34 +01002284 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko65333882021-06-10 14:12:16 +02002285 lysc_update_path(&ctx, NULL, "{augment}");
2286 lysc_update_path(&ctx, NULL, aug->nodeid);
2287 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2288 lysc_update_path(&ctx, NULL, NULL);
2289 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002290 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002291
Michal Vasko65333882021-06-10 14:12:16 +02002292 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002293 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002294 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002295 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002296 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002297 }
2298
2299 LY_ARRAY_FOR(submod->deviations, u) {
Michal Vasko65333882021-06-10 14:12:16 +02002300 lysc_update_path(&ctx, NULL, "{deviation}");
2301 lysc_update_path(&ctx, NULL, submod->deviations[u].nodeid);
2302 ret = lys_nodeid_mod_check(&ctx, submod->deviations[u].nodeid, 1, &set, NULL, &m);
2303 lysc_update_path(&ctx, NULL, NULL);
2304 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002305 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002306
Michal Vasko65333882021-06-10 14:12:16 +02002307 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002308 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002309 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2310 }
2311 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002312 }
2313 }
2314
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002315 for (i = 0; i < mod_set.count; ++i) {
2316 m = mod_set.objs[i];
Michal Vasko1ccbf542021-04-19 11:35:00 +02002317
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002318 if (m == mod) {
2319 /* will be applied normally later */
2320 continue;
2321 }
2322
2323 /* we do not actually need the target modules compiled with out amends, they just need to be implemented
2324 * not compiled yet and marked for compilation */
2325
2326 if (!m->implemented) {
2327 /* implement the target module */
Michal Vaskoc56d6372021-10-19 12:29:00 +02002328 imp_f = (mod->ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL;
2329 r = lys_implement(m, imp_f, unres);
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002330 if (r == LY_ERECOMPILE) {
2331 /* implement all the modules right away to save possible later recompilation */
2332 ret = r;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002333 continue;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002334 } else if (r) {
2335 /* error */
2336 ret = r;
Michal Vasko65333882021-06-10 14:12:16 +02002337 goto cleanup;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002338 }
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002339 } else if (m->compiled) {
2340 /* target module was already compiled without our amends (augment/deviation), we need to recompile it */
2341 m->to_compile = 1;
2342 ret = LY_ERECOMPILE;
2343 continue;
2344 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002345 }
2346
Michal Vasko1ccbf542021-04-19 11:35:00 +02002347cleanup:
2348 ly_set_erase(&set, NULL);
2349 ly_set_erase(&mod_set, NULL);
2350 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002351}
2352
2353void
2354lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2355{
2356 uint32_t i;
2357 LY_ARRAY_COUNT_TYPE u, count;
2358 struct lys_module *m;
2359
2360 for (i = 0; i < ctx->list.count; ++i) {
2361 m = ctx->list.objs[i];
2362
2363 if (m->augmented_by) {
2364 count = LY_ARRAY_COUNT(m->augmented_by);
2365 for (u = 0; u < count; ++u) {
2366 if (m->augmented_by[u] == mod) {
2367 /* keep the order */
2368 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002369 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u - 1) * sizeof *m->augmented_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002370 }
2371 LY_ARRAY_DECREMENT(m->augmented_by);
2372 break;
2373 }
2374 }
2375 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2376 LY_ARRAY_FREE(m->augmented_by);
2377 m->augmented_by = NULL;
2378 }
2379 }
2380
2381 if (m->deviated_by) {
2382 count = LY_ARRAY_COUNT(m->deviated_by);
2383 for (u = 0; u < count; ++u) {
2384 if (m->deviated_by[u] == mod) {
2385 /* keep the order */
2386 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002387 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u - 1) * sizeof *m->deviated_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002388 }
2389 LY_ARRAY_DECREMENT(m->deviated_by);
2390 break;
2391 }
2392 }
2393 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2394 LY_ARRAY_FREE(m->deviated_by);
2395 m->deviated_by = NULL;
2396 }
2397 }
2398 }
2399}