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