blob: 865c8b7e61cb2382c14bddaafa0579929ed7de00 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_amend.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of augments, deviations, and refines.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
16
17#include "schema_compile_amend.h"
18
19#include <assert.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020020#include <stddef.h>
21#include <stdint.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020022#include <stdlib.h>
23#include <string.h>
24
25#include "common.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020026#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020027#include "plugins_exts.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020028#include "schema_compile.h"
29#include "schema_compile_node.h"
Michal Vasko29dd11e2020-11-23 16:52:22 +010030#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020031#include "set.h"
32#include "tree.h"
33#include "tree_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020034#include "tree_schema.h"
35#include "tree_schema_internal.h"
36#include "xpath.h"
37
38static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest,
39 size_t nametest_len, const struct lysp_module *mod, const char **name, size_t *name_len);
40
41static LY_ERR
42lys_nodeid_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct lys_module **target_mod,
43 struct lyxp_expr **expr)
44{
45 LY_ERR ret = LY_SUCCESS;
46 struct lyxp_expr *e = NULL;
47 struct lys_module *tmod = NULL, *mod;
48 const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid";
49 uint32_t i;
50
51 /* parse */
52 ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e);
53 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010054 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020055 nodeid_type, nodeid);
56 ret = LY_EVALID;
57 goto cleanup;
58 }
59
60 if (abs) {
61 /* absolute schema nodeid */
62 i = 0;
63 } else {
64 /* descendant schema nodeid */
65 if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010066 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020067 nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]);
68 ret = LY_EVALID;
69 goto cleanup;
70 }
71 i = 1;
72 }
73
74 /* check all the tokens */
75 for ( ; i < e->used; i += 2) {
76 if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010077 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020078 nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]);
79 ret = LY_EVALID;
80 goto cleanup;
81 } else if (e->used == i + 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010082 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020083 "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr);
84 ret = LY_EVALID;
85 goto cleanup;
86 } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010087 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020088 nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
89 ret = LY_EVALID;
90 goto cleanup;
91 } else if (abs) {
92 mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1],
93 e->tok_len[i + 1], ctx->pmod, NULL, NULL);
94 LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup);
95
96 /* only keep the first module */
97 if (!tmod) {
98 tmod = mod;
99 }
100
101 /* all the modules must be implemented */
102 if (!mod->implemented) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100103 ret = lys_set_implemented_r(mod, NULL, ctx->unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200104 LY_CHECK_GOTO(ret, cleanup);
105 }
106 }
107 }
108
109cleanup:
110 if (ret || !expr) {
111 lyxp_expr_free(ctx->ctx, e);
112 e = NULL;
113 }
114 if (expr) {
115 *expr = ret ? NULL : e;
116 }
117 if (target_mod) {
118 *target_mod = ret ? NULL : tmod;
119 }
120 return ret;
121}
122
123/**
124 * @brief Check whether 2 schema nodeids match.
125 *
126 * @param[in] ctx libyang context.
127 * @param[in] exp1 First schema nodeid.
128 * @param[in] exp1p_mod Module of @p exp1 nodes without any prefix.
129 * @param[in] exp2 Second schema nodeid.
130 * @param[in] exp2_pmod Module of @p exp2 nodes without any prefix.
131 * @return Whether the schema nodeids match or not.
132 */
133static ly_bool
134lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lysp_module *exp1_pmod,
135 const struct lyxp_expr *exp2, const struct lysp_module *exp2_pmod)
136{
137 uint32_t i;
138 const struct lys_module *mod1, *mod2;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100139 const char *name1 = NULL, *name2 = NULL;
140 size_t name1_len = 0, name2_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200141
142 if (exp1->used != exp2->used) {
143 return 0;
144 }
145
146 for (i = 0; i < exp1->used; ++i) {
147 assert(exp1->tokens[i] == exp2->tokens[i]);
148
149 if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) {
150 /* check modules of all the nodes in the node ID */
151 mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_pmod,
152 &name1, &name1_len);
153 assert(mod1);
154 mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_pmod,
155 &name2, &name2_len);
156 assert(mod2);
157
158 /* compare modules */
159 if (mod1 != mod2) {
160 return 0;
161 }
162
163 /* compare names */
164 if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) {
165 return 0;
166 }
167 }
168 }
169
170 return 1;
171}
172
173LY_ERR
174lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node)
175{
176 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200177 struct lyxp_expr *exp = NULL;
178 struct lysc_augment *aug;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100179 struct lysp_node_augment *aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200180 struct lysc_refine *rfn;
181 struct lysp_refine **new_rfn;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100182 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200183 uint32_t i;
184
Radek Krejci2a9fc652021-01-22 17:44:34 +0100185 LY_LIST_FOR(uses_p->augments, aug_p) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200186 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +0100187 lysc_update_path(ctx, NULL, aug_p->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200188
189 /* parse the nodeid */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100190 LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, aug_p->nodeid, 0, NULL, &exp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200191
192 /* allocate new compiled augment and store it in the set */
193 aug = calloc(1, sizeof *aug);
194 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
195 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, 1, NULL), cleanup);
196
197 aug->nodeid = exp;
198 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +0100199 aug->aug_pmod = ctx->pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200200 aug->nodeid_ctx_node = ctx_node;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100201 aug->aug_p = aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200202
203 lysc_update_path(ctx, NULL, NULL);
204 lysc_update_path(ctx, NULL, NULL);
205 }
206
207 LY_ARRAY_FOR(uses_p->refines, u) {
208 lysc_update_path(ctx, NULL, "{refine}");
209 lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid);
210
211 /* parse the nodeid */
212 LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->refines[u].nodeid, 0, NULL, &exp), cleanup);
213
214 /* try to find the node in already compiled refines */
215 rfn = NULL;
216 for (i = 0; i < ctx->uses_rfns.count; ++i) {
217 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->pmod, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid,
218 ctx->pmod)) {
219 rfn = ctx->uses_rfns.objs[i];
220 break;
221 }
222 }
223
224 if (!rfn) {
225 /* allocate new compiled refine */
226 rfn = calloc(1, sizeof *rfn);
227 LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
228 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, 1, NULL), cleanup);
229
230 rfn->nodeid = exp;
231 exp = NULL;
Michal Vasko7d3708f2021-02-03 10:50:24 +0100232 rfn->nodeid_pmod = ctx->cur_mod->parsed;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200233 rfn->nodeid_ctx_node = ctx_node;
Michal Vaskod8655722021-01-12 15:20:36 +0100234 rfn->uses_p = uses_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200235 } else {
236 /* just free exp */
237 lyxp_expr_free(ctx->ctx, exp);
238 exp = NULL;
239 }
240
241 /* add new parsed refine structure */
242 LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup);
243 *new_rfn = &uses_p->refines[u];
244
245 lysc_update_path(ctx, NULL, NULL);
246 lysc_update_path(ctx, NULL, NULL);
247 }
248
249cleanup:
250 lyxp_expr_free(ctx->ctx, exp);
251 return ret;
252}
253
254static LY_ERR
255lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext)
256{
257 LY_ERR ret = LY_SUCCESS;
258
259 *ext = *orig_ext;
260 DUP_STRING(ctx, orig_ext->name, ext->name, ret);
261 DUP_STRING(ctx, orig_ext->argument, ext->argument, ret);
262
263 return ret;
264}
265
266static LY_ERR
267lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr)
268{
269 LY_ERR ret = LY_SUCCESS;
270
271 if (orig_restr) {
272 DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret);
273 restr->arg.mod = orig_restr->arg.mod;
274 DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret);
275 DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret);
276 DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret);
277 DUP_STRING(ctx, orig_restr->ref, restr->ref, ret);
278 DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup);
279 }
280
281 return ret;
282}
283
284static LY_ERR
285lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str)
286{
287 LY_ERR ret = LY_SUCCESS;
288
289 DUP_STRING(ctx, *orig_str, *str, ret);
290
291 return ret;
292}
293
294LY_ERR
295lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname)
296{
297 LY_ERR ret = LY_SUCCESS;
298
299 if (!orig_qname->str) {
300 return LY_SUCCESS;
301 }
302
303 DUP_STRING(ctx, orig_qname->str, qname->str, ret);
304 assert(orig_qname->mod);
305 qname->mod = orig_qname->mod;
306
307 return ret;
308}
309
310static LY_ERR
311lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm)
312{
313 LY_ERR ret = LY_SUCCESS;
314
315 DUP_STRING(ctx, orig_enm->name, enm->name, ret);
316 DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret);
317 DUP_STRING(ctx, orig_enm->ref, enm->ref, ret);
318 enm->value = orig_enm->value;
319 DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup);
320 DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup);
321 enm->flags = orig_enm->flags;
322
323 return ret;
324}
325
326static LY_ERR
327lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type)
328{
329 LY_ERR ret = LY_SUCCESS;
330
331 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
332
333 if (orig_type->range) {
334 type->range = calloc(1, sizeof *type->range);
335 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
336 LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range));
337 }
338
339 if (orig_type->length) {
340 type->length = calloc(1, sizeof *type->length);
341 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
342 LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length));
343 }
344
345 DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup);
346 DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup);
347 DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup);
348 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done);
349 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
350 DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup);
351 DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup);
352
353 type->pmod = orig_type->pmod;
354 type->compiled = orig_type->compiled;
355
356 type->fraction_digits = orig_type->fraction_digits;
357 type->require_instance = orig_type->require_instance;
358 type->flags = orig_type->flags;
359
360done:
361 return ret;
362}
363
364static LY_ERR
365lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when)
366{
367 LY_ERR ret = LY_SUCCESS;
368
369 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
370 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
371 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
372 DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup);
373
374 return ret;
375}
376
377static LY_ERR
378lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
379{
380 LY_ERR ret = LY_SUCCESS;
381
382 node->parent = NULL;
383 node->nodetype = orig->nodetype;
384 node->flags = orig->flags;
385 node->next = NULL;
386 DUP_STRING(ctx, orig->name, node->name, ret);
387 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
388 DUP_STRING(ctx, orig->ref, node->ref, ret);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200389 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
390 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
391
392 return ret;
393}
394
Radek Krejci9a3823e2021-01-27 20:26:46 +0100395#define DUP_PWHEN(CTX, ORIG, NEW) \
396 if (ORIG) { \
397 NEW = calloc(1, sizeof *NEW); \
398 LY_CHECK_ERR_RET(!NEW, LOGMEM(CTX), LY_EMEM); \
399 LY_CHECK_RET(lysp_when_dup(CTX, NEW, ORIG)); \
400 }
401
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200402static LY_ERR
403lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
404{
405 LY_ERR ret = LY_SUCCESS;
406 struct lysp_node_container *cont;
407 const struct lysp_node_container *orig_cont;
408 struct lysp_node_leaf *leaf;
409 const struct lysp_node_leaf *orig_leaf;
410 struct lysp_node_leaflist *llist;
411 const struct lysp_node_leaflist *orig_llist;
412 struct lysp_node_list *list;
413 const struct lysp_node_list *orig_list;
414 struct lysp_node_choice *choice;
415 const struct lysp_node_choice *orig_choice;
416 struct lysp_node_case *cas;
417 const struct lysp_node_case *orig_cas;
418 struct lysp_node_anydata *any;
419 const struct lysp_node_anydata *orig_any;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100420 struct lysp_node_action *action;
421 const struct lysp_node_action *orig_action;
422 struct lysp_node_action_inout *action_inout;
423 const struct lysp_node_action_inout *orig_action_inout;
424 struct lysp_node_notif *notif;
425 const struct lysp_node_notif *orig_notif;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200426
Radek Krejci2a9fc652021-01-22 17:44:34 +0100427 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA |
428 LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200429
430 /* common part */
431 LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig));
432
433 /* specific part */
434 switch (node->nodetype) {
435 case LYS_CONTAINER:
436 cont = (struct lysp_node_container *)node;
437 orig_cont = (const struct lysp_node_container *)orig;
438
Radek Krejci9a3823e2021-01-27 20:26:46 +0100439 DUP_PWHEN(ctx, orig_cont->when, cont->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200440 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
441 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
442 /* we do not need the rest */
443 break;
444 case LYS_LEAF:
445 leaf = (struct lysp_node_leaf *)node;
446 orig_leaf = (const struct lysp_node_leaf *)orig;
447
Radek Krejci9a3823e2021-01-27 20:26:46 +0100448 DUP_PWHEN(ctx, orig_leaf->when, leaf->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200449 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
450 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
451 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
452 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
453 break;
454 case LYS_LEAFLIST:
455 llist = (struct lysp_node_leaflist *)node;
456 orig_llist = (const struct lysp_node_leaflist *)orig;
457
Radek Krejci9a3823e2021-01-27 20:26:46 +0100458 DUP_PWHEN(ctx, orig_llist->when, llist->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200459 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
460 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
461 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
462 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
463 llist->min = orig_llist->min;
464 llist->max = orig_llist->max;
465 break;
466 case LYS_LIST:
467 list = (struct lysp_node_list *)node;
468 orig_list = (const struct lysp_node_list *)orig;
469
Radek Krejci9a3823e2021-01-27 20:26:46 +0100470 DUP_PWHEN(ctx, orig_list->when, list->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200471 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
472 DUP_STRING(ctx, orig_list->key, list->key, ret);
473 /* we do not need these arrays */
474 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
475 list->min = orig_list->min;
476 list->max = orig_list->max;
477 break;
478 case LYS_CHOICE:
479 choice = (struct lysp_node_choice *)node;
480 orig_choice = (const struct lysp_node_choice *)orig;
481
Radek Krejci9a3823e2021-01-27 20:26:46 +0100482 DUP_PWHEN(ctx, orig_choice->when, choice->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200483 /* we do not need children */
484 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
485 break;
486 case LYS_CASE:
487 cas = (struct lysp_node_case *)node;
488 orig_cas = (const struct lysp_node_case *)orig;
489
Radek Krejci9a3823e2021-01-27 20:26:46 +0100490 DUP_PWHEN(ctx, orig_cas->when, cas->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200491 /* we do not need children */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200492 break;
493 case LYS_ANYDATA:
494 case LYS_ANYXML:
495 any = (struct lysp_node_anydata *)node;
496 orig_any = (const struct lysp_node_anydata *)orig;
497
Radek Krejci9a3823e2021-01-27 20:26:46 +0100498 DUP_PWHEN(ctx, orig_any->when, any->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200499 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
500 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100501 case LYS_RPC:
502 case LYS_ACTION:
503 action = (struct lysp_node_action *)node;
504 orig_action = (const struct lysp_node_action *)orig;
505
506 action->input.nodetype = orig_action->input.nodetype;
507 action->output.nodetype = orig_action->output.nodetype;
508 /* we do not need the rest */
509 break;
510 case LYS_INPUT:
511 case LYS_OUTPUT:
512 action_inout = (struct lysp_node_action_inout *)node;
513 orig_action_inout = (const struct lysp_node_action_inout *)orig;
514
515 DUP_ARRAY(ctx, orig_action_inout->musts, action_inout->musts, lysp_restr_dup);
516 /* we do not need the rest */
517 break;
518 case LYS_NOTIF:
519 notif = (struct lysp_node_notif *)node;
520 orig_notif = (const struct lysp_node_notif *)orig;
521
522 DUP_ARRAY(ctx, orig_notif->musts, notif->musts, lysp_restr_dup);
523 /* we do not need the rest */
524 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200525 default:
526 LOGINT_RET(ctx);
527 }
528
529 return ret;
530}
531
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200532/**
533 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
534 *
535 * @param[in] ctx libyang context.
536 * @param[in] pnode Node to duplicate.
537 * @param[in] with_links Whether to also copy any links (child, parent pointers).
538 * @param[out] dup_p Duplicated parsed node.
539 * @return LY_ERR value.
540 */
541static LY_ERR
542lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
543{
544 LY_ERR ret = LY_SUCCESS;
545 void *mem = NULL;
546
547 if (!pnode) {
548 *dup_p = NULL;
549 return LY_SUCCESS;
550 }
551
552 switch (pnode->nodetype) {
553 case LYS_CONTAINER:
554 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200555 break;
556 case LYS_LEAF:
557 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200558 break;
559 case LYS_LEAFLIST:
560 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200561 break;
562 case LYS_LIST:
563 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200564 break;
565 case LYS_CHOICE:
566 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200567 break;
568 case LYS_CASE:
569 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200570 break;
571 case LYS_ANYDATA:
572 case LYS_ANYXML:
573 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200574 break;
575 case LYS_INPUT:
576 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100577 mem = calloc(1, sizeof(struct lysp_node_action_inout));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200578 break;
579 case LYS_ACTION:
580 case LYS_RPC:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100581 mem = calloc(1, sizeof(struct lysp_node_action));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200582 break;
583 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100584 mem = calloc(1, sizeof(struct lysp_node_notif));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200585 break;
586 default:
587 LOGINT_RET(ctx);
588 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100589 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
590 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200591
592 if (with_links) {
593 /* copy also parent and child pointers */
594 ((struct lysp_node *)mem)->parent = pnode->parent;
595 switch (pnode->nodetype) {
596 case LYS_CONTAINER:
597 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
598 break;
599 case LYS_LIST:
600 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
601 break;
602 case LYS_CHOICE:
603 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
604 break;
605 case LYS_CASE:
606 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
607 break;
608 default:
609 break;
610 }
611 }
612
613cleanup:
614 if (ret) {
615 free(mem);
616 } else {
617 *dup_p = mem;
618 }
619 return ret;
620}
621
622#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 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 +0200624 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
625 ret = LY_EVALID; \
626 goto cleanup;
627
628#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
629 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100630 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 +0200631 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
632 ret = LY_EVALID; \
633 goto cleanup; \
634 }
635
636/**
637 * @brief Apply refine.
638 *
639 * @param[in] ctx Compile context.
640 * @param[in] rfn Refine to apply.
641 * @param[in,out] target Refine target.
642 * @return LY_ERR value.
643 */
644static LY_ERR
645lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
646{
647 LY_ERR ret = LY_SUCCESS;
648 LY_ARRAY_COUNT_TYPE u;
649 struct lysp_qname *qname;
650 struct lysp_restr **musts, *must;
651 uint32_t *num;
652
653 /* default value */
654 if (rfn->dflts) {
655 switch (target->nodetype) {
656 case LYS_LEAF:
657 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
658
Michal Vaskoe180ed02021-02-05 16:31:20 +0100659 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200660 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
661 break;
662 case LYS_LEAFLIST:
663 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100664 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200665 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
666 ret = LY_EVALID;
667 goto cleanup;
668 }
669
670 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
671 ((struct lysp_node_leaflist *)target)->dflts = NULL;
672 LY_ARRAY_FOR(rfn->dflts, u) {
673 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
674 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
675 }
676 break;
677 case LYS_CHOICE:
678 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
679
Michal Vaskoe180ed02021-02-05 16:31:20 +0100680 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200681 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
682 break;
683 default:
684 AMEND_WRONG_NODETYPE("refine", "replace", "default");
685 }
686 }
687
688 /* description */
689 if (rfn->dsc) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100690 lydict_remove(ctx->ctx, target->dsc);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200691 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
692 }
693
694 /* reference */
695 if (rfn->ref) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100696 lydict_remove(ctx->ctx, target->ref);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200697 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
698 }
699
700 /* config */
701 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci91531e12021-02-08 19:57:54 +0100702 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200703 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +0100704 (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT)) ? "RPC/action" :
705 ctx->options & LYS_IS_NOTIF ? "notification" : "a subtree ignoring config", ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200706 } else {
707 target->flags &= ~LYS_CONFIG_MASK;
708 target->flags |= rfn->flags & LYS_CONFIG_MASK;
709 }
710 }
711
712 /* mandatory */
713 if (rfn->flags & LYS_MAND_MASK) {
714 switch (target->nodetype) {
715 case LYS_LEAF:
716 case LYS_CHOICE:
717 case LYS_ANYDATA:
718 case LYS_ANYXML:
719 break;
720 default:
721 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
722 }
723
724 target->flags &= ~LYS_MAND_MASK;
725 target->flags |= rfn->flags & LYS_MAND_MASK;
726 }
727
728 /* presence */
729 if (rfn->presence) {
730 switch (target->nodetype) {
731 case LYS_CONTAINER:
732 break;
733 default:
734 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
735 }
736
Michal Vaskoe180ed02021-02-05 16:31:20 +0100737 lydict_remove(ctx->ctx, ((struct lysp_node_container *)target)->presence);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200738 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
739 }
740
741 /* must */
742 if (rfn->musts) {
743 switch (target->nodetype) {
744 case LYS_CONTAINER:
745 case LYS_LIST:
746 case LYS_LEAF:
747 case LYS_LEAFLIST:
748 case LYS_ANYDATA:
749 case LYS_ANYXML:
750 musts = &((struct lysp_node_container *)target)->musts;
751 break;
752 default:
753 AMEND_WRONG_NODETYPE("refine", "add", "must");
754 }
755
756 LY_ARRAY_FOR(rfn->musts, u) {
757 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
758 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
759 }
760 }
761
762 /* min-elements */
763 if (rfn->flags & LYS_SET_MIN) {
764 switch (target->nodetype) {
765 case LYS_LEAFLIST:
766 num = &((struct lysp_node_leaflist *)target)->min;
767 break;
768 case LYS_LIST:
769 num = &((struct lysp_node_list *)target)->min;
770 break;
771 default:
772 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
773 }
774
775 *num = rfn->min;
776 }
777
778 /* max-elements */
779 if (rfn->flags & LYS_SET_MAX) {
780 switch (target->nodetype) {
781 case LYS_LEAFLIST:
782 num = &((struct lysp_node_leaflist *)target)->max;
783 break;
784 case LYS_LIST:
785 num = &((struct lysp_node_list *)target)->max;
786 break;
787 default:
788 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
789 }
790
791 *num = rfn->max;
792 }
793
794 /* if-feature */
795 if (rfn->iffeatures) {
796 switch (target->nodetype) {
797 case LYS_LEAF:
798 case LYS_LEAFLIST:
799 case LYS_LIST:
800 case LYS_CONTAINER:
801 case LYS_CHOICE:
802 case LYS_CASE:
803 case LYS_ANYDATA:
804 case LYS_ANYXML:
805 break;
806 default:
807 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
808 }
809
810 LY_ARRAY_FOR(rfn->iffeatures, u) {
811 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
812 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
813 }
814 }
815
816 /* extension */
817 /* TODO refine extensions */
818
819cleanup:
820 return ret;
821}
822
823/**
824 * @brief Apply deviate add.
825 *
826 * @param[in] ctx Compile context.
827 * @param[in] d Deviate add to apply.
828 * @param[in,out] target Deviation target.
829 * @return LY_ERR value.
830 */
831static LY_ERR
832lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
833{
834 LY_ERR ret = LY_SUCCESS;
835 LY_ARRAY_COUNT_TYPE u;
836 struct lysp_qname *qname;
837 uint32_t *num;
838 struct lysp_restr **musts, *must;
839
840#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
841 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100842 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200843 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
844 ret = LY_EVALID; \
845 goto cleanup; \
846 }
847
848 /* [units-stmt] */
849 if (d->units) {
850 switch (target->nodetype) {
851 case LYS_LEAF:
852 case LYS_LEAFLIST:
853 break;
854 default:
855 AMEND_WRONG_NODETYPE("deviation", "add", "units");
856 }
857
858 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
859 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
860 }
861
862 /* *must-stmt */
863 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100864 musts = lysp_node_musts_p(target);
865 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200866 AMEND_WRONG_NODETYPE("deviation", "add", "must");
867 }
868
869 LY_ARRAY_FOR(d->musts, u) {
870 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
871 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
872 }
873 }
874
875 /* *unique-stmt */
876 if (d->uniques) {
877 switch (target->nodetype) {
878 case LYS_LIST:
879 break;
880 default:
881 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
882 }
883
884 LY_ARRAY_FOR(d->uniques, u) {
885 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
886 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
887 }
888 }
889
890 /* *default-stmt */
891 if (d->dflts) {
892 switch (target->nodetype) {
893 case LYS_LEAF:
894 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
895 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
896
897 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
898 break;
899 case LYS_LEAFLIST:
900 LY_ARRAY_FOR(d->dflts, u) {
901 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
902 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
903 }
904 break;
905 case LYS_CHOICE:
906 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
907 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
908
909 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
910 break;
911 default:
912 AMEND_WRONG_NODETYPE("deviation", "add", "default");
913 }
914 }
915
916 /* [config-stmt] */
917 if (d->flags & LYS_CONFIG_MASK) {
918 switch (target->nodetype) {
919 case LYS_CONTAINER:
920 case LYS_LEAF:
921 case LYS_LEAFLIST:
922 case LYS_LIST:
923 case LYS_CHOICE:
924 case LYS_ANYDATA:
925 case LYS_ANYXML:
926 break;
927 default:
928 AMEND_WRONG_NODETYPE("deviation", "add", "config");
929 }
930
931 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100932 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200933 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
934 target->flags & LYS_CONFIG_W ? "true" : "false");
935 ret = LY_EVALID;
936 goto cleanup;
937 }
938
939 target->flags |= d->flags & LYS_CONFIG_MASK;
940 }
941
942 /* [mandatory-stmt] */
943 if (d->flags & LYS_MAND_MASK) {
944 switch (target->nodetype) {
945 case LYS_LEAF:
946 case LYS_CHOICE:
947 case LYS_ANYDATA:
948 case LYS_ANYXML:
949 break;
950 default:
951 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
952 }
953
954 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100955 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200956 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
957 target->flags & LYS_MAND_TRUE ? "true" : "false");
958 ret = LY_EVALID;
959 goto cleanup;
960 }
961
962 target->flags |= d->flags & LYS_MAND_MASK;
963 }
964
965 /* [min-elements-stmt] */
966 if (d->flags & LYS_SET_MIN) {
967 switch (target->nodetype) {
968 case LYS_LEAFLIST:
969 num = &((struct lysp_node_leaflist *)target)->min;
970 break;
971 case LYS_LIST:
972 num = &((struct lysp_node_list *)target)->min;
973 break;
974 default:
975 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
976 }
977
978 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100979 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200980 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
981 ret = LY_EVALID;
982 goto cleanup;
983 }
984
985 *num = d->min;
986 }
987
988 /* [max-elements-stmt] */
989 if (d->flags & LYS_SET_MAX) {
990 switch (target->nodetype) {
991 case LYS_LEAFLIST:
992 num = &((struct lysp_node_leaflist *)target)->max;
993 break;
994 case LYS_LIST:
995 num = &((struct lysp_node_list *)target)->max;
996 break;
997 default:
998 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
999 }
1000
1001 if (target->flags & LYS_SET_MAX) {
1002 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001003 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001004 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1005 *num);
1006 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001007 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001008 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1009 }
1010 ret = LY_EVALID;
1011 goto cleanup;
1012 }
1013
1014 *num = d->max;
1015 }
1016
1017cleanup:
1018 return ret;
1019}
1020
1021/**
1022 * @brief Apply deviate delete.
1023 *
1024 * @param[in] ctx Compile context.
1025 * @param[in] d Deviate delete to apply.
1026 * @param[in,out] target Deviation target.
1027 * @return LY_ERR value.
1028 */
1029static LY_ERR
1030lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1031{
1032 LY_ERR ret = LY_SUCCESS;
1033 struct lysp_restr **musts;
1034 LY_ARRAY_COUNT_TYPE u, v;
1035 struct lysp_qname **uniques, **dflts;
1036
1037#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1038 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1039 int found = 0; \
1040 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1041 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1042 found = 1; \
1043 break; \
1044 } \
1045 } \
1046 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001047 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001048 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1049 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1050 ret = LY_EVALID; \
1051 goto cleanup; \
1052 } \
1053 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1054 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
1055 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1056 } \
1057 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1058 LY_ARRAY_FREE(ORIG_ARRAY); \
1059 ORIG_ARRAY = NULL; \
1060 }
1061
1062#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1063 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001064 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001065 ret = LY_EVALID; \
1066 goto cleanup; \
1067 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001068 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001069 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1070 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1071 ret = LY_EVALID; \
1072 goto cleanup; \
1073 }
1074
1075 /* [units-stmt] */
1076 if (d->units) {
1077 switch (target->nodetype) {
1078 case LYS_LEAF:
1079 case LYS_LEAFLIST:
1080 break;
1081 default:
1082 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1083 }
1084
1085 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001086 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001087 ((struct lysp_node_leaf *)target)->units = NULL;
1088 }
1089
1090 /* *must-stmt */
1091 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001092 musts = lysp_node_musts_p(target);
1093 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001094 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1095 }
1096
1097 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1098 }
1099
1100 /* *unique-stmt */
1101 if (d->uniques) {
1102 switch (target->nodetype) {
1103 case LYS_LIST:
1104 break;
1105 default:
1106 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1107 }
1108
1109 uniques = &((struct lysp_node_list *)target)->uniques;
1110 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1111 }
1112
1113 /* *default-stmt */
1114 if (d->dflts) {
1115 switch (target->nodetype) {
1116 case LYS_LEAF:
1117 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1118 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1119
Michal Vaskoe180ed02021-02-05 16:31:20 +01001120 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001121 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1122 break;
1123 case LYS_LEAFLIST:
1124 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1125 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1126 break;
1127 case LYS_CHOICE:
1128 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1129 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1130
Michal Vaskoe180ed02021-02-05 16:31:20 +01001131 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001132 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1133 break;
1134 default:
1135 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1136 }
1137 }
1138
1139cleanup:
1140 return ret;
1141}
1142
1143/**
1144 * @brief Apply deviate replace.
1145 *
1146 * @param[in] ctx Compile context.
1147 * @param[in] d Deviate replace to apply.
1148 * @param[in,out] target Deviation target.
1149 * @return LY_ERR value.
1150 */
1151static LY_ERR
1152lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1153{
1154 LY_ERR ret = LY_SUCCESS;
1155 uint32_t *num;
1156
1157#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1158 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001159 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001160 ret = LY_EVALID; \
1161 goto cleanup; \
1162 }
1163
1164 /* [type-stmt] */
1165 if (d->type) {
1166 switch (target->nodetype) {
1167 case LYS_LEAF:
1168 case LYS_LEAFLIST:
1169 break;
1170 default:
1171 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1172 }
1173
1174 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1175 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1176 }
1177
1178 /* [units-stmt] */
1179 if (d->units) {
1180 switch (target->nodetype) {
1181 case LYS_LEAF:
1182 case LYS_LEAFLIST:
1183 break;
1184 default:
1185 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1186 }
1187
1188 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001189 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001190 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1191 }
1192
1193 /* [default-stmt] */
1194 if (d->dflt.str) {
1195 switch (target->nodetype) {
1196 case LYS_LEAF:
1197 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1198
Michal Vaskoe180ed02021-02-05 16:31:20 +01001199 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001200 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1201 break;
1202 case LYS_CHOICE:
1203 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1204
Michal Vaskoe180ed02021-02-05 16:31:20 +01001205 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001206 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1207 break;
1208 default:
1209 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1210 }
1211 }
1212
1213 /* [config-stmt] */
1214 if (d->flags & LYS_CONFIG_MASK) {
1215 switch (target->nodetype) {
1216 case LYS_CONTAINER:
1217 case LYS_LEAF:
1218 case LYS_LEAFLIST:
1219 case LYS_LIST:
1220 case LYS_CHOICE:
1221 case LYS_ANYDATA:
1222 case LYS_ANYXML:
1223 break;
1224 default:
1225 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1226 }
1227
1228 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001229 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1230 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001231 ret = LY_EVALID;
1232 goto cleanup;
1233 }
1234
1235 target->flags &= ~LYS_CONFIG_MASK;
1236 target->flags |= d->flags & LYS_CONFIG_MASK;
1237 }
1238
1239 /* [mandatory-stmt] */
1240 if (d->flags & LYS_MAND_MASK) {
1241 switch (target->nodetype) {
1242 case LYS_LEAF:
1243 case LYS_CHOICE:
1244 case LYS_ANYDATA:
1245 case LYS_ANYXML:
1246 break;
1247 default:
1248 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1249 }
1250
1251 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001252 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1253 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 ret = LY_EVALID;
1255 goto cleanup;
1256 }
1257
1258 target->flags &= ~LYS_MAND_MASK;
1259 target->flags |= d->flags & LYS_MAND_MASK;
1260 }
1261
1262 /* [min-elements-stmt] */
1263 if (d->flags & LYS_SET_MIN) {
1264 switch (target->nodetype) {
1265 case LYS_LEAFLIST:
1266 num = &((struct lysp_node_leaflist *)target)->min;
1267 break;
1268 case LYS_LIST:
1269 num = &((struct lysp_node_list *)target)->min;
1270 break;
1271 default:
1272 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1273 }
1274
1275 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001276 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001277 ret = LY_EVALID;
1278 goto cleanup;
1279 }
1280
1281 *num = d->min;
1282 }
1283
1284 /* [max-elements-stmt] */
1285 if (d->flags & LYS_SET_MAX) {
1286 switch (target->nodetype) {
1287 case LYS_LEAFLIST:
1288 num = &((struct lysp_node_leaflist *)target)->max;
1289 break;
1290 case LYS_LIST:
1291 num = &((struct lysp_node_list *)target)->max;
1292 break;
1293 default:
1294 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1295 }
1296
1297 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001298 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001299 ret = LY_EVALID;
1300 goto cleanup;
1301 }
1302
1303 *num = d->max;
1304 }
1305
1306cleanup:
1307 return ret;
1308}
1309
1310/**
1311 * @brief Get module of a single nodeid node name test.
1312 *
1313 * @param[in] ctx libyang context.
1314 * @param[in] nametest Nametest with an optional prefix.
1315 * @param[in] nametest_len Length of @p nametest.
1316 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1317 * @param[out] name Optional pointer to the name test without the prefix.
1318 * @param[out] name_len Length of @p name.
1319 * @return Resolved module.
1320 */
1321static const struct lys_module *
1322lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1323 const struct lysp_module *mod, const char **name, size_t *name_len)
1324{
1325 const struct lys_module *target_mod;
1326 const char *ptr;
1327
1328 ptr = ly_strnchr(nametest, ':', nametest_len);
1329 if (ptr) {
1330 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)mod);
1331 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001332 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001333 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
1334 nametest_len, nametest, ptr - nametest, nametest, LYSP_MODULE_NAME(mod));
1335 return NULL;
1336 }
1337
1338 if (name) {
1339 *name = ptr + 1;
1340 *name_len = nametest_len - ((ptr - nametest) + 1);
1341 }
1342 } else {
1343 target_mod = mod->mod;
1344 if (name) {
1345 *name = nametest;
1346 *name_len = nametest_len;
1347 }
1348 }
1349
1350 return target_mod;
1351}
1352
1353/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001354 * @brief Check whether a compiled node matches a single schema nodeid name test.
1355 *
1356 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1357 * @param[in] mod Expected module.
1358 * @param[in] name Expected name.
1359 * @param[in] name_len Length of @p name.
1360 * @return Whether it is a match or not.
1361 */
1362static ly_bool
1363lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1364 size_t name_len)
1365{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001366 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001367 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001368 return 0;
1369 }
1370
1371 /* compare names */
Michal Vasko544e58a2021-01-28 14:33:41 +01001372 if (ly_strncmp((*node)->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001373 return 0;
1374 }
1375
Michal Vasko2a668712020-10-21 11:48:09 +02001376 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001377 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001378
1379 return 1;
1380}
1381
1382/**
1383 * @brief Check whether a node matches specific schema nodeid.
1384 *
1385 * @param[in] exp Parsed nodeid to match.
1386 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1387 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1388 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1389 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1390 * @param[in] pnode_mod Compiled @p pnode to-be module.
1391 * @return Whether it is a match or not.
1392 */
1393static ly_bool
1394lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1395 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1396{
1397 uint32_t i;
1398 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001399 const char *name = NULL;
1400 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001401
1402 /* compare last node in the node ID */
1403 i = exp->used - 1;
1404
1405 /* get exp node ID module */
1406 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);
1407 assert(mod);
1408
1409 if (pnode) {
1410 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001411 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001412 return 0;
1413 }
1414 } else {
1415 /* using parent directly */
1416 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1417 return 0;
1418 }
1419 }
1420
1421 /* now compare all the compiled parents */
1422 while (i > 1) {
1423 i -= 2;
1424 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1425
1426 if (!parent) {
1427 /* no more parents but path continues */
1428 return 0;
1429 }
1430
1431 /* get exp node ID module */
1432 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1433 &name_len);
1434 assert(mod);
1435
1436 /* compare with the parent */
1437 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1438 return 0;
1439 }
1440 }
1441
1442 if (ctx_node && (ctx_node != parent)) {
1443 /* descendant path has not finished in the context node */
1444 return 0;
1445 } else if (!ctx_node && parent) {
1446 /* some parent was not matched */
1447 return 0;
1448 }
1449
1450 return 1;
1451}
1452
1453void
1454lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1455{
1456 if (aug) {
1457 lyxp_expr_free(ctx, aug->nodeid);
1458
1459 free(aug);
1460 }
1461}
1462
1463void
1464lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1465{
1466 if (dev) {
1467 lyxp_expr_free(ctx, dev->nodeid);
1468 LY_ARRAY_FREE(dev->devs);
1469 LY_ARRAY_FREE(dev->dev_pmods);
1470
1471 free(dev);
1472 }
1473}
1474
1475void
1476lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1477{
1478 if (rfn) {
1479 lyxp_expr_free(ctx, rfn->nodeid);
1480 LY_ARRAY_FREE(rfn->rfns);
1481
1482 free(rfn);
1483 }
1484}
1485
1486void
1487lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1488{
1489 if (!dev_pnode) {
1490 return;
1491 }
1492
1493 switch (dev_pnode->nodetype) {
1494 case LYS_CONTAINER:
1495 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1496 break;
1497 case LYS_LIST:
1498 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1499 break;
1500 case LYS_CHOICE:
1501 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1502 break;
1503 case LYS_CASE:
1504 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1505 break;
1506 case LYS_LEAF:
1507 case LYS_LEAFLIST:
1508 case LYS_ANYXML:
1509 case LYS_ANYDATA:
1510 /* no children */
1511 break;
1512 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001513 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001514 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001515 case LYS_RPC:
1516 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001517 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1518 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001519 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001520 case LYS_INPUT:
1521 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001522 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001523 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001524 free(dev_pnode);
1525 return;
1526 default:
1527 LOGINT(ctx);
1528 return;
1529 }
1530
1531 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1532}
1533
1534LY_ERR
1535lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1536 struct lysp_node **dev_pnode, ly_bool *not_supported)
1537{
1538 LY_ERR ret = LY_SUCCESS;
1539 uint32_t i;
1540 LY_ARRAY_COUNT_TYPE u;
1541 struct lys_module *orig_mod = ctx->cur_mod;
1542 struct lysp_module *orig_pmod = ctx->pmod;
1543 char orig_path[LYSC_CTX_BUFSIZE];
1544 struct lysc_refine *rfn;
1545 struct lysc_deviation *dev;
1546 struct lysp_deviation *dev_p;
1547 struct lysp_deviate *d;
1548
1549 *dev_pnode = NULL;
1550 *not_supported = 0;
1551
1552 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1553 rfn = ctx->uses_rfns.objs[i];
1554
Michal Vasko28fe5f62021-03-03 16:37:39 +01001555 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 +02001556 /* not our target node */
1557 continue;
1558 }
1559
1560 if (!*dev_pnode) {
1561 /* first refine on this node, create a copy first */
1562 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1563 }
1564
Michal Vaskob8df5762021-01-12 15:15:53 +01001565 /* use modules from the refine */
1566 ctx->cur_mod = rfn->nodeid_pmod->mod;
1567 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1568
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001569 /* apply all the refines by changing (the copy of) the parsed node */
1570 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001571 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001572 lysc_update_path(ctx, NULL, "{refine}");
1573 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001574
1575 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001576 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1577 lysc_update_path(ctx, NULL, NULL);
1578 lysc_update_path(ctx, NULL, NULL);
1579 LY_CHECK_GOTO(ret, cleanup);
1580 }
1581
1582 /* refine was applied, remove it */
1583 lysc_refine_free(ctx->ctx, rfn);
1584 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1585
1586 /* all the refines for one target node are in one structure, we are done */
1587 break;
1588 }
1589
1590 for (i = 0; i < ctx->devs.count; ++i) {
1591 dev = ctx->devs.objs[i];
1592
Michal Vasko28fe5f62021-03-03 16:37:39 +01001593 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001594 /* not our target node */
1595 continue;
1596 }
1597
1598 if (dev->not_supported) {
1599 /* it is not supported, no more deviations */
1600 *not_supported = 1;
1601 goto dev_applied;
1602 }
1603
1604 if (!*dev_pnode) {
1605 /* first deviation on this node, create a copy first */
1606 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1607 }
1608
1609 /* apply all the deviates by changing (the copy of) the parsed node */
1610 LY_ARRAY_FOR(dev->devs, u) {
1611 dev_p = dev->devs[u];
1612 LY_LIST_FOR(dev_p->deviates, d) {
1613 /* generate correct path */
1614 strcpy(orig_path, ctx->path);
1615 ctx->path_len = 1;
1616 ctx->cur_mod = dev->dev_pmods[u]->mod;
1617 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1618 lysc_update_path(ctx, NULL, "{deviation}");
1619 lysc_update_path(ctx, NULL, dev_p->nodeid);
1620
1621 switch (d->mod) {
1622 case LYS_DEV_ADD:
1623 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1624 break;
1625 case LYS_DEV_DELETE:
1626 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1627 break;
1628 case LYS_DEV_REPLACE:
1629 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1630 break;
1631 default:
1632 LOGINT(ctx->ctx);
1633 ret = LY_EINT;
1634 }
1635
1636 /* restore previous path */
1637 strcpy(ctx->path, orig_path);
1638 ctx->path_len = strlen(ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001639
1640 LY_CHECK_GOTO(ret, cleanup);
1641 }
1642 }
1643
1644dev_applied:
1645 /* deviation was applied, remove it */
1646 lysc_deviation_free(ctx->ctx, dev);
1647 ly_set_rm_index(&ctx->devs, i, NULL);
1648
1649 /* all the deviations for one target node are in one structure, we are done */
1650 break;
1651 }
1652
1653cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001654 ctx->cur_mod = orig_mod;
1655 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001656 if (ret) {
1657 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1658 *dev_pnode = NULL;
1659 *not_supported = 0;
1660 }
1661 return ret;
1662}
1663
1664/**
1665 * @brief Compile the parsed augment connecting it into its target.
1666 *
1667 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1668 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1669 * are already implemented and compiled.
1670 *
1671 * @param[in] ctx Compile context.
1672 * @param[in] aug_p Parsed augment to compile.
1673 * @param[in] target Target node of the augment.
1674 * @return LY_SUCCESS on success.
1675 * @return LY_EVALID on failure.
1676 */
1677static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001678lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001679{
1680 LY_ERR ret = LY_SUCCESS;
1681 struct lysp_node *pnode;
1682 struct lysc_node *node;
1683 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001684 struct lysc_node_action **actions;
1685 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001686 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001687 struct ly_set child_set = {0};
Michal Vasko29dd11e2020-11-23 16:52:22 +01001688 uint32_t i, opt_prev = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001689
1690 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001691 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001692 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1693 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1694 ret = LY_EVALID;
1695 goto cleanup;
1696 }
1697
1698 /* check for mandatory nodes
1699 * - new cases augmenting some choice can have mandatory nodes
1700 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1701 */
1702 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1703 allow_mandatory = 1;
1704 }
1705
1706 LY_LIST_FOR(aug_p->child, pnode) {
1707 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1708 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1709 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1710 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001711 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001712 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1713 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1714 ret = LY_EVALID;
1715 goto cleanup;
1716 }
1717
1718 /* compile the children */
1719 if (target->nodetype == LYS_CHOICE) {
1720 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001721 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1722 if (target->nodetype == LYS_INPUT) {
1723 ctx->options |= LYS_COMPILE_RPC_INPUT;
1724 } else {
1725 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
1726 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001727 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001728 } else {
1729 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1730 }
1731
Michal Vasko29dd11e2020-11-23 16:52:22 +01001732 /* eval if-features again for the rest of this node processing */
1733 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1734 if (!enabled) {
1735 ctx->options |= LYS_COMPILE_DISABLED;
1736 }
1737
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001738 /* since the augment node is not present in the compiled tree, we need to pass some of its
1739 * statements to all its children */
1740 for (i = 0; i < child_set.count; ++i) {
1741 node = child_set.snodes[i];
1742 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1743 node->flags &= ~LYS_MAND_TRUE;
1744 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001745 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001746 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1747 ret = LY_EVALID;
1748 goto cleanup;
1749 }
1750
1751 if (aug_p->when) {
1752 /* pass augment's when to all the children */
Michal Vasko72244882021-01-12 15:21:05 +01001753 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001754 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod9062b02020-11-04 17:15:50 +01001755
1756 if ((node->nodetype == LYS_CONTAINER) && !(node->flags & LYS_PRESENCE)) {
1757 /* container with a when condition */
1758 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its "
1759 "inherited \"when\" condition.", node->name);
1760 node->flags |= LYS_PRESENCE;
1761 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001762 }
1763 }
1764 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001765
1766 /* restore options */
1767 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001768 }
1769
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001770 actions = lysc_node_actions_p(target);
1771 notifs = lysc_node_notifs_p(target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001772
1773 if (aug_p->actions) {
1774 if (!actions) {
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 RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001777 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001778 ret = LY_EVALID;
1779 goto cleanup;
1780 }
1781
1782 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001783 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001784 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001785
Michal Vasko012807e2021-02-03 09:52:18 +01001786 /* eval if-features again for the rest of this node processing */
1787 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1788 if (!enabled) {
1789 ctx->options |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001790 }
Michal Vasko012807e2021-02-03 09:52:18 +01001791
1792 /* since the augment node is not present in the compiled tree, we need to pass some of its
1793 * statements to all its children */
1794 for (i = 0; i < child_set.count; ++i) {
1795 node = child_set.snodes[i];
1796 if (aug_p->when) {
1797 /* pass augment's when to all the actions */
1798 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1799 LY_CHECK_GOTO(ret, cleanup);
1800 }
1801 }
1802 ly_set_erase(&child_set, NULL);
1803
1804 /* restore options */
1805 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001806 }
1807 }
1808 if (aug_p->notifs) {
1809 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001810 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001811 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001812 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001813 ret = LY_EVALID;
1814 goto cleanup;
1815 }
1816
1817 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001818 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001819 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001820
Michal Vasko012807e2021-02-03 09:52:18 +01001821 /* eval if-features again for the rest of this node processing */
1822 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1823 if (!enabled) {
1824 ctx->options |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001825 }
Michal Vasko012807e2021-02-03 09:52:18 +01001826
1827 /* since the augment node is not present in the compiled tree, we need to pass some of its
1828 * statements to all its children */
1829 for (i = 0; i < child_set.count; ++i) {
1830 node = child_set.snodes[i];
1831 if (aug_p->when) {
1832 /* pass augment's when to all the actions */
1833 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1834 LY_CHECK_GOTO(ret, cleanup);
1835 }
1836 }
1837 ly_set_erase(&child_set, NULL);
1838
1839 /* restore options */
1840 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001841 }
1842 }
1843
1844cleanup:
1845 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001846 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001847 return ret;
1848}
1849
1850LY_ERR
1851lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1852{
1853 LY_ERR ret = LY_SUCCESS;
1854 struct lys_module *orig_mod = ctx->cur_mod;
1855 struct lysp_module *orig_pmod = ctx->pmod;
1856 uint32_t i;
1857 char orig_path[LYSC_CTX_BUFSIZE];
1858 struct lysc_augment *aug;
1859
1860 /* uses augments */
1861 for (i = 0; i < ctx->uses_augs.count; ) {
1862 aug = ctx->uses_augs.objs[i];
1863
Michal Vasko28fe5f62021-03-03 16:37:39 +01001864 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001865 /* not our target node */
1866 ++i;
1867 continue;
1868 }
1869
Michal Vaskob8df5762021-01-12 15:15:53 +01001870 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001871 lysc_update_path(ctx, NULL, "{augment}");
1872 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001873 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001874
1875 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001876 ret = lys_compile_augment(ctx, aug->aug_p, node);
1877 lysc_update_path(ctx, NULL, NULL);
1878 lysc_update_path(ctx, NULL, NULL);
1879 LY_CHECK_GOTO(ret, cleanup);
1880
1881 /* augment was applied, remove it (index may have changed because other augments could have been applied) */
1882 ly_set_rm(&ctx->uses_augs, aug, NULL);
1883 lysc_augment_free(ctx->ctx, aug);
1884 }
1885
1886 /* top-level augments */
1887 for (i = 0; i < ctx->augs.count; ) {
1888 aug = ctx->augs.objs[i];
1889
Michal Vasko28fe5f62021-03-03 16:37:39 +01001890 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001891 /* not our target node */
1892 ++i;
1893 continue;
1894 }
1895
Michal Vaskob8df5762021-01-12 15:15:53 +01001896 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001897 strcpy(orig_path, ctx->path);
1898 ctx->path_len = 1;
1899 lysc_update_path(ctx, NULL, "{augment}");
1900 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001901 ctx->cur_mod = aug->aug_pmod->mod;
1902 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001903
1904 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001905 ret = lys_compile_augment(ctx, aug->aug_p, node);
1906 strcpy(ctx->path, orig_path);
1907 ctx->path_len = strlen(ctx->path);
1908 LY_CHECK_GOTO(ret, cleanup);
1909
1910 /* augment was applied, remove it */
1911 ly_set_rm(&ctx->augs, aug, NULL);
1912 lysc_augment_free(ctx->ctx, aug);
1913 }
1914
1915cleanup:
1916 ctx->cur_mod = orig_mod;
1917 ctx->pmod = orig_pmod;
1918 return ret;
1919}
1920
1921/**
1922 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1923 *
1924 * @param[in] ctx Compile context.
1925 * @param[in] aug_p Parsed augment to be applied.
1926 * @param[in] pmod Both current and prefix module for @p aug_p.
1927 * @return LY_ERR value.
1928 */
1929static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001930lys_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 +02001931{
1932 LY_ERR ret = LY_SUCCESS;
1933 struct lyxp_expr *exp = NULL;
1934 struct lysc_augment *aug;
1935 const struct lys_module *mod;
1936
1937 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1938 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
1939 LY_CHECK_GOTO(ret, cleanup);
1940
1941 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
1942 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
1943 if (mod != ctx->cur_mod) {
1944 /* augment for another module, ignore */
1945 goto cleanup;
1946 }
1947
1948 /* allocate new compiled augment and store it in the set */
1949 aug = calloc(1, sizeof *aug);
1950 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1951 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
1952
1953 aug->nodeid = exp;
1954 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001955 aug->aug_pmod = pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001956 aug->aug_p = aug_p;
1957
1958cleanup:
1959 lyxp_expr_free(ctx->ctx, exp);
1960 return ret;
1961}
1962
1963LY_ERR
1964lys_precompile_own_augments(struct lysc_ctx *ctx)
1965{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001966 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001967
1968 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001969 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
1970 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001971
1972 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001973 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
1974 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001975 }
1976
1977 /* collect all submodules augments */
1978 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001979 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
1980 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 +02001981 }
1982 }
1983 }
1984
1985 return LY_SUCCESS;
1986}
1987
1988/**
1989 * @brief Prepare a deviation to be applied during data nodes compilation.
1990 *
1991 * @param[in] ctx Compile context.
1992 * @param[in] dev_p Parsed deviation to be applied.
1993 * @param[in] pmod Both current and prefix module for @p dev_p.
1994 * @return LY_ERR value.
1995 */
1996static LY_ERR
1997lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
1998{
1999 LY_ERR ret = LY_SUCCESS;
2000 struct lysc_deviation *dev = NULL;
2001 struct lyxp_expr *exp = NULL;
2002 struct lysp_deviation **new_dev;
2003 const struct lys_module *mod;
2004 const struct lysp_module **new_dev_pmod;
2005 uint32_t i;
2006
2007 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2008 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2009 LY_CHECK_GOTO(ret, cleanup);
2010
2011 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2012 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2013 if (mod != ctx->cur_mod) {
2014 /* deviation for another module, ignore */
2015 goto cleanup;
2016 }
2017
2018 /* try to find the node in already compiled deviations */
2019 for (i = 0; i < ctx->devs.count; ++i) {
2020 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2021 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2022 dev = ctx->devs.objs[i];
2023 break;
2024 }
2025 }
2026
2027 if (!dev) {
2028 /* allocate new compiled deviation */
2029 dev = calloc(1, sizeof *dev);
2030 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2031 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2032
2033 dev->nodeid = exp;
2034 exp = NULL;
2035 }
2036
2037 /* add new parsed deviation structure */
2038 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2039 *new_dev = dev_p;
2040 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2041 *new_dev_pmod = pmod;
2042
2043cleanup:
2044 lyxp_expr_free(ctx->ctx, exp);
2045 return ret;
2046}
2047
2048LY_ERR
2049lys_precompile_own_deviations(struct lysc_ctx *ctx)
2050{
2051 LY_ARRAY_COUNT_TYPE u, v, w;
2052 const struct lys_module *dev_mod;
2053 struct lysc_deviation *dev;
2054 struct lysp_deviate *d;
2055 int not_supported;
2056 uint32_t i;
2057
2058 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2059 dev_mod = ctx->cur_mod->deviated_by[u];
2060
2061 /* compile all module deviations */
2062 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2063 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2064 }
2065
2066 /* compile all submodules deviations */
2067 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2068 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2069 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2070 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2071 }
2072 }
2073 }
2074
2075 /* set not-supported flags for all the deviations */
2076 for (i = 0; i < ctx->devs.count; ++i) {
2077 dev = ctx->devs.objs[i];
2078 not_supported = 0;
2079
2080 LY_ARRAY_FOR(dev->devs, u) {
2081 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2082 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2083 not_supported = 1;
2084 break;
2085 }
2086 }
2087 if (not_supported) {
2088 break;
2089 }
2090 }
2091 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002092 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002093 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
2094 return LY_EVALID;
2095 }
2096
2097 dev->not_supported = not_supported;
2098 }
2099
2100 return LY_SUCCESS;
2101}
2102
2103/**
2104 * @brief Add a module reference into an array, checks for duplicities.
2105 *
2106 * @param[in] ctx Compile context.
2107 * @param[in] mod Module reference to add.
2108 * @param[in,out] mod_array Module sized array to add to.
2109 * @return LY_ERR value.
2110 */
2111static LY_ERR
2112lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2113{
2114 LY_ARRAY_COUNT_TYPE u;
2115 struct lys_module **new_mod;
2116
2117 LY_ARRAY_FOR(*mod_array, u) {
2118 if ((*mod_array)[u] == mod) {
2119 /* already there */
2120 return LY_EEXIST;
2121 }
2122 }
2123
2124 /* add the new module ref */
2125 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2126 *new_mod = mod;
2127
2128 return LY_SUCCESS;
2129}
2130
2131LY_ERR
2132lys_precompile_augments_deviations(struct lysc_ctx *ctx)
2133{
Michal Vasko405cc9e2020-12-01 12:01:27 +01002134 LY_ERR ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002135 LY_ARRAY_COUNT_TYPE u, v;
2136 const struct lysp_module *mod_p;
2137 const struct lysc_node *target;
2138 struct lys_module *mod;
2139 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002140 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002141 ly_bool has_dev = 0;
2142 uint16_t flags;
2143 uint32_t idx, opt_prev = ctx->options;
2144
Michal Vasko405cc9e2020-12-01 12:01:27 +01002145 for (idx = 0; idx < ctx->unres->implementing.count; ++idx) {
2146 if (ctx->cur_mod == ctx->unres->implementing.objs[idx]) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002147 break;
2148 }
2149 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01002150 if (idx == ctx->unres->implementing.count) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002151 /* it was already implemented and all the augments and deviations fully applied */
2152 return LY_SUCCESS;
2153 }
2154
2155 mod_p = ctx->cur_mod->parsed;
2156
Radek Krejci2a9fc652021-01-22 17:44:34 +01002157 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002158 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002159 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002160
2161 /* get target module */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002162 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002163 LY_CHECK_RET(ret);
2164
2165 /* add this module into the target module augmented_by, if not there already from previous augments */
2166 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2167
2168 /* if we are compiling this module, we cannot add augments to it yet */
2169 if (mod != ctx->cur_mod) {
2170 /* apply the augment, find the target node first */
2171 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002172 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002173 (void *)mod_p, 0, &target, &flags);
2174 LY_CHECK_RET(ret);
2175
2176 /* apply the augment */
2177 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002178 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002179 ctx->options = opt_prev;
2180 LY_CHECK_RET(ret);
2181 }
2182
2183 lysc_update_path(ctx, NULL, NULL);
2184 lysc_update_path(ctx, NULL, NULL);
2185 }
2186
2187 LY_ARRAY_FOR(mod_p->deviations, u) {
2188 /* get target module */
2189 lysc_update_path(ctx, NULL, "{deviation}");
2190 lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid);
2191 ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL);
2192 lysc_update_path(ctx, NULL, NULL);
2193 lysc_update_path(ctx, NULL, NULL);
2194 LY_CHECK_RET(ret);
2195
2196 /* add this module into the target module deviated_by, if not there already from previous deviations */
2197 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2198
2199 /* new deviation added to the target module */
2200 has_dev = 1;
2201 }
2202
2203 /* the same for augments and deviations in submodules */
2204 LY_ARRAY_FOR(mod_p->includes, v) {
2205 submod = mod_p->includes[v].submodule;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002206 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002207 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002208 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002209
Radek Krejci2a9fc652021-01-22 17:44:34 +01002210 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002211 LY_CHECK_RET(ret);
2212
2213 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2214 if (mod != ctx->cur_mod) {
2215 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002216 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002217 submod, 0, &target, &flags);
2218 LY_CHECK_RET(ret);
2219
2220 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002221 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002222 ctx->options = opt_prev;
2223 LY_CHECK_RET(ret);
2224 }
2225
2226 lysc_update_path(ctx, NULL, NULL);
2227 lysc_update_path(ctx, NULL, NULL);
2228 }
2229
2230 LY_ARRAY_FOR(submod->deviations, u) {
2231 lysc_update_path(ctx, NULL, "{deviation}");
2232 lysc_update_path(ctx, NULL, submod->deviations[u].nodeid);
2233 ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL);
2234 lysc_update_path(ctx, NULL, NULL);
2235 lysc_update_path(ctx, NULL, NULL);
2236 LY_CHECK_RET(ret);
2237
2238 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2239 has_dev = 1;
2240 }
2241 }
2242
Michal Vasko405cc9e2020-12-01 12:01:27 +01002243 if (has_dev) {
2244 /* all modules (may) need to be recompiled */
2245 ctx->unres->recompile = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002246 }
2247
Michal Vasko405cc9e2020-12-01 12:01:27 +01002248 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002249}
2250
2251void
2252lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2253{
2254 uint32_t i;
2255 LY_ARRAY_COUNT_TYPE u, count;
2256 struct lys_module *m;
2257
2258 for (i = 0; i < ctx->list.count; ++i) {
2259 m = ctx->list.objs[i];
2260
2261 if (m->augmented_by) {
2262 count = LY_ARRAY_COUNT(m->augmented_by);
2263 for (u = 0; u < count; ++u) {
2264 if (m->augmented_by[u] == mod) {
2265 /* keep the order */
2266 if (u < count - 1) {
2267 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2268 }
2269 LY_ARRAY_DECREMENT(m->augmented_by);
2270 break;
2271 }
2272 }
2273 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2274 LY_ARRAY_FREE(m->augmented_by);
2275 m->augmented_by = NULL;
2276 }
2277 }
2278
2279 if (m->deviated_by) {
2280 count = LY_ARRAY_COUNT(m->deviated_by);
2281 for (u = 0; u < count; ++u) {
2282 if (m->deviated_by[u] == mod) {
2283 /* keep the order */
2284 if (u < count - 1) {
2285 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2286 }
2287 LY_ARRAY_DECREMENT(m->deviated_by);
2288 break;
2289 }
2290 }
2291 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2292 LY_ARRAY_FREE(m->deviated_by);
2293 m->deviated_by = NULL;
2294 }
2295 }
2296 }
2297}