blob: 2808430e7c8c04d9770c1ee4766233001dd28594 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of common nodes.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020017
18#include "schema_compile_node.h"
19
20#include <assert.h>
21#include <ctype.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "common.h"
29#include "compat.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020030#include "dict.h"
31#include "log.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020032#include "plugins.h"
Radek Krejci77114102021-03-10 15:21:57 +010033#include "plugins_exts_compile.h"
Radek Krejci04699f02021-03-22 21:50:29 +010034#include "plugins_internal.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020035#include "plugins_types.h"
36#include "schema_compile.h"
37#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010038#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020039#include "set.h"
40#include "tree.h"
41#include "tree_data.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020043#include "tree_schema.h"
44#include "tree_schema_internal.h"
45#include "xpath.h"
46
47static struct lysc_ext_instance *
48lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
49{
50 /* TODO - extensions, increase refcount */
51 (void) ctx;
52 (void) orig;
53 return NULL;
54}
55
56/**
57 * @brief Add/replace a leaf default value in unres.
58 * Can also be used for a single leaf-list default value.
59 *
60 * @param[in] ctx Compile context.
61 * @param[in] leaf Leaf with the default value.
62 * @param[in] dflt Default value to use.
63 * @return LY_ERR value.
64 */
65static LY_ERR
66lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
67{
68 struct lysc_unres_dflt *r = NULL;
69 uint32_t i;
70
Michal Vasko12606ee2020-11-25 17:05:11 +010071 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010072 return LY_SUCCESS;
73 }
74
Michal Vasko405cc9e2020-12-01 12:01:27 +010075 for (i = 0; i < ctx->unres->dflts.count; ++i) {
76 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020077 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010078 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020079 lysp_qname_free(ctx->ctx, r->dflt);
80 free(r->dflt);
81 break;
82 }
83 }
84 if (!r) {
85 /* add new unres item */
86 r = calloc(1, sizeof *r);
87 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
88 r->leaf = leaf;
89
Michal Vasko405cc9e2020-12-01 12:01:27 +010090 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020091 }
92
93 r->dflt = malloc(sizeof *r->dflt);
94 LY_CHECK_GOTO(!r->dflt, error);
95 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
96
97 return LY_SUCCESS;
98
99error:
100 free(r->dflt);
101 LOGMEM(ctx->ctx);
102 return LY_EMEM;
103}
104
105/**
106 * @brief Add/replace a leaf-list default value(s) in unres.
107 *
108 * @param[in] ctx Compile context.
109 * @param[in] llist Leaf-list with the default value.
110 * @param[in] dflts Sized array of the default values.
111 * @return LY_ERR value.
112 */
113static LY_ERR
114lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
115{
116 struct lysc_unres_dflt *r = NULL;
117 uint32_t i;
118
Michal Vasko12606ee2020-11-25 17:05:11 +0100119 if (ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100120 return LY_SUCCESS;
121 }
122
Michal Vasko405cc9e2020-12-01 12:01:27 +0100123 for (i = 0; i < ctx->unres->dflts.count; ++i) {
124 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200125 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100126 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200127 lysp_qname_free(ctx->ctx, r->dflt);
128 free(r->dflt);
129 r->dflt = NULL;
130 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
131 r->dflts = NULL;
132 break;
133 }
134 }
135 if (!r) {
136 r = calloc(1, sizeof *r);
137 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
138 r->llist = llist;
139
Michal Vasko405cc9e2020-12-01 12:01:27 +0100140 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200141 }
142
143 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
144
145 return LY_SUCCESS;
146}
147
148/**
149 * @brief Duplicate the compiled pattern structure.
150 *
151 * Instead of duplicating memory, the reference counter in the @p orig is increased.
152 *
153 * @param[in] orig The pattern structure to duplicate.
154 * @return The duplicated structure to use.
155 */
156static struct lysc_pattern *
157lysc_pattern_dup(struct lysc_pattern *orig)
158{
159 ++orig->refcount;
160 return orig;
161}
162
163/**
164 * @brief Duplicate the array of compiled patterns.
165 *
166 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
167 *
168 * @param[in] ctx Libyang context for logging.
169 * @param[in] orig The patterns sized array to duplicate.
170 * @return New sized array as a copy of @p orig.
171 * @return NULL in case of memory allocation error.
172 */
173static struct lysc_pattern **
174lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
175{
176 struct lysc_pattern **dup = NULL;
177 LY_ARRAY_COUNT_TYPE u;
178
179 assert(orig);
180
181 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
182 LY_ARRAY_FOR(orig, u) {
183 dup[u] = lysc_pattern_dup(orig[u]);
184 LY_ARRAY_INCREMENT(dup);
185 }
186 return dup;
187}
188
189/**
190 * @brief Duplicate compiled range structure.
191 *
192 * @param[in] ctx Libyang context for logging.
193 * @param[in] orig The range structure to be duplicated.
194 * @return New compiled range structure as a copy of @p orig.
195 * @return NULL in case of memory allocation error.
196 */
197static struct lysc_range *
198lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
199{
200 struct lysc_range *dup;
201 LY_ERR ret;
202
203 assert(orig);
204
205 dup = calloc(1, sizeof *dup);
206 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
207 if (orig->parts) {
208 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100209 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200210 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
211 }
212 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
213 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
214 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
215
216 return dup;
217cleanup:
218 free(dup);
219 (void) ret; /* set but not used due to the return type */
220 return NULL;
221}
222
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200223/**
224 * @brief Compile information from the when statement
225 *
226 * @param[in] ctx Compile context.
227 * @param[in] when_p Parsed when structure.
228 * @param[in] flags Flags of the parsed node with the when statement.
229 * @param[in] ctx_node Context node for the when statement.
230 * @param[out] when Pointer where to store pointer to the created compiled when structure.
231 * @return LY_ERR value.
232 */
233static LY_ERR
234lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
235 struct lysc_when **when)
236{
237 LY_ERR ret = LY_SUCCESS;
Radek Krejci2926c1b2021-03-16 14:45:45 +0100238 LY_PREFIX_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200239
240 *when = calloc(1, sizeof **when);
241 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
242 (*when)->refcount = 1;
243 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200244 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci2926c1b2021-03-16 14:45:45 +0100245 LY_PREF_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200246 (*when)->context = (struct lysc_node *)ctx_node;
247 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
248 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100249 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200250 (*when)->flags = flags & LYS_STATUS_MASK;
251
252done:
253 return ret;
254}
255
256LY_ERR
257lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
258 struct lysc_node *node, struct lysc_when **when_c)
259{
260 struct lysc_when **new_when, ***node_when;
261
262 assert(when_p);
263
264 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100265 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200266
267 /* create new when pointer */
268 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
269 if (!when_c || !(*when_c)) {
270 /* compile when */
271 LY_CHECK_RET(lys_compile_when_(ctx, when_p, flags, ctx_node, new_when));
272
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200273 /* remember the compiled when for sharing */
274 if (when_c) {
275 *when_c = *new_when;
276 }
277 } else {
278 /* use the previously compiled when */
279 ++(*when_c)->refcount;
280 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100281 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200282
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100283 if (!(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
284 /* do not check "when" semantics in a grouping, but repeat the check for different node because
285 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100286 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200287 }
288
289 return LY_SUCCESS;
290}
291
292/**
293 * @brief Compile information from the must statement
294 * @param[in] ctx Compile context.
295 * @param[in] must_p The parsed must statement structure.
296 * @param[in,out] must Prepared (empty) compiled must structure to fill.
297 * @return LY_ERR value.
298 */
299static LY_ERR
300lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
301{
302 LY_ERR ret = LY_SUCCESS;
Radek Krejci2926c1b2021-03-16 14:45:45 +0100303 LY_PREFIX_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200304
305 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200306 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
Radek Krejci2926c1b2021-03-16 14:45:45 +0100307 LY_PREF_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200308 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
309 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
310 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
311 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100312 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200313
314done:
315 return ret;
316}
317
318/**
319 * @brief Validate and normalize numeric value from a range definition.
320 * @param[in] ctx Compile context.
321 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
322 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
323 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
324 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
325 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
326 * @param[in] value String value of the range boundary.
327 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
328 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
329 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
330 */
331static LY_ERR
332range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
333 size_t *len, char **valcopy)
334{
335 size_t fraction = 0, size;
336
337 *len = 0;
338
339 assert(value);
340 /* parse value */
341 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
342 return LY_EVALID;
343 }
344
345 if ((value[*len] == '-') || (value[*len] == '+')) {
346 ++(*len);
347 }
348
349 while (isdigit(value[*len])) {
350 ++(*len);
351 }
352
353 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
354 if (basetype == LY_TYPE_DEC64) {
355 goto decimal;
356 } else {
357 *valcopy = strndup(value, *len);
358 return LY_SUCCESS;
359 }
360 }
361 fraction = *len;
362
363 ++(*len);
364 while (isdigit(value[*len])) {
365 ++(*len);
366 }
367
368 if (basetype == LY_TYPE_DEC64) {
369decimal:
370 assert(frdigits);
371 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100372 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200373 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100374 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200375 return LY_EINVAL;
376 }
377 if (fraction) {
378 size = (*len) + (frdigits - ((*len) - 1 - fraction));
379 } else {
380 size = (*len) + frdigits + 1;
381 }
382 *valcopy = malloc(size * sizeof **valcopy);
383 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
384
385 (*valcopy)[size - 1] = '\0';
386 if (fraction) {
387 memcpy(&(*valcopy)[0], &value[0], fraction);
388 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
389 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
390 } else {
391 memcpy(&(*valcopy)[0], &value[0], *len);
392 memset(&(*valcopy)[*len], '0', frdigits);
393 }
394 }
395 return LY_SUCCESS;
396}
397
398/**
399 * @brief Check that values in range are in ascendant order.
400 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
401 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
402 * max can be also equal.
403 * @param[in] value Current value to check.
404 * @param[in] prev_value The last seen value.
405 * @return LY_SUCCESS or LY_EEXIST for invalid order.
406 */
407static LY_ERR
408range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
409{
410 if (unsigned_value) {
411 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
412 return LY_EEXIST;
413 }
414 } else {
415 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
416 return LY_EEXIST;
417 }
418 }
419 return LY_SUCCESS;
420}
421
422/**
423 * @brief Set min/max value of the range part.
424 * @param[in] ctx Compile context.
425 * @param[in] part Range part structure to fill.
426 * @param[in] max Flag to distinguish if storing min or max value.
427 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
428 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
429 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
430 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
431 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
432 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
433 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
434 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
435 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
436 * frdigits value), LY_EMEM.
437 */
438static LY_ERR
439range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
440 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
441{
442 LY_ERR ret = LY_SUCCESS;
443 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100444 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200445
446 if (value) {
447 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
448 LY_CHECK_GOTO(ret, finalize);
449 }
450 if (!valcopy && base_range) {
451 if (max) {
452 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
453 } else {
454 part->min_64 = base_range->parts[0].min_64;
455 }
456 if (!first) {
457 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
458 }
459 goto finalize;
460 }
461
462 switch (basetype) {
463 case LY_TYPE_INT8: /* range */
464 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100465 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200466 } else if (max) {
467 part->max_64 = INT64_C(127);
468 } else {
469 part->min_64 = INT64_C(-128);
470 }
471 if (!ret && !first) {
472 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
473 }
474 break;
475 case LY_TYPE_INT16: /* range */
476 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100477 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
478 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200479 } else if (max) {
480 part->max_64 = INT64_C(32767);
481 } else {
482 part->min_64 = INT64_C(-32768);
483 }
484 if (!ret && !first) {
485 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
486 }
487 break;
488 case LY_TYPE_INT32: /* range */
489 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100490 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
491 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200492 } else if (max) {
493 part->max_64 = INT64_C(2147483647);
494 } else {
495 part->min_64 = INT64_C(-2147483648);
496 }
497 if (!ret && !first) {
498 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
499 }
500 break;
501 case LY_TYPE_INT64: /* range */
502 case LY_TYPE_DEC64: /* range */
503 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100504 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
505 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200506 } else if (max) {
507 part->max_64 = INT64_C(9223372036854775807);
508 } else {
509 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
510 }
511 if (!ret && !first) {
512 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
513 }
514 break;
515 case LY_TYPE_UINT8: /* range */
516 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100517 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200518 } else if (max) {
519 part->max_u64 = UINT64_C(255);
520 } else {
521 part->min_u64 = UINT64_C(0);
522 }
523 if (!ret && !first) {
524 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
525 }
526 break;
527 case LY_TYPE_UINT16: /* range */
528 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100529 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200530 } else if (max) {
531 part->max_u64 = UINT64_C(65535);
532 } else {
533 part->min_u64 = UINT64_C(0);
534 }
535 if (!ret && !first) {
536 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
537 }
538 break;
539 case LY_TYPE_UINT32: /* range */
540 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100541 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
542 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200543 } else if (max) {
544 part->max_u64 = UINT64_C(4294967295);
545 } else {
546 part->min_u64 = UINT64_C(0);
547 }
548 if (!ret && !first) {
549 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
550 }
551 break;
552 case LY_TYPE_UINT64: /* range */
553 case LY_TYPE_STRING: /* length */
554 case LY_TYPE_BINARY: /* length */
555 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100556 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
557 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200558 } else if (max) {
559 part->max_u64 = UINT64_C(18446744073709551615);
560 } else {
561 part->min_u64 = UINT64_C(0);
562 }
563 if (!ret && !first) {
564 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
565 }
566 break;
567 default:
568 LOGINT(ctx->ctx);
569 ret = LY_EINT;
570 }
571
572finalize:
573 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100574 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200575 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
576 length_restr ? "length" : "range", valcopy ? valcopy : *value);
577 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100578 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200579 "Invalid %s restriction - invalid value \"%s\".",
580 length_restr ? "length" : "range", valcopy ? valcopy : *value);
581 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100582 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200583 "Invalid %s restriction - values are not in ascending order (%s).",
584 length_restr ? "length" : "range",
585 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
586 } else if (!ret && value) {
587 *value = *value + len;
588 }
589 free(valcopy);
590 return ret;
591}
592
593/**
594 * @brief Compile the parsed range restriction.
595 * @param[in] ctx Compile context.
596 * @param[in] range_p Parsed range structure to compile.
597 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
598 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
599 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
600 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
601 * range restriction must be more restrictive than the base_range.
602 * @param[in,out] range Pointer to the created current range structure.
603 * @return LY_ERR value.
604 */
605static LY_ERR
606lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
607 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
608{
609 LY_ERR ret = LY_EVALID;
610 const char *expr;
611 struct lysc_range_part *parts = NULL, *part;
612 ly_bool range_expected = 0, uns;
613 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
614
615 assert(range);
616 assert(range_p);
617
618 expr = range_p->arg.str;
619 while (1) {
620 if (isspace(*expr)) {
621 ++expr;
622 } else if (*expr == '\0') {
623 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100624 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200625 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
626 length_restr ? "length" : "range", range_p->arg);
627 goto cleanup;
628 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100629 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200630 "Invalid %s restriction - unexpected end of the expression (%s).",
631 length_restr ? "length" : "range", range_p->arg);
632 goto cleanup;
633 }
634 parts_done++;
635 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100636 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200637 if (parts) {
638 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100639 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200640 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100641 (int)(expr - range_p->arg.str), range_p->arg.str);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200642 goto cleanup;
643 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100644 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200645
646 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
647 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
648 part->max_64 = part->min_64;
649 } else if (*expr == '|') {
650 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100651 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200652 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
653 goto cleanup;
654 }
655 expr++;
656 parts_done++;
657 /* process next part of the expression */
658 } else if (!strncmp(expr, "..", 2)) {
659 expr += 2;
660 while (isspace(*expr)) {
661 expr++;
662 }
663 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100664 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200665 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
666 goto cleanup;
667 }
668 /* continue expecting the upper boundary */
669 range_expected = 1;
670 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
671 /* number */
672 if (range_expected) {
673 part = &parts[LY_ARRAY_COUNT(parts) - 1];
674 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
675 range_expected = 0;
676 } else {
677 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
678 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
679 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
680 part->max_64 = part->min_64;
681 }
682
683 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100684 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
685 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200686 while (isspace(*expr)) {
687 expr++;
688 }
689 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100690 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200691 length_restr ? "length" : "range", expr);
692 goto cleanup;
693 }
694 if (range_expected) {
695 part = &parts[LY_ARRAY_COUNT(parts) - 1];
696 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
697 range_expected = 0;
698 } else {
699 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
700 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
701 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
702 part->min_64 = part->max_64;
703 }
704 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100705 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200706 length_restr ? "length" : "range", expr);
707 goto cleanup;
708 }
709 }
710
711 /* check with the previous range/length restriction */
712 if (base_range) {
713 switch (basetype) {
714 case LY_TYPE_BINARY:
715 case LY_TYPE_UINT8:
716 case LY_TYPE_UINT16:
717 case LY_TYPE_UINT32:
718 case LY_TYPE_UINT64:
719 case LY_TYPE_STRING:
720 uns = 1;
721 break;
722 case LY_TYPE_DEC64:
723 case LY_TYPE_INT8:
724 case LY_TYPE_INT16:
725 case LY_TYPE_INT32:
726 case LY_TYPE_INT64:
727 uns = 0;
728 break;
729 default:
730 LOGINT(ctx->ctx);
731 ret = LY_EINT;
732 goto cleanup;
733 }
734 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
735 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
736 goto baseerror;
737 }
738 /* current lower bound is not lower than the base */
739 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
740 /* base has single value */
741 if (base_range->parts[v].min_64 == parts[u].min_64) {
742 /* both lower bounds are the same */
743 if (parts[u].min_64 != parts[u].max_64) {
744 /* current continues with a range */
745 goto baseerror;
746 } else {
747 /* equal single values, move both forward */
748 ++v;
749 continue;
750 }
751 } else {
752 /* base is single value lower than current range, so the
753 * value from base range is removed in the current,
754 * move only base and repeat checking */
755 ++v;
756 --u;
757 continue;
758 }
759 } else {
760 /* base is the range */
761 if (parts[u].min_64 == parts[u].max_64) {
762 /* current is a single value */
763 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
764 /* current is behind the base range, so base range is omitted,
765 * move the base and keep the current for further check */
766 ++v;
767 --u;
768 } /* else it is within the base range, so move the current, but keep the base */
769 continue;
770 } else {
771 /* both are ranges - check the higher bound, the lower was already checked */
772 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
773 /* higher bound is higher than the current higher bound */
774 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
775 /* but the current lower bound is also higher, so the base range is omitted,
776 * continue with the same current, but move the base */
777 --u;
778 ++v;
779 continue;
780 }
781 /* current range starts within the base range but end behind it */
782 goto baseerror;
783 } else {
784 /* current range is smaller than the base,
785 * move current, but stay with the base */
786 continue;
787 }
788 }
789 }
790 }
791 if (u != parts_done) {
792baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100793 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200794 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
795 length_restr ? "length" : "range", range_p->arg);
796 goto cleanup;
797 }
798 }
799
800 if (!(*range)) {
801 *range = calloc(1, sizeof **range);
802 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
803 }
804
805 /* we rewrite the following values as the types chain is being processed */
806 if (range_p->eapptag) {
807 lydict_remove(ctx->ctx, (*range)->eapptag);
808 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
809 }
810 if (range_p->emsg) {
811 lydict_remove(ctx->ctx, (*range)->emsg);
812 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
813 }
814 if (range_p->dsc) {
815 lydict_remove(ctx->ctx, (*range)->dsc);
816 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
817 }
818 if (range_p->ref) {
819 lydict_remove(ctx->ctx, (*range)->ref);
820 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
821 }
822 /* extensions are taken only from the last range by the caller */
823
824 (*range)->parts = parts;
825 parts = NULL;
826 ret = LY_SUCCESS;
827cleanup:
828 LY_ARRAY_FREE(parts);
829
830 return ret;
831}
832
833LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100834lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200835{
836 size_t idx, idx2, start, end, size, brack;
837 char *perl_regex, *ptr;
Christian Hoppsdafed222021-03-22 13:02:42 -0400838 int err_code, compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200839 const char *orig_ptr;
840 PCRE2_SIZE err_offset;
841 pcre2_code *code_local;
842
843#define URANGE_LEN 19
844 char *ublock2urange[][2] = {
845 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
846 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
847 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
848 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
849 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
850 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
851 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
852 {"Greek", "[\\x{0370}-\\x{03FF}]"},
853 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
854 {"Armenian", "[\\x{0530}-\\x{058F}]"},
855 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
856 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
857 {"Syriac", "[\\x{0700}-\\x{074F}]"},
858 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
859 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
860 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
861 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
862 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
863 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
864 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
865 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
866 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
867 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
868 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
869 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
870 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
871 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
872 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
873 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
874 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
875 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
876 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
877 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
878 {"Ogham", "[\\x{1680}-\\x{169F}]"},
879 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
880 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
881 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
882 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
883 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
884 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
885 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
886 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
887 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
888 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
889 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
890 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
891 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
892 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
893 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
894 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
895 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
896 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
897 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
898 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
899 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
900 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
901 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
902 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
903 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
904 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
905 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
906 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
907 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
908 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
909 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
910 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
911 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
912 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
913 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
914 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
915 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
916 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
917 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
918 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
919 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
920 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
921 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
922 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
923 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
924 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
925 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
926 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
927 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100928 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200929 {NULL, NULL}
930 };
931
932 /* adjust the expression to a Perl equivalent
933 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
934
935 /* allocate space for the transformed pattern */
936 size = strlen(pattern) + 1;
Christian Hoppsdafed222021-03-22 13:02:42 -0400937 compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
938#ifdef PCRE2_ENDANCHORED
939 compile_opts |= PCRE2_ENDANCHORED;
940#else
941 /* add space for trailing $ anchor */
942 size++;
943#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200944 perl_regex = malloc(size);
945 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
946 perl_regex[0] = '\0';
947
948 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
949 brack = 0;
950 idx = 0;
951 orig_ptr = pattern;
952 while (orig_ptr[0]) {
953 switch (orig_ptr[0]) {
954 case '$':
955 case '^':
956 if (!brack) {
957 /* make space for the extra character */
958 ++size;
959 perl_regex = ly_realloc(perl_regex, size);
960 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
961
962 /* print escape slash */
963 perl_regex[idx] = '\\';
964 ++idx;
965 }
966 break;
967 case '[':
968 /* must not be escaped */
969 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
970 ++brack;
971 }
972 break;
973 case ']':
974 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
975 /* pattern was checked and compiled already */
976 assert(brack);
977 --brack;
978 }
979 break;
980 default:
981 break;
982 }
983
984 /* copy char */
985 perl_regex[idx] = orig_ptr[0];
986
987 ++idx;
988 ++orig_ptr;
989 }
Christian Hoppsdafed222021-03-22 13:02:42 -0400990#ifndef PCRE2_ENDANCHORED
991 /* anchor match to end of subject */
992 perl_regex[idx++] = '$';
993#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200994 perl_regex[idx] = '\0';
995
996 /* substitute Unicode Character Blocks with exact Character Ranges */
997 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
998 start = ptr - perl_regex;
999
1000 ptr = strchr(ptr, '}');
1001 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001002 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001003 free(perl_regex);
1004 return LY_EVALID;
1005 }
1006 end = (ptr - perl_regex) + 1;
1007
1008 /* need more space */
1009 if (end - start < URANGE_LEN) {
1010 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1011 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1012 }
1013
1014 /* find our range */
1015 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001016 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1017 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001018 break;
1019 }
1020 }
1021 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001022 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001023 free(perl_regex);
1024 return LY_EVALID;
1025 }
1026
1027 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1028 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1029 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1030 ++idx;
1031 }
1032 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1033 --idx;
1034 }
1035 }
1036 if (idx) {
1037 /* skip brackets */
1038 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1039 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1040 } else {
1041 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1042 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1043 }
1044 }
1045
1046 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001047 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001048 &err_code, &err_offset, NULL);
1049 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001050 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1051 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001052 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001053 free(perl_regex);
1054 return LY_EVALID;
1055 }
1056 free(perl_regex);
1057
1058 if (code) {
1059 *code = code_local;
1060 } else {
1061 free(code_local);
1062 }
1063
1064 return LY_SUCCESS;
1065
1066#undef URANGE_LEN
1067}
1068
1069/**
1070 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1071 * @param[in] ctx Compile context.
1072 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1073 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1074 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1075 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1076 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1077 */
1078static LY_ERR
1079lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1080 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1081{
1082 struct lysc_pattern **pattern;
1083 LY_ARRAY_COUNT_TYPE u;
1084 LY_ERR ret = LY_SUCCESS;
1085
1086 /* first, copy the patterns from the base type */
1087 if (base_patterns) {
1088 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1089 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1090 }
1091
1092 LY_ARRAY_FOR(patterns_p, u) {
1093 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1094 *pattern = calloc(1, sizeof **pattern);
1095 ++(*pattern)->refcount;
1096
Radek Krejci2efc45b2020-12-22 16:25:44 +01001097 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001098 LY_CHECK_RET(ret);
1099
Radek Krejcif13b87b2020-12-01 22:02:17 +01001100 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001101 (*pattern)->inverted = 1;
1102 }
1103 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1104 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1105 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1106 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1107 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001108 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001109 }
1110done:
1111 return ret;
1112}
1113
1114/**
1115 * @brief map of the possible restrictions combination for the specific built-in type.
1116 */
1117static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1118 0 /* LY_TYPE_UNKNOWN */,
1119 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1120 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1121 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1122 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1123 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1124 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1125 LYS_SET_BIT /* LY_TYPE_BITS */,
1126 0 /* LY_TYPE_BOOL */,
1127 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1128 0 /* LY_TYPE_EMPTY */,
1129 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1130 LYS_SET_BASE /* LY_TYPE_IDENT */,
1131 LYS_SET_REQINST /* LY_TYPE_INST */,
1132 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1133 LYS_SET_TYPE /* LY_TYPE_UNION */,
1134 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1135 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1136 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1137 LYS_SET_RANGE /* LY_TYPE_INT64 */
1138};
1139
1140/**
1141 * @brief stringification of the YANG built-in data types
1142 */
1143const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001144 LY_TYPE_UNKNOWN_STR,
1145 LY_TYPE_BINARY_STR,
1146 LY_TYPE_UINT8_STR,
1147 LY_TYPE_UINT16_STR,
1148 LY_TYPE_UINT32_STR,
1149 LY_TYPE_UINT64_STR,
1150 LY_TYPE_STRING_STR,
1151 LY_TYPE_BITS_STR,
1152 LY_TYPE_BOOL_STR,
1153 LY_TYPE_DEC64_STR,
1154 LY_TYPE_EMPTY_STR,
1155 LY_TYPE_ENUM_STR,
1156 LY_TYPE_IDENT_STR,
1157 LY_TYPE_INST_STR,
1158 LY_TYPE_LEAFREF_STR,
1159 LY_TYPE_UNION_STR,
1160 LY_TYPE_INT8_STR,
1161 LY_TYPE_INT16_STR,
1162 LY_TYPE_INT32_STR,
1163 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001164};
1165
1166/**
1167 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1168 * @param[in] ctx Compile context.
1169 * @param[in] enums_p Array of the parsed enum structures to compile.
1170 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1171 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001172 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001173 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1174 */
1175static LY_ERR
1176lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001177 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001178{
1179 LY_ERR ret = LY_SUCCESS;
1180 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001181 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1182 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001183 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001184 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001185
1186 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001187 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001188 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1189 return LY_EVALID;
1190 }
1191
1192 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001193 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001194 if (base_enums) {
1195 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1196 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001197 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001198 break;
1199 }
1200 }
1201 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001202 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001203 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001204 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001205 return LY_EVALID;
1206 }
1207 match = v;
1208 }
1209
1210 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001211 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001212 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001213 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001214 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001215 LY_ARRAY_FOR(*bitenums, v) {
1216 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001217 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001218 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001219 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 return LY_EVALID;
1221 }
1222 }
1223 } else if (base_enums) {
1224 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001225 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001226 } else {
1227 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001228 if (u == 0) {
1229 cur_val = 0;
1230 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001231 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001232 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001233 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001234 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001235 } else {
1236 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001237 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001238 }
1239
1240 /* save highest value for auto assing */
1241 if (highest_value < cur_val) {
1242 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001243 }
1244 } else { /* LY_TYPE_BITS */
1245 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001246 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001247 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001248 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001249 LY_ARRAY_FOR(*bitenums, v) {
1250 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001251 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001252 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001253 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 return LY_EVALID;
1255 }
1256 }
1257 } else if (base_enums) {
1258 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001259 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 } else {
1261 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001262 if (u == 0) {
1263 cur_pos = 0;
1264 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001265 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001266 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001267 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001268 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001269 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001270 } else {
1271 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001272 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001273 }
1274
1275 /* save highest position for auto assing */
1276 if (highest_position < cur_pos) {
1277 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001278 }
1279 }
1280
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001281 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001282 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001283 if (basetype == LY_TYPE_ENUM) {
1284 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001285 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001286 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001287 enums_p[u].name, base_enums[match].value, cur_val);
1288 return LY_EVALID;
1289 }
1290 } else {
1291 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001292 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001293 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001294 enums_p[u].name, base_enums[match].position, cur_pos);
1295 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001296 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001297 }
1298 }
1299
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001300 /* evaluate if-ffeatures */
1301 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1302 if (!enabled) {
1303 continue;
1304 }
1305
1306 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001307 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001308 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1309 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1310 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001311 e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) | (basetype == LY_TYPE_ENUM ? LYS_IS_ENUM : 0);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001312 if (basetype == LY_TYPE_ENUM) {
1313 e->value = cur_val;
1314 } else {
1315 e->position = cur_pos;
1316 }
Radek Krejciab430862021-03-02 20:13:40 +01001317 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001318
1319 if (basetype == LY_TYPE_BITS) {
1320 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001321 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001322 if (v != u) {
1323 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001324 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1325 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001326 }
1327 }
1328 }
1329
1330done:
1331 return ret;
1332}
1333
Radek Krejci6a205692020-12-09 13:58:22 +01001334static LY_ERR
1335lys_compile_type_union(struct lysc_ctx *ctx, struct lysp_type *ptypes, struct lysp_node *context_pnode, uint16_t context_flags,
Michal Vaskoa99b3572021-02-01 11:54:58 +01001336 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001337{
1338 LY_ERR ret = LY_SUCCESS;
1339 struct lysc_type **utypes = *utypes_p;
1340 struct lysc_type_union *un_aux = NULL;
1341
1342 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1343 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001344 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1345 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001346 LY_CHECK_GOTO(ret, error);
1347 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1348 /* add space for additional types from the union subtype */
1349 un_aux = (struct lysc_type_union *)utypes[u + additional];
1350 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1351 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1352
1353 /* copy subtypes of the subtype union */
1354 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1355 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1356 struct lysc_type_leafref *lref;
1357
1358 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1359 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1360 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1361 lref = (struct lysc_type_leafref *)utypes[u + additional];
1362
1363 lref->basetype = LY_TYPE_LEAFREF;
1364 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1365 LY_CHECK_GOTO(ret, error);
1366 lref->refcount = 1;
1367 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
1368 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci0b013302021-03-29 15:22:32 +02001369 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_PREF_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001370 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001371 LY_CHECK_GOTO(ret, error);
1372 /* TODO extensions */
1373
1374 } else {
1375 utypes[u + additional] = un_aux->types[v];
1376 ++un_aux->types[v]->refcount;
1377 }
1378 ++additional;
1379 LY_ARRAY_INCREMENT(utypes);
1380 }
1381 /* compensate u increment in main loop */
1382 --additional;
1383
1384 /* free the replaced union subtype */
1385 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1386 un_aux = NULL;
1387 } else {
1388 LY_ARRAY_INCREMENT(utypes);
1389 }
1390 }
1391
1392 *utypes_p = utypes;
1393 return LY_SUCCESS;
1394
1395error:
1396 if (un_aux) {
1397 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1398 }
1399 *utypes_p = utypes;
1400 return ret;
1401}
1402
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001403/**
1404 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1405 * @param[in] ctx Compile context.
1406 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1407 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001408 * @param[in] context_name Name of the context node or referencing typedef for logging.
1409 * @param[in] type_p Parsed type to compile.
1410 * @param[in] basetype Base YANG built-in type of the type to compile.
1411 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1412 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1413 * @param[out] type Newly created type structure with the filled information about the type.
1414 * @return LY_ERR value.
1415 */
1416static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001417lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1418 struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001419{
1420 LY_ERR ret = LY_SUCCESS;
1421 struct lysc_type_bin *bin;
1422 struct lysc_type_num *num;
1423 struct lysc_type_str *str;
1424 struct lysc_type_bits *bits;
1425 struct lysc_type_enum *enumeration;
1426 struct lysc_type_dec *dec;
1427 struct lysc_type_identityref *idref;
1428 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001429 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001430
1431 switch (basetype) {
1432 case LY_TYPE_BINARY:
1433 bin = (struct lysc_type_bin *)(*type);
1434
1435 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1436 if (type_p->length) {
1437 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1438 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1439 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001440 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001441 }
1442 }
1443 break;
1444 case LY_TYPE_BITS:
1445 /* RFC 7950 9.7 - bits */
1446 bits = (struct lysc_type_bits *)(*type);
1447 if (type_p->bits) {
1448 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1449 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1450 (struct lysc_type_bitenum_item **)&bits->bits));
1451 }
1452
1453 if (!base && !type_p->flags) {
1454 /* type derived from bits built-in type must contain at least one bit */
1455 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001456 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001457 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001458 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001459 }
1460 return LY_EVALID;
1461 }
1462 break;
1463 case LY_TYPE_DEC64:
1464 dec = (struct lysc_type_dec *)(*type);
1465
1466 /* RFC 7950 9.3.4 - fraction-digits */
1467 if (!base) {
1468 if (!type_p->fraction_digits) {
1469 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001470 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001471 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001472 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001473 }
1474 return LY_EVALID;
1475 }
1476 dec->fraction_digits = type_p->fraction_digits;
1477 } else {
1478 if (type_p->fraction_digits) {
1479 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1480 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001481 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001482 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1483 tpdfname);
1484 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001485 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001486 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1487 }
1488 return LY_EVALID;
1489 }
1490 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1491 }
1492
1493 /* RFC 7950 9.2.4 - range */
1494 if (type_p->range) {
1495 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1496 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1497 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001498 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001499 }
1500 }
1501 break;
1502 case LY_TYPE_STRING:
1503 str = (struct lysc_type_str *)(*type);
1504
1505 /* RFC 7950 9.4.4 - length */
1506 if (type_p->length) {
1507 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1508 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1509 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001510 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001511 }
1512 } else if (base && ((struct lysc_type_str *)base)->length) {
1513 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1514 }
1515
1516 /* RFC 7950 9.4.5 - pattern */
1517 if (type_p->patterns) {
1518 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1519 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1520 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1521 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1522 }
1523 break;
1524 case LY_TYPE_ENUM:
1525 enumeration = (struct lysc_type_enum *)(*type);
1526
1527 /* RFC 7950 9.6 - enum */
1528 if (type_p->enums) {
1529 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1530 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1531 }
1532
1533 if (!base && !type_p->flags) {
1534 /* type derived from enumerations built-in type must contain at least one enum */
1535 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001536 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001537 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001538 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001539 }
1540 return LY_EVALID;
1541 }
1542 break;
1543 case LY_TYPE_INT8:
1544 case LY_TYPE_UINT8:
1545 case LY_TYPE_INT16:
1546 case LY_TYPE_UINT16:
1547 case LY_TYPE_INT32:
1548 case LY_TYPE_UINT32:
1549 case LY_TYPE_INT64:
1550 case LY_TYPE_UINT64:
1551 num = (struct lysc_type_num *)(*type);
1552
1553 /* RFC 6020 9.2.4 - range */
1554 if (type_p->range) {
1555 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1556 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1557 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001558 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001559 }
1560 }
1561 break;
1562 case LY_TYPE_IDENT:
1563 idref = (struct lysc_type_identityref *)(*type);
1564
1565 /* RFC 7950 9.10.2 - base */
1566 if (type_p->bases) {
1567 if (base) {
1568 /* only the directly derived identityrefs can contain base specification */
1569 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001570 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001571 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1572 tpdfname);
1573 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001574 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001575 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1576 }
1577 return LY_EVALID;
1578 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001579 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001580 }
1581
1582 if (!base && !type_p->flags) {
1583 /* type derived from identityref built-in type must contain at least one base */
1584 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001585 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001586 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001587 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001588 }
1589 return LY_EVALID;
1590 }
1591 break;
1592 case LY_TYPE_LEAFREF:
1593 lref = (struct lysc_type_leafref *)*type;
1594
1595 /* RFC 7950 9.9.3 - require-instance */
1596 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001597 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001598 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001599 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001600 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1601 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001602 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001603 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1604 }
1605 return LY_EVALID;
1606 }
1607 lref->require_instance = type_p->require_instance;
1608 } else if (base) {
1609 /* inherit */
1610 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1611 } else {
1612 /* default is true */
1613 lref->require_instance = 1;
1614 }
1615 if (type_p->path) {
Radek Krejci2926c1b2021-03-16 14:45:45 +01001616 LY_PREFIX_FORMAT format;
1617
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001618 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001619 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr),
Radek Krejci2926c1b2021-03-16 14:45:45 +01001620 LY_PREF_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001621 } else if (base) {
1622 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001623 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_PREF_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001624 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001625 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001626 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001627 return LY_EVALID;
1628 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001629 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001630 return LY_EVALID;
1631 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001632 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001633 break;
1634 case LY_TYPE_INST:
1635 /* RFC 7950 9.9.3 - require-instance */
1636 if (type_p->flags & LYS_SET_REQINST) {
1637 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1638 } else {
1639 /* default is true */
1640 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1641 }
1642 break;
1643 case LY_TYPE_UNION:
1644 un = (struct lysc_type_union *)(*type);
1645
1646 /* RFC 7950 7.4 - type */
1647 if (type_p->types) {
1648 if (base) {
1649 /* only the directly derived union can contain types specification */
1650 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001651 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001652 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1653 tpdfname);
1654 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001655 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001656 "Invalid type substatement for the type not directly derived from union built-in type.");
1657 }
1658 return LY_EVALID;
1659 }
1660 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001661 LY_CHECK_RET(lys_compile_type_union(ctx, type_p->types, context_pnode, context_flags, context_name, &un->types));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001662 }
1663
1664 if (!base && !type_p->flags) {
1665 /* type derived from union built-in type must contain at least one type */
1666 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001667 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001668 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001669 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001670 }
1671 return LY_EVALID;
1672 }
1673 break;
1674 case LY_TYPE_BOOL:
1675 case LY_TYPE_EMPTY:
1676 case LY_TYPE_UNKNOWN: /* just to complete switch */
1677 break;
1678 }
1679
1680 if (tpdfname) {
1681 switch (basetype) {
1682 case LY_TYPE_BINARY:
1683 type_p->compiled = *type;
1684 *type = calloc(1, sizeof(struct lysc_type_bin));
1685 break;
1686 case LY_TYPE_BITS:
1687 type_p->compiled = *type;
1688 *type = calloc(1, sizeof(struct lysc_type_bits));
1689 break;
1690 case LY_TYPE_DEC64:
1691 type_p->compiled = *type;
1692 *type = calloc(1, sizeof(struct lysc_type_dec));
1693 break;
1694 case LY_TYPE_STRING:
1695 type_p->compiled = *type;
1696 *type = calloc(1, sizeof(struct lysc_type_str));
1697 break;
1698 case LY_TYPE_ENUM:
1699 type_p->compiled = *type;
1700 *type = calloc(1, sizeof(struct lysc_type_enum));
1701 break;
1702 case LY_TYPE_INT8:
1703 case LY_TYPE_UINT8:
1704 case LY_TYPE_INT16:
1705 case LY_TYPE_UINT16:
1706 case LY_TYPE_INT32:
1707 case LY_TYPE_UINT32:
1708 case LY_TYPE_INT64:
1709 case LY_TYPE_UINT64:
1710 type_p->compiled = *type;
1711 *type = calloc(1, sizeof(struct lysc_type_num));
1712 break;
1713 case LY_TYPE_IDENT:
1714 type_p->compiled = *type;
1715 *type = calloc(1, sizeof(struct lysc_type_identityref));
1716 break;
1717 case LY_TYPE_LEAFREF:
1718 type_p->compiled = *type;
1719 *type = calloc(1, sizeof(struct lysc_type_leafref));
1720 break;
1721 case LY_TYPE_INST:
1722 type_p->compiled = *type;
1723 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1724 break;
1725 case LY_TYPE_UNION:
1726 type_p->compiled = *type;
1727 *type = calloc(1, sizeof(struct lysc_type_union));
1728 break;
1729 case LY_TYPE_BOOL:
1730 case LY_TYPE_EMPTY:
1731 case LY_TYPE_UNKNOWN: /* just to complete switch */
1732 break;
1733 }
1734 }
1735 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1736
1737cleanup:
1738 return ret;
1739}
1740
Radek Krejcibf940f92021-03-24 21:04:13 +01001741/**
1742 * @brief Find the correct plugin implementing the described type
1743 *
1744 * @param[in] mod Module where the type is defined
1745 * @param[in] name Name of the type (typedef)
1746 * @param[in] basetype Type's basetype (when the built-in base plugin is supposed to be used)
1747 * @return Pointer to the plugin implementing the described data type.
1748 */
1749static struct lyplg_type *
1750lys_compile_type_get_plugin(struct lys_module *mod, const char *name, LY_DATA_TYPE basetype)
1751{
1752 struct lyplg_type *p;
1753
1754 /* try to find loaded user type plugins */
1755 p = lyplg_find(LYPLG_TYPE, mod->name, mod->revision, name);
1756 if (!p) {
1757 /* use the internal built-in type implementation */
1758 p = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
1759 }
1760
1761 return p;
1762}
1763
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001764LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001765lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1766 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001767{
1768 LY_ERR ret = LY_SUCCESS;
1769 ly_bool dummyloops = 0;
1770 struct type_context {
1771 const struct lysp_tpdf *tpdf;
1772 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001773 } *tctx, *tctx_prev = NULL, *tctx_iter;
1774 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1775 struct lysc_type *base = NULL, *prev_type;
1776 struct ly_set tpdf_chain = {0};
1777
1778 (*type) = NULL;
1779 if (dflt) {
1780 *dflt = NULL;
1781 }
1782
1783 tctx = calloc(1, sizeof *tctx);
1784 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001785 for (ret = lysp_type_find(type_p->name, context_pnode, type_p->pmod, &basetype, &tctx->tpdf, &tctx->node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001786 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001787 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1788 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001789 if (basetype) {
1790 break;
1791 }
1792
1793 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001794 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1795 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001796 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1797
1798 if (units && !*units) {
1799 /* inherit units */
1800 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1801 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1802 }
1803 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1804 /* inherit default */
1805 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1806 }
1807 if (dummyloops && (!units || *units) && dflt && *dflt) {
1808 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1809 break;
1810 }
1811
Radek Krejci720d2612021-03-03 19:44:22 +01001812 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1813 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1814 * and we need now recompile the type again in the updated context. */
1815 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1816 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1817 }
1818
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001819 if (tctx->tpdf->type.compiled) {
1820 /* it is not necessary to continue, the rest of the chain was already compiled,
1821 * but we still may need to inherit default and units values, so start dummy loops */
1822 basetype = tctx->tpdf->type.compiled->basetype;
1823 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1824 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1825
1826 if ((units && !*units) || (dflt && !*dflt)) {
1827 dummyloops = 1;
1828 goto preparenext;
1829 } else {
1830 tctx = NULL;
1831 break;
1832 }
1833 }
1834
1835 /* circular typedef reference detection */
1836 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1837 /* local part */
1838 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001839 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001840 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001841 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1842 free(tctx);
1843 ret = LY_EVALID;
1844 goto cleanup;
1845 }
1846 }
1847 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1848 /* global part for unions corner case */
1849 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001850 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001851 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001852 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1853 free(tctx);
1854 ret = LY_EVALID;
1855 goto cleanup;
1856 }
1857 }
1858
1859 /* store information for the following processing */
1860 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1861 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1862
1863preparenext:
1864 /* prepare next loop */
1865 tctx_prev = tctx;
1866 tctx = calloc(1, sizeof *tctx);
1867 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1868 }
1869 free(tctx);
1870
1871 /* allocate type according to the basetype */
1872 switch (basetype) {
1873 case LY_TYPE_BINARY:
1874 *type = calloc(1, sizeof(struct lysc_type_bin));
1875 break;
1876 case LY_TYPE_BITS:
1877 *type = calloc(1, sizeof(struct lysc_type_bits));
1878 break;
1879 case LY_TYPE_BOOL:
1880 case LY_TYPE_EMPTY:
1881 *type = calloc(1, sizeof(struct lysc_type));
1882 break;
1883 case LY_TYPE_DEC64:
1884 *type = calloc(1, sizeof(struct lysc_type_dec));
1885 break;
1886 case LY_TYPE_ENUM:
1887 *type = calloc(1, sizeof(struct lysc_type_enum));
1888 break;
1889 case LY_TYPE_IDENT:
1890 *type = calloc(1, sizeof(struct lysc_type_identityref));
1891 break;
1892 case LY_TYPE_INST:
1893 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1894 break;
1895 case LY_TYPE_LEAFREF:
1896 *type = calloc(1, sizeof(struct lysc_type_leafref));
1897 break;
1898 case LY_TYPE_STRING:
1899 *type = calloc(1, sizeof(struct lysc_type_str));
1900 break;
1901 case LY_TYPE_UNION:
1902 *type = calloc(1, sizeof(struct lysc_type_union));
1903 break;
1904 case LY_TYPE_INT8:
1905 case LY_TYPE_UINT8:
1906 case LY_TYPE_INT16:
1907 case LY_TYPE_UINT16:
1908 case LY_TYPE_INT32:
1909 case LY_TYPE_UINT32:
1910 case LY_TYPE_INT64:
1911 case LY_TYPE_UINT64:
1912 *type = calloc(1, sizeof(struct lysc_type_num));
1913 break;
1914 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001915 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001916 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1917 ret = LY_EVALID;
1918 goto cleanup;
1919 }
1920 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1921 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001922 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001923 ly_data_type2str[basetype]);
1924 free(*type);
1925 (*type) = NULL;
1926 ret = LY_EVALID;
1927 goto cleanup;
1928 }
1929
1930 /* get restrictions from the referred typedefs */
1931 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1932 tctx = (struct type_context *)tpdf_chain.objs[u];
1933
1934 /* remember the typedef context for circular check */
1935 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1936 LY_CHECK_GOTO(ret, cleanup);
1937
1938 if (tctx->tpdf->type.compiled) {
1939 base = tctx->tpdf->type.compiled;
1940 continue;
1941 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1942 /* no change, just use the type information from the base */
1943 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1944 ++base->refcount;
1945 continue;
1946 }
1947
1948 ++(*type)->refcount;
1949 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001950 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001951 tctx->tpdf->name, ly_data_type2str[basetype]);
1952 ret = LY_EVALID;
1953 goto cleanup;
1954 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001955 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001956 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1957 tctx->tpdf->name, tctx->tpdf->dflt.str);
1958 ret = LY_EVALID;
1959 goto cleanup;
1960 }
1961
1962 (*type)->basetype = basetype;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001963 /* collect extensions */
1964 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
1965 /* get plugin for the type */
Radek Krejcibf940f92021-03-24 21:04:13 +01001966 (*type)->plugin = lys_compile_type_get_plugin(tctx->tpdf->type.pmod->mod, tctx->tpdf->name, basetype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001967 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001968 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001969 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1970 LY_CHECK_GOTO(ret, cleanup);
1971 base = prev_type;
1972 }
1973 /* remove the processed typedef contexts from the stack for circular check */
1974 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1975
1976 /* process the type definition in leaf */
1977 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1978 /* get restrictions from the node itself */
1979 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01001980 (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001981 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001982 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001983 LY_CHECK_GOTO(ret, cleanup);
1984 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1985 /* no specific restriction in leaf's type definition, copy from the base */
1986 free(*type);
1987 (*type) = base;
1988 ++(*type)->refcount;
1989 }
1990
Radek Krejciab430862021-03-02 20:13:40 +01001991 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001992
1993cleanup:
1994 ly_set_erase(&tpdf_chain, free);
1995 return ret;
1996}
1997
1998/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01001999 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
2000 */
2001#define LYS_STATUS_USES LYS_CONFIG_MASK
2002
2003/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002004 * @brief Compile status information of the given node.
2005 *
2006 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
2007 * has the status correctly set during the compilation.
2008 *
2009 * @param[in] ctx Compile context
2010 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
2011 * If the status was set explicitly on the node, it is already set in the flags value and we just check
2012 * the compatibility with the parent's status value.
2013 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
2014 * @return LY_ERR value.
2015 */
2016static LY_ERR
2017lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
2018{
2019 /* status - it is not inherited by specification, but it does not make sense to have
2020 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2021 if (!((*node_flags) & LYS_STATUS_MASK)) {
2022 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002023 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002024 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01002025 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002026 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2027 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2028 }
2029 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
2030 } else {
2031 (*node_flags) |= LYS_STATUS_CURR;
2032 }
2033 } else if (parent_flags & LYS_STATUS_MASK) {
2034 /* check status compatibility with the parent */
2035 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
2036 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002037 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002038 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2039 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2040 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002041 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002042 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2043 }
2044 return LY_EVALID;
2045 }
2046 }
2047 return LY_SUCCESS;
2048}
2049
2050/**
2051 * @brief Check uniqness of the node/action/notification name.
2052 *
2053 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2054 * structures, but they share the namespace so we need to check their name collisions.
2055 *
2056 * @param[in] ctx Compile context.
2057 * @param[in] parent Parent of the nodes to check, can be NULL.
2058 * @param[in] name Name of the item to find in the given lists.
2059 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2060 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2061 */
2062static LY_ERR
2063lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2064 const struct lysc_node *exclude)
2065{
2066 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002067 const struct lysc_node_action *actions;
2068 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002069 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002070 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002071
2072#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2073
2074 if (exclude->nodetype == LYS_CASE) {
2075 /* check restricted only to all the cases */
2076 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002077 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002078 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002079 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002080 return LY_EEXIST;
2081 }
2082 }
2083
2084 return LY_SUCCESS;
2085 }
2086
2087 /* no reason for our parent to be choice anymore */
2088 assert(!parent || (parent->nodetype != LYS_CHOICE));
2089
2090 if (parent && (parent->nodetype == LYS_CASE)) {
2091 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002092
2093 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2094 iter = lysc_data_parent(parent);
2095 do {
2096 parent = parent->parent;
2097 if (parent && (parent->nodetype == LYS_CHOICE)) {
2098 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2099 }
2100 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002101 }
2102
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002103 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002104 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2105 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2106 if (exclude->flags & LYS_IS_OUTPUT) {
2107 getnext_flags |= LYS_GETNEXT_OUTPUT;
2108 parent = lysc_node_child(parent)->next;
2109 } else {
2110 parent = lysc_node_child(parent);
2111 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002112 }
2113
2114 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002115 if (!parent && ctx->ext) {
2116 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2117 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2118 goto error;
2119 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002120
Radek Krejci5fa32a32021-02-08 18:12:38 +01002121 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2122 if (iter->nodetype == LYS_CHOICE) {
2123 iter2 = NULL;
2124 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2125 if (CHECK_NODE(iter2, exclude, name)) {
2126 goto error;
2127 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002128 }
2129 }
2130 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002131 } else {
2132 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2133 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2134 goto error;
2135 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002136
Radek Krejci5fa32a32021-02-08 18:12:38 +01002137 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2138 if (iter->nodetype == LYS_CHOICE) {
2139 iter2 = NULL;
2140 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2141 if (CHECK_NODE(iter2, exclude, name)) {
2142 goto error;
2143 }
2144 }
2145 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002146 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002147
Radek Krejci5fa32a32021-02-08 18:12:38 +01002148 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2149 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2150 if (CHECK_NODE(iter, exclude, name)) {
2151 goto error;
2152 }
2153 }
2154
2155 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2156 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2157 if (CHECK_NODE(iter, exclude, name)) {
2158 goto error;
2159 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002160 }
2161 }
Radek Krejci078e4342021-02-12 18:17:26 +01002162 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002163 return LY_SUCCESS;
2164
2165error:
Radek Krejci078e4342021-02-12 18:17:26 +01002166 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002167 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002168 return LY_EEXIST;
2169
2170#undef CHECK_NODE
2171}
2172
Radek Krejci2a9fc652021-01-22 17:44:34 +01002173/**
2174 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2175 * keep specific order of augments targetting the same node.
2176 *
2177 * @param[in] ctx Compile context
2178 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2179 * the choice itself is expected instead of a specific case node.
2180 * @param[in] node Schema node to connect into the list.
2181 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2182 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2183 */
2184static LY_ERR
2185lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002186{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002187 struct lysc_node **children, *anchor = NULL;
2188 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002189
Radek Krejci2a9fc652021-01-22 17:44:34 +01002190 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002191
Radek Krejci2a9fc652021-01-22 17:44:34 +01002192 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002193 if (node->nodetype == LYS_INPUT) {
2194 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2195 /* input node is part of the action but link it with output */
2196 node->next = &((struct lysc_node_action *)parent)->output.node;
2197 node->prev = node->next;
2198 return LY_SUCCESS;
2199 } else if (node->nodetype == LYS_OUTPUT) {
2200 /* output node is part of the action but link it with input */
2201 node->next = NULL;
2202 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002203 return LY_SUCCESS;
2204 } else if (node->nodetype == LYS_ACTION) {
2205 children = (struct lysc_node **)lysc_node_actions_p(parent);
2206 } else if (node->nodetype == LYS_NOTIF) {
2207 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2208 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002209 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002210 }
2211 assert(children);
2212
2213 if (!(*children)) {
2214 /* first child */
2215 *children = node;
2216 } else if (node->flags & LYS_KEY) {
2217 /* special handling of adding keys */
2218 assert(node->module == parent->module);
2219 anchor = *children;
2220 if (anchor->flags & LYS_KEY) {
2221 while ((anchor->flags & LYS_KEY) && anchor->next) {
2222 anchor = anchor->next;
2223 }
2224 /* insert after the last key */
2225 insert_after = 1;
2226 } /* else insert before anchor (at the beginning) */
2227 } else if ((*children)->prev->module == node->module) {
2228 /* last child is from the same module, keep the order and insert at the end */
2229 anchor = (*children)->prev;
2230 insert_after = 1;
2231 } else if (parent->module == node->module) {
2232 /* adding module child after some augments were connected */
2233 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2234 } else {
2235 /* some augments are already connected and we are connecting new ones,
2236 * keep module name order and insert the node into the children list */
2237 anchor = *children;
2238 do {
2239 anchor = anchor->prev;
2240
2241 /* check that we have not found the last augment node from our module or
2242 * the first augment node from a "smaller" module or
2243 * the first node from a local module */
2244 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2245 (anchor->module == parent->module)) {
2246 /* insert after */
2247 insert_after = 1;
2248 break;
2249 }
2250
2251 /* we have traversed all the nodes, insert before anchor (as the first node) */
2252 } while (anchor->prev->next);
2253 }
2254
2255 /* insert */
2256 if (anchor) {
2257 if (insert_after) {
2258 node->next = anchor->next;
2259 node->prev = anchor;
2260 anchor->next = node;
2261 if (node->next) {
2262 /* middle node */
2263 node->next->prev = node;
2264 } else {
2265 /* last node */
2266 (*children)->prev = node;
2267 }
2268 } else {
2269 node->next = anchor;
2270 node->prev = anchor->prev;
2271 anchor->prev = node;
2272 if (anchor == *children) {
2273 /* first node */
2274 *children = node;
2275 } else {
2276 /* middle node */
2277 node->prev->next = node;
2278 }
2279 }
2280 }
2281
2282 /* check the name uniqueness (even for an only child, it may be in case) */
2283 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2284 return LY_EEXIST;
2285 }
2286 } else {
2287 /* top-level element */
2288 struct lysc_node **list;
2289
Radek Krejci6b88a462021-02-17 12:39:34 +01002290 if (ctx->ext) {
2291 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2292 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002293 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2294 } else if (node->nodetype == LYS_NOTIF) {
2295 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2296 } else {
2297 list = &ctx->cur_mod->compiled->data;
2298 }
2299 if (!(*list)) {
2300 *list = node;
2301 } else {
2302 /* insert at the end of the module's top-level nodes list */
2303 (*list)->prev->next = node;
2304 node->prev = (*list)->prev;
2305 (*list)->prev = node;
2306 }
2307
2308 /* check the name uniqueness on top-level */
2309 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2310 return LY_EEXIST;
2311 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002312 }
2313
Radek Krejci2a9fc652021-01-22 17:44:34 +01002314 return LY_SUCCESS;
2315}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002316
Radek Krejci2a9fc652021-01-22 17:44:34 +01002317/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002318 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002319 *
2320 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002321 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002322 * @return LY_ERR value.
2323 */
2324static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002325lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002326{
2327 /* case never has any explicit config */
2328 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002329
Radek Krejci91531e12021-02-08 19:57:54 +01002330 if (ctx->options & LYS_COMPILE_NO_CONFIG) {
2331 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002332 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002333 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2334 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002335 if (node->parent) {
2336 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002337 } else {
2338 /* default is config true */
2339 node->flags |= LYS_CONFIG_W;
2340 }
2341 } else {
2342 /* config set explicitly */
2343 node->flags |= LYS_SET_CONFIG;
2344 }
2345
Radek Krejci91531e12021-02-08 19:57:54 +01002346 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002347 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002348 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002349 }
2350
Radek Krejci2a9fc652021-01-22 17:44:34 +01002351 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002352}
2353
Radek Krejci91531e12021-02-08 19:57:54 +01002354/**
2355 * @brief Set various flags of the compiled nodes
2356 *
2357 * @param[in] ctx Compile context.
2358 * @param[in] node Compiled node where the flags will be set.
2359 * @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).
2360 * Zero means no uses, non-zero value with no status bit set mean the default status.
2361 */
2362static LY_ERR
2363lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2364{
2365 /* inherit config flags */
2366 LY_CHECK_RET(lys_compile_config(ctx, node));
2367
2368 /* status - it is not inherited by specification, but it does not make sense to have
2369 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2370 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (node->parent ? node->parent->flags : 0)));
2371
2372 /* other flags */
2373 if ((ctx->options & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
2374 node->flags |= LYS_IS_INPUT;
2375 } else if ((ctx->options & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
2376 node->flags |= LYS_IS_OUTPUT;
2377 } else if ((ctx->options & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
2378 node->flags |= LYS_IS_NOTIF;
2379 }
2380
2381 return LY_SUCCESS;
2382}
2383
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002384LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002385lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2386 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2387 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002388{
2389 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002390 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002391 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002392 struct lysp_when *pwhen = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002393
Radek Krejci2a9fc652021-01-22 17:44:34 +01002394 node->nodetype = pnode->nodetype;
2395 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002396 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002397 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002398 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002399
Radek Krejci2a9fc652021-01-22 17:44:34 +01002400 /* compile any deviations for this node */
2401 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002402 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002403 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002404 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002405 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002406 }
2407
Radek Krejci2a9fc652021-01-22 17:44:34 +01002408 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002409
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002410 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002411 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Radek Krejciaf0548b2021-02-15 15:11:22 +01002412 if (!enabled && !(ctx->options & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002413 ly_set_add(&ctx->disabled, node, 1, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002414 ctx->options |= LYS_COMPILE_DISABLED;
2415 }
2416
Radek Krejci91531e12021-02-08 19:57:54 +01002417 /* config, status and other flags */
2418 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002419 LY_CHECK_GOTO(ret, error);
2420
2421 /* list ordering */
2422 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002423 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002424 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002425 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
2426 (ctx->options & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002427 node->flags &= ~LYS_ORDBY_MASK;
2428 node->flags |= LYS_ORDBY_SYSTEM;
2429 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2430 /* default ordering is system */
2431 node->flags |= LYS_ORDBY_SYSTEM;
2432 }
2433 }
2434
Radek Krejci2a9fc652021-01-22 17:44:34 +01002435 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2436 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2437 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2438
2439 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2440 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2441
Radek Krejci9a3823e2021-01-27 20:26:46 +01002442 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002443 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002444 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002445 LY_CHECK_GOTO(ret, cleanup);
2446 }
2447
2448 /* connect any augments */
2449 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2450
2451 /* nodetype-specific part */
2452 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2453
2454 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002455 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002456 if (node->flags & LYS_MAND_TRUE) {
2457 /* inherit LYS_MAND_TRUE in parent containers */
2458 lys_compile_mandatory_parents(parent, 1);
2459 }
2460
2461 if (child_set) {
2462 /* add the new node into set */
2463 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2464 }
2465
2466 goto cleanup;
2467
2468error:
2469 lysc_node_free(ctx->ctx, node, 0);
2470cleanup:
2471 if (ret && dev_pnode) {
2472 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2473 }
2474 lysp_dev_node_free(ctx->ctx, dev_pnode);
2475 return ret;
2476}
2477
2478/**
2479 * @brief Compile parsed action's input/output node information.
2480 * @param[in] ctx Compile context
2481 * @param[in] pnode Parsed inout node.
2482 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2483 * is enriched with the inout-specific information.
2484 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2485 */
2486LY_ERR
2487lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2488{
2489 LY_ERR ret = LY_SUCCESS;
2490 struct lysp_node *child_p;
2491 uint32_t prev_options = ctx->options;
2492
2493 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2494 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2495
2496 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002497 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Radek Krejci91531e12021-02-08 19:57:54 +01002498 ctx->options |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002499
Radek Krejci01180ac2021-01-27 08:48:22 +01002500 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002501 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2502 }
2503
2504 ctx->options = prev_options;
2505
2506done:
2507 return ret;
2508}
2509
2510/**
2511 * @brief Compile parsed action node information.
2512 * @param[in] ctx Compile context
2513 * @param[in] pnode Parsed action node.
2514 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2515 * is enriched with the action-specific information.
2516 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2517 */
2518LY_ERR
2519lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2520{
2521 LY_ERR ret;
2522 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2523 struct lysc_node_action *action = (struct lysc_node_action *)node;
2524 struct lysp_node_action_inout *input, implicit_input = {
2525 .nodetype = LYS_INPUT,
2526 .name = "input",
2527 .parent = pnode,
2528 };
2529 struct lysp_node_action_inout *output, implicit_output = {
2530 .nodetype = LYS_OUTPUT,
2531 .name = "output",
2532 .parent = pnode,
2533 };
2534
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002535 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002536 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002537 if (action_p->input.nodetype == LYS_UNKNOWN) {
2538 input = &implicit_input;
2539 } else {
2540 input = &action_p->input;
2541 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002542 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 +01002543 lysc_update_path(ctx, NULL, NULL);
2544 LY_CHECK_GOTO(ret, done);
2545
2546 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002547 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002548 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002549 output = &implicit_output;
2550 } else {
2551 output = &action_p->output;
2552 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002553 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 +01002554 lysc_update_path(ctx, NULL, NULL);
2555 LY_CHECK_GOTO(ret, done);
2556
2557 /* do not check "must" semantics in a grouping */
2558 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2559 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2560 LY_CHECK_GOTO(ret, done);
2561 }
2562
2563done:
2564 return ret;
2565}
2566
2567/**
2568 * @brief Compile parsed action node information.
2569 * @param[in] ctx Compile context
2570 * @param[in] pnode Parsed action node.
2571 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2572 * is enriched with the action-specific information.
2573 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2574 */
2575LY_ERR
2576lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2577{
2578 LY_ERR ret = LY_SUCCESS;
2579 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2580 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2581 struct lysp_node *child_p;
2582
2583 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002584 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002585 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002586 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002587 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002588 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002589
Radek Krejci01180ac2021-01-27 08:48:22 +01002590 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002591 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002592 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002593 }
2594
Radek Krejci2a9fc652021-01-22 17:44:34 +01002595done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002596 return ret;
2597}
2598
2599/**
2600 * @brief Compile parsed container node information.
2601 * @param[in] ctx Compile context
2602 * @param[in] pnode Parsed container node.
2603 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2604 * is enriched with the container-specific information.
2605 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2606 */
2607static LY_ERR
2608lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2609{
2610 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2611 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2612 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002613 LY_ERR ret = LY_SUCCESS;
2614
2615 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002616 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002617 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002618 }
2619
2620 /* more cases when the container has meaning but is kept NP for convenience:
2621 * - when condition
2622 * - direct child action/notification
2623 */
2624
2625 LY_LIST_FOR(cont_p->child, child_p) {
2626 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2627 LY_CHECK_GOTO(ret, done);
2628 }
2629
Michal Vasko5347e3a2020-11-03 17:14:57 +01002630 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002631 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002632 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002633 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002634 LY_CHECK_GOTO(ret, done);
2635 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002636
2637 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2638 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2639 LY_CHECK_GOTO(ret, done);
2640 }
2641 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2642 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2643 LY_CHECK_GOTO(ret, done);
2644 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002645
2646done:
2647 return ret;
2648}
2649
Michal Vasko72244882021-01-12 15:21:05 +01002650/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002651 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2652 * @param[in] ctx Compile context.
2653 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2654 * @param[in] type_p Parsed type to compile.
2655 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2656 * @return LY_ERR value.
2657 */
2658static LY_ERR
2659lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2660 struct lysc_node_leaf *leaf)
2661{
2662 struct lysp_qname *dflt;
2663
Michal Vaskoa99b3572021-02-01 11:54:58 +01002664 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002665 leaf->units ? NULL : &leaf->units, &dflt));
2666
2667 /* store default value, if any */
2668 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2669 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2670 }
2671
Michal Vasko12606ee2020-11-25 17:05:11 +01002672 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002673 /* 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 +01002674 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002675 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002676 LY_ARRAY_COUNT_TYPE u;
2677 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2678 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2679 /* 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 +01002680 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002681 }
2682 }
2683 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2684 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002685 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 +02002686 return LY_EVALID;
2687 }
2688 }
2689
2690 return LY_SUCCESS;
2691}
2692
2693/**
2694 * @brief Compile parsed leaf node information.
2695 * @param[in] ctx Compile context
2696 * @param[in] pnode Parsed leaf node.
2697 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2698 * is enriched with the leaf-specific information.
2699 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2700 */
2701static LY_ERR
2702lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2703{
2704 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2705 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002706 LY_ERR ret = LY_SUCCESS;
2707
Michal Vasko5347e3a2020-11-03 17:14:57 +01002708 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002709 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002710 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002711 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002712 LY_CHECK_GOTO(ret, done);
2713 }
2714 if (leaf_p->units) {
2715 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2716 leaf->flags |= LYS_SET_UNITS;
2717 }
2718
2719 /* compile type */
2720 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2721 LY_CHECK_GOTO(ret, done);
2722
2723 /* store/update default value */
2724 if (leaf_p->dflt.str) {
2725 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2726 leaf->flags |= LYS_SET_DFLT;
2727 }
2728
2729 /* checks */
2730 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002731 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002732 "Invalid mandatory leaf with a default value.");
2733 return LY_EVALID;
2734 }
2735
2736done:
2737 return ret;
2738}
2739
2740/**
2741 * @brief Compile parsed leaf-list node information.
2742 * @param[in] ctx Compile context
2743 * @param[in] pnode Parsed leaf-list node.
2744 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2745 * is enriched with the leaf-list-specific information.
2746 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2747 */
2748static LY_ERR
2749lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2750{
2751 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2752 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002753 LY_ERR ret = LY_SUCCESS;
2754
Michal Vasko5347e3a2020-11-03 17:14:57 +01002755 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002756 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002757 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002758 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002759 LY_CHECK_GOTO(ret, done);
2760 }
2761 if (llist_p->units) {
2762 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2763 llist->flags |= LYS_SET_UNITS;
2764 }
2765
2766 /* compile type */
2767 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2768 LY_CHECK_GOTO(ret, done);
2769
2770 /* store/update default values */
2771 if (llist_p->dflts) {
2772 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002773 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002774 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2775 return LY_EVALID;
2776 }
2777
2778 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2779 llist->flags |= LYS_SET_DFLT;
2780 }
2781
2782 llist->min = llist_p->min;
2783 if (llist->min) {
2784 llist->flags |= LYS_MAND_TRUE;
2785 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002786 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2787
2788 if (llist->flags & LYS_CONFIG_R) {
2789 /* state leaf-list is always ordered-by user */
2790 llist->flags &= ~LYS_ORDBY_SYSTEM;
2791 llist->flags |= LYS_ORDBY_USER;
2792 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002793
2794 /* checks */
2795 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002796 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 +02002797 return LY_EVALID;
2798 }
2799
2800 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002801 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002802 llist->min, llist->max);
2803 return LY_EVALID;
2804 }
2805
2806done:
2807 return ret;
2808}
2809
2810LY_ERR
2811lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2812 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2813 const struct lysc_node **target, uint16_t *result_flag)
2814{
2815 LY_ERR ret = LY_EVALID;
2816 const char *name, *prefix, *id;
2817 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002818 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002819 const char *nodeid_type;
2820 uint32_t getnext_extra_flag = 0;
2821 uint16_t current_nodetype = 0;
2822
2823 assert(nodeid);
2824 assert(target);
2825 assert(result_flag);
2826 *target = NULL;
2827 *result_flag = 0;
2828
2829 id = nodeid;
2830
2831 if (ctx_node) {
2832 /* descendant-schema-nodeid */
2833 nodeid_type = "descendant";
2834
2835 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002836 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002837 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002838 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002839 return LY_EVALID;
2840 }
2841 } else {
2842 /* absolute-schema-nodeid */
2843 nodeid_type = "absolute";
2844
2845 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002846 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002847 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002848 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002849 return LY_EVALID;
2850 }
2851 ++id;
2852 }
2853
2854 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2855 if (prefix) {
2856 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2857 if (!mod) {
2858 /* module must always be found */
2859 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002860 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002861 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002862 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002863 return LY_ENOTFOUND;
2864 }
2865 } else {
2866 switch (format) {
2867 case LY_PREF_SCHEMA:
2868 case LY_PREF_SCHEMA_RESOLVED:
2869 /* use the current module */
2870 mod = cur_mod;
2871 break;
2872 case LY_PREF_JSON:
2873 if (!ctx_node) {
2874 LOGINT_RET(ctx->ctx);
2875 }
2876 /* inherit the module of the previous context node */
2877 mod = ctx_node->module;
2878 break;
2879 case LY_PREF_XML:
2880 /* not really defined */
2881 LOGINT_RET(ctx->ctx);
2882 }
2883 }
2884
2885 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2886 /* move through input/output manually */
2887 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002888 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2889 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002890 return LY_ENOTFOUND;
2891 }
2892 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002893 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002894 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002895 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002896 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2897 } else {
2898 /* only input or output is valid */
2899 ctx_node = NULL;
2900 }
2901 } else {
2902 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002903 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002904 getnext_extra_flag = 0;
2905 }
2906 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002907 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2908 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002909 return LY_ENOTFOUND;
2910 }
2911 current_nodetype = ctx_node->nodetype;
2912
2913 if (current_nodetype == LYS_NOTIF) {
2914 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2915 } else if (current_nodetype == LYS_INPUT) {
2916 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2917 } else if (current_nodetype == LYS_OUTPUT) {
2918 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2919 }
2920
2921 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2922 break;
2923 }
2924 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002925 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002926 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002927 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002928 return LY_EVALID;
2929 }
2930 ++id;
2931 }
2932
2933 if (ret == LY_SUCCESS) {
2934 *target = ctx_node;
2935 if (nodetype && !(current_nodetype & nodetype)) {
2936 return LY_EDENIED;
2937 }
2938 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002939 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002940 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002941 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002942 }
2943
2944 return ret;
2945}
2946
2947/**
2948 * @brief Compile information about list's uniques.
2949 * @param[in] ctx Compile context.
2950 * @param[in] uniques Sized array list of unique statements.
2951 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2952 * @return LY_ERR value.
2953 */
2954static LY_ERR
2955lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2956{
2957 LY_ERR ret = LY_SUCCESS;
2958 struct lysc_node_leaf **key, ***unique;
2959 struct lysc_node *parent;
2960 const char *keystr, *delim;
2961 size_t len;
2962 LY_ARRAY_COUNT_TYPE v;
2963 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2964 uint16_t flags;
2965
2966 LY_ARRAY_FOR(uniques, v) {
2967 config = -1;
2968 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2969 keystr = uniques[v].str;
2970 while (keystr) {
2971 delim = strpbrk(keystr, " \t\n");
2972 if (delim) {
2973 len = delim - keystr;
2974 while (isspace(*delim)) {
2975 ++delim;
2976 }
2977 } else {
2978 len = strlen(keystr);
2979 }
2980
2981 /* unique node must be present */
2982 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002983 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002984 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2985 if (ret != LY_SUCCESS) {
2986 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002987 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002988 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002989 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002990 }
2991 return LY_EVALID;
2992 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002993 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002994 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01002995 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002996 return LY_EVALID;
2997 }
2998
2999 /* all referenced leafs must be of the same config type */
3000 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3001 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003002 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003003 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3004 return LY_EVALID;
3005 } else if ((*key)->flags & LYS_CONFIG_W) {
3006 config = 1;
3007 } else { /* LYS_CONFIG_R */
3008 config = 0;
3009 }
3010
3011 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3012 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3013 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003014 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003015 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3016 return LY_EVALID;
3017 }
3018 }
3019
3020 /* check status */
3021 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3022 (*key)->flags, (*key)->module, (*key)->name));
3023
3024 /* mark leaf as unique */
3025 (*key)->flags |= LYS_UNIQUE;
3026
3027 /* next unique value in line */
3028 keystr = delim;
3029 }
3030 /* next unique definition */
3031 }
3032
3033 return LY_SUCCESS;
3034}
3035
3036/**
3037 * @brief Compile parsed list node information.
3038 * @param[in] ctx Compile context
3039 * @param[in] pnode Parsed list node.
3040 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3041 * is enriched with the list-specific information.
3042 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3043 */
3044static LY_ERR
3045lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3046{
3047 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3048 struct lysc_node_list *list = (struct lysc_node_list *)node;
3049 struct lysp_node *child_p;
3050 struct lysc_node_leaf *key, *prev_key = NULL;
3051 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003052 const char *keystr, *delim;
3053 LY_ERR ret = LY_SUCCESS;
3054
3055 list->min = list_p->min;
3056 if (list->min) {
3057 list->flags |= LYS_MAND_TRUE;
3058 }
3059 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3060
3061 LY_LIST_FOR(list_p->child, child_p) {
3062 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3063 }
3064
Michal Vasko5347e3a2020-11-03 17:14:57 +01003065 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003066 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003067 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003068 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003069 }
3070
3071 /* keys */
3072 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003073 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003074 return LY_EVALID;
3075 }
3076
3077 /* find all the keys (must be direct children) */
3078 keystr = list_p->key;
3079 if (!keystr) {
3080 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003081 list->flags &= ~LYS_ORDBY_SYSTEM;
3082 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003083 }
3084 while (keystr) {
3085 delim = strpbrk(keystr, " \t\n");
3086 if (delim) {
3087 len = delim - keystr;
3088 while (isspace(*delim)) {
3089 ++delim;
3090 }
3091 } else {
3092 len = strlen(keystr);
3093 }
3094
3095 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003096 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3097 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003098 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003099 return LY_EVALID;
3100 }
3101 /* keys must be unique */
3102 if (key->flags & LYS_KEY) {
3103 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003104 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003105 return LY_EVALID;
3106 }
3107
Radek Krejcia6016992021-03-03 10:13:41 +01003108 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003109 /* key must have the same config flag as the list itself */
3110 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003111 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003112 return LY_EVALID;
3113 }
3114 if (ctx->pmod->version < LYS_VERSION_1_1) {
3115 /* YANG 1.0 denies key to be of empty type */
3116 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003117 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003118 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3119 return LY_EVALID;
3120 }
3121 } else {
3122 /* when and if-feature are illegal on list keys */
3123 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003124 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003125 "List's key must not have any \"when\" statement.");
3126 return LY_EVALID;
3127 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003128 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003129 }
3130
3131 /* check status */
3132 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3133 key->flags, key->module, key->name));
3134
3135 /* ignore default values of the key */
3136 if (key->dflt) {
3137 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3138 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3139 free(key->dflt);
3140 key->dflt = NULL;
3141 }
3142 /* mark leaf as key */
3143 key->flags |= LYS_KEY;
3144
3145 /* move it to the correct position */
3146 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3147 /* fix links in closest previous siblings of the key */
3148 if (key->next) {
3149 key->next->prev = key->prev;
3150 } else {
3151 /* last child */
3152 list->child->prev = key->prev;
3153 }
3154 if (key->prev->next) {
3155 key->prev->next = key->next;
3156 }
3157 /* fix links in the key */
3158 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003159 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003160 key->next = prev_key->next;
3161 } else {
3162 key->prev = list->child->prev;
3163 key->next = list->child;
3164 }
3165 /* fix links in closes future siblings of the key */
3166 if (prev_key) {
3167 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003168 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003169 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003170 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003171 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003172 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003173 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003174 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003175 }
3176 /* fix links in parent */
3177 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003178 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003179 }
3180 }
3181
3182 /* next key value */
3183 prev_key = key;
3184 keystr = delim;
3185 lysc_update_path(ctx, NULL, NULL);
3186 }
3187
3188 /* uniques */
3189 if (list_p->uniques) {
3190 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3191 }
3192
Radek Krejci2a9fc652021-01-22 17:44:34 +01003193 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3194 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3195 LY_CHECK_GOTO(ret, done);
3196 }
3197 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3198 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3199 LY_CHECK_GOTO(ret, done);
3200 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003201
3202 /* checks */
3203 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003204 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003205 list->min, list->max);
3206 return LY_EVALID;
3207 }
3208
3209done:
3210 return ret;
3211}
3212
3213/**
3214 * @brief Do some checks and set the default choice's case.
3215 *
3216 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3217 *
3218 * @param[in] ctx Compile context.
3219 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3220 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3221 * @return LY_ERR value.
3222 */
3223static LY_ERR
3224lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3225{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003226 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003227 const struct lys_module *mod;
3228 const char *prefix = NULL, *name;
3229 size_t prefix_len = 0;
3230
3231 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3232 name = strchr(dflt->str, ':');
3233 if (name) {
3234 prefix = dflt->str;
3235 prefix_len = name - prefix;
3236 ++name;
3237 } else {
3238 name = dflt->str;
3239 }
3240 if (prefix) {
3241 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
3242 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003243 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003244 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003245 return LY_EVALID;
3246 }
3247 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003248 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003249 }
3250
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003251 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 +02003252 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003253 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003254 "Default case \"%s\" not found.", dflt->str);
3255 return LY_EVALID;
3256 }
3257
3258 /* no mandatory nodes directly under the default case */
3259 LY_LIST_FOR(ch->dflt->child, iter) {
3260 if (iter->parent != (struct lysc_node *)ch->dflt) {
3261 break;
3262 }
3263 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003264 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003265 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3266 return LY_EVALID;
3267 }
3268 }
3269
3270 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003271 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003272 return LY_EVALID;
3273 }
3274
3275 ch->dflt->flags |= LYS_SET_DFLT;
3276 return LY_SUCCESS;
3277}
3278
3279LY_ERR
3280lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3281 struct ly_set *child_set)
3282{
3283 LY_ERR ret = LY_SUCCESS;
3284 struct lysp_node *child_p_next = child_p->next;
3285 struct lysp_node_case *cs_p;
3286
3287 if (child_p->nodetype == LYS_CASE) {
3288 /* standard case under choice */
3289 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3290 } else {
3291 /* we need the implicit case first, so create a fake parsed case */
3292 cs_p = calloc(1, sizeof *cs_p);
3293 cs_p->nodetype = LYS_CASE;
3294 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3295 cs_p->child = child_p;
3296
3297 /* make the child the only case child */
3298 child_p->next = NULL;
3299
3300 /* compile it normally */
3301 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3302
3303free_fake_node:
3304 /* free the fake parsed node and correct pointers back */
3305 cs_p->child = NULL;
3306 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3307 child_p->next = child_p_next;
3308 }
3309
3310 return ret;
3311}
3312
3313/**
3314 * @brief Compile parsed choice node information.
3315 *
3316 * @param[in] ctx Compile context
3317 * @param[in] pnode Parsed choice node.
3318 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3319 * is enriched with the choice-specific information.
3320 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3321 */
3322static LY_ERR
3323lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3324{
3325 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3326 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3327 struct lysp_node *child_p;
3328 LY_ERR ret = LY_SUCCESS;
3329
3330 assert(node->nodetype == LYS_CHOICE);
3331
3332 LY_LIST_FOR(ch_p->child, child_p) {
3333 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3334 }
3335
3336 /* default branch */
3337 if (ch_p->dflt.str) {
3338 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3339 }
3340
3341 return ret;
3342}
3343
3344/**
3345 * @brief Compile parsed anydata or anyxml node information.
3346 * @param[in] ctx Compile context
3347 * @param[in] pnode Parsed anydata or anyxml node.
3348 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3349 * is enriched with the any-specific information.
3350 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3351 */
3352static LY_ERR
3353lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3354{
3355 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3356 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003357 LY_ERR ret = LY_SUCCESS;
3358
Michal Vasko5347e3a2020-11-03 17:14:57 +01003359 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003360 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003361 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003362 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003363 LY_CHECK_GOTO(ret, done);
3364 }
3365
Radek Krejci91531e12021-02-08 19:57:54 +01003366 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003367 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003368 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3369 }
3370done:
3371 return ret;
3372}
3373
3374/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003375 * @brief Prepare the case structure in choice node for the new data node.
3376 *
3377 * 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
3378 * created in the choice when the first child was processed.
3379 *
3380 * @param[in] ctx Compile context.
3381 * @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,
3382 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3383 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3384 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3385 * @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,
3386 * it is linked from the case structure only in case it is its first child.
3387 */
3388static LY_ERR
3389lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3390{
3391 struct lysp_node *child_p;
3392 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3393
3394 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3395 /* we have to add an implicit case node into the parent choice */
3396 } else if (pnode->nodetype == LYS_CASE) {
3397 /* explicit parent case */
3398 LY_LIST_FOR(cs_p->child, child_p) {
3399 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3400 }
3401 } else {
3402 LOGINT_RET(ctx->ctx);
3403 }
3404
3405 return LY_SUCCESS;
3406}
3407
3408void
3409lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3410{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003411 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003412
3413 if (add) { /* set flag */
3414 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3415 parent = parent->parent) {
3416 parent->flags |= LYS_MAND_TRUE;
3417 }
3418 } else { /* unset flag */
3419 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003420 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003421 if (iter->flags & LYS_MAND_TRUE) {
3422 /* there is another mandatory node */
3423 return;
3424 }
3425 }
3426 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3427 parent->flags &= ~LYS_MAND_TRUE;
3428 }
3429 }
3430}
3431
3432/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003433 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003434 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003435 * @param[in] name Name of the grouping to find,
3436 * @return NULL when there is no grouping with the specified name
3437 * @return Pointer to the grouping of the specified @p name.
3438 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003439static struct lysp_node_grp *
3440match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003441{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003442 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003443
Radek Krejci2a9fc652021-01-22 17:44:34 +01003444 LY_LIST_FOR(grps, grp) {
3445 if (!strcmp(grp->name, name)) {
3446 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003447 }
3448 }
3449
3450 return NULL;
3451}
3452
3453/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003454 * @brief Find grouping for a uses.
3455 *
3456 * @param[in] ctx Compile context.
3457 * @param[in] uses_p Parsed uses node.
3458 * @param[out] gpr_p Found grouping on success.
3459 * @param[out] grp_pmod Module of @p grp_p on success.
3460 * @return LY_ERR value.
3461 */
3462static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003463lys_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 +02003464 struct lysp_module **grp_pmod)
3465{
3466 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003467 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003468 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003469 const char *id, *name, *prefix, *local_pref;
3470 size_t prefix_len, name_len;
3471 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003472 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003473
3474 *grp_p = NULL;
3475 *grp_pmod = NULL;
3476
3477 /* search for the grouping definition */
3478 id = uses_p->name;
3479 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3480 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3481 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3482 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003483 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003484 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003485 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3486 found = ctx->pmod;
3487 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003488 }
3489 }
3490 } else {
3491 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003492 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003493 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003494 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003495 "Invalid prefix used for grouping reference.", uses_p->name);
3496 return LY_EVALID;
3497 }
3498 pmod = mod->parsed;
3499 }
3500
3501 if (!found) {
3502 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003503 if ((grp = match_grouping(pmod->groupings, name))) {
3504 found = pmod;
3505 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003506 /* ... and all the submodules */
3507 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003508 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3509 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003510 break;
3511 }
3512 }
3513 }
3514 }
3515 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003516 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003517 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3518 return LY_EVALID;
3519 }
3520
3521 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3522 /* remember that the grouping is instantiated to avoid its standalone validation */
3523 grp->flags |= LYS_USED_GRP;
3524 }
3525
3526 *grp_p = grp;
3527 *grp_pmod = found;
3528 return LY_SUCCESS;
3529}
3530
3531/**
3532 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3533 * If present, also apply uses's modificators.
3534 *
3535 * @param[in] ctx Compile context
3536 * @param[in] uses_p Parsed uses schema node.
3537 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3538 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3539 * the compile context.
3540 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3541 */
3542static LY_ERR
3543lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3544{
3545 struct lysp_node *pnode;
3546 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003547 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003548 uint32_t i, grp_stack_count;
3549 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3550 LY_ERR ret = LY_SUCCESS;
3551 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003552 struct ly_set uses_child_set = {0};
3553
3554 /* find the referenced grouping */
3555 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3556
3557 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3558 grp_stack_count = ctx->groupings.count;
3559 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3560 if (grp_stack_count == ctx->groupings.count) {
3561 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003562 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003563 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3564 return LY_EVALID;
3565 }
3566
3567 /* check status */
3568 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3569 LY_CHECK_GOTO(ret, cleanup);
3570
3571 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3572 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3573 LY_CHECK_GOTO(ret, cleanup);
3574
3575 /* switch context's parsed module being processed */
3576 ctx->pmod = grp_mod;
3577
3578 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003579 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003580 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3581 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 +02003582 LY_CHECK_GOTO(ret, cleanup);
3583 }
3584
3585 if (child_set) {
3586 /* add these children to our compiled child_set as well since uses is a schema-only node */
3587 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3588 }
3589
3590 if (uses_p->when) {
3591 /* pass uses's when to all the data children */
3592 for (i = 0; i < uses_child_set.count; ++i) {
3593 child = uses_child_set.snodes[i];
3594
Michal Vasko72244882021-01-12 15:21:05 +01003595 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 +02003596 LY_CHECK_GOTO(ret, cleanup);
3597 }
3598 }
3599
3600 /* compile actions */
3601 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003602 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003603 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3604 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003605 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003606 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3607 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003608 ret = LY_EVALID;
3609 goto cleanup;
3610 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003611 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3612 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3613 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3614 LY_CHECK_GOTO(ret, cleanup);
3615 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003616
3617 if (uses_p->when) {
3618 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003619 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3620 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 +02003621 LY_CHECK_GOTO(ret, cleanup);
3622 }
3623 }
3624 }
3625
3626 /* compile notifications */
3627 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003628 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003629 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3630 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003631 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003632 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3633 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003634 ret = LY_EVALID;
3635 goto cleanup;
3636 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003637
3638 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3639 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3640 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3641 LY_CHECK_GOTO(ret, cleanup);
3642 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003643
3644 if (uses_p->when) {
3645 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003646 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3647 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 +02003648 LY_CHECK_GOTO(ret, cleanup);
3649 }
3650 }
3651 }
3652
3653 /* check that all augments were applied */
3654 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003655 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3656 /* augment of some parent uses, irrelevant now */
3657 continue;
3658 }
3659
3660 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003661 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3662 ret = LY_ENOTFOUND;
3663 }
3664 LY_CHECK_GOTO(ret, cleanup);
3665
3666 /* check that all refines were applied */
3667 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003668 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3669 /* refine of some paretn uses, irrelevant now */
3670 continue;
3671 }
3672
3673 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003674 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3675 ret = LY_ENOTFOUND;
3676 }
3677 LY_CHECK_GOTO(ret, cleanup);
3678
3679cleanup:
3680 /* reload previous context's parsed module being processed */
3681 ctx->pmod = mod_old;
3682
3683 /* remove the grouping from the stack for circular groupings dependency check */
3684 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3685 assert(ctx->groupings.count == grp_stack_count);
3686
3687 ly_set_erase(&uses_child_set, NULL);
3688 return ret;
3689}
3690
3691static int
3692lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3693{
3694 struct lysp_node *iter;
3695 int len = 0;
3696
3697 *path = NULL;
3698 for (iter = node; iter && len >= 0; iter = iter->parent) {
3699 char *s = *path;
3700 char *id;
3701
3702 switch (iter->nodetype) {
3703 case LYS_USES:
3704 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3705 break;
3706 case LYS_GROUPING:
3707 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3708 break;
3709 case LYS_AUGMENT:
3710 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3711 break;
3712 default:
3713 id = strdup(iter->name);
3714 break;
3715 }
3716
3717 if (!iter->parent) {
3718 /* print prefix */
3719 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3720 } else {
3721 /* prefix is the same as in parent */
3722 len = asprintf(path, "/%s%s", id, s ? s : "");
3723 }
3724 free(s);
3725 free(id);
3726 }
3727
3728 if (len < 0) {
3729 free(*path);
3730 *path = NULL;
3731 } else if (len == 0) {
3732 *path = strdup("/");
3733 len = 1;
3734 }
3735 return len;
3736}
3737
3738LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003739lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003740{
3741 LY_ERR ret;
3742 char *path;
3743 int len;
3744
3745 struct lysp_node_uses fake_uses = {
3746 .parent = pnode,
3747 .nodetype = LYS_USES,
3748 .flags = 0, .next = NULL,
3749 .name = grp->name,
3750 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3751 .refines = NULL, .augments = NULL
3752 };
3753 struct lysc_node_container fake_container = {
3754 .nodetype = LYS_CONTAINER,
3755 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3756 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003757 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003758 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003759 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003760 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003761 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3762 };
3763
3764 if (grp->parent) {
3765 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3766 }
3767
3768 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3769 if (len < 0) {
3770 LOGMEM(ctx->ctx);
3771 return LY_EMEM;
3772 }
3773 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3774 ctx->path_len = (uint32_t)len;
3775 free(path);
3776
3777 lysc_update_path(ctx, NULL, "{grouping}");
3778 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003779 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003780 lysc_update_path(ctx, NULL, NULL);
3781 lysc_update_path(ctx, NULL, NULL);
3782
3783 ctx->path_len = 1;
3784 ctx->path[1] = '\0';
3785
3786 /* cleanup */
3787 lysc_node_container_free(ctx->ctx, &fake_container);
3788
3789 return ret;
3790}
3791
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003792LY_ERR
3793lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3794 struct ly_set *child_set)
3795{
3796 LY_ERR ret = LY_SUCCESS;
3797 struct lysc_node *node = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003798 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003799
3800 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3801
3802 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003803 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003804 } else {
3805 lysc_update_path(ctx, NULL, "{uses}");
3806 lysc_update_path(ctx, NULL, pnode->name);
3807 }
3808
3809 switch (pnode->nodetype) {
3810 case LYS_CONTAINER:
3811 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3812 node_compile_spec = lys_compile_node_container;
3813 break;
3814 case LYS_LEAF:
3815 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3816 node_compile_spec = lys_compile_node_leaf;
3817 break;
3818 case LYS_LIST:
3819 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3820 node_compile_spec = lys_compile_node_list;
3821 break;
3822 case LYS_LEAFLIST:
3823 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3824 node_compile_spec = lys_compile_node_leaflist;
3825 break;
3826 case LYS_CHOICE:
3827 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3828 node_compile_spec = lys_compile_node_choice;
3829 break;
3830 case LYS_CASE:
3831 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3832 node_compile_spec = lys_compile_node_case;
3833 break;
3834 case LYS_ANYXML:
3835 case LYS_ANYDATA:
3836 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3837 node_compile_spec = lys_compile_node_any;
3838 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003839 case LYS_RPC:
3840 case LYS_ACTION:
Radek Krejci91531e12021-02-08 19:57:54 +01003841 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003842 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3843 "Action \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003844 (ctx->options & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003845 return LY_EVALID;
3846 }
3847 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3848 node_compile_spec = lys_compile_node_action;
Radek Krejci91531e12021-02-08 19:57:54 +01003849 ctx->options |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003850 break;
3851 case LYS_NOTIF:
Radek Krejci91531e12021-02-08 19:57:54 +01003852 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003853 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3854 "Notification \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003855 (ctx->options & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003856 return LY_EVALID;
3857 }
3858 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3859 node_compile_spec = lys_compile_node_notif;
3860 ctx->options |= LYS_COMPILE_NOTIFICATION;
3861 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003862 case LYS_USES:
3863 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3864 lysc_update_path(ctx, NULL, NULL);
3865 lysc_update_path(ctx, NULL, NULL);
3866 return ret;
3867 default:
3868 LOGINT(ctx->ctx);
3869 return LY_EINT;
3870 }
3871 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3872
Radek Krejci2a9fc652021-01-22 17:44:34 +01003873 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003874
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003875 ctx->options = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003876 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003877 return ret;
3878}