blob: 8571aca1d41836443235dfb15d514cc54017bd9c [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"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020040#include "tree_schema.h"
41#include "tree_schema_internal.h"
42#include "xpath.h"
43
44static struct lysc_ext_instance *
45lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
46{
47 /* TODO - extensions, increase refcount */
48 (void) ctx;
49 (void) orig;
50 return NULL;
51}
52
53/**
54 * @brief Add/replace a leaf default value in unres.
55 * Can also be used for a single leaf-list default value.
56 *
57 * @param[in] ctx Compile context.
58 * @param[in] leaf Leaf with the default value.
59 * @param[in] dflt Default value to use.
60 * @return LY_ERR value.
61 */
62static LY_ERR
63lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
64{
65 struct lysc_unres_dflt *r = NULL;
66 uint32_t i;
67
Michal Vasko12606ee2020-11-25 17:05:11 +010068 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010069 return LY_SUCCESS;
70 }
71
Michal Vasko405cc9e2020-12-01 12:01:27 +010072 for (i = 0; i < ctx->unres->dflts.count; ++i) {
73 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020074 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010075 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020076 lysp_qname_free(ctx->ctx, r->dflt);
77 free(r->dflt);
78 break;
79 }
80 }
81 if (!r) {
82 /* add new unres item */
83 r = calloc(1, sizeof *r);
84 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
85 r->leaf = leaf;
86
Michal Vasko405cc9e2020-12-01 12:01:27 +010087 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020088 }
89
90 r->dflt = malloc(sizeof *r->dflt);
91 LY_CHECK_GOTO(!r->dflt, error);
92 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
93
94 return LY_SUCCESS;
95
96error:
97 free(r->dflt);
98 LOGMEM(ctx->ctx);
99 return LY_EMEM;
100}
101
102/**
103 * @brief Add/replace a leaf-list default value(s) in unres.
104 *
105 * @param[in] ctx Compile context.
106 * @param[in] llist Leaf-list with the default value.
107 * @param[in] dflts Sized array of the default values.
108 * @return LY_ERR value.
109 */
110static LY_ERR
111lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
112{
113 struct lysc_unres_dflt *r = NULL;
114 uint32_t i;
115
Michal Vasko12606ee2020-11-25 17:05:11 +0100116 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100117 return LY_SUCCESS;
118 }
119
Michal Vasko405cc9e2020-12-01 12:01:27 +0100120 for (i = 0; i < ctx->unres->dflts.count; ++i) {
121 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200122 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100123 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200124 lysp_qname_free(ctx->ctx, r->dflt);
125 free(r->dflt);
126 r->dflt = NULL;
127 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
128 r->dflts = NULL;
129 break;
130 }
131 }
132 if (!r) {
133 r = calloc(1, sizeof *r);
134 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
135 r->llist = llist;
136
Michal Vasko405cc9e2020-12-01 12:01:27 +0100137 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200138 }
139
140 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
141
142 return LY_SUCCESS;
143}
144
145/**
146 * @brief Duplicate the compiled pattern structure.
147 *
148 * Instead of duplicating memory, the reference counter in the @p orig is increased.
149 *
150 * @param[in] orig The pattern structure to duplicate.
151 * @return The duplicated structure to use.
152 */
153static struct lysc_pattern *
154lysc_pattern_dup(struct lysc_pattern *orig)
155{
156 ++orig->refcount;
157 return orig;
158}
159
160/**
161 * @brief Duplicate the array of compiled patterns.
162 *
163 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
164 *
165 * @param[in] ctx Libyang context for logging.
166 * @param[in] orig The patterns sized array to duplicate.
167 * @return New sized array as a copy of @p orig.
168 * @return NULL in case of memory allocation error.
169 */
170static struct lysc_pattern **
171lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
172{
173 struct lysc_pattern **dup = NULL;
174 LY_ARRAY_COUNT_TYPE u;
175
176 assert(orig);
177
178 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
179 LY_ARRAY_FOR(orig, u) {
180 dup[u] = lysc_pattern_dup(orig[u]);
181 LY_ARRAY_INCREMENT(dup);
182 }
183 return dup;
184}
185
186/**
187 * @brief Duplicate compiled range structure.
188 *
189 * @param[in] ctx Libyang context for logging.
190 * @param[in] orig The range structure to be duplicated.
191 * @return New compiled range structure as a copy of @p orig.
192 * @return NULL in case of memory allocation error.
193 */
194static struct lysc_range *
195lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
196{
197 struct lysc_range *dup;
198 LY_ERR ret;
199
200 assert(orig);
201
202 dup = calloc(1, sizeof *dup);
203 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
204 if (orig->parts) {
205 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
206 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
207 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
208 }
209 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
210 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
211 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
212
213 return dup;
214cleanup:
215 free(dup);
216 (void) ret; /* set but not used due to the return type */
217 return NULL;
218}
219
220struct lysc_node *
221lysc_xpath_context(struct lysc_node *start)
222{
223 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
224 start = start->parent) {}
225 return start;
226}
227
228/**
229 * @brief Compile information from the when statement
230 *
231 * @param[in] ctx Compile context.
232 * @param[in] when_p Parsed when structure.
233 * @param[in] flags Flags of the parsed node with the when statement.
234 * @param[in] ctx_node Context node for the when statement.
235 * @param[out] when Pointer where to store pointer to the created compiled when structure.
236 * @return LY_ERR value.
237 */
238static LY_ERR
239lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
240 struct lysc_when **when)
241{
242 LY_ERR ret = LY_SUCCESS;
243
244 *when = calloc(1, sizeof **when);
245 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
246 (*when)->refcount = 1;
247 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
248 LY_CHECK_RET(lysc_prefixes_compile(when_p->cond, strlen(when_p->cond), ctx->pmod, &(*when)->prefixes));
249 (*when)->context = (struct lysc_node *)ctx_node;
250 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
251 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
252 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
253 (*when)->flags = flags & LYS_STATUS_MASK;
254
255done:
256 return ret;
257}
258
259LY_ERR
260lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
261 struct lysc_node *node, struct lysc_when **when_c)
262{
263 struct lysc_when **new_when, ***node_when;
264
265 assert(when_p);
266
267 /* get the when array */
268 if (node->nodetype & LYS_ACTION) {
269 node_when = &((struct lysc_action *)node)->when;
270 } else if (node->nodetype == LYS_NOTIF) {
271 node_when = &((struct lysc_notif *)node)->when;
272 } else {
273 node_when = &node->when;
274 }
275
276 /* create new when pointer */
277 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
278 if (!when_c || !(*when_c)) {
279 /* compile when */
280 LY_CHECK_RET(lys_compile_when_(ctx, when_p, flags, ctx_node, new_when));
281
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200282 /* remember the compiled when for sharing */
283 if (when_c) {
284 *when_c = *new_when;
285 }
286 } else {
287 /* use the previously compiled when */
288 ++(*when_c)->refcount;
289 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100290 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200291
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100292 if (!(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
293 /* do not check "when" semantics in a grouping, but repeat the check for different node because
294 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100295 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200296 }
297
298 return LY_SUCCESS;
299}
300
301/**
302 * @brief Compile information from the must statement
303 * @param[in] ctx Compile context.
304 * @param[in] must_p The parsed must statement structure.
305 * @param[in,out] must Prepared (empty) compiled must structure to fill.
306 * @return LY_ERR value.
307 */
308static LY_ERR
309lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
310{
311 LY_ERR ret = LY_SUCCESS;
312
313 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
314 LY_CHECK_RET(lysc_prefixes_compile(must_p->arg.str, strlen(must_p->arg.str), must_p->arg.mod, &must->prefixes));
315 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
316 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
317 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
318 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
319 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
320
321done:
322 return ret;
323}
324
325/**
326 * @brief Validate and normalize numeric value from a range definition.
327 * @param[in] ctx Compile context.
328 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
329 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
330 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
331 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
332 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
333 * @param[in] value String value of the range boundary.
334 * @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.
335 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
336 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
337 */
338static LY_ERR
339range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
340 size_t *len, char **valcopy)
341{
342 size_t fraction = 0, size;
343
344 *len = 0;
345
346 assert(value);
347 /* parse value */
348 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
349 return LY_EVALID;
350 }
351
352 if ((value[*len] == '-') || (value[*len] == '+')) {
353 ++(*len);
354 }
355
356 while (isdigit(value[*len])) {
357 ++(*len);
358 }
359
360 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
361 if (basetype == LY_TYPE_DEC64) {
362 goto decimal;
363 } else {
364 *valcopy = strndup(value, *len);
365 return LY_SUCCESS;
366 }
367 }
368 fraction = *len;
369
370 ++(*len);
371 while (isdigit(value[*len])) {
372 ++(*len);
373 }
374
375 if (basetype == LY_TYPE_DEC64) {
376decimal:
377 assert(frdigits);
378 if (fraction && (*len - 1 - fraction > frdigits)) {
379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
380 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
381 *len, value, frdigits);
382 return LY_EINVAL;
383 }
384 if (fraction) {
385 size = (*len) + (frdigits - ((*len) - 1 - fraction));
386 } else {
387 size = (*len) + frdigits + 1;
388 }
389 *valcopy = malloc(size * sizeof **valcopy);
390 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
391
392 (*valcopy)[size - 1] = '\0';
393 if (fraction) {
394 memcpy(&(*valcopy)[0], &value[0], fraction);
395 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
396 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
397 } else {
398 memcpy(&(*valcopy)[0], &value[0], *len);
399 memset(&(*valcopy)[*len], '0', frdigits);
400 }
401 }
402 return LY_SUCCESS;
403}
404
405/**
406 * @brief Check that values in range are in ascendant order.
407 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
408 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
409 * max can be also equal.
410 * @param[in] value Current value to check.
411 * @param[in] prev_value The last seen value.
412 * @return LY_SUCCESS or LY_EEXIST for invalid order.
413 */
414static LY_ERR
415range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
416{
417 if (unsigned_value) {
418 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
419 return LY_EEXIST;
420 }
421 } else {
422 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
423 return LY_EEXIST;
424 }
425 }
426 return LY_SUCCESS;
427}
428
429/**
430 * @brief Set min/max value of the range part.
431 * @param[in] ctx Compile context.
432 * @param[in] part Range part structure to fill.
433 * @param[in] max Flag to distinguish if storing min or max value.
434 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
435 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
436 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
437 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
438 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
439 * @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.
440 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
441 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
442 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
443 * frdigits value), LY_EMEM.
444 */
445static LY_ERR
446range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
447 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
448{
449 LY_ERR ret = LY_SUCCESS;
450 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100451 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200452
453 if (value) {
454 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
455 LY_CHECK_GOTO(ret, finalize);
456 }
457 if (!valcopy && base_range) {
458 if (max) {
459 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
460 } else {
461 part->min_64 = base_range->parts[0].min_64;
462 }
463 if (!first) {
464 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
465 }
466 goto finalize;
467 }
468
469 switch (basetype) {
470 case LY_TYPE_INT8: /* range */
471 if (valcopy) {
472 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
473 } else if (max) {
474 part->max_64 = INT64_C(127);
475 } else {
476 part->min_64 = INT64_C(-128);
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_INT16: /* range */
483 if (valcopy) {
484 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
485 } else if (max) {
486 part->max_64 = INT64_C(32767);
487 } else {
488 part->min_64 = INT64_C(-32768);
489 }
490 if (!ret && !first) {
491 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
492 }
493 break;
494 case LY_TYPE_INT32: /* range */
495 if (valcopy) {
496 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
497 } else if (max) {
498 part->max_64 = INT64_C(2147483647);
499 } else {
500 part->min_64 = INT64_C(-2147483648);
501 }
502 if (!ret && !first) {
503 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
504 }
505 break;
506 case LY_TYPE_INT64: /* range */
507 case LY_TYPE_DEC64: /* range */
508 if (valcopy) {
509 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
510 max ? &part->max_64 : &part->min_64);
511 } else if (max) {
512 part->max_64 = INT64_C(9223372036854775807);
513 } else {
514 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
515 }
516 if (!ret && !first) {
517 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
518 }
519 break;
520 case LY_TYPE_UINT8: /* range */
521 if (valcopy) {
522 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
523 } else if (max) {
524 part->max_u64 = UINT64_C(255);
525 } else {
526 part->min_u64 = UINT64_C(0);
527 }
528 if (!ret && !first) {
529 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
530 }
531 break;
532 case LY_TYPE_UINT16: /* range */
533 if (valcopy) {
534 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
535 } else if (max) {
536 part->max_u64 = UINT64_C(65535);
537 } else {
538 part->min_u64 = UINT64_C(0);
539 }
540 if (!ret && !first) {
541 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
542 }
543 break;
544 case LY_TYPE_UINT32: /* range */
545 if (valcopy) {
546 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
547 } else if (max) {
548 part->max_u64 = UINT64_C(4294967295);
549 } else {
550 part->min_u64 = UINT64_C(0);
551 }
552 if (!ret && !first) {
553 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
554 }
555 break;
556 case LY_TYPE_UINT64: /* range */
557 case LY_TYPE_STRING: /* length */
558 case LY_TYPE_BINARY: /* length */
559 if (valcopy) {
560 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
561 } else if (max) {
562 part->max_u64 = UINT64_C(18446744073709551615);
563 } else {
564 part->min_u64 = UINT64_C(0);
565 }
566 if (!ret && !first) {
567 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
568 }
569 break;
570 default:
571 LOGINT(ctx->ctx);
572 ret = LY_EINT;
573 }
574
575finalize:
576 if (ret == LY_EDENIED) {
577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
578 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
579 length_restr ? "length" : "range", valcopy ? valcopy : *value);
580 } else if (ret == LY_EVALID) {
581 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
582 "Invalid %s restriction - invalid value \"%s\".",
583 length_restr ? "length" : "range", valcopy ? valcopy : *value);
584 } else if (ret == LY_EEXIST) {
585 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
586 "Invalid %s restriction - values are not in ascending order (%s).",
587 length_restr ? "length" : "range",
588 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
589 } else if (!ret && value) {
590 *value = *value + len;
591 }
592 free(valcopy);
593 return ret;
594}
595
596/**
597 * @brief Compile the parsed range restriction.
598 * @param[in] ctx Compile context.
599 * @param[in] range_p Parsed range structure to compile.
600 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
601 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
602 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
603 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
604 * range restriction must be more restrictive than the base_range.
605 * @param[in,out] range Pointer to the created current range structure.
606 * @return LY_ERR value.
607 */
608static LY_ERR
609lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
610 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
611{
612 LY_ERR ret = LY_EVALID;
613 const char *expr;
614 struct lysc_range_part *parts = NULL, *part;
615 ly_bool range_expected = 0, uns;
616 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
617
618 assert(range);
619 assert(range_p);
620
621 expr = range_p->arg.str;
622 while (1) {
623 if (isspace(*expr)) {
624 ++expr;
625 } else if (*expr == '\0') {
626 if (range_expected) {
627 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
628 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
629 length_restr ? "length" : "range", range_p->arg);
630 goto cleanup;
631 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
632 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
633 "Invalid %s restriction - unexpected end of the expression (%s).",
634 length_restr ? "length" : "range", range_p->arg);
635 goto cleanup;
636 }
637 parts_done++;
638 break;
639 } else if (!strncmp(expr, "min", 3)) {
640 if (parts) {
641 /* min cannot be used elsewhere than in the first part */
642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
643 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
644 expr - range_p->arg.str, range_p->arg.str);
645 goto cleanup;
646 }
647 expr += 3;
648
649 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
650 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
651 part->max_64 = part->min_64;
652 } else if (*expr == '|') {
653 if (!parts || range_expected) {
654 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
655 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
656 goto cleanup;
657 }
658 expr++;
659 parts_done++;
660 /* process next part of the expression */
661 } else if (!strncmp(expr, "..", 2)) {
662 expr += 2;
663 while (isspace(*expr)) {
664 expr++;
665 }
666 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
668 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
669 goto cleanup;
670 }
671 /* continue expecting the upper boundary */
672 range_expected = 1;
673 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
674 /* number */
675 if (range_expected) {
676 part = &parts[LY_ARRAY_COUNT(parts) - 1];
677 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
678 range_expected = 0;
679 } else {
680 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
681 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
682 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
683 part->max_64 = part->min_64;
684 }
685
686 /* continue with possible another expression part */
687 } else if (!strncmp(expr, "max", 3)) {
688 expr += 3;
689 while (isspace(*expr)) {
690 expr++;
691 }
692 if (*expr != '\0') {
693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
694 length_restr ? "length" : "range", expr);
695 goto cleanup;
696 }
697 if (range_expected) {
698 part = &parts[LY_ARRAY_COUNT(parts) - 1];
699 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
700 range_expected = 0;
701 } else {
702 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
703 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
704 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
705 part->min_64 = part->max_64;
706 }
707 } else {
708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
709 length_restr ? "length" : "range", expr);
710 goto cleanup;
711 }
712 }
713
714 /* check with the previous range/length restriction */
715 if (base_range) {
716 switch (basetype) {
717 case LY_TYPE_BINARY:
718 case LY_TYPE_UINT8:
719 case LY_TYPE_UINT16:
720 case LY_TYPE_UINT32:
721 case LY_TYPE_UINT64:
722 case LY_TYPE_STRING:
723 uns = 1;
724 break;
725 case LY_TYPE_DEC64:
726 case LY_TYPE_INT8:
727 case LY_TYPE_INT16:
728 case LY_TYPE_INT32:
729 case LY_TYPE_INT64:
730 uns = 0;
731 break;
732 default:
733 LOGINT(ctx->ctx);
734 ret = LY_EINT;
735 goto cleanup;
736 }
737 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
738 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
739 goto baseerror;
740 }
741 /* current lower bound is not lower than the base */
742 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
743 /* base has single value */
744 if (base_range->parts[v].min_64 == parts[u].min_64) {
745 /* both lower bounds are the same */
746 if (parts[u].min_64 != parts[u].max_64) {
747 /* current continues with a range */
748 goto baseerror;
749 } else {
750 /* equal single values, move both forward */
751 ++v;
752 continue;
753 }
754 } else {
755 /* base is single value lower than current range, so the
756 * value from base range is removed in the current,
757 * move only base and repeat checking */
758 ++v;
759 --u;
760 continue;
761 }
762 } else {
763 /* base is the range */
764 if (parts[u].min_64 == parts[u].max_64) {
765 /* current is a single value */
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 /* current is behind the base range, so base range is omitted,
768 * move the base and keep the current for further check */
769 ++v;
770 --u;
771 } /* else it is within the base range, so move the current, but keep the base */
772 continue;
773 } else {
774 /* both are ranges - check the higher bound, the lower was already checked */
775 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
776 /* higher bound is higher than the current higher bound */
777 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
778 /* but the current lower bound is also higher, so the base range is omitted,
779 * continue with the same current, but move the base */
780 --u;
781 ++v;
782 continue;
783 }
784 /* current range starts within the base range but end behind it */
785 goto baseerror;
786 } else {
787 /* current range is smaller than the base,
788 * move current, but stay with the base */
789 continue;
790 }
791 }
792 }
793 }
794 if (u != parts_done) {
795baseerror:
796 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
797 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
798 length_restr ? "length" : "range", range_p->arg);
799 goto cleanup;
800 }
801 }
802
803 if (!(*range)) {
804 *range = calloc(1, sizeof **range);
805 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
806 }
807
808 /* we rewrite the following values as the types chain is being processed */
809 if (range_p->eapptag) {
810 lydict_remove(ctx->ctx, (*range)->eapptag);
811 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
812 }
813 if (range_p->emsg) {
814 lydict_remove(ctx->ctx, (*range)->emsg);
815 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
816 }
817 if (range_p->dsc) {
818 lydict_remove(ctx->ctx, (*range)->dsc);
819 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
820 }
821 if (range_p->ref) {
822 lydict_remove(ctx->ctx, (*range)->ref);
823 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
824 }
825 /* extensions are taken only from the last range by the caller */
826
827 (*range)->parts = parts;
828 parts = NULL;
829 ret = LY_SUCCESS;
830cleanup:
831 LY_ARRAY_FREE(parts);
832
833 return ret;
834}
835
836LY_ERR
837lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
838{
839 size_t idx, idx2, start, end, size, brack;
840 char *perl_regex, *ptr;
841 int err_code;
842 const char *orig_ptr;
843 PCRE2_SIZE err_offset;
844 pcre2_code *code_local;
845
846#define URANGE_LEN 19
847 char *ublock2urange[][2] = {
848 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
849 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
850 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
851 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
852 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
853 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
854 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
855 {"Greek", "[\\x{0370}-\\x{03FF}]"},
856 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
857 {"Armenian", "[\\x{0530}-\\x{058F}]"},
858 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
859 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
860 {"Syriac", "[\\x{0700}-\\x{074F}]"},
861 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
862 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
863 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
864 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
865 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
866 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
867 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
868 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
869 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
870 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
871 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
872 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
873 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
874 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
875 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
876 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
877 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
878 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
879 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
880 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
881 {"Ogham", "[\\x{1680}-\\x{169F}]"},
882 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
883 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
884 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
885 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
886 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
887 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
888 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
889 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
890 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
891 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
892 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
893 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
894 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
895 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
896 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
897 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
898 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
899 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
900 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
901 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
902 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
903 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
904 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
905 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
906 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
907 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
908 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
909 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
910 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
911 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
912 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
913 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
914 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
915 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
916 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
917 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
918 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
919 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
920 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
921 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
922 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
923 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
924 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
925 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
926 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
927 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
928 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
929 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
930 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
931 {NULL, NULL}
932 };
933
934 /* adjust the expression to a Perl equivalent
935 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
936
937 /* allocate space for the transformed pattern */
938 size = strlen(pattern) + 1;
939 perl_regex = malloc(size);
940 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
941 perl_regex[0] = '\0';
942
943 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
944 brack = 0;
945 idx = 0;
946 orig_ptr = pattern;
947 while (orig_ptr[0]) {
948 switch (orig_ptr[0]) {
949 case '$':
950 case '^':
951 if (!brack) {
952 /* make space for the extra character */
953 ++size;
954 perl_regex = ly_realloc(perl_regex, size);
955 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
956
957 /* print escape slash */
958 perl_regex[idx] = '\\';
959 ++idx;
960 }
961 break;
962 case '[':
963 /* must not be escaped */
964 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
965 ++brack;
966 }
967 break;
968 case ']':
969 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
970 /* pattern was checked and compiled already */
971 assert(brack);
972 --brack;
973 }
974 break;
975 default:
976 break;
977 }
978
979 /* copy char */
980 perl_regex[idx] = orig_ptr[0];
981
982 ++idx;
983 ++orig_ptr;
984 }
985 perl_regex[idx] = '\0';
986
987 /* substitute Unicode Character Blocks with exact Character Ranges */
988 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
989 start = ptr - perl_regex;
990
991 ptr = strchr(ptr, '}');
992 if (!ptr) {
993 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
994 pattern, perl_regex + start + 2, "unterminated character property");
995 free(perl_regex);
996 return LY_EVALID;
997 }
998 end = (ptr - perl_regex) + 1;
999
1000 /* need more space */
1001 if (end - start < URANGE_LEN) {
1002 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1003 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1004 }
1005
1006 /* find our range */
1007 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1008 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1009 break;
1010 }
1011 }
1012 if (!ublock2urange[idx][0]) {
1013 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
1014 pattern, perl_regex + start + 5, "unknown block name");
1015 free(perl_regex);
1016 return LY_EVALID;
1017 }
1018
1019 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1020 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1021 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1022 ++idx;
1023 }
1024 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1025 --idx;
1026 }
1027 }
1028 if (idx) {
1029 /* skip brackets */
1030 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1031 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1032 } else {
1033 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1034 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1035 }
1036 }
1037
1038 /* must return 0, already checked during parsing */
1039 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1040 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1041 &err_code, &err_offset, NULL);
1042 if (!code_local) {
1043 PCRE2_UCHAR err_msg[256] = {0};
1044 pcre2_get_error_message(err_code, err_msg, 256);
1045 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1046 free(perl_regex);
1047 return LY_EVALID;
1048 }
1049 free(perl_regex);
1050
1051 if (code) {
1052 *code = code_local;
1053 } else {
1054 free(code_local);
1055 }
1056
1057 return LY_SUCCESS;
1058
1059#undef URANGE_LEN
1060}
1061
1062/**
1063 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1064 * @param[in] ctx Compile context.
1065 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1066 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1067 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1068 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1069 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1070 */
1071static LY_ERR
1072lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1073 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1074{
1075 struct lysc_pattern **pattern;
1076 LY_ARRAY_COUNT_TYPE u;
1077 LY_ERR ret = LY_SUCCESS;
1078
1079 /* first, copy the patterns from the base type */
1080 if (base_patterns) {
1081 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1082 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1083 }
1084
1085 LY_ARRAY_FOR(patterns_p, u) {
1086 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1087 *pattern = calloc(1, sizeof **pattern);
1088 ++(*pattern)->refcount;
1089
1090 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg.str[1], &(*pattern)->code);
1091 LY_CHECK_RET(ret);
1092
1093 if (patterns_p[u].arg.str[0] == 0x15) {
1094 (*pattern)->inverted = 1;
1095 }
1096 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1097 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1098 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1099 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1100 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
1101 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
1102 }
1103done:
1104 return ret;
1105}
1106
1107/**
1108 * @brief map of the possible restrictions combination for the specific built-in type.
1109 */
1110static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1111 0 /* LY_TYPE_UNKNOWN */,
1112 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1113 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1114 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1115 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1116 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1117 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1118 LYS_SET_BIT /* LY_TYPE_BITS */,
1119 0 /* LY_TYPE_BOOL */,
1120 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1121 0 /* LY_TYPE_EMPTY */,
1122 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1123 LYS_SET_BASE /* LY_TYPE_IDENT */,
1124 LYS_SET_REQINST /* LY_TYPE_INST */,
1125 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1126 LYS_SET_TYPE /* LY_TYPE_UNION */,
1127 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1128 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1129 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1130 LYS_SET_RANGE /* LY_TYPE_INT64 */
1131};
1132
1133/**
1134 * @brief stringification of the YANG built-in data types
1135 */
1136const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
1137 "unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1138 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1139 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
1140};
1141
1142/**
1143 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1144 * @param[in] ctx Compile context.
1145 * @param[in] enums_p Array of the parsed enum structures to compile.
1146 * @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.
1147 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1148 * @param[out] enums Newly created array of the compiled enums information for the current type.
1149 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1150 */
1151static LY_ERR
1152lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
1153 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
1154{
1155 LY_ERR ret = LY_SUCCESS;
1156 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001157 int32_t highest_value = INT32_MIN, cur_val;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001158 uint32_t highest_position = 0, cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001159 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001160 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001161
1162 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
1163 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1164 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1165 return LY_EVALID;
1166 }
1167
1168 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001169 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001170 if (base_enums) {
1171 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1172 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001173 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001174 break;
1175 }
1176 }
1177 if (v == LY_ARRAY_COUNT(base_enums)) {
1178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1179 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001180 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001181 return LY_EVALID;
1182 }
1183 match = v;
1184 }
1185
1186 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001187 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001188 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001189 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001190 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001191 LY_ARRAY_FOR(*enums, v) {
1192 if (cur_val == (*enums)[v].value) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001193 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1194 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001195 cur_val, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001196 return LY_EVALID;
1197 }
1198 }
1199 } else if (base_enums) {
1200 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001201 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001202 } else {
1203 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001204 if (u == 0) {
1205 cur_val = 0;
1206 } else if (highest_value == INT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1208 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001209 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001210 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001211 } else {
1212 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001213 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001214 }
1215
1216 /* save highest value for auto assing */
1217 if (highest_value < cur_val) {
1218 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001219 }
1220 } else { /* LY_TYPE_BITS */
1221 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001222 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001223 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001224 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001225 LY_ARRAY_FOR(*enums, v) {
1226 if (cur_pos == (*enums)[v].position) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001227 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1228 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001229 cur_pos, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001230 return LY_EVALID;
1231 }
1232 }
1233 } else if (base_enums) {
1234 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001235 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001236 } else {
1237 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001238 if (u == 0) {
1239 cur_pos = 0;
1240 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001241 /* counter overflow */
1242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1243 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001244 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001245 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001246 } else {
1247 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001248 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001249 }
1250
1251 /* save highest position for auto assing */
1252 if (highest_position < cur_pos) {
1253 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 }
1255 }
1256
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001257 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001258 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001259 if (basetype == LY_TYPE_ENUM) {
1260 if (cur_val != base_enums[match].value) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1262 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001263 enums_p[u].name, base_enums[match].value, cur_val);
1264 return LY_EVALID;
1265 }
1266 } else {
1267 if (cur_pos != base_enums[match].position) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001268 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1269 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001270 enums_p[u].name, base_enums[match].position, cur_pos);
1271 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001272 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001273 }
1274 }
1275
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001276 /* evaluate if-ffeatures */
1277 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1278 if (!enabled) {
1279 continue;
1280 }
1281
1282 /* add new enum/bit */
1283 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1284 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1285 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1286 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
1287 e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) | (basetype == LY_TYPE_ENUM ? LYS_ISENUM : 0);
1288 if (basetype == LY_TYPE_ENUM) {
1289 e->value = cur_val;
1290 } else {
1291 e->position = cur_pos;
1292 }
1293 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM :
1294 LYEXT_PAR_TYPE_BIT, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001295
1296 if (basetype == LY_TYPE_BITS) {
1297 /* keep bits ordered by position */
1298 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
1299 if (v != u) {
1300 memcpy(&storage, e, sizeof *e);
1301 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1302 memcpy(&(*enums)[v], &storage, sizeof storage);
1303 }
1304 }
1305 }
1306
1307done:
1308 return ret;
1309}
1310
1311/**
1312 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1313 * @param[in] ctx Compile context.
1314 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1315 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
1316 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
1317 * @param[in] context_name Name of the context node or referencing typedef for logging.
1318 * @param[in] type_p Parsed type to compile.
1319 * @param[in] basetype Base YANG built-in type of the type to compile.
1320 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1321 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1322 * @param[out] type Newly created type structure with the filled information about the type.
1323 * @return LY_ERR value.
1324 */
1325static LY_ERR
1326lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
1327 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, LY_DATA_TYPE basetype,
1328 const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
1329{
1330 LY_ERR ret = LY_SUCCESS;
1331 struct lysc_type_bin *bin;
1332 struct lysc_type_num *num;
1333 struct lysc_type_str *str;
1334 struct lysc_type_bits *bits;
1335 struct lysc_type_enum *enumeration;
1336 struct lysc_type_dec *dec;
1337 struct lysc_type_identityref *idref;
1338 struct lysc_type_leafref *lref;
1339 struct lysc_type_union *un, *un_aux;
1340
1341 switch (basetype) {
1342 case LY_TYPE_BINARY:
1343 bin = (struct lysc_type_bin *)(*type);
1344
1345 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1346 if (type_p->length) {
1347 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1348 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1349 if (!tpdfname) {
1350 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, cleanup);
1351 }
1352 }
1353 break;
1354 case LY_TYPE_BITS:
1355 /* RFC 7950 9.7 - bits */
1356 bits = (struct lysc_type_bits *)(*type);
1357 if (type_p->bits) {
1358 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1359 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1360 (struct lysc_type_bitenum_item **)&bits->bits));
1361 }
1362
1363 if (!base && !type_p->flags) {
1364 /* type derived from bits built-in type must contain at least one bit */
1365 if (tpdfname) {
1366 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
1367 } else {
1368 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
1369 }
1370 return LY_EVALID;
1371 }
1372 break;
1373 case LY_TYPE_DEC64:
1374 dec = (struct lysc_type_dec *)(*type);
1375
1376 /* RFC 7950 9.3.4 - fraction-digits */
1377 if (!base) {
1378 if (!type_p->fraction_digits) {
1379 if (tpdfname) {
1380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
1381 } else {
1382 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
1383 }
1384 return LY_EVALID;
1385 }
1386 dec->fraction_digits = type_p->fraction_digits;
1387 } else {
1388 if (type_p->fraction_digits) {
1389 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1390 if (tpdfname) {
1391 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1392 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1393 tpdfname);
1394 } else {
1395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1396 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1397 }
1398 return LY_EVALID;
1399 }
1400 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1401 }
1402
1403 /* RFC 7950 9.2.4 - range */
1404 if (type_p->range) {
1405 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1406 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1407 if (!tpdfname) {
1408 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, cleanup);
1409 }
1410 }
1411 break;
1412 case LY_TYPE_STRING:
1413 str = (struct lysc_type_str *)(*type);
1414
1415 /* RFC 7950 9.4.4 - length */
1416 if (type_p->length) {
1417 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1418 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1419 if (!tpdfname) {
1420 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, cleanup);
1421 }
1422 } else if (base && ((struct lysc_type_str *)base)->length) {
1423 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1424 }
1425
1426 /* RFC 7950 9.4.5 - pattern */
1427 if (type_p->patterns) {
1428 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1429 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1430 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1431 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1432 }
1433 break;
1434 case LY_TYPE_ENUM:
1435 enumeration = (struct lysc_type_enum *)(*type);
1436
1437 /* RFC 7950 9.6 - enum */
1438 if (type_p->enums) {
1439 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1440 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1441 }
1442
1443 if (!base && !type_p->flags) {
1444 /* type derived from enumerations built-in type must contain at least one enum */
1445 if (tpdfname) {
1446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
1447 } else {
1448 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
1449 }
1450 return LY_EVALID;
1451 }
1452 break;
1453 case LY_TYPE_INT8:
1454 case LY_TYPE_UINT8:
1455 case LY_TYPE_INT16:
1456 case LY_TYPE_UINT16:
1457 case LY_TYPE_INT32:
1458 case LY_TYPE_UINT32:
1459 case LY_TYPE_INT64:
1460 case LY_TYPE_UINT64:
1461 num = (struct lysc_type_num *)(*type);
1462
1463 /* RFC 6020 9.2.4 - range */
1464 if (type_p->range) {
1465 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1466 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1467 if (!tpdfname) {
1468 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, cleanup);
1469 }
1470 }
1471 break;
1472 case LY_TYPE_IDENT:
1473 idref = (struct lysc_type_identityref *)(*type);
1474
1475 /* RFC 7950 9.10.2 - base */
1476 if (type_p->bases) {
1477 if (base) {
1478 /* only the directly derived identityrefs can contain base specification */
1479 if (tpdfname) {
1480 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1481 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1482 tpdfname);
1483 } else {
1484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1485 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1486 }
1487 return LY_EVALID;
1488 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001489 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 +02001490 }
1491
1492 if (!base && !type_p->flags) {
1493 /* type derived from identityref built-in type must contain at least one base */
1494 if (tpdfname) {
1495 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
1496 } else {
1497 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
1498 }
1499 return LY_EVALID;
1500 }
1501 break;
1502 case LY_TYPE_LEAFREF:
1503 lref = (struct lysc_type_leafref *)*type;
1504
1505 /* RFC 7950 9.9.3 - require-instance */
1506 if (type_p->flags & LYS_SET_REQINST) {
1507 if (context_mod->version < LYS_VERSION_1_1) {
1508 if (tpdfname) {
1509 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1510 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1511 } else {
1512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1513 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1514 }
1515 return LY_EVALID;
1516 }
1517 lref->require_instance = type_p->require_instance;
1518 } else if (base) {
1519 /* inherit */
1520 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1521 } else {
1522 /* default is true */
1523 lref->require_instance = 1;
1524 }
1525 if (type_p->path) {
1526 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
1527 LY_CHECK_RET(lysc_prefixes_compile(type_p->path->expr, strlen(type_p->path->expr), type_p->pmod,
1528 &lref->prefixes));
1529 } else if (base) {
1530 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
1531 LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)base)->prefixes, &lref->prefixes));
1532 } else if (tpdfname) {
1533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
1534 return LY_EVALID;
1535 } else {
1536 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
1537 return LY_EVALID;
1538 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001539 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001540 break;
1541 case LY_TYPE_INST:
1542 /* RFC 7950 9.9.3 - require-instance */
1543 if (type_p->flags & LYS_SET_REQINST) {
1544 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1545 } else {
1546 /* default is true */
1547 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1548 }
1549 break;
1550 case LY_TYPE_UNION:
1551 un = (struct lysc_type_union *)(*type);
1552
1553 /* RFC 7950 7.4 - type */
1554 if (type_p->types) {
1555 if (base) {
1556 /* only the directly derived union can contain types specification */
1557 if (tpdfname) {
1558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1559 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1560 tpdfname);
1561 } else {
1562 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1563 "Invalid type substatement for the type not directly derived from union built-in type.");
1564 }
1565 return LY_EVALID;
1566 }
1567 /* compile the type */
1568 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
1569 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
1570 LY_CHECK_RET(lys_compile_type(ctx, context_pnode, context_flags, context_mod, context_name,
1571 &type_p->types[u], &un->types[u + additional], NULL, NULL));
1572 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
1573 /* add space for additional types from the union subtype */
1574 un_aux = (struct lysc_type_union *)un->types[u + additional];
1575 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
1576 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
1577
1578 /* copy subtypes of the subtype union */
1579 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1580 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1581 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1582 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1583 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx); lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
1584 lref = (struct lysc_type_leafref *)un->types[u + additional];
1585
1586 lref->basetype = LY_TYPE_LEAFREF;
1587 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path));
1588 lref->refcount = 1;
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001589 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001590 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
1591 LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)un_aux->types[v])->prefixes,
1592 &lref->prefixes));
1593 /* TODO extensions */
1594
1595 } else {
1596 un->types[u + additional] = un_aux->types[v];
1597 ++un_aux->types[v]->refcount;
1598 }
1599 ++additional;
1600 LY_ARRAY_INCREMENT(un->types);
1601 }
1602 /* compensate u increment in main loop */
1603 --additional;
1604
1605 /* free the replaced union subtype */
1606 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1607 } else {
1608 LY_ARRAY_INCREMENT(un->types);
1609 }
1610 }
1611 }
1612
1613 if (!base && !type_p->flags) {
1614 /* type derived from union built-in type must contain at least one type */
1615 if (tpdfname) {
1616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
1617 } else {
1618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
1619 }
1620 return LY_EVALID;
1621 }
1622 break;
1623 case LY_TYPE_BOOL:
1624 case LY_TYPE_EMPTY:
1625 case LY_TYPE_UNKNOWN: /* just to complete switch */
1626 break;
1627 }
1628
1629 if (tpdfname) {
1630 switch (basetype) {
1631 case LY_TYPE_BINARY:
1632 type_p->compiled = *type;
1633 *type = calloc(1, sizeof(struct lysc_type_bin));
1634 break;
1635 case LY_TYPE_BITS:
1636 type_p->compiled = *type;
1637 *type = calloc(1, sizeof(struct lysc_type_bits));
1638 break;
1639 case LY_TYPE_DEC64:
1640 type_p->compiled = *type;
1641 *type = calloc(1, sizeof(struct lysc_type_dec));
1642 break;
1643 case LY_TYPE_STRING:
1644 type_p->compiled = *type;
1645 *type = calloc(1, sizeof(struct lysc_type_str));
1646 break;
1647 case LY_TYPE_ENUM:
1648 type_p->compiled = *type;
1649 *type = calloc(1, sizeof(struct lysc_type_enum));
1650 break;
1651 case LY_TYPE_INT8:
1652 case LY_TYPE_UINT8:
1653 case LY_TYPE_INT16:
1654 case LY_TYPE_UINT16:
1655 case LY_TYPE_INT32:
1656 case LY_TYPE_UINT32:
1657 case LY_TYPE_INT64:
1658 case LY_TYPE_UINT64:
1659 type_p->compiled = *type;
1660 *type = calloc(1, sizeof(struct lysc_type_num));
1661 break;
1662 case LY_TYPE_IDENT:
1663 type_p->compiled = *type;
1664 *type = calloc(1, sizeof(struct lysc_type_identityref));
1665 break;
1666 case LY_TYPE_LEAFREF:
1667 type_p->compiled = *type;
1668 *type = calloc(1, sizeof(struct lysc_type_leafref));
1669 break;
1670 case LY_TYPE_INST:
1671 type_p->compiled = *type;
1672 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1673 break;
1674 case LY_TYPE_UNION:
1675 type_p->compiled = *type;
1676 *type = calloc(1, sizeof(struct lysc_type_union));
1677 break;
1678 case LY_TYPE_BOOL:
1679 case LY_TYPE_EMPTY:
1680 case LY_TYPE_UNKNOWN: /* just to complete switch */
1681 break;
1682 }
1683 }
1684 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1685
1686cleanup:
1687 return ret;
1688}
1689
1690LY_ERR
1691lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
1692 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
1693 struct lysc_type **type, const char **units, struct lysp_qname **dflt)
1694{
1695 LY_ERR ret = LY_SUCCESS;
1696 ly_bool dummyloops = 0;
1697 struct type_context {
1698 const struct lysp_tpdf *tpdf;
1699 struct lysp_node *node;
1700 struct lysp_module *mod;
1701 } *tctx, *tctx_prev = NULL, *tctx_iter;
1702 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1703 struct lysc_type *base = NULL, *prev_type;
1704 struct ly_set tpdf_chain = {0};
1705
1706 (*type) = NULL;
1707 if (dflt) {
1708 *dflt = NULL;
1709 }
1710
1711 tctx = calloc(1, sizeof *tctx);
1712 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1713 for (ret = lysp_type_find(type_p->name, context_pnode, ctx->pmod, &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
1714 ret == LY_SUCCESS;
1715 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
1716 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
1717 if (basetype) {
1718 break;
1719 }
1720
1721 /* check status */
1722 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
1723 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
1724 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1725
1726 if (units && !*units) {
1727 /* inherit units */
1728 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1729 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1730 }
1731 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1732 /* inherit default */
1733 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1734 }
1735 if (dummyloops && (!units || *units) && dflt && *dflt) {
1736 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1737 break;
1738 }
1739
1740 if (tctx->tpdf->type.compiled) {
1741 /* it is not necessary to continue, the rest of the chain was already compiled,
1742 * but we still may need to inherit default and units values, so start dummy loops */
1743 basetype = tctx->tpdf->type.compiled->basetype;
1744 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1745 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1746
1747 if ((units && !*units) || (dflt && !*dflt)) {
1748 dummyloops = 1;
1749 goto preparenext;
1750 } else {
1751 tctx = NULL;
1752 break;
1753 }
1754 }
1755
1756 /* circular typedef reference detection */
1757 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1758 /* local part */
1759 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
1760 if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) {
1761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1762 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1763 free(tctx);
1764 ret = LY_EVALID;
1765 goto cleanup;
1766 }
1767 }
1768 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1769 /* global part for unions corner case */
1770 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
1771 if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) {
1772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1773 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1774 free(tctx);
1775 ret = LY_EVALID;
1776 goto cleanup;
1777 }
1778 }
1779
1780 /* store information for the following processing */
1781 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1782 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1783
1784preparenext:
1785 /* prepare next loop */
1786 tctx_prev = tctx;
1787 tctx = calloc(1, sizeof *tctx);
1788 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1789 }
1790 free(tctx);
1791
1792 /* allocate type according to the basetype */
1793 switch (basetype) {
1794 case LY_TYPE_BINARY:
1795 *type = calloc(1, sizeof(struct lysc_type_bin));
1796 break;
1797 case LY_TYPE_BITS:
1798 *type = calloc(1, sizeof(struct lysc_type_bits));
1799 break;
1800 case LY_TYPE_BOOL:
1801 case LY_TYPE_EMPTY:
1802 *type = calloc(1, sizeof(struct lysc_type));
1803 break;
1804 case LY_TYPE_DEC64:
1805 *type = calloc(1, sizeof(struct lysc_type_dec));
1806 break;
1807 case LY_TYPE_ENUM:
1808 *type = calloc(1, sizeof(struct lysc_type_enum));
1809 break;
1810 case LY_TYPE_IDENT:
1811 *type = calloc(1, sizeof(struct lysc_type_identityref));
1812 break;
1813 case LY_TYPE_INST:
1814 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1815 break;
1816 case LY_TYPE_LEAFREF:
1817 *type = calloc(1, sizeof(struct lysc_type_leafref));
1818 break;
1819 case LY_TYPE_STRING:
1820 *type = calloc(1, sizeof(struct lysc_type_str));
1821 break;
1822 case LY_TYPE_UNION:
1823 *type = calloc(1, sizeof(struct lysc_type_union));
1824 break;
1825 case LY_TYPE_INT8:
1826 case LY_TYPE_UINT8:
1827 case LY_TYPE_INT16:
1828 case LY_TYPE_UINT16:
1829 case LY_TYPE_INT32:
1830 case LY_TYPE_UINT32:
1831 case LY_TYPE_INT64:
1832 case LY_TYPE_UINT64:
1833 *type = calloc(1, sizeof(struct lysc_type_num));
1834 break;
1835 case LY_TYPE_UNKNOWN:
1836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1837 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1838 ret = LY_EVALID;
1839 goto cleanup;
1840 }
1841 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1842 if (~type_substmt_map[basetype] & type_p->flags) {
1843 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
1844 ly_data_type2str[basetype]);
1845 free(*type);
1846 (*type) = NULL;
1847 ret = LY_EVALID;
1848 goto cleanup;
1849 }
1850
1851 /* get restrictions from the referred typedefs */
1852 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1853 tctx = (struct type_context *)tpdf_chain.objs[u];
1854
1855 /* remember the typedef context for circular check */
1856 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1857 LY_CHECK_GOTO(ret, cleanup);
1858
1859 if (tctx->tpdf->type.compiled) {
1860 base = tctx->tpdf->type.compiled;
1861 continue;
1862 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1863 /* no change, just use the type information from the base */
1864 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1865 ++base->refcount;
1866 continue;
1867 }
1868
1869 ++(*type)->refcount;
1870 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
1871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
1872 tctx->tpdf->name, ly_data_type2str[basetype]);
1873 ret = LY_EVALID;
1874 goto cleanup;
1875 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
1876 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1877 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1878 tctx->tpdf->name, tctx->tpdf->dflt.str);
1879 ret = LY_EVALID;
1880 goto cleanup;
1881 }
1882
1883 (*type)->basetype = basetype;
1884 /* TODO user type plugins */
1885 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1886 prev_type = *type;
1887 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name,
1888 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1889 LY_CHECK_GOTO(ret, cleanup);
1890 base = prev_type;
1891 }
1892 /* remove the processed typedef contexts from the stack for circular check */
1893 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1894
1895 /* process the type definition in leaf */
1896 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1897 /* get restrictions from the node itself */
1898 (*type)->basetype = basetype;
1899 /* TODO user type plugins */
1900 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1901 ++(*type)->refcount;
1902 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_mod, context_name, type_p, basetype, NULL,
1903 base, type);
1904 LY_CHECK_GOTO(ret, cleanup);
1905 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1906 /* no specific restriction in leaf's type definition, copy from the base */
1907 free(*type);
1908 (*type) = base;
1909 ++(*type)->refcount;
1910 }
1911
1912 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
1913
1914cleanup:
1915 ly_set_erase(&tpdf_chain, free);
1916 return ret;
1917}
1918
1919/**
1920 * @brief Compile status information of the given node.
1921 *
1922 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
1923 * has the status correctly set during the compilation.
1924 *
1925 * @param[in] ctx Compile context
1926 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
1927 * If the status was set explicitly on the node, it is already set in the flags value and we just check
1928 * the compatibility with the parent's status value.
1929 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
1930 * @return LY_ERR value.
1931 */
1932static LY_ERR
1933lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
1934{
1935 /* status - it is not inherited by specification, but it does not make sense to have
1936 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
1937 if (!((*node_flags) & LYS_STATUS_MASK)) {
1938 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
1939 if ((parent_flags & 0x3) != 0x3) {
1940 /* do not print the warning when inheriting status from uses - the uses_status value has a special
1941 * combination of bits (0x3) which marks the uses_status value */
1942 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
1943 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1944 }
1945 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
1946 } else {
1947 (*node_flags) |= LYS_STATUS_CURR;
1948 }
1949 } else if (parent_flags & LYS_STATUS_MASK) {
1950 /* check status compatibility with the parent */
1951 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
1952 if ((*node_flags) & LYS_STATUS_CURR) {
1953 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1954 "A \"current\" status is in conflict with the parent's \"%s\" status.",
1955 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1956 } else { /* LYS_STATUS_DEPRC */
1957 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1958 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
1959 }
1960 return LY_EVALID;
1961 }
1962 }
1963 return LY_SUCCESS;
1964}
1965
1966/**
1967 * @brief Check uniqness of the node/action/notification name.
1968 *
1969 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
1970 * structures, but they share the namespace so we need to check their name collisions.
1971 *
1972 * @param[in] ctx Compile context.
1973 * @param[in] parent Parent of the nodes to check, can be NULL.
1974 * @param[in] name Name of the item to find in the given lists.
1975 * @param[in] exclude Node that was just added that should be excluded from the name checking.
1976 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
1977 */
1978static LY_ERR
1979lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
1980 const struct lysc_node *exclude)
1981{
1982 const struct lysc_node *iter, *iter2;
1983 const struct lysc_action *actions;
1984 const struct lysc_notif *notifs;
1985 uint32_t getnext_flags;
1986 LY_ARRAY_COUNT_TYPE u;
1987
1988#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
1989
1990 if (exclude->nodetype == LYS_CASE) {
1991 /* check restricted only to all the cases */
1992 assert(parent->nodetype == LYS_CHOICE);
1993 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
1994 if (CHECK_NODE(iter, exclude, name)) {
1995 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
1996 return LY_EEXIST;
1997 }
1998 }
1999
2000 return LY_SUCCESS;
2001 }
2002
2003 /* no reason for our parent to be choice anymore */
2004 assert(!parent || (parent->nodetype != LYS_CHOICE));
2005
2006 if (parent && (parent->nodetype == LYS_CASE)) {
2007 /* move to the first data definition parent */
2008 parent = lysc_data_parent(parent);
2009 }
2010
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002011 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002012 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
2013 getnext_flags |= LYS_GETNEXT_OUTPUT;
2014 }
2015
2016 iter = NULL;
2017 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2018 if (CHECK_NODE(iter, exclude, name)) {
2019 goto error;
2020 }
2021
2022 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2023 if (iter->nodetype == LYS_CHOICE) {
2024 iter2 = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002025 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002026 if (CHECK_NODE(iter2, exclude, name)) {
2027 goto error;
2028 }
2029 }
2030 }
2031 }
2032
2033 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2034 LY_ARRAY_FOR(actions, u) {
2035 if (CHECK_NODE(&actions[u], exclude, name)) {
2036 goto error;
2037 }
2038 }
2039
2040 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2041 LY_ARRAY_FOR(notifs, u) {
2042 if (CHECK_NODE(&notifs[u], exclude, name)) {
2043 goto error;
2044 }
2045 }
2046 return LY_SUCCESS;
2047
2048error:
2049 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
2050 return LY_EEXIST;
2051
2052#undef CHECK_NODE
2053}
2054
2055LY_ERR
2056lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, struct lysc_node *parent,
2057 struct lysc_action *action, uint16_t uses_status)
2058{
2059 LY_ERR ret = LY_SUCCESS;
2060 struct lysp_node *child_p, *dev_pnode = NULL, *dev_input_p = NULL, *dev_output_p = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002061 struct lysp_action_inout *inout_p;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002062 ly_bool not_supported, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002063 uint32_t opt_prev = ctx->options;
2064
2065 lysc_update_path(ctx, parent, action_p->name);
2066
2067 /* apply deviation on the action/RPC */
2068 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)action_p, parent, &dev_pnode, &not_supported));
2069 if (not_supported) {
2070 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002071 ret = LY_EDENIED;
2072 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002073 } else if (dev_pnode) {
2074 action_p = (struct lysp_action *)dev_pnode;
2075 }
2076
2077 /* member needed for uniqueness check lys_getnext() */
2078 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
2079 action->module = ctx->cur_mod;
2080 action->parent = parent;
2081
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002082 LY_CHECK_GOTO(ret = lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002083
2084 if (ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) {
2085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2086 "Action \"%s\" is placed inside %s.", action_p->name,
2087 ctx->options & LYS_COMPILE_RPC_MASK ? "another RPC/action" : "notification");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002088 ret = LY_EVALID;
2089 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002090 }
2091
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002092 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
2093
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002094 /* if-features */
2095 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, action_p->iffeatures, &enabled));
Michal Vasko12606ee2020-11-25 17:05:11 +01002096 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002097 ly_set_add(&ctx->disabled, action, 1, NULL);
2098 ctx->options |= LYS_COMPILE_DISABLED;
2099 }
2100
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002101 /* status - it is not inherited by specification, but it does not make sense to have
2102 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002103 ret = lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
2104 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002105
2106 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
2107 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
2108 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002109
2110 /* connect any action augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002111 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)action), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002112
2113 /* input */
2114 lysc_update_path(ctx, (struct lysc_node *)action, "input");
2115
2116 /* apply deviations on input */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002117 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->input,
2118 (struct lysc_node *)action, &dev_input_p, &not_supported), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002119 if (not_supported) {
2120 inout_p = NULL;
2121 } else if (dev_input_p) {
2122 inout_p = (struct lysp_action_inout *)dev_input_p;
2123 } else {
2124 inout_p = &action_p->input;
2125 }
2126
2127 if (inout_p) {
2128 action->input.nodetype = LYS_INPUT;
Michal Vasko5347e3a2020-11-03 17:14:57 +01002129 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->input.musts, lys_compile_must, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002130 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
2131 ctx->options |= LYS_COMPILE_RPC_INPUT;
2132
2133 /* connect any input augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002134 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)&action->input), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002135
2136 LY_LIST_FOR(inout_p->data, child_p) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002137 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002138 }
2139 ctx->options = opt_prev;
2140 }
2141
2142 lysc_update_path(ctx, NULL, NULL);
2143
2144 /* output */
2145 lysc_update_path(ctx, (struct lysc_node *)action, "output");
2146
2147 /* apply deviations on output */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002148 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->output,
2149 (struct lysc_node *)action, &dev_output_p, &not_supported), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002150 if (not_supported) {
2151 inout_p = NULL;
2152 } else if (dev_output_p) {
2153 inout_p = (struct lysp_action_inout *)dev_output_p;
2154 } else {
2155 inout_p = &action_p->output;
2156 }
2157
2158 if (inout_p) {
2159 action->output.nodetype = LYS_OUTPUT;
Michal Vasko5347e3a2020-11-03 17:14:57 +01002160 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->output.musts, lys_compile_must, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002161 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
2162 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
2163
2164 /* connect any output augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002165 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)&action->output), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002166
2167 LY_LIST_FOR(inout_p->data, child_p) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002168 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002169 }
2170 ctx->options = opt_prev;
2171 }
2172
2173 lysc_update_path(ctx, NULL, NULL);
2174
Michal Vasko208a04a2020-10-21 15:17:12 +02002175 /* wait with extensions compilation until all the children are compiled */
2176 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
2177
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002178 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002179 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002180 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002181 LY_CHECK_GOTO(ret, cleanup);
2182 }
2183
2184 lysc_update_path(ctx, NULL, NULL);
2185
2186cleanup:
2187 lysp_dev_node_free(ctx->ctx, dev_pnode);
2188 lysp_dev_node_free(ctx->ctx, dev_input_p);
2189 lysp_dev_node_free(ctx->ctx, dev_output_p);
2190 ctx->options = opt_prev;
2191 return ret;
2192}
2193
2194LY_ERR
2195lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, struct lysc_node *parent, struct lysc_notif *notif,
2196 uint16_t uses_status)
2197{
2198 LY_ERR ret = LY_SUCCESS;
2199 struct lysp_node *child_p, *dev_pnode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002200 ly_bool not_supported, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002201 uint32_t opt_prev = ctx->options;
2202
2203 lysc_update_path(ctx, parent, notif_p->name);
2204
2205 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)notif_p, parent, &dev_pnode, &not_supported));
2206 if (not_supported) {
2207 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002208 ret = LY_EDENIED;
2209 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002210 } else if (dev_pnode) {
2211 notif_p = (struct lysp_notif *)dev_pnode;
2212 }
2213
2214 /* member needed for uniqueness check lys_getnext() */
2215 notif->nodetype = LYS_NOTIF;
2216 notif->module = ctx->cur_mod;
2217 notif->parent = parent;
2218
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002219 LY_CHECK_GOTO(ret = lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002220
2221 if (ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) {
2222 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2223 "Notification \"%s\" is placed inside %s.", notif_p->name,
2224 ctx->options & LYS_COMPILE_RPC_MASK ? "RPC/action" : "another notification");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002225 ret = LY_EVALID;
2226 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002227 }
2228
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002229 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
2230
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002231 /* if-features */
2232 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, notif_p->iffeatures, &enabled));
Michal Vasko12606ee2020-11-25 17:05:11 +01002233 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002234 ly_set_add(&ctx->disabled, notif, 1, NULL);
2235 ctx->options |= LYS_COMPILE_DISABLED;
2236 }
2237
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002238 /* status - it is not inherited by specification, but it does not make sense to have
2239 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2240 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
2241 LY_CHECK_GOTO(ret, cleanup);
2242
2243 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
2244 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
2245 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Michal Vasko5347e3a2020-11-03 17:14:57 +01002246 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002247 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002248 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002249 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002250 LY_CHECK_GOTO(ret, cleanup);
2251 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002252
2253 ctx->options |= LYS_COMPILE_NOTIFICATION;
2254
2255 /* connect any notification augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002256 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)notif), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002257
2258 LY_LIST_FOR(notif_p->data, child_p) {
2259 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status, NULL);
2260 LY_CHECK_GOTO(ret, cleanup);
2261 }
2262
Michal Vasko208a04a2020-10-21 15:17:12 +02002263 /* wait with extension compilation until all the children are compiled */
2264 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
2265
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002266 lysc_update_path(ctx, NULL, NULL);
2267
2268cleanup:
2269 lysp_dev_node_free(ctx->ctx, dev_pnode);
2270 ctx->options = opt_prev;
2271 return ret;
2272}
2273
2274/**
2275 * @brief Compile parsed container node information.
2276 * @param[in] ctx Compile context
2277 * @param[in] pnode Parsed container node.
2278 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2279 * is enriched with the container-specific information.
2280 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2281 */
2282static LY_ERR
2283lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2284{
2285 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2286 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2287 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002288 LY_ERR ret = LY_SUCCESS;
2289
2290 if (cont_p->presence) {
2291 /* explicit presence */
2292 cont->flags |= LYS_PRESENCE;
2293 } else if (cont_p->musts) {
2294 /* container with a must condition */
2295 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
2296 cont->flags |= LYS_PRESENCE;
2297 } else if (cont_p->when) {
2298 /* container with a when condition */
2299 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"when\" condition.", cont_p->name);
2300 cont->flags |= LYS_PRESENCE;
2301 } else if (cont_p->parent) {
2302 if (cont_p->parent->nodetype == LYS_CHOICE) {
2303 /* container is an implicit case, so its existence decides the existence of the whole case */
2304 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
2305 cont_p->name, cont_p->parent->name);
2306 cont->flags |= LYS_PRESENCE;
2307 } else if ((cont_p->parent->nodetype == LYS_CASE) &&
2308 (((struct lysp_node_case *)cont_p->parent)->child == pnode) && !cont_p->next) {
2309 /* container is the only node in a case, so its existence decides the existence of the whole case */
2310 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
2311 cont_p->name, cont_p->parent->name);
2312 cont->flags |= LYS_PRESENCE;
2313 }
2314 }
2315
2316 /* more cases when the container has meaning but is kept NP for convenience:
2317 * - when condition
2318 * - direct child action/notification
2319 */
2320
2321 LY_LIST_FOR(cont_p->child, child_p) {
2322 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2323 LY_CHECK_GOTO(ret, done);
2324 }
2325
Michal Vasko5347e3a2020-11-03 17:14:57 +01002326 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002327 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002328 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002329 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002330 LY_CHECK_GOTO(ret, done);
2331 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01002332 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, node, lys_compile_action, 0, ret, done);
2333 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, node, lys_compile_notif, 0, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002334
2335done:
2336 return ret;
2337}
2338
2339/*
2340 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2341 * @param[in] ctx Compile context.
2342 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2343 * @param[in] type_p Parsed type to compile.
2344 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2345 * @return LY_ERR value.
2346 */
2347static LY_ERR
2348lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2349 struct lysc_node_leaf *leaf)
2350{
2351 struct lysp_qname *dflt;
2352
2353 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->pmod, leaf->name, type_p, &leaf->type,
2354 leaf->units ? NULL : &leaf->units, &dflt));
2355
2356 /* store default value, if any */
2357 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2358 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2359 }
2360
Michal Vasko12606ee2020-11-25 17:05:11 +01002361 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002362 /* 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 +01002363 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002364 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002365 LY_ARRAY_COUNT_TYPE u;
2366 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2367 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2368 /* 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 +01002369 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002370 }
2371 }
2372 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2373 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2374 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2375 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2376 return LY_EVALID;
2377 }
2378 }
2379
2380 return LY_SUCCESS;
2381}
2382
2383/**
2384 * @brief Compile parsed leaf node information.
2385 * @param[in] ctx Compile context
2386 * @param[in] pnode Parsed leaf node.
2387 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2388 * is enriched with the leaf-specific information.
2389 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2390 */
2391static LY_ERR
2392lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2393{
2394 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2395 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002396 LY_ERR ret = LY_SUCCESS;
2397
Michal Vasko5347e3a2020-11-03 17:14:57 +01002398 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002399 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002400 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002401 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002402 LY_CHECK_GOTO(ret, done);
2403 }
2404 if (leaf_p->units) {
2405 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2406 leaf->flags |= LYS_SET_UNITS;
2407 }
2408
2409 /* compile type */
2410 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2411 LY_CHECK_GOTO(ret, done);
2412
2413 /* store/update default value */
2414 if (leaf_p->dflt.str) {
2415 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2416 leaf->flags |= LYS_SET_DFLT;
2417 }
2418
2419 /* checks */
2420 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
2421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2422 "Invalid mandatory leaf with a default value.");
2423 return LY_EVALID;
2424 }
2425
2426done:
2427 return ret;
2428}
2429
2430/**
2431 * @brief Compile parsed leaf-list node information.
2432 * @param[in] ctx Compile context
2433 * @param[in] pnode Parsed leaf-list node.
2434 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2435 * is enriched with the leaf-list-specific information.
2436 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2437 */
2438static LY_ERR
2439lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2440{
2441 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2442 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002443 LY_ERR ret = LY_SUCCESS;
2444
Michal Vasko5347e3a2020-11-03 17:14:57 +01002445 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002446 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002447 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002448 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002449 LY_CHECK_GOTO(ret, done);
2450 }
2451 if (llist_p->units) {
2452 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2453 llist->flags |= LYS_SET_UNITS;
2454 }
2455
2456 /* compile type */
2457 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2458 LY_CHECK_GOTO(ret, done);
2459
2460 /* store/update default values */
2461 if (llist_p->dflts) {
2462 if (ctx->pmod->version < LYS_VERSION_1_1) {
2463 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2464 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2465 return LY_EVALID;
2466 }
2467
2468 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2469 llist->flags |= LYS_SET_DFLT;
2470 }
2471
2472 llist->min = llist_p->min;
2473 if (llist->min) {
2474 llist->flags |= LYS_MAND_TRUE;
2475 }
2476 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2477
2478 /* checks */
2479 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
2480 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2481 "Invalid mandatory leaf-list with default value(s).");
2482 return LY_EVALID;
2483 }
2484
2485 if (llist->min > llist->max) {
2486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
2487 llist->min, llist->max);
2488 return LY_EVALID;
2489 }
2490
2491done:
2492 return ret;
2493}
2494
2495LY_ERR
2496lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2497 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2498 const struct lysc_node **target, uint16_t *result_flag)
2499{
2500 LY_ERR ret = LY_EVALID;
2501 const char *name, *prefix, *id;
2502 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002503 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002504 const char *nodeid_type;
2505 uint32_t getnext_extra_flag = 0;
2506 uint16_t current_nodetype = 0;
2507
2508 assert(nodeid);
2509 assert(target);
2510 assert(result_flag);
2511 *target = NULL;
2512 *result_flag = 0;
2513
2514 id = nodeid;
2515
2516 if (ctx_node) {
2517 /* descendant-schema-nodeid */
2518 nodeid_type = "descendant";
2519
2520 if (*id == '/') {
2521 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2522 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
2523 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2524 return LY_EVALID;
2525 }
2526 } else {
2527 /* absolute-schema-nodeid */
2528 nodeid_type = "absolute";
2529
2530 if (*id != '/') {
2531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2532 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
2533 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2534 return LY_EVALID;
2535 }
2536 ++id;
2537 }
2538
2539 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2540 if (prefix) {
2541 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2542 if (!mod) {
2543 /* module must always be found */
2544 assert(prefix);
2545 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2546 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
2547 nodeid_type, id - nodeid, nodeid, prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
2548 return LY_ENOTFOUND;
2549 }
2550 } else {
2551 switch (format) {
2552 case LY_PREF_SCHEMA:
2553 case LY_PREF_SCHEMA_RESOLVED:
2554 /* use the current module */
2555 mod = cur_mod;
2556 break;
2557 case LY_PREF_JSON:
2558 if (!ctx_node) {
2559 LOGINT_RET(ctx->ctx);
2560 }
2561 /* inherit the module of the previous context node */
2562 mod = ctx_node->module;
2563 break;
2564 case LY_PREF_XML:
2565 /* not really defined */
2566 LOGINT_RET(ctx->ctx);
2567 }
2568 }
2569
2570 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2571 /* move through input/output manually */
2572 if (mod != ctx_node->module) {
2573 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2574 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
2575 return LY_ENOTFOUND;
2576 }
2577 if (!ly_strncmp("input", name, name_len)) {
2578 ctx_node = (struct lysc_node *)&((struct lysc_action *)ctx_node)->input;
2579 } else if (!ly_strncmp("output", name, name_len)) {
2580 ctx_node = (struct lysc_node *)&((struct lysc_action *)ctx_node)->output;
2581 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2582 } else {
2583 /* only input or output is valid */
2584 ctx_node = NULL;
2585 }
2586 } else {
2587 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002588 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002589 getnext_extra_flag = 0;
2590 }
2591 if (!ctx_node) {
2592 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2593 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
2594 return LY_ENOTFOUND;
2595 }
2596 current_nodetype = ctx_node->nodetype;
2597
2598 if (current_nodetype == LYS_NOTIF) {
2599 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2600 } else if (current_nodetype == LYS_INPUT) {
2601 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2602 } else if (current_nodetype == LYS_OUTPUT) {
2603 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2604 }
2605
2606 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2607 break;
2608 }
2609 if (*id != '/') {
2610 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2611 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
2612 nodeid_type, id - nodeid + 1, nodeid);
2613 return LY_EVALID;
2614 }
2615 ++id;
2616 }
2617
2618 if (ret == LY_SUCCESS) {
2619 *target = ctx_node;
2620 if (nodetype && !(current_nodetype & nodetype)) {
2621 return LY_EDENIED;
2622 }
2623 } else {
2624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2625 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
2626 nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2627 }
2628
2629 return ret;
2630}
2631
2632/**
2633 * @brief Compile information about list's uniques.
2634 * @param[in] ctx Compile context.
2635 * @param[in] uniques Sized array list of unique statements.
2636 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2637 * @return LY_ERR value.
2638 */
2639static LY_ERR
2640lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2641{
2642 LY_ERR ret = LY_SUCCESS;
2643 struct lysc_node_leaf **key, ***unique;
2644 struct lysc_node *parent;
2645 const char *keystr, *delim;
2646 size_t len;
2647 LY_ARRAY_COUNT_TYPE v;
2648 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2649 uint16_t flags;
2650
2651 LY_ARRAY_FOR(uniques, v) {
2652 config = -1;
2653 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2654 keystr = uniques[v].str;
2655 while (keystr) {
2656 delim = strpbrk(keystr, " \t\n");
2657 if (delim) {
2658 len = delim - keystr;
2659 while (isspace(*delim)) {
2660 ++delim;
2661 }
2662 } else {
2663 len = strlen(keystr);
2664 }
2665
2666 /* unique node must be present */
2667 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
2668 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, uniques[v].mod->mod,
2669 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2670 if (ret != LY_SUCCESS) {
2671 if (ret == LY_EDENIED) {
2672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2673 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
2674 len, keystr, lys_nodetype2str((*key)->nodetype));
2675 }
2676 return LY_EVALID;
2677 } else if (flags) {
2678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2679 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
2680 len, keystr, flags & LYS_COMPILE_NOTIFICATION ? "notification" : "RPC/action");
2681 return LY_EVALID;
2682 }
2683
2684 /* all referenced leafs must be of the same config type */
2685 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2686 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
2687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2688 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2689 return LY_EVALID;
2690 } else if ((*key)->flags & LYS_CONFIG_W) {
2691 config = 1;
2692 } else { /* LYS_CONFIG_R */
2693 config = 0;
2694 }
2695
2696 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
2697 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
2698 if (parent->nodetype == LYS_LIST) {
2699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2700 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
2701 return LY_EVALID;
2702 }
2703 }
2704
2705 /* check status */
2706 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2707 (*key)->flags, (*key)->module, (*key)->name));
2708
2709 /* mark leaf as unique */
2710 (*key)->flags |= LYS_UNIQUE;
2711
2712 /* next unique value in line */
2713 keystr = delim;
2714 }
2715 /* next unique definition */
2716 }
2717
2718 return LY_SUCCESS;
2719}
2720
2721/**
2722 * @brief Compile parsed list node information.
2723 * @param[in] ctx Compile context
2724 * @param[in] pnode Parsed list node.
2725 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2726 * is enriched with the list-specific information.
2727 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2728 */
2729static LY_ERR
2730lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2731{
2732 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
2733 struct lysc_node_list *list = (struct lysc_node_list *)node;
2734 struct lysp_node *child_p;
2735 struct lysc_node_leaf *key, *prev_key = NULL;
2736 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002737 const char *keystr, *delim;
2738 LY_ERR ret = LY_SUCCESS;
2739
2740 list->min = list_p->min;
2741 if (list->min) {
2742 list->flags |= LYS_MAND_TRUE;
2743 }
2744 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2745
2746 LY_LIST_FOR(list_p->child, child_p) {
2747 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
2748 }
2749
Michal Vasko5347e3a2020-11-03 17:14:57 +01002750 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002751 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002752 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002753 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002754 }
2755
2756 /* keys */
2757 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
2758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
2759 return LY_EVALID;
2760 }
2761
2762 /* find all the keys (must be direct children) */
2763 keystr = list_p->key;
2764 if (!keystr) {
2765 /* keyless list */
2766 list->flags |= LYS_KEYLESS;
2767 }
2768 while (keystr) {
2769 delim = strpbrk(keystr, " \t\n");
2770 if (delim) {
2771 len = delim - keystr;
2772 while (isspace(*delim)) {
2773 ++delim;
2774 }
2775 } else {
2776 len = strlen(keystr);
2777 }
2778
2779 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002780 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
2781 if (!key) {
2782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002783 return LY_EVALID;
2784 }
2785 /* keys must be unique */
2786 if (key->flags & LYS_KEY) {
2787 /* the node was already marked as a key */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002789 return LY_EVALID;
2790 }
2791
2792 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
2793 /* key must have the same config flag as the list itself */
2794 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
2795 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
2796 return LY_EVALID;
2797 }
2798 if (ctx->pmod->version < LYS_VERSION_1_1) {
2799 /* YANG 1.0 denies key to be of empty type */
2800 if (key->type->basetype == LY_TYPE_EMPTY) {
2801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2802 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
2803 return LY_EVALID;
2804 }
2805 } else {
2806 /* when and if-feature are illegal on list keys */
2807 if (key->when) {
2808 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2809 "List's key must not have any \"when\" statement.");
2810 return LY_EVALID;
2811 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002812 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002813 }
2814
2815 /* check status */
2816 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2817 key->flags, key->module, key->name));
2818
2819 /* ignore default values of the key */
2820 if (key->dflt) {
2821 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
2822 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
2823 free(key->dflt);
2824 key->dflt = NULL;
2825 }
2826 /* mark leaf as key */
2827 key->flags |= LYS_KEY;
2828
2829 /* move it to the correct position */
2830 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
2831 /* fix links in closest previous siblings of the key */
2832 if (key->next) {
2833 key->next->prev = key->prev;
2834 } else {
2835 /* last child */
2836 list->child->prev = key->prev;
2837 }
2838 if (key->prev->next) {
2839 key->prev->next = key->next;
2840 }
2841 /* fix links in the key */
2842 if (prev_key) {
2843 key->prev = (struct lysc_node *)prev_key;
2844 key->next = prev_key->next;
2845 } else {
2846 key->prev = list->child->prev;
2847 key->next = list->child;
2848 }
2849 /* fix links in closes future siblings of the key */
2850 if (prev_key) {
2851 if (prev_key->next) {
2852 prev_key->next->prev = (struct lysc_node *)key;
2853 } else {
2854 list->child->prev = (struct lysc_node *)key;
2855 }
2856 prev_key->next = (struct lysc_node *)key;
2857 } else {
2858 list->child->prev = (struct lysc_node *)key;
2859 }
2860 /* fix links in parent */
2861 if (!key->prev->next) {
2862 list->child = (struct lysc_node *)key;
2863 }
2864 }
2865
2866 /* next key value */
2867 prev_key = key;
2868 keystr = delim;
2869 lysc_update_path(ctx, NULL, NULL);
2870 }
2871
2872 /* uniques */
2873 if (list_p->uniques) {
2874 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
2875 }
2876
Michal Vasko5347e3a2020-11-03 17:14:57 +01002877 COMPILE_OP_ARRAY_GOTO(ctx, list_p->actions, list->actions, node, lys_compile_action, 0, ret, done);
2878 COMPILE_OP_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, node, lys_compile_notif, 0, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002879
2880 /* checks */
2881 if (list->min > list->max) {
2882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
2883 list->min, list->max);
2884 return LY_EVALID;
2885 }
2886
2887done:
2888 return ret;
2889}
2890
2891/**
2892 * @brief Do some checks and set the default choice's case.
2893 *
2894 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
2895 *
2896 * @param[in] ctx Compile context.
2897 * @param[in] dflt Name of the default branch. Can even contain a prefix.
2898 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
2899 * @return LY_ERR value.
2900 */
2901static LY_ERR
2902lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
2903{
2904 struct lysc_node *iter, *node = (struct lysc_node *)ch;
2905 const struct lys_module *mod;
2906 const char *prefix = NULL, *name;
2907 size_t prefix_len = 0;
2908
2909 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
2910 name = strchr(dflt->str, ':');
2911 if (name) {
2912 prefix = dflt->str;
2913 prefix_len = name - prefix;
2914 ++name;
2915 } else {
2916 name = dflt->str;
2917 }
2918 if (prefix) {
2919 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
2920 if (!mod) {
2921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
2922 "in imports of \"%s\".", prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
2923 return LY_EVALID;
2924 }
2925 } else {
2926 mod = node->module;
2927 }
2928
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002929 ch->dflt = (struct lysc_node_case *)lys_find_child(node, mod, name, 0, LYS_CASE, LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002930 if (!ch->dflt) {
2931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2932 "Default case \"%s\" not found.", dflt->str);
2933 return LY_EVALID;
2934 }
2935
2936 /* no mandatory nodes directly under the default case */
2937 LY_LIST_FOR(ch->dflt->child, iter) {
2938 if (iter->parent != (struct lysc_node *)ch->dflt) {
2939 break;
2940 }
2941 if (iter->flags & LYS_MAND_TRUE) {
2942 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2943 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
2944 return LY_EVALID;
2945 }
2946 }
2947
2948 if (ch->flags & LYS_MAND_TRUE) {
2949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
2950 return LY_EVALID;
2951 }
2952
2953 ch->dflt->flags |= LYS_SET_DFLT;
2954 return LY_SUCCESS;
2955}
2956
2957LY_ERR
2958lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
2959 struct ly_set *child_set)
2960{
2961 LY_ERR ret = LY_SUCCESS;
2962 struct lysp_node *child_p_next = child_p->next;
2963 struct lysp_node_case *cs_p;
2964
2965 if (child_p->nodetype == LYS_CASE) {
2966 /* standard case under choice */
2967 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
2968 } else {
2969 /* we need the implicit case first, so create a fake parsed case */
2970 cs_p = calloc(1, sizeof *cs_p);
2971 cs_p->nodetype = LYS_CASE;
2972 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
2973 cs_p->child = child_p;
2974
2975 /* make the child the only case child */
2976 child_p->next = NULL;
2977
2978 /* compile it normally */
2979 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
2980
2981free_fake_node:
2982 /* free the fake parsed node and correct pointers back */
2983 cs_p->child = NULL;
2984 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
2985 child_p->next = child_p_next;
2986 }
2987
2988 return ret;
2989}
2990
2991/**
2992 * @brief Compile parsed choice node information.
2993 *
2994 * @param[in] ctx Compile context
2995 * @param[in] pnode Parsed choice node.
2996 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2997 * is enriched with the choice-specific information.
2998 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2999 */
3000static LY_ERR
3001lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3002{
3003 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3004 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3005 struct lysp_node *child_p;
3006 LY_ERR ret = LY_SUCCESS;
3007
3008 assert(node->nodetype == LYS_CHOICE);
3009
3010 LY_LIST_FOR(ch_p->child, child_p) {
3011 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3012 }
3013
3014 /* default branch */
3015 if (ch_p->dflt.str) {
3016 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3017 }
3018
3019 return ret;
3020}
3021
3022/**
3023 * @brief Compile parsed anydata or anyxml node information.
3024 * @param[in] ctx Compile context
3025 * @param[in] pnode Parsed anydata or anyxml node.
3026 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3027 * is enriched with the any-specific information.
3028 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3029 */
3030static LY_ERR
3031lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3032{
3033 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3034 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003035 LY_ERR ret = LY_SUCCESS;
3036
Michal Vasko5347e3a2020-11-03 17:14:57 +01003037 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003038 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003039 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003040 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003041 LY_CHECK_GOTO(ret, done);
3042 }
3043
Michal Vasko75620aa2020-10-21 09:25:51 +02003044 if (!(ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) && (any->flags & LYS_CONFIG_W)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003045 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3046 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3047 }
3048done:
3049 return ret;
3050}
3051
3052/**
3053 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
3054 * keep specific order of augments targetting the same node.
3055 *
3056 * @param[in] ctx Compile context
3057 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3058 * the choice itself is expected instead of a specific case node.
3059 * @param[in] node Schema node to connect into the list.
3060 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3061 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
3062 */
3063static LY_ERR
3064lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
3065{
3066 struct lysc_node **children, *anchor = NULL;
3067 int insert_after = 0;
3068
3069 node->parent = parent;
3070
3071 if (parent) {
3072 if (parent->nodetype == LYS_CHOICE) {
3073 assert(node->nodetype == LYS_CASE);
3074 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
3075 } else {
3076 children = lysc_node_children_p(parent, ctx->options);
3077 }
3078 assert(children);
3079
3080 if (!(*children)) {
3081 /* first child */
3082 *children = node;
3083 } else if (node->flags & LYS_KEY) {
3084 /* special handling of adding keys */
3085 assert(node->module == parent->module);
3086 anchor = *children;
3087 if (anchor->flags & LYS_KEY) {
3088 while ((anchor->flags & LYS_KEY) && anchor->next) {
3089 anchor = anchor->next;
3090 }
3091 /* insert after the last key */
3092 insert_after = 1;
3093 } /* else insert before anchor (at the beginning) */
3094 } else if ((*children)->prev->module == node->module) {
3095 /* last child is from the same module, keep the order and insert at the end */
3096 anchor = (*children)->prev;
3097 insert_after = 1;
3098 } else if (parent->module == node->module) {
3099 /* adding module child after some augments were connected */
3100 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
3101 } else {
3102 /* some augments are already connected and we are connecting new ones,
3103 * keep module name order and insert the node into the children list */
3104 anchor = *children;
3105 do {
3106 anchor = anchor->prev;
3107
3108 /* check that we have not found the last augment node from our module or
3109 * the first augment node from a "smaller" module or
3110 * the first node from a local module */
3111 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
3112 (anchor->module == parent->module)) {
3113 /* insert after */
3114 insert_after = 1;
3115 break;
3116 }
3117
3118 /* we have traversed all the nodes, insert before anchor (as the first node) */
3119 } while (anchor->prev->next);
3120 }
3121
3122 /* insert */
3123 if (anchor) {
3124 if (insert_after) {
3125 node->next = anchor->next;
3126 node->prev = anchor;
3127 anchor->next = node;
3128 if (node->next) {
3129 /* middle node */
3130 node->next->prev = node;
3131 } else {
3132 /* last node */
3133 (*children)->prev = node;
3134 }
3135 } else {
3136 node->next = anchor;
3137 node->prev = anchor->prev;
3138 anchor->prev = node;
3139 if (anchor == *children) {
3140 /* first node */
3141 *children = node;
3142 } else {
3143 /* middle node */
3144 node->prev->next = node;
3145 }
3146 }
3147 }
3148
3149 /* check the name uniqueness (even for an only child, it may be in case) */
3150 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
3151 return LY_EEXIST;
3152 }
3153 } else {
3154 /* top-level element */
3155 if (!ctx->cur_mod->compiled->data) {
3156 ctx->cur_mod->compiled->data = node;
3157 } else {
3158 /* insert at the end of the module's top-level nodes list */
3159 ctx->cur_mod->compiled->data->prev->next = node;
3160 node->prev = ctx->cur_mod->compiled->data->prev;
3161 ctx->cur_mod->compiled->data->prev = node;
3162 }
3163
3164 /* check the name uniqueness on top-level */
3165 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
3166 return LY_EEXIST;
3167 }
3168 }
3169
3170 return LY_SUCCESS;
3171}
3172
3173/**
3174 * @brief Prepare the case structure in choice node for the new data node.
3175 *
3176 * 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
3177 * created in the choice when the first child was processed.
3178 *
3179 * @param[in] ctx Compile context.
3180 * @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,
3181 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3182 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3183 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3184 * @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,
3185 * it is linked from the case structure only in case it is its first child.
3186 */
3187static LY_ERR
3188lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3189{
3190 struct lysp_node *child_p;
3191 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3192
3193 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3194 /* we have to add an implicit case node into the parent choice */
3195 } else if (pnode->nodetype == LYS_CASE) {
3196 /* explicit parent case */
3197 LY_LIST_FOR(cs_p->child, child_p) {
3198 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3199 }
3200 } else {
3201 LOGINT_RET(ctx->ctx);
3202 }
3203
3204 return LY_SUCCESS;
3205}
3206
3207void
3208lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3209{
3210 struct lysc_node *iter;
3211
3212 if (add) { /* set flag */
3213 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3214 parent = parent->parent) {
3215 parent->flags |= LYS_MAND_TRUE;
3216 }
3217 } else { /* unset flag */
3218 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
3219 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
3220 if (iter->flags & LYS_MAND_TRUE) {
3221 /* there is another mandatory node */
3222 return;
3223 }
3224 }
3225 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3226 parent->flags &= ~LYS_MAND_TRUE;
3227 }
3228 }
3229}
3230
3231/**
3232 * @brief Find grouping for a uses.
3233 *
3234 * @param[in] ctx Compile context.
3235 * @param[in] uses_p Parsed uses node.
3236 * @param[out] gpr_p Found grouping on success.
3237 * @param[out] grp_pmod Module of @p grp_p on success.
3238 * @return LY_ERR value.
3239 */
3240static LY_ERR
3241lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
3242 struct lysp_module **grp_pmod)
3243{
3244 struct lysp_node *pnode;
3245 struct lysp_grp *grp;
3246 LY_ARRAY_COUNT_TYPE u, v;
3247 const char *id, *name, *prefix, *local_pref;
3248 size_t prefix_len, name_len;
3249 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003250 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003251
3252 *grp_p = NULL;
3253 *grp_pmod = NULL;
3254
3255 /* search for the grouping definition */
3256 id = uses_p->name;
3257 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3258 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3259 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3260 /* current module, search local groupings first */
3261 pmod = ctx->pmod;
3262 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
3263 grp = (struct lysp_grp *)lysp_node_groupings(pnode);
3264 LY_ARRAY_FOR(grp, u) {
3265 if (!strcmp(grp[u].name, name)) {
3266 grp = &grp[u];
3267 found = ctx->pmod;
3268 break;
3269 }
3270 }
3271 }
3272 } else {
3273 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003274 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003275 if (!mod) {
3276 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3277 "Invalid prefix used for grouping reference.", uses_p->name);
3278 return LY_EVALID;
3279 }
3280 pmod = mod->parsed;
3281 }
3282
3283 if (!found) {
3284 /* search in top-level groupings of the main module ... */
3285 grp = pmod->groupings;
3286 LY_ARRAY_FOR(grp, u) {
3287 if (!strcmp(grp[u].name, name)) {
3288 grp = &grp[u];
3289 found = pmod;
3290 break;
3291 }
3292 }
3293 if (!found) {
3294 /* ... and all the submodules */
3295 LY_ARRAY_FOR(pmod->includes, u) {
3296 grp = pmod->includes[u].submodule->groupings;
3297 LY_ARRAY_FOR(grp, v) {
3298 if (!strcmp(grp[v].name, name)) {
3299 grp = &grp[v];
3300 found = (struct lysp_module *)pmod->includes[u].submodule;
3301 break;
3302 }
3303 }
3304 if (found) {
3305 break;
3306 }
3307 }
3308 }
3309 }
3310 if (!found) {
3311 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3312 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3313 return LY_EVALID;
3314 }
3315
3316 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3317 /* remember that the grouping is instantiated to avoid its standalone validation */
3318 grp->flags |= LYS_USED_GRP;
3319 }
3320
3321 *grp_p = grp;
3322 *grp_pmod = found;
3323 return LY_SUCCESS;
3324}
3325
3326/**
3327 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3328 * If present, also apply uses's modificators.
3329 *
3330 * @param[in] ctx Compile context
3331 * @param[in] uses_p Parsed uses schema node.
3332 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3333 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3334 * the compile context.
3335 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3336 */
3337static LY_ERR
3338lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3339{
3340 struct lysp_node *pnode;
3341 struct lysc_node *child;
3342 struct lysp_grp *grp = NULL;
3343 uint32_t i, grp_stack_count;
3344 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3345 LY_ERR ret = LY_SUCCESS;
3346 struct lysc_when *when_shared = NULL;
3347 LY_ARRAY_COUNT_TYPE u;
3348 struct lysc_notif **notifs = NULL;
3349 struct lysc_action **actions = NULL;
3350 struct ly_set uses_child_set = {0};
3351
3352 /* find the referenced grouping */
3353 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3354
3355 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3356 grp_stack_count = ctx->groupings.count;
3357 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3358 if (grp_stack_count == ctx->groupings.count) {
3359 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
3360 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3361 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3362 return LY_EVALID;
3363 }
3364
3365 /* check status */
3366 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3367 LY_CHECK_GOTO(ret, cleanup);
3368
3369 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3370 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3371 LY_CHECK_GOTO(ret, cleanup);
3372
3373 /* switch context's parsed module being processed */
3374 ctx->pmod = grp_mod;
3375
3376 /* compile data nodes */
3377 LY_LIST_FOR(grp->data, pnode) {
3378 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
3379 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3, &uses_child_set);
3380 LY_CHECK_GOTO(ret, cleanup);
3381 }
3382
3383 if (child_set) {
3384 /* add these children to our compiled child_set as well since uses is a schema-only node */
3385 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3386 }
3387
3388 if (uses_p->when) {
3389 /* pass uses's when to all the data children */
3390 for (i = 0; i < uses_child_set.count; ++i) {
3391 child = uses_child_set.snodes[i];
3392
3393 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent), child, &when_shared);
3394 LY_CHECK_GOTO(ret, cleanup);
3395 }
3396 }
3397
3398 /* compile actions */
3399 if (grp->actions) {
3400 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3401 if (!actions) {
3402 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3403 grp->actions[0].name, lys_nodetype2str(grp->actions[0].nodetype), parent->name,
3404 lys_nodetype2str(parent->nodetype));
3405 ret = LY_EVALID;
3406 goto cleanup;
3407 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01003408 COMPILE_OP_ARRAY_GOTO(ctx, grp->actions, *actions, parent, lys_compile_action, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003409
3410 if (uses_p->when) {
3411 /* inherit when */
3412 LY_ARRAY_FOR(*actions, u) {
3413 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
3414 (struct lysc_node *)&(*actions)[u], &when_shared);
3415 LY_CHECK_GOTO(ret, cleanup);
3416 }
3417 }
3418 }
3419
3420 /* compile notifications */
3421 if (grp->notifs) {
3422 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3423 if (!notifs) {
3424 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3425 grp->notifs[0].name, lys_nodetype2str(grp->notifs[0].nodetype), parent->name,
3426 lys_nodetype2str(parent->nodetype));
3427 ret = LY_EVALID;
3428 goto cleanup;
3429 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01003430 COMPILE_OP_ARRAY_GOTO(ctx, grp->notifs, *notifs, parent, lys_compile_notif, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003431
3432 if (uses_p->when) {
3433 /* inherit when */
3434 LY_ARRAY_FOR(*notifs, u) {
3435 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
3436 (struct lysc_node *)&(*notifs)[u], &when_shared);
3437 LY_CHECK_GOTO(ret, cleanup);
3438 }
3439 }
3440 }
3441
3442 /* check that all augments were applied */
3443 for (i = 0; i < ctx->uses_augs.count; ++i) {
3444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3445 "Augment target node \"%s\" in grouping \"%s\" was not found.",
3446 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3447 ret = LY_ENOTFOUND;
3448 }
3449 LY_CHECK_GOTO(ret, cleanup);
3450
3451 /* check that all refines were applied */
3452 for (i = 0; i < ctx->uses_rfns.count; ++i) {
3453 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3454 "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
3455 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3456 ret = LY_ENOTFOUND;
3457 }
3458 LY_CHECK_GOTO(ret, cleanup);
3459
3460cleanup:
3461 /* reload previous context's parsed module being processed */
3462 ctx->pmod = mod_old;
3463
3464 /* remove the grouping from the stack for circular groupings dependency check */
3465 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3466 assert(ctx->groupings.count == grp_stack_count);
3467
3468 ly_set_erase(&uses_child_set, NULL);
3469 return ret;
3470}
3471
3472static int
3473lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3474{
3475 struct lysp_node *iter;
3476 int len = 0;
3477
3478 *path = NULL;
3479 for (iter = node; iter && len >= 0; iter = iter->parent) {
3480 char *s = *path;
3481 char *id;
3482
3483 switch (iter->nodetype) {
3484 case LYS_USES:
3485 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3486 break;
3487 case LYS_GROUPING:
3488 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3489 break;
3490 case LYS_AUGMENT:
3491 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3492 break;
3493 default:
3494 id = strdup(iter->name);
3495 break;
3496 }
3497
3498 if (!iter->parent) {
3499 /* print prefix */
3500 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3501 } else {
3502 /* prefix is the same as in parent */
3503 len = asprintf(path, "/%s%s", id, s ? s : "");
3504 }
3505 free(s);
3506 free(id);
3507 }
3508
3509 if (len < 0) {
3510 free(*path);
3511 *path = NULL;
3512 } else if (len == 0) {
3513 *path = strdup("/");
3514 len = 1;
3515 }
3516 return len;
3517}
3518
3519LY_ERR
3520lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_grp *grp)
3521{
3522 LY_ERR ret;
3523 char *path;
3524 int len;
3525
3526 struct lysp_node_uses fake_uses = {
3527 .parent = pnode,
3528 .nodetype = LYS_USES,
3529 .flags = 0, .next = NULL,
3530 .name = grp->name,
3531 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3532 .refines = NULL, .augments = NULL
3533 };
3534 struct lysc_node_container fake_container = {
3535 .nodetype = LYS_CONTAINER,
3536 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3537 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003538 .parent = NULL, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003539 .prev = (struct lysc_node *)&fake_container,
3540 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003541 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003542 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3543 };
3544
3545 if (grp->parent) {
3546 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3547 }
3548
3549 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3550 if (len < 0) {
3551 LOGMEM(ctx->ctx);
3552 return LY_EMEM;
3553 }
3554 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3555 ctx->path_len = (uint32_t)len;
3556 free(path);
3557
3558 lysc_update_path(ctx, NULL, "{grouping}");
3559 lysc_update_path(ctx, NULL, grp->name);
3560 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
3561 lysc_update_path(ctx, NULL, NULL);
3562 lysc_update_path(ctx, NULL, NULL);
3563
3564 ctx->path_len = 1;
3565 ctx->path[1] = '\0';
3566
3567 /* cleanup */
3568 lysc_node_container_free(ctx->ctx, &fake_container);
3569
3570 return ret;
3571}
3572
3573/**
3574 * @brief Set config flags for a node.
3575 *
3576 * @param[in] ctx Compile context.
3577 * @param[in] node Compiled node config to set.
3578 * @param[in] parent Parent of @p node.
3579 * @return LY_ERR value.
3580 */
3581static LY_ERR
3582lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
3583{
3584 if (node->nodetype == LYS_CASE) {
3585 /* case never has any config */
3586 assert(!(node->flags & LYS_CONFIG_MASK));
3587 return LY_SUCCESS;
3588 }
3589
3590 /* adjust parent to always get the ancestor with config */
3591 if (parent && (parent->nodetype == LYS_CASE)) {
3592 parent = parent->parent;
3593 assert(parent);
3594 }
3595
3596 if (ctx->options & (LYS_COMPILE_RPC_INPUT | LYS_COMPILE_RPC_OUTPUT)) {
3597 /* ignore config statements inside RPC/action data */
3598 node->flags &= ~LYS_CONFIG_MASK;
3599 node->flags |= (ctx->options & LYS_COMPILE_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
3600 } else if (ctx->options & LYS_COMPILE_NOTIFICATION) {
3601 /* ignore config statements inside Notification data */
3602 node->flags &= ~LYS_CONFIG_MASK;
3603 node->flags |= LYS_CONFIG_R;
3604 } else if (!(node->flags & LYS_CONFIG_MASK)) {
3605 /* config not explicitely set, inherit it from parent */
3606 if (parent) {
3607 node->flags |= parent->flags & LYS_CONFIG_MASK;
3608 } else {
3609 /* default is config true */
3610 node->flags |= LYS_CONFIG_W;
3611 }
3612 } else {
3613 /* config set explicitely */
3614 node->flags |= LYS_SET_CONFIG;
3615 }
3616
3617 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3619 "Configuration node cannot be child of any state data node.");
3620 return LY_EVALID;
3621 }
3622
3623 return LY_SUCCESS;
3624}
3625
3626LY_ERR
3627lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3628 struct ly_set *child_set)
3629{
3630 LY_ERR ret = LY_SUCCESS;
3631 struct lysc_node *node = NULL;
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003632 struct lysp_node *dev_pnode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003633 ly_bool not_supported, enabled;
3634 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003635
3636 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3637
3638 if (pnode->nodetype != LYS_USES) {
3639 lysc_update_path(ctx, parent, pnode->name);
3640 } else {
3641 lysc_update_path(ctx, NULL, "{uses}");
3642 lysc_update_path(ctx, NULL, pnode->name);
3643 }
3644
3645 switch (pnode->nodetype) {
3646 case LYS_CONTAINER:
3647 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3648 node_compile_spec = lys_compile_node_container;
3649 break;
3650 case LYS_LEAF:
3651 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3652 node_compile_spec = lys_compile_node_leaf;
3653 break;
3654 case LYS_LIST:
3655 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3656 node_compile_spec = lys_compile_node_list;
3657 break;
3658 case LYS_LEAFLIST:
3659 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3660 node_compile_spec = lys_compile_node_leaflist;
3661 break;
3662 case LYS_CHOICE:
3663 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3664 node_compile_spec = lys_compile_node_choice;
3665 break;
3666 case LYS_CASE:
3667 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3668 node_compile_spec = lys_compile_node_case;
3669 break;
3670 case LYS_ANYXML:
3671 case LYS_ANYDATA:
3672 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3673 node_compile_spec = lys_compile_node_any;
3674 break;
3675 case LYS_USES:
3676 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3677 lysc_update_path(ctx, NULL, NULL);
3678 lysc_update_path(ctx, NULL, NULL);
3679 return ret;
3680 default:
3681 LOGINT(ctx->ctx);
3682 return LY_EINT;
3683 }
3684 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3685
3686 /* compile any deviations for this node */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003687 LY_CHECK_ERR_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported),
3688 free(node), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003689 if (not_supported) {
3690 free(node);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003691 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003692 } else if (dev_pnode) {
3693 pnode = dev_pnode;
3694 }
3695
3696 node->nodetype = pnode->nodetype;
3697 node->module = ctx->cur_mod;
3698 node->prev = node;
3699 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
3700
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003701 /* if-features */
3702 LY_CHECK_ERR_RET(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), free(node), ret);
Michal Vasko12606ee2020-11-25 17:05:11 +01003703 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003704 ly_set_add(&ctx->disabled, node, 1, NULL);
3705 ctx->options |= LYS_COMPILE_DISABLED;
3706 }
3707
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003708 /* config */
3709 ret = lys_compile_config(ctx, node, parent);
3710 LY_CHECK_GOTO(ret, error);
3711
3712 /* list ordering */
3713 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3714 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
3715 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
3716 (ctx->options & LYS_COMPILE_RPC_OUTPUT) ? "RPC/action output parameters" :
3717 (ctx->options & LYS_COMPILE_NOTIFICATION) ? "notification content" : "state data", ctx->path);
3718 node->flags &= ~LYS_ORDBY_MASK;
3719 node->flags |= LYS_ORDBY_SYSTEM;
3720 } else if (!(node->flags & LYS_ORDBY_MASK)) {
3721 /* default ordering is system */
3722 node->flags |= LYS_ORDBY_SYSTEM;
3723 }
3724 }
3725
3726 /* status - it is not inherited by specification, but it does not make sense to have
3727 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3728 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
3729
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003730 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
3731 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
3732 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003733
3734 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
3735 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
3736
3737 if (pnode->when) {
3738 /* compile when */
3739 ret = lys_compile_when(ctx, pnode->when, pnode->flags, lysc_xpath_context(node), node, NULL);
3740 LY_CHECK_GOTO(ret, cleanup);
3741 }
3742
3743 /* connect any augments */
3744 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
3745
3746 /* nodetype-specific part */
3747 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
3748
3749 /* final compilation tasks that require the node to be connected */
3750 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, LYEXT_PAR_NODE, ret, cleanup);
3751 if (node->flags & LYS_MAND_TRUE) {
3752 /* inherit LYS_MAND_TRUE in parent containers */
3753 lys_compile_mandatory_parents(parent, 1);
3754 }
3755
3756 if (child_set) {
3757 /* add the new node into set */
3758 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
3759 }
3760
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003761 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003762
3763error:
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003764 lysc_node_free(ctx->ctx, node, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003765cleanup:
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003766 ctx->options = prev_opts;
3767 if (ret && dev_pnode) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003769 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003770 lysp_dev_node_free(ctx->ctx, dev_pnode);
3771 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003772 return ret;
3773}