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