blob: 240804cda54704455863207f151fbb6df7cedbe1 [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;
199 aug->nodeid_pmod = ctx->pmod;
200 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;
232 rfn->nodeid_pmod = ctx->pmod;
233 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
659 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
660 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
680 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
681 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) {
690 FREE_STRING(ctx->ctx, target->dsc);
691 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
692 }
693
694 /* reference */
695 if (rfn->ref) {
696 FREE_STRING(ctx->ctx, target->ref);
697 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
698 }
699
700 /* config */
701 if (rfn->flags & LYS_CONFIG_MASK) {
702 if (ctx->options & (LYS_COMPILE_NOTIFICATION | LYS_COMPILE_RPC_INPUT | LYS_COMPILE_RPC_OUTPUT)) {
703 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
704 ctx->options & LYS_COMPILE_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
705 } else {
706 target->flags &= ~LYS_CONFIG_MASK;
707 target->flags |= rfn->flags & LYS_CONFIG_MASK;
708 }
709 }
710
711 /* mandatory */
712 if (rfn->flags & LYS_MAND_MASK) {
713 switch (target->nodetype) {
714 case LYS_LEAF:
715 case LYS_CHOICE:
716 case LYS_ANYDATA:
717 case LYS_ANYXML:
718 break;
719 default:
720 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
721 }
722
723 target->flags &= ~LYS_MAND_MASK;
724 target->flags |= rfn->flags & LYS_MAND_MASK;
725 }
726
727 /* presence */
728 if (rfn->presence) {
729 switch (target->nodetype) {
730 case LYS_CONTAINER:
731 break;
732 default:
733 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
734 }
735
736 FREE_STRING(ctx->ctx, ((struct lysp_node_container *)target)->presence);
737 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
738 }
739
740 /* must */
741 if (rfn->musts) {
742 switch (target->nodetype) {
743 case LYS_CONTAINER:
744 case LYS_LIST:
745 case LYS_LEAF:
746 case LYS_LEAFLIST:
747 case LYS_ANYDATA:
748 case LYS_ANYXML:
749 musts = &((struct lysp_node_container *)target)->musts;
750 break;
751 default:
752 AMEND_WRONG_NODETYPE("refine", "add", "must");
753 }
754
755 LY_ARRAY_FOR(rfn->musts, u) {
756 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
757 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
758 }
759 }
760
761 /* min-elements */
762 if (rfn->flags & LYS_SET_MIN) {
763 switch (target->nodetype) {
764 case LYS_LEAFLIST:
765 num = &((struct lysp_node_leaflist *)target)->min;
766 break;
767 case LYS_LIST:
768 num = &((struct lysp_node_list *)target)->min;
769 break;
770 default:
771 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
772 }
773
774 *num = rfn->min;
775 }
776
777 /* max-elements */
778 if (rfn->flags & LYS_SET_MAX) {
779 switch (target->nodetype) {
780 case LYS_LEAFLIST:
781 num = &((struct lysp_node_leaflist *)target)->max;
782 break;
783 case LYS_LIST:
784 num = &((struct lysp_node_list *)target)->max;
785 break;
786 default:
787 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
788 }
789
790 *num = rfn->max;
791 }
792
793 /* if-feature */
794 if (rfn->iffeatures) {
795 switch (target->nodetype) {
796 case LYS_LEAF:
797 case LYS_LEAFLIST:
798 case LYS_LIST:
799 case LYS_CONTAINER:
800 case LYS_CHOICE:
801 case LYS_CASE:
802 case LYS_ANYDATA:
803 case LYS_ANYXML:
804 break;
805 default:
806 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
807 }
808
809 LY_ARRAY_FOR(rfn->iffeatures, u) {
810 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
811 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
812 }
813 }
814
815 /* extension */
816 /* TODO refine extensions */
817
818cleanup:
819 return ret;
820}
821
822/**
823 * @brief Apply deviate add.
824 *
825 * @param[in] ctx Compile context.
826 * @param[in] d Deviate add to apply.
827 * @param[in,out] target Deviation target.
828 * @return LY_ERR value.
829 */
830static LY_ERR
831lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
832{
833 LY_ERR ret = LY_SUCCESS;
834 LY_ARRAY_COUNT_TYPE u;
835 struct lysp_qname *qname;
836 uint32_t *num;
837 struct lysp_restr **musts, *must;
838
839#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
840 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100841 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200842 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
843 ret = LY_EVALID; \
844 goto cleanup; \
845 }
846
847 /* [units-stmt] */
848 if (d->units) {
849 switch (target->nodetype) {
850 case LYS_LEAF:
851 case LYS_LEAFLIST:
852 break;
853 default:
854 AMEND_WRONG_NODETYPE("deviation", "add", "units");
855 }
856
857 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
858 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
859 }
860
861 /* *must-stmt */
862 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100863 musts = lysp_node_musts_p(target);
864 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200865 AMEND_WRONG_NODETYPE("deviation", "add", "must");
866 }
867
868 LY_ARRAY_FOR(d->musts, u) {
869 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
870 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
871 }
872 }
873
874 /* *unique-stmt */
875 if (d->uniques) {
876 switch (target->nodetype) {
877 case LYS_LIST:
878 break;
879 default:
880 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
881 }
882
883 LY_ARRAY_FOR(d->uniques, u) {
884 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
885 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
886 }
887 }
888
889 /* *default-stmt */
890 if (d->dflts) {
891 switch (target->nodetype) {
892 case LYS_LEAF:
893 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
894 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
895
896 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
897 break;
898 case LYS_LEAFLIST:
899 LY_ARRAY_FOR(d->dflts, u) {
900 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
901 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
902 }
903 break;
904 case LYS_CHOICE:
905 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
906 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
907
908 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
909 break;
910 default:
911 AMEND_WRONG_NODETYPE("deviation", "add", "default");
912 }
913 }
914
915 /* [config-stmt] */
916 if (d->flags & LYS_CONFIG_MASK) {
917 switch (target->nodetype) {
918 case LYS_CONTAINER:
919 case LYS_LEAF:
920 case LYS_LEAFLIST:
921 case LYS_LIST:
922 case LYS_CHOICE:
923 case LYS_ANYDATA:
924 case LYS_ANYXML:
925 break;
926 default:
927 AMEND_WRONG_NODETYPE("deviation", "add", "config");
928 }
929
930 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100931 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200932 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
933 target->flags & LYS_CONFIG_W ? "true" : "false");
934 ret = LY_EVALID;
935 goto cleanup;
936 }
937
938 target->flags |= d->flags & LYS_CONFIG_MASK;
939 }
940
941 /* [mandatory-stmt] */
942 if (d->flags & LYS_MAND_MASK) {
943 switch (target->nodetype) {
944 case LYS_LEAF:
945 case LYS_CHOICE:
946 case LYS_ANYDATA:
947 case LYS_ANYXML:
948 break;
949 default:
950 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
951 }
952
953 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100954 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200955 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
956 target->flags & LYS_MAND_TRUE ? "true" : "false");
957 ret = LY_EVALID;
958 goto cleanup;
959 }
960
961 target->flags |= d->flags & LYS_MAND_MASK;
962 }
963
964 /* [min-elements-stmt] */
965 if (d->flags & LYS_SET_MIN) {
966 switch (target->nodetype) {
967 case LYS_LEAFLIST:
968 num = &((struct lysp_node_leaflist *)target)->min;
969 break;
970 case LYS_LIST:
971 num = &((struct lysp_node_list *)target)->min;
972 break;
973 default:
974 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
975 }
976
977 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100978 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200979 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
980 ret = LY_EVALID;
981 goto cleanup;
982 }
983
984 *num = d->min;
985 }
986
987 /* [max-elements-stmt] */
988 if (d->flags & LYS_SET_MAX) {
989 switch (target->nodetype) {
990 case LYS_LEAFLIST:
991 num = &((struct lysp_node_leaflist *)target)->max;
992 break;
993 case LYS_LIST:
994 num = &((struct lysp_node_list *)target)->max;
995 break;
996 default:
997 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
998 }
999
1000 if (target->flags & LYS_SET_MAX) {
1001 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001002 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001003 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1004 *num);
1005 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001006 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001007 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1008 }
1009 ret = LY_EVALID;
1010 goto cleanup;
1011 }
1012
1013 *num = d->max;
1014 }
1015
1016cleanup:
1017 return ret;
1018}
1019
1020/**
1021 * @brief Apply deviate delete.
1022 *
1023 * @param[in] ctx Compile context.
1024 * @param[in] d Deviate delete to apply.
1025 * @param[in,out] target Deviation target.
1026 * @return LY_ERR value.
1027 */
1028static LY_ERR
1029lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1030{
1031 LY_ERR ret = LY_SUCCESS;
1032 struct lysp_restr **musts;
1033 LY_ARRAY_COUNT_TYPE u, v;
1034 struct lysp_qname **uniques, **dflts;
1035
1036#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1037 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1038 int found = 0; \
1039 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1040 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1041 found = 1; \
1042 break; \
1043 } \
1044 } \
1045 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001046 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001047 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1048 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1049 ret = LY_EVALID; \
1050 goto cleanup; \
1051 } \
1052 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1053 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
1054 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1055 } \
1056 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1057 LY_ARRAY_FREE(ORIG_ARRAY); \
1058 ORIG_ARRAY = NULL; \
1059 }
1060
1061#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1062 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001063 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001064 ret = LY_EVALID; \
1065 goto cleanup; \
1066 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001067 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001068 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1069 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1070 ret = LY_EVALID; \
1071 goto cleanup; \
1072 }
1073
1074 /* [units-stmt] */
1075 if (d->units) {
1076 switch (target->nodetype) {
1077 case LYS_LEAF:
1078 case LYS_LEAFLIST:
1079 break;
1080 default:
1081 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1082 }
1083
1084 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
1085 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1086 ((struct lysp_node_leaf *)target)->units = NULL;
1087 }
1088
1089 /* *must-stmt */
1090 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001091 musts = lysp_node_musts_p(target);
1092 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001093 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1094 }
1095
1096 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1097 }
1098
1099 /* *unique-stmt */
1100 if (d->uniques) {
1101 switch (target->nodetype) {
1102 case LYS_LIST:
1103 break;
1104 default:
1105 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1106 }
1107
1108 uniques = &((struct lysp_node_list *)target)->uniques;
1109 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1110 }
1111
1112 /* *default-stmt */
1113 if (d->dflts) {
1114 switch (target->nodetype) {
1115 case LYS_LEAF:
1116 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1117 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1118
1119 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1120 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1121 break;
1122 case LYS_LEAFLIST:
1123 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1124 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1125 break;
1126 case LYS_CHOICE:
1127 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1128 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1129
1130 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1131 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1132 break;
1133 default:
1134 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1135 }
1136 }
1137
1138cleanup:
1139 return ret;
1140}
1141
1142/**
1143 * @brief Apply deviate replace.
1144 *
1145 * @param[in] ctx Compile context.
1146 * @param[in] d Deviate replace to apply.
1147 * @param[in,out] target Deviation target.
1148 * @return LY_ERR value.
1149 */
1150static LY_ERR
1151lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1152{
1153 LY_ERR ret = LY_SUCCESS;
1154 uint32_t *num;
1155
1156#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1157 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001158 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001159 ret = LY_EVALID; \
1160 goto cleanup; \
1161 }
1162
1163 /* [type-stmt] */
1164 if (d->type) {
1165 switch (target->nodetype) {
1166 case LYS_LEAF:
1167 case LYS_LEAFLIST:
1168 break;
1169 default:
1170 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1171 }
1172
1173 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1174 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1175 }
1176
1177 /* [units-stmt] */
1178 if (d->units) {
1179 switch (target->nodetype) {
1180 case LYS_LEAF:
1181 case LYS_LEAFLIST:
1182 break;
1183 default:
1184 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1185 }
1186
1187 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
1188 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1189 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1190 }
1191
1192 /* [default-stmt] */
1193 if (d->dflt.str) {
1194 switch (target->nodetype) {
1195 case LYS_LEAF:
1196 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1197
1198 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1199 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1200 break;
1201 case LYS_CHOICE:
1202 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1203
1204 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1205 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1206 break;
1207 default:
1208 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1209 }
1210 }
1211
1212 /* [config-stmt] */
1213 if (d->flags & LYS_CONFIG_MASK) {
1214 switch (target->nodetype) {
1215 case LYS_CONTAINER:
1216 case LYS_LEAF:
1217 case LYS_LEAFLIST:
1218 case LYS_LIST:
1219 case LYS_CHOICE:
1220 case LYS_ANYDATA:
1221 case LYS_ANYXML:
1222 break;
1223 default:
1224 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1225 }
1226
1227 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001228 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1229 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001230 ret = LY_EVALID;
1231 goto cleanup;
1232 }
1233
1234 target->flags &= ~LYS_CONFIG_MASK;
1235 target->flags |= d->flags & LYS_CONFIG_MASK;
1236 }
1237
1238 /* [mandatory-stmt] */
1239 if (d->flags & LYS_MAND_MASK) {
1240 switch (target->nodetype) {
1241 case LYS_LEAF:
1242 case LYS_CHOICE:
1243 case LYS_ANYDATA:
1244 case LYS_ANYXML:
1245 break;
1246 default:
1247 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1248 }
1249
1250 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001251 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1252 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001253 ret = LY_EVALID;
1254 goto cleanup;
1255 }
1256
1257 target->flags &= ~LYS_MAND_MASK;
1258 target->flags |= d->flags & LYS_MAND_MASK;
1259 }
1260
1261 /* [min-elements-stmt] */
1262 if (d->flags & LYS_SET_MIN) {
1263 switch (target->nodetype) {
1264 case LYS_LEAFLIST:
1265 num = &((struct lysp_node_leaflist *)target)->min;
1266 break;
1267 case LYS_LIST:
1268 num = &((struct lysp_node_list *)target)->min;
1269 break;
1270 default:
1271 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1272 }
1273
1274 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001275 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001276 ret = LY_EVALID;
1277 goto cleanup;
1278 }
1279
1280 *num = d->min;
1281 }
1282
1283 /* [max-elements-stmt] */
1284 if (d->flags & LYS_SET_MAX) {
1285 switch (target->nodetype) {
1286 case LYS_LEAFLIST:
1287 num = &((struct lysp_node_leaflist *)target)->max;
1288 break;
1289 case LYS_LIST:
1290 num = &((struct lysp_node_list *)target)->max;
1291 break;
1292 default:
1293 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1294 }
1295
1296 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001297 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001298 ret = LY_EVALID;
1299 goto cleanup;
1300 }
1301
1302 *num = d->max;
1303 }
1304
1305cleanup:
1306 return ret;
1307}
1308
1309/**
1310 * @brief Get module of a single nodeid node name test.
1311 *
1312 * @param[in] ctx libyang context.
1313 * @param[in] nametest Nametest with an optional prefix.
1314 * @param[in] nametest_len Length of @p nametest.
1315 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1316 * @param[out] name Optional pointer to the name test without the prefix.
1317 * @param[out] name_len Length of @p name.
1318 * @return Resolved module.
1319 */
1320static const struct lys_module *
1321lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1322 const struct lysp_module *mod, const char **name, size_t *name_len)
1323{
1324 const struct lys_module *target_mod;
1325 const char *ptr;
1326
1327 ptr = ly_strnchr(nametest, ':', nametest_len);
1328 if (ptr) {
1329 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)mod);
1330 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001331 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001332 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
1333 nametest_len, nametest, ptr - nametest, nametest, LYSP_MODULE_NAME(mod));
1334 return NULL;
1335 }
1336
1337 if (name) {
1338 *name = ptr + 1;
1339 *name_len = nametest_len - ((ptr - nametest) + 1);
1340 }
1341 } else {
1342 target_mod = mod->mod;
1343 if (name) {
1344 *name = nametest;
1345 *name_len = nametest_len;
1346 }
1347 }
1348
1349 return target_mod;
1350}
1351
1352/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001353 * @brief Check whether a compiled node matches a single schema nodeid name test.
1354 *
1355 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1356 * @param[in] mod Expected module.
1357 * @param[in] name Expected name.
1358 * @param[in] name_len Length of @p name.
1359 * @return Whether it is a match or not.
1360 */
1361static ly_bool
1362lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1363 size_t name_len)
1364{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001365 const char *node_name;
1366
1367 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001368 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001369 return 0;
1370 }
1371
1372 /* compare names */
1373 if ((*node)->nodetype == LYS_INPUT) {
1374 node_name = "input";
1375 } else if ((*node)->nodetype == LYS_OUTPUT) {
1376 node_name = "output";
1377 } else {
1378 node_name = (*node)->name;
1379 }
1380 if (ly_strncmp(node_name, name, name_len)) {
1381 return 0;
1382 }
1383
Michal Vasko2a668712020-10-21 11:48:09 +02001384 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001385 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001386
1387 return 1;
1388}
1389
1390/**
1391 * @brief Check whether a node matches specific schema nodeid.
1392 *
1393 * @param[in] exp Parsed nodeid to match.
1394 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1395 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1396 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1397 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1398 * @param[in] pnode_mod Compiled @p pnode to-be module.
1399 * @return Whether it is a match or not.
1400 */
1401static ly_bool
1402lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1403 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1404{
1405 uint32_t i;
1406 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001407 const char *name = NULL;
1408 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001409
1410 /* compare last node in the node ID */
1411 i = exp->used - 1;
1412
1413 /* get exp node ID module */
1414 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);
1415 assert(mod);
1416
1417 if (pnode) {
1418 /* compare on the last parsed-only node */
Radek Krejci9a3823e2021-01-27 20:26:46 +01001419 if (pnode_mod != mod || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001420 return 0;
1421 }
1422 } else {
1423 /* using parent directly */
1424 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1425 return 0;
1426 }
1427 }
1428
1429 /* now compare all the compiled parents */
1430 while (i > 1) {
1431 i -= 2;
1432 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1433
1434 if (!parent) {
1435 /* no more parents but path continues */
1436 return 0;
1437 }
1438
1439 /* get exp node ID module */
1440 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1441 &name_len);
1442 assert(mod);
1443
1444 /* compare with the parent */
1445 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1446 return 0;
1447 }
1448 }
1449
1450 if (ctx_node && (ctx_node != parent)) {
1451 /* descendant path has not finished in the context node */
1452 return 0;
1453 } else if (!ctx_node && parent) {
1454 /* some parent was not matched */
1455 return 0;
1456 }
1457
1458 return 1;
1459}
1460
1461void
1462lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1463{
1464 if (aug) {
1465 lyxp_expr_free(ctx, aug->nodeid);
1466
1467 free(aug);
1468 }
1469}
1470
1471void
1472lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1473{
1474 if (dev) {
1475 lyxp_expr_free(ctx, dev->nodeid);
1476 LY_ARRAY_FREE(dev->devs);
1477 LY_ARRAY_FREE(dev->dev_pmods);
1478
1479 free(dev);
1480 }
1481}
1482
1483void
1484lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1485{
1486 if (rfn) {
1487 lyxp_expr_free(ctx, rfn->nodeid);
1488 LY_ARRAY_FREE(rfn->rfns);
1489
1490 free(rfn);
1491 }
1492}
1493
1494void
1495lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1496{
1497 if (!dev_pnode) {
1498 return;
1499 }
1500
1501 switch (dev_pnode->nodetype) {
1502 case LYS_CONTAINER:
1503 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1504 break;
1505 case LYS_LIST:
1506 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1507 break;
1508 case LYS_CHOICE:
1509 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1510 break;
1511 case LYS_CASE:
1512 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1513 break;
1514 case LYS_LEAF:
1515 case LYS_LEAFLIST:
1516 case LYS_ANYXML:
1517 case LYS_ANYDATA:
1518 /* no children */
1519 break;
1520 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001521 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001522 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001523 case LYS_RPC:
1524 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001525 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1526 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001527 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001528 case LYS_INPUT:
1529 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001530 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001531 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001532 free(dev_pnode);
1533 return;
1534 default:
1535 LOGINT(ctx);
1536 return;
1537 }
1538
1539 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1540}
1541
1542LY_ERR
1543lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1544 struct lysp_node **dev_pnode, ly_bool *not_supported)
1545{
1546 LY_ERR ret = LY_SUCCESS;
1547 uint32_t i;
1548 LY_ARRAY_COUNT_TYPE u;
1549 struct lys_module *orig_mod = ctx->cur_mod;
1550 struct lysp_module *orig_pmod = ctx->pmod;
1551 char orig_path[LYSC_CTX_BUFSIZE];
1552 struct lysc_refine *rfn;
1553 struct lysc_deviation *dev;
1554 struct lysp_deviation *dev_p;
1555 struct lysp_deviate *d;
1556
1557 *dev_pnode = NULL;
1558 *not_supported = 0;
1559
1560 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1561 rfn = ctx->uses_rfns.objs[i];
1562
1563 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, ctx->cur_mod)) {
1564 /* not our target node */
1565 continue;
1566 }
1567
1568 if (!*dev_pnode) {
1569 /* first refine on this node, create a copy first */
1570 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1571 }
1572
Michal Vaskob8df5762021-01-12 15:15:53 +01001573 /* use modules from the refine */
1574 ctx->cur_mod = rfn->nodeid_pmod->mod;
1575 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1576
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001577 /* apply all the refines by changing (the copy of) the parsed node */
1578 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001579 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001580 lysc_update_path(ctx, NULL, "{refine}");
1581 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001582
1583 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001584 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1585 lysc_update_path(ctx, NULL, NULL);
1586 lysc_update_path(ctx, NULL, NULL);
1587 LY_CHECK_GOTO(ret, cleanup);
1588 }
1589
1590 /* refine was applied, remove it */
1591 lysc_refine_free(ctx->ctx, rfn);
1592 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1593
1594 /* all the refines for one target node are in one structure, we are done */
1595 break;
1596 }
1597
1598 for (i = 0; i < ctx->devs.count; ++i) {
1599 dev = ctx->devs.objs[i];
1600
1601 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, ctx->cur_mod)) {
1602 /* not our target node */
1603 continue;
1604 }
1605
1606 if (dev->not_supported) {
1607 /* it is not supported, no more deviations */
1608 *not_supported = 1;
1609 goto dev_applied;
1610 }
1611
1612 if (!*dev_pnode) {
1613 /* first deviation on this node, create a copy first */
1614 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1615 }
1616
1617 /* apply all the deviates by changing (the copy of) the parsed node */
1618 LY_ARRAY_FOR(dev->devs, u) {
1619 dev_p = dev->devs[u];
1620 LY_LIST_FOR(dev_p->deviates, d) {
1621 /* generate correct path */
1622 strcpy(orig_path, ctx->path);
1623 ctx->path_len = 1;
1624 ctx->cur_mod = dev->dev_pmods[u]->mod;
1625 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1626 lysc_update_path(ctx, NULL, "{deviation}");
1627 lysc_update_path(ctx, NULL, dev_p->nodeid);
1628
1629 switch (d->mod) {
1630 case LYS_DEV_ADD:
1631 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1632 break;
1633 case LYS_DEV_DELETE:
1634 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1635 break;
1636 case LYS_DEV_REPLACE:
1637 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1638 break;
1639 default:
1640 LOGINT(ctx->ctx);
1641 ret = LY_EINT;
1642 }
1643
1644 /* restore previous path */
1645 strcpy(ctx->path, orig_path);
1646 ctx->path_len = strlen(ctx->path);
1647 ctx->cur_mod = orig_mod;
1648 ctx->pmod = orig_pmod;
1649
1650 LY_CHECK_GOTO(ret, cleanup);
1651 }
1652 }
1653
1654dev_applied:
1655 /* deviation was applied, remove it */
1656 lysc_deviation_free(ctx->ctx, dev);
1657 ly_set_rm_index(&ctx->devs, i, NULL);
1658
1659 /* all the deviations for one target node are in one structure, we are done */
1660 break;
1661 }
1662
1663cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001664 ctx->cur_mod = orig_mod;
1665 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001666 if (ret) {
1667 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1668 *dev_pnode = NULL;
1669 *not_supported = 0;
1670 }
1671 return ret;
1672}
1673
1674/**
1675 * @brief Compile the parsed augment connecting it into its target.
1676 *
1677 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1678 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1679 * are already implemented and compiled.
1680 *
1681 * @param[in] ctx Compile context.
1682 * @param[in] aug_p Parsed augment to compile.
1683 * @param[in] target Target node of the augment.
1684 * @return LY_SUCCESS on success.
1685 * @return LY_EVALID on failure.
1686 */
1687static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001688lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001689{
1690 LY_ERR ret = LY_SUCCESS;
1691 struct lysp_node *pnode;
1692 struct lysc_node *node;
1693 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001694 struct lysc_node_action **actions;
1695 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001696 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001697 struct ly_set child_set = {0};
Michal Vasko29dd11e2020-11-23 16:52:22 +01001698 uint32_t i, opt_prev = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001699
1700 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001701 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001702 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1703 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1704 ret = LY_EVALID;
1705 goto cleanup;
1706 }
1707
1708 /* check for mandatory nodes
1709 * - new cases augmenting some choice can have mandatory nodes
1710 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1711 */
1712 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1713 allow_mandatory = 1;
1714 }
1715
1716 LY_LIST_FOR(aug_p->child, pnode) {
1717 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1718 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1719 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1720 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001721 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001722 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1723 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1724 ret = LY_EVALID;
1725 goto cleanup;
1726 }
1727
1728 /* compile the children */
1729 if (target->nodetype == LYS_CHOICE) {
1730 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001731 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1732 if (target->nodetype == LYS_INPUT) {
1733 ctx->options |= LYS_COMPILE_RPC_INPUT;
1734 } else {
1735 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
1736 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001737 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001738 } else {
1739 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1740 }
1741
Michal Vasko29dd11e2020-11-23 16:52:22 +01001742 /* eval if-features again for the rest of this node processing */
1743 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1744 if (!enabled) {
1745 ctx->options |= LYS_COMPILE_DISABLED;
1746 }
1747
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001748 /* since the augment node is not present in the compiled tree, we need to pass some of its
1749 * statements to all its children */
1750 for (i = 0; i < child_set.count; ++i) {
1751 node = child_set.snodes[i];
1752 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1753 node->flags &= ~LYS_MAND_TRUE;
1754 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001755 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001756 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1757 ret = LY_EVALID;
1758 goto cleanup;
1759 }
1760
1761 if (aug_p->when) {
1762 /* pass augment's when to all the children */
Michal Vasko72244882021-01-12 15:21:05 +01001763 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 +02001764 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod9062b02020-11-04 17:15:50 +01001765
1766 if ((node->nodetype == LYS_CONTAINER) && !(node->flags & LYS_PRESENCE)) {
1767 /* container with a when condition */
1768 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its "
1769 "inherited \"when\" condition.", node->name);
1770 node->flags |= LYS_PRESENCE;
1771 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001772 }
1773 }
1774 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001775
1776 /* restore options */
1777 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001778 }
1779
1780 switch (target->nodetype) {
1781 case LYS_CONTAINER:
1782 actions = &((struct lysc_node_container *)target)->actions;
1783 notifs = &((struct lysc_node_container *)target)->notifs;
1784 break;
1785 case LYS_LIST:
1786 actions = &((struct lysc_node_list *)target)->actions;
1787 notifs = &((struct lysc_node_list *)target)->notifs;
1788 break;
1789 default:
1790 actions = NULL;
1791 notifs = NULL;
1792 break;
1793 }
1794
1795 if (aug_p->actions) {
1796 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001797 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001798 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001799 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001800 ret = LY_EVALID;
1801 goto cleanup;
1802 }
1803
1804 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001805 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
1806 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, NULL), cleanup);
1807 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001808
1809 if (aug_p->when) {
1810 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001811 struct lysc_node *iter;
1812
1813 LY_LIST_FOR((struct lysc_node *)*actions, iter) {
1814 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), iter, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001815 LY_CHECK_GOTO(ret, cleanup);
1816 }
1817 }
1818 }
1819 if (aug_p->notifs) {
1820 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001821 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001822 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001823 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001824 ret = LY_EVALID;
1825 goto cleanup;
1826 }
1827
1828 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001829 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
1830 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, NULL), cleanup);
1831 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001832
1833 if (aug_p->when) {
1834 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001835 struct lysc_node *iter;
1836
1837 LY_LIST_FOR((struct lysc_node *)*notifs, iter) {
1838 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), iter, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001839 LY_CHECK_GOTO(ret, cleanup);
1840 }
1841 }
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
1864 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, aug->nodeid_ctx_node, node, NULL, NULL)) {
1865 /* 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 Vaskob8df5762021-01-12 15:15:53 +01001873 ctx->cur_mod = aug->nodeid_pmod->mod;
1874 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
1875
1876 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001877 ret = lys_compile_augment(ctx, aug->aug_p, node);
1878 lysc_update_path(ctx, NULL, NULL);
1879 lysc_update_path(ctx, NULL, NULL);
1880 LY_CHECK_GOTO(ret, cleanup);
1881
1882 /* augment was applied, remove it (index may have changed because other augments could have been applied) */
1883 ly_set_rm(&ctx->uses_augs, aug, NULL);
1884 lysc_augment_free(ctx->ctx, aug);
1885 }
1886
1887 /* top-level augments */
1888 for (i = 0; i < ctx->augs.count; ) {
1889 aug = ctx->augs.objs[i];
1890
1891 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, NULL, node, NULL, NULL)) {
1892 /* not our target node */
1893 ++i;
1894 continue;
1895 }
1896
Michal Vaskob8df5762021-01-12 15:15:53 +01001897 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001898 strcpy(orig_path, ctx->path);
1899 ctx->path_len = 1;
1900 lysc_update_path(ctx, NULL, "{augment}");
1901 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
1902 ctx->cur_mod = aug->nodeid_pmod->mod;
1903 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001904
1905 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001906 ret = lys_compile_augment(ctx, aug->aug_p, node);
1907 strcpy(ctx->path, orig_path);
1908 ctx->path_len = strlen(ctx->path);
1909 LY_CHECK_GOTO(ret, cleanup);
1910
1911 /* augment was applied, remove it */
1912 ly_set_rm(&ctx->augs, aug, NULL);
1913 lysc_augment_free(ctx->ctx, aug);
1914 }
1915
1916cleanup:
1917 ctx->cur_mod = orig_mod;
1918 ctx->pmod = orig_pmod;
1919 return ret;
1920}
1921
1922/**
1923 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1924 *
1925 * @param[in] ctx Compile context.
1926 * @param[in] aug_p Parsed augment to be applied.
1927 * @param[in] pmod Both current and prefix module for @p aug_p.
1928 * @return LY_ERR value.
1929 */
1930static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001931lys_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 +02001932{
1933 LY_ERR ret = LY_SUCCESS;
1934 struct lyxp_expr *exp = NULL;
1935 struct lysc_augment *aug;
1936 const struct lys_module *mod;
1937
1938 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1939 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
1940 LY_CHECK_GOTO(ret, cleanup);
1941
1942 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
1943 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
1944 if (mod != ctx->cur_mod) {
1945 /* augment for another module, ignore */
1946 goto cleanup;
1947 }
1948
1949 /* allocate new compiled augment and store it in the set */
1950 aug = calloc(1, sizeof *aug);
1951 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1952 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
1953
1954 aug->nodeid = exp;
1955 exp = NULL;
1956 aug->nodeid_pmod = pmod;
1957 aug->aug_p = aug_p;
1958
1959cleanup:
1960 lyxp_expr_free(ctx->ctx, exp);
1961 return ret;
1962}
1963
1964LY_ERR
1965lys_precompile_own_augments(struct lysc_ctx *ctx)
1966{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001967 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001968
1969 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001970 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
1971 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001972
1973 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001974 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
1975 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001976 }
1977
1978 /* collect all submodules augments */
1979 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001980 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
1981 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 +02001982 }
1983 }
1984 }
1985
1986 return LY_SUCCESS;
1987}
1988
1989/**
1990 * @brief Prepare a deviation to be applied during data nodes compilation.
1991 *
1992 * @param[in] ctx Compile context.
1993 * @param[in] dev_p Parsed deviation to be applied.
1994 * @param[in] pmod Both current and prefix module for @p dev_p.
1995 * @return LY_ERR value.
1996 */
1997static LY_ERR
1998lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
1999{
2000 LY_ERR ret = LY_SUCCESS;
2001 struct lysc_deviation *dev = NULL;
2002 struct lyxp_expr *exp = NULL;
2003 struct lysp_deviation **new_dev;
2004 const struct lys_module *mod;
2005 const struct lysp_module **new_dev_pmod;
2006 uint32_t i;
2007
2008 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2009 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2010 LY_CHECK_GOTO(ret, cleanup);
2011
2012 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2013 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2014 if (mod != ctx->cur_mod) {
2015 /* deviation for another module, ignore */
2016 goto cleanup;
2017 }
2018
2019 /* try to find the node in already compiled deviations */
2020 for (i = 0; i < ctx->devs.count; ++i) {
2021 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2022 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2023 dev = ctx->devs.objs[i];
2024 break;
2025 }
2026 }
2027
2028 if (!dev) {
2029 /* allocate new compiled deviation */
2030 dev = calloc(1, sizeof *dev);
2031 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2032 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2033
2034 dev->nodeid = exp;
2035 exp = NULL;
2036 }
2037
2038 /* add new parsed deviation structure */
2039 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2040 *new_dev = dev_p;
2041 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2042 *new_dev_pmod = pmod;
2043
2044cleanup:
2045 lyxp_expr_free(ctx->ctx, exp);
2046 return ret;
2047}
2048
2049LY_ERR
2050lys_precompile_own_deviations(struct lysc_ctx *ctx)
2051{
2052 LY_ARRAY_COUNT_TYPE u, v, w;
2053 const struct lys_module *dev_mod;
2054 struct lysc_deviation *dev;
2055 struct lysp_deviate *d;
2056 int not_supported;
2057 uint32_t i;
2058
2059 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2060 dev_mod = ctx->cur_mod->deviated_by[u];
2061
2062 /* compile all module deviations */
2063 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2064 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2065 }
2066
2067 /* compile all submodules deviations */
2068 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2069 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2070 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2071 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2072 }
2073 }
2074 }
2075
2076 /* set not-supported flags for all the deviations */
2077 for (i = 0; i < ctx->devs.count; ++i) {
2078 dev = ctx->devs.objs[i];
2079 not_supported = 0;
2080
2081 LY_ARRAY_FOR(dev->devs, u) {
2082 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2083 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2084 not_supported = 1;
2085 break;
2086 }
2087 }
2088 if (not_supported) {
2089 break;
2090 }
2091 }
2092 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002093 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002094 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
2095 return LY_EVALID;
2096 }
2097
2098 dev->not_supported = not_supported;
2099 }
2100
2101 return LY_SUCCESS;
2102}
2103
2104/**
2105 * @brief Add a module reference into an array, checks for duplicities.
2106 *
2107 * @param[in] ctx Compile context.
2108 * @param[in] mod Module reference to add.
2109 * @param[in,out] mod_array Module sized array to add to.
2110 * @return LY_ERR value.
2111 */
2112static LY_ERR
2113lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2114{
2115 LY_ARRAY_COUNT_TYPE u;
2116 struct lys_module **new_mod;
2117
2118 LY_ARRAY_FOR(*mod_array, u) {
2119 if ((*mod_array)[u] == mod) {
2120 /* already there */
2121 return LY_EEXIST;
2122 }
2123 }
2124
2125 /* add the new module ref */
2126 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2127 *new_mod = mod;
2128
2129 return LY_SUCCESS;
2130}
2131
2132LY_ERR
2133lys_precompile_augments_deviations(struct lysc_ctx *ctx)
2134{
Michal Vasko405cc9e2020-12-01 12:01:27 +01002135 LY_ERR ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002136 LY_ARRAY_COUNT_TYPE u, v;
2137 const struct lysp_module *mod_p;
2138 const struct lysc_node *target;
2139 struct lys_module *mod;
2140 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002141 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002142 ly_bool has_dev = 0;
2143 uint16_t flags;
2144 uint32_t idx, opt_prev = ctx->options;
2145
Michal Vasko405cc9e2020-12-01 12:01:27 +01002146 for (idx = 0; idx < ctx->unres->implementing.count; ++idx) {
2147 if (ctx->cur_mod == ctx->unres->implementing.objs[idx]) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002148 break;
2149 }
2150 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01002151 if (idx == ctx->unres->implementing.count) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002152 /* it was already implemented and all the augments and deviations fully applied */
2153 return LY_SUCCESS;
2154 }
2155
2156 mod_p = ctx->cur_mod->parsed;
2157
Radek Krejci2a9fc652021-01-22 17:44:34 +01002158 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002159 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002160 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002161
2162 /* get target module */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002163 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002164 LY_CHECK_RET(ret);
2165
2166 /* add this module into the target module augmented_by, if not there already from previous augments */
2167 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2168
2169 /* if we are compiling this module, we cannot add augments to it yet */
2170 if (mod != ctx->cur_mod) {
2171 /* apply the augment, find the target node first */
2172 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002173 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002174 (void *)mod_p, 0, &target, &flags);
2175 LY_CHECK_RET(ret);
2176
2177 /* apply the augment */
2178 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002179 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002180 ctx->options = opt_prev;
2181 LY_CHECK_RET(ret);
2182 }
2183
2184 lysc_update_path(ctx, NULL, NULL);
2185 lysc_update_path(ctx, NULL, NULL);
2186 }
2187
2188 LY_ARRAY_FOR(mod_p->deviations, u) {
2189 /* get target module */
2190 lysc_update_path(ctx, NULL, "{deviation}");
2191 lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid);
2192 ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL);
2193 lysc_update_path(ctx, NULL, NULL);
2194 lysc_update_path(ctx, NULL, NULL);
2195 LY_CHECK_RET(ret);
2196
2197 /* add this module into the target module deviated_by, if not there already from previous deviations */
2198 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2199
2200 /* new deviation added to the target module */
2201 has_dev = 1;
2202 }
2203
2204 /* the same for augments and deviations in submodules */
2205 LY_ARRAY_FOR(mod_p->includes, v) {
2206 submod = mod_p->includes[v].submodule;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002207 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002208 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002209 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002210
Radek Krejci2a9fc652021-01-22 17:44:34 +01002211 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002212 LY_CHECK_RET(ret);
2213
2214 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2215 if (mod != ctx->cur_mod) {
2216 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002217 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002218 submod, 0, &target, &flags);
2219 LY_CHECK_RET(ret);
2220
2221 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002222 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002223 ctx->options = opt_prev;
2224 LY_CHECK_RET(ret);
2225 }
2226
2227 lysc_update_path(ctx, NULL, NULL);
2228 lysc_update_path(ctx, NULL, NULL);
2229 }
2230
2231 LY_ARRAY_FOR(submod->deviations, u) {
2232 lysc_update_path(ctx, NULL, "{deviation}");
2233 lysc_update_path(ctx, NULL, submod->deviations[u].nodeid);
2234 ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL);
2235 lysc_update_path(ctx, NULL, NULL);
2236 lysc_update_path(ctx, NULL, NULL);
2237 LY_CHECK_RET(ret);
2238
2239 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2240 has_dev = 1;
2241 }
2242 }
2243
Michal Vasko405cc9e2020-12-01 12:01:27 +01002244 if (has_dev) {
2245 /* all modules (may) need to be recompiled */
2246 ctx->unres->recompile = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002247 }
2248
Michal Vasko405cc9e2020-12-01 12:01:27 +01002249 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002250}
2251
2252void
2253lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2254{
2255 uint32_t i;
2256 LY_ARRAY_COUNT_TYPE u, count;
2257 struct lys_module *m;
2258
2259 for (i = 0; i < ctx->list.count; ++i) {
2260 m = ctx->list.objs[i];
2261
2262 if (m->augmented_by) {
2263 count = LY_ARRAY_COUNT(m->augmented_by);
2264 for (u = 0; u < count; ++u) {
2265 if (m->augmented_by[u] == mod) {
2266 /* keep the order */
2267 if (u < count - 1) {
2268 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2269 }
2270 LY_ARRAY_DECREMENT(m->augmented_by);
2271 break;
2272 }
2273 }
2274 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2275 LY_ARRAY_FREE(m->augmented_by);
2276 m->augmented_by = NULL;
2277 }
2278 }
2279
2280 if (m->deviated_by) {
2281 count = LY_ARRAY_COUNT(m->deviated_by);
2282 for (u = 0; u < count; ++u) {
2283 if (m->deviated_by[u] == mod) {
2284 /* keep the order */
2285 if (u < count - 1) {
2286 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2287 }
2288 LY_ARRAY_DECREMENT(m->deviated_by);
2289 break;
2290 }
2291 }
2292 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2293 LY_ARRAY_FREE(m->deviated_by);
2294 m->deviated_by = NULL;
2295 }
2296 }
2297 }
2298}