blob: 07ce08d55f471c73f8ac7989855777d8c3e72581 [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}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100931 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200932 {NULL, NULL}
933 };
934
935 /* adjust the expression to a Perl equivalent
936 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
937
938 /* allocate space for the transformed pattern */
939 size = strlen(pattern) + 1;
940 perl_regex = malloc(size);
941 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
942 perl_regex[0] = '\0';
943
944 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
945 brack = 0;
946 idx = 0;
947 orig_ptr = pattern;
948 while (orig_ptr[0]) {
949 switch (orig_ptr[0]) {
950 case '$':
951 case '^':
952 if (!brack) {
953 /* make space for the extra character */
954 ++size;
955 perl_regex = ly_realloc(perl_regex, size);
956 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
957
958 /* print escape slash */
959 perl_regex[idx] = '\\';
960 ++idx;
961 }
962 break;
963 case '[':
964 /* must not be escaped */
965 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
966 ++brack;
967 }
968 break;
969 case ']':
970 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
971 /* pattern was checked and compiled already */
972 assert(brack);
973 --brack;
974 }
975 break;
976 default:
977 break;
978 }
979
980 /* copy char */
981 perl_regex[idx] = orig_ptr[0];
982
983 ++idx;
984 ++orig_ptr;
985 }
986 perl_regex[idx] = '\0';
987
988 /* substitute Unicode Character Blocks with exact Character Ranges */
989 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
990 start = ptr - perl_regex;
991
992 ptr = strchr(ptr, '}');
993 if (!ptr) {
994 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
995 pattern, perl_regex + start + 2, "unterminated character property");
996 free(perl_regex);
997 return LY_EVALID;
998 }
999 end = (ptr - perl_regex) + 1;
1000
1001 /* need more space */
1002 if (end - start < URANGE_LEN) {
1003 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1004 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1005 }
1006
1007 /* find our range */
1008 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1009 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1010 break;
1011 }
1012 }
1013 if (!ublock2urange[idx][0]) {
1014 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
1015 pattern, perl_regex + start + 5, "unknown block name");
1016 free(perl_regex);
1017 return LY_EVALID;
1018 }
1019
1020 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1021 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1022 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1023 ++idx;
1024 }
1025 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1026 --idx;
1027 }
1028 }
1029 if (idx) {
1030 /* skip brackets */
1031 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1032 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1033 } else {
1034 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1035 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1036 }
1037 }
1038
1039 /* must return 0, already checked during parsing */
1040 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1041 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1042 &err_code, &err_offset, NULL);
1043 if (!code_local) {
1044 PCRE2_UCHAR err_msg[256] = {0};
1045 pcre2_get_error_message(err_code, err_msg, 256);
1046 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1047 free(perl_regex);
1048 return LY_EVALID;
1049 }
1050 free(perl_regex);
1051
1052 if (code) {
1053 *code = code_local;
1054 } else {
1055 free(code_local);
1056 }
1057
1058 return LY_SUCCESS;
1059
1060#undef URANGE_LEN
1061}
1062
1063/**
1064 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1065 * @param[in] ctx Compile context.
1066 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1067 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1068 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1069 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1070 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1071 */
1072static LY_ERR
1073lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1074 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1075{
1076 struct lysc_pattern **pattern;
1077 LY_ARRAY_COUNT_TYPE u;
1078 LY_ERR ret = LY_SUCCESS;
1079
1080 /* first, copy the patterns from the base type */
1081 if (base_patterns) {
1082 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1083 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1084 }
1085
1086 LY_ARRAY_FOR(patterns_p, u) {
1087 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1088 *pattern = calloc(1, sizeof **pattern);
1089 ++(*pattern)->refcount;
1090
1091 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg.str[1], &(*pattern)->code);
1092 LY_CHECK_RET(ret);
1093
1094 if (patterns_p[u].arg.str[0] == 0x15) {
1095 (*pattern)->inverted = 1;
1096 }
1097 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1098 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1099 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1100 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1101 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
1102 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
1103 }
1104done:
1105 return ret;
1106}
1107
1108/**
1109 * @brief map of the possible restrictions combination for the specific built-in type.
1110 */
1111static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1112 0 /* LY_TYPE_UNKNOWN */,
1113 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1114 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1115 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1116 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1117 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1118 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1119 LYS_SET_BIT /* LY_TYPE_BITS */,
1120 0 /* LY_TYPE_BOOL */,
1121 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1122 0 /* LY_TYPE_EMPTY */,
1123 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1124 LYS_SET_BASE /* LY_TYPE_IDENT */,
1125 LYS_SET_REQINST /* LY_TYPE_INST */,
1126 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1127 LYS_SET_TYPE /* LY_TYPE_UNION */,
1128 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1129 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1130 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1131 LYS_SET_RANGE /* LY_TYPE_INT64 */
1132};
1133
1134/**
1135 * @brief stringification of the YANG built-in data types
1136 */
1137const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
1138 "unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1139 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1140 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
1141};
1142
1143/**
1144 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1145 * @param[in] ctx Compile context.
1146 * @param[in] enums_p Array of the parsed enum structures to compile.
1147 * @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.
1148 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1149 * @param[out] enums Newly created array of the compiled enums information for the current type.
1150 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1151 */
1152static LY_ERR
1153lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
1154 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
1155{
1156 LY_ERR ret = LY_SUCCESS;
1157 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001158 int32_t highest_value = INT32_MIN, cur_val;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001159 uint32_t highest_position = 0, cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001160 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001161 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001162
1163 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
1164 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1165 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1166 return LY_EVALID;
1167 }
1168
1169 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001170 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001171 if (base_enums) {
1172 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1173 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001174 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001175 break;
1176 }
1177 }
1178 if (v == LY_ARRAY_COUNT(base_enums)) {
1179 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1180 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001181 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001182 return LY_EVALID;
1183 }
1184 match = v;
1185 }
1186
1187 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001188 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001189 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001190 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001191 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001192 LY_ARRAY_FOR(*enums, v) {
1193 if (cur_val == (*enums)[v].value) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001194 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1195 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001196 cur_val, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001197 return LY_EVALID;
1198 }
1199 }
1200 } else if (base_enums) {
1201 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001202 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001203 } else {
1204 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001205 if (u == 0) {
1206 cur_val = 0;
1207 } else if (highest_value == INT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001208 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1209 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001210 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001211 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001212 } else {
1213 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001214 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001215 }
1216
1217 /* save highest value for auto assing */
1218 if (highest_value < cur_val) {
1219 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 }
1221 } else { /* LY_TYPE_BITS */
1222 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001223 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001224 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001225 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001226 LY_ARRAY_FOR(*enums, v) {
1227 if (cur_pos == (*enums)[v].position) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001228 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1229 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001230 cur_pos, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001231 return LY_EVALID;
1232 }
1233 }
1234 } else if (base_enums) {
1235 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001236 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001237 } else {
1238 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001239 if (u == 0) {
1240 cur_pos = 0;
1241 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001242 /* counter overflow */
1243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1244 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001245 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001246 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001247 } else {
1248 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001250 }
1251
1252 /* save highest position for auto assing */
1253 if (highest_position < cur_pos) {
1254 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001255 }
1256 }
1257
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001258 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001259 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001260 if (basetype == LY_TYPE_ENUM) {
1261 if (cur_val != base_enums[match].value) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1263 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001264 enums_p[u].name, base_enums[match].value, cur_val);
1265 return LY_EVALID;
1266 }
1267 } else {
1268 if (cur_pos != base_enums[match].position) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001269 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1270 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001271 enums_p[u].name, base_enums[match].position, cur_pos);
1272 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001273 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001274 }
1275 }
1276
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001277 /* evaluate if-ffeatures */
1278 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1279 if (!enabled) {
1280 continue;
1281 }
1282
1283 /* add new enum/bit */
1284 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1285 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1286 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1287 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
1288 e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) | (basetype == LY_TYPE_ENUM ? LYS_ISENUM : 0);
1289 if (basetype == LY_TYPE_ENUM) {
1290 e->value = cur_val;
1291 } else {
1292 e->position = cur_pos;
1293 }
1294 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM :
1295 LYEXT_PAR_TYPE_BIT, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001296
1297 if (basetype == LY_TYPE_BITS) {
1298 /* keep bits ordered by position */
1299 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
1300 if (v != u) {
1301 memcpy(&storage, e, sizeof *e);
1302 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1303 memcpy(&(*enums)[v], &storage, sizeof storage);
1304 }
1305 }
1306 }
1307
1308done:
1309 return ret;
1310}
1311
1312/**
1313 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1314 * @param[in] ctx Compile context.
1315 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1316 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
1317 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
1318 * @param[in] context_name Name of the context node or referencing typedef for logging.
1319 * @param[in] type_p Parsed type to compile.
1320 * @param[in] basetype Base YANG built-in type of the type to compile.
1321 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1322 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1323 * @param[out] type Newly created type structure with the filled information about the type.
1324 * @return LY_ERR value.
1325 */
1326static LY_ERR
1327lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
1328 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, LY_DATA_TYPE basetype,
1329 const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
1330{
1331 LY_ERR ret = LY_SUCCESS;
1332 struct lysc_type_bin *bin;
1333 struct lysc_type_num *num;
1334 struct lysc_type_str *str;
1335 struct lysc_type_bits *bits;
1336 struct lysc_type_enum *enumeration;
1337 struct lysc_type_dec *dec;
1338 struct lysc_type_identityref *idref;
1339 struct lysc_type_leafref *lref;
1340 struct lysc_type_union *un, *un_aux;
1341
1342 switch (basetype) {
1343 case LY_TYPE_BINARY:
1344 bin = (struct lysc_type_bin *)(*type);
1345
1346 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1347 if (type_p->length) {
1348 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1349 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1350 if (!tpdfname) {
1351 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, cleanup);
1352 }
1353 }
1354 break;
1355 case LY_TYPE_BITS:
1356 /* RFC 7950 9.7 - bits */
1357 bits = (struct lysc_type_bits *)(*type);
1358 if (type_p->bits) {
1359 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1360 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1361 (struct lysc_type_bitenum_item **)&bits->bits));
1362 }
1363
1364 if (!base && !type_p->flags) {
1365 /* type derived from bits built-in type must contain at least one bit */
1366 if (tpdfname) {
1367 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
1368 } else {
1369 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
1370 }
1371 return LY_EVALID;
1372 }
1373 break;
1374 case LY_TYPE_DEC64:
1375 dec = (struct lysc_type_dec *)(*type);
1376
1377 /* RFC 7950 9.3.4 - fraction-digits */
1378 if (!base) {
1379 if (!type_p->fraction_digits) {
1380 if (tpdfname) {
1381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
1382 } else {
1383 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
1384 }
1385 return LY_EVALID;
1386 }
1387 dec->fraction_digits = type_p->fraction_digits;
1388 } else {
1389 if (type_p->fraction_digits) {
1390 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1391 if (tpdfname) {
1392 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1393 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1394 tpdfname);
1395 } else {
1396 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1397 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1398 }
1399 return LY_EVALID;
1400 }
1401 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1402 }
1403
1404 /* RFC 7950 9.2.4 - range */
1405 if (type_p->range) {
1406 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1407 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1408 if (!tpdfname) {
1409 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, cleanup);
1410 }
1411 }
1412 break;
1413 case LY_TYPE_STRING:
1414 str = (struct lysc_type_str *)(*type);
1415
1416 /* RFC 7950 9.4.4 - length */
1417 if (type_p->length) {
1418 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1419 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1420 if (!tpdfname) {
1421 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, cleanup);
1422 }
1423 } else if (base && ((struct lysc_type_str *)base)->length) {
1424 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1425 }
1426
1427 /* RFC 7950 9.4.5 - pattern */
1428 if (type_p->patterns) {
1429 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1430 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1431 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1432 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1433 }
1434 break;
1435 case LY_TYPE_ENUM:
1436 enumeration = (struct lysc_type_enum *)(*type);
1437
1438 /* RFC 7950 9.6 - enum */
1439 if (type_p->enums) {
1440 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1441 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1442 }
1443
1444 if (!base && !type_p->flags) {
1445 /* type derived from enumerations built-in type must contain at least one enum */
1446 if (tpdfname) {
1447 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
1448 } else {
1449 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
1450 }
1451 return LY_EVALID;
1452 }
1453 break;
1454 case LY_TYPE_INT8:
1455 case LY_TYPE_UINT8:
1456 case LY_TYPE_INT16:
1457 case LY_TYPE_UINT16:
1458 case LY_TYPE_INT32:
1459 case LY_TYPE_UINT32:
1460 case LY_TYPE_INT64:
1461 case LY_TYPE_UINT64:
1462 num = (struct lysc_type_num *)(*type);
1463
1464 /* RFC 6020 9.2.4 - range */
1465 if (type_p->range) {
1466 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1467 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1468 if (!tpdfname) {
1469 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, cleanup);
1470 }
1471 }
1472 break;
1473 case LY_TYPE_IDENT:
1474 idref = (struct lysc_type_identityref *)(*type);
1475
1476 /* RFC 7950 9.10.2 - base */
1477 if (type_p->bases) {
1478 if (base) {
1479 /* only the directly derived identityrefs can contain base specification */
1480 if (tpdfname) {
1481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1482 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1483 tpdfname);
1484 } else {
1485 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1486 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1487 }
1488 return LY_EVALID;
1489 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001490 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 +02001491 }
1492
1493 if (!base && !type_p->flags) {
1494 /* type derived from identityref built-in type must contain at least one base */
1495 if (tpdfname) {
1496 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
1497 } else {
1498 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
1499 }
1500 return LY_EVALID;
1501 }
1502 break;
1503 case LY_TYPE_LEAFREF:
1504 lref = (struct lysc_type_leafref *)*type;
1505
1506 /* RFC 7950 9.9.3 - require-instance */
1507 if (type_p->flags & LYS_SET_REQINST) {
1508 if (context_mod->version < LYS_VERSION_1_1) {
1509 if (tpdfname) {
1510 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1511 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1512 } else {
1513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1514 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1515 }
1516 return LY_EVALID;
1517 }
1518 lref->require_instance = type_p->require_instance;
1519 } else if (base) {
1520 /* inherit */
1521 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1522 } else {
1523 /* default is true */
1524 lref->require_instance = 1;
1525 }
1526 if (type_p->path) {
1527 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
1528 LY_CHECK_RET(lysc_prefixes_compile(type_p->path->expr, strlen(type_p->path->expr), type_p->pmod,
1529 &lref->prefixes));
1530 } else if (base) {
1531 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
1532 LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)base)->prefixes, &lref->prefixes));
1533 } else if (tpdfname) {
1534 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
1535 return LY_EVALID;
1536 } else {
1537 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
1538 return LY_EVALID;
1539 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001540 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001541 break;
1542 case LY_TYPE_INST:
1543 /* RFC 7950 9.9.3 - require-instance */
1544 if (type_p->flags & LYS_SET_REQINST) {
1545 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1546 } else {
1547 /* default is true */
1548 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1549 }
1550 break;
1551 case LY_TYPE_UNION:
1552 un = (struct lysc_type_union *)(*type);
1553
1554 /* RFC 7950 7.4 - type */
1555 if (type_p->types) {
1556 if (base) {
1557 /* only the directly derived union can contain types specification */
1558 if (tpdfname) {
1559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1560 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1561 tpdfname);
1562 } else {
1563 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1564 "Invalid type substatement for the type not directly derived from union built-in type.");
1565 }
1566 return LY_EVALID;
1567 }
1568 /* compile the type */
1569 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
1570 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
1571 LY_CHECK_RET(lys_compile_type(ctx, context_pnode, context_flags, context_mod, context_name,
1572 &type_p->types[u], &un->types[u + additional], NULL, NULL));
1573 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
1574 /* add space for additional types from the union subtype */
1575 un_aux = (struct lysc_type_union *)un->types[u + additional];
1576 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
1577 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
1578
1579 /* copy subtypes of the subtype union */
1580 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1581 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1582 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1583 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1584 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx); lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
1585 lref = (struct lysc_type_leafref *)un->types[u + additional];
1586
1587 lref->basetype = LY_TYPE_LEAFREF;
1588 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path));
1589 lref->refcount = 1;
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001590 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001591 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
1592 LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)un_aux->types[v])->prefixes,
1593 &lref->prefixes));
1594 /* TODO extensions */
1595
1596 } else {
1597 un->types[u + additional] = un_aux->types[v];
1598 ++un_aux->types[v]->refcount;
1599 }
1600 ++additional;
1601 LY_ARRAY_INCREMENT(un->types);
1602 }
1603 /* compensate u increment in main loop */
1604 --additional;
1605
1606 /* free the replaced union subtype */
1607 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1608 } else {
1609 LY_ARRAY_INCREMENT(un->types);
1610 }
1611 }
1612 }
1613
1614 if (!base && !type_p->flags) {
1615 /* type derived from union built-in type must contain at least one type */
1616 if (tpdfname) {
1617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
1618 } else {
1619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
1620 }
1621 return LY_EVALID;
1622 }
1623 break;
1624 case LY_TYPE_BOOL:
1625 case LY_TYPE_EMPTY:
1626 case LY_TYPE_UNKNOWN: /* just to complete switch */
1627 break;
1628 }
1629
1630 if (tpdfname) {
1631 switch (basetype) {
1632 case LY_TYPE_BINARY:
1633 type_p->compiled = *type;
1634 *type = calloc(1, sizeof(struct lysc_type_bin));
1635 break;
1636 case LY_TYPE_BITS:
1637 type_p->compiled = *type;
1638 *type = calloc(1, sizeof(struct lysc_type_bits));
1639 break;
1640 case LY_TYPE_DEC64:
1641 type_p->compiled = *type;
1642 *type = calloc(1, sizeof(struct lysc_type_dec));
1643 break;
1644 case LY_TYPE_STRING:
1645 type_p->compiled = *type;
1646 *type = calloc(1, sizeof(struct lysc_type_str));
1647 break;
1648 case LY_TYPE_ENUM:
1649 type_p->compiled = *type;
1650 *type = calloc(1, sizeof(struct lysc_type_enum));
1651 break;
1652 case LY_TYPE_INT8:
1653 case LY_TYPE_UINT8:
1654 case LY_TYPE_INT16:
1655 case LY_TYPE_UINT16:
1656 case LY_TYPE_INT32:
1657 case LY_TYPE_UINT32:
1658 case LY_TYPE_INT64:
1659 case LY_TYPE_UINT64:
1660 type_p->compiled = *type;
1661 *type = calloc(1, sizeof(struct lysc_type_num));
1662 break;
1663 case LY_TYPE_IDENT:
1664 type_p->compiled = *type;
1665 *type = calloc(1, sizeof(struct lysc_type_identityref));
1666 break;
1667 case LY_TYPE_LEAFREF:
1668 type_p->compiled = *type;
1669 *type = calloc(1, sizeof(struct lysc_type_leafref));
1670 break;
1671 case LY_TYPE_INST:
1672 type_p->compiled = *type;
1673 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1674 break;
1675 case LY_TYPE_UNION:
1676 type_p->compiled = *type;
1677 *type = calloc(1, sizeof(struct lysc_type_union));
1678 break;
1679 case LY_TYPE_BOOL:
1680 case LY_TYPE_EMPTY:
1681 case LY_TYPE_UNKNOWN: /* just to complete switch */
1682 break;
1683 }
1684 }
1685 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1686
1687cleanup:
1688 return ret;
1689}
1690
1691LY_ERR
1692lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
1693 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
1694 struct lysc_type **type, const char **units, struct lysp_qname **dflt)
1695{
1696 LY_ERR ret = LY_SUCCESS;
1697 ly_bool dummyloops = 0;
1698 struct type_context {
1699 const struct lysp_tpdf *tpdf;
1700 struct lysp_node *node;
1701 struct lysp_module *mod;
1702 } *tctx, *tctx_prev = NULL, *tctx_iter;
1703 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1704 struct lysc_type *base = NULL, *prev_type;
1705 struct ly_set tpdf_chain = {0};
1706
1707 (*type) = NULL;
1708 if (dflt) {
1709 *dflt = NULL;
1710 }
1711
1712 tctx = calloc(1, sizeof *tctx);
1713 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1714 for (ret = lysp_type_find(type_p->name, context_pnode, ctx->pmod, &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
1715 ret == LY_SUCCESS;
1716 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
1717 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
1718 if (basetype) {
1719 break;
1720 }
1721
1722 /* check status */
1723 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
1724 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
1725 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1726
1727 if (units && !*units) {
1728 /* inherit units */
1729 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1730 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1731 }
1732 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1733 /* inherit default */
1734 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1735 }
1736 if (dummyloops && (!units || *units) && dflt && *dflt) {
1737 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1738 break;
1739 }
1740
1741 if (tctx->tpdf->type.compiled) {
1742 /* it is not necessary to continue, the rest of the chain was already compiled,
1743 * but we still may need to inherit default and units values, so start dummy loops */
1744 basetype = tctx->tpdf->type.compiled->basetype;
1745 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1746 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1747
1748 if ((units && !*units) || (dflt && !*dflt)) {
1749 dummyloops = 1;
1750 goto preparenext;
1751 } else {
1752 tctx = NULL;
1753 break;
1754 }
1755 }
1756
1757 /* circular typedef reference detection */
1758 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1759 /* local part */
1760 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
1761 if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) {
1762 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1763 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1764 free(tctx);
1765 ret = LY_EVALID;
1766 goto cleanup;
1767 }
1768 }
1769 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1770 /* global part for unions corner case */
1771 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
1772 if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) {
1773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1774 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1775 free(tctx);
1776 ret = LY_EVALID;
1777 goto cleanup;
1778 }
1779 }
1780
1781 /* store information for the following processing */
1782 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1783 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1784
1785preparenext:
1786 /* prepare next loop */
1787 tctx_prev = tctx;
1788 tctx = calloc(1, sizeof *tctx);
1789 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1790 }
1791 free(tctx);
1792
1793 /* allocate type according to the basetype */
1794 switch (basetype) {
1795 case LY_TYPE_BINARY:
1796 *type = calloc(1, sizeof(struct lysc_type_bin));
1797 break;
1798 case LY_TYPE_BITS:
1799 *type = calloc(1, sizeof(struct lysc_type_bits));
1800 break;
1801 case LY_TYPE_BOOL:
1802 case LY_TYPE_EMPTY:
1803 *type = calloc(1, sizeof(struct lysc_type));
1804 break;
1805 case LY_TYPE_DEC64:
1806 *type = calloc(1, sizeof(struct lysc_type_dec));
1807 break;
1808 case LY_TYPE_ENUM:
1809 *type = calloc(1, sizeof(struct lysc_type_enum));
1810 break;
1811 case LY_TYPE_IDENT:
1812 *type = calloc(1, sizeof(struct lysc_type_identityref));
1813 break;
1814 case LY_TYPE_INST:
1815 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1816 break;
1817 case LY_TYPE_LEAFREF:
1818 *type = calloc(1, sizeof(struct lysc_type_leafref));
1819 break;
1820 case LY_TYPE_STRING:
1821 *type = calloc(1, sizeof(struct lysc_type_str));
1822 break;
1823 case LY_TYPE_UNION:
1824 *type = calloc(1, sizeof(struct lysc_type_union));
1825 break;
1826 case LY_TYPE_INT8:
1827 case LY_TYPE_UINT8:
1828 case LY_TYPE_INT16:
1829 case LY_TYPE_UINT16:
1830 case LY_TYPE_INT32:
1831 case LY_TYPE_UINT32:
1832 case LY_TYPE_INT64:
1833 case LY_TYPE_UINT64:
1834 *type = calloc(1, sizeof(struct lysc_type_num));
1835 break;
1836 case LY_TYPE_UNKNOWN:
1837 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1838 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1839 ret = LY_EVALID;
1840 goto cleanup;
1841 }
1842 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1843 if (~type_substmt_map[basetype] & type_p->flags) {
1844 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
1845 ly_data_type2str[basetype]);
1846 free(*type);
1847 (*type) = NULL;
1848 ret = LY_EVALID;
1849 goto cleanup;
1850 }
1851
1852 /* get restrictions from the referred typedefs */
1853 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1854 tctx = (struct type_context *)tpdf_chain.objs[u];
1855
1856 /* remember the typedef context for circular check */
1857 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1858 LY_CHECK_GOTO(ret, cleanup);
1859
1860 if (tctx->tpdf->type.compiled) {
1861 base = tctx->tpdf->type.compiled;
1862 continue;
1863 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1864 /* no change, just use the type information from the base */
1865 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1866 ++base->refcount;
1867 continue;
1868 }
1869
1870 ++(*type)->refcount;
1871 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
1872 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
1873 tctx->tpdf->name, ly_data_type2str[basetype]);
1874 ret = LY_EVALID;
1875 goto cleanup;
1876 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
1877 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1878 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1879 tctx->tpdf->name, tctx->tpdf->dflt.str);
1880 ret = LY_EVALID;
1881 goto cleanup;
1882 }
1883
1884 (*type)->basetype = basetype;
1885 /* TODO user type plugins */
1886 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1887 prev_type = *type;
1888 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name,
1889 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1890 LY_CHECK_GOTO(ret, cleanup);
1891 base = prev_type;
1892 }
1893 /* remove the processed typedef contexts from the stack for circular check */
1894 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1895
1896 /* process the type definition in leaf */
1897 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1898 /* get restrictions from the node itself */
1899 (*type)->basetype = basetype;
1900 /* TODO user type plugins */
1901 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1902 ++(*type)->refcount;
1903 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_mod, context_name, type_p, basetype, NULL,
1904 base, type);
1905 LY_CHECK_GOTO(ret, cleanup);
1906 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1907 /* no specific restriction in leaf's type definition, copy from the base */
1908 free(*type);
1909 (*type) = base;
1910 ++(*type)->refcount;
1911 }
1912
1913 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
1914
1915cleanup:
1916 ly_set_erase(&tpdf_chain, free);
1917 return ret;
1918}
1919
1920/**
1921 * @brief Compile status information of the given node.
1922 *
1923 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
1924 * has the status correctly set during the compilation.
1925 *
1926 * @param[in] ctx Compile context
1927 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
1928 * If the status was set explicitly on the node, it is already set in the flags value and we just check
1929 * the compatibility with the parent's status value.
1930 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
1931 * @return LY_ERR value.
1932 */
1933static LY_ERR
1934lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
1935{
1936 /* status - it is not inherited by specification, but it does not make sense to have
1937 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
1938 if (!((*node_flags) & LYS_STATUS_MASK)) {
1939 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
1940 if ((parent_flags & 0x3) != 0x3) {
1941 /* do not print the warning when inheriting status from uses - the uses_status value has a special
1942 * combination of bits (0x3) which marks the uses_status value */
1943 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
1944 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1945 }
1946 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
1947 } else {
1948 (*node_flags) |= LYS_STATUS_CURR;
1949 }
1950 } else if (parent_flags & LYS_STATUS_MASK) {
1951 /* check status compatibility with the parent */
1952 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
1953 if ((*node_flags) & LYS_STATUS_CURR) {
1954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1955 "A \"current\" status is in conflict with the parent's \"%s\" status.",
1956 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1957 } else { /* LYS_STATUS_DEPRC */
1958 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1959 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
1960 }
1961 return LY_EVALID;
1962 }
1963 }
1964 return LY_SUCCESS;
1965}
1966
1967/**
1968 * @brief Check uniqness of the node/action/notification name.
1969 *
1970 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
1971 * structures, but they share the namespace so we need to check their name collisions.
1972 *
1973 * @param[in] ctx Compile context.
1974 * @param[in] parent Parent of the nodes to check, can be NULL.
1975 * @param[in] name Name of the item to find in the given lists.
1976 * @param[in] exclude Node that was just added that should be excluded from the name checking.
1977 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
1978 */
1979static LY_ERR
1980lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
1981 const struct lysc_node *exclude)
1982{
1983 const struct lysc_node *iter, *iter2;
1984 const struct lysc_action *actions;
1985 const struct lysc_notif *notifs;
1986 uint32_t getnext_flags;
1987 LY_ARRAY_COUNT_TYPE u;
1988
1989#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
1990
1991 if (exclude->nodetype == LYS_CASE) {
1992 /* check restricted only to all the cases */
1993 assert(parent->nodetype == LYS_CHOICE);
1994 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
1995 if (CHECK_NODE(iter, exclude, name)) {
1996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
1997 return LY_EEXIST;
1998 }
1999 }
2000
2001 return LY_SUCCESS;
2002 }
2003
2004 /* no reason for our parent to be choice anymore */
2005 assert(!parent || (parent->nodetype != LYS_CHOICE));
2006
2007 if (parent && (parent->nodetype == LYS_CASE)) {
2008 /* move to the first data definition parent */
2009 parent = lysc_data_parent(parent);
2010 }
2011
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002012 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002013 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
2014 getnext_flags |= LYS_GETNEXT_OUTPUT;
2015 }
2016
2017 iter = NULL;
2018 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2019 if (CHECK_NODE(iter, exclude, name)) {
2020 goto error;
2021 }
2022
2023 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2024 if (iter->nodetype == LYS_CHOICE) {
2025 iter2 = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002026 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002027 if (CHECK_NODE(iter2, exclude, name)) {
2028 goto error;
2029 }
2030 }
2031 }
2032 }
2033
2034 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2035 LY_ARRAY_FOR(actions, u) {
2036 if (CHECK_NODE(&actions[u], exclude, name)) {
2037 goto error;
2038 }
2039 }
2040
2041 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2042 LY_ARRAY_FOR(notifs, u) {
2043 if (CHECK_NODE(&notifs[u], exclude, name)) {
2044 goto error;
2045 }
2046 }
2047 return LY_SUCCESS;
2048
2049error:
2050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
2051 return LY_EEXIST;
2052
2053#undef CHECK_NODE
2054}
2055
2056LY_ERR
2057lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, struct lysc_node *parent,
2058 struct lysc_action *action, uint16_t uses_status)
2059{
2060 LY_ERR ret = LY_SUCCESS;
2061 struct lysp_node *child_p, *dev_pnode = NULL, *dev_input_p = NULL, *dev_output_p = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002062 struct lysp_action_inout *inout_p;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002063 ly_bool not_supported, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002064 uint32_t opt_prev = ctx->options;
2065
2066 lysc_update_path(ctx, parent, action_p->name);
2067
2068 /* apply deviation on the action/RPC */
2069 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)action_p, parent, &dev_pnode, &not_supported));
2070 if (not_supported) {
2071 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002072 ret = LY_EDENIED;
2073 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002074 } else if (dev_pnode) {
2075 action_p = (struct lysp_action *)dev_pnode;
2076 }
2077
2078 /* member needed for uniqueness check lys_getnext() */
2079 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
2080 action->module = ctx->cur_mod;
2081 action->parent = parent;
2082
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002083 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 +02002084
2085 if (ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) {
2086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2087 "Action \"%s\" is placed inside %s.", action_p->name,
2088 ctx->options & LYS_COMPILE_RPC_MASK ? "another RPC/action" : "notification");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002089 ret = LY_EVALID;
2090 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002091 }
2092
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002093 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
2094
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002095 /* if-features */
2096 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, action_p->iffeatures, &enabled));
Michal Vasko12606ee2020-11-25 17:05:11 +01002097 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002098 ly_set_add(&ctx->disabled, action, 1, NULL);
2099 ctx->options |= LYS_COMPILE_DISABLED;
2100 }
2101
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002102 /* status - it is not inherited by specification, but it does not make sense to have
2103 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002104 ret = lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
2105 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002106
2107 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
2108 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
2109 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002110
2111 /* connect any action augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002112 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)action), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002113
2114 /* input */
2115 lysc_update_path(ctx, (struct lysc_node *)action, "input");
2116
2117 /* apply deviations on input */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002118 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->input,
2119 (struct lysc_node *)action, &dev_input_p, &not_supported), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002120 if (not_supported) {
2121 inout_p = NULL;
2122 } else if (dev_input_p) {
2123 inout_p = (struct lysp_action_inout *)dev_input_p;
2124 } else {
2125 inout_p = &action_p->input;
2126 }
2127
2128 if (inout_p) {
2129 action->input.nodetype = LYS_INPUT;
Michal Vasko5347e3a2020-11-03 17:14:57 +01002130 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->input.musts, lys_compile_must, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002131 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
2132 ctx->options |= LYS_COMPILE_RPC_INPUT;
2133
2134 /* connect any input augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002135 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)&action->input), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002136
2137 LY_LIST_FOR(inout_p->data, child_p) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002138 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 +02002139 }
2140 ctx->options = opt_prev;
2141 }
2142
2143 lysc_update_path(ctx, NULL, NULL);
2144
2145 /* output */
2146 lysc_update_path(ctx, (struct lysc_node *)action, "output");
2147
2148 /* apply deviations on output */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002149 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->output,
2150 (struct lysc_node *)action, &dev_output_p, &not_supported), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002151 if (not_supported) {
2152 inout_p = NULL;
2153 } else if (dev_output_p) {
2154 inout_p = (struct lysp_action_inout *)dev_output_p;
2155 } else {
2156 inout_p = &action_p->output;
2157 }
2158
2159 if (inout_p) {
2160 action->output.nodetype = LYS_OUTPUT;
Michal Vasko5347e3a2020-11-03 17:14:57 +01002161 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->output.musts, lys_compile_must, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002162 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
2163 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
2164
2165 /* connect any output augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002166 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)&action->output), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002167
2168 LY_LIST_FOR(inout_p->data, child_p) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002169 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 +02002170 }
2171 ctx->options = opt_prev;
2172 }
2173
2174 lysc_update_path(ctx, NULL, NULL);
2175
Michal Vasko208a04a2020-10-21 15:17:12 +02002176 /* wait with extensions compilation until all the children are compiled */
2177 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
2178
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002179 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002180 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002181 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002182 LY_CHECK_GOTO(ret, cleanup);
2183 }
2184
2185 lysc_update_path(ctx, NULL, NULL);
2186
2187cleanup:
2188 lysp_dev_node_free(ctx->ctx, dev_pnode);
2189 lysp_dev_node_free(ctx->ctx, dev_input_p);
2190 lysp_dev_node_free(ctx->ctx, dev_output_p);
2191 ctx->options = opt_prev;
2192 return ret;
2193}
2194
2195LY_ERR
2196lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, struct lysc_node *parent, struct lysc_notif *notif,
2197 uint16_t uses_status)
2198{
2199 LY_ERR ret = LY_SUCCESS;
2200 struct lysp_node *child_p, *dev_pnode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002201 ly_bool not_supported, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002202 uint32_t opt_prev = ctx->options;
2203
2204 lysc_update_path(ctx, parent, notif_p->name);
2205
2206 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)notif_p, parent, &dev_pnode, &not_supported));
2207 if (not_supported) {
2208 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002209 ret = LY_EDENIED;
2210 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002211 } else if (dev_pnode) {
2212 notif_p = (struct lysp_notif *)dev_pnode;
2213 }
2214
2215 /* member needed for uniqueness check lys_getnext() */
2216 notif->nodetype = LYS_NOTIF;
2217 notif->module = ctx->cur_mod;
2218 notif->parent = parent;
2219
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002220 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 +02002221
2222 if (ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) {
2223 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2224 "Notification \"%s\" is placed inside %s.", notif_p->name,
2225 ctx->options & LYS_COMPILE_RPC_MASK ? "RPC/action" : "another notification");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002226 ret = LY_EVALID;
2227 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002228 }
2229
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002230 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
2231
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002232 /* if-features */
2233 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, notif_p->iffeatures, &enabled));
Michal Vasko12606ee2020-11-25 17:05:11 +01002234 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002235 ly_set_add(&ctx->disabled, notif, 1, NULL);
2236 ctx->options |= LYS_COMPILE_DISABLED;
2237 }
2238
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002239 /* status - it is not inherited by specification, but it does not make sense to have
2240 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2241 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
2242 LY_CHECK_GOTO(ret, cleanup);
2243
2244 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
2245 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
2246 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Michal Vasko5347e3a2020-11-03 17:14:57 +01002247 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002248 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002249 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002250 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002251 LY_CHECK_GOTO(ret, cleanup);
2252 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002253
2254 ctx->options |= LYS_COMPILE_NOTIFICATION;
2255
2256 /* connect any notification augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002257 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)notif), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002258
2259 LY_LIST_FOR(notif_p->data, child_p) {
2260 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status, NULL);
2261 LY_CHECK_GOTO(ret, cleanup);
2262 }
2263
Michal Vasko208a04a2020-10-21 15:17:12 +02002264 /* wait with extension compilation until all the children are compiled */
2265 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
2266
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002267 lysc_update_path(ctx, NULL, NULL);
2268
2269cleanup:
2270 lysp_dev_node_free(ctx->ctx, dev_pnode);
2271 ctx->options = opt_prev;
2272 return ret;
2273}
2274
2275/**
2276 * @brief Compile parsed container node information.
2277 * @param[in] ctx Compile context
2278 * @param[in] pnode Parsed container node.
2279 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2280 * is enriched with the container-specific information.
2281 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2282 */
2283static LY_ERR
2284lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2285{
2286 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2287 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2288 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002289 LY_ERR ret = LY_SUCCESS;
2290
2291 if (cont_p->presence) {
2292 /* explicit presence */
2293 cont->flags |= LYS_PRESENCE;
2294 } else if (cont_p->musts) {
2295 /* container with a must condition */
2296 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
2297 cont->flags |= LYS_PRESENCE;
2298 } else if (cont_p->when) {
2299 /* container with a when condition */
2300 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"when\" condition.", cont_p->name);
2301 cont->flags |= LYS_PRESENCE;
2302 } else if (cont_p->parent) {
2303 if (cont_p->parent->nodetype == LYS_CHOICE) {
2304 /* container is an implicit case, so its existence decides the existence of the whole case */
2305 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
2306 cont_p->name, cont_p->parent->name);
2307 cont->flags |= LYS_PRESENCE;
2308 } else if ((cont_p->parent->nodetype == LYS_CASE) &&
2309 (((struct lysp_node_case *)cont_p->parent)->child == pnode) && !cont_p->next) {
2310 /* container is the only node in a case, so its existence decides the existence of the whole case */
2311 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
2312 cont_p->name, cont_p->parent->name);
2313 cont->flags |= LYS_PRESENCE;
2314 }
2315 }
2316
2317 /* more cases when the container has meaning but is kept NP for convenience:
2318 * - when condition
2319 * - direct child action/notification
2320 */
2321
2322 LY_LIST_FOR(cont_p->child, child_p) {
2323 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2324 LY_CHECK_GOTO(ret, done);
2325 }
2326
Michal Vasko5347e3a2020-11-03 17:14:57 +01002327 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002328 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002329 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002330 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002331 LY_CHECK_GOTO(ret, done);
2332 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01002333 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, node, lys_compile_action, 0, ret, done);
2334 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, node, lys_compile_notif, 0, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002335
2336done:
2337 return ret;
2338}
2339
2340/*
2341 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2342 * @param[in] ctx Compile context.
2343 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2344 * @param[in] type_p Parsed type to compile.
2345 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2346 * @return LY_ERR value.
2347 */
2348static LY_ERR
2349lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2350 struct lysc_node_leaf *leaf)
2351{
2352 struct lysp_qname *dflt;
2353
2354 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->pmod, leaf->name, type_p, &leaf->type,
2355 leaf->units ? NULL : &leaf->units, &dflt));
2356
2357 /* store default value, if any */
2358 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2359 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2360 }
2361
Michal Vasko12606ee2020-11-25 17:05:11 +01002362 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002363 /* 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 +01002364 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002365 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002366 LY_ARRAY_COUNT_TYPE u;
2367 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2368 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2369 /* 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 +01002370 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002371 }
2372 }
2373 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2374 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2375 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2376 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2377 return LY_EVALID;
2378 }
2379 }
2380
2381 return LY_SUCCESS;
2382}
2383
2384/**
2385 * @brief Compile parsed leaf node information.
2386 * @param[in] ctx Compile context
2387 * @param[in] pnode Parsed leaf node.
2388 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2389 * is enriched with the leaf-specific information.
2390 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2391 */
2392static LY_ERR
2393lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2394{
2395 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2396 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002397 LY_ERR ret = LY_SUCCESS;
2398
Michal Vasko5347e3a2020-11-03 17:14:57 +01002399 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002400 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002401 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002402 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002403 LY_CHECK_GOTO(ret, done);
2404 }
2405 if (leaf_p->units) {
2406 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2407 leaf->flags |= LYS_SET_UNITS;
2408 }
2409
2410 /* compile type */
2411 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2412 LY_CHECK_GOTO(ret, done);
2413
2414 /* store/update default value */
2415 if (leaf_p->dflt.str) {
2416 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2417 leaf->flags |= LYS_SET_DFLT;
2418 }
2419
2420 /* checks */
2421 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
2422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2423 "Invalid mandatory leaf with a default value.");
2424 return LY_EVALID;
2425 }
2426
2427done:
2428 return ret;
2429}
2430
2431/**
2432 * @brief Compile parsed leaf-list node information.
2433 * @param[in] ctx Compile context
2434 * @param[in] pnode Parsed leaf-list node.
2435 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2436 * is enriched with the leaf-list-specific information.
2437 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2438 */
2439static LY_ERR
2440lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2441{
2442 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2443 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002444 LY_ERR ret = LY_SUCCESS;
2445
Michal Vasko5347e3a2020-11-03 17:14:57 +01002446 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002447 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002448 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002449 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002450 LY_CHECK_GOTO(ret, done);
2451 }
2452 if (llist_p->units) {
2453 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2454 llist->flags |= LYS_SET_UNITS;
2455 }
2456
2457 /* compile type */
2458 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2459 LY_CHECK_GOTO(ret, done);
2460
2461 /* store/update default values */
2462 if (llist_p->dflts) {
2463 if (ctx->pmod->version < LYS_VERSION_1_1) {
2464 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2465 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2466 return LY_EVALID;
2467 }
2468
2469 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2470 llist->flags |= LYS_SET_DFLT;
2471 }
2472
2473 llist->min = llist_p->min;
2474 if (llist->min) {
2475 llist->flags |= LYS_MAND_TRUE;
2476 }
2477 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2478
2479 /* checks */
2480 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
2481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2482 "Invalid mandatory leaf-list with default value(s).");
2483 return LY_EVALID;
2484 }
2485
2486 if (llist->min > llist->max) {
2487 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
2488 llist->min, llist->max);
2489 return LY_EVALID;
2490 }
2491
2492done:
2493 return ret;
2494}
2495
2496LY_ERR
2497lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2498 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2499 const struct lysc_node **target, uint16_t *result_flag)
2500{
2501 LY_ERR ret = LY_EVALID;
2502 const char *name, *prefix, *id;
2503 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002504 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002505 const char *nodeid_type;
2506 uint32_t getnext_extra_flag = 0;
2507 uint16_t current_nodetype = 0;
2508
2509 assert(nodeid);
2510 assert(target);
2511 assert(result_flag);
2512 *target = NULL;
2513 *result_flag = 0;
2514
2515 id = nodeid;
2516
2517 if (ctx_node) {
2518 /* descendant-schema-nodeid */
2519 nodeid_type = "descendant";
2520
2521 if (*id == '/') {
2522 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2523 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
2524 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2525 return LY_EVALID;
2526 }
2527 } else {
2528 /* absolute-schema-nodeid */
2529 nodeid_type = "absolute";
2530
2531 if (*id != '/') {
2532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2533 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
2534 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2535 return LY_EVALID;
2536 }
2537 ++id;
2538 }
2539
2540 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2541 if (prefix) {
2542 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2543 if (!mod) {
2544 /* module must always be found */
2545 assert(prefix);
2546 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2547 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
2548 nodeid_type, id - nodeid, nodeid, prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
2549 return LY_ENOTFOUND;
2550 }
2551 } else {
2552 switch (format) {
2553 case LY_PREF_SCHEMA:
2554 case LY_PREF_SCHEMA_RESOLVED:
2555 /* use the current module */
2556 mod = cur_mod;
2557 break;
2558 case LY_PREF_JSON:
2559 if (!ctx_node) {
2560 LOGINT_RET(ctx->ctx);
2561 }
2562 /* inherit the module of the previous context node */
2563 mod = ctx_node->module;
2564 break;
2565 case LY_PREF_XML:
2566 /* not really defined */
2567 LOGINT_RET(ctx->ctx);
2568 }
2569 }
2570
2571 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2572 /* move through input/output manually */
2573 if (mod != ctx_node->module) {
2574 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2575 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
2576 return LY_ENOTFOUND;
2577 }
2578 if (!ly_strncmp("input", name, name_len)) {
2579 ctx_node = (struct lysc_node *)&((struct lysc_action *)ctx_node)->input;
2580 } else if (!ly_strncmp("output", name, name_len)) {
2581 ctx_node = (struct lysc_node *)&((struct lysc_action *)ctx_node)->output;
2582 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2583 } else {
2584 /* only input or output is valid */
2585 ctx_node = NULL;
2586 }
2587 } else {
2588 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002589 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002590 getnext_extra_flag = 0;
2591 }
2592 if (!ctx_node) {
2593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2594 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
2595 return LY_ENOTFOUND;
2596 }
2597 current_nodetype = ctx_node->nodetype;
2598
2599 if (current_nodetype == LYS_NOTIF) {
2600 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2601 } else if (current_nodetype == LYS_INPUT) {
2602 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2603 } else if (current_nodetype == LYS_OUTPUT) {
2604 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2605 }
2606
2607 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2608 break;
2609 }
2610 if (*id != '/') {
2611 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2612 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
2613 nodeid_type, id - nodeid + 1, nodeid);
2614 return LY_EVALID;
2615 }
2616 ++id;
2617 }
2618
2619 if (ret == LY_SUCCESS) {
2620 *target = ctx_node;
2621 if (nodetype && !(current_nodetype & nodetype)) {
2622 return LY_EDENIED;
2623 }
2624 } else {
2625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2626 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
2627 nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2628 }
2629
2630 return ret;
2631}
2632
2633/**
2634 * @brief Compile information about list's uniques.
2635 * @param[in] ctx Compile context.
2636 * @param[in] uniques Sized array list of unique statements.
2637 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2638 * @return LY_ERR value.
2639 */
2640static LY_ERR
2641lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2642{
2643 LY_ERR ret = LY_SUCCESS;
2644 struct lysc_node_leaf **key, ***unique;
2645 struct lysc_node *parent;
2646 const char *keystr, *delim;
2647 size_t len;
2648 LY_ARRAY_COUNT_TYPE v;
2649 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2650 uint16_t flags;
2651
2652 LY_ARRAY_FOR(uniques, v) {
2653 config = -1;
2654 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2655 keystr = uniques[v].str;
2656 while (keystr) {
2657 delim = strpbrk(keystr, " \t\n");
2658 if (delim) {
2659 len = delim - keystr;
2660 while (isspace(*delim)) {
2661 ++delim;
2662 }
2663 } else {
2664 len = strlen(keystr);
2665 }
2666
2667 /* unique node must be present */
2668 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
2669 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, uniques[v].mod->mod,
2670 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2671 if (ret != LY_SUCCESS) {
2672 if (ret == LY_EDENIED) {
2673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2674 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
2675 len, keystr, lys_nodetype2str((*key)->nodetype));
2676 }
2677 return LY_EVALID;
2678 } else if (flags) {
2679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2680 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
2681 len, keystr, flags & LYS_COMPILE_NOTIFICATION ? "notification" : "RPC/action");
2682 return LY_EVALID;
2683 }
2684
2685 /* all referenced leafs must be of the same config type */
2686 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2687 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
2688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2689 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2690 return LY_EVALID;
2691 } else if ((*key)->flags & LYS_CONFIG_W) {
2692 config = 1;
2693 } else { /* LYS_CONFIG_R */
2694 config = 0;
2695 }
2696
2697 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
2698 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
2699 if (parent->nodetype == LYS_LIST) {
2700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2701 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
2702 return LY_EVALID;
2703 }
2704 }
2705
2706 /* check status */
2707 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2708 (*key)->flags, (*key)->module, (*key)->name));
2709
2710 /* mark leaf as unique */
2711 (*key)->flags |= LYS_UNIQUE;
2712
2713 /* next unique value in line */
2714 keystr = delim;
2715 }
2716 /* next unique definition */
2717 }
2718
2719 return LY_SUCCESS;
2720}
2721
2722/**
2723 * @brief Compile parsed list node information.
2724 * @param[in] ctx Compile context
2725 * @param[in] pnode Parsed list node.
2726 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2727 * is enriched with the list-specific information.
2728 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2729 */
2730static LY_ERR
2731lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2732{
2733 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
2734 struct lysc_node_list *list = (struct lysc_node_list *)node;
2735 struct lysp_node *child_p;
2736 struct lysc_node_leaf *key, *prev_key = NULL;
2737 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002738 const char *keystr, *delim;
2739 LY_ERR ret = LY_SUCCESS;
2740
2741 list->min = list_p->min;
2742 if (list->min) {
2743 list->flags |= LYS_MAND_TRUE;
2744 }
2745 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2746
2747 LY_LIST_FOR(list_p->child, child_p) {
2748 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
2749 }
2750
Michal Vasko5347e3a2020-11-03 17:14:57 +01002751 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002752 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002753 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002754 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002755 }
2756
2757 /* keys */
2758 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
2759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
2760 return LY_EVALID;
2761 }
2762
2763 /* find all the keys (must be direct children) */
2764 keystr = list_p->key;
2765 if (!keystr) {
2766 /* keyless list */
2767 list->flags |= LYS_KEYLESS;
2768 }
2769 while (keystr) {
2770 delim = strpbrk(keystr, " \t\n");
2771 if (delim) {
2772 len = delim - keystr;
2773 while (isspace(*delim)) {
2774 ++delim;
2775 }
2776 } else {
2777 len = strlen(keystr);
2778 }
2779
2780 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002781 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
2782 if (!key) {
2783 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 +02002784 return LY_EVALID;
2785 }
2786 /* keys must be unique */
2787 if (key->flags & LYS_KEY) {
2788 /* the node was already marked as a key */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002790 return LY_EVALID;
2791 }
2792
2793 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
2794 /* key must have the same config flag as the list itself */
2795 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
2796 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
2797 return LY_EVALID;
2798 }
2799 if (ctx->pmod->version < LYS_VERSION_1_1) {
2800 /* YANG 1.0 denies key to be of empty type */
2801 if (key->type->basetype == LY_TYPE_EMPTY) {
2802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2803 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
2804 return LY_EVALID;
2805 }
2806 } else {
2807 /* when and if-feature are illegal on list keys */
2808 if (key->when) {
2809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2810 "List's key must not have any \"when\" statement.");
2811 return LY_EVALID;
2812 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002813 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002814 }
2815
2816 /* check status */
2817 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2818 key->flags, key->module, key->name));
2819
2820 /* ignore default values of the key */
2821 if (key->dflt) {
2822 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
2823 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
2824 free(key->dflt);
2825 key->dflt = NULL;
2826 }
2827 /* mark leaf as key */
2828 key->flags |= LYS_KEY;
2829
2830 /* move it to the correct position */
2831 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
2832 /* fix links in closest previous siblings of the key */
2833 if (key->next) {
2834 key->next->prev = key->prev;
2835 } else {
2836 /* last child */
2837 list->child->prev = key->prev;
2838 }
2839 if (key->prev->next) {
2840 key->prev->next = key->next;
2841 }
2842 /* fix links in the key */
2843 if (prev_key) {
2844 key->prev = (struct lysc_node *)prev_key;
2845 key->next = prev_key->next;
2846 } else {
2847 key->prev = list->child->prev;
2848 key->next = list->child;
2849 }
2850 /* fix links in closes future siblings of the key */
2851 if (prev_key) {
2852 if (prev_key->next) {
2853 prev_key->next->prev = (struct lysc_node *)key;
2854 } else {
2855 list->child->prev = (struct lysc_node *)key;
2856 }
2857 prev_key->next = (struct lysc_node *)key;
2858 } else {
2859 list->child->prev = (struct lysc_node *)key;
2860 }
2861 /* fix links in parent */
2862 if (!key->prev->next) {
2863 list->child = (struct lysc_node *)key;
2864 }
2865 }
2866
2867 /* next key value */
2868 prev_key = key;
2869 keystr = delim;
2870 lysc_update_path(ctx, NULL, NULL);
2871 }
2872
2873 /* uniques */
2874 if (list_p->uniques) {
2875 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
2876 }
2877
Michal Vasko5347e3a2020-11-03 17:14:57 +01002878 COMPILE_OP_ARRAY_GOTO(ctx, list_p->actions, list->actions, node, lys_compile_action, 0, ret, done);
2879 COMPILE_OP_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, node, lys_compile_notif, 0, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002880
2881 /* checks */
2882 if (list->min > list->max) {
2883 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
2884 list->min, list->max);
2885 return LY_EVALID;
2886 }
2887
2888done:
2889 return ret;
2890}
2891
2892/**
2893 * @brief Do some checks and set the default choice's case.
2894 *
2895 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
2896 *
2897 * @param[in] ctx Compile context.
2898 * @param[in] dflt Name of the default branch. Can even contain a prefix.
2899 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
2900 * @return LY_ERR value.
2901 */
2902static LY_ERR
2903lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
2904{
2905 struct lysc_node *iter, *node = (struct lysc_node *)ch;
2906 const struct lys_module *mod;
2907 const char *prefix = NULL, *name;
2908 size_t prefix_len = 0;
2909
2910 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
2911 name = strchr(dflt->str, ':');
2912 if (name) {
2913 prefix = dflt->str;
2914 prefix_len = name - prefix;
2915 ++name;
2916 } else {
2917 name = dflt->str;
2918 }
2919 if (prefix) {
2920 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
2921 if (!mod) {
2922 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
2923 "in imports of \"%s\".", prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
2924 return LY_EVALID;
2925 }
2926 } else {
2927 mod = node->module;
2928 }
2929
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002930 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 +02002931 if (!ch->dflt) {
2932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2933 "Default case \"%s\" not found.", dflt->str);
2934 return LY_EVALID;
2935 }
2936
2937 /* no mandatory nodes directly under the default case */
2938 LY_LIST_FOR(ch->dflt->child, iter) {
2939 if (iter->parent != (struct lysc_node *)ch->dflt) {
2940 break;
2941 }
2942 if (iter->flags & LYS_MAND_TRUE) {
2943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2944 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
2945 return LY_EVALID;
2946 }
2947 }
2948
2949 if (ch->flags & LYS_MAND_TRUE) {
2950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
2951 return LY_EVALID;
2952 }
2953
2954 ch->dflt->flags |= LYS_SET_DFLT;
2955 return LY_SUCCESS;
2956}
2957
2958LY_ERR
2959lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
2960 struct ly_set *child_set)
2961{
2962 LY_ERR ret = LY_SUCCESS;
2963 struct lysp_node *child_p_next = child_p->next;
2964 struct lysp_node_case *cs_p;
2965
2966 if (child_p->nodetype == LYS_CASE) {
2967 /* standard case under choice */
2968 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
2969 } else {
2970 /* we need the implicit case first, so create a fake parsed case */
2971 cs_p = calloc(1, sizeof *cs_p);
2972 cs_p->nodetype = LYS_CASE;
2973 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
2974 cs_p->child = child_p;
2975
2976 /* make the child the only case child */
2977 child_p->next = NULL;
2978
2979 /* compile it normally */
2980 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
2981
2982free_fake_node:
2983 /* free the fake parsed node and correct pointers back */
2984 cs_p->child = NULL;
2985 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
2986 child_p->next = child_p_next;
2987 }
2988
2989 return ret;
2990}
2991
2992/**
2993 * @brief Compile parsed choice node information.
2994 *
2995 * @param[in] ctx Compile context
2996 * @param[in] pnode Parsed choice node.
2997 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2998 * is enriched with the choice-specific information.
2999 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3000 */
3001static LY_ERR
3002lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3003{
3004 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3005 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3006 struct lysp_node *child_p;
3007 LY_ERR ret = LY_SUCCESS;
3008
3009 assert(node->nodetype == LYS_CHOICE);
3010
3011 LY_LIST_FOR(ch_p->child, child_p) {
3012 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3013 }
3014
3015 /* default branch */
3016 if (ch_p->dflt.str) {
3017 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3018 }
3019
3020 return ret;
3021}
3022
3023/**
3024 * @brief Compile parsed anydata or anyxml node information.
3025 * @param[in] ctx Compile context
3026 * @param[in] pnode Parsed anydata or anyxml node.
3027 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3028 * is enriched with the any-specific information.
3029 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3030 */
3031static LY_ERR
3032lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3033{
3034 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3035 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003036 LY_ERR ret = LY_SUCCESS;
3037
Michal Vasko5347e3a2020-11-03 17:14:57 +01003038 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003039 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003040 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003041 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003042 LY_CHECK_GOTO(ret, done);
3043 }
3044
Michal Vasko75620aa2020-10-21 09:25:51 +02003045 if (!(ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) && (any->flags & LYS_CONFIG_W)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003046 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3047 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3048 }
3049done:
3050 return ret;
3051}
3052
3053/**
3054 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
3055 * keep specific order of augments targetting the same node.
3056 *
3057 * @param[in] ctx Compile context
3058 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3059 * the choice itself is expected instead of a specific case node.
3060 * @param[in] node Schema node to connect into the list.
3061 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3062 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
3063 */
3064static LY_ERR
3065lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
3066{
3067 struct lysc_node **children, *anchor = NULL;
3068 int insert_after = 0;
3069
3070 node->parent = parent;
3071
3072 if (parent) {
3073 if (parent->nodetype == LYS_CHOICE) {
3074 assert(node->nodetype == LYS_CASE);
3075 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
3076 } else {
3077 children = lysc_node_children_p(parent, ctx->options);
3078 }
3079 assert(children);
3080
3081 if (!(*children)) {
3082 /* first child */
3083 *children = node;
3084 } else if (node->flags & LYS_KEY) {
3085 /* special handling of adding keys */
3086 assert(node->module == parent->module);
3087 anchor = *children;
3088 if (anchor->flags & LYS_KEY) {
3089 while ((anchor->flags & LYS_KEY) && anchor->next) {
3090 anchor = anchor->next;
3091 }
3092 /* insert after the last key */
3093 insert_after = 1;
3094 } /* else insert before anchor (at the beginning) */
3095 } else if ((*children)->prev->module == node->module) {
3096 /* last child is from the same module, keep the order and insert at the end */
3097 anchor = (*children)->prev;
3098 insert_after = 1;
3099 } else if (parent->module == node->module) {
3100 /* adding module child after some augments were connected */
3101 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
3102 } else {
3103 /* some augments are already connected and we are connecting new ones,
3104 * keep module name order and insert the node into the children list */
3105 anchor = *children;
3106 do {
3107 anchor = anchor->prev;
3108
3109 /* check that we have not found the last augment node from our module or
3110 * the first augment node from a "smaller" module or
3111 * the first node from a local module */
3112 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
3113 (anchor->module == parent->module)) {
3114 /* insert after */
3115 insert_after = 1;
3116 break;
3117 }
3118
3119 /* we have traversed all the nodes, insert before anchor (as the first node) */
3120 } while (anchor->prev->next);
3121 }
3122
3123 /* insert */
3124 if (anchor) {
3125 if (insert_after) {
3126 node->next = anchor->next;
3127 node->prev = anchor;
3128 anchor->next = node;
3129 if (node->next) {
3130 /* middle node */
3131 node->next->prev = node;
3132 } else {
3133 /* last node */
3134 (*children)->prev = node;
3135 }
3136 } else {
3137 node->next = anchor;
3138 node->prev = anchor->prev;
3139 anchor->prev = node;
3140 if (anchor == *children) {
3141 /* first node */
3142 *children = node;
3143 } else {
3144 /* middle node */
3145 node->prev->next = node;
3146 }
3147 }
3148 }
3149
3150 /* check the name uniqueness (even for an only child, it may be in case) */
3151 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
3152 return LY_EEXIST;
3153 }
3154 } else {
3155 /* top-level element */
3156 if (!ctx->cur_mod->compiled->data) {
3157 ctx->cur_mod->compiled->data = node;
3158 } else {
3159 /* insert at the end of the module's top-level nodes list */
3160 ctx->cur_mod->compiled->data->prev->next = node;
3161 node->prev = ctx->cur_mod->compiled->data->prev;
3162 ctx->cur_mod->compiled->data->prev = node;
3163 }
3164
3165 /* check the name uniqueness on top-level */
3166 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
3167 return LY_EEXIST;
3168 }
3169 }
3170
3171 return LY_SUCCESS;
3172}
3173
3174/**
3175 * @brief Prepare the case structure in choice node for the new data node.
3176 *
3177 * 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
3178 * created in the choice when the first child was processed.
3179 *
3180 * @param[in] ctx Compile context.
3181 * @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,
3182 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3183 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3184 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3185 * @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,
3186 * it is linked from the case structure only in case it is its first child.
3187 */
3188static LY_ERR
3189lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3190{
3191 struct lysp_node *child_p;
3192 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3193
3194 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3195 /* we have to add an implicit case node into the parent choice */
3196 } else if (pnode->nodetype == LYS_CASE) {
3197 /* explicit parent case */
3198 LY_LIST_FOR(cs_p->child, child_p) {
3199 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3200 }
3201 } else {
3202 LOGINT_RET(ctx->ctx);
3203 }
3204
3205 return LY_SUCCESS;
3206}
3207
3208void
3209lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3210{
3211 struct lysc_node *iter;
3212
3213 if (add) { /* set flag */
3214 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3215 parent = parent->parent) {
3216 parent->flags |= LYS_MAND_TRUE;
3217 }
3218 } else { /* unset flag */
3219 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
3220 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
3221 if (iter->flags & LYS_MAND_TRUE) {
3222 /* there is another mandatory node */
3223 return;
3224 }
3225 }
3226 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3227 parent->flags &= ~LYS_MAND_TRUE;
3228 }
3229 }
3230}
3231
3232/**
3233 * @brief Find grouping for a uses.
3234 *
3235 * @param[in] ctx Compile context.
3236 * @param[in] uses_p Parsed uses node.
3237 * @param[out] gpr_p Found grouping on success.
3238 * @param[out] grp_pmod Module of @p grp_p on success.
3239 * @return LY_ERR value.
3240 */
3241static LY_ERR
3242lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
3243 struct lysp_module **grp_pmod)
3244{
3245 struct lysp_node *pnode;
3246 struct lysp_grp *grp;
3247 LY_ARRAY_COUNT_TYPE u, v;
3248 const char *id, *name, *prefix, *local_pref;
3249 size_t prefix_len, name_len;
3250 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003251 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003252
3253 *grp_p = NULL;
3254 *grp_pmod = NULL;
3255
3256 /* search for the grouping definition */
3257 id = uses_p->name;
3258 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3259 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3260 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3261 /* current module, search local groupings first */
3262 pmod = ctx->pmod;
3263 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
3264 grp = (struct lysp_grp *)lysp_node_groupings(pnode);
3265 LY_ARRAY_FOR(grp, u) {
3266 if (!strcmp(grp[u].name, name)) {
3267 grp = &grp[u];
3268 found = ctx->pmod;
3269 break;
3270 }
3271 }
3272 }
3273 } else {
3274 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003275 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003276 if (!mod) {
3277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3278 "Invalid prefix used for grouping reference.", uses_p->name);
3279 return LY_EVALID;
3280 }
3281 pmod = mod->parsed;
3282 }
3283
3284 if (!found) {
3285 /* search in top-level groupings of the main module ... */
3286 grp = pmod->groupings;
3287 LY_ARRAY_FOR(grp, u) {
3288 if (!strcmp(grp[u].name, name)) {
3289 grp = &grp[u];
3290 found = pmod;
3291 break;
3292 }
3293 }
3294 if (!found) {
3295 /* ... and all the submodules */
3296 LY_ARRAY_FOR(pmod->includes, u) {
3297 grp = pmod->includes[u].submodule->groupings;
3298 LY_ARRAY_FOR(grp, v) {
3299 if (!strcmp(grp[v].name, name)) {
3300 grp = &grp[v];
3301 found = (struct lysp_module *)pmod->includes[u].submodule;
3302 break;
3303 }
3304 }
3305 if (found) {
3306 break;
3307 }
3308 }
3309 }
3310 }
3311 if (!found) {
3312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3313 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3314 return LY_EVALID;
3315 }
3316
3317 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3318 /* remember that the grouping is instantiated to avoid its standalone validation */
3319 grp->flags |= LYS_USED_GRP;
3320 }
3321
3322 *grp_p = grp;
3323 *grp_pmod = found;
3324 return LY_SUCCESS;
3325}
3326
3327/**
3328 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3329 * If present, also apply uses's modificators.
3330 *
3331 * @param[in] ctx Compile context
3332 * @param[in] uses_p Parsed uses schema node.
3333 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3334 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3335 * the compile context.
3336 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3337 */
3338static LY_ERR
3339lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3340{
3341 struct lysp_node *pnode;
3342 struct lysc_node *child;
3343 struct lysp_grp *grp = NULL;
3344 uint32_t i, grp_stack_count;
3345 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3346 LY_ERR ret = LY_SUCCESS;
3347 struct lysc_when *when_shared = NULL;
3348 LY_ARRAY_COUNT_TYPE u;
3349 struct lysc_notif **notifs = NULL;
3350 struct lysc_action **actions = NULL;
3351 struct ly_set uses_child_set = {0};
3352
3353 /* find the referenced grouping */
3354 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3355
3356 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3357 grp_stack_count = ctx->groupings.count;
3358 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3359 if (grp_stack_count == ctx->groupings.count) {
3360 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
3361 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3362 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3363 return LY_EVALID;
3364 }
3365
3366 /* check status */
3367 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3368 LY_CHECK_GOTO(ret, cleanup);
3369
3370 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3371 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3372 LY_CHECK_GOTO(ret, cleanup);
3373
3374 /* switch context's parsed module being processed */
3375 ctx->pmod = grp_mod;
3376
3377 /* compile data nodes */
3378 LY_LIST_FOR(grp->data, pnode) {
3379 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
3380 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3, &uses_child_set);
3381 LY_CHECK_GOTO(ret, cleanup);
3382 }
3383
3384 if (child_set) {
3385 /* add these children to our compiled child_set as well since uses is a schema-only node */
3386 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3387 }
3388
3389 if (uses_p->when) {
3390 /* pass uses's when to all the data children */
3391 for (i = 0; i < uses_child_set.count; ++i) {
3392 child = uses_child_set.snodes[i];
3393
3394 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent), child, &when_shared);
3395 LY_CHECK_GOTO(ret, cleanup);
3396 }
3397 }
3398
3399 /* compile actions */
3400 if (grp->actions) {
3401 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3402 if (!actions) {
3403 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3404 grp->actions[0].name, lys_nodetype2str(grp->actions[0].nodetype), parent->name,
3405 lys_nodetype2str(parent->nodetype));
3406 ret = LY_EVALID;
3407 goto cleanup;
3408 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01003409 COMPILE_OP_ARRAY_GOTO(ctx, grp->actions, *actions, parent, lys_compile_action, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003410
3411 if (uses_p->when) {
3412 /* inherit when */
3413 LY_ARRAY_FOR(*actions, u) {
3414 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
3415 (struct lysc_node *)&(*actions)[u], &when_shared);
3416 LY_CHECK_GOTO(ret, cleanup);
3417 }
3418 }
3419 }
3420
3421 /* compile notifications */
3422 if (grp->notifs) {
3423 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3424 if (!notifs) {
3425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3426 grp->notifs[0].name, lys_nodetype2str(grp->notifs[0].nodetype), parent->name,
3427 lys_nodetype2str(parent->nodetype));
3428 ret = LY_EVALID;
3429 goto cleanup;
3430 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01003431 COMPILE_OP_ARRAY_GOTO(ctx, grp->notifs, *notifs, parent, lys_compile_notif, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003432
3433 if (uses_p->when) {
3434 /* inherit when */
3435 LY_ARRAY_FOR(*notifs, u) {
3436 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
3437 (struct lysc_node *)&(*notifs)[u], &when_shared);
3438 LY_CHECK_GOTO(ret, cleanup);
3439 }
3440 }
3441 }
3442
3443 /* check that all augments were applied */
3444 for (i = 0; i < ctx->uses_augs.count; ++i) {
3445 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3446 "Augment target node \"%s\" in grouping \"%s\" was not found.",
3447 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3448 ret = LY_ENOTFOUND;
3449 }
3450 LY_CHECK_GOTO(ret, cleanup);
3451
3452 /* check that all refines were applied */
3453 for (i = 0; i < ctx->uses_rfns.count; ++i) {
3454 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3455 "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
3456 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3457 ret = LY_ENOTFOUND;
3458 }
3459 LY_CHECK_GOTO(ret, cleanup);
3460
3461cleanup:
3462 /* reload previous context's parsed module being processed */
3463 ctx->pmod = mod_old;
3464
3465 /* remove the grouping from the stack for circular groupings dependency check */
3466 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3467 assert(ctx->groupings.count == grp_stack_count);
3468
3469 ly_set_erase(&uses_child_set, NULL);
3470 return ret;
3471}
3472
3473static int
3474lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3475{
3476 struct lysp_node *iter;
3477 int len = 0;
3478
3479 *path = NULL;
3480 for (iter = node; iter && len >= 0; iter = iter->parent) {
3481 char *s = *path;
3482 char *id;
3483
3484 switch (iter->nodetype) {
3485 case LYS_USES:
3486 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3487 break;
3488 case LYS_GROUPING:
3489 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3490 break;
3491 case LYS_AUGMENT:
3492 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3493 break;
3494 default:
3495 id = strdup(iter->name);
3496 break;
3497 }
3498
3499 if (!iter->parent) {
3500 /* print prefix */
3501 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3502 } else {
3503 /* prefix is the same as in parent */
3504 len = asprintf(path, "/%s%s", id, s ? s : "");
3505 }
3506 free(s);
3507 free(id);
3508 }
3509
3510 if (len < 0) {
3511 free(*path);
3512 *path = NULL;
3513 } else if (len == 0) {
3514 *path = strdup("/");
3515 len = 1;
3516 }
3517 return len;
3518}
3519
3520LY_ERR
3521lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_grp *grp)
3522{
3523 LY_ERR ret;
3524 char *path;
3525 int len;
3526
3527 struct lysp_node_uses fake_uses = {
3528 .parent = pnode,
3529 .nodetype = LYS_USES,
3530 .flags = 0, .next = NULL,
3531 .name = grp->name,
3532 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3533 .refines = NULL, .augments = NULL
3534 };
3535 struct lysc_node_container fake_container = {
3536 .nodetype = LYS_CONTAINER,
3537 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3538 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003539 .parent = NULL, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003540 .prev = (struct lysc_node *)&fake_container,
3541 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003542 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003543 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3544 };
3545
3546 if (grp->parent) {
3547 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3548 }
3549
3550 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3551 if (len < 0) {
3552 LOGMEM(ctx->ctx);
3553 return LY_EMEM;
3554 }
3555 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3556 ctx->path_len = (uint32_t)len;
3557 free(path);
3558
3559 lysc_update_path(ctx, NULL, "{grouping}");
3560 lysc_update_path(ctx, NULL, grp->name);
3561 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
3562 lysc_update_path(ctx, NULL, NULL);
3563 lysc_update_path(ctx, NULL, NULL);
3564
3565 ctx->path_len = 1;
3566 ctx->path[1] = '\0';
3567
3568 /* cleanup */
3569 lysc_node_container_free(ctx->ctx, &fake_container);
3570
3571 return ret;
3572}
3573
3574/**
3575 * @brief Set config flags for a node.
3576 *
3577 * @param[in] ctx Compile context.
3578 * @param[in] node Compiled node config to set.
3579 * @param[in] parent Parent of @p node.
3580 * @return LY_ERR value.
3581 */
3582static LY_ERR
3583lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
3584{
3585 if (node->nodetype == LYS_CASE) {
3586 /* case never has any config */
3587 assert(!(node->flags & LYS_CONFIG_MASK));
3588 return LY_SUCCESS;
3589 }
3590
3591 /* adjust parent to always get the ancestor with config */
3592 if (parent && (parent->nodetype == LYS_CASE)) {
3593 parent = parent->parent;
3594 assert(parent);
3595 }
3596
3597 if (ctx->options & (LYS_COMPILE_RPC_INPUT | LYS_COMPILE_RPC_OUTPUT)) {
3598 /* ignore config statements inside RPC/action data */
3599 node->flags &= ~LYS_CONFIG_MASK;
3600 node->flags |= (ctx->options & LYS_COMPILE_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
3601 } else if (ctx->options & LYS_COMPILE_NOTIFICATION) {
3602 /* ignore config statements inside Notification data */
3603 node->flags &= ~LYS_CONFIG_MASK;
3604 node->flags |= LYS_CONFIG_R;
3605 } else if (!(node->flags & LYS_CONFIG_MASK)) {
3606 /* config not explicitely set, inherit it from parent */
3607 if (parent) {
3608 node->flags |= parent->flags & LYS_CONFIG_MASK;
3609 } else {
3610 /* default is config true */
3611 node->flags |= LYS_CONFIG_W;
3612 }
3613 } else {
3614 /* config set explicitely */
3615 node->flags |= LYS_SET_CONFIG;
3616 }
3617
3618 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3620 "Configuration node cannot be child of any state data node.");
3621 return LY_EVALID;
3622 }
3623
3624 return LY_SUCCESS;
3625}
3626
3627LY_ERR
3628lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3629 struct ly_set *child_set)
3630{
3631 LY_ERR ret = LY_SUCCESS;
3632 struct lysc_node *node = NULL;
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003633 struct lysp_node *dev_pnode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003634 ly_bool not_supported, enabled;
3635 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003636
3637 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3638
3639 if (pnode->nodetype != LYS_USES) {
3640 lysc_update_path(ctx, parent, pnode->name);
3641 } else {
3642 lysc_update_path(ctx, NULL, "{uses}");
3643 lysc_update_path(ctx, NULL, pnode->name);
3644 }
3645
3646 switch (pnode->nodetype) {
3647 case LYS_CONTAINER:
3648 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3649 node_compile_spec = lys_compile_node_container;
3650 break;
3651 case LYS_LEAF:
3652 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3653 node_compile_spec = lys_compile_node_leaf;
3654 break;
3655 case LYS_LIST:
3656 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3657 node_compile_spec = lys_compile_node_list;
3658 break;
3659 case LYS_LEAFLIST:
3660 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3661 node_compile_spec = lys_compile_node_leaflist;
3662 break;
3663 case LYS_CHOICE:
3664 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3665 node_compile_spec = lys_compile_node_choice;
3666 break;
3667 case LYS_CASE:
3668 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3669 node_compile_spec = lys_compile_node_case;
3670 break;
3671 case LYS_ANYXML:
3672 case LYS_ANYDATA:
3673 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3674 node_compile_spec = lys_compile_node_any;
3675 break;
3676 case LYS_USES:
3677 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3678 lysc_update_path(ctx, NULL, NULL);
3679 lysc_update_path(ctx, NULL, NULL);
3680 return ret;
3681 default:
3682 LOGINT(ctx->ctx);
3683 return LY_EINT;
3684 }
3685 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3686
3687 /* compile any deviations for this node */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003688 LY_CHECK_ERR_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported),
3689 free(node), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003690 if (not_supported) {
3691 free(node);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003692 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003693 } else if (dev_pnode) {
3694 pnode = dev_pnode;
3695 }
3696
3697 node->nodetype = pnode->nodetype;
3698 node->module = ctx->cur_mod;
3699 node->prev = node;
3700 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
3701
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003702 /* if-features */
3703 LY_CHECK_ERR_RET(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), free(node), ret);
Michal Vasko12606ee2020-11-25 17:05:11 +01003704 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003705 ly_set_add(&ctx->disabled, node, 1, NULL);
3706 ctx->options |= LYS_COMPILE_DISABLED;
3707 }
3708
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003709 /* config */
3710 ret = lys_compile_config(ctx, node, parent);
3711 LY_CHECK_GOTO(ret, error);
3712
3713 /* list ordering */
3714 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3715 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
3716 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
3717 (ctx->options & LYS_COMPILE_RPC_OUTPUT) ? "RPC/action output parameters" :
3718 (ctx->options & LYS_COMPILE_NOTIFICATION) ? "notification content" : "state data", ctx->path);
3719 node->flags &= ~LYS_ORDBY_MASK;
3720 node->flags |= LYS_ORDBY_SYSTEM;
3721 } else if (!(node->flags & LYS_ORDBY_MASK)) {
3722 /* default ordering is system */
3723 node->flags |= LYS_ORDBY_SYSTEM;
3724 }
3725 }
3726
3727 /* status - it is not inherited by specification, but it does not make sense to have
3728 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3729 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
3730
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003731 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
3732 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
3733 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003734
3735 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
3736 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
3737
3738 if (pnode->when) {
3739 /* compile when */
3740 ret = lys_compile_when(ctx, pnode->when, pnode->flags, lysc_xpath_context(node), node, NULL);
3741 LY_CHECK_GOTO(ret, cleanup);
3742 }
3743
3744 /* connect any augments */
3745 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
3746
3747 /* nodetype-specific part */
3748 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
3749
3750 /* final compilation tasks that require the node to be connected */
3751 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, LYEXT_PAR_NODE, ret, cleanup);
3752 if (node->flags & LYS_MAND_TRUE) {
3753 /* inherit LYS_MAND_TRUE in parent containers */
3754 lys_compile_mandatory_parents(parent, 1);
3755 }
3756
3757 if (child_set) {
3758 /* add the new node into set */
3759 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
3760 }
3761
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003762 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003763
3764error:
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003765 lysc_node_free(ctx->ctx, node, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003766cleanup:
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003767 ctx->options = prev_opts;
3768 if (ret && dev_pnode) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003769 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 +02003770 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003771 lysp_dev_node_free(ctx->ctx, dev_pnode);
3772 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003773 return ret;
3774}