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