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