blob: ba5b96cbd13514ea77f96b34392695c2112f364d [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of common nodes.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020017
18#include "schema_compile_node.h"
19
20#include <assert.h>
21#include <ctype.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "common.h"
29#include "compat.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020030#include "dict.h"
31#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020032#include "plugins_exts.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020033#include "plugins_types.h"
34#include "schema_compile.h"
35#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010036#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "set.h"
38#include "tree.h"
39#include "tree_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020040#include "tree_schema.h"
41#include "tree_schema_internal.h"
42#include "xpath.h"
43
44static struct lysc_ext_instance *
45lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
46{
47 /* TODO - extensions, increase refcount */
48 (void) ctx;
49 (void) orig;
50 return NULL;
51}
52
53/**
54 * @brief Add/replace a leaf default value in unres.
55 * Can also be used for a single leaf-list default value.
56 *
57 * @param[in] ctx Compile context.
58 * @param[in] leaf Leaf with the default value.
59 * @param[in] dflt Default value to use.
60 * @return LY_ERR value.
61 */
62static LY_ERR
63lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
64{
65 struct lysc_unres_dflt *r = NULL;
66 uint32_t i;
67
Michal Vasko12606ee2020-11-25 17:05:11 +010068 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010069 return LY_SUCCESS;
70 }
71
Michal Vasko405cc9e2020-12-01 12:01:27 +010072 for (i = 0; i < ctx->unres->dflts.count; ++i) {
73 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020074 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010075 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020076 lysp_qname_free(ctx->ctx, r->dflt);
77 free(r->dflt);
78 break;
79 }
80 }
81 if (!r) {
82 /* add new unres item */
83 r = calloc(1, sizeof *r);
84 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
85 r->leaf = leaf;
86
Michal Vasko405cc9e2020-12-01 12:01:27 +010087 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020088 }
89
90 r->dflt = malloc(sizeof *r->dflt);
91 LY_CHECK_GOTO(!r->dflt, error);
92 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
93
94 return LY_SUCCESS;
95
96error:
97 free(r->dflt);
98 LOGMEM(ctx->ctx);
99 return LY_EMEM;
100}
101
102/**
103 * @brief Add/replace a leaf-list default value(s) in unres.
104 *
105 * @param[in] ctx Compile context.
106 * @param[in] llist Leaf-list with the default value.
107 * @param[in] dflts Sized array of the default values.
108 * @return LY_ERR value.
109 */
110static LY_ERR
111lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
112{
113 struct lysc_unres_dflt *r = NULL;
114 uint32_t i;
115
Michal Vasko12606ee2020-11-25 17:05:11 +0100116 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100117 return LY_SUCCESS;
118 }
119
Michal Vasko405cc9e2020-12-01 12:01:27 +0100120 for (i = 0; i < ctx->unres->dflts.count; ++i) {
121 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200122 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100123 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200124 lysp_qname_free(ctx->ctx, r->dflt);
125 free(r->dflt);
126 r->dflt = NULL;
127 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
128 r->dflts = NULL;
129 break;
130 }
131 }
132 if (!r) {
133 r = calloc(1, sizeof *r);
134 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
135 r->llist = llist;
136
Michal Vasko405cc9e2020-12-01 12:01:27 +0100137 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200138 }
139
140 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
141
142 return LY_SUCCESS;
143}
144
145/**
146 * @brief Duplicate the compiled pattern structure.
147 *
148 * Instead of duplicating memory, the reference counter in the @p orig is increased.
149 *
150 * @param[in] orig The pattern structure to duplicate.
151 * @return The duplicated structure to use.
152 */
153static struct lysc_pattern *
154lysc_pattern_dup(struct lysc_pattern *orig)
155{
156 ++orig->refcount;
157 return orig;
158}
159
160/**
161 * @brief Duplicate the array of compiled patterns.
162 *
163 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
164 *
165 * @param[in] ctx Libyang context for logging.
166 * @param[in] orig The patterns sized array to duplicate.
167 * @return New sized array as a copy of @p orig.
168 * @return NULL in case of memory allocation error.
169 */
170static struct lysc_pattern **
171lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
172{
173 struct lysc_pattern **dup = NULL;
174 LY_ARRAY_COUNT_TYPE u;
175
176 assert(orig);
177
178 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
179 LY_ARRAY_FOR(orig, u) {
180 dup[u] = lysc_pattern_dup(orig[u]);
181 LY_ARRAY_INCREMENT(dup);
182 }
183 return dup;
184}
185
186/**
187 * @brief Duplicate compiled range structure.
188 *
189 * @param[in] ctx Libyang context for logging.
190 * @param[in] orig The range structure to be duplicated.
191 * @return New compiled range structure as a copy of @p orig.
192 * @return NULL in case of memory allocation error.
193 */
194static struct lysc_range *
195lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
196{
197 struct lysc_range *dup;
198 LY_ERR ret;
199
200 assert(orig);
201
202 dup = calloc(1, sizeof *dup);
203 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
204 if (orig->parts) {
205 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100206 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200207 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
208 }
209 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
210 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
211 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
212
213 return dup;
214cleanup:
215 free(dup);
216 (void) ret; /* set but not used due to the return type */
217 return NULL;
218}
219
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)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100379 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200380 "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) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100581 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200582 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
583 length_restr ? "length" : "range", valcopy ? valcopy : *value);
584 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100585 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200586 "Invalid %s restriction - invalid value \"%s\".",
587 length_restr ? "length" : "range", valcopy ? valcopy : *value);
588 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100589 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200590 "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) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100631 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200632 "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))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100636 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200637 "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 */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100646 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200647 "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) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100658 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200659 "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)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100671 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200672 "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') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100697 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200698 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 {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100712 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200713 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:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100800 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200801 "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
Radek Krejci2efc45b2020-12-22 16:25:44 +0100841lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200842{
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) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100998 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200999 free(perl_regex);
1000 return LY_EVALID;
1001 }
1002 end = (ptr - perl_regex) + 1;
1003
1004 /* need more space */
1005 if (end - start < URANGE_LEN) {
1006 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1007 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1008 }
1009
1010 /* find our range */
1011 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001012 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1013 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001014 break;
1015 }
1016 }
1017 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001018 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001019 free(perl_regex);
1020 return LY_EVALID;
1021 }
1022
1023 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1024 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1025 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1026 ++idx;
1027 }
1028 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1029 --idx;
1030 }
1031 }
1032 if (idx) {
1033 /* skip brackets */
1034 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1035 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1036 } else {
1037 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1038 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1039 }
1040 }
1041
1042 /* must return 0, already checked during parsing */
1043 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1044 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1045 &err_code, &err_offset, NULL);
1046 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001047 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1048 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001049 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001050 free(perl_regex);
1051 return LY_EVALID;
1052 }
1053 free(perl_regex);
1054
1055 if (code) {
1056 *code = code_local;
1057 } else {
1058 free(code_local);
1059 }
1060
1061 return LY_SUCCESS;
1062
1063#undef URANGE_LEN
1064}
1065
1066/**
1067 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1068 * @param[in] ctx Compile context.
1069 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1070 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1071 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1072 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1073 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1074 */
1075static LY_ERR
1076lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1077 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1078{
1079 struct lysc_pattern **pattern;
1080 LY_ARRAY_COUNT_TYPE u;
1081 LY_ERR ret = LY_SUCCESS;
1082
1083 /* first, copy the patterns from the base type */
1084 if (base_patterns) {
1085 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1086 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1087 }
1088
1089 LY_ARRAY_FOR(patterns_p, u) {
1090 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1091 *pattern = calloc(1, sizeof **pattern);
1092 ++(*pattern)->refcount;
1093
Radek Krejci2efc45b2020-12-22 16:25:44 +01001094 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001095 LY_CHECK_RET(ret);
1096
Radek Krejcif13b87b2020-12-01 22:02:17 +01001097 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001098 (*pattern)->inverted = 1;
1099 }
1100 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1101 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1102 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1103 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1104 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
1105 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
1106 }
1107done:
1108 return ret;
1109}
1110
1111/**
1112 * @brief map of the possible restrictions combination for the specific built-in type.
1113 */
1114static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1115 0 /* LY_TYPE_UNKNOWN */,
1116 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1117 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1118 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1119 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1120 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1121 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1122 LYS_SET_BIT /* LY_TYPE_BITS */,
1123 0 /* LY_TYPE_BOOL */,
1124 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1125 0 /* LY_TYPE_EMPTY */,
1126 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1127 LYS_SET_BASE /* LY_TYPE_IDENT */,
1128 LYS_SET_REQINST /* LY_TYPE_INST */,
1129 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1130 LYS_SET_TYPE /* LY_TYPE_UNION */,
1131 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1132 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1133 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1134 LYS_SET_RANGE /* LY_TYPE_INT64 */
1135};
1136
1137/**
1138 * @brief stringification of the YANG built-in data types
1139 */
1140const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
1141 "unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1142 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1143 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
1144};
1145
1146/**
1147 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1148 * @param[in] ctx Compile context.
1149 * @param[in] enums_p Array of the parsed enum structures to compile.
1150 * @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.
1151 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1152 * @param[out] enums Newly created array of the compiled enums information for the current type.
1153 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1154 */
1155static LY_ERR
1156lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
1157 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
1158{
1159 LY_ERR ret = LY_SUCCESS;
1160 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001161 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1162 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001163 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001164 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001165
1166 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001167 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001168 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1169 return LY_EVALID;
1170 }
1171
1172 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001173 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001174 if (base_enums) {
1175 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1176 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001177 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001178 break;
1179 }
1180 }
1181 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001182 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001183 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001184 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001185 return LY_EVALID;
1186 }
1187 match = v;
1188 }
1189
1190 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001191 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001192 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001193 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001194 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001195 LY_ARRAY_FOR(*enums, v) {
1196 if (cur_val == (*enums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001197 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001198 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001199 cur_val, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001200 return LY_EVALID;
1201 }
1202 }
1203 } else if (base_enums) {
1204 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001205 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001206 } else {
1207 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001208 if (u == 0) {
1209 cur_val = 0;
1210 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001211 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001212 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001213 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001214 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001215 } else {
1216 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001217 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001218 }
1219
1220 /* save highest value for auto assing */
1221 if (highest_value < cur_val) {
1222 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001223 }
1224 } else { /* LY_TYPE_BITS */
1225 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001226 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001227 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001228 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001229 LY_ARRAY_FOR(*enums, v) {
1230 if (cur_pos == (*enums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001231 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001232 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001233 cur_pos, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001234 return LY_EVALID;
1235 }
1236 }
1237 } else if (base_enums) {
1238 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001239 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001240 } else {
1241 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001242 if (u == 0) {
1243 cur_pos = 0;
1244 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001245 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001246 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001247 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001248 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001250 } else {
1251 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001252 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001253 }
1254
1255 /* save highest position for auto assing */
1256 if (highest_position < cur_pos) {
1257 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001258 }
1259 }
1260
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001261 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001262 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001263 if (basetype == LY_TYPE_ENUM) {
1264 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001265 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001266 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001267 enums_p[u].name, base_enums[match].value, cur_val);
1268 return LY_EVALID;
1269 }
1270 } else {
1271 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001272 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001273 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001274 enums_p[u].name, base_enums[match].position, cur_pos);
1275 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001276 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001277 }
1278 }
1279
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001280 /* evaluate if-ffeatures */
1281 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1282 if (!enabled) {
1283 continue;
1284 }
1285
1286 /* add new enum/bit */
1287 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1288 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1289 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1290 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
1291 e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) | (basetype == LY_TYPE_ENUM ? LYS_ISENUM : 0);
1292 if (basetype == LY_TYPE_ENUM) {
1293 e->value = cur_val;
1294 } else {
1295 e->position = cur_pos;
1296 }
1297 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM :
1298 LYEXT_PAR_TYPE_BIT, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001299
1300 if (basetype == LY_TYPE_BITS) {
1301 /* keep bits ordered by position */
1302 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
1303 if (v != u) {
1304 memcpy(&storage, e, sizeof *e);
1305 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1306 memcpy(&(*enums)[v], &storage, sizeof storage);
1307 }
1308 }
1309 }
1310
1311done:
1312 return ret;
1313}
1314
Radek Krejci6a205692020-12-09 13:58:22 +01001315static LY_ERR
1316lys_compile_type_union(struct lysc_ctx *ctx, struct lysp_type *ptypes, struct lysp_node *context_pnode, uint16_t context_flags,
1317 struct lysp_module *context_mod, const char *context_name, struct lysc_type ***utypes_p)
1318{
1319 LY_ERR ret = LY_SUCCESS;
1320 struct lysc_type **utypes = *utypes_p;
1321 struct lysc_type_union *un_aux = NULL;
1322
1323 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1324 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
1325 ret = lys_compile_type(ctx, context_pnode, context_flags, context_mod, context_name, &ptypes[u], &utypes[u + additional], NULL, NULL);
1326 LY_CHECK_GOTO(ret, error);
1327 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1328 /* add space for additional types from the union subtype */
1329 un_aux = (struct lysc_type_union *)utypes[u + additional];
1330 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1331 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1332
1333 /* copy subtypes of the subtype union */
1334 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1335 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1336 struct lysc_type_leafref *lref;
1337
1338 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1339 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1340 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1341 lref = (struct lysc_type_leafref *)utypes[u + additional];
1342
1343 lref->basetype = LY_TYPE_LEAFREF;
1344 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1345 LY_CHECK_GOTO(ret, error);
1346 lref->refcount = 1;
1347 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
1348 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
1349 ret = lysc_prefixes_dup(((struct lysc_type_leafref *)un_aux->types[v])->prefixes, &lref->prefixes);
1350 LY_CHECK_GOTO(ret, error);
1351 /* TODO extensions */
1352
1353 } else {
1354 utypes[u + additional] = un_aux->types[v];
1355 ++un_aux->types[v]->refcount;
1356 }
1357 ++additional;
1358 LY_ARRAY_INCREMENT(utypes);
1359 }
1360 /* compensate u increment in main loop */
1361 --additional;
1362
1363 /* free the replaced union subtype */
1364 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1365 un_aux = NULL;
1366 } else {
1367 LY_ARRAY_INCREMENT(utypes);
1368 }
1369 }
1370
1371 *utypes_p = utypes;
1372 return LY_SUCCESS;
1373
1374error:
1375 if (un_aux) {
1376 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1377 }
1378 *utypes_p = utypes;
1379 return ret;
1380}
1381
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001382/**
1383 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1384 * @param[in] ctx Compile context.
1385 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1386 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
1387 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
1388 * @param[in] context_name Name of the context node or referencing typedef for logging.
1389 * @param[in] type_p Parsed type to compile.
1390 * @param[in] basetype Base YANG built-in type of the type to compile.
1391 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1392 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1393 * @param[out] type Newly created type structure with the filled information about the type.
1394 * @return LY_ERR value.
1395 */
1396static LY_ERR
1397lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
1398 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, LY_DATA_TYPE basetype,
1399 const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
1400{
1401 LY_ERR ret = LY_SUCCESS;
1402 struct lysc_type_bin *bin;
1403 struct lysc_type_num *num;
1404 struct lysc_type_str *str;
1405 struct lysc_type_bits *bits;
1406 struct lysc_type_enum *enumeration;
1407 struct lysc_type_dec *dec;
1408 struct lysc_type_identityref *idref;
1409 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001410 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001411
1412 switch (basetype) {
1413 case LY_TYPE_BINARY:
1414 bin = (struct lysc_type_bin *)(*type);
1415
1416 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1417 if (type_p->length) {
1418 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1419 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1420 if (!tpdfname) {
1421 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, cleanup);
1422 }
1423 }
1424 break;
1425 case LY_TYPE_BITS:
1426 /* RFC 7950 9.7 - bits */
1427 bits = (struct lysc_type_bits *)(*type);
1428 if (type_p->bits) {
1429 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1430 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1431 (struct lysc_type_bitenum_item **)&bits->bits));
1432 }
1433
1434 if (!base && !type_p->flags) {
1435 /* type derived from bits built-in type must contain at least one bit */
1436 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001437 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001438 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001439 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001440 }
1441 return LY_EVALID;
1442 }
1443 break;
1444 case LY_TYPE_DEC64:
1445 dec = (struct lysc_type_dec *)(*type);
1446
1447 /* RFC 7950 9.3.4 - fraction-digits */
1448 if (!base) {
1449 if (!type_p->fraction_digits) {
1450 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001451 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001452 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001453 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001454 }
1455 return LY_EVALID;
1456 }
1457 dec->fraction_digits = type_p->fraction_digits;
1458 } else {
1459 if (type_p->fraction_digits) {
1460 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1461 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001462 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001463 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1464 tpdfname);
1465 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001466 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001467 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1468 }
1469 return LY_EVALID;
1470 }
1471 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1472 }
1473
1474 /* RFC 7950 9.2.4 - range */
1475 if (type_p->range) {
1476 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1477 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1478 if (!tpdfname) {
1479 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, cleanup);
1480 }
1481 }
1482 break;
1483 case LY_TYPE_STRING:
1484 str = (struct lysc_type_str *)(*type);
1485
1486 /* RFC 7950 9.4.4 - length */
1487 if (type_p->length) {
1488 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1489 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1490 if (!tpdfname) {
1491 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, cleanup);
1492 }
1493 } else if (base && ((struct lysc_type_str *)base)->length) {
1494 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1495 }
1496
1497 /* RFC 7950 9.4.5 - pattern */
1498 if (type_p->patterns) {
1499 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1500 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1501 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1502 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1503 }
1504 break;
1505 case LY_TYPE_ENUM:
1506 enumeration = (struct lysc_type_enum *)(*type);
1507
1508 /* RFC 7950 9.6 - enum */
1509 if (type_p->enums) {
1510 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1511 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1512 }
1513
1514 if (!base && !type_p->flags) {
1515 /* type derived from enumerations built-in type must contain at least one enum */
1516 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001517 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001518 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001519 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001520 }
1521 return LY_EVALID;
1522 }
1523 break;
1524 case LY_TYPE_INT8:
1525 case LY_TYPE_UINT8:
1526 case LY_TYPE_INT16:
1527 case LY_TYPE_UINT16:
1528 case LY_TYPE_INT32:
1529 case LY_TYPE_UINT32:
1530 case LY_TYPE_INT64:
1531 case LY_TYPE_UINT64:
1532 num = (struct lysc_type_num *)(*type);
1533
1534 /* RFC 6020 9.2.4 - range */
1535 if (type_p->range) {
1536 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1537 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1538 if (!tpdfname) {
1539 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, cleanup);
1540 }
1541 }
1542 break;
1543 case LY_TYPE_IDENT:
1544 idref = (struct lysc_type_identityref *)(*type);
1545
1546 /* RFC 7950 9.10.2 - base */
1547 if (type_p->bases) {
1548 if (base) {
1549 /* only the directly derived identityrefs can contain base specification */
1550 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001551 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001552 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1553 tpdfname);
1554 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001555 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001556 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1557 }
1558 return LY_EVALID;
1559 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001560 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 +02001561 }
1562
1563 if (!base && !type_p->flags) {
1564 /* type derived from identityref built-in type must contain at least one base */
1565 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001566 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001567 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001568 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001569 }
1570 return LY_EVALID;
1571 }
1572 break;
1573 case LY_TYPE_LEAFREF:
1574 lref = (struct lysc_type_leafref *)*type;
1575
1576 /* RFC 7950 9.9.3 - require-instance */
1577 if (type_p->flags & LYS_SET_REQINST) {
1578 if (context_mod->version < LYS_VERSION_1_1) {
1579 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001580 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001581 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1582 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001583 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001584 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1585 }
1586 return LY_EVALID;
1587 }
1588 lref->require_instance = type_p->require_instance;
1589 } else if (base) {
1590 /* inherit */
1591 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1592 } else {
1593 /* default is true */
1594 lref->require_instance = 1;
1595 }
1596 if (type_p->path) {
1597 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
1598 LY_CHECK_RET(lysc_prefixes_compile(type_p->path->expr, strlen(type_p->path->expr), type_p->pmod,
1599 &lref->prefixes));
1600 } else if (base) {
1601 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
1602 LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)base)->prefixes, &lref->prefixes));
1603 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001604 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001605 return LY_EVALID;
1606 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001607 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001608 return LY_EVALID;
1609 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001610 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001611 break;
1612 case LY_TYPE_INST:
1613 /* RFC 7950 9.9.3 - require-instance */
1614 if (type_p->flags & LYS_SET_REQINST) {
1615 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1616 } else {
1617 /* default is true */
1618 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1619 }
1620 break;
1621 case LY_TYPE_UNION:
1622 un = (struct lysc_type_union *)(*type);
1623
1624 /* RFC 7950 7.4 - type */
1625 if (type_p->types) {
1626 if (base) {
1627 /* only the directly derived union can contain types specification */
1628 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001629 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001630 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1631 tpdfname);
1632 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001633 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001634 "Invalid type substatement for the type not directly derived from union built-in type.");
1635 }
1636 return LY_EVALID;
1637 }
1638 /* compile the type */
Radek Krejci6a205692020-12-09 13:58:22 +01001639 LY_CHECK_RET(lys_compile_type_union(ctx, type_p->types, context_pnode, context_flags, context_mod, context_name,
1640 &un->types));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001641 }
1642
1643 if (!base && !type_p->flags) {
1644 /* type derived from union built-in type must contain at least one type */
1645 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001646 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001647 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001648 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001649 }
1650 return LY_EVALID;
1651 }
1652 break;
1653 case LY_TYPE_BOOL:
1654 case LY_TYPE_EMPTY:
1655 case LY_TYPE_UNKNOWN: /* just to complete switch */
1656 break;
1657 }
1658
1659 if (tpdfname) {
1660 switch (basetype) {
1661 case LY_TYPE_BINARY:
1662 type_p->compiled = *type;
1663 *type = calloc(1, sizeof(struct lysc_type_bin));
1664 break;
1665 case LY_TYPE_BITS:
1666 type_p->compiled = *type;
1667 *type = calloc(1, sizeof(struct lysc_type_bits));
1668 break;
1669 case LY_TYPE_DEC64:
1670 type_p->compiled = *type;
1671 *type = calloc(1, sizeof(struct lysc_type_dec));
1672 break;
1673 case LY_TYPE_STRING:
1674 type_p->compiled = *type;
1675 *type = calloc(1, sizeof(struct lysc_type_str));
1676 break;
1677 case LY_TYPE_ENUM:
1678 type_p->compiled = *type;
1679 *type = calloc(1, sizeof(struct lysc_type_enum));
1680 break;
1681 case LY_TYPE_INT8:
1682 case LY_TYPE_UINT8:
1683 case LY_TYPE_INT16:
1684 case LY_TYPE_UINT16:
1685 case LY_TYPE_INT32:
1686 case LY_TYPE_UINT32:
1687 case LY_TYPE_INT64:
1688 case LY_TYPE_UINT64:
1689 type_p->compiled = *type;
1690 *type = calloc(1, sizeof(struct lysc_type_num));
1691 break;
1692 case LY_TYPE_IDENT:
1693 type_p->compiled = *type;
1694 *type = calloc(1, sizeof(struct lysc_type_identityref));
1695 break;
1696 case LY_TYPE_LEAFREF:
1697 type_p->compiled = *type;
1698 *type = calloc(1, sizeof(struct lysc_type_leafref));
1699 break;
1700 case LY_TYPE_INST:
1701 type_p->compiled = *type;
1702 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1703 break;
1704 case LY_TYPE_UNION:
1705 type_p->compiled = *type;
1706 *type = calloc(1, sizeof(struct lysc_type_union));
1707 break;
1708 case LY_TYPE_BOOL:
1709 case LY_TYPE_EMPTY:
1710 case LY_TYPE_UNKNOWN: /* just to complete switch */
1711 break;
1712 }
1713 }
1714 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1715
1716cleanup:
1717 return ret;
1718}
1719
1720LY_ERR
1721lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
1722 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
1723 struct lysc_type **type, const char **units, struct lysp_qname **dflt)
1724{
1725 LY_ERR ret = LY_SUCCESS;
1726 ly_bool dummyloops = 0;
1727 struct type_context {
1728 const struct lysp_tpdf *tpdf;
1729 struct lysp_node *node;
1730 struct lysp_module *mod;
1731 } *tctx, *tctx_prev = NULL, *tctx_iter;
1732 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1733 struct lysc_type *base = NULL, *prev_type;
1734 struct ly_set tpdf_chain = {0};
1735
1736 (*type) = NULL;
1737 if (dflt) {
1738 *dflt = NULL;
1739 }
1740
1741 tctx = calloc(1, sizeof *tctx);
1742 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1743 for (ret = lysp_type_find(type_p->name, context_pnode, ctx->pmod, &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
1744 ret == LY_SUCCESS;
1745 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
1746 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
1747 if (basetype) {
1748 break;
1749 }
1750
1751 /* check status */
1752 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
1753 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
1754 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1755
1756 if (units && !*units) {
1757 /* inherit units */
1758 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1759 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1760 }
1761 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1762 /* inherit default */
1763 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1764 }
1765 if (dummyloops && (!units || *units) && dflt && *dflt) {
1766 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1767 break;
1768 }
1769
1770 if (tctx->tpdf->type.compiled) {
1771 /* it is not necessary to continue, the rest of the chain was already compiled,
1772 * but we still may need to inherit default and units values, so start dummy loops */
1773 basetype = tctx->tpdf->type.compiled->basetype;
1774 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1775 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1776
1777 if ((units && !*units) || (dflt && !*dflt)) {
1778 dummyloops = 1;
1779 goto preparenext;
1780 } else {
1781 tctx = NULL;
1782 break;
1783 }
1784 }
1785
1786 /* circular typedef reference detection */
1787 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1788 /* local part */
1789 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
1790 if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001791 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001792 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1793 free(tctx);
1794 ret = LY_EVALID;
1795 goto cleanup;
1796 }
1797 }
1798 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1799 /* global part for unions corner case */
1800 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
1801 if ((tctx_iter->mod == tctx->mod) && (tctx_iter->node == tctx->node) && (tctx_iter->tpdf == tctx->tpdf)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001802 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001803 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1804 free(tctx);
1805 ret = LY_EVALID;
1806 goto cleanup;
1807 }
1808 }
1809
1810 /* store information for the following processing */
1811 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1812 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1813
1814preparenext:
1815 /* prepare next loop */
1816 tctx_prev = tctx;
1817 tctx = calloc(1, sizeof *tctx);
1818 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1819 }
1820 free(tctx);
1821
1822 /* allocate type according to the basetype */
1823 switch (basetype) {
1824 case LY_TYPE_BINARY:
1825 *type = calloc(1, sizeof(struct lysc_type_bin));
1826 break;
1827 case LY_TYPE_BITS:
1828 *type = calloc(1, sizeof(struct lysc_type_bits));
1829 break;
1830 case LY_TYPE_BOOL:
1831 case LY_TYPE_EMPTY:
1832 *type = calloc(1, sizeof(struct lysc_type));
1833 break;
1834 case LY_TYPE_DEC64:
1835 *type = calloc(1, sizeof(struct lysc_type_dec));
1836 break;
1837 case LY_TYPE_ENUM:
1838 *type = calloc(1, sizeof(struct lysc_type_enum));
1839 break;
1840 case LY_TYPE_IDENT:
1841 *type = calloc(1, sizeof(struct lysc_type_identityref));
1842 break;
1843 case LY_TYPE_INST:
1844 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1845 break;
1846 case LY_TYPE_LEAFREF:
1847 *type = calloc(1, sizeof(struct lysc_type_leafref));
1848 break;
1849 case LY_TYPE_STRING:
1850 *type = calloc(1, sizeof(struct lysc_type_str));
1851 break;
1852 case LY_TYPE_UNION:
1853 *type = calloc(1, sizeof(struct lysc_type_union));
1854 break;
1855 case LY_TYPE_INT8:
1856 case LY_TYPE_UINT8:
1857 case LY_TYPE_INT16:
1858 case LY_TYPE_UINT16:
1859 case LY_TYPE_INT32:
1860 case LY_TYPE_UINT32:
1861 case LY_TYPE_INT64:
1862 case LY_TYPE_UINT64:
1863 *type = calloc(1, sizeof(struct lysc_type_num));
1864 break;
1865 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001866 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001867 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1868 ret = LY_EVALID;
1869 goto cleanup;
1870 }
1871 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1872 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001873 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001874 ly_data_type2str[basetype]);
1875 free(*type);
1876 (*type) = NULL;
1877 ret = LY_EVALID;
1878 goto cleanup;
1879 }
1880
1881 /* get restrictions from the referred typedefs */
1882 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1883 tctx = (struct type_context *)tpdf_chain.objs[u];
1884
1885 /* remember the typedef context for circular check */
1886 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1887 LY_CHECK_GOTO(ret, cleanup);
1888
1889 if (tctx->tpdf->type.compiled) {
1890 base = tctx->tpdf->type.compiled;
1891 continue;
1892 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1893 /* no change, just use the type information from the base */
1894 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1895 ++base->refcount;
1896 continue;
1897 }
1898
1899 ++(*type)->refcount;
1900 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001901 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001902 tctx->tpdf->name, ly_data_type2str[basetype]);
1903 ret = LY_EVALID;
1904 goto cleanup;
1905 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001906 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001907 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1908 tctx->tpdf->name, tctx->tpdf->dflt.str);
1909 ret = LY_EVALID;
1910 goto cleanup;
1911 }
1912
1913 (*type)->basetype = basetype;
1914 /* TODO user type plugins */
1915 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1916 prev_type = *type;
1917 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name,
1918 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1919 LY_CHECK_GOTO(ret, cleanup);
1920 base = prev_type;
1921 }
1922 /* remove the processed typedef contexts from the stack for circular check */
1923 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1924
1925 /* process the type definition in leaf */
1926 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1927 /* get restrictions from the node itself */
1928 (*type)->basetype = basetype;
1929 /* TODO user type plugins */
1930 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1931 ++(*type)->refcount;
1932 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_mod, context_name, type_p, basetype, NULL,
1933 base, type);
1934 LY_CHECK_GOTO(ret, cleanup);
1935 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1936 /* no specific restriction in leaf's type definition, copy from the base */
1937 free(*type);
1938 (*type) = base;
1939 ++(*type)->refcount;
1940 }
1941
1942 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
1943
1944cleanup:
1945 ly_set_erase(&tpdf_chain, free);
1946 return ret;
1947}
1948
1949/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01001950 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
1951 */
1952#define LYS_STATUS_USES LYS_CONFIG_MASK
1953
1954/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001955 * @brief Compile status information of the given node.
1956 *
1957 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
1958 * has the status correctly set during the compilation.
1959 *
1960 * @param[in] ctx Compile context
1961 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
1962 * If the status was set explicitly on the node, it is already set in the flags value and we just check
1963 * the compatibility with the parent's status value.
1964 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
1965 * @return LY_ERR value.
1966 */
1967static LY_ERR
1968lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
1969{
1970 /* status - it is not inherited by specification, but it does not make sense to have
1971 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
1972 if (!((*node_flags) & LYS_STATUS_MASK)) {
1973 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001974 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001975 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01001976 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001977 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
1978 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1979 }
1980 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
1981 } else {
1982 (*node_flags) |= LYS_STATUS_CURR;
1983 }
1984 } else if (parent_flags & LYS_STATUS_MASK) {
1985 /* check status compatibility with the parent */
1986 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
1987 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001988 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001989 "A \"current\" status is in conflict with the parent's \"%s\" status.",
1990 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1991 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001992 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001993 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
1994 }
1995 return LY_EVALID;
1996 }
1997 }
1998 return LY_SUCCESS;
1999}
2000
2001/**
2002 * @brief Check uniqness of the node/action/notification name.
2003 *
2004 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2005 * structures, but they share the namespace so we need to check their name collisions.
2006 *
2007 * @param[in] ctx Compile context.
2008 * @param[in] parent Parent of the nodes to check, can be NULL.
2009 * @param[in] name Name of the item to find in the given lists.
2010 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2011 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2012 */
2013static LY_ERR
2014lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2015 const struct lysc_node *exclude)
2016{
2017 const struct lysc_node *iter, *iter2;
2018 const struct lysc_action *actions;
2019 const struct lysc_notif *notifs;
2020 uint32_t getnext_flags;
2021 LY_ARRAY_COUNT_TYPE u;
2022
2023#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2024
2025 if (exclude->nodetype == LYS_CASE) {
2026 /* check restricted only to all the cases */
2027 assert(parent->nodetype == LYS_CHOICE);
2028 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
2029 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002030 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002031 return LY_EEXIST;
2032 }
2033 }
2034
2035 return LY_SUCCESS;
2036 }
2037
2038 /* no reason for our parent to be choice anymore */
2039 assert(!parent || (parent->nodetype != LYS_CHOICE));
2040
2041 if (parent && (parent->nodetype == LYS_CASE)) {
2042 /* move to the first data definition parent */
2043 parent = lysc_data_parent(parent);
2044 }
2045
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002046 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002047 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
2048 getnext_flags |= LYS_GETNEXT_OUTPUT;
2049 }
2050
2051 iter = NULL;
2052 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2053 if (CHECK_NODE(iter, exclude, name)) {
2054 goto error;
2055 }
2056
2057 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2058 if (iter->nodetype == LYS_CHOICE) {
2059 iter2 = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002060 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002061 if (CHECK_NODE(iter2, exclude, name)) {
2062 goto error;
2063 }
2064 }
2065 }
2066 }
2067
2068 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2069 LY_ARRAY_FOR(actions, u) {
2070 if (CHECK_NODE(&actions[u], exclude, name)) {
2071 goto error;
2072 }
2073 }
2074
2075 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2076 LY_ARRAY_FOR(notifs, u) {
2077 if (CHECK_NODE(&notifs[u], exclude, name)) {
2078 goto error;
2079 }
2080 }
2081 return LY_SUCCESS;
2082
2083error:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002084 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002085 return LY_EEXIST;
2086
2087#undef CHECK_NODE
2088}
2089
2090LY_ERR
2091lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, struct lysc_node *parent,
2092 struct lysc_action *action, uint16_t uses_status)
2093{
2094 LY_ERR ret = LY_SUCCESS;
2095 struct lysp_node *child_p, *dev_pnode = NULL, *dev_input_p = NULL, *dev_output_p = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002096 struct lysp_action_inout *inout_p;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002097 ly_bool not_supported, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002098 uint32_t opt_prev = ctx->options;
2099
2100 lysc_update_path(ctx, parent, action_p->name);
2101
2102 /* apply deviation on the action/RPC */
2103 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)action_p, parent, &dev_pnode, &not_supported));
2104 if (not_supported) {
2105 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002106 ret = LY_EDENIED;
2107 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002108 } else if (dev_pnode) {
2109 action_p = (struct lysp_action *)dev_pnode;
2110 }
2111
2112 /* member needed for uniqueness check lys_getnext() */
2113 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
2114 action->module = ctx->cur_mod;
2115 action->parent = parent;
2116
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002117 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 +02002118
2119 if (ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002120 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002121 "Action \"%s\" is placed inside %s.", action_p->name,
2122 ctx->options & LYS_COMPILE_RPC_MASK ? "another RPC/action" : "notification");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002123 ret = LY_EVALID;
2124 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002125 }
2126
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002127 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
2128
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002129 /* if-features */
2130 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, action_p->iffeatures, &enabled));
Michal Vasko12606ee2020-11-25 17:05:11 +01002131 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002132 ly_set_add(&ctx->disabled, action, 1, NULL);
2133 ctx->options |= LYS_COMPILE_DISABLED;
2134 }
2135
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002136 /* status - it is not inherited by specification, but it does not make sense to have
2137 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002138 ret = lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
2139 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002140
2141 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
2142 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
2143 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002144
2145 /* connect any action augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002146 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)action), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002147
2148 /* input */
2149 lysc_update_path(ctx, (struct lysc_node *)action, "input");
2150
2151 /* apply deviations on input */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002152 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->input,
2153 (struct lysc_node *)action, &dev_input_p, &not_supported), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002154 if (not_supported) {
2155 inout_p = NULL;
2156 } else if (dev_input_p) {
2157 inout_p = (struct lysp_action_inout *)dev_input_p;
2158 } else {
2159 inout_p = &action_p->input;
2160 }
2161
2162 if (inout_p) {
2163 action->input.nodetype = LYS_INPUT;
Michal Vasko5347e3a2020-11-03 17:14:57 +01002164 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->input.musts, lys_compile_must, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002165 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
2166 ctx->options |= LYS_COMPILE_RPC_INPUT;
2167
2168 /* connect any input augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002169 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)&action->input), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002170
2171 LY_LIST_FOR(inout_p->data, child_p) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002172 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 +02002173 }
2174 ctx->options = opt_prev;
2175 }
2176
2177 lysc_update_path(ctx, NULL, NULL);
2178
2179 /* output */
2180 lysc_update_path(ctx, (struct lysc_node *)action, "output");
2181
2182 /* apply deviations on output */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002183 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->output,
2184 (struct lysc_node *)action, &dev_output_p, &not_supported), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002185 if (not_supported) {
2186 inout_p = NULL;
2187 } else if (dev_output_p) {
2188 inout_p = (struct lysp_action_inout *)dev_output_p;
2189 } else {
2190 inout_p = &action_p->output;
2191 }
2192
2193 if (inout_p) {
2194 action->output.nodetype = LYS_OUTPUT;
Michal Vasko5347e3a2020-11-03 17:14:57 +01002195 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->output.musts, lys_compile_must, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002196 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
2197 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
2198
2199 /* connect any output augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002200 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)&action->output), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002201
2202 LY_LIST_FOR(inout_p->data, child_p) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002203 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 +02002204 }
2205 ctx->options = opt_prev;
2206 }
2207
2208 lysc_update_path(ctx, NULL, NULL);
2209
Michal Vasko208a04a2020-10-21 15:17:12 +02002210 /* wait with extensions compilation until all the children are compiled */
2211 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
2212
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002213 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002214 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002215 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002216 LY_CHECK_GOTO(ret, cleanup);
2217 }
2218
2219 lysc_update_path(ctx, NULL, NULL);
2220
2221cleanup:
2222 lysp_dev_node_free(ctx->ctx, dev_pnode);
2223 lysp_dev_node_free(ctx->ctx, dev_input_p);
2224 lysp_dev_node_free(ctx->ctx, dev_output_p);
2225 ctx->options = opt_prev;
2226 return ret;
2227}
2228
2229LY_ERR
2230lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, struct lysc_node *parent, struct lysc_notif *notif,
2231 uint16_t uses_status)
2232{
2233 LY_ERR ret = LY_SUCCESS;
2234 struct lysp_node *child_p, *dev_pnode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002235 ly_bool not_supported, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002236 uint32_t opt_prev = ctx->options;
2237
2238 lysc_update_path(ctx, parent, notif_p->name);
2239
2240 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)notif_p, parent, &dev_pnode, &not_supported));
2241 if (not_supported) {
2242 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002243 ret = LY_EDENIED;
2244 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002245 } else if (dev_pnode) {
2246 notif_p = (struct lysp_notif *)dev_pnode;
2247 }
2248
2249 /* member needed for uniqueness check lys_getnext() */
2250 notif->nodetype = LYS_NOTIF;
2251 notif->module = ctx->cur_mod;
2252 notif->parent = parent;
2253
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002254 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 +02002255
2256 if (ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002257 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002258 "Notification \"%s\" is placed inside %s.", notif_p->name,
2259 ctx->options & LYS_COMPILE_RPC_MASK ? "RPC/action" : "another notification");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002260 ret = LY_EVALID;
2261 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002262 }
2263
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002264 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
2265
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002266 /* if-features */
2267 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, notif_p->iffeatures, &enabled));
Michal Vasko12606ee2020-11-25 17:05:11 +01002268 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002269 ly_set_add(&ctx->disabled, notif, 1, NULL);
2270 ctx->options |= LYS_COMPILE_DISABLED;
2271 }
2272
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002273 /* status - it is not inherited by specification, but it does not make sense to have
2274 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2275 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
2276 LY_CHECK_GOTO(ret, cleanup);
2277
2278 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
2279 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
2280 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Michal Vasko5347e3a2020-11-03 17:14:57 +01002281 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002282 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002283 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002284 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002285 LY_CHECK_GOTO(ret, cleanup);
2286 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002287
2288 ctx->options |= LYS_COMPILE_NOTIFICATION;
2289
2290 /* connect any notification augments */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002291 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, (struct lysc_node *)notif), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002292
2293 LY_LIST_FOR(notif_p->data, child_p) {
2294 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status, NULL);
2295 LY_CHECK_GOTO(ret, cleanup);
2296 }
2297
Michal Vasko208a04a2020-10-21 15:17:12 +02002298 /* wait with extension compilation until all the children are compiled */
2299 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
2300
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002301 lysc_update_path(ctx, NULL, NULL);
2302
2303cleanup:
2304 lysp_dev_node_free(ctx->ctx, dev_pnode);
2305 ctx->options = opt_prev;
2306 return ret;
2307}
2308
2309/**
2310 * @brief Compile parsed container node information.
2311 * @param[in] ctx Compile context
2312 * @param[in] pnode Parsed container node.
2313 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2314 * is enriched with the container-specific information.
2315 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2316 */
2317static LY_ERR
2318lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2319{
2320 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2321 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2322 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002323 LY_ERR ret = LY_SUCCESS;
2324
2325 if (cont_p->presence) {
2326 /* explicit presence */
2327 cont->flags |= LYS_PRESENCE;
2328 } else if (cont_p->musts) {
2329 /* container with a must condition */
2330 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
2331 cont->flags |= LYS_PRESENCE;
2332 } else if (cont_p->when) {
2333 /* container with a when condition */
2334 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"when\" condition.", cont_p->name);
2335 cont->flags |= LYS_PRESENCE;
2336 } else if (cont_p->parent) {
2337 if (cont_p->parent->nodetype == LYS_CHOICE) {
2338 /* container is an implicit case, so its existence decides the existence of the whole case */
2339 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
2340 cont_p->name, cont_p->parent->name);
2341 cont->flags |= LYS_PRESENCE;
2342 } else if ((cont_p->parent->nodetype == LYS_CASE) &&
2343 (((struct lysp_node_case *)cont_p->parent)->child == pnode) && !cont_p->next) {
2344 /* container is the only node in a case, so its existence decides the existence of the whole case */
2345 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
2346 cont_p->name, cont_p->parent->name);
2347 cont->flags |= LYS_PRESENCE;
2348 }
2349 }
2350
2351 /* more cases when the container has meaning but is kept NP for convenience:
2352 * - when condition
2353 * - direct child action/notification
2354 */
2355
2356 LY_LIST_FOR(cont_p->child, child_p) {
2357 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2358 LY_CHECK_GOTO(ret, done);
2359 }
2360
Michal Vasko5347e3a2020-11-03 17:14:57 +01002361 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002362 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002363 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002364 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002365 LY_CHECK_GOTO(ret, done);
2366 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01002367 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, node, lys_compile_action, 0, ret, done);
2368 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, node, lys_compile_notif, 0, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002369
2370done:
2371 return ret;
2372}
2373
2374/*
2375 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2376 * @param[in] ctx Compile context.
2377 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2378 * @param[in] type_p Parsed type to compile.
2379 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2380 * @return LY_ERR value.
2381 */
2382static LY_ERR
2383lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2384 struct lysc_node_leaf *leaf)
2385{
2386 struct lysp_qname *dflt;
2387
2388 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->pmod, leaf->name, type_p, &leaf->type,
2389 leaf->units ? NULL : &leaf->units, &dflt));
2390
2391 /* store default value, if any */
2392 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2393 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2394 }
2395
Michal Vasko12606ee2020-11-25 17:05:11 +01002396 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002397 /* 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 +01002398 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002399 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002400 LY_ARRAY_COUNT_TYPE u;
2401 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2402 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2403 /* 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 +01002404 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002405 }
2406 }
2407 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2408 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002409 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002410 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2411 return LY_EVALID;
2412 }
2413 }
2414
2415 return LY_SUCCESS;
2416}
2417
2418/**
2419 * @brief Compile parsed leaf node information.
2420 * @param[in] ctx Compile context
2421 * @param[in] pnode Parsed leaf node.
2422 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2423 * is enriched with the leaf-specific information.
2424 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2425 */
2426static LY_ERR
2427lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2428{
2429 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2430 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002431 LY_ERR ret = LY_SUCCESS;
2432
Michal Vasko5347e3a2020-11-03 17:14:57 +01002433 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002434 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002435 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002436 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002437 LY_CHECK_GOTO(ret, done);
2438 }
2439 if (leaf_p->units) {
2440 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2441 leaf->flags |= LYS_SET_UNITS;
2442 }
2443
2444 /* compile type */
2445 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2446 LY_CHECK_GOTO(ret, done);
2447
2448 /* store/update default value */
2449 if (leaf_p->dflt.str) {
2450 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2451 leaf->flags |= LYS_SET_DFLT;
2452 }
2453
2454 /* checks */
2455 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002456 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002457 "Invalid mandatory leaf with a default value.");
2458 return LY_EVALID;
2459 }
2460
2461done:
2462 return ret;
2463}
2464
2465/**
2466 * @brief Compile parsed leaf-list node information.
2467 * @param[in] ctx Compile context
2468 * @param[in] pnode Parsed leaf-list node.
2469 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2470 * is enriched with the leaf-list-specific information.
2471 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2472 */
2473static LY_ERR
2474lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2475{
2476 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2477 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002478 LY_ERR ret = LY_SUCCESS;
2479
Michal Vasko5347e3a2020-11-03 17:14:57 +01002480 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002481 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002482 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002483 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002484 LY_CHECK_GOTO(ret, done);
2485 }
2486 if (llist_p->units) {
2487 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2488 llist->flags |= LYS_SET_UNITS;
2489 }
2490
2491 /* compile type */
2492 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2493 LY_CHECK_GOTO(ret, done);
2494
2495 /* store/update default values */
2496 if (llist_p->dflts) {
2497 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002498 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002499 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2500 return LY_EVALID;
2501 }
2502
2503 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2504 llist->flags |= LYS_SET_DFLT;
2505 }
2506
2507 llist->min = llist_p->min;
2508 if (llist->min) {
2509 llist->flags |= LYS_MAND_TRUE;
2510 }
2511 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2512
2513 /* checks */
2514 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002515 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "The default statement is present on leaf-list with a nonzero min-elements.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002516 return LY_EVALID;
2517 }
2518
2519 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002520 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002521 llist->min, llist->max);
2522 return LY_EVALID;
2523 }
2524
2525done:
2526 return ret;
2527}
2528
2529LY_ERR
2530lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2531 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2532 const struct lysc_node **target, uint16_t *result_flag)
2533{
2534 LY_ERR ret = LY_EVALID;
2535 const char *name, *prefix, *id;
2536 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002537 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002538 const char *nodeid_type;
2539 uint32_t getnext_extra_flag = 0;
2540 uint16_t current_nodetype = 0;
2541
2542 assert(nodeid);
2543 assert(target);
2544 assert(result_flag);
2545 *target = NULL;
2546 *result_flag = 0;
2547
2548 id = nodeid;
2549
2550 if (ctx_node) {
2551 /* descendant-schema-nodeid */
2552 nodeid_type = "descendant";
2553
2554 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002555 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002556 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
2557 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2558 return LY_EVALID;
2559 }
2560 } else {
2561 /* absolute-schema-nodeid */
2562 nodeid_type = "absolute";
2563
2564 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002565 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002566 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
2567 nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2568 return LY_EVALID;
2569 }
2570 ++id;
2571 }
2572
2573 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2574 if (prefix) {
2575 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2576 if (!mod) {
2577 /* module must always be found */
2578 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002579 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002580 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
2581 nodeid_type, id - nodeid, nodeid, prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
2582 return LY_ENOTFOUND;
2583 }
2584 } else {
2585 switch (format) {
2586 case LY_PREF_SCHEMA:
2587 case LY_PREF_SCHEMA_RESOLVED:
2588 /* use the current module */
2589 mod = cur_mod;
2590 break;
2591 case LY_PREF_JSON:
2592 if (!ctx_node) {
2593 LOGINT_RET(ctx->ctx);
2594 }
2595 /* inherit the module of the previous context node */
2596 mod = ctx_node->module;
2597 break;
2598 case LY_PREF_XML:
2599 /* not really defined */
2600 LOGINT_RET(ctx->ctx);
2601 }
2602 }
2603
2604 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2605 /* move through input/output manually */
2606 if (mod != ctx_node->module) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002607 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002608 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
2609 return LY_ENOTFOUND;
2610 }
2611 if (!ly_strncmp("input", name, name_len)) {
2612 ctx_node = (struct lysc_node *)&((struct lysc_action *)ctx_node)->input;
2613 } else if (!ly_strncmp("output", name, name_len)) {
2614 ctx_node = (struct lysc_node *)&((struct lysc_action *)ctx_node)->output;
2615 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2616 } else {
2617 /* only input or output is valid */
2618 ctx_node = NULL;
2619 }
2620 } else {
2621 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002622 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002623 getnext_extra_flag = 0;
2624 }
2625 if (!ctx_node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002626 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002627 "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", nodeid_type, id - nodeid, nodeid);
2628 return LY_ENOTFOUND;
2629 }
2630 current_nodetype = ctx_node->nodetype;
2631
2632 if (current_nodetype == LYS_NOTIF) {
2633 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2634 } else if (current_nodetype == LYS_INPUT) {
2635 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2636 } else if (current_nodetype == LYS_OUTPUT) {
2637 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2638 }
2639
2640 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2641 break;
2642 }
2643 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002644 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002645 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
2646 nodeid_type, id - nodeid + 1, nodeid);
2647 return LY_EVALID;
2648 }
2649 ++id;
2650 }
2651
2652 if (ret == LY_SUCCESS) {
2653 *target = ctx_node;
2654 if (nodetype && !(current_nodetype & nodetype)) {
2655 return LY_EDENIED;
2656 }
2657 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002658 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002659 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
2660 nodeid_type, nodeid_len ? nodeid_len : strlen(nodeid), nodeid);
2661 }
2662
2663 return ret;
2664}
2665
2666/**
2667 * @brief Compile information about list's uniques.
2668 * @param[in] ctx Compile context.
2669 * @param[in] uniques Sized array list of unique statements.
2670 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2671 * @return LY_ERR value.
2672 */
2673static LY_ERR
2674lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2675{
2676 LY_ERR ret = LY_SUCCESS;
2677 struct lysc_node_leaf **key, ***unique;
2678 struct lysc_node *parent;
2679 const char *keystr, *delim;
2680 size_t len;
2681 LY_ARRAY_COUNT_TYPE v;
2682 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2683 uint16_t flags;
2684
2685 LY_ARRAY_FOR(uniques, v) {
2686 config = -1;
2687 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2688 keystr = uniques[v].str;
2689 while (keystr) {
2690 delim = strpbrk(keystr, " \t\n");
2691 if (delim) {
2692 len = delim - keystr;
2693 while (isspace(*delim)) {
2694 ++delim;
2695 }
2696 } else {
2697 len = strlen(keystr);
2698 }
2699
2700 /* unique node must be present */
2701 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
2702 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, uniques[v].mod->mod,
2703 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2704 if (ret != LY_SUCCESS) {
2705 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002706 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002707 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
2708 len, keystr, lys_nodetype2str((*key)->nodetype));
2709 }
2710 return LY_EVALID;
2711 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002712 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002713 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
2714 len, keystr, flags & LYS_COMPILE_NOTIFICATION ? "notification" : "RPC/action");
2715 return LY_EVALID;
2716 }
2717
2718 /* all referenced leafs must be of the same config type */
2719 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2720 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002721 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002722 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2723 return LY_EVALID;
2724 } else if ((*key)->flags & LYS_CONFIG_W) {
2725 config = 1;
2726 } else { /* LYS_CONFIG_R */
2727 config = 0;
2728 }
2729
2730 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
2731 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
2732 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002733 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002734 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
2735 return LY_EVALID;
2736 }
2737 }
2738
2739 /* check status */
2740 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2741 (*key)->flags, (*key)->module, (*key)->name));
2742
2743 /* mark leaf as unique */
2744 (*key)->flags |= LYS_UNIQUE;
2745
2746 /* next unique value in line */
2747 keystr = delim;
2748 }
2749 /* next unique definition */
2750 }
2751
2752 return LY_SUCCESS;
2753}
2754
2755/**
2756 * @brief Compile parsed list node information.
2757 * @param[in] ctx Compile context
2758 * @param[in] pnode Parsed list node.
2759 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2760 * is enriched with the list-specific information.
2761 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2762 */
2763static LY_ERR
2764lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2765{
2766 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
2767 struct lysc_node_list *list = (struct lysc_node_list *)node;
2768 struct lysp_node *child_p;
2769 struct lysc_node_leaf *key, *prev_key = NULL;
2770 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002771 const char *keystr, *delim;
2772 LY_ERR ret = LY_SUCCESS;
2773
2774 list->min = list_p->min;
2775 if (list->min) {
2776 list->flags |= LYS_MAND_TRUE;
2777 }
2778 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2779
2780 LY_LIST_FOR(list_p->child, child_p) {
2781 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
2782 }
2783
Michal Vasko5347e3a2020-11-03 17:14:57 +01002784 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002785 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002786 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002787 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002788 }
2789
2790 /* keys */
2791 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002792 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002793 return LY_EVALID;
2794 }
2795
2796 /* find all the keys (must be direct children) */
2797 keystr = list_p->key;
2798 if (!keystr) {
2799 /* keyless list */
2800 list->flags |= LYS_KEYLESS;
2801 }
2802 while (keystr) {
2803 delim = strpbrk(keystr, " \t\n");
2804 if (delim) {
2805 len = delim - keystr;
2806 while (isspace(*delim)) {
2807 ++delim;
2808 }
2809 } else {
2810 len = strlen(keystr);
2811 }
2812
2813 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002814 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
2815 if (!key) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002816 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002817 return LY_EVALID;
2818 }
2819 /* keys must be unique */
2820 if (key->flags & LYS_KEY) {
2821 /* the node was already marked as a key */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002822 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002823 return LY_EVALID;
2824 }
2825
2826 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
2827 /* key must have the same config flag as the list itself */
2828 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002829 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002830 return LY_EVALID;
2831 }
2832 if (ctx->pmod->version < LYS_VERSION_1_1) {
2833 /* YANG 1.0 denies key to be of empty type */
2834 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002835 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002836 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
2837 return LY_EVALID;
2838 }
2839 } else {
2840 /* when and if-feature are illegal on list keys */
2841 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002842 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002843 "List's key must not have any \"when\" statement.");
2844 return LY_EVALID;
2845 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002846 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002847 }
2848
2849 /* check status */
2850 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2851 key->flags, key->module, key->name));
2852
2853 /* ignore default values of the key */
2854 if (key->dflt) {
2855 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
2856 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
2857 free(key->dflt);
2858 key->dflt = NULL;
2859 }
2860 /* mark leaf as key */
2861 key->flags |= LYS_KEY;
2862
2863 /* move it to the correct position */
2864 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
2865 /* fix links in closest previous siblings of the key */
2866 if (key->next) {
2867 key->next->prev = key->prev;
2868 } else {
2869 /* last child */
2870 list->child->prev = key->prev;
2871 }
2872 if (key->prev->next) {
2873 key->prev->next = key->next;
2874 }
2875 /* fix links in the key */
2876 if (prev_key) {
2877 key->prev = (struct lysc_node *)prev_key;
2878 key->next = prev_key->next;
2879 } else {
2880 key->prev = list->child->prev;
2881 key->next = list->child;
2882 }
2883 /* fix links in closes future siblings of the key */
2884 if (prev_key) {
2885 if (prev_key->next) {
2886 prev_key->next->prev = (struct lysc_node *)key;
2887 } else {
2888 list->child->prev = (struct lysc_node *)key;
2889 }
2890 prev_key->next = (struct lysc_node *)key;
2891 } else {
2892 list->child->prev = (struct lysc_node *)key;
2893 }
2894 /* fix links in parent */
2895 if (!key->prev->next) {
2896 list->child = (struct lysc_node *)key;
2897 }
2898 }
2899
2900 /* next key value */
2901 prev_key = key;
2902 keystr = delim;
2903 lysc_update_path(ctx, NULL, NULL);
2904 }
2905
2906 /* uniques */
2907 if (list_p->uniques) {
2908 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
2909 }
2910
Michal Vasko5347e3a2020-11-03 17:14:57 +01002911 COMPILE_OP_ARRAY_GOTO(ctx, list_p->actions, list->actions, node, lys_compile_action, 0, ret, done);
2912 COMPILE_OP_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, node, lys_compile_notif, 0, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002913
2914 /* checks */
2915 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002916 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002917 list->min, list->max);
2918 return LY_EVALID;
2919 }
2920
2921done:
2922 return ret;
2923}
2924
2925/**
2926 * @brief Do some checks and set the default choice's case.
2927 *
2928 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
2929 *
2930 * @param[in] ctx Compile context.
2931 * @param[in] dflt Name of the default branch. Can even contain a prefix.
2932 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
2933 * @return LY_ERR value.
2934 */
2935static LY_ERR
2936lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
2937{
2938 struct lysc_node *iter, *node = (struct lysc_node *)ch;
2939 const struct lys_module *mod;
2940 const char *prefix = NULL, *name;
2941 size_t prefix_len = 0;
2942
2943 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
2944 name = strchr(dflt->str, ':');
2945 if (name) {
2946 prefix = dflt->str;
2947 prefix_len = name - prefix;
2948 ++name;
2949 } else {
2950 name = dflt->str;
2951 }
2952 if (prefix) {
2953 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
2954 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002955 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002956 "in imports of \"%s\".", prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
2957 return LY_EVALID;
2958 }
2959 } else {
2960 mod = node->module;
2961 }
2962
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002963 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 +02002964 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002965 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002966 "Default case \"%s\" not found.", dflt->str);
2967 return LY_EVALID;
2968 }
2969
2970 /* no mandatory nodes directly under the default case */
2971 LY_LIST_FOR(ch->dflt->child, iter) {
2972 if (iter->parent != (struct lysc_node *)ch->dflt) {
2973 break;
2974 }
2975 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002976 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002977 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
2978 return LY_EVALID;
2979 }
2980 }
2981
2982 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002983 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002984 return LY_EVALID;
2985 }
2986
2987 ch->dflt->flags |= LYS_SET_DFLT;
2988 return LY_SUCCESS;
2989}
2990
2991LY_ERR
2992lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
2993 struct ly_set *child_set)
2994{
2995 LY_ERR ret = LY_SUCCESS;
2996 struct lysp_node *child_p_next = child_p->next;
2997 struct lysp_node_case *cs_p;
2998
2999 if (child_p->nodetype == LYS_CASE) {
3000 /* standard case under choice */
3001 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3002 } else {
3003 /* we need the implicit case first, so create a fake parsed case */
3004 cs_p = calloc(1, sizeof *cs_p);
3005 cs_p->nodetype = LYS_CASE;
3006 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3007 cs_p->child = child_p;
3008
3009 /* make the child the only case child */
3010 child_p->next = NULL;
3011
3012 /* compile it normally */
3013 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3014
3015free_fake_node:
3016 /* free the fake parsed node and correct pointers back */
3017 cs_p->child = NULL;
3018 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3019 child_p->next = child_p_next;
3020 }
3021
3022 return ret;
3023}
3024
3025/**
3026 * @brief Compile parsed choice node information.
3027 *
3028 * @param[in] ctx Compile context
3029 * @param[in] pnode Parsed choice node.
3030 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3031 * is enriched with the choice-specific information.
3032 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3033 */
3034static LY_ERR
3035lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3036{
3037 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3038 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3039 struct lysp_node *child_p;
3040 LY_ERR ret = LY_SUCCESS;
3041
3042 assert(node->nodetype == LYS_CHOICE);
3043
3044 LY_LIST_FOR(ch_p->child, child_p) {
3045 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3046 }
3047
3048 /* default branch */
3049 if (ch_p->dflt.str) {
3050 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3051 }
3052
3053 return ret;
3054}
3055
3056/**
3057 * @brief Compile parsed anydata or anyxml node information.
3058 * @param[in] ctx Compile context
3059 * @param[in] pnode Parsed anydata or anyxml node.
3060 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3061 * is enriched with the any-specific information.
3062 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3063 */
3064static LY_ERR
3065lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3066{
3067 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3068 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003069 LY_ERR ret = LY_SUCCESS;
3070
Michal Vasko5347e3a2020-11-03 17:14:57 +01003071 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003072 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003073 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003074 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003075 LY_CHECK_GOTO(ret, done);
3076 }
3077
Michal Vasko75620aa2020-10-21 09:25:51 +02003078 if (!(ctx->options & (LYS_COMPILE_RPC_MASK | LYS_COMPILE_NOTIFICATION)) && (any->flags & LYS_CONFIG_W)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003079 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3080 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3081 }
3082done:
3083 return ret;
3084}
3085
3086/**
3087 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
3088 * keep specific order of augments targetting the same node.
3089 *
3090 * @param[in] ctx Compile context
3091 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3092 * the choice itself is expected instead of a specific case node.
3093 * @param[in] node Schema node to connect into the list.
3094 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3095 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
3096 */
3097static LY_ERR
3098lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
3099{
3100 struct lysc_node **children, *anchor = NULL;
3101 int insert_after = 0;
3102
3103 node->parent = parent;
3104
3105 if (parent) {
3106 if (parent->nodetype == LYS_CHOICE) {
3107 assert(node->nodetype == LYS_CASE);
3108 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
3109 } else {
3110 children = lysc_node_children_p(parent, ctx->options);
3111 }
3112 assert(children);
3113
3114 if (!(*children)) {
3115 /* first child */
3116 *children = node;
3117 } else if (node->flags & LYS_KEY) {
3118 /* special handling of adding keys */
3119 assert(node->module == parent->module);
3120 anchor = *children;
3121 if (anchor->flags & LYS_KEY) {
3122 while ((anchor->flags & LYS_KEY) && anchor->next) {
3123 anchor = anchor->next;
3124 }
3125 /* insert after the last key */
3126 insert_after = 1;
3127 } /* else insert before anchor (at the beginning) */
3128 } else if ((*children)->prev->module == node->module) {
3129 /* last child is from the same module, keep the order and insert at the end */
3130 anchor = (*children)->prev;
3131 insert_after = 1;
3132 } else if (parent->module == node->module) {
3133 /* adding module child after some augments were connected */
3134 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
3135 } else {
3136 /* some augments are already connected and we are connecting new ones,
3137 * keep module name order and insert the node into the children list */
3138 anchor = *children;
3139 do {
3140 anchor = anchor->prev;
3141
3142 /* check that we have not found the last augment node from our module or
3143 * the first augment node from a "smaller" module or
3144 * the first node from a local module */
3145 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
3146 (anchor->module == parent->module)) {
3147 /* insert after */
3148 insert_after = 1;
3149 break;
3150 }
3151
3152 /* we have traversed all the nodes, insert before anchor (as the first node) */
3153 } while (anchor->prev->next);
3154 }
3155
3156 /* insert */
3157 if (anchor) {
3158 if (insert_after) {
3159 node->next = anchor->next;
3160 node->prev = anchor;
3161 anchor->next = node;
3162 if (node->next) {
3163 /* middle node */
3164 node->next->prev = node;
3165 } else {
3166 /* last node */
3167 (*children)->prev = node;
3168 }
3169 } else {
3170 node->next = anchor;
3171 node->prev = anchor->prev;
3172 anchor->prev = node;
3173 if (anchor == *children) {
3174 /* first node */
3175 *children = node;
3176 } else {
3177 /* middle node */
3178 node->prev->next = node;
3179 }
3180 }
3181 }
3182
3183 /* check the name uniqueness (even for an only child, it may be in case) */
3184 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
3185 return LY_EEXIST;
3186 }
3187 } else {
3188 /* top-level element */
3189 if (!ctx->cur_mod->compiled->data) {
3190 ctx->cur_mod->compiled->data = node;
3191 } else {
3192 /* insert at the end of the module's top-level nodes list */
3193 ctx->cur_mod->compiled->data->prev->next = node;
3194 node->prev = ctx->cur_mod->compiled->data->prev;
3195 ctx->cur_mod->compiled->data->prev = node;
3196 }
3197
3198 /* check the name uniqueness on top-level */
3199 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
3200 return LY_EEXIST;
3201 }
3202 }
3203
3204 return LY_SUCCESS;
3205}
3206
3207/**
3208 * @brief Prepare the case structure in choice node for the new data node.
3209 *
3210 * 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
3211 * created in the choice when the first child was processed.
3212 *
3213 * @param[in] ctx Compile context.
3214 * @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,
3215 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3216 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3217 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3218 * @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,
3219 * it is linked from the case structure only in case it is its first child.
3220 */
3221static LY_ERR
3222lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3223{
3224 struct lysp_node *child_p;
3225 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3226
3227 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3228 /* we have to add an implicit case node into the parent choice */
3229 } else if (pnode->nodetype == LYS_CASE) {
3230 /* explicit parent case */
3231 LY_LIST_FOR(cs_p->child, child_p) {
3232 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3233 }
3234 } else {
3235 LOGINT_RET(ctx->ctx);
3236 }
3237
3238 return LY_SUCCESS;
3239}
3240
3241void
3242lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3243{
3244 struct lysc_node *iter;
3245
3246 if (add) { /* set flag */
3247 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3248 parent = parent->parent) {
3249 parent->flags |= LYS_MAND_TRUE;
3250 }
3251 } else { /* unset flag */
3252 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
3253 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
3254 if (iter->flags & LYS_MAND_TRUE) {
3255 /* there is another mandatory node */
3256 return;
3257 }
3258 }
3259 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3260 parent->flags &= ~LYS_MAND_TRUE;
3261 }
3262 }
3263}
3264
3265/**
3266 * @brief Find grouping for a uses.
3267 *
3268 * @param[in] ctx Compile context.
3269 * @param[in] uses_p Parsed uses node.
3270 * @param[out] gpr_p Found grouping on success.
3271 * @param[out] grp_pmod Module of @p grp_p on success.
3272 * @return LY_ERR value.
3273 */
3274static LY_ERR
3275lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
3276 struct lysp_module **grp_pmod)
3277{
3278 struct lysp_node *pnode;
3279 struct lysp_grp *grp;
3280 LY_ARRAY_COUNT_TYPE u, v;
3281 const char *id, *name, *prefix, *local_pref;
3282 size_t prefix_len, name_len;
3283 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003284 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003285
3286 *grp_p = NULL;
3287 *grp_pmod = NULL;
3288
3289 /* search for the grouping definition */
3290 id = uses_p->name;
3291 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3292 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3293 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3294 /* current module, search local groupings first */
3295 pmod = ctx->pmod;
3296 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
3297 grp = (struct lysp_grp *)lysp_node_groupings(pnode);
3298 LY_ARRAY_FOR(grp, u) {
3299 if (!strcmp(grp[u].name, name)) {
3300 grp = &grp[u];
3301 found = ctx->pmod;
3302 break;
3303 }
3304 }
3305 }
3306 } else {
3307 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003308 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003309 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003310 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003311 "Invalid prefix used for grouping reference.", uses_p->name);
3312 return LY_EVALID;
3313 }
3314 pmod = mod->parsed;
3315 }
3316
3317 if (!found) {
3318 /* search in top-level groupings of the main module ... */
3319 grp = pmod->groupings;
3320 LY_ARRAY_FOR(grp, u) {
3321 if (!strcmp(grp[u].name, name)) {
3322 grp = &grp[u];
3323 found = pmod;
3324 break;
3325 }
3326 }
3327 if (!found) {
3328 /* ... and all the submodules */
3329 LY_ARRAY_FOR(pmod->includes, u) {
3330 grp = pmod->includes[u].submodule->groupings;
3331 LY_ARRAY_FOR(grp, v) {
3332 if (!strcmp(grp[v].name, name)) {
3333 grp = &grp[v];
3334 found = (struct lysp_module *)pmod->includes[u].submodule;
3335 break;
3336 }
3337 }
3338 if (found) {
3339 break;
3340 }
3341 }
3342 }
3343 }
3344 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003345 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003346 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3347 return LY_EVALID;
3348 }
3349
3350 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3351 /* remember that the grouping is instantiated to avoid its standalone validation */
3352 grp->flags |= LYS_USED_GRP;
3353 }
3354
3355 *grp_p = grp;
3356 *grp_pmod = found;
3357 return LY_SUCCESS;
3358}
3359
3360/**
3361 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3362 * If present, also apply uses's modificators.
3363 *
3364 * @param[in] ctx Compile context
3365 * @param[in] uses_p Parsed uses schema node.
3366 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3367 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3368 * the compile context.
3369 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3370 */
3371static LY_ERR
3372lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3373{
3374 struct lysp_node *pnode;
3375 struct lysc_node *child;
3376 struct lysp_grp *grp = NULL;
3377 uint32_t i, grp_stack_count;
3378 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3379 LY_ERR ret = LY_SUCCESS;
3380 struct lysc_when *when_shared = NULL;
3381 LY_ARRAY_COUNT_TYPE u;
3382 struct lysc_notif **notifs = NULL;
3383 struct lysc_action **actions = NULL;
3384 struct ly_set uses_child_set = {0};
3385
3386 /* find the referenced grouping */
3387 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3388
3389 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3390 grp_stack_count = ctx->groupings.count;
3391 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3392 if (grp_stack_count == ctx->groupings.count) {
3393 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003394 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003395 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3396 return LY_EVALID;
3397 }
3398
3399 /* check status */
3400 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3401 LY_CHECK_GOTO(ret, cleanup);
3402
3403 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3404 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3405 LY_CHECK_GOTO(ret, cleanup);
3406
3407 /* switch context's parsed module being processed */
3408 ctx->pmod = grp_mod;
3409
3410 /* compile data nodes */
3411 LY_LIST_FOR(grp->data, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003412 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3413 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003414 LY_CHECK_GOTO(ret, cleanup);
3415 }
3416
3417 if (child_set) {
3418 /* add these children to our compiled child_set as well since uses is a schema-only node */
3419 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3420 }
3421
3422 if (uses_p->when) {
3423 /* pass uses's when to all the data children */
3424 for (i = 0; i < uses_child_set.count; ++i) {
3425 child = uses_child_set.snodes[i];
3426
3427 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent), child, &when_shared);
3428 LY_CHECK_GOTO(ret, cleanup);
3429 }
3430 }
3431
3432 /* compile actions */
3433 if (grp->actions) {
3434 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3435 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003436 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003437 grp->actions[0].name, lys_nodetype2str(grp->actions[0].nodetype), parent->name,
3438 lys_nodetype2str(parent->nodetype));
3439 ret = LY_EVALID;
3440 goto cleanup;
3441 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01003442 COMPILE_OP_ARRAY_GOTO(ctx, grp->actions, *actions, parent, lys_compile_action, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003443
3444 if (uses_p->when) {
3445 /* inherit when */
3446 LY_ARRAY_FOR(*actions, u) {
3447 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
3448 (struct lysc_node *)&(*actions)[u], &when_shared);
3449 LY_CHECK_GOTO(ret, cleanup);
3450 }
3451 }
3452 }
3453
3454 /* compile notifications */
3455 if (grp->notifs) {
3456 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3457 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003458 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003459 grp->notifs[0].name, lys_nodetype2str(grp->notifs[0].nodetype), parent->name,
3460 lys_nodetype2str(parent->nodetype));
3461 ret = LY_EVALID;
3462 goto cleanup;
3463 }
Michal Vasko5347e3a2020-11-03 17:14:57 +01003464 COMPILE_OP_ARRAY_GOTO(ctx, grp->notifs, *notifs, parent, lys_compile_notif, 0, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003465
3466 if (uses_p->when) {
3467 /* inherit when */
3468 LY_ARRAY_FOR(*notifs, u) {
3469 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
3470 (struct lysc_node *)&(*notifs)[u], &when_shared);
3471 LY_CHECK_GOTO(ret, cleanup);
3472 }
3473 }
3474 }
3475
3476 /* check that all augments were applied */
3477 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003478 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3479 /* augment of some parent uses, irrelevant now */
3480 continue;
3481 }
3482
3483 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003484 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3485 ret = LY_ENOTFOUND;
3486 }
3487 LY_CHECK_GOTO(ret, cleanup);
3488
3489 /* check that all refines were applied */
3490 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003491 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3492 /* refine of some paretn uses, irrelevant now */
3493 continue;
3494 }
3495
3496 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003497 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3498 ret = LY_ENOTFOUND;
3499 }
3500 LY_CHECK_GOTO(ret, cleanup);
3501
3502cleanup:
3503 /* reload previous context's parsed module being processed */
3504 ctx->pmod = mod_old;
3505
3506 /* remove the grouping from the stack for circular groupings dependency check */
3507 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3508 assert(ctx->groupings.count == grp_stack_count);
3509
3510 ly_set_erase(&uses_child_set, NULL);
3511 return ret;
3512}
3513
3514static int
3515lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3516{
3517 struct lysp_node *iter;
3518 int len = 0;
3519
3520 *path = NULL;
3521 for (iter = node; iter && len >= 0; iter = iter->parent) {
3522 char *s = *path;
3523 char *id;
3524
3525 switch (iter->nodetype) {
3526 case LYS_USES:
3527 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3528 break;
3529 case LYS_GROUPING:
3530 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3531 break;
3532 case LYS_AUGMENT:
3533 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3534 break;
3535 default:
3536 id = strdup(iter->name);
3537 break;
3538 }
3539
3540 if (!iter->parent) {
3541 /* print prefix */
3542 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3543 } else {
3544 /* prefix is the same as in parent */
3545 len = asprintf(path, "/%s%s", id, s ? s : "");
3546 }
3547 free(s);
3548 free(id);
3549 }
3550
3551 if (len < 0) {
3552 free(*path);
3553 *path = NULL;
3554 } else if (len == 0) {
3555 *path = strdup("/");
3556 len = 1;
3557 }
3558 return len;
3559}
3560
3561LY_ERR
3562lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_grp *grp)
3563{
3564 LY_ERR ret;
3565 char *path;
3566 int len;
3567
3568 struct lysp_node_uses fake_uses = {
3569 .parent = pnode,
3570 .nodetype = LYS_USES,
3571 .flags = 0, .next = NULL,
3572 .name = grp->name,
3573 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3574 .refines = NULL, .augments = NULL
3575 };
3576 struct lysc_node_container fake_container = {
3577 .nodetype = LYS_CONTAINER,
3578 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3579 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003580 .parent = NULL, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003581 .prev = (struct lysc_node *)&fake_container,
3582 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003583 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003584 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3585 };
3586
3587 if (grp->parent) {
3588 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3589 }
3590
3591 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3592 if (len < 0) {
3593 LOGMEM(ctx->ctx);
3594 return LY_EMEM;
3595 }
3596 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3597 ctx->path_len = (uint32_t)len;
3598 free(path);
3599
3600 lysc_update_path(ctx, NULL, "{grouping}");
3601 lysc_update_path(ctx, NULL, grp->name);
3602 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
3603 lysc_update_path(ctx, NULL, NULL);
3604 lysc_update_path(ctx, NULL, NULL);
3605
3606 ctx->path_len = 1;
3607 ctx->path[1] = '\0';
3608
3609 /* cleanup */
3610 lysc_node_container_free(ctx->ctx, &fake_container);
3611
3612 return ret;
3613}
3614
3615/**
3616 * @brief Set config flags for a node.
3617 *
3618 * @param[in] ctx Compile context.
3619 * @param[in] node Compiled node config to set.
3620 * @param[in] parent Parent of @p node.
3621 * @return LY_ERR value.
3622 */
3623static LY_ERR
3624lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
3625{
3626 if (node->nodetype == LYS_CASE) {
3627 /* case never has any config */
3628 assert(!(node->flags & LYS_CONFIG_MASK));
3629 return LY_SUCCESS;
3630 }
3631
3632 /* adjust parent to always get the ancestor with config */
3633 if (parent && (parent->nodetype == LYS_CASE)) {
3634 parent = parent->parent;
3635 assert(parent);
3636 }
3637
3638 if (ctx->options & (LYS_COMPILE_RPC_INPUT | LYS_COMPILE_RPC_OUTPUT)) {
3639 /* ignore config statements inside RPC/action data */
3640 node->flags &= ~LYS_CONFIG_MASK;
3641 node->flags |= (ctx->options & LYS_COMPILE_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
3642 } else if (ctx->options & LYS_COMPILE_NOTIFICATION) {
3643 /* ignore config statements inside Notification data */
3644 node->flags &= ~LYS_CONFIG_MASK;
3645 node->flags |= LYS_CONFIG_R;
3646 } else if (!(node->flags & LYS_CONFIG_MASK)) {
3647 /* config not explicitely set, inherit it from parent */
3648 if (parent) {
3649 node->flags |= parent->flags & LYS_CONFIG_MASK;
3650 } else {
3651 /* default is config true */
3652 node->flags |= LYS_CONFIG_W;
3653 }
3654 } else {
3655 /* config set explicitely */
3656 node->flags |= LYS_SET_CONFIG;
3657 }
3658
3659 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003660 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003661 "Configuration node cannot be child of any state data node.");
3662 return LY_EVALID;
3663 }
3664
3665 return LY_SUCCESS;
3666}
3667
3668LY_ERR
3669lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3670 struct ly_set *child_set)
3671{
3672 LY_ERR ret = LY_SUCCESS;
3673 struct lysc_node *node = NULL;
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003674 struct lysp_node *dev_pnode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003675 ly_bool not_supported, enabled;
3676 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003677
3678 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3679
3680 if (pnode->nodetype != LYS_USES) {
3681 lysc_update_path(ctx, parent, pnode->name);
3682 } else {
3683 lysc_update_path(ctx, NULL, "{uses}");
3684 lysc_update_path(ctx, NULL, pnode->name);
3685 }
3686
3687 switch (pnode->nodetype) {
3688 case LYS_CONTAINER:
3689 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3690 node_compile_spec = lys_compile_node_container;
3691 break;
3692 case LYS_LEAF:
3693 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3694 node_compile_spec = lys_compile_node_leaf;
3695 break;
3696 case LYS_LIST:
3697 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3698 node_compile_spec = lys_compile_node_list;
3699 break;
3700 case LYS_LEAFLIST:
3701 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3702 node_compile_spec = lys_compile_node_leaflist;
3703 break;
3704 case LYS_CHOICE:
3705 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3706 node_compile_spec = lys_compile_node_choice;
3707 break;
3708 case LYS_CASE:
3709 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3710 node_compile_spec = lys_compile_node_case;
3711 break;
3712 case LYS_ANYXML:
3713 case LYS_ANYDATA:
3714 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3715 node_compile_spec = lys_compile_node_any;
3716 break;
3717 case LYS_USES:
3718 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3719 lysc_update_path(ctx, NULL, NULL);
3720 lysc_update_path(ctx, NULL, NULL);
3721 return ret;
3722 default:
3723 LOGINT(ctx->ctx);
3724 return LY_EINT;
3725 }
3726 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3727
3728 /* compile any deviations for this node */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003729 LY_CHECK_ERR_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported),
3730 free(node), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003731 if (not_supported) {
3732 free(node);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003733 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003734 } else if (dev_pnode) {
3735 pnode = dev_pnode;
3736 }
3737
3738 node->nodetype = pnode->nodetype;
3739 node->module = ctx->cur_mod;
3740 node->prev = node;
3741 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
3742
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003743 /* if-features */
3744 LY_CHECK_ERR_RET(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), free(node), ret);
Michal Vasko12606ee2020-11-25 17:05:11 +01003745 if (!enabled && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003746 ly_set_add(&ctx->disabled, node, 1, NULL);
3747 ctx->options |= LYS_COMPILE_DISABLED;
3748 }
3749
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003750 /* config */
3751 ret = lys_compile_config(ctx, node, parent);
3752 LY_CHECK_GOTO(ret, error);
3753
3754 /* list ordering */
3755 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3756 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
3757 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
3758 (ctx->options & LYS_COMPILE_RPC_OUTPUT) ? "RPC/action output parameters" :
3759 (ctx->options & LYS_COMPILE_NOTIFICATION) ? "notification content" : "state data", ctx->path);
3760 node->flags &= ~LYS_ORDBY_MASK;
3761 node->flags |= LYS_ORDBY_SYSTEM;
3762 } else if (!(node->flags & LYS_ORDBY_MASK)) {
3763 /* default ordering is system */
3764 node->flags |= LYS_ORDBY_SYSTEM;
3765 }
3766 }
3767
3768 /* status - it is not inherited by specification, but it does not make sense to have
3769 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3770 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
3771
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003772 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
3773 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
3774 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003775
3776 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
3777 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
3778
3779 if (pnode->when) {
3780 /* compile when */
3781 ret = lys_compile_when(ctx, pnode->when, pnode->flags, lysc_xpath_context(node), node, NULL);
3782 LY_CHECK_GOTO(ret, cleanup);
3783 }
3784
3785 /* connect any augments */
3786 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
3787
3788 /* nodetype-specific part */
3789 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
3790
3791 /* final compilation tasks that require the node to be connected */
3792 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, LYEXT_PAR_NODE, ret, cleanup);
3793 if (node->flags & LYS_MAND_TRUE) {
3794 /* inherit LYS_MAND_TRUE in parent containers */
3795 lys_compile_mandatory_parents(parent, 1);
3796 }
3797
3798 if (child_set) {
3799 /* add the new node into set */
3800 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
3801 }
3802
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003803 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003804
3805error:
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003806 lysc_node_free(ctx->ctx, node, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003807cleanup:
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003808 ctx->options = prev_opts;
3809 if (ret && dev_pnode) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003810 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003811 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003812 lysp_dev_node_free(ctx->ctx, dev_pnode);
3813 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003814 return ret;
3815}