blob: 52d0d8f5792f3566a71f50ba16138a6cb8570228 [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 Krejci2926c1b2021-03-16 14:45:45 +0100243 LY_CHECK_RET(ly_type_prefix_data_add(ctx->ctx, when_p->cond, strlen(when_p->cond),
244 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 Krejci2926c1b2021-03-16 14:45:45 +0100305 LY_CHECK_RET(ly_type_prefix_data_add(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
306 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;
837 int err_code;
838 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;
936 perl_regex = malloc(size);
937 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
938 perl_regex[0] = '\0';
939
940 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
941 brack = 0;
942 idx = 0;
943 orig_ptr = pattern;
944 while (orig_ptr[0]) {
945 switch (orig_ptr[0]) {
946 case '$':
947 case '^':
948 if (!brack) {
949 /* make space for the extra character */
950 ++size;
951 perl_regex = ly_realloc(perl_regex, size);
952 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
953
954 /* print escape slash */
955 perl_regex[idx] = '\\';
956 ++idx;
957 }
958 break;
959 case '[':
960 /* must not be escaped */
961 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
962 ++brack;
963 }
964 break;
965 case ']':
966 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
967 /* pattern was checked and compiled already */
968 assert(brack);
969 --brack;
970 }
971 break;
972 default:
973 break;
974 }
975
976 /* copy char */
977 perl_regex[idx] = orig_ptr[0];
978
979 ++idx;
980 ++orig_ptr;
981 }
982 perl_regex[idx] = '\0';
983
984 /* substitute Unicode Character Blocks with exact Character Ranges */
985 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
986 start = ptr - perl_regex;
987
988 ptr = strchr(ptr, '}');
989 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100990 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200991 free(perl_regex);
992 return LY_EVALID;
993 }
994 end = (ptr - perl_regex) + 1;
995
996 /* need more space */
997 if (end - start < URANGE_LEN) {
998 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
999 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1000 }
1001
1002 /* find our range */
1003 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001004 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1005 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001006 break;
1007 }
1008 }
1009 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001010 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001011 free(perl_regex);
1012 return LY_EVALID;
1013 }
1014
1015 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1016 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1017 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1018 ++idx;
1019 }
1020 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1021 --idx;
1022 }
1023 }
1024 if (idx) {
1025 /* skip brackets */
1026 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1027 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1028 } else {
1029 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1030 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1031 }
1032 }
1033
1034 /* must return 0, already checked during parsing */
1035 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1036 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1037 &err_code, &err_offset, NULL);
1038 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001039 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1040 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001041 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001042 free(perl_regex);
1043 return LY_EVALID;
1044 }
1045 free(perl_regex);
1046
1047 if (code) {
1048 *code = code_local;
1049 } else {
1050 free(code_local);
1051 }
1052
1053 return LY_SUCCESS;
1054
1055#undef URANGE_LEN
1056}
1057
1058/**
1059 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1060 * @param[in] ctx Compile context.
1061 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1062 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1063 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1064 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1065 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1066 */
1067static LY_ERR
1068lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1069 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1070{
1071 struct lysc_pattern **pattern;
1072 LY_ARRAY_COUNT_TYPE u;
1073 LY_ERR ret = LY_SUCCESS;
1074
1075 /* first, copy the patterns from the base type */
1076 if (base_patterns) {
1077 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1078 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1079 }
1080
1081 LY_ARRAY_FOR(patterns_p, u) {
1082 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1083 *pattern = calloc(1, sizeof **pattern);
1084 ++(*pattern)->refcount;
1085
Radek Krejci2efc45b2020-12-22 16:25:44 +01001086 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001087 LY_CHECK_RET(ret);
1088
Radek Krejcif13b87b2020-12-01 22:02:17 +01001089 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001090 (*pattern)->inverted = 1;
1091 }
1092 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1093 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1094 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1095 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1096 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001097 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001098 }
1099done:
1100 return ret;
1101}
1102
1103/**
1104 * @brief map of the possible restrictions combination for the specific built-in type.
1105 */
1106static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1107 0 /* LY_TYPE_UNKNOWN */,
1108 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1109 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1110 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1111 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1112 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1113 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1114 LYS_SET_BIT /* LY_TYPE_BITS */,
1115 0 /* LY_TYPE_BOOL */,
1116 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1117 0 /* LY_TYPE_EMPTY */,
1118 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1119 LYS_SET_BASE /* LY_TYPE_IDENT */,
1120 LYS_SET_REQINST /* LY_TYPE_INST */,
1121 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1122 LYS_SET_TYPE /* LY_TYPE_UNION */,
1123 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1124 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1125 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1126 LYS_SET_RANGE /* LY_TYPE_INT64 */
1127};
1128
1129/**
1130 * @brief stringification of the YANG built-in data types
1131 */
1132const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
1133 "unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1134 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1135 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
1136};
1137
1138/**
1139 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1140 * @param[in] ctx Compile context.
1141 * @param[in] enums_p Array of the parsed enum structures to compile.
1142 * @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.
1143 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1144 * @param[out] enums Newly created array of the compiled enums information for the current type.
1145 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1146 */
1147static LY_ERR
1148lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
1149 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
1150{
1151 LY_ERR ret = LY_SUCCESS;
1152 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001153 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1154 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001155 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001156 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001157
1158 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001159 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001160 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1161 return LY_EVALID;
1162 }
1163
1164 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001165 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001166 if (base_enums) {
1167 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1168 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001169 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001170 break;
1171 }
1172 }
1173 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001174 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001175 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001176 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001177 return LY_EVALID;
1178 }
1179 match = v;
1180 }
1181
1182 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001183 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001184 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001185 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001186 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001187 LY_ARRAY_FOR(*enums, v) {
1188 if (cur_val == (*enums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001189 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001190 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001191 cur_val, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001192 return LY_EVALID;
1193 }
1194 }
1195 } else if (base_enums) {
1196 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001197 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001198 } else {
1199 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001200 if (u == 0) {
1201 cur_val = 0;
1202 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001203 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001204 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001205 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001206 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001207 } else {
1208 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001209 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001210 }
1211
1212 /* save highest value for auto assing */
1213 if (highest_value < cur_val) {
1214 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001215 }
1216 } else { /* LY_TYPE_BITS */
1217 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001218 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001219 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 /* check collision with other values */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001221 LY_ARRAY_FOR(*enums, v) {
1222 if (cur_pos == (*enums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001223 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001224 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001225 cur_pos, enums_p[u].name, (*enums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001226 return LY_EVALID;
1227 }
1228 }
1229 } else if (base_enums) {
1230 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001231 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001232 } else {
1233 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001234 if (u == 0) {
1235 cur_pos = 0;
1236 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001237 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001238 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001239 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001240 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001241 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001242 } else {
1243 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001244 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001245 }
1246
1247 /* save highest position for auto assing */
1248 if (highest_position < cur_pos) {
1249 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001250 }
1251 }
1252
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001253 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001255 if (basetype == LY_TYPE_ENUM) {
1256 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001257 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001258 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001259 enums_p[u].name, base_enums[match].value, cur_val);
1260 return LY_EVALID;
1261 }
1262 } else {
1263 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001264 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001265 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001266 enums_p[u].name, base_enums[match].position, cur_pos);
1267 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001268 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001269 }
1270 }
1271
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001272 /* evaluate if-ffeatures */
1273 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1274 if (!enabled) {
1275 continue;
1276 }
1277
1278 /* add new enum/bit */
1279 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1280 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1281 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1282 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001283 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 +01001284 if (basetype == LY_TYPE_ENUM) {
1285 e->value = cur_val;
1286 } else {
1287 e->position = cur_pos;
1288 }
Radek Krejciab430862021-03-02 20:13:40 +01001289 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001290
1291 if (basetype == LY_TYPE_BITS) {
1292 /* keep bits ordered by position */
1293 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
1294 if (v != u) {
1295 memcpy(&storage, e, sizeof *e);
1296 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1297 memcpy(&(*enums)[v], &storage, sizeof storage);
1298 }
1299 }
1300 }
1301
1302done:
1303 return ret;
1304}
1305
Radek Krejci6a205692020-12-09 13:58:22 +01001306static LY_ERR
1307lys_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 +01001308 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001309{
1310 LY_ERR ret = LY_SUCCESS;
1311 struct lysc_type **utypes = *utypes_p;
1312 struct lysc_type_union *un_aux = NULL;
1313
1314 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1315 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001316 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1317 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001318 LY_CHECK_GOTO(ret, error);
1319 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1320 /* add space for additional types from the union subtype */
1321 un_aux = (struct lysc_type_union *)utypes[u + additional];
1322 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1323 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1324
1325 /* copy subtypes of the subtype union */
1326 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1327 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1328 struct lysc_type_leafref *lref;
1329
1330 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1331 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1332 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1333 lref = (struct lysc_type_leafref *)utypes[u + additional];
1334
1335 lref->basetype = LY_TYPE_LEAFREF;
1336 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1337 LY_CHECK_GOTO(ret, error);
1338 lref->refcount = 1;
1339 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
1340 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001341 ret = ly_type_prefix_data_dup(ctx->ctx, LY_PREF_SCHEMA_RESOLVED,
1342 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001343 LY_CHECK_GOTO(ret, error);
1344 /* TODO extensions */
1345
1346 } else {
1347 utypes[u + additional] = un_aux->types[v];
1348 ++un_aux->types[v]->refcount;
1349 }
1350 ++additional;
1351 LY_ARRAY_INCREMENT(utypes);
1352 }
1353 /* compensate u increment in main loop */
1354 --additional;
1355
1356 /* free the replaced union subtype */
1357 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1358 un_aux = NULL;
1359 } else {
1360 LY_ARRAY_INCREMENT(utypes);
1361 }
1362 }
1363
1364 *utypes_p = utypes;
1365 return LY_SUCCESS;
1366
1367error:
1368 if (un_aux) {
1369 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1370 }
1371 *utypes_p = utypes;
1372 return ret;
1373}
1374
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001375/**
1376 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1377 * @param[in] ctx Compile context.
1378 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1379 * @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 +02001380 * @param[in] context_name Name of the context node or referencing typedef for logging.
1381 * @param[in] type_p Parsed type to compile.
1382 * @param[in] basetype Base YANG built-in type of the type to compile.
1383 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1384 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1385 * @param[out] type Newly created type structure with the filled information about the type.
1386 * @return LY_ERR value.
1387 */
1388static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001389lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1390 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 +02001391{
1392 LY_ERR ret = LY_SUCCESS;
1393 struct lysc_type_bin *bin;
1394 struct lysc_type_num *num;
1395 struct lysc_type_str *str;
1396 struct lysc_type_bits *bits;
1397 struct lysc_type_enum *enumeration;
1398 struct lysc_type_dec *dec;
1399 struct lysc_type_identityref *idref;
1400 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001401 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001402
1403 switch (basetype) {
1404 case LY_TYPE_BINARY:
1405 bin = (struct lysc_type_bin *)(*type);
1406
1407 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1408 if (type_p->length) {
1409 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1410 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1411 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001412 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001413 }
1414 }
1415 break;
1416 case LY_TYPE_BITS:
1417 /* RFC 7950 9.7 - bits */
1418 bits = (struct lysc_type_bits *)(*type);
1419 if (type_p->bits) {
1420 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1421 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1422 (struct lysc_type_bitenum_item **)&bits->bits));
1423 }
1424
1425 if (!base && !type_p->flags) {
1426 /* type derived from bits built-in type must contain at least one bit */
1427 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001428 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001429 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001430 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001431 }
1432 return LY_EVALID;
1433 }
1434 break;
1435 case LY_TYPE_DEC64:
1436 dec = (struct lysc_type_dec *)(*type);
1437
1438 /* RFC 7950 9.3.4 - fraction-digits */
1439 if (!base) {
1440 if (!type_p->fraction_digits) {
1441 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001442 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001443 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001444 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001445 }
1446 return LY_EVALID;
1447 }
1448 dec->fraction_digits = type_p->fraction_digits;
1449 } else {
1450 if (type_p->fraction_digits) {
1451 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1452 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001453 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001454 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1455 tpdfname);
1456 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001457 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001458 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1459 }
1460 return LY_EVALID;
1461 }
1462 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1463 }
1464
1465 /* RFC 7950 9.2.4 - range */
1466 if (type_p->range) {
1467 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1468 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1469 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001470 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001471 }
1472 }
1473 break;
1474 case LY_TYPE_STRING:
1475 str = (struct lysc_type_str *)(*type);
1476
1477 /* RFC 7950 9.4.4 - length */
1478 if (type_p->length) {
1479 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1480 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1481 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001482 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001483 }
1484 } else if (base && ((struct lysc_type_str *)base)->length) {
1485 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1486 }
1487
1488 /* RFC 7950 9.4.5 - pattern */
1489 if (type_p->patterns) {
1490 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1491 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1492 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1493 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1494 }
1495 break;
1496 case LY_TYPE_ENUM:
1497 enumeration = (struct lysc_type_enum *)(*type);
1498
1499 /* RFC 7950 9.6 - enum */
1500 if (type_p->enums) {
1501 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1502 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1503 }
1504
1505 if (!base && !type_p->flags) {
1506 /* type derived from enumerations built-in type must contain at least one enum */
1507 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001508 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001509 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001510 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001511 }
1512 return LY_EVALID;
1513 }
1514 break;
1515 case LY_TYPE_INT8:
1516 case LY_TYPE_UINT8:
1517 case LY_TYPE_INT16:
1518 case LY_TYPE_UINT16:
1519 case LY_TYPE_INT32:
1520 case LY_TYPE_UINT32:
1521 case LY_TYPE_INT64:
1522 case LY_TYPE_UINT64:
1523 num = (struct lysc_type_num *)(*type);
1524
1525 /* RFC 6020 9.2.4 - range */
1526 if (type_p->range) {
1527 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1528 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1529 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001530 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001531 }
1532 }
1533 break;
1534 case LY_TYPE_IDENT:
1535 idref = (struct lysc_type_identityref *)(*type);
1536
1537 /* RFC 7950 9.10.2 - base */
1538 if (type_p->bases) {
1539 if (base) {
1540 /* only the directly derived identityrefs can contain base specification */
1541 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001542 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001543 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1544 tpdfname);
1545 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001546 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001547 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1548 }
1549 return LY_EVALID;
1550 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001551 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 +02001552 }
1553
1554 if (!base && !type_p->flags) {
1555 /* type derived from identityref built-in type must contain at least one base */
1556 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001557 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001558 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001559 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001560 }
1561 return LY_EVALID;
1562 }
1563 break;
1564 case LY_TYPE_LEAFREF:
1565 lref = (struct lysc_type_leafref *)*type;
1566
1567 /* RFC 7950 9.9.3 - require-instance */
1568 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001569 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001570 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001571 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001572 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1573 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001574 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001575 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1576 }
1577 return LY_EVALID;
1578 }
1579 lref->require_instance = type_p->require_instance;
1580 } else if (base) {
1581 /* inherit */
1582 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1583 } else {
1584 /* default is true */
1585 lref->require_instance = 1;
1586 }
1587 if (type_p->path) {
Radek Krejci2926c1b2021-03-16 14:45:45 +01001588 LY_PREFIX_FORMAT format;
1589
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001590 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci2926c1b2021-03-16 14:45:45 +01001591 LY_CHECK_RET(ly_type_prefix_data_add(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr),
1592 LY_PREF_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001593 } else if (base) {
1594 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci2926c1b2021-03-16 14:45:45 +01001595 LY_CHECK_RET(ly_type_prefix_data_dup(ctx->ctx, LY_PREF_SCHEMA_RESOLVED,
1596 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001597 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001598 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001599 return LY_EVALID;
1600 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001601 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001602 return LY_EVALID;
1603 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001604 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001605 break;
1606 case LY_TYPE_INST:
1607 /* RFC 7950 9.9.3 - require-instance */
1608 if (type_p->flags & LYS_SET_REQINST) {
1609 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1610 } else {
1611 /* default is true */
1612 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1613 }
1614 break;
1615 case LY_TYPE_UNION:
1616 un = (struct lysc_type_union *)(*type);
1617
1618 /* RFC 7950 7.4 - type */
1619 if (type_p->types) {
1620 if (base) {
1621 /* only the directly derived union can contain types specification */
1622 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001623 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001624 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1625 tpdfname);
1626 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001627 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001628 "Invalid type substatement for the type not directly derived from union built-in type.");
1629 }
1630 return LY_EVALID;
1631 }
1632 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001633 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 +02001634 }
1635
1636 if (!base && !type_p->flags) {
1637 /* type derived from union built-in type must contain at least one type */
1638 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001639 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001640 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001641 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001642 }
1643 return LY_EVALID;
1644 }
1645 break;
1646 case LY_TYPE_BOOL:
1647 case LY_TYPE_EMPTY:
1648 case LY_TYPE_UNKNOWN: /* just to complete switch */
1649 break;
1650 }
1651
1652 if (tpdfname) {
1653 switch (basetype) {
1654 case LY_TYPE_BINARY:
1655 type_p->compiled = *type;
1656 *type = calloc(1, sizeof(struct lysc_type_bin));
1657 break;
1658 case LY_TYPE_BITS:
1659 type_p->compiled = *type;
1660 *type = calloc(1, sizeof(struct lysc_type_bits));
1661 break;
1662 case LY_TYPE_DEC64:
1663 type_p->compiled = *type;
1664 *type = calloc(1, sizeof(struct lysc_type_dec));
1665 break;
1666 case LY_TYPE_STRING:
1667 type_p->compiled = *type;
1668 *type = calloc(1, sizeof(struct lysc_type_str));
1669 break;
1670 case LY_TYPE_ENUM:
1671 type_p->compiled = *type;
1672 *type = calloc(1, sizeof(struct lysc_type_enum));
1673 break;
1674 case LY_TYPE_INT8:
1675 case LY_TYPE_UINT8:
1676 case LY_TYPE_INT16:
1677 case LY_TYPE_UINT16:
1678 case LY_TYPE_INT32:
1679 case LY_TYPE_UINT32:
1680 case LY_TYPE_INT64:
1681 case LY_TYPE_UINT64:
1682 type_p->compiled = *type;
1683 *type = calloc(1, sizeof(struct lysc_type_num));
1684 break;
1685 case LY_TYPE_IDENT:
1686 type_p->compiled = *type;
1687 *type = calloc(1, sizeof(struct lysc_type_identityref));
1688 break;
1689 case LY_TYPE_LEAFREF:
1690 type_p->compiled = *type;
1691 *type = calloc(1, sizeof(struct lysc_type_leafref));
1692 break;
1693 case LY_TYPE_INST:
1694 type_p->compiled = *type;
1695 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1696 break;
1697 case LY_TYPE_UNION:
1698 type_p->compiled = *type;
1699 *type = calloc(1, sizeof(struct lysc_type_union));
1700 break;
1701 case LY_TYPE_BOOL:
1702 case LY_TYPE_EMPTY:
1703 case LY_TYPE_UNKNOWN: /* just to complete switch */
1704 break;
1705 }
1706 }
1707 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1708
1709cleanup:
1710 return ret;
1711}
1712
1713LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001714lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1715 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001716{
1717 LY_ERR ret = LY_SUCCESS;
1718 ly_bool dummyloops = 0;
1719 struct type_context {
1720 const struct lysp_tpdf *tpdf;
1721 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001722 } *tctx, *tctx_prev = NULL, *tctx_iter;
1723 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1724 struct lysc_type *base = NULL, *prev_type;
1725 struct ly_set tpdf_chain = {0};
1726
1727 (*type) = NULL;
1728 if (dflt) {
1729 *dflt = NULL;
1730 }
1731
1732 tctx = calloc(1, sizeof *tctx);
1733 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001734 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 +02001735 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001736 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1737 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001738 if (basetype) {
1739 break;
1740 }
1741
1742 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001743 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1744 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001745 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1746
1747 if (units && !*units) {
1748 /* inherit units */
1749 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1750 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1751 }
1752 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1753 /* inherit default */
1754 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1755 }
1756 if (dummyloops && (!units || *units) && dflt && *dflt) {
1757 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1758 break;
1759 }
1760
Radek Krejci720d2612021-03-03 19:44:22 +01001761 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1762 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1763 * and we need now recompile the type again in the updated context. */
1764 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1765 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1766 }
1767
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001768 if (tctx->tpdf->type.compiled) {
1769 /* it is not necessary to continue, the rest of the chain was already compiled,
1770 * but we still may need to inherit default and units values, so start dummy loops */
1771 basetype = tctx->tpdf->type.compiled->basetype;
1772 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1773 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1774
1775 if ((units && !*units) || (dflt && !*dflt)) {
1776 dummyloops = 1;
1777 goto preparenext;
1778 } else {
1779 tctx = NULL;
1780 break;
1781 }
1782 }
1783
1784 /* circular typedef reference detection */
1785 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1786 /* local part */
1787 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001788 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001789 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001790 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1791 free(tctx);
1792 ret = LY_EVALID;
1793 goto cleanup;
1794 }
1795 }
1796 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1797 /* global part for unions corner case */
1798 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001799 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001800 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001801 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1802 free(tctx);
1803 ret = LY_EVALID;
1804 goto cleanup;
1805 }
1806 }
1807
1808 /* store information for the following processing */
1809 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1810 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1811
1812preparenext:
1813 /* prepare next loop */
1814 tctx_prev = tctx;
1815 tctx = calloc(1, sizeof *tctx);
1816 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1817 }
1818 free(tctx);
1819
1820 /* allocate type according to the basetype */
1821 switch (basetype) {
1822 case LY_TYPE_BINARY:
1823 *type = calloc(1, sizeof(struct lysc_type_bin));
1824 break;
1825 case LY_TYPE_BITS:
1826 *type = calloc(1, sizeof(struct lysc_type_bits));
1827 break;
1828 case LY_TYPE_BOOL:
1829 case LY_TYPE_EMPTY:
1830 *type = calloc(1, sizeof(struct lysc_type));
1831 break;
1832 case LY_TYPE_DEC64:
1833 *type = calloc(1, sizeof(struct lysc_type_dec));
1834 break;
1835 case LY_TYPE_ENUM:
1836 *type = calloc(1, sizeof(struct lysc_type_enum));
1837 break;
1838 case LY_TYPE_IDENT:
1839 *type = calloc(1, sizeof(struct lysc_type_identityref));
1840 break;
1841 case LY_TYPE_INST:
1842 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1843 break;
1844 case LY_TYPE_LEAFREF:
1845 *type = calloc(1, sizeof(struct lysc_type_leafref));
1846 break;
1847 case LY_TYPE_STRING:
1848 *type = calloc(1, sizeof(struct lysc_type_str));
1849 break;
1850 case LY_TYPE_UNION:
1851 *type = calloc(1, sizeof(struct lysc_type_union));
1852 break;
1853 case LY_TYPE_INT8:
1854 case LY_TYPE_UINT8:
1855 case LY_TYPE_INT16:
1856 case LY_TYPE_UINT16:
1857 case LY_TYPE_INT32:
1858 case LY_TYPE_UINT32:
1859 case LY_TYPE_INT64:
1860 case LY_TYPE_UINT64:
1861 *type = calloc(1, sizeof(struct lysc_type_num));
1862 break;
1863 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001864 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001865 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1866 ret = LY_EVALID;
1867 goto cleanup;
1868 }
1869 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1870 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001871 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001872 ly_data_type2str[basetype]);
1873 free(*type);
1874 (*type) = NULL;
1875 ret = LY_EVALID;
1876 goto cleanup;
1877 }
1878
1879 /* get restrictions from the referred typedefs */
1880 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1881 tctx = (struct type_context *)tpdf_chain.objs[u];
1882
1883 /* remember the typedef context for circular check */
1884 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1885 LY_CHECK_GOTO(ret, cleanup);
1886
1887 if (tctx->tpdf->type.compiled) {
1888 base = tctx->tpdf->type.compiled;
1889 continue;
1890 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1891 /* no change, just use the type information from the base */
1892 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1893 ++base->refcount;
1894 continue;
1895 }
1896
1897 ++(*type)->refcount;
1898 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001899 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001900 tctx->tpdf->name, ly_data_type2str[basetype]);
1901 ret = LY_EVALID;
1902 goto cleanup;
1903 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001904 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001905 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1906 tctx->tpdf->name, tctx->tpdf->dflt.str);
1907 ret = LY_EVALID;
1908 goto cleanup;
1909 }
1910
1911 (*type)->basetype = basetype;
1912 /* TODO user type plugins */
1913 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1914 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001915 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001916 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1917 LY_CHECK_GOTO(ret, cleanup);
1918 base = prev_type;
1919 }
1920 /* remove the processed typedef contexts from the stack for circular check */
1921 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1922
1923 /* process the type definition in leaf */
1924 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1925 /* get restrictions from the node itself */
1926 (*type)->basetype = basetype;
1927 /* TODO user type plugins */
1928 (*type)->plugin = &ly_builtin_type_plugins[basetype];
1929 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001930 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001931 LY_CHECK_GOTO(ret, cleanup);
1932 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1933 /* no specific restriction in leaf's type definition, copy from the base */
1934 free(*type);
1935 (*type) = base;
1936 ++(*type)->refcount;
1937 }
1938
Radek Krejciab430862021-03-02 20:13:40 +01001939 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001940
1941cleanup:
1942 ly_set_erase(&tpdf_chain, free);
1943 return ret;
1944}
1945
1946/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01001947 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
1948 */
1949#define LYS_STATUS_USES LYS_CONFIG_MASK
1950
1951/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001952 * @brief Compile status information of the given node.
1953 *
1954 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
1955 * has the status correctly set during the compilation.
1956 *
1957 * @param[in] ctx Compile context
1958 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
1959 * If the status was set explicitly on the node, it is already set in the flags value and we just check
1960 * the compatibility with the parent's status value.
1961 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
1962 * @return LY_ERR value.
1963 */
1964static LY_ERR
1965lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
1966{
1967 /* status - it is not inherited by specification, but it does not make sense to have
1968 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
1969 if (!((*node_flags) & LYS_STATUS_MASK)) {
1970 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001971 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001972 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01001973 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001974 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
1975 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1976 }
1977 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
1978 } else {
1979 (*node_flags) |= LYS_STATUS_CURR;
1980 }
1981 } else if (parent_flags & LYS_STATUS_MASK) {
1982 /* check status compatibility with the parent */
1983 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
1984 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001985 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001986 "A \"current\" status is in conflict with the parent's \"%s\" status.",
1987 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1988 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001989 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001990 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
1991 }
1992 return LY_EVALID;
1993 }
1994 }
1995 return LY_SUCCESS;
1996}
1997
1998/**
1999 * @brief Check uniqness of the node/action/notification name.
2000 *
2001 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2002 * structures, but they share the namespace so we need to check their name collisions.
2003 *
2004 * @param[in] ctx Compile context.
2005 * @param[in] parent Parent of the nodes to check, can be NULL.
2006 * @param[in] name Name of the item to find in the given lists.
2007 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2008 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2009 */
2010static LY_ERR
2011lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2012 const struct lysc_node *exclude)
2013{
2014 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002015 const struct lysc_node_action *actions;
2016 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002017 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002018 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002019
2020#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2021
2022 if (exclude->nodetype == LYS_CASE) {
2023 /* check restricted only to all the cases */
2024 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002025 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002026 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002027 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002028 return LY_EEXIST;
2029 }
2030 }
2031
2032 return LY_SUCCESS;
2033 }
2034
2035 /* no reason for our parent to be choice anymore */
2036 assert(!parent || (parent->nodetype != LYS_CHOICE));
2037
2038 if (parent && (parent->nodetype == LYS_CASE)) {
2039 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002040
2041 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2042 iter = lysc_data_parent(parent);
2043 do {
2044 parent = parent->parent;
2045 if (parent && (parent->nodetype == LYS_CHOICE)) {
2046 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2047 }
2048 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002049 }
2050
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002051 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002052 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2053 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2054 if (exclude->flags & LYS_IS_OUTPUT) {
2055 getnext_flags |= LYS_GETNEXT_OUTPUT;
2056 parent = lysc_node_child(parent)->next;
2057 } else {
2058 parent = lysc_node_child(parent);
2059 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002060 }
2061
2062 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002063 if (!parent && ctx->ext) {
2064 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2065 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2066 goto error;
2067 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002068
Radek Krejci5fa32a32021-02-08 18:12:38 +01002069 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2070 if (iter->nodetype == LYS_CHOICE) {
2071 iter2 = NULL;
2072 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2073 if (CHECK_NODE(iter2, exclude, name)) {
2074 goto error;
2075 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002076 }
2077 }
2078 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002079 } else {
2080 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2081 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2082 goto error;
2083 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002084
Radek Krejci5fa32a32021-02-08 18:12:38 +01002085 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2086 if (iter->nodetype == LYS_CHOICE) {
2087 iter2 = NULL;
2088 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2089 if (CHECK_NODE(iter2, exclude, name)) {
2090 goto error;
2091 }
2092 }
2093 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002094 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002095
Radek Krejci5fa32a32021-02-08 18:12:38 +01002096 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2097 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2098 if (CHECK_NODE(iter, exclude, name)) {
2099 goto error;
2100 }
2101 }
2102
2103 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2104 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2105 if (CHECK_NODE(iter, exclude, name)) {
2106 goto error;
2107 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002108 }
2109 }
Radek Krejci078e4342021-02-12 18:17:26 +01002110 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002111 return LY_SUCCESS;
2112
2113error:
Radek Krejci078e4342021-02-12 18:17:26 +01002114 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002115 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002116 return LY_EEXIST;
2117
2118#undef CHECK_NODE
2119}
2120
Radek Krejci2a9fc652021-01-22 17:44:34 +01002121/**
2122 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2123 * keep specific order of augments targetting the same node.
2124 *
2125 * @param[in] ctx Compile context
2126 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2127 * the choice itself is expected instead of a specific case node.
2128 * @param[in] node Schema node to connect into the list.
2129 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2130 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2131 */
2132static LY_ERR
2133lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002134{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002135 struct lysc_node **children, *anchor = NULL;
2136 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002137
Radek Krejci2a9fc652021-01-22 17:44:34 +01002138 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002139
Radek Krejci2a9fc652021-01-22 17:44:34 +01002140 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002141 if (node->nodetype == LYS_INPUT) {
2142 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2143 /* input node is part of the action but link it with output */
2144 node->next = &((struct lysc_node_action *)parent)->output.node;
2145 node->prev = node->next;
2146 return LY_SUCCESS;
2147 } else if (node->nodetype == LYS_OUTPUT) {
2148 /* output node is part of the action but link it with input */
2149 node->next = NULL;
2150 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002151 return LY_SUCCESS;
2152 } else if (node->nodetype == LYS_ACTION) {
2153 children = (struct lysc_node **)lysc_node_actions_p(parent);
2154 } else if (node->nodetype == LYS_NOTIF) {
2155 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2156 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002157 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002158 }
2159 assert(children);
2160
2161 if (!(*children)) {
2162 /* first child */
2163 *children = node;
2164 } else if (node->flags & LYS_KEY) {
2165 /* special handling of adding keys */
2166 assert(node->module == parent->module);
2167 anchor = *children;
2168 if (anchor->flags & LYS_KEY) {
2169 while ((anchor->flags & LYS_KEY) && anchor->next) {
2170 anchor = anchor->next;
2171 }
2172 /* insert after the last key */
2173 insert_after = 1;
2174 } /* else insert before anchor (at the beginning) */
2175 } else if ((*children)->prev->module == node->module) {
2176 /* last child is from the same module, keep the order and insert at the end */
2177 anchor = (*children)->prev;
2178 insert_after = 1;
2179 } else if (parent->module == node->module) {
2180 /* adding module child after some augments were connected */
2181 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2182 } else {
2183 /* some augments are already connected and we are connecting new ones,
2184 * keep module name order and insert the node into the children list */
2185 anchor = *children;
2186 do {
2187 anchor = anchor->prev;
2188
2189 /* check that we have not found the last augment node from our module or
2190 * the first augment node from a "smaller" module or
2191 * the first node from a local module */
2192 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2193 (anchor->module == parent->module)) {
2194 /* insert after */
2195 insert_after = 1;
2196 break;
2197 }
2198
2199 /* we have traversed all the nodes, insert before anchor (as the first node) */
2200 } while (anchor->prev->next);
2201 }
2202
2203 /* insert */
2204 if (anchor) {
2205 if (insert_after) {
2206 node->next = anchor->next;
2207 node->prev = anchor;
2208 anchor->next = node;
2209 if (node->next) {
2210 /* middle node */
2211 node->next->prev = node;
2212 } else {
2213 /* last node */
2214 (*children)->prev = node;
2215 }
2216 } else {
2217 node->next = anchor;
2218 node->prev = anchor->prev;
2219 anchor->prev = node;
2220 if (anchor == *children) {
2221 /* first node */
2222 *children = node;
2223 } else {
2224 /* middle node */
2225 node->prev->next = node;
2226 }
2227 }
2228 }
2229
2230 /* check the name uniqueness (even for an only child, it may be in case) */
2231 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2232 return LY_EEXIST;
2233 }
2234 } else {
2235 /* top-level element */
2236 struct lysc_node **list;
2237
Radek Krejci6b88a462021-02-17 12:39:34 +01002238 if (ctx->ext) {
2239 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2240 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002241 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2242 } else if (node->nodetype == LYS_NOTIF) {
2243 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2244 } else {
2245 list = &ctx->cur_mod->compiled->data;
2246 }
2247 if (!(*list)) {
2248 *list = node;
2249 } else {
2250 /* insert at the end of the module's top-level nodes list */
2251 (*list)->prev->next = node;
2252 node->prev = (*list)->prev;
2253 (*list)->prev = node;
2254 }
2255
2256 /* check the name uniqueness on top-level */
2257 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2258 return LY_EEXIST;
2259 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002260 }
2261
Radek Krejci2a9fc652021-01-22 17:44:34 +01002262 return LY_SUCCESS;
2263}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002264
Radek Krejci2a9fc652021-01-22 17:44:34 +01002265/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002266 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002267 *
2268 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002269 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002270 * @return LY_ERR value.
2271 */
2272static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002273lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002274{
2275 /* case never has any explicit config */
2276 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002277
Radek Krejci91531e12021-02-08 19:57:54 +01002278 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
2279 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002280 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002281 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2282 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002283 if (node->parent) {
2284 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002285 } else {
2286 /* default is config true */
2287 node->flags |= LYS_CONFIG_W;
2288 }
2289 } else {
2290 /* config set explicitly */
2291 node->flags |= LYS_SET_CONFIG;
2292 }
2293
Radek Krejci91531e12021-02-08 19:57:54 +01002294 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002295 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002296 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002297 }
2298
Radek Krejci2a9fc652021-01-22 17:44:34 +01002299 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002300}
2301
Radek Krejci91531e12021-02-08 19:57:54 +01002302/**
2303 * @brief Set various flags of the compiled nodes
2304 *
2305 * @param[in] ctx Compile context.
2306 * @param[in] node Compiled node where the flags will be set.
2307 * @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).
2308 * Zero means no uses, non-zero value with no status bit set mean the default status.
2309 */
2310static LY_ERR
2311lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2312{
2313 /* inherit config flags */
2314 LY_CHECK_RET(lys_compile_config(ctx, node));
2315
2316 /* status - it is not inherited by specification, but it does not make sense to have
2317 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2318 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (node->parent ? node->parent->flags : 0)));
2319
2320 /* other flags */
2321 if ((ctx->options & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
2322 node->flags |= LYS_IS_INPUT;
2323 } else if ((ctx->options & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
2324 node->flags |= LYS_IS_OUTPUT;
2325 } else if ((ctx->options & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
2326 node->flags |= LYS_IS_NOTIF;
2327 }
2328
2329 return LY_SUCCESS;
2330}
2331
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002332LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002333lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2334 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2335 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002336{
2337 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002338 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002339 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002340 struct lysp_when *pwhen = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002341
Radek Krejci2a9fc652021-01-22 17:44:34 +01002342 node->nodetype = pnode->nodetype;
2343 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002344 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002345 node->prev = node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002346
Radek Krejci2a9fc652021-01-22 17:44:34 +01002347 /* compile any deviations for this node */
2348 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002349 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002350 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002351 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002352 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002353 }
2354
Radek Krejci2a9fc652021-01-22 17:44:34 +01002355 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002356
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002357 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002358 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Radek Krejciaf0548b2021-02-15 15:11:22 +01002359 if (!enabled && !(ctx->options & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002360 ly_set_add(&ctx->disabled, node, 1, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002361 ctx->options |= LYS_COMPILE_DISABLED;
2362 }
2363
Radek Krejci91531e12021-02-08 19:57:54 +01002364 /* config, status and other flags */
2365 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002366 LY_CHECK_GOTO(ret, error);
2367
2368 /* list ordering */
2369 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002370 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002371 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002372 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
2373 (ctx->options & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002374 node->flags &= ~LYS_ORDBY_MASK;
2375 node->flags |= LYS_ORDBY_SYSTEM;
2376 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2377 /* default ordering is system */
2378 node->flags |= LYS_ORDBY_SYSTEM;
2379 }
2380 }
2381
Radek Krejci2a9fc652021-01-22 17:44:34 +01002382 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2383 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2384 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2385
2386 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2387 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2388
Radek Krejci9a3823e2021-01-27 20:26:46 +01002389 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002390 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002391 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002392 LY_CHECK_GOTO(ret, cleanup);
2393 }
2394
2395 /* connect any augments */
2396 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2397
2398 /* nodetype-specific part */
2399 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2400
2401 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002402 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002403 if (node->flags & LYS_MAND_TRUE) {
2404 /* inherit LYS_MAND_TRUE in parent containers */
2405 lys_compile_mandatory_parents(parent, 1);
2406 }
2407
2408 if (child_set) {
2409 /* add the new node into set */
2410 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2411 }
2412
2413 goto cleanup;
2414
2415error:
2416 lysc_node_free(ctx->ctx, node, 0);
2417cleanup:
2418 if (ret && dev_pnode) {
2419 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2420 }
2421 lysp_dev_node_free(ctx->ctx, dev_pnode);
2422 return ret;
2423}
2424
2425/**
2426 * @brief Compile parsed action's input/output node information.
2427 * @param[in] ctx Compile context
2428 * @param[in] pnode Parsed inout node.
2429 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2430 * is enriched with the inout-specific information.
2431 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2432 */
2433LY_ERR
2434lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2435{
2436 LY_ERR ret = LY_SUCCESS;
2437 struct lysp_node *child_p;
2438 uint32_t prev_options = ctx->options;
2439
2440 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2441 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2442
2443 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002444 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Radek Krejci91531e12021-02-08 19:57:54 +01002445 ctx->options |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002446
Radek Krejci01180ac2021-01-27 08:48:22 +01002447 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002448 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2449 }
2450
2451 ctx->options = prev_options;
2452
2453done:
2454 return ret;
2455}
2456
2457/**
2458 * @brief Compile parsed action node information.
2459 * @param[in] ctx Compile context
2460 * @param[in] pnode Parsed action node.
2461 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2462 * is enriched with the action-specific information.
2463 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2464 */
2465LY_ERR
2466lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2467{
2468 LY_ERR ret;
2469 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2470 struct lysc_node_action *action = (struct lysc_node_action *)node;
2471 struct lysp_node_action_inout *input, implicit_input = {
2472 .nodetype = LYS_INPUT,
2473 .name = "input",
2474 .parent = pnode,
2475 };
2476 struct lysp_node_action_inout *output, implicit_output = {
2477 .nodetype = LYS_OUTPUT,
2478 .name = "output",
2479 .parent = pnode,
2480 };
2481
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002482 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002483 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002484 if (action_p->input.nodetype == LYS_UNKNOWN) {
2485 input = &implicit_input;
2486 } else {
2487 input = &action_p->input;
2488 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002489 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 +01002490 lysc_update_path(ctx, NULL, NULL);
2491 LY_CHECK_GOTO(ret, done);
2492
2493 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002494 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002495 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002496 output = &implicit_output;
2497 } else {
2498 output = &action_p->output;
2499 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002500 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 +01002501 lysc_update_path(ctx, NULL, NULL);
2502 LY_CHECK_GOTO(ret, done);
2503
2504 /* do not check "must" semantics in a grouping */
2505 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2506 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2507 LY_CHECK_GOTO(ret, done);
2508 }
2509
2510done:
2511 return ret;
2512}
2513
2514/**
2515 * @brief Compile parsed action node information.
2516 * @param[in] ctx Compile context
2517 * @param[in] pnode Parsed action node.
2518 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2519 * is enriched with the action-specific information.
2520 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2521 */
2522LY_ERR
2523lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2524{
2525 LY_ERR ret = LY_SUCCESS;
2526 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2527 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2528 struct lysp_node *child_p;
2529
2530 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002531 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002532 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002533 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002534 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002535 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002536
Radek Krejci01180ac2021-01-27 08:48:22 +01002537 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002538 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002539 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002540 }
2541
Radek Krejci2a9fc652021-01-22 17:44:34 +01002542done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002543 return ret;
2544}
2545
2546/**
2547 * @brief Compile parsed container node information.
2548 * @param[in] ctx Compile context
2549 * @param[in] pnode Parsed container node.
2550 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2551 * is enriched with the container-specific information.
2552 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2553 */
2554static LY_ERR
2555lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2556{
2557 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2558 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2559 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002560 LY_ERR ret = LY_SUCCESS;
2561
2562 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002563 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002564 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002565 }
2566
2567 /* more cases when the container has meaning but is kept NP for convenience:
2568 * - when condition
2569 * - direct child action/notification
2570 */
2571
2572 LY_LIST_FOR(cont_p->child, child_p) {
2573 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2574 LY_CHECK_GOTO(ret, done);
2575 }
2576
Michal Vasko5347e3a2020-11-03 17:14:57 +01002577 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002578 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002579 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002580 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002581 LY_CHECK_GOTO(ret, done);
2582 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002583
2584 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2585 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2586 LY_CHECK_GOTO(ret, done);
2587 }
2588 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2589 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2590 LY_CHECK_GOTO(ret, done);
2591 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002592
2593done:
2594 return ret;
2595}
2596
Michal Vasko72244882021-01-12 15:21:05 +01002597/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002598 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2599 * @param[in] ctx Compile context.
2600 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2601 * @param[in] type_p Parsed type to compile.
2602 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2603 * @return LY_ERR value.
2604 */
2605static LY_ERR
2606lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2607 struct lysc_node_leaf *leaf)
2608{
2609 struct lysp_qname *dflt;
2610
Michal Vaskoa99b3572021-02-01 11:54:58 +01002611 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002612 leaf->units ? NULL : &leaf->units, &dflt));
2613
2614 /* store default value, if any */
2615 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2616 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2617 }
2618
Michal Vasko12606ee2020-11-25 17:05:11 +01002619 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002620 /* 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 +01002621 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002622 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002623 LY_ARRAY_COUNT_TYPE u;
2624 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2625 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2626 /* 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 +01002627 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002628 }
2629 }
2630 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2631 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002632 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 +02002633 return LY_EVALID;
2634 }
2635 }
2636
2637 return LY_SUCCESS;
2638}
2639
2640/**
2641 * @brief Compile parsed leaf node information.
2642 * @param[in] ctx Compile context
2643 * @param[in] pnode Parsed leaf node.
2644 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2645 * is enriched with the leaf-specific information.
2646 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2647 */
2648static LY_ERR
2649lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2650{
2651 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2652 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002653 LY_ERR ret = LY_SUCCESS;
2654
Michal Vasko5347e3a2020-11-03 17:14:57 +01002655 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002656 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002657 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002658 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002659 LY_CHECK_GOTO(ret, done);
2660 }
2661 if (leaf_p->units) {
2662 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2663 leaf->flags |= LYS_SET_UNITS;
2664 }
2665
2666 /* compile type */
2667 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2668 LY_CHECK_GOTO(ret, done);
2669
2670 /* store/update default value */
2671 if (leaf_p->dflt.str) {
2672 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2673 leaf->flags |= LYS_SET_DFLT;
2674 }
2675
2676 /* checks */
2677 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002678 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002679 "Invalid mandatory leaf with a default value.");
2680 return LY_EVALID;
2681 }
2682
2683done:
2684 return ret;
2685}
2686
2687/**
2688 * @brief Compile parsed leaf-list node information.
2689 * @param[in] ctx Compile context
2690 * @param[in] pnode Parsed leaf-list node.
2691 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2692 * is enriched with the leaf-list-specific information.
2693 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2694 */
2695static LY_ERR
2696lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2697{
2698 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2699 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002700 LY_ERR ret = LY_SUCCESS;
2701
Michal Vasko5347e3a2020-11-03 17:14:57 +01002702 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002703 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002704 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002705 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002706 LY_CHECK_GOTO(ret, done);
2707 }
2708 if (llist_p->units) {
2709 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2710 llist->flags |= LYS_SET_UNITS;
2711 }
2712
2713 /* compile type */
2714 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2715 LY_CHECK_GOTO(ret, done);
2716
2717 /* store/update default values */
2718 if (llist_p->dflts) {
2719 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002720 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002721 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2722 return LY_EVALID;
2723 }
2724
2725 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2726 llist->flags |= LYS_SET_DFLT;
2727 }
2728
2729 llist->min = llist_p->min;
2730 if (llist->min) {
2731 llist->flags |= LYS_MAND_TRUE;
2732 }
2733 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2734
2735 /* checks */
2736 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002737 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 +02002738 return LY_EVALID;
2739 }
2740
2741 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002742 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002743 llist->min, llist->max);
2744 return LY_EVALID;
2745 }
2746
2747done:
2748 return ret;
2749}
2750
2751LY_ERR
2752lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2753 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2754 const struct lysc_node **target, uint16_t *result_flag)
2755{
2756 LY_ERR ret = LY_EVALID;
2757 const char *name, *prefix, *id;
2758 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002759 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002760 const char *nodeid_type;
2761 uint32_t getnext_extra_flag = 0;
2762 uint16_t current_nodetype = 0;
2763
2764 assert(nodeid);
2765 assert(target);
2766 assert(result_flag);
2767 *target = NULL;
2768 *result_flag = 0;
2769
2770 id = nodeid;
2771
2772 if (ctx_node) {
2773 /* descendant-schema-nodeid */
2774 nodeid_type = "descendant";
2775
2776 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002777 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002778 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002779 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002780 return LY_EVALID;
2781 }
2782 } else {
2783 /* absolute-schema-nodeid */
2784 nodeid_type = "absolute";
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 absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
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 ++id;
2793 }
2794
2795 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2796 if (prefix) {
2797 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2798 if (!mod) {
2799 /* module must always be found */
2800 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002801 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002802 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002803 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002804 return LY_ENOTFOUND;
2805 }
2806 } else {
2807 switch (format) {
2808 case LY_PREF_SCHEMA:
2809 case LY_PREF_SCHEMA_RESOLVED:
2810 /* use the current module */
2811 mod = cur_mod;
2812 break;
2813 case LY_PREF_JSON:
2814 if (!ctx_node) {
2815 LOGINT_RET(ctx->ctx);
2816 }
2817 /* inherit the module of the previous context node */
2818 mod = ctx_node->module;
2819 break;
2820 case LY_PREF_XML:
2821 /* not really defined */
2822 LOGINT_RET(ctx->ctx);
2823 }
2824 }
2825
2826 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2827 /* move through input/output manually */
2828 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002829 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2830 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002831 return LY_ENOTFOUND;
2832 }
2833 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002834 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002835 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002836 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002837 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2838 } else {
2839 /* only input or output is valid */
2840 ctx_node = NULL;
2841 }
2842 } else {
2843 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002844 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002845 getnext_extra_flag = 0;
2846 }
2847 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002848 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2849 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002850 return LY_ENOTFOUND;
2851 }
2852 current_nodetype = ctx_node->nodetype;
2853
2854 if (current_nodetype == LYS_NOTIF) {
2855 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2856 } else if (current_nodetype == LYS_INPUT) {
2857 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2858 } else if (current_nodetype == LYS_OUTPUT) {
2859 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2860 }
2861
2862 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2863 break;
2864 }
2865 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002866 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002867 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002868 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002869 return LY_EVALID;
2870 }
2871 ++id;
2872 }
2873
2874 if (ret == LY_SUCCESS) {
2875 *target = ctx_node;
2876 if (nodetype && !(current_nodetype & nodetype)) {
2877 return LY_EDENIED;
2878 }
2879 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002880 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002881 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002882 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002883 }
2884
2885 return ret;
2886}
2887
2888/**
2889 * @brief Compile information about list's uniques.
2890 * @param[in] ctx Compile context.
2891 * @param[in] uniques Sized array list of unique statements.
2892 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2893 * @return LY_ERR value.
2894 */
2895static LY_ERR
2896lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2897{
2898 LY_ERR ret = LY_SUCCESS;
2899 struct lysc_node_leaf **key, ***unique;
2900 struct lysc_node *parent;
2901 const char *keystr, *delim;
2902 size_t len;
2903 LY_ARRAY_COUNT_TYPE v;
2904 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2905 uint16_t flags;
2906
2907 LY_ARRAY_FOR(uniques, v) {
2908 config = -1;
2909 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2910 keystr = uniques[v].str;
2911 while (keystr) {
2912 delim = strpbrk(keystr, " \t\n");
2913 if (delim) {
2914 len = delim - keystr;
2915 while (isspace(*delim)) {
2916 ++delim;
2917 }
2918 } else {
2919 len = strlen(keystr);
2920 }
2921
2922 /* unique node must be present */
2923 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002924 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002925 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2926 if (ret != LY_SUCCESS) {
2927 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002928 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002929 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002930 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002931 }
2932 return LY_EVALID;
2933 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002934 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002935 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01002936 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002937 return LY_EVALID;
2938 }
2939
2940 /* all referenced leafs must be of the same config type */
2941 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2942 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002943 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002944 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2945 return LY_EVALID;
2946 } else if ((*key)->flags & LYS_CONFIG_W) {
2947 config = 1;
2948 } else { /* LYS_CONFIG_R */
2949 config = 0;
2950 }
2951
2952 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
2953 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
2954 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002955 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002956 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
2957 return LY_EVALID;
2958 }
2959 }
2960
2961 /* check status */
2962 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2963 (*key)->flags, (*key)->module, (*key)->name));
2964
2965 /* mark leaf as unique */
2966 (*key)->flags |= LYS_UNIQUE;
2967
2968 /* next unique value in line */
2969 keystr = delim;
2970 }
2971 /* next unique definition */
2972 }
2973
2974 return LY_SUCCESS;
2975}
2976
2977/**
2978 * @brief Compile parsed list node information.
2979 * @param[in] ctx Compile context
2980 * @param[in] pnode Parsed list node.
2981 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2982 * is enriched with the list-specific information.
2983 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2984 */
2985static LY_ERR
2986lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2987{
2988 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
2989 struct lysc_node_list *list = (struct lysc_node_list *)node;
2990 struct lysp_node *child_p;
2991 struct lysc_node_leaf *key, *prev_key = NULL;
2992 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002993 const char *keystr, *delim;
2994 LY_ERR ret = LY_SUCCESS;
2995
2996 list->min = list_p->min;
2997 if (list->min) {
2998 list->flags |= LYS_MAND_TRUE;
2999 }
3000 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3001
3002 LY_LIST_FOR(list_p->child, child_p) {
3003 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3004 }
3005
Michal Vasko5347e3a2020-11-03 17:14:57 +01003006 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003007 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003008 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003009 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003010 }
3011
3012 /* keys */
3013 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003014 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003015 return LY_EVALID;
3016 }
3017
3018 /* find all the keys (must be direct children) */
3019 keystr = list_p->key;
3020 if (!keystr) {
3021 /* keyless list */
3022 list->flags |= LYS_KEYLESS;
3023 }
3024 while (keystr) {
3025 delim = strpbrk(keystr, " \t\n");
3026 if (delim) {
3027 len = delim - keystr;
3028 while (isspace(*delim)) {
3029 ++delim;
3030 }
3031 } else {
3032 len = strlen(keystr);
3033 }
3034
3035 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003036 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3037 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003038 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003039 return LY_EVALID;
3040 }
3041 /* keys must be unique */
3042 if (key->flags & LYS_KEY) {
3043 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003044 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003045 return LY_EVALID;
3046 }
3047
Radek Krejcia6016992021-03-03 10:13:41 +01003048 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003049 /* key must have the same config flag as the list itself */
3050 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003051 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003052 return LY_EVALID;
3053 }
3054 if (ctx->pmod->version < LYS_VERSION_1_1) {
3055 /* YANG 1.0 denies key to be of empty type */
3056 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003057 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003058 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3059 return LY_EVALID;
3060 }
3061 } else {
3062 /* when and if-feature are illegal on list keys */
3063 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003064 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003065 "List's key must not have any \"when\" statement.");
3066 return LY_EVALID;
3067 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003068 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003069 }
3070
3071 /* check status */
3072 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3073 key->flags, key->module, key->name));
3074
3075 /* ignore default values of the key */
3076 if (key->dflt) {
3077 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3078 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3079 free(key->dflt);
3080 key->dflt = NULL;
3081 }
3082 /* mark leaf as key */
3083 key->flags |= LYS_KEY;
3084
3085 /* move it to the correct position */
3086 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3087 /* fix links in closest previous siblings of the key */
3088 if (key->next) {
3089 key->next->prev = key->prev;
3090 } else {
3091 /* last child */
3092 list->child->prev = key->prev;
3093 }
3094 if (key->prev->next) {
3095 key->prev->next = key->next;
3096 }
3097 /* fix links in the key */
3098 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003099 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003100 key->next = prev_key->next;
3101 } else {
3102 key->prev = list->child->prev;
3103 key->next = list->child;
3104 }
3105 /* fix links in closes future siblings of the key */
3106 if (prev_key) {
3107 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003108 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003109 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003110 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003111 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003112 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003113 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003114 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003115 }
3116 /* fix links in parent */
3117 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003118 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003119 }
3120 }
3121
3122 /* next key value */
3123 prev_key = key;
3124 keystr = delim;
3125 lysc_update_path(ctx, NULL, NULL);
3126 }
3127
3128 /* uniques */
3129 if (list_p->uniques) {
3130 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3131 }
3132
Radek Krejci2a9fc652021-01-22 17:44:34 +01003133 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3134 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3135 LY_CHECK_GOTO(ret, done);
3136 }
3137 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3138 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3139 LY_CHECK_GOTO(ret, done);
3140 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003141
3142 /* checks */
3143 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003144 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003145 list->min, list->max);
3146 return LY_EVALID;
3147 }
3148
3149done:
3150 return ret;
3151}
3152
3153/**
3154 * @brief Do some checks and set the default choice's case.
3155 *
3156 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3157 *
3158 * @param[in] ctx Compile context.
3159 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3160 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3161 * @return LY_ERR value.
3162 */
3163static LY_ERR
3164lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3165{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003166 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003167 const struct lys_module *mod;
3168 const char *prefix = NULL, *name;
3169 size_t prefix_len = 0;
3170
3171 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3172 name = strchr(dflt->str, ':');
3173 if (name) {
3174 prefix = dflt->str;
3175 prefix_len = name - prefix;
3176 ++name;
3177 } else {
3178 name = dflt->str;
3179 }
3180 if (prefix) {
3181 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
3182 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003183 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003184 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003185 return LY_EVALID;
3186 }
3187 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003188 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003189 }
3190
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003191 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 +02003192 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003193 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003194 "Default case \"%s\" not found.", dflt->str);
3195 return LY_EVALID;
3196 }
3197
3198 /* no mandatory nodes directly under the default case */
3199 LY_LIST_FOR(ch->dflt->child, iter) {
3200 if (iter->parent != (struct lysc_node *)ch->dflt) {
3201 break;
3202 }
3203 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003204 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003205 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3206 return LY_EVALID;
3207 }
3208 }
3209
3210 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003211 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003212 return LY_EVALID;
3213 }
3214
3215 ch->dflt->flags |= LYS_SET_DFLT;
3216 return LY_SUCCESS;
3217}
3218
3219LY_ERR
3220lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3221 struct ly_set *child_set)
3222{
3223 LY_ERR ret = LY_SUCCESS;
3224 struct lysp_node *child_p_next = child_p->next;
3225 struct lysp_node_case *cs_p;
3226
3227 if (child_p->nodetype == LYS_CASE) {
3228 /* standard case under choice */
3229 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3230 } else {
3231 /* we need the implicit case first, so create a fake parsed case */
3232 cs_p = calloc(1, sizeof *cs_p);
3233 cs_p->nodetype = LYS_CASE;
3234 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3235 cs_p->child = child_p;
3236
3237 /* make the child the only case child */
3238 child_p->next = NULL;
3239
3240 /* compile it normally */
3241 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3242
3243free_fake_node:
3244 /* free the fake parsed node and correct pointers back */
3245 cs_p->child = NULL;
3246 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3247 child_p->next = child_p_next;
3248 }
3249
3250 return ret;
3251}
3252
3253/**
3254 * @brief Compile parsed choice node information.
3255 *
3256 * @param[in] ctx Compile context
3257 * @param[in] pnode Parsed choice node.
3258 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3259 * is enriched with the choice-specific information.
3260 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3261 */
3262static LY_ERR
3263lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3264{
3265 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3266 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3267 struct lysp_node *child_p;
3268 LY_ERR ret = LY_SUCCESS;
3269
3270 assert(node->nodetype == LYS_CHOICE);
3271
3272 LY_LIST_FOR(ch_p->child, child_p) {
3273 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3274 }
3275
3276 /* default branch */
3277 if (ch_p->dflt.str) {
3278 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3279 }
3280
3281 return ret;
3282}
3283
3284/**
3285 * @brief Compile parsed anydata or anyxml node information.
3286 * @param[in] ctx Compile context
3287 * @param[in] pnode Parsed anydata or anyxml node.
3288 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3289 * is enriched with the any-specific information.
3290 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3291 */
3292static LY_ERR
3293lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3294{
3295 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3296 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003297 LY_ERR ret = LY_SUCCESS;
3298
Michal Vasko5347e3a2020-11-03 17:14:57 +01003299 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003300 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003301 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003302 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003303 LY_CHECK_GOTO(ret, done);
3304 }
3305
Radek Krejci91531e12021-02-08 19:57:54 +01003306 if (any->flags & LYS_CONFIG_W) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003307 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3308 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3309 }
3310done:
3311 return ret;
3312}
3313
3314/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003315 * @brief Prepare the case structure in choice node for the new data node.
3316 *
3317 * 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
3318 * created in the choice when the first child was processed.
3319 *
3320 * @param[in] ctx Compile context.
3321 * @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,
3322 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3323 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3324 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3325 * @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,
3326 * it is linked from the case structure only in case it is its first child.
3327 */
3328static LY_ERR
3329lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3330{
3331 struct lysp_node *child_p;
3332 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3333
3334 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3335 /* we have to add an implicit case node into the parent choice */
3336 } else if (pnode->nodetype == LYS_CASE) {
3337 /* explicit parent case */
3338 LY_LIST_FOR(cs_p->child, child_p) {
3339 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3340 }
3341 } else {
3342 LOGINT_RET(ctx->ctx);
3343 }
3344
3345 return LY_SUCCESS;
3346}
3347
3348void
3349lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3350{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003351 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003352
3353 if (add) { /* set flag */
3354 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3355 parent = parent->parent) {
3356 parent->flags |= LYS_MAND_TRUE;
3357 }
3358 } else { /* unset flag */
3359 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003360 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003361 if (iter->flags & LYS_MAND_TRUE) {
3362 /* there is another mandatory node */
3363 return;
3364 }
3365 }
3366 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3367 parent->flags &= ~LYS_MAND_TRUE;
3368 }
3369 }
3370}
3371
3372/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003373 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003374 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003375 * @param[in] name Name of the grouping to find,
3376 * @return NULL when there is no grouping with the specified name
3377 * @return Pointer to the grouping of the specified @p name.
3378 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003379static struct lysp_node_grp *
3380match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003381{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003382 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003383
Radek Krejci2a9fc652021-01-22 17:44:34 +01003384 LY_LIST_FOR(grps, grp) {
3385 if (!strcmp(grp->name, name)) {
3386 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003387 }
3388 }
3389
3390 return NULL;
3391}
3392
3393/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003394 * @brief Find grouping for a uses.
3395 *
3396 * @param[in] ctx Compile context.
3397 * @param[in] uses_p Parsed uses node.
3398 * @param[out] gpr_p Found grouping on success.
3399 * @param[out] grp_pmod Module of @p grp_p on success.
3400 * @return LY_ERR value.
3401 */
3402static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003403lys_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 +02003404 struct lysp_module **grp_pmod)
3405{
3406 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003407 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003408 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003409 const char *id, *name, *prefix, *local_pref;
3410 size_t prefix_len, name_len;
3411 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003412 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003413
3414 *grp_p = NULL;
3415 *grp_pmod = NULL;
3416
3417 /* search for the grouping definition */
3418 id = uses_p->name;
3419 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3420 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3421 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3422 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003423 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003424 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003425 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3426 found = ctx->pmod;
3427 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003428 }
3429 }
3430 } else {
3431 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003432 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003433 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003434 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003435 "Invalid prefix used for grouping reference.", uses_p->name);
3436 return LY_EVALID;
3437 }
3438 pmod = mod->parsed;
3439 }
3440
3441 if (!found) {
3442 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003443 if ((grp = match_grouping(pmod->groupings, name))) {
3444 found = pmod;
3445 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003446 /* ... and all the submodules */
3447 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003448 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3449 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003450 break;
3451 }
3452 }
3453 }
3454 }
3455 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003456 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003457 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3458 return LY_EVALID;
3459 }
3460
3461 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3462 /* remember that the grouping is instantiated to avoid its standalone validation */
3463 grp->flags |= LYS_USED_GRP;
3464 }
3465
3466 *grp_p = grp;
3467 *grp_pmod = found;
3468 return LY_SUCCESS;
3469}
3470
3471/**
3472 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3473 * If present, also apply uses's modificators.
3474 *
3475 * @param[in] ctx Compile context
3476 * @param[in] uses_p Parsed uses schema node.
3477 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3478 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3479 * the compile context.
3480 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3481 */
3482static LY_ERR
3483lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3484{
3485 struct lysp_node *pnode;
3486 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003487 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003488 uint32_t i, grp_stack_count;
3489 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3490 LY_ERR ret = LY_SUCCESS;
3491 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003492 struct ly_set uses_child_set = {0};
3493
3494 /* find the referenced grouping */
3495 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3496
3497 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3498 grp_stack_count = ctx->groupings.count;
3499 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3500 if (grp_stack_count == ctx->groupings.count) {
3501 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003502 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003503 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3504 return LY_EVALID;
3505 }
3506
3507 /* check status */
3508 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3509 LY_CHECK_GOTO(ret, cleanup);
3510
3511 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3512 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3513 LY_CHECK_GOTO(ret, cleanup);
3514
3515 /* switch context's parsed module being processed */
3516 ctx->pmod = grp_mod;
3517
3518 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003519 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003520 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3521 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 +02003522 LY_CHECK_GOTO(ret, cleanup);
3523 }
3524
3525 if (child_set) {
3526 /* add these children to our compiled child_set as well since uses is a schema-only node */
3527 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3528 }
3529
3530 if (uses_p->when) {
3531 /* pass uses's when to all the data children */
3532 for (i = 0; i < uses_child_set.count; ++i) {
3533 child = uses_child_set.snodes[i];
3534
Michal Vasko72244882021-01-12 15:21:05 +01003535 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 +02003536 LY_CHECK_GOTO(ret, cleanup);
3537 }
3538 }
3539
3540 /* compile actions */
3541 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003542 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003543 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3544 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003545 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003546 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3547 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003548 ret = LY_EVALID;
3549 goto cleanup;
3550 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003551 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3552 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3553 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3554 LY_CHECK_GOTO(ret, cleanup);
3555 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003556
3557 if (uses_p->when) {
3558 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003559 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3560 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 +02003561 LY_CHECK_GOTO(ret, cleanup);
3562 }
3563 }
3564 }
3565
3566 /* compile notifications */
3567 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003568 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003569 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3570 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003571 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003572 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3573 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003574 ret = LY_EVALID;
3575 goto cleanup;
3576 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003577
3578 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3579 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3580 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3581 LY_CHECK_GOTO(ret, cleanup);
3582 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003583
3584 if (uses_p->when) {
3585 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003586 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3587 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 +02003588 LY_CHECK_GOTO(ret, cleanup);
3589 }
3590 }
3591 }
3592
3593 /* check that all augments were applied */
3594 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003595 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3596 /* augment of some parent uses, irrelevant now */
3597 continue;
3598 }
3599
3600 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003601 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3602 ret = LY_ENOTFOUND;
3603 }
3604 LY_CHECK_GOTO(ret, cleanup);
3605
3606 /* check that all refines were applied */
3607 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003608 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3609 /* refine of some paretn uses, irrelevant now */
3610 continue;
3611 }
3612
3613 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003614 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3615 ret = LY_ENOTFOUND;
3616 }
3617 LY_CHECK_GOTO(ret, cleanup);
3618
3619cleanup:
3620 /* reload previous context's parsed module being processed */
3621 ctx->pmod = mod_old;
3622
3623 /* remove the grouping from the stack for circular groupings dependency check */
3624 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3625 assert(ctx->groupings.count == grp_stack_count);
3626
3627 ly_set_erase(&uses_child_set, NULL);
3628 return ret;
3629}
3630
3631static int
3632lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3633{
3634 struct lysp_node *iter;
3635 int len = 0;
3636
3637 *path = NULL;
3638 for (iter = node; iter && len >= 0; iter = iter->parent) {
3639 char *s = *path;
3640 char *id;
3641
3642 switch (iter->nodetype) {
3643 case LYS_USES:
3644 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3645 break;
3646 case LYS_GROUPING:
3647 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3648 break;
3649 case LYS_AUGMENT:
3650 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3651 break;
3652 default:
3653 id = strdup(iter->name);
3654 break;
3655 }
3656
3657 if (!iter->parent) {
3658 /* print prefix */
3659 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3660 } else {
3661 /* prefix is the same as in parent */
3662 len = asprintf(path, "/%s%s", id, s ? s : "");
3663 }
3664 free(s);
3665 free(id);
3666 }
3667
3668 if (len < 0) {
3669 free(*path);
3670 *path = NULL;
3671 } else if (len == 0) {
3672 *path = strdup("/");
3673 len = 1;
3674 }
3675 return len;
3676}
3677
3678LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003679lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003680{
3681 LY_ERR ret;
3682 char *path;
3683 int len;
3684
3685 struct lysp_node_uses fake_uses = {
3686 .parent = pnode,
3687 .nodetype = LYS_USES,
3688 .flags = 0, .next = NULL,
3689 .name = grp->name,
3690 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3691 .refines = NULL, .augments = NULL
3692 };
3693 struct lysc_node_container fake_container = {
3694 .nodetype = LYS_CONTAINER,
3695 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3696 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003697 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003698 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003699 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003700 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003701 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3702 };
3703
3704 if (grp->parent) {
3705 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3706 }
3707
3708 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3709 if (len < 0) {
3710 LOGMEM(ctx->ctx);
3711 return LY_EMEM;
3712 }
3713 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3714 ctx->path_len = (uint32_t)len;
3715 free(path);
3716
3717 lysc_update_path(ctx, NULL, "{grouping}");
3718 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003719 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003720 lysc_update_path(ctx, NULL, NULL);
3721 lysc_update_path(ctx, NULL, NULL);
3722
3723 ctx->path_len = 1;
3724 ctx->path[1] = '\0';
3725
3726 /* cleanup */
3727 lysc_node_container_free(ctx->ctx, &fake_container);
3728
3729 return ret;
3730}
3731
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003732LY_ERR
3733lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3734 struct ly_set *child_set)
3735{
3736 LY_ERR ret = LY_SUCCESS;
3737 struct lysc_node *node = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003738 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003739
3740 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3741
3742 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003743 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003744 } else {
3745 lysc_update_path(ctx, NULL, "{uses}");
3746 lysc_update_path(ctx, NULL, pnode->name);
3747 }
3748
3749 switch (pnode->nodetype) {
3750 case LYS_CONTAINER:
3751 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3752 node_compile_spec = lys_compile_node_container;
3753 break;
3754 case LYS_LEAF:
3755 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3756 node_compile_spec = lys_compile_node_leaf;
3757 break;
3758 case LYS_LIST:
3759 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3760 node_compile_spec = lys_compile_node_list;
3761 break;
3762 case LYS_LEAFLIST:
3763 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3764 node_compile_spec = lys_compile_node_leaflist;
3765 break;
3766 case LYS_CHOICE:
3767 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3768 node_compile_spec = lys_compile_node_choice;
3769 break;
3770 case LYS_CASE:
3771 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3772 node_compile_spec = lys_compile_node_case;
3773 break;
3774 case LYS_ANYXML:
3775 case LYS_ANYDATA:
3776 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3777 node_compile_spec = lys_compile_node_any;
3778 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003779 case LYS_RPC:
3780 case LYS_ACTION:
Radek Krejci91531e12021-02-08 19:57:54 +01003781 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003782 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3783 "Action \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003784 (ctx->options & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003785 return LY_EVALID;
3786 }
3787 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3788 node_compile_spec = lys_compile_node_action;
Radek Krejci91531e12021-02-08 19:57:54 +01003789 ctx->options |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003790 break;
3791 case LYS_NOTIF:
Radek Krejci91531e12021-02-08 19:57:54 +01003792 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003793 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3794 "Notification \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003795 (ctx->options & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003796 return LY_EVALID;
3797 }
3798 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3799 node_compile_spec = lys_compile_node_notif;
3800 ctx->options |= LYS_COMPILE_NOTIFICATION;
3801 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003802 case LYS_USES:
3803 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3804 lysc_update_path(ctx, NULL, NULL);
3805 lysc_update_path(ctx, NULL, NULL);
3806 return ret;
3807 default:
3808 LOGINT(ctx->ctx);
3809 return LY_EINT;
3810 }
3811 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3812
Radek Krejci2a9fc652021-01-22 17:44:34 +01003813 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003814
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003815 ctx->options = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003816 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003817 return ret;
3818}