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