blob: 1092aee082c87a2e32592425759998025d9c3f28 [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"
Radek Krejci0aa1f702021-04-01 16:16:19 +020032#include "plugins.h"
Radek Krejci77114102021-03-10 15:21:57 +010033#include "plugins_exts_compile.h"
Radek Krejci04699f02021-03-22 21:50:29 +010034#include "plugins_internal.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020035#include "plugins_types.h"
36#include "schema_compile.h"
37#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010038#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020039#include "set.h"
40#include "tree.h"
41#include "tree_data.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020043#include "tree_schema.h"
44#include "tree_schema_internal.h"
45#include "xpath.h"
46
47static struct lysc_ext_instance *
48lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
49{
50 /* TODO - extensions, increase refcount */
51 (void) ctx;
52 (void) orig;
53 return NULL;
54}
55
56/**
57 * @brief Add/replace a leaf default value in unres.
58 * Can also be used for a single leaf-list default value.
59 *
60 * @param[in] ctx Compile context.
61 * @param[in] leaf Leaf with the default value.
62 * @param[in] dflt Default value to use.
63 * @return LY_ERR value.
64 */
65static LY_ERR
66lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
67{
68 struct lysc_unres_dflt *r = NULL;
69 uint32_t i;
70
Michal Vasko12606ee2020-11-25 17:05:11 +010071 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010072 return LY_SUCCESS;
73 }
74
Michal Vasko405cc9e2020-12-01 12:01:27 +010075 for (i = 0; i < ctx->unres->dflts.count; ++i) {
76 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020077 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010078 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020079 lysp_qname_free(ctx->ctx, r->dflt);
80 free(r->dflt);
81 break;
82 }
83 }
84 if (!r) {
85 /* add new unres item */
86 r = calloc(1, sizeof *r);
87 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
88 r->leaf = leaf;
89
Michal Vasko405cc9e2020-12-01 12:01:27 +010090 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020091 }
92
93 r->dflt = malloc(sizeof *r->dflt);
94 LY_CHECK_GOTO(!r->dflt, error);
95 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
96
97 return LY_SUCCESS;
98
99error:
100 free(r->dflt);
101 LOGMEM(ctx->ctx);
102 return LY_EMEM;
103}
104
105/**
106 * @brief Add/replace a leaf-list default value(s) in unres.
107 *
108 * @param[in] ctx Compile context.
109 * @param[in] llist Leaf-list with the default value.
110 * @param[in] dflts Sized array of the default values.
111 * @return LY_ERR value.
112 */
113static LY_ERR
114lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
115{
116 struct lysc_unres_dflt *r = NULL;
117 uint32_t i;
118
Michal Vasko12606ee2020-11-25 17:05:11 +0100119 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100120 return LY_SUCCESS;
121 }
122
Michal Vasko405cc9e2020-12-01 12:01:27 +0100123 for (i = 0; i < ctx->unres->dflts.count; ++i) {
124 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200125 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100126 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200127 lysp_qname_free(ctx->ctx, r->dflt);
128 free(r->dflt);
129 r->dflt = NULL;
130 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
131 r->dflts = NULL;
132 break;
133 }
134 }
135 if (!r) {
136 r = calloc(1, sizeof *r);
137 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
138 r->llist = llist;
139
Michal Vasko405cc9e2020-12-01 12:01:27 +0100140 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200141 }
142
143 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
144
145 return LY_SUCCESS;
146}
147
148/**
149 * @brief Duplicate the compiled pattern structure.
150 *
151 * Instead of duplicating memory, the reference counter in the @p orig is increased.
152 *
153 * @param[in] orig The pattern structure to duplicate.
154 * @return The duplicated structure to use.
155 */
156static struct lysc_pattern *
157lysc_pattern_dup(struct lysc_pattern *orig)
158{
159 ++orig->refcount;
160 return orig;
161}
162
163/**
164 * @brief Duplicate the array of compiled patterns.
165 *
166 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
167 *
168 * @param[in] ctx Libyang context for logging.
169 * @param[in] orig The patterns sized array to duplicate.
170 * @return New sized array as a copy of @p orig.
171 * @return NULL in case of memory allocation error.
172 */
173static struct lysc_pattern **
174lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
175{
176 struct lysc_pattern **dup = NULL;
177 LY_ARRAY_COUNT_TYPE u;
178
179 assert(orig);
180
181 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
182 LY_ARRAY_FOR(orig, u) {
183 dup[u] = lysc_pattern_dup(orig[u]);
184 LY_ARRAY_INCREMENT(dup);
185 }
186 return dup;
187}
188
189/**
190 * @brief Duplicate compiled range structure.
191 *
192 * @param[in] ctx Libyang context for logging.
193 * @param[in] orig The range structure to be duplicated.
194 * @return New compiled range structure as a copy of @p orig.
195 * @return NULL in case of memory allocation error.
196 */
197static struct lysc_range *
198lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
199{
200 struct lysc_range *dup;
201 LY_ERR ret;
202
203 assert(orig);
204
205 dup = calloc(1, sizeof *dup);
206 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
207 if (orig->parts) {
208 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100209 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200210 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
211 }
212 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
213 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
214 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
215
216 return dup;
217cleanup:
218 free(dup);
219 (void) ret; /* set but not used due to the return type */
220 return NULL;
221}
222
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200223/**
224 * @brief Compile information from the when statement
225 *
226 * @param[in] ctx Compile context.
227 * @param[in] when_p Parsed when structure.
228 * @param[in] flags Flags of the parsed node with the when statement.
229 * @param[in] ctx_node Context node for the when statement.
230 * @param[out] when Pointer where to store pointer to the created compiled when structure.
231 * @return LY_ERR value.
232 */
233static LY_ERR
234lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
235 struct lysc_when **when)
236{
237 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200238 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200239
240 *when = calloc(1, sizeof **when);
241 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
242 (*when)->refcount = 1;
243 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200244 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200245 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200246 (*when)->context = (struct lysc_node *)ctx_node;
247 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
248 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100249 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200250 (*when)->flags = flags & LYS_STATUS_MASK;
251
252done:
253 return ret;
254}
255
256LY_ERR
257lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
258 struct lysc_node *node, struct lysc_when **when_c)
259{
260 struct lysc_when **new_when, ***node_when;
261
262 assert(when_p);
263
264 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100265 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200266
267 /* create new when pointer */
268 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
269 if (!when_c || !(*when_c)) {
270 /* compile when */
271 LY_CHECK_RET(lys_compile_when_(ctx, when_p, flags, ctx_node, new_when));
272
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200273 /* remember the compiled when for sharing */
274 if (when_c) {
275 *when_c = *new_when;
276 }
277 } else {
278 /* use the previously compiled when */
279 ++(*when_c)->refcount;
280 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100281 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200282
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100283 if (!(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
284 /* do not check "when" semantics in a grouping, but repeat the check for different node because
285 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100286 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200287 }
288
289 return LY_SUCCESS;
290}
291
292/**
293 * @brief Compile information from the must statement
294 * @param[in] ctx Compile context.
295 * @param[in] must_p The parsed must statement structure.
296 * @param[in,out] must Prepared (empty) compiled must structure to fill.
297 * @return LY_ERR value.
298 */
299static LY_ERR
300lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
301{
302 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200303 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200304
305 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200306 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
Radek Krejci8df109d2021-04-23 12:19:08 +0200307 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200308 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
309 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
310 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
311 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100312 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200313
314done:
315 return ret;
316}
317
318/**
319 * @brief Validate and normalize numeric value from a range definition.
320 * @param[in] ctx Compile context.
321 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
322 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
323 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
324 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
325 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
326 * @param[in] value String value of the range boundary.
327 * @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.
328 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
329 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
330 */
331static LY_ERR
332range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
333 size_t *len, char **valcopy)
334{
335 size_t fraction = 0, size;
336
337 *len = 0;
338
339 assert(value);
340 /* parse value */
341 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
342 return LY_EVALID;
343 }
344
345 if ((value[*len] == '-') || (value[*len] == '+')) {
346 ++(*len);
347 }
348
349 while (isdigit(value[*len])) {
350 ++(*len);
351 }
352
353 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
354 if (basetype == LY_TYPE_DEC64) {
355 goto decimal;
356 } else {
357 *valcopy = strndup(value, *len);
358 return LY_SUCCESS;
359 }
360 }
361 fraction = *len;
362
363 ++(*len);
364 while (isdigit(value[*len])) {
365 ++(*len);
366 }
367
368 if (basetype == LY_TYPE_DEC64) {
369decimal:
370 assert(frdigits);
371 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100372 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200373 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100374 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200375 return LY_EINVAL;
376 }
377 if (fraction) {
378 size = (*len) + (frdigits - ((*len) - 1 - fraction));
379 } else {
380 size = (*len) + frdigits + 1;
381 }
382 *valcopy = malloc(size * sizeof **valcopy);
383 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
384
385 (*valcopy)[size - 1] = '\0';
386 if (fraction) {
387 memcpy(&(*valcopy)[0], &value[0], fraction);
388 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
389 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
390 } else {
391 memcpy(&(*valcopy)[0], &value[0], *len);
392 memset(&(*valcopy)[*len], '0', frdigits);
393 }
394 }
395 return LY_SUCCESS;
396}
397
398/**
399 * @brief Check that values in range are in ascendant order.
400 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
401 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
402 * max can be also equal.
403 * @param[in] value Current value to check.
404 * @param[in] prev_value The last seen value.
405 * @return LY_SUCCESS or LY_EEXIST for invalid order.
406 */
407static LY_ERR
408range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
409{
410 if (unsigned_value) {
411 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
412 return LY_EEXIST;
413 }
414 } else {
415 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
416 return LY_EEXIST;
417 }
418 }
419 return LY_SUCCESS;
420}
421
422/**
423 * @brief Set min/max value of the range part.
424 * @param[in] ctx Compile context.
425 * @param[in] part Range part structure to fill.
426 * @param[in] max Flag to distinguish if storing min or max value.
427 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
428 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
429 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
430 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
431 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
432 * @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.
433 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
434 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
435 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
436 * frdigits value), LY_EMEM.
437 */
438static LY_ERR
439range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
440 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
441{
442 LY_ERR ret = LY_SUCCESS;
443 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100444 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200445
446 if (value) {
447 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
448 LY_CHECK_GOTO(ret, finalize);
449 }
450 if (!valcopy && base_range) {
451 if (max) {
452 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
453 } else {
454 part->min_64 = base_range->parts[0].min_64;
455 }
456 if (!first) {
457 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
458 }
459 goto finalize;
460 }
461
462 switch (basetype) {
463 case LY_TYPE_INT8: /* range */
464 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100465 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 +0200466 } else if (max) {
467 part->max_64 = INT64_C(127);
468 } else {
469 part->min_64 = INT64_C(-128);
470 }
471 if (!ret && !first) {
472 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
473 }
474 break;
475 case LY_TYPE_INT16: /* range */
476 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100477 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
478 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200479 } else if (max) {
480 part->max_64 = INT64_C(32767);
481 } else {
482 part->min_64 = INT64_C(-32768);
483 }
484 if (!ret && !first) {
485 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
486 }
487 break;
488 case LY_TYPE_INT32: /* range */
489 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100490 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
491 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200492 } else if (max) {
493 part->max_64 = INT64_C(2147483647);
494 } else {
495 part->min_64 = INT64_C(-2147483648);
496 }
497 if (!ret && !first) {
498 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
499 }
500 break;
501 case LY_TYPE_INT64: /* range */
502 case LY_TYPE_DEC64: /* range */
503 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100504 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
505 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200506 } else if (max) {
507 part->max_64 = INT64_C(9223372036854775807);
508 } else {
509 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
510 }
511 if (!ret && !first) {
512 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
513 }
514 break;
515 case LY_TYPE_UINT8: /* range */
516 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100517 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 +0200518 } else if (max) {
519 part->max_u64 = UINT64_C(255);
520 } else {
521 part->min_u64 = UINT64_C(0);
522 }
523 if (!ret && !first) {
524 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
525 }
526 break;
527 case LY_TYPE_UINT16: /* range */
528 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100529 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 +0200530 } else if (max) {
531 part->max_u64 = UINT64_C(65535);
532 } else {
533 part->min_u64 = UINT64_C(0);
534 }
535 if (!ret && !first) {
536 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
537 }
538 break;
539 case LY_TYPE_UINT32: /* range */
540 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100541 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
542 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200543 } else if (max) {
544 part->max_u64 = UINT64_C(4294967295);
545 } else {
546 part->min_u64 = UINT64_C(0);
547 }
548 if (!ret && !first) {
549 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
550 }
551 break;
552 case LY_TYPE_UINT64: /* range */
553 case LY_TYPE_STRING: /* length */
554 case LY_TYPE_BINARY: /* length */
555 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100556 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
557 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200558 } else if (max) {
559 part->max_u64 = UINT64_C(18446744073709551615);
560 } else {
561 part->min_u64 = UINT64_C(0);
562 }
563 if (!ret && !first) {
564 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
565 }
566 break;
567 default:
568 LOGINT(ctx->ctx);
569 ret = LY_EINT;
570 }
571
572finalize:
573 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100574 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200575 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
576 length_restr ? "length" : "range", valcopy ? valcopy : *value);
577 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100578 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200579 "Invalid %s restriction - invalid value \"%s\".",
580 length_restr ? "length" : "range", valcopy ? valcopy : *value);
581 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100582 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200583 "Invalid %s restriction - values are not in ascending order (%s).",
584 length_restr ? "length" : "range",
585 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
586 } else if (!ret && value) {
587 *value = *value + len;
588 }
589 free(valcopy);
590 return ret;
591}
592
593/**
594 * @brief Compile the parsed range restriction.
595 * @param[in] ctx Compile context.
596 * @param[in] range_p Parsed range structure to compile.
597 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
598 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
599 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
600 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
601 * range restriction must be more restrictive than the base_range.
602 * @param[in,out] range Pointer to the created current range structure.
603 * @return LY_ERR value.
604 */
605static LY_ERR
606lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
607 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
608{
aPiecek1c4da362021-04-29 14:26:34 +0200609 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200610 const char *expr;
611 struct lysc_range_part *parts = NULL, *part;
612 ly_bool range_expected = 0, uns;
613 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
614
615 assert(range);
616 assert(range_p);
617
618 expr = range_p->arg.str;
619 while (1) {
620 if (isspace(*expr)) {
621 ++expr;
622 } else if (*expr == '\0') {
623 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100624 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200625 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
626 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200627 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200628 goto cleanup;
629 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100630 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200631 "Invalid %s restriction - unexpected end of the expression (%s).",
632 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200633 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200634 goto cleanup;
635 }
636 parts_done++;
637 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100638 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200639 if (parts) {
640 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100641 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200642 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100643 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200644 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200645 goto cleanup;
646 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100647 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200648
649 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200650 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200651 part->max_64 = part->min_64;
652 } else if (*expr == '|') {
653 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100654 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200655 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200656 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200657 goto cleanup;
658 }
659 expr++;
660 parts_done++;
661 /* process next part of the expression */
662 } else if (!strncmp(expr, "..", 2)) {
663 expr += 2;
664 while (isspace(*expr)) {
665 expr++;
666 }
667 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100668 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200669 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200670 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200671 goto cleanup;
672 }
673 /* continue expecting the upper boundary */
674 range_expected = 1;
675 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
676 /* number */
677 if (range_expected) {
678 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200679 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200680 range_expected = 0;
681 } else {
682 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200683 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200684 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
685 part->max_64 = part->min_64;
686 }
687
688 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100689 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
690 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200691 while (isspace(*expr)) {
692 expr++;
693 }
694 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100695 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200696 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200697 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200698 goto cleanup;
699 }
700 if (range_expected) {
701 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200702 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200703 range_expected = 0;
704 } else {
705 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200706 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200707 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
708 part->min_64 = part->max_64;
709 }
710 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100711 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200712 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200713 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200714 goto cleanup;
715 }
716 }
717
718 /* check with the previous range/length restriction */
719 if (base_range) {
720 switch (basetype) {
721 case LY_TYPE_BINARY:
722 case LY_TYPE_UINT8:
723 case LY_TYPE_UINT16:
724 case LY_TYPE_UINT32:
725 case LY_TYPE_UINT64:
726 case LY_TYPE_STRING:
727 uns = 1;
728 break;
729 case LY_TYPE_DEC64:
730 case LY_TYPE_INT8:
731 case LY_TYPE_INT16:
732 case LY_TYPE_INT32:
733 case LY_TYPE_INT64:
734 uns = 0;
735 break;
736 default:
737 LOGINT(ctx->ctx);
738 ret = LY_EINT;
739 goto cleanup;
740 }
741 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
742 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
743 goto baseerror;
744 }
745 /* current lower bound is not lower than the base */
746 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
747 /* base has single value */
748 if (base_range->parts[v].min_64 == parts[u].min_64) {
749 /* both lower bounds are the same */
750 if (parts[u].min_64 != parts[u].max_64) {
751 /* current continues with a range */
752 goto baseerror;
753 } else {
754 /* equal single values, move both forward */
755 ++v;
756 continue;
757 }
758 } else {
759 /* base is single value lower than current range, so the
760 * value from base range is removed in the current,
761 * move only base and repeat checking */
762 ++v;
763 --u;
764 continue;
765 }
766 } else {
767 /* base is the range */
768 if (parts[u].min_64 == parts[u].max_64) {
769 /* current is a single value */
770 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
771 /* current is behind the base range, so base range is omitted,
772 * move the base and keep the current for further check */
773 ++v;
774 --u;
775 } /* else it is within the base range, so move the current, but keep the base */
776 continue;
777 } else {
778 /* both are ranges - check the higher bound, the lower was already checked */
779 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
780 /* higher bound is higher than the current higher bound */
781 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
782 /* but the current lower bound is also higher, so the base range is omitted,
783 * continue with the same current, but move the base */
784 --u;
785 ++v;
786 continue;
787 }
788 /* current range starts within the base range but end behind it */
789 goto baseerror;
790 } else {
791 /* current range is smaller than the base,
792 * move current, but stay with the base */
793 continue;
794 }
795 }
796 }
797 }
798 if (u != parts_done) {
799baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100800 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200801 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
802 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200803 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200804 goto cleanup;
805 }
806 }
807
808 if (!(*range)) {
809 *range = calloc(1, sizeof **range);
810 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
811 }
812
813 /* we rewrite the following values as the types chain is being processed */
814 if (range_p->eapptag) {
815 lydict_remove(ctx->ctx, (*range)->eapptag);
816 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
817 }
818 if (range_p->emsg) {
819 lydict_remove(ctx->ctx, (*range)->emsg);
820 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
821 }
822 if (range_p->dsc) {
823 lydict_remove(ctx->ctx, (*range)->dsc);
824 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
825 }
826 if (range_p->ref) {
827 lydict_remove(ctx->ctx, (*range)->ref);
828 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
829 }
830 /* extensions are taken only from the last range by the caller */
831
832 (*range)->parts = parts;
833 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200834cleanup:
835 LY_ARRAY_FREE(parts);
836
837 return ret;
838}
839
840LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100841lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200842{
843 size_t idx, idx2, start, end, size, brack;
844 char *perl_regex, *ptr;
Christian Hoppsdafed222021-03-22 13:02:42 -0400845 int err_code, compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200846 const char *orig_ptr;
847 PCRE2_SIZE err_offset;
848 pcre2_code *code_local;
849
850#define URANGE_LEN 19
851 char *ublock2urange[][2] = {
852 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
853 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
854 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
855 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
856 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
857 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
858 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
859 {"Greek", "[\\x{0370}-\\x{03FF}]"},
860 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
861 {"Armenian", "[\\x{0530}-\\x{058F}]"},
862 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
863 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
864 {"Syriac", "[\\x{0700}-\\x{074F}]"},
865 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
866 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
867 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
868 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
869 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
870 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
871 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
872 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
873 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
874 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
875 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
876 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
877 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
878 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
879 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
880 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
881 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
882 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
883 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
884 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
885 {"Ogham", "[\\x{1680}-\\x{169F}]"},
886 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
887 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
888 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
889 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
890 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
891 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
892 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
893 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
894 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
895 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
896 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
897 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
898 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
899 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
900 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
901 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
902 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
903 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
904 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
905 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
906 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
907 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
908 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
909 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
910 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
911 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
912 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
913 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
914 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
915 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
916 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
917 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
918 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
919 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
920 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
921 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
922 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
923 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
924 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
925 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
926 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
927 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
928 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
929 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
930 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
931 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
932 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
933 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
934 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100935 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200936 {NULL, NULL}
937 };
938
939 /* adjust the expression to a Perl equivalent
940 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
941
942 /* allocate space for the transformed pattern */
943 size = strlen(pattern) + 1;
Christian Hoppsdafed222021-03-22 13:02:42 -0400944 compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
945#ifdef PCRE2_ENDANCHORED
946 compile_opts |= PCRE2_ENDANCHORED;
947#else
948 /* add space for trailing $ anchor */
949 size++;
950#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200951 perl_regex = malloc(size);
952 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
953 perl_regex[0] = '\0';
954
955 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
956 brack = 0;
957 idx = 0;
958 orig_ptr = pattern;
959 while (orig_ptr[0]) {
960 switch (orig_ptr[0]) {
961 case '$':
962 case '^':
963 if (!brack) {
964 /* make space for the extra character */
965 ++size;
966 perl_regex = ly_realloc(perl_regex, size);
967 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
968
969 /* print escape slash */
970 perl_regex[idx] = '\\';
971 ++idx;
972 }
973 break;
974 case '[':
975 /* must not be escaped */
976 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
977 ++brack;
978 }
979 break;
980 case ']':
981 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
982 /* pattern was checked and compiled already */
983 assert(brack);
984 --brack;
985 }
986 break;
987 default:
988 break;
989 }
990
991 /* copy char */
992 perl_regex[idx] = orig_ptr[0];
993
994 ++idx;
995 ++orig_ptr;
996 }
Christian Hoppsdafed222021-03-22 13:02:42 -0400997#ifndef PCRE2_ENDANCHORED
998 /* anchor match to end of subject */
999 perl_regex[idx++] = '$';
1000#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001001 perl_regex[idx] = '\0';
1002
1003 /* substitute Unicode Character Blocks with exact Character Ranges */
1004 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1005 start = ptr - perl_regex;
1006
1007 ptr = strchr(ptr, '}');
1008 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001009 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001010 free(perl_regex);
1011 return LY_EVALID;
1012 }
1013 end = (ptr - perl_regex) + 1;
1014
1015 /* need more space */
1016 if (end - start < URANGE_LEN) {
1017 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1018 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1019 }
1020
1021 /* find our range */
1022 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001023 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1024 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001025 break;
1026 }
1027 }
1028 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001029 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001030 free(perl_regex);
1031 return LY_EVALID;
1032 }
1033
1034 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1035 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1036 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1037 ++idx;
1038 }
1039 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1040 --idx;
1041 }
1042 }
1043 if (idx) {
1044 /* skip brackets */
1045 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1046 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1047 } else {
1048 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1049 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1050 }
1051 }
1052
1053 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001054 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001055 &err_code, &err_offset, NULL);
1056 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001057 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1058 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001059 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001060 free(perl_regex);
1061 return LY_EVALID;
1062 }
1063 free(perl_regex);
1064
1065 if (code) {
1066 *code = code_local;
1067 } else {
1068 free(code_local);
1069 }
1070
1071 return LY_SUCCESS;
1072
1073#undef URANGE_LEN
1074}
1075
1076/**
1077 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1078 * @param[in] ctx Compile context.
1079 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1080 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1081 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1082 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1083 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1084 */
1085static LY_ERR
1086lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1087 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1088{
1089 struct lysc_pattern **pattern;
1090 LY_ARRAY_COUNT_TYPE u;
1091 LY_ERR ret = LY_SUCCESS;
1092
1093 /* first, copy the patterns from the base type */
1094 if (base_patterns) {
1095 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1096 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1097 }
1098
1099 LY_ARRAY_FOR(patterns_p, u) {
1100 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1101 *pattern = calloc(1, sizeof **pattern);
1102 ++(*pattern)->refcount;
1103
Radek Krejci2efc45b2020-12-22 16:25:44 +01001104 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001105 LY_CHECK_RET(ret);
1106
Radek Krejcif13b87b2020-12-01 22:02:17 +01001107 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001108 (*pattern)->inverted = 1;
1109 }
1110 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1111 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1112 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1113 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1114 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001115 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001116 }
1117done:
1118 return ret;
1119}
1120
1121/**
1122 * @brief map of the possible restrictions combination for the specific built-in type.
1123 */
1124static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1125 0 /* LY_TYPE_UNKNOWN */,
1126 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1127 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1128 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1129 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1130 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1131 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1132 LYS_SET_BIT /* LY_TYPE_BITS */,
1133 0 /* LY_TYPE_BOOL */,
1134 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1135 0 /* LY_TYPE_EMPTY */,
1136 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1137 LYS_SET_BASE /* LY_TYPE_IDENT */,
1138 LYS_SET_REQINST /* LY_TYPE_INST */,
1139 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1140 LYS_SET_TYPE /* LY_TYPE_UNION */,
1141 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1142 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1143 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1144 LYS_SET_RANGE /* LY_TYPE_INT64 */
1145};
1146
1147/**
1148 * @brief stringification of the YANG built-in data types
1149 */
1150const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001151 LY_TYPE_UNKNOWN_STR,
1152 LY_TYPE_BINARY_STR,
1153 LY_TYPE_UINT8_STR,
1154 LY_TYPE_UINT16_STR,
1155 LY_TYPE_UINT32_STR,
1156 LY_TYPE_UINT64_STR,
1157 LY_TYPE_STRING_STR,
1158 LY_TYPE_BITS_STR,
1159 LY_TYPE_BOOL_STR,
1160 LY_TYPE_DEC64_STR,
1161 LY_TYPE_EMPTY_STR,
1162 LY_TYPE_ENUM_STR,
1163 LY_TYPE_IDENT_STR,
1164 LY_TYPE_INST_STR,
1165 LY_TYPE_LEAFREF_STR,
1166 LY_TYPE_UNION_STR,
1167 LY_TYPE_INT8_STR,
1168 LY_TYPE_INT16_STR,
1169 LY_TYPE_INT32_STR,
1170 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001171};
1172
1173/**
1174 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1175 * @param[in] ctx Compile context.
1176 * @param[in] enums_p Array of the parsed enum structures to compile.
1177 * @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.
1178 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001179 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001180 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1181 */
1182static LY_ERR
1183lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001184 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001185{
1186 LY_ERR ret = LY_SUCCESS;
1187 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001188 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1189 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001190 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001191 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001192
1193 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001194 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001195 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1196 return LY_EVALID;
1197 }
1198
1199 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001200 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001201 if (base_enums) {
1202 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1203 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001204 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001205 break;
1206 }
1207 }
1208 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001209 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001210 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001211 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001212 return LY_EVALID;
1213 }
1214 match = v;
1215 }
1216
1217 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001218 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001219 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001220 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001221 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001222 LY_ARRAY_FOR(*bitenums, v) {
1223 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001224 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001225 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001226 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001227 return LY_EVALID;
1228 }
1229 }
1230 } else if (base_enums) {
1231 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001232 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001233 } else {
1234 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001235 if (u == 0) {
1236 cur_val = 0;
1237 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001238 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001239 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001240 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001241 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001242 } else {
1243 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001244 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001245 }
1246
1247 /* save highest value for auto assing */
1248 if (highest_value < cur_val) {
1249 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001250 }
1251 } else { /* LY_TYPE_BITS */
1252 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001253 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001254 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001255 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001256 LY_ARRAY_FOR(*bitenums, v) {
1257 if (cur_pos == (*bitenums)[v].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 %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001260 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001261 return LY_EVALID;
1262 }
1263 }
1264 } else if (base_enums) {
1265 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001266 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001267 } else {
1268 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001269 if (u == 0) {
1270 cur_pos = 0;
1271 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001272 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001273 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001274 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001275 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001276 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001277 } else {
1278 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001279 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001280 }
1281
1282 /* save highest position for auto assing */
1283 if (highest_position < cur_pos) {
1284 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001285 }
1286 }
1287
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001288 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001289 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001290 if (basetype == LY_TYPE_ENUM) {
1291 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001292 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001293 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001294 enums_p[u].name, base_enums[match].value, cur_val);
1295 return LY_EVALID;
1296 }
1297 } else {
1298 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001299 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001300 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001301 enums_p[u].name, base_enums[match].position, cur_pos);
1302 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001303 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001304 }
1305 }
1306
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001307 /* evaluate if-ffeatures */
1308 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1309 if (!enabled) {
1310 continue;
1311 }
1312
1313 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001314 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001315 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1316 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1317 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001318 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 +01001319 if (basetype == LY_TYPE_ENUM) {
1320 e->value = cur_val;
1321 } else {
1322 e->position = cur_pos;
1323 }
Radek Krejciab430862021-03-02 20:13:40 +01001324 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001325
1326 if (basetype == LY_TYPE_BITS) {
1327 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001328 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001329 if (v != u) {
1330 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001331 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1332 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001333 }
1334 }
1335 }
1336
1337done:
1338 return ret;
1339}
1340
Radek Krejci6a205692020-12-09 13:58:22 +01001341static LY_ERR
1342lys_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 +01001343 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001344{
1345 LY_ERR ret = LY_SUCCESS;
1346 struct lysc_type **utypes = *utypes_p;
1347 struct lysc_type_union *un_aux = NULL;
1348
1349 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1350 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001351 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1352 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001353 LY_CHECK_GOTO(ret, error);
1354 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1355 /* add space for additional types from the union subtype */
1356 un_aux = (struct lysc_type_union *)utypes[u + additional];
1357 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1358 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1359
1360 /* copy subtypes of the subtype union */
1361 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1362 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1363 struct lysc_type_leafref *lref;
1364
1365 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1366 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1367 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1368 lref = (struct lysc_type_leafref *)utypes[u + additional];
1369
1370 lref->basetype = LY_TYPE_LEAFREF;
1371 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1372 LY_CHECK_GOTO(ret, error);
1373 lref->refcount = 1;
1374 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
1375 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001376 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001377 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001378 LY_CHECK_GOTO(ret, error);
1379 /* TODO extensions */
1380
1381 } else {
1382 utypes[u + additional] = un_aux->types[v];
1383 ++un_aux->types[v]->refcount;
1384 }
1385 ++additional;
1386 LY_ARRAY_INCREMENT(utypes);
1387 }
1388 /* compensate u increment in main loop */
1389 --additional;
1390
1391 /* free the replaced union subtype */
1392 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1393 un_aux = NULL;
1394 } else {
1395 LY_ARRAY_INCREMENT(utypes);
1396 }
1397 }
1398
1399 *utypes_p = utypes;
1400 return LY_SUCCESS;
1401
1402error:
1403 if (un_aux) {
1404 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1405 }
1406 *utypes_p = utypes;
1407 return ret;
1408}
1409
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001410/**
1411 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1412 * @param[in] ctx Compile context.
1413 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1414 * @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 +02001415 * @param[in] context_name Name of the context node or referencing typedef for logging.
1416 * @param[in] type_p Parsed type to compile.
1417 * @param[in] basetype Base YANG built-in type of the type to compile.
1418 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1419 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1420 * @param[out] type Newly created type structure with the filled information about the type.
1421 * @return LY_ERR value.
1422 */
1423static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001424lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1425 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 +02001426{
1427 LY_ERR ret = LY_SUCCESS;
1428 struct lysc_type_bin *bin;
1429 struct lysc_type_num *num;
1430 struct lysc_type_str *str;
1431 struct lysc_type_bits *bits;
1432 struct lysc_type_enum *enumeration;
1433 struct lysc_type_dec *dec;
1434 struct lysc_type_identityref *idref;
1435 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001436 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001437
1438 switch (basetype) {
1439 case LY_TYPE_BINARY:
1440 bin = (struct lysc_type_bin *)(*type);
1441
1442 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1443 if (type_p->length) {
1444 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1445 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1446 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001447 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001448 }
1449 }
1450 break;
1451 case LY_TYPE_BITS:
1452 /* RFC 7950 9.7 - bits */
1453 bits = (struct lysc_type_bits *)(*type);
1454 if (type_p->bits) {
1455 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1456 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1457 (struct lysc_type_bitenum_item **)&bits->bits));
1458 }
1459
1460 if (!base && !type_p->flags) {
1461 /* type derived from bits built-in type must contain at least one bit */
1462 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001463 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001464 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001465 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001466 }
1467 return LY_EVALID;
1468 }
1469 break;
1470 case LY_TYPE_DEC64:
1471 dec = (struct lysc_type_dec *)(*type);
1472
1473 /* RFC 7950 9.3.4 - fraction-digits */
1474 if (!base) {
1475 if (!type_p->fraction_digits) {
1476 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001477 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001478 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001479 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001480 }
1481 return LY_EVALID;
1482 }
1483 dec->fraction_digits = type_p->fraction_digits;
1484 } else {
1485 if (type_p->fraction_digits) {
1486 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1487 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001488 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001489 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1490 tpdfname);
1491 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001492 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001493 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1494 }
1495 return LY_EVALID;
1496 }
1497 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1498 }
1499
1500 /* RFC 7950 9.2.4 - range */
1501 if (type_p->range) {
1502 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1503 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1504 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001505 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001506 }
1507 }
1508 break;
1509 case LY_TYPE_STRING:
1510 str = (struct lysc_type_str *)(*type);
1511
1512 /* RFC 7950 9.4.4 - length */
1513 if (type_p->length) {
1514 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1515 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1516 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001517 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001518 }
1519 } else if (base && ((struct lysc_type_str *)base)->length) {
1520 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1521 }
1522
1523 /* RFC 7950 9.4.5 - pattern */
1524 if (type_p->patterns) {
1525 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1526 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1527 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1528 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1529 }
1530 break;
1531 case LY_TYPE_ENUM:
1532 enumeration = (struct lysc_type_enum *)(*type);
1533
1534 /* RFC 7950 9.6 - enum */
1535 if (type_p->enums) {
1536 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1537 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1538 }
1539
1540 if (!base && !type_p->flags) {
1541 /* type derived from enumerations built-in type must contain at least one enum */
1542 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001543 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001544 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001545 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001546 }
1547 return LY_EVALID;
1548 }
1549 break;
1550 case LY_TYPE_INT8:
1551 case LY_TYPE_UINT8:
1552 case LY_TYPE_INT16:
1553 case LY_TYPE_UINT16:
1554 case LY_TYPE_INT32:
1555 case LY_TYPE_UINT32:
1556 case LY_TYPE_INT64:
1557 case LY_TYPE_UINT64:
1558 num = (struct lysc_type_num *)(*type);
1559
1560 /* RFC 6020 9.2.4 - range */
1561 if (type_p->range) {
1562 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1563 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1564 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001565 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001566 }
1567 }
1568 break;
1569 case LY_TYPE_IDENT:
1570 idref = (struct lysc_type_identityref *)(*type);
1571
1572 /* RFC 7950 9.10.2 - base */
1573 if (type_p->bases) {
1574 if (base) {
1575 /* only the directly derived identityrefs can contain base specification */
1576 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001577 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001578 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1579 tpdfname);
1580 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001581 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001582 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1583 }
1584 return LY_EVALID;
1585 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001586 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 +02001587 }
1588
1589 if (!base && !type_p->flags) {
1590 /* type derived from identityref built-in type must contain at least one base */
1591 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001592 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001593 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001594 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001595 }
1596 return LY_EVALID;
1597 }
1598 break;
1599 case LY_TYPE_LEAFREF:
1600 lref = (struct lysc_type_leafref *)*type;
1601
1602 /* RFC 7950 9.9.3 - require-instance */
1603 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001604 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001605 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001606 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001607 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1608 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001609 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001610 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1611 }
1612 return LY_EVALID;
1613 }
1614 lref->require_instance = type_p->require_instance;
1615 } else if (base) {
1616 /* inherit */
1617 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1618 } else {
1619 /* default is true */
1620 lref->require_instance = 1;
1621 }
1622 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001623 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001624
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001625 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001626 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr),
Radek Krejci8df109d2021-04-23 12:19:08 +02001627 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001628 } else if (base) {
1629 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci8df109d2021-04-23 12:19:08 +02001630 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001631 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001632 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001633 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001634 return LY_EVALID;
1635 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001636 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001637 return LY_EVALID;
1638 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001639 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001640 break;
1641 case LY_TYPE_INST:
1642 /* RFC 7950 9.9.3 - require-instance */
1643 if (type_p->flags & LYS_SET_REQINST) {
1644 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1645 } else {
1646 /* default is true */
1647 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1648 }
1649 break;
1650 case LY_TYPE_UNION:
1651 un = (struct lysc_type_union *)(*type);
1652
1653 /* RFC 7950 7.4 - type */
1654 if (type_p->types) {
1655 if (base) {
1656 /* only the directly derived union can contain types specification */
1657 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001658 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001659 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1660 tpdfname);
1661 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001662 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001663 "Invalid type substatement for the type not directly derived from union built-in type.");
1664 }
1665 return LY_EVALID;
1666 }
1667 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001668 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 +02001669 }
1670
1671 if (!base && !type_p->flags) {
1672 /* type derived from union built-in type must contain at least one type */
1673 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001674 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001675 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001676 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001677 }
1678 return LY_EVALID;
1679 }
1680 break;
1681 case LY_TYPE_BOOL:
1682 case LY_TYPE_EMPTY:
1683 case LY_TYPE_UNKNOWN: /* just to complete switch */
1684 break;
1685 }
1686
1687 if (tpdfname) {
1688 switch (basetype) {
1689 case LY_TYPE_BINARY:
1690 type_p->compiled = *type;
1691 *type = calloc(1, sizeof(struct lysc_type_bin));
1692 break;
1693 case LY_TYPE_BITS:
1694 type_p->compiled = *type;
1695 *type = calloc(1, sizeof(struct lysc_type_bits));
1696 break;
1697 case LY_TYPE_DEC64:
1698 type_p->compiled = *type;
1699 *type = calloc(1, sizeof(struct lysc_type_dec));
1700 break;
1701 case LY_TYPE_STRING:
1702 type_p->compiled = *type;
1703 *type = calloc(1, sizeof(struct lysc_type_str));
1704 break;
1705 case LY_TYPE_ENUM:
1706 type_p->compiled = *type;
1707 *type = calloc(1, sizeof(struct lysc_type_enum));
1708 break;
1709 case LY_TYPE_INT8:
1710 case LY_TYPE_UINT8:
1711 case LY_TYPE_INT16:
1712 case LY_TYPE_UINT16:
1713 case LY_TYPE_INT32:
1714 case LY_TYPE_UINT32:
1715 case LY_TYPE_INT64:
1716 case LY_TYPE_UINT64:
1717 type_p->compiled = *type;
1718 *type = calloc(1, sizeof(struct lysc_type_num));
1719 break;
1720 case LY_TYPE_IDENT:
1721 type_p->compiled = *type;
1722 *type = calloc(1, sizeof(struct lysc_type_identityref));
1723 break;
1724 case LY_TYPE_LEAFREF:
1725 type_p->compiled = *type;
1726 *type = calloc(1, sizeof(struct lysc_type_leafref));
1727 break;
1728 case LY_TYPE_INST:
1729 type_p->compiled = *type;
1730 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1731 break;
1732 case LY_TYPE_UNION:
1733 type_p->compiled = *type;
1734 *type = calloc(1, sizeof(struct lysc_type_union));
1735 break;
1736 case LY_TYPE_BOOL:
1737 case LY_TYPE_EMPTY:
1738 case LY_TYPE_UNKNOWN: /* just to complete switch */
1739 break;
1740 }
1741 }
1742 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1743
1744cleanup:
1745 return ret;
1746}
1747
Radek Krejcibf940f92021-03-24 21:04:13 +01001748/**
1749 * @brief Find the correct plugin implementing the described type
1750 *
1751 * @param[in] mod Module where the type is defined
1752 * @param[in] name Name of the type (typedef)
1753 * @param[in] basetype Type's basetype (when the built-in base plugin is supposed to be used)
1754 * @return Pointer to the plugin implementing the described data type.
1755 */
1756static struct lyplg_type *
1757lys_compile_type_get_plugin(struct lys_module *mod, const char *name, LY_DATA_TYPE basetype)
1758{
1759 struct lyplg_type *p;
1760
1761 /* try to find loaded user type plugins */
1762 p = lyplg_find(LYPLG_TYPE, mod->name, mod->revision, name);
1763 if (!p) {
1764 /* use the internal built-in type implementation */
1765 p = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
1766 }
1767
1768 return p;
1769}
1770
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001771LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001772lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1773 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001774{
1775 LY_ERR ret = LY_SUCCESS;
1776 ly_bool dummyloops = 0;
1777 struct type_context {
1778 const struct lysp_tpdf *tpdf;
1779 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001780 } *tctx, *tctx_prev = NULL, *tctx_iter;
1781 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1782 struct lysc_type *base = NULL, *prev_type;
1783 struct ly_set tpdf_chain = {0};
1784
1785 (*type) = NULL;
1786 if (dflt) {
1787 *dflt = NULL;
1788 }
1789
1790 tctx = calloc(1, sizeof *tctx);
1791 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001792 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 +02001793 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001794 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1795 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001796 if (basetype) {
1797 break;
1798 }
1799
1800 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001801 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1802 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001803 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1804
1805 if (units && !*units) {
1806 /* inherit units */
1807 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1808 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1809 }
1810 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1811 /* inherit default */
1812 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1813 }
1814 if (dummyloops && (!units || *units) && dflt && *dflt) {
1815 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1816 break;
1817 }
1818
Radek Krejci720d2612021-03-03 19:44:22 +01001819 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1820 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1821 * and we need now recompile the type again in the updated context. */
1822 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1823 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1824 }
1825
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826 if (tctx->tpdf->type.compiled) {
1827 /* it is not necessary to continue, the rest of the chain was already compiled,
1828 * but we still may need to inherit default and units values, so start dummy loops */
1829 basetype = tctx->tpdf->type.compiled->basetype;
1830 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1831 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1832
1833 if ((units && !*units) || (dflt && !*dflt)) {
1834 dummyloops = 1;
1835 goto preparenext;
1836 } else {
1837 tctx = NULL;
1838 break;
1839 }
1840 }
1841
1842 /* circular typedef reference detection */
1843 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1844 /* local part */
1845 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001846 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001847 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001848 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1849 free(tctx);
1850 ret = LY_EVALID;
1851 goto cleanup;
1852 }
1853 }
1854 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1855 /* global part for unions corner case */
1856 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001857 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001858 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001859 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1860 free(tctx);
1861 ret = LY_EVALID;
1862 goto cleanup;
1863 }
1864 }
1865
1866 /* store information for the following processing */
1867 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1868 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1869
1870preparenext:
1871 /* prepare next loop */
1872 tctx_prev = tctx;
1873 tctx = calloc(1, sizeof *tctx);
1874 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1875 }
1876 free(tctx);
1877
1878 /* allocate type according to the basetype */
1879 switch (basetype) {
1880 case LY_TYPE_BINARY:
1881 *type = calloc(1, sizeof(struct lysc_type_bin));
1882 break;
1883 case LY_TYPE_BITS:
1884 *type = calloc(1, sizeof(struct lysc_type_bits));
1885 break;
1886 case LY_TYPE_BOOL:
1887 case LY_TYPE_EMPTY:
1888 *type = calloc(1, sizeof(struct lysc_type));
1889 break;
1890 case LY_TYPE_DEC64:
1891 *type = calloc(1, sizeof(struct lysc_type_dec));
1892 break;
1893 case LY_TYPE_ENUM:
1894 *type = calloc(1, sizeof(struct lysc_type_enum));
1895 break;
1896 case LY_TYPE_IDENT:
1897 *type = calloc(1, sizeof(struct lysc_type_identityref));
1898 break;
1899 case LY_TYPE_INST:
1900 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1901 break;
1902 case LY_TYPE_LEAFREF:
1903 *type = calloc(1, sizeof(struct lysc_type_leafref));
1904 break;
1905 case LY_TYPE_STRING:
1906 *type = calloc(1, sizeof(struct lysc_type_str));
1907 break;
1908 case LY_TYPE_UNION:
1909 *type = calloc(1, sizeof(struct lysc_type_union));
1910 break;
1911 case LY_TYPE_INT8:
1912 case LY_TYPE_UINT8:
1913 case LY_TYPE_INT16:
1914 case LY_TYPE_UINT16:
1915 case LY_TYPE_INT32:
1916 case LY_TYPE_UINT32:
1917 case LY_TYPE_INT64:
1918 case LY_TYPE_UINT64:
1919 *type = calloc(1, sizeof(struct lysc_type_num));
1920 break;
1921 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001922 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001923 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1924 ret = LY_EVALID;
1925 goto cleanup;
1926 }
1927 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1928 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001929 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001930 ly_data_type2str[basetype]);
1931 free(*type);
1932 (*type) = NULL;
1933 ret = LY_EVALID;
1934 goto cleanup;
1935 }
1936
1937 /* get restrictions from the referred typedefs */
1938 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1939 tctx = (struct type_context *)tpdf_chain.objs[u];
1940
1941 /* remember the typedef context for circular check */
1942 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1943 LY_CHECK_GOTO(ret, cleanup);
1944
1945 if (tctx->tpdf->type.compiled) {
1946 base = tctx->tpdf->type.compiled;
1947 continue;
1948 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1949 /* no change, just use the type information from the base */
1950 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1951 ++base->refcount;
1952 continue;
1953 }
1954
1955 ++(*type)->refcount;
1956 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001957 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001958 tctx->tpdf->name, ly_data_type2str[basetype]);
1959 ret = LY_EVALID;
1960 goto cleanup;
1961 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001962 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001963 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1964 tctx->tpdf->name, tctx->tpdf->dflt.str);
1965 ret = LY_EVALID;
1966 goto cleanup;
1967 }
1968
1969 (*type)->basetype = basetype;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001970 /* collect extensions */
1971 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
1972 /* get plugin for the type */
Radek Krejcibf940f92021-03-24 21:04:13 +01001973 (*type)->plugin = lys_compile_type_get_plugin(tctx->tpdf->type.pmod->mod, tctx->tpdf->name, basetype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001974 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001975 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001976 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1977 LY_CHECK_GOTO(ret, cleanup);
1978 base = prev_type;
1979 }
1980 /* remove the processed typedef contexts from the stack for circular check */
1981 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1982
1983 /* process the type definition in leaf */
1984 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1985 /* get restrictions from the node itself */
1986 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01001987 (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001988 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001989 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001990 LY_CHECK_GOTO(ret, cleanup);
1991 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1992 /* no specific restriction in leaf's type definition, copy from the base */
1993 free(*type);
1994 (*type) = base;
1995 ++(*type)->refcount;
1996 }
1997
Radek Krejciab430862021-03-02 20:13:40 +01001998 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001999
2000cleanup:
2001 ly_set_erase(&tpdf_chain, free);
2002 return ret;
2003}
2004
2005/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01002006 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
2007 */
2008#define LYS_STATUS_USES LYS_CONFIG_MASK
2009
2010/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002011 * @brief Compile status information of the given node.
2012 *
2013 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
2014 * has the status correctly set during the compilation.
2015 *
2016 * @param[in] ctx Compile context
2017 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
2018 * If the status was set explicitly on the node, it is already set in the flags value and we just check
2019 * the compatibility with the parent's status value.
2020 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
2021 * @return LY_ERR value.
2022 */
2023static LY_ERR
2024lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
2025{
2026 /* status - it is not inherited by specification, but it does not make sense to have
2027 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2028 if (!((*node_flags) & LYS_STATUS_MASK)) {
2029 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002030 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002031 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01002032 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002033 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2034 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2035 }
2036 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
2037 } else {
2038 (*node_flags) |= LYS_STATUS_CURR;
2039 }
2040 } else if (parent_flags & LYS_STATUS_MASK) {
2041 /* check status compatibility with the parent */
2042 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
2043 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002044 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002045 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2046 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2047 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002048 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002049 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2050 }
2051 return LY_EVALID;
2052 }
2053 }
2054 return LY_SUCCESS;
2055}
2056
2057/**
2058 * @brief Check uniqness of the node/action/notification name.
2059 *
2060 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2061 * structures, but they share the namespace so we need to check their name collisions.
2062 *
2063 * @param[in] ctx Compile context.
2064 * @param[in] parent Parent of the nodes to check, can be NULL.
2065 * @param[in] name Name of the item to find in the given lists.
2066 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2067 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2068 */
2069static LY_ERR
2070lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2071 const struct lysc_node *exclude)
2072{
2073 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002074 const struct lysc_node_action *actions;
2075 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002076 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002077 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002078
2079#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2080
2081 if (exclude->nodetype == LYS_CASE) {
2082 /* check restricted only to all the cases */
2083 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002084 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002085 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002086 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002087 return LY_EEXIST;
2088 }
2089 }
2090
2091 return LY_SUCCESS;
2092 }
2093
2094 /* no reason for our parent to be choice anymore */
2095 assert(!parent || (parent->nodetype != LYS_CHOICE));
2096
2097 if (parent && (parent->nodetype == LYS_CASE)) {
2098 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002099
2100 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2101 iter = lysc_data_parent(parent);
2102 do {
2103 parent = parent->parent;
2104 if (parent && (parent->nodetype == LYS_CHOICE)) {
2105 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2106 }
2107 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002108 }
2109
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002110 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002111 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2112 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2113 if (exclude->flags & LYS_IS_OUTPUT) {
2114 getnext_flags |= LYS_GETNEXT_OUTPUT;
2115 parent = lysc_node_child(parent)->next;
2116 } else {
2117 parent = lysc_node_child(parent);
2118 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002119 }
2120
2121 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002122 if (!parent && ctx->ext) {
2123 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2124 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2125 goto error;
2126 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002127
Radek Krejci5fa32a32021-02-08 18:12:38 +01002128 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2129 if (iter->nodetype == LYS_CHOICE) {
2130 iter2 = NULL;
2131 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2132 if (CHECK_NODE(iter2, exclude, name)) {
2133 goto error;
2134 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002135 }
2136 }
2137 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002138 } else {
2139 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2140 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2141 goto error;
2142 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002143
Radek Krejci5fa32a32021-02-08 18:12:38 +01002144 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2145 if (iter->nodetype == LYS_CHOICE) {
2146 iter2 = NULL;
2147 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2148 if (CHECK_NODE(iter2, exclude, name)) {
2149 goto error;
2150 }
2151 }
2152 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002153 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002154
Radek Krejci5fa32a32021-02-08 18:12:38 +01002155 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2156 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2157 if (CHECK_NODE(iter, exclude, name)) {
2158 goto error;
2159 }
2160 }
2161
2162 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2163 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2164 if (CHECK_NODE(iter, exclude, name)) {
2165 goto error;
2166 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002167 }
2168 }
Radek Krejci078e4342021-02-12 18:17:26 +01002169 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002170 return LY_SUCCESS;
2171
2172error:
Radek Krejci078e4342021-02-12 18:17:26 +01002173 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002174 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002175 return LY_EEXIST;
2176
2177#undef CHECK_NODE
2178}
2179
Radek Krejci2a9fc652021-01-22 17:44:34 +01002180/**
2181 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2182 * keep specific order of augments targetting the same node.
2183 *
2184 * @param[in] ctx Compile context
2185 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2186 * the choice itself is expected instead of a specific case node.
2187 * @param[in] node Schema node to connect into the list.
2188 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2189 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2190 */
2191static LY_ERR
2192lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002193{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002194 struct lysc_node **children, *anchor = NULL;
2195 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002196
Radek Krejci2a9fc652021-01-22 17:44:34 +01002197 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002198
Radek Krejci2a9fc652021-01-22 17:44:34 +01002199 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002200 if (node->nodetype == LYS_INPUT) {
2201 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2202 /* input node is part of the action but link it with output */
2203 node->next = &((struct lysc_node_action *)parent)->output.node;
2204 node->prev = node->next;
2205 return LY_SUCCESS;
2206 } else if (node->nodetype == LYS_OUTPUT) {
2207 /* output node is part of the action but link it with input */
2208 node->next = NULL;
2209 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002210 return LY_SUCCESS;
2211 } else if (node->nodetype == LYS_ACTION) {
2212 children = (struct lysc_node **)lysc_node_actions_p(parent);
2213 } else if (node->nodetype == LYS_NOTIF) {
2214 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2215 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002216 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002217 }
2218 assert(children);
2219
2220 if (!(*children)) {
2221 /* first child */
2222 *children = node;
2223 } else if (node->flags & LYS_KEY) {
2224 /* special handling of adding keys */
2225 assert(node->module == parent->module);
2226 anchor = *children;
2227 if (anchor->flags & LYS_KEY) {
2228 while ((anchor->flags & LYS_KEY) && anchor->next) {
2229 anchor = anchor->next;
2230 }
2231 /* insert after the last key */
2232 insert_after = 1;
2233 } /* else insert before anchor (at the beginning) */
2234 } else if ((*children)->prev->module == node->module) {
2235 /* last child is from the same module, keep the order and insert at the end */
2236 anchor = (*children)->prev;
2237 insert_after = 1;
2238 } else if (parent->module == node->module) {
2239 /* adding module child after some augments were connected */
2240 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2241 } else {
2242 /* some augments are already connected and we are connecting new ones,
2243 * keep module name order and insert the node into the children list */
2244 anchor = *children;
2245 do {
2246 anchor = anchor->prev;
2247
2248 /* check that we have not found the last augment node from our module or
2249 * the first augment node from a "smaller" module or
2250 * the first node from a local module */
2251 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2252 (anchor->module == parent->module)) {
2253 /* insert after */
2254 insert_after = 1;
2255 break;
2256 }
2257
2258 /* we have traversed all the nodes, insert before anchor (as the first node) */
2259 } while (anchor->prev->next);
2260 }
2261
2262 /* insert */
2263 if (anchor) {
2264 if (insert_after) {
2265 node->next = anchor->next;
2266 node->prev = anchor;
2267 anchor->next = node;
2268 if (node->next) {
2269 /* middle node */
2270 node->next->prev = node;
2271 } else {
2272 /* last node */
2273 (*children)->prev = node;
2274 }
2275 } else {
2276 node->next = anchor;
2277 node->prev = anchor->prev;
2278 anchor->prev = node;
2279 if (anchor == *children) {
2280 /* first node */
2281 *children = node;
2282 } else {
2283 /* middle node */
2284 node->prev->next = node;
2285 }
2286 }
2287 }
2288
2289 /* check the name uniqueness (even for an only child, it may be in case) */
2290 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2291 return LY_EEXIST;
2292 }
2293 } else {
2294 /* top-level element */
2295 struct lysc_node **list;
2296
Radek Krejci6b88a462021-02-17 12:39:34 +01002297 if (ctx->ext) {
2298 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2299 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002300 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2301 } else if (node->nodetype == LYS_NOTIF) {
2302 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2303 } else {
2304 list = &ctx->cur_mod->compiled->data;
2305 }
2306 if (!(*list)) {
2307 *list = node;
2308 } else {
2309 /* insert at the end of the module's top-level nodes list */
2310 (*list)->prev->next = node;
2311 node->prev = (*list)->prev;
2312 (*list)->prev = node;
2313 }
2314
2315 /* check the name uniqueness on top-level */
2316 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2317 return LY_EEXIST;
2318 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002319 }
2320
Radek Krejci2a9fc652021-01-22 17:44:34 +01002321 return LY_SUCCESS;
2322}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002323
Radek Krejci2a9fc652021-01-22 17:44:34 +01002324/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002325 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002326 *
2327 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002328 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002329 * @return LY_ERR value.
2330 */
2331static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002332lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002333{
2334 /* case never has any explicit config */
2335 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002336
Radek Krejci91531e12021-02-08 19:57:54 +01002337 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
2338 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002339 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002340 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2341 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002342 if (node->parent) {
2343 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002344 } else {
2345 /* default is config true */
2346 node->flags |= LYS_CONFIG_W;
2347 }
2348 } else {
2349 /* config set explicitly */
2350 node->flags |= LYS_SET_CONFIG;
2351 }
2352
Radek Krejci91531e12021-02-08 19:57:54 +01002353 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002354 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002355 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002356 }
2357
Radek Krejci2a9fc652021-01-22 17:44:34 +01002358 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002359}
2360
Radek Krejci91531e12021-02-08 19:57:54 +01002361/**
2362 * @brief Set various flags of the compiled nodes
2363 *
2364 * @param[in] ctx Compile context.
2365 * @param[in] node Compiled node where the flags will be set.
2366 * @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).
2367 * Zero means no uses, non-zero value with no status bit set mean the default status.
2368 */
2369static LY_ERR
2370lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2371{
2372 /* inherit config flags */
2373 LY_CHECK_RET(lys_compile_config(ctx, node));
2374
2375 /* status - it is not inherited by specification, but it does not make sense to have
2376 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2377 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (node->parent ? node->parent->flags : 0)));
2378
2379 /* other flags */
2380 if ((ctx->options & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
2381 node->flags |= LYS_IS_INPUT;
2382 } else if ((ctx->options & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
2383 node->flags |= LYS_IS_OUTPUT;
2384 } else if ((ctx->options & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
2385 node->flags |= LYS_IS_NOTIF;
2386 }
2387
2388 return LY_SUCCESS;
2389}
2390
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002391LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002392lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2393 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2394 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002395{
2396 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002397 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002398 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002399 struct lysp_when *pwhen = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002400
Radek Krejci2a9fc652021-01-22 17:44:34 +01002401 node->nodetype = pnode->nodetype;
2402 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002403 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002404 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002405 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002406
Radek Krejci2a9fc652021-01-22 17:44:34 +01002407 /* compile any deviations for this node */
2408 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002409 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002410 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002411 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002412 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002413 }
2414
Radek Krejci2a9fc652021-01-22 17:44:34 +01002415 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002416
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002417 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002418 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Radek Krejciaf0548b2021-02-15 15:11:22 +01002419 if (!enabled && !(ctx->options & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002420 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002421 ctx->options |= LYS_COMPILE_DISABLED;
2422 }
2423
Radek Krejci91531e12021-02-08 19:57:54 +01002424 /* config, status and other flags */
2425 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002426 LY_CHECK_GOTO(ret, error);
2427
2428 /* list ordering */
2429 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002430 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002431 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002432 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
2433 (ctx->options & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002434 node->flags &= ~LYS_ORDBY_MASK;
2435 node->flags |= LYS_ORDBY_SYSTEM;
2436 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2437 /* default ordering is system */
2438 node->flags |= LYS_ORDBY_SYSTEM;
2439 }
2440 }
2441
Radek Krejci2a9fc652021-01-22 17:44:34 +01002442 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2443 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2444 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2445
2446 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2447 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2448
Radek Krejci9a3823e2021-01-27 20:26:46 +01002449 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002450 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002451 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002452 LY_CHECK_GOTO(ret, cleanup);
2453 }
2454
2455 /* connect any augments */
2456 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2457
2458 /* nodetype-specific part */
2459 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2460
2461 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002462 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002463 if (node->flags & LYS_MAND_TRUE) {
2464 /* inherit LYS_MAND_TRUE in parent containers */
2465 lys_compile_mandatory_parents(parent, 1);
2466 }
2467
2468 if (child_set) {
2469 /* add the new node into set */
2470 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2471 }
2472
2473 goto cleanup;
2474
2475error:
2476 lysc_node_free(ctx->ctx, node, 0);
2477cleanup:
2478 if (ret && dev_pnode) {
2479 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2480 }
2481 lysp_dev_node_free(ctx->ctx, dev_pnode);
2482 return ret;
2483}
2484
2485/**
2486 * @brief Compile parsed action's input/output node information.
2487 * @param[in] ctx Compile context
2488 * @param[in] pnode Parsed inout node.
2489 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2490 * is enriched with the inout-specific information.
2491 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2492 */
2493LY_ERR
2494lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2495{
2496 LY_ERR ret = LY_SUCCESS;
2497 struct lysp_node *child_p;
2498 uint32_t prev_options = ctx->options;
2499
2500 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2501 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2502
2503 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002504 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Radek Krejci91531e12021-02-08 19:57:54 +01002505 ctx->options |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002506
Radek Krejci01180ac2021-01-27 08:48:22 +01002507 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002508 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2509 }
2510
2511 ctx->options = prev_options;
2512
2513done:
2514 return ret;
2515}
2516
2517/**
2518 * @brief Compile parsed action node information.
2519 * @param[in] ctx Compile context
2520 * @param[in] pnode Parsed action node.
2521 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2522 * is enriched with the action-specific information.
2523 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2524 */
2525LY_ERR
2526lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2527{
2528 LY_ERR ret;
2529 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2530 struct lysc_node_action *action = (struct lysc_node_action *)node;
2531 struct lysp_node_action_inout *input, implicit_input = {
2532 .nodetype = LYS_INPUT,
2533 .name = "input",
2534 .parent = pnode,
2535 };
2536 struct lysp_node_action_inout *output, implicit_output = {
2537 .nodetype = LYS_OUTPUT,
2538 .name = "output",
2539 .parent = pnode,
2540 };
2541
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002542 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002543 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002544 if (action_p->input.nodetype == LYS_UNKNOWN) {
2545 input = &implicit_input;
2546 } else {
2547 input = &action_p->input;
2548 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002549 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 +01002550 lysc_update_path(ctx, NULL, NULL);
2551 LY_CHECK_GOTO(ret, done);
2552
2553 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002554 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002555 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002556 output = &implicit_output;
2557 } else {
2558 output = &action_p->output;
2559 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002560 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 +01002561 lysc_update_path(ctx, NULL, NULL);
2562 LY_CHECK_GOTO(ret, done);
2563
2564 /* do not check "must" semantics in a grouping */
2565 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2566 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2567 LY_CHECK_GOTO(ret, done);
2568 }
2569
2570done:
2571 return ret;
2572}
2573
2574/**
2575 * @brief Compile parsed action node information.
2576 * @param[in] ctx Compile context
2577 * @param[in] pnode Parsed action node.
2578 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2579 * is enriched with the action-specific information.
2580 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2581 */
2582LY_ERR
2583lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2584{
2585 LY_ERR ret = LY_SUCCESS;
2586 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2587 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2588 struct lysp_node *child_p;
2589
2590 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002591 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002592 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002593 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002594 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002595 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002596
Radek Krejci01180ac2021-01-27 08:48:22 +01002597 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002598 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002599 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002600 }
2601
Radek Krejci2a9fc652021-01-22 17:44:34 +01002602done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002603 return ret;
2604}
2605
2606/**
2607 * @brief Compile parsed container node information.
2608 * @param[in] ctx Compile context
2609 * @param[in] pnode Parsed container node.
2610 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2611 * is enriched with the container-specific information.
2612 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2613 */
2614static LY_ERR
2615lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2616{
2617 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2618 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2619 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002620 LY_ERR ret = LY_SUCCESS;
2621
2622 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002623 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002624 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002625 }
2626
2627 /* more cases when the container has meaning but is kept NP for convenience:
2628 * - when condition
2629 * - direct child action/notification
2630 */
2631
2632 LY_LIST_FOR(cont_p->child, child_p) {
2633 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2634 LY_CHECK_GOTO(ret, done);
2635 }
2636
Michal Vasko5347e3a2020-11-03 17:14:57 +01002637 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002638 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002639 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002640 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002641 LY_CHECK_GOTO(ret, done);
2642 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002643
2644 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2645 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2646 LY_CHECK_GOTO(ret, done);
2647 }
2648 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2649 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2650 LY_CHECK_GOTO(ret, done);
2651 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002652
2653done:
2654 return ret;
2655}
2656
Michal Vasko72244882021-01-12 15:21:05 +01002657/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002658 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2659 * @param[in] ctx Compile context.
2660 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2661 * @param[in] type_p Parsed type to compile.
2662 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2663 * @return LY_ERR value.
2664 */
2665static LY_ERR
2666lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2667 struct lysc_node_leaf *leaf)
2668{
2669 struct lysp_qname *dflt;
2670
Michal Vaskoa99b3572021-02-01 11:54:58 +01002671 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002672 leaf->units ? NULL : &leaf->units, &dflt));
2673
2674 /* store default value, if any */
2675 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2676 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2677 }
2678
Michal Vasko12606ee2020-11-25 17:05:11 +01002679 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002680 /* 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 +01002681 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002682 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002683 LY_ARRAY_COUNT_TYPE u;
2684 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2685 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2686 /* 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 +01002687 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002688 }
2689 }
2690 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2691 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002692 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 +02002693 return LY_EVALID;
2694 }
2695 }
2696
2697 return LY_SUCCESS;
2698}
2699
2700/**
2701 * @brief Compile parsed leaf node information.
2702 * @param[in] ctx Compile context
2703 * @param[in] pnode Parsed leaf node.
2704 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2705 * is enriched with the leaf-specific information.
2706 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2707 */
2708static LY_ERR
2709lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2710{
2711 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2712 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002713 LY_ERR ret = LY_SUCCESS;
2714
Michal Vasko5347e3a2020-11-03 17:14:57 +01002715 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002716 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002717 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002718 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002719 LY_CHECK_GOTO(ret, done);
2720 }
2721 if (leaf_p->units) {
2722 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2723 leaf->flags |= LYS_SET_UNITS;
2724 }
2725
2726 /* compile type */
2727 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2728 LY_CHECK_GOTO(ret, done);
2729
2730 /* store/update default value */
2731 if (leaf_p->dflt.str) {
2732 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2733 leaf->flags |= LYS_SET_DFLT;
2734 }
2735
2736 /* checks */
2737 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002738 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002739 "Invalid mandatory leaf with a default value.");
2740 return LY_EVALID;
2741 }
2742
2743done:
2744 return ret;
2745}
2746
2747/**
2748 * @brief Compile parsed leaf-list node information.
2749 * @param[in] ctx Compile context
2750 * @param[in] pnode Parsed leaf-list node.
2751 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2752 * is enriched with the leaf-list-specific information.
2753 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2754 */
2755static LY_ERR
2756lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2757{
2758 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2759 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002760 LY_ERR ret = LY_SUCCESS;
2761
Michal Vasko5347e3a2020-11-03 17:14:57 +01002762 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002763 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002764 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002765 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002766 LY_CHECK_GOTO(ret, done);
2767 }
2768 if (llist_p->units) {
2769 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2770 llist->flags |= LYS_SET_UNITS;
2771 }
2772
2773 /* compile type */
2774 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2775 LY_CHECK_GOTO(ret, done);
2776
2777 /* store/update default values */
2778 if (llist_p->dflts) {
2779 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002780 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002781 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2782 return LY_EVALID;
2783 }
2784
2785 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2786 llist->flags |= LYS_SET_DFLT;
2787 }
2788
2789 llist->min = llist_p->min;
2790 if (llist->min) {
2791 llist->flags |= LYS_MAND_TRUE;
2792 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002793 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2794
2795 if (llist->flags & LYS_CONFIG_R) {
2796 /* state leaf-list is always ordered-by user */
2797 llist->flags &= ~LYS_ORDBY_SYSTEM;
2798 llist->flags |= LYS_ORDBY_USER;
2799 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002800
2801 /* checks */
2802 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002803 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 +02002804 return LY_EVALID;
2805 }
2806
2807 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002808 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002809 llist->min, llist->max);
2810 return LY_EVALID;
2811 }
2812
2813done:
2814 return ret;
2815}
2816
2817LY_ERR
2818lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
Radek Krejci8df109d2021-04-23 12:19:08 +02002819 const struct lys_module *cur_mod, LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002820 const struct lysc_node **target, uint16_t *result_flag)
2821{
2822 LY_ERR ret = LY_EVALID;
2823 const char *name, *prefix, *id;
2824 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002825 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002826 const char *nodeid_type;
2827 uint32_t getnext_extra_flag = 0;
2828 uint16_t current_nodetype = 0;
2829
2830 assert(nodeid);
2831 assert(target);
2832 assert(result_flag);
2833 *target = NULL;
2834 *result_flag = 0;
2835
2836 id = nodeid;
2837
2838 if (ctx_node) {
2839 /* descendant-schema-nodeid */
2840 nodeid_type = "descendant";
2841
2842 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002843 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002844 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002845 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002846 return LY_EVALID;
2847 }
2848 } else {
2849 /* absolute-schema-nodeid */
2850 nodeid_type = "absolute";
2851
2852 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002853 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002854 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002855 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002856 return LY_EVALID;
2857 }
2858 ++id;
2859 }
2860
2861 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2862 if (prefix) {
2863 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2864 if (!mod) {
2865 /* module must always be found */
2866 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002867 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002868 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002869 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002870 return LY_ENOTFOUND;
2871 }
2872 } else {
2873 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02002874 case LY_VALUE_SCHEMA:
2875 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002876 /* use the current module */
2877 mod = cur_mod;
2878 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02002879 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02002880 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002881 if (!ctx_node) {
2882 LOGINT_RET(ctx->ctx);
2883 }
2884 /* inherit the module of the previous context node */
2885 mod = ctx_node->module;
2886 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02002887 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02002888 case LY_VALUE_XML:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002889 /* not really defined */
2890 LOGINT_RET(ctx->ctx);
2891 }
2892 }
2893
2894 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2895 /* move through input/output manually */
2896 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002897 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2898 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002899 return LY_ENOTFOUND;
2900 }
2901 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002902 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002903 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002904 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002905 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2906 } else {
2907 /* only input or output is valid */
2908 ctx_node = NULL;
2909 }
2910 } else {
2911 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002912 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002913 getnext_extra_flag = 0;
2914 }
2915 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002916 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2917 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002918 return LY_ENOTFOUND;
2919 }
2920 current_nodetype = ctx_node->nodetype;
2921
2922 if (current_nodetype == LYS_NOTIF) {
2923 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2924 } else if (current_nodetype == LYS_INPUT) {
2925 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2926 } else if (current_nodetype == LYS_OUTPUT) {
2927 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2928 }
2929
2930 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2931 break;
2932 }
2933 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002934 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002935 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002936 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002937 return LY_EVALID;
2938 }
2939 ++id;
2940 }
2941
2942 if (ret == LY_SUCCESS) {
2943 *target = ctx_node;
2944 if (nodetype && !(current_nodetype & nodetype)) {
2945 return LY_EDENIED;
2946 }
2947 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002948 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002949 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002950 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002951 }
2952
2953 return ret;
2954}
2955
2956/**
2957 * @brief Compile information about list's uniques.
2958 * @param[in] ctx Compile context.
2959 * @param[in] uniques Sized array list of unique statements.
2960 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2961 * @return LY_ERR value.
2962 */
2963static LY_ERR
2964lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2965{
2966 LY_ERR ret = LY_SUCCESS;
2967 struct lysc_node_leaf **key, ***unique;
2968 struct lysc_node *parent;
2969 const char *keystr, *delim;
2970 size_t len;
2971 LY_ARRAY_COUNT_TYPE v;
2972 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2973 uint16_t flags;
2974
2975 LY_ARRAY_FOR(uniques, v) {
2976 config = -1;
2977 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2978 keystr = uniques[v].str;
2979 while (keystr) {
2980 delim = strpbrk(keystr, " \t\n");
2981 if (delim) {
2982 len = delim - keystr;
2983 while (isspace(*delim)) {
2984 ++delim;
2985 }
2986 } else {
2987 len = strlen(keystr);
2988 }
2989
2990 /* unique node must be present */
2991 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002992 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02002993 LY_VALUE_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002994 if (ret != LY_SUCCESS) {
2995 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002996 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002997 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002998 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002999 }
3000 return LY_EVALID;
3001 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003002 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003003 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003004 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003005 return LY_EVALID;
3006 }
3007
3008 /* all referenced leafs must be of the same config type */
3009 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3010 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003011 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003012 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3013 return LY_EVALID;
3014 } else if ((*key)->flags & LYS_CONFIG_W) {
3015 config = 1;
3016 } else { /* LYS_CONFIG_R */
3017 config = 0;
3018 }
3019
3020 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3021 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3022 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003023 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003024 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3025 return LY_EVALID;
3026 }
3027 }
3028
3029 /* check status */
3030 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3031 (*key)->flags, (*key)->module, (*key)->name));
3032
3033 /* mark leaf as unique */
3034 (*key)->flags |= LYS_UNIQUE;
3035
3036 /* next unique value in line */
3037 keystr = delim;
3038 }
3039 /* next unique definition */
3040 }
3041
3042 return LY_SUCCESS;
3043}
3044
3045/**
3046 * @brief Compile parsed list node information.
3047 * @param[in] ctx Compile context
3048 * @param[in] pnode Parsed list node.
3049 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3050 * is enriched with the list-specific information.
3051 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3052 */
3053static LY_ERR
3054lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3055{
3056 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3057 struct lysc_node_list *list = (struct lysc_node_list *)node;
3058 struct lysp_node *child_p;
3059 struct lysc_node_leaf *key, *prev_key = NULL;
3060 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003061 const char *keystr, *delim;
3062 LY_ERR ret = LY_SUCCESS;
3063
3064 list->min = list_p->min;
3065 if (list->min) {
3066 list->flags |= LYS_MAND_TRUE;
3067 }
3068 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3069
3070 LY_LIST_FOR(list_p->child, child_p) {
3071 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3072 }
3073
Michal Vasko5347e3a2020-11-03 17:14:57 +01003074 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003075 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003076 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003077 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003078 }
3079
3080 /* keys */
3081 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003082 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003083 return LY_EVALID;
3084 }
3085
3086 /* find all the keys (must be direct children) */
3087 keystr = list_p->key;
3088 if (!keystr) {
3089 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003090 list->flags &= ~LYS_ORDBY_SYSTEM;
3091 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003092 }
3093 while (keystr) {
3094 delim = strpbrk(keystr, " \t\n");
3095 if (delim) {
3096 len = delim - keystr;
3097 while (isspace(*delim)) {
3098 ++delim;
3099 }
3100 } else {
3101 len = strlen(keystr);
3102 }
3103
3104 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003105 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3106 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003107 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003108 return LY_EVALID;
3109 }
3110 /* keys must be unique */
3111 if (key->flags & LYS_KEY) {
3112 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003113 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003114 return LY_EVALID;
3115 }
3116
Radek Krejcia6016992021-03-03 10:13:41 +01003117 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003118 /* key must have the same config flag as the list itself */
3119 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003120 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003121 return LY_EVALID;
3122 }
3123 if (ctx->pmod->version < LYS_VERSION_1_1) {
3124 /* YANG 1.0 denies key to be of empty type */
3125 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003126 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003127 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3128 return LY_EVALID;
3129 }
3130 } else {
3131 /* when and if-feature are illegal on list keys */
3132 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003133 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003134 "List's key must not have any \"when\" statement.");
3135 return LY_EVALID;
3136 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003137 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003138 }
3139
3140 /* check status */
3141 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3142 key->flags, key->module, key->name));
3143
3144 /* ignore default values of the key */
3145 if (key->dflt) {
3146 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3147 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3148 free(key->dflt);
3149 key->dflt = NULL;
3150 }
3151 /* mark leaf as key */
3152 key->flags |= LYS_KEY;
3153
3154 /* move it to the correct position */
3155 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3156 /* fix links in closest previous siblings of the key */
3157 if (key->next) {
3158 key->next->prev = key->prev;
3159 } else {
3160 /* last child */
3161 list->child->prev = key->prev;
3162 }
3163 if (key->prev->next) {
3164 key->prev->next = key->next;
3165 }
3166 /* fix links in the key */
3167 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003168 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003169 key->next = prev_key->next;
3170 } else {
3171 key->prev = list->child->prev;
3172 key->next = list->child;
3173 }
3174 /* fix links in closes future siblings of the key */
3175 if (prev_key) {
3176 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003177 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003178 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003179 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003180 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003181 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003182 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003183 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003184 }
3185 /* fix links in parent */
3186 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003187 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003188 }
3189 }
3190
3191 /* next key value */
3192 prev_key = key;
3193 keystr = delim;
3194 lysc_update_path(ctx, NULL, NULL);
3195 }
3196
3197 /* uniques */
3198 if (list_p->uniques) {
3199 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3200 }
3201
Radek Krejci2a9fc652021-01-22 17:44:34 +01003202 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3203 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3204 LY_CHECK_GOTO(ret, done);
3205 }
3206 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3207 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3208 LY_CHECK_GOTO(ret, done);
3209 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003210
3211 /* checks */
3212 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003213 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003214 list->min, list->max);
3215 return LY_EVALID;
3216 }
3217
3218done:
3219 return ret;
3220}
3221
3222/**
3223 * @brief Do some checks and set the default choice's case.
3224 *
3225 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3226 *
3227 * @param[in] ctx Compile context.
3228 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3229 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3230 * @return LY_ERR value.
3231 */
3232static LY_ERR
3233lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3234{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003235 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003236 const struct lys_module *mod;
3237 const char *prefix = NULL, *name;
3238 size_t prefix_len = 0;
3239
3240 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3241 name = strchr(dflt->str, ':');
3242 if (name) {
3243 prefix = dflt->str;
3244 prefix_len = name - prefix;
3245 ++name;
3246 } else {
3247 name = dflt->str;
3248 }
3249 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003250 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003251 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003252 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003253 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003254 return LY_EVALID;
3255 }
3256 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003257 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003258 }
3259
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003260 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 +02003261 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003262 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003263 "Default case \"%s\" not found.", dflt->str);
3264 return LY_EVALID;
3265 }
3266
3267 /* no mandatory nodes directly under the default case */
3268 LY_LIST_FOR(ch->dflt->child, iter) {
3269 if (iter->parent != (struct lysc_node *)ch->dflt) {
3270 break;
3271 }
3272 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003273 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003274 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3275 return LY_EVALID;
3276 }
3277 }
3278
3279 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003280 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003281 return LY_EVALID;
3282 }
3283
3284 ch->dflt->flags |= LYS_SET_DFLT;
3285 return LY_SUCCESS;
3286}
3287
3288LY_ERR
3289lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3290 struct ly_set *child_set)
3291{
3292 LY_ERR ret = LY_SUCCESS;
3293 struct lysp_node *child_p_next = child_p->next;
3294 struct lysp_node_case *cs_p;
3295
3296 if (child_p->nodetype == LYS_CASE) {
3297 /* standard case under choice */
3298 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3299 } else {
3300 /* we need the implicit case first, so create a fake parsed case */
3301 cs_p = calloc(1, sizeof *cs_p);
3302 cs_p->nodetype = LYS_CASE;
3303 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3304 cs_p->child = child_p;
3305
3306 /* make the child the only case child */
3307 child_p->next = NULL;
3308
3309 /* compile it normally */
3310 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3311
3312free_fake_node:
3313 /* free the fake parsed node and correct pointers back */
3314 cs_p->child = NULL;
3315 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3316 child_p->next = child_p_next;
3317 }
3318
3319 return ret;
3320}
3321
3322/**
3323 * @brief Compile parsed choice node information.
3324 *
3325 * @param[in] ctx Compile context
3326 * @param[in] pnode Parsed choice node.
3327 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3328 * is enriched with the choice-specific information.
3329 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3330 */
3331static LY_ERR
3332lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3333{
3334 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3335 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3336 struct lysp_node *child_p;
3337 LY_ERR ret = LY_SUCCESS;
3338
3339 assert(node->nodetype == LYS_CHOICE);
3340
3341 LY_LIST_FOR(ch_p->child, child_p) {
3342 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3343 }
3344
3345 /* default branch */
3346 if (ch_p->dflt.str) {
3347 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3348 }
3349
3350 return ret;
3351}
3352
3353/**
3354 * @brief Compile parsed anydata or anyxml node information.
3355 * @param[in] ctx Compile context
3356 * @param[in] pnode Parsed anydata or anyxml node.
3357 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3358 * is enriched with the any-specific information.
3359 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3360 */
3361static LY_ERR
3362lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3363{
3364 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3365 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003366 LY_ERR ret = LY_SUCCESS;
3367
Michal Vasko5347e3a2020-11-03 17:14:57 +01003368 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003369 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003370 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003371 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003372 LY_CHECK_GOTO(ret, done);
3373 }
3374
Radek Krejci91531e12021-02-08 19:57:54 +01003375 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003376 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003377 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3378 }
3379done:
3380 return ret;
3381}
3382
3383/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003384 * @brief Prepare the case structure in choice node for the new data node.
3385 *
3386 * 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
3387 * created in the choice when the first child was processed.
3388 *
3389 * @param[in] ctx Compile context.
3390 * @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,
3391 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3392 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3393 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3394 * @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,
3395 * it is linked from the case structure only in case it is its first child.
3396 */
3397static LY_ERR
3398lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3399{
3400 struct lysp_node *child_p;
3401 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3402
3403 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3404 /* we have to add an implicit case node into the parent choice */
3405 } else if (pnode->nodetype == LYS_CASE) {
3406 /* explicit parent case */
3407 LY_LIST_FOR(cs_p->child, child_p) {
3408 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3409 }
3410 } else {
3411 LOGINT_RET(ctx->ctx);
3412 }
3413
3414 return LY_SUCCESS;
3415}
3416
3417void
3418lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3419{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003420 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003421
3422 if (add) { /* set flag */
3423 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3424 parent = parent->parent) {
3425 parent->flags |= LYS_MAND_TRUE;
3426 }
3427 } else { /* unset flag */
3428 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003429 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003430 if (iter->flags & LYS_MAND_TRUE) {
3431 /* there is another mandatory node */
3432 return;
3433 }
3434 }
3435 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3436 parent->flags &= ~LYS_MAND_TRUE;
3437 }
3438 }
3439}
3440
3441/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003442 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003443 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003444 * @param[in] name Name of the grouping to find,
3445 * @return NULL when there is no grouping with the specified name
3446 * @return Pointer to the grouping of the specified @p name.
3447 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003448static struct lysp_node_grp *
3449match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003450{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003451 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003452
Radek Krejci2a9fc652021-01-22 17:44:34 +01003453 LY_LIST_FOR(grps, grp) {
3454 if (!strcmp(grp->name, name)) {
3455 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003456 }
3457 }
3458
3459 return NULL;
3460}
3461
3462/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003463 * @brief Find grouping for a uses.
3464 *
3465 * @param[in] ctx Compile context.
3466 * @param[in] uses_p Parsed uses node.
3467 * @param[out] gpr_p Found grouping on success.
3468 * @param[out] grp_pmod Module of @p grp_p on success.
3469 * @return LY_ERR value.
3470 */
3471static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003472lys_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 +02003473 struct lysp_module **grp_pmod)
3474{
3475 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003476 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003477 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003478 const char *id, *name, *prefix, *local_pref;
3479 size_t prefix_len, name_len;
3480 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003481 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003482
3483 *grp_p = NULL;
3484 *grp_pmod = NULL;
3485
3486 /* search for the grouping definition */
3487 id = uses_p->name;
3488 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3489 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3490 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3491 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003492 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003493 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003494 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3495 found = ctx->pmod;
3496 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003497 }
3498 }
3499 } else {
3500 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003501 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003502 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003503 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003504 "Invalid prefix used for grouping reference.", uses_p->name);
3505 return LY_EVALID;
3506 }
3507 pmod = mod->parsed;
3508 }
3509
3510 if (!found) {
3511 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003512 if ((grp = match_grouping(pmod->groupings, name))) {
3513 found = pmod;
3514 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003515 /* ... and all the submodules */
3516 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003517 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3518 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003519 break;
3520 }
3521 }
3522 }
3523 }
3524 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003525 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003526 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3527 return LY_EVALID;
3528 }
3529
3530 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3531 /* remember that the grouping is instantiated to avoid its standalone validation */
3532 grp->flags |= LYS_USED_GRP;
3533 }
3534
3535 *grp_p = grp;
3536 *grp_pmod = found;
3537 return LY_SUCCESS;
3538}
3539
3540/**
3541 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3542 * If present, also apply uses's modificators.
3543 *
3544 * @param[in] ctx Compile context
3545 * @param[in] uses_p Parsed uses schema node.
3546 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3547 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3548 * the compile context.
3549 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3550 */
3551static LY_ERR
3552lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3553{
3554 struct lysp_node *pnode;
3555 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003556 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003557 uint32_t i, grp_stack_count;
3558 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3559 LY_ERR ret = LY_SUCCESS;
3560 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003561 struct ly_set uses_child_set = {0};
3562
3563 /* find the referenced grouping */
3564 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3565
3566 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3567 grp_stack_count = ctx->groupings.count;
3568 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3569 if (grp_stack_count == ctx->groupings.count) {
3570 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003571 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003572 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3573 return LY_EVALID;
3574 }
3575
3576 /* check status */
3577 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3578 LY_CHECK_GOTO(ret, cleanup);
3579
3580 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3581 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3582 LY_CHECK_GOTO(ret, cleanup);
3583
3584 /* switch context's parsed module being processed */
3585 ctx->pmod = grp_mod;
3586
3587 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003588 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003589 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3590 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 +02003591 LY_CHECK_GOTO(ret, cleanup);
3592 }
3593
3594 if (child_set) {
3595 /* add these children to our compiled child_set as well since uses is a schema-only node */
3596 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3597 }
3598
3599 if (uses_p->when) {
3600 /* pass uses's when to all the data children */
3601 for (i = 0; i < uses_child_set.count; ++i) {
3602 child = uses_child_set.snodes[i];
3603
Michal Vasko72244882021-01-12 15:21:05 +01003604 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 +02003605 LY_CHECK_GOTO(ret, cleanup);
3606 }
3607 }
3608
3609 /* compile actions */
3610 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003611 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003612 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3613 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003614 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003615 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3616 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003617 ret = LY_EVALID;
3618 goto cleanup;
3619 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003620 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3621 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3622 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3623 LY_CHECK_GOTO(ret, cleanup);
3624 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003625
3626 if (uses_p->when) {
3627 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003628 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3629 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 +02003630 LY_CHECK_GOTO(ret, cleanup);
3631 }
3632 }
3633 }
3634
3635 /* compile notifications */
3636 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003637 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003638 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3639 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003640 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003641 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3642 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003643 ret = LY_EVALID;
3644 goto cleanup;
3645 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003646
3647 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3648 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3649 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3650 LY_CHECK_GOTO(ret, cleanup);
3651 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003652
3653 if (uses_p->when) {
3654 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003655 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3656 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 +02003657 LY_CHECK_GOTO(ret, cleanup);
3658 }
3659 }
3660 }
3661
3662 /* check that all augments were applied */
3663 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003664 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3665 /* augment of some parent uses, irrelevant now */
3666 continue;
3667 }
3668
3669 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003670 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3671 ret = LY_ENOTFOUND;
3672 }
3673 LY_CHECK_GOTO(ret, cleanup);
3674
3675 /* check that all refines were applied */
3676 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003677 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3678 /* refine of some paretn uses, irrelevant now */
3679 continue;
3680 }
3681
3682 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003683 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3684 ret = LY_ENOTFOUND;
3685 }
3686 LY_CHECK_GOTO(ret, cleanup);
3687
3688cleanup:
3689 /* reload previous context's parsed module being processed */
3690 ctx->pmod = mod_old;
3691
3692 /* remove the grouping from the stack for circular groupings dependency check */
3693 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3694 assert(ctx->groupings.count == grp_stack_count);
3695
3696 ly_set_erase(&uses_child_set, NULL);
3697 return ret;
3698}
3699
3700static int
3701lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3702{
3703 struct lysp_node *iter;
3704 int len = 0;
3705
3706 *path = NULL;
3707 for (iter = node; iter && len >= 0; iter = iter->parent) {
3708 char *s = *path;
3709 char *id;
3710
3711 switch (iter->nodetype) {
3712 case LYS_USES:
3713 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3714 break;
3715 case LYS_GROUPING:
3716 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3717 break;
3718 case LYS_AUGMENT:
3719 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3720 break;
3721 default:
3722 id = strdup(iter->name);
3723 break;
3724 }
3725
3726 if (!iter->parent) {
3727 /* print prefix */
3728 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3729 } else {
3730 /* prefix is the same as in parent */
3731 len = asprintf(path, "/%s%s", id, s ? s : "");
3732 }
3733 free(s);
3734 free(id);
3735 }
3736
3737 if (len < 0) {
3738 free(*path);
3739 *path = NULL;
3740 } else if (len == 0) {
3741 *path = strdup("/");
3742 len = 1;
3743 }
3744 return len;
3745}
3746
3747LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003748lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003749{
3750 LY_ERR ret;
3751 char *path;
3752 int len;
3753
3754 struct lysp_node_uses fake_uses = {
3755 .parent = pnode,
3756 .nodetype = LYS_USES,
3757 .flags = 0, .next = NULL,
3758 .name = grp->name,
3759 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3760 .refines = NULL, .augments = NULL
3761 };
3762 struct lysc_node_container fake_container = {
3763 .nodetype = LYS_CONTAINER,
3764 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3765 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003766 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003767 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003768 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003769 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003770 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3771 };
3772
3773 if (grp->parent) {
3774 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3775 }
3776
3777 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3778 if (len < 0) {
3779 LOGMEM(ctx->ctx);
3780 return LY_EMEM;
3781 }
3782 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3783 ctx->path_len = (uint32_t)len;
3784 free(path);
3785
3786 lysc_update_path(ctx, NULL, "{grouping}");
3787 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003788 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003789 lysc_update_path(ctx, NULL, NULL);
3790 lysc_update_path(ctx, NULL, NULL);
3791
3792 ctx->path_len = 1;
3793 ctx->path[1] = '\0';
3794
3795 /* cleanup */
3796 lysc_node_container_free(ctx->ctx, &fake_container);
3797
3798 return ret;
3799}
3800
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003801LY_ERR
3802lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3803 struct ly_set *child_set)
3804{
3805 LY_ERR ret = LY_SUCCESS;
3806 struct lysc_node *node = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003807 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003808
3809 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3810
3811 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003812 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003813 } else {
3814 lysc_update_path(ctx, NULL, "{uses}");
3815 lysc_update_path(ctx, NULL, pnode->name);
3816 }
3817
3818 switch (pnode->nodetype) {
3819 case LYS_CONTAINER:
3820 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3821 node_compile_spec = lys_compile_node_container;
3822 break;
3823 case LYS_LEAF:
3824 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3825 node_compile_spec = lys_compile_node_leaf;
3826 break;
3827 case LYS_LIST:
3828 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3829 node_compile_spec = lys_compile_node_list;
3830 break;
3831 case LYS_LEAFLIST:
3832 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3833 node_compile_spec = lys_compile_node_leaflist;
3834 break;
3835 case LYS_CHOICE:
3836 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3837 node_compile_spec = lys_compile_node_choice;
3838 break;
3839 case LYS_CASE:
3840 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3841 node_compile_spec = lys_compile_node_case;
3842 break;
3843 case LYS_ANYXML:
3844 case LYS_ANYDATA:
3845 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3846 node_compile_spec = lys_compile_node_any;
3847 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003848 case LYS_RPC:
3849 case LYS_ACTION:
Radek Krejci91531e12021-02-08 19:57:54 +01003850 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003851 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3852 "Action \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003853 (ctx->options & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003854 return LY_EVALID;
3855 }
3856 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3857 node_compile_spec = lys_compile_node_action;
Radek Krejci91531e12021-02-08 19:57:54 +01003858 ctx->options |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003859 break;
3860 case LYS_NOTIF:
Radek Krejci91531e12021-02-08 19:57:54 +01003861 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003862 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3863 "Notification \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003864 (ctx->options & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003865 return LY_EVALID;
3866 }
3867 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3868 node_compile_spec = lys_compile_node_notif;
3869 ctx->options |= LYS_COMPILE_NOTIFICATION;
3870 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003871 case LYS_USES:
3872 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3873 lysc_update_path(ctx, NULL, NULL);
3874 lysc_update_path(ctx, NULL, NULL);
3875 return ret;
3876 default:
3877 LOGINT(ctx->ctx);
3878 return LY_EINT;
3879 }
3880 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3881
Radek Krejci2a9fc652021-01-22 17:44:34 +01003882 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003883
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003884 ctx->options = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003885 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003886 return ret;
3887}