blob: 0cb0c841efd7f0a3c7fc8c7d6518a0ba5c7e9369 [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{
1511 if (!dev_pnode) {
1512 return;
1513 }
1514
1515 switch (dev_pnode->nodetype) {
1516 case LYS_CONTAINER:
1517 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1518 break;
1519 case LYS_LIST:
1520 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1521 break;
1522 case LYS_CHOICE:
1523 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1524 break;
1525 case LYS_CASE:
1526 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1527 break;
1528 case LYS_LEAF:
1529 case LYS_LEAFLIST:
1530 case LYS_ANYXML:
1531 case LYS_ANYDATA:
1532 /* no children */
1533 break;
1534 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001535 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001536 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001537 case LYS_RPC:
1538 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001539 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1540 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001541 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001542 case LYS_INPUT:
1543 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001544 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001545 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001546 free(dev_pnode);
1547 return;
1548 default:
1549 LOGINT(ctx);
1550 return;
1551 }
1552
1553 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1554}
1555
1556LY_ERR
1557lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1558 struct lysp_node **dev_pnode, ly_bool *not_supported)
1559{
1560 LY_ERR ret = LY_SUCCESS;
1561 uint32_t i;
1562 LY_ARRAY_COUNT_TYPE u;
1563 struct lys_module *orig_mod = ctx->cur_mod;
1564 struct lysp_module *orig_pmod = ctx->pmod;
1565 char orig_path[LYSC_CTX_BUFSIZE];
1566 struct lysc_refine *rfn;
1567 struct lysc_deviation *dev;
1568 struct lysp_deviation *dev_p;
1569 struct lysp_deviate *d;
1570
1571 *dev_pnode = NULL;
1572 *not_supported = 0;
1573
1574 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1575 rfn = ctx->uses_rfns.objs[i];
1576
Michal Vasko28fe5f62021-03-03 16:37:39 +01001577 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 +02001578 /* not our target node */
1579 continue;
1580 }
1581
1582 if (!*dev_pnode) {
1583 /* first refine on this node, create a copy first */
1584 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1585 }
1586
Michal Vaskob8df5762021-01-12 15:15:53 +01001587 /* use modules from the refine */
1588 ctx->cur_mod = rfn->nodeid_pmod->mod;
1589 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1590
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001591 /* apply all the refines by changing (the copy of) the parsed node */
1592 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001593 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001594 lysc_update_path(ctx, NULL, "{refine}");
1595 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001596
1597 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001598 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1599 lysc_update_path(ctx, NULL, NULL);
1600 lysc_update_path(ctx, NULL, NULL);
1601 LY_CHECK_GOTO(ret, cleanup);
1602 }
1603
1604 /* refine was applied, remove it */
1605 lysc_refine_free(ctx->ctx, rfn);
1606 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1607
1608 /* all the refines for one target node are in one structure, we are done */
1609 break;
1610 }
1611
1612 for (i = 0; i < ctx->devs.count; ++i) {
1613 dev = ctx->devs.objs[i];
1614
Michal Vasko28fe5f62021-03-03 16:37:39 +01001615 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001616 /* not our target node */
1617 continue;
1618 }
1619
1620 if (dev->not_supported) {
1621 /* it is not supported, no more deviations */
1622 *not_supported = 1;
1623 goto dev_applied;
1624 }
1625
1626 if (!*dev_pnode) {
1627 /* first deviation on this node, create a copy first */
1628 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1629 }
1630
1631 /* apply all the deviates by changing (the copy of) the parsed node */
1632 LY_ARRAY_FOR(dev->devs, u) {
1633 dev_p = dev->devs[u];
1634 LY_LIST_FOR(dev_p->deviates, d) {
1635 /* generate correct path */
1636 strcpy(orig_path, ctx->path);
1637 ctx->path_len = 1;
1638 ctx->cur_mod = dev->dev_pmods[u]->mod;
1639 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1640 lysc_update_path(ctx, NULL, "{deviation}");
1641 lysc_update_path(ctx, NULL, dev_p->nodeid);
1642
1643 switch (d->mod) {
1644 case LYS_DEV_ADD:
1645 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1646 break;
1647 case LYS_DEV_DELETE:
1648 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1649 break;
1650 case LYS_DEV_REPLACE:
1651 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1652 break;
1653 default:
1654 LOGINT(ctx->ctx);
1655 ret = LY_EINT;
1656 }
1657
1658 /* restore previous path */
1659 strcpy(ctx->path, orig_path);
1660 ctx->path_len = strlen(ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001661
1662 LY_CHECK_GOTO(ret, cleanup);
1663 }
1664 }
1665
1666dev_applied:
1667 /* deviation was applied, remove it */
1668 lysc_deviation_free(ctx->ctx, dev);
1669 ly_set_rm_index(&ctx->devs, i, NULL);
1670
1671 /* all the deviations for one target node are in one structure, we are done */
1672 break;
1673 }
1674
1675cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001676 ctx->cur_mod = orig_mod;
1677 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001678 if (ret) {
1679 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1680 *dev_pnode = NULL;
1681 *not_supported = 0;
1682 }
1683 return ret;
1684}
1685
1686/**
1687 * @brief Compile the parsed augment connecting it into its target.
1688 *
1689 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1690 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1691 * are already implemented and compiled.
1692 *
1693 * @param[in] ctx Compile context.
1694 * @param[in] aug_p Parsed augment to compile.
1695 * @param[in] target Target node of the augment.
1696 * @return LY_SUCCESS on success.
1697 * @return LY_EVALID on failure.
1698 */
1699static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001700lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001701{
1702 LY_ERR ret = LY_SUCCESS;
1703 struct lysp_node *pnode;
1704 struct lysc_node *node;
1705 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001706 struct lysc_node_action **actions;
1707 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001708 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001709 struct ly_set child_set = {0};
Michal Vasko7c565922021-06-10 14:58:27 +02001710 uint32_t i, opt_prev = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001711
1712 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001713 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001714 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1715 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1716 ret = LY_EVALID;
1717 goto cleanup;
1718 }
1719
1720 /* check for mandatory nodes
1721 * - new cases augmenting some choice can have mandatory nodes
1722 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1723 */
1724 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1725 allow_mandatory = 1;
1726 }
1727
1728 LY_LIST_FOR(aug_p->child, pnode) {
1729 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1730 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1731 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1732 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001733 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001734 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1735 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1736 ret = LY_EVALID;
1737 goto cleanup;
1738 }
1739
1740 /* compile the children */
1741 if (target->nodetype == LYS_CHOICE) {
1742 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001743 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1744 if (target->nodetype == LYS_INPUT) {
Michal Vasko7c565922021-06-10 14:58:27 +02001745 ctx->compile_opts |= LYS_COMPILE_RPC_INPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001746 } else {
Michal Vasko7c565922021-06-10 14:58:27 +02001747 ctx->compile_opts |= LYS_COMPILE_RPC_OUTPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001748 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001749 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001750 } else {
1751 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1752 }
1753
Michal Vasko29dd11e2020-11-23 16:52:22 +01001754 /* eval if-features again for the rest of this node processing */
1755 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1756 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001757 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001758 }
1759
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001760 /* since the augment node is not present in the compiled tree, we need to pass some of its
1761 * statements to all its children */
1762 for (i = 0; i < child_set.count; ++i) {
1763 node = child_set.snodes[i];
1764 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1765 node->flags &= ~LYS_MAND_TRUE;
1766 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001767 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001768 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1769 ret = LY_EVALID;
1770 goto cleanup;
1771 }
1772
1773 if (aug_p->when) {
1774 /* pass augment's when to all the children */
Michal Vasko72244882021-01-12 15:21:05 +01001775 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 +02001776 LY_CHECK_GOTO(ret, cleanup);
1777 }
1778 }
1779 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001780
1781 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001782 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001783 }
1784
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001785 actions = lysc_node_actions_p(target);
1786 notifs = lysc_node_notifs_p(target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001787
1788 if (aug_p->actions) {
1789 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001790 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001791 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001792 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001793 ret = LY_EVALID;
1794 goto cleanup;
1795 }
1796
1797 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001798 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001799 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001800
Michal Vasko012807e2021-02-03 09:52:18 +01001801 /* eval if-features again for the rest of this node processing */
1802 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1803 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001804 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001805 }
Michal Vasko012807e2021-02-03 09:52:18 +01001806
1807 /* since the augment node is not present in the compiled tree, we need to pass some of its
1808 * statements to all its children */
1809 for (i = 0; i < child_set.count; ++i) {
1810 node = child_set.snodes[i];
1811 if (aug_p->when) {
1812 /* pass augment's when to all the actions */
1813 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1814 LY_CHECK_GOTO(ret, cleanup);
1815 }
1816 }
1817 ly_set_erase(&child_set, NULL);
1818
1819 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001820 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001821 }
1822 }
1823 if (aug_p->notifs) {
1824 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001825 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001827 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001828 ret = LY_EVALID;
1829 goto cleanup;
1830 }
1831
1832 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001833 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001834 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001835
Michal Vasko012807e2021-02-03 09:52:18 +01001836 /* eval if-features again for the rest of this node processing */
1837 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1838 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001839 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001840 }
Michal Vasko012807e2021-02-03 09:52:18 +01001841
1842 /* since the augment node is not present in the compiled tree, we need to pass some of its
1843 * statements to all its children */
1844 for (i = 0; i < child_set.count; ++i) {
1845 node = child_set.snodes[i];
1846 if (aug_p->when) {
1847 /* pass augment's when to all the actions */
1848 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1849 LY_CHECK_GOTO(ret, cleanup);
1850 }
1851 }
1852 ly_set_erase(&child_set, NULL);
1853
1854 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001855 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001856 }
1857 }
1858
1859cleanup:
1860 ly_set_erase(&child_set, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02001861 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001862 return ret;
1863}
1864
1865LY_ERR
1866lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1867{
1868 LY_ERR ret = LY_SUCCESS;
1869 struct lys_module *orig_mod = ctx->cur_mod;
1870 struct lysp_module *orig_pmod = ctx->pmod;
1871 uint32_t i;
1872 char orig_path[LYSC_CTX_BUFSIZE];
1873 struct lysc_augment *aug;
1874
1875 /* uses augments */
1876 for (i = 0; i < ctx->uses_augs.count; ) {
1877 aug = ctx->uses_augs.objs[i];
1878
Michal Vasko28fe5f62021-03-03 16:37:39 +01001879 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001880 /* not our target node */
1881 ++i;
1882 continue;
1883 }
1884
Michal Vaskob8df5762021-01-12 15:15:53 +01001885 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001886 lysc_update_path(ctx, NULL, "{augment}");
1887 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001888 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001889
1890 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001891 ret = lys_compile_augment(ctx, aug->aug_p, node);
1892 lysc_update_path(ctx, NULL, NULL);
1893 lysc_update_path(ctx, NULL, NULL);
1894 LY_CHECK_GOTO(ret, cleanup);
1895
Michal Vaskoc75f2042021-06-08 14:57:03 +02001896 /* augment was applied, remove it (index and the whole set may have changed because other augments
1897 * could have been applied) */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001898 ly_set_rm(&ctx->uses_augs, aug, NULL);
1899 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001900 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001901 }
1902
1903 /* top-level augments */
1904 for (i = 0; i < ctx->augs.count; ) {
1905 aug = ctx->augs.objs[i];
1906
Michal Vasko28fe5f62021-03-03 16:37:39 +01001907 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001908 /* not our target node */
1909 ++i;
1910 continue;
1911 }
1912
Michal Vaskob8df5762021-01-12 15:15:53 +01001913 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001914 strcpy(orig_path, ctx->path);
1915 ctx->path_len = 1;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001916 ctx->cur_mod = aug->aug_pmod->mod;
1917 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001918 lysc_update_path(ctx, NULL, "{augment}");
1919 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001920
1921 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001922 ret = lys_compile_augment(ctx, aug->aug_p, node);
1923 strcpy(ctx->path, orig_path);
1924 ctx->path_len = strlen(ctx->path);
1925 LY_CHECK_GOTO(ret, cleanup);
1926
1927 /* augment was applied, remove it */
1928 ly_set_rm(&ctx->augs, aug, NULL);
1929 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001930 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001931 }
1932
1933cleanup:
1934 ctx->cur_mod = orig_mod;
1935 ctx->pmod = orig_pmod;
1936 return ret;
1937}
1938
1939/**
1940 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1941 *
1942 * @param[in] ctx Compile context.
1943 * @param[in] aug_p Parsed augment to be applied.
1944 * @param[in] pmod Both current and prefix module for @p aug_p.
1945 * @return LY_ERR value.
1946 */
1947static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001948lys_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 +02001949{
1950 LY_ERR ret = LY_SUCCESS;
1951 struct lyxp_expr *exp = NULL;
1952 struct lysc_augment *aug;
1953 const struct lys_module *mod;
1954
1955 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1956 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
1957 LY_CHECK_GOTO(ret, cleanup);
1958
1959 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
1960 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
1961 if (mod != ctx->cur_mod) {
1962 /* augment for another module, ignore */
1963 goto cleanup;
1964 }
1965
1966 /* allocate new compiled augment and store it in the set */
1967 aug = calloc(1, sizeof *aug);
1968 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1969 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
1970
1971 aug->nodeid = exp;
1972 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001973 aug->aug_pmod = pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001974 aug->aug_p = aug_p;
1975
1976cleanup:
1977 lyxp_expr_free(ctx->ctx, exp);
1978 return ret;
1979}
1980
1981LY_ERR
1982lys_precompile_own_augments(struct lysc_ctx *ctx)
1983{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001984 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001985
1986 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001987 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
1988 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001989
1990 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001991 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
1992 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001993 }
1994
1995 /* collect all submodules augments */
1996 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001997 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
1998 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 +02001999 }
2000 }
2001 }
2002
2003 return LY_SUCCESS;
2004}
2005
2006/**
2007 * @brief Prepare a deviation to be applied during data nodes compilation.
2008 *
2009 * @param[in] ctx Compile context.
2010 * @param[in] dev_p Parsed deviation to be applied.
2011 * @param[in] pmod Both current and prefix module for @p dev_p.
2012 * @return LY_ERR value.
2013 */
2014static LY_ERR
2015lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2016{
2017 LY_ERR ret = LY_SUCCESS;
2018 struct lysc_deviation *dev = NULL;
2019 struct lyxp_expr *exp = NULL;
2020 struct lysp_deviation **new_dev;
2021 const struct lys_module *mod;
2022 const struct lysp_module **new_dev_pmod;
2023 uint32_t i;
2024
2025 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2026 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2027 LY_CHECK_GOTO(ret, cleanup);
2028
2029 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2030 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2031 if (mod != ctx->cur_mod) {
2032 /* deviation for another module, ignore */
2033 goto cleanup;
2034 }
2035
2036 /* try to find the node in already compiled deviations */
2037 for (i = 0; i < ctx->devs.count; ++i) {
2038 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2039 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2040 dev = ctx->devs.objs[i];
2041 break;
2042 }
2043 }
2044
2045 if (!dev) {
2046 /* allocate new compiled deviation */
2047 dev = calloc(1, sizeof *dev);
2048 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2049 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2050
2051 dev->nodeid = exp;
2052 exp = NULL;
2053 }
2054
2055 /* add new parsed deviation structure */
2056 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2057 *new_dev = dev_p;
2058 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2059 *new_dev_pmod = pmod;
2060
2061cleanup:
2062 lyxp_expr_free(ctx->ctx, exp);
2063 return ret;
2064}
2065
2066LY_ERR
2067lys_precompile_own_deviations(struct lysc_ctx *ctx)
2068{
2069 LY_ARRAY_COUNT_TYPE u, v, w;
Michal Vaskoe8220db2021-06-02 15:39:05 +02002070 struct lys_module *orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002071 const struct lys_module *dev_mod;
2072 struct lysc_deviation *dev;
2073 struct lysp_deviate *d;
2074 int not_supported;
2075 uint32_t i;
2076
2077 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2078 dev_mod = ctx->cur_mod->deviated_by[u];
2079
2080 /* compile all module deviations */
2081 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2082 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2083 }
2084
2085 /* compile all submodules deviations */
2086 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2087 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2088 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2089 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2090 }
2091 }
2092 }
2093
2094 /* set not-supported flags for all the deviations */
2095 for (i = 0; i < ctx->devs.count; ++i) {
2096 dev = ctx->devs.objs[i];
2097 not_supported = 0;
2098
2099 LY_ARRAY_FOR(dev->devs, u) {
2100 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2101 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2102 not_supported = 1;
2103 break;
2104 }
2105 }
2106 if (not_supported) {
2107 break;
2108 }
2109 }
2110 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Michal Vaskoe8220db2021-06-02 15:39:05 +02002111 orig_cur_mod = ctx->cur_mod;
2112 ctx->cur_mod = dev->dev_pmods[u]->mod;
2113 lysc_update_path(ctx, NULL, "{deviation}");
2114 lysc_update_path(ctx, NULL, dev->nodeid->expr);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002115 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002116 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
Michal Vaskoe8220db2021-06-02 15:39:05 +02002117 lysc_update_path(ctx, NULL, NULL);
2118 lysc_update_path(ctx, NULL, NULL);
2119 ctx->cur_mod = orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002120 return LY_EVALID;
2121 }
2122
2123 dev->not_supported = not_supported;
2124 }
2125
2126 return LY_SUCCESS;
2127}
2128
2129/**
2130 * @brief Add a module reference into an array, checks for duplicities.
2131 *
2132 * @param[in] ctx Compile context.
2133 * @param[in] mod Module reference to add.
2134 * @param[in,out] mod_array Module sized array to add to.
2135 * @return LY_ERR value.
2136 */
2137static LY_ERR
2138lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2139{
2140 LY_ARRAY_COUNT_TYPE u;
2141 struct lys_module **new_mod;
2142
2143 LY_ARRAY_FOR(*mod_array, u) {
2144 if ((*mod_array)[u] == mod) {
2145 /* already there */
2146 return LY_EEXIST;
2147 }
2148 }
2149
2150 /* add the new module ref */
2151 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2152 *new_mod = mod;
2153
2154 return LY_SUCCESS;
2155}
2156
Michal Vasko1ccbf542021-04-19 11:35:00 +02002157/**
2158 * @brief Check whether all modules in a set are implemented.
2159 *
2160 * @param[in] mod_set Module set to check.
2161 * @return Whether all modules are implemented or not.
2162 */
2163static ly_bool
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002164lys_precompile_mod_set_is_all_implemented(const struct ly_set *mod_set)
Michal Vasko1ccbf542021-04-19 11:35:00 +02002165{
2166 uint32_t i;
2167 const struct lys_module *mod;
2168
2169 for (i = 0; i < mod_set->count; ++i) {
2170 mod = mod_set->objs[i];
2171 if (!mod->implemented) {
2172 return 0;
2173 }
2174 }
2175
2176 return 1;
2177}
2178
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002179LY_ERR
Michal Vasko65333882021-06-10 14:12:16 +02002180lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002181{
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002182 LY_ERR ret = LY_SUCCESS, r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002183 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko65333882021-06-10 14:12:16 +02002184 struct lysc_ctx ctx = {0};
2185 struct lysp_module *mod_p;
2186 struct lys_module *m;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002187 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002188 struct lysp_node_augment *aug;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002189 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002190 struct ly_set mod_set = {0}, set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002191
Michal Vasko65333882021-06-10 14:12:16 +02002192 mod_p = mod->parsed;
2193
2194 /* prepare context */
2195 ctx.ctx = mod->ctx;
2196 ctx.cur_mod = mod;
2197 ctx.pmod = mod_p;
2198 ctx.path_len = 1;
2199 ctx.path[0] = '/';
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002200
Radek Krejci2a9fc652021-01-22 17:44:34 +01002201 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002202 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002203 lysc_update_path(&ctx, NULL, "{augment}");
2204 lysc_update_path(&ctx, NULL, aug->nodeid);
2205 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2206 lysc_update_path(&ctx, NULL, NULL);
2207 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002208 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002209
Michal Vasko1ccbf542021-04-19 11:35:00 +02002210 /* add this module into the target module augmented_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002211 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002212 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002213 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002214 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002215 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002216 }
2217
2218 LY_ARRAY_FOR(mod_p->deviations, u) {
2219 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002220 lysc_update_path(&ctx, NULL, "{deviation}");
2221 lysc_update_path(&ctx, NULL, mod_p->deviations[u].nodeid);
2222 ret = lys_nodeid_mod_check(&ctx, mod_p->deviations[u].nodeid, 1, &set, NULL, &m);
2223 lysc_update_path(&ctx, NULL, NULL);
2224 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002225 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002226
Michal Vasko1ccbf542021-04-19 11:35:00 +02002227 /* add this module into the target module deviated_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002228 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002229 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002230 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2231 }
2232 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002233 }
2234
2235 /* the same for augments and deviations in submodules */
2236 LY_ARRAY_FOR(mod_p->includes, v) {
2237 submod = mod_p->includes[v].submodule;
Michal Vasko65333882021-06-10 14:12:16 +02002238 ctx.pmod = (struct lysp_module *)submod;
Michal Vaskoce3d6172021-06-08 14:59:49 +02002239
Radek Krejci2a9fc652021-01-22 17:44:34 +01002240 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko65333882021-06-10 14:12:16 +02002241 lysc_update_path(&ctx, NULL, "{augment}");
2242 lysc_update_path(&ctx, NULL, aug->nodeid);
2243 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2244 lysc_update_path(&ctx, NULL, NULL);
2245 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002246 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002247
Michal Vasko65333882021-06-10 14:12:16 +02002248 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002249 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002250 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002251 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002252 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002253 }
2254
2255 LY_ARRAY_FOR(submod->deviations, u) {
Michal Vasko65333882021-06-10 14:12:16 +02002256 lysc_update_path(&ctx, NULL, "{deviation}");
2257 lysc_update_path(&ctx, NULL, submod->deviations[u].nodeid);
2258 ret = lys_nodeid_mod_check(&ctx, submod->deviations[u].nodeid, 1, &set, NULL, &m);
2259 lysc_update_path(&ctx, NULL, NULL);
2260 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002261 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002262
Michal Vasko65333882021-06-10 14:12:16 +02002263 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002264 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002265 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2266 }
2267 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002268 }
2269 }
2270
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002271 for (i = 0; i < mod_set.count; ++i) {
2272 m = mod_set.objs[i];
Michal Vasko1ccbf542021-04-19 11:35:00 +02002273
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002274 if (m == mod) {
2275 /* will be applied normally later */
2276 continue;
2277 }
2278
2279 /* we do not actually need the target modules compiled with out amends, they just need to be implemented
2280 * not compiled yet and marked for compilation */
2281
2282 if (!m->implemented) {
2283 /* implement the target module */
2284 r = lys_implement(m, NULL, unres);
2285 if (r == LY_ERECOMPILE) {
2286 /* implement all the modules right away to save possible later recompilation */
2287 ret = r;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002288 continue;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002289 } else if (r) {
2290 /* error */
2291 ret = r;
Michal Vasko65333882021-06-10 14:12:16 +02002292 goto cleanup;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002293 }
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002294 } else if (m->compiled) {
2295 /* target module was already compiled without our amends (augment/deviation), we need to recompile it */
2296 m->to_compile = 1;
2297 ret = LY_ERECOMPILE;
2298 continue;
2299 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002300 }
2301
Michal Vasko1ccbf542021-04-19 11:35:00 +02002302cleanup:
2303 ly_set_erase(&set, NULL);
2304 ly_set_erase(&mod_set, NULL);
2305 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002306}
2307
2308void
2309lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2310{
2311 uint32_t i;
2312 LY_ARRAY_COUNT_TYPE u, count;
2313 struct lys_module *m;
2314
2315 for (i = 0; i < ctx->list.count; ++i) {
2316 m = ctx->list.objs[i];
2317
2318 if (m->augmented_by) {
2319 count = LY_ARRAY_COUNT(m->augmented_by);
2320 for (u = 0; u < count; ++u) {
2321 if (m->augmented_by[u] == mod) {
2322 /* keep the order */
2323 if (u < count - 1) {
2324 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2325 }
2326 LY_ARRAY_DECREMENT(m->augmented_by);
2327 break;
2328 }
2329 }
2330 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2331 LY_ARRAY_FREE(m->augmented_by);
2332 m->augmented_by = NULL;
2333 }
2334 }
2335
2336 if (m->deviated_by) {
2337 count = LY_ARRAY_COUNT(m->deviated_by);
2338 for (u = 0; u < count; ++u) {
2339 if (m->deviated_by[u] == mod) {
2340 /* keep the order */
2341 if (u < count - 1) {
2342 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2343 }
2344 LY_ARRAY_DECREMENT(m->deviated_by);
2345 break;
2346 }
2347 }
2348 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2349 LY_ARRAY_FREE(m->deviated_by);
2350 m->deviated_by = NULL;
2351 }
2352 }
2353 }
2354}