blob: 3dc0fdce0cb062f404c30f8f4d89bd0f33b860b2 [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 Vasko0b50f6b2022-10-05 15:07:55 +02007 * Copyright (c) 2015 - 2022 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
46static struct lysc_ext_instance *
47lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
48{
49 /* TODO - extensions, increase refcount */
50 (void) ctx;
51 (void) orig;
52 return NULL;
53}
54
55/**
Michal Vaskoc130e162021-10-19 11:30:00 +020056 * @brief Add a node with must(s) to unres.
57 *
58 * @param[in] ctx Compile context.
59 * @param[in] node Compiled node with must(s).
60 * @param[in] pnode Parsed ndoe with must(s).
61 * @return LY_ERR value.
62 */
63static LY_ERR
64lysc_unres_must_add(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_node *pnode)
65{
66 struct lysc_unres_must *m = NULL;
67 LY_ARRAY_COUNT_TYPE u;
68 struct lysc_must *musts;
69 struct lysp_restr *pmusts;
70 LY_ERR ret;
71
72 /* do not check must(s) in a grouping */
73 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
74 return LY_SUCCESS;
75 }
76
77 musts = lysc_node_musts(node);
78 pmusts = lysp_node_musts(pnode);
79 assert(LY_ARRAY_COUNT(musts) == LY_ARRAY_COUNT(pmusts));
80
81 if (!musts) {
82 /* no must */
83 return LY_SUCCESS;
84 }
85
86 /* add new unres must */
87 m = calloc(1, sizeof *m);
88 LY_CHECK_ERR_GOTO(!m, ret = LY_EMEM, error);
89 m->node = node;
90
91 /* add must local modules */
92 LY_ARRAY_CREATE_GOTO(ctx->ctx, m->local_mods, LY_ARRAY_COUNT(pmusts), ret, error);
93 LY_ARRAY_FOR(pmusts, u) {
94 m->local_mods[u] = pmusts[u].arg.mod;
95 LY_ARRAY_INCREMENT(m->local_mods);
96 }
97
Michal Vaskoedb0fa52022-10-04 10:36:00 +020098 /* store ext */
99 m->ext = ctx->ext;
100
Michal Vaskoc130e162021-10-19 11:30:00 +0200101 LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->musts, m, 1, NULL), ret = LY_EMEM, error);
102
103 return LY_SUCCESS;
104
105error:
106 if (m) {
107 LY_ARRAY_FREE(m->local_mods);
108 free(m);
109 }
110 LOGMEM(ctx->ctx);
111 return ret;
112}
113
114static LY_ERR
115lysc_unres_leafref_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const struct lysp_module *local_mod)
116{
117 struct lysc_unres_leafref *l = NULL;
118 struct ly_set *leafrefs_set;
119 LY_ARRAY_COUNT_TYPE u;
120 int is_lref = 0;
121
122 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
123 /* do not check leafrefs in groupings */
124 return LY_SUCCESS;
125 }
126
127 /* use special set for disabled leafrefs */
128 leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
129
130 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
131 /* leafref */
132 is_lref = 1;
133 } else if (leaf->type->basetype == LY_TYPE_UNION) {
134 /* union with leafrefs */
135 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
136 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
137 is_lref = 1;
138 break;
139 }
140 }
141 }
142
143 if (is_lref) {
144 /* add new unresolved leafref node */
145 l = calloc(1, sizeof *l);
146 LY_CHECK_ERR_RET(!l, LOGMEM(ctx->ctx), LY_EMEM);
147
148 l->node = &leaf->node;
149 l->local_mod = local_mod;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200150 l->ext = ctx->ext;
Michal Vaskoc130e162021-10-19 11:30:00 +0200151
152 LY_CHECK_ERR_RET(ly_set_add(leafrefs_set, l, 1, NULL), free(l); LOGMEM(ctx->ctx), LY_EMEM);
153 }
154
155 return LY_SUCCESS;
156}
157
158/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200159 * @brief Add/replace a leaf default value in unres.
160 * Can also be used for a single leaf-list default value.
161 *
162 * @param[in] ctx Compile context.
163 * @param[in] leaf Leaf with the default value.
164 * @param[in] dflt Default value to use.
165 * @return LY_ERR value.
166 */
167static LY_ERR
168lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
169{
170 struct lysc_unres_dflt *r = NULL;
171 uint32_t i;
172
Michal Vasko7c565922021-06-10 14:58:27 +0200173 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100174 return LY_SUCCESS;
175 }
176
Michal Vasko405cc9e2020-12-01 12:01:27 +0100177 for (i = 0; i < ctx->unres->dflts.count; ++i) {
178 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200179 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100180 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200181 lysp_qname_free(ctx->ctx, r->dflt);
182 free(r->dflt);
183 break;
184 }
185 }
186 if (!r) {
187 /* add new unres item */
188 r = calloc(1, sizeof *r);
189 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
190 r->leaf = leaf;
191
Michal Vasko405cc9e2020-12-01 12:01:27 +0100192 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200193 }
194
195 r->dflt = malloc(sizeof *r->dflt);
196 LY_CHECK_GOTO(!r->dflt, error);
Michal Vaskoc621d862022-11-08 14:23:45 +0100197 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, dflt, r->dflt), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200198
199 return LY_SUCCESS;
200
201error:
202 free(r->dflt);
203 LOGMEM(ctx->ctx);
204 return LY_EMEM;
205}
206
207/**
208 * @brief Add/replace a leaf-list default value(s) in unres.
209 *
210 * @param[in] ctx Compile context.
211 * @param[in] llist Leaf-list with the default value.
212 * @param[in] dflts Sized array of the default values.
213 * @return LY_ERR value.
214 */
215static LY_ERR
216lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
217{
218 struct lysc_unres_dflt *r = NULL;
219 uint32_t i;
220
Michal Vasko7c565922021-06-10 14:58:27 +0200221 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100222 return LY_SUCCESS;
223 }
224
Michal Vasko405cc9e2020-12-01 12:01:27 +0100225 for (i = 0; i < ctx->unres->dflts.count; ++i) {
226 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200227 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100228 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200229 lysp_qname_free(ctx->ctx, r->dflt);
230 free(r->dflt);
231 r->dflt = NULL;
232 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
233 r->dflts = NULL;
234 break;
235 }
236 }
237 if (!r) {
238 r = calloc(1, sizeof *r);
239 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
240 r->llist = llist;
241
Michal Vasko405cc9e2020-12-01 12:01:27 +0100242 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200243 }
244
245 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
246
247 return LY_SUCCESS;
248}
249
250/**
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100251 * @brief Add a bits/enumeration type to unres.
252 *
253 * @param[in] ctx Compile context.
254 * @param[in] leaf Leaf of type bits/enumeration whose disabled items to free.
255 * @return LY_ERR value.
256 */
257static LY_ERR
258lysc_unres_bitenum_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
259{
260 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
261 /* skip groupings and redundant for disabled nodes */
262 return LY_SUCCESS;
263 }
264
265 LY_CHECK_RET(ly_set_add(&ctx->unres->disabled_bitenums, leaf, 1, NULL));
266
267 return LY_SUCCESS;
268}
269
270/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200271 * @brief Duplicate the compiled pattern structure.
272 *
273 * Instead of duplicating memory, the reference counter in the @p orig is increased.
274 *
275 * @param[in] orig The pattern structure to duplicate.
276 * @return The duplicated structure to use.
277 */
278static struct lysc_pattern *
279lysc_pattern_dup(struct lysc_pattern *orig)
280{
281 ++orig->refcount;
282 return orig;
283}
284
285/**
286 * @brief Duplicate the array of compiled patterns.
287 *
288 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
289 *
290 * @param[in] ctx Libyang context for logging.
291 * @param[in] orig The patterns sized array to duplicate.
292 * @return New sized array as a copy of @p orig.
293 * @return NULL in case of memory allocation error.
294 */
295static struct lysc_pattern **
296lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
297{
298 struct lysc_pattern **dup = NULL;
299 LY_ARRAY_COUNT_TYPE u;
300
301 assert(orig);
302
303 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
304 LY_ARRAY_FOR(orig, u) {
305 dup[u] = lysc_pattern_dup(orig[u]);
306 LY_ARRAY_INCREMENT(dup);
307 }
308 return dup;
309}
310
311/**
312 * @brief Duplicate compiled range structure.
313 *
314 * @param[in] ctx Libyang context for logging.
315 * @param[in] orig The range structure to be duplicated.
316 * @return New compiled range structure as a copy of @p orig.
317 * @return NULL in case of memory allocation error.
318 */
319static struct lysc_range *
320lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
321{
322 struct lysc_range *dup;
323 LY_ERR ret;
324
325 assert(orig);
326
327 dup = calloc(1, sizeof *dup);
328 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
329 if (orig->parts) {
330 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100331 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200332 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
333 }
334 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
335 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
336 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
337
338 return dup;
339cleanup:
340 free(dup);
341 (void) ret; /* set but not used due to the return type */
342 return NULL;
343}
344
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200345/**
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200346 * @brief Print status into a string.
347 *
348 * @param[in] flags Flags with the status to print.
349 * @return String status.
350 */
351static const char *
352lys_status2str(uint16_t flags)
353{
354 flags &= LYS_STATUS_MASK;
355
356 switch (flags) {
357 case 0:
358 case LYS_STATUS_CURR:
359 return "current";
360 case LYS_STATUS_DEPRC:
361 return "deprecated";
362 case LYS_STATUS_OBSLT:
363 return "obsolete";
364 default:
365 LOGINT(NULL);
366 return NULL;
367 }
368}
369
370/**
Michal Vaskodfd254d2021-06-24 09:24:59 +0200371 * @brief Compile status information of the given statement.
372 *
373 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
374 * has the status correctly set during the compilation.
375 *
376 * @param[in] ctx Compile context
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200377 * @param[in] parsed_flags Parsed statement flags.
378 * @param[in] inherited_flags Parsed inherited flags from a schema-only statement (augment, uses, ext instance, ...).
379 * @param[in] parent_flags Compiled parent node flags.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200380 * @param[in] parent_name Name of the parent node, for logging.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200381 * @param[in] stmt_name Statement name, for logging.
382 * @param[in,out] stmt_flags Statement flags with the correct status set.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200383 * @return LY_ERR value.
384 */
385static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200386lys_compile_status(struct lysc_ctx *ctx, uint16_t parsed_flags, uint16_t inherited_flags, uint16_t parent_flags,
387 const char *parent_name, const char *stmt_name, uint16_t *stmt_flags)
Michal Vaskodfd254d2021-06-24 09:24:59 +0200388{
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200389 /* normalize to status-only */
390 parsed_flags &= LYS_STATUS_MASK;
391 inherited_flags &= LYS_STATUS_MASK;
392 parent_flags &= LYS_STATUS_MASK;
393
394 /* check for conflicts */
395 if (parent_flags && parsed_flags && (parent_flags > parsed_flags)) {
396 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Status \"%s\" of \"%s\" is in conflict with \"%s\" status of parent \"%s\".",
397 lys_status2str(parsed_flags), stmt_name, lys_status2str(parent_flags), parent_name);
398 return LY_EVALID;
399 } else if (inherited_flags && parsed_flags && (inherited_flags > parsed_flags)) {
400 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Inherited schema-only status \"%s\" is in conflict with \"%s\" status of \"%s\".",
401 lys_status2str(inherited_flags), lys_status2str(parsed_flags), stmt_name);
402 return LY_EVALID;
403 } else if (parent_flags && inherited_flags && (parent_flags > inherited_flags)) {
404 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Status \"%s\" of parent \"%s\" is in conflict with inherited schema-only status \"%s\".",
405 lys_status2str(parent_flags), parent_name, lys_status2str(inherited_flags));
406 return LY_EVALID;
Michal Vaskodfd254d2021-06-24 09:24:59 +0200407 }
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200408
409 /* clear */
410 (*stmt_flags) &= ~LYS_STATUS_MASK;
411
412 if (parsed_flags) {
413 /* explicit status */
414 (*stmt_flags) |= parsed_flags;
415 } else if (inherited_flags) {
416 /* inherited status from a schema-only statement */
417 (*stmt_flags) |= inherited_flags;
418 } else if (parent_flags) {
419 /* inherited status from a parent node */
420 (*stmt_flags) |= parent_flags;
421 } else {
422 /* default status */
423 (*stmt_flags) |= LYS_STATUS_CURR;
424 }
425
Michal Vaskodfd254d2021-06-24 09:24:59 +0200426 return LY_SUCCESS;
427}
428
429/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200430 * @brief Compile information from the when statement
431 *
432 * @param[in] ctx Compile context.
433 * @param[in] when_p Parsed when structure.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200434 * @param[in] inherited_flags Inherited flags from a schema-only statement.
435 * @param[in] parent Parent node, if any.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200436 * @param[in] ctx_node Context node for the when statement.
437 * @param[out] when Pointer where to store pointer to the created compiled when structure.
438 * @return LY_ERR value.
439 */
440static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200441lys_compile_when_(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags,
442 const struct lysc_node *parent, const struct lysc_node *ctx_node, struct lysc_when **when)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200443{
444 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200445 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200446
447 *when = calloc(1, sizeof **when);
448 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
449 (*when)->refcount = 1;
450 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200451 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200452 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200453 (*when)->context = (struct lysc_node *)ctx_node;
454 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
455 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100456 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200457 LY_CHECK_RET(lys_compile_status(ctx, 0, inherited_flags, parent ? parent->flags : 0, parent ? parent->name : NULL,
458 "when", &(*when)->flags));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200459
460done:
461 return ret;
462}
463
464LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200465lys_compile_when(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags, const struct lysc_node *parent,
466 const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200467{
Michal Vasko193dacd2022-10-13 08:43:05 +0200468 LY_ERR rc = LY_SUCCESS;
469 struct lysc_when **new_when, ***node_when, *ptr;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200470
Michal Vasko193dacd2022-10-13 08:43:05 +0200471 assert(when_p && (node || when_c));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200472
Michal Vasko193dacd2022-10-13 08:43:05 +0200473 if (node) {
474 /* get the when array */
475 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200476
Michal Vasko193dacd2022-10-13 08:43:05 +0200477 /* create new when pointer */
478 LY_ARRAY_NEW_GOTO(ctx->ctx, *node_when, new_when, rc, cleanup);
479 } else {
480 /* individual when */
481 new_when = &ptr;
482 *new_when = calloc(1, sizeof **new_when);
483 LY_CHECK_ERR_GOTO(!*new_when, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
484 }
485
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200486 if (!when_c || !(*when_c)) {
487 /* compile when */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200488 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 +0200489
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200490 /* remember the compiled when for sharing */
491 if (when_c) {
492 *when_c = *new_when;
493 }
494 } else {
495 /* use the previously compiled when */
496 ++(*when_c)->refcount;
497 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100498 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200499
Michal Vasko6f08e962021-07-23 08:30:47 +0200500 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100501 /* do not check "when" semantics in a grouping, but repeat the check for different node because
502 * of dummy node check */
Michal Vasko193dacd2022-10-13 08:43:05 +0200503 LY_CHECK_GOTO(rc = ly_set_add(&ctx->unres->whens, node, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200504 }
505
Michal Vasko193dacd2022-10-13 08:43:05 +0200506cleanup:
507 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200508}
509
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200510LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200511lys_compile_must(struct lysc_ctx *ctx, const struct lysp_restr *must_p, struct lysc_must *must)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200512{
513 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200514 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200515
516 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200517 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 +0200518 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200519 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
520 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
521 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
522 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100523 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200524
525done:
526 return ret;
527}
528
529/**
530 * @brief Validate and normalize numeric value from a range definition.
531 * @param[in] ctx Compile context.
532 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
533 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
534 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
535 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
536 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
537 * @param[in] value String value of the range boundary.
538 * @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.
539 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
540 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
541 */
542static LY_ERR
543range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
544 size_t *len, char **valcopy)
545{
546 size_t fraction = 0, size;
547
548 *len = 0;
549
550 assert(value);
551 /* parse value */
552 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
553 return LY_EVALID;
554 }
555
556 if ((value[*len] == '-') || (value[*len] == '+')) {
557 ++(*len);
558 }
559
560 while (isdigit(value[*len])) {
561 ++(*len);
562 }
563
564 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
565 if (basetype == LY_TYPE_DEC64) {
566 goto decimal;
567 } else {
568 *valcopy = strndup(value, *len);
569 return LY_SUCCESS;
570 }
571 }
572 fraction = *len;
573
574 ++(*len);
575 while (isdigit(value[*len])) {
576 ++(*len);
577 }
578
579 if (basetype == LY_TYPE_DEC64) {
580decimal:
581 assert(frdigits);
582 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100583 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200584 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100585 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200586 return LY_EINVAL;
587 }
588 if (fraction) {
589 size = (*len) + (frdigits - ((*len) - 1 - fraction));
590 } else {
591 size = (*len) + frdigits + 1;
592 }
593 *valcopy = malloc(size * sizeof **valcopy);
594 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
595
596 (*valcopy)[size - 1] = '\0';
597 if (fraction) {
598 memcpy(&(*valcopy)[0], &value[0], fraction);
599 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
600 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
601 } else {
602 memcpy(&(*valcopy)[0], &value[0], *len);
603 memset(&(*valcopy)[*len], '0', frdigits);
604 }
605 }
606 return LY_SUCCESS;
607}
608
609/**
610 * @brief Check that values in range are in ascendant order.
611 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
612 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
613 * max can be also equal.
614 * @param[in] value Current value to check.
615 * @param[in] prev_value The last seen value.
616 * @return LY_SUCCESS or LY_EEXIST for invalid order.
617 */
618static LY_ERR
619range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
620{
621 if (unsigned_value) {
622 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
623 return LY_EEXIST;
624 }
625 } else {
626 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
627 return LY_EEXIST;
628 }
629 }
630 return LY_SUCCESS;
631}
632
633/**
634 * @brief Set min/max value of the range part.
635 * @param[in] ctx Compile context.
636 * @param[in] part Range part structure to fill.
637 * @param[in] max Flag to distinguish if storing min or max value.
638 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
639 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
640 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
641 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
642 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
643 * @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.
644 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
645 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
646 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
647 * frdigits value), LY_EMEM.
648 */
649static LY_ERR
650range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
651 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
652{
653 LY_ERR ret = LY_SUCCESS;
654 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100655 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200656
657 if (value) {
658 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
659 LY_CHECK_GOTO(ret, finalize);
660 }
661 if (!valcopy && base_range) {
662 if (max) {
663 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
664 } else {
665 part->min_64 = base_range->parts[0].min_64;
666 }
667 if (!first) {
668 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
669 }
670 goto finalize;
671 }
672
673 switch (basetype) {
674 case LY_TYPE_INT8: /* range */
675 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100676 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 +0200677 } else if (max) {
678 part->max_64 = INT64_C(127);
679 } else {
680 part->min_64 = INT64_C(-128);
681 }
682 if (!ret && !first) {
683 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
684 }
685 break;
686 case LY_TYPE_INT16: /* range */
687 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100688 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
689 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200690 } else if (max) {
691 part->max_64 = INT64_C(32767);
692 } else {
693 part->min_64 = INT64_C(-32768);
694 }
695 if (!ret && !first) {
696 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
697 }
698 break;
699 case LY_TYPE_INT32: /* range */
700 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100701 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
702 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200703 } else if (max) {
704 part->max_64 = INT64_C(2147483647);
705 } else {
706 part->min_64 = INT64_C(-2147483648);
707 }
708 if (!ret && !first) {
709 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
710 }
711 break;
712 case LY_TYPE_INT64: /* range */
713 case LY_TYPE_DEC64: /* range */
714 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100715 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
716 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200717 } else if (max) {
718 part->max_64 = INT64_C(9223372036854775807);
719 } else {
720 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
721 }
722 if (!ret && !first) {
723 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
724 }
725 break;
726 case LY_TYPE_UINT8: /* range */
727 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100728 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 +0200729 } else if (max) {
730 part->max_u64 = UINT64_C(255);
731 } else {
732 part->min_u64 = UINT64_C(0);
733 }
734 if (!ret && !first) {
735 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
736 }
737 break;
738 case LY_TYPE_UINT16: /* range */
739 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100740 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 +0200741 } else if (max) {
742 part->max_u64 = UINT64_C(65535);
743 } else {
744 part->min_u64 = UINT64_C(0);
745 }
746 if (!ret && !first) {
747 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
748 }
749 break;
750 case LY_TYPE_UINT32: /* range */
751 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100752 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
753 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200754 } else if (max) {
755 part->max_u64 = UINT64_C(4294967295);
756 } else {
757 part->min_u64 = UINT64_C(0);
758 }
759 if (!ret && !first) {
760 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
761 }
762 break;
763 case LY_TYPE_UINT64: /* range */
764 case LY_TYPE_STRING: /* length */
765 case LY_TYPE_BINARY: /* length */
766 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100767 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
768 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200769 } else if (max) {
770 part->max_u64 = UINT64_C(18446744073709551615);
771 } else {
772 part->min_u64 = UINT64_C(0);
773 }
774 if (!ret && !first) {
775 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
776 }
777 break;
778 default:
779 LOGINT(ctx->ctx);
780 ret = LY_EINT;
781 }
782
783finalize:
784 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100785 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200786 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
787 length_restr ? "length" : "range", valcopy ? valcopy : *value);
788 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100789 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200790 "Invalid %s restriction - invalid value \"%s\".",
791 length_restr ? "length" : "range", valcopy ? valcopy : *value);
792 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100793 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200794 "Invalid %s restriction - values are not in ascending order (%s).",
795 length_restr ? "length" : "range",
796 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
797 } else if (!ret && value) {
798 *value = *value + len;
799 }
800 free(valcopy);
801 return ret;
802}
803
Michal Vasko193dacd2022-10-13 08:43:05 +0200804LY_ERR
805lys_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 +0200806 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
807{
aPiecek1c4da362021-04-29 14:26:34 +0200808 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200809 const char *expr;
810 struct lysc_range_part *parts = NULL, *part;
811 ly_bool range_expected = 0, uns;
812 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
813
814 assert(range);
815 assert(range_p);
816
817 expr = range_p->arg.str;
818 while (1) {
819 if (isspace(*expr)) {
820 ++expr;
821 } else if (*expr == '\0') {
822 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100823 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200824 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200825 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200826 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200827 goto cleanup;
828 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100829 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200830 "Invalid %s restriction - unexpected end of the expression (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200831 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200832 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200833 goto cleanup;
834 }
835 parts_done++;
836 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100837 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200838 if (parts) {
839 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100840 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200841 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100842 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200843 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200844 goto cleanup;
845 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100846 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200847
848 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200849 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 +0200850 part->max_64 = part->min_64;
851 } else if (*expr == '|') {
852 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100853 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200854 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200855 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200856 goto cleanup;
857 }
858 expr++;
859 parts_done++;
860 /* process next part of the expression */
861 } else if (!strncmp(expr, "..", 2)) {
862 expr += 2;
863 while (isspace(*expr)) {
864 expr++;
865 }
866 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100867 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200868 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200869 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200870 goto cleanup;
871 }
872 /* continue expecting the upper boundary */
873 range_expected = 1;
874 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
875 /* number */
876 if (range_expected) {
877 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200878 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 +0200879 range_expected = 0;
880 } else {
881 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200882 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 +0200883 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
884 part->max_64 = part->min_64;
885 }
886
887 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100888 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
889 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200890 while (isspace(*expr)) {
891 expr++;
892 }
893 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100894 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200895 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200896 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200897 goto cleanup;
898 }
899 if (range_expected) {
900 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200901 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 +0200902 range_expected = 0;
903 } else {
904 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200905 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 +0200906 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
907 part->min_64 = part->max_64;
908 }
909 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100910 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200911 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200912 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200913 goto cleanup;
914 }
915 }
916
917 /* check with the previous range/length restriction */
918 if (base_range) {
919 switch (basetype) {
920 case LY_TYPE_BINARY:
921 case LY_TYPE_UINT8:
922 case LY_TYPE_UINT16:
923 case LY_TYPE_UINT32:
924 case LY_TYPE_UINT64:
925 case LY_TYPE_STRING:
926 uns = 1;
927 break;
928 case LY_TYPE_DEC64:
929 case LY_TYPE_INT8:
930 case LY_TYPE_INT16:
931 case LY_TYPE_INT32:
932 case LY_TYPE_INT64:
933 uns = 0;
934 break;
935 default:
936 LOGINT(ctx->ctx);
937 ret = LY_EINT;
938 goto cleanup;
939 }
940 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
941 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
942 goto baseerror;
943 }
944 /* current lower bound is not lower than the base */
945 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
946 /* base has single value */
947 if (base_range->parts[v].min_64 == parts[u].min_64) {
948 /* both lower bounds are the same */
949 if (parts[u].min_64 != parts[u].max_64) {
950 /* current continues with a range */
951 goto baseerror;
952 } else {
953 /* equal single values, move both forward */
954 ++v;
955 continue;
956 }
957 } else {
958 /* base is single value lower than current range, so the
959 * value from base range is removed in the current,
960 * move only base and repeat checking */
961 ++v;
962 --u;
963 continue;
964 }
965 } else {
966 /* base is the range */
967 if (parts[u].min_64 == parts[u].max_64) {
968 /* current is a single value */
969 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
970 /* current is behind the base range, so base range is omitted,
971 * move the base and keep the current for further check */
972 ++v;
973 --u;
974 } /* else it is within the base range, so move the current, but keep the base */
975 continue;
976 } else {
977 /* both are ranges - check the higher bound, the lower was already checked */
978 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
979 /* higher bound is higher than the current higher bound */
980 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
981 /* but the current lower bound is also higher, so the base range is omitted,
982 * continue with the same current, but move the base */
983 --u;
984 ++v;
985 continue;
986 }
987 /* current range starts within the base range but end behind it */
988 goto baseerror;
989 } else {
990 /* current range is smaller than the base,
991 * move current, but stay with the base */
992 continue;
993 }
994 }
995 }
996 }
997 if (u != parts_done) {
998baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100999 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001000 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
Michal Vasko20c0b9f2021-08-24 08:16:27 +02001001 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +02001002 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001003 goto cleanup;
1004 }
1005 }
1006
1007 if (!(*range)) {
1008 *range = calloc(1, sizeof **range);
1009 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1010 }
1011
1012 /* we rewrite the following values as the types chain is being processed */
1013 if (range_p->eapptag) {
1014 lydict_remove(ctx->ctx, (*range)->eapptag);
1015 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
1016 }
1017 if (range_p->emsg) {
1018 lydict_remove(ctx->ctx, (*range)->emsg);
1019 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
1020 }
1021 if (range_p->dsc) {
1022 lydict_remove(ctx->ctx, (*range)->dsc);
1023 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
1024 }
1025 if (range_p->ref) {
1026 lydict_remove(ctx->ctx, (*range)->ref);
1027 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
1028 }
1029 /* extensions are taken only from the last range by the caller */
1030
1031 (*range)->parts = parts;
1032 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001033cleanup:
1034 LY_ARRAY_FREE(parts);
1035
1036 return ret;
1037}
1038
Michal Vasko215d7fc2022-07-15 14:50:44 +02001039/**
1040 * @brief Transform characters block in an XML Schema pattern into Perl character ranges.
1041 *
1042 * @param[in] ctx libyang context.
1043 * @param[in] pattern Original pattern.
1044 * @param[in,out] regex Pattern to modify.
1045 * @return LY_ERR value.
1046 */
1047static LY_ERR
1048lys_compile_pattern_chblocks_xmlschema2perl(const struct ly_ctx *ctx, const char *pattern, char **regex)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001049{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001050#define URANGE_LEN 19
1051 char *ublock2urange[][2] = {
1052 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1053 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1054 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1055 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1056 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1057 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1058 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1059 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1060 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1061 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1062 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1063 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1064 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1065 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1066 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1067 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1068 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1069 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1070 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1071 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1072 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1073 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1074 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1075 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1076 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1077 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1078 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1079 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1080 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1081 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1082 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1083 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1084 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1085 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1086 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1087 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1088 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1089 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1090 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1091 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1092 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1093 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1094 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1095 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1096 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1097 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1098 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1099 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1100 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1101 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1102 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1103 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1104 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1105 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1106 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1107 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1108 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1109 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1110 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1111 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1112 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1113 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1114 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1115 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1116 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1117 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1118 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1119 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1120 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1121 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1122 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1123 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1124 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1125 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1126 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1127 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1128 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1129 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1130 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1131 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1132 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1133 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1134 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +01001135 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001136 {NULL, NULL}
1137 };
1138
Michal Vasko215d7fc2022-07-15 14:50:44 +02001139 size_t idx, idx2, start, end;
1140 char *perl_regex, *ptr;
1141
1142 perl_regex = *regex;
1143
1144 /* substitute Unicode Character Blocks with exact Character Ranges */
1145 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1146 start = ptr - perl_regex;
1147
1148 ptr = strchr(ptr, '}');
1149 if (!ptr) {
1150 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
1151 return LY_EVALID;
1152 }
1153 end = (ptr - perl_regex) + 1;
1154
1155 /* need more space */
1156 if (end - start < URANGE_LEN) {
1157 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko78419922022-07-15 15:15:20 +02001158 *regex = perl_regex;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001159 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1160 }
1161
1162 /* find our range */
1163 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1164 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1165 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1166 break;
1167 }
1168 }
1169 if (!ublock2urange[idx][0]) {
1170 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
1171 return LY_EVALID;
1172 }
1173
1174 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1175 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1176 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1177 ++idx;
1178 }
1179 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1180 --idx;
1181 }
1182 }
1183 if (idx) {
1184 /* skip brackets */
1185 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1186 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1187 } else {
1188 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1189 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1190 }
1191 }
1192
Michal Vasko215d7fc2022-07-15 14:50:44 +02001193 return LY_SUCCESS;
1194}
1195
1196LY_ERR
1197lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
1198{
1199 size_t idx, size, brack;
1200 char *perl_regex;
1201 int err_code, compile_opts;
1202 const char *orig_ptr;
1203 PCRE2_SIZE err_offset;
1204 pcre2_code *code_local;
aPiecek9e716992022-07-21 16:29:47 +02001205 ly_bool escaped;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001206 LY_ERR r;
1207
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001208 /* adjust the expression to a Perl equivalent
1209 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1210
1211 /* allocate space for the transformed pattern */
1212 size = strlen(pattern) + 1;
Michal Vasko03d430c2022-07-22 09:05:56 +02001213 compile_opts = PCRE2_UTF | PCRE2_UCP | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
Christian Hoppsdafed222021-03-22 13:02:42 -04001214#ifdef PCRE2_ENDANCHORED
1215 compile_opts |= PCRE2_ENDANCHORED;
1216#else
1217 /* add space for trailing $ anchor */
1218 size++;
1219#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 perl_regex = malloc(size);
1221 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1222 perl_regex[0] = '\0';
1223
1224 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1225 brack = 0;
1226 idx = 0;
aPiecek9e716992022-07-21 16:29:47 +02001227 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001228 orig_ptr = pattern;
1229 while (orig_ptr[0]) {
1230 switch (orig_ptr[0]) {
1231 case '$':
1232 case '^':
1233 if (!brack) {
1234 /* make space for the extra character */
1235 ++size;
1236 perl_regex = ly_realloc(perl_regex, size);
1237 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1238
1239 /* print escape slash */
1240 perl_regex[idx] = '\\';
1241 ++idx;
1242 }
1243 break;
aPiecek9e716992022-07-21 16:29:47 +02001244 case '\\':
1245 /* escape character found or backslash is escaped */
1246 escaped = !escaped;
1247 /* copy backslash and continue with the next character */
1248 perl_regex[idx] = orig_ptr[0];
1249 ++idx;
1250 ++orig_ptr;
1251 continue;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001252 case '[':
aPiecek9e716992022-07-21 16:29:47 +02001253 if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 ++brack;
1255 }
1256 break;
1257 case ']':
aPiecek9e716992022-07-21 16:29:47 +02001258 if (!brack && !escaped) {
1259 /* If ']' does not terminate a character class expression, then pcre2_compile() implicitly escapes the
1260 * ']' character. But this seems to be against the regular expressions rules declared in
1261 * "XML schema: Datatypes" and therefore an error is returned. So for example if pattern is '\[a]' then
1262 * pcre2 match characters '[a]' literally but in YANG such pattern is not allowed.
1263 */
1264 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, orig_ptr, "character group doesn't begin with '['");
1265 free(perl_regex);
1266 return LY_EVALID;
1267 } else if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001268 --brack;
1269 }
1270 break;
1271 default:
1272 break;
1273 }
1274
1275 /* copy char */
1276 perl_regex[idx] = orig_ptr[0];
1277
1278 ++idx;
1279 ++orig_ptr;
aPiecek9e716992022-07-21 16:29:47 +02001280 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001281 }
Christian Hoppsdafed222021-03-22 13:02:42 -04001282#ifndef PCRE2_ENDANCHORED
1283 /* anchor match to end of subject */
1284 perl_regex[idx++] = '$';
1285#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001286 perl_regex[idx] = '\0';
1287
Michal Vasko215d7fc2022-07-15 14:50:44 +02001288 /* transform character blocks */
1289 if ((r = lys_compile_pattern_chblocks_xmlschema2perl(ctx, pattern, &perl_regex))) {
1290 free(perl_regex);
1291 return r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001292 }
1293
1294 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001295 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001296 &err_code, &err_offset, NULL);
1297 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001298 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1299 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001300 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001301 free(perl_regex);
1302 return LY_EVALID;
1303 }
1304 free(perl_regex);
1305
1306 if (code) {
1307 *code = code_local;
1308 } else {
1309 free(code_local);
1310 }
1311
1312 return LY_SUCCESS;
1313
1314#undef URANGE_LEN
1315}
1316
Michal Vasko51de7b72022-04-29 09:50:22 +02001317LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001318lys_compile_type_patterns(struct lysc_ctx *ctx, const struct lysp_restr *patterns_p, struct lysc_pattern **base_patterns,
1319 struct lysc_pattern ***patterns)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001320{
1321 struct lysc_pattern **pattern;
1322 LY_ARRAY_COUNT_TYPE u;
1323 LY_ERR ret = LY_SUCCESS;
1324
1325 /* first, copy the patterns from the base type */
1326 if (base_patterns) {
1327 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1328 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1329 }
1330
1331 LY_ARRAY_FOR(patterns_p, u) {
1332 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1333 *pattern = calloc(1, sizeof **pattern);
1334 ++(*pattern)->refcount;
1335
Radek Krejci2efc45b2020-12-22 16:25:44 +01001336 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001337 LY_CHECK_RET(ret);
1338
Radek Krejcif13b87b2020-12-01 22:02:17 +01001339 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001340 (*pattern)->inverted = 1;
1341 }
1342 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1343 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1344 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1345 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1346 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001347 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001348 }
1349done:
1350 return ret;
1351}
1352
1353/**
1354 * @brief map of the possible restrictions combination for the specific built-in type.
1355 */
1356static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1357 0 /* LY_TYPE_UNKNOWN */,
1358 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1359 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1360 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1361 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1362 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1363 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1364 LYS_SET_BIT /* LY_TYPE_BITS */,
1365 0 /* LY_TYPE_BOOL */,
1366 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1367 0 /* LY_TYPE_EMPTY */,
1368 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1369 LYS_SET_BASE /* LY_TYPE_IDENT */,
1370 LYS_SET_REQINST /* LY_TYPE_INST */,
1371 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1372 LYS_SET_TYPE /* LY_TYPE_UNION */,
1373 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1374 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1375 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1376 LYS_SET_RANGE /* LY_TYPE_INT64 */
1377};
1378
1379/**
1380 * @brief stringification of the YANG built-in data types
1381 */
1382const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001383 LY_TYPE_UNKNOWN_STR,
1384 LY_TYPE_BINARY_STR,
1385 LY_TYPE_UINT8_STR,
1386 LY_TYPE_UINT16_STR,
1387 LY_TYPE_UINT32_STR,
1388 LY_TYPE_UINT64_STR,
1389 LY_TYPE_STRING_STR,
1390 LY_TYPE_BITS_STR,
1391 LY_TYPE_BOOL_STR,
1392 LY_TYPE_DEC64_STR,
1393 LY_TYPE_EMPTY_STR,
1394 LY_TYPE_ENUM_STR,
1395 LY_TYPE_IDENT_STR,
1396 LY_TYPE_INST_STR,
1397 LY_TYPE_LEAFREF_STR,
1398 LY_TYPE_UNION_STR,
1399 LY_TYPE_INT8_STR,
1400 LY_TYPE_INT16_STR,
1401 LY_TYPE_INT32_STR,
1402 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001403};
1404
Michal Vasko193dacd2022-10-13 08:43:05 +02001405LY_ERR
1406lys_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 +01001407 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001408{
1409 LY_ERR ret = LY_SUCCESS;
1410 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001411 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1412 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001413 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001414 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001415
1416 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001417 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001418 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1419 return LY_EVALID;
1420 }
1421
1422 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001423 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001424 if (base_enums) {
1425 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1426 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001427 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001428 break;
1429 }
1430 }
1431 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001432 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001433 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001434 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001435 return LY_EVALID;
1436 }
1437 match = v;
1438 }
1439
1440 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001441 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001442 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001443 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001444 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001445 LY_ARRAY_FOR(*bitenums, v) {
1446 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001447 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001448 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001449 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001450 return LY_EVALID;
1451 }
1452 }
1453 } else if (base_enums) {
1454 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001455 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001456 } else {
1457 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001458 if (u == 0) {
1459 cur_val = 0;
1460 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001461 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001462 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001463 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001464 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001465 } else {
1466 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001467 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001468 }
1469
1470 /* save highest value for auto assing */
1471 if (highest_value < cur_val) {
1472 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001473 }
1474 } else { /* LY_TYPE_BITS */
1475 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001476 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001477 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001478 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001479 LY_ARRAY_FOR(*bitenums, v) {
1480 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001481 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001482 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001483 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001484 return LY_EVALID;
1485 }
1486 }
1487 } else if (base_enums) {
1488 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001489 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001490 } else {
1491 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001492 if (u == 0) {
1493 cur_pos = 0;
1494 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001495 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001496 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001497 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001498 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001499 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001500 } else {
1501 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001502 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001503 }
1504
1505 /* save highest position for auto assing */
1506 if (highest_position < cur_pos) {
1507 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001508 }
1509 }
1510
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001511 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001512 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001513 if (basetype == LY_TYPE_ENUM) {
1514 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001515 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001516 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001517 enums_p[u].name, base_enums[match].value, cur_val);
1518 return LY_EVALID;
1519 }
1520 } else {
1521 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001522 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001523 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001524 enums_p[u].name, base_enums[match].position, cur_pos);
1525 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001526 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001527 }
1528 }
1529
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001530 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001531 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001532 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1533 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1534 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001535 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 +01001536 if (basetype == LY_TYPE_ENUM) {
1537 e->value = cur_val;
1538 } else {
1539 e->position = cur_pos;
1540 }
Radek Krejciab430862021-03-02 20:13:40 +01001541 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001542
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001543 /* evaluate if-ffeatures */
1544 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1545 if (!enabled) {
1546 /* set only flag, later resolved and removed */
1547 e->flags |= LYS_DISABLED;
1548 }
1549
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001550 if (basetype == LY_TYPE_BITS) {
1551 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001552 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001553 if (v != u) {
1554 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001555 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1556 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001557 }
1558 }
1559 }
1560
1561done:
1562 return ret;
1563}
1564
Radek Krejci6a205692020-12-09 13:58:22 +01001565static LY_ERR
1566lys_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 +01001567 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001568{
1569 LY_ERR ret = LY_SUCCESS;
1570 struct lysc_type **utypes = *utypes_p;
1571 struct lysc_type_union *un_aux = NULL;
1572
1573 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1574 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001575 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1576 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001577 LY_CHECK_GOTO(ret, error);
1578 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1579 /* add space for additional types from the union subtype */
1580 un_aux = (struct lysc_type_union *)utypes[u + additional];
1581 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1582 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1583
1584 /* copy subtypes of the subtype union */
1585 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1586 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1587 struct lysc_type_leafref *lref;
1588
1589 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1590 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1591 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1592 lref = (struct lysc_type_leafref *)utypes[u + additional];
1593
1594 lref->basetype = LY_TYPE_LEAFREF;
Michal Vaskoe33134a2022-07-29 14:54:40 +02001595 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 +01001596 LY_CHECK_GOTO(ret, error);
Michal Vasko080064b2021-08-31 15:20:44 +02001597 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001598 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001599 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001600 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001601 LY_CHECK_GOTO(ret, error);
1602 /* TODO extensions */
1603
1604 } else {
1605 utypes[u + additional] = un_aux->types[v];
Michal Vasko04338d92021-09-01 07:58:14 +02001606 LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount);
Radek Krejci6a205692020-12-09 13:58:22 +01001607 }
1608 ++additional;
1609 LY_ARRAY_INCREMENT(utypes);
1610 }
1611 /* compensate u increment in main loop */
1612 --additional;
1613
1614 /* free the replaced union subtype */
Michal Vaskoc636ea42022-09-16 10:20:31 +02001615 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001616 un_aux = NULL;
1617 } else {
1618 LY_ARRAY_INCREMENT(utypes);
1619 }
1620 }
1621
1622 *utypes_p = utypes;
1623 return LY_SUCCESS;
1624
1625error:
1626 if (un_aux) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001627 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001628 }
1629 *utypes_p = utypes;
1630 return ret;
1631}
1632
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001633/**
1634 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1635 * @param[in] ctx Compile context.
1636 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1637 * @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 +02001638 * @param[in] context_name Name of the context node or referencing typedef for logging.
1639 * @param[in] type_p Parsed type to compile.
1640 * @param[in] basetype Base YANG built-in type of the type to compile.
1641 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1642 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1643 * @param[out] type Newly created type structure with the filled information about the type.
1644 * @return LY_ERR value.
1645 */
1646static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001647lys_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 +02001648 struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, const struct lysc_type *base,
1649 struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001650{
1651 LY_ERR ret = LY_SUCCESS;
1652 struct lysc_type_bin *bin;
1653 struct lysc_type_num *num;
1654 struct lysc_type_str *str;
1655 struct lysc_type_bits *bits;
1656 struct lysc_type_enum *enumeration;
1657 struct lysc_type_dec *dec;
1658 struct lysc_type_identityref *idref;
1659 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001660 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001661
1662 switch (basetype) {
1663 case LY_TYPE_BINARY:
1664 bin = (struct lysc_type_bin *)(*type);
1665
1666 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1667 if (type_p->length) {
1668 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1669 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1670 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001671 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001672 }
1673 }
1674 break;
1675 case LY_TYPE_BITS:
1676 /* RFC 7950 9.7 - bits */
1677 bits = (struct lysc_type_bits *)(*type);
1678 if (type_p->bits) {
1679 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1680 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1681 (struct lysc_type_bitenum_item **)&bits->bits));
1682 }
1683
1684 if (!base && !type_p->flags) {
1685 /* type derived from bits built-in type must contain at least one bit */
1686 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001687 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001688 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001689 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001690 }
1691 return LY_EVALID;
1692 }
1693 break;
1694 case LY_TYPE_DEC64:
1695 dec = (struct lysc_type_dec *)(*type);
1696
1697 /* RFC 7950 9.3.4 - fraction-digits */
1698 if (!base) {
1699 if (!type_p->fraction_digits) {
1700 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001701 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001702 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001703 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001704 }
1705 return LY_EVALID;
1706 }
1707 dec->fraction_digits = type_p->fraction_digits;
1708 } else {
1709 if (type_p->fraction_digits) {
1710 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1711 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001712 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001713 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1714 tpdfname);
1715 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001716 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001717 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1718 }
1719 return LY_EVALID;
1720 }
1721 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1722 }
1723
1724 /* RFC 7950 9.2.4 - range */
1725 if (type_p->range) {
1726 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1727 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1728 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001729 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001730 }
1731 }
1732 break;
1733 case LY_TYPE_STRING:
1734 str = (struct lysc_type_str *)(*type);
1735
1736 /* RFC 7950 9.4.4 - length */
1737 if (type_p->length) {
1738 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1739 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1740 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001741 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001742 }
1743 } else if (base && ((struct lysc_type_str *)base)->length) {
1744 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1745 }
1746
1747 /* RFC 7950 9.4.5 - pattern */
1748 if (type_p->patterns) {
1749 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1750 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1751 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1752 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1753 }
1754 break;
1755 case LY_TYPE_ENUM:
1756 enumeration = (struct lysc_type_enum *)(*type);
1757
1758 /* RFC 7950 9.6 - enum */
1759 if (type_p->enums) {
1760 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1761 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1762 }
1763
1764 if (!base && !type_p->flags) {
1765 /* type derived from enumerations built-in type must contain at least one enum */
1766 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001767 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001768 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001769 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001770 }
1771 return LY_EVALID;
1772 }
1773 break;
1774 case LY_TYPE_INT8:
1775 case LY_TYPE_UINT8:
1776 case LY_TYPE_INT16:
1777 case LY_TYPE_UINT16:
1778 case LY_TYPE_INT32:
1779 case LY_TYPE_UINT32:
1780 case LY_TYPE_INT64:
1781 case LY_TYPE_UINT64:
1782 num = (struct lysc_type_num *)(*type);
1783
1784 /* RFC 6020 9.2.4 - range */
1785 if (type_p->range) {
1786 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1787 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1788 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001789 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001790 }
1791 }
1792 break;
1793 case LY_TYPE_IDENT:
1794 idref = (struct lysc_type_identityref *)(*type);
1795
1796 /* RFC 7950 9.10.2 - base */
1797 if (type_p->bases) {
1798 if (base) {
1799 /* only the directly derived identityrefs can contain base specification */
1800 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001801 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001802 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1803 tpdfname);
1804 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001805 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001806 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1807 }
1808 return LY_EVALID;
1809 }
aPiecekf4a0a192021-08-03 15:14:17 +02001810 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001811 }
1812
1813 if (!base && !type_p->flags) {
1814 /* type derived from identityref built-in type must contain at least one base */
1815 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001816 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001817 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001818 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001819 }
1820 return LY_EVALID;
1821 }
1822 break;
1823 case LY_TYPE_LEAFREF:
1824 lref = (struct lysc_type_leafref *)*type;
1825
1826 /* RFC 7950 9.9.3 - require-instance */
1827 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001828 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001829 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001830 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001831 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1832 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001833 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001834 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1835 }
1836 return LY_EVALID;
1837 }
1838 lref->require_instance = type_p->require_instance;
1839 } else if (base) {
1840 /* inherit */
1841 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1842 } else {
1843 /* default is true */
1844 lref->require_instance = 1;
1845 }
1846 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001847 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001848
Michal Vaskoe33134a2022-07-29 14:54:40 +02001849 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, 0, 0, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001850 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr),
Radek Krejci8df109d2021-04-23 12:19:08 +02001851 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001852 } else if (base) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02001853 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, 0, 0, &lref->path));
Radek Krejci8df109d2021-04-23 12:19:08 +02001854 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001855 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001856 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001857 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001858 return LY_EVALID;
1859 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001860 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001861 return LY_EVALID;
1862 }
1863 break;
1864 case LY_TYPE_INST:
1865 /* RFC 7950 9.9.3 - require-instance */
1866 if (type_p->flags & LYS_SET_REQINST) {
1867 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1868 } else {
1869 /* default is true */
1870 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1871 }
1872 break;
1873 case LY_TYPE_UNION:
1874 un = (struct lysc_type_union *)(*type);
1875
1876 /* RFC 7950 7.4 - type */
1877 if (type_p->types) {
1878 if (base) {
1879 /* only the directly derived union can contain types specification */
1880 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001881 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001882 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1883 tpdfname);
1884 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001885 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001886 "Invalid type substatement for the type not directly derived from union built-in type.");
1887 }
1888 return LY_EVALID;
1889 }
1890 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001891 LY_CHECK_RET(lys_compile_type_union(ctx, type_p->types, context_pnode, context_flags, context_name, &un->types));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001892 }
1893
1894 if (!base && !type_p->flags) {
1895 /* type derived from union built-in type must contain at least one type */
1896 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001897 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001898 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001899 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001900 }
1901 return LY_EVALID;
1902 }
1903 break;
1904 case LY_TYPE_BOOL:
1905 case LY_TYPE_EMPTY:
1906 case LY_TYPE_UNKNOWN: /* just to complete switch */
1907 break;
1908 }
1909
1910 if (tpdfname) {
1911 switch (basetype) {
1912 case LY_TYPE_BINARY:
1913 type_p->compiled = *type;
1914 *type = calloc(1, sizeof(struct lysc_type_bin));
1915 break;
1916 case LY_TYPE_BITS:
1917 type_p->compiled = *type;
1918 *type = calloc(1, sizeof(struct lysc_type_bits));
1919 break;
1920 case LY_TYPE_DEC64:
1921 type_p->compiled = *type;
1922 *type = calloc(1, sizeof(struct lysc_type_dec));
1923 break;
1924 case LY_TYPE_STRING:
1925 type_p->compiled = *type;
1926 *type = calloc(1, sizeof(struct lysc_type_str));
1927 break;
1928 case LY_TYPE_ENUM:
1929 type_p->compiled = *type;
1930 *type = calloc(1, sizeof(struct lysc_type_enum));
1931 break;
1932 case LY_TYPE_INT8:
1933 case LY_TYPE_UINT8:
1934 case LY_TYPE_INT16:
1935 case LY_TYPE_UINT16:
1936 case LY_TYPE_INT32:
1937 case LY_TYPE_UINT32:
1938 case LY_TYPE_INT64:
1939 case LY_TYPE_UINT64:
1940 type_p->compiled = *type;
1941 *type = calloc(1, sizeof(struct lysc_type_num));
1942 break;
1943 case LY_TYPE_IDENT:
1944 type_p->compiled = *type;
1945 *type = calloc(1, sizeof(struct lysc_type_identityref));
1946 break;
1947 case LY_TYPE_LEAFREF:
1948 type_p->compiled = *type;
1949 *type = calloc(1, sizeof(struct lysc_type_leafref));
1950 break;
1951 case LY_TYPE_INST:
1952 type_p->compiled = *type;
1953 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1954 break;
1955 case LY_TYPE_UNION:
1956 type_p->compiled = *type;
1957 *type = calloc(1, sizeof(struct lysc_type_union));
1958 break;
1959 case LY_TYPE_BOOL:
1960 case LY_TYPE_EMPTY:
1961 case LY_TYPE_UNKNOWN: /* just to complete switch */
1962 break;
1963 }
1964 }
1965 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1966
1967cleanup:
1968 return ret;
1969}
1970
1971LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001972lys_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 +02001973 const struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001974{
1975 LY_ERR ret = LY_SUCCESS;
1976 ly_bool dummyloops = 0;
1977 struct type_context {
1978 const struct lysp_tpdf *tpdf;
1979 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001980 } *tctx, *tctx_prev = NULL, *tctx_iter;
1981 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1982 struct lysc_type *base = NULL, *prev_type;
1983 struct ly_set tpdf_chain = {0};
Michal Vasko388a6632021-08-06 11:27:43 +02001984 struct lyplg_type *plugin;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001985
1986 (*type) = NULL;
1987 if (dflt) {
1988 *dflt = NULL;
1989 }
1990
1991 tctx = calloc(1, sizeof *tctx);
1992 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001993 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 +02001994 ret == LY_SUCCESS;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001995 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 +01001996 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001997 if (basetype) {
1998 break;
1999 }
2000
2001 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01002002 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
2003 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002004 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2005
2006 if (units && !*units) {
2007 /* inherit units */
2008 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2009 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2010 }
2011 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
2012 /* inherit default */
2013 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
2014 }
2015 if (dummyloops && (!units || *units) && dflt && *dflt) {
2016 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2017 break;
2018 }
2019
Michal Vasko080064b2021-08-31 15:20:44 +02002020 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
Radek Krejci720d2612021-03-03 19:44:22 +01002021 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
2022 * and we need now recompile the type again in the updated context. */
Michal Vaskoc636ea42022-09-16 10:20:31 +02002023 lysc_type_free(&ctx->free_ctx, tctx->tpdf->type.compiled);
Radek Krejci720d2612021-03-03 19:44:22 +01002024 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
2025 }
2026
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002027 if (tctx->tpdf->type.compiled) {
2028 /* it is not necessary to continue, the rest of the chain was already compiled,
2029 * but we still may need to inherit default and units values, so start dummy loops */
2030 basetype = tctx->tpdf->type.compiled->basetype;
2031 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2032 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2033
2034 if ((units && !*units) || (dflt && !*dflt)) {
2035 dummyloops = 1;
2036 goto preparenext;
2037 } else {
2038 tctx = NULL;
2039 break;
2040 }
2041 }
2042
2043 /* circular typedef reference detection */
2044 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
2045 /* local part */
2046 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002047 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002048 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002049 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2050 free(tctx);
2051 ret = LY_EVALID;
2052 goto cleanup;
2053 }
2054 }
2055 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
2056 /* global part for unions corner case */
2057 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002058 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002059 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002060 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2061 free(tctx);
2062 ret = LY_EVALID;
2063 goto cleanup;
2064 }
2065 }
2066
2067 /* store information for the following processing */
2068 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2069 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2070
2071preparenext:
2072 /* prepare next loop */
2073 tctx_prev = tctx;
2074 tctx = calloc(1, sizeof *tctx);
2075 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2076 }
2077 free(tctx);
2078
2079 /* allocate type according to the basetype */
2080 switch (basetype) {
2081 case LY_TYPE_BINARY:
2082 *type = calloc(1, sizeof(struct lysc_type_bin));
2083 break;
2084 case LY_TYPE_BITS:
2085 *type = calloc(1, sizeof(struct lysc_type_bits));
2086 break;
2087 case LY_TYPE_BOOL:
2088 case LY_TYPE_EMPTY:
2089 *type = calloc(1, sizeof(struct lysc_type));
2090 break;
2091 case LY_TYPE_DEC64:
2092 *type = calloc(1, sizeof(struct lysc_type_dec));
2093 break;
2094 case LY_TYPE_ENUM:
2095 *type = calloc(1, sizeof(struct lysc_type_enum));
2096 break;
2097 case LY_TYPE_IDENT:
2098 *type = calloc(1, sizeof(struct lysc_type_identityref));
2099 break;
2100 case LY_TYPE_INST:
2101 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2102 break;
2103 case LY_TYPE_LEAFREF:
2104 *type = calloc(1, sizeof(struct lysc_type_leafref));
2105 break;
2106 case LY_TYPE_STRING:
2107 *type = calloc(1, sizeof(struct lysc_type_str));
2108 break;
2109 case LY_TYPE_UNION:
2110 *type = calloc(1, sizeof(struct lysc_type_union));
2111 break;
2112 case LY_TYPE_INT8:
2113 case LY_TYPE_UINT8:
2114 case LY_TYPE_INT16:
2115 case LY_TYPE_UINT16:
2116 case LY_TYPE_INT32:
2117 case LY_TYPE_UINT32:
2118 case LY_TYPE_INT64:
2119 case LY_TYPE_UINT64:
2120 *type = calloc(1, sizeof(struct lysc_type_num));
2121 break;
2122 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002123 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002124 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2125 ret = LY_EVALID;
2126 goto cleanup;
2127 }
2128 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
2129 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002130 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002131 ly_data_type2str[basetype]);
2132 free(*type);
2133 (*type) = NULL;
2134 ret = LY_EVALID;
2135 goto cleanup;
2136 }
2137
2138 /* get restrictions from the referred typedefs */
2139 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2140 tctx = (struct type_context *)tpdf_chain.objs[u];
2141
2142 /* remember the typedef context for circular check */
2143 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
2144 LY_CHECK_GOTO(ret, cleanup);
2145
2146 if (tctx->tpdf->type.compiled) {
Michal Vasko388a6632021-08-06 11:27:43 +02002147 /* already compiled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002148 base = tctx->tpdf->type.compiled;
2149 continue;
Michal Vasko388a6632021-08-06 11:27:43 +02002150 }
2151
Michal Vaskoddd76592022-01-17 13:34:48 +01002152 /* try to find loaded user type plugins */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002153 plugin = lyplg_type_plugin_find(tctx->tpdf->type.pmod->mod->name, tctx->tpdf->type.pmod->mod->revision,
Michal Vaskoddd76592022-01-17 13:34:48 +01002154 tctx->tpdf->name);
Haithem Ben Ghorbal45be15f2022-07-12 18:02:36 +02002155 if (!plugin && base) {
2156 /* use the base type implementation if available */
2157 plugin = base->plugin;
2158 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002159 if (!plugin) {
2160 /* use the internal built-in type implementation */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002161 plugin = lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vaskoddd76592022-01-17 13:34:48 +01002162 }
Michal Vasko388a6632021-08-06 11:27:43 +02002163 assert(plugin);
2164
2165 if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags) &&
2166 (plugin == base->plugin)) {
2167 /* no change, reuse the compiled base */
2168 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002169 LY_ATOMIC_INC_BARRIER(base->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002170 continue;
2171 }
2172
Michal Vasko04338d92021-09-01 07:58:14 +02002173 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002174 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002175 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002176 tctx->tpdf->name, ly_data_type2str[basetype]);
2177 ret = LY_EVALID;
2178 goto cleanup;
2179 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002180 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002181 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2182 tctx->tpdf->name, tctx->tpdf->dflt.str);
2183 ret = LY_EVALID;
2184 goto cleanup;
2185 }
2186
2187 (*type)->basetype = basetype;
Michal Vasko388a6632021-08-06 11:27:43 +02002188 (*type)->plugin = plugin;
2189
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002190 /* collect extensions */
2191 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko388a6632021-08-06 11:27:43 +02002192
2193 /* compile the new typedef */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002194 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01002195 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002196 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
2197 LY_CHECK_GOTO(ret, cleanup);
2198 base = prev_type;
2199 }
2200 /* remove the processed typedef contexts from the stack for circular check */
2201 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
2202
2203 /* process the type definition in leaf */
2204 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
2205 /* get restrictions from the node itself */
2206 (*type)->basetype = basetype;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002207 (*type)->plugin = base ? base->plugin : lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vasko04338d92021-09-01 07:58:14 +02002208 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko193dacd2022-10-13 08:43:05 +02002209 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, (struct lysp_type *)type_p, basetype,
2210 NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002211 LY_CHECK_GOTO(ret, cleanup);
2212 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
2213 /* no specific restriction in leaf's type definition, copy from the base */
2214 free(*type);
2215 (*type) = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002216 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002217 }
2218
Radek Krejciab430862021-03-02 20:13:40 +01002219 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002220
2221cleanup:
2222 ly_set_erase(&tpdf_chain, free);
2223 return ret;
2224}
2225
2226/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002227 * @brief Check uniqness of the node/action/notification name.
2228 *
2229 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2230 * structures, but they share the namespace so we need to check their name collisions.
2231 *
2232 * @param[in] ctx Compile context.
2233 * @param[in] parent Parent of the nodes to check, can be NULL.
2234 * @param[in] name Name of the item to find in the given lists.
2235 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2236 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2237 */
2238static LY_ERR
2239lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2240 const struct lysc_node *exclude)
2241{
2242 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002243 const struct lysc_node_action *actions;
2244 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002245 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002246 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002247
2248#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2249
2250 if (exclude->nodetype == LYS_CASE) {
2251 /* check restricted only to all the cases */
2252 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002253 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002254 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002255 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002256 return LY_EEXIST;
2257 }
2258 }
2259
2260 return LY_SUCCESS;
2261 }
2262
2263 /* no reason for our parent to be choice anymore */
2264 assert(!parent || (parent->nodetype != LYS_CHOICE));
2265
2266 if (parent && (parent->nodetype == LYS_CASE)) {
2267 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002268
2269 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2270 iter = lysc_data_parent(parent);
2271 do {
2272 parent = parent->parent;
2273 if (parent && (parent->nodetype == LYS_CHOICE)) {
2274 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2275 }
2276 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002277 }
2278
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002279 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002280 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2281 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2282 if (exclude->flags & LYS_IS_OUTPUT) {
2283 getnext_flags |= LYS_GETNEXT_OUTPUT;
2284 parent = lysc_node_child(parent)->next;
2285 } else {
2286 parent = lysc_node_child(parent);
2287 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002288 }
2289
2290 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002291 if (!parent && ctx->ext) {
2292 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2293 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2294 goto error;
2295 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002296
Radek Krejci5fa32a32021-02-08 18:12:38 +01002297 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2298 if (iter->nodetype == LYS_CHOICE) {
2299 iter2 = NULL;
2300 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2301 if (CHECK_NODE(iter2, exclude, name)) {
2302 goto error;
2303 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002304 }
2305 }
2306 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002307 } else {
2308 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2309 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2310 goto error;
2311 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002312
Radek Krejci5fa32a32021-02-08 18:12:38 +01002313 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2314 if (iter->nodetype == LYS_CHOICE) {
2315 iter2 = NULL;
2316 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2317 if (CHECK_NODE(iter2, exclude, name)) {
2318 goto error;
2319 }
2320 }
2321 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002322 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002323
Radek Krejci5fa32a32021-02-08 18:12:38 +01002324 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2325 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2326 if (CHECK_NODE(iter, exclude, name)) {
2327 goto error;
2328 }
2329 }
2330
2331 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2332 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2333 if (CHECK_NODE(iter, exclude, name)) {
2334 goto error;
2335 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002336 }
2337 }
Radek Krejci078e4342021-02-12 18:17:26 +01002338 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002339 return LY_SUCCESS;
2340
2341error:
Radek Krejci078e4342021-02-12 18:17:26 +01002342 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002343 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002344 return LY_EEXIST;
2345
2346#undef CHECK_NODE
2347}
2348
Michal Vasko193dacd2022-10-13 08:43:05 +02002349LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002350lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002351{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002352 struct lysc_node **children, *anchor = NULL;
2353 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002354
Radek Krejci2a9fc652021-01-22 17:44:34 +01002355 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002356
Radek Krejci2a9fc652021-01-22 17:44:34 +01002357 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002358 if (node->nodetype == LYS_INPUT) {
2359 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2360 /* input node is part of the action but link it with output */
2361 node->next = &((struct lysc_node_action *)parent)->output.node;
2362 node->prev = node->next;
2363 return LY_SUCCESS;
2364 } else if (node->nodetype == LYS_OUTPUT) {
2365 /* output node is part of the action but link it with input */
2366 node->next = NULL;
2367 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002368 return LY_SUCCESS;
2369 } else if (node->nodetype == LYS_ACTION) {
2370 children = (struct lysc_node **)lysc_node_actions_p(parent);
2371 } else if (node->nodetype == LYS_NOTIF) {
2372 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2373 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002374 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002375 }
2376 assert(children);
2377
2378 if (!(*children)) {
2379 /* first child */
2380 *children = node;
2381 } else if (node->flags & LYS_KEY) {
2382 /* special handling of adding keys */
2383 assert(node->module == parent->module);
2384 anchor = *children;
2385 if (anchor->flags & LYS_KEY) {
2386 while ((anchor->flags & LYS_KEY) && anchor->next) {
2387 anchor = anchor->next;
2388 }
2389 /* insert after the last key */
2390 insert_after = 1;
2391 } /* else insert before anchor (at the beginning) */
2392 } else if ((*children)->prev->module == node->module) {
2393 /* last child is from the same module, keep the order and insert at the end */
2394 anchor = (*children)->prev;
2395 insert_after = 1;
2396 } else if (parent->module == node->module) {
2397 /* adding module child after some augments were connected */
2398 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2399 } else {
2400 /* some augments are already connected and we are connecting new ones,
2401 * keep module name order and insert the node into the children list */
2402 anchor = *children;
2403 do {
2404 anchor = anchor->prev;
2405
2406 /* check that we have not found the last augment node from our module or
2407 * the first augment node from a "smaller" module or
2408 * the first node from a local module */
2409 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2410 (anchor->module == parent->module)) {
2411 /* insert after */
2412 insert_after = 1;
2413 break;
2414 }
2415
2416 /* we have traversed all the nodes, insert before anchor (as the first node) */
2417 } while (anchor->prev->next);
2418 }
2419
2420 /* insert */
2421 if (anchor) {
2422 if (insert_after) {
2423 node->next = anchor->next;
2424 node->prev = anchor;
2425 anchor->next = node;
2426 if (node->next) {
2427 /* middle node */
2428 node->next->prev = node;
2429 } else {
2430 /* last node */
2431 (*children)->prev = node;
2432 }
2433 } else {
2434 node->next = anchor;
2435 node->prev = anchor->prev;
2436 anchor->prev = node;
2437 if (anchor == *children) {
2438 /* first node */
2439 *children = node;
2440 } else {
2441 /* middle node */
2442 node->prev->next = node;
2443 }
2444 }
2445 }
2446
2447 /* check the name uniqueness (even for an only child, it may be in case) */
2448 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2449 return LY_EEXIST;
2450 }
2451 } else {
2452 /* top-level element */
2453 struct lysc_node **list;
2454
Radek Krejci6b88a462021-02-17 12:39:34 +01002455 if (ctx->ext) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002456 lyplg_ext_get_storage_p(ctx->ext, LY_STMT_DATA_NODE_MASK, (const void ***)&list);
Radek Krejci6b88a462021-02-17 12:39:34 +01002457 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002458 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2459 } else if (node->nodetype == LYS_NOTIF) {
2460 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2461 } else {
2462 list = &ctx->cur_mod->compiled->data;
2463 }
2464 if (!(*list)) {
2465 *list = node;
2466 } else {
2467 /* insert at the end of the module's top-level nodes list */
2468 (*list)->prev->next = node;
2469 node->prev = (*list)->prev;
2470 (*list)->prev = node;
2471 }
2472
2473 /* check the name uniqueness on top-level */
2474 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2475 return LY_EEXIST;
2476 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002477 }
2478
Radek Krejci2a9fc652021-01-22 17:44:34 +01002479 return LY_SUCCESS;
2480}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002481
Radek Krejci2a9fc652021-01-22 17:44:34 +01002482/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002483 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002484 *
2485 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002486 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002487 * @return LY_ERR value.
2488 */
2489static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002490lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002491{
2492 /* case never has any explicit config */
2493 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002494
Michal Vasko7c565922021-06-10 14:58:27 +02002495 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002496 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002497 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002498 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2499 /* config not explicitly set, inherit it from parent */
Michal Vaskoecdc2222022-01-24 08:45:44 +01002500 assert(!node->parent || (node->parent->flags & LYS_CONFIG_MASK) || (node->parent->nodetype & LYS_AUGMENT));
2501 if (node->parent && (node->parent->flags & LYS_CONFIG_MASK)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002502 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002503 } else {
2504 /* default is config true */
2505 node->flags |= LYS_CONFIG_W;
2506 }
2507 } else {
2508 /* config set explicitly */
2509 node->flags |= LYS_SET_CONFIG;
2510 }
2511
Radek Krejci91531e12021-02-08 19:57:54 +01002512 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002513 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002514 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002515 }
2516
Radek Krejci2a9fc652021-01-22 17:44:34 +01002517 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002518}
2519
Radek Krejci91531e12021-02-08 19:57:54 +01002520/**
2521 * @brief Set various flags of the compiled nodes
2522 *
2523 * @param[in] ctx Compile context.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002524 * @param[in] parsed_flags Parsed node flags.
2525 * @param[in] inherited_flags Inherited flags from a schema-only statement.
2526 * @param[in,out] node Compiled node where the flags will be set.
Radek Krejci91531e12021-02-08 19:57:54 +01002527 */
2528static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002529lys_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 +01002530{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002531 uint16_t parent_flags;
2532 const char *parent_name;
2533
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002534 /* copy flags except for status */
2535 node->flags = (parsed_flags & LYS_FLAGS_COMPILED_MASK) & ~LYS_STATUS_MASK;
2536
Radek Krejci91531e12021-02-08 19:57:54 +01002537 /* inherit config flags */
2538 LY_CHECK_RET(lys_compile_config(ctx, node));
2539
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002540 /* compile status */
2541 parent_flags = node->parent ? node->parent->flags : 0;
2542 parent_name = node->parent ? node->parent->name : NULL;
2543 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 +01002544
2545 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002546 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002547 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002548 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002549 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002550 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002551 node->flags |= LYS_IS_NOTIF;
2552 }
2553
2554 return LY_SUCCESS;
2555}
2556
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002557static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002558lys_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 +01002559 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2560 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002561{
2562 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002563 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002564 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002565 struct lysp_when *pwhen = NULL;
Michal Vaskoe02e7402021-07-23 08:29:28 +02002566 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002567
Radek Krejci2a9fc652021-01-22 17:44:34 +01002568 node->nodetype = pnode->nodetype;
2569 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002570 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002571 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002572 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002573
Radek Krejci2a9fc652021-01-22 17:44:34 +01002574 /* compile any deviations for this node */
2575 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vaskoe02e7402021-07-23 08:29:28 +02002576 if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2577 /* if not supported, keep it just like disabled nodes by if-feature */
2578 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
2579 ctx->compile_opts |= LYS_COMPILE_DISABLED;
2580 }
2581 if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002582 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002583 }
2584
Michal Vaskodfd254d2021-06-24 09:24:59 +02002585 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2586 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2587 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002588
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002589 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002590 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002591 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002592 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002593 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002594 }
2595
Radek Krejci91531e12021-02-08 19:57:54 +01002596 /* config, status and other flags */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002597 LY_CHECK_GOTO(ret = lys_compile_node_flags(ctx, pnode->flags, inherited_flags, node), error);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002598
2599 /* list ordering */
2600 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002601 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002602 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002603 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002604 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002605 node->flags &= ~LYS_ORDBY_MASK;
2606 node->flags |= LYS_ORDBY_SYSTEM;
2607 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2608 /* default ordering is system */
2609 node->flags |= LYS_ORDBY_SYSTEM;
2610 }
2611 }
2612
Radek Krejci2a9fc652021-01-22 17:44:34 +01002613 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2614 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2615
Radek Krejci9a3823e2021-01-27 20:26:46 +01002616 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002617 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002618 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002619 LY_CHECK_GOTO(ret, cleanup);
2620 }
2621
Radek Krejci2a9fc652021-01-22 17:44:34 +01002622 /* nodetype-specific part */
2623 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2624
2625 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002626 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002627 if (node->flags & LYS_MAND_TRUE) {
2628 /* inherit LYS_MAND_TRUE in parent containers */
2629 lys_compile_mandatory_parents(parent, 1);
2630 }
2631
2632 if (child_set) {
2633 /* add the new node into set */
2634 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2635 }
2636
2637 goto cleanup;
2638
2639error:
Michal Vaskoc636ea42022-09-16 10:20:31 +02002640 lysc_node_free(&ctx->free_ctx, node, 0);
2641
Radek Krejci2a9fc652021-01-22 17:44:34 +01002642cleanup:
2643 if (ret && dev_pnode) {
2644 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2645 }
Michal Vaskoe02e7402021-07-23 08:29:28 +02002646 ctx->compile_opts = prev_opts;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002647 lysp_dev_node_free(ctx, dev_pnode);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002648 return ret;
2649}
2650
Radek Krejci2a9fc652021-01-22 17:44:34 +01002651LY_ERR
2652lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2653{
2654 LY_ERR ret = LY_SUCCESS;
2655 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002656 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002657
2658 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2659 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2660
2661 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002662 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002663 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002664
Radek Krejci01180ac2021-01-27 08:48:22 +01002665 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002666 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2667 }
2668
Michal Vasko95f736c2022-06-08 12:03:31 +02002669 /* connect any augments */
2670 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2671
Michal Vasko7c565922021-06-10 14:58:27 +02002672 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002673
2674done:
2675 return ret;
2676}
2677
2678/**
2679 * @brief Compile parsed action node information.
Michal Vasko193dacd2022-10-13 08:43:05 +02002680 *
Radek Krejci2a9fc652021-01-22 17:44:34 +01002681 * @param[in] ctx Compile context
2682 * @param[in] pnode Parsed action node.
2683 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2684 * is enriched with the action-specific information.
2685 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2686 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002687static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002688lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2689{
2690 LY_ERR ret;
2691 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2692 struct lysc_node_action *action = (struct lysc_node_action *)node;
2693 struct lysp_node_action_inout *input, implicit_input = {
2694 .nodetype = LYS_INPUT,
2695 .name = "input",
2696 .parent = pnode,
2697 };
2698 struct lysp_node_action_inout *output, implicit_output = {
2699 .nodetype = LYS_OUTPUT,
2700 .name = "output",
2701 .parent = pnode,
2702 };
2703
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002704 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002705 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002706 if (action_p->input.nodetype == LYS_UNKNOWN) {
2707 input = &implicit_input;
2708 } else {
2709 input = &action_p->input;
2710 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002711 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 +01002712 lysc_update_path(ctx, NULL, NULL);
2713 LY_CHECK_GOTO(ret, done);
2714
Michal Vaskoc130e162021-10-19 11:30:00 +02002715 /* add must(s) to unres */
2716 ret = lysc_unres_must_add(ctx, &action->input.node, &input->node);
2717 LY_CHECK_GOTO(ret, done);
2718
Radek Krejci2a9fc652021-01-22 17:44:34 +01002719 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002720 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002721 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002722 output = &implicit_output;
2723 } else {
2724 output = &action_p->output;
2725 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002726 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 +01002727 lysc_update_path(ctx, NULL, NULL);
2728 LY_CHECK_GOTO(ret, done);
2729
Michal Vaskoc130e162021-10-19 11:30:00 +02002730 /* add must(s) to unres */
2731 ret = lysc_unres_must_add(ctx, &action->output.node, &output->node);
2732 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002733
2734done:
2735 return ret;
2736}
2737
2738/**
2739 * @brief Compile parsed action node information.
2740 * @param[in] ctx Compile context
2741 * @param[in] pnode Parsed action node.
2742 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2743 * is enriched with the action-specific information.
2744 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2745 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002746static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002747lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2748{
2749 LY_ERR ret = LY_SUCCESS;
2750 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2751 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2752 struct lysp_node *child_p;
2753
2754 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002755
2756 /* add must(s) to unres */
2757 ret = lysc_unres_must_add(ctx, node, pnode);
2758 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002759
Radek Krejci01180ac2021-01-27 08:48:22 +01002760 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002761 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002762 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002763 }
2764
Michal Vasko95f736c2022-06-08 12:03:31 +02002765 /* connect any augments */
2766 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2767
Radek Krejci2a9fc652021-01-22 17:44:34 +01002768done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002769 return ret;
2770}
2771
2772/**
2773 * @brief Compile parsed container node information.
2774 * @param[in] ctx Compile context
2775 * @param[in] pnode Parsed container node.
2776 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2777 * is enriched with the container-specific information.
2778 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2779 */
2780static LY_ERR
2781lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2782{
2783 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2784 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2785 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002786 LY_ERR ret = LY_SUCCESS;
2787
2788 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002789 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002790 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002791 }
2792
2793 /* more cases when the container has meaning but is kept NP for convenience:
2794 * - when condition
2795 * - direct child action/notification
2796 */
2797
2798 LY_LIST_FOR(cont_p->child, child_p) {
2799 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2800 LY_CHECK_GOTO(ret, done);
2801 }
2802
Michal Vasko5347e3a2020-11-03 17:14:57 +01002803 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002804
2805 /* add must(s) to unres */
2806 ret = lysc_unres_must_add(ctx, node, pnode);
2807 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002808
Michal Vasko95f736c2022-06-08 12:03:31 +02002809 /* connect any augments */
2810 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2811
Radek Krejci2a9fc652021-01-22 17:44:34 +01002812 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2813 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2814 LY_CHECK_GOTO(ret, done);
2815 }
2816 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2817 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2818 LY_CHECK_GOTO(ret, done);
2819 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002820
2821done:
2822 return ret;
2823}
2824
Michal Vasko72244882021-01-12 15:21:05 +01002825/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002826 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2827 * @param[in] ctx Compile context.
2828 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2829 * @param[in] type_p Parsed type to compile.
2830 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2831 * @return LY_ERR value.
2832 */
2833static LY_ERR
2834lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2835 struct lysc_node_leaf *leaf)
2836{
2837 struct lysp_qname *dflt;
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002838 struct lysc_type **t;
2839 LY_ARRAY_COUNT_TYPE u, count;
2840 ly_bool in_unres = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002841
Michal Vaskoa99b3572021-02-01 11:54:58 +01002842 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002843 leaf->units ? NULL : &leaf->units, &dflt));
2844
2845 /* store default value, if any */
2846 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2847 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2848 }
2849
Michal Vaskoc130e162021-10-19 11:30:00 +02002850 /* store leafref(s) to be resolved */
2851 LY_CHECK_RET(lysc_unres_leafref_add(ctx, leaf, type_p->pmod));
2852
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002853 /* type-specific checks */
2854 if (leaf->type->basetype == LY_TYPE_UNION) {
2855 t = ((struct lysc_type_union *)leaf->type)->types;
2856 count = LY_ARRAY_COUNT(t);
2857 } else {
2858 t = &leaf->type;
2859 count = 1;
2860 }
2861 for (u = 0; u < count; ++u) {
2862 if (t[u]->basetype == LY_TYPE_EMPTY) {
2863 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2864 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2865 return LY_EVALID;
2866 }
2867 } else if (!in_unres && ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM))) {
2868 /* store in unres for all disabled bits/enums to be removed */
2869 LY_CHECK_RET(lysc_unres_bitenum_add(ctx, leaf));
2870 in_unres = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002871 }
2872 }
2873
2874 return LY_SUCCESS;
2875}
2876
2877/**
2878 * @brief Compile parsed leaf node information.
2879 * @param[in] ctx Compile context
2880 * @param[in] pnode Parsed leaf node.
2881 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2882 * is enriched with the leaf-specific information.
2883 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2884 */
2885static LY_ERR
2886lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2887{
2888 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2889 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002890 LY_ERR ret = LY_SUCCESS;
2891
Michal Vasko5347e3a2020-11-03 17:14:57 +01002892 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002893
2894 /* add must(s) to unres */
2895 ret = lysc_unres_must_add(ctx, node, pnode);
2896 LY_CHECK_GOTO(ret, done);
2897
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002898 if (leaf_p->units) {
2899 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2900 leaf->flags |= LYS_SET_UNITS;
2901 }
2902
2903 /* compile type */
2904 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2905 LY_CHECK_GOTO(ret, done);
2906
2907 /* store/update default value */
2908 if (leaf_p->dflt.str) {
2909 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2910 leaf->flags |= LYS_SET_DFLT;
2911 }
2912
2913 /* checks */
2914 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002915 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002916 return LY_EVALID;
2917 }
2918
2919done:
2920 return ret;
2921}
2922
2923/**
2924 * @brief Compile parsed leaf-list node information.
2925 * @param[in] ctx Compile context
2926 * @param[in] pnode Parsed leaf-list node.
2927 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2928 * is enriched with the leaf-list-specific information.
2929 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2930 */
2931static LY_ERR
2932lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2933{
2934 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2935 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002936 LY_ERR ret = LY_SUCCESS;
2937
Michal Vasko5347e3a2020-11-03 17:14:57 +01002938 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002939
2940 /* add must(s) to unres */
2941 ret = lysc_unres_must_add(ctx, node, pnode);
2942 LY_CHECK_GOTO(ret, done);
2943
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002944 if (llist_p->units) {
2945 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2946 llist->flags |= LYS_SET_UNITS;
2947 }
2948
2949 /* compile type */
2950 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2951 LY_CHECK_GOTO(ret, done);
2952
2953 /* store/update default values */
2954 if (llist_p->dflts) {
2955 if (ctx->pmod->version < LYS_VERSION_1_1) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002956 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002957 return LY_EVALID;
2958 }
2959
2960 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2961 llist->flags |= LYS_SET_DFLT;
2962 }
2963
2964 llist->min = llist_p->min;
2965 if (llist->min) {
2966 llist->flags |= LYS_MAND_TRUE;
2967 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002968 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2969
2970 if (llist->flags & LYS_CONFIG_R) {
2971 /* state leaf-list is always ordered-by user */
2972 llist->flags &= ~LYS_ORDBY_SYSTEM;
2973 llist->flags |= LYS_ORDBY_USER;
2974 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002975
2976 /* checks */
2977 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002978 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 +02002979 return LY_EVALID;
2980 }
2981
2982 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002983 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002984 llist->min, llist->max);
2985 return LY_EVALID;
2986 }
2987
2988done:
2989 return ret;
2990}
2991
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002992/**
2993 * @brief Find the node according to the given descendant/absolute schema nodeid.
2994 * Used in unique, refine and augment statements.
2995 *
2996 * @param[in] ctx Compile context
2997 * @param[in] nodeid Descendant-schema-nodeid (according to the YANG grammar)
2998 * @param[in] nodeid_len Length of the given nodeid, if it is not NULL-terminated string.
2999 * @param[in] ctx_node Context node for a relative nodeid.
3000 * @param[in] format Format of any prefixes.
3001 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
3002 * @param[in] nodetype Optional (can be 0) restriction for target's nodetype. If target exists, but does not match
3003 * the given nodetype, LY_EDENIED is returned (and target is provided), but no error message is printed.
3004 * The value can be even an ORed value to allow multiple nodetypes.
3005 * @param[out] target Found target node if any.
3006 * @param[out] result_flag Output parameter to announce if the schema nodeid goes through the action's input/output or a Notification.
3007 * The LYSC_OPT_RPC_INPUT, LYSC_OPT_RPC_OUTPUT and LYSC_OPT_NOTIFICATION are used as flags.
3008 * @return LY_ERR values - LY_ENOTFOUND, LY_EVALID, LY_EDENIED or LY_SUCCESS.
3009 */
3010static LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003011lysc_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 +01003012 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 +02003013{
3014 LY_ERR ret = LY_EVALID;
3015 const char *name, *prefix, *id;
3016 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01003017 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003018 const char *nodeid_type;
3019 uint32_t getnext_extra_flag = 0;
3020 uint16_t current_nodetype = 0;
3021
3022 assert(nodeid);
3023 assert(target);
3024 assert(result_flag);
3025 *target = NULL;
3026 *result_flag = 0;
3027
3028 id = nodeid;
3029
3030 if (ctx_node) {
3031 /* descendant-schema-nodeid */
3032 nodeid_type = "descendant";
3033
3034 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003035 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003036 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01003037 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003038 return LY_EVALID;
3039 }
3040 } else {
3041 /* absolute-schema-nodeid */
3042 nodeid_type = "absolute";
3043
3044 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003045 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003046 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003047 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003048 return LY_EVALID;
3049 }
3050 ++id;
3051 }
3052
3053 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
3054 if (prefix) {
3055 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
3056 if (!mod) {
3057 /* module must always be found */
3058 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003059 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003060 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003061 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003062 return LY_ENOTFOUND;
3063 }
3064 } else {
3065 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003066 case LY_VALUE_SCHEMA:
3067 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003068 /* use the current module */
Michal Vasko56d8da82021-12-16 15:41:30 +01003069 mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003070 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003071 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02003072 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003073 if (!ctx_node) {
3074 LOGINT_RET(ctx->ctx);
3075 }
3076 /* inherit the module of the previous context node */
3077 mod = ctx_node->module;
3078 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02003079 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02003080 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01003081 case LY_VALUE_STR_NS:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003082 /* not really defined */
3083 LOGINT_RET(ctx->ctx);
3084 }
3085 }
3086
3087 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
3088 /* move through input/output manually */
3089 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01003090 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3091 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003092 return LY_ENOTFOUND;
3093 }
3094 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003095 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003096 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003097 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003098 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
3099 } else {
3100 /* only input or output is valid */
3101 ctx_node = NULL;
3102 }
Michal Vasko0b50f6b2022-10-05 15:07:55 +02003103 } else if (ctx->ext && !ctx_node) {
3104 /* top-level extension nodes */
3105 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 +02003106 } else {
3107 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003108 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003109 getnext_extra_flag = 0;
3110 }
3111 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01003112 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3113 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003114 return LY_ENOTFOUND;
3115 }
3116 current_nodetype = ctx_node->nodetype;
3117
3118 if (current_nodetype == LYS_NOTIF) {
3119 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
3120 } else if (current_nodetype == LYS_INPUT) {
3121 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
3122 } else if (current_nodetype == LYS_OUTPUT) {
3123 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
3124 }
3125
3126 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
3127 break;
3128 }
3129 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003130 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003131 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01003132 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003133 return LY_EVALID;
3134 }
3135 ++id;
3136 }
3137
3138 if (ret == LY_SUCCESS) {
3139 *target = ctx_node;
3140 if (nodetype && !(current_nodetype & nodetype)) {
3141 return LY_EDENIED;
3142 }
3143 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003144 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003145 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01003146 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003147 }
3148
3149 return ret;
3150}
3151
3152/**
3153 * @brief Compile information about list's uniques.
3154 * @param[in] ctx Compile context.
3155 * @param[in] uniques Sized array list of unique statements.
3156 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3157 * @return LY_ERR value.
3158 */
3159static LY_ERR
3160lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
3161{
3162 LY_ERR ret = LY_SUCCESS;
3163 struct lysc_node_leaf **key, ***unique;
3164 struct lysc_node *parent;
3165 const char *keystr, *delim;
3166 size_t len;
3167 LY_ARRAY_COUNT_TYPE v;
3168 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
3169 uint16_t flags;
3170
3171 LY_ARRAY_FOR(uniques, v) {
3172 config = -1;
3173 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3174 keystr = uniques[v].str;
3175 while (keystr) {
3176 delim = strpbrk(keystr, " \t\n");
3177 if (delim) {
3178 len = delim - keystr;
3179 while (isspace(*delim)) {
3180 ++delim;
3181 }
3182 } else {
3183 len = strlen(keystr);
3184 }
3185
3186 /* unique node must be present */
3187 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko56d8da82021-12-16 15:41:30 +01003188 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, LY_VALUE_SCHEMA, (void *)uniques[v].mod,
3189 LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003190 if (ret != LY_SUCCESS) {
3191 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003192 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003193 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01003194 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003195 }
3196 return LY_EVALID;
3197 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003198 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003199 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003200 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003201 return LY_EVALID;
3202 }
3203
3204 /* all referenced leafs must be of the same config type */
3205 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3206 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003207 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003208 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3209 return LY_EVALID;
3210 } else if ((*key)->flags & LYS_CONFIG_W) {
3211 config = 1;
3212 } else { /* LYS_CONFIG_R */
3213 config = 0;
3214 }
3215
3216 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3217 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3218 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003219 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003220 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3221 return LY_EVALID;
3222 }
3223 }
3224
3225 /* check status */
Michal Vaskoc130e162021-10-19 11:30:00 +02003226 LY_CHECK_RET(lysc_check_status(ctx, list->flags, uniques[v].mod->mod, list->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003227 (*key)->flags, (*key)->module, (*key)->name));
3228
3229 /* mark leaf as unique */
3230 (*key)->flags |= LYS_UNIQUE;
3231
3232 /* next unique value in line */
3233 keystr = delim;
3234 }
3235 /* next unique definition */
3236 }
3237
3238 return LY_SUCCESS;
3239}
3240
3241/**
3242 * @brief Compile parsed list node information.
3243 * @param[in] ctx Compile context
3244 * @param[in] pnode Parsed list node.
3245 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3246 * is enriched with the list-specific information.
3247 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3248 */
3249static LY_ERR
3250lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3251{
3252 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3253 struct lysc_node_list *list = (struct lysc_node_list *)node;
3254 struct lysp_node *child_p;
Michal Vasko2d6507a2022-03-16 09:40:52 +01003255 struct lysc_node *parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003256 struct lysc_node_leaf *key, *prev_key = NULL;
3257 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003258 const char *keystr, *delim;
3259 LY_ERR ret = LY_SUCCESS;
3260
3261 list->min = list_p->min;
3262 if (list->min) {
3263 list->flags |= LYS_MAND_TRUE;
3264 }
3265 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3266
3267 LY_LIST_FOR(list_p->child, child_p) {
3268 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3269 }
3270
Michal Vasko5347e3a2020-11-03 17:14:57 +01003271 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003272
3273 /* add must(s) to unres */
3274 ret = lysc_unres_must_add(ctx, node, pnode);
3275 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003276
3277 /* keys */
Michal Vasko2d6507a2022-03-16 09:40:52 +01003278 if (list->flags & LYS_CONFIG_W) {
3279 parent = node;
3280 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
3281 /* compiling individual grouping, we can check this only if there is an explicit config set */
3282 while (parent) {
3283 if (parent->flags & LYS_SET_CONFIG) {
3284 break;
3285 }
3286 parent = parent->parent;
3287 }
3288 }
3289
3290 if (parent && (!list_p->key || !list_p->key[0])) {
3291 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3292 return LY_EVALID;
3293 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003294 }
3295
3296 /* find all the keys (must be direct children) */
3297 keystr = list_p->key;
3298 if (!keystr) {
3299 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003300 list->flags &= ~LYS_ORDBY_SYSTEM;
3301 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003302 }
3303 while (keystr) {
3304 delim = strpbrk(keystr, " \t\n");
3305 if (delim) {
3306 len = delim - keystr;
3307 while (isspace(*delim)) {
3308 ++delim;
3309 }
3310 } else {
3311 len = strlen(keystr);
3312 }
3313
3314 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003315 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3316 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003317 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003318 return LY_EVALID;
3319 }
3320 /* keys must be unique */
3321 if (key->flags & LYS_KEY) {
3322 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003323 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003324 return LY_EVALID;
3325 }
3326
Radek Krejcia6016992021-03-03 10:13:41 +01003327 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003328 /* key must have the same config flag as the list itself */
3329 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003330 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of a configuration list must not be a state leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003331 return LY_EVALID;
3332 }
3333 if (ctx->pmod->version < LYS_VERSION_1_1) {
3334 /* YANG 1.0 denies key to be of empty type */
3335 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003336 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003337 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3338 return LY_EVALID;
3339 }
3340 } else {
3341 /* when and if-feature are illegal on list keys */
3342 if (key->when) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003343 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List's key must not have any \"when\" statement.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003344 return LY_EVALID;
3345 }
Michal Vasko93b4ab92022-05-13 12:29:59 +02003346 /* unable to check if-features but compilation would fail if disabled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003347 }
3348
3349 /* check status */
Michal Vaskoc636ea42022-09-16 10:20:31 +02003350 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 +02003351
3352 /* ignore default values of the key */
3353 if (key->dflt) {
3354 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003355 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)key->dflt->realtype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003356 free(key->dflt);
3357 key->dflt = NULL;
3358 }
3359 /* mark leaf as key */
3360 key->flags |= LYS_KEY;
3361
3362 /* move it to the correct position */
3363 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3364 /* fix links in closest previous siblings of the key */
3365 if (key->next) {
3366 key->next->prev = key->prev;
3367 } else {
3368 /* last child */
3369 list->child->prev = key->prev;
3370 }
3371 if (key->prev->next) {
3372 key->prev->next = key->next;
3373 }
3374 /* fix links in the key */
3375 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003376 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003377 key->next = prev_key->next;
3378 } else {
3379 key->prev = list->child->prev;
3380 key->next = list->child;
3381 }
3382 /* fix links in closes future siblings of the key */
3383 if (prev_key) {
3384 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003385 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003386 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003387 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003388 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003389 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003390 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003391 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003392 }
3393 /* fix links in parent */
3394 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003395 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003396 }
3397 }
3398
3399 /* next key value */
3400 prev_key = key;
3401 keystr = delim;
3402 lysc_update_path(ctx, NULL, NULL);
3403 }
3404
Michal Vasko95f736c2022-06-08 12:03:31 +02003405 /* connect any augments */
3406 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3407
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003408 /* uniques */
3409 if (list_p->uniques) {
3410 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3411 }
3412
Radek Krejci2a9fc652021-01-22 17:44:34 +01003413 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3414 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3415 LY_CHECK_GOTO(ret, done);
3416 }
3417 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3418 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3419 LY_CHECK_GOTO(ret, done);
3420 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003421
3422 /* checks */
3423 if (list->min > list->max) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003424 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 +02003425 return LY_EVALID;
3426 }
3427
3428done:
3429 return ret;
3430}
3431
3432/**
3433 * @brief Do some checks and set the default choice's case.
3434 *
3435 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3436 *
3437 * @param[in] ctx Compile context.
3438 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3439 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3440 * @return LY_ERR value.
3441 */
3442static LY_ERR
3443lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3444{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003445 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003446 const struct lys_module *mod;
3447 const char *prefix = NULL, *name;
3448 size_t prefix_len = 0;
3449
3450 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3451 name = strchr(dflt->str, ':');
3452 if (name) {
3453 prefix = dflt->str;
3454 prefix_len = name - prefix;
3455 ++name;
3456 } else {
3457 name = dflt->str;
3458 }
3459 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003460 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003461 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003462 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003463 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003464 return LY_EVALID;
3465 }
3466 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003467 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003468 }
3469
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003470 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 +02003471 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003472 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003473 "Default case \"%s\" not found.", dflt->str);
3474 return LY_EVALID;
3475 }
3476
3477 /* no mandatory nodes directly under the default case */
3478 LY_LIST_FOR(ch->dflt->child, iter) {
3479 if (iter->parent != (struct lysc_node *)ch->dflt) {
3480 break;
3481 }
3482 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003483 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003484 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3485 return LY_EVALID;
3486 }
3487 }
3488
3489 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003490 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003491 return LY_EVALID;
3492 }
3493
3494 ch->dflt->flags |= LYS_SET_DFLT;
3495 return LY_SUCCESS;
3496}
3497
3498LY_ERR
3499lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3500 struct ly_set *child_set)
3501{
3502 LY_ERR ret = LY_SUCCESS;
3503 struct lysp_node *child_p_next = child_p->next;
3504 struct lysp_node_case *cs_p;
Michal Vasko4eb49d52022-02-17 15:05:00 +01003505 struct lysc_node_case *cs_c;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003506
3507 if (child_p->nodetype == LYS_CASE) {
3508 /* standard case under choice */
3509 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3510 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003511 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003512 cs_p = calloc(1, sizeof *cs_p);
3513 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003514 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003515 cs_p->child = child_p;
3516
3517 /* make the child the only case child */
3518 child_p->next = NULL;
3519
3520 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003521 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 +02003522
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003523 if (((struct lysc_node_choice *)node)->cases) {
Michal Vasko4eb49d52022-02-17 15:05:00 +01003524 /* find our case node */
3525 cs_c = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases;
3526 while (cs_c->name != cs_p->name) {
3527 cs_c = (struct lysc_node_case *)cs_c->next;
3528 assert(cs_c);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003529 }
aPiecekaa320c92021-05-21 07:34:24 +02003530
Michal Vasko4eb49d52022-02-17 15:05:00 +01003531 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3532 /* compiled case node cannot point to his corresponding parsed node
3533 * because it exists temporarily so it must be set to NULL
3534 */
3535 assert(cs_c->priv == cs_p);
3536 cs_c->priv = NULL;
3537 }
3538
3539 /* status is copied from his child and not from his parent as usual. */
3540 if (cs_c->child) {
3541 cs_c->flags &= ~LYS_STATUS_MASK;
3542 cs_c->flags |= (LYS_STATUS_MASK & cs_c->child->flags);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003543 }
3544 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003545
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003546revert_sh_case:
3547 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003548 cs_p->child = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003549 lysp_node_free(&ctx->free_ctx, (struct lysp_node *)cs_p);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003550 child_p->next = child_p_next;
3551 }
3552
3553 return ret;
3554}
3555
3556/**
3557 * @brief Compile parsed choice node information.
3558 *
3559 * @param[in] ctx Compile context
3560 * @param[in] pnode Parsed choice node.
3561 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3562 * is enriched with the choice-specific information.
3563 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3564 */
3565static LY_ERR
3566lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3567{
3568 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3569 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3570 struct lysp_node *child_p;
3571 LY_ERR ret = LY_SUCCESS;
3572
3573 assert(node->nodetype == LYS_CHOICE);
3574
3575 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003576 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, child_p, node, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003577 }
3578
Michal Vasko95f736c2022-06-08 12:03:31 +02003579 /* connect any augments */
3580 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3581
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003582 /* default branch */
3583 if (ch_p->dflt.str) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003584 LY_CHECK_GOTO(ret = lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003585 }
3586
Michal Vasko95f736c2022-06-08 12:03:31 +02003587done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003588 return ret;
3589}
3590
3591/**
3592 * @brief Compile parsed anydata or anyxml node information.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003593 *
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003594 * @param[in] ctx Compile context
3595 * @param[in] pnode Parsed anydata or anyxml node.
3596 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3597 * is enriched with the any-specific information.
3598 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3599 */
3600static LY_ERR
3601lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3602{
3603 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3604 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003605 LY_ERR ret = LY_SUCCESS;
3606
Michal Vasko5347e3a2020-11-03 17:14:57 +01003607 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003608
3609 /* add must(s) to unres */
3610 ret = lysc_unres_must_add(ctx, node, pnode);
3611 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003612
Radek Krejci91531e12021-02-08 19:57:54 +01003613 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003614 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko193dacd2022-10-13 08:43:05 +02003615 lyplg_ext_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003616 }
3617done:
3618 return ret;
3619}
3620
3621/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003622 * @brief Prepare the case structure in choice node for the new data node.
3623 *
3624 * 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
3625 * created in the choice when the first child was processed.
3626 *
3627 * @param[in] ctx Compile context.
3628 * @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,
3629 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3630 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3631 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3632 * @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,
3633 * it is linked from the case structure only in case it is its first child.
3634 */
3635static LY_ERR
3636lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3637{
Michal Vasko95f736c2022-06-08 12:03:31 +02003638 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003639 struct lysp_node *child_p;
3640 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3641
3642 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3643 /* we have to add an implicit case node into the parent choice */
3644 } else if (pnode->nodetype == LYS_CASE) {
3645 /* explicit parent case */
3646 LY_LIST_FOR(cs_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003647 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003648 }
3649 } else {
3650 LOGINT_RET(ctx->ctx);
3651 }
3652
Michal Vasko95f736c2022-06-08 12:03:31 +02003653 /* connect any augments */
3654 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3655
3656done:
3657 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003658}
3659
3660void
3661lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3662{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003663 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003664
3665 if (add) { /* set flag */
3666 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3667 parent = parent->parent) {
3668 parent->flags |= LYS_MAND_TRUE;
3669 }
3670 } else { /* unset flag */
3671 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003672 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003673 if (iter->flags & LYS_MAND_TRUE) {
3674 /* there is another mandatory node */
3675 return;
3676 }
3677 }
3678 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3679 parent->flags &= ~LYS_MAND_TRUE;
3680 }
3681 }
3682}
3683
3684/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003685 * @brief Get the grouping with the specified name from given groupings sized array.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003686 *
3687 * @param[in] node Linked list of nodes with groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003688 * @param[in] name Name of the grouping to find,
3689 * @return NULL when there is no grouping with the specified name
3690 * @return Pointer to the grouping of the specified @p name.
3691 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003692static struct lysp_node_grp *
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003693match_grouping(const struct lysp_node_grp *node, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003694{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003695 LY_LIST_FOR(node, node) {
3696 if ((node->nodetype == LYS_GROUPING) && !strcmp(node->name, name)) {
3697 return (struct lysp_node_grp *)node;
Radek Krejciabd33a42021-01-20 17:18:59 +01003698 }
3699 }
3700
3701 return NULL;
3702}
3703
3704/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003705 * @brief Find grouping for a uses.
3706 *
3707 * @param[in] ctx Compile context.
3708 * @param[in] uses_p Parsed uses node.
3709 * @param[out] gpr_p Found grouping on success.
3710 * @param[out] grp_pmod Module of @p grp_p on success.
3711 * @return LY_ERR value.
3712 */
3713static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003714lys_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 +02003715 struct lysp_module **grp_pmod)
3716{
3717 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003718 struct lysp_node_grp *grp;
Michal Vasko193dacd2022-10-13 08:43:05 +02003719 const struct lysp_node_grp *ext_grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003720 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003721 const char *id, *name, *prefix, *local_pref;
3722 size_t prefix_len, name_len;
3723 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003724 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003725
3726 *grp_p = NULL;
3727 *grp_pmod = NULL;
3728
3729 /* search for the grouping definition */
3730 id = uses_p->name;
3731 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3732 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3733 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3734 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003735 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003736 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003737 if ((grp = match_grouping(lysp_node_groupings(pnode), name))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003738 found = ctx->pmod;
3739 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003740 }
3741 }
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003742
3743 /* if in an extension, search possible groupings in it */
Michal Vaskob62e1e32022-11-08 10:50:41 +01003744 if (!found && ctx->ext) {
Michal Vaskofbd037c2022-11-08 10:34:20 +01003745 lyplg_ext_parsed_get_storage(ctx->ext, LY_STMT_GROUPING, sizeof ext_grp, (const void **)&ext_grp);
Michal Vasko193dacd2022-10-13 08:43:05 +02003746 if ((grp = match_grouping(ext_grp, name))) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003747 found = ctx->pmod;
3748 }
3749 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003750 } else {
3751 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003752 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003753 if (!mod) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003754 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid prefix used for grouping reference.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003755 return LY_EVALID;
3756 }
3757 pmod = mod->parsed;
3758 }
3759
3760 if (!found) {
3761 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003762 if ((grp = match_grouping(pmod->groupings, name))) {
3763 found = pmod;
3764 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003765 /* ... and all the submodules */
3766 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003767 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3768 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003769 break;
3770 }
3771 }
3772 }
3773 }
3774 if (!found) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003775 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003776 return LY_EVALID;
3777 }
3778
Michal Vasko7c565922021-06-10 14:58:27 +02003779 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003780 /* remember that the grouping is instantiated to avoid its standalone validation */
3781 grp->flags |= LYS_USED_GRP;
3782 }
3783
3784 *grp_p = grp;
3785 *grp_pmod = found;
3786 return LY_SUCCESS;
3787}
3788
3789/**
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003790 * @brief Compile uses grouping children.
3791 *
3792 * @param[in] ctx Compile context.
3793 * @param[in] uses_p Parsed uses.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003794 * @param[in] inherited_flags Inherited flags from the uses.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003795 * @param[in] child First grouping child to compile.
3796 * @param[in] grp_mod Grouping parsed module.
3797 * @param[in] parent Uses compiled parent, may be NULL if top-level.
3798 * @param[in,out] child_set Set of all compiled child nodes.
3799 * @param[in] child_unres_disabled Whether the children are to be put into unres disabled set or not.
3800 * @return LY_SUCCESS on success.
3801 * @return LY_EVALID on failure.
3802 */
3803static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003804lys_compile_uses_children(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, uint16_t inherited_flags,
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003805 struct lysp_node *child, struct lysp_module *grp_mod, struct lysc_node *parent, struct ly_set *child_set,
3806 ly_bool child_unres_disabled)
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003807{
3808 LY_ERR rc = LY_SUCCESS;
3809 struct lysp_module *mod_old = ctx->pmod;
3810 uint32_t child_i, opt_prev = ctx->compile_opts;
3811 ly_bool enabled;
3812 struct lysp_node *pnode;
3813 struct lysc_node *node;
3814 struct lysc_when *when_shared = NULL;
3815
3816 assert(child_set);
3817
Michal Vasko5940c302022-07-14 13:54:38 +02003818 child_i = child_set->count;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003819 LY_LIST_FOR(child, pnode) {
3820 /* compile the nodes with their parsed (grouping) module */
3821 ctx->pmod = grp_mod;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003822 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, parent, inherited_flags, child_set), cleanup);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003823
3824 /* eval if-features again for the rest of this node processing */
3825 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
3826 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3827 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3828 }
3829
3830 /* restore the parsed module */
3831 ctx->pmod = mod_old;
3832
3833 /* since the uses node is not present in the compiled tree, we need to pass some of its
3834 * statements to all its children */
3835 while (child_i < child_set->count) {
3836 node = child_set->snodes[child_i];
3837
3838 if (uses_p->when) {
3839 /* pass uses when to all the children */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003840 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 +02003841 LY_CHECK_GOTO(rc, cleanup);
3842 }
3843
3844 if (child_unres_disabled) {
3845 /* child is disabled by the uses if-features */
3846 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
3847 }
3848
3849 /* child processed */
3850 ++child_i;
3851 }
3852
3853 /* next iter */
3854 ctx->compile_opts = opt_prev;
3855 }
3856
3857cleanup:
3858 ctx->compile_opts = opt_prev;
3859 return rc;
3860}
3861
3862/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003863 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3864 * If present, also apply uses's modificators.
3865 *
3866 * @param[in] ctx Compile context
3867 * @param[in] uses_p Parsed uses schema node.
3868 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3869 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3870 * the compile context.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003871 * @param[in] inherited_flags Inherited flags from a schema-only statement.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003872 * @param[in] child_set Optional set of all the compiled children.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003873 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3874 */
3875static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003876lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, uint16_t inherited_flags,
3877 struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003878{
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003879 LY_ERR rc = LY_SUCCESS;
3880 ly_bool enabled, child_unres_disabled = 0;
3881 uint32_t i, grp_stack_count, opt_prev = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003882 struct lysp_node_grp *grp = NULL;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003883 uint16_t uses_flags = 0;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003884 struct lysp_module *grp_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003885 struct ly_set uses_child_set = {0};
3886
3887 /* find the referenced grouping */
3888 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3889
3890 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3891 grp_stack_count = ctx->groupings.count;
3892 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3893 if (grp_stack_count == ctx->groupings.count) {
3894 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003895 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003896 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3897 return LY_EVALID;
3898 }
3899
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003900 /* nodetype checks */
3901 if (grp->actions && (parent && !lysc_node_actions_p(parent))) {
3902 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3903 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3904 parent->name, lys_nodetype2str(parent->nodetype));
3905 rc = LY_EVALID;
3906 goto cleanup;
3907 }
3908 if (grp->notifs && (parent && !lysc_node_notifs_p(parent))) {
3909 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3910 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3911 parent->name, lys_nodetype2str(parent->nodetype));
3912 rc = LY_EVALID;
3913 goto cleanup;
3914 }
3915
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003916 /* check status */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003917 rc = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3918 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003919
3920 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003921 rc = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3922 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003923
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003924 /* compile special uses status flags */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003925 rc = lys_compile_status(ctx, uses_p->flags, inherited_flags, parent ? parent->flags : 0,
3926 parent ? parent->name : NULL, "<uses>", &uses_flags);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003927 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003928
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003929 /* uses if-features */
3930 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, uses_p->iffeatures, &enabled), cleanup);
3931 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3932 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3933 child_unres_disabled = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003934 }
3935
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003936 /* uses grouping children */
3937 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, grp->child, grp_mod, parent,
3938 child_set ? child_set : &uses_child_set, child_unres_disabled);
3939 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003940
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003941 /* uses grouping RPCs/actions */
3942 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->actions, grp_mod, parent,
3943 child_set ? child_set : &uses_child_set, child_unres_disabled);
3944 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003945
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003946 /* uses grouping notifications */
3947 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->notifs, grp_mod, parent,
3948 child_set ? child_set : &uses_child_set, child_unres_disabled);
3949 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003950
3951 /* check that all augments were applied */
3952 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003953 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3954 /* augment of some parent uses, irrelevant now */
3955 continue;
3956 }
3957
3958 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003959 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003960 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003961 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003962 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003963
3964 /* check that all refines were applied */
3965 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003966 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003967 /* refine of some parent uses, irrelevant now */
Michal Vaskod8655722021-01-12 15:20:36 +01003968 continue;
3969 }
3970
3971 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003972 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003973 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003974 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003975 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003976
Michal Vasko193dacd2022-10-13 08:43:05 +02003977 /* compile uses and grouping extensions into the parent */
3978 COMPILE_EXTS_GOTO(ctx, uses_p->exts, parent->exts, parent, rc, cleanup);
3979 COMPILE_EXTS_GOTO(ctx, grp->exts, parent->exts, parent, rc, cleanup);
3980
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003981cleanup:
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003982 /* restore previous context */
3983 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003984
3985 /* remove the grouping from the stack for circular groupings dependency check */
3986 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3987 assert(ctx->groupings.count == grp_stack_count);
3988
3989 ly_set_erase(&uses_child_set, NULL);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003990 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003991}
3992
3993static int
3994lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3995{
3996 struct lysp_node *iter;
3997 int len = 0;
3998
3999 *path = NULL;
4000 for (iter = node; iter && len >= 0; iter = iter->parent) {
4001 char *s = *path;
4002 char *id;
4003
4004 switch (iter->nodetype) {
4005 case LYS_USES:
4006 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
4007 break;
4008 case LYS_GROUPING:
4009 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
4010 break;
4011 case LYS_AUGMENT:
4012 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
4013 break;
4014 default:
4015 id = strdup(iter->name);
4016 break;
4017 }
4018
4019 if (!iter->parent) {
4020 /* print prefix */
4021 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
4022 } else {
4023 /* prefix is the same as in parent */
4024 len = asprintf(path, "/%s%s", id, s ? s : "");
4025 }
4026 free(s);
4027 free(id);
4028 }
4029
4030 if (len < 0) {
4031 free(*path);
4032 *path = NULL;
4033 } else if (len == 0) {
4034 *path = strdup("/");
4035 len = 1;
4036 }
4037 return len;
4038}
4039
4040LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01004041lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004042{
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004043 LY_ERR rc = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004044 char *path;
4045 int len;
4046
Michal Vasko8254d852022-04-25 10:05:59 +02004047 /* use grouping status to avoid errors */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004048 struct lysp_node_uses fake_uses = {
4049 .parent = pnode,
4050 .nodetype = LYS_USES,
Michal Vasko8254d852022-04-25 10:05:59 +02004051 .flags = grp->flags & LYS_STATUS_MASK, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004052 .name = grp->name,
4053 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4054 .refines = NULL, .augments = NULL
4055 };
4056 struct lysc_node_container fake_container = {
4057 .nodetype = LYS_CONTAINER,
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004058 .flags = 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004059 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02004060 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01004061 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004062 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004063 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004064 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4065 };
4066
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004067 /* compile fake container flags */
4068 LY_CHECK_GOTO(rc = lys_compile_node_flags(ctx, pnode ? pnode->flags : 0, 0, &fake_container.node), cleanup);
4069
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004070 if (grp->parent) {
4071 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4072 }
4073
4074 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
4075 if (len < 0) {
4076 LOGMEM(ctx->ctx);
4077 return LY_EMEM;
4078 }
4079 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
4080 ctx->path_len = (uint32_t)len;
4081 free(path);
4082
4083 lysc_update_path(ctx, NULL, "{grouping}");
4084 lysc_update_path(ctx, NULL, grp->name);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004085 rc = lys_compile_uses(ctx, &fake_uses, &fake_container.node, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004086 lysc_update_path(ctx, NULL, NULL);
4087 lysc_update_path(ctx, NULL, NULL);
4088
4089 ctx->path_len = 1;
4090 ctx->path[1] = '\0';
4091
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004092cleanup:
Michal Vaskoc636ea42022-09-16 10:20:31 +02004093 lysc_node_container_free(&ctx->free_ctx, &fake_container);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004094 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004095}
4096
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004097LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004098lys_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 +02004099 struct ly_set *child_set)
4100{
4101 LY_ERR ret = LY_SUCCESS;
4102 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02004103 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004104
4105 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
4106
4107 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01004108 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004109 } else {
4110 lysc_update_path(ctx, NULL, "{uses}");
4111 lysc_update_path(ctx, NULL, pnode->name);
4112 }
4113
4114 switch (pnode->nodetype) {
4115 case LYS_CONTAINER:
4116 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
4117 node_compile_spec = lys_compile_node_container;
4118 break;
4119 case LYS_LEAF:
4120 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
4121 node_compile_spec = lys_compile_node_leaf;
4122 break;
4123 case LYS_LIST:
4124 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
4125 node_compile_spec = lys_compile_node_list;
4126 break;
4127 case LYS_LEAFLIST:
4128 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
4129 node_compile_spec = lys_compile_node_leaflist;
4130 break;
4131 case LYS_CHOICE:
4132 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
4133 node_compile_spec = lys_compile_node_choice;
4134 break;
4135 case LYS_CASE:
4136 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
4137 node_compile_spec = lys_compile_node_case;
4138 break;
4139 case LYS_ANYXML:
4140 case LYS_ANYDATA:
4141 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
4142 node_compile_spec = lys_compile_node_any;
4143 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004144 case LYS_RPC:
4145 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02004146 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004147 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4148 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004149 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004150 return LY_EVALID;
4151 }
4152 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
4153 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02004154 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004155 break;
4156 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02004157 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004158 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4159 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004160 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004161 return LY_EVALID;
4162 }
4163 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
4164 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02004165 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004166 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004167 case LYS_USES:
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004168 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, inherited_flags, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004169 lysc_update_path(ctx, NULL, NULL);
4170 lysc_update_path(ctx, NULL, NULL);
4171 return ret;
4172 default:
4173 LOGINT(ctx->ctx);
4174 return LY_EINT;
4175 }
4176 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4177
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004178 ret = lys_compile_node_(ctx, pnode, parent, inherited_flags, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004179
Michal Vasko7c565922021-06-10 14:58:27 +02004180 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004181 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004182 return ret;
4183}