blob: d81aa4792d7c365921d259883b9314865c34e07c [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 Krejci2b18bf12020-11-06 11:20:20 +01001157 int32_t value = 0, cur_val = 0;
1158 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) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001188 cur_val = (int32_t)enums_p[u].value;
1189 if (!u || (cur_val >= value)) {
1190 value = cur_val + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001191 }
1192 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001193 LY_ARRAY_FOR(*enums, v) {
1194 if (cur_val == (*enums)[v].value) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001195 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1196 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001197 cur_val, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001198 return LY_EVALID;
1199 }
1200 }
1201 } else if (base_enums) {
1202 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001203 cur_val = base_enums[match].value;
1204 if (!u || (cur_val >= value)) {
1205 value = cur_val + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001206 }
1207 } else {
1208 /* assign value automatically */
1209 if (u && (value == INT32_MIN)) {
1210 /* counter overflow */
1211 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1212 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001213 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001214 return LY_EVALID;
1215 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001216 cur_val = value++;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001217 }
1218 } else { /* LY_TYPE_BITS */
1219 if (enums_p[u].flags & LYS_SET_VALUE) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001220 cur_pos = (uint32_t)enums_p[u].value;
1221 if (!u || (cur_pos >= position)) {
1222 position = cur_pos + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001223 }
1224 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001225 LY_ARRAY_FOR(*enums, v) {
1226 if (cur_pos == (*enums)[v].position) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001227 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1228 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001229 cur_pos, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001230 return LY_EVALID;
1231 }
1232 }
1233 } else if (base_enums) {
1234 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001235 cur_pos = base_enums[match].position;
1236 if (!u || (cur_pos >= position)) {
1237 position = cur_pos + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001238 }
1239 } else {
1240 /* assign value automatically */
1241 if (u && (position == 0)) {
1242 /* counter overflow */
1243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1244 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001245 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001246 return LY_EVALID;
1247 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001248 cur_pos = position++;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 }
1250 }
1251
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001252 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001253 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001254 if (basetype == LY_TYPE_ENUM) {
1255 if (cur_val != base_enums[match].value) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1257 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001258 enums_p[u].name, base_enums[match].value, cur_val);
1259 return LY_EVALID;
1260 }
1261 } else {
1262 if (cur_pos != base_enums[match].position) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001263 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1264 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001265 enums_p[u].name, base_enums[match].position, cur_pos);
1266 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001267 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001268 }
1269 }
1270
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001271 /* evaluate if-ffeatures */
1272 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1273 if (!enabled) {
1274 continue;
1275 }
1276
1277 /* add new enum/bit */
1278 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1279 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1280 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1281 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
1282 e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) | (basetype == LY_TYPE_ENUM ? LYS_ISENUM : 0);
1283 if (basetype == LY_TYPE_ENUM) {
1284 e->value = cur_val;
1285 } else {
1286 e->position = cur_pos;
1287 }
1288 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM :
1289 LYEXT_PAR_TYPE_BIT, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001290
1291 if (basetype == LY_TYPE_BITS) {
1292 /* keep bits ordered by position */
1293 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
1294 if (v != u) {
1295 memcpy(&storage, e, sizeof *e);
1296 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1297 memcpy(&(*enums)[v], &storage, sizeof storage);
1298 }
1299 }
1300 }
1301
1302done:
1303 return ret;
1304}
1305
1306/**
1307 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1308 * @param[in] ctx Compile context.
1309 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1310 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
1311 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
1312 * @param[in] context_name Name of the context node or referencing typedef for logging.
1313 * @param[in] type_p Parsed type to compile.
1314 * @param[in] basetype Base YANG built-in type of the type to compile.
1315 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1316 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1317 * @param[out] type Newly created type structure with the filled information about the type.
1318 * @return LY_ERR value.
1319 */
1320static LY_ERR
1321lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
1322 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, LY_DATA_TYPE basetype,
1323 const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
1324{
1325 LY_ERR ret = LY_SUCCESS;
1326 struct lysc_type_bin *bin;
1327 struct lysc_type_num *num;
1328 struct lysc_type_str *str;
1329 struct lysc_type_bits *bits;
1330 struct lysc_type_enum *enumeration;
1331 struct lysc_type_dec *dec;
1332 struct lysc_type_identityref *idref;
1333 struct lysc_type_leafref *lref;
1334 struct lysc_type_union *un, *un_aux;
1335
1336 switch (basetype) {
1337 case LY_TYPE_BINARY:
1338 bin = (struct lysc_type_bin *)(*type);
1339
1340 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1341 if (type_p->length) {
1342 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1343 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1344 if (!tpdfname) {
1345 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, cleanup);
1346 }
1347 }
1348 break;
1349 case LY_TYPE_BITS:
1350 /* RFC 7950 9.7 - bits */
1351 bits = (struct lysc_type_bits *)(*type);
1352 if (type_p->bits) {
1353 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1354 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1355 (struct lysc_type_bitenum_item **)&bits->bits));
1356 }
1357
1358 if (!base && !type_p->flags) {
1359 /* type derived from bits built-in type must contain at least one bit */
1360 if (tpdfname) {
1361 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
1362 } else {
1363 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
1364 }
1365 return LY_EVALID;
1366 }
1367 break;
1368 case LY_TYPE_DEC64:
1369 dec = (struct lysc_type_dec *)(*type);
1370
1371 /* RFC 7950 9.3.4 - fraction-digits */
1372 if (!base) {
1373 if (!type_p->fraction_digits) {
1374 if (tpdfname) {
1375 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
1376 } else {
1377 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
1378 }
1379 return LY_EVALID;
1380 }
1381 dec->fraction_digits = type_p->fraction_digits;
1382 } else {
1383 if (type_p->fraction_digits) {
1384 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1385 if (tpdfname) {
1386 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1387 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1388 tpdfname);
1389 } else {
1390 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1391 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1392 }
1393 return LY_EVALID;
1394 }
1395 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1396 }
1397
1398 /* RFC 7950 9.2.4 - range */
1399 if (type_p->range) {
1400 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1401 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1402 if (!tpdfname) {
1403 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, cleanup);
1404 }
1405 }
1406 break;
1407 case LY_TYPE_STRING:
1408 str = (struct lysc_type_str *)(*type);
1409
1410 /* RFC 7950 9.4.4 - length */
1411 if (type_p->length) {
1412 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1413 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1414 if (!tpdfname) {
1415 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, cleanup);
1416 }
1417 } else if (base && ((struct lysc_type_str *)base)->length) {
1418 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1419 }
1420
1421 /* RFC 7950 9.4.5 - pattern */
1422 if (type_p->patterns) {
1423 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1424 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1425 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1426 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1427 }
1428 break;
1429 case LY_TYPE_ENUM:
1430 enumeration = (struct lysc_type_enum *)(*type);
1431
1432 /* RFC 7950 9.6 - enum */
1433 if (type_p->enums) {
1434 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1435 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1436 }
1437
1438 if (!base && !type_p->flags) {
1439 /* type derived from enumerations built-in type must contain at least one enum */
1440 if (tpdfname) {
1441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
1442 } else {
1443 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
1444 }
1445 return LY_EVALID;
1446 }
1447 break;
1448 case LY_TYPE_INT8:
1449 case LY_TYPE_UINT8:
1450 case LY_TYPE_INT16:
1451 case LY_TYPE_UINT16:
1452 case LY_TYPE_INT32:
1453 case LY_TYPE_UINT32:
1454 case LY_TYPE_INT64:
1455 case LY_TYPE_UINT64:
1456 num = (struct lysc_type_num *)(*type);
1457
1458 /* RFC 6020 9.2.4 - range */
1459 if (type_p->range) {
1460 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1461 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1462 if (!tpdfname) {
1463 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, cleanup);
1464 }
1465 }
1466 break;
1467 case LY_TYPE_IDENT:
1468 idref = (struct lysc_type_identityref *)(*type);
1469
1470 /* RFC 7950 9.10.2 - base */
1471 if (type_p->bases) {
1472 if (base) {
1473 /* only the directly derived identityrefs can contain base specification */
1474 if (tpdfname) {
1475 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1476 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1477 tpdfname);
1478 } else {
1479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1480 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1481 }
1482 return LY_EVALID;
1483 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001484 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 +02001485 }
1486
1487 if (!base && !type_p->flags) {
1488 /* type derived from identityref built-in type must contain at least one base */
1489 if (tpdfname) {
1490 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
1491 } else {
1492 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
1493 }
1494 return LY_EVALID;
1495 }
1496 break;
1497 case LY_TYPE_LEAFREF:
1498 lref = (struct lysc_type_leafref *)*type;
1499
1500 /* RFC 7950 9.9.3 - require-instance */
1501 if (type_p->flags & LYS_SET_REQINST) {
1502 if (context_mod->version < LYS_VERSION_1_1) {
1503 if (tpdfname) {
1504 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1505 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1506 } else {
1507 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1508 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1509 }
1510 return LY_EVALID;
1511 }
1512 lref->require_instance = type_p->require_instance;
1513 } else if (base) {
1514 /* inherit */
1515 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1516 } else {
1517 /* default is true */
1518 lref->require_instance = 1;
1519 }
1520 if (type_p->path) {
1521 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
1522 LY_CHECK_RET(lysc_prefixes_compile(type_p->path->expr, strlen(type_p->path->expr), type_p->pmod,
1523 &lref->prefixes));
1524 } else if (base) {
1525 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
1526 LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)base)->prefixes, &lref->prefixes));
1527 } else if (tpdfname) {
1528 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
1529 return LY_EVALID;
1530 } else {
1531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
1532 return LY_EVALID;
1533 }
1534 break;
1535 case LY_TYPE_INST:
1536 /* RFC 7950 9.9.3 - require-instance */
1537 if (type_p->flags & LYS_SET_REQINST) {
1538 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1539 } else {
1540 /* default is true */
1541 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1542 }
1543 break;
1544 case LY_TYPE_UNION:
1545 un = (struct lysc_type_union *)(*type);
1546
1547 /* RFC 7950 7.4 - type */
1548 if (type_p->types) {
1549 if (base) {
1550 /* only the directly derived union can contain types specification */
1551 if (tpdfname) {
1552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1553 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1554 tpdfname);
1555 } else {
1556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1557 "Invalid type substatement for the type not directly derived from union built-in type.");
1558 }
1559 return LY_EVALID;
1560 }
1561 /* compile the type */
1562 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
1563 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
1564 LY_CHECK_RET(lys_compile_type(ctx, context_pnode, context_flags, context_mod, context_name,
1565 &type_p->types[u], &un->types[u + additional], NULL, NULL));
1566 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
1567 /* add space for additional types from the union subtype */
1568 un_aux = (struct lysc_type_union *)un->types[u + additional];
1569 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
1570 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
1571
1572 /* copy subtypes of the subtype union */
1573 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1574 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1575 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1576 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1577 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx); lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
1578 lref = (struct lysc_type_leafref *)un->types[u + additional];
1579
1580 lref->basetype = LY_TYPE_LEAFREF;
1581 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path));
1582 lref->refcount = 1;
1583 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
1584 LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)un_aux->types[v])->prefixes,
1585 &lref->prefixes));
1586 /* TODO extensions */
1587
1588 } else {
1589 un->types[u + additional] = un_aux->types[v];
1590 ++un_aux->types[v]->refcount;
1591 }
1592 ++additional;
1593 LY_ARRAY_INCREMENT(un->types);
1594 }
1595 /* compensate u increment in main loop */
1596 --additional;
1597
1598 /* free the replaced union subtype */
1599 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1600 } else {
1601 LY_ARRAY_INCREMENT(un->types);
1602 }
1603 }
1604 }
1605
1606 if (!base && !type_p->flags) {
1607 /* type derived from union built-in type must contain at least one type */
1608 if (tpdfname) {
1609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
1610 } else {
1611 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
1612 }
1613 return LY_EVALID;
1614 }
1615 break;
1616 case LY_TYPE_BOOL:
1617 case LY_TYPE_EMPTY:
1618 case LY_TYPE_UNKNOWN: /* just to complete switch */
1619 break;
1620 }
1621
1622 if (tpdfname) {
1623 switch (basetype) {
1624 case LY_TYPE_BINARY:
1625 type_p->compiled = *type;
1626 *type = calloc(1, sizeof(struct lysc_type_bin));
1627 break;
1628 case LY_TYPE_BITS:
1629 type_p->compiled = *type;
1630 *type = calloc(1, sizeof(struct lysc_type_bits));
1631 break;
1632 case LY_TYPE_DEC64:
1633 type_p->compiled = *type;
1634 *type = calloc(1, sizeof(struct lysc_type_dec));
1635 break;
1636 case LY_TYPE_STRING:
1637 type_p->compiled = *type;
1638 *type = calloc(1, sizeof(struct lysc_type_str));
1639 break;
1640 case LY_TYPE_ENUM:
1641 type_p->compiled = *type;
1642 *type = calloc(1, sizeof(struct lysc_type_enum));
1643 break;
1644 case LY_TYPE_INT8:
1645 case LY_TYPE_UINT8:
1646 case LY_TYPE_INT16:
1647 case LY_TYPE_UINT16:
1648 case LY_TYPE_INT32:
1649 case LY_TYPE_UINT32:
1650 case LY_TYPE_INT64:
1651 case LY_TYPE_UINT64:
1652 type_p->compiled = *type;
1653 *type = calloc(1, sizeof(struct lysc_type_num));
1654 break;
1655 case LY_TYPE_IDENT:
1656 type_p->compiled = *type;
1657 *type = calloc(1, sizeof(struct lysc_type_identityref));
1658 break;
1659 case LY_TYPE_LEAFREF:
1660 type_p->compiled = *type;
1661 *type = calloc(1, sizeof(struct lysc_type_leafref));
1662 break;
1663 case LY_TYPE_INST:
1664 type_p->compiled = *type;
1665 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1666 break;
1667 case LY_TYPE_UNION:
1668 type_p->compiled = *type;
1669 *type = calloc(1, sizeof(struct lysc_type_union));
1670 break;
1671 case LY_TYPE_BOOL:
1672 case LY_TYPE_EMPTY:
1673 case LY_TYPE_UNKNOWN: /* just to complete switch */
1674 break;
1675 }
1676 }
1677 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1678
1679cleanup:
1680 return ret;
1681}
1682
1683LY_ERR
1684lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
1685 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
1686 struct lysc_type **type, const char **units, struct lysp_qname **dflt)
1687{
1688 LY_ERR ret = LY_SUCCESS;
1689 ly_bool dummyloops = 0;
1690 struct type_context {
1691 const struct lysp_tpdf *tpdf;
1692 struct lysp_node *node;
1693 struct lysp_module *mod;
1694 } *tctx, *tctx_prev = NULL, *tctx_iter;
1695 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1696 struct lysc_type *base = NULL, *prev_type;
1697 struct ly_set tpdf_chain = {0};
1698
1699 (*type) = NULL;
1700 if (dflt) {
1701 *dflt = NULL;
1702 }
1703
1704 tctx = calloc(1, sizeof *tctx);
1705 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1706 for (ret = lysp_type_find(type_p->name, context_pnode, ctx->pmod, &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
1707 ret == LY_SUCCESS;
1708 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
1709 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
1710 if (basetype) {
1711 break;
1712 }
1713
1714 /* check status */
1715 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
1716 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
1717 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1718
1719 if (units && !*units) {
1720 /* inherit units */
1721 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1722 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1723 }
1724 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1725 /* inherit default */
1726 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1727 }
1728 if (dummyloops && (!units || *units) && dflt && *dflt) {
1729 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1730 break;
1731 }
1732
1733 if (tctx->tpdf->type.compiled) {
1734 /* it is not necessary to continue, the rest of the chain was already compiled,
1735 * but we still may need to inherit default and units values, so start dummy loops */
1736 basetype = tctx->tpdf->type.compiled->basetype;
1737 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1738 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1739
1740 if ((units && !*units) || (dflt && !*dflt)) {
1741 dummyloops = 1;
1742 goto preparenext;
1743 } else {
1744 tctx = NULL;
1745 break;
1746 }
1747 }
1748
1749 /* circular typedef reference detection */
1750 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1751 /* local part */
1752 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
1753 if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) {
1754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1755 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1756 free(tctx);
1757 ret = LY_EVALID;
1758 goto cleanup;
1759 }
1760 }
1761 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1762 /* global part for unions corner case */
1763 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
1764 if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) {
1765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1766 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1767 free(tctx);
1768 ret = LY_EVALID;
1769 goto cleanup;
1770 }
1771 }
1772
1773 /* store information for the following processing */
1774 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1775 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1776
1777preparenext:
1778 /* prepare next loop */
1779 tctx_prev = tctx;
1780 tctx = calloc(1, sizeof *tctx);
1781 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1782 }
1783 free(tctx);
1784
1785 /* allocate type according to the basetype */
1786 switch (basetype) {
1787 case LY_TYPE_BINARY:
1788 *type = calloc(1, sizeof(struct lysc_type_bin));
1789 break;
1790 case LY_TYPE_BITS:
1791 *type = calloc(1, sizeof(struct lysc_type_bits));
1792 break;
1793 case LY_TYPE_BOOL:
1794 case LY_TYPE_EMPTY:
1795 *type = calloc(1, sizeof(struct lysc_type));
1796 break;
1797 case LY_TYPE_DEC64:
1798 *type = calloc(1, sizeof(struct lysc_type_dec));
1799 break;
1800 case LY_TYPE_ENUM:
1801 *type = calloc(1, sizeof(struct lysc_type_enum));
1802 break;
1803 case LY_TYPE_IDENT:
1804 *type = calloc(1, sizeof(struct lysc_type_identityref));
1805 break;
1806 case LY_TYPE_INST:
1807 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1808 break;
1809 case LY_TYPE_LEAFREF:
1810 *type = calloc(1, sizeof(struct lysc_type_leafref));
1811 break;
1812 case LY_TYPE_STRING:
1813 *type = calloc(1, sizeof(struct lysc_type_str));
1814 break;
1815 case LY_TYPE_UNION:
1816 *type = calloc(1, sizeof(struct lysc_type_union));
1817 break;
1818 case LY_TYPE_INT8:
1819 case LY_TYPE_UINT8:
1820 case LY_TYPE_INT16:
1821 case LY_TYPE_UINT16:
1822 case LY_TYPE_INT32:
1823 case LY_TYPE_UINT32:
1824 case LY_TYPE_INT64:
1825 case LY_TYPE_UINT64:
1826 *type = calloc(1, sizeof(struct lysc_type_num));
1827 break;
1828 case LY_TYPE_UNKNOWN:
1829 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1830 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1831 ret = LY_EVALID;
1832 goto cleanup;
1833 }
1834 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1835 if (~type_substmt_map[basetype] & type_p->flags) {
1836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
1837 ly_data_type2str[basetype]);
1838 free(*type);
1839 (*type) = NULL;
1840 ret = LY_EVALID;
1841 goto cleanup;
1842 }
1843
1844 /* get restrictions from the referred typedefs */
1845 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1846 tctx = (struct type_context *)tpdf_chain.objs[u];
1847
1848 /* remember the typedef context for circular check */
1849 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1850 LY_CHECK_GOTO(ret, cleanup);
1851
1852 if (tctx->tpdf->type.compiled) {
1853 base = tctx->tpdf->type.compiled;
1854 continue;
1855 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1856 /* no change, just use the type information from the base */
1857 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1858 ++base->refcount;
1859 continue;
1860 }
1861
1862 ++(*type)->refcount;
1863 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
1864 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
1865 tctx->tpdf->name, ly_data_type2str[basetype]);
1866 ret = LY_EVALID;
1867 goto cleanup;
1868 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
1869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1870 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1871 tctx->tpdf->name, tctx->tpdf->dflt.str);
1872 ret = LY_EVALID;
1873 goto cleanup;
1874 }
1875
1876 (*type)->basetype = basetype;
1877 /* TODO user type plugins */
1878 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1879 prev_type = *type;
1880 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name,
1881 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1882 LY_CHECK_GOTO(ret, cleanup);
1883 base = prev_type;
1884 }
1885 /* remove the processed typedef contexts from the stack for circular check */
1886 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1887
1888 /* process the type definition in leaf */
1889 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1890 /* get restrictions from the node itself */
1891 (*type)->basetype = basetype;
1892 /* TODO user type plugins */
1893 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1894 ++(*type)->refcount;
1895 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_mod, context_name, type_p, basetype, NULL,
1896 base, type);
1897 LY_CHECK_GOTO(ret, cleanup);
1898 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1899 /* no specific restriction in leaf's type definition, copy from the base */
1900 free(*type);
1901 (*type) = base;
1902 ++(*type)->refcount;
1903 }
1904
1905 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
1906
1907cleanup:
1908 ly_set_erase(&tpdf_chain, free);
1909 return ret;
1910}
1911
1912/**
1913 * @brief Compile status information of the given node.
1914 *
1915 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
1916 * has the status correctly set during the compilation.
1917 *
1918 * @param[in] ctx Compile context
1919 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
1920 * If the status was set explicitly on the node, it is already set in the flags value and we just check
1921 * the compatibility with the parent's status value.
1922 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
1923 * @return LY_ERR value.
1924 */
1925static LY_ERR
1926lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
1927{
1928 /* status - it is not inherited by specification, but it does not make sense to have
1929 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
1930 if (!((*node_flags) & LYS_STATUS_MASK)) {
1931 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
1932 if ((parent_flags & 0x3) != 0x3) {
1933 /* do not print the warning when inheriting status from uses - the uses_status value has a special
1934 * combination of bits (0x3) which marks the uses_status value */
1935 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
1936 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1937 }
1938 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
1939 } else {
1940 (*node_flags) |= LYS_STATUS_CURR;
1941 }
1942 } else if (parent_flags & LYS_STATUS_MASK) {
1943 /* check status compatibility with the parent */
1944 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
1945 if ((*node_flags) & LYS_STATUS_CURR) {
1946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1947 "A \"current\" status is in conflict with the parent's \"%s\" status.",
1948 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1949 } else { /* LYS_STATUS_DEPRC */
1950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1951 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
1952 }
1953 return LY_EVALID;
1954 }
1955 }
1956 return LY_SUCCESS;
1957}
1958
1959/**
1960 * @brief Check uniqness of the node/action/notification name.
1961 *
1962 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
1963 * structures, but they share the namespace so we need to check their name collisions.
1964 *
1965 * @param[in] ctx Compile context.
1966 * @param[in] parent Parent of the nodes to check, can be NULL.
1967 * @param[in] name Name of the item to find in the given lists.
1968 * @param[in] exclude Node that was just added that should be excluded from the name checking.
1969 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
1970 */
1971static LY_ERR
1972lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
1973 const struct lysc_node *exclude)
1974{
1975 const struct lysc_node *iter, *iter2;
1976 const struct lysc_action *actions;
1977 const struct lysc_notif *notifs;
1978 uint32_t getnext_flags;
1979 LY_ARRAY_COUNT_TYPE u;
1980
1981#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
1982
1983 if (exclude->nodetype == LYS_CASE) {
1984 /* check restricted only to all the cases */
1985 assert(parent->nodetype == LYS_CHOICE);
1986 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
1987 if (CHECK_NODE(iter, exclude, name)) {
1988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
1989 return LY_EEXIST;
1990 }
1991 }
1992
1993 return LY_SUCCESS;
1994 }
1995
1996 /* no reason for our parent to be choice anymore */
1997 assert(!parent || (parent->nodetype != LYS_CHOICE));
1998
1999 if (parent && (parent->nodetype == LYS_CASE)) {
2000 /* move to the first data definition parent */
2001 parent = lysc_data_parent(parent);
2002 }
2003
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002004 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002005 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
2006 getnext_flags |= LYS_GETNEXT_OUTPUT;
2007 }
2008
2009 iter = NULL;
2010 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2011 if (CHECK_NODE(iter, exclude, name)) {
2012 goto error;
2013 }
2014
2015 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2016 if (iter->nodetype == LYS_CHOICE) {
2017 iter2 = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002018 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002019 if (CHECK_NODE(iter2, exclude, name)) {
2020 goto error;
2021 }
2022 }
2023 }
2024 }
2025
2026 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2027 LY_ARRAY_FOR(actions, u) {
2028 if (CHECK_NODE(&actions[u], exclude, name)) {
2029 goto error;
2030 }
2031 }
2032
2033 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2034 LY_ARRAY_FOR(notifs, u) {
2035 if (CHECK_NODE(&notifs[u], exclude, name)) {
2036 goto error;
2037 }
2038 }
2039 return LY_SUCCESS;
2040
2041error:
2042 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
2043 return LY_EEXIST;
2044
2045#undef CHECK_NODE
2046}
2047
2048LY_ERR
2049lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, struct lysc_node *parent,
2050 struct lysc_action *action, uint16_t uses_status)
2051{
2052 LY_ERR ret = LY_SUCCESS;
2053 struct lysp_node *child_p, *dev_pnode = NULL, *dev_input_p = NULL, *dev_output_p = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002054 struct lysp_action_inout *inout_p;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002055 ly_bool not_supported, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002056 uint32_t opt_prev = ctx->options;
2057
2058 lysc_update_path(ctx, parent, action_p->name);
2059
2060 /* apply deviation on the action/RPC */
2061 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)action_p, parent, &dev_pnode, &not_supported));
2062 if (not_supported) {
2063 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002064 ret = LY_EDENIED;
2065 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002066 } else if (dev_pnode) {
2067 action_p = (struct lysp_action *)dev_pnode;
2068 }
2069
2070 /* member needed for uniqueness check lys_getnext() */
2071 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
2072 action->module = ctx->cur_mod;
2073 action->parent = parent;
2074
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002075 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 +02002076
2077 if (ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) {
2078 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2079 "Action \"%s\" is placed inside %s.", action_p->name,
2080 ctx->options & LYS_COMPILE_RPC_MASK ? "another RPC/action" : "notification");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002081 ret = LY_EVALID;
2082 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002083 }
2084
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002085 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
2086
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002087 /* if-features */
2088 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, action_p->iffeatures, &enabled));
Michal Vasko12606ee2020-11-25 17:05:11 +01002089 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002090 ly_set_add(&ctx->disabled, action, 1, NULL);
2091 ctx->options |= LYS_COMPILE_DISABLED;
2092 }
2093
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002094 /* status - it is not inherited by specification, but it does not make sense to have
2095 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002096 ret = lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
2097 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002098
2099 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
2100 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
2101 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002102
2103 /* connect any action augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002104 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)action), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002105
2106 /* input */
2107 lysc_update_path(ctx, (struct lysc_node *)action, "input");
2108
2109 /* apply deviations on input */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002110 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->input,
2111 (struct lysc_node *)action, &dev_input_p, &not_supported), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002112 if (not_supported) {
2113 inout_p = NULL;
2114 } else if (dev_input_p) {
2115 inout_p = (struct lysp_action_inout *)dev_input_p;
2116 } else {
2117 inout_p = &action_p->input;
2118 }
2119
2120 if (inout_p) {
2121 action->input.nodetype = LYS_INPUT;
Michal Vasko5347e3a2020-11-03 17:14:57 +01002122 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->input.musts, lys_compile_must, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002123 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
2124 ctx->options |= LYS_COMPILE_RPC_INPUT;
2125
2126 /* connect any input augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002127 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)&action->input), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002128
2129 LY_LIST_FOR(inout_p->data, child_p) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002130 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 +02002131 }
2132 ctx->options = opt_prev;
2133 }
2134
2135 lysc_update_path(ctx, NULL, NULL);
2136
2137 /* output */
2138 lysc_update_path(ctx, (struct lysc_node *)action, "output");
2139
2140 /* apply deviations on output */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002141 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->output,
2142 (struct lysc_node *)action, &dev_output_p, &not_supported), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002143 if (not_supported) {
2144 inout_p = NULL;
2145 } else if (dev_output_p) {
2146 inout_p = (struct lysp_action_inout *)dev_output_p;
2147 } else {
2148 inout_p = &action_p->output;
2149 }
2150
2151 if (inout_p) {
2152 action->output.nodetype = LYS_OUTPUT;
Michal Vasko5347e3a2020-11-03 17:14:57 +01002153 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->output.musts, lys_compile_must, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002154 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
2155 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
2156
2157 /* connect any output augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002158 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)&action->output), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002159
2160 LY_LIST_FOR(inout_p->data, child_p) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002161 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 +02002162 }
2163 ctx->options = opt_prev;
2164 }
2165
2166 lysc_update_path(ctx, NULL, NULL);
2167
Michal Vasko208a04a2020-10-21 15:17:12 +02002168 /* wait with extensions compilation until all the children are compiled */
2169 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
2170
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002171 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002172 /* do not check "must" semantics in a grouping */
2173 ret = ly_set_add(&ctx->xpath, action, 0, NULL);
2174 LY_CHECK_GOTO(ret, cleanup);
2175 }
2176
2177 lysc_update_path(ctx, NULL, NULL);
2178
2179cleanup:
2180 lysp_dev_node_free(ctx->ctx, dev_pnode);
2181 lysp_dev_node_free(ctx->ctx, dev_input_p);
2182 lysp_dev_node_free(ctx->ctx, dev_output_p);
2183 ctx->options = opt_prev;
2184 return ret;
2185}
2186
2187LY_ERR
2188lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, struct lysc_node *parent, struct lysc_notif *notif,
2189 uint16_t uses_status)
2190{
2191 LY_ERR ret = LY_SUCCESS;
2192 struct lysp_node *child_p, *dev_pnode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002193 ly_bool not_supported, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002194 uint32_t opt_prev = ctx->options;
2195
2196 lysc_update_path(ctx, parent, notif_p->name);
2197
2198 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)notif_p, parent, &dev_pnode, &not_supported));
2199 if (not_supported) {
2200 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002201 ret = LY_EDENIED;
2202 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002203 } else if (dev_pnode) {
2204 notif_p = (struct lysp_notif *)dev_pnode;
2205 }
2206
2207 /* member needed for uniqueness check lys_getnext() */
2208 notif->nodetype = LYS_NOTIF;
2209 notif->module = ctx->cur_mod;
2210 notif->parent = parent;
2211
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002212 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 +02002213
2214 if (ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) {
2215 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2216 "Notification \"%s\" is placed inside %s.", notif_p->name,
2217 ctx->options & LYS_COMPILE_RPC_MASK ? "RPC/action" : "another notification");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002218 ret = LY_EVALID;
2219 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002220 }
2221
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002222 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
2223
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002224 /* if-features */
2225 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, notif_p->iffeatures, &enabled));
Michal Vasko12606ee2020-11-25 17:05:11 +01002226 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002227 ly_set_add(&ctx->disabled, notif, 1, NULL);
2228 ctx->options |= LYS_COMPILE_DISABLED;
2229 }
2230
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002231 /* status - it is not inherited by specification, but it does not make sense to have
2232 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2233 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
2234 LY_CHECK_GOTO(ret, cleanup);
2235
2236 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
2237 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
2238 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Michal Vasko5347e3a2020-11-03 17:14:57 +01002239 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002240 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002241 /* do not check "must" semantics in a grouping */
2242 ret = ly_set_add(&ctx->xpath, notif, 0, NULL);
2243 LY_CHECK_GOTO(ret, cleanup);
2244 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002245
2246 ctx->options |= LYS_COMPILE_NOTIFICATION;
2247
2248 /* connect any notification augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002249 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)notif), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002250
2251 LY_LIST_FOR(notif_p->data, child_p) {
2252 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status, NULL);
2253 LY_CHECK_GOTO(ret, cleanup);
2254 }
2255
Michal Vasko208a04a2020-10-21 15:17:12 +02002256 /* wait with extension compilation until all the children are compiled */
2257 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
2258
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002259 lysc_update_path(ctx, NULL, NULL);
2260
2261cleanup:
2262 lysp_dev_node_free(ctx->ctx, dev_pnode);
2263 ctx->options = opt_prev;
2264 return ret;
2265}
2266
2267/**
2268 * @brief Compile parsed container node information.
2269 * @param[in] ctx Compile context
2270 * @param[in] pnode Parsed container node.
2271 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2272 * is enriched with the container-specific information.
2273 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2274 */
2275static LY_ERR
2276lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2277{
2278 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2279 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2280 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002281 LY_ERR ret = LY_SUCCESS;
2282
2283 if (cont_p->presence) {
2284 /* explicit presence */
2285 cont->flags |= LYS_PRESENCE;
2286 } else if (cont_p->musts) {
2287 /* container with a must condition */
2288 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
2289 cont->flags |= LYS_PRESENCE;
2290 } else if (cont_p->when) {
2291 /* container with a when condition */
2292 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"when\" condition.", cont_p->name);
2293 cont->flags |= LYS_PRESENCE;
2294 } else if (cont_p->parent) {
2295 if (cont_p->parent->nodetype == LYS_CHOICE) {
2296 /* container is an implicit case, so its existence decides the existence of the whole case */
2297 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
2298 cont_p->name, cont_p->parent->name);
2299 cont->flags |= LYS_PRESENCE;
2300 } else if ((cont_p->parent->nodetype == LYS_CASE) &&
2301 (((struct lysp_node_case *)cont_p->parent)->child == pnode) && !cont_p->next) {
2302 /* container is the only node in a case, so its existence decides the existence of the whole case */
2303 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
2304 cont_p->name, cont_p->parent->name);
2305 cont->flags |= LYS_PRESENCE;
2306 }
2307 }
2308
2309 /* more cases when the container has meaning but is kept NP for convenience:
2310 * - when condition
2311 * - direct child action/notification
2312 */
2313
2314 LY_LIST_FOR(cont_p->child, child_p) {
2315 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2316 LY_CHECK_GOTO(ret, done);
2317 }
2318
Michal Vasko5347e3a2020-11-03 17:14:57 +01002319 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002320 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002321 /* do not check "must" semantics in a grouping */
2322 ret = ly_set_add(&ctx->xpath, cont, 0, NULL);
2323 LY_CHECK_GOTO(ret, done);
2324 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01002325 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, node, lys_compile_action, 0, ret, done);
2326 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, node, lys_compile_notif, 0, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002327
2328done:
2329 return ret;
2330}
2331
2332/*
2333 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2334 * @param[in] ctx Compile context.
2335 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2336 * @param[in] type_p Parsed type to compile.
2337 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2338 * @return LY_ERR value.
2339 */
2340static LY_ERR
2341lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2342 struct lysc_node_leaf *leaf)
2343{
2344 struct lysp_qname *dflt;
2345
2346 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->pmod, leaf->name, type_p, &leaf->type,
2347 leaf->units ? NULL : &leaf->units, &dflt));
2348
2349 /* store default value, if any */
2350 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2351 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2352 }
2353
Michal Vasko12606ee2020-11-25 17:05:11 +01002354 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002355 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2356 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002357 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002358 LY_ARRAY_COUNT_TYPE u;
2359 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2360 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2361 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2362 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
2363 }
2364 }
2365 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2366 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2367 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2368 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2369 return LY_EVALID;
2370 }
2371 }
2372
2373 return LY_SUCCESS;
2374}
2375
2376/**
2377 * @brief Compile parsed leaf node information.
2378 * @param[in] ctx Compile context
2379 * @param[in] pnode Parsed leaf node.
2380 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2381 * is enriched with the leaf-specific information.
2382 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2383 */
2384static LY_ERR
2385lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2386{
2387 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2388 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002389 LY_ERR ret = LY_SUCCESS;
2390
Michal Vasko5347e3a2020-11-03 17:14:57 +01002391 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002392 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002393 /* do not check "must" semantics in a grouping */
2394 ret = ly_set_add(&ctx->xpath, leaf, 0, NULL);
2395 LY_CHECK_GOTO(ret, done);
2396 }
2397 if (leaf_p->units) {
2398 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2399 leaf->flags |= LYS_SET_UNITS;
2400 }
2401
2402 /* compile type */
2403 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2404 LY_CHECK_GOTO(ret, done);
2405
2406 /* store/update default value */
2407 if (leaf_p->dflt.str) {
2408 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2409 leaf->flags |= LYS_SET_DFLT;
2410 }
2411
2412 /* checks */
2413 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
2414 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2415 "Invalid mandatory leaf with a default value.");
2416 return LY_EVALID;
2417 }
2418
2419done:
2420 return ret;
2421}
2422
2423/**
2424 * @brief Compile parsed leaf-list node information.
2425 * @param[in] ctx Compile context
2426 * @param[in] pnode Parsed leaf-list node.
2427 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2428 * is enriched with the leaf-list-specific information.
2429 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2430 */
2431static LY_ERR
2432lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2433{
2434 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2435 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002436 LY_ERR ret = LY_SUCCESS;
2437
Michal Vasko5347e3a2020-11-03 17:14:57 +01002438 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002439 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002440 /* do not check "must" semantics in a grouping */
2441 ret = ly_set_add(&ctx->xpath, llist, 0, NULL);
2442 LY_CHECK_GOTO(ret, done);
2443 }
2444 if (llist_p->units) {
2445 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2446 llist->flags |= LYS_SET_UNITS;
2447 }
2448
2449 /* compile type */
2450 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2451 LY_CHECK_GOTO(ret, done);
2452
2453 /* store/update default values */
2454 if (llist_p->dflts) {
2455 if (ctx->pmod->version < LYS_VERSION_1_1) {
2456 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2457 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2458 return LY_EVALID;
2459 }
2460
2461 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2462 llist->flags |= LYS_SET_DFLT;
2463 }
2464
2465 llist->min = llist_p->min;
2466 if (llist->min) {
2467 llist->flags |= LYS_MAND_TRUE;
2468 }
2469 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2470
2471 /* checks */
2472 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
2473 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2474 "Invalid mandatory leaf-list with default value(s).");
2475 return LY_EVALID;
2476 }
2477
2478 if (llist->min > llist->max) {
2479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
2480 llist->min, llist->max);
2481 return LY_EVALID;
2482 }
2483
2484done:
2485 return ret;
2486}
2487
2488LY_ERR
2489lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2490 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2491 const struct lysc_node **target, uint16_t *result_flag)
2492{
2493 LY_ERR ret = LY_EVALID;
2494 const char *name, *prefix, *id;
2495 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002496 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002497 const char *nodeid_type;
2498 uint32_t getnext_extra_flag = 0;
2499 uint16_t current_nodetype = 0;
2500
2501 assert(nodeid);
2502 assert(target);
2503 assert(result_flag);
2504 *target = NULL;
2505 *result_flag = 0;
2506
2507 id = nodeid;
2508
2509 if (ctx_node) {
2510 /* descendant-schema-nodeid */
2511 nodeid_type = "descendant";
2512
2513 if (*id == '/') {
2514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2515 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
2516 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2517 return LY_EVALID;
2518 }
2519 } else {
2520 /* absolute-schema-nodeid */
2521 nodeid_type = "absolute";
2522
2523 if (*id != '/') {
2524 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2525 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
2526 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2527 return LY_EVALID;
2528 }
2529 ++id;
2530 }
2531
2532 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2533 if (prefix) {
2534 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2535 if (!mod) {
2536 /* module must always be found */
2537 assert(prefix);
2538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2539 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
2540 nodeid_type, id - nodeid, nodeid, prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
2541 return LY_ENOTFOUND;
2542 }
2543 } else {
2544 switch (format) {
2545 case LY_PREF_SCHEMA:
2546 case LY_PREF_SCHEMA_RESOLVED:
2547 /* use the current module */
2548 mod = cur_mod;
2549 break;
2550 case LY_PREF_JSON:
2551 if (!ctx_node) {
2552 LOGINT_RET(ctx->ctx);
2553 }
2554 /* inherit the module of the previous context node */
2555 mod = ctx_node->module;
2556 break;
2557 case LY_PREF_XML:
2558 /* not really defined */
2559 LOGINT_RET(ctx->ctx);
2560 }
2561 }
2562
2563 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2564 /* move through input/output manually */
2565 if (mod != ctx_node->module) {
2566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2567 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
2568 return LY_ENOTFOUND;
2569 }
2570 if (!ly_strncmp("input", name, name_len)) {
2571 ctx_node = (struct lysc_node *)&((struct lysc_action *)ctx_node)->input;
2572 } else if (!ly_strncmp("output", name, name_len)) {
2573 ctx_node = (struct lysc_node *)&((struct lysc_action *)ctx_node)->output;
2574 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2575 } else {
2576 /* only input or output is valid */
2577 ctx_node = NULL;
2578 }
2579 } else {
2580 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002581 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002582 getnext_extra_flag = 0;
2583 }
2584 if (!ctx_node) {
2585 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2586 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
2587 return LY_ENOTFOUND;
2588 }
2589 current_nodetype = ctx_node->nodetype;
2590
2591 if (current_nodetype == LYS_NOTIF) {
2592 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2593 } else if (current_nodetype == LYS_INPUT) {
2594 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2595 } else if (current_nodetype == LYS_OUTPUT) {
2596 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2597 }
2598
2599 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2600 break;
2601 }
2602 if (*id != '/') {
2603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2604 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
2605 nodeid_type, id - nodeid + 1, nodeid);
2606 return LY_EVALID;
2607 }
2608 ++id;
2609 }
2610
2611 if (ret == LY_SUCCESS) {
2612 *target = ctx_node;
2613 if (nodetype && !(current_nodetype & nodetype)) {
2614 return LY_EDENIED;
2615 }
2616 } else {
2617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2618 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
2619 nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2620 }
2621
2622 return ret;
2623}
2624
2625/**
2626 * @brief Compile information about list's uniques.
2627 * @param[in] ctx Compile context.
2628 * @param[in] uniques Sized array list of unique statements.
2629 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2630 * @return LY_ERR value.
2631 */
2632static LY_ERR
2633lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2634{
2635 LY_ERR ret = LY_SUCCESS;
2636 struct lysc_node_leaf **key, ***unique;
2637 struct lysc_node *parent;
2638 const char *keystr, *delim;
2639 size_t len;
2640 LY_ARRAY_COUNT_TYPE v;
2641 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2642 uint16_t flags;
2643
2644 LY_ARRAY_FOR(uniques, v) {
2645 config = -1;
2646 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2647 keystr = uniques[v].str;
2648 while (keystr) {
2649 delim = strpbrk(keystr, " \t\n");
2650 if (delim) {
2651 len = delim - keystr;
2652 while (isspace(*delim)) {
2653 ++delim;
2654 }
2655 } else {
2656 len = strlen(keystr);
2657 }
2658
2659 /* unique node must be present */
2660 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
2661 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, uniques[v].mod->mod,
2662 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2663 if (ret != LY_SUCCESS) {
2664 if (ret == LY_EDENIED) {
2665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2666 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
2667 len, keystr, lys_nodetype2str((*key)->nodetype));
2668 }
2669 return LY_EVALID;
2670 } else if (flags) {
2671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2672 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
2673 len, keystr, flags & LYS_COMPILE_NOTIFICATION ? "notification" : "RPC/action");
2674 return LY_EVALID;
2675 }
2676
2677 /* all referenced leafs must be of the same config type */
2678 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2679 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
2680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2681 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2682 return LY_EVALID;
2683 } else if ((*key)->flags & LYS_CONFIG_W) {
2684 config = 1;
2685 } else { /* LYS_CONFIG_R */
2686 config = 0;
2687 }
2688
2689 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
2690 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
2691 if (parent->nodetype == LYS_LIST) {
2692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2693 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
2694 return LY_EVALID;
2695 }
2696 }
2697
2698 /* check status */
2699 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2700 (*key)->flags, (*key)->module, (*key)->name));
2701
2702 /* mark leaf as unique */
2703 (*key)->flags |= LYS_UNIQUE;
2704
2705 /* next unique value in line */
2706 keystr = delim;
2707 }
2708 /* next unique definition */
2709 }
2710
2711 return LY_SUCCESS;
2712}
2713
2714/**
2715 * @brief Compile parsed list node information.
2716 * @param[in] ctx Compile context
2717 * @param[in] pnode Parsed list node.
2718 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2719 * is enriched with the list-specific information.
2720 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2721 */
2722static LY_ERR
2723lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2724{
2725 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
2726 struct lysc_node_list *list = (struct lysc_node_list *)node;
2727 struct lysp_node *child_p;
2728 struct lysc_node_leaf *key, *prev_key = NULL;
2729 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002730 const char *keystr, *delim;
2731 LY_ERR ret = LY_SUCCESS;
2732
2733 list->min = list_p->min;
2734 if (list->min) {
2735 list->flags |= LYS_MAND_TRUE;
2736 }
2737 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2738
2739 LY_LIST_FOR(list_p->child, child_p) {
2740 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
2741 }
2742
Michal Vasko5347e3a2020-11-03 17:14:57 +01002743 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002744 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002745 /* do not check "must" semantics in a grouping */
2746 LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL));
2747 }
2748
2749 /* keys */
2750 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
2751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
2752 return LY_EVALID;
2753 }
2754
2755 /* find all the keys (must be direct children) */
2756 keystr = list_p->key;
2757 if (!keystr) {
2758 /* keyless list */
2759 list->flags |= LYS_KEYLESS;
2760 }
2761 while (keystr) {
2762 delim = strpbrk(keystr, " \t\n");
2763 if (delim) {
2764 len = delim - keystr;
2765 while (isspace(*delim)) {
2766 ++delim;
2767 }
2768 } else {
2769 len = strlen(keystr);
2770 }
2771
2772 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002773 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
2774 if (!key) {
2775 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 +02002776 return LY_EVALID;
2777 }
2778 /* keys must be unique */
2779 if (key->flags & LYS_KEY) {
2780 /* the node was already marked as a key */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002782 return LY_EVALID;
2783 }
2784
2785 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
2786 /* key must have the same config flag as the list itself */
2787 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
2788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
2789 return LY_EVALID;
2790 }
2791 if (ctx->pmod->version < LYS_VERSION_1_1) {
2792 /* YANG 1.0 denies key to be of empty type */
2793 if (key->type->basetype == LY_TYPE_EMPTY) {
2794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2795 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
2796 return LY_EVALID;
2797 }
2798 } else {
2799 /* when and if-feature are illegal on list keys */
2800 if (key->when) {
2801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2802 "List's key must not have any \"when\" statement.");
2803 return LY_EVALID;
2804 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002805 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002806 }
2807
2808 /* check status */
2809 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2810 key->flags, key->module, key->name));
2811
2812 /* ignore default values of the key */
2813 if (key->dflt) {
2814 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
2815 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
2816 free(key->dflt);
2817 key->dflt = NULL;
2818 }
2819 /* mark leaf as key */
2820 key->flags |= LYS_KEY;
2821
2822 /* move it to the correct position */
2823 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
2824 /* fix links in closest previous siblings of the key */
2825 if (key->next) {
2826 key->next->prev = key->prev;
2827 } else {
2828 /* last child */
2829 list->child->prev = key->prev;
2830 }
2831 if (key->prev->next) {
2832 key->prev->next = key->next;
2833 }
2834 /* fix links in the key */
2835 if (prev_key) {
2836 key->prev = (struct lysc_node *)prev_key;
2837 key->next = prev_key->next;
2838 } else {
2839 key->prev = list->child->prev;
2840 key->next = list->child;
2841 }
2842 /* fix links in closes future siblings of the key */
2843 if (prev_key) {
2844 if (prev_key->next) {
2845 prev_key->next->prev = (struct lysc_node *)key;
2846 } else {
2847 list->child->prev = (struct lysc_node *)key;
2848 }
2849 prev_key->next = (struct lysc_node *)key;
2850 } else {
2851 list->child->prev = (struct lysc_node *)key;
2852 }
2853 /* fix links in parent */
2854 if (!key->prev->next) {
2855 list->child = (struct lysc_node *)key;
2856 }
2857 }
2858
2859 /* next key value */
2860 prev_key = key;
2861 keystr = delim;
2862 lysc_update_path(ctx, NULL, NULL);
2863 }
2864
2865 /* uniques */
2866 if (list_p->uniques) {
2867 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
2868 }
2869
Michal Vasko5347e3a2020-11-03 17:14:57 +01002870 COMPILE_OP_ARRAY_GOTO(ctx, list_p->actions, list->actions, node, lys_compile_action, 0, ret, done);
2871 COMPILE_OP_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, node, lys_compile_notif, 0, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002872
2873 /* checks */
2874 if (list->min > list->max) {
2875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
2876 list->min, list->max);
2877 return LY_EVALID;
2878 }
2879
2880done:
2881 return ret;
2882}
2883
2884/**
2885 * @brief Do some checks and set the default choice's case.
2886 *
2887 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
2888 *
2889 * @param[in] ctx Compile context.
2890 * @param[in] dflt Name of the default branch. Can even contain a prefix.
2891 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
2892 * @return LY_ERR value.
2893 */
2894static LY_ERR
2895lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
2896{
2897 struct lysc_node *iter, *node = (struct lysc_node *)ch;
2898 const struct lys_module *mod;
2899 const char *prefix = NULL, *name;
2900 size_t prefix_len = 0;
2901
2902 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
2903 name = strchr(dflt->str, ':');
2904 if (name) {
2905 prefix = dflt->str;
2906 prefix_len = name - prefix;
2907 ++name;
2908 } else {
2909 name = dflt->str;
2910 }
2911 if (prefix) {
2912 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
2913 if (!mod) {
2914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
2915 "in imports of \"%s\".", prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
2916 return LY_EVALID;
2917 }
2918 } else {
2919 mod = node->module;
2920 }
2921
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002922 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 +02002923 if (!ch->dflt) {
2924 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2925 "Default case \"%s\" not found.", dflt->str);
2926 return LY_EVALID;
2927 }
2928
2929 /* no mandatory nodes directly under the default case */
2930 LY_LIST_FOR(ch->dflt->child, iter) {
2931 if (iter->parent != (struct lysc_node *)ch->dflt) {
2932 break;
2933 }
2934 if (iter->flags & LYS_MAND_TRUE) {
2935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2936 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
2937 return LY_EVALID;
2938 }
2939 }
2940
2941 if (ch->flags & LYS_MAND_TRUE) {
2942 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
2943 return LY_EVALID;
2944 }
2945
2946 ch->dflt->flags |= LYS_SET_DFLT;
2947 return LY_SUCCESS;
2948}
2949
2950LY_ERR
2951lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
2952 struct ly_set *child_set)
2953{
2954 LY_ERR ret = LY_SUCCESS;
2955 struct lysp_node *child_p_next = child_p->next;
2956 struct lysp_node_case *cs_p;
2957
2958 if (child_p->nodetype == LYS_CASE) {
2959 /* standard case under choice */
2960 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
2961 } else {
2962 /* we need the implicit case first, so create a fake parsed case */
2963 cs_p = calloc(1, sizeof *cs_p);
2964 cs_p->nodetype = LYS_CASE;
2965 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
2966 cs_p->child = child_p;
2967
2968 /* make the child the only case child */
2969 child_p->next = NULL;
2970
2971 /* compile it normally */
2972 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
2973
2974free_fake_node:
2975 /* free the fake parsed node and correct pointers back */
2976 cs_p->child = NULL;
2977 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
2978 child_p->next = child_p_next;
2979 }
2980
2981 return ret;
2982}
2983
2984/**
2985 * @brief Compile parsed choice node information.
2986 *
2987 * @param[in] ctx Compile context
2988 * @param[in] pnode Parsed choice node.
2989 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2990 * is enriched with the choice-specific information.
2991 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2992 */
2993static LY_ERR
2994lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2995{
2996 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
2997 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
2998 struct lysp_node *child_p;
2999 LY_ERR ret = LY_SUCCESS;
3000
3001 assert(node->nodetype == LYS_CHOICE);
3002
3003 LY_LIST_FOR(ch_p->child, child_p) {
3004 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3005 }
3006
3007 /* default branch */
3008 if (ch_p->dflt.str) {
3009 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3010 }
3011
3012 return ret;
3013}
3014
3015/**
3016 * @brief Compile parsed anydata or anyxml node information.
3017 * @param[in] ctx Compile context
3018 * @param[in] pnode Parsed anydata or anyxml node.
3019 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3020 * is enriched with the any-specific information.
3021 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3022 */
3023static LY_ERR
3024lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3025{
3026 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3027 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003028 LY_ERR ret = LY_SUCCESS;
3029
Michal Vasko5347e3a2020-11-03 17:14:57 +01003030 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003031 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003032 /* do not check "must" semantics in a grouping */
3033 ret = ly_set_add(&ctx->xpath, any, 0, NULL);
3034 LY_CHECK_GOTO(ret, done);
3035 }
3036
Michal Vasko75620aa2020-10-21 09:25:51 +02003037 if (!(ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) && (any->flags & LYS_CONFIG_W)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003038 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3039 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3040 }
3041done:
3042 return ret;
3043}
3044
3045/**
3046 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
3047 * keep specific order of augments targetting the same node.
3048 *
3049 * @param[in] ctx Compile context
3050 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3051 * the choice itself is expected instead of a specific case node.
3052 * @param[in] node Schema node to connect into the list.
3053 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3054 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
3055 */
3056static LY_ERR
3057lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
3058{
3059 struct lysc_node **children, *anchor = NULL;
3060 int insert_after = 0;
3061
3062 node->parent = parent;
3063
3064 if (parent) {
3065 if (parent->nodetype == LYS_CHOICE) {
3066 assert(node->nodetype == LYS_CASE);
3067 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
3068 } else {
3069 children = lysc_node_children_p(parent, ctx->options);
3070 }
3071 assert(children);
3072
3073 if (!(*children)) {
3074 /* first child */
3075 *children = node;
3076 } else if (node->flags & LYS_KEY) {
3077 /* special handling of adding keys */
3078 assert(node->module == parent->module);
3079 anchor = *children;
3080 if (anchor->flags & LYS_KEY) {
3081 while ((anchor->flags & LYS_KEY) && anchor->next) {
3082 anchor = anchor->next;
3083 }
3084 /* insert after the last key */
3085 insert_after = 1;
3086 } /* else insert before anchor (at the beginning) */
3087 } else if ((*children)->prev->module == node->module) {
3088 /* last child is from the same module, keep the order and insert at the end */
3089 anchor = (*children)->prev;
3090 insert_after = 1;
3091 } else if (parent->module == node->module) {
3092 /* adding module child after some augments were connected */
3093 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
3094 } else {
3095 /* some augments are already connected and we are connecting new ones,
3096 * keep module name order and insert the node into the children list */
3097 anchor = *children;
3098 do {
3099 anchor = anchor->prev;
3100
3101 /* check that we have not found the last augment node from our module or
3102 * the first augment node from a "smaller" module or
3103 * the first node from a local module */
3104 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
3105 (anchor->module == parent->module)) {
3106 /* insert after */
3107 insert_after = 1;
3108 break;
3109 }
3110
3111 /* we have traversed all the nodes, insert before anchor (as the first node) */
3112 } while (anchor->prev->next);
3113 }
3114
3115 /* insert */
3116 if (anchor) {
3117 if (insert_after) {
3118 node->next = anchor->next;
3119 node->prev = anchor;
3120 anchor->next = node;
3121 if (node->next) {
3122 /* middle node */
3123 node->next->prev = node;
3124 } else {
3125 /* last node */
3126 (*children)->prev = node;
3127 }
3128 } else {
3129 node->next = anchor;
3130 node->prev = anchor->prev;
3131 anchor->prev = node;
3132 if (anchor == *children) {
3133 /* first node */
3134 *children = node;
3135 } else {
3136 /* middle node */
3137 node->prev->next = node;
3138 }
3139 }
3140 }
3141
3142 /* check the name uniqueness (even for an only child, it may be in case) */
3143 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
3144 return LY_EEXIST;
3145 }
3146 } else {
3147 /* top-level element */
3148 if (!ctx->cur_mod->compiled->data) {
3149 ctx->cur_mod->compiled->data = node;
3150 } else {
3151 /* insert at the end of the module's top-level nodes list */
3152 ctx->cur_mod->compiled->data->prev->next = node;
3153 node->prev = ctx->cur_mod->compiled->data->prev;
3154 ctx->cur_mod->compiled->data->prev = node;
3155 }
3156
3157 /* check the name uniqueness on top-level */
3158 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
3159 return LY_EEXIST;
3160 }
3161 }
3162
3163 return LY_SUCCESS;
3164}
3165
3166/**
3167 * @brief Prepare the case structure in choice node for the new data node.
3168 *
3169 * 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
3170 * created in the choice when the first child was processed.
3171 *
3172 * @param[in] ctx Compile context.
3173 * @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,
3174 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3175 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3176 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3177 * @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,
3178 * it is linked from the case structure only in case it is its first child.
3179 */
3180static LY_ERR
3181lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3182{
3183 struct lysp_node *child_p;
3184 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3185
3186 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3187 /* we have to add an implicit case node into the parent choice */
3188 } else if (pnode->nodetype == LYS_CASE) {
3189 /* explicit parent case */
3190 LY_LIST_FOR(cs_p->child, child_p) {
3191 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3192 }
3193 } else {
3194 LOGINT_RET(ctx->ctx);
3195 }
3196
3197 return LY_SUCCESS;
3198}
3199
3200void
3201lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3202{
3203 struct lysc_node *iter;
3204
3205 if (add) { /* set flag */
3206 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3207 parent = parent->parent) {
3208 parent->flags |= LYS_MAND_TRUE;
3209 }
3210 } else { /* unset flag */
3211 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
3212 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
3213 if (iter->flags & LYS_MAND_TRUE) {
3214 /* there is another mandatory node */
3215 return;
3216 }
3217 }
3218 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3219 parent->flags &= ~LYS_MAND_TRUE;
3220 }
3221 }
3222}
3223
3224/**
3225 * @brief Find grouping for a uses.
3226 *
3227 * @param[in] ctx Compile context.
3228 * @param[in] uses_p Parsed uses node.
3229 * @param[out] gpr_p Found grouping on success.
3230 * @param[out] grp_pmod Module of @p grp_p on success.
3231 * @return LY_ERR value.
3232 */
3233static LY_ERR
3234lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
3235 struct lysp_module **grp_pmod)
3236{
3237 struct lysp_node *pnode;
3238 struct lysp_grp *grp;
3239 LY_ARRAY_COUNT_TYPE u, v;
3240 const char *id, *name, *prefix, *local_pref;
3241 size_t prefix_len, name_len;
3242 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003243 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003244
3245 *grp_p = NULL;
3246 *grp_pmod = NULL;
3247
3248 /* search for the grouping definition */
3249 id = uses_p->name;
3250 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3251 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3252 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3253 /* current module, search local groupings first */
3254 pmod = ctx->pmod;
3255 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
3256 grp = (struct lysp_grp *)lysp_node_groupings(pnode);
3257 LY_ARRAY_FOR(grp, u) {
3258 if (!strcmp(grp[u].name, name)) {
3259 grp = &grp[u];
3260 found = ctx->pmod;
3261 break;
3262 }
3263 }
3264 }
3265 } else {
3266 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003267 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003268 if (!mod) {
3269 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3270 "Invalid prefix used for grouping reference.", uses_p->name);
3271 return LY_EVALID;
3272 }
3273 pmod = mod->parsed;
3274 }
3275
3276 if (!found) {
3277 /* search in top-level groupings of the main module ... */
3278 grp = pmod->groupings;
3279 LY_ARRAY_FOR(grp, u) {
3280 if (!strcmp(grp[u].name, name)) {
3281 grp = &grp[u];
3282 found = pmod;
3283 break;
3284 }
3285 }
3286 if (!found) {
3287 /* ... and all the submodules */
3288 LY_ARRAY_FOR(pmod->includes, u) {
3289 grp = pmod->includes[u].submodule->groupings;
3290 LY_ARRAY_FOR(grp, v) {
3291 if (!strcmp(grp[v].name, name)) {
3292 grp = &grp[v];
3293 found = (struct lysp_module *)pmod->includes[u].submodule;
3294 break;
3295 }
3296 }
3297 if (found) {
3298 break;
3299 }
3300 }
3301 }
3302 }
3303 if (!found) {
3304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3305 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3306 return LY_EVALID;
3307 }
3308
3309 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3310 /* remember that the grouping is instantiated to avoid its standalone validation */
3311 grp->flags |= LYS_USED_GRP;
3312 }
3313
3314 *grp_p = grp;
3315 *grp_pmod = found;
3316 return LY_SUCCESS;
3317}
3318
3319/**
3320 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3321 * If present, also apply uses's modificators.
3322 *
3323 * @param[in] ctx Compile context
3324 * @param[in] uses_p Parsed uses schema node.
3325 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3326 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3327 * the compile context.
3328 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3329 */
3330static LY_ERR
3331lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3332{
3333 struct lysp_node *pnode;
3334 struct lysc_node *child;
3335 struct lysp_grp *grp = NULL;
3336 uint32_t i, grp_stack_count;
3337 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3338 LY_ERR ret = LY_SUCCESS;
3339 struct lysc_when *when_shared = NULL;
3340 LY_ARRAY_COUNT_TYPE u;
3341 struct lysc_notif **notifs = NULL;
3342 struct lysc_action **actions = NULL;
3343 struct ly_set uses_child_set = {0};
3344
3345 /* find the referenced grouping */
3346 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3347
3348 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3349 grp_stack_count = ctx->groupings.count;
3350 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3351 if (grp_stack_count == ctx->groupings.count) {
3352 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
3353 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3354 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3355 return LY_EVALID;
3356 }
3357
3358 /* check status */
3359 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3360 LY_CHECK_GOTO(ret, cleanup);
3361
3362 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3363 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3364 LY_CHECK_GOTO(ret, cleanup);
3365
3366 /* switch context's parsed module being processed */
3367 ctx->pmod = grp_mod;
3368
3369 /* compile data nodes */
3370 LY_LIST_FOR(grp->data, pnode) {
3371 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
3372 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3, &uses_child_set);
3373 LY_CHECK_GOTO(ret, cleanup);
3374 }
3375
3376 if (child_set) {
3377 /* add these children to our compiled child_set as well since uses is a schema-only node */
3378 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3379 }
3380
3381 if (uses_p->when) {
3382 /* pass uses's when to all the data children */
3383 for (i = 0; i < uses_child_set.count; ++i) {
3384 child = uses_child_set.snodes[i];
3385
3386 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent), child, &when_shared);
3387 LY_CHECK_GOTO(ret, cleanup);
3388 }
3389 }
3390
3391 /* compile actions */
3392 if (grp->actions) {
3393 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3394 if (!actions) {
3395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3396 grp->actions[0].name, lys_nodetype2str(grp->actions[0].nodetype), parent->name,
3397 lys_nodetype2str(parent->nodetype));
3398 ret = LY_EVALID;
3399 goto cleanup;
3400 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01003401 COMPILE_OP_ARRAY_GOTO(ctx, grp->actions, *actions, parent, lys_compile_action, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003402
3403 if (uses_p->when) {
3404 /* inherit when */
3405 LY_ARRAY_FOR(*actions, u) {
3406 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
3407 (struct lysc_node *)&(*actions)[u], &when_shared);
3408 LY_CHECK_GOTO(ret, cleanup);
3409 }
3410 }
3411 }
3412
3413 /* compile notifications */
3414 if (grp->notifs) {
3415 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3416 if (!notifs) {
3417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3418 grp->notifs[0].name, lys_nodetype2str(grp->notifs[0].nodetype), parent->name,
3419 lys_nodetype2str(parent->nodetype));
3420 ret = LY_EVALID;
3421 goto cleanup;
3422 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01003423 COMPILE_OP_ARRAY_GOTO(ctx, grp->notifs, *notifs, parent, lys_compile_notif, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003424
3425 if (uses_p->when) {
3426 /* inherit when */
3427 LY_ARRAY_FOR(*notifs, u) {
3428 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
3429 (struct lysc_node *)&(*notifs)[u], &when_shared);
3430 LY_CHECK_GOTO(ret, cleanup);
3431 }
3432 }
3433 }
3434
3435 /* check that all augments were applied */
3436 for (i = 0; i < ctx->uses_augs.count; ++i) {
3437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3438 "Augment target node \"%s\" in grouping \"%s\" was not found.",
3439 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3440 ret = LY_ENOTFOUND;
3441 }
3442 LY_CHECK_GOTO(ret, cleanup);
3443
3444 /* check that all refines were applied */
3445 for (i = 0; i < ctx->uses_rfns.count; ++i) {
3446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3447 "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
3448 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3449 ret = LY_ENOTFOUND;
3450 }
3451 LY_CHECK_GOTO(ret, cleanup);
3452
3453cleanup:
3454 /* reload previous context's parsed module being processed */
3455 ctx->pmod = mod_old;
3456
3457 /* remove the grouping from the stack for circular groupings dependency check */
3458 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3459 assert(ctx->groupings.count == grp_stack_count);
3460
3461 ly_set_erase(&uses_child_set, NULL);
3462 return ret;
3463}
3464
3465static int
3466lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3467{
3468 struct lysp_node *iter;
3469 int len = 0;
3470
3471 *path = NULL;
3472 for (iter = node; iter && len >= 0; iter = iter->parent) {
3473 char *s = *path;
3474 char *id;
3475
3476 switch (iter->nodetype) {
3477 case LYS_USES:
3478 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3479 break;
3480 case LYS_GROUPING:
3481 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3482 break;
3483 case LYS_AUGMENT:
3484 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3485 break;
3486 default:
3487 id = strdup(iter->name);
3488 break;
3489 }
3490
3491 if (!iter->parent) {
3492 /* print prefix */
3493 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3494 } else {
3495 /* prefix is the same as in parent */
3496 len = asprintf(path, "/%s%s", id, s ? s : "");
3497 }
3498 free(s);
3499 free(id);
3500 }
3501
3502 if (len < 0) {
3503 free(*path);
3504 *path = NULL;
3505 } else if (len == 0) {
3506 *path = strdup("/");
3507 len = 1;
3508 }
3509 return len;
3510}
3511
3512LY_ERR
3513lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_grp *grp)
3514{
3515 LY_ERR ret;
3516 char *path;
3517 int len;
3518
3519 struct lysp_node_uses fake_uses = {
3520 .parent = pnode,
3521 .nodetype = LYS_USES,
3522 .flags = 0, .next = NULL,
3523 .name = grp->name,
3524 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3525 .refines = NULL, .augments = NULL
3526 };
3527 struct lysc_node_container fake_container = {
3528 .nodetype = LYS_CONTAINER,
3529 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3530 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003531 .parent = NULL, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003532 .prev = (struct lysc_node *)&fake_container,
3533 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003534 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003535 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3536 };
3537
3538 if (grp->parent) {
3539 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3540 }
3541
3542 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3543 if (len < 0) {
3544 LOGMEM(ctx->ctx);
3545 return LY_EMEM;
3546 }
3547 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3548 ctx->path_len = (uint32_t)len;
3549 free(path);
3550
3551 lysc_update_path(ctx, NULL, "{grouping}");
3552 lysc_update_path(ctx, NULL, grp->name);
3553 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
3554 lysc_update_path(ctx, NULL, NULL);
3555 lysc_update_path(ctx, NULL, NULL);
3556
3557 ctx->path_len = 1;
3558 ctx->path[1] = '\0';
3559
3560 /* cleanup */
3561 lysc_node_container_free(ctx->ctx, &fake_container);
3562
3563 return ret;
3564}
3565
3566/**
3567 * @brief Set config flags for a node.
3568 *
3569 * @param[in] ctx Compile context.
3570 * @param[in] node Compiled node config to set.
3571 * @param[in] parent Parent of @p node.
3572 * @return LY_ERR value.
3573 */
3574static LY_ERR
3575lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
3576{
3577 if (node->nodetype == LYS_CASE) {
3578 /* case never has any config */
3579 assert(!(node->flags & LYS_CONFIG_MASK));
3580 return LY_SUCCESS;
3581 }
3582
3583 /* adjust parent to always get the ancestor with config */
3584 if (parent && (parent->nodetype == LYS_CASE)) {
3585 parent = parent->parent;
3586 assert(parent);
3587 }
3588
3589 if (ctx->options & (LYS_COMPILE_RPC_INPUT | LYS_COMPILE_RPC_OUTPUT)) {
3590 /* ignore config statements inside RPC/action data */
3591 node->flags &= ~LYS_CONFIG_MASK;
3592 node->flags |= (ctx->options & LYS_COMPILE_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
3593 } else if (ctx->options & LYS_COMPILE_NOTIFICATION) {
3594 /* ignore config statements inside Notification data */
3595 node->flags &= ~LYS_CONFIG_MASK;
3596 node->flags |= LYS_CONFIG_R;
3597 } else if (!(node->flags & LYS_CONFIG_MASK)) {
3598 /* config not explicitely set, inherit it from parent */
3599 if (parent) {
3600 node->flags |= parent->flags & LYS_CONFIG_MASK;
3601 } else {
3602 /* default is config true */
3603 node->flags |= LYS_CONFIG_W;
3604 }
3605 } else {
3606 /* config set explicitely */
3607 node->flags |= LYS_SET_CONFIG;
3608 }
3609
3610 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3611 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3612 "Configuration node cannot be child of any state data node.");
3613 return LY_EVALID;
3614 }
3615
3616 return LY_SUCCESS;
3617}
3618
3619LY_ERR
3620lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3621 struct ly_set *child_set)
3622{
3623 LY_ERR ret = LY_SUCCESS;
3624 struct lysc_node *node = NULL;
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003625 struct lysp_node *dev_pnode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003626 ly_bool not_supported, enabled;
3627 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003628
3629 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3630
3631 if (pnode->nodetype != LYS_USES) {
3632 lysc_update_path(ctx, parent, pnode->name);
3633 } else {
3634 lysc_update_path(ctx, NULL, "{uses}");
3635 lysc_update_path(ctx, NULL, pnode->name);
3636 }
3637
3638 switch (pnode->nodetype) {
3639 case LYS_CONTAINER:
3640 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3641 node_compile_spec = lys_compile_node_container;
3642 break;
3643 case LYS_LEAF:
3644 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3645 node_compile_spec = lys_compile_node_leaf;
3646 break;
3647 case LYS_LIST:
3648 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3649 node_compile_spec = lys_compile_node_list;
3650 break;
3651 case LYS_LEAFLIST:
3652 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3653 node_compile_spec = lys_compile_node_leaflist;
3654 break;
3655 case LYS_CHOICE:
3656 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3657 node_compile_spec = lys_compile_node_choice;
3658 break;
3659 case LYS_CASE:
3660 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3661 node_compile_spec = lys_compile_node_case;
3662 break;
3663 case LYS_ANYXML:
3664 case LYS_ANYDATA:
3665 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3666 node_compile_spec = lys_compile_node_any;
3667 break;
3668 case LYS_USES:
3669 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3670 lysc_update_path(ctx, NULL, NULL);
3671 lysc_update_path(ctx, NULL, NULL);
3672 return ret;
3673 default:
3674 LOGINT(ctx->ctx);
3675 return LY_EINT;
3676 }
3677 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3678
3679 /* compile any deviations for this node */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003680 LY_CHECK_ERR_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported),
3681 free(node), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003682 if (not_supported) {
3683 free(node);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003684 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003685 } else if (dev_pnode) {
3686 pnode = dev_pnode;
3687 }
3688
3689 node->nodetype = pnode->nodetype;
3690 node->module = ctx->cur_mod;
3691 node->prev = node;
3692 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
3693
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003694 /* if-features */
3695 LY_CHECK_ERR_RET(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), free(node), ret);
Michal Vasko12606ee2020-11-25 17:05:11 +01003696 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003697 ly_set_add(&ctx->disabled, node, 1, NULL);
3698 ctx->options |= LYS_COMPILE_DISABLED;
3699 }
3700
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003701 /* config */
3702 ret = lys_compile_config(ctx, node, parent);
3703 LY_CHECK_GOTO(ret, error);
3704
3705 /* list ordering */
3706 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3707 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
3708 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
3709 (ctx->options & LYS_COMPILE_RPC_OUTPUT) ? "RPC/action output parameters" :
3710 (ctx->options & LYS_COMPILE_NOTIFICATION) ? "notification content" : "state data", ctx->path);
3711 node->flags &= ~LYS_ORDBY_MASK;
3712 node->flags |= LYS_ORDBY_SYSTEM;
3713 } else if (!(node->flags & LYS_ORDBY_MASK)) {
3714 /* default ordering is system */
3715 node->flags |= LYS_ORDBY_SYSTEM;
3716 }
3717 }
3718
3719 /* status - it is not inherited by specification, but it does not make sense to have
3720 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3721 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
3722
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003723 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
3724 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
3725 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003726
3727 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
3728 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
3729
3730 if (pnode->when) {
3731 /* compile when */
3732 ret = lys_compile_when(ctx, pnode->when, pnode->flags, lysc_xpath_context(node), node, NULL);
3733 LY_CHECK_GOTO(ret, cleanup);
3734 }
3735
3736 /* connect any augments */
3737 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
3738
3739 /* nodetype-specific part */
3740 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
3741
3742 /* final compilation tasks that require the node to be connected */
3743 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, LYEXT_PAR_NODE, ret, cleanup);
3744 if (node->flags & LYS_MAND_TRUE) {
3745 /* inherit LYS_MAND_TRUE in parent containers */
3746 lys_compile_mandatory_parents(parent, 1);
3747 }
3748
3749 if (child_set) {
3750 /* add the new node into set */
3751 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
3752 }
3753
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003754 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003755
3756error:
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003757 lysc_node_free(ctx->ctx, node, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003758cleanup:
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003759 ctx->options = prev_opts;
3760 if (ret && dev_pnode) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003761 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 +02003762 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003763 lysp_dev_node_free(ctx->ctx, dev_pnode);
3764 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003765 return ret;
3766}