blob: 01eb98c982ef8449cadf69b7a0437d8344ba847f [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
348 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
349
350 if (orig_type->range) {
351 type->range = calloc(1, sizeof *type->range);
352 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
353 LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range));
354 }
355
356 if (orig_type->length) {
357 type->length = calloc(1, sizeof *type->length);
358 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
359 LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length));
360 }
361
362 DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup);
363 DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup);
364 DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup);
365 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done);
366 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
367 DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup);
368 DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup);
369
370 type->pmod = orig_type->pmod;
371 type->compiled = orig_type->compiled;
372
373 type->fraction_digits = orig_type->fraction_digits;
374 type->require_instance = orig_type->require_instance;
375 type->flags = orig_type->flags;
376
377done:
378 return ret;
379}
380
381static LY_ERR
382lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when)
383{
384 LY_ERR ret = LY_SUCCESS;
385
386 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
387 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
388 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
389 DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup);
390
391 return ret;
392}
393
394static LY_ERR
395lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
396{
397 LY_ERR ret = LY_SUCCESS;
398
399 node->parent = NULL;
400 node->nodetype = orig->nodetype;
401 node->flags = orig->flags;
402 node->next = NULL;
403 DUP_STRING(ctx, orig->name, node->name, ret);
404 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
405 DUP_STRING(ctx, orig->ref, node->ref, ret);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200406 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
407 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
408
409 return ret;
410}
411
Radek Krejci9a3823e2021-01-27 20:26:46 +0100412#define DUP_PWHEN(CTX, ORIG, NEW) \
413 if (ORIG) { \
414 NEW = calloc(1, sizeof *NEW); \
415 LY_CHECK_ERR_RET(!NEW, LOGMEM(CTX), LY_EMEM); \
416 LY_CHECK_RET(lysp_when_dup(CTX, NEW, ORIG)); \
417 }
418
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200419static LY_ERR
420lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
421{
422 LY_ERR ret = LY_SUCCESS;
423 struct lysp_node_container *cont;
424 const struct lysp_node_container *orig_cont;
425 struct lysp_node_leaf *leaf;
426 const struct lysp_node_leaf *orig_leaf;
427 struct lysp_node_leaflist *llist;
428 const struct lysp_node_leaflist *orig_llist;
429 struct lysp_node_list *list;
430 const struct lysp_node_list *orig_list;
431 struct lysp_node_choice *choice;
432 const struct lysp_node_choice *orig_choice;
433 struct lysp_node_case *cas;
434 const struct lysp_node_case *orig_cas;
435 struct lysp_node_anydata *any;
436 const struct lysp_node_anydata *orig_any;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100437 struct lysp_node_action *action;
438 const struct lysp_node_action *orig_action;
439 struct lysp_node_action_inout *action_inout;
440 const struct lysp_node_action_inout *orig_action_inout;
441 struct lysp_node_notif *notif;
442 const struct lysp_node_notif *orig_notif;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200443
Radek Krejci2a9fc652021-01-22 17:44:34 +0100444 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA |
445 LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200446
447 /* common part */
448 LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig));
449
450 /* specific part */
451 switch (node->nodetype) {
452 case LYS_CONTAINER:
453 cont = (struct lysp_node_container *)node;
454 orig_cont = (const struct lysp_node_container *)orig;
455
Radek Krejci9a3823e2021-01-27 20:26:46 +0100456 DUP_PWHEN(ctx, orig_cont->when, cont->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200457 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
458 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
459 /* we do not need the rest */
460 break;
461 case LYS_LEAF:
462 leaf = (struct lysp_node_leaf *)node;
463 orig_leaf = (const struct lysp_node_leaf *)orig;
464
Radek Krejci9a3823e2021-01-27 20:26:46 +0100465 DUP_PWHEN(ctx, orig_leaf->when, leaf->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200466 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
467 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
468 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
469 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
470 break;
471 case LYS_LEAFLIST:
472 llist = (struct lysp_node_leaflist *)node;
473 orig_llist = (const struct lysp_node_leaflist *)orig;
474
Radek Krejci9a3823e2021-01-27 20:26:46 +0100475 DUP_PWHEN(ctx, orig_llist->when, llist->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200476 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
477 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
478 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
479 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
480 llist->min = orig_llist->min;
481 llist->max = orig_llist->max;
482 break;
483 case LYS_LIST:
484 list = (struct lysp_node_list *)node;
485 orig_list = (const struct lysp_node_list *)orig;
486
Radek Krejci9a3823e2021-01-27 20:26:46 +0100487 DUP_PWHEN(ctx, orig_list->when, list->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200488 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
489 DUP_STRING(ctx, orig_list->key, list->key, ret);
490 /* we do not need these arrays */
491 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
492 list->min = orig_list->min;
493 list->max = orig_list->max;
494 break;
495 case LYS_CHOICE:
496 choice = (struct lysp_node_choice *)node;
497 orig_choice = (const struct lysp_node_choice *)orig;
498
Radek Krejci9a3823e2021-01-27 20:26:46 +0100499 DUP_PWHEN(ctx, orig_choice->when, choice->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200500 /* we do not need children */
501 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
502 break;
503 case LYS_CASE:
504 cas = (struct lysp_node_case *)node;
505 orig_cas = (const struct lysp_node_case *)orig;
506
Radek Krejci9a3823e2021-01-27 20:26:46 +0100507 DUP_PWHEN(ctx, orig_cas->when, cas->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200508 /* we do not need children */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200509 break;
510 case LYS_ANYDATA:
511 case LYS_ANYXML:
512 any = (struct lysp_node_anydata *)node;
513 orig_any = (const struct lysp_node_anydata *)orig;
514
Radek Krejci9a3823e2021-01-27 20:26:46 +0100515 DUP_PWHEN(ctx, orig_any->when, any->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200516 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
517 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100518 case LYS_RPC:
519 case LYS_ACTION:
520 action = (struct lysp_node_action *)node;
521 orig_action = (const struct lysp_node_action *)orig;
522
523 action->input.nodetype = orig_action->input.nodetype;
524 action->output.nodetype = orig_action->output.nodetype;
525 /* we do not need the rest */
526 break;
527 case LYS_INPUT:
528 case LYS_OUTPUT:
529 action_inout = (struct lysp_node_action_inout *)node;
530 orig_action_inout = (const struct lysp_node_action_inout *)orig;
531
532 DUP_ARRAY(ctx, orig_action_inout->musts, action_inout->musts, lysp_restr_dup);
533 /* we do not need the rest */
534 break;
535 case LYS_NOTIF:
536 notif = (struct lysp_node_notif *)node;
537 orig_notif = (const struct lysp_node_notif *)orig;
538
539 DUP_ARRAY(ctx, orig_notif->musts, notif->musts, lysp_restr_dup);
540 /* we do not need the rest */
541 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200542 default:
543 LOGINT_RET(ctx);
544 }
545
546 return ret;
547}
548
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200549/**
550 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
551 *
552 * @param[in] ctx libyang context.
553 * @param[in] pnode Node to duplicate.
554 * @param[in] with_links Whether to also copy any links (child, parent pointers).
555 * @param[out] dup_p Duplicated parsed node.
556 * @return LY_ERR value.
557 */
558static LY_ERR
559lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
560{
561 LY_ERR ret = LY_SUCCESS;
562 void *mem = NULL;
563
564 if (!pnode) {
565 *dup_p = NULL;
566 return LY_SUCCESS;
567 }
568
569 switch (pnode->nodetype) {
570 case LYS_CONTAINER:
571 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200572 break;
573 case LYS_LEAF:
574 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200575 break;
576 case LYS_LEAFLIST:
577 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200578 break;
579 case LYS_LIST:
580 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200581 break;
582 case LYS_CHOICE:
583 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200584 break;
585 case LYS_CASE:
586 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200587 break;
588 case LYS_ANYDATA:
589 case LYS_ANYXML:
590 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200591 break;
592 case LYS_INPUT:
593 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100594 mem = calloc(1, sizeof(struct lysp_node_action_inout));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200595 break;
596 case LYS_ACTION:
597 case LYS_RPC:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100598 mem = calloc(1, sizeof(struct lysp_node_action));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200599 break;
600 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100601 mem = calloc(1, sizeof(struct lysp_node_notif));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200602 break;
603 default:
604 LOGINT_RET(ctx);
605 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100606 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
607 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200608
609 if (with_links) {
610 /* copy also parent and child pointers */
611 ((struct lysp_node *)mem)->parent = pnode->parent;
612 switch (pnode->nodetype) {
613 case LYS_CONTAINER:
614 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
615 break;
616 case LYS_LIST:
617 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
618 break;
619 case LYS_CHOICE:
620 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
621 break;
622 case LYS_CASE:
623 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
624 break;
625 default:
626 break;
627 }
628 }
629
630cleanup:
631 if (ret) {
632 free(mem);
633 } else {
634 *dup_p = mem;
635 }
636 return ret;
637}
638
639#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100640 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 +0200641 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
642 ret = LY_EVALID; \
643 goto cleanup;
644
645#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
646 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100647 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 +0200648 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
649 ret = LY_EVALID; \
650 goto cleanup; \
651 }
652
653/**
654 * @brief Apply refine.
655 *
656 * @param[in] ctx Compile context.
657 * @param[in] rfn Refine to apply.
658 * @param[in,out] target Refine target.
659 * @return LY_ERR value.
660 */
661static LY_ERR
662lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
663{
664 LY_ERR ret = LY_SUCCESS;
665 LY_ARRAY_COUNT_TYPE u;
666 struct lysp_qname *qname;
667 struct lysp_restr **musts, *must;
668 uint32_t *num;
669
670 /* default value */
671 if (rfn->dflts) {
672 switch (target->nodetype) {
673 case LYS_LEAF:
674 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
675
Michal Vaskoe180ed02021-02-05 16:31:20 +0100676 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200677 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
678 break;
679 case LYS_LEAFLIST:
680 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100681 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200682 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
683 ret = LY_EVALID;
684 goto cleanup;
685 }
686
687 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
688 ((struct lysp_node_leaflist *)target)->dflts = NULL;
689 LY_ARRAY_FOR(rfn->dflts, u) {
690 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
691 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
692 }
693 break;
694 case LYS_CHOICE:
695 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
696
Michal Vaskoe180ed02021-02-05 16:31:20 +0100697 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200698 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
699 break;
700 default:
701 AMEND_WRONG_NODETYPE("refine", "replace", "default");
702 }
703 }
704
705 /* description */
706 if (rfn->dsc) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100707 lydict_remove(ctx->ctx, target->dsc);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200708 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
709 }
710
711 /* reference */
712 if (rfn->ref) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100713 lydict_remove(ctx->ctx, target->ref);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200714 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
715 }
716
717 /* config */
718 if (rfn->flags & LYS_CONFIG_MASK) {
Michal Vasko7c565922021-06-10 14:58:27 +0200719 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200720 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vasko7c565922021-06-10 14:58:27 +0200721 (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT)) ? "RPC/action" :
722 ctx->compile_opts & LYS_IS_NOTIF ? "notification" : "a subtree ignoring config", ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200723 } else {
724 target->flags &= ~LYS_CONFIG_MASK;
725 target->flags |= rfn->flags & LYS_CONFIG_MASK;
726 }
727 }
728
729 /* mandatory */
730 if (rfn->flags & LYS_MAND_MASK) {
731 switch (target->nodetype) {
732 case LYS_LEAF:
733 case LYS_CHOICE:
734 case LYS_ANYDATA:
735 case LYS_ANYXML:
736 break;
737 default:
738 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
739 }
740
741 target->flags &= ~LYS_MAND_MASK;
742 target->flags |= rfn->flags & LYS_MAND_MASK;
743 }
744
745 /* presence */
746 if (rfn->presence) {
747 switch (target->nodetype) {
748 case LYS_CONTAINER:
749 break;
750 default:
751 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
752 }
753
Michal Vaskoe180ed02021-02-05 16:31:20 +0100754 lydict_remove(ctx->ctx, ((struct lysp_node_container *)target)->presence);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200755 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
756 }
757
758 /* must */
759 if (rfn->musts) {
760 switch (target->nodetype) {
761 case LYS_CONTAINER:
762 case LYS_LIST:
763 case LYS_LEAF:
764 case LYS_LEAFLIST:
765 case LYS_ANYDATA:
766 case LYS_ANYXML:
767 musts = &((struct lysp_node_container *)target)->musts;
768 break;
769 default:
770 AMEND_WRONG_NODETYPE("refine", "add", "must");
771 }
772
773 LY_ARRAY_FOR(rfn->musts, u) {
774 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
775 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
776 }
777 }
778
779 /* min-elements */
780 if (rfn->flags & LYS_SET_MIN) {
781 switch (target->nodetype) {
782 case LYS_LEAFLIST:
783 num = &((struct lysp_node_leaflist *)target)->min;
784 break;
785 case LYS_LIST:
786 num = &((struct lysp_node_list *)target)->min;
787 break;
788 default:
789 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
790 }
791
792 *num = rfn->min;
793 }
794
795 /* max-elements */
796 if (rfn->flags & LYS_SET_MAX) {
797 switch (target->nodetype) {
798 case LYS_LEAFLIST:
799 num = &((struct lysp_node_leaflist *)target)->max;
800 break;
801 case LYS_LIST:
802 num = &((struct lysp_node_list *)target)->max;
803 break;
804 default:
805 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
806 }
807
808 *num = rfn->max;
809 }
810
811 /* if-feature */
812 if (rfn->iffeatures) {
813 switch (target->nodetype) {
814 case LYS_LEAF:
815 case LYS_LEAFLIST:
816 case LYS_LIST:
817 case LYS_CONTAINER:
818 case LYS_CHOICE:
819 case LYS_CASE:
820 case LYS_ANYDATA:
821 case LYS_ANYXML:
822 break;
823 default:
824 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
825 }
826
827 LY_ARRAY_FOR(rfn->iffeatures, u) {
828 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
829 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
830 }
831 }
832
833 /* extension */
834 /* TODO refine extensions */
835
836cleanup:
837 return ret;
838}
839
840/**
841 * @brief Apply deviate add.
842 *
843 * @param[in] ctx Compile context.
844 * @param[in] d Deviate add to apply.
845 * @param[in,out] target Deviation target.
846 * @return LY_ERR value.
847 */
848static LY_ERR
849lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
850{
851 LY_ERR ret = LY_SUCCESS;
852 LY_ARRAY_COUNT_TYPE u;
853 struct lysp_qname *qname;
854 uint32_t *num;
855 struct lysp_restr **musts, *must;
856
857#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
858 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100859 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200860 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
861 ret = LY_EVALID; \
862 goto cleanup; \
863 }
864
865 /* [units-stmt] */
866 if (d->units) {
867 switch (target->nodetype) {
868 case LYS_LEAF:
869 case LYS_LEAFLIST:
870 break;
871 default:
872 AMEND_WRONG_NODETYPE("deviation", "add", "units");
873 }
874
875 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
876 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
877 }
878
879 /* *must-stmt */
880 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100881 musts = lysp_node_musts_p(target);
882 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200883 AMEND_WRONG_NODETYPE("deviation", "add", "must");
884 }
885
886 LY_ARRAY_FOR(d->musts, u) {
887 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
888 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
889 }
890 }
891
892 /* *unique-stmt */
893 if (d->uniques) {
894 switch (target->nodetype) {
895 case LYS_LIST:
896 break;
897 default:
898 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
899 }
900
901 LY_ARRAY_FOR(d->uniques, u) {
902 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
903 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
904 }
905 }
906
907 /* *default-stmt */
908 if (d->dflts) {
909 switch (target->nodetype) {
910 case LYS_LEAF:
911 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
912 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
913
914 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
915 break;
916 case LYS_LEAFLIST:
917 LY_ARRAY_FOR(d->dflts, u) {
918 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
919 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
920 }
921 break;
922 case LYS_CHOICE:
923 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
924 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
925
926 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
927 break;
928 default:
929 AMEND_WRONG_NODETYPE("deviation", "add", "default");
930 }
931 }
932
933 /* [config-stmt] */
934 if (d->flags & LYS_CONFIG_MASK) {
935 switch (target->nodetype) {
936 case LYS_CONTAINER:
937 case LYS_LEAF:
938 case LYS_LEAFLIST:
939 case LYS_LIST:
940 case LYS_CHOICE:
941 case LYS_ANYDATA:
942 case LYS_ANYXML:
943 break;
944 default:
945 AMEND_WRONG_NODETYPE("deviation", "add", "config");
946 }
947
948 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100949 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200950 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
951 target->flags & LYS_CONFIG_W ? "true" : "false");
952 ret = LY_EVALID;
953 goto cleanup;
954 }
955
956 target->flags |= d->flags & LYS_CONFIG_MASK;
957 }
958
959 /* [mandatory-stmt] */
960 if (d->flags & LYS_MAND_MASK) {
961 switch (target->nodetype) {
962 case LYS_LEAF:
963 case LYS_CHOICE:
964 case LYS_ANYDATA:
965 case LYS_ANYXML:
966 break;
967 default:
968 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
969 }
970
971 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100972 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200973 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
974 target->flags & LYS_MAND_TRUE ? "true" : "false");
975 ret = LY_EVALID;
976 goto cleanup;
977 }
978
979 target->flags |= d->flags & LYS_MAND_MASK;
980 }
981
982 /* [min-elements-stmt] */
983 if (d->flags & LYS_SET_MIN) {
984 switch (target->nodetype) {
985 case LYS_LEAFLIST:
986 num = &((struct lysp_node_leaflist *)target)->min;
987 break;
988 case LYS_LIST:
989 num = &((struct lysp_node_list *)target)->min;
990 break;
991 default:
992 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
993 }
994
995 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100996 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200997 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
998 ret = LY_EVALID;
999 goto cleanup;
1000 }
1001
1002 *num = d->min;
1003 }
1004
1005 /* [max-elements-stmt] */
1006 if (d->flags & LYS_SET_MAX) {
1007 switch (target->nodetype) {
1008 case LYS_LEAFLIST:
1009 num = &((struct lysp_node_leaflist *)target)->max;
1010 break;
1011 case LYS_LIST:
1012 num = &((struct lysp_node_list *)target)->max;
1013 break;
1014 default:
1015 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1016 }
1017
1018 if (target->flags & LYS_SET_MAX) {
1019 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001020 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001021 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1022 *num);
1023 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001024 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001025 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1026 }
1027 ret = LY_EVALID;
1028 goto cleanup;
1029 }
1030
1031 *num = d->max;
1032 }
1033
1034cleanup:
1035 return ret;
1036}
1037
1038/**
1039 * @brief Apply deviate delete.
1040 *
1041 * @param[in] ctx Compile context.
1042 * @param[in] d Deviate delete to apply.
1043 * @param[in,out] target Deviation target.
1044 * @return LY_ERR value.
1045 */
1046static LY_ERR
1047lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1048{
1049 LY_ERR ret = LY_SUCCESS;
1050 struct lysp_restr **musts;
1051 LY_ARRAY_COUNT_TYPE u, v;
1052 struct lysp_qname **uniques, **dflts;
1053
1054#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1055 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1056 int found = 0; \
1057 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1058 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1059 found = 1; \
1060 break; \
1061 } \
1062 } \
1063 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001064 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001065 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1066 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1067 ret = LY_EVALID; \
1068 goto cleanup; \
1069 } \
1070 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1071 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
Michal Vasko08e9b112021-06-11 15:41:17 +02001072 if (v < LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1073 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1074 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001075 } \
1076 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1077 LY_ARRAY_FREE(ORIG_ARRAY); \
1078 ORIG_ARRAY = NULL; \
1079 }
1080
1081#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1082 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001083 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001084 ret = LY_EVALID; \
1085 goto cleanup; \
1086 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001087 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001088 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1089 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1090 ret = LY_EVALID; \
1091 goto cleanup; \
1092 }
1093
1094 /* [units-stmt] */
1095 if (d->units) {
1096 switch (target->nodetype) {
1097 case LYS_LEAF:
1098 case LYS_LEAFLIST:
1099 break;
1100 default:
1101 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1102 }
1103
1104 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001105 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001106 ((struct lysp_node_leaf *)target)->units = NULL;
1107 }
1108
1109 /* *must-stmt */
1110 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001111 musts = lysp_node_musts_p(target);
1112 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001113 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1114 }
1115
1116 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1117 }
1118
1119 /* *unique-stmt */
1120 if (d->uniques) {
1121 switch (target->nodetype) {
1122 case LYS_LIST:
1123 break;
1124 default:
1125 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1126 }
1127
1128 uniques = &((struct lysp_node_list *)target)->uniques;
1129 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1130 }
1131
1132 /* *default-stmt */
1133 if (d->dflts) {
1134 switch (target->nodetype) {
1135 case LYS_LEAF:
1136 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1137 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1138
Michal Vaskoe180ed02021-02-05 16:31:20 +01001139 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001140 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1141 break;
1142 case LYS_LEAFLIST:
1143 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1144 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1145 break;
1146 case LYS_CHOICE:
1147 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1148 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1149
Michal Vaskoe180ed02021-02-05 16:31:20 +01001150 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001151 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1152 break;
1153 default:
1154 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1155 }
1156 }
1157
1158cleanup:
1159 return ret;
1160}
1161
1162/**
1163 * @brief Apply deviate replace.
1164 *
1165 * @param[in] ctx Compile context.
1166 * @param[in] d Deviate replace to apply.
1167 * @param[in,out] target Deviation target.
1168 * @return LY_ERR value.
1169 */
1170static LY_ERR
1171lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1172{
1173 LY_ERR ret = LY_SUCCESS;
1174 uint32_t *num;
1175
1176#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1177 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001178 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001179 ret = LY_EVALID; \
1180 goto cleanup; \
1181 }
1182
1183 /* [type-stmt] */
1184 if (d->type) {
1185 switch (target->nodetype) {
1186 case LYS_LEAF:
1187 case LYS_LEAFLIST:
1188 break;
1189 default:
1190 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1191 }
1192
1193 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1194 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1195 }
1196
1197 /* [units-stmt] */
1198 if (d->units) {
1199 switch (target->nodetype) {
1200 case LYS_LEAF:
1201 case LYS_LEAFLIST:
1202 break;
1203 default:
1204 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1205 }
1206
1207 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001208 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001209 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1210 }
1211
1212 /* [default-stmt] */
1213 if (d->dflt.str) {
1214 switch (target->nodetype) {
1215 case LYS_LEAF:
1216 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1217
Michal Vaskoe180ed02021-02-05 16:31:20 +01001218 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001219 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1220 break;
1221 case LYS_CHOICE:
1222 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1223
Michal Vaskoe180ed02021-02-05 16:31:20 +01001224 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001225 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1226 break;
1227 default:
1228 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1229 }
1230 }
1231
1232 /* [config-stmt] */
1233 if (d->flags & LYS_CONFIG_MASK) {
1234 switch (target->nodetype) {
1235 case LYS_CONTAINER:
1236 case LYS_LEAF:
1237 case LYS_LEAFLIST:
1238 case LYS_LIST:
1239 case LYS_CHOICE:
1240 case LYS_ANYDATA:
1241 case LYS_ANYXML:
1242 break;
1243 default:
1244 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1245 }
1246
1247 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001248 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1249 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001250 ret = LY_EVALID;
1251 goto cleanup;
1252 }
1253
1254 target->flags &= ~LYS_CONFIG_MASK;
1255 target->flags |= d->flags & LYS_CONFIG_MASK;
1256 }
1257
1258 /* [mandatory-stmt] */
1259 if (d->flags & LYS_MAND_MASK) {
1260 switch (target->nodetype) {
1261 case LYS_LEAF:
1262 case LYS_CHOICE:
1263 case LYS_ANYDATA:
1264 case LYS_ANYXML:
1265 break;
1266 default:
1267 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1268 }
1269
1270 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001271 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1272 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001273 ret = LY_EVALID;
1274 goto cleanup;
1275 }
1276
1277 target->flags &= ~LYS_MAND_MASK;
1278 target->flags |= d->flags & LYS_MAND_MASK;
1279 }
1280
1281 /* [min-elements-stmt] */
1282 if (d->flags & LYS_SET_MIN) {
1283 switch (target->nodetype) {
1284 case LYS_LEAFLIST:
1285 num = &((struct lysp_node_leaflist *)target)->min;
1286 break;
1287 case LYS_LIST:
1288 num = &((struct lysp_node_list *)target)->min;
1289 break;
1290 default:
1291 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1292 }
1293
1294 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001295 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001296 ret = LY_EVALID;
1297 goto cleanup;
1298 }
1299
1300 *num = d->min;
1301 }
1302
1303 /* [max-elements-stmt] */
1304 if (d->flags & LYS_SET_MAX) {
1305 switch (target->nodetype) {
1306 case LYS_LEAFLIST:
1307 num = &((struct lysp_node_leaflist *)target)->max;
1308 break;
1309 case LYS_LIST:
1310 num = &((struct lysp_node_list *)target)->max;
1311 break;
1312 default:
1313 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1314 }
1315
1316 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001317 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001318 ret = LY_EVALID;
1319 goto cleanup;
1320 }
1321
1322 *num = d->max;
1323 }
1324
1325cleanup:
1326 return ret;
1327}
1328
1329/**
1330 * @brief Get module of a single nodeid node name test.
1331 *
1332 * @param[in] ctx libyang context.
1333 * @param[in] nametest Nametest with an optional prefix.
1334 * @param[in] nametest_len Length of @p nametest.
1335 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1336 * @param[out] name Optional pointer to the name test without the prefix.
1337 * @param[out] name_len Length of @p name.
1338 * @return Resolved module.
1339 */
1340static const struct lys_module *
1341lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1342 const struct lysp_module *mod, const char **name, size_t *name_len)
1343{
1344 const struct lys_module *target_mod;
1345 const char *ptr;
1346
1347 ptr = ly_strnchr(nametest, ':', nametest_len);
1348 if (ptr) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001349 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_VALUE_SCHEMA, (void *)mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001350 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001351 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001352 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001353 (int)nametest_len, nametest, (int)(ptr - nametest), nametest, LYSP_MODULE_NAME(mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001354 return NULL;
1355 }
1356
1357 if (name) {
1358 *name = ptr + 1;
1359 *name_len = nametest_len - ((ptr - nametest) + 1);
1360 }
1361 } else {
1362 target_mod = mod->mod;
1363 if (name) {
1364 *name = nametest;
1365 *name_len = nametest_len;
1366 }
1367 }
1368
1369 return target_mod;
1370}
1371
1372/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001373 * @brief Check whether a compiled node matches a single schema nodeid name test.
1374 *
1375 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1376 * @param[in] mod Expected module.
1377 * @param[in] name Expected name.
1378 * @param[in] name_len Length of @p name.
1379 * @return Whether it is a match or not.
1380 */
1381static ly_bool
1382lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1383 size_t name_len)
1384{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001385 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001386 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001387 return 0;
1388 }
1389
1390 /* compare names */
Michal Vasko544e58a2021-01-28 14:33:41 +01001391 if (ly_strncmp((*node)->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001392 return 0;
1393 }
1394
Michal Vasko2a668712020-10-21 11:48:09 +02001395 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001396 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001397
1398 return 1;
1399}
1400
1401/**
1402 * @brief Check whether a node matches specific schema nodeid.
1403 *
1404 * @param[in] exp Parsed nodeid to match.
1405 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1406 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1407 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1408 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1409 * @param[in] pnode_mod Compiled @p pnode to-be module.
1410 * @return Whether it is a match or not.
1411 */
1412static ly_bool
1413lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1414 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1415{
1416 uint32_t i;
1417 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001418 const char *name = NULL;
1419 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001420
1421 /* compare last node in the node ID */
1422 i = exp->used - 1;
1423
1424 /* get exp node ID module */
1425 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);
1426 assert(mod);
1427
1428 if (pnode) {
1429 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001430 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001431 return 0;
1432 }
1433 } else {
1434 /* using parent directly */
1435 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1436 return 0;
1437 }
1438 }
1439
1440 /* now compare all the compiled parents */
1441 while (i > 1) {
1442 i -= 2;
1443 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1444
1445 if (!parent) {
1446 /* no more parents but path continues */
1447 return 0;
1448 }
1449
1450 /* get exp node ID module */
1451 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1452 &name_len);
1453 assert(mod);
1454
1455 /* compare with the parent */
1456 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1457 return 0;
1458 }
1459 }
1460
1461 if (ctx_node && (ctx_node != parent)) {
1462 /* descendant path has not finished in the context node */
1463 return 0;
1464 } else if (!ctx_node && parent) {
1465 /* some parent was not matched */
1466 return 0;
1467 }
1468
1469 return 1;
1470}
1471
1472void
1473lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1474{
1475 if (aug) {
1476 lyxp_expr_free(ctx, aug->nodeid);
1477
1478 free(aug);
1479 }
1480}
1481
1482void
1483lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1484{
1485 if (dev) {
1486 lyxp_expr_free(ctx, dev->nodeid);
1487 LY_ARRAY_FREE(dev->devs);
1488 LY_ARRAY_FREE(dev->dev_pmods);
1489
1490 free(dev);
1491 }
1492}
1493
1494void
1495lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1496{
1497 if (rfn) {
1498 lyxp_expr_free(ctx, rfn->nodeid);
1499 LY_ARRAY_FREE(rfn->rfns);
1500
1501 free(rfn);
1502 }
1503}
1504
1505void
1506lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1507{
1508 if (!dev_pnode) {
1509 return;
1510 }
1511
1512 switch (dev_pnode->nodetype) {
1513 case LYS_CONTAINER:
1514 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1515 break;
1516 case LYS_LIST:
1517 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1518 break;
1519 case LYS_CHOICE:
1520 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1521 break;
1522 case LYS_CASE:
1523 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1524 break;
1525 case LYS_LEAF:
1526 case LYS_LEAFLIST:
1527 case LYS_ANYXML:
1528 case LYS_ANYDATA:
1529 /* no children */
1530 break;
1531 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001532 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001533 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001534 case LYS_RPC:
1535 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001536 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1537 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001538 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001539 case LYS_INPUT:
1540 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001541 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001542 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001543 free(dev_pnode);
1544 return;
1545 default:
1546 LOGINT(ctx);
1547 return;
1548 }
1549
1550 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1551}
1552
1553LY_ERR
1554lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1555 struct lysp_node **dev_pnode, ly_bool *not_supported)
1556{
1557 LY_ERR ret = LY_SUCCESS;
1558 uint32_t i;
1559 LY_ARRAY_COUNT_TYPE u;
1560 struct lys_module *orig_mod = ctx->cur_mod;
1561 struct lysp_module *orig_pmod = ctx->pmod;
1562 char orig_path[LYSC_CTX_BUFSIZE];
1563 struct lysc_refine *rfn;
1564 struct lysc_deviation *dev;
1565 struct lysp_deviation *dev_p;
1566 struct lysp_deviate *d;
1567
1568 *dev_pnode = NULL;
1569 *not_supported = 0;
1570
1571 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1572 rfn = ctx->uses_rfns.objs[i];
1573
Michal Vasko28fe5f62021-03-03 16:37:39 +01001574 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 +02001575 /* not our target node */
1576 continue;
1577 }
1578
1579 if (!*dev_pnode) {
1580 /* first refine on this node, create a copy first */
1581 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1582 }
1583
Michal Vaskob8df5762021-01-12 15:15:53 +01001584 /* use modules from the refine */
1585 ctx->cur_mod = rfn->nodeid_pmod->mod;
1586 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1587
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001588 /* apply all the refines by changing (the copy of) the parsed node */
1589 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001590 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001591 lysc_update_path(ctx, NULL, "{refine}");
1592 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001593
1594 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001595 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1596 lysc_update_path(ctx, NULL, NULL);
1597 lysc_update_path(ctx, NULL, NULL);
1598 LY_CHECK_GOTO(ret, cleanup);
1599 }
1600
1601 /* refine was applied, remove it */
1602 lysc_refine_free(ctx->ctx, rfn);
1603 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1604
1605 /* all the refines for one target node are in one structure, we are done */
1606 break;
1607 }
1608
1609 for (i = 0; i < ctx->devs.count; ++i) {
1610 dev = ctx->devs.objs[i];
1611
Michal Vasko28fe5f62021-03-03 16:37:39 +01001612 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001613 /* not our target node */
1614 continue;
1615 }
1616
1617 if (dev->not_supported) {
1618 /* it is not supported, no more deviations */
1619 *not_supported = 1;
1620 goto dev_applied;
1621 }
1622
1623 if (!*dev_pnode) {
1624 /* first deviation on this node, create a copy first */
1625 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1626 }
1627
1628 /* apply all the deviates by changing (the copy of) the parsed node */
1629 LY_ARRAY_FOR(dev->devs, u) {
1630 dev_p = dev->devs[u];
1631 LY_LIST_FOR(dev_p->deviates, d) {
1632 /* generate correct path */
1633 strcpy(orig_path, ctx->path);
1634 ctx->path_len = 1;
1635 ctx->cur_mod = dev->dev_pmods[u]->mod;
1636 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1637 lysc_update_path(ctx, NULL, "{deviation}");
1638 lysc_update_path(ctx, NULL, dev_p->nodeid);
1639
1640 switch (d->mod) {
1641 case LYS_DEV_ADD:
1642 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1643 break;
1644 case LYS_DEV_DELETE:
1645 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1646 break;
1647 case LYS_DEV_REPLACE:
1648 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1649 break;
1650 default:
1651 LOGINT(ctx->ctx);
1652 ret = LY_EINT;
1653 }
1654
1655 /* restore previous path */
1656 strcpy(ctx->path, orig_path);
1657 ctx->path_len = strlen(ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001658
1659 LY_CHECK_GOTO(ret, cleanup);
1660 }
1661 }
1662
1663dev_applied:
1664 /* deviation was applied, remove it */
1665 lysc_deviation_free(ctx->ctx, dev);
1666 ly_set_rm_index(&ctx->devs, i, NULL);
1667
1668 /* all the deviations for one target node are in one structure, we are done */
1669 break;
1670 }
1671
1672cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001673 ctx->cur_mod = orig_mod;
1674 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001675 if (ret) {
1676 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1677 *dev_pnode = NULL;
1678 *not_supported = 0;
1679 }
1680 return ret;
1681}
1682
1683/**
1684 * @brief Compile the parsed augment connecting it into its target.
1685 *
1686 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1687 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1688 * are already implemented and compiled.
1689 *
1690 * @param[in] ctx Compile context.
1691 * @param[in] aug_p Parsed augment to compile.
1692 * @param[in] target Target node of the augment.
1693 * @return LY_SUCCESS on success.
1694 * @return LY_EVALID on failure.
1695 */
1696static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001697lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001698{
1699 LY_ERR ret = LY_SUCCESS;
1700 struct lysp_node *pnode;
1701 struct lysc_node *node;
1702 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001703 struct lysc_node_action **actions;
1704 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001705 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001706 struct ly_set child_set = {0};
Michal Vasko7c565922021-06-10 14:58:27 +02001707 uint32_t i, opt_prev = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001708
1709 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001710 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001711 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1712 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1713 ret = LY_EVALID;
1714 goto cleanup;
1715 }
1716
1717 /* check for mandatory nodes
1718 * - new cases augmenting some choice can have mandatory nodes
1719 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1720 */
1721 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1722 allow_mandatory = 1;
1723 }
1724
1725 LY_LIST_FOR(aug_p->child, pnode) {
1726 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1727 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1728 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1729 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001730 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001731 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1732 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1733 ret = LY_EVALID;
1734 goto cleanup;
1735 }
1736
1737 /* compile the children */
1738 if (target->nodetype == LYS_CHOICE) {
1739 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001740 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1741 if (target->nodetype == LYS_INPUT) {
Michal Vasko7c565922021-06-10 14:58:27 +02001742 ctx->compile_opts |= LYS_COMPILE_RPC_INPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001743 } else {
Michal Vasko7c565922021-06-10 14:58:27 +02001744 ctx->compile_opts |= LYS_COMPILE_RPC_OUTPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001745 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001746 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001747 } else {
1748 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1749 }
1750
Michal Vasko29dd11e2020-11-23 16:52:22 +01001751 /* eval if-features again for the rest of this node processing */
1752 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1753 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001754 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001755 }
1756
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001757 /* since the augment node is not present in the compiled tree, we need to pass some of its
1758 * statements to all its children */
1759 for (i = 0; i < child_set.count; ++i) {
1760 node = child_set.snodes[i];
1761 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1762 node->flags &= ~LYS_MAND_TRUE;
1763 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001764 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001765 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1766 ret = LY_EVALID;
1767 goto cleanup;
1768 }
1769
1770 if (aug_p->when) {
1771 /* pass augment's when to all the children */
Michal Vasko72244882021-01-12 15:21:05 +01001772 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001773 LY_CHECK_GOTO(ret, cleanup);
1774 }
1775 }
1776 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001777
1778 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001779 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001780 }
1781
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001782 actions = lysc_node_actions_p(target);
1783 notifs = lysc_node_notifs_p(target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001784
1785 if (aug_p->actions) {
1786 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001787 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001788 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001789 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001790 ret = LY_EVALID;
1791 goto cleanup;
1792 }
1793
1794 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001795 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001796 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001797
Michal Vasko012807e2021-02-03 09:52:18 +01001798 /* eval if-features again for the rest of this node processing */
1799 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1800 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001801 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001802 }
Michal Vasko012807e2021-02-03 09:52:18 +01001803
1804 /* since the augment node is not present in the compiled tree, we need to pass some of its
1805 * statements to all its children */
1806 for (i = 0; i < child_set.count; ++i) {
1807 node = child_set.snodes[i];
1808 if (aug_p->when) {
1809 /* pass augment's when to all the actions */
1810 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1811 LY_CHECK_GOTO(ret, cleanup);
1812 }
1813 }
1814 ly_set_erase(&child_set, NULL);
1815
1816 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001817 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001818 }
1819 }
1820 if (aug_p->notifs) {
1821 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001822 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001823 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001824 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001825 ret = LY_EVALID;
1826 goto cleanup;
1827 }
1828
1829 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001830 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001831 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001832
Michal Vasko012807e2021-02-03 09:52:18 +01001833 /* eval if-features again for the rest of this node processing */
1834 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1835 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001836 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001837 }
Michal Vasko012807e2021-02-03 09:52:18 +01001838
1839 /* since the augment node is not present in the compiled tree, we need to pass some of its
1840 * statements to all its children */
1841 for (i = 0; i < child_set.count; ++i) {
1842 node = child_set.snodes[i];
1843 if (aug_p->when) {
1844 /* pass augment's when to all the actions */
1845 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1846 LY_CHECK_GOTO(ret, cleanup);
1847 }
1848 }
1849 ly_set_erase(&child_set, NULL);
1850
1851 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001852 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001853 }
1854 }
1855
1856cleanup:
1857 ly_set_erase(&child_set, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02001858 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001859 return ret;
1860}
1861
1862LY_ERR
1863lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1864{
1865 LY_ERR ret = LY_SUCCESS;
1866 struct lys_module *orig_mod = ctx->cur_mod;
1867 struct lysp_module *orig_pmod = ctx->pmod;
1868 uint32_t i;
1869 char orig_path[LYSC_CTX_BUFSIZE];
1870 struct lysc_augment *aug;
1871
1872 /* uses augments */
1873 for (i = 0; i < ctx->uses_augs.count; ) {
1874 aug = ctx->uses_augs.objs[i];
1875
Michal Vasko28fe5f62021-03-03 16:37:39 +01001876 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001877 /* not our target node */
1878 ++i;
1879 continue;
1880 }
1881
Michal Vaskob8df5762021-01-12 15:15:53 +01001882 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001883 lysc_update_path(ctx, NULL, "{augment}");
1884 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001885 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001886
1887 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001888 ret = lys_compile_augment(ctx, aug->aug_p, node);
1889 lysc_update_path(ctx, NULL, NULL);
1890 lysc_update_path(ctx, NULL, NULL);
1891 LY_CHECK_GOTO(ret, cleanup);
1892
Michal Vaskoc75f2042021-06-08 14:57:03 +02001893 /* augment was applied, remove it (index and the whole set may have changed because other augments
1894 * could have been applied) */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001895 ly_set_rm(&ctx->uses_augs, aug, NULL);
1896 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001897 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001898 }
1899
1900 /* top-level augments */
1901 for (i = 0; i < ctx->augs.count; ) {
1902 aug = ctx->augs.objs[i];
1903
Michal Vasko28fe5f62021-03-03 16:37:39 +01001904 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001905 /* not our target node */
1906 ++i;
1907 continue;
1908 }
1909
Michal Vaskob8df5762021-01-12 15:15:53 +01001910 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001911 strcpy(orig_path, ctx->path);
1912 ctx->path_len = 1;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001913 ctx->cur_mod = aug->aug_pmod->mod;
1914 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001915 lysc_update_path(ctx, NULL, "{augment}");
1916 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001917
1918 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001919 ret = lys_compile_augment(ctx, aug->aug_p, node);
1920 strcpy(ctx->path, orig_path);
1921 ctx->path_len = strlen(ctx->path);
1922 LY_CHECK_GOTO(ret, cleanup);
1923
1924 /* augment was applied, remove it */
1925 ly_set_rm(&ctx->augs, aug, NULL);
1926 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001927 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001928 }
1929
1930cleanup:
1931 ctx->cur_mod = orig_mod;
1932 ctx->pmod = orig_pmod;
1933 return ret;
1934}
1935
1936/**
1937 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1938 *
1939 * @param[in] ctx Compile context.
1940 * @param[in] aug_p Parsed augment to be applied.
1941 * @param[in] pmod Both current and prefix module for @p aug_p.
1942 * @return LY_ERR value.
1943 */
1944static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001945lys_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 +02001946{
1947 LY_ERR ret = LY_SUCCESS;
1948 struct lyxp_expr *exp = NULL;
1949 struct lysc_augment *aug;
1950 const struct lys_module *mod;
1951
1952 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1953 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
1954 LY_CHECK_GOTO(ret, cleanup);
1955
1956 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
1957 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
1958 if (mod != ctx->cur_mod) {
1959 /* augment for another module, ignore */
1960 goto cleanup;
1961 }
1962
1963 /* allocate new compiled augment and store it in the set */
1964 aug = calloc(1, sizeof *aug);
1965 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1966 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
1967
1968 aug->nodeid = exp;
1969 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001970 aug->aug_pmod = pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001971 aug->aug_p = aug_p;
1972
1973cleanup:
1974 lyxp_expr_free(ctx->ctx, exp);
1975 return ret;
1976}
1977
1978LY_ERR
1979lys_precompile_own_augments(struct lysc_ctx *ctx)
1980{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001981 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001982
1983 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001984 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
1985 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001986
1987 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001988 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
1989 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001990 }
1991
1992 /* collect all submodules augments */
1993 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001994 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
1995 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 +02001996 }
1997 }
1998 }
1999
2000 return LY_SUCCESS;
2001}
2002
2003/**
2004 * @brief Prepare a deviation to be applied during data nodes compilation.
2005 *
2006 * @param[in] ctx Compile context.
2007 * @param[in] dev_p Parsed deviation to be applied.
2008 * @param[in] pmod Both current and prefix module for @p dev_p.
2009 * @return LY_ERR value.
2010 */
2011static LY_ERR
2012lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2013{
2014 LY_ERR ret = LY_SUCCESS;
2015 struct lysc_deviation *dev = NULL;
2016 struct lyxp_expr *exp = NULL;
2017 struct lysp_deviation **new_dev;
2018 const struct lys_module *mod;
2019 const struct lysp_module **new_dev_pmod;
2020 uint32_t i;
2021
2022 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2023 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2024 LY_CHECK_GOTO(ret, cleanup);
2025
2026 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2027 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2028 if (mod != ctx->cur_mod) {
2029 /* deviation for another module, ignore */
2030 goto cleanup;
2031 }
2032
2033 /* try to find the node in already compiled deviations */
2034 for (i = 0; i < ctx->devs.count; ++i) {
2035 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2036 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2037 dev = ctx->devs.objs[i];
2038 break;
2039 }
2040 }
2041
2042 if (!dev) {
2043 /* allocate new compiled deviation */
2044 dev = calloc(1, sizeof *dev);
2045 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2046 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2047
2048 dev->nodeid = exp;
2049 exp = NULL;
2050 }
2051
2052 /* add new parsed deviation structure */
2053 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2054 *new_dev = dev_p;
2055 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2056 *new_dev_pmod = pmod;
2057
2058cleanup:
2059 lyxp_expr_free(ctx->ctx, exp);
2060 return ret;
2061}
2062
2063LY_ERR
2064lys_precompile_own_deviations(struct lysc_ctx *ctx)
2065{
2066 LY_ARRAY_COUNT_TYPE u, v, w;
Michal Vaskoe8220db2021-06-02 15:39:05 +02002067 struct lys_module *orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002068 const struct lys_module *dev_mod;
2069 struct lysc_deviation *dev;
2070 struct lysp_deviate *d;
2071 int not_supported;
2072 uint32_t i;
2073
2074 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2075 dev_mod = ctx->cur_mod->deviated_by[u];
2076
2077 /* compile all module deviations */
2078 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2079 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2080 }
2081
2082 /* compile all submodules deviations */
2083 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2084 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2085 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2086 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2087 }
2088 }
2089 }
2090
2091 /* set not-supported flags for all the deviations */
2092 for (i = 0; i < ctx->devs.count; ++i) {
2093 dev = ctx->devs.objs[i];
2094 not_supported = 0;
2095
2096 LY_ARRAY_FOR(dev->devs, u) {
2097 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2098 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2099 not_supported = 1;
2100 break;
2101 }
2102 }
2103 if (not_supported) {
2104 break;
2105 }
2106 }
2107 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Michal Vaskoe8220db2021-06-02 15:39:05 +02002108 orig_cur_mod = ctx->cur_mod;
2109 ctx->cur_mod = dev->dev_pmods[u]->mod;
2110 lysc_update_path(ctx, NULL, "{deviation}");
2111 lysc_update_path(ctx, NULL, dev->nodeid->expr);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002112 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002113 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
Michal Vaskoe8220db2021-06-02 15:39:05 +02002114 lysc_update_path(ctx, NULL, NULL);
2115 lysc_update_path(ctx, NULL, NULL);
2116 ctx->cur_mod = orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002117 return LY_EVALID;
2118 }
2119
2120 dev->not_supported = not_supported;
2121 }
2122
2123 return LY_SUCCESS;
2124}
2125
2126/**
2127 * @brief Add a module reference into an array, checks for duplicities.
2128 *
2129 * @param[in] ctx Compile context.
2130 * @param[in] mod Module reference to add.
2131 * @param[in,out] mod_array Module sized array to add to.
2132 * @return LY_ERR value.
2133 */
2134static LY_ERR
2135lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2136{
2137 LY_ARRAY_COUNT_TYPE u;
2138 struct lys_module **new_mod;
2139
2140 LY_ARRAY_FOR(*mod_array, u) {
2141 if ((*mod_array)[u] == mod) {
2142 /* already there */
2143 return LY_EEXIST;
2144 }
2145 }
2146
2147 /* add the new module ref */
2148 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2149 *new_mod = mod;
2150
2151 return LY_SUCCESS;
2152}
2153
Michal Vasko1ccbf542021-04-19 11:35:00 +02002154/**
2155 * @brief Check whether all modules in a set are implemented.
2156 *
2157 * @param[in] mod_set Module set to check.
2158 * @return Whether all modules are implemented or not.
2159 */
2160static ly_bool
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002161lys_precompile_mod_set_is_all_implemented(const struct ly_set *mod_set)
Michal Vasko1ccbf542021-04-19 11:35:00 +02002162{
2163 uint32_t i;
2164 const struct lys_module *mod;
2165
2166 for (i = 0; i < mod_set->count; ++i) {
2167 mod = mod_set->objs[i];
2168 if (!mod->implemented) {
2169 return 0;
2170 }
2171 }
2172
2173 return 1;
2174}
2175
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002176LY_ERR
Michal Vasko65333882021-06-10 14:12:16 +02002177lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002178{
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002179 LY_ERR ret = LY_SUCCESS, r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002180 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko65333882021-06-10 14:12:16 +02002181 struct lysc_ctx ctx = {0};
2182 struct lysp_module *mod_p;
2183 struct lys_module *m;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002184 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002185 struct lysp_node_augment *aug;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002186 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002187 struct ly_set mod_set = {0}, set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002188
Michal Vasko65333882021-06-10 14:12:16 +02002189 mod_p = mod->parsed;
2190
2191 /* prepare context */
2192 ctx.ctx = mod->ctx;
2193 ctx.cur_mod = mod;
2194 ctx.pmod = mod_p;
2195 ctx.path_len = 1;
2196 ctx.path[0] = '/';
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002197
Radek Krejci2a9fc652021-01-22 17:44:34 +01002198 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002199 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002200 lysc_update_path(&ctx, NULL, "{augment}");
2201 lysc_update_path(&ctx, NULL, aug->nodeid);
2202 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2203 lysc_update_path(&ctx, NULL, NULL);
2204 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002205 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002206
Michal Vasko1ccbf542021-04-19 11:35:00 +02002207 /* add this module into the target module augmented_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002208 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002209 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002210 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002211 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002212 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002213 }
2214
2215 LY_ARRAY_FOR(mod_p->deviations, u) {
2216 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002217 lysc_update_path(&ctx, NULL, "{deviation}");
2218 lysc_update_path(&ctx, NULL, mod_p->deviations[u].nodeid);
2219 ret = lys_nodeid_mod_check(&ctx, mod_p->deviations[u].nodeid, 1, &set, NULL, &m);
2220 lysc_update_path(&ctx, NULL, NULL);
2221 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002222 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002223
Michal Vasko1ccbf542021-04-19 11:35:00 +02002224 /* add this module into the target module deviated_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002225 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002226 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002227 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2228 }
2229 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002230 }
2231
2232 /* the same for augments and deviations in submodules */
2233 LY_ARRAY_FOR(mod_p->includes, v) {
2234 submod = mod_p->includes[v].submodule;
Michal Vasko65333882021-06-10 14:12:16 +02002235 ctx.pmod = (struct lysp_module *)submod;
Michal Vaskoce3d6172021-06-08 14:59:49 +02002236
Radek Krejci2a9fc652021-01-22 17:44:34 +01002237 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko65333882021-06-10 14:12:16 +02002238 lysc_update_path(&ctx, NULL, "{augment}");
2239 lysc_update_path(&ctx, NULL, aug->nodeid);
2240 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2241 lysc_update_path(&ctx, NULL, NULL);
2242 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002243 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002244
Michal Vasko65333882021-06-10 14:12:16 +02002245 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002246 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002247 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002248 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002249 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002250 }
2251
2252 LY_ARRAY_FOR(submod->deviations, u) {
Michal Vasko65333882021-06-10 14:12:16 +02002253 lysc_update_path(&ctx, NULL, "{deviation}");
2254 lysc_update_path(&ctx, NULL, submod->deviations[u].nodeid);
2255 ret = lys_nodeid_mod_check(&ctx, submod->deviations[u].nodeid, 1, &set, NULL, &m);
2256 lysc_update_path(&ctx, NULL, NULL);
2257 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002258 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002259
Michal Vasko65333882021-06-10 14:12:16 +02002260 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002261 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002262 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2263 }
2264 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002265 }
2266 }
2267
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002268 for (i = 0; i < mod_set.count; ++i) {
2269 m = mod_set.objs[i];
Michal Vasko1ccbf542021-04-19 11:35:00 +02002270
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002271 if (m == mod) {
2272 /* will be applied normally later */
2273 continue;
2274 }
2275
2276 /* we do not actually need the target modules compiled with out amends, they just need to be implemented
2277 * not compiled yet and marked for compilation */
2278
2279 if (!m->implemented) {
2280 /* implement the target module */
2281 r = lys_implement(m, NULL, unres);
2282 if (r == LY_ERECOMPILE) {
2283 /* implement all the modules right away to save possible later recompilation */
2284 ret = r;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002285 continue;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002286 } else if (r) {
2287 /* error */
2288 ret = r;
Michal Vasko65333882021-06-10 14:12:16 +02002289 goto cleanup;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002290 }
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002291 } else if (m->compiled) {
2292 /* target module was already compiled without our amends (augment/deviation), we need to recompile it */
2293 m->to_compile = 1;
2294 ret = LY_ERECOMPILE;
2295 continue;
2296 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002297 }
2298
Michal Vasko1ccbf542021-04-19 11:35:00 +02002299cleanup:
2300 ly_set_erase(&set, NULL);
2301 ly_set_erase(&mod_set, NULL);
2302 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002303}
2304
2305void
2306lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2307{
2308 uint32_t i;
2309 LY_ARRAY_COUNT_TYPE u, count;
2310 struct lys_module *m;
2311
2312 for (i = 0; i < ctx->list.count; ++i) {
2313 m = ctx->list.objs[i];
2314
2315 if (m->augmented_by) {
2316 count = LY_ARRAY_COUNT(m->augmented_by);
2317 for (u = 0; u < count; ++u) {
2318 if (m->augmented_by[u] == mod) {
2319 /* keep the order */
2320 if (u < count - 1) {
2321 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2322 }
2323 LY_ARRAY_DECREMENT(m->augmented_by);
2324 break;
2325 }
2326 }
2327 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2328 LY_ARRAY_FREE(m->augmented_by);
2329 m->augmented_by = NULL;
2330 }
2331 }
2332
2333 if (m->deviated_by) {
2334 count = LY_ARRAY_COUNT(m->deviated_by);
2335 for (u = 0; u < count; ++u) {
2336 if (m->deviated_by[u] == mod) {
2337 /* keep the order */
2338 if (u < count - 1) {
2339 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2340 }
2341 LY_ARRAY_DECREMENT(m->deviated_by);
2342 break;
2343 }
2344 }
2345 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2346 LY_ARRAY_FREE(m->deviated_by);
2347 m->deviated_by = NULL;
2348 }
2349 }
2350 }
2351}