blob: 2c7d40343517123e27646838d49c9eb6f33b1db2 [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) {
Michal Vaskoec8f4272021-10-27 09:14:03 +0200613 /* copy also parent, child, action, and notification pointers */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200614 ((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;
Michal Vaskoec8f4272021-10-27 09:14:03 +0200618 ((struct lysp_node_container *)mem)->actions = ((struct lysp_node_container *)pnode)->actions;
619 ((struct lysp_node_container *)mem)->notifs = ((struct lysp_node_container *)pnode)->notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200620 break;
621 case LYS_LIST:
622 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
Michal Vaskoec8f4272021-10-27 09:14:03 +0200623 ((struct lysp_node_list *)mem)->actions = ((struct lysp_node_list *)pnode)->actions;
624 ((struct lysp_node_list *)mem)->notifs = ((struct lysp_node_list *)pnode)->notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200625 break;
626 case LYS_CHOICE:
627 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
628 break;
629 case LYS_CASE:
630 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
631 break;
632 default:
633 break;
634 }
635 }
636
637cleanup:
638 if (ret) {
639 free(mem);
640 } else {
641 *dup_p = mem;
642 }
643 return ret;
644}
645
646#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100647 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 +0200648 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
649 ret = LY_EVALID; \
650 goto cleanup;
651
652#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
653 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100654 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 +0200655 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
656 ret = LY_EVALID; \
657 goto cleanup; \
658 }
659
660/**
661 * @brief Apply refine.
662 *
663 * @param[in] ctx Compile context.
664 * @param[in] rfn Refine to apply.
665 * @param[in,out] target Refine target.
666 * @return LY_ERR value.
667 */
668static LY_ERR
669lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
670{
671 LY_ERR ret = LY_SUCCESS;
672 LY_ARRAY_COUNT_TYPE u;
673 struct lysp_qname *qname;
674 struct lysp_restr **musts, *must;
675 uint32_t *num;
676
677 /* default value */
678 if (rfn->dflts) {
679 switch (target->nodetype) {
680 case LYS_LEAF:
681 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
682
Michal Vaskoe180ed02021-02-05 16:31:20 +0100683 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200684 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
685 break;
686 case LYS_LEAFLIST:
687 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100688 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200689 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
690 ret = LY_EVALID;
691 goto cleanup;
692 }
693
694 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
695 ((struct lysp_node_leaflist *)target)->dflts = NULL;
696 LY_ARRAY_FOR(rfn->dflts, u) {
697 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
698 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
699 }
700 break;
701 case LYS_CHOICE:
702 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
703
Michal Vaskoe180ed02021-02-05 16:31:20 +0100704 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200705 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
706 break;
707 default:
708 AMEND_WRONG_NODETYPE("refine", "replace", "default");
709 }
710 }
711
712 /* description */
713 if (rfn->dsc) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100714 lydict_remove(ctx->ctx, target->dsc);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200715 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
716 }
717
718 /* reference */
719 if (rfn->ref) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100720 lydict_remove(ctx->ctx, target->ref);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200721 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
722 }
723
724 /* config */
725 if (rfn->flags & LYS_CONFIG_MASK) {
Michal Vasko7c565922021-06-10 14:58:27 +0200726 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200727 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vasko7c565922021-06-10 14:58:27 +0200728 (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT)) ? "RPC/action" :
729 ctx->compile_opts & LYS_IS_NOTIF ? "notification" : "a subtree ignoring config", ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200730 } else {
731 target->flags &= ~LYS_CONFIG_MASK;
732 target->flags |= rfn->flags & LYS_CONFIG_MASK;
733 }
734 }
735
736 /* mandatory */
737 if (rfn->flags & LYS_MAND_MASK) {
738 switch (target->nodetype) {
739 case LYS_LEAF:
740 case LYS_CHOICE:
741 case LYS_ANYDATA:
742 case LYS_ANYXML:
743 break;
744 default:
745 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
746 }
747
748 target->flags &= ~LYS_MAND_MASK;
749 target->flags |= rfn->flags & LYS_MAND_MASK;
750 }
751
752 /* presence */
753 if (rfn->presence) {
754 switch (target->nodetype) {
755 case LYS_CONTAINER:
756 break;
757 default:
758 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
759 }
760
Michal Vaskoe180ed02021-02-05 16:31:20 +0100761 lydict_remove(ctx->ctx, ((struct lysp_node_container *)target)->presence);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200762 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
763 }
764
765 /* must */
766 if (rfn->musts) {
767 switch (target->nodetype) {
768 case LYS_CONTAINER:
769 case LYS_LIST:
770 case LYS_LEAF:
771 case LYS_LEAFLIST:
772 case LYS_ANYDATA:
773 case LYS_ANYXML:
774 musts = &((struct lysp_node_container *)target)->musts;
775 break;
776 default:
777 AMEND_WRONG_NODETYPE("refine", "add", "must");
778 }
779
780 LY_ARRAY_FOR(rfn->musts, u) {
781 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
782 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
783 }
784 }
785
786 /* min-elements */
787 if (rfn->flags & LYS_SET_MIN) {
788 switch (target->nodetype) {
789 case LYS_LEAFLIST:
790 num = &((struct lysp_node_leaflist *)target)->min;
791 break;
792 case LYS_LIST:
793 num = &((struct lysp_node_list *)target)->min;
794 break;
795 default:
796 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
797 }
798
799 *num = rfn->min;
800 }
801
802 /* max-elements */
803 if (rfn->flags & LYS_SET_MAX) {
804 switch (target->nodetype) {
805 case LYS_LEAFLIST:
806 num = &((struct lysp_node_leaflist *)target)->max;
807 break;
808 case LYS_LIST:
809 num = &((struct lysp_node_list *)target)->max;
810 break;
811 default:
812 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
813 }
814
815 *num = rfn->max;
816 }
817
818 /* if-feature */
819 if (rfn->iffeatures) {
820 switch (target->nodetype) {
821 case LYS_LEAF:
822 case LYS_LEAFLIST:
823 case LYS_LIST:
824 case LYS_CONTAINER:
825 case LYS_CHOICE:
826 case LYS_CASE:
827 case LYS_ANYDATA:
828 case LYS_ANYXML:
829 break;
830 default:
831 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
832 }
833
834 LY_ARRAY_FOR(rfn->iffeatures, u) {
835 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
836 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
837 }
838 }
839
840 /* extension */
841 /* TODO refine extensions */
842
843cleanup:
844 return ret;
845}
846
847/**
848 * @brief Apply deviate add.
849 *
850 * @param[in] ctx Compile context.
851 * @param[in] d Deviate add to apply.
852 * @param[in,out] target Deviation target.
853 * @return LY_ERR value.
854 */
855static LY_ERR
856lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
857{
858 LY_ERR ret = LY_SUCCESS;
859 LY_ARRAY_COUNT_TYPE u;
860 struct lysp_qname *qname;
861 uint32_t *num;
862 struct lysp_restr **musts, *must;
863
864#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
865 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100866 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200867 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
868 ret = LY_EVALID; \
869 goto cleanup; \
870 }
871
872 /* [units-stmt] */
873 if (d->units) {
874 switch (target->nodetype) {
875 case LYS_LEAF:
876 case LYS_LEAFLIST:
877 break;
878 default:
879 AMEND_WRONG_NODETYPE("deviation", "add", "units");
880 }
881
882 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
883 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
884 }
885
886 /* *must-stmt */
887 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100888 musts = lysp_node_musts_p(target);
889 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200890 AMEND_WRONG_NODETYPE("deviation", "add", "must");
891 }
892
893 LY_ARRAY_FOR(d->musts, u) {
894 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
895 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
896 }
897 }
898
899 /* *unique-stmt */
900 if (d->uniques) {
901 switch (target->nodetype) {
902 case LYS_LIST:
903 break;
904 default:
905 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
906 }
907
908 LY_ARRAY_FOR(d->uniques, u) {
909 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
910 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
911 }
912 }
913
914 /* *default-stmt */
915 if (d->dflts) {
916 switch (target->nodetype) {
917 case LYS_LEAF:
918 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
919 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
920
921 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
922 break;
923 case LYS_LEAFLIST:
924 LY_ARRAY_FOR(d->dflts, u) {
925 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
926 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
927 }
928 break;
929 case LYS_CHOICE:
930 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
931 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
932
933 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
934 break;
935 default:
936 AMEND_WRONG_NODETYPE("deviation", "add", "default");
937 }
938 }
939
940 /* [config-stmt] */
941 if (d->flags & LYS_CONFIG_MASK) {
942 switch (target->nodetype) {
943 case LYS_CONTAINER:
944 case LYS_LEAF:
945 case LYS_LEAFLIST:
946 case LYS_LIST:
947 case LYS_CHOICE:
948 case LYS_ANYDATA:
949 case LYS_ANYXML:
950 break;
951 default:
952 AMEND_WRONG_NODETYPE("deviation", "add", "config");
953 }
954
955 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100956 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200957 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
958 target->flags & LYS_CONFIG_W ? "true" : "false");
959 ret = LY_EVALID;
960 goto cleanup;
961 }
962
963 target->flags |= d->flags & LYS_CONFIG_MASK;
964 }
965
966 /* [mandatory-stmt] */
967 if (d->flags & LYS_MAND_MASK) {
968 switch (target->nodetype) {
969 case LYS_LEAF:
970 case LYS_CHOICE:
971 case LYS_ANYDATA:
972 case LYS_ANYXML:
973 break;
974 default:
975 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
976 }
977
978 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100979 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200980 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
981 target->flags & LYS_MAND_TRUE ? "true" : "false");
982 ret = LY_EVALID;
983 goto cleanup;
984 }
985
986 target->flags |= d->flags & LYS_MAND_MASK;
987 }
988
989 /* [min-elements-stmt] */
990 if (d->flags & LYS_SET_MIN) {
991 switch (target->nodetype) {
992 case LYS_LEAFLIST:
993 num = &((struct lysp_node_leaflist *)target)->min;
994 break;
995 case LYS_LIST:
996 num = &((struct lysp_node_list *)target)->min;
997 break;
998 default:
999 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
1000 }
1001
1002 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001003 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001004 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
1005 ret = LY_EVALID;
1006 goto cleanup;
1007 }
1008
1009 *num = d->min;
1010 }
1011
1012 /* [max-elements-stmt] */
1013 if (d->flags & LYS_SET_MAX) {
1014 switch (target->nodetype) {
1015 case LYS_LEAFLIST:
1016 num = &((struct lysp_node_leaflist *)target)->max;
1017 break;
1018 case LYS_LIST:
1019 num = &((struct lysp_node_list *)target)->max;
1020 break;
1021 default:
1022 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1023 }
1024
1025 if (target->flags & LYS_SET_MAX) {
1026 if (*num) {
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 \"%u\").",
1029 *num);
1030 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001031 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001032 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1033 }
1034 ret = LY_EVALID;
1035 goto cleanup;
1036 }
1037
1038 *num = d->max;
1039 }
1040
1041cleanup:
1042 return ret;
1043}
1044
1045/**
1046 * @brief Apply deviate delete.
1047 *
1048 * @param[in] ctx Compile context.
1049 * @param[in] d Deviate delete to apply.
1050 * @param[in,out] target Deviation target.
1051 * @return LY_ERR value.
1052 */
1053static LY_ERR
1054lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1055{
1056 LY_ERR ret = LY_SUCCESS;
1057 struct lysp_restr **musts;
1058 LY_ARRAY_COUNT_TYPE u, v;
1059 struct lysp_qname **uniques, **dflts;
1060
1061#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1062 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1063 int found = 0; \
1064 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1065 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1066 found = 1; \
1067 break; \
1068 } \
1069 } \
1070 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001071 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001072 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1073 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1074 ret = LY_EVALID; \
1075 goto cleanup; \
1076 } \
1077 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1078 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
Michal Vasko08e9b112021-06-11 15:41:17 +02001079 if (v < LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1080 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1081 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001082 } \
1083 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1084 LY_ARRAY_FREE(ORIG_ARRAY); \
1085 ORIG_ARRAY = NULL; \
1086 }
1087
1088#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1089 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001090 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001091 ret = LY_EVALID; \
1092 goto cleanup; \
1093 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001094 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001095 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1096 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1097 ret = LY_EVALID; \
1098 goto cleanup; \
1099 }
1100
1101 /* [units-stmt] */
1102 if (d->units) {
1103 switch (target->nodetype) {
1104 case LYS_LEAF:
1105 case LYS_LEAFLIST:
1106 break;
1107 default:
1108 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1109 }
1110
1111 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001112 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001113 ((struct lysp_node_leaf *)target)->units = NULL;
1114 }
1115
1116 /* *must-stmt */
1117 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001118 musts = lysp_node_musts_p(target);
1119 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001120 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1121 }
1122
1123 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1124 }
1125
1126 /* *unique-stmt */
1127 if (d->uniques) {
1128 switch (target->nodetype) {
1129 case LYS_LIST:
1130 break;
1131 default:
1132 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1133 }
1134
1135 uniques = &((struct lysp_node_list *)target)->uniques;
1136 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1137 }
1138
1139 /* *default-stmt */
1140 if (d->dflts) {
1141 switch (target->nodetype) {
1142 case LYS_LEAF:
1143 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1144 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1145
Michal Vaskoe180ed02021-02-05 16:31:20 +01001146 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001147 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1148 break;
1149 case LYS_LEAFLIST:
1150 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1151 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1152 break;
1153 case LYS_CHOICE:
1154 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1155 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1156
Michal Vaskoe180ed02021-02-05 16:31:20 +01001157 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001158 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1159 break;
1160 default:
1161 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1162 }
1163 }
1164
1165cleanup:
1166 return ret;
1167}
1168
1169/**
1170 * @brief Apply deviate replace.
1171 *
1172 * @param[in] ctx Compile context.
1173 * @param[in] d Deviate replace to apply.
1174 * @param[in,out] target Deviation target.
1175 * @return LY_ERR value.
1176 */
1177static LY_ERR
1178lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1179{
1180 LY_ERR ret = LY_SUCCESS;
1181 uint32_t *num;
1182
1183#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1184 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001185 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001186 ret = LY_EVALID; \
1187 goto cleanup; \
1188 }
1189
1190 /* [type-stmt] */
1191 if (d->type) {
1192 switch (target->nodetype) {
1193 case LYS_LEAF:
1194 case LYS_LEAFLIST:
1195 break;
1196 default:
1197 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1198 }
1199
1200 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1201 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1202 }
1203
1204 /* [units-stmt] */
1205 if (d->units) {
1206 switch (target->nodetype) {
1207 case LYS_LEAF:
1208 case LYS_LEAFLIST:
1209 break;
1210 default:
1211 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1212 }
1213
1214 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001215 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001216 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1217 }
1218
1219 /* [default-stmt] */
1220 if (d->dflt.str) {
1221 switch (target->nodetype) {
1222 case LYS_LEAF:
1223 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1224
Michal Vaskoe180ed02021-02-05 16:31:20 +01001225 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001226 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1227 break;
1228 case LYS_CHOICE:
1229 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1230
Michal Vaskoe180ed02021-02-05 16:31:20 +01001231 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001232 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1233 break;
1234 default:
1235 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1236 }
1237 }
1238
1239 /* [config-stmt] */
1240 if (d->flags & LYS_CONFIG_MASK) {
1241 switch (target->nodetype) {
1242 case LYS_CONTAINER:
1243 case LYS_LEAF:
1244 case LYS_LEAFLIST:
1245 case LYS_LIST:
1246 case LYS_CHOICE:
1247 case LYS_ANYDATA:
1248 case LYS_ANYXML:
1249 break;
1250 default:
1251 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1252 }
1253
1254 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001255 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1256 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001257 ret = LY_EVALID;
1258 goto cleanup;
1259 }
1260
1261 target->flags &= ~LYS_CONFIG_MASK;
1262 target->flags |= d->flags & LYS_CONFIG_MASK;
1263 }
1264
1265 /* [mandatory-stmt] */
1266 if (d->flags & LYS_MAND_MASK) {
1267 switch (target->nodetype) {
1268 case LYS_LEAF:
1269 case LYS_CHOICE:
1270 case LYS_ANYDATA:
1271 case LYS_ANYXML:
1272 break;
1273 default:
1274 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1275 }
1276
1277 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001278 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1279 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001280 ret = LY_EVALID;
1281 goto cleanup;
1282 }
1283
1284 target->flags &= ~LYS_MAND_MASK;
1285 target->flags |= d->flags & LYS_MAND_MASK;
1286 }
1287
1288 /* [min-elements-stmt] */
1289 if (d->flags & LYS_SET_MIN) {
1290 switch (target->nodetype) {
1291 case LYS_LEAFLIST:
1292 num = &((struct lysp_node_leaflist *)target)->min;
1293 break;
1294 case LYS_LIST:
1295 num = &((struct lysp_node_list *)target)->min;
1296 break;
1297 default:
1298 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1299 }
1300
1301 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001302 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001303 ret = LY_EVALID;
1304 goto cleanup;
1305 }
1306
1307 *num = d->min;
1308 }
1309
1310 /* [max-elements-stmt] */
1311 if (d->flags & LYS_SET_MAX) {
1312 switch (target->nodetype) {
1313 case LYS_LEAFLIST:
1314 num = &((struct lysp_node_leaflist *)target)->max;
1315 break;
1316 case LYS_LIST:
1317 num = &((struct lysp_node_list *)target)->max;
1318 break;
1319 default:
1320 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1321 }
1322
1323 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001324 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001325 ret = LY_EVALID;
1326 goto cleanup;
1327 }
1328
1329 *num = d->max;
1330 }
1331
1332cleanup:
1333 return ret;
1334}
1335
1336/**
1337 * @brief Get module of a single nodeid node name test.
1338 *
1339 * @param[in] ctx libyang context.
1340 * @param[in] nametest Nametest with an optional prefix.
1341 * @param[in] nametest_len Length of @p nametest.
1342 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1343 * @param[out] name Optional pointer to the name test without the prefix.
1344 * @param[out] name_len Length of @p name.
1345 * @return Resolved module.
1346 */
1347static const struct lys_module *
1348lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1349 const struct lysp_module *mod, const char **name, size_t *name_len)
1350{
1351 const struct lys_module *target_mod;
1352 const char *ptr;
1353
1354 ptr = ly_strnchr(nametest, ':', nametest_len);
1355 if (ptr) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001356 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_VALUE_SCHEMA, (void *)mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001357 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001358 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001359 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001360 (int)nametest_len, nametest, (int)(ptr - nametest), nametest, LYSP_MODULE_NAME(mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001361 return NULL;
1362 }
1363
1364 if (name) {
1365 *name = ptr + 1;
1366 *name_len = nametest_len - ((ptr - nametest) + 1);
1367 }
1368 } else {
1369 target_mod = mod->mod;
1370 if (name) {
1371 *name = nametest;
1372 *name_len = nametest_len;
1373 }
1374 }
1375
1376 return target_mod;
1377}
1378
1379/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001380 * @brief Check whether a compiled node matches a single schema nodeid name test.
1381 *
1382 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1383 * @param[in] mod Expected module.
1384 * @param[in] name Expected name.
1385 * @param[in] name_len Length of @p name.
1386 * @return Whether it is a match or not.
1387 */
1388static ly_bool
1389lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1390 size_t name_len)
1391{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001392 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001393 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001394 return 0;
1395 }
1396
1397 /* compare names */
Michal Vasko544e58a2021-01-28 14:33:41 +01001398 if (ly_strncmp((*node)->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001399 return 0;
1400 }
1401
Michal Vasko2a668712020-10-21 11:48:09 +02001402 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001403 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001404
1405 return 1;
1406}
1407
1408/**
1409 * @brief Check whether a node matches specific schema nodeid.
1410 *
1411 * @param[in] exp Parsed nodeid to match.
1412 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1413 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1414 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1415 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1416 * @param[in] pnode_mod Compiled @p pnode to-be module.
1417 * @return Whether it is a match or not.
1418 */
1419static ly_bool
1420lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1421 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1422{
1423 uint32_t i;
1424 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001425 const char *name = NULL;
1426 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001427
1428 /* compare last node in the node ID */
1429 i = exp->used - 1;
1430
1431 /* get exp node ID module */
1432 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);
1433 assert(mod);
1434
1435 if (pnode) {
1436 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001437 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001438 return 0;
1439 }
1440 } else {
1441 /* using parent directly */
1442 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1443 return 0;
1444 }
1445 }
1446
1447 /* now compare all the compiled parents */
1448 while (i > 1) {
1449 i -= 2;
1450 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1451
1452 if (!parent) {
1453 /* no more parents but path continues */
1454 return 0;
1455 }
1456
1457 /* get exp node ID module */
1458 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1459 &name_len);
1460 assert(mod);
1461
1462 /* compare with the parent */
1463 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1464 return 0;
1465 }
1466 }
1467
1468 if (ctx_node && (ctx_node != parent)) {
1469 /* descendant path has not finished in the context node */
1470 return 0;
1471 } else if (!ctx_node && parent) {
1472 /* some parent was not matched */
1473 return 0;
1474 }
1475
1476 return 1;
1477}
1478
1479void
1480lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1481{
1482 if (aug) {
1483 lyxp_expr_free(ctx, aug->nodeid);
1484
1485 free(aug);
1486 }
1487}
1488
1489void
1490lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1491{
1492 if (dev) {
1493 lyxp_expr_free(ctx, dev->nodeid);
1494 LY_ARRAY_FREE(dev->devs);
1495 LY_ARRAY_FREE(dev->dev_pmods);
1496
1497 free(dev);
1498 }
1499}
1500
1501void
1502lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1503{
1504 if (rfn) {
1505 lyxp_expr_free(ctx, rfn->nodeid);
1506 LY_ARRAY_FREE(rfn->rfns);
1507
1508 free(rfn);
1509 }
1510}
1511
1512void
1513lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1514{
Michal Vasko426aa7e2021-06-21 13:30:52 +02001515 LY_ARRAY_COUNT_TYPE u;
1516
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001517 if (!dev_pnode) {
1518 return;
1519 }
1520
1521 switch (dev_pnode->nodetype) {
1522 case LYS_CONTAINER:
1523 ((struct lysp_node_container *)dev_pnode)->child = NULL;
Michal Vaskoec8f4272021-10-27 09:14:03 +02001524 ((struct lysp_node_container *)dev_pnode)->actions = NULL;
1525 ((struct lysp_node_container *)dev_pnode)->notifs = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001526 break;
1527 case LYS_LIST:
1528 ((struct lysp_node_list *)dev_pnode)->child = NULL;
Michal Vaskoec8f4272021-10-27 09:14:03 +02001529 ((struct lysp_node_list *)dev_pnode)->actions = NULL;
1530 ((struct lysp_node_list *)dev_pnode)->notifs = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001531 break;
1532 case LYS_CHOICE:
1533 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1534 break;
1535 case LYS_CASE:
1536 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1537 break;
1538 case LYS_LEAF:
1539 case LYS_LEAFLIST:
1540 case LYS_ANYXML:
1541 case LYS_ANYDATA:
1542 /* no children */
1543 break;
1544 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001545 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001546 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001547 case LYS_RPC:
1548 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001549 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1550 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001551 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001552 case LYS_INPUT:
1553 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001554 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001555 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001556 free(dev_pnode);
1557 return;
1558 default:
1559 LOGINT(ctx);
1560 return;
1561 }
1562
Michal Vasko426aa7e2021-06-21 13:30:52 +02001563 /* extension parsed tree and children were not duplicated */
1564 LY_ARRAY_FOR(dev_pnode->exts, u) {
1565 dev_pnode->exts[u].parsed = NULL;
1566 dev_pnode->exts[u].child = NULL;
1567 }
1568
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001569 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1570}
1571
1572LY_ERR
1573lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1574 struct lysp_node **dev_pnode, ly_bool *not_supported)
1575{
1576 LY_ERR ret = LY_SUCCESS;
1577 uint32_t i;
1578 LY_ARRAY_COUNT_TYPE u;
1579 struct lys_module *orig_mod = ctx->cur_mod;
1580 struct lysp_module *orig_pmod = ctx->pmod;
1581 char orig_path[LYSC_CTX_BUFSIZE];
1582 struct lysc_refine *rfn;
1583 struct lysc_deviation *dev;
1584 struct lysp_deviation *dev_p;
1585 struct lysp_deviate *d;
1586
1587 *dev_pnode = NULL;
1588 *not_supported = 0;
1589
1590 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1591 rfn = ctx->uses_rfns.objs[i];
1592
Michal Vasko28fe5f62021-03-03 16:37:39 +01001593 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 +02001594 /* not our target node */
1595 continue;
1596 }
1597
1598 if (!*dev_pnode) {
1599 /* first refine on this node, create a copy first */
1600 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1601 }
1602
Michal Vaskob8df5762021-01-12 15:15:53 +01001603 /* use modules from the refine */
1604 ctx->cur_mod = rfn->nodeid_pmod->mod;
1605 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1606
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001607 /* apply all the refines by changing (the copy of) the parsed node */
1608 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001609 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001610 lysc_update_path(ctx, NULL, "{refine}");
1611 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001612
1613 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001614 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1615 lysc_update_path(ctx, NULL, NULL);
1616 lysc_update_path(ctx, NULL, NULL);
1617 LY_CHECK_GOTO(ret, cleanup);
1618 }
1619
1620 /* refine was applied, remove it */
1621 lysc_refine_free(ctx->ctx, rfn);
1622 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1623
1624 /* all the refines for one target node are in one structure, we are done */
1625 break;
1626 }
1627
1628 for (i = 0; i < ctx->devs.count; ++i) {
1629 dev = ctx->devs.objs[i];
1630
Michal Vasko28fe5f62021-03-03 16:37:39 +01001631 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001632 /* not our target node */
1633 continue;
1634 }
1635
1636 if (dev->not_supported) {
1637 /* it is not supported, no more deviations */
1638 *not_supported = 1;
1639 goto dev_applied;
1640 }
1641
1642 if (!*dev_pnode) {
1643 /* first deviation on this node, create a copy first */
1644 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1645 }
1646
1647 /* apply all the deviates by changing (the copy of) the parsed node */
1648 LY_ARRAY_FOR(dev->devs, u) {
1649 dev_p = dev->devs[u];
1650 LY_LIST_FOR(dev_p->deviates, d) {
1651 /* generate correct path */
1652 strcpy(orig_path, ctx->path);
1653 ctx->path_len = 1;
1654 ctx->cur_mod = dev->dev_pmods[u]->mod;
1655 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1656 lysc_update_path(ctx, NULL, "{deviation}");
1657 lysc_update_path(ctx, NULL, dev_p->nodeid);
1658
1659 switch (d->mod) {
1660 case LYS_DEV_ADD:
1661 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1662 break;
1663 case LYS_DEV_DELETE:
1664 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1665 break;
1666 case LYS_DEV_REPLACE:
1667 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1668 break;
1669 default:
1670 LOGINT(ctx->ctx);
1671 ret = LY_EINT;
1672 }
1673
1674 /* restore previous path */
1675 strcpy(ctx->path, orig_path);
1676 ctx->path_len = strlen(ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001677
1678 LY_CHECK_GOTO(ret, cleanup);
1679 }
1680 }
1681
1682dev_applied:
1683 /* deviation was applied, remove it */
1684 lysc_deviation_free(ctx->ctx, dev);
1685 ly_set_rm_index(&ctx->devs, i, NULL);
1686
1687 /* all the deviations for one target node are in one structure, we are done */
1688 break;
1689 }
1690
1691cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001692 ctx->cur_mod = orig_mod;
1693 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001694 if (ret) {
1695 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1696 *dev_pnode = NULL;
1697 *not_supported = 0;
1698 }
1699 return ret;
1700}
1701
1702/**
1703 * @brief Compile the parsed augment connecting it into its target.
1704 *
1705 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1706 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1707 * are already implemented and compiled.
1708 *
1709 * @param[in] ctx Compile context.
1710 * @param[in] aug_p Parsed augment to compile.
1711 * @param[in] target Target node of the augment.
1712 * @return LY_SUCCESS on success.
1713 * @return LY_EVALID on failure.
1714 */
1715static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001716lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001717{
1718 LY_ERR ret = LY_SUCCESS;
1719 struct lysp_node *pnode;
1720 struct lysc_node *node;
1721 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001722 struct lysc_node_action **actions;
1723 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001724 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001725 struct ly_set child_set = {0};
Michal Vasko7c565922021-06-10 14:58:27 +02001726 uint32_t i, opt_prev = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001727
1728 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001729 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001730 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1731 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1732 ret = LY_EVALID;
1733 goto cleanup;
1734 }
1735
1736 /* check for mandatory nodes
1737 * - new cases augmenting some choice can have mandatory nodes
1738 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1739 */
1740 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1741 allow_mandatory = 1;
1742 }
1743
1744 LY_LIST_FOR(aug_p->child, pnode) {
1745 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1746 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1747 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1748 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001749 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001750 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1751 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1752 ret = LY_EVALID;
1753 goto cleanup;
1754 }
1755
1756 /* compile the children */
1757 if (target->nodetype == LYS_CHOICE) {
1758 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001759 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1760 if (target->nodetype == LYS_INPUT) {
Michal Vasko7c565922021-06-10 14:58:27 +02001761 ctx->compile_opts |= LYS_COMPILE_RPC_INPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001762 } else {
Michal Vasko7c565922021-06-10 14:58:27 +02001763 ctx->compile_opts |= LYS_COMPILE_RPC_OUTPUT;
Michal Vasko6fb4c042020-11-24 18:05:26 +01001764 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001765 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001766 } else {
1767 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1768 }
1769
Michal Vasko29dd11e2020-11-23 16:52:22 +01001770 /* eval if-features again for the rest of this node processing */
1771 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1772 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001773 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001774 }
1775
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001776 /* since the augment node is not present in the compiled tree, we need to pass some of its
1777 * statements to all its children */
1778 for (i = 0; i < child_set.count; ++i) {
1779 node = child_set.snodes[i];
1780 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1781 node->flags &= ~LYS_MAND_TRUE;
1782 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001783 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko23864e02021-06-24 09:23:58 +02001784 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.",
1785 node->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001786 ret = LY_EVALID;
1787 goto cleanup;
1788 }
1789
1790 if (aug_p->when) {
1791 /* pass augment's when to all the children */
Michal Vaskodfd254d2021-06-24 09:24:59 +02001792 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, lysc_data_node(target), node, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001793 LY_CHECK_GOTO(ret, cleanup);
1794 }
1795 }
1796 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001797
1798 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001799 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001800 }
1801
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001802 actions = lysc_node_actions_p(target);
1803 notifs = lysc_node_notifs_p(target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001804
1805 if (aug_p->actions) {
1806 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001807 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001808 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001809 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001810 ret = LY_EVALID;
1811 goto cleanup;
1812 }
1813
1814 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001815 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001816 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001817
Michal Vasko012807e2021-02-03 09:52:18 +01001818 /* eval if-features again for the rest of this node processing */
1819 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1820 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001821 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001822 }
Michal Vasko012807e2021-02-03 09:52:18 +01001823
1824 /* since the augment node is not present in the compiled tree, we need to pass some of its
1825 * statements to all its children */
1826 for (i = 0; i < child_set.count; ++i) {
1827 node = child_set.snodes[i];
1828 if (aug_p->when) {
1829 /* pass augment's when to all the actions */
Michal Vaskodfd254d2021-06-24 09:24:59 +02001830 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, lysc_data_node(target), node, &when_shared);
Michal Vasko012807e2021-02-03 09:52:18 +01001831 LY_CHECK_GOTO(ret, cleanup);
1832 }
1833 }
1834 ly_set_erase(&child_set, NULL);
1835
1836 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001837 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001838 }
1839 }
1840 if (aug_p->notifs) {
1841 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001842 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001843 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001844 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001845 ret = LY_EVALID;
1846 goto cleanup;
1847 }
1848
1849 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001850 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001851 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001852
Michal Vasko012807e2021-02-03 09:52:18 +01001853 /* eval if-features again for the rest of this node processing */
1854 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1855 if (!enabled) {
Michal Vasko7c565922021-06-10 14:58:27 +02001856 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001857 }
Michal Vasko012807e2021-02-03 09:52:18 +01001858
1859 /* since the augment node is not present in the compiled tree, we need to pass some of its
1860 * statements to all its children */
1861 for (i = 0; i < child_set.count; ++i) {
1862 node = child_set.snodes[i];
1863 if (aug_p->when) {
1864 /* pass augment's when to all the actions */
Michal Vaskodfd254d2021-06-24 09:24:59 +02001865 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, lysc_data_node(target), node, &when_shared);
Michal Vasko012807e2021-02-03 09:52:18 +01001866 LY_CHECK_GOTO(ret, cleanup);
1867 }
1868 }
1869 ly_set_erase(&child_set, NULL);
1870
1871 /* restore options */
Michal Vasko7c565922021-06-10 14:58:27 +02001872 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001873 }
1874 }
1875
1876cleanup:
1877 ly_set_erase(&child_set, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02001878 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001879 return ret;
1880}
1881
1882LY_ERR
1883lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1884{
1885 LY_ERR ret = LY_SUCCESS;
1886 struct lys_module *orig_mod = ctx->cur_mod;
1887 struct lysp_module *orig_pmod = ctx->pmod;
1888 uint32_t i;
1889 char orig_path[LYSC_CTX_BUFSIZE];
1890 struct lysc_augment *aug;
1891
1892 /* uses augments */
1893 for (i = 0; i < ctx->uses_augs.count; ) {
1894 aug = ctx->uses_augs.objs[i];
1895
Michal Vasko28fe5f62021-03-03 16:37:39 +01001896 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001897 /* not our target node */
1898 ++i;
1899 continue;
1900 }
1901
Michal Vaskob8df5762021-01-12 15:15:53 +01001902 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001903 lysc_update_path(ctx, NULL, "{augment}");
1904 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001905 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001906
1907 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001908 ret = lys_compile_augment(ctx, aug->aug_p, node);
1909 lysc_update_path(ctx, NULL, NULL);
1910 lysc_update_path(ctx, NULL, NULL);
1911 LY_CHECK_GOTO(ret, cleanup);
1912
Michal Vaskoc75f2042021-06-08 14:57:03 +02001913 /* augment was applied, remove it (index and the whole set may have changed because other augments
1914 * could have been applied) */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001915 ly_set_rm(&ctx->uses_augs, aug, NULL);
1916 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001917 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001918 }
1919
1920 /* top-level augments */
1921 for (i = 0; i < ctx->augs.count; ) {
1922 aug = ctx->augs.objs[i];
1923
Michal Vasko28fe5f62021-03-03 16:37:39 +01001924 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001925 /* not our target node */
1926 ++i;
1927 continue;
1928 }
1929
Michal Vaskob8df5762021-01-12 15:15:53 +01001930 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001931 strcpy(orig_path, ctx->path);
1932 ctx->path_len = 1;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001933 ctx->cur_mod = aug->aug_pmod->mod;
1934 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001935 lysc_update_path(ctx, NULL, "{augment}");
1936 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001937
1938 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001939 ret = lys_compile_augment(ctx, aug->aug_p, node);
1940 strcpy(ctx->path, orig_path);
1941 ctx->path_len = strlen(ctx->path);
1942 LY_CHECK_GOTO(ret, cleanup);
1943
1944 /* augment was applied, remove it */
1945 ly_set_rm(&ctx->augs, aug, NULL);
1946 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001947 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001948 }
1949
1950cleanup:
1951 ctx->cur_mod = orig_mod;
1952 ctx->pmod = orig_pmod;
1953 return ret;
1954}
1955
1956/**
1957 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1958 *
1959 * @param[in] ctx Compile context.
1960 * @param[in] aug_p Parsed augment to be applied.
1961 * @param[in] pmod Both current and prefix module for @p aug_p.
1962 * @return LY_ERR value.
1963 */
1964static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001965lys_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 +02001966{
1967 LY_ERR ret = LY_SUCCESS;
1968 struct lyxp_expr *exp = NULL;
1969 struct lysc_augment *aug;
1970 const struct lys_module *mod;
1971
1972 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1973 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
1974 LY_CHECK_GOTO(ret, cleanup);
1975
1976 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
1977 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
1978 if (mod != ctx->cur_mod) {
1979 /* augment for another module, ignore */
1980 goto cleanup;
1981 }
1982
1983 /* allocate new compiled augment and store it in the set */
1984 aug = calloc(1, sizeof *aug);
1985 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1986 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
1987
1988 aug->nodeid = exp;
1989 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001990 aug->aug_pmod = pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001991 aug->aug_p = aug_p;
1992
1993cleanup:
1994 lyxp_expr_free(ctx->ctx, exp);
1995 return ret;
1996}
1997
1998LY_ERR
1999lys_precompile_own_augments(struct lysc_ctx *ctx)
2000{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002001 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002002
2003 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002004 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
2005 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002006
2007 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002008 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
2009 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002010 }
2011
2012 /* collect all submodules augments */
2013 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002014 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
2015 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 +02002016 }
2017 }
2018 }
2019
2020 return LY_SUCCESS;
2021}
2022
2023/**
2024 * @brief Prepare a deviation to be applied during data nodes compilation.
2025 *
2026 * @param[in] ctx Compile context.
2027 * @param[in] dev_p Parsed deviation to be applied.
2028 * @param[in] pmod Both current and prefix module for @p dev_p.
2029 * @return LY_ERR value.
2030 */
2031static LY_ERR
2032lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2033{
2034 LY_ERR ret = LY_SUCCESS;
2035 struct lysc_deviation *dev = NULL;
2036 struct lyxp_expr *exp = NULL;
2037 struct lysp_deviation **new_dev;
2038 const struct lys_module *mod;
2039 const struct lysp_module **new_dev_pmod;
2040 uint32_t i;
2041
2042 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2043 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2044 LY_CHECK_GOTO(ret, cleanup);
2045
2046 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2047 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2048 if (mod != ctx->cur_mod) {
2049 /* deviation for another module, ignore */
2050 goto cleanup;
2051 }
2052
2053 /* try to find the node in already compiled deviations */
2054 for (i = 0; i < ctx->devs.count; ++i) {
2055 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2056 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2057 dev = ctx->devs.objs[i];
2058 break;
2059 }
2060 }
2061
2062 if (!dev) {
2063 /* allocate new compiled deviation */
2064 dev = calloc(1, sizeof *dev);
2065 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2066 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2067
2068 dev->nodeid = exp;
2069 exp = NULL;
2070 }
2071
2072 /* add new parsed deviation structure */
2073 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2074 *new_dev = dev_p;
2075 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2076 *new_dev_pmod = pmod;
2077
2078cleanup:
2079 lyxp_expr_free(ctx->ctx, exp);
2080 return ret;
2081}
2082
2083LY_ERR
2084lys_precompile_own_deviations(struct lysc_ctx *ctx)
2085{
2086 LY_ARRAY_COUNT_TYPE u, v, w;
Michal Vaskoe8220db2021-06-02 15:39:05 +02002087 struct lys_module *orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002088 const struct lys_module *dev_mod;
2089 struct lysc_deviation *dev;
2090 struct lysp_deviate *d;
2091 int not_supported;
2092 uint32_t i;
2093
2094 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2095 dev_mod = ctx->cur_mod->deviated_by[u];
2096
2097 /* compile all module deviations */
2098 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2099 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2100 }
2101
2102 /* compile all submodules deviations */
2103 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2104 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2105 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2106 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2107 }
2108 }
2109 }
2110
2111 /* set not-supported flags for all the deviations */
2112 for (i = 0; i < ctx->devs.count; ++i) {
2113 dev = ctx->devs.objs[i];
2114 not_supported = 0;
2115
2116 LY_ARRAY_FOR(dev->devs, u) {
2117 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2118 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2119 not_supported = 1;
2120 break;
2121 }
2122 }
2123 if (not_supported) {
2124 break;
2125 }
2126 }
2127 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Michal Vaskoe8220db2021-06-02 15:39:05 +02002128 orig_cur_mod = ctx->cur_mod;
2129 ctx->cur_mod = dev->dev_pmods[u]->mod;
2130 lysc_update_path(ctx, NULL, "{deviation}");
2131 lysc_update_path(ctx, NULL, dev->nodeid->expr);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002132 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002133 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
Michal Vaskoe8220db2021-06-02 15:39:05 +02002134 lysc_update_path(ctx, NULL, NULL);
2135 lysc_update_path(ctx, NULL, NULL);
2136 ctx->cur_mod = orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002137 return LY_EVALID;
2138 }
2139
2140 dev->not_supported = not_supported;
2141 }
2142
2143 return LY_SUCCESS;
2144}
2145
2146/**
2147 * @brief Add a module reference into an array, checks for duplicities.
2148 *
2149 * @param[in] ctx Compile context.
2150 * @param[in] mod Module reference to add.
2151 * @param[in,out] mod_array Module sized array to add to.
2152 * @return LY_ERR value.
2153 */
2154static LY_ERR
2155lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2156{
2157 LY_ARRAY_COUNT_TYPE u;
2158 struct lys_module **new_mod;
2159
2160 LY_ARRAY_FOR(*mod_array, u) {
2161 if ((*mod_array)[u] == mod) {
2162 /* already there */
2163 return LY_EEXIST;
2164 }
2165 }
2166
2167 /* add the new module ref */
2168 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2169 *new_mod = mod;
2170
2171 return LY_SUCCESS;
2172}
2173
Michal Vasko1ccbf542021-04-19 11:35:00 +02002174/**
2175 * @brief Check whether all modules in a set are implemented.
2176 *
2177 * @param[in] mod_set Module set to check.
2178 * @return Whether all modules are implemented or not.
2179 */
2180static ly_bool
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002181lys_precompile_mod_set_is_all_implemented(const struct ly_set *mod_set)
Michal Vasko1ccbf542021-04-19 11:35:00 +02002182{
2183 uint32_t i;
2184 const struct lys_module *mod;
2185
2186 for (i = 0; i < mod_set->count; ++i) {
2187 mod = mod_set->objs[i];
2188 if (!mod->implemented) {
2189 return 0;
2190 }
2191 }
2192
2193 return 1;
2194}
2195
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002196LY_ERR
Michal Vasko65333882021-06-10 14:12:16 +02002197lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002198{
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002199 LY_ERR ret = LY_SUCCESS, r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002200 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko65333882021-06-10 14:12:16 +02002201 struct lysc_ctx ctx = {0};
2202 struct lysp_module *mod_p;
2203 struct lys_module *m;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002204 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002205 struct lysp_node_augment *aug;
Michal Vaskoc56d6372021-10-19 12:29:00 +02002206 const char **imp_f, *all_f[] = {"*", NULL};
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002207 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002208 struct ly_set mod_set = {0}, set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002209
Michal Vasko65333882021-06-10 14:12:16 +02002210 mod_p = mod->parsed;
2211
2212 /* prepare context */
2213 ctx.ctx = mod->ctx;
2214 ctx.cur_mod = mod;
2215 ctx.pmod = mod_p;
2216 ctx.path_len = 1;
2217 ctx.path[0] = '/';
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002218
Radek Krejci2a9fc652021-01-22 17:44:34 +01002219 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002220 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002221 lysc_update_path(&ctx, NULL, "{augment}");
2222 lysc_update_path(&ctx, NULL, aug->nodeid);
2223 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2224 lysc_update_path(&ctx, NULL, NULL);
2225 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002226 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002227
Michal Vasko1ccbf542021-04-19 11:35:00 +02002228 /* add this module into the target module augmented_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002229 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002230 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002231 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002232 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002233 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002234 }
2235
2236 LY_ARRAY_FOR(mod_p->deviations, u) {
2237 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002238 lysc_update_path(&ctx, NULL, "{deviation}");
2239 lysc_update_path(&ctx, NULL, mod_p->deviations[u].nodeid);
2240 ret = lys_nodeid_mod_check(&ctx, mod_p->deviations[u].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 Vasko1ccbf542021-04-19 11:35:00 +02002245 /* add this module into the target module deviated_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002246 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002247 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002248 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2249 }
2250 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002251 }
2252
2253 /* the same for augments and deviations in submodules */
2254 LY_ARRAY_FOR(mod_p->includes, v) {
2255 submod = mod_p->includes[v].submodule;
Michal Vasko65333882021-06-10 14:12:16 +02002256 ctx.pmod = (struct lysp_module *)submod;
Michal Vaskoce3d6172021-06-08 14:59:49 +02002257
Radek Krejci2a9fc652021-01-22 17:44:34 +01002258 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko65333882021-06-10 14:12:16 +02002259 lysc_update_path(&ctx, NULL, "{augment}");
2260 lysc_update_path(&ctx, NULL, aug->nodeid);
2261 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2262 lysc_update_path(&ctx, NULL, NULL);
2263 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002264 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002265
Michal Vasko65333882021-06-10 14:12:16 +02002266 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002267 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002268 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002269 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002270 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002271 }
2272
2273 LY_ARRAY_FOR(submod->deviations, u) {
Michal Vasko65333882021-06-10 14:12:16 +02002274 lysc_update_path(&ctx, NULL, "{deviation}");
2275 lysc_update_path(&ctx, NULL, submod->deviations[u].nodeid);
2276 ret = lys_nodeid_mod_check(&ctx, submod->deviations[u].nodeid, 1, &set, NULL, &m);
2277 lysc_update_path(&ctx, NULL, NULL);
2278 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002279 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002280
Michal Vasko65333882021-06-10 14:12:16 +02002281 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vaskoffe7dfe2021-06-15 11:58:38 +02002282 !lys_precompile_mod_set_is_all_implemented(&set)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002283 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2284 }
2285 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002286 }
2287 }
2288
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002289 for (i = 0; i < mod_set.count; ++i) {
2290 m = mod_set.objs[i];
Michal Vasko1ccbf542021-04-19 11:35:00 +02002291
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002292 if (m == mod) {
2293 /* will be applied normally later */
2294 continue;
2295 }
2296
2297 /* we do not actually need the target modules compiled with out amends, they just need to be implemented
2298 * not compiled yet and marked for compilation */
2299
2300 if (!m->implemented) {
2301 /* implement the target module */
Michal Vaskoc56d6372021-10-19 12:29:00 +02002302 imp_f = (mod->ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL;
2303 r = lys_implement(m, imp_f, unres);
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002304 if (r == LY_ERECOMPILE) {
2305 /* implement all the modules right away to save possible later recompilation */
2306 ret = r;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002307 continue;
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002308 } else if (r) {
2309 /* error */
2310 ret = r;
Michal Vasko65333882021-06-10 14:12:16 +02002311 goto cleanup;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002312 }
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002313 } else if (m->compiled) {
2314 /* target module was already compiled without our amends (augment/deviation), we need to recompile it */
2315 m->to_compile = 1;
2316 ret = LY_ERECOMPILE;
2317 continue;
2318 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002319 }
2320
Michal Vasko1ccbf542021-04-19 11:35:00 +02002321cleanup:
2322 ly_set_erase(&set, NULL);
2323 ly_set_erase(&mod_set, NULL);
2324 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002325}
2326
2327void
2328lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2329{
2330 uint32_t i;
2331 LY_ARRAY_COUNT_TYPE u, count;
2332 struct lys_module *m;
2333
2334 for (i = 0; i < ctx->list.count; ++i) {
2335 m = ctx->list.objs[i];
2336
2337 if (m->augmented_by) {
2338 count = LY_ARRAY_COUNT(m->augmented_by);
2339 for (u = 0; u < count; ++u) {
2340 if (m->augmented_by[u] == mod) {
2341 /* keep the order */
2342 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002343 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u - 1) * sizeof *m->augmented_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002344 }
2345 LY_ARRAY_DECREMENT(m->augmented_by);
2346 break;
2347 }
2348 }
2349 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2350 LY_ARRAY_FREE(m->augmented_by);
2351 m->augmented_by = NULL;
2352 }
2353 }
2354
2355 if (m->deviated_by) {
2356 count = LY_ARRAY_COUNT(m->deviated_by);
2357 for (u = 0; u < count; ++u) {
2358 if (m->deviated_by[u] == mod) {
2359 /* keep the order */
2360 if (u < count - 1) {
Michal Vasko69bd24a2021-06-29 08:12:06 +02002361 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u - 1) * sizeof *m->deviated_by);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002362 }
2363 LY_ARRAY_DECREMENT(m->deviated_by);
2364 break;
2365 }
2366 }
2367 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2368 LY_ARRAY_FREE(m->deviated_by);
2369 m->deviated_by = NULL;
2370 }
2371 }
2372 }
2373}