blob: 9221ab40e60b8dca2ee0236001598021b2c52ea9 [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;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002398
Radek Krejci2a9fc652021-01-22 17:44:34 +01002399 /* compile any deviations for this node */
2400 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002401 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002402 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002403 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002404 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002405 }
2406
Radek Krejci2a9fc652021-01-22 17:44:34 +01002407 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002408
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002409 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002410 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Radek Krejciaf0548b2021-02-15 15:11:22 +01002411 if (!enabled && !(ctx->options & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002412 ly_set_add(&ctx->disabled, node, 1, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002413 ctx->options |= LYS_COMPILE_DISABLED;
2414 }
2415
Radek Krejci91531e12021-02-08 19:57:54 +01002416 /* config, status and other flags */
2417 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002418 LY_CHECK_GOTO(ret, error);
2419
2420 /* list ordering */
2421 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002422 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002423 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002424 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
2425 (ctx->options & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002426 node->flags &= ~LYS_ORDBY_MASK;
2427 node->flags |= LYS_ORDBY_SYSTEM;
2428 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2429 /* default ordering is system */
2430 node->flags |= LYS_ORDBY_SYSTEM;
2431 }
2432 }
2433
Radek Krejci2a9fc652021-01-22 17:44:34 +01002434 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2435 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2436 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2437
2438 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2439 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2440
Radek Krejci9a3823e2021-01-27 20:26:46 +01002441 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002442 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002443 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002444 LY_CHECK_GOTO(ret, cleanup);
2445 }
2446
2447 /* connect any augments */
2448 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2449
2450 /* nodetype-specific part */
2451 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2452
2453 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002454 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002455 if (node->flags & LYS_MAND_TRUE) {
2456 /* inherit LYS_MAND_TRUE in parent containers */
2457 lys_compile_mandatory_parents(parent, 1);
2458 }
2459
2460 if (child_set) {
2461 /* add the new node into set */
2462 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2463 }
2464
2465 goto cleanup;
2466
2467error:
2468 lysc_node_free(ctx->ctx, node, 0);
2469cleanup:
2470 if (ret && dev_pnode) {
2471 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2472 }
2473 lysp_dev_node_free(ctx->ctx, dev_pnode);
2474 return ret;
2475}
2476
2477/**
2478 * @brief Compile parsed action's input/output node information.
2479 * @param[in] ctx Compile context
2480 * @param[in] pnode Parsed inout node.
2481 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2482 * is enriched with the inout-specific information.
2483 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2484 */
2485LY_ERR
2486lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2487{
2488 LY_ERR ret = LY_SUCCESS;
2489 struct lysp_node *child_p;
2490 uint32_t prev_options = ctx->options;
2491
2492 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2493 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2494
2495 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002496 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Radek Krejci91531e12021-02-08 19:57:54 +01002497 ctx->options |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002498
Radek Krejci01180ac2021-01-27 08:48:22 +01002499 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002500 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2501 }
2502
2503 ctx->options = prev_options;
2504
2505done:
2506 return ret;
2507}
2508
2509/**
2510 * @brief Compile parsed action node information.
2511 * @param[in] ctx Compile context
2512 * @param[in] pnode Parsed action node.
2513 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2514 * is enriched with the action-specific information.
2515 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2516 */
2517LY_ERR
2518lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2519{
2520 LY_ERR ret;
2521 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2522 struct lysc_node_action *action = (struct lysc_node_action *)node;
2523 struct lysp_node_action_inout *input, implicit_input = {
2524 .nodetype = LYS_INPUT,
2525 .name = "input",
2526 .parent = pnode,
2527 };
2528 struct lysp_node_action_inout *output, implicit_output = {
2529 .nodetype = LYS_OUTPUT,
2530 .name = "output",
2531 .parent = pnode,
2532 };
2533
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002534 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002535 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002536 if (action_p->input.nodetype == LYS_UNKNOWN) {
2537 input = &implicit_input;
2538 } else {
2539 input = &action_p->input;
2540 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002541 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 +01002542 lysc_update_path(ctx, NULL, NULL);
2543 LY_CHECK_GOTO(ret, done);
2544
2545 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002546 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002547 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002548 output = &implicit_output;
2549 } else {
2550 output = &action_p->output;
2551 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002552 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 +01002553 lysc_update_path(ctx, NULL, NULL);
2554 LY_CHECK_GOTO(ret, done);
2555
2556 /* do not check "must" semantics in a grouping */
2557 if ((action->input.musts || action->output.musts) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2558 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2559 LY_CHECK_GOTO(ret, done);
2560 }
2561
2562done:
2563 return ret;
2564}
2565
2566/**
2567 * @brief Compile parsed action node information.
2568 * @param[in] ctx Compile context
2569 * @param[in] pnode Parsed action node.
2570 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2571 * is enriched with the action-specific information.
2572 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2573 */
2574LY_ERR
2575lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2576{
2577 LY_ERR ret = LY_SUCCESS;
2578 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2579 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2580 struct lysp_node *child_p;
2581
2582 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002583 if (notif_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002584 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002585 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002586 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002587 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002588
Radek Krejci01180ac2021-01-27 08:48:22 +01002589 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002590 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002591 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002592 }
2593
Radek Krejci2a9fc652021-01-22 17:44:34 +01002594done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002595 return ret;
2596}
2597
2598/**
2599 * @brief Compile parsed container node information.
2600 * @param[in] ctx Compile context
2601 * @param[in] pnode Parsed container node.
2602 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2603 * is enriched with the container-specific information.
2604 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2605 */
2606static LY_ERR
2607lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2608{
2609 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2610 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2611 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002612 LY_ERR ret = LY_SUCCESS;
2613
2614 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002615 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002616 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002617 }
2618
2619 /* more cases when the container has meaning but is kept NP for convenience:
2620 * - when condition
2621 * - direct child action/notification
2622 */
2623
2624 LY_LIST_FOR(cont_p->child, child_p) {
2625 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2626 LY_CHECK_GOTO(ret, done);
2627 }
2628
Michal Vasko5347e3a2020-11-03 17:14:57 +01002629 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002630 if (cont_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002631 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002632 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002633 LY_CHECK_GOTO(ret, done);
2634 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002635
2636 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2637 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2638 LY_CHECK_GOTO(ret, done);
2639 }
2640 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2641 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2642 LY_CHECK_GOTO(ret, done);
2643 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002644
2645done:
2646 return ret;
2647}
2648
Michal Vasko72244882021-01-12 15:21:05 +01002649/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002650 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2651 * @param[in] ctx Compile context.
2652 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2653 * @param[in] type_p Parsed type to compile.
2654 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2655 * @return LY_ERR value.
2656 */
2657static LY_ERR
2658lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2659 struct lysc_node_leaf *leaf)
2660{
2661 struct lysp_qname *dflt;
2662
Michal Vaskoa99b3572021-02-01 11:54:58 +01002663 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002664 leaf->units ? NULL : &leaf->units, &dflt));
2665
2666 /* store default value, if any */
2667 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2668 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2669 }
2670
Michal Vasko12606ee2020-11-25 17:05:11 +01002671 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002672 /* 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 +01002673 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko12606ee2020-11-25 17:05:11 +01002674 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->options & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002675 LY_ARRAY_COUNT_TYPE u;
2676 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2677 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2678 /* 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 +01002679 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002680 }
2681 }
2682 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2683 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002684 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 +02002685 return LY_EVALID;
2686 }
2687 }
2688
2689 return LY_SUCCESS;
2690}
2691
2692/**
2693 * @brief Compile parsed leaf node information.
2694 * @param[in] ctx Compile context
2695 * @param[in] pnode Parsed leaf node.
2696 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2697 * is enriched with the leaf-specific information.
2698 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2699 */
2700static LY_ERR
2701lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2702{
2703 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2704 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002705 LY_ERR ret = LY_SUCCESS;
2706
Michal Vasko5347e3a2020-11-03 17:14:57 +01002707 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002708 if (leaf_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002709 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002710 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002711 LY_CHECK_GOTO(ret, done);
2712 }
2713 if (leaf_p->units) {
2714 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2715 leaf->flags |= LYS_SET_UNITS;
2716 }
2717
2718 /* compile type */
2719 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2720 LY_CHECK_GOTO(ret, done);
2721
2722 /* store/update default value */
2723 if (leaf_p->dflt.str) {
2724 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2725 leaf->flags |= LYS_SET_DFLT;
2726 }
2727
2728 /* checks */
2729 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002730 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002731 "Invalid mandatory leaf with a default value.");
2732 return LY_EVALID;
2733 }
2734
2735done:
2736 return ret;
2737}
2738
2739/**
2740 * @brief Compile parsed leaf-list node information.
2741 * @param[in] ctx Compile context
2742 * @param[in] pnode Parsed leaf-list node.
2743 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2744 * is enriched with the leaf-list-specific information.
2745 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2746 */
2747static LY_ERR
2748lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2749{
2750 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2751 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002752 LY_ERR ret = LY_SUCCESS;
2753
Michal Vasko5347e3a2020-11-03 17:14:57 +01002754 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002755 if (llist_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002756 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002757 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002758 LY_CHECK_GOTO(ret, done);
2759 }
2760 if (llist_p->units) {
2761 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2762 llist->flags |= LYS_SET_UNITS;
2763 }
2764
2765 /* compile type */
2766 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2767 LY_CHECK_GOTO(ret, done);
2768
2769 /* store/update default values */
2770 if (llist_p->dflts) {
2771 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002772 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002773 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2774 return LY_EVALID;
2775 }
2776
2777 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2778 llist->flags |= LYS_SET_DFLT;
2779 }
2780
2781 llist->min = llist_p->min;
2782 if (llist->min) {
2783 llist->flags |= LYS_MAND_TRUE;
2784 }
2785 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
2786
2787 /* checks */
2788 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002789 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 +02002790 return LY_EVALID;
2791 }
2792
2793 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002794 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002795 llist->min, llist->max);
2796 return LY_EVALID;
2797 }
2798
2799done:
2800 return ret;
2801}
2802
2803LY_ERR
2804lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
2805 const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, uint16_t nodetype,
2806 const struct lysc_node **target, uint16_t *result_flag)
2807{
2808 LY_ERR ret = LY_EVALID;
2809 const char *name, *prefix, *id;
2810 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002811 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002812 const char *nodeid_type;
2813 uint32_t getnext_extra_flag = 0;
2814 uint16_t current_nodetype = 0;
2815
2816 assert(nodeid);
2817 assert(target);
2818 assert(result_flag);
2819 *target = NULL;
2820 *result_flag = 0;
2821
2822 id = nodeid;
2823
2824 if (ctx_node) {
2825 /* descendant-schema-nodeid */
2826 nodeid_type = "descendant";
2827
2828 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002829 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002830 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002831 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002832 return LY_EVALID;
2833 }
2834 } else {
2835 /* absolute-schema-nodeid */
2836 nodeid_type = "absolute";
2837
2838 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002839 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002840 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002841 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002842 return LY_EVALID;
2843 }
2844 ++id;
2845 }
2846
2847 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2848 if (prefix) {
2849 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2850 if (!mod) {
2851 /* module must always be found */
2852 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002853 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002854 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002855 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002856 return LY_ENOTFOUND;
2857 }
2858 } else {
2859 switch (format) {
2860 case LY_PREF_SCHEMA:
2861 case LY_PREF_SCHEMA_RESOLVED:
2862 /* use the current module */
2863 mod = cur_mod;
2864 break;
2865 case LY_PREF_JSON:
2866 if (!ctx_node) {
2867 LOGINT_RET(ctx->ctx);
2868 }
2869 /* inherit the module of the previous context node */
2870 mod = ctx_node->module;
2871 break;
2872 case LY_PREF_XML:
2873 /* not really defined */
2874 LOGINT_RET(ctx->ctx);
2875 }
2876 }
2877
2878 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2879 /* move through input/output manually */
2880 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002881 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2882 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002883 return LY_ENOTFOUND;
2884 }
2885 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002886 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002887 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002888 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002889 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2890 } else {
2891 /* only input or output is valid */
2892 ctx_node = NULL;
2893 }
2894 } else {
2895 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002896 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002897 getnext_extra_flag = 0;
2898 }
2899 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002900 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2901 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002902 return LY_ENOTFOUND;
2903 }
2904 current_nodetype = ctx_node->nodetype;
2905
2906 if (current_nodetype == LYS_NOTIF) {
2907 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2908 } else if (current_nodetype == LYS_INPUT) {
2909 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2910 } else if (current_nodetype == LYS_OUTPUT) {
2911 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2912 }
2913
2914 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2915 break;
2916 }
2917 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002918 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002919 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002920 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002921 return LY_EVALID;
2922 }
2923 ++id;
2924 }
2925
2926 if (ret == LY_SUCCESS) {
2927 *target = ctx_node;
2928 if (nodetype && !(current_nodetype & nodetype)) {
2929 return LY_EDENIED;
2930 }
2931 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002932 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002933 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002934 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002935 }
2936
2937 return ret;
2938}
2939
2940/**
2941 * @brief Compile information about list's uniques.
2942 * @param[in] ctx Compile context.
2943 * @param[in] uniques Sized array list of unique statements.
2944 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2945 * @return LY_ERR value.
2946 */
2947static LY_ERR
2948lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2949{
2950 LY_ERR ret = LY_SUCCESS;
2951 struct lysc_node_leaf **key, ***unique;
2952 struct lysc_node *parent;
2953 const char *keystr, *delim;
2954 size_t len;
2955 LY_ARRAY_COUNT_TYPE v;
2956 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2957 uint16_t flags;
2958
2959 LY_ARRAY_FOR(uniques, v) {
2960 config = -1;
2961 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2962 keystr = uniques[v].str;
2963 while (keystr) {
2964 delim = strpbrk(keystr, " \t\n");
2965 if (delim) {
2966 len = delim - keystr;
2967 while (isspace(*delim)) {
2968 ++delim;
2969 }
2970 } else {
2971 len = strlen(keystr);
2972 }
2973
2974 /* unique node must be present */
2975 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002976 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002977 LY_PREF_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
2978 if (ret != LY_SUCCESS) {
2979 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002980 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002981 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002982 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002983 }
2984 return LY_EVALID;
2985 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002986 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002987 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01002988 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002989 return LY_EVALID;
2990 }
2991
2992 /* all referenced leafs must be of the same config type */
2993 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
2994 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002995 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002996 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
2997 return LY_EVALID;
2998 } else if ((*key)->flags & LYS_CONFIG_W) {
2999 config = 1;
3000 } else { /* LYS_CONFIG_R */
3001 config = 0;
3002 }
3003
3004 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3005 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3006 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003007 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003008 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3009 return LY_EVALID;
3010 }
3011 }
3012
3013 /* check status */
3014 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3015 (*key)->flags, (*key)->module, (*key)->name));
3016
3017 /* mark leaf as unique */
3018 (*key)->flags |= LYS_UNIQUE;
3019
3020 /* next unique value in line */
3021 keystr = delim;
3022 }
3023 /* next unique definition */
3024 }
3025
3026 return LY_SUCCESS;
3027}
3028
3029/**
3030 * @brief Compile parsed list node information.
3031 * @param[in] ctx Compile context
3032 * @param[in] pnode Parsed list node.
3033 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3034 * is enriched with the list-specific information.
3035 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3036 */
3037static LY_ERR
3038lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3039{
3040 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3041 struct lysc_node_list *list = (struct lysc_node_list *)node;
3042 struct lysp_node *child_p;
3043 struct lysc_node_leaf *key, *prev_key = NULL;
3044 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003045 const char *keystr, *delim;
3046 LY_ERR ret = LY_SUCCESS;
3047
3048 list->min = list_p->min;
3049 if (list->min) {
3050 list->flags |= LYS_MAND_TRUE;
3051 }
3052 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3053
3054 LY_LIST_FOR(list_p->child, child_p) {
3055 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3056 }
3057
Michal Vasko5347e3a2020-11-03 17:14:57 +01003058 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003059 if (list_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003060 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003061 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003062 }
3063
3064 /* keys */
3065 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003066 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003067 return LY_EVALID;
3068 }
3069
3070 /* find all the keys (must be direct children) */
3071 keystr = list_p->key;
3072 if (!keystr) {
3073 /* keyless list */
3074 list->flags |= LYS_KEYLESS;
3075 }
3076 while (keystr) {
3077 delim = strpbrk(keystr, " \t\n");
3078 if (delim) {
3079 len = delim - keystr;
3080 while (isspace(*delim)) {
3081 ++delim;
3082 }
3083 } else {
3084 len = strlen(keystr);
3085 }
3086
3087 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003088 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3089 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003090 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003091 return LY_EVALID;
3092 }
3093 /* keys must be unique */
3094 if (key->flags & LYS_KEY) {
3095 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003096 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003097 return LY_EVALID;
3098 }
3099
Radek Krejcia6016992021-03-03 10:13:41 +01003100 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003101 /* key must have the same config flag as the list itself */
3102 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003103 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003104 return LY_EVALID;
3105 }
3106 if (ctx->pmod->version < LYS_VERSION_1_1) {
3107 /* YANG 1.0 denies key to be of empty type */
3108 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003109 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003110 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3111 return LY_EVALID;
3112 }
3113 } else {
3114 /* when and if-feature are illegal on list keys */
3115 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003116 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003117 "List's key must not have any \"when\" statement.");
3118 return LY_EVALID;
3119 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003120 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003121 }
3122
3123 /* check status */
3124 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3125 key->flags, key->module, key->name));
3126
3127 /* ignore default values of the key */
3128 if (key->dflt) {
3129 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3130 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3131 free(key->dflt);
3132 key->dflt = NULL;
3133 }
3134 /* mark leaf as key */
3135 key->flags |= LYS_KEY;
3136
3137 /* move it to the correct position */
3138 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3139 /* fix links in closest previous siblings of the key */
3140 if (key->next) {
3141 key->next->prev = key->prev;
3142 } else {
3143 /* last child */
3144 list->child->prev = key->prev;
3145 }
3146 if (key->prev->next) {
3147 key->prev->next = key->next;
3148 }
3149 /* fix links in the key */
3150 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003151 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003152 key->next = prev_key->next;
3153 } else {
3154 key->prev = list->child->prev;
3155 key->next = list->child;
3156 }
3157 /* fix links in closes future siblings of the key */
3158 if (prev_key) {
3159 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003160 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003161 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003162 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003163 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003164 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003165 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003166 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003167 }
3168 /* fix links in parent */
3169 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003170 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003171 }
3172 }
3173
3174 /* next key value */
3175 prev_key = key;
3176 keystr = delim;
3177 lysc_update_path(ctx, NULL, NULL);
3178 }
3179
3180 /* uniques */
3181 if (list_p->uniques) {
3182 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3183 }
3184
Radek Krejci2a9fc652021-01-22 17:44:34 +01003185 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3186 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3187 LY_CHECK_GOTO(ret, done);
3188 }
3189 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3190 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3191 LY_CHECK_GOTO(ret, done);
3192 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003193
3194 /* checks */
3195 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003196 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003197 list->min, list->max);
3198 return LY_EVALID;
3199 }
3200
3201done:
3202 return ret;
3203}
3204
3205/**
3206 * @brief Do some checks and set the default choice's case.
3207 *
3208 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3209 *
3210 * @param[in] ctx Compile context.
3211 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3212 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3213 * @return LY_ERR value.
3214 */
3215static LY_ERR
3216lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3217{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003218 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003219 const struct lys_module *mod;
3220 const char *prefix = NULL, *name;
3221 size_t prefix_len = 0;
3222
3223 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3224 name = strchr(dflt->str, ':');
3225 if (name) {
3226 prefix = dflt->str;
3227 prefix_len = name - prefix;
3228 ++name;
3229 } else {
3230 name = dflt->str;
3231 }
3232 if (prefix) {
3233 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
3234 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003235 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003236 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003237 return LY_EVALID;
3238 }
3239 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003240 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003241 }
3242
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003243 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 +02003244 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003245 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003246 "Default case \"%s\" not found.", dflt->str);
3247 return LY_EVALID;
3248 }
3249
3250 /* no mandatory nodes directly under the default case */
3251 LY_LIST_FOR(ch->dflt->child, iter) {
3252 if (iter->parent != (struct lysc_node *)ch->dflt) {
3253 break;
3254 }
3255 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003256 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003257 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3258 return LY_EVALID;
3259 }
3260 }
3261
3262 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003263 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003264 return LY_EVALID;
3265 }
3266
3267 ch->dflt->flags |= LYS_SET_DFLT;
3268 return LY_SUCCESS;
3269}
3270
3271LY_ERR
3272lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3273 struct ly_set *child_set)
3274{
3275 LY_ERR ret = LY_SUCCESS;
3276 struct lysp_node *child_p_next = child_p->next;
3277 struct lysp_node_case *cs_p;
3278
3279 if (child_p->nodetype == LYS_CASE) {
3280 /* standard case under choice */
3281 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3282 } else {
3283 /* we need the implicit case first, so create a fake parsed case */
3284 cs_p = calloc(1, sizeof *cs_p);
3285 cs_p->nodetype = LYS_CASE;
3286 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3287 cs_p->child = child_p;
3288
3289 /* make the child the only case child */
3290 child_p->next = NULL;
3291
3292 /* compile it normally */
3293 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3294
3295free_fake_node:
3296 /* free the fake parsed node and correct pointers back */
3297 cs_p->child = NULL;
3298 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3299 child_p->next = child_p_next;
3300 }
3301
3302 return ret;
3303}
3304
3305/**
3306 * @brief Compile parsed choice node information.
3307 *
3308 * @param[in] ctx Compile context
3309 * @param[in] pnode Parsed choice node.
3310 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3311 * is enriched with the choice-specific information.
3312 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3313 */
3314static LY_ERR
3315lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3316{
3317 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3318 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3319 struct lysp_node *child_p;
3320 LY_ERR ret = LY_SUCCESS;
3321
3322 assert(node->nodetype == LYS_CHOICE);
3323
3324 LY_LIST_FOR(ch_p->child, child_p) {
3325 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3326 }
3327
3328 /* default branch */
3329 if (ch_p->dflt.str) {
3330 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3331 }
3332
3333 return ret;
3334}
3335
3336/**
3337 * @brief Compile parsed anydata or anyxml node information.
3338 * @param[in] ctx Compile context
3339 * @param[in] pnode Parsed anydata or anyxml node.
3340 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3341 * is enriched with the any-specific information.
3342 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3343 */
3344static LY_ERR
3345lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3346{
3347 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3348 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003349 LY_ERR ret = LY_SUCCESS;
3350
Michal Vasko5347e3a2020-11-03 17:14:57 +01003351 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003352 if (any_p->musts && !(ctx->options & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003353 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003354 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003355 LY_CHECK_GOTO(ret, done);
3356 }
3357
Radek Krejci91531e12021-02-08 19:57:54 +01003358 if (any->flags & LYS_CONFIG_W) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003359 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
3360 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3361 }
3362done:
3363 return ret;
3364}
3365
3366/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003367 * @brief Prepare the case structure in choice node for the new data node.
3368 *
3369 * 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
3370 * created in the choice when the first child was processed.
3371 *
3372 * @param[in] ctx Compile context.
3373 * @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,
3374 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3375 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3376 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3377 * @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,
3378 * it is linked from the case structure only in case it is its first child.
3379 */
3380static LY_ERR
3381lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3382{
3383 struct lysp_node *child_p;
3384 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3385
3386 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3387 /* we have to add an implicit case node into the parent choice */
3388 } else if (pnode->nodetype == LYS_CASE) {
3389 /* explicit parent case */
3390 LY_LIST_FOR(cs_p->child, child_p) {
3391 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3392 }
3393 } else {
3394 LOGINT_RET(ctx->ctx);
3395 }
3396
3397 return LY_SUCCESS;
3398}
3399
3400void
3401lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3402{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003403 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003404
3405 if (add) { /* set flag */
3406 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3407 parent = parent->parent) {
3408 parent->flags |= LYS_MAND_TRUE;
3409 }
3410 } else { /* unset flag */
3411 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003412 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003413 if (iter->flags & LYS_MAND_TRUE) {
3414 /* there is another mandatory node */
3415 return;
3416 }
3417 }
3418 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3419 parent->flags &= ~LYS_MAND_TRUE;
3420 }
3421 }
3422}
3423
3424/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003425 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003426 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003427 * @param[in] name Name of the grouping to find,
3428 * @return NULL when there is no grouping with the specified name
3429 * @return Pointer to the grouping of the specified @p name.
3430 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003431static struct lysp_node_grp *
3432match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003433{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003434 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003435
Radek Krejci2a9fc652021-01-22 17:44:34 +01003436 LY_LIST_FOR(grps, grp) {
3437 if (!strcmp(grp->name, name)) {
3438 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003439 }
3440 }
3441
3442 return NULL;
3443}
3444
3445/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003446 * @brief Find grouping for a uses.
3447 *
3448 * @param[in] ctx Compile context.
3449 * @param[in] uses_p Parsed uses node.
3450 * @param[out] gpr_p Found grouping on success.
3451 * @param[out] grp_pmod Module of @p grp_p on success.
3452 * @return LY_ERR value.
3453 */
3454static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003455lys_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 +02003456 struct lysp_module **grp_pmod)
3457{
3458 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003459 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003460 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003461 const char *id, *name, *prefix, *local_pref;
3462 size_t prefix_len, name_len;
3463 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003464 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003465
3466 *grp_p = NULL;
3467 *grp_pmod = NULL;
3468
3469 /* search for the grouping definition */
3470 id = uses_p->name;
3471 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3472 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3473 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3474 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003475 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003476 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003477 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3478 found = ctx->pmod;
3479 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003480 }
3481 }
3482 } else {
3483 /* foreign module, find it first */
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003484 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003485 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003486 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003487 "Invalid prefix used for grouping reference.", uses_p->name);
3488 return LY_EVALID;
3489 }
3490 pmod = mod->parsed;
3491 }
3492
3493 if (!found) {
3494 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003495 if ((grp = match_grouping(pmod->groupings, name))) {
3496 found = pmod;
3497 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003498 /* ... and all the submodules */
3499 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003500 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3501 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003502 break;
3503 }
3504 }
3505 }
3506 }
3507 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003508 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003509 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3510 return LY_EVALID;
3511 }
3512
3513 if (!(ctx->options & LYS_COMPILE_GROUPING)) {
3514 /* remember that the grouping is instantiated to avoid its standalone validation */
3515 grp->flags |= LYS_USED_GRP;
3516 }
3517
3518 *grp_p = grp;
3519 *grp_pmod = found;
3520 return LY_SUCCESS;
3521}
3522
3523/**
3524 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3525 * If present, also apply uses's modificators.
3526 *
3527 * @param[in] ctx Compile context
3528 * @param[in] uses_p Parsed uses schema node.
3529 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3530 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3531 * the compile context.
3532 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3533 */
3534static LY_ERR
3535lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3536{
3537 struct lysp_node *pnode;
3538 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003539 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003540 uint32_t i, grp_stack_count;
3541 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3542 LY_ERR ret = LY_SUCCESS;
3543 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003544 struct ly_set uses_child_set = {0};
3545
3546 /* find the referenced grouping */
3547 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3548
3549 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3550 grp_stack_count = ctx->groupings.count;
3551 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3552 if (grp_stack_count == ctx->groupings.count) {
3553 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003554 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003555 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3556 return LY_EVALID;
3557 }
3558
3559 /* check status */
3560 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3561 LY_CHECK_GOTO(ret, cleanup);
3562
3563 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3564 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3565 LY_CHECK_GOTO(ret, cleanup);
3566
3567 /* switch context's parsed module being processed */
3568 ctx->pmod = grp_mod;
3569
3570 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003571 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003572 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3573 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 +02003574 LY_CHECK_GOTO(ret, cleanup);
3575 }
3576
3577 if (child_set) {
3578 /* add these children to our compiled child_set as well since uses is a schema-only node */
3579 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3580 }
3581
3582 if (uses_p->when) {
3583 /* pass uses's when to all the data children */
3584 for (i = 0; i < uses_child_set.count; ++i) {
3585 child = uses_child_set.snodes[i];
3586
Michal Vasko72244882021-01-12 15:21:05 +01003587 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003588 LY_CHECK_GOTO(ret, cleanup);
3589 }
3590 }
3591
3592 /* compile actions */
3593 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003594 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003595 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3596 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003597 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003598 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3599 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003600 ret = LY_EVALID;
3601 goto cleanup;
3602 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003603 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3604 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3605 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3606 LY_CHECK_GOTO(ret, cleanup);
3607 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003608
3609 if (uses_p->when) {
3610 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003611 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3612 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 +02003613 LY_CHECK_GOTO(ret, cleanup);
3614 }
3615 }
3616 }
3617
3618 /* compile notifications */
3619 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003620 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003621 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3622 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003623 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003624 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3625 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003626 ret = LY_EVALID;
3627 goto cleanup;
3628 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003629
3630 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3631 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3632 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3633 LY_CHECK_GOTO(ret, cleanup);
3634 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003635
3636 if (uses_p->when) {
3637 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003638 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3639 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 +02003640 LY_CHECK_GOTO(ret, cleanup);
3641 }
3642 }
3643 }
3644
3645 /* check that all augments were applied */
3646 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003647 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3648 /* augment of some parent uses, irrelevant now */
3649 continue;
3650 }
3651
3652 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003653 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3654 ret = LY_ENOTFOUND;
3655 }
3656 LY_CHECK_GOTO(ret, cleanup);
3657
3658 /* check that all refines were applied */
3659 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003660 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3661 /* refine of some paretn uses, irrelevant now */
3662 continue;
3663 }
3664
3665 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003666 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3667 ret = LY_ENOTFOUND;
3668 }
3669 LY_CHECK_GOTO(ret, cleanup);
3670
3671cleanup:
3672 /* reload previous context's parsed module being processed */
3673 ctx->pmod = mod_old;
3674
3675 /* remove the grouping from the stack for circular groupings dependency check */
3676 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3677 assert(ctx->groupings.count == grp_stack_count);
3678
3679 ly_set_erase(&uses_child_set, NULL);
3680 return ret;
3681}
3682
3683static int
3684lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3685{
3686 struct lysp_node *iter;
3687 int len = 0;
3688
3689 *path = NULL;
3690 for (iter = node; iter && len >= 0; iter = iter->parent) {
3691 char *s = *path;
3692 char *id;
3693
3694 switch (iter->nodetype) {
3695 case LYS_USES:
3696 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3697 break;
3698 case LYS_GROUPING:
3699 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3700 break;
3701 case LYS_AUGMENT:
3702 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3703 break;
3704 default:
3705 id = strdup(iter->name);
3706 break;
3707 }
3708
3709 if (!iter->parent) {
3710 /* print prefix */
3711 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3712 } else {
3713 /* prefix is the same as in parent */
3714 len = asprintf(path, "/%s%s", id, s ? s : "");
3715 }
3716 free(s);
3717 free(id);
3718 }
3719
3720 if (len < 0) {
3721 free(*path);
3722 *path = NULL;
3723 } else if (len == 0) {
3724 *path = strdup("/");
3725 len = 1;
3726 }
3727 return len;
3728}
3729
3730LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003731lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003732{
3733 LY_ERR ret;
3734 char *path;
3735 int len;
3736
3737 struct lysp_node_uses fake_uses = {
3738 .parent = pnode,
3739 .nodetype = LYS_USES,
3740 .flags = 0, .next = NULL,
3741 .name = grp->name,
3742 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3743 .refines = NULL, .augments = NULL
3744 };
3745 struct lysc_node_container fake_container = {
3746 .nodetype = LYS_CONTAINER,
3747 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3748 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003749 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003750 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003751 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003752 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003753 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3754 };
3755
3756 if (grp->parent) {
3757 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3758 }
3759
3760 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3761 if (len < 0) {
3762 LOGMEM(ctx->ctx);
3763 return LY_EMEM;
3764 }
3765 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3766 ctx->path_len = (uint32_t)len;
3767 free(path);
3768
3769 lysc_update_path(ctx, NULL, "{grouping}");
3770 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003771 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003772 lysc_update_path(ctx, NULL, NULL);
3773 lysc_update_path(ctx, NULL, NULL);
3774
3775 ctx->path_len = 1;
3776 ctx->path[1] = '\0';
3777
3778 /* cleanup */
3779 lysc_node_container_free(ctx->ctx, &fake_container);
3780
3781 return ret;
3782}
3783
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003784LY_ERR
3785lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3786 struct ly_set *child_set)
3787{
3788 LY_ERR ret = LY_SUCCESS;
3789 struct lysc_node *node = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003790 uint32_t prev_opts = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003791
3792 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3793
3794 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003795 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003796 } else {
3797 lysc_update_path(ctx, NULL, "{uses}");
3798 lysc_update_path(ctx, NULL, pnode->name);
3799 }
3800
3801 switch (pnode->nodetype) {
3802 case LYS_CONTAINER:
3803 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3804 node_compile_spec = lys_compile_node_container;
3805 break;
3806 case LYS_LEAF:
3807 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3808 node_compile_spec = lys_compile_node_leaf;
3809 break;
3810 case LYS_LIST:
3811 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3812 node_compile_spec = lys_compile_node_list;
3813 break;
3814 case LYS_LEAFLIST:
3815 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3816 node_compile_spec = lys_compile_node_leaflist;
3817 break;
3818 case LYS_CHOICE:
3819 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3820 node_compile_spec = lys_compile_node_choice;
3821 break;
3822 case LYS_CASE:
3823 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3824 node_compile_spec = lys_compile_node_case;
3825 break;
3826 case LYS_ANYXML:
3827 case LYS_ANYDATA:
3828 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3829 node_compile_spec = lys_compile_node_any;
3830 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003831 case LYS_RPC:
3832 case LYS_ACTION:
Radek Krejci91531e12021-02-08 19:57:54 +01003833 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003834 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3835 "Action \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003836 (ctx->options & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003837 return LY_EVALID;
3838 }
3839 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3840 node_compile_spec = lys_compile_node_action;
Radek Krejci91531e12021-02-08 19:57:54 +01003841 ctx->options |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003842 break;
3843 case LYS_NOTIF:
Radek Krejci91531e12021-02-08 19:57:54 +01003844 if (ctx->options & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003845 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3846 "Notification \"%s\" is placed inside %s.", pnode->name,
Radek Krejci91531e12021-02-08 19:57:54 +01003847 (ctx->options & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003848 return LY_EVALID;
3849 }
3850 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3851 node_compile_spec = lys_compile_node_notif;
3852 ctx->options |= LYS_COMPILE_NOTIFICATION;
3853 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003854 case LYS_USES:
3855 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3856 lysc_update_path(ctx, NULL, NULL);
3857 lysc_update_path(ctx, NULL, NULL);
3858 return ret;
3859 default:
3860 LOGINT(ctx->ctx);
3861 return LY_EINT;
3862 }
3863 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3864
Radek Krejci2a9fc652021-01-22 17:44:34 +01003865 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003866
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003867 ctx->options = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003868 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003869 return ret;
3870}