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