blob: 0b5f85214d8ddc62258807a22b54366cc68a90a2 [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"
Radek Krejci77114102021-03-10 15:21:57 +010026#include "dict.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020027#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "plugins_exts_compile.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020029#include "schema_compile.h"
30#include "schema_compile_node.h"
Michal Vasko29dd11e2020-11-23 16:52:22 +010031#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020032#include "set.h"
33#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010034#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020035#include "tree_schema.h"
36#include "tree_schema_internal.h"
37#include "xpath.h"
38
39static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest,
40 size_t nametest_len, const struct lysp_module *mod, const char **name, size_t *name_len);
41
Michal Vasko1ccbf542021-04-19 11:35:00 +020042/**
43 * @brief Check the syntax of a node-id and collect all the referenced modules.
44 *
45 * @param[in] ctx Compile context.
46 * @param[in] nodeid Node-id to check.
47 * @param[in] abs Whether @p nodeid is absolute.
48 * @param[in,out] mod_set Set to add referenced modules into.
49 * @param[out] expr Optional node-id parsed into an expression.
50 * @param[out] target_mod Optional target module of the node-id.
51 * @return LY_ERR value.
52 */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020053static LY_ERR
Michal Vasko1ccbf542021-04-19 11:35:00 +020054lys_nodeid_mod_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct ly_set *mod_set,
55 struct lyxp_expr **expr, struct lys_module **target_mod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020056{
57 LY_ERR ret = LY_SUCCESS;
58 struct lyxp_expr *e = NULL;
59 struct lys_module *tmod = NULL, *mod;
60 const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid";
61 uint32_t i;
62
63 /* parse */
64 ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e);
65 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010066 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020067 nodeid_type, nodeid);
68 ret = LY_EVALID;
69 goto cleanup;
70 }
71
72 if (abs) {
73 /* absolute schema nodeid */
74 i = 0;
75 } else {
76 /* descendant schema nodeid */
77 if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010078 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020079 nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]);
80 ret = LY_EVALID;
81 goto cleanup;
82 }
83 i = 1;
84 }
85
86 /* check all the tokens */
87 for ( ; i < e->used; i += 2) {
88 if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010089 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020090 nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]);
91 ret = LY_EVALID;
92 goto cleanup;
93 } else if (e->used == i + 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010094 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020095 "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr);
96 ret = LY_EVALID;
97 goto cleanup;
98 } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010099 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200100 nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
101 ret = LY_EVALID;
102 goto cleanup;
103 } else if (abs) {
104 mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1],
105 e->tok_len[i + 1], ctx->pmod, NULL, NULL);
106 LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup);
107
108 /* only keep the first module */
109 if (!tmod) {
110 tmod = mod;
111 }
112
Michal Vasko1ccbf542021-04-19 11:35:00 +0200113 /* store the referenced module */
114 LY_CHECK_GOTO(ret = ly_set_add(mod_set, mod, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200115 }
116 }
117
118cleanup:
119 if (ret || !expr) {
120 lyxp_expr_free(ctx->ctx, e);
121 e = NULL;
122 }
123 if (expr) {
124 *expr = ret ? NULL : e;
125 }
126 if (target_mod) {
127 *target_mod = ret ? NULL : tmod;
128 }
129 return ret;
130}
131
132/**
133 * @brief Check whether 2 schema nodeids match.
134 *
135 * @param[in] ctx libyang context.
136 * @param[in] exp1 First schema nodeid.
137 * @param[in] exp1p_mod Module of @p exp1 nodes without any prefix.
138 * @param[in] exp2 Second schema nodeid.
139 * @param[in] exp2_pmod Module of @p exp2 nodes without any prefix.
140 * @return Whether the schema nodeids match or not.
141 */
142static ly_bool
143lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lysp_module *exp1_pmod,
144 const struct lyxp_expr *exp2, const struct lysp_module *exp2_pmod)
145{
146 uint32_t i;
147 const struct lys_module *mod1, *mod2;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100148 const char *name1 = NULL, *name2 = NULL;
149 size_t name1_len = 0, name2_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200150
151 if (exp1->used != exp2->used) {
152 return 0;
153 }
154
155 for (i = 0; i < exp1->used; ++i) {
156 assert(exp1->tokens[i] == exp2->tokens[i]);
157
158 if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) {
159 /* check modules of all the nodes in the node ID */
160 mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_pmod,
161 &name1, &name1_len);
162 assert(mod1);
163 mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_pmod,
164 &name2, &name2_len);
165 assert(mod2);
166
167 /* compare modules */
168 if (mod1 != mod2) {
169 return 0;
170 }
171
172 /* compare names */
173 if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) {
174 return 0;
175 }
176 }
177 }
178
179 return 1;
180}
181
182LY_ERR
183lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node)
184{
185 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200186 struct lyxp_expr *exp = NULL;
187 struct lysc_augment *aug;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100188 struct lysp_node_augment *aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200189 struct lysc_refine *rfn;
190 struct lysp_refine **new_rfn;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100191 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200192 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +0200193 struct ly_set mod_set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200194
Radek Krejci2a9fc652021-01-22 17:44:34 +0100195 LY_LIST_FOR(uses_p->augments, aug_p) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200196 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +0100197 lysc_update_path(ctx, NULL, aug_p->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200198
199 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200200 LY_CHECK_GOTO(ret = lys_nodeid_mod_check(ctx, aug_p->nodeid, 0, &mod_set, &exp, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200201
202 /* allocate new compiled augment and store it in the set */
203 aug = calloc(1, sizeof *aug);
204 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
205 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, 1, NULL), cleanup);
206
207 aug->nodeid = exp;
208 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +0100209 aug->aug_pmod = ctx->pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200210 aug->nodeid_ctx_node = ctx_node;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100211 aug->aug_p = aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200212
213 lysc_update_path(ctx, NULL, NULL);
214 lysc_update_path(ctx, NULL, NULL);
215 }
216
217 LY_ARRAY_FOR(uses_p->refines, u) {
218 lysc_update_path(ctx, NULL, "{refine}");
219 lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid);
220
221 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200222 LY_CHECK_GOTO(ret = lys_nodeid_mod_check(ctx, uses_p->refines[u].nodeid, 0, &mod_set, &exp, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200223
224 /* try to find the node in already compiled refines */
225 rfn = NULL;
226 for (i = 0; i < ctx->uses_rfns.count; ++i) {
227 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->pmod, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid,
228 ctx->pmod)) {
229 rfn = ctx->uses_rfns.objs[i];
230 break;
231 }
232 }
233
234 if (!rfn) {
235 /* allocate new compiled refine */
236 rfn = calloc(1, sizeof *rfn);
237 LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
238 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, 1, NULL), cleanup);
239
240 rfn->nodeid = exp;
241 exp = NULL;
Michal Vasko7d3708f2021-02-03 10:50:24 +0100242 rfn->nodeid_pmod = ctx->cur_mod->parsed;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200243 rfn->nodeid_ctx_node = ctx_node;
Michal Vaskod8655722021-01-12 15:20:36 +0100244 rfn->uses_p = uses_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200245 } else {
246 /* just free exp */
247 lyxp_expr_free(ctx->ctx, exp);
248 exp = NULL;
249 }
250
251 /* add new parsed refine structure */
252 LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup);
253 *new_rfn = &uses_p->refines[u];
254
255 lysc_update_path(ctx, NULL, NULL);
256 lysc_update_path(ctx, NULL, NULL);
257 }
258
259cleanup:
Michal Vasko1ccbf542021-04-19 11:35:00 +0200260 if (ret) {
261 lysc_update_path(ctx, NULL, NULL);
262 lysc_update_path(ctx, NULL, NULL);
263 }
264 /* should include only this module, will fail later if not */
265 ly_set_erase(&mod_set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200266 lyxp_expr_free(ctx->ctx, exp);
267 return ret;
268}
269
270static LY_ERR
271lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext)
272{
273 LY_ERR ret = LY_SUCCESS;
274
275 *ext = *orig_ext;
276 DUP_STRING(ctx, orig_ext->name, ext->name, ret);
277 DUP_STRING(ctx, orig_ext->argument, ext->argument, ret);
278
279 return ret;
280}
281
282static LY_ERR
283lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr)
284{
285 LY_ERR ret = LY_SUCCESS;
286
287 if (orig_restr) {
288 DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret);
289 restr->arg.mod = orig_restr->arg.mod;
290 DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret);
291 DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret);
292 DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret);
293 DUP_STRING(ctx, orig_restr->ref, restr->ref, ret);
294 DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup);
295 }
296
297 return ret;
298}
299
300static LY_ERR
301lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str)
302{
303 LY_ERR ret = LY_SUCCESS;
304
305 DUP_STRING(ctx, *orig_str, *str, ret);
306
307 return ret;
308}
309
310LY_ERR
311lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname)
312{
313 LY_ERR ret = LY_SUCCESS;
314
315 if (!orig_qname->str) {
316 return LY_SUCCESS;
317 }
318
319 DUP_STRING(ctx, orig_qname->str, qname->str, ret);
320 assert(orig_qname->mod);
321 qname->mod = orig_qname->mod;
322
323 return ret;
324}
325
326static LY_ERR
327lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm)
328{
329 LY_ERR ret = LY_SUCCESS;
330
331 DUP_STRING(ctx, orig_enm->name, enm->name, ret);
332 DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret);
333 DUP_STRING(ctx, orig_enm->ref, enm->ref, ret);
334 enm->value = orig_enm->value;
335 DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup);
336 DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup);
337 enm->flags = orig_enm->flags;
338
339 return ret;
340}
341
342static LY_ERR
343lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type)
344{
345 LY_ERR ret = LY_SUCCESS;
346
347 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
348
349 if (orig_type->range) {
350 type->range = calloc(1, sizeof *type->range);
351 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
352 LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range));
353 }
354
355 if (orig_type->length) {
356 type->length = calloc(1, sizeof *type->length);
357 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
358 LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length));
359 }
360
361 DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup);
362 DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup);
363 DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup);
364 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done);
365 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
366 DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup);
367 DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup);
368
369 type->pmod = orig_type->pmod;
370 type->compiled = orig_type->compiled;
371
372 type->fraction_digits = orig_type->fraction_digits;
373 type->require_instance = orig_type->require_instance;
374 type->flags = orig_type->flags;
375
376done:
377 return ret;
378}
379
380static LY_ERR
381lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when)
382{
383 LY_ERR ret = LY_SUCCESS;
384
385 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
386 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
387 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
388 DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup);
389
390 return ret;
391}
392
393static LY_ERR
394lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
395{
396 LY_ERR ret = LY_SUCCESS;
397
398 node->parent = NULL;
399 node->nodetype = orig->nodetype;
400 node->flags = orig->flags;
401 node->next = NULL;
402 DUP_STRING(ctx, orig->name, node->name, ret);
403 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
404 DUP_STRING(ctx, orig->ref, node->ref, ret);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200405 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
406 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
407
408 return ret;
409}
410
Radek Krejci9a3823e2021-01-27 20:26:46 +0100411#define DUP_PWHEN(CTX, ORIG, NEW) \
412 if (ORIG) { \
413 NEW = calloc(1, sizeof *NEW); \
414 LY_CHECK_ERR_RET(!NEW, LOGMEM(CTX), LY_EMEM); \
415 LY_CHECK_RET(lysp_when_dup(CTX, NEW, ORIG)); \
416 }
417
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200418static LY_ERR
419lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
420{
421 LY_ERR ret = LY_SUCCESS;
422 struct lysp_node_container *cont;
423 const struct lysp_node_container *orig_cont;
424 struct lysp_node_leaf *leaf;
425 const struct lysp_node_leaf *orig_leaf;
426 struct lysp_node_leaflist *llist;
427 const struct lysp_node_leaflist *orig_llist;
428 struct lysp_node_list *list;
429 const struct lysp_node_list *orig_list;
430 struct lysp_node_choice *choice;
431 const struct lysp_node_choice *orig_choice;
432 struct lysp_node_case *cas;
433 const struct lysp_node_case *orig_cas;
434 struct lysp_node_anydata *any;
435 const struct lysp_node_anydata *orig_any;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100436 struct lysp_node_action *action;
437 const struct lysp_node_action *orig_action;
438 struct lysp_node_action_inout *action_inout;
439 const struct lysp_node_action_inout *orig_action_inout;
440 struct lysp_node_notif *notif;
441 const struct lysp_node_notif *orig_notif;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200442
Radek Krejci2a9fc652021-01-22 17:44:34 +0100443 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA |
444 LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200445
446 /* common part */
447 LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig));
448
449 /* specific part */
450 switch (node->nodetype) {
451 case LYS_CONTAINER:
452 cont = (struct lysp_node_container *)node;
453 orig_cont = (const struct lysp_node_container *)orig;
454
Radek Krejci9a3823e2021-01-27 20:26:46 +0100455 DUP_PWHEN(ctx, orig_cont->when, cont->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200456 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
457 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
458 /* we do not need the rest */
459 break;
460 case LYS_LEAF:
461 leaf = (struct lysp_node_leaf *)node;
462 orig_leaf = (const struct lysp_node_leaf *)orig;
463
Radek Krejci9a3823e2021-01-27 20:26:46 +0100464 DUP_PWHEN(ctx, orig_leaf->when, leaf->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200465 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
466 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
467 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
468 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
469 break;
470 case LYS_LEAFLIST:
471 llist = (struct lysp_node_leaflist *)node;
472 orig_llist = (const struct lysp_node_leaflist *)orig;
473
Radek Krejci9a3823e2021-01-27 20:26:46 +0100474 DUP_PWHEN(ctx, orig_llist->when, llist->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200475 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
476 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
477 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
478 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
479 llist->min = orig_llist->min;
480 llist->max = orig_llist->max;
481 break;
482 case LYS_LIST:
483 list = (struct lysp_node_list *)node;
484 orig_list = (const struct lysp_node_list *)orig;
485
Radek Krejci9a3823e2021-01-27 20:26:46 +0100486 DUP_PWHEN(ctx, orig_list->when, list->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200487 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
488 DUP_STRING(ctx, orig_list->key, list->key, ret);
489 /* we do not need these arrays */
490 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
491 list->min = orig_list->min;
492 list->max = orig_list->max;
493 break;
494 case LYS_CHOICE:
495 choice = (struct lysp_node_choice *)node;
496 orig_choice = (const struct lysp_node_choice *)orig;
497
Radek Krejci9a3823e2021-01-27 20:26:46 +0100498 DUP_PWHEN(ctx, orig_choice->when, choice->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200499 /* we do not need children */
500 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
501 break;
502 case LYS_CASE:
503 cas = (struct lysp_node_case *)node;
504 orig_cas = (const struct lysp_node_case *)orig;
505
Radek Krejci9a3823e2021-01-27 20:26:46 +0100506 DUP_PWHEN(ctx, orig_cas->when, cas->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200507 /* we do not need children */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200508 break;
509 case LYS_ANYDATA:
510 case LYS_ANYXML:
511 any = (struct lysp_node_anydata *)node;
512 orig_any = (const struct lysp_node_anydata *)orig;
513
Radek Krejci9a3823e2021-01-27 20:26:46 +0100514 DUP_PWHEN(ctx, orig_any->when, any->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200515 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
516 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100517 case LYS_RPC:
518 case LYS_ACTION:
519 action = (struct lysp_node_action *)node;
520 orig_action = (const struct lysp_node_action *)orig;
521
522 action->input.nodetype = orig_action->input.nodetype;
523 action->output.nodetype = orig_action->output.nodetype;
524 /* we do not need the rest */
525 break;
526 case LYS_INPUT:
527 case LYS_OUTPUT:
528 action_inout = (struct lysp_node_action_inout *)node;
529 orig_action_inout = (const struct lysp_node_action_inout *)orig;
530
531 DUP_ARRAY(ctx, orig_action_inout->musts, action_inout->musts, lysp_restr_dup);
532 /* we do not need the rest */
533 break;
534 case LYS_NOTIF:
535 notif = (struct lysp_node_notif *)node;
536 orig_notif = (const struct lysp_node_notif *)orig;
537
538 DUP_ARRAY(ctx, orig_notif->musts, notif->musts, lysp_restr_dup);
539 /* we do not need the rest */
540 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200541 default:
542 LOGINT_RET(ctx);
543 }
544
545 return ret;
546}
547
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200548/**
549 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
550 *
551 * @param[in] ctx libyang context.
552 * @param[in] pnode Node to duplicate.
553 * @param[in] with_links Whether to also copy any links (child, parent pointers).
554 * @param[out] dup_p Duplicated parsed node.
555 * @return LY_ERR value.
556 */
557static LY_ERR
558lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
559{
560 LY_ERR ret = LY_SUCCESS;
561 void *mem = NULL;
562
563 if (!pnode) {
564 *dup_p = NULL;
565 return LY_SUCCESS;
566 }
567
568 switch (pnode->nodetype) {
569 case LYS_CONTAINER:
570 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200571 break;
572 case LYS_LEAF:
573 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200574 break;
575 case LYS_LEAFLIST:
576 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200577 break;
578 case LYS_LIST:
579 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200580 break;
581 case LYS_CHOICE:
582 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200583 break;
584 case LYS_CASE:
585 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200586 break;
587 case LYS_ANYDATA:
588 case LYS_ANYXML:
589 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200590 break;
591 case LYS_INPUT:
592 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100593 mem = calloc(1, sizeof(struct lysp_node_action_inout));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200594 break;
595 case LYS_ACTION:
596 case LYS_RPC:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100597 mem = calloc(1, sizeof(struct lysp_node_action));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200598 break;
599 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100600 mem = calloc(1, sizeof(struct lysp_node_notif));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200601 break;
602 default:
603 LOGINT_RET(ctx);
604 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100605 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
606 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200607
608 if (with_links) {
609 /* copy also parent and child pointers */
610 ((struct lysp_node *)mem)->parent = pnode->parent;
611 switch (pnode->nodetype) {
612 case LYS_CONTAINER:
613 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
614 break;
615 case LYS_LIST:
616 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
617 break;
618 case LYS_CHOICE:
619 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
620 break;
621 case LYS_CASE:
622 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
623 break;
624 default:
625 break;
626 }
627 }
628
629cleanup:
630 if (ret) {
631 free(mem);
632 } else {
633 *dup_p = mem;
634 }
635 return ret;
636}
637
638#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100639 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 +0200640 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
641 ret = LY_EVALID; \
642 goto cleanup;
643
644#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
645 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100646 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 +0200647 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
648 ret = LY_EVALID; \
649 goto cleanup; \
650 }
651
652/**
653 * @brief Apply refine.
654 *
655 * @param[in] ctx Compile context.
656 * @param[in] rfn Refine to apply.
657 * @param[in,out] target Refine target.
658 * @return LY_ERR value.
659 */
660static LY_ERR
661lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
662{
663 LY_ERR ret = LY_SUCCESS;
664 LY_ARRAY_COUNT_TYPE u;
665 struct lysp_qname *qname;
666 struct lysp_restr **musts, *must;
667 uint32_t *num;
668
669 /* default value */
670 if (rfn->dflts) {
671 switch (target->nodetype) {
672 case LYS_LEAF:
673 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
674
Michal Vaskoe180ed02021-02-05 16:31:20 +0100675 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200676 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
677 break;
678 case LYS_LEAFLIST:
679 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100680 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200681 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
682 ret = LY_EVALID;
683 goto cleanup;
684 }
685
686 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
687 ((struct lysp_node_leaflist *)target)->dflts = NULL;
688 LY_ARRAY_FOR(rfn->dflts, u) {
689 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
690 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
691 }
692 break;
693 case LYS_CHOICE:
694 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
695
Michal Vaskoe180ed02021-02-05 16:31:20 +0100696 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200697 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
698 break;
699 default:
700 AMEND_WRONG_NODETYPE("refine", "replace", "default");
701 }
702 }
703
704 /* description */
705 if (rfn->dsc) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100706 lydict_remove(ctx->ctx, target->dsc);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200707 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
708 }
709
710 /* reference */
711 if (rfn->ref) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100712 lydict_remove(ctx->ctx, target->ref);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200713 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
714 }
715
716 /* config */
717 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci91531e12021-02-08 19:57:54 +0100718 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200719 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +0100720 (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT)) ? "RPC/action" :
721 ctx->options & LYS_IS_NOTIF ? "notification" : "a subtree ignoring config", ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200722 } else {
723 target->flags &= ~LYS_CONFIG_MASK;
724 target->flags |= rfn->flags & LYS_CONFIG_MASK;
725 }
726 }
727
728 /* mandatory */
729 if (rfn->flags & LYS_MAND_MASK) {
730 switch (target->nodetype) {
731 case LYS_LEAF:
732 case LYS_CHOICE:
733 case LYS_ANYDATA:
734 case LYS_ANYXML:
735 break;
736 default:
737 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
738 }
739
740 target->flags &= ~LYS_MAND_MASK;
741 target->flags |= rfn->flags & LYS_MAND_MASK;
742 }
743
744 /* presence */
745 if (rfn->presence) {
746 switch (target->nodetype) {
747 case LYS_CONTAINER:
748 break;
749 default:
750 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
751 }
752
Michal Vaskoe180ed02021-02-05 16:31:20 +0100753 lydict_remove(ctx->ctx, ((struct lysp_node_container *)target)->presence);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200754 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
755 }
756
757 /* must */
758 if (rfn->musts) {
759 switch (target->nodetype) {
760 case LYS_CONTAINER:
761 case LYS_LIST:
762 case LYS_LEAF:
763 case LYS_LEAFLIST:
764 case LYS_ANYDATA:
765 case LYS_ANYXML:
766 musts = &((struct lysp_node_container *)target)->musts;
767 break;
768 default:
769 AMEND_WRONG_NODETYPE("refine", "add", "must");
770 }
771
772 LY_ARRAY_FOR(rfn->musts, u) {
773 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
774 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
775 }
776 }
777
778 /* min-elements */
779 if (rfn->flags & LYS_SET_MIN) {
780 switch (target->nodetype) {
781 case LYS_LEAFLIST:
782 num = &((struct lysp_node_leaflist *)target)->min;
783 break;
784 case LYS_LIST:
785 num = &((struct lysp_node_list *)target)->min;
786 break;
787 default:
788 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
789 }
790
791 *num = rfn->min;
792 }
793
794 /* max-elements */
795 if (rfn->flags & LYS_SET_MAX) {
796 switch (target->nodetype) {
797 case LYS_LEAFLIST:
798 num = &((struct lysp_node_leaflist *)target)->max;
799 break;
800 case LYS_LIST:
801 num = &((struct lysp_node_list *)target)->max;
802 break;
803 default:
804 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
805 }
806
807 *num = rfn->max;
808 }
809
810 /* if-feature */
811 if (rfn->iffeatures) {
812 switch (target->nodetype) {
813 case LYS_LEAF:
814 case LYS_LEAFLIST:
815 case LYS_LIST:
816 case LYS_CONTAINER:
817 case LYS_CHOICE:
818 case LYS_CASE:
819 case LYS_ANYDATA:
820 case LYS_ANYXML:
821 break;
822 default:
823 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
824 }
825
826 LY_ARRAY_FOR(rfn->iffeatures, u) {
827 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
828 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
829 }
830 }
831
832 /* extension */
833 /* TODO refine extensions */
834
835cleanup:
836 return ret;
837}
838
839/**
840 * @brief Apply deviate add.
841 *
842 * @param[in] ctx Compile context.
843 * @param[in] d Deviate add to apply.
844 * @param[in,out] target Deviation target.
845 * @return LY_ERR value.
846 */
847static LY_ERR
848lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
849{
850 LY_ERR ret = LY_SUCCESS;
851 LY_ARRAY_COUNT_TYPE u;
852 struct lysp_qname *qname;
853 uint32_t *num;
854 struct lysp_restr **musts, *must;
855
856#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
857 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100858 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200859 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
860 ret = LY_EVALID; \
861 goto cleanup; \
862 }
863
864 /* [units-stmt] */
865 if (d->units) {
866 switch (target->nodetype) {
867 case LYS_LEAF:
868 case LYS_LEAFLIST:
869 break;
870 default:
871 AMEND_WRONG_NODETYPE("deviation", "add", "units");
872 }
873
874 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
875 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
876 }
877
878 /* *must-stmt */
879 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100880 musts = lysp_node_musts_p(target);
881 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200882 AMEND_WRONG_NODETYPE("deviation", "add", "must");
883 }
884
885 LY_ARRAY_FOR(d->musts, u) {
886 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
887 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
888 }
889 }
890
891 /* *unique-stmt */
892 if (d->uniques) {
893 switch (target->nodetype) {
894 case LYS_LIST:
895 break;
896 default:
897 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
898 }
899
900 LY_ARRAY_FOR(d->uniques, u) {
901 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
902 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
903 }
904 }
905
906 /* *default-stmt */
907 if (d->dflts) {
908 switch (target->nodetype) {
909 case LYS_LEAF:
910 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
911 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
912
913 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
914 break;
915 case LYS_LEAFLIST:
916 LY_ARRAY_FOR(d->dflts, u) {
917 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
918 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
919 }
920 break;
921 case LYS_CHOICE:
922 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
923 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
924
925 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
926 break;
927 default:
928 AMEND_WRONG_NODETYPE("deviation", "add", "default");
929 }
930 }
931
932 /* [config-stmt] */
933 if (d->flags & LYS_CONFIG_MASK) {
934 switch (target->nodetype) {
935 case LYS_CONTAINER:
936 case LYS_LEAF:
937 case LYS_LEAFLIST:
938 case LYS_LIST:
939 case LYS_CHOICE:
940 case LYS_ANYDATA:
941 case LYS_ANYXML:
942 break;
943 default:
944 AMEND_WRONG_NODETYPE("deviation", "add", "config");
945 }
946
947 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100948 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200949 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
950 target->flags & LYS_CONFIG_W ? "true" : "false");
951 ret = LY_EVALID;
952 goto cleanup;
953 }
954
955 target->flags |= d->flags & LYS_CONFIG_MASK;
956 }
957
958 /* [mandatory-stmt] */
959 if (d->flags & LYS_MAND_MASK) {
960 switch (target->nodetype) {
961 case LYS_LEAF:
962 case LYS_CHOICE:
963 case LYS_ANYDATA:
964 case LYS_ANYXML:
965 break;
966 default:
967 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
968 }
969
970 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100971 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200972 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
973 target->flags & LYS_MAND_TRUE ? "true" : "false");
974 ret = LY_EVALID;
975 goto cleanup;
976 }
977
978 target->flags |= d->flags & LYS_MAND_MASK;
979 }
980
981 /* [min-elements-stmt] */
982 if (d->flags & LYS_SET_MIN) {
983 switch (target->nodetype) {
984 case LYS_LEAFLIST:
985 num = &((struct lysp_node_leaflist *)target)->min;
986 break;
987 case LYS_LIST:
988 num = &((struct lysp_node_list *)target)->min;
989 break;
990 default:
991 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
992 }
993
994 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100995 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200996 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
997 ret = LY_EVALID;
998 goto cleanup;
999 }
1000
1001 *num = d->min;
1002 }
1003
1004 /* [max-elements-stmt] */
1005 if (d->flags & LYS_SET_MAX) {
1006 switch (target->nodetype) {
1007 case LYS_LEAFLIST:
1008 num = &((struct lysp_node_leaflist *)target)->max;
1009 break;
1010 case LYS_LIST:
1011 num = &((struct lysp_node_list *)target)->max;
1012 break;
1013 default:
1014 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1015 }
1016
1017 if (target->flags & LYS_SET_MAX) {
1018 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001019 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001020 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1021 *num);
1022 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001023 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001024 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1025 }
1026 ret = LY_EVALID;
1027 goto cleanup;
1028 }
1029
1030 *num = d->max;
1031 }
1032
1033cleanup:
1034 return ret;
1035}
1036
1037/**
1038 * @brief Apply deviate delete.
1039 *
1040 * @param[in] ctx Compile context.
1041 * @param[in] d Deviate delete to apply.
1042 * @param[in,out] target Deviation target.
1043 * @return LY_ERR value.
1044 */
1045static LY_ERR
1046lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1047{
1048 LY_ERR ret = LY_SUCCESS;
1049 struct lysp_restr **musts;
1050 LY_ARRAY_COUNT_TYPE u, v;
1051 struct lysp_qname **uniques, **dflts;
1052
1053#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1054 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1055 int found = 0; \
1056 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1057 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1058 found = 1; \
1059 break; \
1060 } \
1061 } \
1062 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001063 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001064 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1065 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1066 ret = LY_EVALID; \
1067 goto cleanup; \
1068 } \
1069 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1070 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
1071 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1072 } \
1073 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1074 LY_ARRAY_FREE(ORIG_ARRAY); \
1075 ORIG_ARRAY = NULL; \
1076 }
1077
1078#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1079 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001080 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001081 ret = LY_EVALID; \
1082 goto cleanup; \
1083 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001084 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001085 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1086 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1087 ret = LY_EVALID; \
1088 goto cleanup; \
1089 }
1090
1091 /* [units-stmt] */
1092 if (d->units) {
1093 switch (target->nodetype) {
1094 case LYS_LEAF:
1095 case LYS_LEAFLIST:
1096 break;
1097 default:
1098 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1099 }
1100
1101 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001102 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001103 ((struct lysp_node_leaf *)target)->units = NULL;
1104 }
1105
1106 /* *must-stmt */
1107 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001108 musts = lysp_node_musts_p(target);
1109 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001110 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1111 }
1112
1113 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1114 }
1115
1116 /* *unique-stmt */
1117 if (d->uniques) {
1118 switch (target->nodetype) {
1119 case LYS_LIST:
1120 break;
1121 default:
1122 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1123 }
1124
1125 uniques = &((struct lysp_node_list *)target)->uniques;
1126 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1127 }
1128
1129 /* *default-stmt */
1130 if (d->dflts) {
1131 switch (target->nodetype) {
1132 case LYS_LEAF:
1133 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1134 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1135
Michal Vaskoe180ed02021-02-05 16:31:20 +01001136 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001137 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1138 break;
1139 case LYS_LEAFLIST:
1140 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1141 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1142 break;
1143 case LYS_CHOICE:
1144 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1145 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1146
Michal Vaskoe180ed02021-02-05 16:31:20 +01001147 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001148 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1149 break;
1150 default:
1151 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1152 }
1153 }
1154
1155cleanup:
1156 return ret;
1157}
1158
1159/**
1160 * @brief Apply deviate replace.
1161 *
1162 * @param[in] ctx Compile context.
1163 * @param[in] d Deviate replace to apply.
1164 * @param[in,out] target Deviation target.
1165 * @return LY_ERR value.
1166 */
1167static LY_ERR
1168lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1169{
1170 LY_ERR ret = LY_SUCCESS;
1171 uint32_t *num;
1172
1173#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1174 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001175 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001176 ret = LY_EVALID; \
1177 goto cleanup; \
1178 }
1179
1180 /* [type-stmt] */
1181 if (d->type) {
1182 switch (target->nodetype) {
1183 case LYS_LEAF:
1184 case LYS_LEAFLIST:
1185 break;
1186 default:
1187 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1188 }
1189
1190 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1191 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1192 }
1193
1194 /* [units-stmt] */
1195 if (d->units) {
1196 switch (target->nodetype) {
1197 case LYS_LEAF:
1198 case LYS_LEAFLIST:
1199 break;
1200 default:
1201 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1202 }
1203
1204 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001205 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001206 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1207 }
1208
1209 /* [default-stmt] */
1210 if (d->dflt.str) {
1211 switch (target->nodetype) {
1212 case LYS_LEAF:
1213 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1214
Michal Vaskoe180ed02021-02-05 16:31:20 +01001215 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001216 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1217 break;
1218 case LYS_CHOICE:
1219 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1220
Michal Vaskoe180ed02021-02-05 16:31:20 +01001221 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001222 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1223 break;
1224 default:
1225 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1226 }
1227 }
1228
1229 /* [config-stmt] */
1230 if (d->flags & LYS_CONFIG_MASK) {
1231 switch (target->nodetype) {
1232 case LYS_CONTAINER:
1233 case LYS_LEAF:
1234 case LYS_LEAFLIST:
1235 case LYS_LIST:
1236 case LYS_CHOICE:
1237 case LYS_ANYDATA:
1238 case LYS_ANYXML:
1239 break;
1240 default:
1241 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1242 }
1243
1244 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001245 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1246 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001247 ret = LY_EVALID;
1248 goto cleanup;
1249 }
1250
1251 target->flags &= ~LYS_CONFIG_MASK;
1252 target->flags |= d->flags & LYS_CONFIG_MASK;
1253 }
1254
1255 /* [mandatory-stmt] */
1256 if (d->flags & LYS_MAND_MASK) {
1257 switch (target->nodetype) {
1258 case LYS_LEAF:
1259 case LYS_CHOICE:
1260 case LYS_ANYDATA:
1261 case LYS_ANYXML:
1262 break;
1263 default:
1264 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1265 }
1266
1267 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001268 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1269 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001270 ret = LY_EVALID;
1271 goto cleanup;
1272 }
1273
1274 target->flags &= ~LYS_MAND_MASK;
1275 target->flags |= d->flags & LYS_MAND_MASK;
1276 }
1277
1278 /* [min-elements-stmt] */
1279 if (d->flags & LYS_SET_MIN) {
1280 switch (target->nodetype) {
1281 case LYS_LEAFLIST:
1282 num = &((struct lysp_node_leaflist *)target)->min;
1283 break;
1284 case LYS_LIST:
1285 num = &((struct lysp_node_list *)target)->min;
1286 break;
1287 default:
1288 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1289 }
1290
1291 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001292 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001293 ret = LY_EVALID;
1294 goto cleanup;
1295 }
1296
1297 *num = d->min;
1298 }
1299
1300 /* [max-elements-stmt] */
1301 if (d->flags & LYS_SET_MAX) {
1302 switch (target->nodetype) {
1303 case LYS_LEAFLIST:
1304 num = &((struct lysp_node_leaflist *)target)->max;
1305 break;
1306 case LYS_LIST:
1307 num = &((struct lysp_node_list *)target)->max;
1308 break;
1309 default:
1310 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1311 }
1312
1313 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001314 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001315 ret = LY_EVALID;
1316 goto cleanup;
1317 }
1318
1319 *num = d->max;
1320 }
1321
1322cleanup:
1323 return ret;
1324}
1325
1326/**
1327 * @brief Get module of a single nodeid node name test.
1328 *
1329 * @param[in] ctx libyang context.
1330 * @param[in] nametest Nametest with an optional prefix.
1331 * @param[in] nametest_len Length of @p nametest.
1332 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1333 * @param[out] name Optional pointer to the name test without the prefix.
1334 * @param[out] name_len Length of @p name.
1335 * @return Resolved module.
1336 */
1337static const struct lys_module *
1338lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1339 const struct lysp_module *mod, const char **name, size_t *name_len)
1340{
1341 const struct lys_module *target_mod;
1342 const char *ptr;
1343
1344 ptr = ly_strnchr(nametest, ':', nametest_len);
1345 if (ptr) {
1346 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)mod);
1347 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001348 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001349 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001350 (int)nametest_len, nametest, (int)(ptr - nametest), nametest, LYSP_MODULE_NAME(mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001351 return NULL;
1352 }
1353
1354 if (name) {
1355 *name = ptr + 1;
1356 *name_len = nametest_len - ((ptr - nametest) + 1);
1357 }
1358 } else {
1359 target_mod = mod->mod;
1360 if (name) {
1361 *name = nametest;
1362 *name_len = nametest_len;
1363 }
1364 }
1365
1366 return target_mod;
1367}
1368
1369/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001370 * @brief Check whether a compiled node matches a single schema nodeid name test.
1371 *
1372 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1373 * @param[in] mod Expected module.
1374 * @param[in] name Expected name.
1375 * @param[in] name_len Length of @p name.
1376 * @return Whether it is a match or not.
1377 */
1378static ly_bool
1379lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1380 size_t name_len)
1381{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001382 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001383 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001384 return 0;
1385 }
1386
1387 /* compare names */
Michal Vasko544e58a2021-01-28 14:33:41 +01001388 if (ly_strncmp((*node)->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001389 return 0;
1390 }
1391
Michal Vasko2a668712020-10-21 11:48:09 +02001392 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001393 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001394
1395 return 1;
1396}
1397
1398/**
1399 * @brief Check whether a node matches specific schema nodeid.
1400 *
1401 * @param[in] exp Parsed nodeid to match.
1402 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1403 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1404 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1405 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1406 * @param[in] pnode_mod Compiled @p pnode to-be module.
1407 * @return Whether it is a match or not.
1408 */
1409static ly_bool
1410lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1411 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1412{
1413 uint32_t i;
1414 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001415 const char *name = NULL;
1416 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001417
1418 /* compare last node in the node ID */
1419 i = exp->used - 1;
1420
1421 /* get exp node ID module */
1422 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);
1423 assert(mod);
1424
1425 if (pnode) {
1426 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001427 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001428 return 0;
1429 }
1430 } else {
1431 /* using parent directly */
1432 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1433 return 0;
1434 }
1435 }
1436
1437 /* now compare all the compiled parents */
1438 while (i > 1) {
1439 i -= 2;
1440 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1441
1442 if (!parent) {
1443 /* no more parents but path continues */
1444 return 0;
1445 }
1446
1447 /* get exp node ID module */
1448 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1449 &name_len);
1450 assert(mod);
1451
1452 /* compare with the parent */
1453 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1454 return 0;
1455 }
1456 }
1457
1458 if (ctx_node && (ctx_node != parent)) {
1459 /* descendant path has not finished in the context node */
1460 return 0;
1461 } else if (!ctx_node && parent) {
1462 /* some parent was not matched */
1463 return 0;
1464 }
1465
1466 return 1;
1467}
1468
1469void
1470lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1471{
1472 if (aug) {
1473 lyxp_expr_free(ctx, aug->nodeid);
1474
1475 free(aug);
1476 }
1477}
1478
1479void
1480lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1481{
1482 if (dev) {
1483 lyxp_expr_free(ctx, dev->nodeid);
1484 LY_ARRAY_FREE(dev->devs);
1485 LY_ARRAY_FREE(dev->dev_pmods);
1486
1487 free(dev);
1488 }
1489}
1490
1491void
1492lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1493{
1494 if (rfn) {
1495 lyxp_expr_free(ctx, rfn->nodeid);
1496 LY_ARRAY_FREE(rfn->rfns);
1497
1498 free(rfn);
1499 }
1500}
1501
1502void
1503lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1504{
1505 if (!dev_pnode) {
1506 return;
1507 }
1508
1509 switch (dev_pnode->nodetype) {
1510 case LYS_CONTAINER:
1511 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1512 break;
1513 case LYS_LIST:
1514 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1515 break;
1516 case LYS_CHOICE:
1517 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1518 break;
1519 case LYS_CASE:
1520 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1521 break;
1522 case LYS_LEAF:
1523 case LYS_LEAFLIST:
1524 case LYS_ANYXML:
1525 case LYS_ANYDATA:
1526 /* no children */
1527 break;
1528 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001529 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001530 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001531 case LYS_RPC:
1532 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001533 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1534 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001535 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001536 case LYS_INPUT:
1537 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001538 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001539 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001540 free(dev_pnode);
1541 return;
1542 default:
1543 LOGINT(ctx);
1544 return;
1545 }
1546
1547 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1548}
1549
1550LY_ERR
1551lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1552 struct lysp_node **dev_pnode, ly_bool *not_supported)
1553{
1554 LY_ERR ret = LY_SUCCESS;
1555 uint32_t i;
1556 LY_ARRAY_COUNT_TYPE u;
1557 struct lys_module *orig_mod = ctx->cur_mod;
1558 struct lysp_module *orig_pmod = ctx->pmod;
1559 char orig_path[LYSC_CTX_BUFSIZE];
1560 struct lysc_refine *rfn;
1561 struct lysc_deviation *dev;
1562 struct lysp_deviation *dev_p;
1563 struct lysp_deviate *d;
1564
1565 *dev_pnode = NULL;
1566 *not_supported = 0;
1567
1568 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1569 rfn = ctx->uses_rfns.objs[i];
1570
Michal Vasko28fe5f62021-03-03 16:37:39 +01001571 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 +02001572 /* not our target node */
1573 continue;
1574 }
1575
1576 if (!*dev_pnode) {
1577 /* first refine on this node, create a copy first */
1578 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1579 }
1580
Michal Vaskob8df5762021-01-12 15:15:53 +01001581 /* use modules from the refine */
1582 ctx->cur_mod = rfn->nodeid_pmod->mod;
1583 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1584
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001585 /* apply all the refines by changing (the copy of) the parsed node */
1586 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001587 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001588 lysc_update_path(ctx, NULL, "{refine}");
1589 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001590
1591 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001592 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1593 lysc_update_path(ctx, NULL, NULL);
1594 lysc_update_path(ctx, NULL, NULL);
1595 LY_CHECK_GOTO(ret, cleanup);
1596 }
1597
1598 /* refine was applied, remove it */
1599 lysc_refine_free(ctx->ctx, rfn);
1600 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1601
1602 /* all the refines for one target node are in one structure, we are done */
1603 break;
1604 }
1605
1606 for (i = 0; i < ctx->devs.count; ++i) {
1607 dev = ctx->devs.objs[i];
1608
Michal Vasko28fe5f62021-03-03 16:37:39 +01001609 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001610 /* not our target node */
1611 continue;
1612 }
1613
1614 if (dev->not_supported) {
1615 /* it is not supported, no more deviations */
1616 *not_supported = 1;
1617 goto dev_applied;
1618 }
1619
1620 if (!*dev_pnode) {
1621 /* first deviation on this node, create a copy first */
1622 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1623 }
1624
1625 /* apply all the deviates by changing (the copy of) the parsed node */
1626 LY_ARRAY_FOR(dev->devs, u) {
1627 dev_p = dev->devs[u];
1628 LY_LIST_FOR(dev_p->deviates, d) {
1629 /* generate correct path */
1630 strcpy(orig_path, ctx->path);
1631 ctx->path_len = 1;
1632 ctx->cur_mod = dev->dev_pmods[u]->mod;
1633 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1634 lysc_update_path(ctx, NULL, "{deviation}");
1635 lysc_update_path(ctx, NULL, dev_p->nodeid);
1636
1637 switch (d->mod) {
1638 case LYS_DEV_ADD:
1639 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1640 break;
1641 case LYS_DEV_DELETE:
1642 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1643 break;
1644 case LYS_DEV_REPLACE:
1645 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1646 break;
1647 default:
1648 LOGINT(ctx->ctx);
1649 ret = LY_EINT;
1650 }
1651
1652 /* restore previous path */
1653 strcpy(ctx->path, orig_path);
1654 ctx->path_len = strlen(ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001655
1656 LY_CHECK_GOTO(ret, cleanup);
1657 }
1658 }
1659
1660dev_applied:
1661 /* deviation was applied, remove it */
1662 lysc_deviation_free(ctx->ctx, dev);
1663 ly_set_rm_index(&ctx->devs, i, NULL);
1664
1665 /* all the deviations for one target node are in one structure, we are done */
1666 break;
1667 }
1668
1669cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001670 ctx->cur_mod = orig_mod;
1671 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001672 if (ret) {
1673 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1674 *dev_pnode = NULL;
1675 *not_supported = 0;
1676 }
1677 return ret;
1678}
1679
1680/**
1681 * @brief Compile the parsed augment connecting it into its target.
1682 *
1683 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1684 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1685 * are already implemented and compiled.
1686 *
1687 * @param[in] ctx Compile context.
1688 * @param[in] aug_p Parsed augment to compile.
1689 * @param[in] target Target node of the augment.
1690 * @return LY_SUCCESS on success.
1691 * @return LY_EVALID on failure.
1692 */
1693static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001694lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001695{
1696 LY_ERR ret = LY_SUCCESS;
1697 struct lysp_node *pnode;
1698 struct lysc_node *node;
1699 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001700 struct lysc_node_action **actions;
1701 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001702 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001703 struct ly_set child_set = {0};
Michal Vasko29dd11e2020-11-23 16:52:22 +01001704 uint32_t i, opt_prev = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001705
1706 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001707 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001708 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1709 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1710 ret = LY_EVALID;
1711 goto cleanup;
1712 }
1713
1714 /* check for mandatory nodes
1715 * - new cases augmenting some choice can have mandatory nodes
1716 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1717 */
1718 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1719 allow_mandatory = 1;
1720 }
1721
1722 LY_LIST_FOR(aug_p->child, pnode) {
1723 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1724 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1725 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1726 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001727 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001728 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1729 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1730 ret = LY_EVALID;
1731 goto cleanup;
1732 }
1733
1734 /* compile the children */
1735 if (target->nodetype == LYS_CHOICE) {
1736 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001737 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1738 if (target->nodetype == LYS_INPUT) {
1739 ctx->options |= LYS_COMPILE_RPC_INPUT;
1740 } else {
1741 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
1742 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001743 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001744 } else {
1745 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1746 }
1747
Michal Vasko29dd11e2020-11-23 16:52:22 +01001748 /* eval if-features again for the rest of this node processing */
1749 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1750 if (!enabled) {
1751 ctx->options |= LYS_COMPILE_DISABLED;
1752 }
1753
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001754 /* since the augment node is not present in the compiled tree, we need to pass some of its
1755 * statements to all its children */
1756 for (i = 0; i < child_set.count; ++i) {
1757 node = child_set.snodes[i];
1758 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1759 node->flags &= ~LYS_MAND_TRUE;
1760 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001761 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001762 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1763 ret = LY_EVALID;
1764 goto cleanup;
1765 }
1766
1767 if (aug_p->when) {
1768 /* pass augment's when to all the children */
Michal Vasko72244882021-01-12 15:21:05 +01001769 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 +02001770 LY_CHECK_GOTO(ret, cleanup);
1771 }
1772 }
1773 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001774
1775 /* restore options */
1776 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001777 }
1778
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001779 actions = lysc_node_actions_p(target);
1780 notifs = lysc_node_notifs_p(target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001781
1782 if (aug_p->actions) {
1783 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001784 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001785 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001786 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001787 ret = LY_EVALID;
1788 goto cleanup;
1789 }
1790
1791 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001792 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001793 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001794
Michal Vasko012807e2021-02-03 09:52:18 +01001795 /* eval if-features again for the rest of this node processing */
1796 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1797 if (!enabled) {
1798 ctx->options |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001799 }
Michal Vasko012807e2021-02-03 09:52:18 +01001800
1801 /* since the augment node is not present in the compiled tree, we need to pass some of its
1802 * statements to all its children */
1803 for (i = 0; i < child_set.count; ++i) {
1804 node = child_set.snodes[i];
1805 if (aug_p->when) {
1806 /* pass augment's when to all the actions */
1807 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1808 LY_CHECK_GOTO(ret, cleanup);
1809 }
1810 }
1811 ly_set_erase(&child_set, NULL);
1812
1813 /* restore options */
1814 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001815 }
1816 }
1817 if (aug_p->notifs) {
1818 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001819 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001820 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001821 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001822 ret = LY_EVALID;
1823 goto cleanup;
1824 }
1825
1826 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001827 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001828 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001829
Michal Vasko012807e2021-02-03 09:52:18 +01001830 /* eval if-features again for the rest of this node processing */
1831 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1832 if (!enabled) {
1833 ctx->options |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001834 }
Michal Vasko012807e2021-02-03 09:52:18 +01001835
1836 /* since the augment node is not present in the compiled tree, we need to pass some of its
1837 * statements to all its children */
1838 for (i = 0; i < child_set.count; ++i) {
1839 node = child_set.snodes[i];
1840 if (aug_p->when) {
1841 /* pass augment's when to all the actions */
1842 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1843 LY_CHECK_GOTO(ret, cleanup);
1844 }
1845 }
1846 ly_set_erase(&child_set, NULL);
1847
1848 /* restore options */
1849 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001850 }
1851 }
1852
1853cleanup:
1854 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001855 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001856 return ret;
1857}
1858
1859LY_ERR
1860lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1861{
1862 LY_ERR ret = LY_SUCCESS;
1863 struct lys_module *orig_mod = ctx->cur_mod;
1864 struct lysp_module *orig_pmod = ctx->pmod;
1865 uint32_t i;
1866 char orig_path[LYSC_CTX_BUFSIZE];
1867 struct lysc_augment *aug;
1868
1869 /* uses augments */
1870 for (i = 0; i < ctx->uses_augs.count; ) {
1871 aug = ctx->uses_augs.objs[i];
1872
Michal Vasko28fe5f62021-03-03 16:37:39 +01001873 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001874 /* not our target node */
1875 ++i;
1876 continue;
1877 }
1878
Michal Vaskob8df5762021-01-12 15:15:53 +01001879 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001880 lysc_update_path(ctx, NULL, "{augment}");
1881 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001882 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001883
1884 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001885 ret = lys_compile_augment(ctx, aug->aug_p, node);
1886 lysc_update_path(ctx, NULL, NULL);
1887 lysc_update_path(ctx, NULL, NULL);
1888 LY_CHECK_GOTO(ret, cleanup);
1889
1890 /* augment was applied, remove it (index may have changed because other augments could have been applied) */
1891 ly_set_rm(&ctx->uses_augs, aug, NULL);
1892 lysc_augment_free(ctx->ctx, aug);
1893 }
1894
1895 /* top-level augments */
1896 for (i = 0; i < ctx->augs.count; ) {
1897 aug = ctx->augs.objs[i];
1898
Michal Vasko28fe5f62021-03-03 16:37:39 +01001899 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001900 /* not our target node */
1901 ++i;
1902 continue;
1903 }
1904
Michal Vaskob8df5762021-01-12 15:15:53 +01001905 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001906 strcpy(orig_path, ctx->path);
1907 ctx->path_len = 1;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001908 ctx->cur_mod = aug->aug_pmod->mod;
1909 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001910 lysc_update_path(ctx, NULL, "{augment}");
1911 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001912
1913 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001914 ret = lys_compile_augment(ctx, aug->aug_p, node);
1915 strcpy(ctx->path, orig_path);
1916 ctx->path_len = strlen(ctx->path);
1917 LY_CHECK_GOTO(ret, cleanup);
1918
1919 /* augment was applied, remove it */
1920 ly_set_rm(&ctx->augs, aug, NULL);
1921 lysc_augment_free(ctx->ctx, aug);
1922 }
1923
1924cleanup:
1925 ctx->cur_mod = orig_mod;
1926 ctx->pmod = orig_pmod;
1927 return ret;
1928}
1929
1930/**
1931 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1932 *
1933 * @param[in] ctx Compile context.
1934 * @param[in] aug_p Parsed augment to be applied.
1935 * @param[in] pmod Both current and prefix module for @p aug_p.
1936 * @return LY_ERR value.
1937 */
1938static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001939lys_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 +02001940{
1941 LY_ERR ret = LY_SUCCESS;
1942 struct lyxp_expr *exp = NULL;
1943 struct lysc_augment *aug;
1944 const struct lys_module *mod;
1945
1946 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1947 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
1948 LY_CHECK_GOTO(ret, cleanup);
1949
1950 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
1951 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
1952 if (mod != ctx->cur_mod) {
1953 /* augment for another module, ignore */
1954 goto cleanup;
1955 }
1956
1957 /* allocate new compiled augment and store it in the set */
1958 aug = calloc(1, sizeof *aug);
1959 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1960 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
1961
1962 aug->nodeid = exp;
1963 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001964 aug->aug_pmod = pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001965 aug->aug_p = aug_p;
1966
1967cleanup:
1968 lyxp_expr_free(ctx->ctx, exp);
1969 return ret;
1970}
1971
1972LY_ERR
1973lys_precompile_own_augments(struct lysc_ctx *ctx)
1974{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001975 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001976
1977 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001978 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
1979 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001980
1981 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001982 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
1983 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001984 }
1985
1986 /* collect all submodules augments */
1987 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001988 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
1989 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 +02001990 }
1991 }
1992 }
1993
1994 return LY_SUCCESS;
1995}
1996
1997/**
1998 * @brief Prepare a deviation to be applied during data nodes compilation.
1999 *
2000 * @param[in] ctx Compile context.
2001 * @param[in] dev_p Parsed deviation to be applied.
2002 * @param[in] pmod Both current and prefix module for @p dev_p.
2003 * @return LY_ERR value.
2004 */
2005static LY_ERR
2006lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2007{
2008 LY_ERR ret = LY_SUCCESS;
2009 struct lysc_deviation *dev = NULL;
2010 struct lyxp_expr *exp = NULL;
2011 struct lysp_deviation **new_dev;
2012 const struct lys_module *mod;
2013 const struct lysp_module **new_dev_pmod;
2014 uint32_t i;
2015
2016 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2017 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2018 LY_CHECK_GOTO(ret, cleanup);
2019
2020 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2021 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2022 if (mod != ctx->cur_mod) {
2023 /* deviation for another module, ignore */
2024 goto cleanup;
2025 }
2026
2027 /* try to find the node in already compiled deviations */
2028 for (i = 0; i < ctx->devs.count; ++i) {
2029 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2030 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2031 dev = ctx->devs.objs[i];
2032 break;
2033 }
2034 }
2035
2036 if (!dev) {
2037 /* allocate new compiled deviation */
2038 dev = calloc(1, sizeof *dev);
2039 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2040 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2041
2042 dev->nodeid = exp;
2043 exp = NULL;
2044 }
2045
2046 /* add new parsed deviation structure */
2047 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2048 *new_dev = dev_p;
2049 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2050 *new_dev_pmod = pmod;
2051
2052cleanup:
2053 lyxp_expr_free(ctx->ctx, exp);
2054 return ret;
2055}
2056
2057LY_ERR
2058lys_precompile_own_deviations(struct lysc_ctx *ctx)
2059{
2060 LY_ARRAY_COUNT_TYPE u, v, w;
2061 const struct lys_module *dev_mod;
2062 struct lysc_deviation *dev;
2063 struct lysp_deviate *d;
2064 int not_supported;
2065 uint32_t i;
2066
2067 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2068 dev_mod = ctx->cur_mod->deviated_by[u];
2069
2070 /* compile all module deviations */
2071 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2072 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2073 }
2074
2075 /* compile all submodules deviations */
2076 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2077 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2078 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2079 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2080 }
2081 }
2082 }
2083
2084 /* set not-supported flags for all the deviations */
2085 for (i = 0; i < ctx->devs.count; ++i) {
2086 dev = ctx->devs.objs[i];
2087 not_supported = 0;
2088
2089 LY_ARRAY_FOR(dev->devs, u) {
2090 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2091 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2092 not_supported = 1;
2093 break;
2094 }
2095 }
2096 if (not_supported) {
2097 break;
2098 }
2099 }
2100 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002101 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002102 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
2103 return LY_EVALID;
2104 }
2105
2106 dev->not_supported = not_supported;
2107 }
2108
2109 return LY_SUCCESS;
2110}
2111
2112/**
2113 * @brief Add a module reference into an array, checks for duplicities.
2114 *
2115 * @param[in] ctx Compile context.
2116 * @param[in] mod Module reference to add.
2117 * @param[in,out] mod_array Module sized array to add to.
2118 * @return LY_ERR value.
2119 */
2120static LY_ERR
2121lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2122{
2123 LY_ARRAY_COUNT_TYPE u;
2124 struct lys_module **new_mod;
2125
2126 LY_ARRAY_FOR(*mod_array, u) {
2127 if ((*mod_array)[u] == mod) {
2128 /* already there */
2129 return LY_EEXIST;
2130 }
2131 }
2132
2133 /* add the new module ref */
2134 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2135 *new_mod = mod;
2136
2137 return LY_SUCCESS;
2138}
2139
Michal Vasko1ccbf542021-04-19 11:35:00 +02002140/**
2141 * @brief Check whether all modules in a set are implemented.
2142 *
2143 * @param[in] mod_set Module set to check.
2144 * @return Whether all modules are implemented or not.
2145 */
2146static ly_bool
2147lys_precompile_mod_set_all_implemented(const struct ly_set *mod_set)
2148{
2149 uint32_t i;
2150 const struct lys_module *mod;
2151
2152 for (i = 0; i < mod_set->count; ++i) {
2153 mod = mod_set->objs[i];
2154 if (!mod->implemented) {
2155 return 0;
2156 }
2157 }
2158
2159 return 1;
2160}
2161
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002162LY_ERR
2163lys_precompile_augments_deviations(struct lysc_ctx *ctx)
2164{
Michal Vasko1ccbf542021-04-19 11:35:00 +02002165 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002166 LY_ARRAY_COUNT_TYPE u, v;
2167 const struct lysp_module *mod_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002168 struct lys_module *mod;
2169 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002170 struct lysp_node_augment *aug;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002171 uint32_t idx;
2172 struct ly_set mod_set = {0}, set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002173
2174 mod_p = ctx->cur_mod->parsed;
2175
Radek Krejci2a9fc652021-01-22 17:44:34 +01002176 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002177 /* get target module */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002178 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002179 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002180 ret = lys_nodeid_mod_check(ctx, aug->nodeid, 1, &set, NULL, &mod);
2181 lysc_update_path(ctx, NULL, NULL);
2182 lysc_update_path(ctx, NULL, NULL);
2183 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002184
Michal Vasko1ccbf542021-04-19 11:35:00 +02002185 /* add this module into the target module augmented_by, if not there and implemented */
2186 if ((lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by) != LY_EEXIST) ||
2187 !lys_precompile_mod_set_all_implemented(&set)) {
2188 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002189 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002190 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002191 }
2192
2193 LY_ARRAY_FOR(mod_p->deviations, u) {
2194 /* get target module */
2195 lysc_update_path(ctx, NULL, "{deviation}");
2196 lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002197 ret = lys_nodeid_mod_check(ctx, mod_p->deviations[u].nodeid, 1, &set, NULL, &mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002198 lysc_update_path(ctx, NULL, NULL);
2199 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002200 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002201
Michal Vasko1ccbf542021-04-19 11:35:00 +02002202 /* add this module into the target module deviated_by, if not there and implemented */
2203 if ((lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by) != LY_EEXIST) ||
2204 !lys_precompile_mod_set_all_implemented(&set)) {
2205 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2206 }
2207 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002208 }
2209
2210 /* the same for augments and deviations in submodules */
2211 LY_ARRAY_FOR(mod_p->includes, v) {
2212 submod = mod_p->includes[v].submodule;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002213 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002214 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002215 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002216 ret = lys_nodeid_mod_check(ctx, aug->nodeid, 1, &set, NULL, &mod);
2217 lysc_update_path(ctx, NULL, NULL);
2218 lysc_update_path(ctx, NULL, NULL);
2219 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002220
Michal Vasko1ccbf542021-04-19 11:35:00 +02002221 if ((lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by) != LY_EEXIST) ||
2222 !lys_precompile_mod_set_all_implemented(&set)) {
2223 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002224 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002225 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002226 }
2227
2228 LY_ARRAY_FOR(submod->deviations, u) {
2229 lysc_update_path(ctx, NULL, "{deviation}");
2230 lysc_update_path(ctx, NULL, submod->deviations[u].nodeid);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002231 ret = lys_nodeid_mod_check(ctx, submod->deviations[u].nodeid, 1, &set, NULL, &mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002232 lysc_update_path(ctx, NULL, NULL);
2233 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002234 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002235
Michal Vasko1ccbf542021-04-19 11:35:00 +02002236 if ((lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by) != LY_EEXIST) ||
2237 !lys_precompile_mod_set_all_implemented(&set)) {
2238 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2239 }
2240 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002241 }
2242 }
2243
Michal Vasko1ccbf542021-04-19 11:35:00 +02002244 if (mod_set.count) {
2245 /* descending order to make sure the modules are implemented in the right order */
2246 idx = mod_set.count;
2247 do {
2248 --idx;
2249 mod = mod_set.objs[idx];
2250
2251 if (mod == ctx->cur_mod) {
2252 /* will be applied normally later */
2253 continue;
2254 }
2255
2256 if (!mod->implemented) {
2257 /* implement (compile) the target module with our augments/deviations */
2258 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod, NULL, ctx->unres), cleanup);
Michal Vaskoe0cd6d92021-04-23 13:44:40 +02002259 } else if (!ctx->unres->full_compilation) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002260 /* target module was already compiled, we need to recompile it */
2261 ctx->unres->recompile = 1;
2262 }
Michal Vaskoe0cd6d92021-04-23 13:44:40 +02002263 /* else the module is implemented and was compiled in this compilation run or will yet be;
2264 * we actually do not need the module compiled now because its compiled nodes will not be accessed,
2265 * augments/deviations are applied during the target module compilation and the rest is in global unres */
Michal Vasko1ccbf542021-04-19 11:35:00 +02002266
2267 if (ctx->unres->recompile) {
2268 /* we need some module recompiled and cannot continue */
2269 goto cleanup;
2270 }
2271 } while (idx);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002272 }
2273
Michal Vasko1ccbf542021-04-19 11:35:00 +02002274cleanup:
2275 ly_set_erase(&set, NULL);
2276 ly_set_erase(&mod_set, NULL);
2277 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002278}
2279
2280void
2281lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2282{
2283 uint32_t i;
2284 LY_ARRAY_COUNT_TYPE u, count;
2285 struct lys_module *m;
2286
2287 for (i = 0; i < ctx->list.count; ++i) {
2288 m = ctx->list.objs[i];
2289
2290 if (m->augmented_by) {
2291 count = LY_ARRAY_COUNT(m->augmented_by);
2292 for (u = 0; u < count; ++u) {
2293 if (m->augmented_by[u] == mod) {
2294 /* keep the order */
2295 if (u < count - 1) {
2296 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2297 }
2298 LY_ARRAY_DECREMENT(m->augmented_by);
2299 break;
2300 }
2301 }
2302 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2303 LY_ARRAY_FREE(m->augmented_by);
2304 m->augmented_by = NULL;
2305 }
2306 }
2307
2308 if (m->deviated_by) {
2309 count = LY_ARRAY_COUNT(m->deviated_by);
2310 for (u = 0; u < count; ++u) {
2311 if (m->deviated_by[u] == mod) {
2312 /* keep the order */
2313 if (u < count - 1) {
2314 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2315 }
2316 LY_ARRAY_DECREMENT(m->deviated_by);
2317 break;
2318 }
2319 }
2320 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2321 LY_ARRAY_FREE(m->deviated_by);
2322 m->deviated_by = NULL;
2323 }
2324 }
2325 }
2326}