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