blob: 7a9a97ebc18586ebf9933e87efee0bf9a45dc834 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko0b50f6b2022-10-05 15:07:55 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02005 * @brief Schema compilation of common nodes.
6 *
Michal Vaskod595eff2023-02-20 11:29:28 +01007 * Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Christian Hopps32874e12021-05-01 09:43:54 -040016#define _GNU_SOURCE /* asprintf, strdup */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020017
18#include "schema_compile_node.h"
19
20#include <assert.h>
21#include <ctype.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "common.h"
29#include "compat.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020030#include "dict.h"
31#include "log.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020032#include "plugins.h"
Radek Krejci04699f02021-03-22 21:50:29 +010033#include "plugins_internal.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020034#include "plugins_types.h"
35#include "schema_compile.h"
36#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010037#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "set.h"
39#include "tree.h"
40#include "tree_data.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020042#include "tree_schema.h"
43#include "tree_schema_internal.h"
44#include "xpath.h"
45
Michal Vaskod595eff2023-02-20 11:29:28 +010046/**
47 * @brief Item for storing typedef chain item.
48 */
49struct lys_type_item {
50 const struct lysp_tpdf *tpdf;
51 struct lysp_node *node;
52};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020053
54/**
Michal Vasko4ed3bd52022-12-02 10:28:04 +010055 * @brief Add a node with a when to unres.
56 *
57 * @param[in] ctx Compile context.
58 * @param[in] when Specific compiled when to check.
59 * @param[in] node Compiled node with when(s).
60 * @return LY_ERR value.
61 */
62static LY_ERR
63lysc_unres_when_add(struct lysc_ctx *ctx, struct lysc_when *when, struct lysc_node *node)
64{
65 LY_ERR rc = LY_SUCCESS;
66 struct lysc_unres_when *w = NULL;
67
68 /* do not check must(s) in a grouping */
69 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
70 goto cleanup;
71 }
72
73 /* add new unres when */
74 w = calloc(1, sizeof *w);
75 LY_CHECK_ERR_GOTO(!w, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
76
77 w->node = node;
78 w->when = when;
79
80 /* add into the unres set */
81 LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->whens, w, 1, NULL), LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
82 w = NULL;
83
84cleanup:
85 free(w);
86 return rc;
87}
88
89/**
Michal Vaskoc130e162021-10-19 11:30:00 +020090 * @brief Add a node with must(s) to unres.
91 *
92 * @param[in] ctx Compile context.
93 * @param[in] node Compiled node with must(s).
94 * @param[in] pnode Parsed ndoe with must(s).
95 * @return LY_ERR value.
96 */
97static LY_ERR
98lysc_unres_must_add(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_node *pnode)
99{
100 struct lysc_unres_must *m = NULL;
101 LY_ARRAY_COUNT_TYPE u;
102 struct lysc_must *musts;
103 struct lysp_restr *pmusts;
104 LY_ERR ret;
105
106 /* do not check must(s) in a grouping */
107 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
108 return LY_SUCCESS;
109 }
110
111 musts = lysc_node_musts(node);
112 pmusts = lysp_node_musts(pnode);
113 assert(LY_ARRAY_COUNT(musts) == LY_ARRAY_COUNT(pmusts));
114
115 if (!musts) {
116 /* no must */
117 return LY_SUCCESS;
118 }
119
120 /* add new unres must */
121 m = calloc(1, sizeof *m);
122 LY_CHECK_ERR_GOTO(!m, ret = LY_EMEM, error);
123 m->node = node;
124
125 /* add must local modules */
126 LY_ARRAY_CREATE_GOTO(ctx->ctx, m->local_mods, LY_ARRAY_COUNT(pmusts), ret, error);
127 LY_ARRAY_FOR(pmusts, u) {
128 m->local_mods[u] = pmusts[u].arg.mod;
129 LY_ARRAY_INCREMENT(m->local_mods);
130 }
131
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200132 /* store ext */
133 m->ext = ctx->ext;
134
Michal Vaskoc130e162021-10-19 11:30:00 +0200135 LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->musts, m, 1, NULL), ret = LY_EMEM, error);
136
137 return LY_SUCCESS;
138
139error:
140 if (m) {
141 LY_ARRAY_FREE(m->local_mods);
142 free(m);
143 }
144 LOGMEM(ctx->ctx);
145 return ret;
146}
147
148static LY_ERR
149lysc_unres_leafref_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const struct lysp_module *local_mod)
150{
151 struct lysc_unres_leafref *l = NULL;
152 struct ly_set *leafrefs_set;
153 LY_ARRAY_COUNT_TYPE u;
154 int is_lref = 0;
155
156 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
157 /* do not check leafrefs in groupings */
158 return LY_SUCCESS;
159 }
160
161 /* use special set for disabled leafrefs */
162 leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
163
164 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
165 /* leafref */
166 is_lref = 1;
167 } else if (leaf->type->basetype == LY_TYPE_UNION) {
168 /* union with leafrefs */
169 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
170 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
171 is_lref = 1;
172 break;
173 }
174 }
175 }
176
177 if (is_lref) {
178 /* add new unresolved leafref node */
179 l = calloc(1, sizeof *l);
180 LY_CHECK_ERR_RET(!l, LOGMEM(ctx->ctx), LY_EMEM);
181
182 l->node = &leaf->node;
183 l->local_mod = local_mod;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200184 l->ext = ctx->ext;
Michal Vaskoc130e162021-10-19 11:30:00 +0200185
186 LY_CHECK_ERR_RET(ly_set_add(leafrefs_set, l, 1, NULL), free(l); LOGMEM(ctx->ctx), LY_EMEM);
187 }
188
189 return LY_SUCCESS;
190}
191
192/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200193 * @brief Add/replace a leaf default value in unres.
194 * Can also be used for a single leaf-list default value.
195 *
196 * @param[in] ctx Compile context.
197 * @param[in] leaf Leaf with the default value.
198 * @param[in] dflt Default value to use.
199 * @return LY_ERR value.
200 */
201static LY_ERR
202lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
203{
204 struct lysc_unres_dflt *r = NULL;
205 uint32_t i;
206
Michal Vasko7c565922021-06-10 14:58:27 +0200207 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100208 return LY_SUCCESS;
209 }
210
Michal Vasko405cc9e2020-12-01 12:01:27 +0100211 for (i = 0; i < ctx->unres->dflts.count; ++i) {
212 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200213 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100214 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200215 lysp_qname_free(ctx->ctx, r->dflt);
216 free(r->dflt);
217 break;
218 }
219 }
220 if (!r) {
221 /* add new unres item */
222 r = calloc(1, sizeof *r);
223 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
224 r->leaf = leaf;
225
Michal Vasko405cc9e2020-12-01 12:01:27 +0100226 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200227 }
228
229 r->dflt = malloc(sizeof *r->dflt);
230 LY_CHECK_GOTO(!r->dflt, error);
Michal Vaskoc621d862022-11-08 14:23:45 +0100231 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, dflt, r->dflt), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200232
233 return LY_SUCCESS;
234
235error:
236 free(r->dflt);
237 LOGMEM(ctx->ctx);
238 return LY_EMEM;
239}
240
241/**
242 * @brief Add/replace a leaf-list default value(s) in unres.
243 *
244 * @param[in] ctx Compile context.
245 * @param[in] llist Leaf-list with the default value.
246 * @param[in] dflts Sized array of the default values.
247 * @return LY_ERR value.
248 */
249static LY_ERR
250lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
251{
252 struct lysc_unres_dflt *r = NULL;
253 uint32_t i;
254
Michal Vasko7c565922021-06-10 14:58:27 +0200255 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100256 return LY_SUCCESS;
257 }
258
Michal Vasko405cc9e2020-12-01 12:01:27 +0100259 for (i = 0; i < ctx->unres->dflts.count; ++i) {
260 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200261 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100262 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200263 lysp_qname_free(ctx->ctx, r->dflt);
264 free(r->dflt);
265 r->dflt = NULL;
266 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
267 r->dflts = NULL;
268 break;
269 }
270 }
271 if (!r) {
272 r = calloc(1, sizeof *r);
273 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
274 r->llist = llist;
275
Michal Vasko405cc9e2020-12-01 12:01:27 +0100276 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200277 }
278
279 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
280
281 return LY_SUCCESS;
282}
283
284/**
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100285 * @brief Add a bits/enumeration type to unres.
286 *
287 * @param[in] ctx Compile context.
288 * @param[in] leaf Leaf of type bits/enumeration whose disabled items to free.
289 * @return LY_ERR value.
290 */
291static LY_ERR
292lysc_unres_bitenum_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
293{
294 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
295 /* skip groupings and redundant for disabled nodes */
296 return LY_SUCCESS;
297 }
298
299 LY_CHECK_RET(ly_set_add(&ctx->unres->disabled_bitenums, leaf, 1, NULL));
300
301 return LY_SUCCESS;
302}
303
304/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200305 * @brief Duplicate the compiled pattern structure.
306 *
307 * Instead of duplicating memory, the reference counter in the @p orig is increased.
308 *
309 * @param[in] orig The pattern structure to duplicate.
310 * @return The duplicated structure to use.
311 */
312static struct lysc_pattern *
313lysc_pattern_dup(struct lysc_pattern *orig)
314{
315 ++orig->refcount;
316 return orig;
317}
318
319/**
320 * @brief Duplicate the array of compiled patterns.
321 *
322 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
323 *
324 * @param[in] ctx Libyang context for logging.
325 * @param[in] orig The patterns sized array to duplicate.
326 * @return New sized array as a copy of @p orig.
327 * @return NULL in case of memory allocation error.
328 */
329static struct lysc_pattern **
330lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
331{
332 struct lysc_pattern **dup = NULL;
333 LY_ARRAY_COUNT_TYPE u;
334
335 assert(orig);
336
337 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
338 LY_ARRAY_FOR(orig, u) {
339 dup[u] = lysc_pattern_dup(orig[u]);
340 LY_ARRAY_INCREMENT(dup);
341 }
342 return dup;
343}
344
345/**
346 * @brief Duplicate compiled range structure.
347 *
Michal Vaskod595eff2023-02-20 11:29:28 +0100348 * @param[in] ctx Compile context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200349 * @param[in] orig The range structure to be duplicated.
Michal Vaskod595eff2023-02-20 11:29:28 +0100350 * @param[in] tpdf_chain Chain of the used typedefs, traversed backwards.
351 * @param[in] tpdf_chain_last Index of the last (backwards) typedef in @p tpdf_chain to use.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200352 * @return New compiled range structure as a copy of @p orig.
353 * @return NULL in case of memory allocation error.
354 */
355static struct lysc_range *
Michal Vaskod595eff2023-02-20 11:29:28 +0100356lysc_range_dup(struct lysc_ctx *ctx, const struct lysc_range *orig, struct ly_set *tpdf_chain, uint32_t tpdf_chain_last)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200357{
358 struct lysc_range *dup;
359 LY_ERR ret;
Michal Vaskod595eff2023-02-20 11:29:28 +0100360 struct lys_type_item *tpdf_item;
361 uint32_t i;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200362
363 assert(orig);
364
365 dup = calloc(1, sizeof *dup);
Michal Vaskod595eff2023-02-20 11:29:28 +0100366 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx->ctx), NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200367 if (orig->parts) {
Michal Vaskod595eff2023-02-20 11:29:28 +0100368 LY_ARRAY_CREATE_GOTO(ctx->ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100369 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200370 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
371 }
Michal Vaskod595eff2023-02-20 11:29:28 +0100372 DUP_STRING_GOTO(ctx->ctx, orig->eapptag, dup->eapptag, ret, cleanup);
373 DUP_STRING_GOTO(ctx->ctx, orig->emsg, dup->emsg, ret, cleanup);
374
375 /* collect all range extensions */
376 if (tpdf_chain->count > tpdf_chain_last) {
377 i = tpdf_chain->count;
378 do {
379 --i;
380 tpdf_item = tpdf_chain->objs[i];
381 if (!tpdf_item->tpdf->type.range) {
382 continue;
383 }
384 COMPILE_EXTS_GOTO(ctx, tpdf_item->tpdf->type.range->exts, dup->exts, dup, ret, cleanup);
385 } while (i > tpdf_chain_last);
386 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200387
388 return dup;
Michal Vaskod595eff2023-02-20 11:29:28 +0100389
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200390cleanup:
391 free(dup);
392 (void) ret; /* set but not used due to the return type */
393 return NULL;
394}
395
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200396/**
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200397 * @brief Print status into a string.
398 *
399 * @param[in] flags Flags with the status to print.
400 * @return String status.
401 */
402static const char *
403lys_status2str(uint16_t flags)
404{
405 flags &= LYS_STATUS_MASK;
406
407 switch (flags) {
408 case 0:
409 case LYS_STATUS_CURR:
410 return "current";
411 case LYS_STATUS_DEPRC:
412 return "deprecated";
413 case LYS_STATUS_OBSLT:
414 return "obsolete";
415 default:
416 LOGINT(NULL);
417 return NULL;
418 }
419}
420
421/**
Michal Vaskodfd254d2021-06-24 09:24:59 +0200422 * @brief Compile status information of the given statement.
423 *
424 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
425 * has the status correctly set during the compilation.
426 *
427 * @param[in] ctx Compile context
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200428 * @param[in] parsed_flags Parsed statement flags.
429 * @param[in] inherited_flags Parsed inherited flags from a schema-only statement (augment, uses, ext instance, ...).
430 * @param[in] parent_flags Compiled parent node flags.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200431 * @param[in] parent_name Name of the parent node, for logging.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200432 * @param[in] stmt_name Statement name, for logging.
433 * @param[in,out] stmt_flags Statement flags with the correct status set.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200434 * @return LY_ERR value.
435 */
436static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200437lys_compile_status(struct lysc_ctx *ctx, uint16_t parsed_flags, uint16_t inherited_flags, uint16_t parent_flags,
438 const char *parent_name, const char *stmt_name, uint16_t *stmt_flags)
Michal Vaskodfd254d2021-06-24 09:24:59 +0200439{
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200440 /* normalize to status-only */
441 parsed_flags &= LYS_STATUS_MASK;
442 inherited_flags &= LYS_STATUS_MASK;
443 parent_flags &= LYS_STATUS_MASK;
444
445 /* check for conflicts */
446 if (parent_flags && parsed_flags && (parent_flags > parsed_flags)) {
447 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Status \"%s\" of \"%s\" is in conflict with \"%s\" status of parent \"%s\".",
448 lys_status2str(parsed_flags), stmt_name, lys_status2str(parent_flags), parent_name);
449 return LY_EVALID;
450 } else if (inherited_flags && parsed_flags && (inherited_flags > parsed_flags)) {
451 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Inherited schema-only status \"%s\" is in conflict with \"%s\" status of \"%s\".",
452 lys_status2str(inherited_flags), lys_status2str(parsed_flags), stmt_name);
453 return LY_EVALID;
454 } else if (parent_flags && inherited_flags && (parent_flags > inherited_flags)) {
455 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Status \"%s\" of parent \"%s\" is in conflict with inherited schema-only status \"%s\".",
456 lys_status2str(parent_flags), parent_name, lys_status2str(inherited_flags));
457 return LY_EVALID;
Michal Vaskodfd254d2021-06-24 09:24:59 +0200458 }
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200459
460 /* clear */
461 (*stmt_flags) &= ~LYS_STATUS_MASK;
462
463 if (parsed_flags) {
464 /* explicit status */
465 (*stmt_flags) |= parsed_flags;
466 } else if (inherited_flags) {
467 /* inherited status from a schema-only statement */
468 (*stmt_flags) |= inherited_flags;
469 } else if (parent_flags) {
470 /* inherited status from a parent node */
471 (*stmt_flags) |= parent_flags;
472 } else {
473 /* default status */
474 (*stmt_flags) |= LYS_STATUS_CURR;
475 }
476
Michal Vaskodfd254d2021-06-24 09:24:59 +0200477 return LY_SUCCESS;
478}
479
480/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200481 * @brief Compile information from the when statement
482 *
483 * @param[in] ctx Compile context.
484 * @param[in] when_p Parsed when structure.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200485 * @param[in] inherited_flags Inherited flags from a schema-only statement.
486 * @param[in] parent Parent node, if any.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200487 * @param[in] ctx_node Context node for the when statement.
488 * @param[out] when Pointer where to store pointer to the created compiled when structure.
489 * @return LY_ERR value.
490 */
491static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200492lys_compile_when_(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags,
493 const struct lysc_node *parent, const struct lysc_node *ctx_node, struct lysc_when **when)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200494{
495 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200496 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200497
498 *when = calloc(1, sizeof **when);
499 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
500 (*when)->refcount = 1;
501 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200502 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200503 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200504 (*when)->context = (struct lysc_node *)ctx_node;
505 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
506 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100507 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200508 LY_CHECK_RET(lys_compile_status(ctx, 0, inherited_flags, parent ? parent->flags : 0, parent ? parent->name : NULL,
509 "when", &(*when)->flags));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200510
511done:
512 return ret;
513}
514
515LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200516lys_compile_when(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags, const struct lysc_node *parent,
517 const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200518{
Michal Vasko193dacd2022-10-13 08:43:05 +0200519 LY_ERR rc = LY_SUCCESS;
520 struct lysc_when **new_when, ***node_when, *ptr;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200521
Michal Vasko193dacd2022-10-13 08:43:05 +0200522 assert(when_p && (node || when_c));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200523
Michal Vasko193dacd2022-10-13 08:43:05 +0200524 if (node) {
525 /* get the when array */
526 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200527
Michal Vasko193dacd2022-10-13 08:43:05 +0200528 /* create new when pointer */
529 LY_ARRAY_NEW_GOTO(ctx->ctx, *node_when, new_when, rc, cleanup);
530 } else {
531 /* individual when */
532 new_when = &ptr;
533 *new_when = calloc(1, sizeof **new_when);
534 LY_CHECK_ERR_GOTO(!*new_when, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
535 }
536
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200537 if (!when_c || !(*when_c)) {
538 /* compile when */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200539 LY_CHECK_GOTO(rc = lys_compile_when_(ctx, when_p, inherited_flags, parent, ctx_node, new_when), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200540
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200541 /* remember the compiled when for sharing */
542 if (when_c) {
543 *when_c = *new_when;
544 }
545 } else {
546 /* use the previously compiled when */
547 ++(*when_c)->refcount;
548 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100549 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200550
Michal Vasko4ed3bd52022-12-02 10:28:04 +0100551 /* add when to unres */
552 LY_CHECK_GOTO(rc = lysc_unres_when_add(ctx, *new_when, node), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200553
Michal Vasko193dacd2022-10-13 08:43:05 +0200554cleanup:
555 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200556}
557
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200558LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200559lys_compile_must(struct lysc_ctx *ctx, const struct lysp_restr *must_p, struct lysc_must *must)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200560{
561 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200562 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200563
564 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200565 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
Radek Krejci8df109d2021-04-23 12:19:08 +0200566 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200567 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
568 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
569 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
570 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100571 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200572
573done:
574 return ret;
575}
576
577/**
578 * @brief Validate and normalize numeric value from a range definition.
579 * @param[in] ctx Compile context.
580 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
581 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
582 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
583 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
584 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
585 * @param[in] value String value of the range boundary.
586 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
587 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
588 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
589 */
590static LY_ERR
591range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
592 size_t *len, char **valcopy)
593{
594 size_t fraction = 0, size;
595
596 *len = 0;
597
598 assert(value);
599 /* parse value */
600 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
601 return LY_EVALID;
602 }
603
604 if ((value[*len] == '-') || (value[*len] == '+')) {
605 ++(*len);
606 }
607
608 while (isdigit(value[*len])) {
609 ++(*len);
610 }
611
612 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
613 if (basetype == LY_TYPE_DEC64) {
614 goto decimal;
615 } else {
616 *valcopy = strndup(value, *len);
617 return LY_SUCCESS;
618 }
619 }
620 fraction = *len;
621
622 ++(*len);
623 while (isdigit(value[*len])) {
624 ++(*len);
625 }
626
627 if (basetype == LY_TYPE_DEC64) {
628decimal:
629 assert(frdigits);
630 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100631 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200632 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100633 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200634 return LY_EINVAL;
635 }
636 if (fraction) {
637 size = (*len) + (frdigits - ((*len) - 1 - fraction));
638 } else {
639 size = (*len) + frdigits + 1;
640 }
641 *valcopy = malloc(size * sizeof **valcopy);
642 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
643
644 (*valcopy)[size - 1] = '\0';
645 if (fraction) {
646 memcpy(&(*valcopy)[0], &value[0], fraction);
647 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
648 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
649 } else {
650 memcpy(&(*valcopy)[0], &value[0], *len);
651 memset(&(*valcopy)[*len], '0', frdigits);
652 }
653 }
654 return LY_SUCCESS;
655}
656
657/**
658 * @brief Check that values in range are in ascendant order.
659 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
660 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
661 * max can be also equal.
662 * @param[in] value Current value to check.
663 * @param[in] prev_value The last seen value.
664 * @return LY_SUCCESS or LY_EEXIST for invalid order.
665 */
666static LY_ERR
667range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
668{
669 if (unsigned_value) {
670 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
671 return LY_EEXIST;
672 }
673 } else {
674 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
675 return LY_EEXIST;
676 }
677 }
678 return LY_SUCCESS;
679}
680
681/**
682 * @brief Set min/max value of the range part.
683 * @param[in] ctx Compile context.
684 * @param[in] part Range part structure to fill.
685 * @param[in] max Flag to distinguish if storing min or max value.
686 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
687 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
688 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
689 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
690 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
691 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
692 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
693 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
694 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
695 * frdigits value), LY_EMEM.
696 */
697static LY_ERR
698range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
699 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
700{
701 LY_ERR ret = LY_SUCCESS;
702 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100703 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200704
705 if (value) {
706 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
707 LY_CHECK_GOTO(ret, finalize);
708 }
709 if (!valcopy && base_range) {
710 if (max) {
711 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
712 } else {
713 part->min_64 = base_range->parts[0].min_64;
714 }
715 if (!first) {
716 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
717 }
718 goto finalize;
719 }
720
721 switch (basetype) {
722 case LY_TYPE_INT8: /* range */
723 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100724 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200725 } else if (max) {
726 part->max_64 = INT64_C(127);
727 } else {
728 part->min_64 = INT64_C(-128);
729 }
730 if (!ret && !first) {
731 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
732 }
733 break;
734 case LY_TYPE_INT16: /* range */
735 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100736 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
737 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200738 } else if (max) {
739 part->max_64 = INT64_C(32767);
740 } else {
741 part->min_64 = INT64_C(-32768);
742 }
743 if (!ret && !first) {
744 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
745 }
746 break;
747 case LY_TYPE_INT32: /* range */
748 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100749 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
750 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200751 } else if (max) {
752 part->max_64 = INT64_C(2147483647);
753 } else {
754 part->min_64 = INT64_C(-2147483648);
755 }
756 if (!ret && !first) {
757 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
758 }
759 break;
760 case LY_TYPE_INT64: /* range */
761 case LY_TYPE_DEC64: /* range */
762 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100763 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
764 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200765 } else if (max) {
766 part->max_64 = INT64_C(9223372036854775807);
767 } else {
768 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
769 }
770 if (!ret && !first) {
771 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
772 }
773 break;
774 case LY_TYPE_UINT8: /* range */
775 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100776 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200777 } else if (max) {
778 part->max_u64 = UINT64_C(255);
779 } else {
780 part->min_u64 = UINT64_C(0);
781 }
782 if (!ret && !first) {
783 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
784 }
785 break;
786 case LY_TYPE_UINT16: /* range */
787 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100788 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200789 } else if (max) {
790 part->max_u64 = UINT64_C(65535);
791 } else {
792 part->min_u64 = UINT64_C(0);
793 }
794 if (!ret && !first) {
795 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
796 }
797 break;
798 case LY_TYPE_UINT32: /* range */
799 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100800 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
801 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200802 } else if (max) {
803 part->max_u64 = UINT64_C(4294967295);
804 } else {
805 part->min_u64 = UINT64_C(0);
806 }
807 if (!ret && !first) {
808 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
809 }
810 break;
811 case LY_TYPE_UINT64: /* range */
812 case LY_TYPE_STRING: /* length */
813 case LY_TYPE_BINARY: /* length */
814 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100815 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
816 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200817 } else if (max) {
818 part->max_u64 = UINT64_C(18446744073709551615);
819 } else {
820 part->min_u64 = UINT64_C(0);
821 }
822 if (!ret && !first) {
823 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
824 }
825 break;
826 default:
827 LOGINT(ctx->ctx);
828 ret = LY_EINT;
829 }
830
831finalize:
832 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100833 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200834 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
835 length_restr ? "length" : "range", valcopy ? valcopy : *value);
836 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100837 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200838 "Invalid %s restriction - invalid value \"%s\".",
839 length_restr ? "length" : "range", valcopy ? valcopy : *value);
840 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100841 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200842 "Invalid %s restriction - values are not in ascending order (%s).",
843 length_restr ? "length" : "range",
844 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
845 } else if (!ret && value) {
846 *value = *value + len;
847 }
848 free(valcopy);
849 return ret;
850}
851
Michal Vasko193dacd2022-10-13 08:43:05 +0200852LY_ERR
853lys_compile_type_range(struct lysc_ctx *ctx, const struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200854 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
855{
aPiecek1c4da362021-04-29 14:26:34 +0200856 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200857 const char *expr;
858 struct lysc_range_part *parts = NULL, *part;
859 ly_bool range_expected = 0, uns;
860 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
861
862 assert(range);
863 assert(range_p);
864
865 expr = range_p->arg.str;
866 while (1) {
867 if (isspace(*expr)) {
868 ++expr;
869 } else if (*expr == '\0') {
870 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100871 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200872 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200873 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200874 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200875 goto cleanup;
876 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100877 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200878 "Invalid %s restriction - unexpected end of the expression (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200879 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200880 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200881 goto cleanup;
882 }
883 parts_done++;
884 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100885 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200886 if (parts) {
887 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100888 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200889 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100890 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200891 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200892 goto cleanup;
893 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100894 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200895
896 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200897 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200898 part->max_64 = part->min_64;
899 } else if (*expr == '|') {
900 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100901 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200902 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200903 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200904 goto cleanup;
905 }
906 expr++;
907 parts_done++;
908 /* process next part of the expression */
909 } else if (!strncmp(expr, "..", 2)) {
910 expr += 2;
911 while (isspace(*expr)) {
912 expr++;
913 }
914 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100915 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200916 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200917 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200918 goto cleanup;
919 }
920 /* continue expecting the upper boundary */
921 range_expected = 1;
922 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
923 /* number */
924 if (range_expected) {
925 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200926 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200927 range_expected = 0;
928 } else {
929 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200930 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200931 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
932 part->max_64 = part->min_64;
933 }
934
935 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100936 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
937 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200938 while (isspace(*expr)) {
939 expr++;
940 }
941 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100942 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200943 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200944 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200945 goto cleanup;
946 }
947 if (range_expected) {
948 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200949 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200950 range_expected = 0;
951 } else {
952 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200953 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200954 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
955 part->min_64 = part->max_64;
956 }
957 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100958 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200959 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200960 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200961 goto cleanup;
962 }
963 }
964
965 /* check with the previous range/length restriction */
966 if (base_range) {
967 switch (basetype) {
968 case LY_TYPE_BINARY:
969 case LY_TYPE_UINT8:
970 case LY_TYPE_UINT16:
971 case LY_TYPE_UINT32:
972 case LY_TYPE_UINT64:
973 case LY_TYPE_STRING:
974 uns = 1;
975 break;
976 case LY_TYPE_DEC64:
977 case LY_TYPE_INT8:
978 case LY_TYPE_INT16:
979 case LY_TYPE_INT32:
980 case LY_TYPE_INT64:
981 uns = 0;
982 break;
983 default:
984 LOGINT(ctx->ctx);
985 ret = LY_EINT;
986 goto cleanup;
987 }
988 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
989 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
990 goto baseerror;
991 }
992 /* current lower bound is not lower than the base */
993 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
994 /* base has single value */
995 if (base_range->parts[v].min_64 == parts[u].min_64) {
996 /* both lower bounds are the same */
997 if (parts[u].min_64 != parts[u].max_64) {
998 /* current continues with a range */
999 goto baseerror;
1000 } else {
1001 /* equal single values, move both forward */
1002 ++v;
1003 continue;
1004 }
1005 } else {
1006 /* base is single value lower than current range, so the
1007 * value from base range is removed in the current,
1008 * move only base and repeat checking */
1009 ++v;
1010 --u;
1011 continue;
1012 }
1013 } else {
1014 /* base is the range */
1015 if (parts[u].min_64 == parts[u].max_64) {
1016 /* current is a single value */
1017 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
1018 /* current is behind the base range, so base range is omitted,
1019 * move the base and keep the current for further check */
1020 ++v;
1021 --u;
1022 } /* else it is within the base range, so move the current, but keep the base */
1023 continue;
1024 } else {
1025 /* both are ranges - check the higher bound, the lower was already checked */
1026 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
1027 /* higher bound is higher than the current higher bound */
1028 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
1029 /* but the current lower bound is also higher, so the base range is omitted,
1030 * continue with the same current, but move the base */
1031 --u;
1032 ++v;
1033 continue;
1034 }
1035 /* current range starts within the base range but end behind it */
1036 goto baseerror;
1037 } else {
1038 /* current range is smaller than the base,
1039 * move current, but stay with the base */
1040 continue;
1041 }
1042 }
1043 }
1044 }
1045 if (u != parts_done) {
1046baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001047 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001048 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
Michal Vasko20c0b9f2021-08-24 08:16:27 +02001049 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +02001050 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001051 goto cleanup;
1052 }
1053 }
1054
1055 if (!(*range)) {
1056 *range = calloc(1, sizeof **range);
1057 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1058 }
1059
1060 /* we rewrite the following values as the types chain is being processed */
1061 if (range_p->eapptag) {
1062 lydict_remove(ctx->ctx, (*range)->eapptag);
1063 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
1064 }
1065 if (range_p->emsg) {
1066 lydict_remove(ctx->ctx, (*range)->emsg);
1067 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
1068 }
1069 if (range_p->dsc) {
1070 lydict_remove(ctx->ctx, (*range)->dsc);
1071 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
1072 }
1073 if (range_p->ref) {
1074 lydict_remove(ctx->ctx, (*range)->ref);
1075 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
1076 }
1077 /* extensions are taken only from the last range by the caller */
1078
1079 (*range)->parts = parts;
1080 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001081cleanup:
1082 LY_ARRAY_FREE(parts);
1083
1084 return ret;
1085}
1086
Michal Vasko215d7fc2022-07-15 14:50:44 +02001087/**
1088 * @brief Transform characters block in an XML Schema pattern into Perl character ranges.
1089 *
1090 * @param[in] ctx libyang context.
1091 * @param[in] pattern Original pattern.
1092 * @param[in,out] regex Pattern to modify.
1093 * @return LY_ERR value.
1094 */
1095static LY_ERR
1096lys_compile_pattern_chblocks_xmlschema2perl(const struct ly_ctx *ctx, const char *pattern, char **regex)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001097{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001098#define URANGE_LEN 19
1099 char *ublock2urange[][2] = {
1100 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1101 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1102 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1103 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1104 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1105 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1106 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1107 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1108 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1109 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1110 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1111 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1112 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1113 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1114 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1115 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1116 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1117 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1118 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1119 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1120 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1121 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1122 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1123 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1124 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1125 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1126 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1127 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1128 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1129 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1130 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1131 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1132 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1133 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1134 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1135 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1136 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1137 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1138 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1139 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1140 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1141 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1142 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1143 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1144 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1145 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1146 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1147 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1148 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1149 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1150 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1151 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1152 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1153 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1154 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1155 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1156 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1157 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1158 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1159 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1160 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1161 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1162 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1163 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1164 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1165 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1166 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1167 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1168 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1169 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1170 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1171 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1172 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1173 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1174 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1175 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1176 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1177 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1178 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1179 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1180 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1181 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1182 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +01001183 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001184 {NULL, NULL}
1185 };
1186
Michal Vasko215d7fc2022-07-15 14:50:44 +02001187 size_t idx, idx2, start, end;
1188 char *perl_regex, *ptr;
1189
1190 perl_regex = *regex;
1191
1192 /* substitute Unicode Character Blocks with exact Character Ranges */
1193 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1194 start = ptr - perl_regex;
1195
1196 ptr = strchr(ptr, '}');
1197 if (!ptr) {
1198 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
1199 return LY_EVALID;
1200 }
1201 end = (ptr - perl_regex) + 1;
1202
1203 /* need more space */
1204 if (end - start < URANGE_LEN) {
1205 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko78419922022-07-15 15:15:20 +02001206 *regex = perl_regex;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001207 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1208 }
1209
1210 /* find our range */
1211 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1212 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1213 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1214 break;
1215 }
1216 }
1217 if (!ublock2urange[idx][0]) {
1218 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
1219 return LY_EVALID;
1220 }
1221
1222 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1223 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1224 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1225 ++idx;
1226 }
1227 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1228 --idx;
1229 }
1230 }
1231 if (idx) {
1232 /* skip brackets */
1233 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1234 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1235 } else {
1236 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1237 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1238 }
1239 }
1240
Michal Vasko215d7fc2022-07-15 14:50:44 +02001241 return LY_SUCCESS;
1242}
1243
1244LY_ERR
1245lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
1246{
1247 size_t idx, size, brack;
1248 char *perl_regex;
1249 int err_code, compile_opts;
1250 const char *orig_ptr;
1251 PCRE2_SIZE err_offset;
1252 pcre2_code *code_local;
aPiecek9e716992022-07-21 16:29:47 +02001253 ly_bool escaped;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001254 LY_ERR r;
1255
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001256 /* adjust the expression to a Perl equivalent
1257 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1258
1259 /* allocate space for the transformed pattern */
1260 size = strlen(pattern) + 1;
Michal Vasko03d430c2022-07-22 09:05:56 +02001261 compile_opts = PCRE2_UTF | PCRE2_UCP | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
Christian Hoppsdafed222021-03-22 13:02:42 -04001262#ifdef PCRE2_ENDANCHORED
1263 compile_opts |= PCRE2_ENDANCHORED;
1264#else
1265 /* add space for trailing $ anchor */
1266 size++;
1267#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001268 perl_regex = malloc(size);
1269 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1270 perl_regex[0] = '\0';
1271
1272 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1273 brack = 0;
1274 idx = 0;
aPiecek9e716992022-07-21 16:29:47 +02001275 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001276 orig_ptr = pattern;
1277 while (orig_ptr[0]) {
1278 switch (orig_ptr[0]) {
1279 case '$':
1280 case '^':
1281 if (!brack) {
1282 /* make space for the extra character */
1283 ++size;
1284 perl_regex = ly_realloc(perl_regex, size);
1285 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1286
1287 /* print escape slash */
1288 perl_regex[idx] = '\\';
1289 ++idx;
1290 }
1291 break;
aPiecek9e716992022-07-21 16:29:47 +02001292 case '\\':
1293 /* escape character found or backslash is escaped */
1294 escaped = !escaped;
1295 /* copy backslash and continue with the next character */
1296 perl_regex[idx] = orig_ptr[0];
1297 ++idx;
1298 ++orig_ptr;
1299 continue;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001300 case '[':
aPiecek9e716992022-07-21 16:29:47 +02001301 if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001302 ++brack;
1303 }
1304 break;
1305 case ']':
aPiecek9e716992022-07-21 16:29:47 +02001306 if (!brack && !escaped) {
1307 /* If ']' does not terminate a character class expression, then pcre2_compile() implicitly escapes the
1308 * ']' character. But this seems to be against the regular expressions rules declared in
1309 * "XML schema: Datatypes" and therefore an error is returned. So for example if pattern is '\[a]' then
1310 * pcre2 match characters '[a]' literally but in YANG such pattern is not allowed.
1311 */
1312 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, orig_ptr, "character group doesn't begin with '['");
1313 free(perl_regex);
1314 return LY_EVALID;
1315 } else if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001316 --brack;
1317 }
1318 break;
1319 default:
1320 break;
1321 }
1322
1323 /* copy char */
1324 perl_regex[idx] = orig_ptr[0];
1325
1326 ++idx;
1327 ++orig_ptr;
aPiecek9e716992022-07-21 16:29:47 +02001328 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001329 }
Christian Hoppsdafed222021-03-22 13:02:42 -04001330#ifndef PCRE2_ENDANCHORED
1331 /* anchor match to end of subject */
1332 perl_regex[idx++] = '$';
1333#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001334 perl_regex[idx] = '\0';
1335
Michal Vasko215d7fc2022-07-15 14:50:44 +02001336 /* transform character blocks */
1337 if ((r = lys_compile_pattern_chblocks_xmlschema2perl(ctx, pattern, &perl_regex))) {
1338 free(perl_regex);
1339 return r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001340 }
1341
1342 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001343 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001344 &err_code, &err_offset, NULL);
1345 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001346 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
Michal Vasko2bf4af42023-01-04 12:08:38 +01001347
Radek Krejcif13b87b2020-12-01 22:02:17 +01001348 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001349 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001350 free(perl_regex);
1351 return LY_EVALID;
1352 }
1353 free(perl_regex);
1354
1355 if (code) {
1356 *code = code_local;
1357 } else {
1358 free(code_local);
1359 }
1360
1361 return LY_SUCCESS;
1362
1363#undef URANGE_LEN
1364}
1365
Michal Vasko51de7b72022-04-29 09:50:22 +02001366LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001367lys_compile_type_patterns(struct lysc_ctx *ctx, const struct lysp_restr *patterns_p, struct lysc_pattern **base_patterns,
1368 struct lysc_pattern ***patterns)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001369{
1370 struct lysc_pattern **pattern;
1371 LY_ARRAY_COUNT_TYPE u;
1372 LY_ERR ret = LY_SUCCESS;
1373
1374 /* first, copy the patterns from the base type */
1375 if (base_patterns) {
1376 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1377 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1378 }
1379
1380 LY_ARRAY_FOR(patterns_p, u) {
1381 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1382 *pattern = calloc(1, sizeof **pattern);
1383 ++(*pattern)->refcount;
1384
Radek Krejci2efc45b2020-12-22 16:25:44 +01001385 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001386 LY_CHECK_RET(ret);
1387
Radek Krejcif13b87b2020-12-01 22:02:17 +01001388 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001389 (*pattern)->inverted = 1;
1390 }
1391 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1392 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1393 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1394 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1395 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001396 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001397 }
1398done:
1399 return ret;
1400}
1401
1402/**
1403 * @brief map of the possible restrictions combination for the specific built-in type.
1404 */
1405static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1406 0 /* LY_TYPE_UNKNOWN */,
1407 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1408 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1409 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1410 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1411 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1412 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1413 LYS_SET_BIT /* LY_TYPE_BITS */,
1414 0 /* LY_TYPE_BOOL */,
1415 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1416 0 /* LY_TYPE_EMPTY */,
1417 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1418 LYS_SET_BASE /* LY_TYPE_IDENT */,
1419 LYS_SET_REQINST /* LY_TYPE_INST */,
1420 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1421 LYS_SET_TYPE /* LY_TYPE_UNION */,
1422 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1423 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1424 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1425 LYS_SET_RANGE /* LY_TYPE_INT64 */
1426};
1427
1428/**
1429 * @brief stringification of the YANG built-in data types
1430 */
1431const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001432 LY_TYPE_UNKNOWN_STR,
1433 LY_TYPE_BINARY_STR,
1434 LY_TYPE_UINT8_STR,
1435 LY_TYPE_UINT16_STR,
1436 LY_TYPE_UINT32_STR,
1437 LY_TYPE_UINT64_STR,
1438 LY_TYPE_STRING_STR,
1439 LY_TYPE_BITS_STR,
1440 LY_TYPE_BOOL_STR,
1441 LY_TYPE_DEC64_STR,
1442 LY_TYPE_EMPTY_STR,
1443 LY_TYPE_ENUM_STR,
1444 LY_TYPE_IDENT_STR,
1445 LY_TYPE_INST_STR,
1446 LY_TYPE_LEAFREF_STR,
1447 LY_TYPE_UNION_STR,
1448 LY_TYPE_INT8_STR,
1449 LY_TYPE_INT16_STR,
1450 LY_TYPE_INT32_STR,
1451 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001452};
1453
Michal Vasko193dacd2022-10-13 08:43:05 +02001454LY_ERR
1455lys_compile_type_enums(struct lysc_ctx *ctx, const struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001456 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001457{
1458 LY_ERR ret = LY_SUCCESS;
1459 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001460 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1461 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001462 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001463 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001464
1465 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001466 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001467 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1468 return LY_EVALID;
1469 }
1470
1471 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001472 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001473 if (base_enums) {
1474 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1475 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001476 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001477 break;
1478 }
1479 }
1480 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001481 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001482 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001483 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001484 return LY_EVALID;
1485 }
1486 match = v;
1487 }
1488
1489 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001490 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001491 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001492 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001493 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001494 LY_ARRAY_FOR(*bitenums, v) {
1495 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001496 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001497 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001498 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001499 return LY_EVALID;
1500 }
1501 }
1502 } else if (base_enums) {
1503 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001504 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001505 } else {
1506 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001507 if (u == 0) {
1508 cur_val = 0;
1509 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001510 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001511 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001512 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001513 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001514 } else {
1515 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001516 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001517 }
1518
1519 /* save highest value for auto assing */
1520 if (highest_value < cur_val) {
1521 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001522 }
1523 } else { /* LY_TYPE_BITS */
1524 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001525 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001526 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001527 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001528 LY_ARRAY_FOR(*bitenums, v) {
1529 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001530 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001531 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001532 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001533 return LY_EVALID;
1534 }
1535 }
1536 } else if (base_enums) {
1537 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001538 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001539 } else {
1540 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001541 if (u == 0) {
1542 cur_pos = 0;
1543 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001544 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001545 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001546 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001547 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001548 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001549 } else {
1550 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001551 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001552 }
1553
1554 /* save highest position for auto assing */
1555 if (highest_position < cur_pos) {
1556 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001557 }
1558 }
1559
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001560 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001561 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001562 if (basetype == LY_TYPE_ENUM) {
1563 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001564 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001565 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001566 enums_p[u].name, base_enums[match].value, cur_val);
1567 return LY_EVALID;
1568 }
1569 } else {
1570 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001571 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001572 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001573 enums_p[u].name, base_enums[match].position, cur_pos);
1574 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001575 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001576 }
1577 }
1578
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001579 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001580 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001581 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1582 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1583 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001584 e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) | (basetype == LY_TYPE_ENUM ? LYS_IS_ENUM : 0);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001585 if (basetype == LY_TYPE_ENUM) {
1586 e->value = cur_val;
1587 } else {
1588 e->position = cur_pos;
1589 }
Radek Krejciab430862021-03-02 20:13:40 +01001590 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001591
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001592 /* evaluate if-ffeatures */
1593 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1594 if (!enabled) {
1595 /* set only flag, later resolved and removed */
1596 e->flags |= LYS_DISABLED;
1597 }
1598
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001599 if (basetype == LY_TYPE_BITS) {
1600 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001601 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001602 if (v != u) {
1603 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001604 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1605 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001606 }
1607 }
1608 }
1609
1610done:
1611 return ret;
1612}
1613
Michal Vaskod595eff2023-02-20 11:29:28 +01001614/**
1615 * @brief Compile union type.
1616 *
1617 * @param[in] ctx Compile context.
1618 * @param[in] ptypes Parsed union types.
1619 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1620 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
1621 * @param[in] context_name Name of the context node or referencing typedef for logging.
1622 * @param[out] utypes_p Array of compiled union types.
1623 * @return LY_ERR value.
1624 */
Radek Krejci6a205692020-12-09 13:58:22 +01001625static LY_ERR
1626lys_compile_type_union(struct lysc_ctx *ctx, struct lysp_type *ptypes, struct lysp_node *context_pnode, uint16_t context_flags,
Michal Vaskoa99b3572021-02-01 11:54:58 +01001627 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001628{
1629 LY_ERR ret = LY_SUCCESS;
1630 struct lysc_type **utypes = *utypes_p;
1631 struct lysc_type_union *un_aux = NULL;
1632
1633 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1634 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001635 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1636 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001637 LY_CHECK_GOTO(ret, error);
Michal Vaskod595eff2023-02-20 11:29:28 +01001638 LY_ATOMIC_INC_BARRIER(utypes[u + additional]->refcount);
1639
Radek Krejci6a205692020-12-09 13:58:22 +01001640 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1641 /* add space for additional types from the union subtype */
1642 un_aux = (struct lysc_type_union *)utypes[u + additional];
1643 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1644 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1645
1646 /* copy subtypes of the subtype union */
1647 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1648 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1649 struct lysc_type_leafref *lref;
1650
1651 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1652 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1653 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1654 lref = (struct lysc_type_leafref *)utypes[u + additional];
1655
1656 lref->basetype = LY_TYPE_LEAFREF;
Michal Vaskoe33134a2022-07-29 14:54:40 +02001657 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, 0, 0, &lref->path);
Radek Krejci6a205692020-12-09 13:58:22 +01001658 LY_CHECK_GOTO(ret, error);
Michal Vasko080064b2021-08-31 15:20:44 +02001659 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001660 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001661 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001662 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001663 LY_CHECK_GOTO(ret, error);
Radek Krejci6a205692020-12-09 13:58:22 +01001664 } else {
1665 utypes[u + additional] = un_aux->types[v];
Michal Vasko04338d92021-09-01 07:58:14 +02001666 LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount);
Radek Krejci6a205692020-12-09 13:58:22 +01001667 }
1668 ++additional;
1669 LY_ARRAY_INCREMENT(utypes);
1670 }
1671 /* compensate u increment in main loop */
1672 --additional;
1673
1674 /* free the replaced union subtype */
Michal Vaskoc636ea42022-09-16 10:20:31 +02001675 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001676 un_aux = NULL;
1677 } else {
1678 LY_ARRAY_INCREMENT(utypes);
1679 }
1680 }
1681
1682 *utypes_p = utypes;
1683 return LY_SUCCESS;
1684
1685error:
1686 if (un_aux) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001687 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001688 }
1689 *utypes_p = utypes;
1690 return ret;
1691}
1692
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001693/**
Michal Vaskod595eff2023-02-20 11:29:28 +01001694 * @brief Allocate a new specific type structure according to the basetype.
1695 *
1696 * @param[in] basetype Base type of the new type.
1697 * @return Specific type structure.
1698 */
1699static struct lysc_type *
1700lys_new_type(LY_DATA_TYPE basetype)
1701{
1702 struct lysc_type *type = NULL;
1703
1704 switch (basetype) {
1705 case LY_TYPE_BINARY:
1706 type = calloc(1, sizeof(struct lysc_type_bin));
1707 break;
1708 case LY_TYPE_BITS:
1709 type = calloc(1, sizeof(struct lysc_type_bits));
1710 break;
1711 case LY_TYPE_DEC64:
1712 type = calloc(1, sizeof(struct lysc_type_dec));
1713 break;
1714 case LY_TYPE_STRING:
1715 type = calloc(1, sizeof(struct lysc_type_str));
1716 break;
1717 case LY_TYPE_ENUM:
1718 type = calloc(1, sizeof(struct lysc_type_enum));
1719 break;
1720 case LY_TYPE_INT8:
1721 case LY_TYPE_UINT8:
1722 case LY_TYPE_INT16:
1723 case LY_TYPE_UINT16:
1724 case LY_TYPE_INT32:
1725 case LY_TYPE_UINT32:
1726 case LY_TYPE_INT64:
1727 case LY_TYPE_UINT64:
1728 type = calloc(1, sizeof(struct lysc_type_num));
1729 break;
1730 case LY_TYPE_IDENT:
1731 type = calloc(1, sizeof(struct lysc_type_identityref));
1732 break;
1733 case LY_TYPE_LEAFREF:
1734 type = calloc(1, sizeof(struct lysc_type_leafref));
1735 break;
1736 case LY_TYPE_INST:
1737 type = calloc(1, sizeof(struct lysc_type_instanceid));
1738 break;
1739 case LY_TYPE_UNION:
1740 type = calloc(1, sizeof(struct lysc_type_union));
1741 break;
1742 case LY_TYPE_BOOL:
1743 case LY_TYPE_EMPTY:
1744 type = calloc(1, sizeof(struct lysc_type));
1745 break;
1746 case LY_TYPE_UNKNOWN:
1747 break;
1748 }
1749
1750 return type;
1751}
1752
1753/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001754 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
Michal Vaskod595eff2023-02-20 11:29:28 +01001755 *
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001756 * @param[in] ctx Compile context.
1757 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1758 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001759 * @param[in] context_name Name of the context node or referencing typedef for logging.
1760 * @param[in] type_p Parsed type to compile.
1761 * @param[in] basetype Base YANG built-in type of the type to compile.
1762 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
Michal Vaskod595eff2023-02-20 11:29:28 +01001763 * @param[in] base Latest base (compiled) type from which the current type is being derived.
1764 * @param[in] plugin Type plugin to use.
1765 * @param[in] tpdf_chain Chain of the used typedefs, traversed backwards.
1766 * @param[in] tpdf_chain_last Index of the last (backwards) typedef in @p tpdf_chain to use.
1767 * @param[out] type Compiled type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001768 * @return LY_ERR value.
1769 */
1770static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001771lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
Michal Vaskod595eff2023-02-20 11:29:28 +01001772 const struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, const struct lysc_type *base,
1773 struct lyplg_type *plugin, struct ly_set *tpdf_chain, uint32_t tpdf_chain_last, struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001774{
Michal Vaskod595eff2023-02-20 11:29:28 +01001775 LY_ERR rc = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001776 struct lysc_type_bin *bin;
1777 struct lysc_type_num *num;
1778 struct lysc_type_str *str;
1779 struct lysc_type_bits *bits;
1780 struct lysc_type_enum *enumeration;
1781 struct lysc_type_dec *dec;
1782 struct lysc_type_identityref *idref;
1783 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001784 struct lysc_type_union *un;
Michal Vaskod595eff2023-02-20 11:29:28 +01001785 struct lys_type_item *tpdf_item;
1786 uint32_t i;
1787
1788 /* alloc and init */
1789 *type = lys_new_type(basetype);
1790 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1791
1792 (*type)->basetype = basetype;
1793 (*type)->plugin = plugin;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001794
1795 switch (basetype) {
1796 case LY_TYPE_BINARY:
Michal Vaskod595eff2023-02-20 11:29:28 +01001797 bin = (struct lysc_type_bin *)*type;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001798
1799 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1800 if (type_p->length) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001801 LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1802 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001803 if (!tpdfname) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001804 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001805 }
1806 }
1807 break;
1808 case LY_TYPE_BITS:
1809 /* RFC 7950 9.7 - bits */
Michal Vaskod595eff2023-02-20 11:29:28 +01001810 bits = (struct lysc_type_bits *)*type;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001811 if (type_p->bits) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001812 LY_CHECK_GOTO(rc = lys_compile_type_enums(ctx, type_p->bits, basetype,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001813 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
Michal Vaskod595eff2023-02-20 11:29:28 +01001814 (struct lysc_type_bitenum_item **)&bits->bits), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001815 }
1816
1817 if (!base && !type_p->flags) {
1818 /* type derived from bits built-in type must contain at least one bit */
1819 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001820 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001821 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001822 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001823 }
Michal Vaskod595eff2023-02-20 11:29:28 +01001824 rc = LY_EVALID;
1825 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826 }
1827 break;
1828 case LY_TYPE_DEC64:
Michal Vaskod595eff2023-02-20 11:29:28 +01001829 dec = (struct lysc_type_dec *)*type;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001830
1831 /* RFC 7950 9.3.4 - fraction-digits */
1832 if (!base) {
1833 if (!type_p->fraction_digits) {
1834 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001835 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001836 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001837 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001838 }
Michal Vaskod595eff2023-02-20 11:29:28 +01001839 rc = LY_EVALID;
1840 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001841 }
1842 dec->fraction_digits = type_p->fraction_digits;
1843 } else {
1844 if (type_p->fraction_digits) {
1845 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1846 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001847 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001848 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1849 tpdfname);
1850 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001851 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001852 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1853 }
Michal Vaskod595eff2023-02-20 11:29:28 +01001854 rc = LY_EVALID;
1855 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001856 }
1857 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1858 }
1859
1860 /* RFC 7950 9.2.4 - range */
1861 if (type_p->range) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001862 LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1863 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001864 if (!tpdfname) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001865 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001866 }
1867 }
1868 break;
1869 case LY_TYPE_STRING:
Michal Vaskod595eff2023-02-20 11:29:28 +01001870 str = (struct lysc_type_str *)*type;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001871
1872 /* RFC 7950 9.4.4 - length */
1873 if (type_p->length) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001874 LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1875 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001876 if (!tpdfname) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001877 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001878 }
1879 } else if (base && ((struct lysc_type_str *)base)->length) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001880 str->length = lysc_range_dup(ctx, ((struct lysc_type_str *)base)->length, tpdf_chain, tpdf_chain_last);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001881 }
1882
1883 /* RFC 7950 9.4.5 - pattern */
1884 if (type_p->patterns) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001885 LY_CHECK_GOTO(rc = lys_compile_type_patterns(ctx, type_p->patterns,
1886 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001887 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1888 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1889 }
1890 break;
1891 case LY_TYPE_ENUM:
Michal Vaskod595eff2023-02-20 11:29:28 +01001892 enumeration = (struct lysc_type_enum *)*type;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001893
1894 /* RFC 7950 9.6 - enum */
1895 if (type_p->enums) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001896 LY_CHECK_GOTO(rc = lys_compile_type_enums(ctx, type_p->enums, basetype,
1897 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001898 }
1899
1900 if (!base && !type_p->flags) {
1901 /* type derived from enumerations built-in type must contain at least one enum */
1902 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001903 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001904 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001905 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001906 }
Michal Vaskod595eff2023-02-20 11:29:28 +01001907 rc = LY_EVALID;
1908 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001909 }
1910 break;
1911 case LY_TYPE_INT8:
1912 case LY_TYPE_UINT8:
1913 case LY_TYPE_INT16:
1914 case LY_TYPE_UINT16:
1915 case LY_TYPE_INT32:
1916 case LY_TYPE_UINT32:
1917 case LY_TYPE_INT64:
1918 case LY_TYPE_UINT64:
Michal Vaskod595eff2023-02-20 11:29:28 +01001919 num = (struct lysc_type_num *)*type;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001920
1921 /* RFC 6020 9.2.4 - range */
1922 if (type_p->range) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001923 LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1924 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001925 if (!tpdfname) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001926 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001927 }
1928 }
1929 break;
1930 case LY_TYPE_IDENT:
Michal Vaskod595eff2023-02-20 11:29:28 +01001931 idref = (struct lysc_type_identityref *)*type;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001932
1933 /* RFC 7950 9.10.2 - base */
1934 if (type_p->bases) {
1935 if (base) {
1936 /* only the directly derived identityrefs can contain base specification */
1937 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001938 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001939 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1940 tpdfname);
1941 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001942 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001943 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1944 }
Michal Vaskod595eff2023-02-20 11:29:28 +01001945 rc = LY_EVALID;
1946 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001947 }
Michal Vaskod595eff2023-02-20 11:29:28 +01001948 LY_CHECK_GOTO(rc = lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001949 }
1950
1951 if (!base && !type_p->flags) {
1952 /* type derived from identityref built-in type must contain at least one base */
1953 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001954 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001955 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001956 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001957 }
Michal Vaskod595eff2023-02-20 11:29:28 +01001958 rc = LY_EVALID;
1959 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001960 }
1961 break;
1962 case LY_TYPE_LEAFREF:
1963 lref = (struct lysc_type_leafref *)*type;
1964
1965 /* RFC 7950 9.9.3 - require-instance */
1966 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001967 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001968 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001969 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001970 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1971 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001972 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001973 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1974 }
Michal Vaskod595eff2023-02-20 11:29:28 +01001975 rc = LY_EVALID;
1976 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001977 }
1978 lref->require_instance = type_p->require_instance;
1979 } else if (base) {
1980 /* inherit */
1981 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1982 } else {
1983 /* default is true */
1984 lref->require_instance = 1;
1985 }
1986 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001987 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001988
Michal Vaskod595eff2023-02-20 11:29:28 +01001989 LY_CHECK_GOTO(rc = lyxp_expr_dup(ctx->ctx, type_p->path, 0, 0, &lref->path), cleanup);
1990 LY_CHECK_GOTO(lyplg_type_prefix_data_new(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr),
1991 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001992 } else if (base) {
Michal Vaskod595eff2023-02-20 11:29:28 +01001993 LY_CHECK_GOTO(rc = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, 0, 0, &lref->path), cleanup);
1994 LY_CHECK_GOTO(rc = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
1995 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001996 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001997 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vaskod595eff2023-02-20 11:29:28 +01001998 rc = LY_EVALID;
1999 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002000 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002001 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vaskod595eff2023-02-20 11:29:28 +01002002 rc = LY_EVALID;
2003 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002004 }
2005 break;
2006 case LY_TYPE_INST:
2007 /* RFC 7950 9.9.3 - require-instance */
2008 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskod595eff2023-02-20 11:29:28 +01002009 ((struct lysc_type_instanceid *)*type)->require_instance = type_p->require_instance;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002010 } else {
2011 /* default is true */
Michal Vaskod595eff2023-02-20 11:29:28 +01002012 ((struct lysc_type_instanceid *)*type)->require_instance = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002013 }
2014 break;
2015 case LY_TYPE_UNION:
Michal Vaskod595eff2023-02-20 11:29:28 +01002016 un = (struct lysc_type_union *)*type;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002017
2018 /* RFC 7950 7.4 - type */
2019 if (type_p->types) {
2020 if (base) {
2021 /* only the directly derived union can contain types specification */
2022 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002023 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002024 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2025 tpdfname);
2026 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002027 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002028 "Invalid type substatement for the type not directly derived from union built-in type.");
2029 }
Michal Vaskod595eff2023-02-20 11:29:28 +01002030 rc = LY_EVALID;
2031 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002032 }
2033 /* compile the type */
Michal Vaskod595eff2023-02-20 11:29:28 +01002034 LY_CHECK_GOTO(rc = lys_compile_type_union(ctx, type_p->types, context_pnode, context_flags, context_name,
2035 &un->types), cleanup);
2036 } else if (base) {
2037 /* copy all the types */
2038 const struct lysc_type_union *un_base = (struct lysc_type_union *)base;
2039 LY_ARRAY_COUNT_TYPE u;
2040
2041 LY_ARRAY_CREATE_GOTO(ctx->ctx, un->types, LY_ARRAY_COUNT(un_base->types), rc, cleanup);
2042 LY_ARRAY_FOR(un_base->types, u) {
2043 un->types[u] = un_base->types[u];
2044 LY_ATOMIC_INC_BARRIER(un->types[u]->refcount);
2045 LY_ARRAY_INCREMENT(un->types);
2046 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002047 }
2048
2049 if (!base && !type_p->flags) {
2050 /* type derived from union built-in type must contain at least one type */
2051 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002052 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002053 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002054 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002055 }
Michal Vaskod595eff2023-02-20 11:29:28 +01002056 rc = LY_EVALID;
2057 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002058 }
2059 break;
2060 case LY_TYPE_BOOL:
2061 case LY_TYPE_EMPTY:
2062 case LY_TYPE_UNKNOWN: /* just to complete switch */
2063 break;
2064 }
2065
Michal Vaskod595eff2023-02-20 11:29:28 +01002066 if (tpdf_chain->count > tpdf_chain_last) {
2067 i = tpdf_chain->count;
2068 do {
2069 --i;
2070 tpdf_item = tpdf_chain->objs[i];
2071
2072 /* compile previous typedefs extensions */
2073 COMPILE_EXTS_GOTO(ctx, tpdf_item->tpdf->type.exts, (*type)->exts, *type, rc, cleanup);
2074 } while (i > tpdf_chain_last);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002075 }
Michal Vaskod595eff2023-02-20 11:29:28 +01002076
2077 /* compile new parsed extensions */
2078 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, *type, rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002079
2080cleanup:
Michal Vaskod595eff2023-02-20 11:29:28 +01002081 if (rc) {
2082 LY_ATOMIC_INC_BARRIER((*type)->refcount);
2083 lysc_type_free(&ctx->free_ctx, *type);
2084 *type = NULL;
2085 }
2086 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002087}
2088
2089LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01002090lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
Michal Vasko193dacd2022-10-13 08:43:05 +02002091 const struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002092{
2093 LY_ERR ret = LY_SUCCESS;
2094 ly_bool dummyloops = 0;
Michal Vaskod595eff2023-02-20 11:29:28 +01002095 struct lys_type_item *tctx, *tctx_prev = NULL, *tctx_iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002096 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Michal Vaskod595eff2023-02-20 11:29:28 +01002097 struct lysc_type *base = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002098 struct ly_set tpdf_chain = {0};
Michal Vasko388a6632021-08-06 11:27:43 +02002099 struct lyplg_type *plugin;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002100
Michal Vaskod595eff2023-02-20 11:29:28 +01002101 *type = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002102 if (dflt) {
2103 *dflt = NULL;
2104 }
2105
2106 tctx = calloc(1, sizeof *tctx);
2107 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002108 for (ret = lysp_type_find(type_p->name, context_pnode, type_p->pmod, ctx->ext, &basetype, &tctx->tpdf, &tctx->node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002109 ret == LY_SUCCESS;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002110 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod, ctx->ext,
Michal Vaskoa99b3572021-02-01 11:54:58 +01002111 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002112 if (basetype) {
2113 break;
2114 }
2115
2116 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01002117 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
2118 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002119 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2120
2121 if (units && !*units) {
2122 /* inherit units */
2123 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2124 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2125 }
2126 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
2127 /* inherit default */
2128 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
2129 }
2130 if (dummyloops && (!units || *units) && dflt && *dflt) {
Michal Vaskod595eff2023-02-20 11:29:28 +01002131 basetype = ((struct lys_type_item *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002132 break;
2133 }
2134
Michal Vasko080064b2021-08-31 15:20:44 +02002135 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
Radek Krejci720d2612021-03-03 19:44:22 +01002136 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
2137 * and we need now recompile the type again in the updated context. */
Michal Vaskoc636ea42022-09-16 10:20:31 +02002138 lysc_type_free(&ctx->free_ctx, tctx->tpdf->type.compiled);
Radek Krejci720d2612021-03-03 19:44:22 +01002139 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
2140 }
2141
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002142 if (tctx->tpdf->type.compiled) {
2143 /* it is not necessary to continue, the rest of the chain was already compiled,
2144 * but we still may need to inherit default and units values, so start dummy loops */
2145 basetype = tctx->tpdf->type.compiled->basetype;
2146 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2147 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2148
2149 if ((units && !*units) || (dflt && !*dflt)) {
2150 dummyloops = 1;
2151 goto preparenext;
2152 } else {
2153 tctx = NULL;
2154 break;
2155 }
2156 }
2157
2158 /* circular typedef reference detection */
2159 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
2160 /* local part */
Michal Vaskod595eff2023-02-20 11:29:28 +01002161 tctx_iter = (struct lys_type_item *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002162 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002163 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002164 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2165 free(tctx);
2166 ret = LY_EVALID;
2167 goto cleanup;
2168 }
2169 }
2170 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
2171 /* global part for unions corner case */
Michal Vaskod595eff2023-02-20 11:29:28 +01002172 tctx_iter = (struct lys_type_item *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002173 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002174 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002175 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2176 free(tctx);
2177 ret = LY_EVALID;
2178 goto cleanup;
2179 }
2180 }
2181
2182 /* store information for the following processing */
2183 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2184 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2185
2186preparenext:
2187 /* prepare next loop */
2188 tctx_prev = tctx;
2189 tctx = calloc(1, sizeof *tctx);
2190 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2191 }
2192 free(tctx);
2193
Michal Vaskod595eff2023-02-20 11:29:28 +01002194 /* basic checks */
2195 if (basetype == LY_TYPE_UNKNOWN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002196 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002197 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2198 ret = LY_EVALID;
2199 goto cleanup;
2200 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002201 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002202 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002203 ly_data_type2str[basetype]);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002204 ret = LY_EVALID;
2205 goto cleanup;
2206 }
2207
2208 /* get restrictions from the referred typedefs */
2209 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
Michal Vaskod595eff2023-02-20 11:29:28 +01002210 tctx = (struct lys_type_item *)tpdf_chain.objs[u];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002211
2212 /* remember the typedef context for circular check */
2213 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
2214 LY_CHECK_GOTO(ret, cleanup);
2215
2216 if (tctx->tpdf->type.compiled) {
Michal Vasko388a6632021-08-06 11:27:43 +02002217 /* already compiled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002218 base = tctx->tpdf->type.compiled;
2219 continue;
Michal Vasko388a6632021-08-06 11:27:43 +02002220 }
2221
Michal Vaskoddd76592022-01-17 13:34:48 +01002222 /* try to find loaded user type plugins */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002223 plugin = lyplg_type_plugin_find(tctx->tpdf->type.pmod->mod->name, tctx->tpdf->type.pmod->mod->revision,
Michal Vaskoddd76592022-01-17 13:34:48 +01002224 tctx->tpdf->name);
Haithem Ben Ghorbal45be15f2022-07-12 18:02:36 +02002225 if (!plugin && base) {
2226 /* use the base type implementation if available */
2227 plugin = base->plugin;
2228 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002229 if (!plugin) {
2230 /* use the internal built-in type implementation */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002231 plugin = lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vaskoddd76592022-01-17 13:34:48 +01002232 }
Michal Vasko388a6632021-08-06 11:27:43 +02002233 assert(plugin);
2234
Michal Vaskod595eff2023-02-20 11:29:28 +01002235 if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !tctx->tpdf->type.flags &&
2236 !tctx->tpdf->type.exts && (plugin == base->plugin)) {
Michal Vasko388a6632021-08-06 11:27:43 +02002237 /* no change, reuse the compiled base */
2238 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002239 LY_ATOMIC_INC_BARRIER(base->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002240 continue;
2241 }
2242
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002243 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002244 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002245 tctx->tpdf->name, ly_data_type2str[basetype]);
2246 ret = LY_EVALID;
2247 goto cleanup;
2248 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002249 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002250 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2251 tctx->tpdf->name, tctx->tpdf->dflt.str);
2252 ret = LY_EVALID;
2253 goto cleanup;
2254 }
2255
Michal Vaskod595eff2023-02-20 11:29:28 +01002256 /* compile the typedef type */
2257 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name, &tctx->tpdf->type, basetype,
2258 tctx->tpdf->name, base, plugin, &tpdf_chain, u + 1, &base);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002259 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod595eff2023-02-20 11:29:28 +01002260
2261 /* store separately compiled typedef type to be reused */
2262 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
2263 LY_ATOMIC_INC_BARRIER(base->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002264 }
Michal Vaskod595eff2023-02-20 11:29:28 +01002265
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002266 /* remove the processed typedef contexts from the stack for circular check */
2267 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
2268
2269 /* process the type definition in leaf */
Michal Vaskod595eff2023-02-20 11:29:28 +01002270 if (type_p->flags || type_p->exts || !base || (basetype == LY_TYPE_LEAFREF)) {
2271 /* leaf type has changes that need to be compiled into the type */
2272 plugin = base ? base->plugin : lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vasko193dacd2022-10-13 08:43:05 +02002273 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, (struct lysp_type *)type_p, basetype,
Michal Vaskod595eff2023-02-20 11:29:28 +01002274 NULL, base, plugin, &tpdf_chain, 0, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002275 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod595eff2023-02-20 11:29:28 +01002276 } else {
2277 /* no changes of the type in the leaf, just use the base compiled type */
2278 *type = base;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002279 }
2280
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002281cleanup:
2282 ly_set_erase(&tpdf_chain, free);
2283 return ret;
2284}
2285
2286/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002287 * @brief Check uniqness of the node/action/notification name.
2288 *
2289 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2290 * structures, but they share the namespace so we need to check their name collisions.
2291 *
2292 * @param[in] ctx Compile context.
2293 * @param[in] parent Parent of the nodes to check, can be NULL.
2294 * @param[in] name Name of the item to find in the given lists.
2295 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2296 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2297 */
2298static LY_ERR
2299lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2300 const struct lysc_node *exclude)
2301{
2302 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002303 const struct lysc_node_action *actions;
2304 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002305 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002306 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002307
2308#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2309
2310 if (exclude->nodetype == LYS_CASE) {
2311 /* check restricted only to all the cases */
2312 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002313 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002314 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002315 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002316 return LY_EEXIST;
2317 }
2318 }
2319
2320 return LY_SUCCESS;
2321 }
2322
2323 /* no reason for our parent to be choice anymore */
2324 assert(!parent || (parent->nodetype != LYS_CHOICE));
2325
2326 if (parent && (parent->nodetype == LYS_CASE)) {
2327 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002328
2329 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2330 iter = lysc_data_parent(parent);
2331 do {
2332 parent = parent->parent;
2333 if (parent && (parent->nodetype == LYS_CHOICE)) {
2334 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2335 }
2336 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002337 }
2338
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002339 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002340 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2341 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2342 if (exclude->flags & LYS_IS_OUTPUT) {
2343 getnext_flags |= LYS_GETNEXT_OUTPUT;
2344 parent = lysc_node_child(parent)->next;
2345 } else {
2346 parent = lysc_node_child(parent);
2347 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002348 }
2349
2350 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002351 if (!parent && ctx->ext) {
2352 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2353 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2354 goto error;
2355 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002356
Radek Krejci5fa32a32021-02-08 18:12:38 +01002357 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2358 if (iter->nodetype == LYS_CHOICE) {
2359 iter2 = NULL;
2360 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2361 if (CHECK_NODE(iter2, exclude, name)) {
2362 goto error;
2363 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002364 }
2365 }
2366 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002367 } else {
2368 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2369 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2370 goto error;
2371 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002372
Radek Krejci5fa32a32021-02-08 18:12:38 +01002373 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2374 if (iter->nodetype == LYS_CHOICE) {
2375 iter2 = NULL;
2376 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2377 if (CHECK_NODE(iter2, exclude, name)) {
2378 goto error;
2379 }
2380 }
2381 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002382 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002383
Radek Krejci5fa32a32021-02-08 18:12:38 +01002384 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2385 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2386 if (CHECK_NODE(iter, exclude, name)) {
2387 goto error;
2388 }
2389 }
2390
2391 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2392 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2393 if (CHECK_NODE(iter, exclude, name)) {
2394 goto error;
2395 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002396 }
2397 }
Radek Krejci078e4342021-02-12 18:17:26 +01002398 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002399 return LY_SUCCESS;
2400
2401error:
Radek Krejci078e4342021-02-12 18:17:26 +01002402 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002403 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002404 return LY_EEXIST;
2405
2406#undef CHECK_NODE
2407}
2408
Michal Vasko193dacd2022-10-13 08:43:05 +02002409LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002410lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002411{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002412 struct lysc_node **children, *anchor = NULL;
2413 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002414
Radek Krejci2a9fc652021-01-22 17:44:34 +01002415 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002416
Radek Krejci2a9fc652021-01-22 17:44:34 +01002417 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002418 if (node->nodetype == LYS_INPUT) {
2419 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2420 /* input node is part of the action but link it with output */
2421 node->next = &((struct lysc_node_action *)parent)->output.node;
2422 node->prev = node->next;
2423 return LY_SUCCESS;
2424 } else if (node->nodetype == LYS_OUTPUT) {
2425 /* output node is part of the action but link it with input */
2426 node->next = NULL;
2427 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002428 return LY_SUCCESS;
2429 } else if (node->nodetype == LYS_ACTION) {
2430 children = (struct lysc_node **)lysc_node_actions_p(parent);
2431 } else if (node->nodetype == LYS_NOTIF) {
2432 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2433 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002434 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002435 }
2436 assert(children);
2437
2438 if (!(*children)) {
2439 /* first child */
2440 *children = node;
2441 } else if (node->flags & LYS_KEY) {
2442 /* special handling of adding keys */
2443 assert(node->module == parent->module);
2444 anchor = *children;
2445 if (anchor->flags & LYS_KEY) {
2446 while ((anchor->flags & LYS_KEY) && anchor->next) {
2447 anchor = anchor->next;
2448 }
2449 /* insert after the last key */
2450 insert_after = 1;
2451 } /* else insert before anchor (at the beginning) */
2452 } else if ((*children)->prev->module == node->module) {
2453 /* last child is from the same module, keep the order and insert at the end */
2454 anchor = (*children)->prev;
2455 insert_after = 1;
2456 } else if (parent->module == node->module) {
2457 /* adding module child after some augments were connected */
2458 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2459 } else {
2460 /* some augments are already connected and we are connecting new ones,
2461 * keep module name order and insert the node into the children list */
2462 anchor = *children;
2463 do {
2464 anchor = anchor->prev;
2465
2466 /* check that we have not found the last augment node from our module or
2467 * the first augment node from a "smaller" module or
2468 * the first node from a local module */
2469 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2470 (anchor->module == parent->module)) {
2471 /* insert after */
2472 insert_after = 1;
2473 break;
2474 }
2475
2476 /* we have traversed all the nodes, insert before anchor (as the first node) */
2477 } while (anchor->prev->next);
2478 }
2479
2480 /* insert */
2481 if (anchor) {
2482 if (insert_after) {
2483 node->next = anchor->next;
2484 node->prev = anchor;
2485 anchor->next = node;
2486 if (node->next) {
2487 /* middle node */
2488 node->next->prev = node;
2489 } else {
2490 /* last node */
2491 (*children)->prev = node;
2492 }
2493 } else {
2494 node->next = anchor;
2495 node->prev = anchor->prev;
2496 anchor->prev = node;
2497 if (anchor == *children) {
2498 /* first node */
2499 *children = node;
2500 } else {
2501 /* middle node */
2502 node->prev->next = node;
2503 }
2504 }
2505 }
2506
2507 /* check the name uniqueness (even for an only child, it may be in case) */
2508 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2509 return LY_EEXIST;
2510 }
2511 } else {
2512 /* top-level element */
2513 struct lysc_node **list;
2514
Radek Krejci6b88a462021-02-17 12:39:34 +01002515 if (ctx->ext) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002516 lyplg_ext_get_storage_p(ctx->ext, LY_STMT_DATA_NODE_MASK, (const void ***)&list);
Radek Krejci6b88a462021-02-17 12:39:34 +01002517 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002518 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2519 } else if (node->nodetype == LYS_NOTIF) {
2520 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2521 } else {
2522 list = &ctx->cur_mod->compiled->data;
2523 }
2524 if (!(*list)) {
2525 *list = node;
2526 } else {
2527 /* insert at the end of the module's top-level nodes list */
2528 (*list)->prev->next = node;
2529 node->prev = (*list)->prev;
2530 (*list)->prev = node;
2531 }
2532
2533 /* check the name uniqueness on top-level */
2534 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2535 return LY_EEXIST;
2536 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002537 }
2538
Radek Krejci2a9fc652021-01-22 17:44:34 +01002539 return LY_SUCCESS;
2540}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002541
Radek Krejci2a9fc652021-01-22 17:44:34 +01002542/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002543 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002544 *
2545 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002546 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002547 * @return LY_ERR value.
2548 */
2549static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002550lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002551{
2552 /* case never has any explicit config */
2553 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002554
Michal Vasko7c565922021-06-10 14:58:27 +02002555 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002556 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002557 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002558 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2559 /* config not explicitly set, inherit it from parent */
Michal Vaskoecdc2222022-01-24 08:45:44 +01002560 assert(!node->parent || (node->parent->flags & LYS_CONFIG_MASK) || (node->parent->nodetype & LYS_AUGMENT));
2561 if (node->parent && (node->parent->flags & LYS_CONFIG_MASK)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002562 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002563 } else {
2564 /* default is config true */
2565 node->flags |= LYS_CONFIG_W;
2566 }
2567 } else {
2568 /* config set explicitly */
2569 node->flags |= LYS_SET_CONFIG;
2570 }
2571
Radek Krejci91531e12021-02-08 19:57:54 +01002572 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002573 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002574 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002575 }
2576
Radek Krejci2a9fc652021-01-22 17:44:34 +01002577 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002578}
2579
Radek Krejci91531e12021-02-08 19:57:54 +01002580/**
2581 * @brief Set various flags of the compiled nodes
2582 *
2583 * @param[in] ctx Compile context.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002584 * @param[in] parsed_flags Parsed node flags.
2585 * @param[in] inherited_flags Inherited flags from a schema-only statement.
2586 * @param[in,out] node Compiled node where the flags will be set.
Radek Krejci91531e12021-02-08 19:57:54 +01002587 */
2588static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002589lys_compile_node_flags(struct lysc_ctx *ctx, uint16_t parsed_flags, uint16_t inherited_flags, struct lysc_node *node)
Radek Krejci91531e12021-02-08 19:57:54 +01002590{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002591 uint16_t parent_flags;
2592 const char *parent_name;
2593
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002594 /* copy flags except for status */
2595 node->flags = (parsed_flags & LYS_FLAGS_COMPILED_MASK) & ~LYS_STATUS_MASK;
2596
Radek Krejci91531e12021-02-08 19:57:54 +01002597 /* inherit config flags */
2598 LY_CHECK_RET(lys_compile_config(ctx, node));
2599
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002600 /* compile status */
2601 parent_flags = node->parent ? node->parent->flags : 0;
2602 parent_name = node->parent ? node->parent->name : NULL;
2603 LY_CHECK_RET(lys_compile_status(ctx, parsed_flags, inherited_flags, parent_flags, parent_name, node->name, &node->flags));
Radek Krejci91531e12021-02-08 19:57:54 +01002604
2605 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002606 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002607 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002608 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002609 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002610 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002611 node->flags |= LYS_IS_NOTIF;
2612 }
2613
2614 return LY_SUCCESS;
2615}
2616
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002617static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002618lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t inherited_flags,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002619 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2620 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002621{
2622 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002623 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002624 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002625 struct lysp_when *pwhen = NULL;
Michal Vaskoe02e7402021-07-23 08:29:28 +02002626 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002627
Radek Krejci2a9fc652021-01-22 17:44:34 +01002628 node->nodetype = pnode->nodetype;
2629 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002630 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002631 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002632 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002633
Radek Krejci2a9fc652021-01-22 17:44:34 +01002634 /* compile any deviations for this node */
2635 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vaskoe02e7402021-07-23 08:29:28 +02002636 if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2637 /* if not supported, keep it just like disabled nodes by if-feature */
2638 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
2639 ctx->compile_opts |= LYS_COMPILE_DISABLED;
2640 }
2641 if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002642 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002643 }
2644
Michal Vaskodfd254d2021-06-24 09:24:59 +02002645 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2646 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2647 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002648
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002649 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002650 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002651 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002652 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002653 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002654 }
2655
Radek Krejci91531e12021-02-08 19:57:54 +01002656 /* config, status and other flags */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002657 LY_CHECK_GOTO(ret = lys_compile_node_flags(ctx, pnode->flags, inherited_flags, node), error);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002658
2659 /* list ordering */
2660 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002661 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002662 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002663 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002664 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002665 node->flags &= ~LYS_ORDBY_MASK;
2666 node->flags |= LYS_ORDBY_SYSTEM;
2667 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2668 /* default ordering is system */
2669 node->flags |= LYS_ORDBY_SYSTEM;
2670 }
2671 }
2672
Radek Krejci2a9fc652021-01-22 17:44:34 +01002673 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2674 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2675
Radek Krejci9a3823e2021-01-27 20:26:46 +01002676 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002677 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002678 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002679 LY_CHECK_GOTO(ret, cleanup);
2680 }
2681
Radek Krejci2a9fc652021-01-22 17:44:34 +01002682 /* nodetype-specific part */
2683 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2684
2685 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002686 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002687 if (node->flags & LYS_MAND_TRUE) {
2688 /* inherit LYS_MAND_TRUE in parent containers */
2689 lys_compile_mandatory_parents(parent, 1);
2690 }
2691
2692 if (child_set) {
2693 /* add the new node into set */
2694 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2695 }
2696
2697 goto cleanup;
2698
2699error:
Michal Vaskoc636ea42022-09-16 10:20:31 +02002700 lysc_node_free(&ctx->free_ctx, node, 0);
2701
Radek Krejci2a9fc652021-01-22 17:44:34 +01002702cleanup:
2703 if (ret && dev_pnode) {
2704 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2705 }
Michal Vaskoe02e7402021-07-23 08:29:28 +02002706 ctx->compile_opts = prev_opts;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002707 lysp_dev_node_free(ctx, dev_pnode);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002708 return ret;
2709}
2710
Radek Krejci2a9fc652021-01-22 17:44:34 +01002711LY_ERR
2712lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2713{
2714 LY_ERR ret = LY_SUCCESS;
2715 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002716 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002717
2718 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2719 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2720
2721 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002722 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002723 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002724
Radek Krejci01180ac2021-01-27 08:48:22 +01002725 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002726 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2727 }
2728
Michal Vasko95f736c2022-06-08 12:03:31 +02002729 /* connect any augments */
2730 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2731
Michal Vasko7c565922021-06-10 14:58:27 +02002732 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002733
2734done:
2735 return ret;
2736}
2737
2738/**
2739 * @brief Compile parsed action node information.
Michal Vasko193dacd2022-10-13 08:43:05 +02002740 *
Radek Krejci2a9fc652021-01-22 17:44:34 +01002741 * @param[in] ctx Compile context
2742 * @param[in] pnode Parsed action node.
2743 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2744 * is enriched with the action-specific information.
2745 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2746 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002747static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002748lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2749{
2750 LY_ERR ret;
2751 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2752 struct lysc_node_action *action = (struct lysc_node_action *)node;
2753 struct lysp_node_action_inout *input, implicit_input = {
2754 .nodetype = LYS_INPUT,
2755 .name = "input",
2756 .parent = pnode,
2757 };
2758 struct lysp_node_action_inout *output, implicit_output = {
2759 .nodetype = LYS_OUTPUT,
2760 .name = "output",
2761 .parent = pnode,
2762 };
2763
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002764 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002765 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002766 if (action_p->input.nodetype == LYS_UNKNOWN) {
2767 input = &implicit_input;
2768 } else {
2769 input = &action_p->input;
2770 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002771 ret = lys_compile_node_(ctx, &input->node, &action->node, 0, lys_compile_node_action_inout, &action->input.node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002772 lysc_update_path(ctx, NULL, NULL);
2773 LY_CHECK_GOTO(ret, done);
2774
Michal Vaskoc130e162021-10-19 11:30:00 +02002775 /* add must(s) to unres */
2776 ret = lysc_unres_must_add(ctx, &action->input.node, &input->node);
2777 LY_CHECK_GOTO(ret, done);
2778
Radek Krejci2a9fc652021-01-22 17:44:34 +01002779 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002780 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002781 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002782 output = &implicit_output;
2783 } else {
2784 output = &action_p->output;
2785 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002786 ret = lys_compile_node_(ctx, &output->node, &action->node, 0, lys_compile_node_action_inout, &action->output.node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002787 lysc_update_path(ctx, NULL, NULL);
2788 LY_CHECK_GOTO(ret, done);
2789
Michal Vaskoc130e162021-10-19 11:30:00 +02002790 /* add must(s) to unres */
2791 ret = lysc_unres_must_add(ctx, &action->output.node, &output->node);
2792 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002793
2794done:
2795 return ret;
2796}
2797
2798/**
2799 * @brief Compile parsed action node information.
2800 * @param[in] ctx Compile context
2801 * @param[in] pnode Parsed action node.
2802 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2803 * is enriched with the action-specific information.
2804 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2805 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002806static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002807lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2808{
2809 LY_ERR ret = LY_SUCCESS;
2810 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2811 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2812 struct lysp_node *child_p;
2813
2814 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002815
2816 /* add must(s) to unres */
2817 ret = lysc_unres_must_add(ctx, node, pnode);
2818 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002819
Radek Krejci01180ac2021-01-27 08:48:22 +01002820 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002821 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002822 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002823 }
2824
Michal Vasko95f736c2022-06-08 12:03:31 +02002825 /* connect any augments */
2826 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2827
Radek Krejci2a9fc652021-01-22 17:44:34 +01002828done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002829 return ret;
2830}
2831
2832/**
2833 * @brief Compile parsed container node information.
2834 * @param[in] ctx Compile context
2835 * @param[in] pnode Parsed container node.
2836 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2837 * is enriched with the container-specific information.
2838 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2839 */
2840static LY_ERR
2841lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2842{
2843 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2844 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2845 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002846 LY_ERR ret = LY_SUCCESS;
2847
2848 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002849 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002850 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002851 }
2852
2853 /* more cases when the container has meaning but is kept NP for convenience:
2854 * - when condition
2855 * - direct child action/notification
2856 */
2857
2858 LY_LIST_FOR(cont_p->child, child_p) {
2859 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2860 LY_CHECK_GOTO(ret, done);
2861 }
2862
Michal Vasko5347e3a2020-11-03 17:14:57 +01002863 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002864
2865 /* add must(s) to unres */
2866 ret = lysc_unres_must_add(ctx, node, pnode);
2867 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002868
Michal Vasko95f736c2022-06-08 12:03:31 +02002869 /* connect any augments */
2870 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2871
Radek Krejci2a9fc652021-01-22 17:44:34 +01002872 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2873 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2874 LY_CHECK_GOTO(ret, done);
2875 }
2876 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2877 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2878 LY_CHECK_GOTO(ret, done);
2879 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002880
2881done:
2882 return ret;
2883}
2884
Michal Vasko72244882021-01-12 15:21:05 +01002885/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002886 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2887 * @param[in] ctx Compile context.
2888 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2889 * @param[in] type_p Parsed type to compile.
2890 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2891 * @return LY_ERR value.
2892 */
2893static LY_ERR
2894lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2895 struct lysc_node_leaf *leaf)
2896{
2897 struct lysp_qname *dflt;
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002898 struct lysc_type **t;
2899 LY_ARRAY_COUNT_TYPE u, count;
2900 ly_bool in_unres = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002901
Michal Vaskoa99b3572021-02-01 11:54:58 +01002902 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002903 leaf->units ? NULL : &leaf->units, &dflt));
Michal Vaskod595eff2023-02-20 11:29:28 +01002904 LY_ATOMIC_INC_BARRIER(leaf->type->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002905
2906 /* store default value, if any */
2907 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2908 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2909 }
2910
Michal Vaskoc130e162021-10-19 11:30:00 +02002911 /* store leafref(s) to be resolved */
2912 LY_CHECK_RET(lysc_unres_leafref_add(ctx, leaf, type_p->pmod));
2913
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002914 /* type-specific checks */
2915 if (leaf->type->basetype == LY_TYPE_UNION) {
2916 t = ((struct lysc_type_union *)leaf->type)->types;
2917 count = LY_ARRAY_COUNT(t);
2918 } else {
2919 t = &leaf->type;
2920 count = 1;
2921 }
2922 for (u = 0; u < count; ++u) {
2923 if (t[u]->basetype == LY_TYPE_EMPTY) {
2924 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2925 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2926 return LY_EVALID;
2927 }
2928 } else if (!in_unres && ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM))) {
2929 /* store in unres for all disabled bits/enums to be removed */
2930 LY_CHECK_RET(lysc_unres_bitenum_add(ctx, leaf));
2931 in_unres = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002932 }
2933 }
2934
2935 return LY_SUCCESS;
2936}
2937
2938/**
2939 * @brief Compile parsed leaf node information.
2940 * @param[in] ctx Compile context
2941 * @param[in] pnode Parsed leaf node.
2942 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2943 * is enriched with the leaf-specific information.
2944 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2945 */
2946static LY_ERR
2947lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2948{
2949 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2950 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002951 LY_ERR ret = LY_SUCCESS;
2952
Michal Vasko5347e3a2020-11-03 17:14:57 +01002953 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002954
2955 /* add must(s) to unres */
2956 ret = lysc_unres_must_add(ctx, node, pnode);
2957 LY_CHECK_GOTO(ret, done);
2958
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002959 if (leaf_p->units) {
2960 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2961 leaf->flags |= LYS_SET_UNITS;
2962 }
2963
2964 /* compile type */
2965 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2966 LY_CHECK_GOTO(ret, done);
2967
2968 /* store/update default value */
2969 if (leaf_p->dflt.str) {
2970 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2971 leaf->flags |= LYS_SET_DFLT;
2972 }
2973
2974 /* checks */
2975 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002976 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002977 return LY_EVALID;
2978 }
2979
2980done:
2981 return ret;
2982}
2983
2984/**
2985 * @brief Compile parsed leaf-list node information.
2986 * @param[in] ctx Compile context
2987 * @param[in] pnode Parsed leaf-list node.
2988 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2989 * is enriched with the leaf-list-specific information.
2990 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2991 */
2992static LY_ERR
2993lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2994{
2995 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2996 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002997 LY_ERR ret = LY_SUCCESS;
2998
Michal Vasko5347e3a2020-11-03 17:14:57 +01002999 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003000
3001 /* add must(s) to unres */
3002 ret = lysc_unres_must_add(ctx, node, pnode);
3003 LY_CHECK_GOTO(ret, done);
3004
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003005 if (llist_p->units) {
3006 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
3007 llist->flags |= LYS_SET_UNITS;
3008 }
3009
3010 /* compile type */
3011 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
3012 LY_CHECK_GOTO(ret, done);
3013
3014 /* store/update default values */
3015 if (llist_p->dflts) {
3016 if (ctx->pmod->version < LYS_VERSION_1_1) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003017 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003018 return LY_EVALID;
3019 }
3020
3021 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
3022 llist->flags |= LYS_SET_DFLT;
3023 }
3024
3025 llist->min = llist_p->min;
3026 if (llist->min) {
3027 llist->flags |= LYS_MAND_TRUE;
3028 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02003029 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
3030
3031 if (llist->flags & LYS_CONFIG_R) {
3032 /* state leaf-list is always ordered-by user */
3033 llist->flags &= ~LYS_ORDBY_SYSTEM;
3034 llist->flags |= LYS_ORDBY_USER;
3035 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003036
3037 /* checks */
3038 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003039 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "The default statement is present on leaf-list with a nonzero min-elements.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003040 return LY_EVALID;
3041 }
3042
3043 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003044 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003045 llist->min, llist->max);
3046 return LY_EVALID;
3047 }
3048
3049done:
3050 return ret;
3051}
3052
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003053/**
3054 * @brief Find the node according to the given descendant/absolute schema nodeid.
3055 * Used in unique, refine and augment statements.
3056 *
3057 * @param[in] ctx Compile context
3058 * @param[in] nodeid Descendant-schema-nodeid (according to the YANG grammar)
3059 * @param[in] nodeid_len Length of the given nodeid, if it is not NULL-terminated string.
3060 * @param[in] ctx_node Context node for a relative nodeid.
3061 * @param[in] format Format of any prefixes.
3062 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
3063 * @param[in] nodetype Optional (can be 0) restriction for target's nodetype. If target exists, but does not match
3064 * the given nodetype, LY_EDENIED is returned (and target is provided), but no error message is printed.
3065 * The value can be even an ORed value to allow multiple nodetypes.
3066 * @param[out] target Found target node if any.
3067 * @param[out] result_flag Output parameter to announce if the schema nodeid goes through the action's input/output or a Notification.
3068 * The LYSC_OPT_RPC_INPUT, LYSC_OPT_RPC_OUTPUT and LYSC_OPT_NOTIFICATION are used as flags.
3069 * @return LY_ERR values - LY_ENOTFOUND, LY_EVALID, LY_EDENIED or LY_SUCCESS.
3070 */
3071static LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003072lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
Michal Vasko56d8da82021-12-16 15:41:30 +01003073 LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype, const struct lysc_node **target, uint16_t *result_flag)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003074{
3075 LY_ERR ret = LY_EVALID;
3076 const char *name, *prefix, *id;
3077 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01003078 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003079 const char *nodeid_type;
3080 uint32_t getnext_extra_flag = 0;
3081 uint16_t current_nodetype = 0;
3082
3083 assert(nodeid);
3084 assert(target);
3085 assert(result_flag);
3086 *target = NULL;
3087 *result_flag = 0;
3088
3089 id = nodeid;
3090
3091 if (ctx_node) {
3092 /* descendant-schema-nodeid */
3093 nodeid_type = "descendant";
3094
3095 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003096 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003097 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01003098 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003099 return LY_EVALID;
3100 }
3101 } else {
3102 /* absolute-schema-nodeid */
3103 nodeid_type = "absolute";
3104
3105 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003106 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003107 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003108 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003109 return LY_EVALID;
3110 }
3111 ++id;
3112 }
3113
3114 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
3115 if (prefix) {
3116 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
3117 if (!mod) {
3118 /* module must always be found */
3119 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003120 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003121 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003122 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003123 return LY_ENOTFOUND;
3124 }
3125 } else {
3126 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003127 case LY_VALUE_SCHEMA:
3128 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003129 /* use the current module */
Michal Vasko56d8da82021-12-16 15:41:30 +01003130 mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003131 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003132 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02003133 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003134 if (!ctx_node) {
3135 LOGINT_RET(ctx->ctx);
3136 }
3137 /* inherit the module of the previous context node */
3138 mod = ctx_node->module;
3139 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02003140 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02003141 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01003142 case LY_VALUE_STR_NS:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003143 /* not really defined */
3144 LOGINT_RET(ctx->ctx);
3145 }
3146 }
3147
3148 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
3149 /* move through input/output manually */
3150 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01003151 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3152 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003153 return LY_ENOTFOUND;
3154 }
3155 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003156 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003157 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003158 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003159 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
3160 } else {
3161 /* only input or output is valid */
3162 ctx_node = NULL;
3163 }
Michal Vasko0b50f6b2022-10-05 15:07:55 +02003164 } else if (ctx->ext && !ctx_node) {
3165 /* top-level extension nodes */
3166 ctx_node = lysc_ext_find_node(ctx->ext, mod, name, name_len, 0, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003167 } else {
3168 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003169 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003170 getnext_extra_flag = 0;
3171 }
3172 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01003173 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3174 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003175 return LY_ENOTFOUND;
3176 }
3177 current_nodetype = ctx_node->nodetype;
3178
3179 if (current_nodetype == LYS_NOTIF) {
3180 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
3181 } else if (current_nodetype == LYS_INPUT) {
3182 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
3183 } else if (current_nodetype == LYS_OUTPUT) {
3184 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
3185 }
3186
3187 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
3188 break;
3189 }
3190 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003191 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003192 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01003193 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003194 return LY_EVALID;
3195 }
3196 ++id;
3197 }
3198
3199 if (ret == LY_SUCCESS) {
3200 *target = ctx_node;
3201 if (nodetype && !(current_nodetype & nodetype)) {
3202 return LY_EDENIED;
3203 }
3204 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003205 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003206 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01003207 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003208 }
3209
3210 return ret;
3211}
3212
3213/**
3214 * @brief Compile information about list's uniques.
3215 * @param[in] ctx Compile context.
3216 * @param[in] uniques Sized array list of unique statements.
3217 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3218 * @return LY_ERR value.
3219 */
3220static LY_ERR
3221lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
3222{
3223 LY_ERR ret = LY_SUCCESS;
3224 struct lysc_node_leaf **key, ***unique;
3225 struct lysc_node *parent;
3226 const char *keystr, *delim;
3227 size_t len;
3228 LY_ARRAY_COUNT_TYPE v;
3229 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
3230 uint16_t flags;
3231
3232 LY_ARRAY_FOR(uniques, v) {
3233 config = -1;
3234 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3235 keystr = uniques[v].str;
3236 while (keystr) {
3237 delim = strpbrk(keystr, " \t\n");
3238 if (delim) {
3239 len = delim - keystr;
3240 while (isspace(*delim)) {
3241 ++delim;
3242 }
3243 } else {
3244 len = strlen(keystr);
3245 }
3246
3247 /* unique node must be present */
3248 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko56d8da82021-12-16 15:41:30 +01003249 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, LY_VALUE_SCHEMA, (void *)uniques[v].mod,
3250 LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003251 if (ret != LY_SUCCESS) {
3252 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003253 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003254 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01003255 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003256 }
3257 return LY_EVALID;
3258 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003259 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003260 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003261 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003262 return LY_EVALID;
3263 }
3264
3265 /* all referenced leafs must be of the same config type */
3266 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3267 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003268 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003269 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3270 return LY_EVALID;
3271 } else if ((*key)->flags & LYS_CONFIG_W) {
3272 config = 1;
3273 } else { /* LYS_CONFIG_R */
3274 config = 0;
3275 }
3276
3277 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3278 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3279 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003280 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003281 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3282 return LY_EVALID;
3283 }
3284 }
3285
3286 /* check status */
Michal Vaskoc130e162021-10-19 11:30:00 +02003287 LY_CHECK_RET(lysc_check_status(ctx, list->flags, uniques[v].mod->mod, list->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003288 (*key)->flags, (*key)->module, (*key)->name));
3289
3290 /* mark leaf as unique */
3291 (*key)->flags |= LYS_UNIQUE;
3292
3293 /* next unique value in line */
3294 keystr = delim;
3295 }
3296 /* next unique definition */
3297 }
3298
3299 return LY_SUCCESS;
3300}
3301
3302/**
3303 * @brief Compile parsed list node information.
3304 * @param[in] ctx Compile context
3305 * @param[in] pnode Parsed list node.
3306 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3307 * is enriched with the list-specific information.
3308 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3309 */
3310static LY_ERR
3311lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3312{
3313 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3314 struct lysc_node_list *list = (struct lysc_node_list *)node;
3315 struct lysp_node *child_p;
Michal Vasko2d6507a2022-03-16 09:40:52 +01003316 struct lysc_node *parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003317 struct lysc_node_leaf *key, *prev_key = NULL;
3318 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003319 const char *keystr, *delim;
3320 LY_ERR ret = LY_SUCCESS;
3321
3322 list->min = list_p->min;
3323 if (list->min) {
3324 list->flags |= LYS_MAND_TRUE;
3325 }
3326 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3327
3328 LY_LIST_FOR(list_p->child, child_p) {
3329 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3330 }
3331
Michal Vasko5347e3a2020-11-03 17:14:57 +01003332 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003333
3334 /* add must(s) to unres */
3335 ret = lysc_unres_must_add(ctx, node, pnode);
3336 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003337
3338 /* keys */
Michal Vasko2d6507a2022-03-16 09:40:52 +01003339 if (list->flags & LYS_CONFIG_W) {
3340 parent = node;
3341 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
3342 /* compiling individual grouping, we can check this only if there is an explicit config set */
3343 while (parent) {
3344 if (parent->flags & LYS_SET_CONFIG) {
3345 break;
3346 }
3347 parent = parent->parent;
3348 }
3349 }
3350
3351 if (parent && (!list_p->key || !list_p->key[0])) {
3352 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3353 return LY_EVALID;
3354 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003355 }
3356
3357 /* find all the keys (must be direct children) */
3358 keystr = list_p->key;
3359 if (!keystr) {
3360 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003361 list->flags &= ~LYS_ORDBY_SYSTEM;
3362 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003363 }
3364 while (keystr) {
3365 delim = strpbrk(keystr, " \t\n");
3366 if (delim) {
3367 len = delim - keystr;
3368 while (isspace(*delim)) {
3369 ++delim;
3370 }
3371 } else {
3372 len = strlen(keystr);
3373 }
3374
3375 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003376 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3377 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003378 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003379 return LY_EVALID;
3380 }
3381 /* keys must be unique */
3382 if (key->flags & LYS_KEY) {
3383 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003384 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003385 return LY_EVALID;
3386 }
3387
Radek Krejcia6016992021-03-03 10:13:41 +01003388 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003389 /* key must have the same config flag as the list itself */
3390 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003391 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of a configuration list must not be a state leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003392 return LY_EVALID;
3393 }
3394 if (ctx->pmod->version < LYS_VERSION_1_1) {
3395 /* YANG 1.0 denies key to be of empty type */
3396 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003397 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003398 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3399 return LY_EVALID;
3400 }
3401 } else {
3402 /* when and if-feature are illegal on list keys */
3403 if (key->when) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003404 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List's key must not have any \"when\" statement.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003405 return LY_EVALID;
3406 }
Michal Vasko93b4ab92022-05-13 12:29:59 +02003407 /* unable to check if-features but compilation would fail if disabled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003408 }
3409
3410 /* check status */
Michal Vaskoc636ea42022-09-16 10:20:31 +02003411 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, key->flags, key->module, key->name));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003412
3413 /* ignore default values of the key */
3414 if (key->dflt) {
3415 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003416 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)key->dflt->realtype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003417 free(key->dflt);
3418 key->dflt = NULL;
3419 }
3420 /* mark leaf as key */
3421 key->flags |= LYS_KEY;
3422
3423 /* move it to the correct position */
3424 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3425 /* fix links in closest previous siblings of the key */
3426 if (key->next) {
3427 key->next->prev = key->prev;
3428 } else {
3429 /* last child */
3430 list->child->prev = key->prev;
3431 }
3432 if (key->prev->next) {
3433 key->prev->next = key->next;
3434 }
3435 /* fix links in the key */
3436 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003437 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003438 key->next = prev_key->next;
3439 } else {
3440 key->prev = list->child->prev;
3441 key->next = list->child;
3442 }
3443 /* fix links in closes future siblings of the key */
3444 if (prev_key) {
3445 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003446 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003447 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003448 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003449 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003450 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003451 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003452 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003453 }
3454 /* fix links in parent */
3455 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003456 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003457 }
3458 }
3459
3460 /* next key value */
3461 prev_key = key;
3462 keystr = delim;
3463 lysc_update_path(ctx, NULL, NULL);
3464 }
3465
Michal Vasko95f736c2022-06-08 12:03:31 +02003466 /* connect any augments */
3467 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3468
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003469 /* uniques */
3470 if (list_p->uniques) {
3471 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3472 }
3473
Radek Krejci2a9fc652021-01-22 17:44:34 +01003474 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3475 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3476 LY_CHECK_GOTO(ret, done);
3477 }
3478 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3479 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3480 LY_CHECK_GOTO(ret, done);
3481 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003482
3483 /* checks */
3484 if (list->min > list->max) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003485 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.", list->min, list->max);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003486 return LY_EVALID;
3487 }
3488
3489done:
3490 return ret;
3491}
3492
3493/**
3494 * @brief Do some checks and set the default choice's case.
3495 *
3496 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3497 *
3498 * @param[in] ctx Compile context.
3499 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3500 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3501 * @return LY_ERR value.
3502 */
3503static LY_ERR
3504lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3505{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003506 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003507 const struct lys_module *mod;
3508 const char *prefix = NULL, *name;
3509 size_t prefix_len = 0;
3510
3511 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3512 name = strchr(dflt->str, ':');
3513 if (name) {
3514 prefix = dflt->str;
3515 prefix_len = name - prefix;
3516 ++name;
3517 } else {
3518 name = dflt->str;
3519 }
3520 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003521 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003522 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003523 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003524 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003525 return LY_EVALID;
3526 }
3527 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003528 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003529 }
3530
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003531 ch->dflt = (struct lysc_node_case *)lys_find_child(&ch->node, mod, name, 0, LYS_CASE, LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003532 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003533 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003534 "Default case \"%s\" not found.", dflt->str);
3535 return LY_EVALID;
3536 }
3537
3538 /* no mandatory nodes directly under the default case */
3539 LY_LIST_FOR(ch->dflt->child, iter) {
3540 if (iter->parent != (struct lysc_node *)ch->dflt) {
3541 break;
3542 }
3543 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003544 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003545 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3546 return LY_EVALID;
3547 }
3548 }
3549
3550 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003551 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003552 return LY_EVALID;
3553 }
3554
3555 ch->dflt->flags |= LYS_SET_DFLT;
3556 return LY_SUCCESS;
3557}
3558
3559LY_ERR
3560lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3561 struct ly_set *child_set)
3562{
3563 LY_ERR ret = LY_SUCCESS;
3564 struct lysp_node *child_p_next = child_p->next;
3565 struct lysp_node_case *cs_p;
Michal Vasko4eb49d52022-02-17 15:05:00 +01003566 struct lysc_node_case *cs_c;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003567
3568 if (child_p->nodetype == LYS_CASE) {
3569 /* standard case under choice */
3570 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3571 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003572 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003573 cs_p = calloc(1, sizeof *cs_p);
Michal Vasko786a8832022-12-05 10:41:27 +01003574 LY_CHECK_ERR_RET(!cs_p, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003575 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003576 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003577 cs_p->child = child_p;
3578
3579 /* make the child the only case child */
3580 child_p->next = NULL;
3581
3582 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003583 LY_CHECK_GOTO(ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set), revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003584
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003585 if (((struct lysc_node_choice *)node)->cases) {
Michal Vasko4eb49d52022-02-17 15:05:00 +01003586 /* find our case node */
3587 cs_c = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases;
3588 while (cs_c->name != cs_p->name) {
3589 cs_c = (struct lysc_node_case *)cs_c->next;
3590 assert(cs_c);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003591 }
aPiecekaa320c92021-05-21 07:34:24 +02003592
Michal Vasko4eb49d52022-02-17 15:05:00 +01003593 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3594 /* compiled case node cannot point to his corresponding parsed node
3595 * because it exists temporarily so it must be set to NULL
3596 */
3597 assert(cs_c->priv == cs_p);
3598 cs_c->priv = NULL;
3599 }
3600
3601 /* status is copied from his child and not from his parent as usual. */
3602 if (cs_c->child) {
3603 cs_c->flags &= ~LYS_STATUS_MASK;
3604 cs_c->flags |= (LYS_STATUS_MASK & cs_c->child->flags);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003605 }
3606 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003607
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003608revert_sh_case:
3609 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003610 cs_p->child = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003611 lysp_node_free(&ctx->free_ctx, (struct lysp_node *)cs_p);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003612 child_p->next = child_p_next;
3613 }
3614
3615 return ret;
3616}
3617
3618/**
3619 * @brief Compile parsed choice node information.
3620 *
3621 * @param[in] ctx Compile context
3622 * @param[in] pnode Parsed choice node.
3623 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3624 * is enriched with the choice-specific information.
3625 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3626 */
3627static LY_ERR
3628lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3629{
3630 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3631 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3632 struct lysp_node *child_p;
3633 LY_ERR ret = LY_SUCCESS;
3634
3635 assert(node->nodetype == LYS_CHOICE);
3636
3637 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003638 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, child_p, node, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003639 }
3640
Michal Vasko95f736c2022-06-08 12:03:31 +02003641 /* connect any augments */
3642 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3643
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003644 /* default branch */
3645 if (ch_p->dflt.str) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003646 LY_CHECK_GOTO(ret = lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003647 }
3648
Michal Vasko95f736c2022-06-08 12:03:31 +02003649done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003650 return ret;
3651}
3652
3653/**
3654 * @brief Compile parsed anydata or anyxml node information.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003655 *
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003656 * @param[in] ctx Compile context
3657 * @param[in] pnode Parsed anydata or anyxml node.
3658 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3659 * is enriched with the any-specific information.
3660 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3661 */
3662static LY_ERR
3663lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3664{
3665 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3666 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003667 LY_ERR ret = LY_SUCCESS;
3668
Michal Vasko5347e3a2020-11-03 17:14:57 +01003669 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003670
3671 /* add must(s) to unres */
3672 ret = lysc_unres_must_add(ctx, node, pnode);
3673 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003674
Radek Krejci91531e12021-02-08 19:57:54 +01003675 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003676 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko193dacd2022-10-13 08:43:05 +02003677 lyplg_ext_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003678 }
3679done:
3680 return ret;
3681}
3682
3683/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003684 * @brief Prepare the case structure in choice node for the new data node.
3685 *
3686 * It is able to handle implicit as well as explicit cases and the situation when the case has multiple data nodes and the case was already
3687 * created in the choice when the first child was processed.
3688 *
3689 * @param[in] ctx Compile context.
3690 * @param[in] pnode Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case,
3691 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3692 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3693 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3694 * @return The case structure where the child node belongs to, NULL in case of error. Note that the child is not connected into the siblings list,
3695 * it is linked from the case structure only in case it is its first child.
3696 */
3697static LY_ERR
3698lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3699{
Michal Vasko95f736c2022-06-08 12:03:31 +02003700 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003701 struct lysp_node *child_p;
3702 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3703
3704 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3705 /* we have to add an implicit case node into the parent choice */
3706 } else if (pnode->nodetype == LYS_CASE) {
3707 /* explicit parent case */
3708 LY_LIST_FOR(cs_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003709 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003710 }
3711 } else {
3712 LOGINT_RET(ctx->ctx);
3713 }
3714
Michal Vasko95f736c2022-06-08 12:03:31 +02003715 /* connect any augments */
3716 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3717
3718done:
3719 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003720}
3721
3722void
3723lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3724{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003725 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003726
3727 if (add) { /* set flag */
3728 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3729 parent = parent->parent) {
3730 parent->flags |= LYS_MAND_TRUE;
3731 }
3732 } else { /* unset flag */
3733 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003734 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003735 if (iter->flags & LYS_MAND_TRUE) {
3736 /* there is another mandatory node */
3737 return;
3738 }
3739 }
3740 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3741 parent->flags &= ~LYS_MAND_TRUE;
3742 }
3743 }
3744}
3745
3746/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003747 * @brief Get the grouping with the specified name from given groupings sized array.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003748 *
3749 * @param[in] node Linked list of nodes with groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003750 * @param[in] name Name of the grouping to find,
3751 * @return NULL when there is no grouping with the specified name
3752 * @return Pointer to the grouping of the specified @p name.
3753 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003754static struct lysp_node_grp *
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003755match_grouping(const struct lysp_node_grp *node, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003756{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003757 LY_LIST_FOR(node, node) {
3758 if ((node->nodetype == LYS_GROUPING) && !strcmp(node->name, name)) {
3759 return (struct lysp_node_grp *)node;
Radek Krejciabd33a42021-01-20 17:18:59 +01003760 }
3761 }
3762
3763 return NULL;
3764}
3765
3766/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003767 * @brief Find grouping for a uses.
3768 *
3769 * @param[in] ctx Compile context.
3770 * @param[in] uses_p Parsed uses node.
3771 * @param[out] gpr_p Found grouping on success.
3772 * @param[out] grp_pmod Module of @p grp_p on success.
3773 * @return LY_ERR value.
3774 */
3775static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003776lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_node_grp **grp_p,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003777 struct lysp_module **grp_pmod)
3778{
3779 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003780 struct lysp_node_grp *grp;
Michal Vasko193dacd2022-10-13 08:43:05 +02003781 const struct lysp_node_grp *ext_grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003782 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003783 const char *id, *name, *prefix, *local_pref;
3784 size_t prefix_len, name_len;
3785 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003786 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003787
3788 *grp_p = NULL;
3789 *grp_pmod = NULL;
3790
3791 /* search for the grouping definition */
3792 id = uses_p->name;
3793 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3794 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3795 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3796 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003797 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003798 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003799 if ((grp = match_grouping(lysp_node_groupings(pnode), name))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003800 found = ctx->pmod;
3801 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003802 }
3803 }
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003804
3805 /* if in an extension, search possible groupings in it */
Michal Vaskob62e1e32022-11-08 10:50:41 +01003806 if (!found && ctx->ext) {
Michal Vaskofbd037c2022-11-08 10:34:20 +01003807 lyplg_ext_parsed_get_storage(ctx->ext, LY_STMT_GROUPING, sizeof ext_grp, (const void **)&ext_grp);
Michal Vasko193dacd2022-10-13 08:43:05 +02003808 if ((grp = match_grouping(ext_grp, name))) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003809 found = ctx->pmod;
3810 }
3811 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003812 } else {
3813 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003814 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003815 if (!mod) {
Michal Vasko9925a112022-12-14 12:15:50 +01003816 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid prefix used for grouping \"%s\" reference.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003817 return LY_EVALID;
3818 }
3819 pmod = mod->parsed;
3820 }
3821
3822 if (!found) {
3823 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003824 if ((grp = match_grouping(pmod->groupings, name))) {
3825 found = pmod;
3826 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003827 /* ... and all the submodules */
3828 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003829 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3830 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003831 break;
3832 }
3833 }
3834 }
3835 }
3836 if (!found) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003837 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003838 return LY_EVALID;
3839 }
3840
Michal Vasko7c565922021-06-10 14:58:27 +02003841 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003842 /* remember that the grouping is instantiated to avoid its standalone validation */
3843 grp->flags |= LYS_USED_GRP;
3844 }
3845
3846 *grp_p = grp;
3847 *grp_pmod = found;
3848 return LY_SUCCESS;
3849}
3850
3851/**
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003852 * @brief Compile uses grouping children.
3853 *
3854 * @param[in] ctx Compile context.
3855 * @param[in] uses_p Parsed uses.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003856 * @param[in] inherited_flags Inherited flags from the uses.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003857 * @param[in] child First grouping child to compile.
3858 * @param[in] grp_mod Grouping parsed module.
3859 * @param[in] parent Uses compiled parent, may be NULL if top-level.
3860 * @param[in,out] child_set Set of all compiled child nodes.
3861 * @param[in] child_unres_disabled Whether the children are to be put into unres disabled set or not.
3862 * @return LY_SUCCESS on success.
3863 * @return LY_EVALID on failure.
3864 */
3865static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003866lys_compile_uses_children(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, uint16_t inherited_flags,
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003867 struct lysp_node *child, struct lysp_module *grp_mod, struct lysc_node *parent, struct ly_set *child_set,
3868 ly_bool child_unres_disabled)
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003869{
3870 LY_ERR rc = LY_SUCCESS;
3871 struct lysp_module *mod_old = ctx->pmod;
3872 uint32_t child_i, opt_prev = ctx->compile_opts;
3873 ly_bool enabled;
3874 struct lysp_node *pnode;
3875 struct lysc_node *node;
3876 struct lysc_when *when_shared = NULL;
3877
3878 assert(child_set);
3879
Michal Vasko5940c302022-07-14 13:54:38 +02003880 child_i = child_set->count;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003881 LY_LIST_FOR(child, pnode) {
3882 /* compile the nodes with their parsed (grouping) module */
3883 ctx->pmod = grp_mod;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003884 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, parent, inherited_flags, child_set), cleanup);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003885
3886 /* eval if-features again for the rest of this node processing */
3887 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
3888 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3889 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3890 }
3891
3892 /* restore the parsed module */
3893 ctx->pmod = mod_old;
3894
3895 /* since the uses node is not present in the compiled tree, we need to pass some of its
3896 * statements to all its children */
3897 while (child_i < child_set->count) {
3898 node = child_set->snodes[child_i];
3899
3900 if (uses_p->when) {
3901 /* pass uses when to all the children */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003902 rc = lys_compile_when(ctx, uses_p->when, inherited_flags, parent, lysc_data_node(parent), node, &when_shared);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003903 LY_CHECK_GOTO(rc, cleanup);
3904 }
3905
3906 if (child_unres_disabled) {
3907 /* child is disabled by the uses if-features */
3908 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
3909 }
3910
3911 /* child processed */
3912 ++child_i;
3913 }
3914
3915 /* next iter */
3916 ctx->compile_opts = opt_prev;
3917 }
3918
3919cleanup:
3920 ctx->compile_opts = opt_prev;
3921 return rc;
3922}
3923
3924/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003925 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3926 * If present, also apply uses's modificators.
3927 *
3928 * @param[in] ctx Compile context
3929 * @param[in] uses_p Parsed uses schema node.
3930 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3931 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3932 * the compile context.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003933 * @param[in] inherited_flags Inherited flags from a schema-only statement.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003934 * @param[in] child_set Optional set of all the compiled children.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003935 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3936 */
3937static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003938lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, uint16_t inherited_flags,
3939 struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003940{
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003941 LY_ERR rc = LY_SUCCESS;
3942 ly_bool enabled, child_unres_disabled = 0;
3943 uint32_t i, grp_stack_count, opt_prev = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003944 struct lysp_node_grp *grp = NULL;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003945 uint16_t uses_flags = 0;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003946 struct lysp_module *grp_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003947 struct ly_set uses_child_set = {0};
3948
3949 /* find the referenced grouping */
3950 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3951
3952 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3953 grp_stack_count = ctx->groupings.count;
3954 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3955 if (grp_stack_count == ctx->groupings.count) {
3956 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003957 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003958 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3959 return LY_EVALID;
3960 }
3961
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003962 /* nodetype checks */
3963 if (grp->actions && (parent && !lysc_node_actions_p(parent))) {
3964 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3965 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3966 parent->name, lys_nodetype2str(parent->nodetype));
3967 rc = LY_EVALID;
3968 goto cleanup;
3969 }
3970 if (grp->notifs && (parent && !lysc_node_notifs_p(parent))) {
3971 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3972 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3973 parent->name, lys_nodetype2str(parent->nodetype));
3974 rc = LY_EVALID;
3975 goto cleanup;
3976 }
3977
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003978 /* check status */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003979 rc = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3980 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003981
3982 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003983 rc = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3984 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003985
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003986 /* compile special uses status flags */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003987 rc = lys_compile_status(ctx, uses_p->flags, inherited_flags, parent ? parent->flags : 0,
3988 parent ? parent->name : NULL, "<uses>", &uses_flags);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003989 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003990
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003991 /* uses if-features */
3992 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, uses_p->iffeatures, &enabled), cleanup);
3993 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3994 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3995 child_unres_disabled = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003996 }
3997
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003998 /* uses grouping children */
3999 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, grp->child, grp_mod, parent,
4000 child_set ? child_set : &uses_child_set, child_unres_disabled);
4001 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004002
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004003 /* uses grouping RPCs/actions */
4004 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->actions, grp_mod, parent,
4005 child_set ? child_set : &uses_child_set, child_unres_disabled);
4006 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004007
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004008 /* uses grouping notifications */
4009 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->notifs, grp_mod, parent,
4010 child_set ? child_set : &uses_child_set, child_unres_disabled);
4011 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004012
4013 /* check that all augments were applied */
4014 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01004015 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
4016 /* augment of some parent uses, irrelevant now */
4017 continue;
4018 }
4019
4020 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004021 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004022 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004023 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004024 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004025
4026 /* check that all refines were applied */
4027 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01004028 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004029 /* refine of some parent uses, irrelevant now */
Michal Vaskod8655722021-01-12 15:20:36 +01004030 continue;
4031 }
4032
4033 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004034 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004035 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004036 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004037 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004038
Michal Vasko193dacd2022-10-13 08:43:05 +02004039 /* compile uses and grouping extensions into the parent */
4040 COMPILE_EXTS_GOTO(ctx, uses_p->exts, parent->exts, parent, rc, cleanup);
4041 COMPILE_EXTS_GOTO(ctx, grp->exts, parent->exts, parent, rc, cleanup);
4042
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004043cleanup:
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004044 /* restore previous context */
4045 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004046
4047 /* remove the grouping from the stack for circular groupings dependency check */
4048 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4049 assert(ctx->groupings.count == grp_stack_count);
4050
4051 ly_set_erase(&uses_child_set, NULL);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004052 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004053}
4054
4055static int
4056lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
4057{
4058 struct lysp_node *iter;
4059 int len = 0;
4060
4061 *path = NULL;
4062 for (iter = node; iter && len >= 0; iter = iter->parent) {
4063 char *s = *path;
4064 char *id;
4065
4066 switch (iter->nodetype) {
4067 case LYS_USES:
4068 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
4069 break;
4070 case LYS_GROUPING:
4071 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
4072 break;
4073 case LYS_AUGMENT:
4074 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
4075 break;
4076 default:
4077 id = strdup(iter->name);
4078 break;
4079 }
4080
4081 if (!iter->parent) {
4082 /* print prefix */
4083 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
4084 } else {
4085 /* prefix is the same as in parent */
4086 len = asprintf(path, "/%s%s", id, s ? s : "");
4087 }
4088 free(s);
4089 free(id);
4090 }
4091
4092 if (len < 0) {
4093 free(*path);
4094 *path = NULL;
4095 } else if (len == 0) {
4096 *path = strdup("/");
4097 len = 1;
4098 }
4099 return len;
4100}
4101
4102LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01004103lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004104{
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004105 LY_ERR rc = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004106 char *path;
4107 int len;
4108
Michal Vasko8254d852022-04-25 10:05:59 +02004109 /* use grouping status to avoid errors */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004110 struct lysp_node_uses fake_uses = {
4111 .parent = pnode,
4112 .nodetype = LYS_USES,
Michal Vasko8254d852022-04-25 10:05:59 +02004113 .flags = grp->flags & LYS_STATUS_MASK, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004114 .name = grp->name,
4115 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4116 .refines = NULL, .augments = NULL
4117 };
4118 struct lysc_node_container fake_container = {
4119 .nodetype = LYS_CONTAINER,
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004120 .flags = 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004121 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02004122 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01004123 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004124 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004125 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004126 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4127 };
4128
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004129 /* compile fake container flags */
4130 LY_CHECK_GOTO(rc = lys_compile_node_flags(ctx, pnode ? pnode->flags : 0, 0, &fake_container.node), cleanup);
4131
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004132 if (grp->parent) {
4133 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4134 }
4135
4136 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
4137 if (len < 0) {
4138 LOGMEM(ctx->ctx);
4139 return LY_EMEM;
4140 }
4141 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
4142 ctx->path_len = (uint32_t)len;
4143 free(path);
4144
4145 lysc_update_path(ctx, NULL, "{grouping}");
4146 lysc_update_path(ctx, NULL, grp->name);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004147 rc = lys_compile_uses(ctx, &fake_uses, &fake_container.node, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004148 lysc_update_path(ctx, NULL, NULL);
4149 lysc_update_path(ctx, NULL, NULL);
4150
4151 ctx->path_len = 1;
4152 ctx->path[1] = '\0';
4153
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004154cleanup:
Michal Vaskoc636ea42022-09-16 10:20:31 +02004155 lysc_node_container_free(&ctx->free_ctx, &fake_container);
Michal Vaskob7402382023-01-04 12:05:06 +01004156 FREE_ARRAY(&ctx->free_ctx, fake_container.exts, lysc_ext_instance_free);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004157 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004158}
4159
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004160LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004161lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t inherited_flags,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004162 struct ly_set *child_set)
4163{
4164 LY_ERR ret = LY_SUCCESS;
4165 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02004166 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004167
4168 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
4169
4170 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01004171 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004172 } else {
4173 lysc_update_path(ctx, NULL, "{uses}");
4174 lysc_update_path(ctx, NULL, pnode->name);
4175 }
4176
4177 switch (pnode->nodetype) {
4178 case LYS_CONTAINER:
4179 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
4180 node_compile_spec = lys_compile_node_container;
4181 break;
4182 case LYS_LEAF:
4183 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
4184 node_compile_spec = lys_compile_node_leaf;
4185 break;
4186 case LYS_LIST:
4187 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
4188 node_compile_spec = lys_compile_node_list;
4189 break;
4190 case LYS_LEAFLIST:
4191 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
4192 node_compile_spec = lys_compile_node_leaflist;
4193 break;
4194 case LYS_CHOICE:
4195 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
4196 node_compile_spec = lys_compile_node_choice;
4197 break;
4198 case LYS_CASE:
4199 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
4200 node_compile_spec = lys_compile_node_case;
4201 break;
4202 case LYS_ANYXML:
4203 case LYS_ANYDATA:
4204 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
4205 node_compile_spec = lys_compile_node_any;
4206 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004207 case LYS_RPC:
4208 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02004209 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004210 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4211 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004212 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004213 return LY_EVALID;
4214 }
4215 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
4216 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02004217 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004218 break;
4219 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02004220 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004221 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4222 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004223 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004224 return LY_EVALID;
4225 }
4226 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
4227 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02004228 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004229 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004230 case LYS_USES:
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004231 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, inherited_flags, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004232 lysc_update_path(ctx, NULL, NULL);
4233 lysc_update_path(ctx, NULL, NULL);
4234 return ret;
4235 default:
4236 LOGINT(ctx->ctx);
4237 return LY_EINT;
4238 }
4239 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4240
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004241 ret = lys_compile_node_(ctx, pnode, parent, inherited_flags, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004242
Michal Vasko7c565922021-06-10 14:58:27 +02004243 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004244 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004245 return ret;
4246}