blob: 9353be6d4b3efaea35dd095ab560e0b0a9894140 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_amend.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko19a09022021-06-15 11:54:08 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02005 * @brief Schema compilation of augments, deviations, and refines.
6 *
Michal Vasko19a09022021-06-15 11:54:08 +02007 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#define _GNU_SOURCE
17
18#include "schema_compile_amend.h"
19
20#include <assert.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020021#include <stddef.h>
22#include <stdint.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020023#include <stdlib.h>
24#include <string.h>
25
26#include "common.h"
Radek Krejci77114102021-03-10 15:21:57 +010027#include "dict.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020028#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010029#include "plugins_exts_compile.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020030#include "schema_compile.h"
31#include "schema_compile_node.h"
Michal Vasko29dd11e2020-11-23 16:52:22 +010032#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020033#include "set.h"
34#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010035#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020036#include "tree_schema.h"
37#include "tree_schema_internal.h"
38#include "xpath.h"
39
40static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest,
41 size_t nametest_len, const struct lysp_module *mod, const char **name, size_t *name_len);
42
Michal Vasko1ccbf542021-04-19 11:35:00 +020043/**
44 * @brief Check the syntax of a node-id and collect all the referenced modules.
45 *
46 * @param[in] ctx Compile context.
47 * @param[in] nodeid Node-id to check.
48 * @param[in] abs Whether @p nodeid is absolute.
49 * @param[in,out] mod_set Set to add referenced modules into.
50 * @param[out] expr Optional node-id parsed into an expression.
51 * @param[out] target_mod Optional target module of the node-id.
52 * @return LY_ERR value.
53 */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020054static LY_ERR
Michal Vasko1ccbf542021-04-19 11:35:00 +020055lys_nodeid_mod_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct ly_set *mod_set,
56 struct lyxp_expr **expr, struct lys_module **target_mod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020057{
58 LY_ERR ret = LY_SUCCESS;
59 struct lyxp_expr *e = NULL;
60 struct lys_module *tmod = NULL, *mod;
61 const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid";
62 uint32_t i;
63
64 /* parse */
65 ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e);
66 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010067 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020068 nodeid_type, nodeid);
69 ret = LY_EVALID;
70 goto cleanup;
71 }
72
73 if (abs) {
74 /* absolute schema nodeid */
75 i = 0;
76 } else {
77 /* descendant schema nodeid */
78 if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010079 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020080 nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]);
81 ret = LY_EVALID;
82 goto cleanup;
83 }
84 i = 1;
85 }
86
87 /* check all the tokens */
88 for ( ; i < e->used; i += 2) {
89 if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010090 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020091 nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]);
92 ret = LY_EVALID;
93 goto cleanup;
94 } else if (e->used == i + 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010095 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020096 "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr);
97 ret = LY_EVALID;
98 goto cleanup;
99 } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100100 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200101 nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
102 ret = LY_EVALID;
103 goto cleanup;
104 } else if (abs) {
105 mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1],
106 e->tok_len[i + 1], ctx->pmod, NULL, NULL);
107 LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup);
108
109 /* only keep the first module */
110 if (!tmod) {
111 tmod = mod;
112 }
113
Michal Vasko1ccbf542021-04-19 11:35:00 +0200114 /* store the referenced module */
115 LY_CHECK_GOTO(ret = ly_set_add(mod_set, mod, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200116 }
117 }
118
119cleanup:
120 if (ret || !expr) {
121 lyxp_expr_free(ctx->ctx, e);
122 e = NULL;
123 }
124 if (expr) {
125 *expr = ret ? NULL : e;
126 }
127 if (target_mod) {
128 *target_mod = ret ? NULL : tmod;
129 }
130 return ret;
131}
132
133/**
134 * @brief Check whether 2 schema nodeids match.
135 *
136 * @param[in] ctx libyang context.
137 * @param[in] exp1 First schema nodeid.
138 * @param[in] exp1p_mod Module of @p exp1 nodes without any prefix.
139 * @param[in] exp2 Second schema nodeid.
140 * @param[in] exp2_pmod Module of @p exp2 nodes without any prefix.
141 * @return Whether the schema nodeids match or not.
142 */
143static ly_bool
144lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lysp_module *exp1_pmod,
145 const struct lyxp_expr *exp2, const struct lysp_module *exp2_pmod)
146{
147 uint32_t i;
148 const struct lys_module *mod1, *mod2;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100149 const char *name1 = NULL, *name2 = NULL;
150 size_t name1_len = 0, name2_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200151
152 if (exp1->used != exp2->used) {
153 return 0;
154 }
155
156 for (i = 0; i < exp1->used; ++i) {
157 assert(exp1->tokens[i] == exp2->tokens[i]);
158
159 if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) {
160 /* check modules of all the nodes in the node ID */
161 mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_pmod,
162 &name1, &name1_len);
163 assert(mod1);
164 mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_pmod,
165 &name2, &name2_len);
166 assert(mod2);
167
168 /* compare modules */
169 if (mod1 != mod2) {
170 return 0;
171 }
172
173 /* compare names */
174 if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) {
175 return 0;
176 }
177 }
178 }
179
180 return 1;
181}
182
183LY_ERR
184lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node)
185{
186 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200187 struct lyxp_expr *exp = NULL;
188 struct lysc_augment *aug;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100189 struct lysp_node_augment *aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200190 struct lysc_refine *rfn;
191 struct lysp_refine **new_rfn;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100192 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200193 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +0200194 struct ly_set mod_set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200195
Radek Krejci2a9fc652021-01-22 17:44:34 +0100196 LY_LIST_FOR(uses_p->augments, aug_p) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200197 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +0100198 lysc_update_path(ctx, NULL, aug_p->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200199
200 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200201 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 +0200202
203 /* allocate new compiled augment and store it in the set */
204 aug = calloc(1, sizeof *aug);
205 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
206 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, 1, NULL), cleanup);
207
208 aug->nodeid = exp;
209 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +0100210 aug->aug_pmod = ctx->pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200211 aug->nodeid_ctx_node = ctx_node;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100212 aug->aug_p = aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200213
214 lysc_update_path(ctx, NULL, NULL);
215 lysc_update_path(ctx, NULL, NULL);
216 }
217
218 LY_ARRAY_FOR(uses_p->refines, u) {
219 lysc_update_path(ctx, NULL, "{refine}");
220 lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid);
221
222 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200223 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 +0200224
225 /* try to find the node in already compiled refines */
226 rfn = NULL;
227 for (i = 0; i < ctx->uses_rfns.count; ++i) {
228 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->pmod, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid,
229 ctx->pmod)) {
230 rfn = ctx->uses_rfns.objs[i];
231 break;
232 }
233 }
234
235 if (!rfn) {
236 /* allocate new compiled refine */
237 rfn = calloc(1, sizeof *rfn);
238 LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
239 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, 1, NULL), cleanup);
240
241 rfn->nodeid = exp;
242 exp = NULL;
Michal Vasko7d3708f2021-02-03 10:50:24 +0100243 rfn->nodeid_pmod = ctx->cur_mod->parsed;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200244 rfn->nodeid_ctx_node = ctx_node;
Michal Vaskod8655722021-01-12 15:20:36 +0100245 rfn->uses_p = uses_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200246 } else {
247 /* just free exp */
248 lyxp_expr_free(ctx->ctx, exp);
249 exp = NULL;
250 }
251
252 /* add new parsed refine structure */
253 LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup);
254 *new_rfn = &uses_p->refines[u];
255
256 lysc_update_path(ctx, NULL, NULL);
257 lysc_update_path(ctx, NULL, NULL);
258 }
259
260cleanup:
Michal Vasko1ccbf542021-04-19 11:35:00 +0200261 if (ret) {
262 lysc_update_path(ctx, NULL, NULL);
263 lysc_update_path(ctx, NULL, NULL);
264 }
265 /* should include only this module, will fail later if not */
266 ly_set_erase(&mod_set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200267 lyxp_expr_free(ctx->ctx, exp);
268 return ret;
269}
270
271static LY_ERR
272lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext)
273{
274 LY_ERR ret = LY_SUCCESS;
275
276 *ext = *orig_ext;
277 DUP_STRING(ctx, orig_ext->name, ext->name, ret);
278 DUP_STRING(ctx, orig_ext->argument, ext->argument, ret);
279
280 return ret;
281}
282
283static LY_ERR
284lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr)
285{
286 LY_ERR ret = LY_SUCCESS;
287
288 if (orig_restr) {
289 DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret);
290 restr->arg.mod = orig_restr->arg.mod;
291 DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret);
292 DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret);
293 DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret);
294 DUP_STRING(ctx, orig_restr->ref, restr->ref, ret);
295 DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup);
296 }
297
298 return ret;
299}
300
301static LY_ERR
302lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str)
303{
304 LY_ERR ret = LY_SUCCESS;
305
306 DUP_STRING(ctx, *orig_str, *str, ret);
307
308 return ret;
309}
310
311LY_ERR
312lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname)
313{
314 LY_ERR ret = LY_SUCCESS;
315
316 if (!orig_qname->str) {
317 return LY_SUCCESS;
318 }
319
320 DUP_STRING(ctx, orig_qname->str, qname->str, ret);
321 assert(orig_qname->mod);
322 qname->mod = orig_qname->mod;
323
324 return ret;
325}
326
327static LY_ERR
328lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm)
329{
330 LY_ERR ret = LY_SUCCESS;
331
332 DUP_STRING(ctx, orig_enm->name, enm->name, ret);
333 DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret);
334 DUP_STRING(ctx, orig_enm->ref, enm->ref, ret);
335 enm->value = orig_enm->value;
336 DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup);
337 DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup);
338 enm->flags = orig_enm->flags;
339
340 return ret;
341}
342
343static LY_ERR
344lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type)
345{
346 LY_ERR ret = LY_SUCCESS;
347
Michal Vaskoea868242021-06-21 09:28:32 +0200348 /* array macros read previous data so we must zero it */
349 memset(type, 0, sizeof *type);
350
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200351 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
352
353 if (orig_type->range) {
354 type->range = calloc(1, sizeof *type->range);
355 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
356 LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range));
357 }
358
359 if (orig_type->length) {
360 type->length = calloc(1, sizeof *type->length);
361 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
362 LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length));
363 }
364
365 DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup);
366 DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup);
367 DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup);
368 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done);
369 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
370 DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup);
371 DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup);
372
373 type->pmod = orig_type->pmod;
374 type->compiled = orig_type->compiled;
375
376 type->fraction_digits = orig_type->fraction_digits;
377 type->require_instance = orig_type->require_instance;
378 type->flags = orig_type->flags;
379
380done:
381 return ret;
382}
383
384static LY_ERR
385lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when)
386{
387 LY_ERR ret = LY_SUCCESS;
388
389 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
390 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
391 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
392 DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup);
393
394 return ret;
395}
396
397static LY_ERR
398lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
399{
400 LY_ERR ret = LY_SUCCESS;
401
402 node->parent = NULL;
403 node->nodetype = orig->nodetype;
404 node->flags = orig->flags;
405 node->next = NULL;
406 DUP_STRING(ctx, orig->name, node->name, ret);
407 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
408 DUP_STRING(ctx, orig->ref, node->ref, ret);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200409 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
410 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
411
412 return ret;
413}
414
Radek Krejci9a3823e2021-01-27 20:26:46 +0100415#define DUP_PWHEN(CTX, ORIG, NEW) \
416 if (ORIG) { \
417 NEW = calloc(1, sizeof *NEW); \
418 LY_CHECK_ERR_RET(!NEW, LOGMEM(CTX), LY_EMEM); \
419 LY_CHECK_RET(lysp_when_dup(CTX, NEW, ORIG)); \
420 }
421
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200422static LY_ERR
423lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
424{
425 LY_ERR ret = LY_SUCCESS;
426 struct lysp_node_container *cont;
427 const struct lysp_node_container *orig_cont;
428 struct lysp_node_leaf *leaf;
429 const struct lysp_node_leaf *orig_leaf;
430 struct lysp_node_leaflist *llist;
431 const struct lysp_node_leaflist *orig_llist;
432 struct lysp_node_list *list;
433 const struct lysp_node_list *orig_list;
434 struct lysp_node_choice *choice;
435 const struct lysp_node_choice *orig_choice;
436 struct lysp_node_case *cas;
437 const struct lysp_node_case *orig_cas;
438 struct lysp_node_anydata *any;
439 const struct lysp_node_anydata *orig_any;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100440 struct lysp_node_action *action;
441 const struct lysp_node_action *orig_action;
442 struct lysp_node_action_inout *action_inout;
443 const struct lysp_node_action_inout *orig_action_inout;
444 struct lysp_node_notif *notif;
445 const struct lysp_node_notif *orig_notif;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200446
Radek Krejci2a9fc652021-01-22 17:44:34 +0100447 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA |
448 LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200449
450 /* common part */
451 LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig));
452
453 /* specific part */
454 switch (node->nodetype) {
455 case LYS_CONTAINER:
456 cont = (struct lysp_node_container *)node;
457 orig_cont = (const struct lysp_node_container *)orig;
458
Radek Krejci9a3823e2021-01-27 20:26:46 +0100459 DUP_PWHEN(ctx, orig_cont->when, cont->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200460 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
461 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
462 /* we do not need the rest */
463 break;
464 case LYS_LEAF:
465 leaf = (struct lysp_node_leaf *)node;
466 orig_leaf = (const struct lysp_node_leaf *)orig;
467
Radek Krejci9a3823e2021-01-27 20:26:46 +0100468 DUP_PWHEN(ctx, orig_leaf->when, leaf->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200469 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
470 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
471 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
472 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
473 break;
474 case LYS_LEAFLIST:
475 llist = (struct lysp_node_leaflist *)node;
476 orig_llist = (const struct lysp_node_leaflist *)orig;
477
Radek Krejci9a3823e2021-01-27 20:26:46 +0100478 DUP_PWHEN(ctx, orig_llist->when, llist->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200479 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
480 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
481 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
482 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
483 llist->min = orig_llist->min;
484 llist->max = orig_llist->max;
485 break;
486 case LYS_LIST:
487 list = (struct lysp_node_list *)node;
488 orig_list = (const struct lysp_node_list *)orig;
489
Radek Krejci9a3823e2021-01-27 20:26:46 +0100490 DUP_PWHEN(ctx, orig_list->when, list->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200491 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
492 DUP_STRING(ctx, orig_list->key, list->key, ret);
493 /* we do not need these arrays */
494 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
495 list->min = orig_list->min;
496 list->max = orig_list->max;
497 break;
498 case LYS_CHOICE:
499 choice = (struct lysp_node_choice *)node;
500 orig_choice = (const struct lysp_node_choice *)orig;
501
Radek Krejci9a3823e2021-01-27 20:26:46 +0100502 DUP_PWHEN(ctx, orig_choice->when, choice->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200503 /* we do not need children */
504 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
505 break;
506 case LYS_CASE:
507 cas = (struct lysp_node_case *)node;
508 orig_cas = (const struct lysp_node_case *)orig;
509
Radek Krejci9a3823e2021-01-27 20:26:46 +0100510 DUP_PWHEN(ctx, orig_cas->when, cas->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200511 /* we do not need children */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200512 break;
513 case LYS_ANYDATA:
514 case LYS_ANYXML:
515 any = (struct lysp_node_anydata *)node;
516 orig_any = (const struct lysp_node_anydata *)orig;
517
Radek Krejci9a3823e2021-01-27 20:26:46 +0100518 DUP_PWHEN(ctx, orig_any->when, any->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200519 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
520 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100521 case LYS_RPC:
522 case LYS_ACTION:
523 action = (struct lysp_node_action *)node;
524 orig_action = (const struct lysp_node_action *)orig;
525
526 action->input.nodetype = orig_action->input.nodetype;
527 action->output.nodetype = orig_action->output.nodetype;
528 /* we do not need the rest */
529 break;
530 case LYS_INPUT:
531 case LYS_OUTPUT:
532 action_inout = (struct lysp_node_action_inout *)node;
533 orig_action_inout = (const struct lysp_node_action_inout *)orig;
534
535 DUP_ARRAY(ctx, orig_action_inout->musts, action_inout->musts, lysp_restr_dup);
536 /* we do not need the rest */
537 break;
538 case LYS_NOTIF:
539 notif = (struct lysp_node_notif *)node;
540 orig_notif = (const struct lysp_node_notif *)orig;
541
542 DUP_ARRAY(ctx, orig_notif->musts, notif->musts, lysp_restr_dup);
543 /* we do not need the rest */
544 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200545 default:
546 LOGINT_RET(ctx);
547 }
548
549 return ret;
550}
551
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200552/**
553 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
554 *
555 * @param[in] ctx libyang context.
556 * @param[in] pnode Node to duplicate.
557 * @param[in] with_links Whether to also copy any links (child, parent pointers).
558 * @param[out] dup_p Duplicated parsed node.
559 * @return LY_ERR value.
560 */
561static LY_ERR
562lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
563{
564 LY_ERR ret = LY_SUCCESS;
565 void *mem = NULL;
566
567 if (!pnode) {
568 *dup_p = NULL;
569 return LY_SUCCESS;
570 }
571
572 switch (pnode->nodetype) {
573 case LYS_CONTAINER:
574 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200575 break;
576 case LYS_LEAF:
577 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200578 break;
579 case LYS_LEAFLIST:
580 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200581 break;
582 case LYS_LIST:
583 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200584 break;
585 case LYS_CHOICE:
586 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200587 break;
588 case LYS_CASE:
589 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200590 break;
591 case LYS_ANYDATA:
592 case LYS_ANYXML:
593 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200594 break;
595 case LYS_INPUT:
596 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100597 mem = calloc(1, sizeof(struct lysp_node_action_inout));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200598 break;
599 case LYS_ACTION:
600 case LYS_RPC:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100601 mem = calloc(1, sizeof(struct lysp_node_action));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200602 break;
603 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100604 mem = calloc(1, sizeof(struct lysp_node_notif));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200605 break;
606 default:
607 LOGINT_RET(ctx);
608 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100609 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
610 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200611
612 if (with_links) {
613 /* copy also parent and child pointers */
614 ((struct lysp_node *)mem)->parent = pnode->parent;
615 switch (pnode->nodetype) {
616 case LYS_CONTAINER:
617 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
618 break;
619 case LYS_LIST:
620 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
621 break;
622 case LYS_CHOICE:
623 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
624 break;
625 case LYS_CASE:
626 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
627 break;
628 default:
629 break;
630 }
631 }
632
633cleanup:
634 if (ret) {
635 free(mem);
636 } else {
637 *dup_p = mem;
638 }
639 return ret;
640}
641
642#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100643 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 +0200644 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
645 ret = LY_EVALID; \
646 goto cleanup;
647
648#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
649 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100650 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 +0200651 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
652 ret = LY_EVALID; \
653 goto cleanup; \
654 }
655
656/**
657 * @brief Apply refine.
658 *
659 * @param[in] ctx Compile context.
660 * @param[in] rfn Refine to apply.
661 * @param[in,out] target Refine target.
662 * @return LY_ERR value.
663 */
664static LY_ERR
665lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
666{
667 LY_ERR ret = LY_SUCCESS;
668 LY_ARRAY_COUNT_TYPE u;
669 struct lysp_qname *qname;
670 struct lysp_restr **musts, *must;
671 uint32_t *num;
672
673 /* default value */
674 if (rfn->dflts) {
675 switch (target->nodetype) {
676 case LYS_LEAF:
677 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
678
Michal Vaskoe180ed02021-02-05 16:31:20 +0100679 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200680 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
681 break;
682 case LYS_LEAFLIST:
683 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100684 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200685 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
686 ret = LY_EVALID;
687 goto cleanup;
688 }
689
690 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
691 ((struct lysp_node_leaflist *)target)->dflts = NULL;
692 LY_ARRAY_FOR(rfn->dflts, u) {
693 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
694 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
695 }
696 break;
697 case LYS_CHOICE:
698 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
699
Michal Vaskoe180ed02021-02-05 16:31:20 +0100700 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200701 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
702 break;
703 default:
704 AMEND_WRONG_NODETYPE("refine", "replace", "default");
705 }
706 }
707
708 /* description */
709 if (rfn->dsc) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100710 lydict_remove(ctx->ctx, target->dsc);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200711 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
712 }
713
714 /* reference */
715 if (rfn->ref) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100716 lydict_remove(ctx->ctx, target->ref);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200717 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
718 }
719
720 /* config */
721 if (rfn->flags & LYS_CONFIG_MASK) {
Michal Vasko7c565922021-06-10 14:58:27 +0200722 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200723 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vasko7c565922021-06-10 14:58:27 +0200724 (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT)) ? "RPC/action" :
725 ctx->compile_opts & LYS_IS_NOTIF ? "notification" : "a subtree ignoring config", ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200726 } else {
727 target->flags &= ~LYS_CONFIG_MASK;
728 target->flags |= rfn->flags & LYS_CONFIG_MASK;
729 }
730 }
731
732 /* mandatory */
733 if (rfn->flags & LYS_MAND_MASK) {
734 switch (target->nodetype) {
735 case LYS_LEAF:
736 case LYS_CHOICE:
737 case LYS_ANYDATA:
738 case LYS_ANYXML:
739 break;
740 default:
741 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
742 }
743
744 target->flags &= ~LYS_MAND_MASK;
745 target->flags |= rfn->flags & LYS_MAND_MASK;
746 }
747
748 /* presence */
749 if (rfn->presence) {
750 switch (target->nodetype) {
751 case LYS_CONTAINER:
752 break;
753 default:
754 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
755 }
756
Michal Vaskoe180ed02021-02-05 16:31:20 +0100757 lydict_remove(ctx->ctx, ((struct lysp_node_container *)target)->presence);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200758 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
759 }
760
761 /* must */
762 if (rfn->musts) {
763 switch (target->nodetype) {
764 case LYS_CONTAINER:
765 case LYS_LIST:
766 case LYS_LEAF:
767 case LYS_LEAFLIST:
768 case LYS_ANYDATA:
769 case LYS_ANYXML:
770 musts = &((struct lysp_node_container *)target)->musts;
771 break;
772 default:
773 AMEND_WRONG_NODETYPE("refine", "add", "must");
774 }
775
776 LY_ARRAY_FOR(rfn->musts, u) {
777 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
778 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
779 }
780 }
781
782 /* min-elements */
783 if (rfn->flags & LYS_SET_MIN) {
784 switch (target->nodetype) {
785 case LYS_LEAFLIST:
786 num = &((struct lysp_node_leaflist *)target)->min;
787 break;
788 case LYS_LIST:
789 num = &((struct lysp_node_list *)target)->min;
790 break;
791 default:
792 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
793 }
794
795 *num = rfn->min;
796 }
797
798 /* max-elements */
799 if (rfn->flags & LYS_SET_MAX) {
800 switch (target->nodetype) {
801 case LYS_LEAFLIST:
802 num = &((struct lysp_node_leaflist *)target)->max;
803 break;
804 case LYS_LIST:
805 num = &((struct lysp_node_list *)target)->max;
806 break;
807 default:
808 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
809 }
810
811 *num = rfn->max;
812 }
813
814 /* if-feature */
815 if (rfn->iffeatures) {
816 switch (target->nodetype) {
817 case LYS_LEAF:
818 case LYS_LEAFLIST:
819 case LYS_LIST:
820 case LYS_CONTAINER:
821 case LYS_CHOICE:
822 case LYS_CASE:
823 case LYS_ANYDATA:
824 case LYS_ANYXML:
825 break;
826 default:
827 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
828 }
829
830 LY_ARRAY_FOR(rfn->iffeatures, u) {
831 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
832 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
833 }
834 }
835
836 /* extension */
837 /* TODO refine extensions */
838
839cleanup:
840 return ret;
841}
842
843/**
844 * @brief Apply deviate add.
845 *
846 * @param[in] ctx Compile context.
847 * @param[in] d Deviate add to apply.
848 * @param[in,out] target Deviation target.
849 * @return LY_ERR value.
850 */
851static LY_ERR
852lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
853{
854 LY_ERR ret = LY_SUCCESS;
855 LY_ARRAY_COUNT_TYPE u;
856 struct lysp_qname *qname;
857 uint32_t *num;
858 struct lysp_restr **musts, *must;
859
860#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
861 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100862 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200863 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
864 ret = LY_EVALID; \
865 goto cleanup; \
866 }
867
868 /* [units-stmt] */
869 if (d->units) {
870 switch (target->nodetype) {
871 case LYS_LEAF:
872 case LYS_LEAFLIST:
873 break;
874 default:
875 AMEND_WRONG_NODETYPE("deviation", "add", "units");
876 }
877
878 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
879 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
880 }
881
882 /* *must-stmt */
883 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100884 musts = lysp_node_musts_p(target);
885 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200886 AMEND_WRONG_NODETYPE("deviation", "add", "must");
887 }
888
889 LY_ARRAY_FOR(d->musts, u) {
890 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
891 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
892 }
893 }
894
895 /* *unique-stmt */
896 if (d->uniques) {
897 switch (target->nodetype) {
898 case LYS_LIST:
899 break;
900 default:
901 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
902 }
903
904 LY_ARRAY_FOR(d->uniques, u) {
905 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
906 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
907 }
908 }
909
910 /* *default-stmt */
911 if (d->dflts) {
912 switch (target->nodetype) {
913 case LYS_LEAF:
914 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
915 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
916
917 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
918 break;
919 case LYS_LEAFLIST:
920 LY_ARRAY_FOR(d->dflts, u) {
921 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
922 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
923 }
924 break;
925 case LYS_CHOICE:
926 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
927 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
928
929 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
930 break;
931 default:
932 AMEND_WRONG_NODETYPE("deviation", "add", "default");
933 }
934 }
935
936 /* [config-stmt] */
937 if (d->flags & LYS_CONFIG_MASK) {
938 switch (target->nodetype) {
939 case LYS_CONTAINER:
940 case LYS_LEAF:
941 case LYS_LEAFLIST:
942 case LYS_LIST:
943 case LYS_CHOICE:
944 case LYS_ANYDATA:
945 case LYS_ANYXML:
946 break;
947 default:
948 AMEND_WRONG_NODETYPE("deviation", "add", "config");
949 }
950
951 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100952 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200953 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
954 target->flags & LYS_CONFIG_W ? "true" : "false");
955 ret = LY_EVALID;
956 goto cleanup;
957 }
958
959 target->flags |= d->flags & LYS_CONFIG_MASK;
960 }
961
962 /* [mandatory-stmt] */
963 if (d->flags & LYS_MAND_MASK) {
964 switch (target->nodetype) {
965 case LYS_LEAF:
966 case LYS_CHOICE:
967 case LYS_ANYDATA:
968 case LYS_ANYXML:
969 break;
970 default:
971 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
972 }
973
974 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100975 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200976 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
977 target->flags & LYS_MAND_TRUE ? "true" : "false");
978 ret = LY_EVALID;
979 goto cleanup;
980 }
981
982 target->flags |= d->flags & LYS_MAND_MASK;
983 }
984
985 /* [min-elements-stmt] */
986 if (d->flags & LYS_SET_MIN) {
987 switch (target->nodetype) {
988 case LYS_LEAFLIST:
989 num = &((struct lysp_node_leaflist *)target)->min;
990 break;
991 case LYS_LIST:
992 num = &((struct lysp_node_list *)target)->min;
993 break;
994 default:
995 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
996 }
997
998 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100999 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001000 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
1001 ret = LY_EVALID;
1002 goto cleanup;
1003 }
1004
1005 *num = d->min;
1006 }
1007
1008 /* [max-elements-stmt] */
1009 if (d->flags & LYS_SET_MAX) {
1010 switch (target->nodetype) {
1011 case LYS_LEAFLIST:
1012 num = &((struct lysp_node_leaflist *)target)->max;
1013 break;
1014 case LYS_LIST:
1015 num = &((struct lysp_node_list *)target)->max;
1016 break;
1017 default:
1018 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1019 }
1020
1021 if (target->flags & LYS_SET_MAX) {
1022 if (*num) {
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 \"%u\").",
1025 *num);
1026 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001027 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001028 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1029 }
1030 ret = LY_EVALID;
1031 goto cleanup;
1032 }
1033
1034 *num = d->max;
1035 }
1036
1037cleanup:
1038 return ret;
1039}
1040
1041/**
1042 * @brief Apply deviate delete.
1043 *
1044 * @param[in] ctx Compile context.
1045 * @param[in] d Deviate delete to apply.
1046 * @param[in,out] target Deviation target.
1047 * @return LY_ERR value.
1048 */
1049static LY_ERR
1050lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1051{
1052 LY_ERR ret = LY_SUCCESS;
1053 struct lysp_restr **musts;
1054 LY_ARRAY_COUNT_TYPE u, v;
1055 struct lysp_qname **uniques, **dflts;
1056
1057#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1058 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1059 int found = 0; \
1060 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1061 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1062 found = 1; \
1063 break; \
1064 } \
1065 } \
1066 if (!found) { \
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 any of the target's property values.", \
1069 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1070 ret = LY_EVALID; \
1071 goto cleanup; \
1072 } \
1073 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1074 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
Michal Vasko08e9b112021-06-11 15:41:17 +02001075 if (v < LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1076 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1077 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001078 } \
1079 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1080 LY_ARRAY_FREE(ORIG_ARRAY); \
1081 ORIG_ARRAY = NULL; \
1082 }
1083
1084#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1085 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001086 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001087 ret = LY_EVALID; \
1088 goto cleanup; \
1089 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001090 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001091 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1092 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1093 ret = LY_EVALID; \
1094 goto cleanup; \
1095 }
1096
1097 /* [units-stmt] */
1098 if (d->units) {
1099 switch (target->nodetype) {
1100 case LYS_LEAF:
1101 case LYS_LEAFLIST:
1102 break;
1103 default:
1104 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1105 }
1106
1107 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001108 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001109 ((struct lysp_node_leaf *)target)->units = NULL;
1110 }
1111
1112 /* *must-stmt */
1113 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001114 musts = lysp_node_musts_p(target);
1115 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001116 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1117 }
1118
1119 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1120 }
1121
1122 /* *unique-stmt */
1123 if (d->uniques) {
1124 switch (target->nodetype) {
1125 case LYS_LIST:
1126 break;
1127 default:
1128 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1129 }
1130
1131 uniques = &((struct lysp_node_list *)target)->uniques;
1132 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1133 }
1134
1135 /* *default-stmt */
1136 if (d->dflts) {
1137 switch (target->nodetype) {
1138 case LYS_LEAF:
1139 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1140 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1141
Michal Vaskoe180ed02021-02-05 16:31:20 +01001142 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001143 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1144 break;
1145 case LYS_LEAFLIST:
1146 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1147 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1148 break;
1149 case LYS_CHOICE:
1150 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1151 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1152
Michal Vaskoe180ed02021-02-05 16:31:20 +01001153 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001154 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1155 break;
1156 default:
1157 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1158 }
1159 }
1160
1161cleanup:
1162 return ret;
1163}
1164
1165/**
1166 * @brief Apply deviate replace.
1167 *
1168 * @param[in] ctx Compile context.
1169 * @param[in] d Deviate replace to apply.
1170 * @param[in,out] target Deviation target.
1171 * @return LY_ERR value.
1172 */
1173static LY_ERR
1174lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1175{
1176 LY_ERR ret = LY_SUCCESS;
1177 uint32_t *num;
1178
1179#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1180 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001181 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001182 ret = LY_EVALID; \
1183 goto cleanup; \
1184 }
1185
1186 /* [type-stmt] */
1187 if (d->type) {
1188 switch (target->nodetype) {
1189 case LYS_LEAF:
1190 case LYS_LEAFLIST:
1191 break;
1192 default:
1193 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1194 }
1195
1196 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1197 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1198 }
1199
1200 /* [units-stmt] */
1201 if (d->units) {
1202 switch (target->nodetype) {
1203 case LYS_LEAF:
1204 case LYS_LEAFLIST:
1205 break;
1206 default:
1207 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1208 }
1209
1210 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001211 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001212 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1213 }
1214
1215 /* [default-stmt] */
1216 if (d->dflt.str) {
1217 switch (target->nodetype) {
1218 case LYS_LEAF:
1219 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1220
Michal Vaskoe180ed02021-02-05 16:31:20 +01001221 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001222 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1223 break;
1224 case LYS_CHOICE:
1225 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1226
Michal Vaskoe180ed02021-02-05 16:31:20 +01001227 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001228 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1229 break;
1230 default:
1231 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1232 }
1233 }
1234
1235 /* [config-stmt] */
1236 if (d->flags & LYS_CONFIG_MASK) {
1237 switch (target->nodetype) {
1238 case LYS_CONTAINER:
1239 case LYS_LEAF:
1240 case LYS_LEAFLIST:
1241 case LYS_LIST:
1242 case LYS_CHOICE:
1243 case LYS_ANYDATA:
1244 case LYS_ANYXML:
1245 break;
1246 default:
1247 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1248 }
1249
1250 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001251 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1252 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001253 ret = LY_EVALID;
1254 goto cleanup;
1255 }
1256
1257 target->flags &= ~LYS_CONFIG_MASK;
1258 target->flags |= d->flags & LYS_CONFIG_MASK;
1259 }
1260
1261 /* [mandatory-stmt] */
1262 if (d->flags & LYS_MAND_MASK) {
1263 switch (target->nodetype) {
1264 case LYS_LEAF:
1265 case LYS_CHOICE:
1266 case LYS_ANYDATA:
1267 case LYS_ANYXML:
1268 break;
1269 default:
1270 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1271 }
1272
1273 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001274 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1275 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001276 ret = LY_EVALID;
1277 goto cleanup;
1278 }
1279
1280 target->flags &= ~LYS_MAND_MASK;
1281 target->flags |= d->flags & LYS_MAND_MASK;
1282 }
1283
1284 /* [min-elements-stmt] */
1285 if (d->flags & LYS_SET_MIN) {
1286 switch (target->nodetype) {
1287 case LYS_LEAFLIST:
1288 num = &((struct lysp_node_leaflist *)target)->min;
1289 break;
1290 case LYS_LIST:
1291 num = &((struct lysp_node_list *)target)->min;
1292 break;
1293 default:
1294 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1295 }
1296
1297 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001298 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001299 ret = LY_EVALID;
1300 goto cleanup;
1301 }
1302
1303 *num = d->min;
1304 }
1305
1306 /* [max-elements-stmt] */
1307 if (d->flags & LYS_SET_MAX) {
1308 switch (target->nodetype) {
1309 case LYS_LEAFLIST:
1310 num = &((struct lysp_node_leaflist *)target)->max;
1311 break;
1312 case LYS_LIST:
1313 num = &((struct lysp_node_list *)target)->max;
1314 break;
1315 default:
1316 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1317 }
1318
1319 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001320 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001321 ret = LY_EVALID;
1322 goto cleanup;
1323 }
1324
1325 *num = d->max;
1326 }
1327
1328cleanup:
1329 return ret;
1330}
1331
1332/**
1333 * @brief Get module of a single nodeid node name test.
1334 *
1335 * @param[in] ctx libyang context.
1336 * @param[in] nametest Nametest with an optional prefix.
1337 * @param[in] nametest_len Length of @p nametest.
1338 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1339 * @param[out] name Optional pointer to the name test without the prefix.
1340 * @param[out] name_len Length of @p name.
1341 * @return Resolved module.
1342 */
1343static const struct lys_module *
1344lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1345 const struct lysp_module *mod, const char **name, size_t *name_len)
1346{
1347 const struct lys_module *target_mod;
1348 const char *ptr;
1349
1350 ptr = ly_strnchr(nametest, ':', nametest_len);
1351 if (ptr) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001352 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_VALUE_SCHEMA, (void *)mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001353 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001354 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001355 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001356 (int)nametest_len, nametest, (int)(ptr - nametest), nametest, LYSP_MODULE_NAME(mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001357 return NULL;
1358 }
1359
1360 if (name) {
1361 *name = ptr + 1;
1362 *name_len = nametest_len - ((ptr - nametest) + 1);
1363 }
1364 } else {
1365 target_mod = mod->mod;
1366 if (name) {
1367 *name = nametest;
1368 *name_len = nametest_len;
1369 }
1370 }
1371
1372 return target_mod;
1373}
1374
1375/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001376 * @brief Check whether a compiled node matches a single schema nodeid name test.
1377 *
1378 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1379 * @param[in] mod Expected module.
1380 * @param[in] name Expected name.
1381 * @param[in] name_len Length of @p name.
1382 * @return Whether it is a match or not.
1383 */
1384static ly_bool
1385lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1386 size_t name_len)
1387{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001388 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001389 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001390 return 0;
1391 }
1392
1393 /* compare names */
Michal Vasko544e58a2021-01-28 14:33:41 +01001394 if (ly_strncmp((*node)->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001395 return 0;
1396 }
1397
Michal Vasko2a668712020-10-21 11:48:09 +02001398 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001399 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001400
1401 return 1;
1402}
1403
1404/**
1405 * @brief Check whether a node matches specific schema nodeid.
1406 *
1407 * @param[in] exp Parsed nodeid to match.
1408 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1409 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1410 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1411 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1412 * @param[in] pnode_mod Compiled @p pnode to-be module.
1413 * @return Whether it is a match or not.
1414 */
1415static ly_bool
1416lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1417 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1418{
1419 uint32_t i;
1420 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001421 const char *name = NULL;
1422 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001423
1424 /* compare last node in the node ID */
1425 i = exp->used - 1;
1426
1427 /* get exp node ID module */
1428 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);
1429 assert(mod);
1430
1431 if (pnode) {
1432 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001433 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001434 return 0;
1435 }
1436 } else {
1437 /* using parent directly */
1438 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1439 return 0;
1440 }
1441 }
1442
1443 /* now compare all the compiled parents */
1444 while (i > 1) {
1445 i -= 2;
1446 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1447
1448 if (!parent) {
1449 /* no more parents but path continues */
1450 return 0;
1451 }
1452
1453 /* get exp node ID module */
1454 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1455 &name_len);
1456 assert(mod);
1457
1458 /* compare with the parent */
1459 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1460 return 0;
1461 }
1462 }
1463
1464 if (ctx_node && (ctx_node != parent)) {
1465 /* descendant path has not finished in the context node */
1466 return 0;
1467 } else if (!ctx_node && parent) {
1468 /* some parent was not matched */
1469 return 0;
1470 }
1471
1472 return 1;
1473}
1474
1475void
1476lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1477{
1478 if (aug) {
1479 lyxp_expr_free(ctx, aug->nodeid);
1480
1481 free(aug);
1482 }
1483}
1484
1485void
1486lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1487{
1488 if (dev) {
1489 lyxp_expr_free(ctx, dev->nodeid);
1490 LY_ARRAY_FREE(dev->devs);
1491 LY_ARRAY_FREE(dev->dev_pmods);
1492
1493 free(dev);
1494 }
1495}
1496
1497void
1498lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1499{
1500 if (rfn) {
1501 lyxp_expr_free(ctx, rfn->nodeid);
1502 LY_ARRAY_FREE(rfn->rfns);
1503
1504 free(rfn);
1505 }
1506}
1507
1508void
1509lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1510{
Michal Vasko426aa7e2021-06-21 13:30:52 +02001511 LY_ARRAY_COUNT_TYPE u;
1512
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001513 if (!dev_pnode) {
1514 return;
1515 }
1516
1517 switch (dev_pnode->nodetype) {
1518 case LYS_CONTAINER:
1519 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1520 break;
1521 case LYS_LIST:
1522 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1523 break;
1524 case LYS_CHOICE:
1525 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1526 break;
1527 case LYS_CASE:
1528 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1529 break;
1530 case LYS_LEAF:
1531 case LYS_LEAFLIST:
1532 case LYS_ANYXML:
1533 case LYS_ANYDATA:
1534 /* no children */
1535 break;
1536 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001537 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001538 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001539 case LYS_RPC:
1540 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001541 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1542 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001543 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001544 case LYS_INPUT:
1545 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001546 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001547 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001548 free(dev_pnode);
1549 return;
1550 default:
1551 LOGINT(ctx);
1552 return;
1553 }
1554
Michal Vasko426aa7e2021-06-21 13:30:52 +02001555 /* extension parsed tree and children were not duplicated */
1556 LY_ARRAY_FOR(dev_pnode->exts, u) {
1557 dev_pnode->exts[u].parsed = NULL;
1558 dev_pnode->exts[u].child = NULL;
1559 }
1560
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001561 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1562}
1563
1564LY_ERR
1565lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1566 struct lysp_node **dev_pnode, ly_bool *not_supported)
1567{
1568 LY_ERR ret = LY_SUCCESS;
1569 uint32_t i;
1570 LY_ARRAY_COUNT_TYPE u;
1571 struct lys_module *orig_mod = ctx->cur_mod;
1572 struct lysp_module *orig_pmod = ctx->pmod;
1573 char orig_path[LYSC_CTX_BUFSIZE];
1574 struct lysc_refine *rfn;
1575 struct lysc_deviation *dev;
1576 struct lysp_deviation *dev_p;
1577 struct lysp_deviate *d;
1578
1579 *dev_pnode = NULL;
1580 *not_supported = 0;
1581
1582 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1583 rfn = ctx->uses_rfns.objs[i];
1584
Michal Vasko28fe5f62021-03-03 16:37:39 +01001585 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 +02001586 /* not our target node */
1587 continue;
1588 }
1589
1590 if (!*dev_pnode) {
1591 /* first refine on this node, create a copy first */
1592 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1593 }
1594
Michal Vaskob8df5762021-01-12 15:15:53 +01001595 /* use modules from the refine */
1596 ctx->cur_mod = rfn->nodeid_pmod->mod;
1597 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1598
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001599 /* apply all the refines by changing (the copy of) the parsed node */
1600 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001601 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001602 lysc_update_path(ctx, NULL, "{refine}");
1603 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001604
1605 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001606 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1607 lysc_update_path(ctx, NULL, NULL);
1608 lysc_update_path(ctx, NULL, NULL);
1609 LY_CHECK_GOTO(ret, cleanup);
1610 }
1611
1612 /* refine was applied, remove it */
1613 lysc_refine_free(ctx->ctx, rfn);
1614 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1615
1616 /* all the refines for one target node are in one structure, we are done */
1617 break;
1618 }
1619
1620 for (i = 0; i < ctx->devs.count; ++i) {
1621 dev = ctx->devs.objs[i];
1622
Michal Vasko28fe5f62021-03-03 16:37:39 +01001623 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001624 /* not our target node */
1625 continue;
1626 }
1627
1628 if (dev->not_supported) {
1629 /* it is not supported, no more deviations */
1630 *not_supported = 1;
1631 goto dev_applied;
1632 }
1633
1634 if (!*dev_pnode) {
1635 /* first deviation on this node, create a copy first */
1636 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1637 }
1638
1639 /* apply all the deviates by changing (the copy of) the parsed node */
1640 LY_ARRAY_FOR(dev->devs, u) {
1641 dev_p = dev->devs[u];
1642 LY_LIST_FOR(dev_p->deviates, d) {
1643 /* generate correct path */
1644 strcpy(orig_path, ctx->path);
1645 ctx->path_len = 1;
1646 ctx->cur_mod = dev->dev_pmods[u]->mod;
1647 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1648 lysc_update_path(ctx, NULL, "{deviation}");
1649 lysc_update_path(ctx, NULL, dev_p->nodeid);
1650
1651 switch (d->mod) {
1652 case LYS_DEV_ADD:
1653 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1654 break;
1655 case LYS_DEV_DELETE:
1656 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1657 break;
1658 case LYS_DEV_REPLACE:
1659 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1660 break;
1661 default:
1662 LOGINT(ctx->ctx);
1663 ret = LY_EINT;
1664 }
1665
1666 /* restore previous path */
1667 strcpy(ctx->path, orig_path);
1668 ctx->path_len = strlen(ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001669
1670 LY_CHECK_GOTO(ret, cleanup);
1671 }
1672 }
1673
1674dev_applied:
1675 /* deviation was applied, remove it */
1676 lysc_deviation_free(ctx->ctx, dev);
1677 ly_set_rm_index(&ctx->devs, i, NULL);
1678
1679 /* all the deviations for one target node are in one structure, we are done */
1680 break;
1681 }
1682
1683cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001684 ctx->cur_mod = orig_mod;
1685 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001686 if (ret) {
1687 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1688 *dev_pnode = NULL;
1689 *not_supported = 0;
1690 }
1691 return ret;
1692}
1693
1694/**
1695 * @brief Compile the parsed augment connecting it into its target.
1696 *
1697 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1698 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1699 * are already implemented and compiled.
1700 *
1701 * @param[in] ctx Compile context.
1702 * @param[in] aug_p Parsed augment to compile.
1703 * @param[in] target Target node of the augment.
1704 * @return LY_SUCCESS on success.
1705 * @return LY_EVALID on failure.
1706 */
1707static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001708lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001709{
1710 LY_ERR ret = LY_SUCCESS;
1711 struct lysp_node *pnode;
1712 struct lysc_node *node;
1713 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001714 struct lysc_node_action **actions;
1715 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001716 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001717 struct ly_set child_set = {0};
Michal Vasko7c565922021-06-10 14:58:27 +02001718 uint32_t i, opt_prev = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001719
1720 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001721 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001722 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1723 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1724 ret = LY_EVALID;
1725 goto cleanup;
1726 }
1727
1728 /* check for mandatory nodes
1729 * - new cases augmenting some choice can have mandatory nodes
1730 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1731 */
1732 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1733 allow_mandatory = 1;
1734 }
1735
1736 LY_LIST_FOR(aug_p->child, pnode) {
1737 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1738 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1739 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1740 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001741 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001742 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1743 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1744 ret = LY_EVALID;
1745 goto cleanup;
1746 }
1747
1748 /* compile the children */
1749 if (target->nodetype == LYS_CHOICE) {
1750 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001751 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1752 if (target->nodetype == LYS_INPUT) {
Michal Vasko7c565922021-06-10 14:58:27 +02001753 ctx->compile_opts |= LYS_COMPILE_RPC_INPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001754 } else {
Michal Vasko7c565922021-06-10 14:58:27 +02001755 ctx->compile_opts |= LYS_COMPILE_RPC_OUTPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001756 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001757 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001758 } else {
1759 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1760 }
1761
Michal Vasko29dd11e2020-11-23 16:52:22 +01001762 /* eval if-features again for the rest of this node processing */
1763 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1764 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001765 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001766 }
1767
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001768 /* since the augment node is not present in the compiled tree, we need to pass some of its
1769 * statements to all its children */
1770 for (i = 0; i < child_set.count; ++i) {
1771 node = child_set.snodes[i];
1772 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1773 node->flags &= ~LYS_MAND_TRUE;
1774 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001775 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko23864e02021-06-24 09:23:58 +02001776 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.",
1777 node->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001778 ret = LY_EVALID;
1779 goto cleanup;
1780 }
1781
1782 if (aug_p->when) {
1783 /* pass augment's when to all the children */
Michal Vaskodfd254d2021-06-24 09:24:59 +02001784 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, lysc_data_node(target), node, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001785 LY_CHECK_GOTO(ret, cleanup);
1786 }
1787 }
1788 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001789
1790 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001791 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001792 }
1793
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001794 actions = lysc_node_actions_p(target);
1795 notifs = lysc_node_notifs_p(target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001796
1797 if (aug_p->actions) {
1798 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001799 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001800 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001801 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001802 ret = LY_EVALID;
1803 goto cleanup;
1804 }
1805
1806 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001807 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001808 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001809
Michal Vasko012807e2021-02-03 09:52:18 +01001810 /* eval if-features again for the rest of this node processing */
1811 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1812 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001813 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001814 }
Michal Vasko012807e2021-02-03 09:52:18 +01001815
1816 /* since the augment node is not present in the compiled tree, we need to pass some of its
1817 * statements to all its children */
1818 for (i = 0; i < child_set.count; ++i) {
1819 node = child_set.snodes[i];
1820 if (aug_p->when) {
1821 /* pass augment's when to all the actions */
Michal Vaskodfd254d2021-06-24 09:24:59 +02001822 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, lysc_data_node(target), node, &when_shared);
Michal Vasko012807e2021-02-03 09:52:18 +01001823 LY_CHECK_GOTO(ret, cleanup);
1824 }
1825 }
1826 ly_set_erase(&child_set, NULL);
1827
1828 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001829 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001830 }
1831 }
1832 if (aug_p->notifs) {
1833 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001834 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001835 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001836 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001837 ret = LY_EVALID;
1838 goto cleanup;
1839 }
1840
1841 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001842 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001843 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001844
Michal Vasko012807e2021-02-03 09:52:18 +01001845 /* eval if-features again for the rest of this node processing */
1846 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1847 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001848 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001849 }
Michal Vasko012807e2021-02-03 09:52:18 +01001850
1851 /* since the augment node is not present in the compiled tree, we need to pass some of its
1852 * statements to all its children */
1853 for (i = 0; i < child_set.count; ++i) {
1854 node = child_set.snodes[i];
1855 if (aug_p->when) {
1856 /* pass augment's when to all the actions */
Michal Vaskodfd254d2021-06-24 09:24:59 +02001857 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, lysc_data_node(target), node, &when_shared);
Michal Vasko012807e2021-02-03 09:52:18 +01001858 LY_CHECK_GOTO(ret, cleanup);
1859 }
1860 }
1861 ly_set_erase(&child_set, NULL);
1862
1863 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001864 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001865 }
1866 }
1867
1868cleanup:
1869 ly_set_erase(&child_set, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02001870 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001871 return ret;
1872}
1873
1874LY_ERR
1875lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1876{
1877 LY_ERR ret = LY_SUCCESS;
1878 struct lys_module *orig_mod = ctx->cur_mod;
1879 struct lysp_module *orig_pmod = ctx->pmod;
1880 uint32_t i;
1881 char orig_path[LYSC_CTX_BUFSIZE];
1882 struct lysc_augment *aug;
1883
1884 /* uses augments */
1885 for (i = 0; i < ctx->uses_augs.count; ) {
1886 aug = ctx->uses_augs.objs[i];
1887
Michal Vasko28fe5f62021-03-03 16:37:39 +01001888 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001889 /* not our target node */
1890 ++i;
1891 continue;
1892 }
1893
Michal Vaskob8df5762021-01-12 15:15:53 +01001894 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001895 lysc_update_path(ctx, NULL, "{augment}");
1896 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001897 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001898
1899 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001900 ret = lys_compile_augment(ctx, aug->aug_p, node);
1901 lysc_update_path(ctx, NULL, NULL);
1902 lysc_update_path(ctx, NULL, NULL);
1903 LY_CHECK_GOTO(ret, cleanup);
1904
Michal Vaskoc75f2042021-06-08 14:57:03 +02001905 /* augment was applied, remove it (index and the whole set may have changed because other augments
1906 * could have been applied) */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001907 ly_set_rm(&ctx->uses_augs, aug, NULL);
1908 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001909 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001910 }
1911
1912 /* top-level augments */
1913 for (i = 0; i < ctx->augs.count; ) {
1914 aug = ctx->augs.objs[i];
1915
Michal Vasko28fe5f62021-03-03 16:37:39 +01001916 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001917 /* not our target node */
1918 ++i;
1919 continue;
1920 }
1921
Michal Vaskob8df5762021-01-12 15:15:53 +01001922 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001923 strcpy(orig_path, ctx->path);
1924 ctx->path_len = 1;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001925 ctx->cur_mod = aug->aug_pmod->mod;
1926 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001927 lysc_update_path(ctx, NULL, "{augment}");
1928 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001929
1930 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001931 ret = lys_compile_augment(ctx, aug->aug_p, node);
1932 strcpy(ctx->path, orig_path);
1933 ctx->path_len = strlen(ctx->path);
1934 LY_CHECK_GOTO(ret, cleanup);
1935
1936 /* augment was applied, remove it */
1937 ly_set_rm(&ctx->augs, aug, NULL);
1938 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001939 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001940 }
1941
1942cleanup:
1943 ctx->cur_mod = orig_mod;
1944 ctx->pmod = orig_pmod;
1945 return ret;
1946}
1947
1948/**
1949 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1950 *
1951 * @param[in] ctx Compile context.
1952 * @param[in] aug_p Parsed augment to be applied.
1953 * @param[in] pmod Both current and prefix module for @p aug_p.
1954 * @return LY_ERR value.
1955 */
1956static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001957lys_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 +02001958{
1959 LY_ERR ret = LY_SUCCESS;
1960 struct lyxp_expr *exp = NULL;
1961 struct lysc_augment *aug;
1962 const struct lys_module *mod;
1963
1964 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1965 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
1966 LY_CHECK_GOTO(ret, cleanup);
1967
1968 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
1969 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
1970 if (mod != ctx->cur_mod) {
1971 /* augment for another module, ignore */
1972 goto cleanup;
1973 }
1974
1975 /* allocate new compiled augment and store it in the set */
1976 aug = calloc(1, sizeof *aug);
1977 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1978 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
1979
1980 aug->nodeid = exp;
1981 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001982 aug->aug_pmod = pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001983 aug->aug_p = aug_p;
1984
1985cleanup:
1986 lyxp_expr_free(ctx->ctx, exp);
1987 return ret;
1988}
1989
1990LY_ERR
1991lys_precompile_own_augments(struct lysc_ctx *ctx)
1992{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001993 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001994
1995 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001996 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
1997 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001998
1999 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002000 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
2001 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002002 }
2003
2004 /* collect all submodules augments */
2005 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002006 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
2007 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 +02002008 }
2009 }
2010 }
2011
2012 return LY_SUCCESS;
2013}
2014
2015/**
2016 * @brief Prepare a deviation to be applied during data nodes compilation.
2017 *
2018 * @param[in] ctx Compile context.
2019 * @param[in] dev_p Parsed deviation to be applied.
2020 * @param[in] pmod Both current and prefix module for @p dev_p.
2021 * @return LY_ERR value.
2022 */
2023static LY_ERR
2024lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2025{
2026 LY_ERR ret = LY_SUCCESS;
2027 struct lysc_deviation *dev = NULL;
2028 struct lyxp_expr *exp = NULL;
2029 struct lysp_deviation **new_dev;
2030 const struct lys_module *mod;
2031 const struct lysp_module **new_dev_pmod;
2032 uint32_t i;
2033
2034 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2035 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2036 LY_CHECK_GOTO(ret, cleanup);
2037
2038 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2039 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2040 if (mod != ctx->cur_mod) {
2041 /* deviation for another module, ignore */
2042 goto cleanup;
2043 }
2044
2045 /* try to find the node in already compiled deviations */
2046 for (i = 0; i < ctx->devs.count; ++i) {
2047 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2048 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2049 dev = ctx->devs.objs[i];
2050 break;
2051 }
2052 }
2053
2054 if (!dev) {
2055 /* allocate new compiled deviation */
2056 dev = calloc(1, sizeof *dev);
2057 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2058 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2059
2060 dev->nodeid = exp;
2061 exp = NULL;
2062 }
2063
2064 /* add new parsed deviation structure */
2065 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2066 *new_dev = dev_p;
2067 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2068 *new_dev_pmod = pmod;
2069
2070cleanup:
2071 lyxp_expr_free(ctx->ctx, exp);
2072 return ret;
2073}
2074
2075LY_ERR
2076lys_precompile_own_deviations(struct lysc_ctx *ctx)
2077{
2078 LY_ARRAY_COUNT_TYPE u, v, w;
Michal Vaskoe8220db2021-06-02 15:39:05 +02002079 struct lys_module *orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002080 const struct lys_module *dev_mod;
2081 struct lysc_deviation *dev;
2082 struct lysp_deviate *d;
2083 int not_supported;
2084 uint32_t i;
2085
2086 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2087 dev_mod = ctx->cur_mod->deviated_by[u];
2088
2089 /* compile all module deviations */
2090 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2091 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2092 }
2093
2094 /* compile all submodules deviations */
2095 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2096 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2097 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2098 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2099 }
2100 }
2101 }
2102
2103 /* set not-supported flags for all the deviations */
2104 for (i = 0; i < ctx->devs.count; ++i) {
2105 dev = ctx->devs.objs[i];
2106 not_supported = 0;
2107
2108 LY_ARRAY_FOR(dev->devs, u) {
2109 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2110 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2111 not_supported = 1;
2112 break;
2113 }
2114 }
2115 if (not_supported) {
2116 break;
2117 }
2118 }
2119 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Michal Vaskoe8220db2021-06-02 15:39:05 +02002120 orig_cur_mod = ctx->cur_mod;
2121 ctx->cur_mod = dev->dev_pmods[u]->mod;
2122 lysc_update_path(ctx, NULL, "{deviation}");
2123 lysc_update_path(ctx, NULL, dev->nodeid->expr);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002124 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002125 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
Michal Vaskoe8220db2021-06-02 15:39:05 +02002126 lysc_update_path(ctx, NULL, NULL);
2127 lysc_update_path(ctx, NULL, NULL);
2128 ctx->cur_mod = orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002129 return LY_EVALID;
2130 }
2131
2132 dev->not_supported = not_supported;
2133 }
2134
2135 return LY_SUCCESS;
2136}
2137
2138/**
2139 * @brief Add a module reference into an array, checks for duplicities.
2140 *
2141 * @param[in] ctx Compile context.
2142 * @param[in] mod Module reference to add.
2143 * @param[in,out] mod_array Module sized array to add to.
2144 * @return LY_ERR value.
2145 */
2146static LY_ERR
2147lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2148{
2149 LY_ARRAY_COUNT_TYPE u;
2150 struct lys_module **new_mod;
2151
2152 LY_ARRAY_FOR(*mod_array, u) {
2153 if ((*mod_array)[u] == mod) {
2154 /* already there */
2155 return LY_EEXIST;
2156 }
2157 }
2158
2159 /* add the new module ref */
2160 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2161 *new_mod = mod;
2162
2163 return LY_SUCCESS;
2164}
2165
Michal Vasko1ccbf542021-04-19 11:35:00 +02002166/**
2167 * @brief Check whether all modules in a set are implemented.
2168 *
2169 * @param[in] mod_set Module set to check.
2170 * @return Whether all modules are implemented or not.
2171 */
2172static ly_bool
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002173lys_precompile_mod_set_is_all_implemented(const struct ly_set *mod_set)
Michal Vasko1ccbf542021-04-19 11:35:00 +02002174{
2175 uint32_t i;
2176 const struct lys_module *mod;
2177
2178 for (i = 0; i < mod_set->count; ++i) {
2179 mod = mod_set->objs[i];
2180 if (!mod->implemented) {
2181 return 0;
2182 }
2183 }
2184
2185 return 1;
2186}
2187
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002188LY_ERR
Michal Vasko65333882021-06-10 14:12:16 +02002189lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002190{
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002191 LY_ERR ret = LY_SUCCESS, r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002192 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko65333882021-06-10 14:12:16 +02002193 struct lysc_ctx ctx = {0};
2194 struct lysp_module *mod_p;
2195 struct lys_module *m;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002196 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002197 struct lysp_node_augment *aug;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002198 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002199 struct ly_set mod_set = {0}, set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002200
Michal Vasko65333882021-06-10 14:12:16 +02002201 mod_p = mod->parsed;
2202
2203 /* prepare context */
2204 ctx.ctx = mod->ctx;
2205 ctx.cur_mod = mod;
2206 ctx.pmod = mod_p;
2207 ctx.path_len = 1;
2208 ctx.path[0] = '/';
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002209
Radek Krejci2a9fc652021-01-22 17:44:34 +01002210 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002211 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002212 lysc_update_path(&ctx, NULL, "{augment}");
2213 lysc_update_path(&ctx, NULL, aug->nodeid);
2214 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2215 lysc_update_path(&ctx, NULL, NULL);
2216 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002217 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002218
Michal Vasko1ccbf542021-04-19 11:35:00 +02002219 /* add this module into the target module augmented_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002220 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002221 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002222 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002223 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002224 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002225 }
2226
2227 LY_ARRAY_FOR(mod_p->deviations, u) {
2228 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002229 lysc_update_path(&ctx, NULL, "{deviation}");
2230 lysc_update_path(&ctx, NULL, mod_p->deviations[u].nodeid);
2231 ret = lys_nodeid_mod_check(&ctx, mod_p->deviations[u].nodeid, 1, &set, NULL, &m);
2232 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 /* add this module into the target module deviated_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002237 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002238 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002239 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2240 }
2241 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002242 }
2243
2244 /* the same for augments and deviations in submodules */
2245 LY_ARRAY_FOR(mod_p->includes, v) {
2246 submod = mod_p->includes[v].submodule;
Michal Vasko65333882021-06-10 14:12:16 +02002247 ctx.pmod = (struct lysp_module *)submod;
Michal Vaskoce3d6172021-06-08 14:59:49 +02002248
Radek Krejci2a9fc652021-01-22 17:44:34 +01002249 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko65333882021-06-10 14:12:16 +02002250 lysc_update_path(&ctx, NULL, "{augment}");
2251 lysc_update_path(&ctx, NULL, aug->nodeid);
2252 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2253 lysc_update_path(&ctx, NULL, NULL);
2254 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002255 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002256
Michal Vasko65333882021-06-10 14:12:16 +02002257 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002258 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002259 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002260 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002261 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002262 }
2263
2264 LY_ARRAY_FOR(submod->deviations, u) {
Michal Vasko65333882021-06-10 14:12:16 +02002265 lysc_update_path(&ctx, NULL, "{deviation}");
2266 lysc_update_path(&ctx, NULL, submod->deviations[u].nodeid);
2267 ret = lys_nodeid_mod_check(&ctx, submod->deviations[u].nodeid, 1, &set, NULL, &m);
2268 lysc_update_path(&ctx, NULL, NULL);
2269 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002270 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002271
Michal Vasko65333882021-06-10 14:12:16 +02002272 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002273 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002274 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2275 }
2276 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002277 }
2278 }
2279
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002280 for (i = 0; i < mod_set.count; ++i) {
2281 m = mod_set.objs[i];
Michal Vasko1ccbf542021-04-19 11:35:00 +02002282
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002283 if (m == mod) {
2284 /* will be applied normally later */
2285 continue;
2286 }
2287
2288 /* we do not actually need the target modules compiled with out amends, they just need to be implemented
2289 * not compiled yet and marked for compilation */
2290
2291 if (!m->implemented) {
2292 /* implement the target module */
2293 r = lys_implement(m, NULL, unres);
2294 if (r == LY_ERECOMPILE) {
2295 /* implement all the modules right away to save possible later recompilation */
2296 ret = r;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002297 continue;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002298 } else if (r) {
2299 /* error */
2300 ret = r;
Michal Vasko65333882021-06-10 14:12:16 +02002301 goto cleanup;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002302 }
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002303 } else if (m->compiled) {
2304 /* target module was already compiled without our amends (augment/deviation), we need to recompile it */
2305 m->to_compile = 1;
2306 ret = LY_ERECOMPILE;
2307 continue;
2308 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002309 }
2310
Michal Vasko1ccbf542021-04-19 11:35:00 +02002311cleanup:
2312 ly_set_erase(&set, NULL);
2313 ly_set_erase(&mod_set, NULL);
2314 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002315}
2316
2317void
2318lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2319{
2320 uint32_t i;
2321 LY_ARRAY_COUNT_TYPE u, count;
2322 struct lys_module *m;
2323
2324 for (i = 0; i < ctx->list.count; ++i) {
2325 m = ctx->list.objs[i];
2326
2327 if (m->augmented_by) {
2328 count = LY_ARRAY_COUNT(m->augmented_by);
2329 for (u = 0; u < count; ++u) {
2330 if (m->augmented_by[u] == mod) {
2331 /* keep the order */
2332 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002333 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u - 1) * sizeof *m->augmented_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002334 }
2335 LY_ARRAY_DECREMENT(m->augmented_by);
2336 break;
2337 }
2338 }
2339 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2340 LY_ARRAY_FREE(m->augmented_by);
2341 m->augmented_by = NULL;
2342 }
2343 }
2344
2345 if (m->deviated_by) {
2346 count = LY_ARRAY_COUNT(m->deviated_by);
2347 for (u = 0; u < count; ++u) {
2348 if (m->deviated_by[u] == mod) {
2349 /* keep the order */
2350 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002351 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u - 1) * sizeof *m->deviated_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002352 }
2353 LY_ARRAY_DECREMENT(m->deviated_by);
2354 break;
2355 }
2356 }
2357 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2358 LY_ARRAY_FREE(m->deviated_by);
2359 m->deviated_by = NULL;
2360 }
2361 }
2362 }
2363}