blob: 7c560223e8f4438464eb6081ad77ba412c3b7de3 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_amend.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of augments, deviations, and refines.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
16
17#include "schema_compile_amend.h"
18
19#include <assert.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020020#include <stddef.h>
21#include <stdint.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020022#include <stdlib.h>
23#include <string.h>
24
25#include "common.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020026#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020027#include "plugins_exts.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020028#include "schema_compile.h"
29#include "schema_compile_node.h"
Michal Vasko29dd11e2020-11-23 16:52:22 +010030#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020031#include "set.h"
32#include "tree.h"
33#include "tree_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020034#include "tree_schema.h"
35#include "tree_schema_internal.h"
36#include "xpath.h"
37
38static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest,
39 size_t nametest_len, const struct lysp_module *mod, const char **name, size_t *name_len);
40
41static LY_ERR
42lys_nodeid_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct lys_module **target_mod,
43 struct lyxp_expr **expr)
44{
45 LY_ERR ret = LY_SUCCESS;
46 struct lyxp_expr *e = NULL;
47 struct lys_module *tmod = NULL, *mod;
48 const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid";
49 uint32_t i;
50
51 /* parse */
52 ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e);
53 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010054 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020055 nodeid_type, nodeid);
56 ret = LY_EVALID;
57 goto cleanup;
58 }
59
60 if (abs) {
61 /* absolute schema nodeid */
62 i = 0;
63 } else {
64 /* descendant schema nodeid */
65 if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010066 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020067 nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]);
68 ret = LY_EVALID;
69 goto cleanup;
70 }
71 i = 1;
72 }
73
74 /* check all the tokens */
75 for ( ; i < e->used; i += 2) {
76 if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010077 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020078 nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]);
79 ret = LY_EVALID;
80 goto cleanup;
81 } else if (e->used == i + 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010082 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020083 "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr);
84 ret = LY_EVALID;
85 goto cleanup;
86 } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010087 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020088 nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
89 ret = LY_EVALID;
90 goto cleanup;
91 } else if (abs) {
92 mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1],
93 e->tok_len[i + 1], ctx->pmod, NULL, NULL);
94 LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup);
95
96 /* only keep the first module */
97 if (!tmod) {
98 tmod = mod;
99 }
100
101 /* all the modules must be implemented */
102 if (!mod->implemented) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100103 ret = lys_set_implemented_r(mod, NULL, ctx->unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200104 LY_CHECK_GOTO(ret, cleanup);
105 }
106 }
107 }
108
109cleanup:
110 if (ret || !expr) {
111 lyxp_expr_free(ctx->ctx, e);
112 e = NULL;
113 }
114 if (expr) {
115 *expr = ret ? NULL : e;
116 }
117 if (target_mod) {
118 *target_mod = ret ? NULL : tmod;
119 }
120 return ret;
121}
122
123/**
124 * @brief Check whether 2 schema nodeids match.
125 *
126 * @param[in] ctx libyang context.
127 * @param[in] exp1 First schema nodeid.
128 * @param[in] exp1p_mod Module of @p exp1 nodes without any prefix.
129 * @param[in] exp2 Second schema nodeid.
130 * @param[in] exp2_pmod Module of @p exp2 nodes without any prefix.
131 * @return Whether the schema nodeids match or not.
132 */
133static ly_bool
134lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lysp_module *exp1_pmod,
135 const struct lyxp_expr *exp2, const struct lysp_module *exp2_pmod)
136{
137 uint32_t i;
138 const struct lys_module *mod1, *mod2;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100139 const char *name1 = NULL, *name2 = NULL;
140 size_t name1_len = 0, name2_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200141
142 if (exp1->used != exp2->used) {
143 return 0;
144 }
145
146 for (i = 0; i < exp1->used; ++i) {
147 assert(exp1->tokens[i] == exp2->tokens[i]);
148
149 if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) {
150 /* check modules of all the nodes in the node ID */
151 mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_pmod,
152 &name1, &name1_len);
153 assert(mod1);
154 mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_pmod,
155 &name2, &name2_len);
156 assert(mod2);
157
158 /* compare modules */
159 if (mod1 != mod2) {
160 return 0;
161 }
162
163 /* compare names */
164 if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) {
165 return 0;
166 }
167 }
168 }
169
170 return 1;
171}
172
173LY_ERR
174lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node)
175{
176 LY_ERR ret = LY_SUCCESS;
177 LY_ARRAY_COUNT_TYPE u;
178 struct lyxp_expr *exp = NULL;
179 struct lysc_augment *aug;
180 struct lysc_refine *rfn;
181 struct lysp_refine **new_rfn;
182 uint32_t i;
183
184 LY_ARRAY_FOR(uses_p->augments, u) {
185 lysc_update_path(ctx, NULL, "{augment}");
186 lysc_update_path(ctx, NULL, uses_p->augments[u].nodeid);
187
188 /* parse the nodeid */
189 LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->augments[u].nodeid, 0, NULL, &exp), cleanup);
190
191 /* allocate new compiled augment and store it in the set */
192 aug = calloc(1, sizeof *aug);
193 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
194 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, 1, NULL), cleanup);
195
196 aug->nodeid = exp;
197 exp = NULL;
198 aug->nodeid_pmod = ctx->pmod;
199 aug->nodeid_ctx_node = ctx_node;
200 aug->aug_p = &uses_p->augments[u];
201
202 lysc_update_path(ctx, NULL, NULL);
203 lysc_update_path(ctx, NULL, NULL);
204 }
205
206 LY_ARRAY_FOR(uses_p->refines, u) {
207 lysc_update_path(ctx, NULL, "{refine}");
208 lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid);
209
210 /* parse the nodeid */
211 LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->refines[u].nodeid, 0, NULL, &exp), cleanup);
212
213 /* try to find the node in already compiled refines */
214 rfn = NULL;
215 for (i = 0; i < ctx->uses_rfns.count; ++i) {
216 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->pmod, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid,
217 ctx->pmod)) {
218 rfn = ctx->uses_rfns.objs[i];
219 break;
220 }
221 }
222
223 if (!rfn) {
224 /* allocate new compiled refine */
225 rfn = calloc(1, sizeof *rfn);
226 LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
227 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, 1, NULL), cleanup);
228
229 rfn->nodeid = exp;
230 exp = NULL;
231 rfn->nodeid_pmod = ctx->pmod;
232 rfn->nodeid_ctx_node = ctx_node;
233 } else {
234 /* just free exp */
235 lyxp_expr_free(ctx->ctx, exp);
236 exp = NULL;
237 }
238
239 /* add new parsed refine structure */
240 LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup);
241 *new_rfn = &uses_p->refines[u];
242
243 lysc_update_path(ctx, NULL, NULL);
244 lysc_update_path(ctx, NULL, NULL);
245 }
246
247cleanup:
248 lyxp_expr_free(ctx->ctx, exp);
249 return ret;
250}
251
252static LY_ERR
253lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext)
254{
255 LY_ERR ret = LY_SUCCESS;
256
257 *ext = *orig_ext;
258 DUP_STRING(ctx, orig_ext->name, ext->name, ret);
259 DUP_STRING(ctx, orig_ext->argument, ext->argument, ret);
260
261 return ret;
262}
263
264static LY_ERR
265lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr)
266{
267 LY_ERR ret = LY_SUCCESS;
268
269 if (orig_restr) {
270 DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret);
271 restr->arg.mod = orig_restr->arg.mod;
272 DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret);
273 DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret);
274 DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret);
275 DUP_STRING(ctx, orig_restr->ref, restr->ref, ret);
276 DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup);
277 }
278
279 return ret;
280}
281
282static LY_ERR
283lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str)
284{
285 LY_ERR ret = LY_SUCCESS;
286
287 DUP_STRING(ctx, *orig_str, *str, ret);
288
289 return ret;
290}
291
292LY_ERR
293lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname)
294{
295 LY_ERR ret = LY_SUCCESS;
296
297 if (!orig_qname->str) {
298 return LY_SUCCESS;
299 }
300
301 DUP_STRING(ctx, orig_qname->str, qname->str, ret);
302 assert(orig_qname->mod);
303 qname->mod = orig_qname->mod;
304
305 return ret;
306}
307
308static LY_ERR
309lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm)
310{
311 LY_ERR ret = LY_SUCCESS;
312
313 DUP_STRING(ctx, orig_enm->name, enm->name, ret);
314 DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret);
315 DUP_STRING(ctx, orig_enm->ref, enm->ref, ret);
316 enm->value = orig_enm->value;
317 DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup);
318 DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup);
319 enm->flags = orig_enm->flags;
320
321 return ret;
322}
323
324static LY_ERR
325lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type)
326{
327 LY_ERR ret = LY_SUCCESS;
328
329 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
330
331 if (orig_type->range) {
332 type->range = calloc(1, sizeof *type->range);
333 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
334 LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range));
335 }
336
337 if (orig_type->length) {
338 type->length = calloc(1, sizeof *type->length);
339 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
340 LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length));
341 }
342
343 DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup);
344 DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup);
345 DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup);
346 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done);
347 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
348 DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup);
349 DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup);
350
351 type->pmod = orig_type->pmod;
352 type->compiled = orig_type->compiled;
353
354 type->fraction_digits = orig_type->fraction_digits;
355 type->require_instance = orig_type->require_instance;
356 type->flags = orig_type->flags;
357
358done:
359 return ret;
360}
361
362static LY_ERR
363lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when)
364{
365 LY_ERR ret = LY_SUCCESS;
366
367 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
368 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
369 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
370 DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup);
371
372 return ret;
373}
374
375static LY_ERR
376lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
377{
378 LY_ERR ret = LY_SUCCESS;
379
380 node->parent = NULL;
381 node->nodetype = orig->nodetype;
382 node->flags = orig->flags;
383 node->next = NULL;
384 DUP_STRING(ctx, orig->name, node->name, ret);
385 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
386 DUP_STRING(ctx, orig->ref, node->ref, ret);
387
388 if (orig->when) {
389 node->when = calloc(1, sizeof *node->when);
390 LY_CHECK_ERR_RET(!node->when, LOGMEM(ctx), LY_EMEM);
391 LY_CHECK_RET(lysp_when_dup(ctx, node->when, orig->when));
392 }
393
394 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
395 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
396
397 return ret;
398}
399
400static LY_ERR
401lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
402{
403 LY_ERR ret = LY_SUCCESS;
404 struct lysp_node_container *cont;
405 const struct lysp_node_container *orig_cont;
406 struct lysp_node_leaf *leaf;
407 const struct lysp_node_leaf *orig_leaf;
408 struct lysp_node_leaflist *llist;
409 const struct lysp_node_leaflist *orig_llist;
410 struct lysp_node_list *list;
411 const struct lysp_node_list *orig_list;
412 struct lysp_node_choice *choice;
413 const struct lysp_node_choice *orig_choice;
414 struct lysp_node_case *cas;
415 const struct lysp_node_case *orig_cas;
416 struct lysp_node_anydata *any;
417 const struct lysp_node_anydata *orig_any;
418
419 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA));
420
421 /* common part */
422 LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig));
423
424 /* specific part */
425 switch (node->nodetype) {
426 case LYS_CONTAINER:
427 cont = (struct lysp_node_container *)node;
428 orig_cont = (const struct lysp_node_container *)orig;
429
430 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
431 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
432 /* we do not need the rest */
433 break;
434 case LYS_LEAF:
435 leaf = (struct lysp_node_leaf *)node;
436 orig_leaf = (const struct lysp_node_leaf *)orig;
437
438 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
439 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
440 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
441 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
442 break;
443 case LYS_LEAFLIST:
444 llist = (struct lysp_node_leaflist *)node;
445 orig_llist = (const struct lysp_node_leaflist *)orig;
446
447 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
448 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
449 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
450 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
451 llist->min = orig_llist->min;
452 llist->max = orig_llist->max;
453 break;
454 case LYS_LIST:
455 list = (struct lysp_node_list *)node;
456 orig_list = (const struct lysp_node_list *)orig;
457
458 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
459 DUP_STRING(ctx, orig_list->key, list->key, ret);
460 /* we do not need these arrays */
461 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
462 list->min = orig_list->min;
463 list->max = orig_list->max;
464 break;
465 case LYS_CHOICE:
466 choice = (struct lysp_node_choice *)node;
467 orig_choice = (const struct lysp_node_choice *)orig;
468
469 /* we do not need children */
470 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
471 break;
472 case LYS_CASE:
473 cas = (struct lysp_node_case *)node;
474 orig_cas = (const struct lysp_node_case *)orig;
475
476 /* we do not need children */
477 (void)cas;
478 (void)orig_cas;
479 break;
480 case LYS_ANYDATA:
481 case LYS_ANYXML:
482 any = (struct lysp_node_anydata *)node;
483 orig_any = (const struct lysp_node_anydata *)orig;
484
485 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
486 break;
487 default:
488 LOGINT_RET(ctx);
489 }
490
491 return ret;
492}
493
494static LY_ERR
495lysp_action_inout_dup(const struct ly_ctx *ctx, struct lysp_action_inout *inout, const struct lysp_action_inout *orig)
496{
497 inout->parent = NULL;
498 inout->nodetype = orig->nodetype;
499 DUP_ARRAY(ctx, orig->musts, inout->musts, lysp_restr_dup);
500 /* we dot need these arrays */
501 DUP_ARRAY(ctx, orig->exts, inout->exts, lysp_ext_dup);
502
503 return LY_SUCCESS;
504}
505
506static LY_ERR
507lysp_action_dup(const struct ly_ctx *ctx, struct lysp_action *act, const struct lysp_action *orig)
508{
509 LY_ERR ret = LY_SUCCESS;
510
511 act->parent = NULL;
512 act->nodetype = orig->nodetype;
513 act->flags = orig->flags;
514 DUP_STRING(ctx, orig->name, act->name, ret);
515 DUP_STRING(ctx, orig->dsc, act->dsc, ret);
516 DUP_STRING(ctx, orig->ref, act->ref, ret);
517 DUP_ARRAY(ctx, orig->iffeatures, act->iffeatures, lysp_qname_dup);
518
519 act->input.nodetype = orig->input.nodetype;
520 act->output.nodetype = orig->output.nodetype;
521 /* we do not need choldren of in/out */
522 DUP_ARRAY(ctx, orig->exts, act->exts, lysp_ext_dup);
523
524 return ret;
525}
526
527static LY_ERR
528lysp_notif_dup(const struct ly_ctx *ctx, struct lysp_notif *notif, const struct lysp_notif *orig)
529{
530 LY_ERR ret = LY_SUCCESS;
531
532 notif->parent = NULL;
533 notif->nodetype = orig->nodetype;
534 notif->flags = orig->flags;
535 DUP_STRING(ctx, orig->name, notif->name, ret);
536 DUP_STRING(ctx, orig->dsc, notif->dsc, ret);
537 DUP_STRING(ctx, orig->ref, notif->ref, ret);
538 DUP_ARRAY(ctx, orig->iffeatures, notif->iffeatures, lysp_qname_dup);
539 DUP_ARRAY(ctx, orig->musts, notif->musts, lysp_restr_dup);
540 /* we do not need these arrays */
541 DUP_ARRAY(ctx, orig->exts, notif->exts, lysp_ext_dup);
542
543 return ret;
544}
545
546/**
547 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
548 *
549 * @param[in] ctx libyang context.
550 * @param[in] pnode Node to duplicate.
551 * @param[in] with_links Whether to also copy any links (child, parent pointers).
552 * @param[out] dup_p Duplicated parsed node.
553 * @return LY_ERR value.
554 */
555static LY_ERR
556lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
557{
558 LY_ERR ret = LY_SUCCESS;
559 void *mem = NULL;
560
561 if (!pnode) {
562 *dup_p = NULL;
563 return LY_SUCCESS;
564 }
565
566 switch (pnode->nodetype) {
567 case LYS_CONTAINER:
568 mem = calloc(1, sizeof(struct lysp_node_container));
569 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
570 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
571 break;
572 case LYS_LEAF:
573 mem = calloc(1, sizeof(struct lysp_node_leaf));
574 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
575 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
576 break;
577 case LYS_LEAFLIST:
578 mem = calloc(1, sizeof(struct lysp_node_leaflist));
579 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
580 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
581 break;
582 case LYS_LIST:
583 mem = calloc(1, sizeof(struct lysp_node_list));
584 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
585 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
586 break;
587 case LYS_CHOICE:
588 mem = calloc(1, sizeof(struct lysp_node_choice));
589 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
590 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
591 break;
592 case LYS_CASE:
593 mem = calloc(1, sizeof(struct lysp_node_case));
594 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
595 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
596 break;
597 case LYS_ANYDATA:
598 case LYS_ANYXML:
599 mem = calloc(1, sizeof(struct lysp_node_anydata));
600 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
601 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
602 break;
603 case LYS_INPUT:
604 case LYS_OUTPUT:
605 mem = calloc(1, sizeof(struct lysp_action_inout));
606 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
607 LY_CHECK_GOTO(ret = lysp_action_inout_dup(ctx, mem, (struct lysp_action_inout *)pnode), cleanup);
608 break;
609 case LYS_ACTION:
610 case LYS_RPC:
611 mem = calloc(1, sizeof(struct lysp_action));
612 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
613 LY_CHECK_GOTO(ret = lysp_action_dup(ctx, mem, (struct lysp_action *)pnode), cleanup);
614 break;
615 case LYS_NOTIF:
616 mem = calloc(1, sizeof(struct lysp_notif));
617 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
618 LY_CHECK_GOTO(ret = lysp_notif_dup(ctx, mem, (struct lysp_notif *)pnode), cleanup);
619 break;
620 default:
621 LOGINT_RET(ctx);
622 }
623
624 if (with_links) {
625 /* copy also parent and child pointers */
626 ((struct lysp_node *)mem)->parent = pnode->parent;
627 switch (pnode->nodetype) {
628 case LYS_CONTAINER:
629 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
630 break;
631 case LYS_LIST:
632 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
633 break;
634 case LYS_CHOICE:
635 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
636 break;
637 case LYS_CASE:
638 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
639 break;
640 default:
641 break;
642 }
643 }
644
645cleanup:
646 if (ret) {
647 free(mem);
648 } else {
649 *dup_p = mem;
650 }
651 return ret;
652}
653
654#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100655 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 +0200656 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
657 ret = LY_EVALID; \
658 goto cleanup;
659
660#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
661 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100662 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 +0200663 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
664 ret = LY_EVALID; \
665 goto cleanup; \
666 }
667
668/**
669 * @brief Apply refine.
670 *
671 * @param[in] ctx Compile context.
672 * @param[in] rfn Refine to apply.
673 * @param[in,out] target Refine target.
674 * @return LY_ERR value.
675 */
676static LY_ERR
677lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
678{
679 LY_ERR ret = LY_SUCCESS;
680 LY_ARRAY_COUNT_TYPE u;
681 struct lysp_qname *qname;
682 struct lysp_restr **musts, *must;
683 uint32_t *num;
684
685 /* default value */
686 if (rfn->dflts) {
687 switch (target->nodetype) {
688 case LYS_LEAF:
689 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
690
691 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
692 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
693 break;
694 case LYS_LEAFLIST:
695 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100696 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200697 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
698 ret = LY_EVALID;
699 goto cleanup;
700 }
701
702 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
703 ((struct lysp_node_leaflist *)target)->dflts = NULL;
704 LY_ARRAY_FOR(rfn->dflts, u) {
705 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
706 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
707 }
708 break;
709 case LYS_CHOICE:
710 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
711
712 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
713 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
714 break;
715 default:
716 AMEND_WRONG_NODETYPE("refine", "replace", "default");
717 }
718 }
719
720 /* description */
721 if (rfn->dsc) {
722 FREE_STRING(ctx->ctx, target->dsc);
723 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
724 }
725
726 /* reference */
727 if (rfn->ref) {
728 FREE_STRING(ctx->ctx, target->ref);
729 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
730 }
731
732 /* config */
733 if (rfn->flags & LYS_CONFIG_MASK) {
734 if (ctx->options & (LYS_COMPILE_NOTIFICATION | LYS_COMPILE_RPC_INPUT | LYS_COMPILE_RPC_OUTPUT)) {
735 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
736 ctx->options & LYS_COMPILE_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
737 } else {
738 target->flags &= ~LYS_CONFIG_MASK;
739 target->flags |= rfn->flags & LYS_CONFIG_MASK;
740 }
741 }
742
743 /* mandatory */
744 if (rfn->flags & LYS_MAND_MASK) {
745 switch (target->nodetype) {
746 case LYS_LEAF:
747 case LYS_CHOICE:
748 case LYS_ANYDATA:
749 case LYS_ANYXML:
750 break;
751 default:
752 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
753 }
754
755 target->flags &= ~LYS_MAND_MASK;
756 target->flags |= rfn->flags & LYS_MAND_MASK;
757 }
758
759 /* presence */
760 if (rfn->presence) {
761 switch (target->nodetype) {
762 case LYS_CONTAINER:
763 break;
764 default:
765 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
766 }
767
768 FREE_STRING(ctx->ctx, ((struct lysp_node_container *)target)->presence);
769 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
770 }
771
772 /* must */
773 if (rfn->musts) {
774 switch (target->nodetype) {
775 case LYS_CONTAINER:
776 case LYS_LIST:
777 case LYS_LEAF:
778 case LYS_LEAFLIST:
779 case LYS_ANYDATA:
780 case LYS_ANYXML:
781 musts = &((struct lysp_node_container *)target)->musts;
782 break;
783 default:
784 AMEND_WRONG_NODETYPE("refine", "add", "must");
785 }
786
787 LY_ARRAY_FOR(rfn->musts, u) {
788 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
789 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
790 }
791 }
792
793 /* min-elements */
794 if (rfn->flags & LYS_SET_MIN) {
795 switch (target->nodetype) {
796 case LYS_LEAFLIST:
797 num = &((struct lysp_node_leaflist *)target)->min;
798 break;
799 case LYS_LIST:
800 num = &((struct lysp_node_list *)target)->min;
801 break;
802 default:
803 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
804 }
805
806 *num = rfn->min;
807 }
808
809 /* max-elements */
810 if (rfn->flags & LYS_SET_MAX) {
811 switch (target->nodetype) {
812 case LYS_LEAFLIST:
813 num = &((struct lysp_node_leaflist *)target)->max;
814 break;
815 case LYS_LIST:
816 num = &((struct lysp_node_list *)target)->max;
817 break;
818 default:
819 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
820 }
821
822 *num = rfn->max;
823 }
824
825 /* if-feature */
826 if (rfn->iffeatures) {
827 switch (target->nodetype) {
828 case LYS_LEAF:
829 case LYS_LEAFLIST:
830 case LYS_LIST:
831 case LYS_CONTAINER:
832 case LYS_CHOICE:
833 case LYS_CASE:
834 case LYS_ANYDATA:
835 case LYS_ANYXML:
836 break;
837 default:
838 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
839 }
840
841 LY_ARRAY_FOR(rfn->iffeatures, u) {
842 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
843 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
844 }
845 }
846
847 /* extension */
848 /* TODO refine extensions */
849
850cleanup:
851 return ret;
852}
853
854/**
855 * @brief Apply deviate add.
856 *
857 * @param[in] ctx Compile context.
858 * @param[in] d Deviate add to apply.
859 * @param[in,out] target Deviation target.
860 * @return LY_ERR value.
861 */
862static LY_ERR
863lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
864{
865 LY_ERR ret = LY_SUCCESS;
866 LY_ARRAY_COUNT_TYPE u;
867 struct lysp_qname *qname;
868 uint32_t *num;
869 struct lysp_restr **musts, *must;
870
871#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
872 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100873 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200874 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
875 ret = LY_EVALID; \
876 goto cleanup; \
877 }
878
879 /* [units-stmt] */
880 if (d->units) {
881 switch (target->nodetype) {
882 case LYS_LEAF:
883 case LYS_LEAFLIST:
884 break;
885 default:
886 AMEND_WRONG_NODETYPE("deviation", "add", "units");
887 }
888
889 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
890 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
891 }
892
893 /* *must-stmt */
894 if (d->musts) {
895 switch (target->nodetype) {
896 case LYS_CONTAINER:
897 case LYS_LIST:
898 case LYS_LEAF:
899 case LYS_LEAFLIST:
900 case LYS_ANYDATA:
901 case LYS_ANYXML:
902 musts = &((struct lysp_node_container *)target)->musts;
903 break;
904 case LYS_NOTIF:
905 musts = &((struct lysp_notif *)target)->musts;
906 break;
907 case LYS_INPUT:
908 case LYS_OUTPUT:
909 musts = &((struct lysp_action_inout *)target)->musts;
910 break;
911 default:
912 AMEND_WRONG_NODETYPE("deviation", "add", "must");
913 }
914
915 LY_ARRAY_FOR(d->musts, u) {
916 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
917 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
918 }
919 }
920
921 /* *unique-stmt */
922 if (d->uniques) {
923 switch (target->nodetype) {
924 case LYS_LIST:
925 break;
926 default:
927 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
928 }
929
930 LY_ARRAY_FOR(d->uniques, u) {
931 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
932 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
933 }
934 }
935
936 /* *default-stmt */
937 if (d->dflts) {
938 switch (target->nodetype) {
939 case LYS_LEAF:
940 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
941 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
942
943 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
944 break;
945 case LYS_LEAFLIST:
946 LY_ARRAY_FOR(d->dflts, u) {
947 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
948 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
949 }
950 break;
951 case LYS_CHOICE:
952 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
953 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
954
955 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
956 break;
957 default:
958 AMEND_WRONG_NODETYPE("deviation", "add", "default");
959 }
960 }
961
962 /* [config-stmt] */
963 if (d->flags & LYS_CONFIG_MASK) {
964 switch (target->nodetype) {
965 case LYS_CONTAINER:
966 case LYS_LEAF:
967 case LYS_LEAFLIST:
968 case LYS_LIST:
969 case LYS_CHOICE:
970 case LYS_ANYDATA:
971 case LYS_ANYXML:
972 break;
973 default:
974 AMEND_WRONG_NODETYPE("deviation", "add", "config");
975 }
976
977 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100978 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200979 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
980 target->flags & LYS_CONFIG_W ? "true" : "false");
981 ret = LY_EVALID;
982 goto cleanup;
983 }
984
985 target->flags |= d->flags & LYS_CONFIG_MASK;
986 }
987
988 /* [mandatory-stmt] */
989 if (d->flags & LYS_MAND_MASK) {
990 switch (target->nodetype) {
991 case LYS_LEAF:
992 case LYS_CHOICE:
993 case LYS_ANYDATA:
994 case LYS_ANYXML:
995 break;
996 default:
997 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
998 }
999
1000 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001001 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001002 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
1003 target->flags & LYS_MAND_TRUE ? "true" : "false");
1004 ret = LY_EVALID;
1005 goto cleanup;
1006 }
1007
1008 target->flags |= d->flags & LYS_MAND_MASK;
1009 }
1010
1011 /* [min-elements-stmt] */
1012 if (d->flags & LYS_SET_MIN) {
1013 switch (target->nodetype) {
1014 case LYS_LEAFLIST:
1015 num = &((struct lysp_node_leaflist *)target)->min;
1016 break;
1017 case LYS_LIST:
1018 num = &((struct lysp_node_list *)target)->min;
1019 break;
1020 default:
1021 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
1022 }
1023
1024 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001025 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001026 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
1027 ret = LY_EVALID;
1028 goto cleanup;
1029 }
1030
1031 *num = d->min;
1032 }
1033
1034 /* [max-elements-stmt] */
1035 if (d->flags & LYS_SET_MAX) {
1036 switch (target->nodetype) {
1037 case LYS_LEAFLIST:
1038 num = &((struct lysp_node_leaflist *)target)->max;
1039 break;
1040 case LYS_LIST:
1041 num = &((struct lysp_node_list *)target)->max;
1042 break;
1043 default:
1044 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1045 }
1046
1047 if (target->flags & LYS_SET_MAX) {
1048 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001049 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001050 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1051 *num);
1052 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001053 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001054 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1055 }
1056 ret = LY_EVALID;
1057 goto cleanup;
1058 }
1059
1060 *num = d->max;
1061 }
1062
1063cleanup:
1064 return ret;
1065}
1066
1067/**
1068 * @brief Apply deviate delete.
1069 *
1070 * @param[in] ctx Compile context.
1071 * @param[in] d Deviate delete to apply.
1072 * @param[in,out] target Deviation target.
1073 * @return LY_ERR value.
1074 */
1075static LY_ERR
1076lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1077{
1078 LY_ERR ret = LY_SUCCESS;
1079 struct lysp_restr **musts;
1080 LY_ARRAY_COUNT_TYPE u, v;
1081 struct lysp_qname **uniques, **dflts;
1082
1083#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1084 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1085 int found = 0; \
1086 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1087 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1088 found = 1; \
1089 break; \
1090 } \
1091 } \
1092 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001093 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001094 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1095 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1096 ret = LY_EVALID; \
1097 goto cleanup; \
1098 } \
1099 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1100 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
1101 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1102 } \
1103 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1104 LY_ARRAY_FREE(ORIG_ARRAY); \
1105 ORIG_ARRAY = NULL; \
1106 }
1107
1108#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1109 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001110 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001111 ret = LY_EVALID; \
1112 goto cleanup; \
1113 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001114 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001115 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1116 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1117 ret = LY_EVALID; \
1118 goto cleanup; \
1119 }
1120
1121 /* [units-stmt] */
1122 if (d->units) {
1123 switch (target->nodetype) {
1124 case LYS_LEAF:
1125 case LYS_LEAFLIST:
1126 break;
1127 default:
1128 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1129 }
1130
1131 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
1132 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1133 ((struct lysp_node_leaf *)target)->units = NULL;
1134 }
1135
1136 /* *must-stmt */
1137 if (d->musts) {
1138 switch (target->nodetype) {
1139 case LYS_CONTAINER:
1140 case LYS_LIST:
1141 case LYS_LEAF:
1142 case LYS_LEAFLIST:
1143 case LYS_ANYDATA:
1144 case LYS_ANYXML:
1145 musts = &((struct lysp_node_container *)target)->musts;
1146 break;
1147 case LYS_NOTIF:
1148 musts = &((struct lysp_notif *)target)->musts;
1149 break;
1150 case LYS_INPUT:
1151 case LYS_OUTPUT:
1152 musts = &((struct lysp_action_inout *)target)->musts;
1153 break;
1154 default:
1155 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1156 }
1157
1158 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1159 }
1160
1161 /* *unique-stmt */
1162 if (d->uniques) {
1163 switch (target->nodetype) {
1164 case LYS_LIST:
1165 break;
1166 default:
1167 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1168 }
1169
1170 uniques = &((struct lysp_node_list *)target)->uniques;
1171 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1172 }
1173
1174 /* *default-stmt */
1175 if (d->dflts) {
1176 switch (target->nodetype) {
1177 case LYS_LEAF:
1178 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1179 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1180
1181 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1182 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1183 break;
1184 case LYS_LEAFLIST:
1185 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1186 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1187 break;
1188 case LYS_CHOICE:
1189 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1190 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1191
1192 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1193 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1194 break;
1195 default:
1196 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1197 }
1198 }
1199
1200cleanup:
1201 return ret;
1202}
1203
1204/**
1205 * @brief Apply deviate replace.
1206 *
1207 * @param[in] ctx Compile context.
1208 * @param[in] d Deviate replace to apply.
1209 * @param[in,out] target Deviation target.
1210 * @return LY_ERR value.
1211 */
1212static LY_ERR
1213lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1214{
1215 LY_ERR ret = LY_SUCCESS;
1216 uint32_t *num;
1217
1218#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1219 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001220 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001221 ret = LY_EVALID; \
1222 goto cleanup; \
1223 }
1224
1225 /* [type-stmt] */
1226 if (d->type) {
1227 switch (target->nodetype) {
1228 case LYS_LEAF:
1229 case LYS_LEAFLIST:
1230 break;
1231 default:
1232 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1233 }
1234
1235 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1236 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1237 }
1238
1239 /* [units-stmt] */
1240 if (d->units) {
1241 switch (target->nodetype) {
1242 case LYS_LEAF:
1243 case LYS_LEAFLIST:
1244 break;
1245 default:
1246 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1247 }
1248
1249 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
1250 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1251 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1252 }
1253
1254 /* [default-stmt] */
1255 if (d->dflt.str) {
1256 switch (target->nodetype) {
1257 case LYS_LEAF:
1258 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1259
1260 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1261 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1262 break;
1263 case LYS_CHOICE:
1264 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1265
1266 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1267 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1268 break;
1269 default:
1270 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1271 }
1272 }
1273
1274 /* [config-stmt] */
1275 if (d->flags & LYS_CONFIG_MASK) {
1276 switch (target->nodetype) {
1277 case LYS_CONTAINER:
1278 case LYS_LEAF:
1279 case LYS_LEAFLIST:
1280 case LYS_LIST:
1281 case LYS_CHOICE:
1282 case LYS_ANYDATA:
1283 case LYS_ANYXML:
1284 break;
1285 default:
1286 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1287 }
1288
1289 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001290 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1291 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001292 ret = LY_EVALID;
1293 goto cleanup;
1294 }
1295
1296 target->flags &= ~LYS_CONFIG_MASK;
1297 target->flags |= d->flags & LYS_CONFIG_MASK;
1298 }
1299
1300 /* [mandatory-stmt] */
1301 if (d->flags & LYS_MAND_MASK) {
1302 switch (target->nodetype) {
1303 case LYS_LEAF:
1304 case LYS_CHOICE:
1305 case LYS_ANYDATA:
1306 case LYS_ANYXML:
1307 break;
1308 default:
1309 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1310 }
1311
1312 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001313 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1314 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001315 ret = LY_EVALID;
1316 goto cleanup;
1317 }
1318
1319 target->flags &= ~LYS_MAND_MASK;
1320 target->flags |= d->flags & LYS_MAND_MASK;
1321 }
1322
1323 /* [min-elements-stmt] */
1324 if (d->flags & LYS_SET_MIN) {
1325 switch (target->nodetype) {
1326 case LYS_LEAFLIST:
1327 num = &((struct lysp_node_leaflist *)target)->min;
1328 break;
1329 case LYS_LIST:
1330 num = &((struct lysp_node_list *)target)->min;
1331 break;
1332 default:
1333 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1334 }
1335
1336 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001337 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001338 ret = LY_EVALID;
1339 goto cleanup;
1340 }
1341
1342 *num = d->min;
1343 }
1344
1345 /* [max-elements-stmt] */
1346 if (d->flags & LYS_SET_MAX) {
1347 switch (target->nodetype) {
1348 case LYS_LEAFLIST:
1349 num = &((struct lysp_node_leaflist *)target)->max;
1350 break;
1351 case LYS_LIST:
1352 num = &((struct lysp_node_list *)target)->max;
1353 break;
1354 default:
1355 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1356 }
1357
1358 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001359 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001360 ret = LY_EVALID;
1361 goto cleanup;
1362 }
1363
1364 *num = d->max;
1365 }
1366
1367cleanup:
1368 return ret;
1369}
1370
1371/**
1372 * @brief Get module of a single nodeid node name test.
1373 *
1374 * @param[in] ctx libyang context.
1375 * @param[in] nametest Nametest with an optional prefix.
1376 * @param[in] nametest_len Length of @p nametest.
1377 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1378 * @param[out] name Optional pointer to the name test without the prefix.
1379 * @param[out] name_len Length of @p name.
1380 * @return Resolved module.
1381 */
1382static const struct lys_module *
1383lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1384 const struct lysp_module *mod, const char **name, size_t *name_len)
1385{
1386 const struct lys_module *target_mod;
1387 const char *ptr;
1388
1389 ptr = ly_strnchr(nametest, ':', nametest_len);
1390 if (ptr) {
1391 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)mod);
1392 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001393 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001394 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
1395 nametest_len, nametest, ptr - nametest, nametest, LYSP_MODULE_NAME(mod));
1396 return NULL;
1397 }
1398
1399 if (name) {
1400 *name = ptr + 1;
1401 *name_len = nametest_len - ((ptr - nametest) + 1);
1402 }
1403 } else {
1404 target_mod = mod->mod;
1405 if (name) {
1406 *name = nametest;
1407 *name_len = nametest_len;
1408 }
1409 }
1410
1411 return target_mod;
1412}
1413
1414/**
1415 * @brief Check whether a parsed node matches a single schema nodeid name test.
1416 *
1417 * @param[in] pnode Parsed node to consider.
1418 * @param[in] pnode_mod Compiled @p pnode to-be module.
1419 * @param[in] mod Expected module.
1420 * @param[in] name Expected name.
1421 * @param[in] name_len Length of @p name.
1422 * @return Whether it is a match or not.
1423 */
1424static ly_bool
1425lysp_schema_nodeid_match_pnode(const struct lysp_node *pnode, const struct lys_module *pnode_mod,
1426 const struct lys_module *mod, const char *name, size_t name_len)
1427{
1428 const char *pname;
1429
1430 /* compare with the module of the node */
1431 if (pnode_mod != mod) {
1432 return 0;
1433 }
1434
1435 /* compare names */
1436 if (pnode->nodetype & (LYS_ACTION | LYS_RPC)) {
1437 pname = ((struct lysp_action *)pnode)->name;
1438 } else if (pnode->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1439 pname = (pnode->nodetype & LYS_INPUT) ? "input" : "output";
1440 } else {
1441 pname = pnode->name;
1442 }
1443 if (ly_strncmp(pname, name, name_len)) {
1444 return 0;
1445 }
1446
1447 return 1;
1448}
1449
1450/**
1451 * @brief Check whether a compiled node matches a single schema nodeid name test.
1452 *
1453 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1454 * @param[in] mod Expected module.
1455 * @param[in] name Expected name.
1456 * @param[in] name_len Length of @p name.
1457 * @return Whether it is a match or not.
1458 */
1459static ly_bool
1460lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1461 size_t name_len)
1462{
1463 const struct lys_module *node_mod;
1464 const char *node_name;
1465
1466 /* compare with the module of the node */
Michal Vasko2a668712020-10-21 11:48:09 +02001467 if ((*node)->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
Michal Vasko208a04a2020-10-21 15:17:12 +02001468 node_mod = lysc_node_parent_full(*node)->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001469 } else {
1470 node_mod = (*node)->module;
1471 }
1472 if (node_mod != mod) {
1473 return 0;
1474 }
1475
1476 /* compare names */
1477 if ((*node)->nodetype == LYS_INPUT) {
1478 node_name = "input";
1479 } else if ((*node)->nodetype == LYS_OUTPUT) {
1480 node_name = "output";
1481 } else {
1482 node_name = (*node)->name;
1483 }
1484 if (ly_strncmp(node_name, name, name_len)) {
1485 return 0;
1486 }
1487
Michal Vasko2a668712020-10-21 11:48:09 +02001488 /* move to next parent */
Michal Vasko208a04a2020-10-21 15:17:12 +02001489 *node = lysc_node_parent_full(*node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001490
1491 return 1;
1492}
1493
1494/**
1495 * @brief Check whether a node matches specific schema nodeid.
1496 *
1497 * @param[in] exp Parsed nodeid to match.
1498 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1499 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1500 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1501 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1502 * @param[in] pnode_mod Compiled @p pnode to-be module.
1503 * @return Whether it is a match or not.
1504 */
1505static ly_bool
1506lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1507 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1508{
1509 uint32_t i;
1510 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001511 const char *name = NULL;
1512 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001513
1514 /* compare last node in the node ID */
1515 i = exp->used - 1;
1516
1517 /* get exp node ID module */
1518 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);
1519 assert(mod);
1520
1521 if (pnode) {
1522 /* compare on the last parsed-only node */
1523 if (!lysp_schema_nodeid_match_pnode(pnode, pnode_mod, mod, name, name_len)) {
1524 return 0;
1525 }
1526 } else {
1527 /* using parent directly */
1528 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1529 return 0;
1530 }
1531 }
1532
1533 /* now compare all the compiled parents */
1534 while (i > 1) {
1535 i -= 2;
1536 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1537
1538 if (!parent) {
1539 /* no more parents but path continues */
1540 return 0;
1541 }
1542
1543 /* get exp node ID module */
1544 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1545 &name_len);
1546 assert(mod);
1547
1548 /* compare with the parent */
1549 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1550 return 0;
1551 }
1552 }
1553
1554 if (ctx_node && (ctx_node != parent)) {
1555 /* descendant path has not finished in the context node */
1556 return 0;
1557 } else if (!ctx_node && parent) {
1558 /* some parent was not matched */
1559 return 0;
1560 }
1561
1562 return 1;
1563}
1564
1565void
1566lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1567{
1568 if (aug) {
1569 lyxp_expr_free(ctx, aug->nodeid);
1570
1571 free(aug);
1572 }
1573}
1574
1575void
1576lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1577{
1578 if (dev) {
1579 lyxp_expr_free(ctx, dev->nodeid);
1580 LY_ARRAY_FREE(dev->devs);
1581 LY_ARRAY_FREE(dev->dev_pmods);
1582
1583 free(dev);
1584 }
1585}
1586
1587void
1588lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1589{
1590 if (rfn) {
1591 lyxp_expr_free(ctx, rfn->nodeid);
1592 LY_ARRAY_FREE(rfn->rfns);
1593
1594 free(rfn);
1595 }
1596}
1597
1598void
1599lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1600{
1601 if (!dev_pnode) {
1602 return;
1603 }
1604
1605 switch (dev_pnode->nodetype) {
1606 case LYS_CONTAINER:
1607 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1608 break;
1609 case LYS_LIST:
1610 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1611 break;
1612 case LYS_CHOICE:
1613 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1614 break;
1615 case LYS_CASE:
1616 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1617 break;
1618 case LYS_LEAF:
1619 case LYS_LEAFLIST:
1620 case LYS_ANYXML:
1621 case LYS_ANYDATA:
1622 /* no children */
1623 break;
1624 case LYS_NOTIF:
1625 ((struct lysp_notif *)dev_pnode)->data = NULL;
1626 lysp_notif_free((struct ly_ctx *)ctx, (struct lysp_notif *)dev_pnode);
1627 free(dev_pnode);
1628 return;
1629 case LYS_RPC:
1630 case LYS_ACTION:
1631 ((struct lysp_action *)dev_pnode)->input.data = NULL;
1632 ((struct lysp_action *)dev_pnode)->output.data = NULL;
1633 lysp_action_free((struct ly_ctx *)ctx, (struct lysp_action *)dev_pnode);
1634 free(dev_pnode);
1635 return;
1636 case LYS_INPUT:
1637 case LYS_OUTPUT:
1638 ((struct lysp_action_inout *)dev_pnode)->data = NULL;
1639 lysp_action_inout_free((struct ly_ctx *)ctx, (struct lysp_action_inout *)dev_pnode);
1640 free(dev_pnode);
1641 return;
1642 default:
1643 LOGINT(ctx);
1644 return;
1645 }
1646
1647 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1648}
1649
1650LY_ERR
1651lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1652 struct lysp_node **dev_pnode, ly_bool *not_supported)
1653{
1654 LY_ERR ret = LY_SUCCESS;
1655 uint32_t i;
1656 LY_ARRAY_COUNT_TYPE u;
1657 struct lys_module *orig_mod = ctx->cur_mod;
1658 struct lysp_module *orig_pmod = ctx->pmod;
1659 char orig_path[LYSC_CTX_BUFSIZE];
1660 struct lysc_refine *rfn;
1661 struct lysc_deviation *dev;
1662 struct lysp_deviation *dev_p;
1663 struct lysp_deviate *d;
1664
1665 *dev_pnode = NULL;
1666 *not_supported = 0;
1667
1668 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1669 rfn = ctx->uses_rfns.objs[i];
1670
1671 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, ctx->cur_mod)) {
1672 /* not our target node */
1673 continue;
1674 }
1675
1676 if (!*dev_pnode) {
1677 /* first refine on this node, create a copy first */
1678 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1679 }
1680
1681 /* apply all the refines by changing (the copy of) the parsed node */
1682 LY_ARRAY_FOR(rfn->rfns, u) {
1683 /* apply refine, keep the current path and add to it */
1684 lysc_update_path(ctx, NULL, "{refine}");
1685 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
1686 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1687 lysc_update_path(ctx, NULL, NULL);
1688 lysc_update_path(ctx, NULL, NULL);
1689 LY_CHECK_GOTO(ret, cleanup);
1690 }
1691
1692 /* refine was applied, remove it */
1693 lysc_refine_free(ctx->ctx, rfn);
1694 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1695
1696 /* all the refines for one target node are in one structure, we are done */
1697 break;
1698 }
1699
1700 for (i = 0; i < ctx->devs.count; ++i) {
1701 dev = ctx->devs.objs[i];
1702
1703 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, ctx->cur_mod)) {
1704 /* not our target node */
1705 continue;
1706 }
1707
1708 if (dev->not_supported) {
1709 /* it is not supported, no more deviations */
1710 *not_supported = 1;
1711 goto dev_applied;
1712 }
1713
1714 if (!*dev_pnode) {
1715 /* first deviation on this node, create a copy first */
1716 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1717 }
1718
1719 /* apply all the deviates by changing (the copy of) the parsed node */
1720 LY_ARRAY_FOR(dev->devs, u) {
1721 dev_p = dev->devs[u];
1722 LY_LIST_FOR(dev_p->deviates, d) {
1723 /* generate correct path */
1724 strcpy(orig_path, ctx->path);
1725 ctx->path_len = 1;
1726 ctx->cur_mod = dev->dev_pmods[u]->mod;
1727 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1728 lysc_update_path(ctx, NULL, "{deviation}");
1729 lysc_update_path(ctx, NULL, dev_p->nodeid);
1730
1731 switch (d->mod) {
1732 case LYS_DEV_ADD:
1733 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1734 break;
1735 case LYS_DEV_DELETE:
1736 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1737 break;
1738 case LYS_DEV_REPLACE:
1739 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1740 break;
1741 default:
1742 LOGINT(ctx->ctx);
1743 ret = LY_EINT;
1744 }
1745
1746 /* restore previous path */
1747 strcpy(ctx->path, orig_path);
1748 ctx->path_len = strlen(ctx->path);
1749 ctx->cur_mod = orig_mod;
1750 ctx->pmod = orig_pmod;
1751
1752 LY_CHECK_GOTO(ret, cleanup);
1753 }
1754 }
1755
1756dev_applied:
1757 /* deviation was applied, remove it */
1758 lysc_deviation_free(ctx->ctx, dev);
1759 ly_set_rm_index(&ctx->devs, i, NULL);
1760
1761 /* all the deviations for one target node are in one structure, we are done */
1762 break;
1763 }
1764
1765cleanup:
1766 if (ret) {
1767 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1768 *dev_pnode = NULL;
1769 *not_supported = 0;
1770 }
1771 return ret;
1772}
1773
1774/**
1775 * @brief Compile the parsed augment connecting it into its target.
1776 *
1777 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1778 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1779 * are already implemented and compiled.
1780 *
1781 * @param[in] ctx Compile context.
1782 * @param[in] aug_p Parsed augment to compile.
1783 * @param[in] target Target node of the augment.
1784 * @return LY_SUCCESS on success.
1785 * @return LY_EVALID on failure.
1786 */
1787static LY_ERR
1788lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysc_node *target)
1789{
1790 LY_ERR ret = LY_SUCCESS;
1791 struct lysp_node *pnode;
1792 struct lysc_node *node;
1793 struct lysc_when *when_shared = NULL;
1794 struct lysc_action **actions;
1795 struct lysc_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001796 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001797 LY_ARRAY_COUNT_TYPE u;
1798 struct ly_set child_set = {0};
Michal Vasko29dd11e2020-11-23 16:52:22 +01001799 uint32_t i, opt_prev = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001800
1801 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001802 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001803 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1804 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1805 ret = LY_EVALID;
1806 goto cleanup;
1807 }
1808
1809 /* check for mandatory nodes
1810 * - new cases augmenting some choice can have mandatory nodes
1811 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1812 */
1813 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1814 allow_mandatory = 1;
1815 }
1816
1817 LY_LIST_FOR(aug_p->child, pnode) {
1818 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1819 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1820 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1821 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001822 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001823 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1824 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1825 ret = LY_EVALID;
1826 goto cleanup;
1827 }
1828
1829 /* compile the children */
1830 if (target->nodetype == LYS_CHOICE) {
1831 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001832 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1833 if (target->nodetype == LYS_INPUT) {
1834 ctx->options |= LYS_COMPILE_RPC_INPUT;
1835 } else {
1836 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
1837 }
1838 ret = lys_compile_node(ctx, pnode, (struct lysc_node *)lysc_node_parent_full(target), 0, &child_set);
1839 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001840 } else {
1841 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1842 }
1843
Michal Vasko29dd11e2020-11-23 16:52:22 +01001844 /* eval if-features again for the rest of this node processing */
1845 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1846 if (!enabled) {
1847 ctx->options |= LYS_COMPILE_DISABLED;
1848 }
1849
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001850 /* since the augment node is not present in the compiled tree, we need to pass some of its
1851 * statements to all its children */
1852 for (i = 0; i < child_set.count; ++i) {
1853 node = child_set.snodes[i];
1854 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1855 node->flags &= ~LYS_MAND_TRUE;
1856 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001857 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001858 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1859 ret = LY_EVALID;
1860 goto cleanup;
1861 }
1862
1863 if (aug_p->when) {
1864 /* pass augment's when to all the children */
1865 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target), node, &when_shared);
1866 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod9062b02020-11-04 17:15:50 +01001867
1868 if ((node->nodetype == LYS_CONTAINER) && !(node->flags & LYS_PRESENCE)) {
1869 /* container with a when condition */
1870 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its "
1871 "inherited \"when\" condition.", node->name);
1872 node->flags |= LYS_PRESENCE;
1873 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001874 }
1875 }
1876 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001877
1878 /* restore options */
1879 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001880 }
1881
1882 switch (target->nodetype) {
1883 case LYS_CONTAINER:
1884 actions = &((struct lysc_node_container *)target)->actions;
1885 notifs = &((struct lysc_node_container *)target)->notifs;
1886 break;
1887 case LYS_LIST:
1888 actions = &((struct lysc_node_list *)target)->actions;
1889 notifs = &((struct lysc_node_list *)target)->notifs;
1890 break;
1891 default:
1892 actions = NULL;
1893 notifs = NULL;
1894 break;
1895 }
1896
1897 if (aug_p->actions) {
1898 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001899 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001900 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
1901 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
1902 ret = LY_EVALID;
1903 goto cleanup;
1904 }
1905
1906 /* compile actions into the target */
Michal Vasko5347e3a2020-11-03 17:14:57 +01001907 COMPILE_OP_ARRAY_GOTO(ctx, aug_p->actions, *actions, target, lys_compile_action, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001908
1909 if (aug_p->when) {
1910 /* inherit when */
1911 LY_ARRAY_FOR(*actions, u) {
1912 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target),
1913 (struct lysc_node *)&(*actions)[u], &when_shared);
1914 LY_CHECK_GOTO(ret, cleanup);
1915 }
1916 }
1917 }
1918 if (aug_p->notifs) {
1919 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001920 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001921 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
1922 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
1923 ret = LY_EVALID;
1924 goto cleanup;
1925 }
1926
1927 /* compile notifications into the target */
Michal Vasko5347e3a2020-11-03 17:14:57 +01001928 COMPILE_OP_ARRAY_GOTO(ctx, aug_p->notifs, *notifs, target, lys_compile_notif, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001929
1930 if (aug_p->when) {
1931 /* inherit when */
1932 LY_ARRAY_FOR(*notifs, u) {
1933 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target),
1934 (struct lysc_node *)&(*notifs)[u], &when_shared);
1935 LY_CHECK_GOTO(ret, cleanup);
1936 }
1937 }
1938 }
1939
1940cleanup:
1941 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001942 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001943 return ret;
1944}
1945
1946LY_ERR
1947lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1948{
1949 LY_ERR ret = LY_SUCCESS;
1950 struct lys_module *orig_mod = ctx->cur_mod;
1951 struct lysp_module *orig_pmod = ctx->pmod;
1952 uint32_t i;
1953 char orig_path[LYSC_CTX_BUFSIZE];
1954 struct lysc_augment *aug;
1955
1956 /* uses augments */
1957 for (i = 0; i < ctx->uses_augs.count; ) {
1958 aug = ctx->uses_augs.objs[i];
1959
1960 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, aug->nodeid_ctx_node, node, NULL, NULL)) {
1961 /* not our target node */
1962 ++i;
1963 continue;
1964 }
1965
1966 /* apply augment, keep the current path and add to it */
1967 lysc_update_path(ctx, NULL, "{augment}");
1968 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
1969 ret = lys_compile_augment(ctx, aug->aug_p, node);
1970 lysc_update_path(ctx, NULL, NULL);
1971 lysc_update_path(ctx, NULL, NULL);
1972 LY_CHECK_GOTO(ret, cleanup);
1973
1974 /* augment was applied, remove it (index may have changed because other augments could have been applied) */
1975 ly_set_rm(&ctx->uses_augs, aug, NULL);
1976 lysc_augment_free(ctx->ctx, aug);
1977 }
1978
1979 /* top-level augments */
1980 for (i = 0; i < ctx->augs.count; ) {
1981 aug = ctx->augs.objs[i];
1982
1983 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, NULL, node, NULL, NULL)) {
1984 /* not our target node */
1985 ++i;
1986 continue;
1987 }
1988
1989 /* apply augment, use the path and modules from the augment */
1990 strcpy(orig_path, ctx->path);
1991 ctx->path_len = 1;
1992 lysc_update_path(ctx, NULL, "{augment}");
1993 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
1994 ctx->cur_mod = aug->nodeid_pmod->mod;
1995 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
1996 ret = lys_compile_augment(ctx, aug->aug_p, node);
1997 strcpy(ctx->path, orig_path);
1998 ctx->path_len = strlen(ctx->path);
1999 LY_CHECK_GOTO(ret, cleanup);
2000
2001 /* augment was applied, remove it */
2002 ly_set_rm(&ctx->augs, aug, NULL);
2003 lysc_augment_free(ctx->ctx, aug);
2004 }
2005
2006cleanup:
2007 ctx->cur_mod = orig_mod;
2008 ctx->pmod = orig_pmod;
2009 return ret;
2010}
2011
2012/**
2013 * @brief Prepare a top-level augment to be applied during data nodes compilation.
2014 *
2015 * @param[in] ctx Compile context.
2016 * @param[in] aug_p Parsed augment to be applied.
2017 * @param[in] pmod Both current and prefix module for @p aug_p.
2018 * @return LY_ERR value.
2019 */
2020static LY_ERR
2021lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysp_module *pmod)
2022{
2023 LY_ERR ret = LY_SUCCESS;
2024 struct lyxp_expr *exp = NULL;
2025 struct lysc_augment *aug;
2026 const struct lys_module *mod;
2027
2028 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2029 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
2030 LY_CHECK_GOTO(ret, cleanup);
2031
2032 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2033 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2034 if (mod != ctx->cur_mod) {
2035 /* augment for another module, ignore */
2036 goto cleanup;
2037 }
2038
2039 /* allocate new compiled augment and store it in the set */
2040 aug = calloc(1, sizeof *aug);
2041 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2042 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
2043
2044 aug->nodeid = exp;
2045 exp = NULL;
2046 aug->nodeid_pmod = pmod;
2047 aug->aug_p = aug_p;
2048
2049cleanup:
2050 lyxp_expr_free(ctx->ctx, exp);
2051 return ret;
2052}
2053
2054LY_ERR
2055lys_precompile_own_augments(struct lysc_ctx *ctx)
2056{
2057 LY_ARRAY_COUNT_TYPE u, v, w;
2058 const struct lys_module *aug_mod;
2059
2060 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
2061 aug_mod = ctx->cur_mod->augmented_by[u];
2062
2063 /* collect all module augments */
2064 LY_ARRAY_FOR(aug_mod->parsed->augments, v) {
2065 LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->augments[v], aug_mod->parsed));
2066 }
2067
2068 /* collect all submodules augments */
2069 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
2070 LY_ARRAY_FOR(aug_mod->parsed->includes[v].submodule->augments, w) {
2071 LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->includes[v].submodule->augments[w],
2072 (struct lysp_module *)aug_mod->parsed->includes[v].submodule));
2073 }
2074 }
2075 }
2076
2077 return LY_SUCCESS;
2078}
2079
2080/**
2081 * @brief Prepare a deviation to be applied during data nodes compilation.
2082 *
2083 * @param[in] ctx Compile context.
2084 * @param[in] dev_p Parsed deviation to be applied.
2085 * @param[in] pmod Both current and prefix module for @p dev_p.
2086 * @return LY_ERR value.
2087 */
2088static LY_ERR
2089lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2090{
2091 LY_ERR ret = LY_SUCCESS;
2092 struct lysc_deviation *dev = NULL;
2093 struct lyxp_expr *exp = NULL;
2094 struct lysp_deviation **new_dev;
2095 const struct lys_module *mod;
2096 const struct lysp_module **new_dev_pmod;
2097 uint32_t i;
2098
2099 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2100 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2101 LY_CHECK_GOTO(ret, cleanup);
2102
2103 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2104 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2105 if (mod != ctx->cur_mod) {
2106 /* deviation for another module, ignore */
2107 goto cleanup;
2108 }
2109
2110 /* try to find the node in already compiled deviations */
2111 for (i = 0; i < ctx->devs.count; ++i) {
2112 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2113 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2114 dev = ctx->devs.objs[i];
2115 break;
2116 }
2117 }
2118
2119 if (!dev) {
2120 /* allocate new compiled deviation */
2121 dev = calloc(1, sizeof *dev);
2122 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2123 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2124
2125 dev->nodeid = exp;
2126 exp = NULL;
2127 }
2128
2129 /* add new parsed deviation structure */
2130 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2131 *new_dev = dev_p;
2132 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2133 *new_dev_pmod = pmod;
2134
2135cleanup:
2136 lyxp_expr_free(ctx->ctx, exp);
2137 return ret;
2138}
2139
2140LY_ERR
2141lys_precompile_own_deviations(struct lysc_ctx *ctx)
2142{
2143 LY_ARRAY_COUNT_TYPE u, v, w;
2144 const struct lys_module *dev_mod;
2145 struct lysc_deviation *dev;
2146 struct lysp_deviate *d;
2147 int not_supported;
2148 uint32_t i;
2149
2150 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2151 dev_mod = ctx->cur_mod->deviated_by[u];
2152
2153 /* compile all module deviations */
2154 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2155 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2156 }
2157
2158 /* compile all submodules deviations */
2159 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2160 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2161 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2162 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2163 }
2164 }
2165 }
2166
2167 /* set not-supported flags for all the deviations */
2168 for (i = 0; i < ctx->devs.count; ++i) {
2169 dev = ctx->devs.objs[i];
2170 not_supported = 0;
2171
2172 LY_ARRAY_FOR(dev->devs, u) {
2173 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2174 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2175 not_supported = 1;
2176 break;
2177 }
2178 }
2179 if (not_supported) {
2180 break;
2181 }
2182 }
2183 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002184 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002185 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
2186 return LY_EVALID;
2187 }
2188
2189 dev->not_supported = not_supported;
2190 }
2191
2192 return LY_SUCCESS;
2193}
2194
2195/**
2196 * @brief Add a module reference into an array, checks for duplicities.
2197 *
2198 * @param[in] ctx Compile context.
2199 * @param[in] mod Module reference to add.
2200 * @param[in,out] mod_array Module sized array to add to.
2201 * @return LY_ERR value.
2202 */
2203static LY_ERR
2204lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2205{
2206 LY_ARRAY_COUNT_TYPE u;
2207 struct lys_module **new_mod;
2208
2209 LY_ARRAY_FOR(*mod_array, u) {
2210 if ((*mod_array)[u] == mod) {
2211 /* already there */
2212 return LY_EEXIST;
2213 }
2214 }
2215
2216 /* add the new module ref */
2217 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2218 *new_mod = mod;
2219
2220 return LY_SUCCESS;
2221}
2222
2223LY_ERR
2224lys_precompile_augments_deviations(struct lysc_ctx *ctx)
2225{
Michal Vasko405cc9e2020-12-01 12:01:27 +01002226 LY_ERR ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002227 LY_ARRAY_COUNT_TYPE u, v;
2228 const struct lysp_module *mod_p;
2229 const struct lysc_node *target;
2230 struct lys_module *mod;
2231 struct lysp_submodule *submod;
2232 ly_bool has_dev = 0;
2233 uint16_t flags;
2234 uint32_t idx, opt_prev = ctx->options;
2235
Michal Vasko405cc9e2020-12-01 12:01:27 +01002236 for (idx = 0; idx < ctx->unres->implementing.count; ++idx) {
2237 if (ctx->cur_mod == ctx->unres->implementing.objs[idx]) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002238 break;
2239 }
2240 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01002241 if (idx == ctx->unres->implementing.count) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002242 /* it was already implemented and all the augments and deviations fully applied */
2243 return LY_SUCCESS;
2244 }
2245
2246 mod_p = ctx->cur_mod->parsed;
2247
2248 LY_ARRAY_FOR(mod_p->augments, u) {
2249 lysc_update_path(ctx, NULL, "{augment}");
2250 lysc_update_path(ctx, NULL, mod_p->augments[u].nodeid);
2251
2252 /* get target module */
2253 ret = lys_nodeid_check(ctx, mod_p->augments[u].nodeid, 1, &mod, NULL);
2254 LY_CHECK_RET(ret);
2255
2256 /* add this module into the target module augmented_by, if not there already from previous augments */
2257 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2258
2259 /* if we are compiling this module, we cannot add augments to it yet */
2260 if (mod != ctx->cur_mod) {
2261 /* apply the augment, find the target node first */
2262 flags = 0;
2263 ret = lysc_resolve_schema_nodeid(ctx, mod_p->augments[u].nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
2264 (void *)mod_p, 0, &target, &flags);
2265 LY_CHECK_RET(ret);
2266
2267 /* apply the augment */
2268 ctx->options |= flags;
2269 ret = lys_compile_augment(ctx, &mod_p->augments[u], (struct lysc_node *)target);
2270 ctx->options = opt_prev;
2271 LY_CHECK_RET(ret);
2272 }
2273
2274 lysc_update_path(ctx, NULL, NULL);
2275 lysc_update_path(ctx, NULL, NULL);
2276 }
2277
2278 LY_ARRAY_FOR(mod_p->deviations, u) {
2279 /* get target module */
2280 lysc_update_path(ctx, NULL, "{deviation}");
2281 lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid);
2282 ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL);
2283 lysc_update_path(ctx, NULL, NULL);
2284 lysc_update_path(ctx, NULL, NULL);
2285 LY_CHECK_RET(ret);
2286
2287 /* add this module into the target module deviated_by, if not there already from previous deviations */
2288 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2289
2290 /* new deviation added to the target module */
2291 has_dev = 1;
2292 }
2293
2294 /* the same for augments and deviations in submodules */
2295 LY_ARRAY_FOR(mod_p->includes, v) {
2296 submod = mod_p->includes[v].submodule;
2297 LY_ARRAY_FOR(submod->augments, u) {
2298 lysc_update_path(ctx, NULL, "{augment}");
2299 lysc_update_path(ctx, NULL, submod->augments[u].nodeid);
2300
2301 ret = lys_nodeid_check(ctx, submod->augments[u].nodeid, 1, &mod, NULL);
2302 LY_CHECK_RET(ret);
2303
2304 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2305 if (mod != ctx->cur_mod) {
2306 flags = 0;
2307 ret = lysc_resolve_schema_nodeid(ctx, mod_p->augments[u].nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
2308 submod, 0, &target, &flags);
2309 LY_CHECK_RET(ret);
2310
2311 ctx->options |= flags;
2312 ret = lys_compile_augment(ctx, &submod->augments[u], (struct lysc_node *)target);
2313 ctx->options = opt_prev;
2314 LY_CHECK_RET(ret);
2315 }
2316
2317 lysc_update_path(ctx, NULL, NULL);
2318 lysc_update_path(ctx, NULL, NULL);
2319 }
2320
2321 LY_ARRAY_FOR(submod->deviations, u) {
2322 lysc_update_path(ctx, NULL, "{deviation}");
2323 lysc_update_path(ctx, NULL, submod->deviations[u].nodeid);
2324 ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL);
2325 lysc_update_path(ctx, NULL, NULL);
2326 lysc_update_path(ctx, NULL, NULL);
2327 LY_CHECK_RET(ret);
2328
2329 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2330 has_dev = 1;
2331 }
2332 }
2333
Michal Vasko405cc9e2020-12-01 12:01:27 +01002334 if (has_dev) {
2335 /* all modules (may) need to be recompiled */
2336 ctx->unres->recompile = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002337 }
2338
Michal Vasko405cc9e2020-12-01 12:01:27 +01002339 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002340}
2341
2342void
2343lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2344{
2345 uint32_t i;
2346 LY_ARRAY_COUNT_TYPE u, count;
2347 struct lys_module *m;
2348
2349 for (i = 0; i < ctx->list.count; ++i) {
2350 m = ctx->list.objs[i];
2351
2352 if (m->augmented_by) {
2353 count = LY_ARRAY_COUNT(m->augmented_by);
2354 for (u = 0; u < count; ++u) {
2355 if (m->augmented_by[u] == mod) {
2356 /* keep the order */
2357 if (u < count - 1) {
2358 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2359 }
2360 LY_ARRAY_DECREMENT(m->augmented_by);
2361 break;
2362 }
2363 }
2364 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2365 LY_ARRAY_FREE(m->augmented_by);
2366 m->augmented_by = NULL;
2367 }
2368 }
2369
2370 if (m->deviated_by) {
2371 count = LY_ARRAY_COUNT(m->deviated_by);
2372 for (u = 0; u < count; ++u) {
2373 if (m->deviated_by[u] == mod) {
2374 /* keep the order */
2375 if (u < count - 1) {
2376 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2377 }
2378 LY_ARRAY_DECREMENT(m->deviated_by);
2379 break;
2380 }
2381 }
2382 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2383 LY_ARRAY_FREE(m->deviated_by);
2384 m->deviated_by = NULL;
2385 }
2386 }
2387 }
2388}