blob: d638a8f0ef2a5eac572e0288a85b80c4003b4cc1 [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"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020034#include "plugins_types.h"
35#include "schema_compile.h"
36#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010037#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "set.h"
39#include "tree.h"
40#include "tree_data.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020042#include "tree_schema.h"
43#include "tree_schema_internal.h"
44#include "xpath.h"
45
46static struct lysc_ext_instance *
47lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
48{
49 /* TODO - extensions, increase refcount */
50 (void) ctx;
51 (void) orig;
52 return NULL;
53}
54
55/**
56 * @brief Add/replace a leaf default value in unres.
57 * Can also be used for a single leaf-list default value.
58 *
59 * @param[in] ctx Compile context.
60 * @param[in] leaf Leaf with the default value.
61 * @param[in] dflt Default value to use.
62 * @return LY_ERR value.
63 */
64static LY_ERR
65lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
66{
67 struct lysc_unres_dflt *r = NULL;
68 uint32_t i;
69
Michal Vasko12606ee2020-11-25 17:05:11 +010070 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010071 return LY_SUCCESS;
72 }
73
Michal Vasko405cc9e2020-12-01 12:01:27 +010074 for (i = 0; i < ctx->unres->dflts.count; ++i) {
75 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020076 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010077 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020078 lysp_qname_free(ctx->ctx, r->dflt);
79 free(r->dflt);
80 break;
81 }
82 }
83 if (!r) {
84 /* add new unres item */
85 r = calloc(1, sizeof *r);
86 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
87 r->leaf = leaf;
88
Michal Vasko405cc9e2020-12-01 12:01:27 +010089 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020090 }
91
92 r->dflt = malloc(sizeof *r->dflt);
93 LY_CHECK_GOTO(!r->dflt, error);
94 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
95
96 return LY_SUCCESS;
97
98error:
99 free(r->dflt);
100 LOGMEM(ctx->ctx);
101 return LY_EMEM;
102}
103
104/**
105 * @brief Add/replace a leaf-list default value(s) in unres.
106 *
107 * @param[in] ctx Compile context.
108 * @param[in] llist Leaf-list with the default value.
109 * @param[in] dflts Sized array of the default values.
110 * @return LY_ERR value.
111 */
112static LY_ERR
113lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
114{
115 struct lysc_unres_dflt *r = NULL;
116 uint32_t i;
117
Michal Vasko12606ee2020-11-25 17:05:11 +0100118 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100119 return LY_SUCCESS;
120 }
121
Michal Vasko405cc9e2020-12-01 12:01:27 +0100122 for (i = 0; i < ctx->unres->dflts.count; ++i) {
123 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200124 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100125 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200126 lysp_qname_free(ctx->ctx, r->dflt);
127 free(r->dflt);
128 r->dflt = NULL;
129 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
130 r->dflts = NULL;
131 break;
132 }
133 }
134 if (!r) {
135 r = calloc(1, sizeof *r);
136 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
137 r->llist = llist;
138
Michal Vasko405cc9e2020-12-01 12:01:27 +0100139 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200140 }
141
142 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
143
144 return LY_SUCCESS;
145}
146
147/**
148 * @brief Duplicate the compiled pattern structure.
149 *
150 * Instead of duplicating memory, the reference counter in the @p orig is increased.
151 *
152 * @param[in] orig The pattern structure to duplicate.
153 * @return The duplicated structure to use.
154 */
155static struct lysc_pattern *
156lysc_pattern_dup(struct lysc_pattern *orig)
157{
158 ++orig->refcount;
159 return orig;
160}
161
162/**
163 * @brief Duplicate the array of compiled patterns.
164 *
165 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
166 *
167 * @param[in] ctx Libyang context for logging.
168 * @param[in] orig The patterns sized array to duplicate.
169 * @return New sized array as a copy of @p orig.
170 * @return NULL in case of memory allocation error.
171 */
172static struct lysc_pattern **
173lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
174{
175 struct lysc_pattern **dup = NULL;
176 LY_ARRAY_COUNT_TYPE u;
177
178 assert(orig);
179
180 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
181 LY_ARRAY_FOR(orig, u) {
182 dup[u] = lysc_pattern_dup(orig[u]);
183 LY_ARRAY_INCREMENT(dup);
184 }
185 return dup;
186}
187
188/**
189 * @brief Duplicate compiled range structure.
190 *
191 * @param[in] ctx Libyang context for logging.
192 * @param[in] orig The range structure to be duplicated.
193 * @return New compiled range structure as a copy of @p orig.
194 * @return NULL in case of memory allocation error.
195 */
196static struct lysc_range *
197lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
198{
199 struct lysc_range *dup;
200 LY_ERR ret;
201
202 assert(orig);
203
204 dup = calloc(1, sizeof *dup);
205 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
206 if (orig->parts) {
207 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100208 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200209 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
210 }
211 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
212 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
213 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
214
215 return dup;
216cleanup:
217 free(dup);
218 (void) ret; /* set but not used due to the return type */
219 return NULL;
220}
221
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200222/**
223 * @brief Compile information from the when statement
224 *
225 * @param[in] ctx Compile context.
226 * @param[in] when_p Parsed when structure.
227 * @param[in] flags Flags of the parsed node with the when statement.
228 * @param[in] ctx_node Context node for the when statement.
229 * @param[out] when Pointer where to store pointer to the created compiled when structure.
230 * @return LY_ERR value.
231 */
232static LY_ERR
233lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
234 struct lysc_when **when)
235{
236 LY_ERR ret = LY_SUCCESS;
Radek Krejci2926c1b2021-03-16 14:45:45 +0100237 LY_PREFIX_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200238
239 *when = calloc(1, sizeof **when);
240 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
241 (*when)->refcount = 1;
242 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci18c99162021-03-16 16:55:16 +0100243 LY_CHECK_RET(ly_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci2926c1b2021-03-16 14:45:45 +0100244 LY_PREF_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200245 (*when)->context = (struct lysc_node *)ctx_node;
246 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
247 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100248 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200249 (*when)->flags = flags & LYS_STATUS_MASK;
250
251done:
252 return ret;
253}
254
255LY_ERR
256lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
257 struct lysc_node *node, struct lysc_when **when_c)
258{
259 struct lysc_when **new_when, ***node_when;
260
261 assert(when_p);
262
263 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100264 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200265
266 /* create new when pointer */
267 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
268 if (!when_c || !(*when_c)) {
269 /* compile when */
270 LY_CHECK_RET(lys_compile_when_(ctx, when_p, flags, ctx_node, new_when));
271
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200272 /* remember the compiled when for sharing */
273 if (when_c) {
274 *when_c = *new_when;
275 }
276 } else {
277 /* use the previously compiled when */
278 ++(*when_c)->refcount;
279 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100280 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200281
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100282 if (!(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
283 /* do not check "when" semantics in a grouping, but repeat the check for different node because
284 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100285 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200286 }
287
288 return LY_SUCCESS;
289}
290
291/**
292 * @brief Compile information from the must statement
293 * @param[in] ctx Compile context.
294 * @param[in] must_p The parsed must statement structure.
295 * @param[in,out] must Prepared (empty) compiled must structure to fill.
296 * @return LY_ERR value.
297 */
298static LY_ERR
299lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
300{
301 LY_ERR ret = LY_SUCCESS;
Radek Krejci2926c1b2021-03-16 14:45:45 +0100302 LY_PREFIX_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200303
304 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci18c99162021-03-16 16:55:16 +0100305 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 +0100306 LY_PREF_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200307 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
308 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
309 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
310 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100311 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200312
313done:
314 return ret;
315}
316
317/**
318 * @brief Validate and normalize numeric value from a range definition.
319 * @param[in] ctx Compile context.
320 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
321 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
322 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
323 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
324 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
325 * @param[in] value String value of the range boundary.
326 * @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.
327 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
328 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
329 */
330static LY_ERR
331range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
332 size_t *len, char **valcopy)
333{
334 size_t fraction = 0, size;
335
336 *len = 0;
337
338 assert(value);
339 /* parse value */
340 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
341 return LY_EVALID;
342 }
343
344 if ((value[*len] == '-') || (value[*len] == '+')) {
345 ++(*len);
346 }
347
348 while (isdigit(value[*len])) {
349 ++(*len);
350 }
351
352 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
353 if (basetype == LY_TYPE_DEC64) {
354 goto decimal;
355 } else {
356 *valcopy = strndup(value, *len);
357 return LY_SUCCESS;
358 }
359 }
360 fraction = *len;
361
362 ++(*len);
363 while (isdigit(value[*len])) {
364 ++(*len);
365 }
366
367 if (basetype == LY_TYPE_DEC64) {
368decimal:
369 assert(frdigits);
370 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100371 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200372 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100373 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200374 return LY_EINVAL;
375 }
376 if (fraction) {
377 size = (*len) + (frdigits - ((*len) - 1 - fraction));
378 } else {
379 size = (*len) + frdigits + 1;
380 }
381 *valcopy = malloc(size * sizeof **valcopy);
382 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
383
384 (*valcopy)[size - 1] = '\0';
385 if (fraction) {
386 memcpy(&(*valcopy)[0], &value[0], fraction);
387 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
388 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
389 } else {
390 memcpy(&(*valcopy)[0], &value[0], *len);
391 memset(&(*valcopy)[*len], '0', frdigits);
392 }
393 }
394 return LY_SUCCESS;
395}
396
397/**
398 * @brief Check that values in range are in ascendant order.
399 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
400 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
401 * max can be also equal.
402 * @param[in] value Current value to check.
403 * @param[in] prev_value The last seen value.
404 * @return LY_SUCCESS or LY_EEXIST for invalid order.
405 */
406static LY_ERR
407range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
408{
409 if (unsigned_value) {
410 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
411 return LY_EEXIST;
412 }
413 } else {
414 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
415 return LY_EEXIST;
416 }
417 }
418 return LY_SUCCESS;
419}
420
421/**
422 * @brief Set min/max value of the range part.
423 * @param[in] ctx Compile context.
424 * @param[in] part Range part structure to fill.
425 * @param[in] max Flag to distinguish if storing min or max value.
426 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
427 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
428 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
429 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
430 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
431 * @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.
432 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
433 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
434 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
435 * frdigits value), LY_EMEM.
436 */
437static LY_ERR
438range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
439 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
440{
441 LY_ERR ret = LY_SUCCESS;
442 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100443 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200444
445 if (value) {
446 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
447 LY_CHECK_GOTO(ret, finalize);
448 }
449 if (!valcopy && base_range) {
450 if (max) {
451 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
452 } else {
453 part->min_64 = base_range->parts[0].min_64;
454 }
455 if (!first) {
456 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
457 }
458 goto finalize;
459 }
460
461 switch (basetype) {
462 case LY_TYPE_INT8: /* range */
463 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100464 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 +0200465 } else if (max) {
466 part->max_64 = INT64_C(127);
467 } else {
468 part->min_64 = INT64_C(-128);
469 }
470 if (!ret && !first) {
471 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
472 }
473 break;
474 case LY_TYPE_INT16: /* range */
475 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100476 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
477 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200478 } else if (max) {
479 part->max_64 = INT64_C(32767);
480 } else {
481 part->min_64 = INT64_C(-32768);
482 }
483 if (!ret && !first) {
484 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
485 }
486 break;
487 case LY_TYPE_INT32: /* range */
488 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100489 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
490 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200491 } else if (max) {
492 part->max_64 = INT64_C(2147483647);
493 } else {
494 part->min_64 = INT64_C(-2147483648);
495 }
496 if (!ret && !first) {
497 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
498 }
499 break;
500 case LY_TYPE_INT64: /* range */
501 case LY_TYPE_DEC64: /* range */
502 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100503 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
504 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200505 } else if (max) {
506 part->max_64 = INT64_C(9223372036854775807);
507 } else {
508 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
509 }
510 if (!ret && !first) {
511 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
512 }
513 break;
514 case LY_TYPE_UINT8: /* range */
515 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100516 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 +0200517 } else if (max) {
518 part->max_u64 = UINT64_C(255);
519 } else {
520 part->min_u64 = UINT64_C(0);
521 }
522 if (!ret && !first) {
523 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
524 }
525 break;
526 case LY_TYPE_UINT16: /* range */
527 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100528 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 +0200529 } else if (max) {
530 part->max_u64 = UINT64_C(65535);
531 } else {
532 part->min_u64 = UINT64_C(0);
533 }
534 if (!ret && !first) {
535 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
536 }
537 break;
538 case LY_TYPE_UINT32: /* range */
539 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100540 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
541 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200542 } else if (max) {
543 part->max_u64 = UINT64_C(4294967295);
544 } else {
545 part->min_u64 = UINT64_C(0);
546 }
547 if (!ret && !first) {
548 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
549 }
550 break;
551 case LY_TYPE_UINT64: /* range */
552 case LY_TYPE_STRING: /* length */
553 case LY_TYPE_BINARY: /* length */
554 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100555 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
556 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200557 } else if (max) {
558 part->max_u64 = UINT64_C(18446744073709551615);
559 } else {
560 part->min_u64 = UINT64_C(0);
561 }
562 if (!ret && !first) {
563 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
564 }
565 break;
566 default:
567 LOGINT(ctx->ctx);
568 ret = LY_EINT;
569 }
570
571finalize:
572 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100573 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200574 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
575 length_restr ? "length" : "range", valcopy ? valcopy : *value);
576 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100577 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200578 "Invalid %s restriction - invalid value \"%s\".",
579 length_restr ? "length" : "range", valcopy ? valcopy : *value);
580 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100581 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200582 "Invalid %s restriction - values are not in ascending order (%s).",
583 length_restr ? "length" : "range",
584 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
585 } else if (!ret && value) {
586 *value = *value + len;
587 }
588 free(valcopy);
589 return ret;
590}
591
592/**
593 * @brief Compile the parsed range restriction.
594 * @param[in] ctx Compile context.
595 * @param[in] range_p Parsed range structure to compile.
596 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
597 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
598 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
599 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
600 * range restriction must be more restrictive than the base_range.
601 * @param[in,out] range Pointer to the created current range structure.
602 * @return LY_ERR value.
603 */
604static LY_ERR
605lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
606 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
607{
608 LY_ERR ret = LY_EVALID;
609 const char *expr;
610 struct lysc_range_part *parts = NULL, *part;
611 ly_bool range_expected = 0, uns;
612 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
613
614 assert(range);
615 assert(range_p);
616
617 expr = range_p->arg.str;
618 while (1) {
619 if (isspace(*expr)) {
620 ++expr;
621 } else if (*expr == '\0') {
622 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200624 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
625 length_restr ? "length" : "range", range_p->arg);
626 goto cleanup;
627 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100628 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200629 "Invalid %s restriction - unexpected end of the expression (%s).",
630 length_restr ? "length" : "range", range_p->arg);
631 goto cleanup;
632 }
633 parts_done++;
634 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100635 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200636 if (parts) {
637 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100638 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200639 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100640 (int)(expr - range_p->arg.str), range_p->arg.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200641 goto cleanup;
642 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100643 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200644
645 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
646 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
647 part->max_64 = part->min_64;
648 } else if (*expr == '|') {
649 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100650 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200651 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
652 goto cleanup;
653 }
654 expr++;
655 parts_done++;
656 /* process next part of the expression */
657 } else if (!strncmp(expr, "..", 2)) {
658 expr += 2;
659 while (isspace(*expr)) {
660 expr++;
661 }
662 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100663 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200664 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
665 goto cleanup;
666 }
667 /* continue expecting the upper boundary */
668 range_expected = 1;
669 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
670 /* number */
671 if (range_expected) {
672 part = &parts[LY_ARRAY_COUNT(parts) - 1];
673 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
674 range_expected = 0;
675 } else {
676 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
677 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
678 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
679 part->max_64 = part->min_64;
680 }
681
682 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100683 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
684 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200685 while (isspace(*expr)) {
686 expr++;
687 }
688 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100689 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200690 length_restr ? "length" : "range", expr);
691 goto cleanup;
692 }
693 if (range_expected) {
694 part = &parts[LY_ARRAY_COUNT(parts) - 1];
695 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
696 range_expected = 0;
697 } else {
698 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
699 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
700 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
701 part->min_64 = part->max_64;
702 }
703 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100704 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200705 length_restr ? "length" : "range", expr);
706 goto cleanup;
707 }
708 }
709
710 /* check with the previous range/length restriction */
711 if (base_range) {
712 switch (basetype) {
713 case LY_TYPE_BINARY:
714 case LY_TYPE_UINT8:
715 case LY_TYPE_UINT16:
716 case LY_TYPE_UINT32:
717 case LY_TYPE_UINT64:
718 case LY_TYPE_STRING:
719 uns = 1;
720 break;
721 case LY_TYPE_DEC64:
722 case LY_TYPE_INT8:
723 case LY_TYPE_INT16:
724 case LY_TYPE_INT32:
725 case LY_TYPE_INT64:
726 uns = 0;
727 break;
728 default:
729 LOGINT(ctx->ctx);
730 ret = LY_EINT;
731 goto cleanup;
732 }
733 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
734 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
735 goto baseerror;
736 }
737 /* current lower bound is not lower than the base */
738 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
739 /* base has single value */
740 if (base_range->parts[v].min_64 == parts[u].min_64) {
741 /* both lower bounds are the same */
742 if (parts[u].min_64 != parts[u].max_64) {
743 /* current continues with a range */
744 goto baseerror;
745 } else {
746 /* equal single values, move both forward */
747 ++v;
748 continue;
749 }
750 } else {
751 /* base is single value lower than current range, so the
752 * value from base range is removed in the current,
753 * move only base and repeat checking */
754 ++v;
755 --u;
756 continue;
757 }
758 } else {
759 /* base is the range */
760 if (parts[u].min_64 == parts[u].max_64) {
761 /* current is a single value */
762 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
763 /* current is behind the base range, so base range is omitted,
764 * move the base and keep the current for further check */
765 ++v;
766 --u;
767 } /* else it is within the base range, so move the current, but keep the base */
768 continue;
769 } else {
770 /* both are ranges - check the higher bound, the lower was already checked */
771 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
772 /* higher bound is higher than the current higher bound */
773 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
774 /* but the current lower bound is also higher, so the base range is omitted,
775 * continue with the same current, but move the base */
776 --u;
777 ++v;
778 continue;
779 }
780 /* current range starts within the base range but end behind it */
781 goto baseerror;
782 } else {
783 /* current range is smaller than the base,
784 * move current, but stay with the base */
785 continue;
786 }
787 }
788 }
789 }
790 if (u != parts_done) {
791baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100792 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200793 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
794 length_restr ? "length" : "range", range_p->arg);
795 goto cleanup;
796 }
797 }
798
799 if (!(*range)) {
800 *range = calloc(1, sizeof **range);
801 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
802 }
803
804 /* we rewrite the following values as the types chain is being processed */
805 if (range_p->eapptag) {
806 lydict_remove(ctx->ctx, (*range)->eapptag);
807 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
808 }
809 if (range_p->emsg) {
810 lydict_remove(ctx->ctx, (*range)->emsg);
811 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
812 }
813 if (range_p->dsc) {
814 lydict_remove(ctx->ctx, (*range)->dsc);
815 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
816 }
817 if (range_p->ref) {
818 lydict_remove(ctx->ctx, (*range)->ref);
819 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
820 }
821 /* extensions are taken only from the last range by the caller */
822
823 (*range)->parts = parts;
824 parts = NULL;
825 ret = LY_SUCCESS;
826cleanup:
827 LY_ARRAY_FREE(parts);
828
829 return ret;
830}
831
832LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100833lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200834{
835 size_t idx, idx2, start, end, size, brack;
836 char *perl_regex, *ptr;
Christian Hoppsdafed222021-03-22 13:02:42 -0400837 int err_code, compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200838 const char *orig_ptr;
839 PCRE2_SIZE err_offset;
840 pcre2_code *code_local;
841
842#define URANGE_LEN 19
843 char *ublock2urange[][2] = {
844 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
845 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
846 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
847 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
848 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
849 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
850 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
851 {"Greek", "[\\x{0370}-\\x{03FF}]"},
852 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
853 {"Armenian", "[\\x{0530}-\\x{058F}]"},
854 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
855 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
856 {"Syriac", "[\\x{0700}-\\x{074F}]"},
857 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
858 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
859 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
860 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
861 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
862 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
863 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
864 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
865 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
866 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
867 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
868 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
869 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
870 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
871 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
872 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
873 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
874 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
875 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
876 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
877 {"Ogham", "[\\x{1680}-\\x{169F}]"},
878 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
879 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
880 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
881 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
882 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
883 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
884 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
885 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
886 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
887 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
888 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
889 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
890 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
891 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
892 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
893 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
894 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
895 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
896 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
897 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
898 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
899 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
900 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
901 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
902 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
903 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
904 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
905 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
906 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
907 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
908 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
909 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
910 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
911 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
912 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
913 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
914 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
915 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
916 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
917 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
918 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
919 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
920 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
921 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
922 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
923 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
924 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
925 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
926 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100927 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200928 {NULL, NULL}
929 };
930
931 /* adjust the expression to a Perl equivalent
932 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
933
934 /* allocate space for the transformed pattern */
935 size = strlen(pattern) + 1;
Christian Hoppsdafed222021-03-22 13:02:42 -0400936 compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
937#ifdef PCRE2_ENDANCHORED
938 compile_opts |= PCRE2_ENDANCHORED;
939#else
940 /* add space for trailing $ anchor */
941 size++;
942#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200943 perl_regex = malloc(size);
944 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
945 perl_regex[0] = '\0';
946
947 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
948 brack = 0;
949 idx = 0;
950 orig_ptr = pattern;
951 while (orig_ptr[0]) {
952 switch (orig_ptr[0]) {
953 case '$':
954 case '^':
955 if (!brack) {
956 /* make space for the extra character */
957 ++size;
958 perl_regex = ly_realloc(perl_regex, size);
959 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
960
961 /* print escape slash */
962 perl_regex[idx] = '\\';
963 ++idx;
964 }
965 break;
966 case '[':
967 /* must not be escaped */
968 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
969 ++brack;
970 }
971 break;
972 case ']':
973 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
974 /* pattern was checked and compiled already */
975 assert(brack);
976 --brack;
977 }
978 break;
979 default:
980 break;
981 }
982
983 /* copy char */
984 perl_regex[idx] = orig_ptr[0];
985
986 ++idx;
987 ++orig_ptr;
988 }
Christian Hoppsdafed222021-03-22 13:02:42 -0400989#ifndef PCRE2_ENDANCHORED
990 /* anchor match to end of subject */
991 perl_regex[idx++] = '$';
992#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200993 perl_regex[idx] = '\0';
994
995 /* substitute Unicode Character Blocks with exact Character Ranges */
996 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
997 start = ptr - perl_regex;
998
999 ptr = strchr(ptr, '}');
1000 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001001 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001002 free(perl_regex);
1003 return LY_EVALID;
1004 }
1005 end = (ptr - perl_regex) + 1;
1006
1007 /* need more space */
1008 if (end - start < URANGE_LEN) {
1009 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1010 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1011 }
1012
1013 /* find our range */
1014 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001015 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1016 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001017 break;
1018 }
1019 }
1020 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001021 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001022 free(perl_regex);
1023 return LY_EVALID;
1024 }
1025
1026 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1027 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1028 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1029 ++idx;
1030 }
1031 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1032 --idx;
1033 }
1034 }
1035 if (idx) {
1036 /* skip brackets */
1037 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1038 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1039 } else {
1040 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1041 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1042 }
1043 }
1044
1045 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001046 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001047 &err_code, &err_offset, NULL);
1048 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001049 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1050 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001051 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001052 free(perl_regex);
1053 return LY_EVALID;
1054 }
1055 free(perl_regex);
1056
1057 if (code) {
1058 *code = code_local;
1059 } else {
1060 free(code_local);
1061 }
1062
1063 return LY_SUCCESS;
1064
1065#undef URANGE_LEN
1066}
1067
1068/**
1069 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1070 * @param[in] ctx Compile context.
1071 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1072 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1073 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1074 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1075 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1076 */
1077static LY_ERR
1078lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1079 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1080{
1081 struct lysc_pattern **pattern;
1082 LY_ARRAY_COUNT_TYPE u;
1083 LY_ERR ret = LY_SUCCESS;
1084
1085 /* first, copy the patterns from the base type */
1086 if (base_patterns) {
1087 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1088 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1089 }
1090
1091 LY_ARRAY_FOR(patterns_p, u) {
1092 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1093 *pattern = calloc(1, sizeof **pattern);
1094 ++(*pattern)->refcount;
1095
Radek Krejci2efc45b2020-12-22 16:25:44 +01001096 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001097 LY_CHECK_RET(ret);
1098
Radek Krejcif13b87b2020-12-01 22:02:17 +01001099 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001100 (*pattern)->inverted = 1;
1101 }
1102 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1103 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1104 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1105 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1106 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001107 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001108 }
1109done:
1110 return ret;
1111}
1112
1113/**
1114 * @brief map of the possible restrictions combination for the specific built-in type.
1115 */
1116static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1117 0 /* LY_TYPE_UNKNOWN */,
1118 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1119 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1120 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1121 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1122 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1123 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1124 LYS_SET_BIT /* LY_TYPE_BITS */,
1125 0 /* LY_TYPE_BOOL */,
1126 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1127 0 /* LY_TYPE_EMPTY */,
1128 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1129 LYS_SET_BASE /* LY_TYPE_IDENT */,
1130 LYS_SET_REQINST /* LY_TYPE_INST */,
1131 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1132 LYS_SET_TYPE /* LY_TYPE_UNION */,
1133 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1134 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1135 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1136 LYS_SET_RANGE /* LY_TYPE_INT64 */
1137};
1138
1139/**
1140 * @brief stringification of the YANG built-in data types
1141 */
1142const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
1143 "unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1144 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1145 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
1146};
1147
1148/**
1149 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1150 * @param[in] ctx Compile context.
1151 * @param[in] enums_p Array of the parsed enum structures to compile.
1152 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1153 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1154 * @param[out] enums Newly created array of the compiled enums information for the current type.
1155 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1156 */
1157static LY_ERR
1158lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
1159 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
1160{
1161 LY_ERR ret = LY_SUCCESS;
1162 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001163 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1164 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001165 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001166 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001167
1168 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001169 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001170 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1171 return LY_EVALID;
1172 }
1173
1174 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001175 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001176 if (base_enums) {
1177 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1178 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001179 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001180 break;
1181 }
1182 }
1183 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001184 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001185 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001186 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001187 return LY_EVALID;
1188 }
1189 match = v;
1190 }
1191
1192 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001193 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001194 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001195 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001196 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001197 LY_ARRAY_FOR(*enums, v) {
1198 if (cur_val == (*enums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001199 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001200 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001201 cur_val, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001202 return LY_EVALID;
1203 }
1204 }
1205 } else if (base_enums) {
1206 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001207 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001208 } else {
1209 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001210 if (u == 0) {
1211 cur_val = 0;
1212 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001213 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001214 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001215 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001216 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001217 } else {
1218 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001219 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001220 }
1221
1222 /* save highest value for auto assing */
1223 if (highest_value < cur_val) {
1224 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001225 }
1226 } else { /* LY_TYPE_BITS */
1227 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001228 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001229 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001230 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001231 LY_ARRAY_FOR(*enums, v) {
1232 if (cur_pos == (*enums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001233 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001234 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001235 cur_pos, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001236 return LY_EVALID;
1237 }
1238 }
1239 } else if (base_enums) {
1240 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001241 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001242 } else {
1243 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001244 if (u == 0) {
1245 cur_pos = 0;
1246 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001247 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001248 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001250 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001251 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001252 } else {
1253 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001255 }
1256
1257 /* save highest position for auto assing */
1258 if (highest_position < cur_pos) {
1259 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 }
1261 }
1262
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001263 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001264 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001265 if (basetype == LY_TYPE_ENUM) {
1266 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001267 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001268 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001269 enums_p[u].name, base_enums[match].value, cur_val);
1270 return LY_EVALID;
1271 }
1272 } else {
1273 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001274 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001275 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001276 enums_p[u].name, base_enums[match].position, cur_pos);
1277 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001278 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001279 }
1280 }
1281
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001282 /* evaluate if-ffeatures */
1283 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1284 if (!enabled) {
1285 continue;
1286 }
1287
1288 /* add new enum/bit */
1289 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1290 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1291 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1292 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001293 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 +01001294 if (basetype == LY_TYPE_ENUM) {
1295 e->value = cur_val;
1296 } else {
1297 e->position = cur_pos;
1298 }
Radek Krejciab430862021-03-02 20:13:40 +01001299 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001300
1301 if (basetype == LY_TYPE_BITS) {
1302 /* keep bits ordered by position */
1303 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
1304 if (v != u) {
1305 memcpy(&storage, e, sizeof *e);
1306 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1307 memcpy(&(*enums)[v], &storage, sizeof storage);
1308 }
1309 }
1310 }
1311
1312done:
1313 return ret;
1314}
1315
Radek Krejci6a205692020-12-09 13:58:22 +01001316static LY_ERR
1317lys_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 +01001318 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001319{
1320 LY_ERR ret = LY_SUCCESS;
1321 struct lysc_type **utypes = *utypes_p;
1322 struct lysc_type_union *un_aux = NULL;
1323
1324 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1325 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001326 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1327 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001328 LY_CHECK_GOTO(ret, error);
1329 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1330 /* add space for additional types from the union subtype */
1331 un_aux = (struct lysc_type_union *)utypes[u + additional];
1332 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1333 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1334
1335 /* copy subtypes of the subtype union */
1336 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1337 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1338 struct lysc_type_leafref *lref;
1339
1340 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1341 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1342 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1343 lref = (struct lysc_type_leafref *)utypes[u + additional];
1344
1345 lref->basetype = LY_TYPE_LEAFREF;
1346 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1347 LY_CHECK_GOTO(ret, error);
1348 lref->refcount = 1;
1349 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
1350 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001351 ret = ly_type_prefix_data_dup(ctx->ctx, LY_PREF_SCHEMA_RESOLVED,
1352 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001353 LY_CHECK_GOTO(ret, error);
1354 /* TODO extensions */
1355
1356 } else {
1357 utypes[u + additional] = un_aux->types[v];
1358 ++un_aux->types[v]->refcount;
1359 }
1360 ++additional;
1361 LY_ARRAY_INCREMENT(utypes);
1362 }
1363 /* compensate u increment in main loop */
1364 --additional;
1365
1366 /* free the replaced union subtype */
1367 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1368 un_aux = NULL;
1369 } else {
1370 LY_ARRAY_INCREMENT(utypes);
1371 }
1372 }
1373
1374 *utypes_p = utypes;
1375 return LY_SUCCESS;
1376
1377error:
1378 if (un_aux) {
1379 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1380 }
1381 *utypes_p = utypes;
1382 return ret;
1383}
1384
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001385/**
1386 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1387 * @param[in] ctx Compile context.
1388 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1389 * @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 +02001390 * @param[in] context_name Name of the context node or referencing typedef for logging.
1391 * @param[in] type_p Parsed type to compile.
1392 * @param[in] basetype Base YANG built-in type of the type to compile.
1393 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1394 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1395 * @param[out] type Newly created type structure with the filled information about the type.
1396 * @return LY_ERR value.
1397 */
1398static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001399lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1400 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 +02001401{
1402 LY_ERR ret = LY_SUCCESS;
1403 struct lysc_type_bin *bin;
1404 struct lysc_type_num *num;
1405 struct lysc_type_str *str;
1406 struct lysc_type_bits *bits;
1407 struct lysc_type_enum *enumeration;
1408 struct lysc_type_dec *dec;
1409 struct lysc_type_identityref *idref;
1410 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001411 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001412
1413 switch (basetype) {
1414 case LY_TYPE_BINARY:
1415 bin = (struct lysc_type_bin *)(*type);
1416
1417 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1418 if (type_p->length) {
1419 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1420 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1421 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001422 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001423 }
1424 }
1425 break;
1426 case LY_TYPE_BITS:
1427 /* RFC 7950 9.7 - bits */
1428 bits = (struct lysc_type_bits *)(*type);
1429 if (type_p->bits) {
1430 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1431 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1432 (struct lysc_type_bitenum_item **)&bits->bits));
1433 }
1434
1435 if (!base && !type_p->flags) {
1436 /* type derived from bits built-in type must contain at least one bit */
1437 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001438 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001439 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001440 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001441 }
1442 return LY_EVALID;
1443 }
1444 break;
1445 case LY_TYPE_DEC64:
1446 dec = (struct lysc_type_dec *)(*type);
1447
1448 /* RFC 7950 9.3.4 - fraction-digits */
1449 if (!base) {
1450 if (!type_p->fraction_digits) {
1451 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001452 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001453 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001454 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001455 }
1456 return LY_EVALID;
1457 }
1458 dec->fraction_digits = type_p->fraction_digits;
1459 } else {
1460 if (type_p->fraction_digits) {
1461 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1462 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001463 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001464 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1465 tpdfname);
1466 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001467 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001468 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1469 }
1470 return LY_EVALID;
1471 }
1472 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1473 }
1474
1475 /* RFC 7950 9.2.4 - range */
1476 if (type_p->range) {
1477 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1478 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1479 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001480 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001481 }
1482 }
1483 break;
1484 case LY_TYPE_STRING:
1485 str = (struct lysc_type_str *)(*type);
1486
1487 /* RFC 7950 9.4.4 - length */
1488 if (type_p->length) {
1489 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1490 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1491 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001492 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001493 }
1494 } else if (base && ((struct lysc_type_str *)base)->length) {
1495 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1496 }
1497
1498 /* RFC 7950 9.4.5 - pattern */
1499 if (type_p->patterns) {
1500 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1501 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1502 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1503 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1504 }
1505 break;
1506 case LY_TYPE_ENUM:
1507 enumeration = (struct lysc_type_enum *)(*type);
1508
1509 /* RFC 7950 9.6 - enum */
1510 if (type_p->enums) {
1511 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1512 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1513 }
1514
1515 if (!base && !type_p->flags) {
1516 /* type derived from enumerations built-in type must contain at least one enum */
1517 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001518 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001519 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001520 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001521 }
1522 return LY_EVALID;
1523 }
1524 break;
1525 case LY_TYPE_INT8:
1526 case LY_TYPE_UINT8:
1527 case LY_TYPE_INT16:
1528 case LY_TYPE_UINT16:
1529 case LY_TYPE_INT32:
1530 case LY_TYPE_UINT32:
1531 case LY_TYPE_INT64:
1532 case LY_TYPE_UINT64:
1533 num = (struct lysc_type_num *)(*type);
1534
1535 /* RFC 6020 9.2.4 - range */
1536 if (type_p->range) {
1537 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1538 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1539 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001540 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001541 }
1542 }
1543 break;
1544 case LY_TYPE_IDENT:
1545 idref = (struct lysc_type_identityref *)(*type);
1546
1547 /* RFC 7950 9.10.2 - base */
1548 if (type_p->bases) {
1549 if (base) {
1550 /* only the directly derived identityrefs can contain base specification */
1551 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001552 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001553 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1554 tpdfname);
1555 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001556 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001557 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1558 }
1559 return LY_EVALID;
1560 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001561 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 +02001562 }
1563
1564 if (!base && !type_p->flags) {
1565 /* type derived from identityref built-in type must contain at least one base */
1566 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001567 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001568 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001569 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001570 }
1571 return LY_EVALID;
1572 }
1573 break;
1574 case LY_TYPE_LEAFREF:
1575 lref = (struct lysc_type_leafref *)*type;
1576
1577 /* RFC 7950 9.9.3 - require-instance */
1578 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001579 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001580 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001581 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001582 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1583 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001584 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001585 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1586 }
1587 return LY_EVALID;
1588 }
1589 lref->require_instance = type_p->require_instance;
1590 } else if (base) {
1591 /* inherit */
1592 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1593 } else {
1594 /* default is true */
1595 lref->require_instance = 1;
1596 }
1597 if (type_p->path) {
Radek Krejci2926c1b2021-03-16 14:45:45 +01001598 LY_PREFIX_FORMAT format;
1599
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001600 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci18c99162021-03-16 16:55:16 +01001601 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 +01001602 LY_PREF_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001603 } else if (base) {
1604 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci2926c1b2021-03-16 14:45:45 +01001605 LY_CHECK_RET(ly_type_prefix_data_dup(ctx->ctx, LY_PREF_SCHEMA_RESOLVED,
1606 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001607 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001608 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001609 return LY_EVALID;
1610 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001611 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001612 return LY_EVALID;
1613 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001614 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001615 break;
1616 case LY_TYPE_INST:
1617 /* RFC 7950 9.9.3 - require-instance */
1618 if (type_p->flags & LYS_SET_REQINST) {
1619 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1620 } else {
1621 /* default is true */
1622 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1623 }
1624 break;
1625 case LY_TYPE_UNION:
1626 un = (struct lysc_type_union *)(*type);
1627
1628 /* RFC 7950 7.4 - type */
1629 if (type_p->types) {
1630 if (base) {
1631 /* only the directly derived union can contain types specification */
1632 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001633 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001634 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1635 tpdfname);
1636 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001637 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001638 "Invalid type substatement for the type not directly derived from union built-in type.");
1639 }
1640 return LY_EVALID;
1641 }
1642 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001643 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 +02001644 }
1645
1646 if (!base && !type_p->flags) {
1647 /* type derived from union built-in type must contain at least one type */
1648 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001649 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001650 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001651 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001652 }
1653 return LY_EVALID;
1654 }
1655 break;
1656 case LY_TYPE_BOOL:
1657 case LY_TYPE_EMPTY:
1658 case LY_TYPE_UNKNOWN: /* just to complete switch */
1659 break;
1660 }
1661
1662 if (tpdfname) {
1663 switch (basetype) {
1664 case LY_TYPE_BINARY:
1665 type_p->compiled = *type;
1666 *type = calloc(1, sizeof(struct lysc_type_bin));
1667 break;
1668 case LY_TYPE_BITS:
1669 type_p->compiled = *type;
1670 *type = calloc(1, sizeof(struct lysc_type_bits));
1671 break;
1672 case LY_TYPE_DEC64:
1673 type_p->compiled = *type;
1674 *type = calloc(1, sizeof(struct lysc_type_dec));
1675 break;
1676 case LY_TYPE_STRING:
1677 type_p->compiled = *type;
1678 *type = calloc(1, sizeof(struct lysc_type_str));
1679 break;
1680 case LY_TYPE_ENUM:
1681 type_p->compiled = *type;
1682 *type = calloc(1, sizeof(struct lysc_type_enum));
1683 break;
1684 case LY_TYPE_INT8:
1685 case LY_TYPE_UINT8:
1686 case LY_TYPE_INT16:
1687 case LY_TYPE_UINT16:
1688 case LY_TYPE_INT32:
1689 case LY_TYPE_UINT32:
1690 case LY_TYPE_INT64:
1691 case LY_TYPE_UINT64:
1692 type_p->compiled = *type;
1693 *type = calloc(1, sizeof(struct lysc_type_num));
1694 break;
1695 case LY_TYPE_IDENT:
1696 type_p->compiled = *type;
1697 *type = calloc(1, sizeof(struct lysc_type_identityref));
1698 break;
1699 case LY_TYPE_LEAFREF:
1700 type_p->compiled = *type;
1701 *type = calloc(1, sizeof(struct lysc_type_leafref));
1702 break;
1703 case LY_TYPE_INST:
1704 type_p->compiled = *type;
1705 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1706 break;
1707 case LY_TYPE_UNION:
1708 type_p->compiled = *type;
1709 *type = calloc(1, sizeof(struct lysc_type_union));
1710 break;
1711 case LY_TYPE_BOOL:
1712 case LY_TYPE_EMPTY:
1713 case LY_TYPE_UNKNOWN: /* just to complete switch */
1714 break;
1715 }
1716 }
1717 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1718
1719cleanup:
1720 return ret;
1721}
1722
1723LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001724lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1725 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001726{
1727 LY_ERR ret = LY_SUCCESS;
1728 ly_bool dummyloops = 0;
1729 struct type_context {
1730 const struct lysp_tpdf *tpdf;
1731 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001732 } *tctx, *tctx_prev = NULL, *tctx_iter;
1733 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1734 struct lysc_type *base = NULL, *prev_type;
1735 struct ly_set tpdf_chain = {0};
1736
1737 (*type) = NULL;
1738 if (dflt) {
1739 *dflt = NULL;
1740 }
1741
1742 tctx = calloc(1, sizeof *tctx);
1743 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001744 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 +02001745 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001746 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1747 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001748 if (basetype) {
1749 break;
1750 }
1751
1752 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001753 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1754 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001755 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1756
1757 if (units && !*units) {
1758 /* inherit units */
1759 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1760 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1761 }
1762 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1763 /* inherit default */
1764 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1765 }
1766 if (dummyloops && (!units || *units) && dflt && *dflt) {
1767 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1768 break;
1769 }
1770
Radek Krejci720d2612021-03-03 19:44:22 +01001771 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1772 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1773 * and we need now recompile the type again in the updated context. */
1774 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1775 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1776 }
1777
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001778 if (tctx->tpdf->type.compiled) {
1779 /* it is not necessary to continue, the rest of the chain was already compiled,
1780 * but we still may need to inherit default and units values, so start dummy loops */
1781 basetype = tctx->tpdf->type.compiled->basetype;
1782 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1783 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1784
1785 if ((units && !*units) || (dflt && !*dflt)) {
1786 dummyloops = 1;
1787 goto preparenext;
1788 } else {
1789 tctx = NULL;
1790 break;
1791 }
1792 }
1793
1794 /* circular typedef reference detection */
1795 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1796 /* local part */
1797 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001798 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001799 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001800 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1801 free(tctx);
1802 ret = LY_EVALID;
1803 goto cleanup;
1804 }
1805 }
1806 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1807 /* global part for unions corner case */
1808 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001809 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001810 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001811 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1812 free(tctx);
1813 ret = LY_EVALID;
1814 goto cleanup;
1815 }
1816 }
1817
1818 /* store information for the following processing */
1819 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1820 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1821
1822preparenext:
1823 /* prepare next loop */
1824 tctx_prev = tctx;
1825 tctx = calloc(1, sizeof *tctx);
1826 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1827 }
1828 free(tctx);
1829
1830 /* allocate type according to the basetype */
1831 switch (basetype) {
1832 case LY_TYPE_BINARY:
1833 *type = calloc(1, sizeof(struct lysc_type_bin));
1834 break;
1835 case LY_TYPE_BITS:
1836 *type = calloc(1, sizeof(struct lysc_type_bits));
1837 break;
1838 case LY_TYPE_BOOL:
1839 case LY_TYPE_EMPTY:
1840 *type = calloc(1, sizeof(struct lysc_type));
1841 break;
1842 case LY_TYPE_DEC64:
1843 *type = calloc(1, sizeof(struct lysc_type_dec));
1844 break;
1845 case LY_TYPE_ENUM:
1846 *type = calloc(1, sizeof(struct lysc_type_enum));
1847 break;
1848 case LY_TYPE_IDENT:
1849 *type = calloc(1, sizeof(struct lysc_type_identityref));
1850 break;
1851 case LY_TYPE_INST:
1852 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1853 break;
1854 case LY_TYPE_LEAFREF:
1855 *type = calloc(1, sizeof(struct lysc_type_leafref));
1856 break;
1857 case LY_TYPE_STRING:
1858 *type = calloc(1, sizeof(struct lysc_type_str));
1859 break;
1860 case LY_TYPE_UNION:
1861 *type = calloc(1, sizeof(struct lysc_type_union));
1862 break;
1863 case LY_TYPE_INT8:
1864 case LY_TYPE_UINT8:
1865 case LY_TYPE_INT16:
1866 case LY_TYPE_UINT16:
1867 case LY_TYPE_INT32:
1868 case LY_TYPE_UINT32:
1869 case LY_TYPE_INT64:
1870 case LY_TYPE_UINT64:
1871 *type = calloc(1, sizeof(struct lysc_type_num));
1872 break;
1873 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001874 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001875 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1876 ret = LY_EVALID;
1877 goto cleanup;
1878 }
1879 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1880 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001881 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001882 ly_data_type2str[basetype]);
1883 free(*type);
1884 (*type) = NULL;
1885 ret = LY_EVALID;
1886 goto cleanup;
1887 }
1888
1889 /* get restrictions from the referred typedefs */
1890 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1891 tctx = (struct type_context *)tpdf_chain.objs[u];
1892
1893 /* remember the typedef context for circular check */
1894 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1895 LY_CHECK_GOTO(ret, cleanup);
1896
1897 if (tctx->tpdf->type.compiled) {
1898 base = tctx->tpdf->type.compiled;
1899 continue;
1900 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1901 /* no change, just use the type information from the base */
1902 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1903 ++base->refcount;
1904 continue;
1905 }
1906
1907 ++(*type)->refcount;
1908 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001909 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001910 tctx->tpdf->name, ly_data_type2str[basetype]);
1911 ret = LY_EVALID;
1912 goto cleanup;
1913 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001914 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001915 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1916 tctx->tpdf->name, tctx->tpdf->dflt.str);
1917 ret = LY_EVALID;
1918 goto cleanup;
1919 }
1920
1921 (*type)->basetype = basetype;
1922 /* TODO user type plugins */
1923 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1924 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001925 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001926 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1927 LY_CHECK_GOTO(ret, cleanup);
1928 base = prev_type;
1929 }
1930 /* remove the processed typedef contexts from the stack for circular check */
1931 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1932
1933 /* process the type definition in leaf */
1934 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1935 /* get restrictions from the node itself */
1936 (*type)->basetype = basetype;
1937 /* TODO user type plugins */
1938 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1939 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001940 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001941 LY_CHECK_GOTO(ret, cleanup);
1942 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1943 /* no specific restriction in leaf's type definition, copy from the base */
1944 free(*type);
1945 (*type) = base;
1946 ++(*type)->refcount;
1947 }
1948
Radek Krejciab430862021-03-02 20:13:40 +01001949 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001950
1951cleanup:
1952 ly_set_erase(&tpdf_chain, free);
1953 return ret;
1954}
1955
1956/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01001957 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
1958 */
1959#define LYS_STATUS_USES LYS_CONFIG_MASK
1960
1961/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001962 * @brief Compile status information of the given node.
1963 *
1964 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
1965 * has the status correctly set during the compilation.
1966 *
1967 * @param[in] ctx Compile context
1968 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
1969 * If the status was set explicitly on the node, it is already set in the flags value and we just check
1970 * the compatibility with the parent's status value.
1971 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
1972 * @return LY_ERR value.
1973 */
1974static LY_ERR
1975lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
1976{
1977 /* status - it is not inherited by specification, but it does not make sense to have
1978 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
1979 if (!((*node_flags) & LYS_STATUS_MASK)) {
1980 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001981 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001982 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01001983 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001984 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
1985 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1986 }
1987 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
1988 } else {
1989 (*node_flags) |= LYS_STATUS_CURR;
1990 }
1991 } else if (parent_flags & LYS_STATUS_MASK) {
1992 /* check status compatibility with the parent */
1993 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
1994 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001995 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001996 "A \"current\" status is in conflict with the parent's \"%s\" status.",
1997 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1998 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001999 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002000 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2001 }
2002 return LY_EVALID;
2003 }
2004 }
2005 return LY_SUCCESS;
2006}
2007
2008/**
2009 * @brief Check uniqness of the node/action/notification name.
2010 *
2011 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2012 * structures, but they share the namespace so we need to check their name collisions.
2013 *
2014 * @param[in] ctx Compile context.
2015 * @param[in] parent Parent of the nodes to check, can be NULL.
2016 * @param[in] name Name of the item to find in the given lists.
2017 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2018 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2019 */
2020static LY_ERR
2021lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2022 const struct lysc_node *exclude)
2023{
2024 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002025 const struct lysc_node_action *actions;
2026 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002027 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002028 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002029
2030#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2031
2032 if (exclude->nodetype == LYS_CASE) {
2033 /* check restricted only to all the cases */
2034 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002035 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002036 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002037 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002038 return LY_EEXIST;
2039 }
2040 }
2041
2042 return LY_SUCCESS;
2043 }
2044
2045 /* no reason for our parent to be choice anymore */
2046 assert(!parent || (parent->nodetype != LYS_CHOICE));
2047
2048 if (parent && (parent->nodetype == LYS_CASE)) {
2049 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002050
2051 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2052 iter = lysc_data_parent(parent);
2053 do {
2054 parent = parent->parent;
2055 if (parent && (parent->nodetype == LYS_CHOICE)) {
2056 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2057 }
2058 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002059 }
2060
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002061 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002062 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2063 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2064 if (exclude->flags & LYS_IS_OUTPUT) {
2065 getnext_flags |= LYS_GETNEXT_OUTPUT;
2066 parent = lysc_node_child(parent)->next;
2067 } else {
2068 parent = lysc_node_child(parent);
2069 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002070 }
2071
2072 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002073 if (!parent && ctx->ext) {
2074 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2075 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2076 goto error;
2077 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002078
Radek Krejci5fa32a32021-02-08 18:12:38 +01002079 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2080 if (iter->nodetype == LYS_CHOICE) {
2081 iter2 = NULL;
2082 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2083 if (CHECK_NODE(iter2, exclude, name)) {
2084 goto error;
2085 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002086 }
2087 }
2088 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002089 } else {
2090 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2091 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2092 goto error;
2093 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002094
Radek Krejci5fa32a32021-02-08 18:12:38 +01002095 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2096 if (iter->nodetype == LYS_CHOICE) {
2097 iter2 = NULL;
2098 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2099 if (CHECK_NODE(iter2, exclude, name)) {
2100 goto error;
2101 }
2102 }
2103 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002104 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002105
Radek Krejci5fa32a32021-02-08 18:12:38 +01002106 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2107 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2108 if (CHECK_NODE(iter, exclude, name)) {
2109 goto error;
2110 }
2111 }
2112
2113 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2114 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2115 if (CHECK_NODE(iter, exclude, name)) {
2116 goto error;
2117 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002118 }
2119 }
Radek Krejci078e4342021-02-12 18:17:26 +01002120 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002121 return LY_SUCCESS;
2122
2123error:
Radek Krejci078e4342021-02-12 18:17:26 +01002124 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002125 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002126 return LY_EEXIST;
2127
2128#undef CHECK_NODE
2129}
2130
Radek Krejci2a9fc652021-01-22 17:44:34 +01002131/**
2132 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2133 * keep specific order of augments targetting the same node.
2134 *
2135 * @param[in] ctx Compile context
2136 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2137 * the choice itself is expected instead of a specific case node.
2138 * @param[in] node Schema node to connect into the list.
2139 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2140 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2141 */
2142static LY_ERR
2143lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002144{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002145 struct lysc_node **children, *anchor = NULL;
2146 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002147
Radek Krejci2a9fc652021-01-22 17:44:34 +01002148 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002149
Radek Krejci2a9fc652021-01-22 17:44:34 +01002150 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002151 if (node->nodetype == LYS_INPUT) {
2152 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2153 /* input node is part of the action but link it with output */
2154 node->next = &((struct lysc_node_action *)parent)->output.node;
2155 node->prev = node->next;
2156 return LY_SUCCESS;
2157 } else if (node->nodetype == LYS_OUTPUT) {
2158 /* output node is part of the action but link it with input */
2159 node->next = NULL;
2160 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002161 return LY_SUCCESS;
2162 } else if (node->nodetype == LYS_ACTION) {
2163 children = (struct lysc_node **)lysc_node_actions_p(parent);
2164 } else if (node->nodetype == LYS_NOTIF) {
2165 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2166 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002167 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002168 }
2169 assert(children);
2170
2171 if (!(*children)) {
2172 /* first child */
2173 *children = node;
2174 } else if (node->flags & LYS_KEY) {
2175 /* special handling of adding keys */
2176 assert(node->module == parent->module);
2177 anchor = *children;
2178 if (anchor->flags & LYS_KEY) {
2179 while ((anchor->flags & LYS_KEY) && anchor->next) {
2180 anchor = anchor->next;
2181 }
2182 /* insert after the last key */
2183 insert_after = 1;
2184 } /* else insert before anchor (at the beginning) */
2185 } else if ((*children)->prev->module == node->module) {
2186 /* last child is from the same module, keep the order and insert at the end */
2187 anchor = (*children)->prev;
2188 insert_after = 1;
2189 } else if (parent->module == node->module) {
2190 /* adding module child after some augments were connected */
2191 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2192 } else {
2193 /* some augments are already connected and we are connecting new ones,
2194 * keep module name order and insert the node into the children list */
2195 anchor = *children;
2196 do {
2197 anchor = anchor->prev;
2198
2199 /* check that we have not found the last augment node from our module or
2200 * the first augment node from a "smaller" module or
2201 * the first node from a local module */
2202 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2203 (anchor->module == parent->module)) {
2204 /* insert after */
2205 insert_after = 1;
2206 break;
2207 }
2208
2209 /* we have traversed all the nodes, insert before anchor (as the first node) */
2210 } while (anchor->prev->next);
2211 }
2212
2213 /* insert */
2214 if (anchor) {
2215 if (insert_after) {
2216 node->next = anchor->next;
2217 node->prev = anchor;
2218 anchor->next = node;
2219 if (node->next) {
2220 /* middle node */
2221 node->next->prev = node;
2222 } else {
2223 /* last node */
2224 (*children)->prev = node;
2225 }
2226 } else {
2227 node->next = anchor;
2228 node->prev = anchor->prev;
2229 anchor->prev = node;
2230 if (anchor == *children) {
2231 /* first node */
2232 *children = node;
2233 } else {
2234 /* middle node */
2235 node->prev->next = node;
2236 }
2237 }
2238 }
2239
2240 /* check the name uniqueness (even for an only child, it may be in case) */
2241 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2242 return LY_EEXIST;
2243 }
2244 } else {
2245 /* top-level element */
2246 struct lysc_node **list;
2247
Radek Krejci6b88a462021-02-17 12:39:34 +01002248 if (ctx->ext) {
2249 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2250 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002251 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2252 } else if (node->nodetype == LYS_NOTIF) {
2253 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2254 } else {
2255 list = &ctx->cur_mod->compiled->data;
2256 }
2257 if (!(*list)) {
2258 *list = node;
2259 } else {
2260 /* insert at the end of the module's top-level nodes list */
2261 (*list)->prev->next = node;
2262 node->prev = (*list)->prev;
2263 (*list)->prev = node;
2264 }
2265
2266 /* check the name uniqueness on top-level */
2267 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2268 return LY_EEXIST;
2269 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002270 }
2271
Radek Krejci2a9fc652021-01-22 17:44:34 +01002272 return LY_SUCCESS;
2273}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002274
Radek Krejci2a9fc652021-01-22 17:44:34 +01002275/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002276 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002277 *
2278 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002279 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002280 * @return LY_ERR value.
2281 */
2282static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002283lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002284{
2285 /* case never has any explicit config */
2286 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002287
Radek Krejci91531e12021-02-08 19:57:54 +01002288 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
2289 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002290 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002291 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2292 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002293 if (node->parent) {
2294 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002295 } else {
2296 /* default is config true */
2297 node->flags |= LYS_CONFIG_W;
2298 }
2299 } else {
2300 /* config set explicitly */
2301 node->flags |= LYS_SET_CONFIG;
2302 }
2303
Radek Krejci91531e12021-02-08 19:57:54 +01002304 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002305 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002306 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002307 }
2308
Radek Krejci2a9fc652021-01-22 17:44:34 +01002309 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002310}
2311
Radek Krejci91531e12021-02-08 19:57:54 +01002312/**
2313 * @brief Set various flags of the compiled nodes
2314 *
2315 * @param[in] ctx Compile context.
2316 * @param[in] node Compiled node where the flags will be set.
2317 * @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).
2318 * Zero means no uses, non-zero value with no status bit set mean the default status.
2319 */
2320static LY_ERR
2321lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2322{
2323 /* inherit config flags */
2324 LY_CHECK_RET(lys_compile_config(ctx, node));
2325
2326 /* status - it is not inherited by specification, but it does not make sense to have
2327 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2328 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (node->parent ? node->parent->flags : 0)));
2329
2330 /* other flags */
2331 if ((ctx->options & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
2332 node->flags |= LYS_IS_INPUT;
2333 } else if ((ctx->options & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
2334 node->flags |= LYS_IS_OUTPUT;
2335 } else if ((ctx->options & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
2336 node->flags |= LYS_IS_NOTIF;
2337 }
2338
2339 return LY_SUCCESS;
2340}
2341
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002342LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002343lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2344 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2345 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002346{
2347 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002348 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002349 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002350 struct lysp_when *pwhen = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002351
Radek Krejci2a9fc652021-01-22 17:44:34 +01002352 node->nodetype = pnode->nodetype;
2353 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002354 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002355 node->prev = node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002356
Radek Krejci2a9fc652021-01-22 17:44:34 +01002357 /* compile any deviations for this node */
2358 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002359 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002360 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002361 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002362 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002363 }
2364
Radek Krejci2a9fc652021-01-22 17:44:34 +01002365 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002366
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002367 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002368 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Radek Krejciaf0548b2021-02-15 15:11:22 +01002369 if (!enabled && !(ctx->options & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002370 ly_set_add(&ctx->disabled, node, 1, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002371 ctx->options |= LYS_COMPILE_DISABLED;
2372 }
2373
Radek Krejci91531e12021-02-08 19:57:54 +01002374 /* config, status and other flags */
2375 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002376 LY_CHECK_GOTO(ret, error);
2377
2378 /* list ordering */
2379 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002380 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002381 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002382 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
2383 (ctx->options & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002384 node->flags &= ~LYS_ORDBY_MASK;
2385 node->flags |= LYS_ORDBY_SYSTEM;
2386 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2387 /* default ordering is system */
2388 node->flags |= LYS_ORDBY_SYSTEM;
2389 }
2390 }
2391
Radek Krejci2a9fc652021-01-22 17:44:34 +01002392 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2393 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2394 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2395
2396 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2397 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2398
Radek Krejci9a3823e2021-01-27 20:26:46 +01002399 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002400 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002401 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002402 LY_CHECK_GOTO(ret, cleanup);
2403 }
2404
2405 /* connect any augments */
2406 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2407
2408 /* nodetype-specific part */
2409 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2410
2411 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002412 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002413 if (node->flags & LYS_MAND_TRUE) {
2414 /* inherit LYS_MAND_TRUE in parent containers */
2415 lys_compile_mandatory_parents(parent, 1);
2416 }
2417
2418 if (child_set) {
2419 /* add the new node into set */
2420 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2421 }
2422
2423 goto cleanup;
2424
2425error:
2426 lysc_node_free(ctx->ctx, node, 0);
2427cleanup:
2428 if (ret && dev_pnode) {
2429 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2430 }
2431 lysp_dev_node_free(ctx->ctx, dev_pnode);
2432 return ret;
2433}
2434
2435/**
2436 * @brief Compile parsed action's input/output node information.
2437 * @param[in] ctx Compile context
2438 * @param[in] pnode Parsed inout node.
2439 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2440 * is enriched with the inout-specific information.
2441 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2442 */
2443LY_ERR
2444lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2445{
2446 LY_ERR ret = LY_SUCCESS;
2447 struct lysp_node *child_p;
2448 uint32_t prev_options = ctx->options;
2449
2450 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2451 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2452
2453 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002454 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Radek Krejci91531e12021-02-08 19:57:54 +01002455 ctx->options |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002456
Radek Krejci01180ac2021-01-27 08:48:22 +01002457 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002458 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2459 }
2460
2461 ctx->options = prev_options;
2462
2463done:
2464 return ret;
2465}
2466
2467/**
2468 * @brief Compile parsed action node information.
2469 * @param[in] ctx Compile context
2470 * @param[in] pnode Parsed action node.
2471 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2472 * is enriched with the action-specific information.
2473 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2474 */
2475LY_ERR
2476lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2477{
2478 LY_ERR ret;
2479 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2480 struct lysc_node_action *action = (struct lysc_node_action *)node;
2481 struct lysp_node_action_inout *input, implicit_input = {
2482 .nodetype = LYS_INPUT,
2483 .name = "input",
2484 .parent = pnode,
2485 };
2486 struct lysp_node_action_inout *output, implicit_output = {
2487 .nodetype = LYS_OUTPUT,
2488 .name = "output",
2489 .parent = pnode,
2490 };
2491
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002492 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002493 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002494 if (action_p->input.nodetype == LYS_UNKNOWN) {
2495 input = &implicit_input;
2496 } else {
2497 input = &action_p->input;
2498 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002499 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 +01002500 lysc_update_path(ctx, NULL, NULL);
2501 LY_CHECK_GOTO(ret, done);
2502
2503 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002504 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002505 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002506 output = &implicit_output;
2507 } else {
2508 output = &action_p->output;
2509 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002510 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 +01002511 lysc_update_path(ctx, NULL, NULL);
2512 LY_CHECK_GOTO(ret, done);
2513
2514 /* do not check "must" semantics in a grouping */
2515 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2516 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2517 LY_CHECK_GOTO(ret, done);
2518 }
2519
2520done:
2521 return ret;
2522}
2523
2524/**
2525 * @brief Compile parsed action node information.
2526 * @param[in] ctx Compile context
2527 * @param[in] pnode Parsed action node.
2528 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2529 * is enriched with the action-specific information.
2530 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2531 */
2532LY_ERR
2533lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2534{
2535 LY_ERR ret = LY_SUCCESS;
2536 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2537 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2538 struct lysp_node *child_p;
2539
2540 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002541 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002542 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002543 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002544 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002545 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002546
Radek Krejci01180ac2021-01-27 08:48:22 +01002547 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002548 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002549 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002550 }
2551
Radek Krejci2a9fc652021-01-22 17:44:34 +01002552done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002553 return ret;
2554}
2555
2556/**
2557 * @brief Compile parsed container node information.
2558 * @param[in] ctx Compile context
2559 * @param[in] pnode Parsed container node.
2560 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2561 * is enriched with the container-specific information.
2562 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2563 */
2564static LY_ERR
2565lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2566{
2567 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2568 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2569 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002570 LY_ERR ret = LY_SUCCESS;
2571
2572 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002573 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002574 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002575 }
2576
2577 /* more cases when the container has meaning but is kept NP for convenience:
2578 * - when condition
2579 * - direct child action/notification
2580 */
2581
2582 LY_LIST_FOR(cont_p->child, child_p) {
2583 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2584 LY_CHECK_GOTO(ret, done);
2585 }
2586
Michal Vasko5347e3a2020-11-03 17:14:57 +01002587 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002588 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002589 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002590 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002591 LY_CHECK_GOTO(ret, done);
2592 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002593
2594 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2595 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2596 LY_CHECK_GOTO(ret, done);
2597 }
2598 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2599 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2600 LY_CHECK_GOTO(ret, done);
2601 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002602
2603done:
2604 return ret;
2605}
2606
Michal Vasko72244882021-01-12 15:21:05 +01002607/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002608 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2609 * @param[in] ctx Compile context.
2610 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2611 * @param[in] type_p Parsed type to compile.
2612 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2613 * @return LY_ERR value.
2614 */
2615static LY_ERR
2616lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2617 struct lysc_node_leaf *leaf)
2618{
2619 struct lysp_qname *dflt;
2620
Michal Vaskoa99b3572021-02-01 11:54:58 +01002621 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002622 leaf->units ? NULL : &leaf->units, &dflt));
2623
2624 /* store default value, if any */
2625 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2626 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2627 }
2628
Michal Vasko12606ee2020-11-25 17:05:11 +01002629 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002630 /* 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 +01002631 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002632 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002633 LY_ARRAY_COUNT_TYPE u;
2634 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2635 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2636 /* 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 +01002637 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002638 }
2639 }
2640 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2641 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002642 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 +02002643 return LY_EVALID;
2644 }
2645 }
2646
2647 return LY_SUCCESS;
2648}
2649
2650/**
2651 * @brief Compile parsed leaf node information.
2652 * @param[in] ctx Compile context
2653 * @param[in] pnode Parsed leaf node.
2654 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2655 * is enriched with the leaf-specific information.
2656 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2657 */
2658static LY_ERR
2659lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2660{
2661 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2662 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002663 LY_ERR ret = LY_SUCCESS;
2664
Michal Vasko5347e3a2020-11-03 17:14:57 +01002665 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002666 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002667 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002668 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002669 LY_CHECK_GOTO(ret, done);
2670 }
2671 if (leaf_p->units) {
2672 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2673 leaf->flags |= LYS_SET_UNITS;
2674 }
2675
2676 /* compile type */
2677 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2678 LY_CHECK_GOTO(ret, done);
2679
2680 /* store/update default value */
2681 if (leaf_p->dflt.str) {
2682 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2683 leaf->flags |= LYS_SET_DFLT;
2684 }
2685
2686 /* checks */
2687 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002688 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002689 "Invalid mandatory leaf with a default value.");
2690 return LY_EVALID;
2691 }
2692
2693done:
2694 return ret;
2695}
2696
2697/**
2698 * @brief Compile parsed leaf-list node information.
2699 * @param[in] ctx Compile context
2700 * @param[in] pnode Parsed leaf-list node.
2701 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2702 * is enriched with the leaf-list-specific information.
2703 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2704 */
2705static LY_ERR
2706lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2707{
2708 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2709 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002710 LY_ERR ret = LY_SUCCESS;
2711
Michal Vasko5347e3a2020-11-03 17:14:57 +01002712 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002713 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002714 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002715 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002716 LY_CHECK_GOTO(ret, done);
2717 }
2718 if (llist_p->units) {
2719 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2720 llist->flags |= LYS_SET_UNITS;
2721 }
2722
2723 /* compile type */
2724 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2725 LY_CHECK_GOTO(ret, done);
2726
2727 /* store/update default values */
2728 if (llist_p->dflts) {
2729 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002730 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002731 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2732 return LY_EVALID;
2733 }
2734
2735 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2736 llist->flags |= LYS_SET_DFLT;
2737 }
2738
2739 llist->min = llist_p->min;
2740 if (llist->min) {
2741 llist->flags |= LYS_MAND_TRUE;
2742 }
2743 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2744
2745 /* checks */
2746 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002747 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 +02002748 return LY_EVALID;
2749 }
2750
2751 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002752 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002753 llist->min, llist->max);
2754 return LY_EVALID;
2755 }
2756
2757done:
2758 return ret;
2759}
2760
2761LY_ERR
2762lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2763 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2764 const struct lysc_node **target, uint16_t *result_flag)
2765{
2766 LY_ERR ret = LY_EVALID;
2767 const char *name, *prefix, *id;
2768 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002769 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002770 const char *nodeid_type;
2771 uint32_t getnext_extra_flag = 0;
2772 uint16_t current_nodetype = 0;
2773
2774 assert(nodeid);
2775 assert(target);
2776 assert(result_flag);
2777 *target = NULL;
2778 *result_flag = 0;
2779
2780 id = nodeid;
2781
2782 if (ctx_node) {
2783 /* descendant-schema-nodeid */
2784 nodeid_type = "descendant";
2785
2786 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002787 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002788 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002789 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002790 return LY_EVALID;
2791 }
2792 } else {
2793 /* absolute-schema-nodeid */
2794 nodeid_type = "absolute";
2795
2796 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002797 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002798 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002799 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002800 return LY_EVALID;
2801 }
2802 ++id;
2803 }
2804
2805 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2806 if (prefix) {
2807 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2808 if (!mod) {
2809 /* module must always be found */
2810 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002811 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002812 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002813 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002814 return LY_ENOTFOUND;
2815 }
2816 } else {
2817 switch (format) {
2818 case LY_PREF_SCHEMA:
2819 case LY_PREF_SCHEMA_RESOLVED:
2820 /* use the current module */
2821 mod = cur_mod;
2822 break;
2823 case LY_PREF_JSON:
2824 if (!ctx_node) {
2825 LOGINT_RET(ctx->ctx);
2826 }
2827 /* inherit the module of the previous context node */
2828 mod = ctx_node->module;
2829 break;
2830 case LY_PREF_XML:
2831 /* not really defined */
2832 LOGINT_RET(ctx->ctx);
2833 }
2834 }
2835
2836 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2837 /* move through input/output manually */
2838 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002839 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2840 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002841 return LY_ENOTFOUND;
2842 }
2843 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002844 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002845 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002846 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002847 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2848 } else {
2849 /* only input or output is valid */
2850 ctx_node = NULL;
2851 }
2852 } else {
2853 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002854 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002855 getnext_extra_flag = 0;
2856 }
2857 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002858 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2859 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002860 return LY_ENOTFOUND;
2861 }
2862 current_nodetype = ctx_node->nodetype;
2863
2864 if (current_nodetype == LYS_NOTIF) {
2865 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2866 } else if (current_nodetype == LYS_INPUT) {
2867 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2868 } else if (current_nodetype == LYS_OUTPUT) {
2869 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2870 }
2871
2872 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2873 break;
2874 }
2875 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002876 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002877 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002878 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002879 return LY_EVALID;
2880 }
2881 ++id;
2882 }
2883
2884 if (ret == LY_SUCCESS) {
2885 *target = ctx_node;
2886 if (nodetype && !(current_nodetype & nodetype)) {
2887 return LY_EDENIED;
2888 }
2889 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002890 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002891 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002892 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002893 }
2894
2895 return ret;
2896}
2897
2898/**
2899 * @brief Compile information about list's uniques.
2900 * @param[in] ctx Compile context.
2901 * @param[in] uniques Sized array list of unique statements.
2902 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2903 * @return LY_ERR value.
2904 */
2905static LY_ERR
2906lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2907{
2908 LY_ERR ret = LY_SUCCESS;
2909 struct lysc_node_leaf **key, ***unique;
2910 struct lysc_node *parent;
2911 const char *keystr, *delim;
2912 size_t len;
2913 LY_ARRAY_COUNT_TYPE v;
2914 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2915 uint16_t flags;
2916
2917 LY_ARRAY_FOR(uniques, v) {
2918 config = -1;
2919 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2920 keystr = uniques[v].str;
2921 while (keystr) {
2922 delim = strpbrk(keystr, " \t\n");
2923 if (delim) {
2924 len = delim - keystr;
2925 while (isspace(*delim)) {
2926 ++delim;
2927 }
2928 } else {
2929 len = strlen(keystr);
2930 }
2931
2932 /* unique node must be present */
2933 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002934 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002935 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2936 if (ret != LY_SUCCESS) {
2937 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002938 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002939 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002940 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002941 }
2942 return LY_EVALID;
2943 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002944 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002945 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01002946 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002947 return LY_EVALID;
2948 }
2949
2950 /* all referenced leafs must be of the same config type */
2951 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2952 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002953 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002954 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2955 return LY_EVALID;
2956 } else if ((*key)->flags & LYS_CONFIG_W) {
2957 config = 1;
2958 } else { /* LYS_CONFIG_R */
2959 config = 0;
2960 }
2961
2962 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
2963 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
2964 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002965 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002966 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
2967 return LY_EVALID;
2968 }
2969 }
2970
2971 /* check status */
2972 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2973 (*key)->flags, (*key)->module, (*key)->name));
2974
2975 /* mark leaf as unique */
2976 (*key)->flags |= LYS_UNIQUE;
2977
2978 /* next unique value in line */
2979 keystr = delim;
2980 }
2981 /* next unique definition */
2982 }
2983
2984 return LY_SUCCESS;
2985}
2986
2987/**
2988 * @brief Compile parsed list node information.
2989 * @param[in] ctx Compile context
2990 * @param[in] pnode Parsed list node.
2991 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2992 * is enriched with the list-specific information.
2993 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2994 */
2995static LY_ERR
2996lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2997{
2998 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
2999 struct lysc_node_list *list = (struct lysc_node_list *)node;
3000 struct lysp_node *child_p;
3001 struct lysc_node_leaf *key, *prev_key = NULL;
3002 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003003 const char *keystr, *delim;
3004 LY_ERR ret = LY_SUCCESS;
3005
3006 list->min = list_p->min;
3007 if (list->min) {
3008 list->flags |= LYS_MAND_TRUE;
3009 }
3010 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3011
3012 LY_LIST_FOR(list_p->child, child_p) {
3013 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3014 }
3015
Michal Vasko5347e3a2020-11-03 17:14:57 +01003016 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003017 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003018 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003019 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003020 }
3021
3022 /* keys */
3023 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003024 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003025 return LY_EVALID;
3026 }
3027
3028 /* find all the keys (must be direct children) */
3029 keystr = list_p->key;
3030 if (!keystr) {
3031 /* keyless list */
3032 list->flags |= LYS_KEYLESS;
3033 }
3034 while (keystr) {
3035 delim = strpbrk(keystr, " \t\n");
3036 if (delim) {
3037 len = delim - keystr;
3038 while (isspace(*delim)) {
3039 ++delim;
3040 }
3041 } else {
3042 len = strlen(keystr);
3043 }
3044
3045 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003046 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3047 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003048 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003049 return LY_EVALID;
3050 }
3051 /* keys must be unique */
3052 if (key->flags & LYS_KEY) {
3053 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003054 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003055 return LY_EVALID;
3056 }
3057
Radek Krejcia6016992021-03-03 10:13:41 +01003058 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003059 /* key must have the same config flag as the list itself */
3060 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003061 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003062 return LY_EVALID;
3063 }
3064 if (ctx->pmod->version < LYS_VERSION_1_1) {
3065 /* YANG 1.0 denies key to be of empty type */
3066 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003067 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003068 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3069 return LY_EVALID;
3070 }
3071 } else {
3072 /* when and if-feature are illegal on list keys */
3073 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003074 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003075 "List's key must not have any \"when\" statement.");
3076 return LY_EVALID;
3077 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003078 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003079 }
3080
3081 /* check status */
3082 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3083 key->flags, key->module, key->name));
3084
3085 /* ignore default values of the key */
3086 if (key->dflt) {
3087 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3088 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3089 free(key->dflt);
3090 key->dflt = NULL;
3091 }
3092 /* mark leaf as key */
3093 key->flags |= LYS_KEY;
3094
3095 /* move it to the correct position */
3096 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3097 /* fix links in closest previous siblings of the key */
3098 if (key->next) {
3099 key->next->prev = key->prev;
3100 } else {
3101 /* last child */
3102 list->child->prev = key->prev;
3103 }
3104 if (key->prev->next) {
3105 key->prev->next = key->next;
3106 }
3107 /* fix links in the key */
3108 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003109 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003110 key->next = prev_key->next;
3111 } else {
3112 key->prev = list->child->prev;
3113 key->next = list->child;
3114 }
3115 /* fix links in closes future siblings of the key */
3116 if (prev_key) {
3117 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003118 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003119 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003120 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003121 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003122 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003123 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003124 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003125 }
3126 /* fix links in parent */
3127 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003128 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003129 }
3130 }
3131
3132 /* next key value */
3133 prev_key = key;
3134 keystr = delim;
3135 lysc_update_path(ctx, NULL, NULL);
3136 }
3137
3138 /* uniques */
3139 if (list_p->uniques) {
3140 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3141 }
3142
Radek Krejci2a9fc652021-01-22 17:44:34 +01003143 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3144 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3145 LY_CHECK_GOTO(ret, done);
3146 }
3147 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3148 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3149 LY_CHECK_GOTO(ret, done);
3150 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003151
3152 /* checks */
3153 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003154 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003155 list->min, list->max);
3156 return LY_EVALID;
3157 }
3158
3159done:
3160 return ret;
3161}
3162
3163/**
3164 * @brief Do some checks and set the default choice's case.
3165 *
3166 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3167 *
3168 * @param[in] ctx Compile context.
3169 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3170 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3171 * @return LY_ERR value.
3172 */
3173static LY_ERR
3174lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3175{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003176 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003177 const struct lys_module *mod;
3178 const char *prefix = NULL, *name;
3179 size_t prefix_len = 0;
3180
3181 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3182 name = strchr(dflt->str, ':');
3183 if (name) {
3184 prefix = dflt->str;
3185 prefix_len = name - prefix;
3186 ++name;
3187 } else {
3188 name = dflt->str;
3189 }
3190 if (prefix) {
3191 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
3192 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003193 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003194 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003195 return LY_EVALID;
3196 }
3197 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003198 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003199 }
3200
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003201 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 +02003202 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003203 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003204 "Default case \"%s\" not found.", dflt->str);
3205 return LY_EVALID;
3206 }
3207
3208 /* no mandatory nodes directly under the default case */
3209 LY_LIST_FOR(ch->dflt->child, iter) {
3210 if (iter->parent != (struct lysc_node *)ch->dflt) {
3211 break;
3212 }
3213 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003214 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003215 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3216 return LY_EVALID;
3217 }
3218 }
3219
3220 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003221 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003222 return LY_EVALID;
3223 }
3224
3225 ch->dflt->flags |= LYS_SET_DFLT;
3226 return LY_SUCCESS;
3227}
3228
3229LY_ERR
3230lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3231 struct ly_set *child_set)
3232{
3233 LY_ERR ret = LY_SUCCESS;
3234 struct lysp_node *child_p_next = child_p->next;
3235 struct lysp_node_case *cs_p;
3236
3237 if (child_p->nodetype == LYS_CASE) {
3238 /* standard case under choice */
3239 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3240 } else {
3241 /* we need the implicit case first, so create a fake parsed case */
3242 cs_p = calloc(1, sizeof *cs_p);
3243 cs_p->nodetype = LYS_CASE;
3244 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3245 cs_p->child = child_p;
3246
3247 /* make the child the only case child */
3248 child_p->next = NULL;
3249
3250 /* compile it normally */
3251 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3252
3253free_fake_node:
3254 /* free the fake parsed node and correct pointers back */
3255 cs_p->child = NULL;
3256 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3257 child_p->next = child_p_next;
3258 }
3259
3260 return ret;
3261}
3262
3263/**
3264 * @brief Compile parsed choice node information.
3265 *
3266 * @param[in] ctx Compile context
3267 * @param[in] pnode Parsed choice node.
3268 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3269 * is enriched with the choice-specific information.
3270 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3271 */
3272static LY_ERR
3273lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3274{
3275 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3276 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3277 struct lysp_node *child_p;
3278 LY_ERR ret = LY_SUCCESS;
3279
3280 assert(node->nodetype == LYS_CHOICE);
3281
3282 LY_LIST_FOR(ch_p->child, child_p) {
3283 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3284 }
3285
3286 /* default branch */
3287 if (ch_p->dflt.str) {
3288 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3289 }
3290
3291 return ret;
3292}
3293
3294/**
3295 * @brief Compile parsed anydata or anyxml node information.
3296 * @param[in] ctx Compile context
3297 * @param[in] pnode Parsed anydata or anyxml node.
3298 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3299 * is enriched with the any-specific information.
3300 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3301 */
3302static LY_ERR
3303lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3304{
3305 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3306 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003307 LY_ERR ret = LY_SUCCESS;
3308
Michal Vasko5347e3a2020-11-03 17:14:57 +01003309 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003310 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003311 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003312 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003313 LY_CHECK_GOTO(ret, done);
3314 }
3315
Radek Krejci91531e12021-02-08 19:57:54 +01003316 if (any->flags & LYS_CONFIG_W) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003317 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3318 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3319 }
3320done:
3321 return ret;
3322}
3323
3324/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003325 * @brief Prepare the case structure in choice node for the new data node.
3326 *
3327 * 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
3328 * created in the choice when the first child was processed.
3329 *
3330 * @param[in] ctx Compile context.
3331 * @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,
3332 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3333 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3334 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3335 * @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,
3336 * it is linked from the case structure only in case it is its first child.
3337 */
3338static LY_ERR
3339lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3340{
3341 struct lysp_node *child_p;
3342 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3343
3344 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3345 /* we have to add an implicit case node into the parent choice */
3346 } else if (pnode->nodetype == LYS_CASE) {
3347 /* explicit parent case */
3348 LY_LIST_FOR(cs_p->child, child_p) {
3349 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3350 }
3351 } else {
3352 LOGINT_RET(ctx->ctx);
3353 }
3354
3355 return LY_SUCCESS;
3356}
3357
3358void
3359lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3360{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003361 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003362
3363 if (add) { /* set flag */
3364 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3365 parent = parent->parent) {
3366 parent->flags |= LYS_MAND_TRUE;
3367 }
3368 } else { /* unset flag */
3369 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003370 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003371 if (iter->flags & LYS_MAND_TRUE) {
3372 /* there is another mandatory node */
3373 return;
3374 }
3375 }
3376 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3377 parent->flags &= ~LYS_MAND_TRUE;
3378 }
3379 }
3380}
3381
3382/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003383 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003384 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003385 * @param[in] name Name of the grouping to find,
3386 * @return NULL when there is no grouping with the specified name
3387 * @return Pointer to the grouping of the specified @p name.
3388 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003389static struct lysp_node_grp *
3390match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003391{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003392 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003393
Radek Krejci2a9fc652021-01-22 17:44:34 +01003394 LY_LIST_FOR(grps, grp) {
3395 if (!strcmp(grp->name, name)) {
3396 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003397 }
3398 }
3399
3400 return NULL;
3401}
3402
3403/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003404 * @brief Find grouping for a uses.
3405 *
3406 * @param[in] ctx Compile context.
3407 * @param[in] uses_p Parsed uses node.
3408 * @param[out] gpr_p Found grouping on success.
3409 * @param[out] grp_pmod Module of @p grp_p on success.
3410 * @return LY_ERR value.
3411 */
3412static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003413lys_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 +02003414 struct lysp_module **grp_pmod)
3415{
3416 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003417 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003418 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003419 const char *id, *name, *prefix, *local_pref;
3420 size_t prefix_len, name_len;
3421 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003422 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003423
3424 *grp_p = NULL;
3425 *grp_pmod = NULL;
3426
3427 /* search for the grouping definition */
3428 id = uses_p->name;
3429 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3430 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3431 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3432 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003433 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003434 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003435 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3436 found = ctx->pmod;
3437 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003438 }
3439 }
3440 } else {
3441 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003442 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003443 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003444 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003445 "Invalid prefix used for grouping reference.", uses_p->name);
3446 return LY_EVALID;
3447 }
3448 pmod = mod->parsed;
3449 }
3450
3451 if (!found) {
3452 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003453 if ((grp = match_grouping(pmod->groupings, name))) {
3454 found = pmod;
3455 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003456 /* ... and all the submodules */
3457 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003458 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3459 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003460 break;
3461 }
3462 }
3463 }
3464 }
3465 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003466 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003467 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3468 return LY_EVALID;
3469 }
3470
3471 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3472 /* remember that the grouping is instantiated to avoid its standalone validation */
3473 grp->flags |= LYS_USED_GRP;
3474 }
3475
3476 *grp_p = grp;
3477 *grp_pmod = found;
3478 return LY_SUCCESS;
3479}
3480
3481/**
3482 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3483 * If present, also apply uses's modificators.
3484 *
3485 * @param[in] ctx Compile context
3486 * @param[in] uses_p Parsed uses schema node.
3487 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3488 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3489 * the compile context.
3490 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3491 */
3492static LY_ERR
3493lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3494{
3495 struct lysp_node *pnode;
3496 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003497 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003498 uint32_t i, grp_stack_count;
3499 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3500 LY_ERR ret = LY_SUCCESS;
3501 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003502 struct ly_set uses_child_set = {0};
3503
3504 /* find the referenced grouping */
3505 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3506
3507 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3508 grp_stack_count = ctx->groupings.count;
3509 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3510 if (grp_stack_count == ctx->groupings.count) {
3511 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003512 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003513 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3514 return LY_EVALID;
3515 }
3516
3517 /* check status */
3518 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3519 LY_CHECK_GOTO(ret, cleanup);
3520
3521 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3522 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3523 LY_CHECK_GOTO(ret, cleanup);
3524
3525 /* switch context's parsed module being processed */
3526 ctx->pmod = grp_mod;
3527
3528 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003529 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003530 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3531 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 +02003532 LY_CHECK_GOTO(ret, cleanup);
3533 }
3534
3535 if (child_set) {
3536 /* add these children to our compiled child_set as well since uses is a schema-only node */
3537 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3538 }
3539
3540 if (uses_p->when) {
3541 /* pass uses's when to all the data children */
3542 for (i = 0; i < uses_child_set.count; ++i) {
3543 child = uses_child_set.snodes[i];
3544
Michal Vasko72244882021-01-12 15:21:05 +01003545 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 +02003546 LY_CHECK_GOTO(ret, cleanup);
3547 }
3548 }
3549
3550 /* compile actions */
3551 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003552 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003553 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3554 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003555 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003556 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3557 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003558 ret = LY_EVALID;
3559 goto cleanup;
3560 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003561 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3562 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3563 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3564 LY_CHECK_GOTO(ret, cleanup);
3565 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003566
3567 if (uses_p->when) {
3568 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003569 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3570 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 +02003571 LY_CHECK_GOTO(ret, cleanup);
3572 }
3573 }
3574 }
3575
3576 /* compile notifications */
3577 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003578 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003579 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3580 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003581 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003582 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3583 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003584 ret = LY_EVALID;
3585 goto cleanup;
3586 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003587
3588 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3589 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3590 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3591 LY_CHECK_GOTO(ret, cleanup);
3592 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003593
3594 if (uses_p->when) {
3595 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003596 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3597 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 +02003598 LY_CHECK_GOTO(ret, cleanup);
3599 }
3600 }
3601 }
3602
3603 /* check that all augments were applied */
3604 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003605 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3606 /* augment of some parent uses, irrelevant now */
3607 continue;
3608 }
3609
3610 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003611 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3612 ret = LY_ENOTFOUND;
3613 }
3614 LY_CHECK_GOTO(ret, cleanup);
3615
3616 /* check that all refines were applied */
3617 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003618 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3619 /* refine of some paretn uses, irrelevant now */
3620 continue;
3621 }
3622
3623 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003624 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3625 ret = LY_ENOTFOUND;
3626 }
3627 LY_CHECK_GOTO(ret, cleanup);
3628
3629cleanup:
3630 /* reload previous context's parsed module being processed */
3631 ctx->pmod = mod_old;
3632
3633 /* remove the grouping from the stack for circular groupings dependency check */
3634 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3635 assert(ctx->groupings.count == grp_stack_count);
3636
3637 ly_set_erase(&uses_child_set, NULL);
3638 return ret;
3639}
3640
3641static int
3642lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3643{
3644 struct lysp_node *iter;
3645 int len = 0;
3646
3647 *path = NULL;
3648 for (iter = node; iter && len >= 0; iter = iter->parent) {
3649 char *s = *path;
3650 char *id;
3651
3652 switch (iter->nodetype) {
3653 case LYS_USES:
3654 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3655 break;
3656 case LYS_GROUPING:
3657 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3658 break;
3659 case LYS_AUGMENT:
3660 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3661 break;
3662 default:
3663 id = strdup(iter->name);
3664 break;
3665 }
3666
3667 if (!iter->parent) {
3668 /* print prefix */
3669 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3670 } else {
3671 /* prefix is the same as in parent */
3672 len = asprintf(path, "/%s%s", id, s ? s : "");
3673 }
3674 free(s);
3675 free(id);
3676 }
3677
3678 if (len < 0) {
3679 free(*path);
3680 *path = NULL;
3681 } else if (len == 0) {
3682 *path = strdup("/");
3683 len = 1;
3684 }
3685 return len;
3686}
3687
3688LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003689lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003690{
3691 LY_ERR ret;
3692 char *path;
3693 int len;
3694
3695 struct lysp_node_uses fake_uses = {
3696 .parent = pnode,
3697 .nodetype = LYS_USES,
3698 .flags = 0, .next = NULL,
3699 .name = grp->name,
3700 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3701 .refines = NULL, .augments = NULL
3702 };
3703 struct lysc_node_container fake_container = {
3704 .nodetype = LYS_CONTAINER,
3705 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3706 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003707 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003708 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003709 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003710 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003711 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3712 };
3713
3714 if (grp->parent) {
3715 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3716 }
3717
3718 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3719 if (len < 0) {
3720 LOGMEM(ctx->ctx);
3721 return LY_EMEM;
3722 }
3723 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3724 ctx->path_len = (uint32_t)len;
3725 free(path);
3726
3727 lysc_update_path(ctx, NULL, "{grouping}");
3728 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003729 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003730 lysc_update_path(ctx, NULL, NULL);
3731 lysc_update_path(ctx, NULL, NULL);
3732
3733 ctx->path_len = 1;
3734 ctx->path[1] = '\0';
3735
3736 /* cleanup */
3737 lysc_node_container_free(ctx->ctx, &fake_container);
3738
3739 return ret;
3740}
3741
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003742LY_ERR
3743lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3744 struct ly_set *child_set)
3745{
3746 LY_ERR ret = LY_SUCCESS;
3747 struct lysc_node *node = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003748 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003749
3750 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3751
3752 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003753 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003754 } else {
3755 lysc_update_path(ctx, NULL, "{uses}");
3756 lysc_update_path(ctx, NULL, pnode->name);
3757 }
3758
3759 switch (pnode->nodetype) {
3760 case LYS_CONTAINER:
3761 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3762 node_compile_spec = lys_compile_node_container;
3763 break;
3764 case LYS_LEAF:
3765 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3766 node_compile_spec = lys_compile_node_leaf;
3767 break;
3768 case LYS_LIST:
3769 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3770 node_compile_spec = lys_compile_node_list;
3771 break;
3772 case LYS_LEAFLIST:
3773 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3774 node_compile_spec = lys_compile_node_leaflist;
3775 break;
3776 case LYS_CHOICE:
3777 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3778 node_compile_spec = lys_compile_node_choice;
3779 break;
3780 case LYS_CASE:
3781 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3782 node_compile_spec = lys_compile_node_case;
3783 break;
3784 case LYS_ANYXML:
3785 case LYS_ANYDATA:
3786 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3787 node_compile_spec = lys_compile_node_any;
3788 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003789 case LYS_RPC:
3790 case LYS_ACTION:
Radek Krejci91531e12021-02-08 19:57:54 +01003791 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003792 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3793 "Action \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003794 (ctx->options & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003795 return LY_EVALID;
3796 }
3797 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3798 node_compile_spec = lys_compile_node_action;
Radek Krejci91531e12021-02-08 19:57:54 +01003799 ctx->options |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003800 break;
3801 case LYS_NOTIF:
Radek Krejci91531e12021-02-08 19:57:54 +01003802 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003803 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3804 "Notification \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003805 (ctx->options & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003806 return LY_EVALID;
3807 }
3808 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3809 node_compile_spec = lys_compile_node_notif;
3810 ctx->options |= LYS_COMPILE_NOTIFICATION;
3811 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003812 case LYS_USES:
3813 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3814 lysc_update_path(ctx, NULL, NULL);
3815 lysc_update_path(ctx, NULL, NULL);
3816 return ret;
3817 default:
3818 LOGINT(ctx->ctx);
3819 return LY_EINT;
3820 }
3821 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3822
Radek Krejci2a9fc652021-01-22 17:44:34 +01003823 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003824
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003825 ctx->options = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003826 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003827 return ret;
3828}