blob: 67c73ae6c6778d3911af39e889a787055b607094 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of common nodes.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
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"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020032#include "plugins_exts.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020033#include "plugins_types.h"
34#include "schema_compile.h"
35#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010036#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "set.h"
38#include "tree.h"
39#include "tree_data.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010040#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020041#include "tree_schema.h"
42#include "tree_schema_internal.h"
43#include "xpath.h"
44
45static struct lysc_ext_instance *
46lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
47{
48 /* TODO - extensions, increase refcount */
49 (void) ctx;
50 (void) orig;
51 return NULL;
52}
53
54/**
55 * @brief Add/replace a leaf default value in unres.
56 * Can also be used for a single leaf-list default value.
57 *
58 * @param[in] ctx Compile context.
59 * @param[in] leaf Leaf with the default value.
60 * @param[in] dflt Default value to use.
61 * @return LY_ERR value.
62 */
63static LY_ERR
64lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
65{
66 struct lysc_unres_dflt *r = NULL;
67 uint32_t i;
68
Michal Vasko12606ee2020-11-25 17:05:11 +010069 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010070 return LY_SUCCESS;
71 }
72
Michal Vasko405cc9e2020-12-01 12:01:27 +010073 for (i = 0; i < ctx->unres->dflts.count; ++i) {
74 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020075 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010076 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020077 lysp_qname_free(ctx->ctx, r->dflt);
78 free(r->dflt);
79 break;
80 }
81 }
82 if (!r) {
83 /* add new unres item */
84 r = calloc(1, sizeof *r);
85 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
86 r->leaf = leaf;
87
Michal Vasko405cc9e2020-12-01 12:01:27 +010088 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020089 }
90
91 r->dflt = malloc(sizeof *r->dflt);
92 LY_CHECK_GOTO(!r->dflt, error);
93 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
94
95 return LY_SUCCESS;
96
97error:
98 free(r->dflt);
99 LOGMEM(ctx->ctx);
100 return LY_EMEM;
101}
102
103/**
104 * @brief Add/replace a leaf-list default value(s) in unres.
105 *
106 * @param[in] ctx Compile context.
107 * @param[in] llist Leaf-list with the default value.
108 * @param[in] dflts Sized array of the default values.
109 * @return LY_ERR value.
110 */
111static LY_ERR
112lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
113{
114 struct lysc_unres_dflt *r = NULL;
115 uint32_t i;
116
Michal Vasko12606ee2020-11-25 17:05:11 +0100117 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100118 return LY_SUCCESS;
119 }
120
Michal Vasko405cc9e2020-12-01 12:01:27 +0100121 for (i = 0; i < ctx->unres->dflts.count; ++i) {
122 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200123 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100124 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200125 lysp_qname_free(ctx->ctx, r->dflt);
126 free(r->dflt);
127 r->dflt = NULL;
128 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
129 r->dflts = NULL;
130 break;
131 }
132 }
133 if (!r) {
134 r = calloc(1, sizeof *r);
135 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
136 r->llist = llist;
137
Michal Vasko405cc9e2020-12-01 12:01:27 +0100138 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200139 }
140
141 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
142
143 return LY_SUCCESS;
144}
145
146/**
147 * @brief Duplicate the compiled pattern structure.
148 *
149 * Instead of duplicating memory, the reference counter in the @p orig is increased.
150 *
151 * @param[in] orig The pattern structure to duplicate.
152 * @return The duplicated structure to use.
153 */
154static struct lysc_pattern *
155lysc_pattern_dup(struct lysc_pattern *orig)
156{
157 ++orig->refcount;
158 return orig;
159}
160
161/**
162 * @brief Duplicate the array of compiled patterns.
163 *
164 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
165 *
166 * @param[in] ctx Libyang context for logging.
167 * @param[in] orig The patterns sized array to duplicate.
168 * @return New sized array as a copy of @p orig.
169 * @return NULL in case of memory allocation error.
170 */
171static struct lysc_pattern **
172lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
173{
174 struct lysc_pattern **dup = NULL;
175 LY_ARRAY_COUNT_TYPE u;
176
177 assert(orig);
178
179 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
180 LY_ARRAY_FOR(orig, u) {
181 dup[u] = lysc_pattern_dup(orig[u]);
182 LY_ARRAY_INCREMENT(dup);
183 }
184 return dup;
185}
186
187/**
188 * @brief Duplicate compiled range structure.
189 *
190 * @param[in] ctx Libyang context for logging.
191 * @param[in] orig The range structure to be duplicated.
192 * @return New compiled range structure as a copy of @p orig.
193 * @return NULL in case of memory allocation error.
194 */
195static struct lysc_range *
196lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
197{
198 struct lysc_range *dup;
199 LY_ERR ret;
200
201 assert(orig);
202
203 dup = calloc(1, sizeof *dup);
204 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
205 if (orig->parts) {
206 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100207 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200208 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
209 }
210 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
211 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
212 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
213
214 return dup;
215cleanup:
216 free(dup);
217 (void) ret; /* set but not used due to the return type */
218 return NULL;
219}
220
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200221/**
222 * @brief Compile information from the when statement
223 *
224 * @param[in] ctx Compile context.
225 * @param[in] when_p Parsed when structure.
226 * @param[in] flags Flags of the parsed node with the when statement.
227 * @param[in] ctx_node Context node for the when statement.
228 * @param[out] when Pointer where to store pointer to the created compiled when structure.
229 * @return LY_ERR value.
230 */
231static LY_ERR
232lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
233 struct lysc_when **when)
234{
235 LY_ERR ret = LY_SUCCESS;
236
237 *when = calloc(1, sizeof **when);
238 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
239 (*when)->refcount = 1;
240 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
241 LY_CHECK_RET(lysc_prefixes_compile(when_p->cond, strlen(when_p->cond), ctx->pmod, &(*when)->prefixes));
242 (*when)->context = (struct lysc_node *)ctx_node;
243 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
244 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100245 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200246 (*when)->flags = flags & LYS_STATUS_MASK;
247
248done:
249 return ret;
250}
251
252LY_ERR
253lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
254 struct lysc_node *node, struct lysc_when **when_c)
255{
256 struct lysc_when **new_when, ***node_when;
257
258 assert(when_p);
259
260 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100261 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200262
263 /* create new when pointer */
264 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
265 if (!when_c || !(*when_c)) {
266 /* compile when */
267 LY_CHECK_RET(lys_compile_when_(ctx, when_p, flags, ctx_node, new_when));
268
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200269 /* remember the compiled when for sharing */
270 if (when_c) {
271 *when_c = *new_when;
272 }
273 } else {
274 /* use the previously compiled when */
275 ++(*when_c)->refcount;
276 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100277 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200278
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100279 if (!(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
280 /* do not check "when" semantics in a grouping, but repeat the check for different node because
281 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100282 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200283 }
284
285 return LY_SUCCESS;
286}
287
288/**
289 * @brief Compile information from the must statement
290 * @param[in] ctx Compile context.
291 * @param[in] must_p The parsed must statement structure.
292 * @param[in,out] must Prepared (empty) compiled must structure to fill.
293 * @return LY_ERR value.
294 */
295static LY_ERR
296lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
297{
298 LY_ERR ret = LY_SUCCESS;
299
300 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
301 LY_CHECK_RET(lysc_prefixes_compile(must_p->arg.str, strlen(must_p->arg.str), must_p->arg.mod, &must->prefixes));
302 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
303 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
304 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
305 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100306 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200307
308done:
309 return ret;
310}
311
312/**
313 * @brief Validate and normalize numeric value from a range definition.
314 * @param[in] ctx Compile context.
315 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
316 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
317 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
318 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
319 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
320 * @param[in] value String value of the range boundary.
321 * @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.
322 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
323 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
324 */
325static LY_ERR
326range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
327 size_t *len, char **valcopy)
328{
329 size_t fraction = 0, size;
330
331 *len = 0;
332
333 assert(value);
334 /* parse value */
335 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
336 return LY_EVALID;
337 }
338
339 if ((value[*len] == '-') || (value[*len] == '+')) {
340 ++(*len);
341 }
342
343 while (isdigit(value[*len])) {
344 ++(*len);
345 }
346
347 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
348 if (basetype == LY_TYPE_DEC64) {
349 goto decimal;
350 } else {
351 *valcopy = strndup(value, *len);
352 return LY_SUCCESS;
353 }
354 }
355 fraction = *len;
356
357 ++(*len);
358 while (isdigit(value[*len])) {
359 ++(*len);
360 }
361
362 if (basetype == LY_TYPE_DEC64) {
363decimal:
364 assert(frdigits);
365 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100366 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200367 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100368 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200369 return LY_EINVAL;
370 }
371 if (fraction) {
372 size = (*len) + (frdigits - ((*len) - 1 - fraction));
373 } else {
374 size = (*len) + frdigits + 1;
375 }
376 *valcopy = malloc(size * sizeof **valcopy);
377 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
378
379 (*valcopy)[size - 1] = '\0';
380 if (fraction) {
381 memcpy(&(*valcopy)[0], &value[0], fraction);
382 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
383 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
384 } else {
385 memcpy(&(*valcopy)[0], &value[0], *len);
386 memset(&(*valcopy)[*len], '0', frdigits);
387 }
388 }
389 return LY_SUCCESS;
390}
391
392/**
393 * @brief Check that values in range are in ascendant order.
394 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
395 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
396 * max can be also equal.
397 * @param[in] value Current value to check.
398 * @param[in] prev_value The last seen value.
399 * @return LY_SUCCESS or LY_EEXIST for invalid order.
400 */
401static LY_ERR
402range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
403{
404 if (unsigned_value) {
405 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
406 return LY_EEXIST;
407 }
408 } else {
409 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
410 return LY_EEXIST;
411 }
412 }
413 return LY_SUCCESS;
414}
415
416/**
417 * @brief Set min/max value of the range part.
418 * @param[in] ctx Compile context.
419 * @param[in] part Range part structure to fill.
420 * @param[in] max Flag to distinguish if storing min or max value.
421 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
422 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
423 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
424 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
425 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
426 * @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.
427 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
428 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
429 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
430 * frdigits value), LY_EMEM.
431 */
432static LY_ERR
433range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
434 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
435{
436 LY_ERR ret = LY_SUCCESS;
437 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100438 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200439
440 if (value) {
441 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
442 LY_CHECK_GOTO(ret, finalize);
443 }
444 if (!valcopy && base_range) {
445 if (max) {
446 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
447 } else {
448 part->min_64 = base_range->parts[0].min_64;
449 }
450 if (!first) {
451 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
452 }
453 goto finalize;
454 }
455
456 switch (basetype) {
457 case LY_TYPE_INT8: /* range */
458 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100459 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 +0200460 } else if (max) {
461 part->max_64 = INT64_C(127);
462 } else {
463 part->min_64 = INT64_C(-128);
464 }
465 if (!ret && !first) {
466 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
467 }
468 break;
469 case LY_TYPE_INT16: /* range */
470 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100471 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
472 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200473 } else if (max) {
474 part->max_64 = INT64_C(32767);
475 } else {
476 part->min_64 = INT64_C(-32768);
477 }
478 if (!ret && !first) {
479 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
480 }
481 break;
482 case LY_TYPE_INT32: /* range */
483 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100484 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
485 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200486 } else if (max) {
487 part->max_64 = INT64_C(2147483647);
488 } else {
489 part->min_64 = INT64_C(-2147483648);
490 }
491 if (!ret && !first) {
492 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
493 }
494 break;
495 case LY_TYPE_INT64: /* range */
496 case LY_TYPE_DEC64: /* range */
497 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100498 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
499 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200500 } else if (max) {
501 part->max_64 = INT64_C(9223372036854775807);
502 } else {
503 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
504 }
505 if (!ret && !first) {
506 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
507 }
508 break;
509 case LY_TYPE_UINT8: /* range */
510 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100511 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 +0200512 } else if (max) {
513 part->max_u64 = UINT64_C(255);
514 } else {
515 part->min_u64 = UINT64_C(0);
516 }
517 if (!ret && !first) {
518 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
519 }
520 break;
521 case LY_TYPE_UINT16: /* range */
522 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100523 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 +0200524 } else if (max) {
525 part->max_u64 = UINT64_C(65535);
526 } else {
527 part->min_u64 = UINT64_C(0);
528 }
529 if (!ret && !first) {
530 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
531 }
532 break;
533 case LY_TYPE_UINT32: /* range */
534 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100535 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
536 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200537 } else if (max) {
538 part->max_u64 = UINT64_C(4294967295);
539 } else {
540 part->min_u64 = UINT64_C(0);
541 }
542 if (!ret && !first) {
543 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
544 }
545 break;
546 case LY_TYPE_UINT64: /* range */
547 case LY_TYPE_STRING: /* length */
548 case LY_TYPE_BINARY: /* length */
549 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100550 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
551 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200552 } else if (max) {
553 part->max_u64 = UINT64_C(18446744073709551615);
554 } else {
555 part->min_u64 = UINT64_C(0);
556 }
557 if (!ret && !first) {
558 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
559 }
560 break;
561 default:
562 LOGINT(ctx->ctx);
563 ret = LY_EINT;
564 }
565
566finalize:
567 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100568 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200569 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
570 length_restr ? "length" : "range", valcopy ? valcopy : *value);
571 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100572 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200573 "Invalid %s restriction - invalid value \"%s\".",
574 length_restr ? "length" : "range", valcopy ? valcopy : *value);
575 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100576 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200577 "Invalid %s restriction - values are not in ascending order (%s).",
578 length_restr ? "length" : "range",
579 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
580 } else if (!ret && value) {
581 *value = *value + len;
582 }
583 free(valcopy);
584 return ret;
585}
586
587/**
588 * @brief Compile the parsed range restriction.
589 * @param[in] ctx Compile context.
590 * @param[in] range_p Parsed range structure to compile.
591 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
592 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
593 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
594 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
595 * range restriction must be more restrictive than the base_range.
596 * @param[in,out] range Pointer to the created current range structure.
597 * @return LY_ERR value.
598 */
599static LY_ERR
600lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
601 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
602{
603 LY_ERR ret = LY_EVALID;
604 const char *expr;
605 struct lysc_range_part *parts = NULL, *part;
606 ly_bool range_expected = 0, uns;
607 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
608
609 assert(range);
610 assert(range_p);
611
612 expr = range_p->arg.str;
613 while (1) {
614 if (isspace(*expr)) {
615 ++expr;
616 } else if (*expr == '\0') {
617 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100618 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200619 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
620 length_restr ? "length" : "range", range_p->arg);
621 goto cleanup;
622 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200624 "Invalid %s restriction - unexpected end of the expression (%s).",
625 length_restr ? "length" : "range", range_p->arg);
626 goto cleanup;
627 }
628 parts_done++;
629 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100630 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200631 if (parts) {
632 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100633 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200634 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci422afb12021-03-04 16:38:16 +0100635 (int)(expr - range_p->arg.str), range_p->arg.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200636 goto cleanup;
637 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100638 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200639
640 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
641 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
642 part->max_64 = part->min_64;
643 } else if (*expr == '|') {
644 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100645 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200646 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
647 goto cleanup;
648 }
649 expr++;
650 parts_done++;
651 /* process next part of the expression */
652 } else if (!strncmp(expr, "..", 2)) {
653 expr += 2;
654 while (isspace(*expr)) {
655 expr++;
656 }
657 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100658 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200659 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
660 goto cleanup;
661 }
662 /* continue expecting the upper boundary */
663 range_expected = 1;
664 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
665 /* number */
666 if (range_expected) {
667 part = &parts[LY_ARRAY_COUNT(parts) - 1];
668 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
669 range_expected = 0;
670 } else {
671 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
672 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
673 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
674 part->max_64 = part->min_64;
675 }
676
677 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100678 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
679 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200680 while (isspace(*expr)) {
681 expr++;
682 }
683 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100684 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200685 length_restr ? "length" : "range", expr);
686 goto cleanup;
687 }
688 if (range_expected) {
689 part = &parts[LY_ARRAY_COUNT(parts) - 1];
690 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
691 range_expected = 0;
692 } else {
693 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
694 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
695 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
696 part->min_64 = part->max_64;
697 }
698 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100699 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200700 length_restr ? "length" : "range", expr);
701 goto cleanup;
702 }
703 }
704
705 /* check with the previous range/length restriction */
706 if (base_range) {
707 switch (basetype) {
708 case LY_TYPE_BINARY:
709 case LY_TYPE_UINT8:
710 case LY_TYPE_UINT16:
711 case LY_TYPE_UINT32:
712 case LY_TYPE_UINT64:
713 case LY_TYPE_STRING:
714 uns = 1;
715 break;
716 case LY_TYPE_DEC64:
717 case LY_TYPE_INT8:
718 case LY_TYPE_INT16:
719 case LY_TYPE_INT32:
720 case LY_TYPE_INT64:
721 uns = 0;
722 break;
723 default:
724 LOGINT(ctx->ctx);
725 ret = LY_EINT;
726 goto cleanup;
727 }
728 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
729 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
730 goto baseerror;
731 }
732 /* current lower bound is not lower than the base */
733 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
734 /* base has single value */
735 if (base_range->parts[v].min_64 == parts[u].min_64) {
736 /* both lower bounds are the same */
737 if (parts[u].min_64 != parts[u].max_64) {
738 /* current continues with a range */
739 goto baseerror;
740 } else {
741 /* equal single values, move both forward */
742 ++v;
743 continue;
744 }
745 } else {
746 /* base is single value lower than current range, so the
747 * value from base range is removed in the current,
748 * move only base and repeat checking */
749 ++v;
750 --u;
751 continue;
752 }
753 } else {
754 /* base is the range */
755 if (parts[u].min_64 == parts[u].max_64) {
756 /* current is a single value */
757 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
758 /* current is behind the base range, so base range is omitted,
759 * move the base and keep the current for further check */
760 ++v;
761 --u;
762 } /* else it is within the base range, so move the current, but keep the base */
763 continue;
764 } else {
765 /* both are ranges - check the higher bound, the lower was already checked */
766 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
767 /* higher bound is higher than the current higher bound */
768 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
769 /* but the current lower bound is also higher, so the base range is omitted,
770 * continue with the same current, but move the base */
771 --u;
772 ++v;
773 continue;
774 }
775 /* current range starts within the base range but end behind it */
776 goto baseerror;
777 } else {
778 /* current range is smaller than the base,
779 * move current, but stay with the base */
780 continue;
781 }
782 }
783 }
784 }
785 if (u != parts_done) {
786baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100787 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200788 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
789 length_restr ? "length" : "range", range_p->arg);
790 goto cleanup;
791 }
792 }
793
794 if (!(*range)) {
795 *range = calloc(1, sizeof **range);
796 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
797 }
798
799 /* we rewrite the following values as the types chain is being processed */
800 if (range_p->eapptag) {
801 lydict_remove(ctx->ctx, (*range)->eapptag);
802 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
803 }
804 if (range_p->emsg) {
805 lydict_remove(ctx->ctx, (*range)->emsg);
806 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
807 }
808 if (range_p->dsc) {
809 lydict_remove(ctx->ctx, (*range)->dsc);
810 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
811 }
812 if (range_p->ref) {
813 lydict_remove(ctx->ctx, (*range)->ref);
814 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
815 }
816 /* extensions are taken only from the last range by the caller */
817
818 (*range)->parts = parts;
819 parts = NULL;
820 ret = LY_SUCCESS;
821cleanup:
822 LY_ARRAY_FREE(parts);
823
824 return ret;
825}
826
827LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100828lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200829{
830 size_t idx, idx2, start, end, size, brack;
831 char *perl_regex, *ptr;
832 int err_code;
833 const char *orig_ptr;
834 PCRE2_SIZE err_offset;
835 pcre2_code *code_local;
836
837#define URANGE_LEN 19
838 char *ublock2urange[][2] = {
839 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
840 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
841 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
842 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
843 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
844 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
845 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
846 {"Greek", "[\\x{0370}-\\x{03FF}]"},
847 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
848 {"Armenian", "[\\x{0530}-\\x{058F}]"},
849 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
850 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
851 {"Syriac", "[\\x{0700}-\\x{074F}]"},
852 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
853 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
854 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
855 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
856 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
857 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
858 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
859 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
860 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
861 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
862 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
863 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
864 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
865 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
866 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
867 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
868 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
869 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
870 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
871 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
872 {"Ogham", "[\\x{1680}-\\x{169F}]"},
873 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
874 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
875 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
876 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
877 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
878 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
879 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
880 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
881 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
882 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
883 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
884 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
885 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
886 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
887 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
888 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
889 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
890 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
891 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
892 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
893 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
894 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
895 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
896 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
897 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
898 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
899 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
900 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
901 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
902 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
903 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
904 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
905 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
906 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
907 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
908 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
909 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
910 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
911 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
912 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
913 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
914 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
915 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
916 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
917 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
918 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
919 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
920 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
921 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100922 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200923 {NULL, NULL}
924 };
925
926 /* adjust the expression to a Perl equivalent
927 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
928
929 /* allocate space for the transformed pattern */
930 size = strlen(pattern) + 1;
931 perl_regex = malloc(size);
932 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
933 perl_regex[0] = '\0';
934
935 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
936 brack = 0;
937 idx = 0;
938 orig_ptr = pattern;
939 while (orig_ptr[0]) {
940 switch (orig_ptr[0]) {
941 case '$':
942 case '^':
943 if (!brack) {
944 /* make space for the extra character */
945 ++size;
946 perl_regex = ly_realloc(perl_regex, size);
947 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
948
949 /* print escape slash */
950 perl_regex[idx] = '\\';
951 ++idx;
952 }
953 break;
954 case '[':
955 /* must not be escaped */
956 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
957 ++brack;
958 }
959 break;
960 case ']':
961 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
962 /* pattern was checked and compiled already */
963 assert(brack);
964 --brack;
965 }
966 break;
967 default:
968 break;
969 }
970
971 /* copy char */
972 perl_regex[idx] = orig_ptr[0];
973
974 ++idx;
975 ++orig_ptr;
976 }
977 perl_regex[idx] = '\0';
978
979 /* substitute Unicode Character Blocks with exact Character Ranges */
980 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
981 start = ptr - perl_regex;
982
983 ptr = strchr(ptr, '}');
984 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100985 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200986 free(perl_regex);
987 return LY_EVALID;
988 }
989 end = (ptr - perl_regex) + 1;
990
991 /* need more space */
992 if (end - start < URANGE_LEN) {
993 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
994 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
995 }
996
997 /* find our range */
998 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100999 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1000 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001001 break;
1002 }
1003 }
1004 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001005 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001006 free(perl_regex);
1007 return LY_EVALID;
1008 }
1009
1010 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1011 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1012 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1013 ++idx;
1014 }
1015 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1016 --idx;
1017 }
1018 }
1019 if (idx) {
1020 /* skip brackets */
1021 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1022 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1023 } else {
1024 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1025 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1026 }
1027 }
1028
1029 /* must return 0, already checked during parsing */
1030 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1031 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1032 &err_code, &err_offset, NULL);
1033 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001034 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1035 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001036 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001037 free(perl_regex);
1038 return LY_EVALID;
1039 }
1040 free(perl_regex);
1041
1042 if (code) {
1043 *code = code_local;
1044 } else {
1045 free(code_local);
1046 }
1047
1048 return LY_SUCCESS;
1049
1050#undef URANGE_LEN
1051}
1052
1053/**
1054 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1055 * @param[in] ctx Compile context.
1056 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1057 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1058 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1059 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1060 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1061 */
1062static LY_ERR
1063lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1064 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1065{
1066 struct lysc_pattern **pattern;
1067 LY_ARRAY_COUNT_TYPE u;
1068 LY_ERR ret = LY_SUCCESS;
1069
1070 /* first, copy the patterns from the base type */
1071 if (base_patterns) {
1072 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1073 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1074 }
1075
1076 LY_ARRAY_FOR(patterns_p, u) {
1077 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1078 *pattern = calloc(1, sizeof **pattern);
1079 ++(*pattern)->refcount;
1080
Radek Krejci2efc45b2020-12-22 16:25:44 +01001081 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001082 LY_CHECK_RET(ret);
1083
Radek Krejcif13b87b2020-12-01 22:02:17 +01001084 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001085 (*pattern)->inverted = 1;
1086 }
1087 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1088 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1089 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1090 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1091 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001092 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001093 }
1094done:
1095 return ret;
1096}
1097
1098/**
1099 * @brief map of the possible restrictions combination for the specific built-in type.
1100 */
1101static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1102 0 /* LY_TYPE_UNKNOWN */,
1103 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1104 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1105 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1106 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1107 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1108 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1109 LYS_SET_BIT /* LY_TYPE_BITS */,
1110 0 /* LY_TYPE_BOOL */,
1111 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1112 0 /* LY_TYPE_EMPTY */,
1113 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1114 LYS_SET_BASE /* LY_TYPE_IDENT */,
1115 LYS_SET_REQINST /* LY_TYPE_INST */,
1116 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1117 LYS_SET_TYPE /* LY_TYPE_UNION */,
1118 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1119 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1120 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1121 LYS_SET_RANGE /* LY_TYPE_INT64 */
1122};
1123
1124/**
1125 * @brief stringification of the YANG built-in data types
1126 */
1127const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
1128 "unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1129 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1130 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
1131};
1132
1133/**
1134 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1135 * @param[in] ctx Compile context.
1136 * @param[in] enums_p Array of the parsed enum structures to compile.
1137 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1138 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1139 * @param[out] enums Newly created array of the compiled enums information for the current type.
1140 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1141 */
1142static LY_ERR
1143lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
1144 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
1145{
1146 LY_ERR ret = LY_SUCCESS;
1147 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001148 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1149 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001150 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001151 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001152
1153 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001154 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001155 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1156 return LY_EVALID;
1157 }
1158
1159 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001160 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001161 if (base_enums) {
1162 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1163 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001164 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001165 break;
1166 }
1167 }
1168 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001169 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001170 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001171 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001172 return LY_EVALID;
1173 }
1174 match = v;
1175 }
1176
1177 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001178 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001179 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001180 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001181 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001182 LY_ARRAY_FOR(*enums, v) {
1183 if (cur_val == (*enums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001184 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001185 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001186 cur_val, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001187 return LY_EVALID;
1188 }
1189 }
1190 } else if (base_enums) {
1191 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001192 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001193 } else {
1194 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001195 if (u == 0) {
1196 cur_val = 0;
1197 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001198 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001199 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001200 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001201 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001202 } else {
1203 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001204 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001205 }
1206
1207 /* save highest value for auto assing */
1208 if (highest_value < cur_val) {
1209 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001210 }
1211 } else { /* LY_TYPE_BITS */
1212 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001213 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001214 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001215 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001216 LY_ARRAY_FOR(*enums, v) {
1217 if (cur_pos == (*enums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001218 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001219 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001220 cur_pos, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001221 return LY_EVALID;
1222 }
1223 }
1224 } else if (base_enums) {
1225 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001226 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001227 } else {
1228 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001229 if (u == 0) {
1230 cur_pos = 0;
1231 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001232 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001233 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001234 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001235 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001236 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001237 } else {
1238 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001239 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001240 }
1241
1242 /* save highest position for auto assing */
1243 if (highest_position < cur_pos) {
1244 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001245 }
1246 }
1247
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001248 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001250 if (basetype == LY_TYPE_ENUM) {
1251 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001252 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001253 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001254 enums_p[u].name, base_enums[match].value, cur_val);
1255 return LY_EVALID;
1256 }
1257 } else {
1258 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001259 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001261 enums_p[u].name, base_enums[match].position, cur_pos);
1262 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001263 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001264 }
1265 }
1266
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001267 /* evaluate if-ffeatures */
1268 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1269 if (!enabled) {
1270 continue;
1271 }
1272
1273 /* add new enum/bit */
1274 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1275 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1276 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1277 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001278 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 +01001279 if (basetype == LY_TYPE_ENUM) {
1280 e->value = cur_val;
1281 } else {
1282 e->position = cur_pos;
1283 }
Radek Krejciab430862021-03-02 20:13:40 +01001284 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001285
1286 if (basetype == LY_TYPE_BITS) {
1287 /* keep bits ordered by position */
1288 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
1289 if (v != u) {
1290 memcpy(&storage, e, sizeof *e);
1291 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1292 memcpy(&(*enums)[v], &storage, sizeof storage);
1293 }
1294 }
1295 }
1296
1297done:
1298 return ret;
1299}
1300
Radek Krejci6a205692020-12-09 13:58:22 +01001301static LY_ERR
1302lys_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 +01001303 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001304{
1305 LY_ERR ret = LY_SUCCESS;
1306 struct lysc_type **utypes = *utypes_p;
1307 struct lysc_type_union *un_aux = NULL;
1308
1309 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1310 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001311 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1312 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001313 LY_CHECK_GOTO(ret, error);
1314 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1315 /* add space for additional types from the union subtype */
1316 un_aux = (struct lysc_type_union *)utypes[u + additional];
1317 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1318 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1319
1320 /* copy subtypes of the subtype union */
1321 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1322 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1323 struct lysc_type_leafref *lref;
1324
1325 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1326 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1327 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1328 lref = (struct lysc_type_leafref *)utypes[u + additional];
1329
1330 lref->basetype = LY_TYPE_LEAFREF;
1331 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1332 LY_CHECK_GOTO(ret, error);
1333 lref->refcount = 1;
1334 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
1335 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
1336 ret = lysc_prefixes_dup(((struct lysc_type_leafref *)un_aux->types[v])->prefixes, &lref->prefixes);
1337 LY_CHECK_GOTO(ret, error);
1338 /* TODO extensions */
1339
1340 } else {
1341 utypes[u + additional] = un_aux->types[v];
1342 ++un_aux->types[v]->refcount;
1343 }
1344 ++additional;
1345 LY_ARRAY_INCREMENT(utypes);
1346 }
1347 /* compensate u increment in main loop */
1348 --additional;
1349
1350 /* free the replaced union subtype */
1351 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1352 un_aux = NULL;
1353 } else {
1354 LY_ARRAY_INCREMENT(utypes);
1355 }
1356 }
1357
1358 *utypes_p = utypes;
1359 return LY_SUCCESS;
1360
1361error:
1362 if (un_aux) {
1363 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1364 }
1365 *utypes_p = utypes;
1366 return ret;
1367}
1368
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001369/**
1370 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1371 * @param[in] ctx Compile context.
1372 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1373 * @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 +02001374 * @param[in] context_name Name of the context node or referencing typedef for logging.
1375 * @param[in] type_p Parsed type to compile.
1376 * @param[in] basetype Base YANG built-in type of the type to compile.
1377 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1378 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1379 * @param[out] type Newly created type structure with the filled information about the type.
1380 * @return LY_ERR value.
1381 */
1382static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001383lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1384 struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001385{
1386 LY_ERR ret = LY_SUCCESS;
1387 struct lysc_type_bin *bin;
1388 struct lysc_type_num *num;
1389 struct lysc_type_str *str;
1390 struct lysc_type_bits *bits;
1391 struct lysc_type_enum *enumeration;
1392 struct lysc_type_dec *dec;
1393 struct lysc_type_identityref *idref;
1394 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001395 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001396
1397 switch (basetype) {
1398 case LY_TYPE_BINARY:
1399 bin = (struct lysc_type_bin *)(*type);
1400
1401 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1402 if (type_p->length) {
1403 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1404 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1405 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001406 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001407 }
1408 }
1409 break;
1410 case LY_TYPE_BITS:
1411 /* RFC 7950 9.7 - bits */
1412 bits = (struct lysc_type_bits *)(*type);
1413 if (type_p->bits) {
1414 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1415 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1416 (struct lysc_type_bitenum_item **)&bits->bits));
1417 }
1418
1419 if (!base && !type_p->flags) {
1420 /* type derived from bits built-in type must contain at least one bit */
1421 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001422 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001423 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001424 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001425 }
1426 return LY_EVALID;
1427 }
1428 break;
1429 case LY_TYPE_DEC64:
1430 dec = (struct lysc_type_dec *)(*type);
1431
1432 /* RFC 7950 9.3.4 - fraction-digits */
1433 if (!base) {
1434 if (!type_p->fraction_digits) {
1435 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001436 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001437 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001438 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001439 }
1440 return LY_EVALID;
1441 }
1442 dec->fraction_digits = type_p->fraction_digits;
1443 } else {
1444 if (type_p->fraction_digits) {
1445 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1446 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001447 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001448 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1449 tpdfname);
1450 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001451 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001452 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1453 }
1454 return LY_EVALID;
1455 }
1456 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1457 }
1458
1459 /* RFC 7950 9.2.4 - range */
1460 if (type_p->range) {
1461 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1462 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1463 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001464 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001465 }
1466 }
1467 break;
1468 case LY_TYPE_STRING:
1469 str = (struct lysc_type_str *)(*type);
1470
1471 /* RFC 7950 9.4.4 - length */
1472 if (type_p->length) {
1473 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1474 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1475 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001476 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001477 }
1478 } else if (base && ((struct lysc_type_str *)base)->length) {
1479 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1480 }
1481
1482 /* RFC 7950 9.4.5 - pattern */
1483 if (type_p->patterns) {
1484 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1485 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1486 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1487 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1488 }
1489 break;
1490 case LY_TYPE_ENUM:
1491 enumeration = (struct lysc_type_enum *)(*type);
1492
1493 /* RFC 7950 9.6 - enum */
1494 if (type_p->enums) {
1495 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1496 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1497 }
1498
1499 if (!base && !type_p->flags) {
1500 /* type derived from enumerations built-in type must contain at least one enum */
1501 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001502 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001503 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001504 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001505 }
1506 return LY_EVALID;
1507 }
1508 break;
1509 case LY_TYPE_INT8:
1510 case LY_TYPE_UINT8:
1511 case LY_TYPE_INT16:
1512 case LY_TYPE_UINT16:
1513 case LY_TYPE_INT32:
1514 case LY_TYPE_UINT32:
1515 case LY_TYPE_INT64:
1516 case LY_TYPE_UINT64:
1517 num = (struct lysc_type_num *)(*type);
1518
1519 /* RFC 6020 9.2.4 - range */
1520 if (type_p->range) {
1521 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1522 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1523 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001524 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001525 }
1526 }
1527 break;
1528 case LY_TYPE_IDENT:
1529 idref = (struct lysc_type_identityref *)(*type);
1530
1531 /* RFC 7950 9.10.2 - base */
1532 if (type_p->bases) {
1533 if (base) {
1534 /* only the directly derived identityrefs can contain base specification */
1535 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001536 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001537 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1538 tpdfname);
1539 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001540 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001541 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1542 }
1543 return LY_EVALID;
1544 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001545 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001546 }
1547
1548 if (!base && !type_p->flags) {
1549 /* type derived from identityref built-in type must contain at least one base */
1550 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001551 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001552 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001553 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001554 }
1555 return LY_EVALID;
1556 }
1557 break;
1558 case LY_TYPE_LEAFREF:
1559 lref = (struct lysc_type_leafref *)*type;
1560
1561 /* RFC 7950 9.9.3 - require-instance */
1562 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001563 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001564 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001565 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001566 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1567 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001568 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001569 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1570 }
1571 return LY_EVALID;
1572 }
1573 lref->require_instance = type_p->require_instance;
1574 } else if (base) {
1575 /* inherit */
1576 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1577 } else {
1578 /* default is true */
1579 lref->require_instance = 1;
1580 }
1581 if (type_p->path) {
1582 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
1583 LY_CHECK_RET(lysc_prefixes_compile(type_p->path->expr, strlen(type_p->path->expr), type_p->pmod,
1584 &lref->prefixes));
1585 } else if (base) {
1586 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
1587 LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)base)->prefixes, &lref->prefixes));
1588 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001589 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001590 return LY_EVALID;
1591 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001592 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001593 return LY_EVALID;
1594 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001595 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001596 break;
1597 case LY_TYPE_INST:
1598 /* RFC 7950 9.9.3 - require-instance */
1599 if (type_p->flags & LYS_SET_REQINST) {
1600 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1601 } else {
1602 /* default is true */
1603 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1604 }
1605 break;
1606 case LY_TYPE_UNION:
1607 un = (struct lysc_type_union *)(*type);
1608
1609 /* RFC 7950 7.4 - type */
1610 if (type_p->types) {
1611 if (base) {
1612 /* only the directly derived union can contain types specification */
1613 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001614 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001615 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1616 tpdfname);
1617 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001618 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001619 "Invalid type substatement for the type not directly derived from union built-in type.");
1620 }
1621 return LY_EVALID;
1622 }
1623 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001624 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 +02001625 }
1626
1627 if (!base && !type_p->flags) {
1628 /* type derived from union built-in type must contain at least one type */
1629 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001630 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001631 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001632 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001633 }
1634 return LY_EVALID;
1635 }
1636 break;
1637 case LY_TYPE_BOOL:
1638 case LY_TYPE_EMPTY:
1639 case LY_TYPE_UNKNOWN: /* just to complete switch */
1640 break;
1641 }
1642
1643 if (tpdfname) {
1644 switch (basetype) {
1645 case LY_TYPE_BINARY:
1646 type_p->compiled = *type;
1647 *type = calloc(1, sizeof(struct lysc_type_bin));
1648 break;
1649 case LY_TYPE_BITS:
1650 type_p->compiled = *type;
1651 *type = calloc(1, sizeof(struct lysc_type_bits));
1652 break;
1653 case LY_TYPE_DEC64:
1654 type_p->compiled = *type;
1655 *type = calloc(1, sizeof(struct lysc_type_dec));
1656 break;
1657 case LY_TYPE_STRING:
1658 type_p->compiled = *type;
1659 *type = calloc(1, sizeof(struct lysc_type_str));
1660 break;
1661 case LY_TYPE_ENUM:
1662 type_p->compiled = *type;
1663 *type = calloc(1, sizeof(struct lysc_type_enum));
1664 break;
1665 case LY_TYPE_INT8:
1666 case LY_TYPE_UINT8:
1667 case LY_TYPE_INT16:
1668 case LY_TYPE_UINT16:
1669 case LY_TYPE_INT32:
1670 case LY_TYPE_UINT32:
1671 case LY_TYPE_INT64:
1672 case LY_TYPE_UINT64:
1673 type_p->compiled = *type;
1674 *type = calloc(1, sizeof(struct lysc_type_num));
1675 break;
1676 case LY_TYPE_IDENT:
1677 type_p->compiled = *type;
1678 *type = calloc(1, sizeof(struct lysc_type_identityref));
1679 break;
1680 case LY_TYPE_LEAFREF:
1681 type_p->compiled = *type;
1682 *type = calloc(1, sizeof(struct lysc_type_leafref));
1683 break;
1684 case LY_TYPE_INST:
1685 type_p->compiled = *type;
1686 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1687 break;
1688 case LY_TYPE_UNION:
1689 type_p->compiled = *type;
1690 *type = calloc(1, sizeof(struct lysc_type_union));
1691 break;
1692 case LY_TYPE_BOOL:
1693 case LY_TYPE_EMPTY:
1694 case LY_TYPE_UNKNOWN: /* just to complete switch */
1695 break;
1696 }
1697 }
1698 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1699
1700cleanup:
1701 return ret;
1702}
1703
1704LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001705lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1706 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001707{
1708 LY_ERR ret = LY_SUCCESS;
1709 ly_bool dummyloops = 0;
1710 struct type_context {
1711 const struct lysp_tpdf *tpdf;
1712 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001713 } *tctx, *tctx_prev = NULL, *tctx_iter;
1714 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1715 struct lysc_type *base = NULL, *prev_type;
1716 struct ly_set tpdf_chain = {0};
1717
1718 (*type) = NULL;
1719 if (dflt) {
1720 *dflt = NULL;
1721 }
1722
1723 tctx = calloc(1, sizeof *tctx);
1724 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001725 for (ret = lysp_type_find(type_p->name, context_pnode, type_p->pmod, &basetype, &tctx->tpdf, &tctx->node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001726 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001727 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1728 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001729 if (basetype) {
1730 break;
1731 }
1732
1733 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001734 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1735 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001736 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1737
1738 if (units && !*units) {
1739 /* inherit units */
1740 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1741 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1742 }
1743 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1744 /* inherit default */
1745 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1746 }
1747 if (dummyloops && (!units || *units) && dflt && *dflt) {
1748 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1749 break;
1750 }
1751
Radek Krejci720d2612021-03-03 19:44:22 +01001752 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1753 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1754 * and we need now recompile the type again in the updated context. */
1755 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1756 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1757 }
1758
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001759 if (tctx->tpdf->type.compiled) {
1760 /* it is not necessary to continue, the rest of the chain was already compiled,
1761 * but we still may need to inherit default and units values, so start dummy loops */
1762 basetype = tctx->tpdf->type.compiled->basetype;
1763 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1764 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1765
1766 if ((units && !*units) || (dflt && !*dflt)) {
1767 dummyloops = 1;
1768 goto preparenext;
1769 } else {
1770 tctx = NULL;
1771 break;
1772 }
1773 }
1774
1775 /* circular typedef reference detection */
1776 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1777 /* local part */
1778 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001779 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001780 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001781 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1782 free(tctx);
1783 ret = LY_EVALID;
1784 goto cleanup;
1785 }
1786 }
1787 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1788 /* global part for unions corner case */
1789 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001790 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001791 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001792 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1793 free(tctx);
1794 ret = LY_EVALID;
1795 goto cleanup;
1796 }
1797 }
1798
1799 /* store information for the following processing */
1800 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1801 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1802
1803preparenext:
1804 /* prepare next loop */
1805 tctx_prev = tctx;
1806 tctx = calloc(1, sizeof *tctx);
1807 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1808 }
1809 free(tctx);
1810
1811 /* allocate type according to the basetype */
1812 switch (basetype) {
1813 case LY_TYPE_BINARY:
1814 *type = calloc(1, sizeof(struct lysc_type_bin));
1815 break;
1816 case LY_TYPE_BITS:
1817 *type = calloc(1, sizeof(struct lysc_type_bits));
1818 break;
1819 case LY_TYPE_BOOL:
1820 case LY_TYPE_EMPTY:
1821 *type = calloc(1, sizeof(struct lysc_type));
1822 break;
1823 case LY_TYPE_DEC64:
1824 *type = calloc(1, sizeof(struct lysc_type_dec));
1825 break;
1826 case LY_TYPE_ENUM:
1827 *type = calloc(1, sizeof(struct lysc_type_enum));
1828 break;
1829 case LY_TYPE_IDENT:
1830 *type = calloc(1, sizeof(struct lysc_type_identityref));
1831 break;
1832 case LY_TYPE_INST:
1833 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1834 break;
1835 case LY_TYPE_LEAFREF:
1836 *type = calloc(1, sizeof(struct lysc_type_leafref));
1837 break;
1838 case LY_TYPE_STRING:
1839 *type = calloc(1, sizeof(struct lysc_type_str));
1840 break;
1841 case LY_TYPE_UNION:
1842 *type = calloc(1, sizeof(struct lysc_type_union));
1843 break;
1844 case LY_TYPE_INT8:
1845 case LY_TYPE_UINT8:
1846 case LY_TYPE_INT16:
1847 case LY_TYPE_UINT16:
1848 case LY_TYPE_INT32:
1849 case LY_TYPE_UINT32:
1850 case LY_TYPE_INT64:
1851 case LY_TYPE_UINT64:
1852 *type = calloc(1, sizeof(struct lysc_type_num));
1853 break;
1854 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001855 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001856 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1857 ret = LY_EVALID;
1858 goto cleanup;
1859 }
1860 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1861 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001862 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001863 ly_data_type2str[basetype]);
1864 free(*type);
1865 (*type) = NULL;
1866 ret = LY_EVALID;
1867 goto cleanup;
1868 }
1869
1870 /* get restrictions from the referred typedefs */
1871 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1872 tctx = (struct type_context *)tpdf_chain.objs[u];
1873
1874 /* remember the typedef context for circular check */
1875 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1876 LY_CHECK_GOTO(ret, cleanup);
1877
1878 if (tctx->tpdf->type.compiled) {
1879 base = tctx->tpdf->type.compiled;
1880 continue;
1881 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1882 /* no change, just use the type information from the base */
1883 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1884 ++base->refcount;
1885 continue;
1886 }
1887
1888 ++(*type)->refcount;
1889 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001890 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001891 tctx->tpdf->name, ly_data_type2str[basetype]);
1892 ret = LY_EVALID;
1893 goto cleanup;
1894 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001895 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001896 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1897 tctx->tpdf->name, tctx->tpdf->dflt.str);
1898 ret = LY_EVALID;
1899 goto cleanup;
1900 }
1901
1902 (*type)->basetype = basetype;
1903 /* TODO user type plugins */
1904 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1905 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001906 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001907 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1908 LY_CHECK_GOTO(ret, cleanup);
1909 base = prev_type;
1910 }
1911 /* remove the processed typedef contexts from the stack for circular check */
1912 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1913
1914 /* process the type definition in leaf */
1915 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1916 /* get restrictions from the node itself */
1917 (*type)->basetype = basetype;
1918 /* TODO user type plugins */
1919 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1920 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001921 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001922 LY_CHECK_GOTO(ret, cleanup);
1923 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1924 /* no specific restriction in leaf's type definition, copy from the base */
1925 free(*type);
1926 (*type) = base;
1927 ++(*type)->refcount;
1928 }
1929
Radek Krejciab430862021-03-02 20:13:40 +01001930 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001931
1932cleanup:
1933 ly_set_erase(&tpdf_chain, free);
1934 return ret;
1935}
1936
1937/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01001938 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
1939 */
1940#define LYS_STATUS_USES LYS_CONFIG_MASK
1941
1942/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001943 * @brief Compile status information of the given node.
1944 *
1945 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
1946 * has the status correctly set during the compilation.
1947 *
1948 * @param[in] ctx Compile context
1949 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
1950 * If the status was set explicitly on the node, it is already set in the flags value and we just check
1951 * the compatibility with the parent's status value.
1952 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
1953 * @return LY_ERR value.
1954 */
1955static LY_ERR
1956lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
1957{
1958 /* status - it is not inherited by specification, but it does not make sense to have
1959 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
1960 if (!((*node_flags) & LYS_STATUS_MASK)) {
1961 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001962 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001963 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01001964 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001965 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
1966 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1967 }
1968 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
1969 } else {
1970 (*node_flags) |= LYS_STATUS_CURR;
1971 }
1972 } else if (parent_flags & LYS_STATUS_MASK) {
1973 /* check status compatibility with the parent */
1974 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
1975 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001976 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001977 "A \"current\" status is in conflict with the parent's \"%s\" status.",
1978 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1979 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001980 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001981 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
1982 }
1983 return LY_EVALID;
1984 }
1985 }
1986 return LY_SUCCESS;
1987}
1988
1989/**
1990 * @brief Check uniqness of the node/action/notification name.
1991 *
1992 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
1993 * structures, but they share the namespace so we need to check their name collisions.
1994 *
1995 * @param[in] ctx Compile context.
1996 * @param[in] parent Parent of the nodes to check, can be NULL.
1997 * @param[in] name Name of the item to find in the given lists.
1998 * @param[in] exclude Node that was just added that should be excluded from the name checking.
1999 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2000 */
2001static LY_ERR
2002lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2003 const struct lysc_node *exclude)
2004{
2005 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002006 const struct lysc_node_action *actions;
2007 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002008 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002009 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002010
2011#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2012
2013 if (exclude->nodetype == LYS_CASE) {
2014 /* check restricted only to all the cases */
2015 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002016 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002017 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002018 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002019 return LY_EEXIST;
2020 }
2021 }
2022
2023 return LY_SUCCESS;
2024 }
2025
2026 /* no reason for our parent to be choice anymore */
2027 assert(!parent || (parent->nodetype != LYS_CHOICE));
2028
2029 if (parent && (parent->nodetype == LYS_CASE)) {
2030 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002031
2032 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2033 iter = lysc_data_parent(parent);
2034 do {
2035 parent = parent->parent;
2036 if (parent && (parent->nodetype == LYS_CHOICE)) {
2037 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2038 }
2039 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002040 }
2041
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002042 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002043 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2044 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2045 if (exclude->flags & LYS_IS_OUTPUT) {
2046 getnext_flags |= LYS_GETNEXT_OUTPUT;
2047 parent = lysc_node_child(parent)->next;
2048 } else {
2049 parent = lysc_node_child(parent);
2050 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002051 }
2052
2053 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002054 if (!parent && ctx->ext) {
2055 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2056 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2057 goto error;
2058 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002059
Radek Krejci5fa32a32021-02-08 18:12:38 +01002060 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2061 if (iter->nodetype == LYS_CHOICE) {
2062 iter2 = NULL;
2063 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2064 if (CHECK_NODE(iter2, exclude, name)) {
2065 goto error;
2066 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002067 }
2068 }
2069 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002070 } else {
2071 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2072 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2073 goto error;
2074 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002075
Radek Krejci5fa32a32021-02-08 18:12:38 +01002076 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2077 if (iter->nodetype == LYS_CHOICE) {
2078 iter2 = NULL;
2079 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2080 if (CHECK_NODE(iter2, exclude, name)) {
2081 goto error;
2082 }
2083 }
2084 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002085 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002086
Radek Krejci5fa32a32021-02-08 18:12:38 +01002087 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2088 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2089 if (CHECK_NODE(iter, exclude, name)) {
2090 goto error;
2091 }
2092 }
2093
2094 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2095 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2096 if (CHECK_NODE(iter, exclude, name)) {
2097 goto error;
2098 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002099 }
2100 }
Radek Krejci078e4342021-02-12 18:17:26 +01002101 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002102 return LY_SUCCESS;
2103
2104error:
Radek Krejci078e4342021-02-12 18:17:26 +01002105 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002106 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002107 return LY_EEXIST;
2108
2109#undef CHECK_NODE
2110}
2111
Radek Krejci2a9fc652021-01-22 17:44:34 +01002112/**
2113 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2114 * keep specific order of augments targetting the same node.
2115 *
2116 * @param[in] ctx Compile context
2117 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2118 * the choice itself is expected instead of a specific case node.
2119 * @param[in] node Schema node to connect into the list.
2120 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2121 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2122 */
2123static LY_ERR
2124lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002125{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002126 struct lysc_node **children, *anchor = NULL;
2127 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002128
Radek Krejci2a9fc652021-01-22 17:44:34 +01002129 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002130
Radek Krejci2a9fc652021-01-22 17:44:34 +01002131 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002132 if (node->nodetype == LYS_INPUT) {
2133 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2134 /* input node is part of the action but link it with output */
2135 node->next = &((struct lysc_node_action *)parent)->output.node;
2136 node->prev = node->next;
2137 return LY_SUCCESS;
2138 } else if (node->nodetype == LYS_OUTPUT) {
2139 /* output node is part of the action but link it with input */
2140 node->next = NULL;
2141 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002142 return LY_SUCCESS;
2143 } else if (node->nodetype == LYS_ACTION) {
2144 children = (struct lysc_node **)lysc_node_actions_p(parent);
2145 } else if (node->nodetype == LYS_NOTIF) {
2146 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2147 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002148 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002149 }
2150 assert(children);
2151
2152 if (!(*children)) {
2153 /* first child */
2154 *children = node;
2155 } else if (node->flags & LYS_KEY) {
2156 /* special handling of adding keys */
2157 assert(node->module == parent->module);
2158 anchor = *children;
2159 if (anchor->flags & LYS_KEY) {
2160 while ((anchor->flags & LYS_KEY) && anchor->next) {
2161 anchor = anchor->next;
2162 }
2163 /* insert after the last key */
2164 insert_after = 1;
2165 } /* else insert before anchor (at the beginning) */
2166 } else if ((*children)->prev->module == node->module) {
2167 /* last child is from the same module, keep the order and insert at the end */
2168 anchor = (*children)->prev;
2169 insert_after = 1;
2170 } else if (parent->module == node->module) {
2171 /* adding module child after some augments were connected */
2172 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2173 } else {
2174 /* some augments are already connected and we are connecting new ones,
2175 * keep module name order and insert the node into the children list */
2176 anchor = *children;
2177 do {
2178 anchor = anchor->prev;
2179
2180 /* check that we have not found the last augment node from our module or
2181 * the first augment node from a "smaller" module or
2182 * the first node from a local module */
2183 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2184 (anchor->module == parent->module)) {
2185 /* insert after */
2186 insert_after = 1;
2187 break;
2188 }
2189
2190 /* we have traversed all the nodes, insert before anchor (as the first node) */
2191 } while (anchor->prev->next);
2192 }
2193
2194 /* insert */
2195 if (anchor) {
2196 if (insert_after) {
2197 node->next = anchor->next;
2198 node->prev = anchor;
2199 anchor->next = node;
2200 if (node->next) {
2201 /* middle node */
2202 node->next->prev = node;
2203 } else {
2204 /* last node */
2205 (*children)->prev = node;
2206 }
2207 } else {
2208 node->next = anchor;
2209 node->prev = anchor->prev;
2210 anchor->prev = node;
2211 if (anchor == *children) {
2212 /* first node */
2213 *children = node;
2214 } else {
2215 /* middle node */
2216 node->prev->next = node;
2217 }
2218 }
2219 }
2220
2221 /* check the name uniqueness (even for an only child, it may be in case) */
2222 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2223 return LY_EEXIST;
2224 }
2225 } else {
2226 /* top-level element */
2227 struct lysc_node **list;
2228
Radek Krejci6b88a462021-02-17 12:39:34 +01002229 if (ctx->ext) {
2230 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2231 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002232 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2233 } else if (node->nodetype == LYS_NOTIF) {
2234 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2235 } else {
2236 list = &ctx->cur_mod->compiled->data;
2237 }
2238 if (!(*list)) {
2239 *list = node;
2240 } else {
2241 /* insert at the end of the module's top-level nodes list */
2242 (*list)->prev->next = node;
2243 node->prev = (*list)->prev;
2244 (*list)->prev = node;
2245 }
2246
2247 /* check the name uniqueness on top-level */
2248 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2249 return LY_EEXIST;
2250 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002251 }
2252
Radek Krejci2a9fc652021-01-22 17:44:34 +01002253 return LY_SUCCESS;
2254}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002255
Radek Krejci2a9fc652021-01-22 17:44:34 +01002256/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002257 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002258 *
2259 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002260 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002261 * @return LY_ERR value.
2262 */
2263static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002264lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002265{
2266 /* case never has any explicit config */
2267 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002268
Radek Krejci91531e12021-02-08 19:57:54 +01002269 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
2270 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002271 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002272 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2273 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002274 if (node->parent) {
2275 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002276 } else {
2277 /* default is config true */
2278 node->flags |= LYS_CONFIG_W;
2279 }
2280 } else {
2281 /* config set explicitly */
2282 node->flags |= LYS_SET_CONFIG;
2283 }
2284
Radek Krejci91531e12021-02-08 19:57:54 +01002285 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002286 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002287 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002288 }
2289
Radek Krejci2a9fc652021-01-22 17:44:34 +01002290 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002291}
2292
Radek Krejci91531e12021-02-08 19:57:54 +01002293/**
2294 * @brief Set various flags of the compiled nodes
2295 *
2296 * @param[in] ctx Compile context.
2297 * @param[in] node Compiled node where the flags will be set.
2298 * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags).
2299 * Zero means no uses, non-zero value with no status bit set mean the default status.
2300 */
2301static LY_ERR
2302lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2303{
2304 /* inherit config flags */
2305 LY_CHECK_RET(lys_compile_config(ctx, node));
2306
2307 /* status - it is not inherited by specification, but it does not make sense to have
2308 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2309 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (node->parent ? node->parent->flags : 0)));
2310
2311 /* other flags */
2312 if ((ctx->options & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
2313 node->flags |= LYS_IS_INPUT;
2314 } else if ((ctx->options & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
2315 node->flags |= LYS_IS_OUTPUT;
2316 } else if ((ctx->options & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
2317 node->flags |= LYS_IS_NOTIF;
2318 }
2319
2320 return LY_SUCCESS;
2321}
2322
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002323LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002324lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2325 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2326 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002327{
2328 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002329 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002330 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002331 struct lysp_when *pwhen = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002332
Radek Krejci2a9fc652021-01-22 17:44:34 +01002333 node->nodetype = pnode->nodetype;
2334 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002335 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002336 node->prev = node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002337
Radek Krejci2a9fc652021-01-22 17:44:34 +01002338 /* compile any deviations for this node */
2339 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002340 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002341 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002342 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002343 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002344 }
2345
Radek Krejci2a9fc652021-01-22 17:44:34 +01002346 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002347
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002348 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002349 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Radek Krejciaf0548b2021-02-15 15:11:22 +01002350 if (!enabled && !(ctx->options & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002351 ly_set_add(&ctx->disabled, node, 1, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002352 ctx->options |= LYS_COMPILE_DISABLED;
2353 }
2354
Radek Krejci91531e12021-02-08 19:57:54 +01002355 /* config, status and other flags */
2356 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002357 LY_CHECK_GOTO(ret, error);
2358
2359 /* list ordering */
2360 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002361 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002362 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002363 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
2364 (ctx->options & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002365 node->flags &= ~LYS_ORDBY_MASK;
2366 node->flags |= LYS_ORDBY_SYSTEM;
2367 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2368 /* default ordering is system */
2369 node->flags |= LYS_ORDBY_SYSTEM;
2370 }
2371 }
2372
Radek Krejci2a9fc652021-01-22 17:44:34 +01002373 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2374 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2375 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2376
2377 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2378 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2379
Radek Krejci9a3823e2021-01-27 20:26:46 +01002380 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002381 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002382 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002383 LY_CHECK_GOTO(ret, cleanup);
2384 }
2385
2386 /* connect any augments */
2387 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2388
2389 /* nodetype-specific part */
2390 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2391
2392 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002393 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002394 if (node->flags & LYS_MAND_TRUE) {
2395 /* inherit LYS_MAND_TRUE in parent containers */
2396 lys_compile_mandatory_parents(parent, 1);
2397 }
2398
2399 if (child_set) {
2400 /* add the new node into set */
2401 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2402 }
2403
2404 goto cleanup;
2405
2406error:
2407 lysc_node_free(ctx->ctx, node, 0);
2408cleanup:
2409 if (ret && dev_pnode) {
2410 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2411 }
2412 lysp_dev_node_free(ctx->ctx, dev_pnode);
2413 return ret;
2414}
2415
2416/**
2417 * @brief Compile parsed action's input/output node information.
2418 * @param[in] ctx Compile context
2419 * @param[in] pnode Parsed inout node.
2420 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2421 * is enriched with the inout-specific information.
2422 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2423 */
2424LY_ERR
2425lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2426{
2427 LY_ERR ret = LY_SUCCESS;
2428 struct lysp_node *child_p;
2429 uint32_t prev_options = ctx->options;
2430
2431 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2432 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2433
2434 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002435 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Radek Krejci91531e12021-02-08 19:57:54 +01002436 ctx->options |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002437
Radek Krejci01180ac2021-01-27 08:48:22 +01002438 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002439 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2440 }
2441
2442 ctx->options = prev_options;
2443
2444done:
2445 return ret;
2446}
2447
2448/**
2449 * @brief Compile parsed action node information.
2450 * @param[in] ctx Compile context
2451 * @param[in] pnode Parsed action node.
2452 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2453 * is enriched with the action-specific information.
2454 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2455 */
2456LY_ERR
2457lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2458{
2459 LY_ERR ret;
2460 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2461 struct lysc_node_action *action = (struct lysc_node_action *)node;
2462 struct lysp_node_action_inout *input, implicit_input = {
2463 .nodetype = LYS_INPUT,
2464 .name = "input",
2465 .parent = pnode,
2466 };
2467 struct lysp_node_action_inout *output, implicit_output = {
2468 .nodetype = LYS_OUTPUT,
2469 .name = "output",
2470 .parent = pnode,
2471 };
2472
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002473 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002474 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002475 if (action_p->input.nodetype == LYS_UNKNOWN) {
2476 input = &implicit_input;
2477 } else {
2478 input = &action_p->input;
2479 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002480 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 +01002481 lysc_update_path(ctx, NULL, NULL);
2482 LY_CHECK_GOTO(ret, done);
2483
2484 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002485 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002486 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002487 output = &implicit_output;
2488 } else {
2489 output = &action_p->output;
2490 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002491 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 +01002492 lysc_update_path(ctx, NULL, NULL);
2493 LY_CHECK_GOTO(ret, done);
2494
2495 /* do not check "must" semantics in a grouping */
2496 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2497 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2498 LY_CHECK_GOTO(ret, done);
2499 }
2500
2501done:
2502 return ret;
2503}
2504
2505/**
2506 * @brief Compile parsed action node information.
2507 * @param[in] ctx Compile context
2508 * @param[in] pnode Parsed action node.
2509 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2510 * is enriched with the action-specific information.
2511 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2512 */
2513LY_ERR
2514lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2515{
2516 LY_ERR ret = LY_SUCCESS;
2517 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2518 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2519 struct lysp_node *child_p;
2520
2521 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002522 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002523 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002524 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002525 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002526 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002527
Radek Krejci01180ac2021-01-27 08:48:22 +01002528 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002529 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002530 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002531 }
2532
Radek Krejci2a9fc652021-01-22 17:44:34 +01002533done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002534 return ret;
2535}
2536
2537/**
2538 * @brief Compile parsed container node information.
2539 * @param[in] ctx Compile context
2540 * @param[in] pnode Parsed container node.
2541 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2542 * is enriched with the container-specific information.
2543 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2544 */
2545static LY_ERR
2546lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2547{
2548 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2549 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2550 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002551 LY_ERR ret = LY_SUCCESS;
2552
2553 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002554 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002555 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002556 }
2557
2558 /* more cases when the container has meaning but is kept NP for convenience:
2559 * - when condition
2560 * - direct child action/notification
2561 */
2562
2563 LY_LIST_FOR(cont_p->child, child_p) {
2564 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2565 LY_CHECK_GOTO(ret, done);
2566 }
2567
Michal Vasko5347e3a2020-11-03 17:14:57 +01002568 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002569 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002570 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002571 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002572 LY_CHECK_GOTO(ret, done);
2573 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002574
2575 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2576 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2577 LY_CHECK_GOTO(ret, done);
2578 }
2579 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2580 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2581 LY_CHECK_GOTO(ret, done);
2582 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002583
2584done:
2585 return ret;
2586}
2587
Michal Vasko72244882021-01-12 15:21:05 +01002588/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002589 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2590 * @param[in] ctx Compile context.
2591 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2592 * @param[in] type_p Parsed type to compile.
2593 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2594 * @return LY_ERR value.
2595 */
2596static LY_ERR
2597lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2598 struct lysc_node_leaf *leaf)
2599{
2600 struct lysp_qname *dflt;
2601
Michal Vaskoa99b3572021-02-01 11:54:58 +01002602 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002603 leaf->units ? NULL : &leaf->units, &dflt));
2604
2605 /* store default value, if any */
2606 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2607 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2608 }
2609
Michal Vasko12606ee2020-11-25 17:05:11 +01002610 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002611 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002612 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002613 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002614 LY_ARRAY_COUNT_TYPE u;
2615 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2616 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2617 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002618 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002619 }
2620 }
2621 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2622 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002623 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002624 return LY_EVALID;
2625 }
2626 }
2627
2628 return LY_SUCCESS;
2629}
2630
2631/**
2632 * @brief Compile parsed leaf node information.
2633 * @param[in] ctx Compile context
2634 * @param[in] pnode Parsed leaf node.
2635 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2636 * is enriched with the leaf-specific information.
2637 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2638 */
2639static LY_ERR
2640lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2641{
2642 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2643 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002644 LY_ERR ret = LY_SUCCESS;
2645
Michal Vasko5347e3a2020-11-03 17:14:57 +01002646 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002647 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002648 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002649 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002650 LY_CHECK_GOTO(ret, done);
2651 }
2652 if (leaf_p->units) {
2653 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2654 leaf->flags |= LYS_SET_UNITS;
2655 }
2656
2657 /* compile type */
2658 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2659 LY_CHECK_GOTO(ret, done);
2660
2661 /* store/update default value */
2662 if (leaf_p->dflt.str) {
2663 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2664 leaf->flags |= LYS_SET_DFLT;
2665 }
2666
2667 /* checks */
2668 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002669 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002670 "Invalid mandatory leaf with a default value.");
2671 return LY_EVALID;
2672 }
2673
2674done:
2675 return ret;
2676}
2677
2678/**
2679 * @brief Compile parsed leaf-list node information.
2680 * @param[in] ctx Compile context
2681 * @param[in] pnode Parsed leaf-list node.
2682 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2683 * is enriched with the leaf-list-specific information.
2684 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2685 */
2686static LY_ERR
2687lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2688{
2689 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2690 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002691 LY_ERR ret = LY_SUCCESS;
2692
Michal Vasko5347e3a2020-11-03 17:14:57 +01002693 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002694 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002695 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002696 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002697 LY_CHECK_GOTO(ret, done);
2698 }
2699 if (llist_p->units) {
2700 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2701 llist->flags |= LYS_SET_UNITS;
2702 }
2703
2704 /* compile type */
2705 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2706 LY_CHECK_GOTO(ret, done);
2707
2708 /* store/update default values */
2709 if (llist_p->dflts) {
2710 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002711 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002712 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2713 return LY_EVALID;
2714 }
2715
2716 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2717 llist->flags |= LYS_SET_DFLT;
2718 }
2719
2720 llist->min = llist_p->min;
2721 if (llist->min) {
2722 llist->flags |= LYS_MAND_TRUE;
2723 }
2724 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2725
2726 /* checks */
2727 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002728 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 +02002729 return LY_EVALID;
2730 }
2731
2732 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002733 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002734 llist->min, llist->max);
2735 return LY_EVALID;
2736 }
2737
2738done:
2739 return ret;
2740}
2741
2742LY_ERR
2743lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2744 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2745 const struct lysc_node **target, uint16_t *result_flag)
2746{
2747 LY_ERR ret = LY_EVALID;
2748 const char *name, *prefix, *id;
2749 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002750 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002751 const char *nodeid_type;
2752 uint32_t getnext_extra_flag = 0;
2753 uint16_t current_nodetype = 0;
2754
2755 assert(nodeid);
2756 assert(target);
2757 assert(result_flag);
2758 *target = NULL;
2759 *result_flag = 0;
2760
2761 id = nodeid;
2762
2763 if (ctx_node) {
2764 /* descendant-schema-nodeid */
2765 nodeid_type = "descendant";
2766
2767 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002768 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002769 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002770 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002771 return LY_EVALID;
2772 }
2773 } else {
2774 /* absolute-schema-nodeid */
2775 nodeid_type = "absolute";
2776
2777 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002778 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002779 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002780 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002781 return LY_EVALID;
2782 }
2783 ++id;
2784 }
2785
2786 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2787 if (prefix) {
2788 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2789 if (!mod) {
2790 /* module must always be found */
2791 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002792 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002793 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002794 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002795 return LY_ENOTFOUND;
2796 }
2797 } else {
2798 switch (format) {
2799 case LY_PREF_SCHEMA:
2800 case LY_PREF_SCHEMA_RESOLVED:
2801 /* use the current module */
2802 mod = cur_mod;
2803 break;
2804 case LY_PREF_JSON:
2805 if (!ctx_node) {
2806 LOGINT_RET(ctx->ctx);
2807 }
2808 /* inherit the module of the previous context node */
2809 mod = ctx_node->module;
2810 break;
2811 case LY_PREF_XML:
2812 /* not really defined */
2813 LOGINT_RET(ctx->ctx);
2814 }
2815 }
2816
2817 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2818 /* move through input/output manually */
2819 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002820 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2821 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002822 return LY_ENOTFOUND;
2823 }
2824 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002825 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002826 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002827 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002828 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2829 } else {
2830 /* only input or output is valid */
2831 ctx_node = NULL;
2832 }
2833 } else {
2834 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002835 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002836 getnext_extra_flag = 0;
2837 }
2838 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002839 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2840 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002841 return LY_ENOTFOUND;
2842 }
2843 current_nodetype = ctx_node->nodetype;
2844
2845 if (current_nodetype == LYS_NOTIF) {
2846 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2847 } else if (current_nodetype == LYS_INPUT) {
2848 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2849 } else if (current_nodetype == LYS_OUTPUT) {
2850 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2851 }
2852
2853 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2854 break;
2855 }
2856 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002857 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002858 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002859 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002860 return LY_EVALID;
2861 }
2862 ++id;
2863 }
2864
2865 if (ret == LY_SUCCESS) {
2866 *target = ctx_node;
2867 if (nodetype && !(current_nodetype & nodetype)) {
2868 return LY_EDENIED;
2869 }
2870 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002871 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002872 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002873 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002874 }
2875
2876 return ret;
2877}
2878
2879/**
2880 * @brief Compile information about list's uniques.
2881 * @param[in] ctx Compile context.
2882 * @param[in] uniques Sized array list of unique statements.
2883 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2884 * @return LY_ERR value.
2885 */
2886static LY_ERR
2887lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2888{
2889 LY_ERR ret = LY_SUCCESS;
2890 struct lysc_node_leaf **key, ***unique;
2891 struct lysc_node *parent;
2892 const char *keystr, *delim;
2893 size_t len;
2894 LY_ARRAY_COUNT_TYPE v;
2895 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2896 uint16_t flags;
2897
2898 LY_ARRAY_FOR(uniques, v) {
2899 config = -1;
2900 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2901 keystr = uniques[v].str;
2902 while (keystr) {
2903 delim = strpbrk(keystr, " \t\n");
2904 if (delim) {
2905 len = delim - keystr;
2906 while (isspace(*delim)) {
2907 ++delim;
2908 }
2909 } else {
2910 len = strlen(keystr);
2911 }
2912
2913 /* unique node must be present */
2914 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002915 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002916 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2917 if (ret != LY_SUCCESS) {
2918 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002919 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002920 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002921 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002922 }
2923 return LY_EVALID;
2924 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002925 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002926 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01002927 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002928 return LY_EVALID;
2929 }
2930
2931 /* all referenced leafs must be of the same config type */
2932 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2933 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002934 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002935 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2936 return LY_EVALID;
2937 } else if ((*key)->flags & LYS_CONFIG_W) {
2938 config = 1;
2939 } else { /* LYS_CONFIG_R */
2940 config = 0;
2941 }
2942
2943 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
2944 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
2945 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002946 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002947 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
2948 return LY_EVALID;
2949 }
2950 }
2951
2952 /* check status */
2953 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2954 (*key)->flags, (*key)->module, (*key)->name));
2955
2956 /* mark leaf as unique */
2957 (*key)->flags |= LYS_UNIQUE;
2958
2959 /* next unique value in line */
2960 keystr = delim;
2961 }
2962 /* next unique definition */
2963 }
2964
2965 return LY_SUCCESS;
2966}
2967
2968/**
2969 * @brief Compile parsed list node information.
2970 * @param[in] ctx Compile context
2971 * @param[in] pnode Parsed list node.
2972 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2973 * is enriched with the list-specific information.
2974 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2975 */
2976static LY_ERR
2977lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2978{
2979 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
2980 struct lysc_node_list *list = (struct lysc_node_list *)node;
2981 struct lysp_node *child_p;
2982 struct lysc_node_leaf *key, *prev_key = NULL;
2983 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002984 const char *keystr, *delim;
2985 LY_ERR ret = LY_SUCCESS;
2986
2987 list->min = list_p->min;
2988 if (list->min) {
2989 list->flags |= LYS_MAND_TRUE;
2990 }
2991 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2992
2993 LY_LIST_FOR(list_p->child, child_p) {
2994 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
2995 }
2996
Michal Vasko5347e3a2020-11-03 17:14:57 +01002997 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002998 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002999 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003000 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003001 }
3002
3003 /* keys */
3004 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003005 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003006 return LY_EVALID;
3007 }
3008
3009 /* find all the keys (must be direct children) */
3010 keystr = list_p->key;
3011 if (!keystr) {
3012 /* keyless list */
3013 list->flags |= LYS_KEYLESS;
3014 }
3015 while (keystr) {
3016 delim = strpbrk(keystr, " \t\n");
3017 if (delim) {
3018 len = delim - keystr;
3019 while (isspace(*delim)) {
3020 ++delim;
3021 }
3022 } else {
3023 len = strlen(keystr);
3024 }
3025
3026 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003027 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3028 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003029 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003030 return LY_EVALID;
3031 }
3032 /* keys must be unique */
3033 if (key->flags & LYS_KEY) {
3034 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003035 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003036 return LY_EVALID;
3037 }
3038
Radek Krejcia6016992021-03-03 10:13:41 +01003039 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003040 /* key must have the same config flag as the list itself */
3041 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003042 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003043 return LY_EVALID;
3044 }
3045 if (ctx->pmod->version < LYS_VERSION_1_1) {
3046 /* YANG 1.0 denies key to be of empty type */
3047 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003048 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003049 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3050 return LY_EVALID;
3051 }
3052 } else {
3053 /* when and if-feature are illegal on list keys */
3054 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003055 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003056 "List's key must not have any \"when\" statement.");
3057 return LY_EVALID;
3058 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003059 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003060 }
3061
3062 /* check status */
3063 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3064 key->flags, key->module, key->name));
3065
3066 /* ignore default values of the key */
3067 if (key->dflt) {
3068 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3069 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3070 free(key->dflt);
3071 key->dflt = NULL;
3072 }
3073 /* mark leaf as key */
3074 key->flags |= LYS_KEY;
3075
3076 /* move it to the correct position */
3077 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3078 /* fix links in closest previous siblings of the key */
3079 if (key->next) {
3080 key->next->prev = key->prev;
3081 } else {
3082 /* last child */
3083 list->child->prev = key->prev;
3084 }
3085 if (key->prev->next) {
3086 key->prev->next = key->next;
3087 }
3088 /* fix links in the key */
3089 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003090 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003091 key->next = prev_key->next;
3092 } else {
3093 key->prev = list->child->prev;
3094 key->next = list->child;
3095 }
3096 /* fix links in closes future siblings of the key */
3097 if (prev_key) {
3098 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003099 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003100 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003101 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003102 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003103 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003104 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003105 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003106 }
3107 /* fix links in parent */
3108 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003109 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003110 }
3111 }
3112
3113 /* next key value */
3114 prev_key = key;
3115 keystr = delim;
3116 lysc_update_path(ctx, NULL, NULL);
3117 }
3118
3119 /* uniques */
3120 if (list_p->uniques) {
3121 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3122 }
3123
Radek Krejci2a9fc652021-01-22 17:44:34 +01003124 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3125 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3126 LY_CHECK_GOTO(ret, done);
3127 }
3128 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3129 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3130 LY_CHECK_GOTO(ret, done);
3131 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003132
3133 /* checks */
3134 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003135 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003136 list->min, list->max);
3137 return LY_EVALID;
3138 }
3139
3140done:
3141 return ret;
3142}
3143
3144/**
3145 * @brief Do some checks and set the default choice's case.
3146 *
3147 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3148 *
3149 * @param[in] ctx Compile context.
3150 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3151 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3152 * @return LY_ERR value.
3153 */
3154static LY_ERR
3155lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3156{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003157 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003158 const struct lys_module *mod;
3159 const char *prefix = NULL, *name;
3160 size_t prefix_len = 0;
3161
3162 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3163 name = strchr(dflt->str, ':');
3164 if (name) {
3165 prefix = dflt->str;
3166 prefix_len = name - prefix;
3167 ++name;
3168 } else {
3169 name = dflt->str;
3170 }
3171 if (prefix) {
3172 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
3173 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003174 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003175 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003176 return LY_EVALID;
3177 }
3178 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003179 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003180 }
3181
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003182 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 +02003183 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003184 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003185 "Default case \"%s\" not found.", dflt->str);
3186 return LY_EVALID;
3187 }
3188
3189 /* no mandatory nodes directly under the default case */
3190 LY_LIST_FOR(ch->dflt->child, iter) {
3191 if (iter->parent != (struct lysc_node *)ch->dflt) {
3192 break;
3193 }
3194 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003195 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003196 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3197 return LY_EVALID;
3198 }
3199 }
3200
3201 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003202 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003203 return LY_EVALID;
3204 }
3205
3206 ch->dflt->flags |= LYS_SET_DFLT;
3207 return LY_SUCCESS;
3208}
3209
3210LY_ERR
3211lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3212 struct ly_set *child_set)
3213{
3214 LY_ERR ret = LY_SUCCESS;
3215 struct lysp_node *child_p_next = child_p->next;
3216 struct lysp_node_case *cs_p;
3217
3218 if (child_p->nodetype == LYS_CASE) {
3219 /* standard case under choice */
3220 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3221 } else {
3222 /* we need the implicit case first, so create a fake parsed case */
3223 cs_p = calloc(1, sizeof *cs_p);
3224 cs_p->nodetype = LYS_CASE;
3225 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3226 cs_p->child = child_p;
3227
3228 /* make the child the only case child */
3229 child_p->next = NULL;
3230
3231 /* compile it normally */
3232 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3233
3234free_fake_node:
3235 /* free the fake parsed node and correct pointers back */
3236 cs_p->child = NULL;
3237 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3238 child_p->next = child_p_next;
3239 }
3240
3241 return ret;
3242}
3243
3244/**
3245 * @brief Compile parsed choice node information.
3246 *
3247 * @param[in] ctx Compile context
3248 * @param[in] pnode Parsed choice node.
3249 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3250 * is enriched with the choice-specific information.
3251 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3252 */
3253static LY_ERR
3254lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3255{
3256 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3257 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3258 struct lysp_node *child_p;
3259 LY_ERR ret = LY_SUCCESS;
3260
3261 assert(node->nodetype == LYS_CHOICE);
3262
3263 LY_LIST_FOR(ch_p->child, child_p) {
3264 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3265 }
3266
3267 /* default branch */
3268 if (ch_p->dflt.str) {
3269 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3270 }
3271
3272 return ret;
3273}
3274
3275/**
3276 * @brief Compile parsed anydata or anyxml node information.
3277 * @param[in] ctx Compile context
3278 * @param[in] pnode Parsed anydata or anyxml node.
3279 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3280 * is enriched with the any-specific information.
3281 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3282 */
3283static LY_ERR
3284lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3285{
3286 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3287 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003288 LY_ERR ret = LY_SUCCESS;
3289
Michal Vasko5347e3a2020-11-03 17:14:57 +01003290 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003291 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003292 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003293 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003294 LY_CHECK_GOTO(ret, done);
3295 }
3296
Radek Krejci91531e12021-02-08 19:57:54 +01003297 if (any->flags & LYS_CONFIG_W) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003298 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3299 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3300 }
3301done:
3302 return ret;
3303}
3304
3305/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003306 * @brief Prepare the case structure in choice node for the new data node.
3307 *
3308 * 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
3309 * created in the choice when the first child was processed.
3310 *
3311 * @param[in] ctx Compile context.
3312 * @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,
3313 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3314 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3315 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3316 * @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,
3317 * it is linked from the case structure only in case it is its first child.
3318 */
3319static LY_ERR
3320lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3321{
3322 struct lysp_node *child_p;
3323 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3324
3325 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3326 /* we have to add an implicit case node into the parent choice */
3327 } else if (pnode->nodetype == LYS_CASE) {
3328 /* explicit parent case */
3329 LY_LIST_FOR(cs_p->child, child_p) {
3330 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3331 }
3332 } else {
3333 LOGINT_RET(ctx->ctx);
3334 }
3335
3336 return LY_SUCCESS;
3337}
3338
3339void
3340lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3341{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003342 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003343
3344 if (add) { /* set flag */
3345 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3346 parent = parent->parent) {
3347 parent->flags |= LYS_MAND_TRUE;
3348 }
3349 } else { /* unset flag */
3350 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003351 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003352 if (iter->flags & LYS_MAND_TRUE) {
3353 /* there is another mandatory node */
3354 return;
3355 }
3356 }
3357 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3358 parent->flags &= ~LYS_MAND_TRUE;
3359 }
3360 }
3361}
3362
3363/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003364 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003365 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003366 * @param[in] name Name of the grouping to find,
3367 * @return NULL when there is no grouping with the specified name
3368 * @return Pointer to the grouping of the specified @p name.
3369 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003370static struct lysp_node_grp *
3371match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003372{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003373 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003374
Radek Krejci2a9fc652021-01-22 17:44:34 +01003375 LY_LIST_FOR(grps, grp) {
3376 if (!strcmp(grp->name, name)) {
3377 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003378 }
3379 }
3380
3381 return NULL;
3382}
3383
3384/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003385 * @brief Find grouping for a uses.
3386 *
3387 * @param[in] ctx Compile context.
3388 * @param[in] uses_p Parsed uses node.
3389 * @param[out] gpr_p Found grouping on success.
3390 * @param[out] grp_pmod Module of @p grp_p on success.
3391 * @return LY_ERR value.
3392 */
3393static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003394lys_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 +02003395 struct lysp_module **grp_pmod)
3396{
3397 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003398 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003399 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003400 const char *id, *name, *prefix, *local_pref;
3401 size_t prefix_len, name_len;
3402 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003403 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003404
3405 *grp_p = NULL;
3406 *grp_pmod = NULL;
3407
3408 /* search for the grouping definition */
3409 id = uses_p->name;
3410 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3411 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3412 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3413 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003414 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003415 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003416 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3417 found = ctx->pmod;
3418 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003419 }
3420 }
3421 } else {
3422 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003423 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003424 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003425 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003426 "Invalid prefix used for grouping reference.", uses_p->name);
3427 return LY_EVALID;
3428 }
3429 pmod = mod->parsed;
3430 }
3431
3432 if (!found) {
3433 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003434 if ((grp = match_grouping(pmod->groupings, name))) {
3435 found = pmod;
3436 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003437 /* ... and all the submodules */
3438 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003439 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3440 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003441 break;
3442 }
3443 }
3444 }
3445 }
3446 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003447 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003448 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3449 return LY_EVALID;
3450 }
3451
3452 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3453 /* remember that the grouping is instantiated to avoid its standalone validation */
3454 grp->flags |= LYS_USED_GRP;
3455 }
3456
3457 *grp_p = grp;
3458 *grp_pmod = found;
3459 return LY_SUCCESS;
3460}
3461
3462/**
3463 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3464 * If present, also apply uses's modificators.
3465 *
3466 * @param[in] ctx Compile context
3467 * @param[in] uses_p Parsed uses schema node.
3468 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3469 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3470 * the compile context.
3471 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3472 */
3473static LY_ERR
3474lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3475{
3476 struct lysp_node *pnode;
3477 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003478 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003479 uint32_t i, grp_stack_count;
3480 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3481 LY_ERR ret = LY_SUCCESS;
3482 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003483 struct ly_set uses_child_set = {0};
3484
3485 /* find the referenced grouping */
3486 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3487
3488 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3489 grp_stack_count = ctx->groupings.count;
3490 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3491 if (grp_stack_count == ctx->groupings.count) {
3492 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003493 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003494 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3495 return LY_EVALID;
3496 }
3497
3498 /* check status */
3499 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3500 LY_CHECK_GOTO(ret, cleanup);
3501
3502 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3503 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3504 LY_CHECK_GOTO(ret, cleanup);
3505
3506 /* switch context's parsed module being processed */
3507 ctx->pmod = grp_mod;
3508
3509 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003510 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003511 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3512 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003513 LY_CHECK_GOTO(ret, cleanup);
3514 }
3515
3516 if (child_set) {
3517 /* add these children to our compiled child_set as well since uses is a schema-only node */
3518 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3519 }
3520
3521 if (uses_p->when) {
3522 /* pass uses's when to all the data children */
3523 for (i = 0; i < uses_child_set.count; ++i) {
3524 child = uses_child_set.snodes[i];
3525
Michal Vasko72244882021-01-12 15:21:05 +01003526 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003527 LY_CHECK_GOTO(ret, cleanup);
3528 }
3529 }
3530
3531 /* compile actions */
3532 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003533 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003534 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3535 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003536 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003537 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3538 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003539 ret = LY_EVALID;
3540 goto cleanup;
3541 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003542 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3543 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3544 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3545 LY_CHECK_GOTO(ret, cleanup);
3546 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003547
3548 if (uses_p->when) {
3549 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003550 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3551 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003552 LY_CHECK_GOTO(ret, cleanup);
3553 }
3554 }
3555 }
3556
3557 /* compile notifications */
3558 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003559 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003560 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3561 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003562 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003563 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3564 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003565 ret = LY_EVALID;
3566 goto cleanup;
3567 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003568
3569 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3570 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3571 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3572 LY_CHECK_GOTO(ret, cleanup);
3573 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003574
3575 if (uses_p->when) {
3576 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003577 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3578 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003579 LY_CHECK_GOTO(ret, cleanup);
3580 }
3581 }
3582 }
3583
3584 /* check that all augments were applied */
3585 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003586 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3587 /* augment of some parent uses, irrelevant now */
3588 continue;
3589 }
3590
3591 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003592 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3593 ret = LY_ENOTFOUND;
3594 }
3595 LY_CHECK_GOTO(ret, cleanup);
3596
3597 /* check that all refines were applied */
3598 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003599 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3600 /* refine of some paretn uses, irrelevant now */
3601 continue;
3602 }
3603
3604 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003605 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3606 ret = LY_ENOTFOUND;
3607 }
3608 LY_CHECK_GOTO(ret, cleanup);
3609
3610cleanup:
3611 /* reload previous context's parsed module being processed */
3612 ctx->pmod = mod_old;
3613
3614 /* remove the grouping from the stack for circular groupings dependency check */
3615 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3616 assert(ctx->groupings.count == grp_stack_count);
3617
3618 ly_set_erase(&uses_child_set, NULL);
3619 return ret;
3620}
3621
3622static int
3623lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3624{
3625 struct lysp_node *iter;
3626 int len = 0;
3627
3628 *path = NULL;
3629 for (iter = node; iter && len >= 0; iter = iter->parent) {
3630 char *s = *path;
3631 char *id;
3632
3633 switch (iter->nodetype) {
3634 case LYS_USES:
3635 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3636 break;
3637 case LYS_GROUPING:
3638 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3639 break;
3640 case LYS_AUGMENT:
3641 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3642 break;
3643 default:
3644 id = strdup(iter->name);
3645 break;
3646 }
3647
3648 if (!iter->parent) {
3649 /* print prefix */
3650 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3651 } else {
3652 /* prefix is the same as in parent */
3653 len = asprintf(path, "/%s%s", id, s ? s : "");
3654 }
3655 free(s);
3656 free(id);
3657 }
3658
3659 if (len < 0) {
3660 free(*path);
3661 *path = NULL;
3662 } else if (len == 0) {
3663 *path = strdup("/");
3664 len = 1;
3665 }
3666 return len;
3667}
3668
3669LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003670lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003671{
3672 LY_ERR ret;
3673 char *path;
3674 int len;
3675
3676 struct lysp_node_uses fake_uses = {
3677 .parent = pnode,
3678 .nodetype = LYS_USES,
3679 .flags = 0, .next = NULL,
3680 .name = grp->name,
3681 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3682 .refines = NULL, .augments = NULL
3683 };
3684 struct lysc_node_container fake_container = {
3685 .nodetype = LYS_CONTAINER,
3686 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3687 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003688 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003689 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003690 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003691 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003692 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3693 };
3694
3695 if (grp->parent) {
3696 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3697 }
3698
3699 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3700 if (len < 0) {
3701 LOGMEM(ctx->ctx);
3702 return LY_EMEM;
3703 }
3704 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3705 ctx->path_len = (uint32_t)len;
3706 free(path);
3707
3708 lysc_update_path(ctx, NULL, "{grouping}");
3709 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003710 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003711 lysc_update_path(ctx, NULL, NULL);
3712 lysc_update_path(ctx, NULL, NULL);
3713
3714 ctx->path_len = 1;
3715 ctx->path[1] = '\0';
3716
3717 /* cleanup */
3718 lysc_node_container_free(ctx->ctx, &fake_container);
3719
3720 return ret;
3721}
3722
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003723LY_ERR
3724lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3725 struct ly_set *child_set)
3726{
3727 LY_ERR ret = LY_SUCCESS;
3728 struct lysc_node *node = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003729 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003730
3731 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3732
3733 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003734 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003735 } else {
3736 lysc_update_path(ctx, NULL, "{uses}");
3737 lysc_update_path(ctx, NULL, pnode->name);
3738 }
3739
3740 switch (pnode->nodetype) {
3741 case LYS_CONTAINER:
3742 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3743 node_compile_spec = lys_compile_node_container;
3744 break;
3745 case LYS_LEAF:
3746 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3747 node_compile_spec = lys_compile_node_leaf;
3748 break;
3749 case LYS_LIST:
3750 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3751 node_compile_spec = lys_compile_node_list;
3752 break;
3753 case LYS_LEAFLIST:
3754 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3755 node_compile_spec = lys_compile_node_leaflist;
3756 break;
3757 case LYS_CHOICE:
3758 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3759 node_compile_spec = lys_compile_node_choice;
3760 break;
3761 case LYS_CASE:
3762 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3763 node_compile_spec = lys_compile_node_case;
3764 break;
3765 case LYS_ANYXML:
3766 case LYS_ANYDATA:
3767 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3768 node_compile_spec = lys_compile_node_any;
3769 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003770 case LYS_RPC:
3771 case LYS_ACTION:
Radek Krejci91531e12021-02-08 19:57:54 +01003772 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003773 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3774 "Action \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003775 (ctx->options & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003776 return LY_EVALID;
3777 }
3778 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3779 node_compile_spec = lys_compile_node_action;
Radek Krejci91531e12021-02-08 19:57:54 +01003780 ctx->options |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003781 break;
3782 case LYS_NOTIF:
Radek Krejci91531e12021-02-08 19:57:54 +01003783 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003784 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3785 "Notification \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003786 (ctx->options & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003787 return LY_EVALID;
3788 }
3789 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3790 node_compile_spec = lys_compile_node_notif;
3791 ctx->options |= LYS_COMPILE_NOTIFICATION;
3792 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003793 case LYS_USES:
3794 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3795 lysc_update_path(ctx, NULL, NULL);
3796 lysc_update_path(ctx, NULL, NULL);
3797 return ret;
3798 default:
3799 LOGINT(ctx->ctx);
3800 return LY_EINT;
3801 }
3802 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3803
Radek Krejci2a9fc652021-01-22 17:44:34 +01003804 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003805
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003806 ctx->options = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003807 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003808 return ret;
3809}