blob: 8093d86588ff67dbefb5b352c9aacda68a7fa096 [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) {
54 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
55 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) {
66 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
67 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) {
77 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
78 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) {
82 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
83 "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) {
87 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
88 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) \
655 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s of %s node - it is not possible to %s \"%s\" property.", \
656 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) { \
662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid %s of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
663 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) {
696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
697 "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) { \
873 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
874 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
875 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
876 ret = LY_EVALID; \
877 goto cleanup; \
878 }
879
880 /* [units-stmt] */
881 if (d->units) {
882 switch (target->nodetype) {
883 case LYS_LEAF:
884 case LYS_LEAFLIST:
885 break;
886 default:
887 AMEND_WRONG_NODETYPE("deviation", "add", "units");
888 }
889
890 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
891 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
892 }
893
894 /* *must-stmt */
895 if (d->musts) {
896 switch (target->nodetype) {
897 case LYS_CONTAINER:
898 case LYS_LIST:
899 case LYS_LEAF:
900 case LYS_LEAFLIST:
901 case LYS_ANYDATA:
902 case LYS_ANYXML:
903 musts = &((struct lysp_node_container *)target)->musts;
904 break;
905 case LYS_NOTIF:
906 musts = &((struct lysp_notif *)target)->musts;
907 break;
908 case LYS_INPUT:
909 case LYS_OUTPUT:
910 musts = &((struct lysp_action_inout *)target)->musts;
911 break;
912 default:
913 AMEND_WRONG_NODETYPE("deviation", "add", "must");
914 }
915
916 LY_ARRAY_FOR(d->musts, u) {
917 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
918 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
919 }
920 }
921
922 /* *unique-stmt */
923 if (d->uniques) {
924 switch (target->nodetype) {
925 case LYS_LIST:
926 break;
927 default:
928 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
929 }
930
931 LY_ARRAY_FOR(d->uniques, u) {
932 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
933 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
934 }
935 }
936
937 /* *default-stmt */
938 if (d->dflts) {
939 switch (target->nodetype) {
940 case LYS_LEAF:
941 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
942 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
943
944 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
945 break;
946 case LYS_LEAFLIST:
947 LY_ARRAY_FOR(d->dflts, u) {
948 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
949 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
950 }
951 break;
952 case LYS_CHOICE:
953 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
954 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
955
956 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
957 break;
958 default:
959 AMEND_WRONG_NODETYPE("deviation", "add", "default");
960 }
961 }
962
963 /* [config-stmt] */
964 if (d->flags & LYS_CONFIG_MASK) {
965 switch (target->nodetype) {
966 case LYS_CONTAINER:
967 case LYS_LEAF:
968 case LYS_LEAFLIST:
969 case LYS_LIST:
970 case LYS_CHOICE:
971 case LYS_ANYDATA:
972 case LYS_ANYXML:
973 break;
974 default:
975 AMEND_WRONG_NODETYPE("deviation", "add", "config");
976 }
977
978 if (target->flags & LYS_CONFIG_MASK) {
979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
980 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
981 target->flags & LYS_CONFIG_W ? "true" : "false");
982 ret = LY_EVALID;
983 goto cleanup;
984 }
985
986 target->flags |= d->flags & LYS_CONFIG_MASK;
987 }
988
989 /* [mandatory-stmt] */
990 if (d->flags & LYS_MAND_MASK) {
991 switch (target->nodetype) {
992 case LYS_LEAF:
993 case LYS_CHOICE:
994 case LYS_ANYDATA:
995 case LYS_ANYXML:
996 break;
997 default:
998 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
999 }
1000
1001 if (target->flags & LYS_MAND_MASK) {
1002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1003 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
1004 target->flags & LYS_MAND_TRUE ? "true" : "false");
1005 ret = LY_EVALID;
1006 goto cleanup;
1007 }
1008
1009 target->flags |= d->flags & LYS_MAND_MASK;
1010 }
1011
1012 /* [min-elements-stmt] */
1013 if (d->flags & LYS_SET_MIN) {
1014 switch (target->nodetype) {
1015 case LYS_LEAFLIST:
1016 num = &((struct lysp_node_leaflist *)target)->min;
1017 break;
1018 case LYS_LIST:
1019 num = &((struct lysp_node_list *)target)->min;
1020 break;
1021 default:
1022 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
1023 }
1024
1025 if (target->flags & LYS_SET_MIN) {
1026 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1027 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
1028 ret = LY_EVALID;
1029 goto cleanup;
1030 }
1031
1032 *num = d->min;
1033 }
1034
1035 /* [max-elements-stmt] */
1036 if (d->flags & LYS_SET_MAX) {
1037 switch (target->nodetype) {
1038 case LYS_LEAFLIST:
1039 num = &((struct lysp_node_leaflist *)target)->max;
1040 break;
1041 case LYS_LIST:
1042 num = &((struct lysp_node_list *)target)->max;
1043 break;
1044 default:
1045 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1046 }
1047
1048 if (target->flags & LYS_SET_MAX) {
1049 if (*num) {
1050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1051 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1052 *num);
1053 } else {
1054 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1055 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1056 }
1057 ret = LY_EVALID;
1058 goto cleanup;
1059 }
1060
1061 *num = d->max;
1062 }
1063
1064cleanup:
1065 return ret;
1066}
1067
1068/**
1069 * @brief Apply deviate delete.
1070 *
1071 * @param[in] ctx Compile context.
1072 * @param[in] d Deviate delete to apply.
1073 * @param[in,out] target Deviation target.
1074 * @return LY_ERR value.
1075 */
1076static LY_ERR
1077lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1078{
1079 LY_ERR ret = LY_SUCCESS;
1080 struct lysp_restr **musts;
1081 LY_ARRAY_COUNT_TYPE u, v;
1082 struct lysp_qname **uniques, **dflts;
1083
1084#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1085 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1086 int found = 0; \
1087 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1088 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1089 found = 1; \
1090 break; \
1091 } \
1092 } \
1093 if (!found) { \
1094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
1095 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1096 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1097 ret = LY_EVALID; \
1098 goto cleanup; \
1099 } \
1100 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1101 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
1102 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1103 } \
1104 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1105 LY_ARRAY_FREE(ORIG_ARRAY); \
1106 ORIG_ARRAY = NULL; \
1107 }
1108
1109#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1110 if (!((TYPE)target)->MEMBER) { \
1111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
1112 ret = LY_EVALID; \
1113 goto cleanup; \
1114 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
1115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
1116 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1117 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1118 ret = LY_EVALID; \
1119 goto cleanup; \
1120 }
1121
1122 /* [units-stmt] */
1123 if (d->units) {
1124 switch (target->nodetype) {
1125 case LYS_LEAF:
1126 case LYS_LEAFLIST:
1127 break;
1128 default:
1129 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1130 }
1131
1132 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
1133 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1134 ((struct lysp_node_leaf *)target)->units = NULL;
1135 }
1136
1137 /* *must-stmt */
1138 if (d->musts) {
1139 switch (target->nodetype) {
1140 case LYS_CONTAINER:
1141 case LYS_LIST:
1142 case LYS_LEAF:
1143 case LYS_LEAFLIST:
1144 case LYS_ANYDATA:
1145 case LYS_ANYXML:
1146 musts = &((struct lysp_node_container *)target)->musts;
1147 break;
1148 case LYS_NOTIF:
1149 musts = &((struct lysp_notif *)target)->musts;
1150 break;
1151 case LYS_INPUT:
1152 case LYS_OUTPUT:
1153 musts = &((struct lysp_action_inout *)target)->musts;
1154 break;
1155 default:
1156 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1157 }
1158
1159 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1160 }
1161
1162 /* *unique-stmt */
1163 if (d->uniques) {
1164 switch (target->nodetype) {
1165 case LYS_LIST:
1166 break;
1167 default:
1168 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1169 }
1170
1171 uniques = &((struct lysp_node_list *)target)->uniques;
1172 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1173 }
1174
1175 /* *default-stmt */
1176 if (d->dflts) {
1177 switch (target->nodetype) {
1178 case LYS_LEAF:
1179 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1180 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1181
1182 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1183 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1184 break;
1185 case LYS_LEAFLIST:
1186 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1187 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1188 break;
1189 case LYS_CHOICE:
1190 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1191 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1192
1193 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1194 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1195 break;
1196 default:
1197 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1198 }
1199 }
1200
1201cleanup:
1202 return ret;
1203}
1204
1205/**
1206 * @brief Apply deviate replace.
1207 *
1208 * @param[in] ctx Compile context.
1209 * @param[in] d Deviate replace to apply.
1210 * @param[in,out] target Deviation target.
1211 * @return LY_ERR value.
1212 */
1213static LY_ERR
1214lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1215{
1216 LY_ERR ret = LY_SUCCESS;
1217 uint32_t *num;
1218
1219#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1220 if (!((TYPE)target)->MEMBER) { \
1221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
1222 ret = LY_EVALID; \
1223 goto cleanup; \
1224 }
1225
1226 /* [type-stmt] */
1227 if (d->type) {
1228 switch (target->nodetype) {
1229 case LYS_LEAF:
1230 case LYS_LEAFLIST:
1231 break;
1232 default:
1233 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1234 }
1235
1236 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1237 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1238 }
1239
1240 /* [units-stmt] */
1241 if (d->units) {
1242 switch (target->nodetype) {
1243 case LYS_LEAF:
1244 case LYS_LEAFLIST:
1245 break;
1246 default:
1247 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1248 }
1249
1250 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
1251 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1252 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1253 }
1254
1255 /* [default-stmt] */
1256 if (d->dflt.str) {
1257 switch (target->nodetype) {
1258 case LYS_LEAF:
1259 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1260
1261 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1262 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1263 break;
1264 case LYS_CHOICE:
1265 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1266
1267 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1268 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1269 break;
1270 default:
1271 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1272 }
1273 }
1274
1275 /* [config-stmt] */
1276 if (d->flags & LYS_CONFIG_MASK) {
1277 switch (target->nodetype) {
1278 case LYS_CONTAINER:
1279 case LYS_LEAF:
1280 case LYS_LEAFLIST:
1281 case LYS_LIST:
1282 case LYS_CHOICE:
1283 case LYS_ANYDATA:
1284 case LYS_ANYXML:
1285 break;
1286 default:
1287 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1288 }
1289
1290 if (!(target->flags & LYS_CONFIG_MASK)) {
1291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
1292 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
1293 ret = LY_EVALID;
1294 goto cleanup;
1295 }
1296
1297 target->flags &= ~LYS_CONFIG_MASK;
1298 target->flags |= d->flags & LYS_CONFIG_MASK;
1299 }
1300
1301 /* [mandatory-stmt] */
1302 if (d->flags & LYS_MAND_MASK) {
1303 switch (target->nodetype) {
1304 case LYS_LEAF:
1305 case LYS_CHOICE:
1306 case LYS_ANYDATA:
1307 case LYS_ANYXML:
1308 break;
1309 default:
1310 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1311 }
1312
1313 if (!(target->flags & LYS_MAND_MASK)) {
1314 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
1315 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
1316 ret = LY_EVALID;
1317 goto cleanup;
1318 }
1319
1320 target->flags &= ~LYS_MAND_MASK;
1321 target->flags |= d->flags & LYS_MAND_MASK;
1322 }
1323
1324 /* [min-elements-stmt] */
1325 if (d->flags & LYS_SET_MIN) {
1326 switch (target->nodetype) {
1327 case LYS_LEAFLIST:
1328 num = &((struct lysp_node_leaflist *)target)->min;
1329 break;
1330 case LYS_LIST:
1331 num = &((struct lysp_node_list *)target)->min;
1332 break;
1333 default:
1334 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1335 }
1336
1337 if (!(target->flags & LYS_SET_MIN)) {
1338 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1339 "Invalid deviation replacing \"min-elements\" property which is not present.");
1340 ret = LY_EVALID;
1341 goto cleanup;
1342 }
1343
1344 *num = d->min;
1345 }
1346
1347 /* [max-elements-stmt] */
1348 if (d->flags & LYS_SET_MAX) {
1349 switch (target->nodetype) {
1350 case LYS_LEAFLIST:
1351 num = &((struct lysp_node_leaflist *)target)->max;
1352 break;
1353 case LYS_LIST:
1354 num = &((struct lysp_node_list *)target)->max;
1355 break;
1356 default:
1357 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1358 }
1359
1360 if (!(target->flags & LYS_SET_MAX)) {
1361 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1362 "Invalid deviation replacing \"max-elements\" property which is not present.");
1363 ret = LY_EVALID;
1364 goto cleanup;
1365 }
1366
1367 *num = d->max;
1368 }
1369
1370cleanup:
1371 return ret;
1372}
1373
1374/**
1375 * @brief Get module of a single nodeid node name test.
1376 *
1377 * @param[in] ctx libyang context.
1378 * @param[in] nametest Nametest with an optional prefix.
1379 * @param[in] nametest_len Length of @p nametest.
1380 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1381 * @param[out] name Optional pointer to the name test without the prefix.
1382 * @param[out] name_len Length of @p name.
1383 * @return Resolved module.
1384 */
1385static const struct lys_module *
1386lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1387 const struct lysp_module *mod, const char **name, size_t *name_len)
1388{
1389 const struct lys_module *target_mod;
1390 const char *ptr;
1391
1392 ptr = ly_strnchr(nametest, ':', nametest_len);
1393 if (ptr) {
1394 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)mod);
1395 if (!target_mod) {
1396 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
1397 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
1398 nametest_len, nametest, ptr - nametest, nametest, LYSP_MODULE_NAME(mod));
1399 return NULL;
1400 }
1401
1402 if (name) {
1403 *name = ptr + 1;
1404 *name_len = nametest_len - ((ptr - nametest) + 1);
1405 }
1406 } else {
1407 target_mod = mod->mod;
1408 if (name) {
1409 *name = nametest;
1410 *name_len = nametest_len;
1411 }
1412 }
1413
1414 return target_mod;
1415}
1416
1417/**
1418 * @brief Check whether a parsed node matches a single schema nodeid name test.
1419 *
1420 * @param[in] pnode Parsed node to consider.
1421 * @param[in] pnode_mod Compiled @p pnode to-be module.
1422 * @param[in] mod Expected module.
1423 * @param[in] name Expected name.
1424 * @param[in] name_len Length of @p name.
1425 * @return Whether it is a match or not.
1426 */
1427static ly_bool
1428lysp_schema_nodeid_match_pnode(const struct lysp_node *pnode, const struct lys_module *pnode_mod,
1429 const struct lys_module *mod, const char *name, size_t name_len)
1430{
1431 const char *pname;
1432
1433 /* compare with the module of the node */
1434 if (pnode_mod != mod) {
1435 return 0;
1436 }
1437
1438 /* compare names */
1439 if (pnode->nodetype & (LYS_ACTION | LYS_RPC)) {
1440 pname = ((struct lysp_action *)pnode)->name;
1441 } else if (pnode->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1442 pname = (pnode->nodetype & LYS_INPUT) ? "input" : "output";
1443 } else {
1444 pname = pnode->name;
1445 }
1446 if (ly_strncmp(pname, name, name_len)) {
1447 return 0;
1448 }
1449
1450 return 1;
1451}
1452
1453/**
1454 * @brief Check whether a compiled node matches a single schema nodeid name test.
1455 *
1456 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1457 * @param[in] mod Expected module.
1458 * @param[in] name Expected name.
1459 * @param[in] name_len Length of @p name.
1460 * @return Whether it is a match or not.
1461 */
1462static ly_bool
1463lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1464 size_t name_len)
1465{
1466 const struct lys_module *node_mod;
1467 const char *node_name;
1468
1469 /* compare with the module of the node */
Michal Vasko2a668712020-10-21 11:48:09 +02001470 if ((*node)->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
Michal Vasko208a04a2020-10-21 15:17:12 +02001471 node_mod = lysc_node_parent_full(*node)->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001472 } else {
1473 node_mod = (*node)->module;
1474 }
1475 if (node_mod != mod) {
1476 return 0;
1477 }
1478
1479 /* compare names */
1480 if ((*node)->nodetype == LYS_INPUT) {
1481 node_name = "input";
1482 } else if ((*node)->nodetype == LYS_OUTPUT) {
1483 node_name = "output";
1484 } else {
1485 node_name = (*node)->name;
1486 }
1487 if (ly_strncmp(node_name, name, name_len)) {
1488 return 0;
1489 }
1490
Michal Vasko2a668712020-10-21 11:48:09 +02001491 /* move to next parent */
Michal Vasko208a04a2020-10-21 15:17:12 +02001492 *node = lysc_node_parent_full(*node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001493
1494 return 1;
1495}
1496
1497/**
1498 * @brief Check whether a node matches specific schema nodeid.
1499 *
1500 * @param[in] exp Parsed nodeid to match.
1501 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1502 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1503 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1504 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1505 * @param[in] pnode_mod Compiled @p pnode to-be module.
1506 * @return Whether it is a match or not.
1507 */
1508static ly_bool
1509lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1510 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1511{
1512 uint32_t i;
1513 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001514 const char *name = NULL;
1515 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001516
1517 /* compare last node in the node ID */
1518 i = exp->used - 1;
1519
1520 /* get exp node ID module */
1521 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);
1522 assert(mod);
1523
1524 if (pnode) {
1525 /* compare on the last parsed-only node */
1526 if (!lysp_schema_nodeid_match_pnode(pnode, pnode_mod, mod, name, name_len)) {
1527 return 0;
1528 }
1529 } else {
1530 /* using parent directly */
1531 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1532 return 0;
1533 }
1534 }
1535
1536 /* now compare all the compiled parents */
1537 while (i > 1) {
1538 i -= 2;
1539 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1540
1541 if (!parent) {
1542 /* no more parents but path continues */
1543 return 0;
1544 }
1545
1546 /* get exp node ID module */
1547 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1548 &name_len);
1549 assert(mod);
1550
1551 /* compare with the parent */
1552 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1553 return 0;
1554 }
1555 }
1556
1557 if (ctx_node && (ctx_node != parent)) {
1558 /* descendant path has not finished in the context node */
1559 return 0;
1560 } else if (!ctx_node && parent) {
1561 /* some parent was not matched */
1562 return 0;
1563 }
1564
1565 return 1;
1566}
1567
1568void
1569lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1570{
1571 if (aug) {
1572 lyxp_expr_free(ctx, aug->nodeid);
1573
1574 free(aug);
1575 }
1576}
1577
1578void
1579lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1580{
1581 if (dev) {
1582 lyxp_expr_free(ctx, dev->nodeid);
1583 LY_ARRAY_FREE(dev->devs);
1584 LY_ARRAY_FREE(dev->dev_pmods);
1585
1586 free(dev);
1587 }
1588}
1589
1590void
1591lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1592{
1593 if (rfn) {
1594 lyxp_expr_free(ctx, rfn->nodeid);
1595 LY_ARRAY_FREE(rfn->rfns);
1596
1597 free(rfn);
1598 }
1599}
1600
1601void
1602lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1603{
1604 if (!dev_pnode) {
1605 return;
1606 }
1607
1608 switch (dev_pnode->nodetype) {
1609 case LYS_CONTAINER:
1610 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1611 break;
1612 case LYS_LIST:
1613 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1614 break;
1615 case LYS_CHOICE:
1616 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1617 break;
1618 case LYS_CASE:
1619 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1620 break;
1621 case LYS_LEAF:
1622 case LYS_LEAFLIST:
1623 case LYS_ANYXML:
1624 case LYS_ANYDATA:
1625 /* no children */
1626 break;
1627 case LYS_NOTIF:
1628 ((struct lysp_notif *)dev_pnode)->data = NULL;
1629 lysp_notif_free((struct ly_ctx *)ctx, (struct lysp_notif *)dev_pnode);
1630 free(dev_pnode);
1631 return;
1632 case LYS_RPC:
1633 case LYS_ACTION:
1634 ((struct lysp_action *)dev_pnode)->input.data = NULL;
1635 ((struct lysp_action *)dev_pnode)->output.data = NULL;
1636 lysp_action_free((struct ly_ctx *)ctx, (struct lysp_action *)dev_pnode);
1637 free(dev_pnode);
1638 return;
1639 case LYS_INPUT:
1640 case LYS_OUTPUT:
1641 ((struct lysp_action_inout *)dev_pnode)->data = NULL;
1642 lysp_action_inout_free((struct ly_ctx *)ctx, (struct lysp_action_inout *)dev_pnode);
1643 free(dev_pnode);
1644 return;
1645 default:
1646 LOGINT(ctx);
1647 return;
1648 }
1649
1650 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1651}
1652
1653LY_ERR
1654lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1655 struct lysp_node **dev_pnode, ly_bool *not_supported)
1656{
1657 LY_ERR ret = LY_SUCCESS;
1658 uint32_t i;
1659 LY_ARRAY_COUNT_TYPE u;
1660 struct lys_module *orig_mod = ctx->cur_mod;
1661 struct lysp_module *orig_pmod = ctx->pmod;
1662 char orig_path[LYSC_CTX_BUFSIZE];
1663 struct lysc_refine *rfn;
1664 struct lysc_deviation *dev;
1665 struct lysp_deviation *dev_p;
1666 struct lysp_deviate *d;
1667
1668 *dev_pnode = NULL;
1669 *not_supported = 0;
1670
1671 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1672 rfn = ctx->uses_rfns.objs[i];
1673
1674 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, ctx->cur_mod)) {
1675 /* not our target node */
1676 continue;
1677 }
1678
1679 if (!*dev_pnode) {
1680 /* first refine on this node, create a copy first */
1681 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1682 }
1683
1684 /* apply all the refines by changing (the copy of) the parsed node */
1685 LY_ARRAY_FOR(rfn->rfns, u) {
1686 /* apply refine, keep the current path and add to it */
1687 lysc_update_path(ctx, NULL, "{refine}");
1688 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
1689 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1690 lysc_update_path(ctx, NULL, NULL);
1691 lysc_update_path(ctx, NULL, NULL);
1692 LY_CHECK_GOTO(ret, cleanup);
1693 }
1694
1695 /* refine was applied, remove it */
1696 lysc_refine_free(ctx->ctx, rfn);
1697 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1698
1699 /* all the refines for one target node are in one structure, we are done */
1700 break;
1701 }
1702
1703 for (i = 0; i < ctx->devs.count; ++i) {
1704 dev = ctx->devs.objs[i];
1705
1706 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, ctx->cur_mod)) {
1707 /* not our target node */
1708 continue;
1709 }
1710
1711 if (dev->not_supported) {
1712 /* it is not supported, no more deviations */
1713 *not_supported = 1;
1714 goto dev_applied;
1715 }
1716
1717 if (!*dev_pnode) {
1718 /* first deviation on this node, create a copy first */
1719 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1720 }
1721
1722 /* apply all the deviates by changing (the copy of) the parsed node */
1723 LY_ARRAY_FOR(dev->devs, u) {
1724 dev_p = dev->devs[u];
1725 LY_LIST_FOR(dev_p->deviates, d) {
1726 /* generate correct path */
1727 strcpy(orig_path, ctx->path);
1728 ctx->path_len = 1;
1729 ctx->cur_mod = dev->dev_pmods[u]->mod;
1730 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1731 lysc_update_path(ctx, NULL, "{deviation}");
1732 lysc_update_path(ctx, NULL, dev_p->nodeid);
1733
1734 switch (d->mod) {
1735 case LYS_DEV_ADD:
1736 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1737 break;
1738 case LYS_DEV_DELETE:
1739 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1740 break;
1741 case LYS_DEV_REPLACE:
1742 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1743 break;
1744 default:
1745 LOGINT(ctx->ctx);
1746 ret = LY_EINT;
1747 }
1748
1749 /* restore previous path */
1750 strcpy(ctx->path, orig_path);
1751 ctx->path_len = strlen(ctx->path);
1752 ctx->cur_mod = orig_mod;
1753 ctx->pmod = orig_pmod;
1754
1755 LY_CHECK_GOTO(ret, cleanup);
1756 }
1757 }
1758
1759dev_applied:
1760 /* deviation was applied, remove it */
1761 lysc_deviation_free(ctx->ctx, dev);
1762 ly_set_rm_index(&ctx->devs, i, NULL);
1763
1764 /* all the deviations for one target node are in one structure, we are done */
1765 break;
1766 }
1767
1768cleanup:
1769 if (ret) {
1770 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1771 *dev_pnode = NULL;
1772 *not_supported = 0;
1773 }
1774 return ret;
1775}
1776
1777/**
1778 * @brief Compile the parsed augment connecting it into its target.
1779 *
1780 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1781 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1782 * are already implemented and compiled.
1783 *
1784 * @param[in] ctx Compile context.
1785 * @param[in] aug_p Parsed augment to compile.
1786 * @param[in] target Target node of the augment.
1787 * @return LY_SUCCESS on success.
1788 * @return LY_EVALID on failure.
1789 */
1790static LY_ERR
1791lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysc_node *target)
1792{
1793 LY_ERR ret = LY_SUCCESS;
1794 struct lysp_node *pnode;
1795 struct lysc_node *node;
1796 struct lysc_when *when_shared = NULL;
1797 struct lysc_action **actions;
1798 struct lysc_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001799 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001800 LY_ARRAY_COUNT_TYPE u;
1801 struct ly_set child_set = {0};
Michal Vasko29dd11e2020-11-23 16:52:22 +01001802 uint32_t i, opt_prev = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001803
1804 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
1805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1806 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1807 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1808 ret = LY_EVALID;
1809 goto cleanup;
1810 }
1811
1812 /* check for mandatory nodes
1813 * - new cases augmenting some choice can have mandatory nodes
1814 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1815 */
1816 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1817 allow_mandatory = 1;
1818 }
1819
1820 LY_LIST_FOR(aug_p->child, pnode) {
1821 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1822 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1823 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1824 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
1825 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1826 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1827 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1828 ret = LY_EVALID;
1829 goto cleanup;
1830 }
1831
1832 /* compile the children */
1833 if (target->nodetype == LYS_CHOICE) {
1834 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001835 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1836 if (target->nodetype == LYS_INPUT) {
1837 ctx->options |= LYS_COMPILE_RPC_INPUT;
1838 } else {
1839 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
1840 }
1841 ret = lys_compile_node(ctx, pnode, (struct lysc_node *)lysc_node_parent_full(target), 0, &child_set);
1842 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001843 } else {
1844 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1845 }
1846
Michal Vasko29dd11e2020-11-23 16:52:22 +01001847 /* eval if-features again for the rest of this node processing */
1848 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1849 if (!enabled) {
1850 ctx->options |= LYS_COMPILE_DISABLED;
1851 }
1852
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001853 /* since the augment node is not present in the compiled tree, we need to pass some of its
1854 * statements to all its children */
1855 for (i = 0; i < child_set.count; ++i) {
1856 node = child_set.snodes[i];
1857 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1858 node->flags &= ~LYS_MAND_TRUE;
1859 lys_compile_mandatory_parents(target, 0);
1860 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1861 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1862 ret = LY_EVALID;
1863 goto cleanup;
1864 }
1865
1866 if (aug_p->when) {
1867 /* pass augment's when to all the children */
1868 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target), node, &when_shared);
1869 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod9062b02020-11-04 17:15:50 +01001870
1871 if ((node->nodetype == LYS_CONTAINER) && !(node->flags & LYS_PRESENCE)) {
1872 /* container with a when condition */
1873 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its "
1874 "inherited \"when\" condition.", node->name);
1875 node->flags |= LYS_PRESENCE;
1876 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001877 }
1878 }
1879 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001880
1881 /* restore options */
1882 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001883 }
1884
1885 switch (target->nodetype) {
1886 case LYS_CONTAINER:
1887 actions = &((struct lysc_node_container *)target)->actions;
1888 notifs = &((struct lysc_node_container *)target)->notifs;
1889 break;
1890 case LYS_LIST:
1891 actions = &((struct lysc_node_list *)target)->actions;
1892 notifs = &((struct lysc_node_list *)target)->notifs;
1893 break;
1894 default:
1895 actions = NULL;
1896 notifs = NULL;
1897 break;
1898 }
1899
1900 if (aug_p->actions) {
1901 if (!actions) {
1902 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1903 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
1904 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
1905 ret = LY_EVALID;
1906 goto cleanup;
1907 }
1908
1909 /* compile actions into the target */
Michal Vasko5347e3a2020-11-03 17:14:57 +01001910 COMPILE_OP_ARRAY_GOTO(ctx, aug_p->actions, *actions, target, lys_compile_action, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001911
1912 if (aug_p->when) {
1913 /* inherit when */
1914 LY_ARRAY_FOR(*actions, u) {
1915 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target),
1916 (struct lysc_node *)&(*actions)[u], &when_shared);
1917 LY_CHECK_GOTO(ret, cleanup);
1918 }
1919 }
1920 }
1921 if (aug_p->notifs) {
1922 if (!notifs) {
1923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1924 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
1925 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
1926 ret = LY_EVALID;
1927 goto cleanup;
1928 }
1929
1930 /* compile notifications into the target */
Michal Vasko5347e3a2020-11-03 17:14:57 +01001931 COMPILE_OP_ARRAY_GOTO(ctx, aug_p->notifs, *notifs, target, lys_compile_notif, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001932
1933 if (aug_p->when) {
1934 /* inherit when */
1935 LY_ARRAY_FOR(*notifs, u) {
1936 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target),
1937 (struct lysc_node *)&(*notifs)[u], &when_shared);
1938 LY_CHECK_GOTO(ret, cleanup);
1939 }
1940 }
1941 }
1942
1943cleanup:
1944 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001945 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001946 return ret;
1947}
1948
1949LY_ERR
1950lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1951{
1952 LY_ERR ret = LY_SUCCESS;
1953 struct lys_module *orig_mod = ctx->cur_mod;
1954 struct lysp_module *orig_pmod = ctx->pmod;
1955 uint32_t i;
1956 char orig_path[LYSC_CTX_BUFSIZE];
1957 struct lysc_augment *aug;
1958
1959 /* uses augments */
1960 for (i = 0; i < ctx->uses_augs.count; ) {
1961 aug = ctx->uses_augs.objs[i];
1962
1963 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, aug->nodeid_ctx_node, node, NULL, NULL)) {
1964 /* not our target node */
1965 ++i;
1966 continue;
1967 }
1968
1969 /* apply augment, keep the current path and add to it */
1970 lysc_update_path(ctx, NULL, "{augment}");
1971 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
1972 ret = lys_compile_augment(ctx, aug->aug_p, node);
1973 lysc_update_path(ctx, NULL, NULL);
1974 lysc_update_path(ctx, NULL, NULL);
1975 LY_CHECK_GOTO(ret, cleanup);
1976
1977 /* augment was applied, remove it (index may have changed because other augments could have been applied) */
1978 ly_set_rm(&ctx->uses_augs, aug, NULL);
1979 lysc_augment_free(ctx->ctx, aug);
1980 }
1981
1982 /* top-level augments */
1983 for (i = 0; i < ctx->augs.count; ) {
1984 aug = ctx->augs.objs[i];
1985
1986 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, NULL, node, NULL, NULL)) {
1987 /* not our target node */
1988 ++i;
1989 continue;
1990 }
1991
1992 /* apply augment, use the path and modules from the augment */
1993 strcpy(orig_path, ctx->path);
1994 ctx->path_len = 1;
1995 lysc_update_path(ctx, NULL, "{augment}");
1996 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
1997 ctx->cur_mod = aug->nodeid_pmod->mod;
1998 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
1999 ret = lys_compile_augment(ctx, aug->aug_p, node);
2000 strcpy(ctx->path, orig_path);
2001 ctx->path_len = strlen(ctx->path);
2002 LY_CHECK_GOTO(ret, cleanup);
2003
2004 /* augment was applied, remove it */
2005 ly_set_rm(&ctx->augs, aug, NULL);
2006 lysc_augment_free(ctx->ctx, aug);
2007 }
2008
2009cleanup:
2010 ctx->cur_mod = orig_mod;
2011 ctx->pmod = orig_pmod;
2012 return ret;
2013}
2014
2015/**
2016 * @brief Prepare a top-level augment to be applied during data nodes compilation.
2017 *
2018 * @param[in] ctx Compile context.
2019 * @param[in] aug_p Parsed augment to be applied.
2020 * @param[in] pmod Both current and prefix module for @p aug_p.
2021 * @return LY_ERR value.
2022 */
2023static LY_ERR
2024lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysp_module *pmod)
2025{
2026 LY_ERR ret = LY_SUCCESS;
2027 struct lyxp_expr *exp = NULL;
2028 struct lysc_augment *aug;
2029 const struct lys_module *mod;
2030
2031 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2032 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
2033 LY_CHECK_GOTO(ret, cleanup);
2034
2035 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2036 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2037 if (mod != ctx->cur_mod) {
2038 /* augment for another module, ignore */
2039 goto cleanup;
2040 }
2041
2042 /* allocate new compiled augment and store it in the set */
2043 aug = calloc(1, sizeof *aug);
2044 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2045 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
2046
2047 aug->nodeid = exp;
2048 exp = NULL;
2049 aug->nodeid_pmod = pmod;
2050 aug->aug_p = aug_p;
2051
2052cleanup:
2053 lyxp_expr_free(ctx->ctx, exp);
2054 return ret;
2055}
2056
2057LY_ERR
2058lys_precompile_own_augments(struct lysc_ctx *ctx)
2059{
2060 LY_ARRAY_COUNT_TYPE u, v, w;
2061 const struct lys_module *aug_mod;
2062
2063 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
2064 aug_mod = ctx->cur_mod->augmented_by[u];
2065
2066 /* collect all module augments */
2067 LY_ARRAY_FOR(aug_mod->parsed->augments, v) {
2068 LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->augments[v], aug_mod->parsed));
2069 }
2070
2071 /* collect all submodules augments */
2072 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
2073 LY_ARRAY_FOR(aug_mod->parsed->includes[v].submodule->augments, w) {
2074 LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->includes[v].submodule->augments[w],
2075 (struct lysp_module *)aug_mod->parsed->includes[v].submodule));
2076 }
2077 }
2078 }
2079
2080 return LY_SUCCESS;
2081}
2082
2083/**
2084 * @brief Prepare a deviation to be applied during data nodes compilation.
2085 *
2086 * @param[in] ctx Compile context.
2087 * @param[in] dev_p Parsed deviation to be applied.
2088 * @param[in] pmod Both current and prefix module for @p dev_p.
2089 * @return LY_ERR value.
2090 */
2091static LY_ERR
2092lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2093{
2094 LY_ERR ret = LY_SUCCESS;
2095 struct lysc_deviation *dev = NULL;
2096 struct lyxp_expr *exp = NULL;
2097 struct lysp_deviation **new_dev;
2098 const struct lys_module *mod;
2099 const struct lysp_module **new_dev_pmod;
2100 uint32_t i;
2101
2102 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2103 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2104 LY_CHECK_GOTO(ret, cleanup);
2105
2106 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2107 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2108 if (mod != ctx->cur_mod) {
2109 /* deviation for another module, ignore */
2110 goto cleanup;
2111 }
2112
2113 /* try to find the node in already compiled deviations */
2114 for (i = 0; i < ctx->devs.count; ++i) {
2115 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2116 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2117 dev = ctx->devs.objs[i];
2118 break;
2119 }
2120 }
2121
2122 if (!dev) {
2123 /* allocate new compiled deviation */
2124 dev = calloc(1, sizeof *dev);
2125 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2126 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2127
2128 dev->nodeid = exp;
2129 exp = NULL;
2130 }
2131
2132 /* add new parsed deviation structure */
2133 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2134 *new_dev = dev_p;
2135 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2136 *new_dev_pmod = pmod;
2137
2138cleanup:
2139 lyxp_expr_free(ctx->ctx, exp);
2140 return ret;
2141}
2142
2143LY_ERR
2144lys_precompile_own_deviations(struct lysc_ctx *ctx)
2145{
2146 LY_ARRAY_COUNT_TYPE u, v, w;
2147 const struct lys_module *dev_mod;
2148 struct lysc_deviation *dev;
2149 struct lysp_deviate *d;
2150 int not_supported;
2151 uint32_t i;
2152
2153 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2154 dev_mod = ctx->cur_mod->deviated_by[u];
2155
2156 /* compile all module deviations */
2157 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2158 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2159 }
2160
2161 /* compile all submodules deviations */
2162 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2163 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2164 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2165 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2166 }
2167 }
2168 }
2169
2170 /* set not-supported flags for all the deviations */
2171 for (i = 0; i < ctx->devs.count; ++i) {
2172 dev = ctx->devs.objs[i];
2173 not_supported = 0;
2174
2175 LY_ARRAY_FOR(dev->devs, u) {
2176 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2177 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2178 not_supported = 1;
2179 break;
2180 }
2181 }
2182 if (not_supported) {
2183 break;
2184 }
2185 }
2186 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
2187 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2188 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
2189 return LY_EVALID;
2190 }
2191
2192 dev->not_supported = not_supported;
2193 }
2194
2195 return LY_SUCCESS;
2196}
2197
2198/**
2199 * @brief Add a module reference into an array, checks for duplicities.
2200 *
2201 * @param[in] ctx Compile context.
2202 * @param[in] mod Module reference to add.
2203 * @param[in,out] mod_array Module sized array to add to.
2204 * @return LY_ERR value.
2205 */
2206static LY_ERR
2207lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2208{
2209 LY_ARRAY_COUNT_TYPE u;
2210 struct lys_module **new_mod;
2211
2212 LY_ARRAY_FOR(*mod_array, u) {
2213 if ((*mod_array)[u] == mod) {
2214 /* already there */
2215 return LY_EEXIST;
2216 }
2217 }
2218
2219 /* add the new module ref */
2220 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2221 *new_mod = mod;
2222
2223 return LY_SUCCESS;
2224}
2225
2226LY_ERR
2227lys_precompile_augments_deviations(struct lysc_ctx *ctx)
2228{
Michal Vasko405cc9e2020-12-01 12:01:27 +01002229 LY_ERR ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002230 LY_ARRAY_COUNT_TYPE u, v;
2231 const struct lysp_module *mod_p;
2232 const struct lysc_node *target;
2233 struct lys_module *mod;
2234 struct lysp_submodule *submod;
2235 ly_bool has_dev = 0;
2236 uint16_t flags;
2237 uint32_t idx, opt_prev = ctx->options;
2238
Michal Vasko405cc9e2020-12-01 12:01:27 +01002239 for (idx = 0; idx < ctx->unres->implementing.count; ++idx) {
2240 if (ctx->cur_mod == ctx->unres->implementing.objs[idx]) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002241 break;
2242 }
2243 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01002244 if (idx == ctx->unres->implementing.count) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002245 /* it was already implemented and all the augments and deviations fully applied */
2246 return LY_SUCCESS;
2247 }
2248
2249 mod_p = ctx->cur_mod->parsed;
2250
2251 LY_ARRAY_FOR(mod_p->augments, u) {
2252 lysc_update_path(ctx, NULL, "{augment}");
2253 lysc_update_path(ctx, NULL, mod_p->augments[u].nodeid);
2254
2255 /* get target module */
2256 ret = lys_nodeid_check(ctx, mod_p->augments[u].nodeid, 1, &mod, NULL);
2257 LY_CHECK_RET(ret);
2258
2259 /* add this module into the target module augmented_by, if not there already from previous augments */
2260 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2261
2262 /* if we are compiling this module, we cannot add augments to it yet */
2263 if (mod != ctx->cur_mod) {
2264 /* apply the augment, find the target node first */
2265 flags = 0;
2266 ret = lysc_resolve_schema_nodeid(ctx, mod_p->augments[u].nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
2267 (void *)mod_p, 0, &target, &flags);
2268 LY_CHECK_RET(ret);
2269
2270 /* apply the augment */
2271 ctx->options |= flags;
2272 ret = lys_compile_augment(ctx, &mod_p->augments[u], (struct lysc_node *)target);
2273 ctx->options = opt_prev;
2274 LY_CHECK_RET(ret);
2275 }
2276
2277 lysc_update_path(ctx, NULL, NULL);
2278 lysc_update_path(ctx, NULL, NULL);
2279 }
2280
2281 LY_ARRAY_FOR(mod_p->deviations, u) {
2282 /* get target module */
2283 lysc_update_path(ctx, NULL, "{deviation}");
2284 lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid);
2285 ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL);
2286 lysc_update_path(ctx, NULL, NULL);
2287 lysc_update_path(ctx, NULL, NULL);
2288 LY_CHECK_RET(ret);
2289
2290 /* add this module into the target module deviated_by, if not there already from previous deviations */
2291 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2292
2293 /* new deviation added to the target module */
2294 has_dev = 1;
2295 }
2296
2297 /* the same for augments and deviations in submodules */
2298 LY_ARRAY_FOR(mod_p->includes, v) {
2299 submod = mod_p->includes[v].submodule;
2300 LY_ARRAY_FOR(submod->augments, u) {
2301 lysc_update_path(ctx, NULL, "{augment}");
2302 lysc_update_path(ctx, NULL, submod->augments[u].nodeid);
2303
2304 ret = lys_nodeid_check(ctx, submod->augments[u].nodeid, 1, &mod, NULL);
2305 LY_CHECK_RET(ret);
2306
2307 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2308 if (mod != ctx->cur_mod) {
2309 flags = 0;
2310 ret = lysc_resolve_schema_nodeid(ctx, mod_p->augments[u].nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
2311 submod, 0, &target, &flags);
2312 LY_CHECK_RET(ret);
2313
2314 ctx->options |= flags;
2315 ret = lys_compile_augment(ctx, &submod->augments[u], (struct lysc_node *)target);
2316 ctx->options = opt_prev;
2317 LY_CHECK_RET(ret);
2318 }
2319
2320 lysc_update_path(ctx, NULL, NULL);
2321 lysc_update_path(ctx, NULL, NULL);
2322 }
2323
2324 LY_ARRAY_FOR(submod->deviations, u) {
2325 lysc_update_path(ctx, NULL, "{deviation}");
2326 lysc_update_path(ctx, NULL, submod->deviations[u].nodeid);
2327 ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL);
2328 lysc_update_path(ctx, NULL, NULL);
2329 lysc_update_path(ctx, NULL, NULL);
2330 LY_CHECK_RET(ret);
2331
2332 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2333 has_dev = 1;
2334 }
2335 }
2336
Michal Vasko405cc9e2020-12-01 12:01:27 +01002337 if (has_dev) {
2338 /* all modules (may) need to be recompiled */
2339 ctx->unres->recompile = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002340 }
2341
Michal Vasko405cc9e2020-12-01 12:01:27 +01002342 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002343}
2344
2345void
2346lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2347{
2348 uint32_t i;
2349 LY_ARRAY_COUNT_TYPE u, count;
2350 struct lys_module *m;
2351
2352 for (i = 0; i < ctx->list.count; ++i) {
2353 m = ctx->list.objs[i];
2354
2355 if (m->augmented_by) {
2356 count = LY_ARRAY_COUNT(m->augmented_by);
2357 for (u = 0; u < count; ++u) {
2358 if (m->augmented_by[u] == mod) {
2359 /* keep the order */
2360 if (u < count - 1) {
2361 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2362 }
2363 LY_ARRAY_DECREMENT(m->augmented_by);
2364 break;
2365 }
2366 }
2367 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2368 LY_ARRAY_FREE(m->augmented_by);
2369 m->augmented_by = NULL;
2370 }
2371 }
2372
2373 if (m->deviated_by) {
2374 count = LY_ARRAY_COUNT(m->deviated_by);
2375 for (u = 0; u < count; ++u) {
2376 if (m->deviated_by[u] == mod) {
2377 /* keep the order */
2378 if (u < count - 1) {
2379 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2380 }
2381 LY_ARRAY_DECREMENT(m->deviated_by);
2382 break;
2383 }
2384 }
2385 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2386 LY_ARRAY_FREE(m->deviated_by);
2387 m->deviated_by = NULL;
2388 }
2389 }
2390 }
2391}