blob: b2cc259e7eda9c63bddd70c11c7cc1f27a958323 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of common nodes.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020017
18#include "schema_compile_node.h"
19
20#include <assert.h>
21#include <ctype.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "common.h"
29#include "compat.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020030#include "dict.h"
31#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020032#include "plugins_exts.h"
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 Krejci2926c1b2021-03-16 14:45:45 +0100238 LY_PREFIX_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 Krejci18c99162021-03-16 16:55:16 +0100244 LY_CHECK_RET(ly_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci2926c1b2021-03-16 14:45:45 +0100245 LY_PREF_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 Krejci2926c1b2021-03-16 14:45:45 +0100303 LY_PREFIX_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 Krejci18c99162021-03-16 16:55:16 +0100306 LY_CHECK_RET(ly_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
Radek Krejci2926c1b2021-03-16 14:45:45 +0100307 LY_PREF_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{
609 LY_ERR ret = LY_EVALID;
610 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);
627 goto cleanup;
628 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100629 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200630 "Invalid %s restriction - unexpected end of the expression (%s).",
631 length_restr ? "length" : "range", range_p->arg);
632 goto cleanup;
633 }
634 parts_done++;
635 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100636 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200637 if (parts) {
638 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100639 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200640 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100641 (int)(expr - range_p->arg.str), range_p->arg.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200642 goto cleanup;
643 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100644 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200645
646 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
647 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
648 part->max_64 = part->min_64;
649 } else if (*expr == '|') {
650 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100651 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200652 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
653 goto cleanup;
654 }
655 expr++;
656 parts_done++;
657 /* process next part of the expression */
658 } else if (!strncmp(expr, "..", 2)) {
659 expr += 2;
660 while (isspace(*expr)) {
661 expr++;
662 }
663 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100664 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200665 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
666 goto cleanup;
667 }
668 /* continue expecting the upper boundary */
669 range_expected = 1;
670 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
671 /* number */
672 if (range_expected) {
673 part = &parts[LY_ARRAY_COUNT(parts) - 1];
674 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
675 range_expected = 0;
676 } else {
677 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
678 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
679 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
680 part->max_64 = part->min_64;
681 }
682
683 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100684 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
685 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200686 while (isspace(*expr)) {
687 expr++;
688 }
689 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100690 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200691 length_restr ? "length" : "range", expr);
692 goto cleanup;
693 }
694 if (range_expected) {
695 part = &parts[LY_ARRAY_COUNT(parts) - 1];
696 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
697 range_expected = 0;
698 } else {
699 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
700 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
701 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
702 part->min_64 = part->max_64;
703 }
704 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100705 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200706 length_restr ? "length" : "range", expr);
707 goto cleanup;
708 }
709 }
710
711 /* check with the previous range/length restriction */
712 if (base_range) {
713 switch (basetype) {
714 case LY_TYPE_BINARY:
715 case LY_TYPE_UINT8:
716 case LY_TYPE_UINT16:
717 case LY_TYPE_UINT32:
718 case LY_TYPE_UINT64:
719 case LY_TYPE_STRING:
720 uns = 1;
721 break;
722 case LY_TYPE_DEC64:
723 case LY_TYPE_INT8:
724 case LY_TYPE_INT16:
725 case LY_TYPE_INT32:
726 case LY_TYPE_INT64:
727 uns = 0;
728 break;
729 default:
730 LOGINT(ctx->ctx);
731 ret = LY_EINT;
732 goto cleanup;
733 }
734 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
735 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
736 goto baseerror;
737 }
738 /* current lower bound is not lower than the base */
739 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
740 /* base has single value */
741 if (base_range->parts[v].min_64 == parts[u].min_64) {
742 /* both lower bounds are the same */
743 if (parts[u].min_64 != parts[u].max_64) {
744 /* current continues with a range */
745 goto baseerror;
746 } else {
747 /* equal single values, move both forward */
748 ++v;
749 continue;
750 }
751 } else {
752 /* base is single value lower than current range, so the
753 * value from base range is removed in the current,
754 * move only base and repeat checking */
755 ++v;
756 --u;
757 continue;
758 }
759 } else {
760 /* base is the range */
761 if (parts[u].min_64 == parts[u].max_64) {
762 /* current is a single value */
763 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
764 /* current is behind the base range, so base range is omitted,
765 * move the base and keep the current for further check */
766 ++v;
767 --u;
768 } /* else it is within the base range, so move the current, but keep the base */
769 continue;
770 } else {
771 /* both are ranges - check the higher bound, the lower was already checked */
772 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
773 /* higher bound is higher than the current higher bound */
774 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
775 /* but the current lower bound is also higher, so the base range is omitted,
776 * continue with the same current, but move the base */
777 --u;
778 ++v;
779 continue;
780 }
781 /* current range starts within the base range but end behind it */
782 goto baseerror;
783 } else {
784 /* current range is smaller than the base,
785 * move current, but stay with the base */
786 continue;
787 }
788 }
789 }
790 }
791 if (u != parts_done) {
792baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100793 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200794 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
795 length_restr ? "length" : "range", range_p->arg);
796 goto cleanup;
797 }
798 }
799
800 if (!(*range)) {
801 *range = calloc(1, sizeof **range);
802 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
803 }
804
805 /* we rewrite the following values as the types chain is being processed */
806 if (range_p->eapptag) {
807 lydict_remove(ctx->ctx, (*range)->eapptag);
808 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
809 }
810 if (range_p->emsg) {
811 lydict_remove(ctx->ctx, (*range)->emsg);
812 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
813 }
814 if (range_p->dsc) {
815 lydict_remove(ctx->ctx, (*range)->dsc);
816 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
817 }
818 if (range_p->ref) {
819 lydict_remove(ctx->ctx, (*range)->ref);
820 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
821 }
822 /* extensions are taken only from the last range by the caller */
823
824 (*range)->parts = parts;
825 parts = NULL;
826 ret = LY_SUCCESS;
827cleanup:
828 LY_ARRAY_FREE(parts);
829
830 return ret;
831}
832
833LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100834lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200835{
836 size_t idx, idx2, start, end, size, brack;
837 char *perl_regex, *ptr;
Christian Hoppsdafed222021-03-22 13:02:42 -0400838 int err_code, compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200839 const char *orig_ptr;
840 PCRE2_SIZE err_offset;
841 pcre2_code *code_local;
842
843#define URANGE_LEN 19
844 char *ublock2urange[][2] = {
845 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
846 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
847 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
848 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
849 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
850 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
851 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
852 {"Greek", "[\\x{0370}-\\x{03FF}]"},
853 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
854 {"Armenian", "[\\x{0530}-\\x{058F}]"},
855 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
856 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
857 {"Syriac", "[\\x{0700}-\\x{074F}]"},
858 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
859 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
860 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
861 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
862 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
863 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
864 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
865 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
866 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
867 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
868 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
869 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
870 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
871 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
872 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
873 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
874 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
875 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
876 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
877 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
878 {"Ogham", "[\\x{1680}-\\x{169F}]"},
879 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
880 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
881 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
882 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
883 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
884 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
885 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
886 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
887 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
888 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
889 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
890 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
891 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
892 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
893 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
894 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
895 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
896 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
897 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
898 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
899 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
900 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
901 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
902 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
903 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
904 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
905 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
906 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
907 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
908 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
909 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
910 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
911 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
912 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
913 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
914 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
915 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
916 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
917 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
918 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
919 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
920 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
921 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
922 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
923 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
924 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
925 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
926 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
927 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100928 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200929 {NULL, NULL}
930 };
931
932 /* adjust the expression to a Perl equivalent
933 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
934
935 /* allocate space for the transformed pattern */
936 size = strlen(pattern) + 1;
Christian Hoppsdafed222021-03-22 13:02:42 -0400937 compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
938#ifdef PCRE2_ENDANCHORED
939 compile_opts |= PCRE2_ENDANCHORED;
940#else
941 /* add space for trailing $ anchor */
942 size++;
943#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200944 perl_regex = malloc(size);
945 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
946 perl_regex[0] = '\0';
947
948 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
949 brack = 0;
950 idx = 0;
951 orig_ptr = pattern;
952 while (orig_ptr[0]) {
953 switch (orig_ptr[0]) {
954 case '$':
955 case '^':
956 if (!brack) {
957 /* make space for the extra character */
958 ++size;
959 perl_regex = ly_realloc(perl_regex, size);
960 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
961
962 /* print escape slash */
963 perl_regex[idx] = '\\';
964 ++idx;
965 }
966 break;
967 case '[':
968 /* must not be escaped */
969 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
970 ++brack;
971 }
972 break;
973 case ']':
974 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
975 /* pattern was checked and compiled already */
976 assert(brack);
977 --brack;
978 }
979 break;
980 default:
981 break;
982 }
983
984 /* copy char */
985 perl_regex[idx] = orig_ptr[0];
986
987 ++idx;
988 ++orig_ptr;
989 }
Christian Hoppsdafed222021-03-22 13:02:42 -0400990#ifndef PCRE2_ENDANCHORED
991 /* anchor match to end of subject */
992 perl_regex[idx++] = '$';
993#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200994 perl_regex[idx] = '\0';
995
996 /* substitute Unicode Character Blocks with exact Character Ranges */
997 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
998 start = ptr - perl_regex;
999
1000 ptr = strchr(ptr, '}');
1001 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001002 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001003 free(perl_regex);
1004 return LY_EVALID;
1005 }
1006 end = (ptr - perl_regex) + 1;
1007
1008 /* need more space */
1009 if (end - start < URANGE_LEN) {
1010 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1011 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1012 }
1013
1014 /* find our range */
1015 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001016 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1017 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001018 break;
1019 }
1020 }
1021 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001022 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001023 free(perl_regex);
1024 return LY_EVALID;
1025 }
1026
1027 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1028 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1029 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1030 ++idx;
1031 }
1032 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1033 --idx;
1034 }
1035 }
1036 if (idx) {
1037 /* skip brackets */
1038 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1039 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1040 } else {
1041 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1042 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1043 }
1044 }
1045
1046 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001047 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001048 &err_code, &err_offset, NULL);
1049 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001050 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1051 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001052 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001053 free(perl_regex);
1054 return LY_EVALID;
1055 }
1056 free(perl_regex);
1057
1058 if (code) {
1059 *code = code_local;
1060 } else {
1061 free(code_local);
1062 }
1063
1064 return LY_SUCCESS;
1065
1066#undef URANGE_LEN
1067}
1068
1069/**
1070 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1071 * @param[in] ctx Compile context.
1072 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1073 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1074 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1075 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1076 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1077 */
1078static LY_ERR
1079lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1080 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1081{
1082 struct lysc_pattern **pattern;
1083 LY_ARRAY_COUNT_TYPE u;
1084 LY_ERR ret = LY_SUCCESS;
1085
1086 /* first, copy the patterns from the base type */
1087 if (base_patterns) {
1088 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1089 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1090 }
1091
1092 LY_ARRAY_FOR(patterns_p, u) {
1093 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1094 *pattern = calloc(1, sizeof **pattern);
1095 ++(*pattern)->refcount;
1096
Radek Krejci2efc45b2020-12-22 16:25:44 +01001097 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001098 LY_CHECK_RET(ret);
1099
Radek Krejcif13b87b2020-12-01 22:02:17 +01001100 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001101 (*pattern)->inverted = 1;
1102 }
1103 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1104 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1105 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1106 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1107 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001108 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001109 }
1110done:
1111 return ret;
1112}
1113
1114/**
1115 * @brief map of the possible restrictions combination for the specific built-in type.
1116 */
1117static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1118 0 /* LY_TYPE_UNKNOWN */,
1119 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1120 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1121 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1122 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1123 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1124 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1125 LYS_SET_BIT /* LY_TYPE_BITS */,
1126 0 /* LY_TYPE_BOOL */,
1127 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1128 0 /* LY_TYPE_EMPTY */,
1129 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1130 LYS_SET_BASE /* LY_TYPE_IDENT */,
1131 LYS_SET_REQINST /* LY_TYPE_INST */,
1132 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1133 LYS_SET_TYPE /* LY_TYPE_UNION */,
1134 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1135 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1136 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1137 LYS_SET_RANGE /* LY_TYPE_INT64 */
1138};
1139
1140/**
1141 * @brief stringification of the YANG built-in data types
1142 */
1143const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001144 LY_TYPE_UNKNOWN_STR,
1145 LY_TYPE_BINARY_STR,
1146 LY_TYPE_UINT8_STR,
1147 LY_TYPE_UINT16_STR,
1148 LY_TYPE_UINT32_STR,
1149 LY_TYPE_UINT64_STR,
1150 LY_TYPE_STRING_STR,
1151 LY_TYPE_BITS_STR,
1152 LY_TYPE_BOOL_STR,
1153 LY_TYPE_DEC64_STR,
1154 LY_TYPE_EMPTY_STR,
1155 LY_TYPE_ENUM_STR,
1156 LY_TYPE_IDENT_STR,
1157 LY_TYPE_INST_STR,
1158 LY_TYPE_LEAFREF_STR,
1159 LY_TYPE_UNION_STR,
1160 LY_TYPE_INT8_STR,
1161 LY_TYPE_INT16_STR,
1162 LY_TYPE_INT32_STR,
1163 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001164};
1165
1166/**
1167 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1168 * @param[in] ctx Compile context.
1169 * @param[in] enums_p Array of the parsed enum structures to compile.
1170 * @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.
1171 * @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 +01001172 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001173 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1174 */
1175static LY_ERR
1176lys_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 +01001177 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001178{
1179 LY_ERR ret = LY_SUCCESS;
1180 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001181 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1182 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001183 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001184 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001185
1186 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001187 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001188 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1189 return LY_EVALID;
1190 }
1191
1192 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001193 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001194 if (base_enums) {
1195 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1196 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001197 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001198 break;
1199 }
1200 }
1201 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001202 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001203 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001204 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001205 return LY_EVALID;
1206 }
1207 match = v;
1208 }
1209
1210 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001211 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001212 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001213 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001214 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001215 LY_ARRAY_FOR(*bitenums, v) {
1216 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001217 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001218 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001219 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 return LY_EVALID;
1221 }
1222 }
1223 } else if (base_enums) {
1224 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001225 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001226 } else {
1227 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001228 if (u == 0) {
1229 cur_val = 0;
1230 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001231 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001232 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001233 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001234 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001235 } else {
1236 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001237 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001238 }
1239
1240 /* save highest value for auto assing */
1241 if (highest_value < cur_val) {
1242 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001243 }
1244 } else { /* LY_TYPE_BITS */
1245 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001246 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001247 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001248 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001249 LY_ARRAY_FOR(*bitenums, v) {
1250 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001251 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001252 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001253 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 return LY_EVALID;
1255 }
1256 }
1257 } else if (base_enums) {
1258 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001259 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 } else {
1261 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001262 if (u == 0) {
1263 cur_pos = 0;
1264 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001265 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001266 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001267 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001268 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001269 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001270 } else {
1271 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001272 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001273 }
1274
1275 /* save highest position for auto assing */
1276 if (highest_position < cur_pos) {
1277 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001278 }
1279 }
1280
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001281 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001282 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001283 if (basetype == LY_TYPE_ENUM) {
1284 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001285 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001286 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001287 enums_p[u].name, base_enums[match].value, cur_val);
1288 return LY_EVALID;
1289 }
1290 } else {
1291 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001292 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001293 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001294 enums_p[u].name, base_enums[match].position, cur_pos);
1295 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001296 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001297 }
1298 }
1299
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001300 /* evaluate if-ffeatures */
1301 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1302 if (!enabled) {
1303 continue;
1304 }
1305
1306 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001307 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001308 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1309 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1310 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001311 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 +01001312 if (basetype == LY_TYPE_ENUM) {
1313 e->value = cur_val;
1314 } else {
1315 e->position = cur_pos;
1316 }
Radek Krejciab430862021-03-02 20:13:40 +01001317 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001318
1319 if (basetype == LY_TYPE_BITS) {
1320 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001321 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001322 if (v != u) {
1323 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001324 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1325 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001326 }
1327 }
1328 }
1329
1330done:
1331 return ret;
1332}
1333
Radek Krejci6a205692020-12-09 13:58:22 +01001334static LY_ERR
1335lys_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 +01001336 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001337{
1338 LY_ERR ret = LY_SUCCESS;
1339 struct lysc_type **utypes = *utypes_p;
1340 struct lysc_type_union *un_aux = NULL;
1341
1342 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1343 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001344 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1345 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001346 LY_CHECK_GOTO(ret, error);
1347 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1348 /* add space for additional types from the union subtype */
1349 un_aux = (struct lysc_type_union *)utypes[u + additional];
1350 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1351 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1352
1353 /* copy subtypes of the subtype union */
1354 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1355 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1356 struct lysc_type_leafref *lref;
1357
1358 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1359 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1360 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1361 lref = (struct lysc_type_leafref *)utypes[u + additional];
1362
1363 lref->basetype = LY_TYPE_LEAFREF;
1364 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1365 LY_CHECK_GOTO(ret, error);
1366 lref->refcount = 1;
1367 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
1368 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001369 ret = ly_type_prefix_data_dup(ctx->ctx, LY_PREF_SCHEMA_RESOLVED,
1370 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001371 LY_CHECK_GOTO(ret, error);
1372 /* TODO extensions */
1373
1374 } else {
1375 utypes[u + additional] = un_aux->types[v];
1376 ++un_aux->types[v]->refcount;
1377 }
1378 ++additional;
1379 LY_ARRAY_INCREMENT(utypes);
1380 }
1381 /* compensate u increment in main loop */
1382 --additional;
1383
1384 /* free the replaced union subtype */
1385 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1386 un_aux = NULL;
1387 } else {
1388 LY_ARRAY_INCREMENT(utypes);
1389 }
1390 }
1391
1392 *utypes_p = utypes;
1393 return LY_SUCCESS;
1394
1395error:
1396 if (un_aux) {
1397 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1398 }
1399 *utypes_p = utypes;
1400 return ret;
1401}
1402
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001403/**
1404 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1405 * @param[in] ctx Compile context.
1406 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1407 * @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 +02001408 * @param[in] context_name Name of the context node or referencing typedef for logging.
1409 * @param[in] type_p Parsed type to compile.
1410 * @param[in] basetype Base YANG built-in type of the type to compile.
1411 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1412 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1413 * @param[out] type Newly created type structure with the filled information about the type.
1414 * @return LY_ERR value.
1415 */
1416static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001417lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1418 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 +02001419{
1420 LY_ERR ret = LY_SUCCESS;
1421 struct lysc_type_bin *bin;
1422 struct lysc_type_num *num;
1423 struct lysc_type_str *str;
1424 struct lysc_type_bits *bits;
1425 struct lysc_type_enum *enumeration;
1426 struct lysc_type_dec *dec;
1427 struct lysc_type_identityref *idref;
1428 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001429 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001430
1431 switch (basetype) {
1432 case LY_TYPE_BINARY:
1433 bin = (struct lysc_type_bin *)(*type);
1434
1435 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1436 if (type_p->length) {
1437 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1438 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1439 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001440 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001441 }
1442 }
1443 break;
1444 case LY_TYPE_BITS:
1445 /* RFC 7950 9.7 - bits */
1446 bits = (struct lysc_type_bits *)(*type);
1447 if (type_p->bits) {
1448 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1449 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1450 (struct lysc_type_bitenum_item **)&bits->bits));
1451 }
1452
1453 if (!base && !type_p->flags) {
1454 /* type derived from bits built-in type must contain at least one bit */
1455 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001456 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001457 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001458 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001459 }
1460 return LY_EVALID;
1461 }
1462 break;
1463 case LY_TYPE_DEC64:
1464 dec = (struct lysc_type_dec *)(*type);
1465
1466 /* RFC 7950 9.3.4 - fraction-digits */
1467 if (!base) {
1468 if (!type_p->fraction_digits) {
1469 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001470 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001471 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001472 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001473 }
1474 return LY_EVALID;
1475 }
1476 dec->fraction_digits = type_p->fraction_digits;
1477 } else {
1478 if (type_p->fraction_digits) {
1479 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1480 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001481 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001482 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1483 tpdfname);
1484 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001485 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001486 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1487 }
1488 return LY_EVALID;
1489 }
1490 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1491 }
1492
1493 /* RFC 7950 9.2.4 - range */
1494 if (type_p->range) {
1495 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1496 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1497 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001498 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001499 }
1500 }
1501 break;
1502 case LY_TYPE_STRING:
1503 str = (struct lysc_type_str *)(*type);
1504
1505 /* RFC 7950 9.4.4 - length */
1506 if (type_p->length) {
1507 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1508 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1509 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001510 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001511 }
1512 } else if (base && ((struct lysc_type_str *)base)->length) {
1513 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1514 }
1515
1516 /* RFC 7950 9.4.5 - pattern */
1517 if (type_p->patterns) {
1518 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1519 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1520 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1521 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1522 }
1523 break;
1524 case LY_TYPE_ENUM:
1525 enumeration = (struct lysc_type_enum *)(*type);
1526
1527 /* RFC 7950 9.6 - enum */
1528 if (type_p->enums) {
1529 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1530 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1531 }
1532
1533 if (!base && !type_p->flags) {
1534 /* type derived from enumerations built-in type must contain at least one enum */
1535 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001536 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001537 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001538 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001539 }
1540 return LY_EVALID;
1541 }
1542 break;
1543 case LY_TYPE_INT8:
1544 case LY_TYPE_UINT8:
1545 case LY_TYPE_INT16:
1546 case LY_TYPE_UINT16:
1547 case LY_TYPE_INT32:
1548 case LY_TYPE_UINT32:
1549 case LY_TYPE_INT64:
1550 case LY_TYPE_UINT64:
1551 num = (struct lysc_type_num *)(*type);
1552
1553 /* RFC 6020 9.2.4 - range */
1554 if (type_p->range) {
1555 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1556 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1557 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001558 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001559 }
1560 }
1561 break;
1562 case LY_TYPE_IDENT:
1563 idref = (struct lysc_type_identityref *)(*type);
1564
1565 /* RFC 7950 9.10.2 - base */
1566 if (type_p->bases) {
1567 if (base) {
1568 /* only the directly derived identityrefs can contain base specification */
1569 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001570 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001571 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1572 tpdfname);
1573 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001574 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001575 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1576 }
1577 return LY_EVALID;
1578 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001579 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 +02001580 }
1581
1582 if (!base && !type_p->flags) {
1583 /* type derived from identityref built-in type must contain at least one base */
1584 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001585 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001586 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001587 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001588 }
1589 return LY_EVALID;
1590 }
1591 break;
1592 case LY_TYPE_LEAFREF:
1593 lref = (struct lysc_type_leafref *)*type;
1594
1595 /* RFC 7950 9.9.3 - require-instance */
1596 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001597 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001598 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001599 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001600 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1601 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001602 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001603 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1604 }
1605 return LY_EVALID;
1606 }
1607 lref->require_instance = type_p->require_instance;
1608 } else if (base) {
1609 /* inherit */
1610 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1611 } else {
1612 /* default is true */
1613 lref->require_instance = 1;
1614 }
1615 if (type_p->path) {
Radek Krejci2926c1b2021-03-16 14:45:45 +01001616 LY_PREFIX_FORMAT format;
1617
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001618 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci18c99162021-03-16 16:55:16 +01001619 LY_CHECK_RET(ly_type_prefix_data_new(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr),
Radek Krejci2926c1b2021-03-16 14:45:45 +01001620 LY_PREF_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001621 } else if (base) {
1622 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci2926c1b2021-03-16 14:45:45 +01001623 LY_CHECK_RET(ly_type_prefix_data_dup(ctx->ctx, LY_PREF_SCHEMA_RESOLVED,
1624 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001625 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001626 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001627 return LY_EVALID;
1628 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001629 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001630 return LY_EVALID;
1631 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001632 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001633 break;
1634 case LY_TYPE_INST:
1635 /* RFC 7950 9.9.3 - require-instance */
1636 if (type_p->flags & LYS_SET_REQINST) {
1637 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1638 } else {
1639 /* default is true */
1640 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1641 }
1642 break;
1643 case LY_TYPE_UNION:
1644 un = (struct lysc_type_union *)(*type);
1645
1646 /* RFC 7950 7.4 - type */
1647 if (type_p->types) {
1648 if (base) {
1649 /* only the directly derived union can contain types specification */
1650 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001651 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001652 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1653 tpdfname);
1654 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001655 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001656 "Invalid type substatement for the type not directly derived from union built-in type.");
1657 }
1658 return LY_EVALID;
1659 }
1660 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001661 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 +02001662 }
1663
1664 if (!base && !type_p->flags) {
1665 /* type derived from union built-in type must contain at least one type */
1666 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001667 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001668 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001669 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001670 }
1671 return LY_EVALID;
1672 }
1673 break;
1674 case LY_TYPE_BOOL:
1675 case LY_TYPE_EMPTY:
1676 case LY_TYPE_UNKNOWN: /* just to complete switch */
1677 break;
1678 }
1679
1680 if (tpdfname) {
1681 switch (basetype) {
1682 case LY_TYPE_BINARY:
1683 type_p->compiled = *type;
1684 *type = calloc(1, sizeof(struct lysc_type_bin));
1685 break;
1686 case LY_TYPE_BITS:
1687 type_p->compiled = *type;
1688 *type = calloc(1, sizeof(struct lysc_type_bits));
1689 break;
1690 case LY_TYPE_DEC64:
1691 type_p->compiled = *type;
1692 *type = calloc(1, sizeof(struct lysc_type_dec));
1693 break;
1694 case LY_TYPE_STRING:
1695 type_p->compiled = *type;
1696 *type = calloc(1, sizeof(struct lysc_type_str));
1697 break;
1698 case LY_TYPE_ENUM:
1699 type_p->compiled = *type;
1700 *type = calloc(1, sizeof(struct lysc_type_enum));
1701 break;
1702 case LY_TYPE_INT8:
1703 case LY_TYPE_UINT8:
1704 case LY_TYPE_INT16:
1705 case LY_TYPE_UINT16:
1706 case LY_TYPE_INT32:
1707 case LY_TYPE_UINT32:
1708 case LY_TYPE_INT64:
1709 case LY_TYPE_UINT64:
1710 type_p->compiled = *type;
1711 *type = calloc(1, sizeof(struct lysc_type_num));
1712 break;
1713 case LY_TYPE_IDENT:
1714 type_p->compiled = *type;
1715 *type = calloc(1, sizeof(struct lysc_type_identityref));
1716 break;
1717 case LY_TYPE_LEAFREF:
1718 type_p->compiled = *type;
1719 *type = calloc(1, sizeof(struct lysc_type_leafref));
1720 break;
1721 case LY_TYPE_INST:
1722 type_p->compiled = *type;
1723 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1724 break;
1725 case LY_TYPE_UNION:
1726 type_p->compiled = *type;
1727 *type = calloc(1, sizeof(struct lysc_type_union));
1728 break;
1729 case LY_TYPE_BOOL:
1730 case LY_TYPE_EMPTY:
1731 case LY_TYPE_UNKNOWN: /* just to complete switch */
1732 break;
1733 }
1734 }
1735 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1736
1737cleanup:
1738 return ret;
1739}
1740
Radek Krejcibf940f92021-03-24 21:04:13 +01001741/**
1742 * @brief Find the correct plugin implementing the described type
1743 *
1744 * @param[in] mod Module where the type is defined
1745 * @param[in] name Name of the type (typedef)
1746 * @param[in] basetype Type's basetype (when the built-in base plugin is supposed to be used)
1747 * @return Pointer to the plugin implementing the described data type.
1748 */
1749static struct lyplg_type *
1750lys_compile_type_get_plugin(struct lys_module *mod, const char *name, LY_DATA_TYPE basetype)
1751{
1752 struct lyplg_type *p;
1753
1754 /* try to find loaded user type plugins */
1755 p = lyplg_find(LYPLG_TYPE, mod->name, mod->revision, name);
1756 if (!p) {
1757 /* use the internal built-in type implementation */
1758 p = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
1759 }
1760
1761 return p;
1762}
1763
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001764LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001765lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1766 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001767{
1768 LY_ERR ret = LY_SUCCESS;
1769 ly_bool dummyloops = 0;
1770 struct type_context {
1771 const struct lysp_tpdf *tpdf;
1772 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001773 } *tctx, *tctx_prev = NULL, *tctx_iter;
1774 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1775 struct lysc_type *base = NULL, *prev_type;
1776 struct ly_set tpdf_chain = {0};
1777
1778 (*type) = NULL;
1779 if (dflt) {
1780 *dflt = NULL;
1781 }
1782
1783 tctx = calloc(1, sizeof *tctx);
1784 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001785 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 +02001786 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001787 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1788 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001789 if (basetype) {
1790 break;
1791 }
1792
1793 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001794 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1795 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001796 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1797
1798 if (units && !*units) {
1799 /* inherit units */
1800 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1801 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1802 }
1803 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1804 /* inherit default */
1805 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1806 }
1807 if (dummyloops && (!units || *units) && dflt && *dflt) {
1808 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1809 break;
1810 }
1811
Radek Krejci720d2612021-03-03 19:44:22 +01001812 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1813 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1814 * and we need now recompile the type again in the updated context. */
1815 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1816 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1817 }
1818
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001819 if (tctx->tpdf->type.compiled) {
1820 /* it is not necessary to continue, the rest of the chain was already compiled,
1821 * but we still may need to inherit default and units values, so start dummy loops */
1822 basetype = tctx->tpdf->type.compiled->basetype;
1823 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1824 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1825
1826 if ((units && !*units) || (dflt && !*dflt)) {
1827 dummyloops = 1;
1828 goto preparenext;
1829 } else {
1830 tctx = NULL;
1831 break;
1832 }
1833 }
1834
1835 /* circular typedef reference detection */
1836 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1837 /* local part */
1838 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001839 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001840 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001841 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1842 free(tctx);
1843 ret = LY_EVALID;
1844 goto cleanup;
1845 }
1846 }
1847 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1848 /* global part for unions corner case */
1849 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001850 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001851 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001852 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1853 free(tctx);
1854 ret = LY_EVALID;
1855 goto cleanup;
1856 }
1857 }
1858
1859 /* store information for the following processing */
1860 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1861 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1862
1863preparenext:
1864 /* prepare next loop */
1865 tctx_prev = tctx;
1866 tctx = calloc(1, sizeof *tctx);
1867 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1868 }
1869 free(tctx);
1870
1871 /* allocate type according to the basetype */
1872 switch (basetype) {
1873 case LY_TYPE_BINARY:
1874 *type = calloc(1, sizeof(struct lysc_type_bin));
1875 break;
1876 case LY_TYPE_BITS:
1877 *type = calloc(1, sizeof(struct lysc_type_bits));
1878 break;
1879 case LY_TYPE_BOOL:
1880 case LY_TYPE_EMPTY:
1881 *type = calloc(1, sizeof(struct lysc_type));
1882 break;
1883 case LY_TYPE_DEC64:
1884 *type = calloc(1, sizeof(struct lysc_type_dec));
1885 break;
1886 case LY_TYPE_ENUM:
1887 *type = calloc(1, sizeof(struct lysc_type_enum));
1888 break;
1889 case LY_TYPE_IDENT:
1890 *type = calloc(1, sizeof(struct lysc_type_identityref));
1891 break;
1892 case LY_TYPE_INST:
1893 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1894 break;
1895 case LY_TYPE_LEAFREF:
1896 *type = calloc(1, sizeof(struct lysc_type_leafref));
1897 break;
1898 case LY_TYPE_STRING:
1899 *type = calloc(1, sizeof(struct lysc_type_str));
1900 break;
1901 case LY_TYPE_UNION:
1902 *type = calloc(1, sizeof(struct lysc_type_union));
1903 break;
1904 case LY_TYPE_INT8:
1905 case LY_TYPE_UINT8:
1906 case LY_TYPE_INT16:
1907 case LY_TYPE_UINT16:
1908 case LY_TYPE_INT32:
1909 case LY_TYPE_UINT32:
1910 case LY_TYPE_INT64:
1911 case LY_TYPE_UINT64:
1912 *type = calloc(1, sizeof(struct lysc_type_num));
1913 break;
1914 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001915 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001916 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1917 ret = LY_EVALID;
1918 goto cleanup;
1919 }
1920 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1921 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001922 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001923 ly_data_type2str[basetype]);
1924 free(*type);
1925 (*type) = NULL;
1926 ret = LY_EVALID;
1927 goto cleanup;
1928 }
1929
1930 /* get restrictions from the referred typedefs */
1931 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1932 tctx = (struct type_context *)tpdf_chain.objs[u];
1933
1934 /* remember the typedef context for circular check */
1935 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1936 LY_CHECK_GOTO(ret, cleanup);
1937
1938 if (tctx->tpdf->type.compiled) {
1939 base = tctx->tpdf->type.compiled;
1940 continue;
1941 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1942 /* no change, just use the type information from the base */
1943 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1944 ++base->refcount;
1945 continue;
1946 }
1947
1948 ++(*type)->refcount;
1949 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001950 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001951 tctx->tpdf->name, ly_data_type2str[basetype]);
1952 ret = LY_EVALID;
1953 goto cleanup;
1954 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001955 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001956 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1957 tctx->tpdf->name, tctx->tpdf->dflt.str);
1958 ret = LY_EVALID;
1959 goto cleanup;
1960 }
1961
1962 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01001963 (*type)->plugin = lys_compile_type_get_plugin(tctx->tpdf->type.pmod->mod, tctx->tpdf->name, basetype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001964 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001965 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001966 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1967 LY_CHECK_GOTO(ret, cleanup);
1968 base = prev_type;
1969 }
1970 /* remove the processed typedef contexts from the stack for circular check */
1971 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1972
1973 /* process the type definition in leaf */
1974 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1975 /* get restrictions from the node itself */
1976 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01001977 (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001978 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001979 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001980 LY_CHECK_GOTO(ret, cleanup);
1981 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1982 /* no specific restriction in leaf's type definition, copy from the base */
1983 free(*type);
1984 (*type) = base;
1985 ++(*type)->refcount;
1986 }
1987
Radek Krejciab430862021-03-02 20:13:40 +01001988 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001989
1990cleanup:
1991 ly_set_erase(&tpdf_chain, free);
1992 return ret;
1993}
1994
1995/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01001996 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
1997 */
1998#define LYS_STATUS_USES LYS_CONFIG_MASK
1999
2000/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002001 * @brief Compile status information of the given node.
2002 *
2003 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
2004 * has the status correctly set during the compilation.
2005 *
2006 * @param[in] ctx Compile context
2007 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
2008 * If the status was set explicitly on the node, it is already set in the flags value and we just check
2009 * the compatibility with the parent's status value.
2010 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
2011 * @return LY_ERR value.
2012 */
2013static LY_ERR
2014lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
2015{
2016 /* status - it is not inherited by specification, but it does not make sense to have
2017 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2018 if (!((*node_flags) & LYS_STATUS_MASK)) {
2019 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002020 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002021 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01002022 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002023 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2024 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2025 }
2026 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
2027 } else {
2028 (*node_flags) |= LYS_STATUS_CURR;
2029 }
2030 } else if (parent_flags & LYS_STATUS_MASK) {
2031 /* check status compatibility with the parent */
2032 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
2033 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002034 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002035 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2036 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2037 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002038 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002039 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2040 }
2041 return LY_EVALID;
2042 }
2043 }
2044 return LY_SUCCESS;
2045}
2046
2047/**
2048 * @brief Check uniqness of the node/action/notification name.
2049 *
2050 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2051 * structures, but they share the namespace so we need to check their name collisions.
2052 *
2053 * @param[in] ctx Compile context.
2054 * @param[in] parent Parent of the nodes to check, can be NULL.
2055 * @param[in] name Name of the item to find in the given lists.
2056 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2057 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2058 */
2059static LY_ERR
2060lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2061 const struct lysc_node *exclude)
2062{
2063 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002064 const struct lysc_node_action *actions;
2065 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002066 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002067 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002068
2069#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2070
2071 if (exclude->nodetype == LYS_CASE) {
2072 /* check restricted only to all the cases */
2073 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002074 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002075 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002076 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002077 return LY_EEXIST;
2078 }
2079 }
2080
2081 return LY_SUCCESS;
2082 }
2083
2084 /* no reason for our parent to be choice anymore */
2085 assert(!parent || (parent->nodetype != LYS_CHOICE));
2086
2087 if (parent && (parent->nodetype == LYS_CASE)) {
2088 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002089
2090 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2091 iter = lysc_data_parent(parent);
2092 do {
2093 parent = parent->parent;
2094 if (parent && (parent->nodetype == LYS_CHOICE)) {
2095 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2096 }
2097 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002098 }
2099
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002100 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002101 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2102 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2103 if (exclude->flags & LYS_IS_OUTPUT) {
2104 getnext_flags |= LYS_GETNEXT_OUTPUT;
2105 parent = lysc_node_child(parent)->next;
2106 } else {
2107 parent = lysc_node_child(parent);
2108 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002109 }
2110
2111 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002112 if (!parent && ctx->ext) {
2113 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2114 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2115 goto error;
2116 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002117
Radek Krejci5fa32a32021-02-08 18:12:38 +01002118 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2119 if (iter->nodetype == LYS_CHOICE) {
2120 iter2 = NULL;
2121 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2122 if (CHECK_NODE(iter2, exclude, name)) {
2123 goto error;
2124 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002125 }
2126 }
2127 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002128 } else {
2129 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2130 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2131 goto error;
2132 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002133
Radek Krejci5fa32a32021-02-08 18:12:38 +01002134 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2135 if (iter->nodetype == LYS_CHOICE) {
2136 iter2 = NULL;
2137 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2138 if (CHECK_NODE(iter2, exclude, name)) {
2139 goto error;
2140 }
2141 }
2142 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002143 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002144
Radek Krejci5fa32a32021-02-08 18:12:38 +01002145 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2146 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2147 if (CHECK_NODE(iter, exclude, name)) {
2148 goto error;
2149 }
2150 }
2151
2152 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2153 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2154 if (CHECK_NODE(iter, exclude, name)) {
2155 goto error;
2156 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002157 }
2158 }
Radek Krejci078e4342021-02-12 18:17:26 +01002159 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002160 return LY_SUCCESS;
2161
2162error:
Radek Krejci078e4342021-02-12 18:17:26 +01002163 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002164 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002165 return LY_EEXIST;
2166
2167#undef CHECK_NODE
2168}
2169
Radek Krejci2a9fc652021-01-22 17:44:34 +01002170/**
2171 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2172 * keep specific order of augments targetting the same node.
2173 *
2174 * @param[in] ctx Compile context
2175 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2176 * the choice itself is expected instead of a specific case node.
2177 * @param[in] node Schema node to connect into the list.
2178 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2179 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2180 */
2181static LY_ERR
2182lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002183{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002184 struct lysc_node **children, *anchor = NULL;
2185 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002186
Radek Krejci2a9fc652021-01-22 17:44:34 +01002187 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002188
Radek Krejci2a9fc652021-01-22 17:44:34 +01002189 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002190 if (node->nodetype == LYS_INPUT) {
2191 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2192 /* input node is part of the action but link it with output */
2193 node->next = &((struct lysc_node_action *)parent)->output.node;
2194 node->prev = node->next;
2195 return LY_SUCCESS;
2196 } else if (node->nodetype == LYS_OUTPUT) {
2197 /* output node is part of the action but link it with input */
2198 node->next = NULL;
2199 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002200 return LY_SUCCESS;
2201 } else if (node->nodetype == LYS_ACTION) {
2202 children = (struct lysc_node **)lysc_node_actions_p(parent);
2203 } else if (node->nodetype == LYS_NOTIF) {
2204 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2205 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002206 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002207 }
2208 assert(children);
2209
2210 if (!(*children)) {
2211 /* first child */
2212 *children = node;
2213 } else if (node->flags & LYS_KEY) {
2214 /* special handling of adding keys */
2215 assert(node->module == parent->module);
2216 anchor = *children;
2217 if (anchor->flags & LYS_KEY) {
2218 while ((anchor->flags & LYS_KEY) && anchor->next) {
2219 anchor = anchor->next;
2220 }
2221 /* insert after the last key */
2222 insert_after = 1;
2223 } /* else insert before anchor (at the beginning) */
2224 } else if ((*children)->prev->module == node->module) {
2225 /* last child is from the same module, keep the order and insert at the end */
2226 anchor = (*children)->prev;
2227 insert_after = 1;
2228 } else if (parent->module == node->module) {
2229 /* adding module child after some augments were connected */
2230 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2231 } else {
2232 /* some augments are already connected and we are connecting new ones,
2233 * keep module name order and insert the node into the children list */
2234 anchor = *children;
2235 do {
2236 anchor = anchor->prev;
2237
2238 /* check that we have not found the last augment node from our module or
2239 * the first augment node from a "smaller" module or
2240 * the first node from a local module */
2241 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2242 (anchor->module == parent->module)) {
2243 /* insert after */
2244 insert_after = 1;
2245 break;
2246 }
2247
2248 /* we have traversed all the nodes, insert before anchor (as the first node) */
2249 } while (anchor->prev->next);
2250 }
2251
2252 /* insert */
2253 if (anchor) {
2254 if (insert_after) {
2255 node->next = anchor->next;
2256 node->prev = anchor;
2257 anchor->next = node;
2258 if (node->next) {
2259 /* middle node */
2260 node->next->prev = node;
2261 } else {
2262 /* last node */
2263 (*children)->prev = node;
2264 }
2265 } else {
2266 node->next = anchor;
2267 node->prev = anchor->prev;
2268 anchor->prev = node;
2269 if (anchor == *children) {
2270 /* first node */
2271 *children = node;
2272 } else {
2273 /* middle node */
2274 node->prev->next = node;
2275 }
2276 }
2277 }
2278
2279 /* check the name uniqueness (even for an only child, it may be in case) */
2280 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2281 return LY_EEXIST;
2282 }
2283 } else {
2284 /* top-level element */
2285 struct lysc_node **list;
2286
Radek Krejci6b88a462021-02-17 12:39:34 +01002287 if (ctx->ext) {
2288 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2289 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002290 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2291 } else if (node->nodetype == LYS_NOTIF) {
2292 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2293 } else {
2294 list = &ctx->cur_mod->compiled->data;
2295 }
2296 if (!(*list)) {
2297 *list = node;
2298 } else {
2299 /* insert at the end of the module's top-level nodes list */
2300 (*list)->prev->next = node;
2301 node->prev = (*list)->prev;
2302 (*list)->prev = node;
2303 }
2304
2305 /* check the name uniqueness on top-level */
2306 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2307 return LY_EEXIST;
2308 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002309 }
2310
Radek Krejci2a9fc652021-01-22 17:44:34 +01002311 return LY_SUCCESS;
2312}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002313
Radek Krejci2a9fc652021-01-22 17:44:34 +01002314/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002315 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002316 *
2317 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002318 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002319 * @return LY_ERR value.
2320 */
2321static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002322lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002323{
2324 /* case never has any explicit config */
2325 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002326
Radek Krejci91531e12021-02-08 19:57:54 +01002327 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
2328 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002329 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002330 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2331 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002332 if (node->parent) {
2333 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002334 } else {
2335 /* default is config true */
2336 node->flags |= LYS_CONFIG_W;
2337 }
2338 } else {
2339 /* config set explicitly */
2340 node->flags |= LYS_SET_CONFIG;
2341 }
2342
Radek Krejci91531e12021-02-08 19:57:54 +01002343 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002344 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002345 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002346 }
2347
Radek Krejci2a9fc652021-01-22 17:44:34 +01002348 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002349}
2350
Radek Krejci91531e12021-02-08 19:57:54 +01002351/**
2352 * @brief Set various flags of the compiled nodes
2353 *
2354 * @param[in] ctx Compile context.
2355 * @param[in] node Compiled node where the flags will be set.
2356 * @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).
2357 * Zero means no uses, non-zero value with no status bit set mean the default status.
2358 */
2359static LY_ERR
2360lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2361{
2362 /* inherit config flags */
2363 LY_CHECK_RET(lys_compile_config(ctx, node));
2364
2365 /* status - it is not inherited by specification, but it does not make sense to have
2366 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2367 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (node->parent ? node->parent->flags : 0)));
2368
2369 /* other flags */
2370 if ((ctx->options & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
2371 node->flags |= LYS_IS_INPUT;
2372 } else if ((ctx->options & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
2373 node->flags |= LYS_IS_OUTPUT;
2374 } else if ((ctx->options & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
2375 node->flags |= LYS_IS_NOTIF;
2376 }
2377
2378 return LY_SUCCESS;
2379}
2380
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002381LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002382lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2383 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2384 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002385{
2386 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002387 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002388 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002389 struct lysp_when *pwhen = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002390
Radek Krejci2a9fc652021-01-22 17:44:34 +01002391 node->nodetype = pnode->nodetype;
2392 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002393 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002394 node->prev = node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002395
Radek Krejci2a9fc652021-01-22 17:44:34 +01002396 /* compile any deviations for this node */
2397 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002398 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002399 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002400 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002401 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002402 }
2403
Radek Krejci2a9fc652021-01-22 17:44:34 +01002404 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002405
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002406 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002407 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Radek Krejciaf0548b2021-02-15 15:11:22 +01002408 if (!enabled && !(ctx->options & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002409 ly_set_add(&ctx->disabled, node, 1, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002410 ctx->options |= LYS_COMPILE_DISABLED;
2411 }
2412
Radek Krejci91531e12021-02-08 19:57:54 +01002413 /* config, status and other flags */
2414 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002415 LY_CHECK_GOTO(ret, error);
2416
2417 /* list ordering */
2418 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002419 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002420 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002421 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
2422 (ctx->options & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002423 node->flags &= ~LYS_ORDBY_MASK;
2424 node->flags |= LYS_ORDBY_SYSTEM;
2425 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2426 /* default ordering is system */
2427 node->flags |= LYS_ORDBY_SYSTEM;
2428 }
2429 }
2430
Radek Krejci2a9fc652021-01-22 17:44:34 +01002431 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2432 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2433 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2434
2435 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2436 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2437
Radek Krejci9a3823e2021-01-27 20:26:46 +01002438 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002439 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002440 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002441 LY_CHECK_GOTO(ret, cleanup);
2442 }
2443
2444 /* connect any augments */
2445 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2446
2447 /* nodetype-specific part */
2448 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2449
2450 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002451 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002452 if (node->flags & LYS_MAND_TRUE) {
2453 /* inherit LYS_MAND_TRUE in parent containers */
2454 lys_compile_mandatory_parents(parent, 1);
2455 }
2456
2457 if (child_set) {
2458 /* add the new node into set */
2459 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2460 }
2461
2462 goto cleanup;
2463
2464error:
2465 lysc_node_free(ctx->ctx, node, 0);
2466cleanup:
2467 if (ret && dev_pnode) {
2468 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2469 }
2470 lysp_dev_node_free(ctx->ctx, dev_pnode);
2471 return ret;
2472}
2473
2474/**
2475 * @brief Compile parsed action's input/output node information.
2476 * @param[in] ctx Compile context
2477 * @param[in] pnode Parsed inout node.
2478 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2479 * is enriched with the inout-specific information.
2480 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2481 */
2482LY_ERR
2483lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2484{
2485 LY_ERR ret = LY_SUCCESS;
2486 struct lysp_node *child_p;
2487 uint32_t prev_options = ctx->options;
2488
2489 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2490 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2491
2492 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002493 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Radek Krejci91531e12021-02-08 19:57:54 +01002494 ctx->options |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002495
Radek Krejci01180ac2021-01-27 08:48:22 +01002496 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002497 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2498 }
2499
2500 ctx->options = prev_options;
2501
2502done:
2503 return ret;
2504}
2505
2506/**
2507 * @brief Compile parsed action node information.
2508 * @param[in] ctx Compile context
2509 * @param[in] pnode Parsed action node.
2510 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2511 * is enriched with the action-specific information.
2512 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2513 */
2514LY_ERR
2515lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2516{
2517 LY_ERR ret;
2518 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2519 struct lysc_node_action *action = (struct lysc_node_action *)node;
2520 struct lysp_node_action_inout *input, implicit_input = {
2521 .nodetype = LYS_INPUT,
2522 .name = "input",
2523 .parent = pnode,
2524 };
2525 struct lysp_node_action_inout *output, implicit_output = {
2526 .nodetype = LYS_OUTPUT,
2527 .name = "output",
2528 .parent = pnode,
2529 };
2530
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002531 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002532 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002533 if (action_p->input.nodetype == LYS_UNKNOWN) {
2534 input = &implicit_input;
2535 } else {
2536 input = &action_p->input;
2537 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002538 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 +01002539 lysc_update_path(ctx, NULL, NULL);
2540 LY_CHECK_GOTO(ret, done);
2541
2542 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002543 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002544 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002545 output = &implicit_output;
2546 } else {
2547 output = &action_p->output;
2548 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002549 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 +01002550 lysc_update_path(ctx, NULL, NULL);
2551 LY_CHECK_GOTO(ret, done);
2552
2553 /* do not check "must" semantics in a grouping */
2554 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2555 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2556 LY_CHECK_GOTO(ret, done);
2557 }
2558
2559done:
2560 return ret;
2561}
2562
2563/**
2564 * @brief Compile parsed action node information.
2565 * @param[in] ctx Compile context
2566 * @param[in] pnode Parsed action node.
2567 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2568 * is enriched with the action-specific information.
2569 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2570 */
2571LY_ERR
2572lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2573{
2574 LY_ERR ret = LY_SUCCESS;
2575 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2576 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2577 struct lysp_node *child_p;
2578
2579 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002580 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002581 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002582 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002583 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002584 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002585
Radek Krejci01180ac2021-01-27 08:48:22 +01002586 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002587 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002588 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002589 }
2590
Radek Krejci2a9fc652021-01-22 17:44:34 +01002591done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002592 return ret;
2593}
2594
2595/**
2596 * @brief Compile parsed container node information.
2597 * @param[in] ctx Compile context
2598 * @param[in] pnode Parsed container node.
2599 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2600 * is enriched with the container-specific information.
2601 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2602 */
2603static LY_ERR
2604lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2605{
2606 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2607 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2608 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002609 LY_ERR ret = LY_SUCCESS;
2610
2611 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002612 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002613 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002614 }
2615
2616 /* more cases when the container has meaning but is kept NP for convenience:
2617 * - when condition
2618 * - direct child action/notification
2619 */
2620
2621 LY_LIST_FOR(cont_p->child, child_p) {
2622 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2623 LY_CHECK_GOTO(ret, done);
2624 }
2625
Michal Vasko5347e3a2020-11-03 17:14:57 +01002626 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002627 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002628 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002629 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002630 LY_CHECK_GOTO(ret, done);
2631 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002632
2633 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2634 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2635 LY_CHECK_GOTO(ret, done);
2636 }
2637 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2638 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2639 LY_CHECK_GOTO(ret, done);
2640 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002641
2642done:
2643 return ret;
2644}
2645
Michal Vasko72244882021-01-12 15:21:05 +01002646/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002647 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2648 * @param[in] ctx Compile context.
2649 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2650 * @param[in] type_p Parsed type to compile.
2651 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2652 * @return LY_ERR value.
2653 */
2654static LY_ERR
2655lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2656 struct lysc_node_leaf *leaf)
2657{
2658 struct lysp_qname *dflt;
2659
Michal Vaskoa99b3572021-02-01 11:54:58 +01002660 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002661 leaf->units ? NULL : &leaf->units, &dflt));
2662
2663 /* store default value, if any */
2664 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2665 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2666 }
2667
Michal Vasko12606ee2020-11-25 17:05:11 +01002668 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002669 /* 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 +01002670 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002671 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002672 LY_ARRAY_COUNT_TYPE u;
2673 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2674 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2675 /* 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 +01002676 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002677 }
2678 }
2679 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2680 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002681 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 +02002682 return LY_EVALID;
2683 }
2684 }
2685
2686 return LY_SUCCESS;
2687}
2688
2689/**
2690 * @brief Compile parsed leaf node information.
2691 * @param[in] ctx Compile context
2692 * @param[in] pnode Parsed leaf node.
2693 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2694 * is enriched with the leaf-specific information.
2695 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2696 */
2697static LY_ERR
2698lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2699{
2700 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2701 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002702 LY_ERR ret = LY_SUCCESS;
2703
Michal Vasko5347e3a2020-11-03 17:14:57 +01002704 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002705 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002706 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002707 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002708 LY_CHECK_GOTO(ret, done);
2709 }
2710 if (leaf_p->units) {
2711 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2712 leaf->flags |= LYS_SET_UNITS;
2713 }
2714
2715 /* compile type */
2716 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2717 LY_CHECK_GOTO(ret, done);
2718
2719 /* store/update default value */
2720 if (leaf_p->dflt.str) {
2721 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2722 leaf->flags |= LYS_SET_DFLT;
2723 }
2724
2725 /* checks */
2726 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002727 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002728 "Invalid mandatory leaf with a default value.");
2729 return LY_EVALID;
2730 }
2731
2732done:
2733 return ret;
2734}
2735
2736/**
2737 * @brief Compile parsed leaf-list node information.
2738 * @param[in] ctx Compile context
2739 * @param[in] pnode Parsed leaf-list node.
2740 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2741 * is enriched with the leaf-list-specific information.
2742 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2743 */
2744static LY_ERR
2745lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2746{
2747 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2748 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002749 LY_ERR ret = LY_SUCCESS;
2750
Michal Vasko5347e3a2020-11-03 17:14:57 +01002751 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002752 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002753 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002754 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002755 LY_CHECK_GOTO(ret, done);
2756 }
2757 if (llist_p->units) {
2758 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2759 llist->flags |= LYS_SET_UNITS;
2760 }
2761
2762 /* compile type */
2763 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2764 LY_CHECK_GOTO(ret, done);
2765
2766 /* store/update default values */
2767 if (llist_p->dflts) {
2768 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002769 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002770 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2771 return LY_EVALID;
2772 }
2773
2774 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2775 llist->flags |= LYS_SET_DFLT;
2776 }
2777
2778 llist->min = llist_p->min;
2779 if (llist->min) {
2780 llist->flags |= LYS_MAND_TRUE;
2781 }
2782 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2783
2784 /* checks */
2785 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002786 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 +02002787 return LY_EVALID;
2788 }
2789
2790 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002791 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002792 llist->min, llist->max);
2793 return LY_EVALID;
2794 }
2795
2796done:
2797 return ret;
2798}
2799
2800LY_ERR
2801lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2802 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2803 const struct lysc_node **target, uint16_t *result_flag)
2804{
2805 LY_ERR ret = LY_EVALID;
2806 const char *name, *prefix, *id;
2807 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002808 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002809 const char *nodeid_type;
2810 uint32_t getnext_extra_flag = 0;
2811 uint16_t current_nodetype = 0;
2812
2813 assert(nodeid);
2814 assert(target);
2815 assert(result_flag);
2816 *target = NULL;
2817 *result_flag = 0;
2818
2819 id = nodeid;
2820
2821 if (ctx_node) {
2822 /* descendant-schema-nodeid */
2823 nodeid_type = "descendant";
2824
2825 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002826 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002827 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002828 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002829 return LY_EVALID;
2830 }
2831 } else {
2832 /* absolute-schema-nodeid */
2833 nodeid_type = "absolute";
2834
2835 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002836 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002837 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002838 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002839 return LY_EVALID;
2840 }
2841 ++id;
2842 }
2843
2844 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2845 if (prefix) {
2846 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2847 if (!mod) {
2848 /* module must always be found */
2849 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002850 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002851 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002852 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002853 return LY_ENOTFOUND;
2854 }
2855 } else {
2856 switch (format) {
2857 case LY_PREF_SCHEMA:
2858 case LY_PREF_SCHEMA_RESOLVED:
2859 /* use the current module */
2860 mod = cur_mod;
2861 break;
2862 case LY_PREF_JSON:
2863 if (!ctx_node) {
2864 LOGINT_RET(ctx->ctx);
2865 }
2866 /* inherit the module of the previous context node */
2867 mod = ctx_node->module;
2868 break;
2869 case LY_PREF_XML:
2870 /* not really defined */
2871 LOGINT_RET(ctx->ctx);
2872 }
2873 }
2874
2875 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2876 /* move through input/output manually */
2877 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002878 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2879 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002880 return LY_ENOTFOUND;
2881 }
2882 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002883 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002884 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002885 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002886 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2887 } else {
2888 /* only input or output is valid */
2889 ctx_node = NULL;
2890 }
2891 } else {
2892 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002893 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002894 getnext_extra_flag = 0;
2895 }
2896 if (!ctx_node) {
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 current_nodetype = ctx_node->nodetype;
2902
2903 if (current_nodetype == LYS_NOTIF) {
2904 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2905 } else if (current_nodetype == LYS_INPUT) {
2906 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2907 } else if (current_nodetype == LYS_OUTPUT) {
2908 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2909 }
2910
2911 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2912 break;
2913 }
2914 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002915 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002916 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002917 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002918 return LY_EVALID;
2919 }
2920 ++id;
2921 }
2922
2923 if (ret == LY_SUCCESS) {
2924 *target = ctx_node;
2925 if (nodetype && !(current_nodetype & nodetype)) {
2926 return LY_EDENIED;
2927 }
2928 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002929 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002930 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002931 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002932 }
2933
2934 return ret;
2935}
2936
2937/**
2938 * @brief Compile information about list's uniques.
2939 * @param[in] ctx Compile context.
2940 * @param[in] uniques Sized array list of unique statements.
2941 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2942 * @return LY_ERR value.
2943 */
2944static LY_ERR
2945lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2946{
2947 LY_ERR ret = LY_SUCCESS;
2948 struct lysc_node_leaf **key, ***unique;
2949 struct lysc_node *parent;
2950 const char *keystr, *delim;
2951 size_t len;
2952 LY_ARRAY_COUNT_TYPE v;
2953 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2954 uint16_t flags;
2955
2956 LY_ARRAY_FOR(uniques, v) {
2957 config = -1;
2958 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2959 keystr = uniques[v].str;
2960 while (keystr) {
2961 delim = strpbrk(keystr, " \t\n");
2962 if (delim) {
2963 len = delim - keystr;
2964 while (isspace(*delim)) {
2965 ++delim;
2966 }
2967 } else {
2968 len = strlen(keystr);
2969 }
2970
2971 /* unique node must be present */
2972 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002973 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002974 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2975 if (ret != LY_SUCCESS) {
2976 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002977 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002978 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002979 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002980 }
2981 return LY_EVALID;
2982 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002983 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002984 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01002985 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002986 return LY_EVALID;
2987 }
2988
2989 /* all referenced leafs must be of the same config type */
2990 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2991 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002992 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002993 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2994 return LY_EVALID;
2995 } else if ((*key)->flags & LYS_CONFIG_W) {
2996 config = 1;
2997 } else { /* LYS_CONFIG_R */
2998 config = 0;
2999 }
3000
3001 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3002 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3003 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003004 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003005 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3006 return LY_EVALID;
3007 }
3008 }
3009
3010 /* check status */
3011 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3012 (*key)->flags, (*key)->module, (*key)->name));
3013
3014 /* mark leaf as unique */
3015 (*key)->flags |= LYS_UNIQUE;
3016
3017 /* next unique value in line */
3018 keystr = delim;
3019 }
3020 /* next unique definition */
3021 }
3022
3023 return LY_SUCCESS;
3024}
3025
3026/**
3027 * @brief Compile parsed list node information.
3028 * @param[in] ctx Compile context
3029 * @param[in] pnode Parsed list node.
3030 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3031 * is enriched with the list-specific information.
3032 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3033 */
3034static LY_ERR
3035lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3036{
3037 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3038 struct lysc_node_list *list = (struct lysc_node_list *)node;
3039 struct lysp_node *child_p;
3040 struct lysc_node_leaf *key, *prev_key = NULL;
3041 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003042 const char *keystr, *delim;
3043 LY_ERR ret = LY_SUCCESS;
3044
3045 list->min = list_p->min;
3046 if (list->min) {
3047 list->flags |= LYS_MAND_TRUE;
3048 }
3049 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3050
3051 LY_LIST_FOR(list_p->child, child_p) {
3052 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3053 }
3054
Michal Vasko5347e3a2020-11-03 17:14:57 +01003055 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003056 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003057 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003058 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003059 }
3060
3061 /* keys */
3062 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003063 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003064 return LY_EVALID;
3065 }
3066
3067 /* find all the keys (must be direct children) */
3068 keystr = list_p->key;
3069 if (!keystr) {
3070 /* keyless list */
3071 list->flags |= LYS_KEYLESS;
3072 }
3073 while (keystr) {
3074 delim = strpbrk(keystr, " \t\n");
3075 if (delim) {
3076 len = delim - keystr;
3077 while (isspace(*delim)) {
3078 ++delim;
3079 }
3080 } else {
3081 len = strlen(keystr);
3082 }
3083
3084 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003085 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3086 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003087 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003088 return LY_EVALID;
3089 }
3090 /* keys must be unique */
3091 if (key->flags & LYS_KEY) {
3092 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003093 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003094 return LY_EVALID;
3095 }
3096
Radek Krejcia6016992021-03-03 10:13:41 +01003097 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003098 /* key must have the same config flag as the list itself */
3099 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003100 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003101 return LY_EVALID;
3102 }
3103 if (ctx->pmod->version < LYS_VERSION_1_1) {
3104 /* YANG 1.0 denies key to be of empty type */
3105 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003106 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003107 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3108 return LY_EVALID;
3109 }
3110 } else {
3111 /* when and if-feature are illegal on list keys */
3112 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003113 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003114 "List's key must not have any \"when\" statement.");
3115 return LY_EVALID;
3116 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003117 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003118 }
3119
3120 /* check status */
3121 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3122 key->flags, key->module, key->name));
3123
3124 /* ignore default values of the key */
3125 if (key->dflt) {
3126 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3127 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3128 free(key->dflt);
3129 key->dflt = NULL;
3130 }
3131 /* mark leaf as key */
3132 key->flags |= LYS_KEY;
3133
3134 /* move it to the correct position */
3135 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3136 /* fix links in closest previous siblings of the key */
3137 if (key->next) {
3138 key->next->prev = key->prev;
3139 } else {
3140 /* last child */
3141 list->child->prev = key->prev;
3142 }
3143 if (key->prev->next) {
3144 key->prev->next = key->next;
3145 }
3146 /* fix links in the key */
3147 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003148 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003149 key->next = prev_key->next;
3150 } else {
3151 key->prev = list->child->prev;
3152 key->next = list->child;
3153 }
3154 /* fix links in closes future siblings of the key */
3155 if (prev_key) {
3156 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003157 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003158 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003159 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003160 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003161 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003162 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003163 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003164 }
3165 /* fix links in parent */
3166 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003167 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003168 }
3169 }
3170
3171 /* next key value */
3172 prev_key = key;
3173 keystr = delim;
3174 lysc_update_path(ctx, NULL, NULL);
3175 }
3176
3177 /* uniques */
3178 if (list_p->uniques) {
3179 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3180 }
3181
Radek Krejci2a9fc652021-01-22 17:44:34 +01003182 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3183 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3184 LY_CHECK_GOTO(ret, done);
3185 }
3186 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3187 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3188 LY_CHECK_GOTO(ret, done);
3189 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003190
3191 /* checks */
3192 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003193 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003194 list->min, list->max);
3195 return LY_EVALID;
3196 }
3197
3198done:
3199 return ret;
3200}
3201
3202/**
3203 * @brief Do some checks and set the default choice's case.
3204 *
3205 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3206 *
3207 * @param[in] ctx Compile context.
3208 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3209 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3210 * @return LY_ERR value.
3211 */
3212static LY_ERR
3213lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3214{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003215 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003216 const struct lys_module *mod;
3217 const char *prefix = NULL, *name;
3218 size_t prefix_len = 0;
3219
3220 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3221 name = strchr(dflt->str, ':');
3222 if (name) {
3223 prefix = dflt->str;
3224 prefix_len = name - prefix;
3225 ++name;
3226 } else {
3227 name = dflt->str;
3228 }
3229 if (prefix) {
3230 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
3231 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003232 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003233 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003234 return LY_EVALID;
3235 }
3236 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003237 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003238 }
3239
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003240 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 +02003241 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003242 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003243 "Default case \"%s\" not found.", dflt->str);
3244 return LY_EVALID;
3245 }
3246
3247 /* no mandatory nodes directly under the default case */
3248 LY_LIST_FOR(ch->dflt->child, iter) {
3249 if (iter->parent != (struct lysc_node *)ch->dflt) {
3250 break;
3251 }
3252 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003253 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003254 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3255 return LY_EVALID;
3256 }
3257 }
3258
3259 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003260 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003261 return LY_EVALID;
3262 }
3263
3264 ch->dflt->flags |= LYS_SET_DFLT;
3265 return LY_SUCCESS;
3266}
3267
3268LY_ERR
3269lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3270 struct ly_set *child_set)
3271{
3272 LY_ERR ret = LY_SUCCESS;
3273 struct lysp_node *child_p_next = child_p->next;
3274 struct lysp_node_case *cs_p;
3275
3276 if (child_p->nodetype == LYS_CASE) {
3277 /* standard case under choice */
3278 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3279 } else {
3280 /* we need the implicit case first, so create a fake parsed case */
3281 cs_p = calloc(1, sizeof *cs_p);
3282 cs_p->nodetype = LYS_CASE;
3283 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3284 cs_p->child = child_p;
3285
3286 /* make the child the only case child */
3287 child_p->next = NULL;
3288
3289 /* compile it normally */
3290 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3291
3292free_fake_node:
3293 /* free the fake parsed node and correct pointers back */
3294 cs_p->child = NULL;
3295 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3296 child_p->next = child_p_next;
3297 }
3298
3299 return ret;
3300}
3301
3302/**
3303 * @brief Compile parsed choice node information.
3304 *
3305 * @param[in] ctx Compile context
3306 * @param[in] pnode Parsed choice node.
3307 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3308 * is enriched with the choice-specific information.
3309 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3310 */
3311static LY_ERR
3312lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3313{
3314 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3315 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3316 struct lysp_node *child_p;
3317 LY_ERR ret = LY_SUCCESS;
3318
3319 assert(node->nodetype == LYS_CHOICE);
3320
3321 LY_LIST_FOR(ch_p->child, child_p) {
3322 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3323 }
3324
3325 /* default branch */
3326 if (ch_p->dflt.str) {
3327 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3328 }
3329
3330 return ret;
3331}
3332
3333/**
3334 * @brief Compile parsed anydata or anyxml node information.
3335 * @param[in] ctx Compile context
3336 * @param[in] pnode Parsed anydata or anyxml node.
3337 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3338 * is enriched with the any-specific information.
3339 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3340 */
3341static LY_ERR
3342lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3343{
3344 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3345 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003346 LY_ERR ret = LY_SUCCESS;
3347
Michal Vasko5347e3a2020-11-03 17:14:57 +01003348 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003349 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003350 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003351 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003352 LY_CHECK_GOTO(ret, done);
3353 }
3354
Radek Krejci91531e12021-02-08 19:57:54 +01003355 if (any->flags & LYS_CONFIG_W) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003356 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3357 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3358 }
3359done:
3360 return ret;
3361}
3362
3363/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003364 * @brief Prepare the case structure in choice node for the new data node.
3365 *
3366 * 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
3367 * created in the choice when the first child was processed.
3368 *
3369 * @param[in] ctx Compile context.
3370 * @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,
3371 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3372 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3373 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3374 * @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,
3375 * it is linked from the case structure only in case it is its first child.
3376 */
3377static LY_ERR
3378lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3379{
3380 struct lysp_node *child_p;
3381 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3382
3383 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3384 /* we have to add an implicit case node into the parent choice */
3385 } else if (pnode->nodetype == LYS_CASE) {
3386 /* explicit parent case */
3387 LY_LIST_FOR(cs_p->child, child_p) {
3388 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3389 }
3390 } else {
3391 LOGINT_RET(ctx->ctx);
3392 }
3393
3394 return LY_SUCCESS;
3395}
3396
3397void
3398lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3399{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003400 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003401
3402 if (add) { /* set flag */
3403 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3404 parent = parent->parent) {
3405 parent->flags |= LYS_MAND_TRUE;
3406 }
3407 } else { /* unset flag */
3408 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003409 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003410 if (iter->flags & LYS_MAND_TRUE) {
3411 /* there is another mandatory node */
3412 return;
3413 }
3414 }
3415 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3416 parent->flags &= ~LYS_MAND_TRUE;
3417 }
3418 }
3419}
3420
3421/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003422 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003423 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003424 * @param[in] name Name of the grouping to find,
3425 * @return NULL when there is no grouping with the specified name
3426 * @return Pointer to the grouping of the specified @p name.
3427 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003428static struct lysp_node_grp *
3429match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003430{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003431 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003432
Radek Krejci2a9fc652021-01-22 17:44:34 +01003433 LY_LIST_FOR(grps, grp) {
3434 if (!strcmp(grp->name, name)) {
3435 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003436 }
3437 }
3438
3439 return NULL;
3440}
3441
3442/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003443 * @brief Find grouping for a uses.
3444 *
3445 * @param[in] ctx Compile context.
3446 * @param[in] uses_p Parsed uses node.
3447 * @param[out] gpr_p Found grouping on success.
3448 * @param[out] grp_pmod Module of @p grp_p on success.
3449 * @return LY_ERR value.
3450 */
3451static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003452lys_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 +02003453 struct lysp_module **grp_pmod)
3454{
3455 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003456 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003457 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003458 const char *id, *name, *prefix, *local_pref;
3459 size_t prefix_len, name_len;
3460 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003461 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003462
3463 *grp_p = NULL;
3464 *grp_pmod = NULL;
3465
3466 /* search for the grouping definition */
3467 id = uses_p->name;
3468 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3469 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3470 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3471 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003472 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003473 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003474 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3475 found = ctx->pmod;
3476 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003477 }
3478 }
3479 } else {
3480 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003481 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003482 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003483 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003484 "Invalid prefix used for grouping reference.", uses_p->name);
3485 return LY_EVALID;
3486 }
3487 pmod = mod->parsed;
3488 }
3489
3490 if (!found) {
3491 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003492 if ((grp = match_grouping(pmod->groupings, name))) {
3493 found = pmod;
3494 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003495 /* ... and all the submodules */
3496 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003497 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3498 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003499 break;
3500 }
3501 }
3502 }
3503 }
3504 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003505 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003506 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3507 return LY_EVALID;
3508 }
3509
3510 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3511 /* remember that the grouping is instantiated to avoid its standalone validation */
3512 grp->flags |= LYS_USED_GRP;
3513 }
3514
3515 *grp_p = grp;
3516 *grp_pmod = found;
3517 return LY_SUCCESS;
3518}
3519
3520/**
3521 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3522 * If present, also apply uses's modificators.
3523 *
3524 * @param[in] ctx Compile context
3525 * @param[in] uses_p Parsed uses schema node.
3526 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3527 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3528 * the compile context.
3529 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3530 */
3531static LY_ERR
3532lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3533{
3534 struct lysp_node *pnode;
3535 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003536 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003537 uint32_t i, grp_stack_count;
3538 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3539 LY_ERR ret = LY_SUCCESS;
3540 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003541 struct ly_set uses_child_set = {0};
3542
3543 /* find the referenced grouping */
3544 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3545
3546 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3547 grp_stack_count = ctx->groupings.count;
3548 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3549 if (grp_stack_count == ctx->groupings.count) {
3550 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003551 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003552 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3553 return LY_EVALID;
3554 }
3555
3556 /* check status */
3557 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3558 LY_CHECK_GOTO(ret, cleanup);
3559
3560 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3561 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3562 LY_CHECK_GOTO(ret, cleanup);
3563
3564 /* switch context's parsed module being processed */
3565 ctx->pmod = grp_mod;
3566
3567 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003568 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003569 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3570 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003571 LY_CHECK_GOTO(ret, cleanup);
3572 }
3573
3574 if (child_set) {
3575 /* add these children to our compiled child_set as well since uses is a schema-only node */
3576 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3577 }
3578
3579 if (uses_p->when) {
3580 /* pass uses's when to all the data children */
3581 for (i = 0; i < uses_child_set.count; ++i) {
3582 child = uses_child_set.snodes[i];
3583
Michal Vasko72244882021-01-12 15:21:05 +01003584 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 +02003585 LY_CHECK_GOTO(ret, cleanup);
3586 }
3587 }
3588
3589 /* compile actions */
3590 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003591 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003592 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3593 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003594 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003595 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3596 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003597 ret = LY_EVALID;
3598 goto cleanup;
3599 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003600 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3601 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3602 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3603 LY_CHECK_GOTO(ret, cleanup);
3604 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003605
3606 if (uses_p->when) {
3607 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003608 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3609 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 +02003610 LY_CHECK_GOTO(ret, cleanup);
3611 }
3612 }
3613 }
3614
3615 /* compile notifications */
3616 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003617 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003618 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3619 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003620 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003621 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3622 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003623 ret = LY_EVALID;
3624 goto cleanup;
3625 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003626
3627 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3628 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3629 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3630 LY_CHECK_GOTO(ret, cleanup);
3631 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003632
3633 if (uses_p->when) {
3634 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003635 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3636 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 +02003637 LY_CHECK_GOTO(ret, cleanup);
3638 }
3639 }
3640 }
3641
3642 /* check that all augments were applied */
3643 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003644 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3645 /* augment of some parent uses, irrelevant now */
3646 continue;
3647 }
3648
3649 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003650 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3651 ret = LY_ENOTFOUND;
3652 }
3653 LY_CHECK_GOTO(ret, cleanup);
3654
3655 /* check that all refines were applied */
3656 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003657 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3658 /* refine of some paretn uses, irrelevant now */
3659 continue;
3660 }
3661
3662 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003663 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3664 ret = LY_ENOTFOUND;
3665 }
3666 LY_CHECK_GOTO(ret, cleanup);
3667
3668cleanup:
3669 /* reload previous context's parsed module being processed */
3670 ctx->pmod = mod_old;
3671
3672 /* remove the grouping from the stack for circular groupings dependency check */
3673 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3674 assert(ctx->groupings.count == grp_stack_count);
3675
3676 ly_set_erase(&uses_child_set, NULL);
3677 return ret;
3678}
3679
3680static int
3681lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3682{
3683 struct lysp_node *iter;
3684 int len = 0;
3685
3686 *path = NULL;
3687 for (iter = node; iter && len >= 0; iter = iter->parent) {
3688 char *s = *path;
3689 char *id;
3690
3691 switch (iter->nodetype) {
3692 case LYS_USES:
3693 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3694 break;
3695 case LYS_GROUPING:
3696 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3697 break;
3698 case LYS_AUGMENT:
3699 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3700 break;
3701 default:
3702 id = strdup(iter->name);
3703 break;
3704 }
3705
3706 if (!iter->parent) {
3707 /* print prefix */
3708 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3709 } else {
3710 /* prefix is the same as in parent */
3711 len = asprintf(path, "/%s%s", id, s ? s : "");
3712 }
3713 free(s);
3714 free(id);
3715 }
3716
3717 if (len < 0) {
3718 free(*path);
3719 *path = NULL;
3720 } else if (len == 0) {
3721 *path = strdup("/");
3722 len = 1;
3723 }
3724 return len;
3725}
3726
3727LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003728lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003729{
3730 LY_ERR ret;
3731 char *path;
3732 int len;
3733
3734 struct lysp_node_uses fake_uses = {
3735 .parent = pnode,
3736 .nodetype = LYS_USES,
3737 .flags = 0, .next = NULL,
3738 .name = grp->name,
3739 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3740 .refines = NULL, .augments = NULL
3741 };
3742 struct lysc_node_container fake_container = {
3743 .nodetype = LYS_CONTAINER,
3744 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3745 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003746 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003747 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003748 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003749 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003750 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3751 };
3752
3753 if (grp->parent) {
3754 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3755 }
3756
3757 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3758 if (len < 0) {
3759 LOGMEM(ctx->ctx);
3760 return LY_EMEM;
3761 }
3762 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3763 ctx->path_len = (uint32_t)len;
3764 free(path);
3765
3766 lysc_update_path(ctx, NULL, "{grouping}");
3767 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003768 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003769 lysc_update_path(ctx, NULL, NULL);
3770 lysc_update_path(ctx, NULL, NULL);
3771
3772 ctx->path_len = 1;
3773 ctx->path[1] = '\0';
3774
3775 /* cleanup */
3776 lysc_node_container_free(ctx->ctx, &fake_container);
3777
3778 return ret;
3779}
3780
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003781LY_ERR
3782lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3783 struct ly_set *child_set)
3784{
3785 LY_ERR ret = LY_SUCCESS;
3786 struct lysc_node *node = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003787 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003788
3789 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3790
3791 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003792 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003793 } else {
3794 lysc_update_path(ctx, NULL, "{uses}");
3795 lysc_update_path(ctx, NULL, pnode->name);
3796 }
3797
3798 switch (pnode->nodetype) {
3799 case LYS_CONTAINER:
3800 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3801 node_compile_spec = lys_compile_node_container;
3802 break;
3803 case LYS_LEAF:
3804 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3805 node_compile_spec = lys_compile_node_leaf;
3806 break;
3807 case LYS_LIST:
3808 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3809 node_compile_spec = lys_compile_node_list;
3810 break;
3811 case LYS_LEAFLIST:
3812 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3813 node_compile_spec = lys_compile_node_leaflist;
3814 break;
3815 case LYS_CHOICE:
3816 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3817 node_compile_spec = lys_compile_node_choice;
3818 break;
3819 case LYS_CASE:
3820 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3821 node_compile_spec = lys_compile_node_case;
3822 break;
3823 case LYS_ANYXML:
3824 case LYS_ANYDATA:
3825 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3826 node_compile_spec = lys_compile_node_any;
3827 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003828 case LYS_RPC:
3829 case LYS_ACTION:
Radek Krejci91531e12021-02-08 19:57:54 +01003830 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003831 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3832 "Action \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003833 (ctx->options & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003834 return LY_EVALID;
3835 }
3836 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3837 node_compile_spec = lys_compile_node_action;
Radek Krejci91531e12021-02-08 19:57:54 +01003838 ctx->options |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003839 break;
3840 case LYS_NOTIF:
Radek Krejci91531e12021-02-08 19:57:54 +01003841 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003842 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3843 "Notification \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003844 (ctx->options & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003845 return LY_EVALID;
3846 }
3847 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3848 node_compile_spec = lys_compile_node_notif;
3849 ctx->options |= LYS_COMPILE_NOTIFICATION;
3850 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003851 case LYS_USES:
3852 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3853 lysc_update_path(ctx, NULL, NULL);
3854 lysc_update_path(ctx, NULL, NULL);
3855 return ret;
3856 default:
3857 LOGINT(ctx->ctx);
3858 return LY_EINT;
3859 }
3860 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3861
Radek Krejci2a9fc652021-01-22 17:44:34 +01003862 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003863
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003864 ctx->options = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003865 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003866 return ret;
3867}