blob: aab32bbeaf7dbbb88e0459b2d1409eff73885c29 [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"
Radek Krejci77114102021-03-10 15:21:57 +010026#include "dict.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020027#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "plugins_exts_compile.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020029#include "schema_compile.h"
30#include "schema_compile_node.h"
Michal Vasko29dd11e2020-11-23 16:52:22 +010031#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020032#include "set.h"
33#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010034#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020035#include "tree_schema.h"
36#include "tree_schema_internal.h"
37#include "xpath.h"
38
39static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest,
40 size_t nametest_len, const struct lysp_module *mod, const char **name, size_t *name_len);
41
Michal Vasko1ccbf542021-04-19 11:35:00 +020042/**
43 * @brief Check the syntax of a node-id and collect all the referenced modules.
44 *
45 * @param[in] ctx Compile context.
46 * @param[in] nodeid Node-id to check.
47 * @param[in] abs Whether @p nodeid is absolute.
48 * @param[in,out] mod_set Set to add referenced modules into.
49 * @param[out] expr Optional node-id parsed into an expression.
50 * @param[out] target_mod Optional target module of the node-id.
51 * @return LY_ERR value.
52 */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020053static LY_ERR
Michal Vasko1ccbf542021-04-19 11:35:00 +020054lys_nodeid_mod_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct ly_set *mod_set,
55 struct lyxp_expr **expr, struct lys_module **target_mod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020056{
57 LY_ERR ret = LY_SUCCESS;
58 struct lyxp_expr *e = NULL;
59 struct lys_module *tmod = NULL, *mod;
60 const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid";
61 uint32_t i;
62
63 /* parse */
64 ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e);
65 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010066 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020067 nodeid_type, nodeid);
68 ret = LY_EVALID;
69 goto cleanup;
70 }
71
72 if (abs) {
73 /* absolute schema nodeid */
74 i = 0;
75 } else {
76 /* descendant schema nodeid */
77 if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010078 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020079 nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]);
80 ret = LY_EVALID;
81 goto cleanup;
82 }
83 i = 1;
84 }
85
86 /* check all the tokens */
87 for ( ; i < e->used; i += 2) {
88 if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010089 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020090 nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]);
91 ret = LY_EVALID;
92 goto cleanup;
93 } else if (e->used == i + 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010094 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020095 "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr);
96 ret = LY_EVALID;
97 goto cleanup;
98 } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010099 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200100 nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
101 ret = LY_EVALID;
102 goto cleanup;
103 } else if (abs) {
104 mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1],
105 e->tok_len[i + 1], ctx->pmod, NULL, NULL);
106 LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup);
107
108 /* only keep the first module */
109 if (!tmod) {
110 tmod = mod;
111 }
112
Michal Vasko1ccbf542021-04-19 11:35:00 +0200113 /* store the referenced module */
114 LY_CHECK_GOTO(ret = ly_set_add(mod_set, mod, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200115 }
116 }
117
118cleanup:
119 if (ret || !expr) {
120 lyxp_expr_free(ctx->ctx, e);
121 e = NULL;
122 }
123 if (expr) {
124 *expr = ret ? NULL : e;
125 }
126 if (target_mod) {
127 *target_mod = ret ? NULL : tmod;
128 }
129 return ret;
130}
131
132/**
133 * @brief Check whether 2 schema nodeids match.
134 *
135 * @param[in] ctx libyang context.
136 * @param[in] exp1 First schema nodeid.
137 * @param[in] exp1p_mod Module of @p exp1 nodes without any prefix.
138 * @param[in] exp2 Second schema nodeid.
139 * @param[in] exp2_pmod Module of @p exp2 nodes without any prefix.
140 * @return Whether the schema nodeids match or not.
141 */
142static ly_bool
143lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lysp_module *exp1_pmod,
144 const struct lyxp_expr *exp2, const struct lysp_module *exp2_pmod)
145{
146 uint32_t i;
147 const struct lys_module *mod1, *mod2;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100148 const char *name1 = NULL, *name2 = NULL;
149 size_t name1_len = 0, name2_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200150
151 if (exp1->used != exp2->used) {
152 return 0;
153 }
154
155 for (i = 0; i < exp1->used; ++i) {
156 assert(exp1->tokens[i] == exp2->tokens[i]);
157
158 if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) {
159 /* check modules of all the nodes in the node ID */
160 mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_pmod,
161 &name1, &name1_len);
162 assert(mod1);
163 mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_pmod,
164 &name2, &name2_len);
165 assert(mod2);
166
167 /* compare modules */
168 if (mod1 != mod2) {
169 return 0;
170 }
171
172 /* compare names */
173 if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) {
174 return 0;
175 }
176 }
177 }
178
179 return 1;
180}
181
182LY_ERR
183lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node)
184{
185 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200186 struct lyxp_expr *exp = NULL;
187 struct lysc_augment *aug;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100188 struct lysp_node_augment *aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200189 struct lysc_refine *rfn;
190 struct lysp_refine **new_rfn;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100191 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200192 uint32_t i;
Michal Vasko1ccbf542021-04-19 11:35:00 +0200193 struct ly_set mod_set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200194
Radek Krejci2a9fc652021-01-22 17:44:34 +0100195 LY_LIST_FOR(uses_p->augments, aug_p) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200196 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +0100197 lysc_update_path(ctx, NULL, aug_p->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200198
199 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200200 LY_CHECK_GOTO(ret = lys_nodeid_mod_check(ctx, aug_p->nodeid, 0, &mod_set, &exp, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200201
202 /* allocate new compiled augment and store it in the set */
203 aug = calloc(1, sizeof *aug);
204 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
205 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, 1, NULL), cleanup);
206
207 aug->nodeid = exp;
208 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +0100209 aug->aug_pmod = ctx->pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200210 aug->nodeid_ctx_node = ctx_node;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100211 aug->aug_p = aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200212
213 lysc_update_path(ctx, NULL, NULL);
214 lysc_update_path(ctx, NULL, NULL);
215 }
216
217 LY_ARRAY_FOR(uses_p->refines, u) {
218 lysc_update_path(ctx, NULL, "{refine}");
219 lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid);
220
221 /* parse the nodeid */
Michal Vasko1ccbf542021-04-19 11:35:00 +0200222 LY_CHECK_GOTO(ret = lys_nodeid_mod_check(ctx, uses_p->refines[u].nodeid, 0, &mod_set, &exp, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200223
224 /* try to find the node in already compiled refines */
225 rfn = NULL;
226 for (i = 0; i < ctx->uses_rfns.count; ++i) {
227 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->pmod, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid,
228 ctx->pmod)) {
229 rfn = ctx->uses_rfns.objs[i];
230 break;
231 }
232 }
233
234 if (!rfn) {
235 /* allocate new compiled refine */
236 rfn = calloc(1, sizeof *rfn);
237 LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
238 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, 1, NULL), cleanup);
239
240 rfn->nodeid = exp;
241 exp = NULL;
Michal Vasko7d3708f2021-02-03 10:50:24 +0100242 rfn->nodeid_pmod = ctx->cur_mod->parsed;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200243 rfn->nodeid_ctx_node = ctx_node;
Michal Vaskod8655722021-01-12 15:20:36 +0100244 rfn->uses_p = uses_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200245 } else {
246 /* just free exp */
247 lyxp_expr_free(ctx->ctx, exp);
248 exp = NULL;
249 }
250
251 /* add new parsed refine structure */
252 LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup);
253 *new_rfn = &uses_p->refines[u];
254
255 lysc_update_path(ctx, NULL, NULL);
256 lysc_update_path(ctx, NULL, NULL);
257 }
258
259cleanup:
Michal Vasko1ccbf542021-04-19 11:35:00 +0200260 if (ret) {
261 lysc_update_path(ctx, NULL, NULL);
262 lysc_update_path(ctx, NULL, NULL);
263 }
264 /* should include only this module, will fail later if not */
265 ly_set_erase(&mod_set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200266 lyxp_expr_free(ctx->ctx, exp);
267 return ret;
268}
269
270static LY_ERR
271lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext)
272{
273 LY_ERR ret = LY_SUCCESS;
274
275 *ext = *orig_ext;
276 DUP_STRING(ctx, orig_ext->name, ext->name, ret);
277 DUP_STRING(ctx, orig_ext->argument, ext->argument, ret);
278
279 return ret;
280}
281
282static LY_ERR
283lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr)
284{
285 LY_ERR ret = LY_SUCCESS;
286
287 if (orig_restr) {
288 DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret);
289 restr->arg.mod = orig_restr->arg.mod;
290 DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret);
291 DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret);
292 DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret);
293 DUP_STRING(ctx, orig_restr->ref, restr->ref, ret);
294 DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup);
295 }
296
297 return ret;
298}
299
300static LY_ERR
301lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str)
302{
303 LY_ERR ret = LY_SUCCESS;
304
305 DUP_STRING(ctx, *orig_str, *str, ret);
306
307 return ret;
308}
309
310LY_ERR
311lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname)
312{
313 LY_ERR ret = LY_SUCCESS;
314
315 if (!orig_qname->str) {
316 return LY_SUCCESS;
317 }
318
319 DUP_STRING(ctx, orig_qname->str, qname->str, ret);
320 assert(orig_qname->mod);
321 qname->mod = orig_qname->mod;
322
323 return ret;
324}
325
326static LY_ERR
327lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm)
328{
329 LY_ERR ret = LY_SUCCESS;
330
331 DUP_STRING(ctx, orig_enm->name, enm->name, ret);
332 DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret);
333 DUP_STRING(ctx, orig_enm->ref, enm->ref, ret);
334 enm->value = orig_enm->value;
335 DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup);
336 DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup);
337 enm->flags = orig_enm->flags;
338
339 return ret;
340}
341
342static LY_ERR
343lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type)
344{
345 LY_ERR ret = LY_SUCCESS;
346
347 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
348
349 if (orig_type->range) {
350 type->range = calloc(1, sizeof *type->range);
351 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
352 LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range));
353 }
354
355 if (orig_type->length) {
356 type->length = calloc(1, sizeof *type->length);
357 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
358 LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length));
359 }
360
361 DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup);
362 DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup);
363 DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup);
364 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done);
365 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
366 DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup);
367 DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup);
368
369 type->pmod = orig_type->pmod;
370 type->compiled = orig_type->compiled;
371
372 type->fraction_digits = orig_type->fraction_digits;
373 type->require_instance = orig_type->require_instance;
374 type->flags = orig_type->flags;
375
376done:
377 return ret;
378}
379
380static LY_ERR
381lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when)
382{
383 LY_ERR ret = LY_SUCCESS;
384
385 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
386 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
387 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
388 DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup);
389
390 return ret;
391}
392
393static LY_ERR
394lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
395{
396 LY_ERR ret = LY_SUCCESS;
397
398 node->parent = NULL;
399 node->nodetype = orig->nodetype;
400 node->flags = orig->flags;
401 node->next = NULL;
402 DUP_STRING(ctx, orig->name, node->name, ret);
403 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
404 DUP_STRING(ctx, orig->ref, node->ref, ret);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200405 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
406 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
407
408 return ret;
409}
410
Radek Krejci9a3823e2021-01-27 20:26:46 +0100411#define DUP_PWHEN(CTX, ORIG, NEW) \
412 if (ORIG) { \
413 NEW = calloc(1, sizeof *NEW); \
414 LY_CHECK_ERR_RET(!NEW, LOGMEM(CTX), LY_EMEM); \
415 LY_CHECK_RET(lysp_when_dup(CTX, NEW, ORIG)); \
416 }
417
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200418static LY_ERR
419lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
420{
421 LY_ERR ret = LY_SUCCESS;
422 struct lysp_node_container *cont;
423 const struct lysp_node_container *orig_cont;
424 struct lysp_node_leaf *leaf;
425 const struct lysp_node_leaf *orig_leaf;
426 struct lysp_node_leaflist *llist;
427 const struct lysp_node_leaflist *orig_llist;
428 struct lysp_node_list *list;
429 const struct lysp_node_list *orig_list;
430 struct lysp_node_choice *choice;
431 const struct lysp_node_choice *orig_choice;
432 struct lysp_node_case *cas;
433 const struct lysp_node_case *orig_cas;
434 struct lysp_node_anydata *any;
435 const struct lysp_node_anydata *orig_any;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100436 struct lysp_node_action *action;
437 const struct lysp_node_action *orig_action;
438 struct lysp_node_action_inout *action_inout;
439 const struct lysp_node_action_inout *orig_action_inout;
440 struct lysp_node_notif *notif;
441 const struct lysp_node_notif *orig_notif;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200442
Radek Krejci2a9fc652021-01-22 17:44:34 +0100443 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA |
444 LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200445
446 /* common part */
447 LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig));
448
449 /* specific part */
450 switch (node->nodetype) {
451 case LYS_CONTAINER:
452 cont = (struct lysp_node_container *)node;
453 orig_cont = (const struct lysp_node_container *)orig;
454
Radek Krejci9a3823e2021-01-27 20:26:46 +0100455 DUP_PWHEN(ctx, orig_cont->when, cont->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200456 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
457 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
458 /* we do not need the rest */
459 break;
460 case LYS_LEAF:
461 leaf = (struct lysp_node_leaf *)node;
462 orig_leaf = (const struct lysp_node_leaf *)orig;
463
Radek Krejci9a3823e2021-01-27 20:26:46 +0100464 DUP_PWHEN(ctx, orig_leaf->when, leaf->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200465 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
466 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
467 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
468 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
469 break;
470 case LYS_LEAFLIST:
471 llist = (struct lysp_node_leaflist *)node;
472 orig_llist = (const struct lysp_node_leaflist *)orig;
473
Radek Krejci9a3823e2021-01-27 20:26:46 +0100474 DUP_PWHEN(ctx, orig_llist->when, llist->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200475 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
476 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
477 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
478 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
479 llist->min = orig_llist->min;
480 llist->max = orig_llist->max;
481 break;
482 case LYS_LIST:
483 list = (struct lysp_node_list *)node;
484 orig_list = (const struct lysp_node_list *)orig;
485
Radek Krejci9a3823e2021-01-27 20:26:46 +0100486 DUP_PWHEN(ctx, orig_list->when, list->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200487 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
488 DUP_STRING(ctx, orig_list->key, list->key, ret);
489 /* we do not need these arrays */
490 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
491 list->min = orig_list->min;
492 list->max = orig_list->max;
493 break;
494 case LYS_CHOICE:
495 choice = (struct lysp_node_choice *)node;
496 orig_choice = (const struct lysp_node_choice *)orig;
497
Radek Krejci9a3823e2021-01-27 20:26:46 +0100498 DUP_PWHEN(ctx, orig_choice->when, choice->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200499 /* we do not need children */
500 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
501 break;
502 case LYS_CASE:
503 cas = (struct lysp_node_case *)node;
504 orig_cas = (const struct lysp_node_case *)orig;
505
Radek Krejci9a3823e2021-01-27 20:26:46 +0100506 DUP_PWHEN(ctx, orig_cas->when, cas->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200507 /* we do not need children */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200508 break;
509 case LYS_ANYDATA:
510 case LYS_ANYXML:
511 any = (struct lysp_node_anydata *)node;
512 orig_any = (const struct lysp_node_anydata *)orig;
513
Radek Krejci9a3823e2021-01-27 20:26:46 +0100514 DUP_PWHEN(ctx, orig_any->when, any->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200515 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
516 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100517 case LYS_RPC:
518 case LYS_ACTION:
519 action = (struct lysp_node_action *)node;
520 orig_action = (const struct lysp_node_action *)orig;
521
522 action->input.nodetype = orig_action->input.nodetype;
523 action->output.nodetype = orig_action->output.nodetype;
524 /* we do not need the rest */
525 break;
526 case LYS_INPUT:
527 case LYS_OUTPUT:
528 action_inout = (struct lysp_node_action_inout *)node;
529 orig_action_inout = (const struct lysp_node_action_inout *)orig;
530
531 DUP_ARRAY(ctx, orig_action_inout->musts, action_inout->musts, lysp_restr_dup);
532 /* we do not need the rest */
533 break;
534 case LYS_NOTIF:
535 notif = (struct lysp_node_notif *)node;
536 orig_notif = (const struct lysp_node_notif *)orig;
537
538 DUP_ARRAY(ctx, orig_notif->musts, notif->musts, lysp_restr_dup);
539 /* we do not need the rest */
540 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200541 default:
542 LOGINT_RET(ctx);
543 }
544
545 return ret;
546}
547
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200548/**
549 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
550 *
551 * @param[in] ctx libyang context.
552 * @param[in] pnode Node to duplicate.
553 * @param[in] with_links Whether to also copy any links (child, parent pointers).
554 * @param[out] dup_p Duplicated parsed node.
555 * @return LY_ERR value.
556 */
557static LY_ERR
558lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
559{
560 LY_ERR ret = LY_SUCCESS;
561 void *mem = NULL;
562
563 if (!pnode) {
564 *dup_p = NULL;
565 return LY_SUCCESS;
566 }
567
568 switch (pnode->nodetype) {
569 case LYS_CONTAINER:
570 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200571 break;
572 case LYS_LEAF:
573 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200574 break;
575 case LYS_LEAFLIST:
576 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200577 break;
578 case LYS_LIST:
579 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200580 break;
581 case LYS_CHOICE:
582 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200583 break;
584 case LYS_CASE:
585 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200586 break;
587 case LYS_ANYDATA:
588 case LYS_ANYXML:
589 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200590 break;
591 case LYS_INPUT:
592 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100593 mem = calloc(1, sizeof(struct lysp_node_action_inout));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200594 break;
595 case LYS_ACTION:
596 case LYS_RPC:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100597 mem = calloc(1, sizeof(struct lysp_node_action));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200598 break;
599 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100600 mem = calloc(1, sizeof(struct lysp_node_notif));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200601 break;
602 default:
603 LOGINT_RET(ctx);
604 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100605 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
606 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200607
608 if (with_links) {
609 /* copy also parent and child pointers */
610 ((struct lysp_node *)mem)->parent = pnode->parent;
611 switch (pnode->nodetype) {
612 case LYS_CONTAINER:
613 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
614 break;
615 case LYS_LIST:
616 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
617 break;
618 case LYS_CHOICE:
619 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
620 break;
621 case LYS_CASE:
622 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
623 break;
624 default:
625 break;
626 }
627 }
628
629cleanup:
630 if (ret) {
631 free(mem);
632 } else {
633 *dup_p = mem;
634 }
635 return ret;
636}
637
638#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100639 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 +0200640 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
641 ret = LY_EVALID; \
642 goto cleanup;
643
644#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
645 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100646 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 +0200647 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
648 ret = LY_EVALID; \
649 goto cleanup; \
650 }
651
652/**
653 * @brief Apply refine.
654 *
655 * @param[in] ctx Compile context.
656 * @param[in] rfn Refine to apply.
657 * @param[in,out] target Refine target.
658 * @return LY_ERR value.
659 */
660static LY_ERR
661lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
662{
663 LY_ERR ret = LY_SUCCESS;
664 LY_ARRAY_COUNT_TYPE u;
665 struct lysp_qname *qname;
666 struct lysp_restr **musts, *must;
667 uint32_t *num;
668
669 /* default value */
670 if (rfn->dflts) {
671 switch (target->nodetype) {
672 case LYS_LEAF:
673 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
674
Michal Vaskoe180ed02021-02-05 16:31:20 +0100675 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200676 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
677 break;
678 case LYS_LEAFLIST:
679 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100680 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200681 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
682 ret = LY_EVALID;
683 goto cleanup;
684 }
685
686 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
687 ((struct lysp_node_leaflist *)target)->dflts = NULL;
688 LY_ARRAY_FOR(rfn->dflts, u) {
689 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
690 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
691 }
692 break;
693 case LYS_CHOICE:
694 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
695
Michal Vaskoe180ed02021-02-05 16:31:20 +0100696 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200697 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
698 break;
699 default:
700 AMEND_WRONG_NODETYPE("refine", "replace", "default");
701 }
702 }
703
704 /* description */
705 if (rfn->dsc) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100706 lydict_remove(ctx->ctx, target->dsc);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200707 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
708 }
709
710 /* reference */
711 if (rfn->ref) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100712 lydict_remove(ctx->ctx, target->ref);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200713 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
714 }
715
716 /* config */
717 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci91531e12021-02-08 19:57:54 +0100718 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200719 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +0100720 (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT)) ? "RPC/action" :
721 ctx->options & LYS_IS_NOTIF ? "notification" : "a subtree ignoring config", ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200722 } else {
723 target->flags &= ~LYS_CONFIG_MASK;
724 target->flags |= rfn->flags & LYS_CONFIG_MASK;
725 }
726 }
727
728 /* mandatory */
729 if (rfn->flags & LYS_MAND_MASK) {
730 switch (target->nodetype) {
731 case LYS_LEAF:
732 case LYS_CHOICE:
733 case LYS_ANYDATA:
734 case LYS_ANYXML:
735 break;
736 default:
737 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
738 }
739
740 target->flags &= ~LYS_MAND_MASK;
741 target->flags |= rfn->flags & LYS_MAND_MASK;
742 }
743
744 /* presence */
745 if (rfn->presence) {
746 switch (target->nodetype) {
747 case LYS_CONTAINER:
748 break;
749 default:
750 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
751 }
752
Michal Vaskoe180ed02021-02-05 16:31:20 +0100753 lydict_remove(ctx->ctx, ((struct lysp_node_container *)target)->presence);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200754 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
755 }
756
757 /* must */
758 if (rfn->musts) {
759 switch (target->nodetype) {
760 case LYS_CONTAINER:
761 case LYS_LIST:
762 case LYS_LEAF:
763 case LYS_LEAFLIST:
764 case LYS_ANYDATA:
765 case LYS_ANYXML:
766 musts = &((struct lysp_node_container *)target)->musts;
767 break;
768 default:
769 AMEND_WRONG_NODETYPE("refine", "add", "must");
770 }
771
772 LY_ARRAY_FOR(rfn->musts, u) {
773 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
774 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
775 }
776 }
777
778 /* min-elements */
779 if (rfn->flags & LYS_SET_MIN) {
780 switch (target->nodetype) {
781 case LYS_LEAFLIST:
782 num = &((struct lysp_node_leaflist *)target)->min;
783 break;
784 case LYS_LIST:
785 num = &((struct lysp_node_list *)target)->min;
786 break;
787 default:
788 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
789 }
790
791 *num = rfn->min;
792 }
793
794 /* max-elements */
795 if (rfn->flags & LYS_SET_MAX) {
796 switch (target->nodetype) {
797 case LYS_LEAFLIST:
798 num = &((struct lysp_node_leaflist *)target)->max;
799 break;
800 case LYS_LIST:
801 num = &((struct lysp_node_list *)target)->max;
802 break;
803 default:
804 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
805 }
806
807 *num = rfn->max;
808 }
809
810 /* if-feature */
811 if (rfn->iffeatures) {
812 switch (target->nodetype) {
813 case LYS_LEAF:
814 case LYS_LEAFLIST:
815 case LYS_LIST:
816 case LYS_CONTAINER:
817 case LYS_CHOICE:
818 case LYS_CASE:
819 case LYS_ANYDATA:
820 case LYS_ANYXML:
821 break;
822 default:
823 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
824 }
825
826 LY_ARRAY_FOR(rfn->iffeatures, u) {
827 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
828 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
829 }
830 }
831
832 /* extension */
833 /* TODO refine extensions */
834
835cleanup:
836 return ret;
837}
838
839/**
840 * @brief Apply deviate add.
841 *
842 * @param[in] ctx Compile context.
843 * @param[in] d Deviate add to apply.
844 * @param[in,out] target Deviation target.
845 * @return LY_ERR value.
846 */
847static LY_ERR
848lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
849{
850 LY_ERR ret = LY_SUCCESS;
851 LY_ARRAY_COUNT_TYPE u;
852 struct lysp_qname *qname;
853 uint32_t *num;
854 struct lysp_restr **musts, *must;
855
856#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
857 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100858 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200859 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
860 ret = LY_EVALID; \
861 goto cleanup; \
862 }
863
864 /* [units-stmt] */
865 if (d->units) {
866 switch (target->nodetype) {
867 case LYS_LEAF:
868 case LYS_LEAFLIST:
869 break;
870 default:
871 AMEND_WRONG_NODETYPE("deviation", "add", "units");
872 }
873
874 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
875 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
876 }
877
878 /* *must-stmt */
879 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100880 musts = lysp_node_musts_p(target);
881 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200882 AMEND_WRONG_NODETYPE("deviation", "add", "must");
883 }
884
885 LY_ARRAY_FOR(d->musts, u) {
886 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
887 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
888 }
889 }
890
891 /* *unique-stmt */
892 if (d->uniques) {
893 switch (target->nodetype) {
894 case LYS_LIST:
895 break;
896 default:
897 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
898 }
899
900 LY_ARRAY_FOR(d->uniques, u) {
901 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
902 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
903 }
904 }
905
906 /* *default-stmt */
907 if (d->dflts) {
908 switch (target->nodetype) {
909 case LYS_LEAF:
910 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
911 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
912
913 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
914 break;
915 case LYS_LEAFLIST:
916 LY_ARRAY_FOR(d->dflts, u) {
917 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
918 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
919 }
920 break;
921 case LYS_CHOICE:
922 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
923 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
924
925 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
926 break;
927 default:
928 AMEND_WRONG_NODETYPE("deviation", "add", "default");
929 }
930 }
931
932 /* [config-stmt] */
933 if (d->flags & LYS_CONFIG_MASK) {
934 switch (target->nodetype) {
935 case LYS_CONTAINER:
936 case LYS_LEAF:
937 case LYS_LEAFLIST:
938 case LYS_LIST:
939 case LYS_CHOICE:
940 case LYS_ANYDATA:
941 case LYS_ANYXML:
942 break;
943 default:
944 AMEND_WRONG_NODETYPE("deviation", "add", "config");
945 }
946
947 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100948 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200949 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
950 target->flags & LYS_CONFIG_W ? "true" : "false");
951 ret = LY_EVALID;
952 goto cleanup;
953 }
954
955 target->flags |= d->flags & LYS_CONFIG_MASK;
956 }
957
958 /* [mandatory-stmt] */
959 if (d->flags & LYS_MAND_MASK) {
960 switch (target->nodetype) {
961 case LYS_LEAF:
962 case LYS_CHOICE:
963 case LYS_ANYDATA:
964 case LYS_ANYXML:
965 break;
966 default:
967 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
968 }
969
970 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100971 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200972 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
973 target->flags & LYS_MAND_TRUE ? "true" : "false");
974 ret = LY_EVALID;
975 goto cleanup;
976 }
977
978 target->flags |= d->flags & LYS_MAND_MASK;
979 }
980
981 /* [min-elements-stmt] */
982 if (d->flags & LYS_SET_MIN) {
983 switch (target->nodetype) {
984 case LYS_LEAFLIST:
985 num = &((struct lysp_node_leaflist *)target)->min;
986 break;
987 case LYS_LIST:
988 num = &((struct lysp_node_list *)target)->min;
989 break;
990 default:
991 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
992 }
993
994 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100995 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200996 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
997 ret = LY_EVALID;
998 goto cleanup;
999 }
1000
1001 *num = d->min;
1002 }
1003
1004 /* [max-elements-stmt] */
1005 if (d->flags & LYS_SET_MAX) {
1006 switch (target->nodetype) {
1007 case LYS_LEAFLIST:
1008 num = &((struct lysp_node_leaflist *)target)->max;
1009 break;
1010 case LYS_LIST:
1011 num = &((struct lysp_node_list *)target)->max;
1012 break;
1013 default:
1014 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1015 }
1016
1017 if (target->flags & LYS_SET_MAX) {
1018 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001019 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001020 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1021 *num);
1022 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001023 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001024 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1025 }
1026 ret = LY_EVALID;
1027 goto cleanup;
1028 }
1029
1030 *num = d->max;
1031 }
1032
1033cleanup:
1034 return ret;
1035}
1036
1037/**
1038 * @brief Apply deviate delete.
1039 *
1040 * @param[in] ctx Compile context.
1041 * @param[in] d Deviate delete to apply.
1042 * @param[in,out] target Deviation target.
1043 * @return LY_ERR value.
1044 */
1045static LY_ERR
1046lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1047{
1048 LY_ERR ret = LY_SUCCESS;
1049 struct lysp_restr **musts;
1050 LY_ARRAY_COUNT_TYPE u, v;
1051 struct lysp_qname **uniques, **dflts;
1052
1053#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1054 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1055 int found = 0; \
1056 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1057 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1058 found = 1; \
1059 break; \
1060 } \
1061 } \
1062 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001063 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001064 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1065 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1066 ret = LY_EVALID; \
1067 goto cleanup; \
1068 } \
1069 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1070 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
Michal Vasko08e9b112021-06-11 15:41:17 +02001071 if (v < LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1072 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1073 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001074 } \
1075 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1076 LY_ARRAY_FREE(ORIG_ARRAY); \
1077 ORIG_ARRAY = NULL; \
1078 }
1079
1080#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1081 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001082 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001083 ret = LY_EVALID; \
1084 goto cleanup; \
1085 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001086 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001087 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1088 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1089 ret = LY_EVALID; \
1090 goto cleanup; \
1091 }
1092
1093 /* [units-stmt] */
1094 if (d->units) {
1095 switch (target->nodetype) {
1096 case LYS_LEAF:
1097 case LYS_LEAFLIST:
1098 break;
1099 default:
1100 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1101 }
1102
1103 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001104 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001105 ((struct lysp_node_leaf *)target)->units = NULL;
1106 }
1107
1108 /* *must-stmt */
1109 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001110 musts = lysp_node_musts_p(target);
1111 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001112 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1113 }
1114
1115 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1116 }
1117
1118 /* *unique-stmt */
1119 if (d->uniques) {
1120 switch (target->nodetype) {
1121 case LYS_LIST:
1122 break;
1123 default:
1124 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1125 }
1126
1127 uniques = &((struct lysp_node_list *)target)->uniques;
1128 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1129 }
1130
1131 /* *default-stmt */
1132 if (d->dflts) {
1133 switch (target->nodetype) {
1134 case LYS_LEAF:
1135 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1136 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1137
Michal Vaskoe180ed02021-02-05 16:31:20 +01001138 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001139 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1140 break;
1141 case LYS_LEAFLIST:
1142 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1143 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1144 break;
1145 case LYS_CHOICE:
1146 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1147 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1148
Michal Vaskoe180ed02021-02-05 16:31:20 +01001149 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001150 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1151 break;
1152 default:
1153 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1154 }
1155 }
1156
1157cleanup:
1158 return ret;
1159}
1160
1161/**
1162 * @brief Apply deviate replace.
1163 *
1164 * @param[in] ctx Compile context.
1165 * @param[in] d Deviate replace to apply.
1166 * @param[in,out] target Deviation target.
1167 * @return LY_ERR value.
1168 */
1169static LY_ERR
1170lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1171{
1172 LY_ERR ret = LY_SUCCESS;
1173 uint32_t *num;
1174
1175#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1176 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001177 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001178 ret = LY_EVALID; \
1179 goto cleanup; \
1180 }
1181
1182 /* [type-stmt] */
1183 if (d->type) {
1184 switch (target->nodetype) {
1185 case LYS_LEAF:
1186 case LYS_LEAFLIST:
1187 break;
1188 default:
1189 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1190 }
1191
1192 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1193 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1194 }
1195
1196 /* [units-stmt] */
1197 if (d->units) {
1198 switch (target->nodetype) {
1199 case LYS_LEAF:
1200 case LYS_LEAFLIST:
1201 break;
1202 default:
1203 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1204 }
1205
1206 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001207 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001208 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1209 }
1210
1211 /* [default-stmt] */
1212 if (d->dflt.str) {
1213 switch (target->nodetype) {
1214 case LYS_LEAF:
1215 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1216
Michal Vaskoe180ed02021-02-05 16:31:20 +01001217 lydict_remove(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001218 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1219 break;
1220 case LYS_CHOICE:
1221 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1222
Michal Vaskoe180ed02021-02-05 16:31:20 +01001223 lydict_remove(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001224 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1225 break;
1226 default:
1227 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1228 }
1229 }
1230
1231 /* [config-stmt] */
1232 if (d->flags & LYS_CONFIG_MASK) {
1233 switch (target->nodetype) {
1234 case LYS_CONTAINER:
1235 case LYS_LEAF:
1236 case LYS_LEAFLIST:
1237 case LYS_LIST:
1238 case LYS_CHOICE:
1239 case LYS_ANYDATA:
1240 case LYS_ANYXML:
1241 break;
1242 default:
1243 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1244 }
1245
1246 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001247 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1248 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 ret = LY_EVALID;
1250 goto cleanup;
1251 }
1252
1253 target->flags &= ~LYS_CONFIG_MASK;
1254 target->flags |= d->flags & LYS_CONFIG_MASK;
1255 }
1256
1257 /* [mandatory-stmt] */
1258 if (d->flags & LYS_MAND_MASK) {
1259 switch (target->nodetype) {
1260 case LYS_LEAF:
1261 case LYS_CHOICE:
1262 case LYS_ANYDATA:
1263 case LYS_ANYXML:
1264 break;
1265 default:
1266 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1267 }
1268
1269 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001270 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1271 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001272 ret = LY_EVALID;
1273 goto cleanup;
1274 }
1275
1276 target->flags &= ~LYS_MAND_MASK;
1277 target->flags |= d->flags & LYS_MAND_MASK;
1278 }
1279
1280 /* [min-elements-stmt] */
1281 if (d->flags & LYS_SET_MIN) {
1282 switch (target->nodetype) {
1283 case LYS_LEAFLIST:
1284 num = &((struct lysp_node_leaflist *)target)->min;
1285 break;
1286 case LYS_LIST:
1287 num = &((struct lysp_node_list *)target)->min;
1288 break;
1289 default:
1290 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1291 }
1292
1293 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001294 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001295 ret = LY_EVALID;
1296 goto cleanup;
1297 }
1298
1299 *num = d->min;
1300 }
1301
1302 /* [max-elements-stmt] */
1303 if (d->flags & LYS_SET_MAX) {
1304 switch (target->nodetype) {
1305 case LYS_LEAFLIST:
1306 num = &((struct lysp_node_leaflist *)target)->max;
1307 break;
1308 case LYS_LIST:
1309 num = &((struct lysp_node_list *)target)->max;
1310 break;
1311 default:
1312 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1313 }
1314
1315 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001316 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001317 ret = LY_EVALID;
1318 goto cleanup;
1319 }
1320
1321 *num = d->max;
1322 }
1323
1324cleanup:
1325 return ret;
1326}
1327
1328/**
1329 * @brief Get module of a single nodeid node name test.
1330 *
1331 * @param[in] ctx libyang context.
1332 * @param[in] nametest Nametest with an optional prefix.
1333 * @param[in] nametest_len Length of @p nametest.
1334 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1335 * @param[out] name Optional pointer to the name test without the prefix.
1336 * @param[out] name_len Length of @p name.
1337 * @return Resolved module.
1338 */
1339static const struct lys_module *
1340lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1341 const struct lysp_module *mod, const char **name, size_t *name_len)
1342{
1343 const struct lys_module *target_mod;
1344 const char *ptr;
1345
1346 ptr = ly_strnchr(nametest, ':', nametest_len);
1347 if (ptr) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001348 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_VALUE_SCHEMA, (void *)mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001349 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001350 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001351 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001352 (int)nametest_len, nametest, (int)(ptr - nametest), nametest, LYSP_MODULE_NAME(mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001353 return NULL;
1354 }
1355
1356 if (name) {
1357 *name = ptr + 1;
1358 *name_len = nametest_len - ((ptr - nametest) + 1);
1359 }
1360 } else {
1361 target_mod = mod->mod;
1362 if (name) {
1363 *name = nametest;
1364 *name_len = nametest_len;
1365 }
1366 }
1367
1368 return target_mod;
1369}
1370
1371/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001372 * @brief Check whether a compiled node matches a single schema nodeid name test.
1373 *
1374 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1375 * @param[in] mod Expected module.
1376 * @param[in] name Expected name.
1377 * @param[in] name_len Length of @p name.
1378 * @return Whether it is a match or not.
1379 */
1380static ly_bool
1381lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1382 size_t name_len)
1383{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001384 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001385 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001386 return 0;
1387 }
1388
1389 /* compare names */
Michal Vasko544e58a2021-01-28 14:33:41 +01001390 if (ly_strncmp((*node)->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001391 return 0;
1392 }
1393
Michal Vasko2a668712020-10-21 11:48:09 +02001394 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001395 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001396
1397 return 1;
1398}
1399
1400/**
1401 * @brief Check whether a node matches specific schema nodeid.
1402 *
1403 * @param[in] exp Parsed nodeid to match.
1404 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1405 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1406 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1407 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1408 * @param[in] pnode_mod Compiled @p pnode to-be module.
1409 * @return Whether it is a match or not.
1410 */
1411static ly_bool
1412lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1413 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1414{
1415 uint32_t i;
1416 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001417 const char *name = NULL;
1418 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001419
1420 /* compare last node in the node ID */
1421 i = exp->used - 1;
1422
1423 /* get exp node ID module */
1424 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);
1425 assert(mod);
1426
1427 if (pnode) {
1428 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001429 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001430 return 0;
1431 }
1432 } else {
1433 /* using parent directly */
1434 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1435 return 0;
1436 }
1437 }
1438
1439 /* now compare all the compiled parents */
1440 while (i > 1) {
1441 i -= 2;
1442 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1443
1444 if (!parent) {
1445 /* no more parents but path continues */
1446 return 0;
1447 }
1448
1449 /* get exp node ID module */
1450 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1451 &name_len);
1452 assert(mod);
1453
1454 /* compare with the parent */
1455 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1456 return 0;
1457 }
1458 }
1459
1460 if (ctx_node && (ctx_node != parent)) {
1461 /* descendant path has not finished in the context node */
1462 return 0;
1463 } else if (!ctx_node && parent) {
1464 /* some parent was not matched */
1465 return 0;
1466 }
1467
1468 return 1;
1469}
1470
1471void
1472lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1473{
1474 if (aug) {
1475 lyxp_expr_free(ctx, aug->nodeid);
1476
1477 free(aug);
1478 }
1479}
1480
1481void
1482lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1483{
1484 if (dev) {
1485 lyxp_expr_free(ctx, dev->nodeid);
1486 LY_ARRAY_FREE(dev->devs);
1487 LY_ARRAY_FREE(dev->dev_pmods);
1488
1489 free(dev);
1490 }
1491}
1492
1493void
1494lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1495{
1496 if (rfn) {
1497 lyxp_expr_free(ctx, rfn->nodeid);
1498 LY_ARRAY_FREE(rfn->rfns);
1499
1500 free(rfn);
1501 }
1502}
1503
1504void
1505lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1506{
1507 if (!dev_pnode) {
1508 return;
1509 }
1510
1511 switch (dev_pnode->nodetype) {
1512 case LYS_CONTAINER:
1513 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1514 break;
1515 case LYS_LIST:
1516 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1517 break;
1518 case LYS_CHOICE:
1519 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1520 break;
1521 case LYS_CASE:
1522 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1523 break;
1524 case LYS_LEAF:
1525 case LYS_LEAFLIST:
1526 case LYS_ANYXML:
1527 case LYS_ANYDATA:
1528 /* no children */
1529 break;
1530 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001531 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001532 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001533 case LYS_RPC:
1534 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001535 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1536 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001537 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001538 case LYS_INPUT:
1539 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001540 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001541 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001542 free(dev_pnode);
1543 return;
1544 default:
1545 LOGINT(ctx);
1546 return;
1547 }
1548
1549 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1550}
1551
1552LY_ERR
1553lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1554 struct lysp_node **dev_pnode, ly_bool *not_supported)
1555{
1556 LY_ERR ret = LY_SUCCESS;
1557 uint32_t i;
1558 LY_ARRAY_COUNT_TYPE u;
1559 struct lys_module *orig_mod = ctx->cur_mod;
1560 struct lysp_module *orig_pmod = ctx->pmod;
1561 char orig_path[LYSC_CTX_BUFSIZE];
1562 struct lysc_refine *rfn;
1563 struct lysc_deviation *dev;
1564 struct lysp_deviation *dev_p;
1565 struct lysp_deviate *d;
1566
1567 *dev_pnode = NULL;
1568 *not_supported = 0;
1569
1570 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1571 rfn = ctx->uses_rfns.objs[i];
1572
Michal Vasko28fe5f62021-03-03 16:37:39 +01001573 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001574 /* not our target node */
1575 continue;
1576 }
1577
1578 if (!*dev_pnode) {
1579 /* first refine on this node, create a copy first */
1580 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1581 }
1582
Michal Vaskob8df5762021-01-12 15:15:53 +01001583 /* use modules from the refine */
1584 ctx->cur_mod = rfn->nodeid_pmod->mod;
1585 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1586
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001587 /* apply all the refines by changing (the copy of) the parsed node */
1588 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001589 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001590 lysc_update_path(ctx, NULL, "{refine}");
1591 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001592
1593 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001594 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1595 lysc_update_path(ctx, NULL, NULL);
1596 lysc_update_path(ctx, NULL, NULL);
1597 LY_CHECK_GOTO(ret, cleanup);
1598 }
1599
1600 /* refine was applied, remove it */
1601 lysc_refine_free(ctx->ctx, rfn);
1602 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1603
1604 /* all the refines for one target node are in one structure, we are done */
1605 break;
1606 }
1607
1608 for (i = 0; i < ctx->devs.count; ++i) {
1609 dev = ctx->devs.objs[i];
1610
Michal Vasko28fe5f62021-03-03 16:37:39 +01001611 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, orig_mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001612 /* not our target node */
1613 continue;
1614 }
1615
1616 if (dev->not_supported) {
1617 /* it is not supported, no more deviations */
1618 *not_supported = 1;
1619 goto dev_applied;
1620 }
1621
1622 if (!*dev_pnode) {
1623 /* first deviation on this node, create a copy first */
1624 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1625 }
1626
1627 /* apply all the deviates by changing (the copy of) the parsed node */
1628 LY_ARRAY_FOR(dev->devs, u) {
1629 dev_p = dev->devs[u];
1630 LY_LIST_FOR(dev_p->deviates, d) {
1631 /* generate correct path */
1632 strcpy(orig_path, ctx->path);
1633 ctx->path_len = 1;
1634 ctx->cur_mod = dev->dev_pmods[u]->mod;
1635 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1636 lysc_update_path(ctx, NULL, "{deviation}");
1637 lysc_update_path(ctx, NULL, dev_p->nodeid);
1638
1639 switch (d->mod) {
1640 case LYS_DEV_ADD:
1641 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1642 break;
1643 case LYS_DEV_DELETE:
1644 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1645 break;
1646 case LYS_DEV_REPLACE:
1647 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1648 break;
1649 default:
1650 LOGINT(ctx->ctx);
1651 ret = LY_EINT;
1652 }
1653
1654 /* restore previous path */
1655 strcpy(ctx->path, orig_path);
1656 ctx->path_len = strlen(ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001657
1658 LY_CHECK_GOTO(ret, cleanup);
1659 }
1660 }
1661
1662dev_applied:
1663 /* deviation was applied, remove it */
1664 lysc_deviation_free(ctx->ctx, dev);
1665 ly_set_rm_index(&ctx->devs, i, NULL);
1666
1667 /* all the deviations for one target node are in one structure, we are done */
1668 break;
1669 }
1670
1671cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001672 ctx->cur_mod = orig_mod;
1673 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001674 if (ret) {
1675 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1676 *dev_pnode = NULL;
1677 *not_supported = 0;
1678 }
1679 return ret;
1680}
1681
1682/**
1683 * @brief Compile the parsed augment connecting it into its target.
1684 *
1685 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1686 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1687 * are already implemented and compiled.
1688 *
1689 * @param[in] ctx Compile context.
1690 * @param[in] aug_p Parsed augment to compile.
1691 * @param[in] target Target node of the augment.
1692 * @return LY_SUCCESS on success.
1693 * @return LY_EVALID on failure.
1694 */
1695static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001696lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001697{
1698 LY_ERR ret = LY_SUCCESS;
1699 struct lysp_node *pnode;
1700 struct lysc_node *node;
1701 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001702 struct lysc_node_action **actions;
1703 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001704 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001705 struct ly_set child_set = {0};
Michal Vasko29dd11e2020-11-23 16:52:22 +01001706 uint32_t i, opt_prev = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001707
1708 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001709 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001710 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1711 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1712 ret = LY_EVALID;
1713 goto cleanup;
1714 }
1715
1716 /* check for mandatory nodes
1717 * - new cases augmenting some choice can have mandatory nodes
1718 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1719 */
1720 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1721 allow_mandatory = 1;
1722 }
1723
1724 LY_LIST_FOR(aug_p->child, pnode) {
1725 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1726 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1727 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1728 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001729 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001730 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1731 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1732 ret = LY_EVALID;
1733 goto cleanup;
1734 }
1735
1736 /* compile the children */
1737 if (target->nodetype == LYS_CHOICE) {
1738 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001739 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1740 if (target->nodetype == LYS_INPUT) {
1741 ctx->options |= LYS_COMPILE_RPC_INPUT;
1742 } else {
1743 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
1744 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001745 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001746 } else {
1747 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1748 }
1749
Michal Vasko29dd11e2020-11-23 16:52:22 +01001750 /* eval if-features again for the rest of this node processing */
1751 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1752 if (!enabled) {
1753 ctx->options |= LYS_COMPILE_DISABLED;
1754 }
1755
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001756 /* since the augment node is not present in the compiled tree, we need to pass some of its
1757 * statements to all its children */
1758 for (i = 0; i < child_set.count; ++i) {
1759 node = child_set.snodes[i];
1760 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1761 node->flags &= ~LYS_MAND_TRUE;
1762 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001763 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001764 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1765 ret = LY_EVALID;
1766 goto cleanup;
1767 }
1768
1769 if (aug_p->when) {
1770 /* pass augment's when to all the children */
Michal Vasko72244882021-01-12 15:21:05 +01001771 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001772 LY_CHECK_GOTO(ret, cleanup);
1773 }
1774 }
1775 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001776
1777 /* restore options */
1778 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001779 }
1780
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001781 actions = lysc_node_actions_p(target);
1782 notifs = lysc_node_notifs_p(target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001783
1784 if (aug_p->actions) {
1785 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001786 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001787 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001788 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001789 ret = LY_EVALID;
1790 goto cleanup;
1791 }
1792
1793 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001794 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001795 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001796
Michal Vasko012807e2021-02-03 09:52:18 +01001797 /* eval if-features again for the rest of this node processing */
1798 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1799 if (!enabled) {
1800 ctx->options |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001801 }
Michal Vasko012807e2021-02-03 09:52:18 +01001802
1803 /* since the augment node is not present in the compiled tree, we need to pass some of its
1804 * statements to all its children */
1805 for (i = 0; i < child_set.count; ++i) {
1806 node = child_set.snodes[i];
1807 if (aug_p->when) {
1808 /* pass augment's when to all the actions */
1809 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1810 LY_CHECK_GOTO(ret, cleanup);
1811 }
1812 }
1813 ly_set_erase(&child_set, NULL);
1814
1815 /* restore options */
1816 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001817 }
1818 }
1819 if (aug_p->notifs) {
1820 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001821 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001822 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001823 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001824 ret = LY_EVALID;
1825 goto cleanup;
1826 }
1827
1828 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001829 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001830 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001831
Michal Vasko012807e2021-02-03 09:52:18 +01001832 /* eval if-features again for the rest of this node processing */
1833 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1834 if (!enabled) {
1835 ctx->options |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001836 }
Michal Vasko012807e2021-02-03 09:52:18 +01001837
1838 /* since the augment node is not present in the compiled tree, we need to pass some of its
1839 * statements to all its children */
1840 for (i = 0; i < child_set.count; ++i) {
1841 node = child_set.snodes[i];
1842 if (aug_p->when) {
1843 /* pass augment's when to all the actions */
1844 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1845 LY_CHECK_GOTO(ret, cleanup);
1846 }
1847 }
1848 ly_set_erase(&child_set, NULL);
1849
1850 /* restore options */
1851 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001852 }
1853 }
1854
1855cleanup:
1856 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001857 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001858 return ret;
1859}
1860
1861LY_ERR
1862lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1863{
1864 LY_ERR ret = LY_SUCCESS;
1865 struct lys_module *orig_mod = ctx->cur_mod;
1866 struct lysp_module *orig_pmod = ctx->pmod;
1867 uint32_t i;
1868 char orig_path[LYSC_CTX_BUFSIZE];
1869 struct lysc_augment *aug;
1870
1871 /* uses augments */
1872 for (i = 0; i < ctx->uses_augs.count; ) {
1873 aug = ctx->uses_augs.objs[i];
1874
Michal Vasko28fe5f62021-03-03 16:37:39 +01001875 if (!lysp_schema_nodeid_match(aug->nodeid, orig_mod->parsed, aug->nodeid_ctx_node, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001876 /* not our target node */
1877 ++i;
1878 continue;
1879 }
1880
Michal Vaskob8df5762021-01-12 15:15:53 +01001881 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001882 lysc_update_path(ctx, NULL, "{augment}");
1883 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vasko28fe5f62021-03-03 16:37:39 +01001884 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001885
1886 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001887 ret = lys_compile_augment(ctx, aug->aug_p, node);
1888 lysc_update_path(ctx, NULL, NULL);
1889 lysc_update_path(ctx, NULL, NULL);
1890 LY_CHECK_GOTO(ret, cleanup);
1891
Michal Vaskoc75f2042021-06-08 14:57:03 +02001892 /* augment was applied, remove it (index and the whole set may have changed because other augments
1893 * could have been applied) */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001894 ly_set_rm(&ctx->uses_augs, aug, NULL);
1895 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001896 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001897 }
1898
1899 /* top-level augments */
1900 for (i = 0; i < ctx->augs.count; ) {
1901 aug = ctx->augs.objs[i];
1902
Michal Vasko28fe5f62021-03-03 16:37:39 +01001903 if (!lysp_schema_nodeid_match(aug->nodeid, aug->aug_pmod, NULL, node, NULL, NULL)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001904 /* not our target node */
1905 ++i;
1906 continue;
1907 }
1908
Michal Vaskob8df5762021-01-12 15:15:53 +01001909 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001910 strcpy(orig_path, ctx->path);
1911 ctx->path_len = 1;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001912 ctx->cur_mod = aug->aug_pmod->mod;
1913 ctx->pmod = (struct lysp_module *)aug->aug_pmod;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001914 lysc_update_path(ctx, NULL, "{augment}");
1915 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001916
1917 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001918 ret = lys_compile_augment(ctx, aug->aug_p, node);
1919 strcpy(ctx->path, orig_path);
1920 ctx->path_len = strlen(ctx->path);
1921 LY_CHECK_GOTO(ret, cleanup);
1922
1923 /* augment was applied, remove it */
1924 ly_set_rm(&ctx->augs, aug, NULL);
1925 lysc_augment_free(ctx->ctx, aug);
Michal Vaskoc75f2042021-06-08 14:57:03 +02001926 i = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001927 }
1928
1929cleanup:
1930 ctx->cur_mod = orig_mod;
1931 ctx->pmod = orig_pmod;
1932 return ret;
1933}
1934
1935/**
1936 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1937 *
1938 * @param[in] ctx Compile context.
1939 * @param[in] aug_p Parsed augment to be applied.
1940 * @param[in] pmod Both current and prefix module for @p aug_p.
1941 * @return LY_ERR value.
1942 */
1943static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001944lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, const struct lysp_module *pmod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001945{
1946 LY_ERR ret = LY_SUCCESS;
1947 struct lyxp_expr *exp = NULL;
1948 struct lysc_augment *aug;
1949 const struct lys_module *mod;
1950
1951 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1952 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
1953 LY_CHECK_GOTO(ret, cleanup);
1954
1955 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
1956 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
1957 if (mod != ctx->cur_mod) {
1958 /* augment for another module, ignore */
1959 goto cleanup;
1960 }
1961
1962 /* allocate new compiled augment and store it in the set */
1963 aug = calloc(1, sizeof *aug);
1964 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1965 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
1966
1967 aug->nodeid = exp;
1968 exp = NULL;
Michal Vasko28fe5f62021-03-03 16:37:39 +01001969 aug->aug_pmod = pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001970 aug->aug_p = aug_p;
1971
1972cleanup:
1973 lyxp_expr_free(ctx->ctx, exp);
1974 return ret;
1975}
1976
1977LY_ERR
1978lys_precompile_own_augments(struct lysc_ctx *ctx)
1979{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001980 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001981
1982 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001983 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
1984 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001985
1986 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001987 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
1988 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001989 }
1990
1991 /* collect all submodules augments */
1992 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001993 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
1994 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, (struct lysp_module *)aug_mod->parsed->includes[v].submodule));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001995 }
1996 }
1997 }
1998
1999 return LY_SUCCESS;
2000}
2001
2002/**
2003 * @brief Prepare a deviation to be applied during data nodes compilation.
2004 *
2005 * @param[in] ctx Compile context.
2006 * @param[in] dev_p Parsed deviation to be applied.
2007 * @param[in] pmod Both current and prefix module for @p dev_p.
2008 * @return LY_ERR value.
2009 */
2010static LY_ERR
2011lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2012{
2013 LY_ERR ret = LY_SUCCESS;
2014 struct lysc_deviation *dev = NULL;
2015 struct lyxp_expr *exp = NULL;
2016 struct lysp_deviation **new_dev;
2017 const struct lys_module *mod;
2018 const struct lysp_module **new_dev_pmod;
2019 uint32_t i;
2020
2021 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2022 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2023 LY_CHECK_GOTO(ret, cleanup);
2024
2025 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2026 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2027 if (mod != ctx->cur_mod) {
2028 /* deviation for another module, ignore */
2029 goto cleanup;
2030 }
2031
2032 /* try to find the node in already compiled deviations */
2033 for (i = 0; i < ctx->devs.count; ++i) {
2034 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2035 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2036 dev = ctx->devs.objs[i];
2037 break;
2038 }
2039 }
2040
2041 if (!dev) {
2042 /* allocate new compiled deviation */
2043 dev = calloc(1, sizeof *dev);
2044 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2045 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2046
2047 dev->nodeid = exp;
2048 exp = NULL;
2049 }
2050
2051 /* add new parsed deviation structure */
2052 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2053 *new_dev = dev_p;
2054 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2055 *new_dev_pmod = pmod;
2056
2057cleanup:
2058 lyxp_expr_free(ctx->ctx, exp);
2059 return ret;
2060}
2061
2062LY_ERR
2063lys_precompile_own_deviations(struct lysc_ctx *ctx)
2064{
2065 LY_ARRAY_COUNT_TYPE u, v, w;
Michal Vaskoe8220db2021-06-02 15:39:05 +02002066 struct lys_module *orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002067 const struct lys_module *dev_mod;
2068 struct lysc_deviation *dev;
2069 struct lysp_deviate *d;
2070 int not_supported;
2071 uint32_t i;
2072
2073 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2074 dev_mod = ctx->cur_mod->deviated_by[u];
2075
2076 /* compile all module deviations */
2077 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2078 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2079 }
2080
2081 /* compile all submodules deviations */
2082 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2083 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2084 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2085 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2086 }
2087 }
2088 }
2089
2090 /* set not-supported flags for all the deviations */
2091 for (i = 0; i < ctx->devs.count; ++i) {
2092 dev = ctx->devs.objs[i];
2093 not_supported = 0;
2094
2095 LY_ARRAY_FOR(dev->devs, u) {
2096 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2097 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2098 not_supported = 1;
2099 break;
2100 }
2101 }
2102 if (not_supported) {
2103 break;
2104 }
2105 }
2106 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Michal Vaskoe8220db2021-06-02 15:39:05 +02002107 orig_cur_mod = ctx->cur_mod;
2108 ctx->cur_mod = dev->dev_pmods[u]->mod;
2109 lysc_update_path(ctx, NULL, "{deviation}");
2110 lysc_update_path(ctx, NULL, dev->nodeid->expr);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002111 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002112 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
Michal Vaskoe8220db2021-06-02 15:39:05 +02002113 lysc_update_path(ctx, NULL, NULL);
2114 lysc_update_path(ctx, NULL, NULL);
2115 ctx->cur_mod = orig_cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002116 return LY_EVALID;
2117 }
2118
2119 dev->not_supported = not_supported;
2120 }
2121
2122 return LY_SUCCESS;
2123}
2124
2125/**
2126 * @brief Add a module reference into an array, checks for duplicities.
2127 *
2128 * @param[in] ctx Compile context.
2129 * @param[in] mod Module reference to add.
2130 * @param[in,out] mod_array Module sized array to add to.
2131 * @return LY_ERR value.
2132 */
2133static LY_ERR
2134lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2135{
2136 LY_ARRAY_COUNT_TYPE u;
2137 struct lys_module **new_mod;
2138
2139 LY_ARRAY_FOR(*mod_array, u) {
2140 if ((*mod_array)[u] == mod) {
2141 /* already there */
2142 return LY_EEXIST;
2143 }
2144 }
2145
2146 /* add the new module ref */
2147 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2148 *new_mod = mod;
2149
2150 return LY_SUCCESS;
2151}
2152
Michal Vasko1ccbf542021-04-19 11:35:00 +02002153/**
2154 * @brief Check whether all modules in a set are implemented.
2155 *
2156 * @param[in] mod_set Module set to check.
2157 * @return Whether all modules are implemented or not.
2158 */
2159static ly_bool
2160lys_precompile_mod_set_all_implemented(const struct ly_set *mod_set)
2161{
2162 uint32_t i;
2163 const struct lys_module *mod;
2164
2165 for (i = 0; i < mod_set->count; ++i) {
2166 mod = mod_set->objs[i];
2167 if (!mod->implemented) {
2168 return 0;
2169 }
2170 }
2171
2172 return 1;
2173}
2174
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002175LY_ERR
Michal Vasko65333882021-06-10 14:12:16 +02002176lys_precompile_augments_deviations(struct lys_module *mod, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002177{
Michal Vasko1ccbf542021-04-19 11:35:00 +02002178 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002179 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko65333882021-06-10 14:12:16 +02002180 struct lysc_ctx ctx = {0};
2181 struct lysp_module *mod_p;
2182 struct lys_module *m;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002183 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002184 struct lysp_node_augment *aug;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002185 uint32_t idx;
2186 struct ly_set mod_set = {0}, set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002187
Michal Vasko65333882021-06-10 14:12:16 +02002188 mod_p = mod->parsed;
2189
2190 /* prepare context */
2191 ctx.ctx = mod->ctx;
2192 ctx.cur_mod = mod;
2193 ctx.pmod = mod_p;
2194 ctx.path_len = 1;
2195 ctx.path[0] = '/';
2196 ctx.unres = unres;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002197
Radek Krejci2a9fc652021-01-22 17:44:34 +01002198 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002199 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002200 lysc_update_path(&ctx, NULL, "{augment}");
2201 lysc_update_path(&ctx, NULL, aug->nodeid);
2202 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2203 lysc_update_path(&ctx, NULL, NULL);
2204 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002205 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002206
Michal Vasko1ccbf542021-04-19 11:35:00 +02002207 /* add this module into the target module augmented_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002208 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vasko1ccbf542021-04-19 11:35:00 +02002209 !lys_precompile_mod_set_all_implemented(&set)) {
2210 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002211 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002212 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002213 }
2214
2215 LY_ARRAY_FOR(mod_p->deviations, u) {
2216 /* get target module */
Michal Vasko65333882021-06-10 14:12:16 +02002217 lysc_update_path(&ctx, NULL, "{deviation}");
2218 lysc_update_path(&ctx, NULL, mod_p->deviations[u].nodeid);
2219 ret = lys_nodeid_mod_check(&ctx, mod_p->deviations[u].nodeid, 1, &set, NULL, &m);
2220 lysc_update_path(&ctx, NULL, NULL);
2221 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002222 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002223
Michal Vasko1ccbf542021-04-19 11:35:00 +02002224 /* add this module into the target module deviated_by, if not there and implemented */
Michal Vasko65333882021-06-10 14:12:16 +02002225 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vasko1ccbf542021-04-19 11:35:00 +02002226 !lys_precompile_mod_set_all_implemented(&set)) {
2227 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2228 }
2229 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002230 }
2231
2232 /* the same for augments and deviations in submodules */
2233 LY_ARRAY_FOR(mod_p->includes, v) {
2234 submod = mod_p->includes[v].submodule;
Michal Vasko65333882021-06-10 14:12:16 +02002235 ctx.pmod = (struct lysp_module *)submod;
Michal Vaskoce3d6172021-06-08 14:59:49 +02002236
Radek Krejci2a9fc652021-01-22 17:44:34 +01002237 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko65333882021-06-10 14:12:16 +02002238 lysc_update_path(&ctx, NULL, "{augment}");
2239 lysc_update_path(&ctx, NULL, aug->nodeid);
2240 ret = lys_nodeid_mod_check(&ctx, aug->nodeid, 1, &set, NULL, &m);
2241 lysc_update_path(&ctx, NULL, NULL);
2242 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002243 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002244
Michal Vasko65333882021-06-10 14:12:16 +02002245 if ((lys_array_add_mod_ref(&ctx, mod, &m->augmented_by) != LY_EEXIST) ||
Michal Vasko1ccbf542021-04-19 11:35:00 +02002246 !lys_precompile_mod_set_all_implemented(&set)) {
2247 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002248 }
Michal Vasko1ccbf542021-04-19 11:35:00 +02002249 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002250 }
2251
2252 LY_ARRAY_FOR(submod->deviations, u) {
Michal Vasko65333882021-06-10 14:12:16 +02002253 lysc_update_path(&ctx, NULL, "{deviation}");
2254 lysc_update_path(&ctx, NULL, submod->deviations[u].nodeid);
2255 ret = lys_nodeid_mod_check(&ctx, submod->deviations[u].nodeid, 1, &set, NULL, &m);
2256 lysc_update_path(&ctx, NULL, NULL);
2257 lysc_update_path(&ctx, NULL, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02002258 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002259
Michal Vasko65333882021-06-10 14:12:16 +02002260 if ((lys_array_add_mod_ref(&ctx, mod, &m->deviated_by) != LY_EEXIST) ||
Michal Vasko1ccbf542021-04-19 11:35:00 +02002261 !lys_precompile_mod_set_all_implemented(&set)) {
2262 LY_CHECK_GOTO(ret = ly_set_merge(&mod_set, &set, 0, NULL), cleanup);
2263 }
2264 ly_set_erase(&set, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002265 }
2266 }
2267
Michal Vasko1ccbf542021-04-19 11:35:00 +02002268 if (mod_set.count) {
2269 /* descending order to make sure the modules are implemented in the right order */
2270 idx = mod_set.count;
2271 do {
2272 --idx;
Michal Vasko65333882021-06-10 14:12:16 +02002273 m = mod_set.objs[idx];
Michal Vasko1ccbf542021-04-19 11:35:00 +02002274
Michal Vasko65333882021-06-10 14:12:16 +02002275 if (m == mod) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002276 /* will be applied normally later */
2277 continue;
2278 }
2279
Michal Vasko65333882021-06-10 14:12:16 +02002280 if (!m->implemented) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002281 /* implement (compile) the target module with our augments/deviations */
Michal Vasko65333882021-06-10 14:12:16 +02002282 LY_CHECK_GOTO(ret = lys_implement(m, NULL, unres), cleanup);
2283 } else if (!unres->full_compilation) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02002284 /* target module was already compiled, we need to recompile it */
Michal Vasko65333882021-06-10 14:12:16 +02002285 ret = LY_ERECOMPILE;
2286 goto cleanup;
Michal Vasko1ccbf542021-04-19 11:35:00 +02002287 }
Michal Vaskoe0cd6d92021-04-23 13:44:40 +02002288 /* else the module is implemented and was compiled in this compilation run or will yet be;
2289 * we actually do not need the module compiled now because its compiled nodes will not be accessed,
2290 * augments/deviations are applied during the target module compilation and the rest is in global unres */
Michal Vasko1ccbf542021-04-19 11:35:00 +02002291
Michal Vasko1ccbf542021-04-19 11:35:00 +02002292 } while (idx);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002293 }
2294
Michal Vasko1ccbf542021-04-19 11:35:00 +02002295cleanup:
2296 ly_set_erase(&set, NULL);
2297 ly_set_erase(&mod_set, NULL);
2298 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002299}
2300
2301void
2302lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2303{
2304 uint32_t i;
2305 LY_ARRAY_COUNT_TYPE u, count;
2306 struct lys_module *m;
2307
2308 for (i = 0; i < ctx->list.count; ++i) {
2309 m = ctx->list.objs[i];
2310
2311 if (m->augmented_by) {
2312 count = LY_ARRAY_COUNT(m->augmented_by);
2313 for (u = 0; u < count; ++u) {
2314 if (m->augmented_by[u] == mod) {
2315 /* keep the order */
2316 if (u < count - 1) {
2317 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2318 }
2319 LY_ARRAY_DECREMENT(m->augmented_by);
2320 break;
2321 }
2322 }
2323 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2324 LY_ARRAY_FREE(m->augmented_by);
2325 m->augmented_by = NULL;
2326 }
2327 }
2328
2329 if (m->deviated_by) {
2330 count = LY_ARRAY_COUNT(m->deviated_by);
2331 for (u = 0; u < count; ++u) {
2332 if (m->deviated_by[u] == mod) {
2333 /* keep the order */
2334 if (u < count - 1) {
2335 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2336 }
2337 LY_ARRAY_DECREMENT(m->deviated_by);
2338 break;
2339 }
2340 }
2341 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2342 LY_ARRAY_FREE(m->deviated_by);
2343 m->deviated_by = NULL;
2344 }
2345 }
2346 }
2347}