blob: d0de62568c761ec1790a5808f78e13b8e67683df [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of common nodes.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020017
18#include "schema_compile_node.h"
19
20#include <assert.h>
21#include <ctype.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "common.h"
29#include "compat.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020030#include "dict.h"
31#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020032#include "plugins_exts.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020033#include "plugins_types.h"
34#include "schema_compile.h"
35#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010036#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "set.h"
38#include "tree.h"
39#include "tree_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020040#include "tree_schema.h"
41#include "tree_schema_internal.h"
42#include "xpath.h"
43
44static struct lysc_ext_instance *
45lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
46{
47 /* TODO - extensions, increase refcount */
48 (void) ctx;
49 (void) orig;
50 return NULL;
51}
52
53/**
54 * @brief Add/replace a leaf default value in unres.
55 * Can also be used for a single leaf-list default value.
56 *
57 * @param[in] ctx Compile context.
58 * @param[in] leaf Leaf with the default value.
59 * @param[in] dflt Default value to use.
60 * @return LY_ERR value.
61 */
62static LY_ERR
63lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
64{
65 struct lysc_unres_dflt *r = NULL;
66 uint32_t i;
67
Michal Vasko12606ee2020-11-25 17:05:11 +010068 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010069 return LY_SUCCESS;
70 }
71
Michal Vasko405cc9e2020-12-01 12:01:27 +010072 for (i = 0; i < ctx->unres->dflts.count; ++i) {
73 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020074 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010075 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020076 lysp_qname_free(ctx->ctx, r->dflt);
77 free(r->dflt);
78 break;
79 }
80 }
81 if (!r) {
82 /* add new unres item */
83 r = calloc(1, sizeof *r);
84 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
85 r->leaf = leaf;
86
Michal Vasko405cc9e2020-12-01 12:01:27 +010087 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020088 }
89
90 r->dflt = malloc(sizeof *r->dflt);
91 LY_CHECK_GOTO(!r->dflt, error);
92 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
93
94 return LY_SUCCESS;
95
96error:
97 free(r->dflt);
98 LOGMEM(ctx->ctx);
99 return LY_EMEM;
100}
101
102/**
103 * @brief Add/replace a leaf-list default value(s) in unres.
104 *
105 * @param[in] ctx Compile context.
106 * @param[in] llist Leaf-list with the default value.
107 * @param[in] dflts Sized array of the default values.
108 * @return LY_ERR value.
109 */
110static LY_ERR
111lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
112{
113 struct lysc_unres_dflt *r = NULL;
114 uint32_t i;
115
Michal Vasko12606ee2020-11-25 17:05:11 +0100116 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100117 return LY_SUCCESS;
118 }
119
Michal Vasko405cc9e2020-12-01 12:01:27 +0100120 for (i = 0; i < ctx->unres->dflts.count; ++i) {
121 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200122 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100123 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200124 lysp_qname_free(ctx->ctx, r->dflt);
125 free(r->dflt);
126 r->dflt = NULL;
127 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
128 r->dflts = NULL;
129 break;
130 }
131 }
132 if (!r) {
133 r = calloc(1, sizeof *r);
134 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
135 r->llist = llist;
136
Michal Vasko405cc9e2020-12-01 12:01:27 +0100137 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200138 }
139
140 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
141
142 return LY_SUCCESS;
143}
144
145/**
146 * @brief Duplicate the compiled pattern structure.
147 *
148 * Instead of duplicating memory, the reference counter in the @p orig is increased.
149 *
150 * @param[in] orig The pattern structure to duplicate.
151 * @return The duplicated structure to use.
152 */
153static struct lysc_pattern *
154lysc_pattern_dup(struct lysc_pattern *orig)
155{
156 ++orig->refcount;
157 return orig;
158}
159
160/**
161 * @brief Duplicate the array of compiled patterns.
162 *
163 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
164 *
165 * @param[in] ctx Libyang context for logging.
166 * @param[in] orig The patterns sized array to duplicate.
167 * @return New sized array as a copy of @p orig.
168 * @return NULL in case of memory allocation error.
169 */
170static struct lysc_pattern **
171lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
172{
173 struct lysc_pattern **dup = NULL;
174 LY_ARRAY_COUNT_TYPE u;
175
176 assert(orig);
177
178 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
179 LY_ARRAY_FOR(orig, u) {
180 dup[u] = lysc_pattern_dup(orig[u]);
181 LY_ARRAY_INCREMENT(dup);
182 }
183 return dup;
184}
185
186/**
187 * @brief Duplicate compiled range structure.
188 *
189 * @param[in] ctx Libyang context for logging.
190 * @param[in] orig The range structure to be duplicated.
191 * @return New compiled range structure as a copy of @p orig.
192 * @return NULL in case of memory allocation error.
193 */
194static struct lysc_range *
195lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
196{
197 struct lysc_range *dup;
198 LY_ERR ret;
199
200 assert(orig);
201
202 dup = calloc(1, sizeof *dup);
203 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
204 if (orig->parts) {
205 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100206 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200207 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
208 }
209 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
210 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
211 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
212
213 return dup;
214cleanup:
215 free(dup);
216 (void) ret; /* set but not used due to the return type */
217 return NULL;
218}
219
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200220/**
221 * @brief Compile information from the when statement
222 *
223 * @param[in] ctx Compile context.
224 * @param[in] when_p Parsed when structure.
225 * @param[in] flags Flags of the parsed node with the when statement.
226 * @param[in] ctx_node Context node for the when statement.
227 * @param[out] when Pointer where to store pointer to the created compiled when structure.
228 * @return LY_ERR value.
229 */
230static LY_ERR
231lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
232 struct lysc_when **when)
233{
234 LY_ERR ret = LY_SUCCESS;
235
236 *when = calloc(1, sizeof **when);
237 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
238 (*when)->refcount = 1;
239 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
240 LY_CHECK_RET(lysc_prefixes_compile(when_p->cond, strlen(when_p->cond), ctx->pmod, &(*when)->prefixes));
241 (*when)->context = (struct lysc_node *)ctx_node;
242 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
243 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100244 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200245 (*when)->flags = flags & LYS_STATUS_MASK;
246
247done:
248 return ret;
249}
250
251LY_ERR
252lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
253 struct lysc_node *node, struct lysc_when **when_c)
254{
255 struct lysc_when **new_when, ***node_when;
256
257 assert(when_p);
258
259 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100260 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200261
262 /* create new when pointer */
263 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
264 if (!when_c || !(*when_c)) {
265 /* compile when */
266 LY_CHECK_RET(lys_compile_when_(ctx, when_p, flags, ctx_node, new_when));
267
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200268 /* remember the compiled when for sharing */
269 if (when_c) {
270 *when_c = *new_when;
271 }
272 } else {
273 /* use the previously compiled when */
274 ++(*when_c)->refcount;
275 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100276 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200277
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100278 if (!(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
279 /* do not check "when" semantics in a grouping, but repeat the check for different node because
280 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100281 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200282 }
283
284 return LY_SUCCESS;
285}
286
287/**
288 * @brief Compile information from the must statement
289 * @param[in] ctx Compile context.
290 * @param[in] must_p The parsed must statement structure.
291 * @param[in,out] must Prepared (empty) compiled must structure to fill.
292 * @return LY_ERR value.
293 */
294static LY_ERR
295lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
296{
297 LY_ERR ret = LY_SUCCESS;
298
299 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
300 LY_CHECK_RET(lysc_prefixes_compile(must_p->arg.str, strlen(must_p->arg.str), must_p->arg.mod, &must->prefixes));
301 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
302 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
303 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
304 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100305 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200306
307done:
308 return ret;
309}
310
311/**
312 * @brief Validate and normalize numeric value from a range definition.
313 * @param[in] ctx Compile context.
314 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
315 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
316 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
317 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
318 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
319 * @param[in] value String value of the range boundary.
320 * @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.
321 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
322 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
323 */
324static LY_ERR
325range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
326 size_t *len, char **valcopy)
327{
328 size_t fraction = 0, size;
329
330 *len = 0;
331
332 assert(value);
333 /* parse value */
334 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
335 return LY_EVALID;
336 }
337
338 if ((value[*len] == '-') || (value[*len] == '+')) {
339 ++(*len);
340 }
341
342 while (isdigit(value[*len])) {
343 ++(*len);
344 }
345
346 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
347 if (basetype == LY_TYPE_DEC64) {
348 goto decimal;
349 } else {
350 *valcopy = strndup(value, *len);
351 return LY_SUCCESS;
352 }
353 }
354 fraction = *len;
355
356 ++(*len);
357 while (isdigit(value[*len])) {
358 ++(*len);
359 }
360
361 if (basetype == LY_TYPE_DEC64) {
362decimal:
363 assert(frdigits);
364 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100365 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200366 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100367 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200368 return LY_EINVAL;
369 }
370 if (fraction) {
371 size = (*len) + (frdigits - ((*len) - 1 - fraction));
372 } else {
373 size = (*len) + frdigits + 1;
374 }
375 *valcopy = malloc(size * sizeof **valcopy);
376 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
377
378 (*valcopy)[size - 1] = '\0';
379 if (fraction) {
380 memcpy(&(*valcopy)[0], &value[0], fraction);
381 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
382 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
383 } else {
384 memcpy(&(*valcopy)[0], &value[0], *len);
385 memset(&(*valcopy)[*len], '0', frdigits);
386 }
387 }
388 return LY_SUCCESS;
389}
390
391/**
392 * @brief Check that values in range are in ascendant order.
393 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
394 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
395 * max can be also equal.
396 * @param[in] value Current value to check.
397 * @param[in] prev_value The last seen value.
398 * @return LY_SUCCESS or LY_EEXIST for invalid order.
399 */
400static LY_ERR
401range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
402{
403 if (unsigned_value) {
404 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
405 return LY_EEXIST;
406 }
407 } else {
408 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
409 return LY_EEXIST;
410 }
411 }
412 return LY_SUCCESS;
413}
414
415/**
416 * @brief Set min/max value of the range part.
417 * @param[in] ctx Compile context.
418 * @param[in] part Range part structure to fill.
419 * @param[in] max Flag to distinguish if storing min or max value.
420 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
421 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
422 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
423 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
424 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
425 * @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.
426 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
427 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
428 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
429 * frdigits value), LY_EMEM.
430 */
431static LY_ERR
432range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
433 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
434{
435 LY_ERR ret = LY_SUCCESS;
436 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100437 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200438
439 if (value) {
440 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
441 LY_CHECK_GOTO(ret, finalize);
442 }
443 if (!valcopy && base_range) {
444 if (max) {
445 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
446 } else {
447 part->min_64 = base_range->parts[0].min_64;
448 }
449 if (!first) {
450 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
451 }
452 goto finalize;
453 }
454
455 switch (basetype) {
456 case LY_TYPE_INT8: /* range */
457 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100458 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 +0200459 } else if (max) {
460 part->max_64 = INT64_C(127);
461 } else {
462 part->min_64 = INT64_C(-128);
463 }
464 if (!ret && !first) {
465 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
466 }
467 break;
468 case LY_TYPE_INT16: /* range */
469 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100470 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
471 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200472 } else if (max) {
473 part->max_64 = INT64_C(32767);
474 } else {
475 part->min_64 = INT64_C(-32768);
476 }
477 if (!ret && !first) {
478 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
479 }
480 break;
481 case LY_TYPE_INT32: /* range */
482 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100483 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
484 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200485 } else if (max) {
486 part->max_64 = INT64_C(2147483647);
487 } else {
488 part->min_64 = INT64_C(-2147483648);
489 }
490 if (!ret && !first) {
491 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
492 }
493 break;
494 case LY_TYPE_INT64: /* range */
495 case LY_TYPE_DEC64: /* range */
496 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100497 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
498 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200499 } else if (max) {
500 part->max_64 = INT64_C(9223372036854775807);
501 } else {
502 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
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_UINT8: /* range */
509 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100510 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 +0200511 } else if (max) {
512 part->max_u64 = UINT64_C(255);
513 } else {
514 part->min_u64 = UINT64_C(0);
515 }
516 if (!ret && !first) {
517 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
518 }
519 break;
520 case LY_TYPE_UINT16: /* range */
521 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100522 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 +0200523 } else if (max) {
524 part->max_u64 = UINT64_C(65535);
525 } else {
526 part->min_u64 = UINT64_C(0);
527 }
528 if (!ret && !first) {
529 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
530 }
531 break;
532 case LY_TYPE_UINT32: /* range */
533 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100534 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
535 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200536 } else if (max) {
537 part->max_u64 = UINT64_C(4294967295);
538 } else {
539 part->min_u64 = UINT64_C(0);
540 }
541 if (!ret && !first) {
542 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
543 }
544 break;
545 case LY_TYPE_UINT64: /* range */
546 case LY_TYPE_STRING: /* length */
547 case LY_TYPE_BINARY: /* length */
548 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100549 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
550 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200551 } else if (max) {
552 part->max_u64 = UINT64_C(18446744073709551615);
553 } else {
554 part->min_u64 = UINT64_C(0);
555 }
556 if (!ret && !first) {
557 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
558 }
559 break;
560 default:
561 LOGINT(ctx->ctx);
562 ret = LY_EINT;
563 }
564
565finalize:
566 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100567 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200568 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
569 length_restr ? "length" : "range", valcopy ? valcopy : *value);
570 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100571 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200572 "Invalid %s restriction - invalid value \"%s\".",
573 length_restr ? "length" : "range", valcopy ? valcopy : *value);
574 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100575 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200576 "Invalid %s restriction - values are not in ascending order (%s).",
577 length_restr ? "length" : "range",
578 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
579 } else if (!ret && value) {
580 *value = *value + len;
581 }
582 free(valcopy);
583 return ret;
584}
585
586/**
587 * @brief Compile the parsed range restriction.
588 * @param[in] ctx Compile context.
589 * @param[in] range_p Parsed range structure to compile.
590 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
591 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
592 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
593 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
594 * range restriction must be more restrictive than the base_range.
595 * @param[in,out] range Pointer to the created current range structure.
596 * @return LY_ERR value.
597 */
598static LY_ERR
599lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
600 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
601{
602 LY_ERR ret = LY_EVALID;
603 const char *expr;
604 struct lysc_range_part *parts = NULL, *part;
605 ly_bool range_expected = 0, uns;
606 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
607
608 assert(range);
609 assert(range_p);
610
611 expr = range_p->arg.str;
612 while (1) {
613 if (isspace(*expr)) {
614 ++expr;
615 } else if (*expr == '\0') {
616 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100617 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200618 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
619 length_restr ? "length" : "range", range_p->arg);
620 goto cleanup;
621 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100622 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200623 "Invalid %s restriction - unexpected end of the expression (%s).",
624 length_restr ? "length" : "range", range_p->arg);
625 goto cleanup;
626 }
627 parts_done++;
628 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100629 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200630 if (parts) {
631 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100632 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200633 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci422afb12021-03-04 16:38:16 +0100634 (int)(expr - range_p->arg.str), range_p->arg.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200635 goto cleanup;
636 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100637 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200638
639 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
640 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
641 part->max_64 = part->min_64;
642 } else if (*expr == '|') {
643 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100644 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200645 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
646 goto cleanup;
647 }
648 expr++;
649 parts_done++;
650 /* process next part of the expression */
651 } else if (!strncmp(expr, "..", 2)) {
652 expr += 2;
653 while (isspace(*expr)) {
654 expr++;
655 }
656 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100657 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200658 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
659 goto cleanup;
660 }
661 /* continue expecting the upper boundary */
662 range_expected = 1;
663 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
664 /* number */
665 if (range_expected) {
666 part = &parts[LY_ARRAY_COUNT(parts) - 1];
667 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
668 range_expected = 0;
669 } else {
670 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
671 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
672 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
673 part->max_64 = part->min_64;
674 }
675
676 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100677 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
678 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200679 while (isspace(*expr)) {
680 expr++;
681 }
682 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100683 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200684 length_restr ? "length" : "range", expr);
685 goto cleanup;
686 }
687 if (range_expected) {
688 part = &parts[LY_ARRAY_COUNT(parts) - 1];
689 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
690 range_expected = 0;
691 } else {
692 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
693 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
694 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
695 part->min_64 = part->max_64;
696 }
697 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100698 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200699 length_restr ? "length" : "range", expr);
700 goto cleanup;
701 }
702 }
703
704 /* check with the previous range/length restriction */
705 if (base_range) {
706 switch (basetype) {
707 case LY_TYPE_BINARY:
708 case LY_TYPE_UINT8:
709 case LY_TYPE_UINT16:
710 case LY_TYPE_UINT32:
711 case LY_TYPE_UINT64:
712 case LY_TYPE_STRING:
713 uns = 1;
714 break;
715 case LY_TYPE_DEC64:
716 case LY_TYPE_INT8:
717 case LY_TYPE_INT16:
718 case LY_TYPE_INT32:
719 case LY_TYPE_INT64:
720 uns = 0;
721 break;
722 default:
723 LOGINT(ctx->ctx);
724 ret = LY_EINT;
725 goto cleanup;
726 }
727 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
728 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
729 goto baseerror;
730 }
731 /* current lower bound is not lower than the base */
732 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
733 /* base has single value */
734 if (base_range->parts[v].min_64 == parts[u].min_64) {
735 /* both lower bounds are the same */
736 if (parts[u].min_64 != parts[u].max_64) {
737 /* current continues with a range */
738 goto baseerror;
739 } else {
740 /* equal single values, move both forward */
741 ++v;
742 continue;
743 }
744 } else {
745 /* base is single value lower than current range, so the
746 * value from base range is removed in the current,
747 * move only base and repeat checking */
748 ++v;
749 --u;
750 continue;
751 }
752 } else {
753 /* base is the range */
754 if (parts[u].min_64 == parts[u].max_64) {
755 /* current is a single value */
756 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
757 /* current is behind the base range, so base range is omitted,
758 * move the base and keep the current for further check */
759 ++v;
760 --u;
761 } /* else it is within the base range, so move the current, but keep the base */
762 continue;
763 } else {
764 /* both are ranges - check the higher bound, the lower was already checked */
765 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
766 /* higher bound is higher than the current higher bound */
767 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
768 /* but the current lower bound is also higher, so the base range is omitted,
769 * continue with the same current, but move the base */
770 --u;
771 ++v;
772 continue;
773 }
774 /* current range starts within the base range but end behind it */
775 goto baseerror;
776 } else {
777 /* current range is smaller than the base,
778 * move current, but stay with the base */
779 continue;
780 }
781 }
782 }
783 }
784 if (u != parts_done) {
785baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100786 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200787 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
788 length_restr ? "length" : "range", range_p->arg);
789 goto cleanup;
790 }
791 }
792
793 if (!(*range)) {
794 *range = calloc(1, sizeof **range);
795 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
796 }
797
798 /* we rewrite the following values as the types chain is being processed */
799 if (range_p->eapptag) {
800 lydict_remove(ctx->ctx, (*range)->eapptag);
801 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
802 }
803 if (range_p->emsg) {
804 lydict_remove(ctx->ctx, (*range)->emsg);
805 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
806 }
807 if (range_p->dsc) {
808 lydict_remove(ctx->ctx, (*range)->dsc);
809 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
810 }
811 if (range_p->ref) {
812 lydict_remove(ctx->ctx, (*range)->ref);
813 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
814 }
815 /* extensions are taken only from the last range by the caller */
816
817 (*range)->parts = parts;
818 parts = NULL;
819 ret = LY_SUCCESS;
820cleanup:
821 LY_ARRAY_FREE(parts);
822
823 return ret;
824}
825
826LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100827lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200828{
829 size_t idx, idx2, start, end, size, brack;
830 char *perl_regex, *ptr;
831 int err_code;
832 const char *orig_ptr;
833 PCRE2_SIZE err_offset;
834 pcre2_code *code_local;
835
836#define URANGE_LEN 19
837 char *ublock2urange[][2] = {
838 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
839 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
840 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
841 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
842 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
843 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
844 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
845 {"Greek", "[\\x{0370}-\\x{03FF}]"},
846 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
847 {"Armenian", "[\\x{0530}-\\x{058F}]"},
848 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
849 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
850 {"Syriac", "[\\x{0700}-\\x{074F}]"},
851 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
852 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
853 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
854 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
855 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
856 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
857 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
858 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
859 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
860 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
861 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
862 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
863 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
864 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
865 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
866 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
867 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
868 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
869 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
870 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
871 {"Ogham", "[\\x{1680}-\\x{169F}]"},
872 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
873 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
874 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
875 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
876 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
877 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
878 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
879 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
880 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
881 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
882 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
883 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
884 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
885 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
886 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
887 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
888 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
889 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
890 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
891 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
892 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
893 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
894 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
895 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
896 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
897 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
898 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
899 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
900 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
901 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
902 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
903 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
904 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
905 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
906 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
907 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
908 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
909 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
910 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
911 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
912 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
913 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
914 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
915 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
916 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
917 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
918 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
919 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
920 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100921 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200922 {NULL, NULL}
923 };
924
925 /* adjust the expression to a Perl equivalent
926 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
927
928 /* allocate space for the transformed pattern */
929 size = strlen(pattern) + 1;
930 perl_regex = malloc(size);
931 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
932 perl_regex[0] = '\0';
933
934 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
935 brack = 0;
936 idx = 0;
937 orig_ptr = pattern;
938 while (orig_ptr[0]) {
939 switch (orig_ptr[0]) {
940 case '$':
941 case '^':
942 if (!brack) {
943 /* make space for the extra character */
944 ++size;
945 perl_regex = ly_realloc(perl_regex, size);
946 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
947
948 /* print escape slash */
949 perl_regex[idx] = '\\';
950 ++idx;
951 }
952 break;
953 case '[':
954 /* must not be escaped */
955 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
956 ++brack;
957 }
958 break;
959 case ']':
960 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
961 /* pattern was checked and compiled already */
962 assert(brack);
963 --brack;
964 }
965 break;
966 default:
967 break;
968 }
969
970 /* copy char */
971 perl_regex[idx] = orig_ptr[0];
972
973 ++idx;
974 ++orig_ptr;
975 }
976 perl_regex[idx] = '\0';
977
978 /* substitute Unicode Character Blocks with exact Character Ranges */
979 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
980 start = ptr - perl_regex;
981
982 ptr = strchr(ptr, '}');
983 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100984 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200985 free(perl_regex);
986 return LY_EVALID;
987 }
988 end = (ptr - perl_regex) + 1;
989
990 /* need more space */
991 if (end - start < URANGE_LEN) {
992 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
993 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
994 }
995
996 /* find our range */
997 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100998 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
999 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001000 break;
1001 }
1002 }
1003 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001004 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001005 free(perl_regex);
1006 return LY_EVALID;
1007 }
1008
1009 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1010 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1011 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1012 ++idx;
1013 }
1014 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1015 --idx;
1016 }
1017 }
1018 if (idx) {
1019 /* skip brackets */
1020 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1021 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1022 } else {
1023 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1024 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1025 }
1026 }
1027
1028 /* must return 0, already checked during parsing */
1029 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1030 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1031 &err_code, &err_offset, NULL);
1032 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001033 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1034 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001035 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001036 free(perl_regex);
1037 return LY_EVALID;
1038 }
1039 free(perl_regex);
1040
1041 if (code) {
1042 *code = code_local;
1043 } else {
1044 free(code_local);
1045 }
1046
1047 return LY_SUCCESS;
1048
1049#undef URANGE_LEN
1050}
1051
1052/**
1053 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1054 * @param[in] ctx Compile context.
1055 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1056 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1057 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1058 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1059 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1060 */
1061static LY_ERR
1062lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1063 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1064{
1065 struct lysc_pattern **pattern;
1066 LY_ARRAY_COUNT_TYPE u;
1067 LY_ERR ret = LY_SUCCESS;
1068
1069 /* first, copy the patterns from the base type */
1070 if (base_patterns) {
1071 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1072 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1073 }
1074
1075 LY_ARRAY_FOR(patterns_p, u) {
1076 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1077 *pattern = calloc(1, sizeof **pattern);
1078 ++(*pattern)->refcount;
1079
Radek Krejci2efc45b2020-12-22 16:25:44 +01001080 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001081 LY_CHECK_RET(ret);
1082
Radek Krejcif13b87b2020-12-01 22:02:17 +01001083 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001084 (*pattern)->inverted = 1;
1085 }
1086 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1087 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1088 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1089 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1090 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001091 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001092 }
1093done:
1094 return ret;
1095}
1096
1097/**
1098 * @brief map of the possible restrictions combination for the specific built-in type.
1099 */
1100static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1101 0 /* LY_TYPE_UNKNOWN */,
1102 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1103 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1104 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1105 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1106 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1107 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1108 LYS_SET_BIT /* LY_TYPE_BITS */,
1109 0 /* LY_TYPE_BOOL */,
1110 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1111 0 /* LY_TYPE_EMPTY */,
1112 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1113 LYS_SET_BASE /* LY_TYPE_IDENT */,
1114 LYS_SET_REQINST /* LY_TYPE_INST */,
1115 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1116 LYS_SET_TYPE /* LY_TYPE_UNION */,
1117 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1118 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1119 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1120 LYS_SET_RANGE /* LY_TYPE_INT64 */
1121};
1122
1123/**
1124 * @brief stringification of the YANG built-in data types
1125 */
1126const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
1127 "unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1128 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1129 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
1130};
1131
1132/**
1133 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1134 * @param[in] ctx Compile context.
1135 * @param[in] enums_p Array of the parsed enum structures to compile.
1136 * @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.
1137 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1138 * @param[out] enums Newly created array of the compiled enums information for the current type.
1139 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1140 */
1141static LY_ERR
1142lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
1143 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
1144{
1145 LY_ERR ret = LY_SUCCESS;
1146 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001147 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1148 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001149 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001150 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001151
1152 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001153 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001154 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1155 return LY_EVALID;
1156 }
1157
1158 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001159 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001160 if (base_enums) {
1161 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1162 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001163 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001164 break;
1165 }
1166 }
1167 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001168 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001169 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001170 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001171 return LY_EVALID;
1172 }
1173 match = v;
1174 }
1175
1176 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001177 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001178 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001179 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001180 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001181 LY_ARRAY_FOR(*enums, v) {
1182 if (cur_val == (*enums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001183 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001184 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001185 cur_val, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001186 return LY_EVALID;
1187 }
1188 }
1189 } else if (base_enums) {
1190 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001191 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001192 } else {
1193 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001194 if (u == 0) {
1195 cur_val = 0;
1196 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001197 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001198 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001199 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001200 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001201 } else {
1202 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001203 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001204 }
1205
1206 /* save highest value for auto assing */
1207 if (highest_value < cur_val) {
1208 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001209 }
1210 } else { /* LY_TYPE_BITS */
1211 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001212 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001213 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001214 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001215 LY_ARRAY_FOR(*enums, v) {
1216 if (cur_pos == (*enums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001217 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001218 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001219 cur_pos, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 return LY_EVALID;
1221 }
1222 }
1223 } else if (base_enums) {
1224 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001225 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001226 } else {
1227 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001228 if (u == 0) {
1229 cur_pos = 0;
1230 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001231 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001232 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001233 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001234 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001235 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001236 } else {
1237 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001238 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001239 }
1240
1241 /* save highest position for auto assing */
1242 if (highest_position < cur_pos) {
1243 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001244 }
1245 }
1246
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001247 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001248 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001249 if (basetype == LY_TYPE_ENUM) {
1250 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001251 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001252 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001253 enums_p[u].name, base_enums[match].value, cur_val);
1254 return LY_EVALID;
1255 }
1256 } else {
1257 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001258 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001259 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001260 enums_p[u].name, base_enums[match].position, cur_pos);
1261 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001262 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001263 }
1264 }
1265
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001266 /* evaluate if-ffeatures */
1267 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1268 if (!enabled) {
1269 continue;
1270 }
1271
1272 /* add new enum/bit */
1273 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1274 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1275 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1276 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001277 e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) | (basetype == LY_TYPE_ENUM ? LYS_IS_ENUM : 0);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001278 if (basetype == LY_TYPE_ENUM) {
1279 e->value = cur_val;
1280 } else {
1281 e->position = cur_pos;
1282 }
Radek Krejciab430862021-03-02 20:13:40 +01001283 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001284
1285 if (basetype == LY_TYPE_BITS) {
1286 /* keep bits ordered by position */
1287 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
1288 if (v != u) {
1289 memcpy(&storage, e, sizeof *e);
1290 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1291 memcpy(&(*enums)[v], &storage, sizeof storage);
1292 }
1293 }
1294 }
1295
1296done:
1297 return ret;
1298}
1299
Radek Krejci6a205692020-12-09 13:58:22 +01001300static LY_ERR
1301lys_compile_type_union(struct lysc_ctx *ctx, struct lysp_type *ptypes, struct lysp_node *context_pnode, uint16_t context_flags,
Michal Vaskoa99b3572021-02-01 11:54:58 +01001302 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001303{
1304 LY_ERR ret = LY_SUCCESS;
1305 struct lysc_type **utypes = *utypes_p;
1306 struct lysc_type_union *un_aux = NULL;
1307
1308 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1309 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001310 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1311 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001312 LY_CHECK_GOTO(ret, error);
1313 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1314 /* add space for additional types from the union subtype */
1315 un_aux = (struct lysc_type_union *)utypes[u + additional];
1316 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1317 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1318
1319 /* copy subtypes of the subtype union */
1320 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1321 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1322 struct lysc_type_leafref *lref;
1323
1324 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1325 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1326 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1327 lref = (struct lysc_type_leafref *)utypes[u + additional];
1328
1329 lref->basetype = LY_TYPE_LEAFREF;
1330 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1331 LY_CHECK_GOTO(ret, error);
1332 lref->refcount = 1;
1333 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
1334 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
1335 ret = lysc_prefixes_dup(((struct lysc_type_leafref *)un_aux->types[v])->prefixes, &lref->prefixes);
1336 LY_CHECK_GOTO(ret, error);
1337 /* TODO extensions */
1338
1339 } else {
1340 utypes[u + additional] = un_aux->types[v];
1341 ++un_aux->types[v]->refcount;
1342 }
1343 ++additional;
1344 LY_ARRAY_INCREMENT(utypes);
1345 }
1346 /* compensate u increment in main loop */
1347 --additional;
1348
1349 /* free the replaced union subtype */
1350 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1351 un_aux = NULL;
1352 } else {
1353 LY_ARRAY_INCREMENT(utypes);
1354 }
1355 }
1356
1357 *utypes_p = utypes;
1358 return LY_SUCCESS;
1359
1360error:
1361 if (un_aux) {
1362 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1363 }
1364 *utypes_p = utypes;
1365 return ret;
1366}
1367
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001368/**
1369 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1370 * @param[in] ctx Compile context.
1371 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1372 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001373 * @param[in] context_name Name of the context node or referencing typedef for logging.
1374 * @param[in] type_p Parsed type to compile.
1375 * @param[in] basetype Base YANG built-in type of the type to compile.
1376 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1377 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1378 * @param[out] type Newly created type structure with the filled information about the type.
1379 * @return LY_ERR value.
1380 */
1381static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001382lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1383 struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001384{
1385 LY_ERR ret = LY_SUCCESS;
1386 struct lysc_type_bin *bin;
1387 struct lysc_type_num *num;
1388 struct lysc_type_str *str;
1389 struct lysc_type_bits *bits;
1390 struct lysc_type_enum *enumeration;
1391 struct lysc_type_dec *dec;
1392 struct lysc_type_identityref *idref;
1393 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001394 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001395
1396 switch (basetype) {
1397 case LY_TYPE_BINARY:
1398 bin = (struct lysc_type_bin *)(*type);
1399
1400 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1401 if (type_p->length) {
1402 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1403 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1404 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001405 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001406 }
1407 }
1408 break;
1409 case LY_TYPE_BITS:
1410 /* RFC 7950 9.7 - bits */
1411 bits = (struct lysc_type_bits *)(*type);
1412 if (type_p->bits) {
1413 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1414 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1415 (struct lysc_type_bitenum_item **)&bits->bits));
1416 }
1417
1418 if (!base && !type_p->flags) {
1419 /* type derived from bits built-in type must contain at least one bit */
1420 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001421 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001422 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001423 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001424 }
1425 return LY_EVALID;
1426 }
1427 break;
1428 case LY_TYPE_DEC64:
1429 dec = (struct lysc_type_dec *)(*type);
1430
1431 /* RFC 7950 9.3.4 - fraction-digits */
1432 if (!base) {
1433 if (!type_p->fraction_digits) {
1434 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001435 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001436 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001437 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001438 }
1439 return LY_EVALID;
1440 }
1441 dec->fraction_digits = type_p->fraction_digits;
1442 } else {
1443 if (type_p->fraction_digits) {
1444 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1445 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001446 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001447 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1448 tpdfname);
1449 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001450 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001451 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1452 }
1453 return LY_EVALID;
1454 }
1455 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1456 }
1457
1458 /* RFC 7950 9.2.4 - range */
1459 if (type_p->range) {
1460 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1461 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1462 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001463 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001464 }
1465 }
1466 break;
1467 case LY_TYPE_STRING:
1468 str = (struct lysc_type_str *)(*type);
1469
1470 /* RFC 7950 9.4.4 - length */
1471 if (type_p->length) {
1472 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1473 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1474 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001475 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001476 }
1477 } else if (base && ((struct lysc_type_str *)base)->length) {
1478 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1479 }
1480
1481 /* RFC 7950 9.4.5 - pattern */
1482 if (type_p->patterns) {
1483 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1484 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1485 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1486 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1487 }
1488 break;
1489 case LY_TYPE_ENUM:
1490 enumeration = (struct lysc_type_enum *)(*type);
1491
1492 /* RFC 7950 9.6 - enum */
1493 if (type_p->enums) {
1494 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1495 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1496 }
1497
1498 if (!base && !type_p->flags) {
1499 /* type derived from enumerations built-in type must contain at least one enum */
1500 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001501 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001502 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001503 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001504 }
1505 return LY_EVALID;
1506 }
1507 break;
1508 case LY_TYPE_INT8:
1509 case LY_TYPE_UINT8:
1510 case LY_TYPE_INT16:
1511 case LY_TYPE_UINT16:
1512 case LY_TYPE_INT32:
1513 case LY_TYPE_UINT32:
1514 case LY_TYPE_INT64:
1515 case LY_TYPE_UINT64:
1516 num = (struct lysc_type_num *)(*type);
1517
1518 /* RFC 6020 9.2.4 - range */
1519 if (type_p->range) {
1520 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1521 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1522 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001523 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001524 }
1525 }
1526 break;
1527 case LY_TYPE_IDENT:
1528 idref = (struct lysc_type_identityref *)(*type);
1529
1530 /* RFC 7950 9.10.2 - base */
1531 if (type_p->bases) {
1532 if (base) {
1533 /* only the directly derived identityrefs can contain base specification */
1534 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001535 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001536 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1537 tpdfname);
1538 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001539 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001540 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1541 }
1542 return LY_EVALID;
1543 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001544 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 +02001545 }
1546
1547 if (!base && !type_p->flags) {
1548 /* type derived from identityref built-in type must contain at least one base */
1549 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001550 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001551 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001552 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001553 }
1554 return LY_EVALID;
1555 }
1556 break;
1557 case LY_TYPE_LEAFREF:
1558 lref = (struct lysc_type_leafref *)*type;
1559
1560 /* RFC 7950 9.9.3 - require-instance */
1561 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001562 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001563 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001564 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001565 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1566 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001567 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001568 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1569 }
1570 return LY_EVALID;
1571 }
1572 lref->require_instance = type_p->require_instance;
1573 } else if (base) {
1574 /* inherit */
1575 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1576 } else {
1577 /* default is true */
1578 lref->require_instance = 1;
1579 }
1580 if (type_p->path) {
1581 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
1582 LY_CHECK_RET(lysc_prefixes_compile(type_p->path->expr, strlen(type_p->path->expr), type_p->pmod,
1583 &lref->prefixes));
1584 } else if (base) {
1585 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
1586 LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)base)->prefixes, &lref->prefixes));
1587 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001588 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001589 return LY_EVALID;
1590 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001591 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001592 return LY_EVALID;
1593 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001594 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001595 break;
1596 case LY_TYPE_INST:
1597 /* RFC 7950 9.9.3 - require-instance */
1598 if (type_p->flags & LYS_SET_REQINST) {
1599 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1600 } else {
1601 /* default is true */
1602 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1603 }
1604 break;
1605 case LY_TYPE_UNION:
1606 un = (struct lysc_type_union *)(*type);
1607
1608 /* RFC 7950 7.4 - type */
1609 if (type_p->types) {
1610 if (base) {
1611 /* only the directly derived union can contain types specification */
1612 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001613 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001614 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1615 tpdfname);
1616 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001617 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001618 "Invalid type substatement for the type not directly derived from union built-in type.");
1619 }
1620 return LY_EVALID;
1621 }
1622 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001623 LY_CHECK_RET(lys_compile_type_union(ctx, type_p->types, context_pnode, context_flags, context_name, &un->types));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001624 }
1625
1626 if (!base && !type_p->flags) {
1627 /* type derived from union built-in type must contain at least one type */
1628 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001629 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001630 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001631 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001632 }
1633 return LY_EVALID;
1634 }
1635 break;
1636 case LY_TYPE_BOOL:
1637 case LY_TYPE_EMPTY:
1638 case LY_TYPE_UNKNOWN: /* just to complete switch */
1639 break;
1640 }
1641
1642 if (tpdfname) {
1643 switch (basetype) {
1644 case LY_TYPE_BINARY:
1645 type_p->compiled = *type;
1646 *type = calloc(1, sizeof(struct lysc_type_bin));
1647 break;
1648 case LY_TYPE_BITS:
1649 type_p->compiled = *type;
1650 *type = calloc(1, sizeof(struct lysc_type_bits));
1651 break;
1652 case LY_TYPE_DEC64:
1653 type_p->compiled = *type;
1654 *type = calloc(1, sizeof(struct lysc_type_dec));
1655 break;
1656 case LY_TYPE_STRING:
1657 type_p->compiled = *type;
1658 *type = calloc(1, sizeof(struct lysc_type_str));
1659 break;
1660 case LY_TYPE_ENUM:
1661 type_p->compiled = *type;
1662 *type = calloc(1, sizeof(struct lysc_type_enum));
1663 break;
1664 case LY_TYPE_INT8:
1665 case LY_TYPE_UINT8:
1666 case LY_TYPE_INT16:
1667 case LY_TYPE_UINT16:
1668 case LY_TYPE_INT32:
1669 case LY_TYPE_UINT32:
1670 case LY_TYPE_INT64:
1671 case LY_TYPE_UINT64:
1672 type_p->compiled = *type;
1673 *type = calloc(1, sizeof(struct lysc_type_num));
1674 break;
1675 case LY_TYPE_IDENT:
1676 type_p->compiled = *type;
1677 *type = calloc(1, sizeof(struct lysc_type_identityref));
1678 break;
1679 case LY_TYPE_LEAFREF:
1680 type_p->compiled = *type;
1681 *type = calloc(1, sizeof(struct lysc_type_leafref));
1682 break;
1683 case LY_TYPE_INST:
1684 type_p->compiled = *type;
1685 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1686 break;
1687 case LY_TYPE_UNION:
1688 type_p->compiled = *type;
1689 *type = calloc(1, sizeof(struct lysc_type_union));
1690 break;
1691 case LY_TYPE_BOOL:
1692 case LY_TYPE_EMPTY:
1693 case LY_TYPE_UNKNOWN: /* just to complete switch */
1694 break;
1695 }
1696 }
1697 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1698
1699cleanup:
1700 return ret;
1701}
1702
1703LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001704lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1705 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001706{
1707 LY_ERR ret = LY_SUCCESS;
1708 ly_bool dummyloops = 0;
1709 struct type_context {
1710 const struct lysp_tpdf *tpdf;
1711 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001712 } *tctx, *tctx_prev = NULL, *tctx_iter;
1713 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1714 struct lysc_type *base = NULL, *prev_type;
1715 struct ly_set tpdf_chain = {0};
1716
1717 (*type) = NULL;
1718 if (dflt) {
1719 *dflt = NULL;
1720 }
1721
1722 tctx = calloc(1, sizeof *tctx);
1723 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001724 for (ret = lysp_type_find(type_p->name, context_pnode, type_p->pmod, &basetype, &tctx->tpdf, &tctx->node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001725 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001726 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1727 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001728 if (basetype) {
1729 break;
1730 }
1731
1732 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001733 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1734 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001735 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1736
1737 if (units && !*units) {
1738 /* inherit units */
1739 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1740 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1741 }
1742 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1743 /* inherit default */
1744 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1745 }
1746 if (dummyloops && (!units || *units) && dflt && *dflt) {
1747 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1748 break;
1749 }
1750
Radek Krejci720d2612021-03-03 19:44:22 +01001751 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1752 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1753 * and we need now recompile the type again in the updated context. */
1754 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1755 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1756 }
1757
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001758 if (tctx->tpdf->type.compiled) {
1759 /* it is not necessary to continue, the rest of the chain was already compiled,
1760 * but we still may need to inherit default and units values, so start dummy loops */
1761 basetype = tctx->tpdf->type.compiled->basetype;
1762 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1763 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1764
1765 if ((units && !*units) || (dflt && !*dflt)) {
1766 dummyloops = 1;
1767 goto preparenext;
1768 } else {
1769 tctx = NULL;
1770 break;
1771 }
1772 }
1773
1774 /* circular typedef reference detection */
1775 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1776 /* local part */
1777 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001778 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001779 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001780 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1781 free(tctx);
1782 ret = LY_EVALID;
1783 goto cleanup;
1784 }
1785 }
1786 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1787 /* global part for unions corner case */
1788 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001789 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001790 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001791 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1792 free(tctx);
1793 ret = LY_EVALID;
1794 goto cleanup;
1795 }
1796 }
1797
1798 /* store information for the following processing */
1799 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1800 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1801
1802preparenext:
1803 /* prepare next loop */
1804 tctx_prev = tctx;
1805 tctx = calloc(1, sizeof *tctx);
1806 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1807 }
1808 free(tctx);
1809
1810 /* allocate type according to the basetype */
1811 switch (basetype) {
1812 case LY_TYPE_BINARY:
1813 *type = calloc(1, sizeof(struct lysc_type_bin));
1814 break;
1815 case LY_TYPE_BITS:
1816 *type = calloc(1, sizeof(struct lysc_type_bits));
1817 break;
1818 case LY_TYPE_BOOL:
1819 case LY_TYPE_EMPTY:
1820 *type = calloc(1, sizeof(struct lysc_type));
1821 break;
1822 case LY_TYPE_DEC64:
1823 *type = calloc(1, sizeof(struct lysc_type_dec));
1824 break;
1825 case LY_TYPE_ENUM:
1826 *type = calloc(1, sizeof(struct lysc_type_enum));
1827 break;
1828 case LY_TYPE_IDENT:
1829 *type = calloc(1, sizeof(struct lysc_type_identityref));
1830 break;
1831 case LY_TYPE_INST:
1832 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1833 break;
1834 case LY_TYPE_LEAFREF:
1835 *type = calloc(1, sizeof(struct lysc_type_leafref));
1836 break;
1837 case LY_TYPE_STRING:
1838 *type = calloc(1, sizeof(struct lysc_type_str));
1839 break;
1840 case LY_TYPE_UNION:
1841 *type = calloc(1, sizeof(struct lysc_type_union));
1842 break;
1843 case LY_TYPE_INT8:
1844 case LY_TYPE_UINT8:
1845 case LY_TYPE_INT16:
1846 case LY_TYPE_UINT16:
1847 case LY_TYPE_INT32:
1848 case LY_TYPE_UINT32:
1849 case LY_TYPE_INT64:
1850 case LY_TYPE_UINT64:
1851 *type = calloc(1, sizeof(struct lysc_type_num));
1852 break;
1853 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001854 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001855 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1856 ret = LY_EVALID;
1857 goto cleanup;
1858 }
1859 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1860 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001861 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001862 ly_data_type2str[basetype]);
1863 free(*type);
1864 (*type) = NULL;
1865 ret = LY_EVALID;
1866 goto cleanup;
1867 }
1868
1869 /* get restrictions from the referred typedefs */
1870 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1871 tctx = (struct type_context *)tpdf_chain.objs[u];
1872
1873 /* remember the typedef context for circular check */
1874 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1875 LY_CHECK_GOTO(ret, cleanup);
1876
1877 if (tctx->tpdf->type.compiled) {
1878 base = tctx->tpdf->type.compiled;
1879 continue;
1880 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1881 /* no change, just use the type information from the base */
1882 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1883 ++base->refcount;
1884 continue;
1885 }
1886
1887 ++(*type)->refcount;
1888 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001889 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001890 tctx->tpdf->name, ly_data_type2str[basetype]);
1891 ret = LY_EVALID;
1892 goto cleanup;
1893 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001894 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001895 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1896 tctx->tpdf->name, tctx->tpdf->dflt.str);
1897 ret = LY_EVALID;
1898 goto cleanup;
1899 }
1900
1901 (*type)->basetype = basetype;
1902 /* TODO user type plugins */
1903 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1904 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001905 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001906 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1907 LY_CHECK_GOTO(ret, cleanup);
1908 base = prev_type;
1909 }
1910 /* remove the processed typedef contexts from the stack for circular check */
1911 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1912
1913 /* process the type definition in leaf */
1914 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1915 /* get restrictions from the node itself */
1916 (*type)->basetype = basetype;
1917 /* TODO user type plugins */
1918 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1919 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001920 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001921 LY_CHECK_GOTO(ret, cleanup);
1922 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1923 /* no specific restriction in leaf's type definition, copy from the base */
1924 free(*type);
1925 (*type) = base;
1926 ++(*type)->refcount;
1927 }
1928
Radek Krejciab430862021-03-02 20:13:40 +01001929 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001930
1931cleanup:
1932 ly_set_erase(&tpdf_chain, free);
1933 return ret;
1934}
1935
1936/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01001937 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
1938 */
1939#define LYS_STATUS_USES LYS_CONFIG_MASK
1940
1941/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001942 * @brief Compile status information of the given node.
1943 *
1944 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
1945 * has the status correctly set during the compilation.
1946 *
1947 * @param[in] ctx Compile context
1948 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
1949 * If the status was set explicitly on the node, it is already set in the flags value and we just check
1950 * the compatibility with the parent's status value.
1951 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
1952 * @return LY_ERR value.
1953 */
1954static LY_ERR
1955lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
1956{
1957 /* status - it is not inherited by specification, but it does not make sense to have
1958 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
1959 if (!((*node_flags) & LYS_STATUS_MASK)) {
1960 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001961 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001962 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01001963 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001964 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
1965 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1966 }
1967 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
1968 } else {
1969 (*node_flags) |= LYS_STATUS_CURR;
1970 }
1971 } else if (parent_flags & LYS_STATUS_MASK) {
1972 /* check status compatibility with the parent */
1973 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
1974 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001975 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001976 "A \"current\" status is in conflict with the parent's \"%s\" status.",
1977 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1978 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001979 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001980 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
1981 }
1982 return LY_EVALID;
1983 }
1984 }
1985 return LY_SUCCESS;
1986}
1987
1988/**
1989 * @brief Check uniqness of the node/action/notification name.
1990 *
1991 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
1992 * structures, but they share the namespace so we need to check their name collisions.
1993 *
1994 * @param[in] ctx Compile context.
1995 * @param[in] parent Parent of the nodes to check, can be NULL.
1996 * @param[in] name Name of the item to find in the given lists.
1997 * @param[in] exclude Node that was just added that should be excluded from the name checking.
1998 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
1999 */
2000static LY_ERR
2001lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2002 const struct lysc_node *exclude)
2003{
2004 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002005 const struct lysc_node_action *actions;
2006 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002007 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002008 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002009
2010#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2011
2012 if (exclude->nodetype == LYS_CASE) {
2013 /* check restricted only to all the cases */
2014 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002015 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002016 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002017 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002018 return LY_EEXIST;
2019 }
2020 }
2021
2022 return LY_SUCCESS;
2023 }
2024
2025 /* no reason for our parent to be choice anymore */
2026 assert(!parent || (parent->nodetype != LYS_CHOICE));
2027
2028 if (parent && (parent->nodetype == LYS_CASE)) {
2029 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002030
2031 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2032 iter = lysc_data_parent(parent);
2033 do {
2034 parent = parent->parent;
2035 if (parent && (parent->nodetype == LYS_CHOICE)) {
2036 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2037 }
2038 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002039 }
2040
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002041 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002042 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2043 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2044 if (exclude->flags & LYS_IS_OUTPUT) {
2045 getnext_flags |= LYS_GETNEXT_OUTPUT;
2046 parent = lysc_node_child(parent)->next;
2047 } else {
2048 parent = lysc_node_child(parent);
2049 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002050 }
2051
2052 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002053 if (!parent && ctx->ext) {
2054 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2055 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2056 goto error;
2057 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002058
Radek Krejci5fa32a32021-02-08 18:12:38 +01002059 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2060 if (iter->nodetype == LYS_CHOICE) {
2061 iter2 = NULL;
2062 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2063 if (CHECK_NODE(iter2, exclude, name)) {
2064 goto error;
2065 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002066 }
2067 }
2068 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002069 } else {
2070 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2071 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2072 goto error;
2073 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002074
Radek Krejci5fa32a32021-02-08 18:12:38 +01002075 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2076 if (iter->nodetype == LYS_CHOICE) {
2077 iter2 = NULL;
2078 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2079 if (CHECK_NODE(iter2, exclude, name)) {
2080 goto error;
2081 }
2082 }
2083 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002084 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002085
Radek Krejci5fa32a32021-02-08 18:12:38 +01002086 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2087 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2088 if (CHECK_NODE(iter, exclude, name)) {
2089 goto error;
2090 }
2091 }
2092
2093 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2094 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2095 if (CHECK_NODE(iter, exclude, name)) {
2096 goto error;
2097 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002098 }
2099 }
Radek Krejci078e4342021-02-12 18:17:26 +01002100 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002101 return LY_SUCCESS;
2102
2103error:
Radek Krejci078e4342021-02-12 18:17:26 +01002104 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002105 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002106 return LY_EEXIST;
2107
2108#undef CHECK_NODE
2109}
2110
Radek Krejci2a9fc652021-01-22 17:44:34 +01002111/**
2112 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2113 * keep specific order of augments targetting the same node.
2114 *
2115 * @param[in] ctx Compile context
2116 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2117 * the choice itself is expected instead of a specific case node.
2118 * @param[in] node Schema node to connect into the list.
2119 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2120 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2121 */
2122static LY_ERR
2123lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002124{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002125 struct lysc_node **children, *anchor = NULL;
2126 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002127
Radek Krejci2a9fc652021-01-22 17:44:34 +01002128 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002129
Radek Krejci2a9fc652021-01-22 17:44:34 +01002130 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002131 if (node->nodetype == LYS_INPUT) {
2132 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2133 /* input node is part of the action but link it with output */
2134 node->next = &((struct lysc_node_action *)parent)->output.node;
2135 node->prev = node->next;
2136 return LY_SUCCESS;
2137 } else if (node->nodetype == LYS_OUTPUT) {
2138 /* output node is part of the action but link it with input */
2139 node->next = NULL;
2140 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002141 return LY_SUCCESS;
2142 } else if (node->nodetype == LYS_ACTION) {
2143 children = (struct lysc_node **)lysc_node_actions_p(parent);
2144 } else if (node->nodetype == LYS_NOTIF) {
2145 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2146 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002147 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002148 }
2149 assert(children);
2150
2151 if (!(*children)) {
2152 /* first child */
2153 *children = node;
2154 } else if (node->flags & LYS_KEY) {
2155 /* special handling of adding keys */
2156 assert(node->module == parent->module);
2157 anchor = *children;
2158 if (anchor->flags & LYS_KEY) {
2159 while ((anchor->flags & LYS_KEY) && anchor->next) {
2160 anchor = anchor->next;
2161 }
2162 /* insert after the last key */
2163 insert_after = 1;
2164 } /* else insert before anchor (at the beginning) */
2165 } else if ((*children)->prev->module == node->module) {
2166 /* last child is from the same module, keep the order and insert at the end */
2167 anchor = (*children)->prev;
2168 insert_after = 1;
2169 } else if (parent->module == node->module) {
2170 /* adding module child after some augments were connected */
2171 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2172 } else {
2173 /* some augments are already connected and we are connecting new ones,
2174 * keep module name order and insert the node into the children list */
2175 anchor = *children;
2176 do {
2177 anchor = anchor->prev;
2178
2179 /* check that we have not found the last augment node from our module or
2180 * the first augment node from a "smaller" module or
2181 * the first node from a local module */
2182 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2183 (anchor->module == parent->module)) {
2184 /* insert after */
2185 insert_after = 1;
2186 break;
2187 }
2188
2189 /* we have traversed all the nodes, insert before anchor (as the first node) */
2190 } while (anchor->prev->next);
2191 }
2192
2193 /* insert */
2194 if (anchor) {
2195 if (insert_after) {
2196 node->next = anchor->next;
2197 node->prev = anchor;
2198 anchor->next = node;
2199 if (node->next) {
2200 /* middle node */
2201 node->next->prev = node;
2202 } else {
2203 /* last node */
2204 (*children)->prev = node;
2205 }
2206 } else {
2207 node->next = anchor;
2208 node->prev = anchor->prev;
2209 anchor->prev = node;
2210 if (anchor == *children) {
2211 /* first node */
2212 *children = node;
2213 } else {
2214 /* middle node */
2215 node->prev->next = node;
2216 }
2217 }
2218 }
2219
2220 /* check the name uniqueness (even for an only child, it may be in case) */
2221 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2222 return LY_EEXIST;
2223 }
2224 } else {
2225 /* top-level element */
2226 struct lysc_node **list;
2227
Radek Krejci6b88a462021-02-17 12:39:34 +01002228 if (ctx->ext) {
2229 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2230 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002231 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2232 } else if (node->nodetype == LYS_NOTIF) {
2233 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2234 } else {
2235 list = &ctx->cur_mod->compiled->data;
2236 }
2237 if (!(*list)) {
2238 *list = node;
2239 } else {
2240 /* insert at the end of the module's top-level nodes list */
2241 (*list)->prev->next = node;
2242 node->prev = (*list)->prev;
2243 (*list)->prev = node;
2244 }
2245
2246 /* check the name uniqueness on top-level */
2247 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2248 return LY_EEXIST;
2249 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002250 }
2251
Radek Krejci2a9fc652021-01-22 17:44:34 +01002252 return LY_SUCCESS;
2253}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002254
Radek Krejci2a9fc652021-01-22 17:44:34 +01002255/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002256 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002257 *
2258 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002259 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002260 * @return LY_ERR value.
2261 */
2262static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002263lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002264{
2265 /* case never has any explicit config */
2266 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002267
Radek Krejci91531e12021-02-08 19:57:54 +01002268 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
2269 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002270 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002271 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2272 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002273 if (node->parent) {
2274 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002275 } else {
2276 /* default is config true */
2277 node->flags |= LYS_CONFIG_W;
2278 }
2279 } else {
2280 /* config set explicitly */
2281 node->flags |= LYS_SET_CONFIG;
2282 }
2283
Radek Krejci91531e12021-02-08 19:57:54 +01002284 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002285 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002286 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002287 }
2288
Radek Krejci2a9fc652021-01-22 17:44:34 +01002289 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002290}
2291
Radek Krejci91531e12021-02-08 19:57:54 +01002292/**
2293 * @brief Set various flags of the compiled nodes
2294 *
2295 * @param[in] ctx Compile context.
2296 * @param[in] node Compiled node where the flags will be set.
2297 * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags).
2298 * Zero means no uses, non-zero value with no status bit set mean the default status.
2299 */
2300static LY_ERR
2301lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2302{
2303 /* inherit config flags */
2304 LY_CHECK_RET(lys_compile_config(ctx, node));
2305
2306 /* status - it is not inherited by specification, but it does not make sense to have
2307 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2308 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (node->parent ? node->parent->flags : 0)));
2309
2310 /* other flags */
2311 if ((ctx->options & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
2312 node->flags |= LYS_IS_INPUT;
2313 } else if ((ctx->options & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
2314 node->flags |= LYS_IS_OUTPUT;
2315 } else if ((ctx->options & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
2316 node->flags |= LYS_IS_NOTIF;
2317 }
2318
2319 return LY_SUCCESS;
2320}
2321
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002322LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002323lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2324 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2325 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002326{
2327 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002328 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002329 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002330 struct lysp_when *pwhen = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002331
Radek Krejci2a9fc652021-01-22 17:44:34 +01002332 node->nodetype = pnode->nodetype;
2333 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002334 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002335 node->prev = node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002336
Radek Krejci2a9fc652021-01-22 17:44:34 +01002337 /* compile any deviations for this node */
2338 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002339 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002340 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002341 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002342 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002343 }
2344
Radek Krejci2a9fc652021-01-22 17:44:34 +01002345 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002346
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002347 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002348 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Radek Krejciaf0548b2021-02-15 15:11:22 +01002349 if (!enabled && !(ctx->options & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002350 ly_set_add(&ctx->disabled, node, 1, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002351 ctx->options |= LYS_COMPILE_DISABLED;
2352 }
2353
Radek Krejci91531e12021-02-08 19:57:54 +01002354 /* config, status and other flags */
2355 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002356 LY_CHECK_GOTO(ret, error);
2357
2358 /* list ordering */
2359 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002360 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002361 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002362 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
2363 (ctx->options & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002364 node->flags &= ~LYS_ORDBY_MASK;
2365 node->flags |= LYS_ORDBY_SYSTEM;
2366 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2367 /* default ordering is system */
2368 node->flags |= LYS_ORDBY_SYSTEM;
2369 }
2370 }
2371
Radek Krejci2a9fc652021-01-22 17:44:34 +01002372 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2373 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2374 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2375
2376 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2377 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2378
Radek Krejci9a3823e2021-01-27 20:26:46 +01002379 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002380 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002381 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002382 LY_CHECK_GOTO(ret, cleanup);
2383 }
2384
2385 /* connect any augments */
2386 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2387
2388 /* nodetype-specific part */
2389 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2390
2391 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002392 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002393 if (node->flags & LYS_MAND_TRUE) {
2394 /* inherit LYS_MAND_TRUE in parent containers */
2395 lys_compile_mandatory_parents(parent, 1);
2396 }
2397
2398 if (child_set) {
2399 /* add the new node into set */
2400 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2401 }
2402
2403 goto cleanup;
2404
2405error:
2406 lysc_node_free(ctx->ctx, node, 0);
2407cleanup:
2408 if (ret && dev_pnode) {
2409 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2410 }
2411 lysp_dev_node_free(ctx->ctx, dev_pnode);
2412 return ret;
2413}
2414
2415/**
2416 * @brief Compile parsed action's input/output node information.
2417 * @param[in] ctx Compile context
2418 * @param[in] pnode Parsed inout node.
2419 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2420 * is enriched with the inout-specific information.
2421 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2422 */
2423LY_ERR
2424lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2425{
2426 LY_ERR ret = LY_SUCCESS;
2427 struct lysp_node *child_p;
2428 uint32_t prev_options = ctx->options;
2429
2430 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2431 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2432
2433 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002434 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Radek Krejci91531e12021-02-08 19:57:54 +01002435 ctx->options |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002436
Radek Krejci01180ac2021-01-27 08:48:22 +01002437 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002438 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2439 }
2440
2441 ctx->options = prev_options;
2442
2443done:
2444 return ret;
2445}
2446
2447/**
2448 * @brief Compile parsed action node information.
2449 * @param[in] ctx Compile context
2450 * @param[in] pnode Parsed action node.
2451 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2452 * is enriched with the action-specific information.
2453 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2454 */
2455LY_ERR
2456lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2457{
2458 LY_ERR ret;
2459 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2460 struct lysc_node_action *action = (struct lysc_node_action *)node;
2461 struct lysp_node_action_inout *input, implicit_input = {
2462 .nodetype = LYS_INPUT,
2463 .name = "input",
2464 .parent = pnode,
2465 };
2466 struct lysp_node_action_inout *output, implicit_output = {
2467 .nodetype = LYS_OUTPUT,
2468 .name = "output",
2469 .parent = pnode,
2470 };
2471
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002472 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002473 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002474 if (action_p->input.nodetype == LYS_UNKNOWN) {
2475 input = &implicit_input;
2476 } else {
2477 input = &action_p->input;
2478 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002479 ret = lys_compile_node_(ctx, &input->node, &action->node, 0, lys_compile_node_action_inout, &action->input.node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002480 lysc_update_path(ctx, NULL, NULL);
2481 LY_CHECK_GOTO(ret, done);
2482
2483 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002484 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002485 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002486 output = &implicit_output;
2487 } else {
2488 output = &action_p->output;
2489 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002490 ret = lys_compile_node_(ctx, &output->node, &action->node, 0, lys_compile_node_action_inout, &action->output.node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002491 lysc_update_path(ctx, NULL, NULL);
2492 LY_CHECK_GOTO(ret, done);
2493
2494 /* do not check "must" semantics in a grouping */
2495 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2496 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2497 LY_CHECK_GOTO(ret, done);
2498 }
2499
2500done:
2501 return ret;
2502}
2503
2504/**
2505 * @brief Compile parsed action node information.
2506 * @param[in] ctx Compile context
2507 * @param[in] pnode Parsed action node.
2508 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2509 * is enriched with the action-specific information.
2510 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2511 */
2512LY_ERR
2513lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2514{
2515 LY_ERR ret = LY_SUCCESS;
2516 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2517 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2518 struct lysp_node *child_p;
2519
2520 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002521 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002522 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002523 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002524 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002525 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002526
Radek Krejci01180ac2021-01-27 08:48:22 +01002527 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002528 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002529 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002530 }
2531
Radek Krejci2a9fc652021-01-22 17:44:34 +01002532done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002533 return ret;
2534}
2535
2536/**
2537 * @brief Compile parsed container node information.
2538 * @param[in] ctx Compile context
2539 * @param[in] pnode Parsed container node.
2540 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2541 * is enriched with the container-specific information.
2542 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2543 */
2544static LY_ERR
2545lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2546{
2547 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2548 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2549 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002550 LY_ERR ret = LY_SUCCESS;
2551
2552 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002553 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002554 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002555 }
2556
2557 /* more cases when the container has meaning but is kept NP for convenience:
2558 * - when condition
2559 * - direct child action/notification
2560 */
2561
2562 LY_LIST_FOR(cont_p->child, child_p) {
2563 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2564 LY_CHECK_GOTO(ret, done);
2565 }
2566
Michal Vasko5347e3a2020-11-03 17:14:57 +01002567 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002568 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002569 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002570 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002571 LY_CHECK_GOTO(ret, done);
2572 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002573
2574 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2575 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2576 LY_CHECK_GOTO(ret, done);
2577 }
2578 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2579 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2580 LY_CHECK_GOTO(ret, done);
2581 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002582
2583done:
2584 return ret;
2585}
2586
Michal Vasko72244882021-01-12 15:21:05 +01002587/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002588 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2589 * @param[in] ctx Compile context.
2590 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2591 * @param[in] type_p Parsed type to compile.
2592 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2593 * @return LY_ERR value.
2594 */
2595static LY_ERR
2596lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2597 struct lysc_node_leaf *leaf)
2598{
2599 struct lysp_qname *dflt;
2600
Michal Vaskoa99b3572021-02-01 11:54:58 +01002601 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002602 leaf->units ? NULL : &leaf->units, &dflt));
2603
2604 /* store default value, if any */
2605 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2606 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2607 }
2608
Michal Vasko12606ee2020-11-25 17:05:11 +01002609 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002610 /* 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 +01002611 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002612 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002613 LY_ARRAY_COUNT_TYPE u;
2614 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2615 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2616 /* 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 +01002617 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002618 }
2619 }
2620 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2621 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002622 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002623 return LY_EVALID;
2624 }
2625 }
2626
2627 return LY_SUCCESS;
2628}
2629
2630/**
2631 * @brief Compile parsed leaf node information.
2632 * @param[in] ctx Compile context
2633 * @param[in] pnode Parsed leaf node.
2634 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2635 * is enriched with the leaf-specific information.
2636 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2637 */
2638static LY_ERR
2639lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2640{
2641 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2642 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002643 LY_ERR ret = LY_SUCCESS;
2644
Michal Vasko5347e3a2020-11-03 17:14:57 +01002645 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002646 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002647 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002648 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002649 LY_CHECK_GOTO(ret, done);
2650 }
2651 if (leaf_p->units) {
2652 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2653 leaf->flags |= LYS_SET_UNITS;
2654 }
2655
2656 /* compile type */
2657 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2658 LY_CHECK_GOTO(ret, done);
2659
2660 /* store/update default value */
2661 if (leaf_p->dflt.str) {
2662 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2663 leaf->flags |= LYS_SET_DFLT;
2664 }
2665
2666 /* checks */
2667 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002668 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002669 "Invalid mandatory leaf with a default value.");
2670 return LY_EVALID;
2671 }
2672
2673done:
2674 return ret;
2675}
2676
2677/**
2678 * @brief Compile parsed leaf-list node information.
2679 * @param[in] ctx Compile context
2680 * @param[in] pnode Parsed leaf-list node.
2681 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2682 * is enriched with the leaf-list-specific information.
2683 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2684 */
2685static LY_ERR
2686lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2687{
2688 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2689 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002690 LY_ERR ret = LY_SUCCESS;
2691
Michal Vasko5347e3a2020-11-03 17:14:57 +01002692 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002693 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002694 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002695 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002696 LY_CHECK_GOTO(ret, done);
2697 }
2698 if (llist_p->units) {
2699 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2700 llist->flags |= LYS_SET_UNITS;
2701 }
2702
2703 /* compile type */
2704 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2705 LY_CHECK_GOTO(ret, done);
2706
2707 /* store/update default values */
2708 if (llist_p->dflts) {
2709 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002710 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002711 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2712 return LY_EVALID;
2713 }
2714
2715 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2716 llist->flags |= LYS_SET_DFLT;
2717 }
2718
2719 llist->min = llist_p->min;
2720 if (llist->min) {
2721 llist->flags |= LYS_MAND_TRUE;
2722 }
2723 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2724
2725 /* checks */
2726 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002727 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 +02002728 return LY_EVALID;
2729 }
2730
2731 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002732 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002733 llist->min, llist->max);
2734 return LY_EVALID;
2735 }
2736
2737done:
2738 return ret;
2739}
2740
2741LY_ERR
2742lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2743 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2744 const struct lysc_node **target, uint16_t *result_flag)
2745{
2746 LY_ERR ret = LY_EVALID;
2747 const char *name, *prefix, *id;
2748 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002749 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002750 const char *nodeid_type;
2751 uint32_t getnext_extra_flag = 0;
2752 uint16_t current_nodetype = 0;
2753
2754 assert(nodeid);
2755 assert(target);
2756 assert(result_flag);
2757 *target = NULL;
2758 *result_flag = 0;
2759
2760 id = nodeid;
2761
2762 if (ctx_node) {
2763 /* descendant-schema-nodeid */
2764 nodeid_type = "descendant";
2765
2766 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002767 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002768 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002769 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002770 return LY_EVALID;
2771 }
2772 } else {
2773 /* absolute-schema-nodeid */
2774 nodeid_type = "absolute";
2775
2776 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002777 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002778 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002779 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002780 return LY_EVALID;
2781 }
2782 ++id;
2783 }
2784
2785 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2786 if (prefix) {
2787 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2788 if (!mod) {
2789 /* module must always be found */
2790 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002791 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002792 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002793 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002794 return LY_ENOTFOUND;
2795 }
2796 } else {
2797 switch (format) {
2798 case LY_PREF_SCHEMA:
2799 case LY_PREF_SCHEMA_RESOLVED:
2800 /* use the current module */
2801 mod = cur_mod;
2802 break;
2803 case LY_PREF_JSON:
2804 if (!ctx_node) {
2805 LOGINT_RET(ctx->ctx);
2806 }
2807 /* inherit the module of the previous context node */
2808 mod = ctx_node->module;
2809 break;
2810 case LY_PREF_XML:
2811 /* not really defined */
2812 LOGINT_RET(ctx->ctx);
2813 }
2814 }
2815
2816 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2817 /* move through input/output manually */
2818 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002819 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2820 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002821 return LY_ENOTFOUND;
2822 }
2823 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002824 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002825 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002826 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002827 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2828 } else {
2829 /* only input or output is valid */
2830 ctx_node = NULL;
2831 }
2832 } else {
2833 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002834 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002835 getnext_extra_flag = 0;
2836 }
2837 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002838 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2839 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002840 return LY_ENOTFOUND;
2841 }
2842 current_nodetype = ctx_node->nodetype;
2843
2844 if (current_nodetype == LYS_NOTIF) {
2845 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2846 } else if (current_nodetype == LYS_INPUT) {
2847 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2848 } else if (current_nodetype == LYS_OUTPUT) {
2849 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2850 }
2851
2852 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2853 break;
2854 }
2855 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002856 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002857 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002858 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002859 return LY_EVALID;
2860 }
2861 ++id;
2862 }
2863
2864 if (ret == LY_SUCCESS) {
2865 *target = ctx_node;
2866 if (nodetype && !(current_nodetype & nodetype)) {
2867 return LY_EDENIED;
2868 }
2869 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002870 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002871 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002872 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002873 }
2874
2875 return ret;
2876}
2877
2878/**
2879 * @brief Compile information about list's uniques.
2880 * @param[in] ctx Compile context.
2881 * @param[in] uniques Sized array list of unique statements.
2882 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2883 * @return LY_ERR value.
2884 */
2885static LY_ERR
2886lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2887{
2888 LY_ERR ret = LY_SUCCESS;
2889 struct lysc_node_leaf **key, ***unique;
2890 struct lysc_node *parent;
2891 const char *keystr, *delim;
2892 size_t len;
2893 LY_ARRAY_COUNT_TYPE v;
2894 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2895 uint16_t flags;
2896
2897 LY_ARRAY_FOR(uniques, v) {
2898 config = -1;
2899 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2900 keystr = uniques[v].str;
2901 while (keystr) {
2902 delim = strpbrk(keystr, " \t\n");
2903 if (delim) {
2904 len = delim - keystr;
2905 while (isspace(*delim)) {
2906 ++delim;
2907 }
2908 } else {
2909 len = strlen(keystr);
2910 }
2911
2912 /* unique node must be present */
2913 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002914 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002915 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2916 if (ret != LY_SUCCESS) {
2917 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002918 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002919 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002920 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002921 }
2922 return LY_EVALID;
2923 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002924 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002925 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01002926 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002927 return LY_EVALID;
2928 }
2929
2930 /* all referenced leafs must be of the same config type */
2931 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2932 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002933 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002934 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2935 return LY_EVALID;
2936 } else if ((*key)->flags & LYS_CONFIG_W) {
2937 config = 1;
2938 } else { /* LYS_CONFIG_R */
2939 config = 0;
2940 }
2941
2942 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
2943 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
2944 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002945 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002946 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
2947 return LY_EVALID;
2948 }
2949 }
2950
2951 /* check status */
2952 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2953 (*key)->flags, (*key)->module, (*key)->name));
2954
2955 /* mark leaf as unique */
2956 (*key)->flags |= LYS_UNIQUE;
2957
2958 /* next unique value in line */
2959 keystr = delim;
2960 }
2961 /* next unique definition */
2962 }
2963
2964 return LY_SUCCESS;
2965}
2966
2967/**
2968 * @brief Compile parsed list node information.
2969 * @param[in] ctx Compile context
2970 * @param[in] pnode Parsed list node.
2971 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2972 * is enriched with the list-specific information.
2973 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2974 */
2975static LY_ERR
2976lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2977{
2978 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
2979 struct lysc_node_list *list = (struct lysc_node_list *)node;
2980 struct lysp_node *child_p;
2981 struct lysc_node_leaf *key, *prev_key = NULL;
2982 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002983 const char *keystr, *delim;
2984 LY_ERR ret = LY_SUCCESS;
2985
2986 list->min = list_p->min;
2987 if (list->min) {
2988 list->flags |= LYS_MAND_TRUE;
2989 }
2990 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2991
2992 LY_LIST_FOR(list_p->child, child_p) {
2993 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
2994 }
2995
Michal Vasko5347e3a2020-11-03 17:14:57 +01002996 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002997 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002998 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002999 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003000 }
3001
3002 /* keys */
3003 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003004 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003005 return LY_EVALID;
3006 }
3007
3008 /* find all the keys (must be direct children) */
3009 keystr = list_p->key;
3010 if (!keystr) {
3011 /* keyless list */
3012 list->flags |= LYS_KEYLESS;
3013 }
3014 while (keystr) {
3015 delim = strpbrk(keystr, " \t\n");
3016 if (delim) {
3017 len = delim - keystr;
3018 while (isspace(*delim)) {
3019 ++delim;
3020 }
3021 } else {
3022 len = strlen(keystr);
3023 }
3024
3025 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003026 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3027 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003028 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003029 return LY_EVALID;
3030 }
3031 /* keys must be unique */
3032 if (key->flags & LYS_KEY) {
3033 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003034 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003035 return LY_EVALID;
3036 }
3037
Radek Krejcia6016992021-03-03 10:13:41 +01003038 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003039 /* key must have the same config flag as the list itself */
3040 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003041 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003042 return LY_EVALID;
3043 }
3044 if (ctx->pmod->version < LYS_VERSION_1_1) {
3045 /* YANG 1.0 denies key to be of empty type */
3046 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003047 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003048 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3049 return LY_EVALID;
3050 }
3051 } else {
3052 /* when and if-feature are illegal on list keys */
3053 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003054 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003055 "List's key must not have any \"when\" statement.");
3056 return LY_EVALID;
3057 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003058 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003059 }
3060
3061 /* check status */
3062 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3063 key->flags, key->module, key->name));
3064
3065 /* ignore default values of the key */
3066 if (key->dflt) {
3067 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3068 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3069 free(key->dflt);
3070 key->dflt = NULL;
3071 }
3072 /* mark leaf as key */
3073 key->flags |= LYS_KEY;
3074
3075 /* move it to the correct position */
3076 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3077 /* fix links in closest previous siblings of the key */
3078 if (key->next) {
3079 key->next->prev = key->prev;
3080 } else {
3081 /* last child */
3082 list->child->prev = key->prev;
3083 }
3084 if (key->prev->next) {
3085 key->prev->next = key->next;
3086 }
3087 /* fix links in the key */
3088 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003089 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003090 key->next = prev_key->next;
3091 } else {
3092 key->prev = list->child->prev;
3093 key->next = list->child;
3094 }
3095 /* fix links in closes future siblings of the key */
3096 if (prev_key) {
3097 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003098 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003099 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003100 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003101 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003102 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003103 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003104 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003105 }
3106 /* fix links in parent */
3107 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003108 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003109 }
3110 }
3111
3112 /* next key value */
3113 prev_key = key;
3114 keystr = delim;
3115 lysc_update_path(ctx, NULL, NULL);
3116 }
3117
3118 /* uniques */
3119 if (list_p->uniques) {
3120 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3121 }
3122
Radek Krejci2a9fc652021-01-22 17:44:34 +01003123 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3124 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3125 LY_CHECK_GOTO(ret, done);
3126 }
3127 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3128 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3129 LY_CHECK_GOTO(ret, done);
3130 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003131
3132 /* checks */
3133 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003134 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003135 list->min, list->max);
3136 return LY_EVALID;
3137 }
3138
3139done:
3140 return ret;
3141}
3142
3143/**
3144 * @brief Do some checks and set the default choice's case.
3145 *
3146 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3147 *
3148 * @param[in] ctx Compile context.
3149 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3150 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3151 * @return LY_ERR value.
3152 */
3153static LY_ERR
3154lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3155{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003156 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003157 const struct lys_module *mod;
3158 const char *prefix = NULL, *name;
3159 size_t prefix_len = 0;
3160
3161 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3162 name = strchr(dflt->str, ':');
3163 if (name) {
3164 prefix = dflt->str;
3165 prefix_len = name - prefix;
3166 ++name;
3167 } else {
3168 name = dflt->str;
3169 }
3170 if (prefix) {
3171 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
3172 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003173 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003174 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003175 return LY_EVALID;
3176 }
3177 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003178 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003179 }
3180
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003181 ch->dflt = (struct lysc_node_case *)lys_find_child(&ch->node, mod, name, 0, LYS_CASE, LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003182 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003183 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003184 "Default case \"%s\" not found.", dflt->str);
3185 return LY_EVALID;
3186 }
3187
3188 /* no mandatory nodes directly under the default case */
3189 LY_LIST_FOR(ch->dflt->child, iter) {
3190 if (iter->parent != (struct lysc_node *)ch->dflt) {
3191 break;
3192 }
3193 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003194 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003195 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3196 return LY_EVALID;
3197 }
3198 }
3199
3200 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003201 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003202 return LY_EVALID;
3203 }
3204
3205 ch->dflt->flags |= LYS_SET_DFLT;
3206 return LY_SUCCESS;
3207}
3208
3209LY_ERR
3210lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3211 struct ly_set *child_set)
3212{
3213 LY_ERR ret = LY_SUCCESS;
3214 struct lysp_node *child_p_next = child_p->next;
3215 struct lysp_node_case *cs_p;
3216
3217 if (child_p->nodetype == LYS_CASE) {
3218 /* standard case under choice */
3219 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3220 } else {
3221 /* we need the implicit case first, so create a fake parsed case */
3222 cs_p = calloc(1, sizeof *cs_p);
3223 cs_p->nodetype = LYS_CASE;
3224 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3225 cs_p->child = child_p;
3226
3227 /* make the child the only case child */
3228 child_p->next = NULL;
3229
3230 /* compile it normally */
3231 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3232
3233free_fake_node:
3234 /* free the fake parsed node and correct pointers back */
3235 cs_p->child = NULL;
3236 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3237 child_p->next = child_p_next;
3238 }
3239
3240 return ret;
3241}
3242
3243/**
3244 * @brief Compile parsed choice node information.
3245 *
3246 * @param[in] ctx Compile context
3247 * @param[in] pnode Parsed choice node.
3248 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3249 * is enriched with the choice-specific information.
3250 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3251 */
3252static LY_ERR
3253lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3254{
3255 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3256 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3257 struct lysp_node *child_p;
3258 LY_ERR ret = LY_SUCCESS;
3259
3260 assert(node->nodetype == LYS_CHOICE);
3261
3262 LY_LIST_FOR(ch_p->child, child_p) {
3263 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3264 }
3265
3266 /* default branch */
3267 if (ch_p->dflt.str) {
3268 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3269 }
3270
3271 return ret;
3272}
3273
3274/**
3275 * @brief Compile parsed anydata or anyxml node information.
3276 * @param[in] ctx Compile context
3277 * @param[in] pnode Parsed anydata or anyxml node.
3278 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3279 * is enriched with the any-specific information.
3280 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3281 */
3282static LY_ERR
3283lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3284{
3285 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3286 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003287 LY_ERR ret = LY_SUCCESS;
3288
Michal Vasko5347e3a2020-11-03 17:14:57 +01003289 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003290 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003291 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003292 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003293 LY_CHECK_GOTO(ret, done);
3294 }
3295
Radek Krejci91531e12021-02-08 19:57:54 +01003296 if (any->flags & LYS_CONFIG_W) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003297 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3298 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3299 }
3300done:
3301 return ret;
3302}
3303
3304/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003305 * @brief Prepare the case structure in choice node for the new data node.
3306 *
3307 * 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
3308 * created in the choice when the first child was processed.
3309 *
3310 * @param[in] ctx Compile context.
3311 * @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,
3312 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3313 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3314 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3315 * @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,
3316 * it is linked from the case structure only in case it is its first child.
3317 */
3318static LY_ERR
3319lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3320{
3321 struct lysp_node *child_p;
3322 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3323
3324 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3325 /* we have to add an implicit case node into the parent choice */
3326 } else if (pnode->nodetype == LYS_CASE) {
3327 /* explicit parent case */
3328 LY_LIST_FOR(cs_p->child, child_p) {
3329 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3330 }
3331 } else {
3332 LOGINT_RET(ctx->ctx);
3333 }
3334
3335 return LY_SUCCESS;
3336}
3337
3338void
3339lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3340{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003341 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003342
3343 if (add) { /* set flag */
3344 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3345 parent = parent->parent) {
3346 parent->flags |= LYS_MAND_TRUE;
3347 }
3348 } else { /* unset flag */
3349 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003350 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003351 if (iter->flags & LYS_MAND_TRUE) {
3352 /* there is another mandatory node */
3353 return;
3354 }
3355 }
3356 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3357 parent->flags &= ~LYS_MAND_TRUE;
3358 }
3359 }
3360}
3361
3362/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003363 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003364 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003365 * @param[in] name Name of the grouping to find,
3366 * @return NULL when there is no grouping with the specified name
3367 * @return Pointer to the grouping of the specified @p name.
3368 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003369static struct lysp_node_grp *
3370match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003371{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003372 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003373
Radek Krejci2a9fc652021-01-22 17:44:34 +01003374 LY_LIST_FOR(grps, grp) {
3375 if (!strcmp(grp->name, name)) {
3376 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003377 }
3378 }
3379
3380 return NULL;
3381}
3382
3383/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003384 * @brief Find grouping for a uses.
3385 *
3386 * @param[in] ctx Compile context.
3387 * @param[in] uses_p Parsed uses node.
3388 * @param[out] gpr_p Found grouping on success.
3389 * @param[out] grp_pmod Module of @p grp_p on success.
3390 * @return LY_ERR value.
3391 */
3392static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003393lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_node_grp **grp_p,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003394 struct lysp_module **grp_pmod)
3395{
3396 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003397 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003398 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003399 const char *id, *name, *prefix, *local_pref;
3400 size_t prefix_len, name_len;
3401 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003402 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003403
3404 *grp_p = NULL;
3405 *grp_pmod = NULL;
3406
3407 /* search for the grouping definition */
3408 id = uses_p->name;
3409 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3410 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3411 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3412 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003413 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003414 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003415 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3416 found = ctx->pmod;
3417 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003418 }
3419 }
3420 } else {
3421 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003422 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003423 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003424 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003425 "Invalid prefix used for grouping reference.", uses_p->name);
3426 return LY_EVALID;
3427 }
3428 pmod = mod->parsed;
3429 }
3430
3431 if (!found) {
3432 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003433 if ((grp = match_grouping(pmod->groupings, name))) {
3434 found = pmod;
3435 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003436 /* ... and all the submodules */
3437 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003438 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3439 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003440 break;
3441 }
3442 }
3443 }
3444 }
3445 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003446 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003447 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3448 return LY_EVALID;
3449 }
3450
3451 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3452 /* remember that the grouping is instantiated to avoid its standalone validation */
3453 grp->flags |= LYS_USED_GRP;
3454 }
3455
3456 *grp_p = grp;
3457 *grp_pmod = found;
3458 return LY_SUCCESS;
3459}
3460
3461/**
3462 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3463 * If present, also apply uses's modificators.
3464 *
3465 * @param[in] ctx Compile context
3466 * @param[in] uses_p Parsed uses schema node.
3467 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3468 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3469 * the compile context.
3470 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3471 */
3472static LY_ERR
3473lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3474{
3475 struct lysp_node *pnode;
3476 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003477 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003478 uint32_t i, grp_stack_count;
3479 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3480 LY_ERR ret = LY_SUCCESS;
3481 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003482 struct ly_set uses_child_set = {0};
3483
3484 /* find the referenced grouping */
3485 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3486
3487 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3488 grp_stack_count = ctx->groupings.count;
3489 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3490 if (grp_stack_count == ctx->groupings.count) {
3491 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003492 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003493 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3494 return LY_EVALID;
3495 }
3496
3497 /* check status */
3498 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3499 LY_CHECK_GOTO(ret, cleanup);
3500
3501 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3502 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3503 LY_CHECK_GOTO(ret, cleanup);
3504
3505 /* switch context's parsed module being processed */
3506 ctx->pmod = grp_mod;
3507
3508 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003509 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003510 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3511 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 +02003512 LY_CHECK_GOTO(ret, cleanup);
3513 }
3514
3515 if (child_set) {
3516 /* add these children to our compiled child_set as well since uses is a schema-only node */
3517 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3518 }
3519
3520 if (uses_p->when) {
3521 /* pass uses's when to all the data children */
3522 for (i = 0; i < uses_child_set.count; ++i) {
3523 child = uses_child_set.snodes[i];
3524
Michal Vasko72244882021-01-12 15:21:05 +01003525 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003526 LY_CHECK_GOTO(ret, cleanup);
3527 }
3528 }
3529
3530 /* compile actions */
3531 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003532 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003533 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3534 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003535 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003536 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3537 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003538 ret = LY_EVALID;
3539 goto cleanup;
3540 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003541 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3542 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3543 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3544 LY_CHECK_GOTO(ret, cleanup);
3545 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003546
3547 if (uses_p->when) {
3548 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003549 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3550 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003551 LY_CHECK_GOTO(ret, cleanup);
3552 }
3553 }
3554 }
3555
3556 /* compile notifications */
3557 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003558 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003559 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3560 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003561 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003562 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3563 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003564 ret = LY_EVALID;
3565 goto cleanup;
3566 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003567
3568 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3569 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3570 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3571 LY_CHECK_GOTO(ret, cleanup);
3572 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003573
3574 if (uses_p->when) {
3575 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003576 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3577 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003578 LY_CHECK_GOTO(ret, cleanup);
3579 }
3580 }
3581 }
3582
3583 /* check that all augments were applied */
3584 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003585 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3586 /* augment of some parent uses, irrelevant now */
3587 continue;
3588 }
3589
3590 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003591 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3592 ret = LY_ENOTFOUND;
3593 }
3594 LY_CHECK_GOTO(ret, cleanup);
3595
3596 /* check that all refines were applied */
3597 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003598 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3599 /* refine of some paretn uses, irrelevant now */
3600 continue;
3601 }
3602
3603 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003604 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3605 ret = LY_ENOTFOUND;
3606 }
3607 LY_CHECK_GOTO(ret, cleanup);
3608
3609cleanup:
3610 /* reload previous context's parsed module being processed */
3611 ctx->pmod = mod_old;
3612
3613 /* remove the grouping from the stack for circular groupings dependency check */
3614 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3615 assert(ctx->groupings.count == grp_stack_count);
3616
3617 ly_set_erase(&uses_child_set, NULL);
3618 return ret;
3619}
3620
3621static int
3622lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3623{
3624 struct lysp_node *iter;
3625 int len = 0;
3626
3627 *path = NULL;
3628 for (iter = node; iter && len >= 0; iter = iter->parent) {
3629 char *s = *path;
3630 char *id;
3631
3632 switch (iter->nodetype) {
3633 case LYS_USES:
3634 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3635 break;
3636 case LYS_GROUPING:
3637 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3638 break;
3639 case LYS_AUGMENT:
3640 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3641 break;
3642 default:
3643 id = strdup(iter->name);
3644 break;
3645 }
3646
3647 if (!iter->parent) {
3648 /* print prefix */
3649 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3650 } else {
3651 /* prefix is the same as in parent */
3652 len = asprintf(path, "/%s%s", id, s ? s : "");
3653 }
3654 free(s);
3655 free(id);
3656 }
3657
3658 if (len < 0) {
3659 free(*path);
3660 *path = NULL;
3661 } else if (len == 0) {
3662 *path = strdup("/");
3663 len = 1;
3664 }
3665 return len;
3666}
3667
3668LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003669lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003670{
3671 LY_ERR ret;
3672 char *path;
3673 int len;
3674
3675 struct lysp_node_uses fake_uses = {
3676 .parent = pnode,
3677 .nodetype = LYS_USES,
3678 .flags = 0, .next = NULL,
3679 .name = grp->name,
3680 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3681 .refines = NULL, .augments = NULL
3682 };
3683 struct lysc_node_container fake_container = {
3684 .nodetype = LYS_CONTAINER,
3685 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3686 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003687 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003688 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003689 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003690 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003691 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3692 };
3693
3694 if (grp->parent) {
3695 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3696 }
3697
3698 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3699 if (len < 0) {
3700 LOGMEM(ctx->ctx);
3701 return LY_EMEM;
3702 }
3703 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3704 ctx->path_len = (uint32_t)len;
3705 free(path);
3706
3707 lysc_update_path(ctx, NULL, "{grouping}");
3708 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003709 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003710 lysc_update_path(ctx, NULL, NULL);
3711 lysc_update_path(ctx, NULL, NULL);
3712
3713 ctx->path_len = 1;
3714 ctx->path[1] = '\0';
3715
3716 /* cleanup */
3717 lysc_node_container_free(ctx->ctx, &fake_container);
3718
3719 return ret;
3720}
3721
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003722LY_ERR
3723lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3724 struct ly_set *child_set)
3725{
3726 LY_ERR ret = LY_SUCCESS;
3727 struct lysc_node *node = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003728 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003729
3730 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3731
3732 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003733 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003734 } else {
3735 lysc_update_path(ctx, NULL, "{uses}");
3736 lysc_update_path(ctx, NULL, pnode->name);
3737 }
3738
3739 switch (pnode->nodetype) {
3740 case LYS_CONTAINER:
3741 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3742 node_compile_spec = lys_compile_node_container;
3743 break;
3744 case LYS_LEAF:
3745 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3746 node_compile_spec = lys_compile_node_leaf;
3747 break;
3748 case LYS_LIST:
3749 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3750 node_compile_spec = lys_compile_node_list;
3751 break;
3752 case LYS_LEAFLIST:
3753 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3754 node_compile_spec = lys_compile_node_leaflist;
3755 break;
3756 case LYS_CHOICE:
3757 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3758 node_compile_spec = lys_compile_node_choice;
3759 break;
3760 case LYS_CASE:
3761 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3762 node_compile_spec = lys_compile_node_case;
3763 break;
3764 case LYS_ANYXML:
3765 case LYS_ANYDATA:
3766 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3767 node_compile_spec = lys_compile_node_any;
3768 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003769 case LYS_RPC:
3770 case LYS_ACTION:
Radek Krejci91531e12021-02-08 19:57:54 +01003771 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003772 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3773 "Action \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003774 (ctx->options & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003775 return LY_EVALID;
3776 }
3777 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3778 node_compile_spec = lys_compile_node_action;
Radek Krejci91531e12021-02-08 19:57:54 +01003779 ctx->options |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003780 break;
3781 case LYS_NOTIF:
Radek Krejci91531e12021-02-08 19:57:54 +01003782 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003783 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3784 "Notification \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003785 (ctx->options & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003786 return LY_EVALID;
3787 }
3788 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3789 node_compile_spec = lys_compile_node_notif;
3790 ctx->options |= LYS_COMPILE_NOTIFICATION;
3791 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003792 case LYS_USES:
3793 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3794 lysc_update_path(ctx, NULL, NULL);
3795 lysc_update_path(ctx, NULL, NULL);
3796 return ret;
3797 default:
3798 LOGINT(ctx->ctx);
3799 return LY_EINT;
3800 }
3801 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3802
Radek Krejci2a9fc652021-01-22 17:44:34 +01003803 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003804
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003805 ctx->options = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003806 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003807 return ret;
3808}