blob: ee177dad2615198c20f9a8733bec0a293d6cfa00 [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
Christian Hopps32874e12021-05-01 09:43:54 -040015#define _GNU_SOURCE /* asprintf, strdup */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020016
17#include "schema_compile_node.h"
18
19#include <assert.h>
20#include <ctype.h>
21#include <stddef.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "common.h"
28#include "compat.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020029#include "dict.h"
30#include "log.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020031#include "plugins.h"
Radek Krejci77114102021-03-10 15:21:57 +010032#include "plugins_exts_compile.h"
Radek Krejci04699f02021-03-22 21:50:29 +010033#include "plugins_internal.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020034#include "plugins_types.h"
35#include "schema_compile.h"
36#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010037#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "set.h"
39#include "tree.h"
40#include "tree_data.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020042#include "tree_schema.h"
43#include "tree_schema_internal.h"
44#include "xpath.h"
45
46static struct lysc_ext_instance *
47lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
48{
49 /* TODO - extensions, increase refcount */
50 (void) ctx;
51 (void) orig;
52 return NULL;
53}
54
55/**
56 * @brief Add/replace a leaf default value in unres.
57 * Can also be used for a single leaf-list default value.
58 *
59 * @param[in] ctx Compile context.
60 * @param[in] leaf Leaf with the default value.
61 * @param[in] dflt Default value to use.
62 * @return LY_ERR value.
63 */
64static LY_ERR
65lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
66{
67 struct lysc_unres_dflt *r = NULL;
68 uint32_t i;
69
Michal Vasko7c565922021-06-10 14:58:27 +020070 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010071 return LY_SUCCESS;
72 }
73
Michal Vasko405cc9e2020-12-01 12:01:27 +010074 for (i = 0; i < ctx->unres->dflts.count; ++i) {
75 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020076 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010077 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020078 lysp_qname_free(ctx->ctx, r->dflt);
79 free(r->dflt);
80 break;
81 }
82 }
83 if (!r) {
84 /* add new unres item */
85 r = calloc(1, sizeof *r);
86 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
87 r->leaf = leaf;
88
Michal Vasko405cc9e2020-12-01 12:01:27 +010089 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020090 }
91
92 r->dflt = malloc(sizeof *r->dflt);
93 LY_CHECK_GOTO(!r->dflt, error);
94 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
95
96 return LY_SUCCESS;
97
98error:
99 free(r->dflt);
100 LOGMEM(ctx->ctx);
101 return LY_EMEM;
102}
103
104/**
105 * @brief Add/replace a leaf-list default value(s) in unres.
106 *
107 * @param[in] ctx Compile context.
108 * @param[in] llist Leaf-list with the default value.
109 * @param[in] dflts Sized array of the default values.
110 * @return LY_ERR value.
111 */
112static LY_ERR
113lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
114{
115 struct lysc_unres_dflt *r = NULL;
116 uint32_t i;
117
Michal Vasko7c565922021-06-10 14:58:27 +0200118 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100119 return LY_SUCCESS;
120 }
121
Michal Vasko405cc9e2020-12-01 12:01:27 +0100122 for (i = 0; i < ctx->unres->dflts.count; ++i) {
123 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200124 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100125 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200126 lysp_qname_free(ctx->ctx, r->dflt);
127 free(r->dflt);
128 r->dflt = NULL;
129 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
130 r->dflts = NULL;
131 break;
132 }
133 }
134 if (!r) {
135 r = calloc(1, sizeof *r);
136 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
137 r->llist = llist;
138
Michal Vasko405cc9e2020-12-01 12:01:27 +0100139 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200140 }
141
142 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
143
144 return LY_SUCCESS;
145}
146
147/**
148 * @brief Duplicate the compiled pattern structure.
149 *
150 * Instead of duplicating memory, the reference counter in the @p orig is increased.
151 *
152 * @param[in] orig The pattern structure to duplicate.
153 * @return The duplicated structure to use.
154 */
155static struct lysc_pattern *
156lysc_pattern_dup(struct lysc_pattern *orig)
157{
158 ++orig->refcount;
159 return orig;
160}
161
162/**
163 * @brief Duplicate the array of compiled patterns.
164 *
165 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
166 *
167 * @param[in] ctx Libyang context for logging.
168 * @param[in] orig The patterns sized array to duplicate.
169 * @return New sized array as a copy of @p orig.
170 * @return NULL in case of memory allocation error.
171 */
172static struct lysc_pattern **
173lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
174{
175 struct lysc_pattern **dup = NULL;
176 LY_ARRAY_COUNT_TYPE u;
177
178 assert(orig);
179
180 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
181 LY_ARRAY_FOR(orig, u) {
182 dup[u] = lysc_pattern_dup(orig[u]);
183 LY_ARRAY_INCREMENT(dup);
184 }
185 return dup;
186}
187
188/**
189 * @brief Duplicate compiled range structure.
190 *
191 * @param[in] ctx Libyang context for logging.
192 * @param[in] orig The range structure to be duplicated.
193 * @return New compiled range structure as a copy of @p orig.
194 * @return NULL in case of memory allocation error.
195 */
196static struct lysc_range *
197lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
198{
199 struct lysc_range *dup;
200 LY_ERR ret;
201
202 assert(orig);
203
204 dup = calloc(1, sizeof *dup);
205 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
206 if (orig->parts) {
207 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100208 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200209 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
210 }
211 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
212 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
213 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
214
215 return dup;
216cleanup:
217 free(dup);
218 (void) ret; /* set but not used due to the return type */
219 return NULL;
220}
221
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200222/**
223 * @brief Compile information from the when statement
224 *
225 * @param[in] ctx Compile context.
226 * @param[in] when_p Parsed when structure.
227 * @param[in] flags Flags of the parsed node with the when statement.
228 * @param[in] ctx_node Context node for the when statement.
229 * @param[out] when Pointer where to store pointer to the created compiled when structure.
230 * @return LY_ERR value.
231 */
232static LY_ERR
233lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
234 struct lysc_when **when)
235{
236 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200237 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200238
239 *when = calloc(1, sizeof **when);
240 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
241 (*when)->refcount = 1;
242 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200243 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200244 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200245 (*when)->context = (struct lysc_node *)ctx_node;
246 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
247 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100248 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200249 (*when)->flags = flags & LYS_STATUS_MASK;
250
251done:
252 return ret;
253}
254
255LY_ERR
256lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
257 struct lysc_node *node, struct lysc_when **when_c)
258{
259 struct lysc_when **new_when, ***node_when;
260
261 assert(when_p);
262
263 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100264 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200265
266 /* create new when pointer */
267 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
268 if (!when_c || !(*when_c)) {
269 /* compile when */
270 LY_CHECK_RET(lys_compile_when_(ctx, when_p, flags, ctx_node, new_when));
271
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200272 /* remember the compiled when for sharing */
273 if (when_c) {
274 *when_c = *new_when;
275 }
276 } else {
277 /* use the previously compiled when */
278 ++(*when_c)->refcount;
279 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100280 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200281
Michal Vasko7c565922021-06-10 14:58:27 +0200282 if (!(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100283 /* do not check "when" semantics in a grouping, but repeat the check for different node because
284 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100285 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200286 }
287
288 return LY_SUCCESS;
289}
290
291/**
292 * @brief Compile information from the must statement
293 * @param[in] ctx Compile context.
294 * @param[in] must_p The parsed must statement structure.
295 * @param[in,out] must Prepared (empty) compiled must structure to fill.
296 * @return LY_ERR value.
297 */
298static LY_ERR
299lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
300{
301 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200302 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200303
304 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200305 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
Radek Krejci8df109d2021-04-23 12:19:08 +0200306 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200307 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
308 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
309 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
310 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100311 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200312
313done:
314 return ret;
315}
316
317/**
318 * @brief Validate and normalize numeric value from a range definition.
319 * @param[in] ctx Compile context.
320 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
321 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
322 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
323 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
324 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
325 * @param[in] value String value of the range boundary.
326 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
327 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
328 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
329 */
330static LY_ERR
331range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
332 size_t *len, char **valcopy)
333{
334 size_t fraction = 0, size;
335
336 *len = 0;
337
338 assert(value);
339 /* parse value */
340 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
341 return LY_EVALID;
342 }
343
344 if ((value[*len] == '-') || (value[*len] == '+')) {
345 ++(*len);
346 }
347
348 while (isdigit(value[*len])) {
349 ++(*len);
350 }
351
352 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
353 if (basetype == LY_TYPE_DEC64) {
354 goto decimal;
355 } else {
356 *valcopy = strndup(value, *len);
357 return LY_SUCCESS;
358 }
359 }
360 fraction = *len;
361
362 ++(*len);
363 while (isdigit(value[*len])) {
364 ++(*len);
365 }
366
367 if (basetype == LY_TYPE_DEC64) {
368decimal:
369 assert(frdigits);
370 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100371 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200372 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100373 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200374 return LY_EINVAL;
375 }
376 if (fraction) {
377 size = (*len) + (frdigits - ((*len) - 1 - fraction));
378 } else {
379 size = (*len) + frdigits + 1;
380 }
381 *valcopy = malloc(size * sizeof **valcopy);
382 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
383
384 (*valcopy)[size - 1] = '\0';
385 if (fraction) {
386 memcpy(&(*valcopy)[0], &value[0], fraction);
387 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
388 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
389 } else {
390 memcpy(&(*valcopy)[0], &value[0], *len);
391 memset(&(*valcopy)[*len], '0', frdigits);
392 }
393 }
394 return LY_SUCCESS;
395}
396
397/**
398 * @brief Check that values in range are in ascendant order.
399 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
400 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
401 * max can be also equal.
402 * @param[in] value Current value to check.
403 * @param[in] prev_value The last seen value.
404 * @return LY_SUCCESS or LY_EEXIST for invalid order.
405 */
406static LY_ERR
407range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
408{
409 if (unsigned_value) {
410 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
411 return LY_EEXIST;
412 }
413 } else {
414 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
415 return LY_EEXIST;
416 }
417 }
418 return LY_SUCCESS;
419}
420
421/**
422 * @brief Set min/max value of the range part.
423 * @param[in] ctx Compile context.
424 * @param[in] part Range part structure to fill.
425 * @param[in] max Flag to distinguish if storing min or max value.
426 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
427 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
428 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
429 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
430 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
431 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
432 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
433 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
434 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
435 * frdigits value), LY_EMEM.
436 */
437static LY_ERR
438range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
439 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
440{
441 LY_ERR ret = LY_SUCCESS;
442 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100443 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200444
445 if (value) {
446 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
447 LY_CHECK_GOTO(ret, finalize);
448 }
449 if (!valcopy && base_range) {
450 if (max) {
451 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
452 } else {
453 part->min_64 = base_range->parts[0].min_64;
454 }
455 if (!first) {
456 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
457 }
458 goto finalize;
459 }
460
461 switch (basetype) {
462 case LY_TYPE_INT8: /* range */
463 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100464 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200465 } else if (max) {
466 part->max_64 = INT64_C(127);
467 } else {
468 part->min_64 = INT64_C(-128);
469 }
470 if (!ret && !first) {
471 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
472 }
473 break;
474 case LY_TYPE_INT16: /* range */
475 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100476 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
477 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200478 } else if (max) {
479 part->max_64 = INT64_C(32767);
480 } else {
481 part->min_64 = INT64_C(-32768);
482 }
483 if (!ret && !first) {
484 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
485 }
486 break;
487 case LY_TYPE_INT32: /* range */
488 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100489 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
490 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200491 } else if (max) {
492 part->max_64 = INT64_C(2147483647);
493 } else {
494 part->min_64 = INT64_C(-2147483648);
495 }
496 if (!ret && !first) {
497 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
498 }
499 break;
500 case LY_TYPE_INT64: /* range */
501 case LY_TYPE_DEC64: /* range */
502 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100503 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
504 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200505 } else if (max) {
506 part->max_64 = INT64_C(9223372036854775807);
507 } else {
508 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
509 }
510 if (!ret && !first) {
511 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
512 }
513 break;
514 case LY_TYPE_UINT8: /* range */
515 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100516 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200517 } else if (max) {
518 part->max_u64 = UINT64_C(255);
519 } else {
520 part->min_u64 = UINT64_C(0);
521 }
522 if (!ret && !first) {
523 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
524 }
525 break;
526 case LY_TYPE_UINT16: /* range */
527 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100528 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200529 } else if (max) {
530 part->max_u64 = UINT64_C(65535);
531 } else {
532 part->min_u64 = UINT64_C(0);
533 }
534 if (!ret && !first) {
535 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
536 }
537 break;
538 case LY_TYPE_UINT32: /* range */
539 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100540 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
541 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200542 } else if (max) {
543 part->max_u64 = UINT64_C(4294967295);
544 } else {
545 part->min_u64 = UINT64_C(0);
546 }
547 if (!ret && !first) {
548 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
549 }
550 break;
551 case LY_TYPE_UINT64: /* range */
552 case LY_TYPE_STRING: /* length */
553 case LY_TYPE_BINARY: /* length */
554 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100555 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
556 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200557 } else if (max) {
558 part->max_u64 = UINT64_C(18446744073709551615);
559 } else {
560 part->min_u64 = UINT64_C(0);
561 }
562 if (!ret && !first) {
563 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
564 }
565 break;
566 default:
567 LOGINT(ctx->ctx);
568 ret = LY_EINT;
569 }
570
571finalize:
572 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100573 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200574 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
575 length_restr ? "length" : "range", valcopy ? valcopy : *value);
576 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100577 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200578 "Invalid %s restriction - invalid value \"%s\".",
579 length_restr ? "length" : "range", valcopy ? valcopy : *value);
580 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100581 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200582 "Invalid %s restriction - values are not in ascending order (%s).",
583 length_restr ? "length" : "range",
584 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
585 } else if (!ret && value) {
586 *value = *value + len;
587 }
588 free(valcopy);
589 return ret;
590}
591
592/**
593 * @brief Compile the parsed range restriction.
594 * @param[in] ctx Compile context.
595 * @param[in] range_p Parsed range structure to compile.
596 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
597 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
598 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
599 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
600 * range restriction must be more restrictive than the base_range.
601 * @param[in,out] range Pointer to the created current range structure.
602 * @return LY_ERR value.
603 */
604static LY_ERR
605lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
606 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
607{
aPiecek1c4da362021-04-29 14:26:34 +0200608 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200609 const char *expr;
610 struct lysc_range_part *parts = NULL, *part;
611 ly_bool range_expected = 0, uns;
612 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
613
614 assert(range);
615 assert(range_p);
616
617 expr = range_p->arg.str;
618 while (1) {
619 if (isspace(*expr)) {
620 ++expr;
621 } else if (*expr == '\0') {
622 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200624 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
625 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200626 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200627 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);
aPiecek1c4da362021-04-29 14:26:34 +0200632 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200633 goto cleanup;
634 }
635 parts_done++;
636 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100637 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200638 if (parts) {
639 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100640 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200641 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100642 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200643 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200644 goto cleanup;
645 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100646 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200647
648 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200649 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200650 part->max_64 = part->min_64;
651 } else if (*expr == '|') {
652 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100653 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200654 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200655 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200656 goto cleanup;
657 }
658 expr++;
659 parts_done++;
660 /* process next part of the expression */
661 } else if (!strncmp(expr, "..", 2)) {
662 expr += 2;
663 while (isspace(*expr)) {
664 expr++;
665 }
666 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100667 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200668 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200669 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200670 goto cleanup;
671 }
672 /* continue expecting the upper boundary */
673 range_expected = 1;
674 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
675 /* number */
676 if (range_expected) {
677 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200678 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200679 range_expected = 0;
680 } else {
681 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200682 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200683 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
684 part->max_64 = part->min_64;
685 }
686
687 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100688 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
689 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200690 while (isspace(*expr)) {
691 expr++;
692 }
693 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100694 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200695 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200696 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200697 goto cleanup;
698 }
699 if (range_expected) {
700 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200701 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200702 range_expected = 0;
703 } else {
704 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200705 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200706 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
707 part->min_64 = part->max_64;
708 }
709 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100710 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200711 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200712 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200713 goto cleanup;
714 }
715 }
716
717 /* check with the previous range/length restriction */
718 if (base_range) {
719 switch (basetype) {
720 case LY_TYPE_BINARY:
721 case LY_TYPE_UINT8:
722 case LY_TYPE_UINT16:
723 case LY_TYPE_UINT32:
724 case LY_TYPE_UINT64:
725 case LY_TYPE_STRING:
726 uns = 1;
727 break;
728 case LY_TYPE_DEC64:
729 case LY_TYPE_INT8:
730 case LY_TYPE_INT16:
731 case LY_TYPE_INT32:
732 case LY_TYPE_INT64:
733 uns = 0;
734 break;
735 default:
736 LOGINT(ctx->ctx);
737 ret = LY_EINT;
738 goto cleanup;
739 }
740 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
741 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
742 goto baseerror;
743 }
744 /* current lower bound is not lower than the base */
745 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
746 /* base has single value */
747 if (base_range->parts[v].min_64 == parts[u].min_64) {
748 /* both lower bounds are the same */
749 if (parts[u].min_64 != parts[u].max_64) {
750 /* current continues with a range */
751 goto baseerror;
752 } else {
753 /* equal single values, move both forward */
754 ++v;
755 continue;
756 }
757 } else {
758 /* base is single value lower than current range, so the
759 * value from base range is removed in the current,
760 * move only base and repeat checking */
761 ++v;
762 --u;
763 continue;
764 }
765 } else {
766 /* base is the range */
767 if (parts[u].min_64 == parts[u].max_64) {
768 /* current is a single value */
769 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
770 /* current is behind the base range, so base range is omitted,
771 * move the base and keep the current for further check */
772 ++v;
773 --u;
774 } /* else it is within the base range, so move the current, but keep the base */
775 continue;
776 } else {
777 /* both are ranges - check the higher bound, the lower was already checked */
778 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
779 /* higher bound is higher than the current higher bound */
780 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
781 /* but the current lower bound is also higher, so the base range is omitted,
782 * continue with the same current, but move the base */
783 --u;
784 ++v;
785 continue;
786 }
787 /* current range starts within the base range but end behind it */
788 goto baseerror;
789 } else {
790 /* current range is smaller than the base,
791 * move current, but stay with the base */
792 continue;
793 }
794 }
795 }
796 }
797 if (u != parts_done) {
798baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100799 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200800 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
801 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200802 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200803 goto cleanup;
804 }
805 }
806
807 if (!(*range)) {
808 *range = calloc(1, sizeof **range);
809 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
810 }
811
812 /* we rewrite the following values as the types chain is being processed */
813 if (range_p->eapptag) {
814 lydict_remove(ctx->ctx, (*range)->eapptag);
815 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
816 }
817 if (range_p->emsg) {
818 lydict_remove(ctx->ctx, (*range)->emsg);
819 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
820 }
821 if (range_p->dsc) {
822 lydict_remove(ctx->ctx, (*range)->dsc);
823 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
824 }
825 if (range_p->ref) {
826 lydict_remove(ctx->ctx, (*range)->ref);
827 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
828 }
829 /* extensions are taken only from the last range by the caller */
830
831 (*range)->parts = parts;
832 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200833cleanup:
834 LY_ARRAY_FREE(parts);
835
836 return ret;
837}
838
839LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100840lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200841{
842 size_t idx, idx2, start, end, size, brack;
843 char *perl_regex, *ptr;
Christian Hoppsdafed222021-03-22 13:02:42 -0400844 int err_code, compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200845 const char *orig_ptr;
846 PCRE2_SIZE err_offset;
847 pcre2_code *code_local;
848
849#define URANGE_LEN 19
850 char *ublock2urange[][2] = {
851 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
852 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
853 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
854 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
855 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
856 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
857 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
858 {"Greek", "[\\x{0370}-\\x{03FF}]"},
859 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
860 {"Armenian", "[\\x{0530}-\\x{058F}]"},
861 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
862 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
863 {"Syriac", "[\\x{0700}-\\x{074F}]"},
864 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
865 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
866 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
867 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
868 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
869 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
870 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
871 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
872 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
873 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
874 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
875 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
876 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
877 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
878 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
879 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
880 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
881 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
882 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
883 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
884 {"Ogham", "[\\x{1680}-\\x{169F}]"},
885 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
886 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
887 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
888 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
889 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
890 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
891 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
892 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
893 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
894 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
895 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
896 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
897 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
898 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
899 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
900 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
901 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
902 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
903 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
904 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
905 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
906 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
907 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
908 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
909 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
910 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
911 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
912 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
913 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
914 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
915 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
916 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
917 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
918 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
919 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
920 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
921 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
922 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
923 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
924 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
925 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
926 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
927 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
928 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
929 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
930 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
931 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
932 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
933 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100934 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200935 {NULL, NULL}
936 };
937
938 /* adjust the expression to a Perl equivalent
939 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
940
941 /* allocate space for the transformed pattern */
942 size = strlen(pattern) + 1;
Christian Hoppsdafed222021-03-22 13:02:42 -0400943 compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
944#ifdef PCRE2_ENDANCHORED
945 compile_opts |= PCRE2_ENDANCHORED;
946#else
947 /* add space for trailing $ anchor */
948 size++;
949#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200950 perl_regex = malloc(size);
951 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
952 perl_regex[0] = '\0';
953
954 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
955 brack = 0;
956 idx = 0;
957 orig_ptr = pattern;
958 while (orig_ptr[0]) {
959 switch (orig_ptr[0]) {
960 case '$':
961 case '^':
962 if (!brack) {
963 /* make space for the extra character */
964 ++size;
965 perl_regex = ly_realloc(perl_regex, size);
966 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
967
968 /* print escape slash */
969 perl_regex[idx] = '\\';
970 ++idx;
971 }
972 break;
973 case '[':
974 /* must not be escaped */
975 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
976 ++brack;
977 }
978 break;
979 case ']':
980 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
981 /* pattern was checked and compiled already */
982 assert(brack);
983 --brack;
984 }
985 break;
986 default:
987 break;
988 }
989
990 /* copy char */
991 perl_regex[idx] = orig_ptr[0];
992
993 ++idx;
994 ++orig_ptr;
995 }
Christian Hoppsdafed222021-03-22 13:02:42 -0400996#ifndef PCRE2_ENDANCHORED
997 /* anchor match to end of subject */
998 perl_regex[idx++] = '$';
999#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001000 perl_regex[idx] = '\0';
1001
1002 /* substitute Unicode Character Blocks with exact Character Ranges */
1003 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1004 start = ptr - perl_regex;
1005
1006 ptr = strchr(ptr, '}');
1007 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001008 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001009 free(perl_regex);
1010 return LY_EVALID;
1011 }
1012 end = (ptr - perl_regex) + 1;
1013
1014 /* need more space */
1015 if (end - start < URANGE_LEN) {
1016 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1017 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1018 }
1019
1020 /* find our range */
1021 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001022 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1023 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001024 break;
1025 }
1026 }
1027 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001028 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001029 free(perl_regex);
1030 return LY_EVALID;
1031 }
1032
1033 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1034 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1035 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1036 ++idx;
1037 }
1038 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1039 --idx;
1040 }
1041 }
1042 if (idx) {
1043 /* skip brackets */
1044 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1045 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1046 } else {
1047 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1048 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1049 }
1050 }
1051
1052 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001053 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001054 &err_code, &err_offset, NULL);
1055 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001056 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1057 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001058 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001059 free(perl_regex);
1060 return LY_EVALID;
1061 }
1062 free(perl_regex);
1063
1064 if (code) {
1065 *code = code_local;
1066 } else {
1067 free(code_local);
1068 }
1069
1070 return LY_SUCCESS;
1071
1072#undef URANGE_LEN
1073}
1074
1075/**
1076 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1077 * @param[in] ctx Compile context.
1078 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1079 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1080 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1081 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1082 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1083 */
1084static LY_ERR
1085lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1086 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1087{
1088 struct lysc_pattern **pattern;
1089 LY_ARRAY_COUNT_TYPE u;
1090 LY_ERR ret = LY_SUCCESS;
1091
1092 /* first, copy the patterns from the base type */
1093 if (base_patterns) {
1094 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1095 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1096 }
1097
1098 LY_ARRAY_FOR(patterns_p, u) {
1099 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1100 *pattern = calloc(1, sizeof **pattern);
1101 ++(*pattern)->refcount;
1102
Radek Krejci2efc45b2020-12-22 16:25:44 +01001103 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001104 LY_CHECK_RET(ret);
1105
Radek Krejcif13b87b2020-12-01 22:02:17 +01001106 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001107 (*pattern)->inverted = 1;
1108 }
1109 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1110 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1111 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1112 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1113 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001114 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001115 }
1116done:
1117 return ret;
1118}
1119
1120/**
1121 * @brief map of the possible restrictions combination for the specific built-in type.
1122 */
1123static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1124 0 /* LY_TYPE_UNKNOWN */,
1125 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1126 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1127 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1128 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1129 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1130 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1131 LYS_SET_BIT /* LY_TYPE_BITS */,
1132 0 /* LY_TYPE_BOOL */,
1133 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1134 0 /* LY_TYPE_EMPTY */,
1135 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1136 LYS_SET_BASE /* LY_TYPE_IDENT */,
1137 LYS_SET_REQINST /* LY_TYPE_INST */,
1138 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1139 LYS_SET_TYPE /* LY_TYPE_UNION */,
1140 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1141 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1142 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1143 LYS_SET_RANGE /* LY_TYPE_INT64 */
1144};
1145
1146/**
1147 * @brief stringification of the YANG built-in data types
1148 */
1149const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001150 LY_TYPE_UNKNOWN_STR,
1151 LY_TYPE_BINARY_STR,
1152 LY_TYPE_UINT8_STR,
1153 LY_TYPE_UINT16_STR,
1154 LY_TYPE_UINT32_STR,
1155 LY_TYPE_UINT64_STR,
1156 LY_TYPE_STRING_STR,
1157 LY_TYPE_BITS_STR,
1158 LY_TYPE_BOOL_STR,
1159 LY_TYPE_DEC64_STR,
1160 LY_TYPE_EMPTY_STR,
1161 LY_TYPE_ENUM_STR,
1162 LY_TYPE_IDENT_STR,
1163 LY_TYPE_INST_STR,
1164 LY_TYPE_LEAFREF_STR,
1165 LY_TYPE_UNION_STR,
1166 LY_TYPE_INT8_STR,
1167 LY_TYPE_INT16_STR,
1168 LY_TYPE_INT32_STR,
1169 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001170};
1171
1172/**
1173 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1174 * @param[in] ctx Compile context.
1175 * @param[in] enums_p Array of the parsed enum structures to compile.
1176 * @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.
1177 * @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 +01001178 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001179 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1180 */
1181static LY_ERR
1182lys_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 +01001183 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001184{
1185 LY_ERR ret = LY_SUCCESS;
1186 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001187 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1188 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001189 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001190 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001191
1192 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001193 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001194 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1195 return LY_EVALID;
1196 }
1197
1198 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001199 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001200 if (base_enums) {
1201 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1202 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001203 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001204 break;
1205 }
1206 }
1207 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001208 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001209 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001210 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001211 return LY_EVALID;
1212 }
1213 match = v;
1214 }
1215
1216 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001217 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001218 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001219 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001221 LY_ARRAY_FOR(*bitenums, v) {
1222 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001223 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001224 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001225 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001226 return LY_EVALID;
1227 }
1228 }
1229 } else if (base_enums) {
1230 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001231 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001232 } else {
1233 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001234 if (u == 0) {
1235 cur_val = 0;
1236 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001237 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001238 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001239 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001240 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001241 } else {
1242 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001243 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001244 }
1245
1246 /* save highest value for auto assing */
1247 if (highest_value < cur_val) {
1248 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 }
1250 } else { /* LY_TYPE_BITS */
1251 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001252 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001253 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001255 LY_ARRAY_FOR(*bitenums, v) {
1256 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001257 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001258 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001259 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 return LY_EVALID;
1261 }
1262 }
1263 } else if (base_enums) {
1264 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001265 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001266 } else {
1267 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001268 if (u == 0) {
1269 cur_pos = 0;
1270 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001271 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001272 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001273 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001274 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001275 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001276 } else {
1277 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001278 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001279 }
1280
1281 /* save highest position for auto assing */
1282 if (highest_position < cur_pos) {
1283 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001284 }
1285 }
1286
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001287 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001288 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001289 if (basetype == LY_TYPE_ENUM) {
1290 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001291 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001292 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001293 enums_p[u].name, base_enums[match].value, cur_val);
1294 return LY_EVALID;
1295 }
1296 } else {
1297 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001298 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001299 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001300 enums_p[u].name, base_enums[match].position, cur_pos);
1301 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001302 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001303 }
1304 }
1305
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001306 /* evaluate if-ffeatures */
1307 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1308 if (!enabled) {
1309 continue;
1310 }
1311
1312 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001313 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001314 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1315 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1316 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001317 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 +01001318 if (basetype == LY_TYPE_ENUM) {
1319 e->value = cur_val;
1320 } else {
1321 e->position = cur_pos;
1322 }
Radek Krejciab430862021-03-02 20:13:40 +01001323 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001324
1325 if (basetype == LY_TYPE_BITS) {
1326 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001327 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001328 if (v != u) {
1329 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001330 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1331 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001332 }
1333 }
1334 }
1335
1336done:
1337 return ret;
1338}
1339
Radek Krejci6a205692020-12-09 13:58:22 +01001340static LY_ERR
1341lys_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 +01001342 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001343{
1344 LY_ERR ret = LY_SUCCESS;
1345 struct lysc_type **utypes = *utypes_p;
1346 struct lysc_type_union *un_aux = NULL;
1347
1348 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1349 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001350 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1351 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001352 LY_CHECK_GOTO(ret, error);
1353 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1354 /* add space for additional types from the union subtype */
1355 un_aux = (struct lysc_type_union *)utypes[u + additional];
1356 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1357 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1358
1359 /* copy subtypes of the subtype union */
1360 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1361 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1362 struct lysc_type_leafref *lref;
1363
1364 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1365 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1366 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1367 lref = (struct lysc_type_leafref *)utypes[u + additional];
1368
1369 lref->basetype = LY_TYPE_LEAFREF;
1370 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1371 LY_CHECK_GOTO(ret, error);
1372 lref->refcount = 1;
1373 lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
1374 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001375 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001376 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001377 LY_CHECK_GOTO(ret, error);
1378 /* TODO extensions */
1379
1380 } else {
1381 utypes[u + additional] = un_aux->types[v];
1382 ++un_aux->types[v]->refcount;
1383 }
1384 ++additional;
1385 LY_ARRAY_INCREMENT(utypes);
1386 }
1387 /* compensate u increment in main loop */
1388 --additional;
1389
1390 /* free the replaced union subtype */
1391 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1392 un_aux = NULL;
1393 } else {
1394 LY_ARRAY_INCREMENT(utypes);
1395 }
1396 }
1397
1398 *utypes_p = utypes;
1399 return LY_SUCCESS;
1400
1401error:
1402 if (un_aux) {
1403 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1404 }
1405 *utypes_p = utypes;
1406 return ret;
1407}
1408
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001409/**
1410 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1411 * @param[in] ctx Compile context.
1412 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1413 * @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 +02001414 * @param[in] context_name Name of the context node or referencing typedef for logging.
1415 * @param[in] type_p Parsed type to compile.
1416 * @param[in] basetype Base YANG built-in type of the type to compile.
1417 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1418 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1419 * @param[out] type Newly created type structure with the filled information about the type.
1420 * @return LY_ERR value.
1421 */
1422static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001423lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1424 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 +02001425{
1426 LY_ERR ret = LY_SUCCESS;
1427 struct lysc_type_bin *bin;
1428 struct lysc_type_num *num;
1429 struct lysc_type_str *str;
1430 struct lysc_type_bits *bits;
1431 struct lysc_type_enum *enumeration;
1432 struct lysc_type_dec *dec;
1433 struct lysc_type_identityref *idref;
1434 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001435 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001436
1437 switch (basetype) {
1438 case LY_TYPE_BINARY:
1439 bin = (struct lysc_type_bin *)(*type);
1440
1441 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1442 if (type_p->length) {
1443 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1444 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1445 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001446 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001447 }
1448 }
1449 break;
1450 case LY_TYPE_BITS:
1451 /* RFC 7950 9.7 - bits */
1452 bits = (struct lysc_type_bits *)(*type);
1453 if (type_p->bits) {
1454 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1455 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1456 (struct lysc_type_bitenum_item **)&bits->bits));
1457 }
1458
1459 if (!base && !type_p->flags) {
1460 /* type derived from bits built-in type must contain at least one bit */
1461 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001462 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001463 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001464 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001465 }
1466 return LY_EVALID;
1467 }
1468 break;
1469 case LY_TYPE_DEC64:
1470 dec = (struct lysc_type_dec *)(*type);
1471
1472 /* RFC 7950 9.3.4 - fraction-digits */
1473 if (!base) {
1474 if (!type_p->fraction_digits) {
1475 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001476 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001477 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001478 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001479 }
1480 return LY_EVALID;
1481 }
1482 dec->fraction_digits = type_p->fraction_digits;
1483 } else {
1484 if (type_p->fraction_digits) {
1485 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1486 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001487 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001488 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1489 tpdfname);
1490 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001491 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001492 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1493 }
1494 return LY_EVALID;
1495 }
1496 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1497 }
1498
1499 /* RFC 7950 9.2.4 - range */
1500 if (type_p->range) {
1501 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1502 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1503 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001504 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001505 }
1506 }
1507 break;
1508 case LY_TYPE_STRING:
1509 str = (struct lysc_type_str *)(*type);
1510
1511 /* RFC 7950 9.4.4 - length */
1512 if (type_p->length) {
1513 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1514 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1515 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001516 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001517 }
1518 } else if (base && ((struct lysc_type_str *)base)->length) {
1519 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1520 }
1521
1522 /* RFC 7950 9.4.5 - pattern */
1523 if (type_p->patterns) {
1524 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1525 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1526 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1527 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1528 }
1529 break;
1530 case LY_TYPE_ENUM:
1531 enumeration = (struct lysc_type_enum *)(*type);
1532
1533 /* RFC 7950 9.6 - enum */
1534 if (type_p->enums) {
1535 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1536 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1537 }
1538
1539 if (!base && !type_p->flags) {
1540 /* type derived from enumerations built-in type must contain at least one enum */
1541 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001542 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001543 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001544 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001545 }
1546 return LY_EVALID;
1547 }
1548 break;
1549 case LY_TYPE_INT8:
1550 case LY_TYPE_UINT8:
1551 case LY_TYPE_INT16:
1552 case LY_TYPE_UINT16:
1553 case LY_TYPE_INT32:
1554 case LY_TYPE_UINT32:
1555 case LY_TYPE_INT64:
1556 case LY_TYPE_UINT64:
1557 num = (struct lysc_type_num *)(*type);
1558
1559 /* RFC 6020 9.2.4 - range */
1560 if (type_p->range) {
1561 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1562 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1563 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001564 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001565 }
1566 }
1567 break;
1568 case LY_TYPE_IDENT:
1569 idref = (struct lysc_type_identityref *)(*type);
1570
1571 /* RFC 7950 9.10.2 - base */
1572 if (type_p->bases) {
1573 if (base) {
1574 /* only the directly derived identityrefs can contain base specification */
1575 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001576 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001577 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1578 tpdfname);
1579 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001580 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001581 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1582 }
1583 return LY_EVALID;
1584 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001585 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 +02001586 }
1587
1588 if (!base && !type_p->flags) {
1589 /* type derived from identityref built-in type must contain at least one base */
1590 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001591 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001592 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001593 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001594 }
1595 return LY_EVALID;
1596 }
1597 break;
1598 case LY_TYPE_LEAFREF:
1599 lref = (struct lysc_type_leafref *)*type;
1600
1601 /* RFC 7950 9.9.3 - require-instance */
1602 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001603 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001604 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001605 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001606 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1607 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001608 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001609 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1610 }
1611 return LY_EVALID;
1612 }
1613 lref->require_instance = type_p->require_instance;
1614 } else if (base) {
1615 /* inherit */
1616 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1617 } else {
1618 /* default is true */
1619 lref->require_instance = 1;
1620 }
1621 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001622 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001623
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001624 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001625 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr),
Radek Krejci8df109d2021-04-23 12:19:08 +02001626 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001627 } else if (base) {
1628 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci8df109d2021-04-23 12:19:08 +02001629 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001630 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001631 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001632 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001633 return LY_EVALID;
1634 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001635 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001636 return LY_EVALID;
1637 }
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001638 lref->cur_mod = type_p->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001639 break;
1640 case LY_TYPE_INST:
1641 /* RFC 7950 9.9.3 - require-instance */
1642 if (type_p->flags & LYS_SET_REQINST) {
1643 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1644 } else {
1645 /* default is true */
1646 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1647 }
1648 break;
1649 case LY_TYPE_UNION:
1650 un = (struct lysc_type_union *)(*type);
1651
1652 /* RFC 7950 7.4 - type */
1653 if (type_p->types) {
1654 if (base) {
1655 /* only the directly derived union can contain types specification */
1656 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001657 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001658 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1659 tpdfname);
1660 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001661 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001662 "Invalid type substatement for the type not directly derived from union built-in type.");
1663 }
1664 return LY_EVALID;
1665 }
1666 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001667 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 +02001668 }
1669
1670 if (!base && !type_p->flags) {
1671 /* type derived from union built-in type must contain at least one type */
1672 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001673 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001674 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001675 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001676 }
1677 return LY_EVALID;
1678 }
1679 break;
1680 case LY_TYPE_BOOL:
1681 case LY_TYPE_EMPTY:
1682 case LY_TYPE_UNKNOWN: /* just to complete switch */
1683 break;
1684 }
1685
1686 if (tpdfname) {
1687 switch (basetype) {
1688 case LY_TYPE_BINARY:
1689 type_p->compiled = *type;
1690 *type = calloc(1, sizeof(struct lysc_type_bin));
1691 break;
1692 case LY_TYPE_BITS:
1693 type_p->compiled = *type;
1694 *type = calloc(1, sizeof(struct lysc_type_bits));
1695 break;
1696 case LY_TYPE_DEC64:
1697 type_p->compiled = *type;
1698 *type = calloc(1, sizeof(struct lysc_type_dec));
1699 break;
1700 case LY_TYPE_STRING:
1701 type_p->compiled = *type;
1702 *type = calloc(1, sizeof(struct lysc_type_str));
1703 break;
1704 case LY_TYPE_ENUM:
1705 type_p->compiled = *type;
1706 *type = calloc(1, sizeof(struct lysc_type_enum));
1707 break;
1708 case LY_TYPE_INT8:
1709 case LY_TYPE_UINT8:
1710 case LY_TYPE_INT16:
1711 case LY_TYPE_UINT16:
1712 case LY_TYPE_INT32:
1713 case LY_TYPE_UINT32:
1714 case LY_TYPE_INT64:
1715 case LY_TYPE_UINT64:
1716 type_p->compiled = *type;
1717 *type = calloc(1, sizeof(struct lysc_type_num));
1718 break;
1719 case LY_TYPE_IDENT:
1720 type_p->compiled = *type;
1721 *type = calloc(1, sizeof(struct lysc_type_identityref));
1722 break;
1723 case LY_TYPE_LEAFREF:
1724 type_p->compiled = *type;
1725 *type = calloc(1, sizeof(struct lysc_type_leafref));
1726 break;
1727 case LY_TYPE_INST:
1728 type_p->compiled = *type;
1729 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1730 break;
1731 case LY_TYPE_UNION:
1732 type_p->compiled = *type;
1733 *type = calloc(1, sizeof(struct lysc_type_union));
1734 break;
1735 case LY_TYPE_BOOL:
1736 case LY_TYPE_EMPTY:
1737 case LY_TYPE_UNKNOWN: /* just to complete switch */
1738 break;
1739 }
1740 }
1741 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1742
1743cleanup:
1744 return ret;
1745}
1746
Radek Krejcibf940f92021-03-24 21:04:13 +01001747/**
1748 * @brief Find the correct plugin implementing the described type
1749 *
1750 * @param[in] mod Module where the type is defined
1751 * @param[in] name Name of the type (typedef)
1752 * @param[in] basetype Type's basetype (when the built-in base plugin is supposed to be used)
1753 * @return Pointer to the plugin implementing the described data type.
1754 */
1755static struct lyplg_type *
1756lys_compile_type_get_plugin(struct lys_module *mod, const char *name, LY_DATA_TYPE basetype)
1757{
1758 struct lyplg_type *p;
1759
1760 /* try to find loaded user type plugins */
1761 p = lyplg_find(LYPLG_TYPE, mod->name, mod->revision, name);
1762 if (!p) {
1763 /* use the internal built-in type implementation */
1764 p = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
1765 }
1766
1767 return p;
1768}
1769
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001770LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001771lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1772 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001773{
1774 LY_ERR ret = LY_SUCCESS;
1775 ly_bool dummyloops = 0;
1776 struct type_context {
1777 const struct lysp_tpdf *tpdf;
1778 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001779 } *tctx, *tctx_prev = NULL, *tctx_iter;
1780 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1781 struct lysc_type *base = NULL, *prev_type;
1782 struct ly_set tpdf_chain = {0};
1783
1784 (*type) = NULL;
1785 if (dflt) {
1786 *dflt = NULL;
1787 }
1788
1789 tctx = calloc(1, sizeof *tctx);
1790 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001791 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 +02001792 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001793 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1794 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001795 if (basetype) {
1796 break;
1797 }
1798
1799 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001800 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1801 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001802 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1803
1804 if (units && !*units) {
1805 /* inherit units */
1806 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1807 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1808 }
1809 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1810 /* inherit default */
1811 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1812 }
1813 if (dummyloops && (!units || *units) && dflt && *dflt) {
1814 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1815 break;
1816 }
1817
Radek Krejci720d2612021-03-03 19:44:22 +01001818 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1819 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1820 * and we need now recompile the type again in the updated context. */
1821 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1822 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1823 }
1824
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001825 if (tctx->tpdf->type.compiled) {
1826 /* it is not necessary to continue, the rest of the chain was already compiled,
1827 * but we still may need to inherit default and units values, so start dummy loops */
1828 basetype = tctx->tpdf->type.compiled->basetype;
1829 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1830 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1831
1832 if ((units && !*units) || (dflt && !*dflt)) {
1833 dummyloops = 1;
1834 goto preparenext;
1835 } else {
1836 tctx = NULL;
1837 break;
1838 }
1839 }
1840
1841 /* circular typedef reference detection */
1842 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1843 /* local part */
1844 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001845 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001846 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001847 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1848 free(tctx);
1849 ret = LY_EVALID;
1850 goto cleanup;
1851 }
1852 }
1853 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1854 /* global part for unions corner case */
1855 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001856 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001857 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001858 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1859 free(tctx);
1860 ret = LY_EVALID;
1861 goto cleanup;
1862 }
1863 }
1864
1865 /* store information for the following processing */
1866 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1867 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1868
1869preparenext:
1870 /* prepare next loop */
1871 tctx_prev = tctx;
1872 tctx = calloc(1, sizeof *tctx);
1873 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1874 }
1875 free(tctx);
1876
1877 /* allocate type according to the basetype */
1878 switch (basetype) {
1879 case LY_TYPE_BINARY:
1880 *type = calloc(1, sizeof(struct lysc_type_bin));
1881 break;
1882 case LY_TYPE_BITS:
1883 *type = calloc(1, sizeof(struct lysc_type_bits));
1884 break;
1885 case LY_TYPE_BOOL:
1886 case LY_TYPE_EMPTY:
1887 *type = calloc(1, sizeof(struct lysc_type));
1888 break;
1889 case LY_TYPE_DEC64:
1890 *type = calloc(1, sizeof(struct lysc_type_dec));
1891 break;
1892 case LY_TYPE_ENUM:
1893 *type = calloc(1, sizeof(struct lysc_type_enum));
1894 break;
1895 case LY_TYPE_IDENT:
1896 *type = calloc(1, sizeof(struct lysc_type_identityref));
1897 break;
1898 case LY_TYPE_INST:
1899 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1900 break;
1901 case LY_TYPE_LEAFREF:
1902 *type = calloc(1, sizeof(struct lysc_type_leafref));
1903 break;
1904 case LY_TYPE_STRING:
1905 *type = calloc(1, sizeof(struct lysc_type_str));
1906 break;
1907 case LY_TYPE_UNION:
1908 *type = calloc(1, sizeof(struct lysc_type_union));
1909 break;
1910 case LY_TYPE_INT8:
1911 case LY_TYPE_UINT8:
1912 case LY_TYPE_INT16:
1913 case LY_TYPE_UINT16:
1914 case LY_TYPE_INT32:
1915 case LY_TYPE_UINT32:
1916 case LY_TYPE_INT64:
1917 case LY_TYPE_UINT64:
1918 *type = calloc(1, sizeof(struct lysc_type_num));
1919 break;
1920 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001921 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001922 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1923 ret = LY_EVALID;
1924 goto cleanup;
1925 }
1926 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1927 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001928 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001929 ly_data_type2str[basetype]);
1930 free(*type);
1931 (*type) = NULL;
1932 ret = LY_EVALID;
1933 goto cleanup;
1934 }
1935
1936 /* get restrictions from the referred typedefs */
1937 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1938 tctx = (struct type_context *)tpdf_chain.objs[u];
1939
1940 /* remember the typedef context for circular check */
1941 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1942 LY_CHECK_GOTO(ret, cleanup);
1943
1944 if (tctx->tpdf->type.compiled) {
1945 base = tctx->tpdf->type.compiled;
1946 continue;
1947 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1948 /* no change, just use the type information from the base */
1949 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1950 ++base->refcount;
1951 continue;
1952 }
1953
1954 ++(*type)->refcount;
1955 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001956 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001957 tctx->tpdf->name, ly_data_type2str[basetype]);
1958 ret = LY_EVALID;
1959 goto cleanup;
1960 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001961 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001962 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1963 tctx->tpdf->name, tctx->tpdf->dflt.str);
1964 ret = LY_EVALID;
1965 goto cleanup;
1966 }
1967
1968 (*type)->basetype = basetype;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001969 /* collect extensions */
1970 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
1971 /* get plugin for the type */
Radek Krejcibf940f92021-03-24 21:04:13 +01001972 (*type)->plugin = lys_compile_type_get_plugin(tctx->tpdf->type.pmod->mod, tctx->tpdf->name, basetype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001973 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001974 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001975 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1976 LY_CHECK_GOTO(ret, cleanup);
1977 base = prev_type;
1978 }
1979 /* remove the processed typedef contexts from the stack for circular check */
1980 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1981
1982 /* process the type definition in leaf */
1983 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1984 /* get restrictions from the node itself */
1985 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01001986 (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001987 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001988 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001989 LY_CHECK_GOTO(ret, cleanup);
1990 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1991 /* no specific restriction in leaf's type definition, copy from the base */
1992 free(*type);
1993 (*type) = base;
1994 ++(*type)->refcount;
1995 }
1996
Radek Krejciab430862021-03-02 20:13:40 +01001997 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001998
1999cleanup:
2000 ly_set_erase(&tpdf_chain, free);
2001 return ret;
2002}
2003
2004/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01002005 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
2006 */
2007#define LYS_STATUS_USES LYS_CONFIG_MASK
2008
2009/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002010 * @brief Compile status information of the given node.
2011 *
2012 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
2013 * has the status correctly set during the compilation.
2014 *
2015 * @param[in] ctx Compile context
2016 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
2017 * If the status was set explicitly on the node, it is already set in the flags value and we just check
2018 * the compatibility with the parent's status value.
2019 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
2020 * @return LY_ERR value.
2021 */
2022static LY_ERR
2023lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
2024{
2025 /* status - it is not inherited by specification, but it does not make sense to have
2026 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2027 if (!((*node_flags) & LYS_STATUS_MASK)) {
2028 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002029 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002030 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01002031 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002032 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2033 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2034 }
2035 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
2036 } else {
2037 (*node_flags) |= LYS_STATUS_CURR;
2038 }
2039 } else if (parent_flags & LYS_STATUS_MASK) {
2040 /* check status compatibility with the parent */
2041 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
2042 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002043 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002044 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2045 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2046 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002047 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002048 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2049 }
2050 return LY_EVALID;
2051 }
2052 }
2053 return LY_SUCCESS;
2054}
2055
2056/**
2057 * @brief Check uniqness of the node/action/notification name.
2058 *
2059 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2060 * structures, but they share the namespace so we need to check their name collisions.
2061 *
2062 * @param[in] ctx Compile context.
2063 * @param[in] parent Parent of the nodes to check, can be NULL.
2064 * @param[in] name Name of the item to find in the given lists.
2065 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2066 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2067 */
2068static LY_ERR
2069lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2070 const struct lysc_node *exclude)
2071{
2072 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002073 const struct lysc_node_action *actions;
2074 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002075 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002076 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002077
2078#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2079
2080 if (exclude->nodetype == LYS_CASE) {
2081 /* check restricted only to all the cases */
2082 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002083 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002084 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002085 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002086 return LY_EEXIST;
2087 }
2088 }
2089
2090 return LY_SUCCESS;
2091 }
2092
2093 /* no reason for our parent to be choice anymore */
2094 assert(!parent || (parent->nodetype != LYS_CHOICE));
2095
2096 if (parent && (parent->nodetype == LYS_CASE)) {
2097 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002098
2099 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2100 iter = lysc_data_parent(parent);
2101 do {
2102 parent = parent->parent;
2103 if (parent && (parent->nodetype == LYS_CHOICE)) {
2104 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2105 }
2106 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002107 }
2108
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002109 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002110 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2111 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2112 if (exclude->flags & LYS_IS_OUTPUT) {
2113 getnext_flags |= LYS_GETNEXT_OUTPUT;
2114 parent = lysc_node_child(parent)->next;
2115 } else {
2116 parent = lysc_node_child(parent);
2117 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002118 }
2119
2120 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002121 if (!parent && ctx->ext) {
2122 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2123 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2124 goto error;
2125 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002126
Radek Krejci5fa32a32021-02-08 18:12:38 +01002127 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2128 if (iter->nodetype == LYS_CHOICE) {
2129 iter2 = NULL;
2130 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2131 if (CHECK_NODE(iter2, exclude, name)) {
2132 goto error;
2133 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002134 }
2135 }
2136 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002137 } else {
2138 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2139 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2140 goto error;
2141 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002142
Radek Krejci5fa32a32021-02-08 18:12:38 +01002143 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2144 if (iter->nodetype == LYS_CHOICE) {
2145 iter2 = NULL;
2146 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2147 if (CHECK_NODE(iter2, exclude, name)) {
2148 goto error;
2149 }
2150 }
2151 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002152 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002153
Radek Krejci5fa32a32021-02-08 18:12:38 +01002154 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2155 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2156 if (CHECK_NODE(iter, exclude, name)) {
2157 goto error;
2158 }
2159 }
2160
2161 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2162 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2163 if (CHECK_NODE(iter, exclude, name)) {
2164 goto error;
2165 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002166 }
2167 }
Radek Krejci078e4342021-02-12 18:17:26 +01002168 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002169 return LY_SUCCESS;
2170
2171error:
Radek Krejci078e4342021-02-12 18:17:26 +01002172 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002173 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002174 return LY_EEXIST;
2175
2176#undef CHECK_NODE
2177}
2178
Radek Krejci2a9fc652021-01-22 17:44:34 +01002179/**
2180 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2181 * keep specific order of augments targetting the same node.
2182 *
2183 * @param[in] ctx Compile context
2184 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2185 * the choice itself is expected instead of a specific case node.
2186 * @param[in] node Schema node to connect into the list.
2187 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2188 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2189 */
2190static LY_ERR
2191lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002192{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002193 struct lysc_node **children, *anchor = NULL;
2194 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002195
Radek Krejci2a9fc652021-01-22 17:44:34 +01002196 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002197
Radek Krejci2a9fc652021-01-22 17:44:34 +01002198 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002199 if (node->nodetype == LYS_INPUT) {
2200 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2201 /* input node is part of the action but link it with output */
2202 node->next = &((struct lysc_node_action *)parent)->output.node;
2203 node->prev = node->next;
2204 return LY_SUCCESS;
2205 } else if (node->nodetype == LYS_OUTPUT) {
2206 /* output node is part of the action but link it with input */
2207 node->next = NULL;
2208 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002209 return LY_SUCCESS;
2210 } else if (node->nodetype == LYS_ACTION) {
2211 children = (struct lysc_node **)lysc_node_actions_p(parent);
2212 } else if (node->nodetype == LYS_NOTIF) {
2213 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2214 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002215 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002216 }
2217 assert(children);
2218
2219 if (!(*children)) {
2220 /* first child */
2221 *children = node;
2222 } else if (node->flags & LYS_KEY) {
2223 /* special handling of adding keys */
2224 assert(node->module == parent->module);
2225 anchor = *children;
2226 if (anchor->flags & LYS_KEY) {
2227 while ((anchor->flags & LYS_KEY) && anchor->next) {
2228 anchor = anchor->next;
2229 }
2230 /* insert after the last key */
2231 insert_after = 1;
2232 } /* else insert before anchor (at the beginning) */
2233 } else if ((*children)->prev->module == node->module) {
2234 /* last child is from the same module, keep the order and insert at the end */
2235 anchor = (*children)->prev;
2236 insert_after = 1;
2237 } else if (parent->module == node->module) {
2238 /* adding module child after some augments were connected */
2239 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2240 } else {
2241 /* some augments are already connected and we are connecting new ones,
2242 * keep module name order and insert the node into the children list */
2243 anchor = *children;
2244 do {
2245 anchor = anchor->prev;
2246
2247 /* check that we have not found the last augment node from our module or
2248 * the first augment node from a "smaller" module or
2249 * the first node from a local module */
2250 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2251 (anchor->module == parent->module)) {
2252 /* insert after */
2253 insert_after = 1;
2254 break;
2255 }
2256
2257 /* we have traversed all the nodes, insert before anchor (as the first node) */
2258 } while (anchor->prev->next);
2259 }
2260
2261 /* insert */
2262 if (anchor) {
2263 if (insert_after) {
2264 node->next = anchor->next;
2265 node->prev = anchor;
2266 anchor->next = node;
2267 if (node->next) {
2268 /* middle node */
2269 node->next->prev = node;
2270 } else {
2271 /* last node */
2272 (*children)->prev = node;
2273 }
2274 } else {
2275 node->next = anchor;
2276 node->prev = anchor->prev;
2277 anchor->prev = node;
2278 if (anchor == *children) {
2279 /* first node */
2280 *children = node;
2281 } else {
2282 /* middle node */
2283 node->prev->next = node;
2284 }
2285 }
2286 }
2287
2288 /* check the name uniqueness (even for an only child, it may be in case) */
2289 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2290 return LY_EEXIST;
2291 }
2292 } else {
2293 /* top-level element */
2294 struct lysc_node **list;
2295
Radek Krejci6b88a462021-02-17 12:39:34 +01002296 if (ctx->ext) {
2297 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2298 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002299 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2300 } else if (node->nodetype == LYS_NOTIF) {
2301 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2302 } else {
2303 list = &ctx->cur_mod->compiled->data;
2304 }
2305 if (!(*list)) {
2306 *list = node;
2307 } else {
2308 /* insert at the end of the module's top-level nodes list */
2309 (*list)->prev->next = node;
2310 node->prev = (*list)->prev;
2311 (*list)->prev = node;
2312 }
2313
2314 /* check the name uniqueness on top-level */
2315 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2316 return LY_EEXIST;
2317 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002318 }
2319
Radek Krejci2a9fc652021-01-22 17:44:34 +01002320 return LY_SUCCESS;
2321}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002322
Radek Krejci2a9fc652021-01-22 17:44:34 +01002323/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002324 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002325 *
2326 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002327 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002328 * @return LY_ERR value.
2329 */
2330static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002331lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002332{
2333 /* case never has any explicit config */
2334 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002335
Michal Vasko7c565922021-06-10 14:58:27 +02002336 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002337 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002338 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002339 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2340 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002341 if (node->parent) {
2342 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002343 } else {
2344 /* default is config true */
2345 node->flags |= LYS_CONFIG_W;
2346 }
2347 } else {
2348 /* config set explicitly */
2349 node->flags |= LYS_SET_CONFIG;
2350 }
2351
Radek Krejci91531e12021-02-08 19:57:54 +01002352 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002353 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002354 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002355 }
2356
Radek Krejci2a9fc652021-01-22 17:44:34 +01002357 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002358}
2359
Radek Krejci91531e12021-02-08 19:57:54 +01002360/**
2361 * @brief Set various flags of the compiled nodes
2362 *
2363 * @param[in] ctx Compile context.
2364 * @param[in] node Compiled node where the flags will be set.
2365 * @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).
2366 * Zero means no uses, non-zero value with no status bit set mean the default status.
2367 */
2368static LY_ERR
2369lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2370{
2371 /* inherit config flags */
2372 LY_CHECK_RET(lys_compile_config(ctx, node));
2373
2374 /* status - it is not inherited by specification, but it does not make sense to have
2375 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2376 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (node->parent ? node->parent->flags : 0)));
2377
2378 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002379 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002380 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002381 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002382 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002383 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002384 node->flags |= LYS_IS_NOTIF;
2385 }
2386
2387 return LY_SUCCESS;
2388}
2389
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002390LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002391lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2392 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2393 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002394{
2395 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002396 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002397 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002398 struct lysp_when *pwhen = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002399
Radek Krejci2a9fc652021-01-22 17:44:34 +01002400 node->nodetype = pnode->nodetype;
2401 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002402 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002403 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002404 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002405
Radek Krejci2a9fc652021-01-22 17:44:34 +01002406 /* compile any deviations for this node */
2407 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002408 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002409 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002410 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002411 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002412 }
2413
Radek Krejci2a9fc652021-01-22 17:44:34 +01002414 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002415
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002416 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002417 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002418 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002419 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002420 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002421 }
2422
Radek Krejci91531e12021-02-08 19:57:54 +01002423 /* config, status and other flags */
2424 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002425 LY_CHECK_GOTO(ret, error);
2426
2427 /* list ordering */
2428 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002429 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002430 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002431 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002432 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002433 node->flags &= ~LYS_ORDBY_MASK;
2434 node->flags |= LYS_ORDBY_SYSTEM;
2435 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2436 /* default ordering is system */
2437 node->flags |= LYS_ORDBY_SYSTEM;
2438 }
2439 }
2440
Radek Krejci2a9fc652021-01-22 17:44:34 +01002441 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2442 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2443 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2444
2445 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2446 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2447
Radek Krejci9a3823e2021-01-27 20:26:46 +01002448 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002449 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002450 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002451 LY_CHECK_GOTO(ret, cleanup);
2452 }
2453
2454 /* connect any augments */
2455 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2456
2457 /* nodetype-specific part */
2458 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2459
2460 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002461 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002462 if (node->flags & LYS_MAND_TRUE) {
2463 /* inherit LYS_MAND_TRUE in parent containers */
2464 lys_compile_mandatory_parents(parent, 1);
2465 }
2466
2467 if (child_set) {
2468 /* add the new node into set */
2469 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2470 }
2471
2472 goto cleanup;
2473
2474error:
2475 lysc_node_free(ctx->ctx, node, 0);
2476cleanup:
2477 if (ret && dev_pnode) {
2478 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2479 }
2480 lysp_dev_node_free(ctx->ctx, dev_pnode);
2481 return ret;
2482}
2483
2484/**
2485 * @brief Compile parsed action's input/output node information.
2486 * @param[in] ctx Compile context
2487 * @param[in] pnode Parsed inout node.
2488 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2489 * is enriched with the inout-specific information.
2490 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2491 */
2492LY_ERR
2493lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2494{
2495 LY_ERR ret = LY_SUCCESS;
2496 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002497 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002498
2499 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2500 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2501
2502 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002503 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002504 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002505
Radek Krejci01180ac2021-01-27 08:48:22 +01002506 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002507 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2508 }
2509
Michal Vasko7c565922021-06-10 14:58:27 +02002510 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002511
2512done:
2513 return ret;
2514}
2515
2516/**
2517 * @brief Compile parsed action node information.
2518 * @param[in] ctx Compile context
2519 * @param[in] pnode Parsed action node.
2520 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2521 * is enriched with the action-specific information.
2522 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2523 */
2524LY_ERR
2525lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2526{
2527 LY_ERR ret;
2528 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2529 struct lysc_node_action *action = (struct lysc_node_action *)node;
2530 struct lysp_node_action_inout *input, implicit_input = {
2531 .nodetype = LYS_INPUT,
2532 .name = "input",
2533 .parent = pnode,
2534 };
2535 struct lysp_node_action_inout *output, implicit_output = {
2536 .nodetype = LYS_OUTPUT,
2537 .name = "output",
2538 .parent = pnode,
2539 };
2540
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002541 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002542 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002543 if (action_p->input.nodetype == LYS_UNKNOWN) {
2544 input = &implicit_input;
2545 } else {
2546 input = &action_p->input;
2547 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002548 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 +01002549 lysc_update_path(ctx, NULL, NULL);
2550 LY_CHECK_GOTO(ret, done);
2551
2552 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002553 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002554 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002555 output = &implicit_output;
2556 } else {
2557 output = &action_p->output;
2558 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002559 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 +01002560 lysc_update_path(ctx, NULL, NULL);
2561 LY_CHECK_GOTO(ret, done);
2562
2563 /* do not check "must" semantics in a grouping */
Michal Vasko7c565922021-06-10 14:58:27 +02002564 if ((action->input.musts || action->output.musts) && !(ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002565 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2566 LY_CHECK_GOTO(ret, done);
2567 }
2568
2569done:
2570 return ret;
2571}
2572
2573/**
2574 * @brief Compile parsed action node information.
2575 * @param[in] ctx Compile context
2576 * @param[in] pnode Parsed action node.
2577 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2578 * is enriched with the action-specific information.
2579 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2580 */
2581LY_ERR
2582lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2583{
2584 LY_ERR ret = LY_SUCCESS;
2585 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2586 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2587 struct lysp_node *child_p;
2588
2589 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002590 if (notif_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002591 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002592 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002593 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002594 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002595
Radek Krejci01180ac2021-01-27 08:48:22 +01002596 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002597 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002598 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002599 }
2600
Radek Krejci2a9fc652021-01-22 17:44:34 +01002601done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002602 return ret;
2603}
2604
2605/**
2606 * @brief Compile parsed container node information.
2607 * @param[in] ctx Compile context
2608 * @param[in] pnode Parsed container node.
2609 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2610 * is enriched with the container-specific information.
2611 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2612 */
2613static LY_ERR
2614lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2615{
2616 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2617 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2618 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002619 LY_ERR ret = LY_SUCCESS;
2620
2621 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002622 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002623 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002624 }
2625
2626 /* more cases when the container has meaning but is kept NP for convenience:
2627 * - when condition
2628 * - direct child action/notification
2629 */
2630
2631 LY_LIST_FOR(cont_p->child, child_p) {
2632 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2633 LY_CHECK_GOTO(ret, done);
2634 }
2635
Michal Vasko5347e3a2020-11-03 17:14:57 +01002636 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002637 if (cont_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002638 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002639 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002640 LY_CHECK_GOTO(ret, done);
2641 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002642
2643 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2644 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2645 LY_CHECK_GOTO(ret, done);
2646 }
2647 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2648 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2649 LY_CHECK_GOTO(ret, done);
2650 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002651
2652done:
2653 return ret;
2654}
2655
Michal Vasko72244882021-01-12 15:21:05 +01002656/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002657 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2658 * @param[in] ctx Compile context.
2659 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2660 * @param[in] type_p Parsed type to compile.
2661 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2662 * @return LY_ERR value.
2663 */
2664static LY_ERR
2665lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2666 struct lysc_node_leaf *leaf)
2667{
2668 struct lysp_qname *dflt;
2669
Michal Vaskoa99b3572021-02-01 11:54:58 +01002670 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002671 leaf->units ? NULL : &leaf->units, &dflt));
2672
2673 /* store default value, if any */
2674 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2675 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2676 }
2677
Michal Vasko7c565922021-06-10 14:58:27 +02002678 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002679 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002680 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko7c565922021-06-10 14:58:27 +02002681 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002682 LY_ARRAY_COUNT_TYPE u;
2683 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2684 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2685 /* 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 +01002686 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002687 }
2688 }
2689 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2690 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002691 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 +02002692 return LY_EVALID;
2693 }
2694 }
2695
2696 return LY_SUCCESS;
2697}
2698
2699/**
2700 * @brief Compile parsed leaf node information.
2701 * @param[in] ctx Compile context
2702 * @param[in] pnode Parsed leaf node.
2703 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2704 * is enriched with the leaf-specific information.
2705 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2706 */
2707static LY_ERR
2708lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2709{
2710 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2711 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002712 LY_ERR ret = LY_SUCCESS;
2713
Michal Vasko5347e3a2020-11-03 17:14:57 +01002714 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002715 if (leaf_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002716 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002717 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002718 LY_CHECK_GOTO(ret, done);
2719 }
2720 if (leaf_p->units) {
2721 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2722 leaf->flags |= LYS_SET_UNITS;
2723 }
2724
2725 /* compile type */
2726 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2727 LY_CHECK_GOTO(ret, done);
2728
2729 /* store/update default value */
2730 if (leaf_p->dflt.str) {
2731 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2732 leaf->flags |= LYS_SET_DFLT;
2733 }
2734
2735 /* checks */
2736 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002737 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002738 "Invalid mandatory leaf with a default value.");
2739 return LY_EVALID;
2740 }
2741
2742done:
2743 return ret;
2744}
2745
2746/**
2747 * @brief Compile parsed leaf-list node information.
2748 * @param[in] ctx Compile context
2749 * @param[in] pnode Parsed leaf-list node.
2750 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2751 * is enriched with the leaf-list-specific information.
2752 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2753 */
2754static LY_ERR
2755lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2756{
2757 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2758 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002759 LY_ERR ret = LY_SUCCESS;
2760
Michal Vasko5347e3a2020-11-03 17:14:57 +01002761 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002762 if (llist_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002763 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002764 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002765 LY_CHECK_GOTO(ret, done);
2766 }
2767 if (llist_p->units) {
2768 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2769 llist->flags |= LYS_SET_UNITS;
2770 }
2771
2772 /* compile type */
2773 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2774 LY_CHECK_GOTO(ret, done);
2775
2776 /* store/update default values */
2777 if (llist_p->dflts) {
2778 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002779 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002780 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2781 return LY_EVALID;
2782 }
2783
2784 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2785 llist->flags |= LYS_SET_DFLT;
2786 }
2787
2788 llist->min = llist_p->min;
2789 if (llist->min) {
2790 llist->flags |= LYS_MAND_TRUE;
2791 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002792 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2793
2794 if (llist->flags & LYS_CONFIG_R) {
2795 /* state leaf-list is always ordered-by user */
2796 llist->flags &= ~LYS_ORDBY_SYSTEM;
2797 llist->flags |= LYS_ORDBY_USER;
2798 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002799
2800 /* checks */
2801 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002802 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 +02002803 return LY_EVALID;
2804 }
2805
2806 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002807 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002808 llist->min, llist->max);
2809 return LY_EVALID;
2810 }
2811
2812done:
2813 return ret;
2814}
2815
2816LY_ERR
2817lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
Radek Krejci8df109d2021-04-23 12:19:08 +02002818 const struct lys_module *cur_mod, LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002819 const struct lysc_node **target, uint16_t *result_flag)
2820{
2821 LY_ERR ret = LY_EVALID;
2822 const char *name, *prefix, *id;
2823 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002824 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002825 const char *nodeid_type;
2826 uint32_t getnext_extra_flag = 0;
2827 uint16_t current_nodetype = 0;
2828
2829 assert(nodeid);
2830 assert(target);
2831 assert(result_flag);
2832 *target = NULL;
2833 *result_flag = 0;
2834
2835 id = nodeid;
2836
2837 if (ctx_node) {
2838 /* descendant-schema-nodeid */
2839 nodeid_type = "descendant";
2840
2841 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002842 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002843 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002844 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002845 return LY_EVALID;
2846 }
2847 } else {
2848 /* absolute-schema-nodeid */
2849 nodeid_type = "absolute";
2850
2851 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002852 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002853 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002854 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002855 return LY_EVALID;
2856 }
2857 ++id;
2858 }
2859
2860 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2861 if (prefix) {
2862 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2863 if (!mod) {
2864 /* module must always be found */
2865 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002866 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002867 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002868 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002869 return LY_ENOTFOUND;
2870 }
2871 } else {
2872 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02002873 case LY_VALUE_SCHEMA:
2874 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002875 /* use the current module */
2876 mod = cur_mod;
2877 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02002878 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02002879 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002880 if (!ctx_node) {
2881 LOGINT_RET(ctx->ctx);
2882 }
2883 /* inherit the module of the previous context node */
2884 mod = ctx_node->module;
2885 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02002886 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02002887 case LY_VALUE_XML:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002888 /* not really defined */
2889 LOGINT_RET(ctx->ctx);
2890 }
2891 }
2892
2893 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2894 /* move through input/output manually */
2895 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002896 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2897 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002898 return LY_ENOTFOUND;
2899 }
2900 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002901 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002902 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002903 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002904 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2905 } else {
2906 /* only input or output is valid */
2907 ctx_node = NULL;
2908 }
2909 } else {
2910 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002911 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002912 getnext_extra_flag = 0;
2913 }
2914 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002915 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2916 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002917 return LY_ENOTFOUND;
2918 }
2919 current_nodetype = ctx_node->nodetype;
2920
2921 if (current_nodetype == LYS_NOTIF) {
2922 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2923 } else if (current_nodetype == LYS_INPUT) {
2924 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2925 } else if (current_nodetype == LYS_OUTPUT) {
2926 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2927 }
2928
2929 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2930 break;
2931 }
2932 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002933 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002934 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002935 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002936 return LY_EVALID;
2937 }
2938 ++id;
2939 }
2940
2941 if (ret == LY_SUCCESS) {
2942 *target = ctx_node;
2943 if (nodetype && !(current_nodetype & nodetype)) {
2944 return LY_EDENIED;
2945 }
2946 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002947 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002948 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002949 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002950 }
2951
2952 return ret;
2953}
2954
2955/**
2956 * @brief Compile information about list's uniques.
2957 * @param[in] ctx Compile context.
2958 * @param[in] uniques Sized array list of unique statements.
2959 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2960 * @return LY_ERR value.
2961 */
2962static LY_ERR
2963lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2964{
2965 LY_ERR ret = LY_SUCCESS;
2966 struct lysc_node_leaf **key, ***unique;
2967 struct lysc_node *parent;
2968 const char *keystr, *delim;
2969 size_t len;
2970 LY_ARRAY_COUNT_TYPE v;
2971 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2972 uint16_t flags;
2973
2974 LY_ARRAY_FOR(uniques, v) {
2975 config = -1;
2976 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2977 keystr = uniques[v].str;
2978 while (keystr) {
2979 delim = strpbrk(keystr, " \t\n");
2980 if (delim) {
2981 len = delim - keystr;
2982 while (isspace(*delim)) {
2983 ++delim;
2984 }
2985 } else {
2986 len = strlen(keystr);
2987 }
2988
2989 /* unique node must be present */
2990 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002991 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02002992 LY_VALUE_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002993 if (ret != LY_SUCCESS) {
2994 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002995 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002996 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002997 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002998 }
2999 return LY_EVALID;
3000 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003001 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003002 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003003 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003004 return LY_EVALID;
3005 }
3006
3007 /* all referenced leafs must be of the same config type */
3008 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3009 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003010 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003011 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3012 return LY_EVALID;
3013 } else if ((*key)->flags & LYS_CONFIG_W) {
3014 config = 1;
3015 } else { /* LYS_CONFIG_R */
3016 config = 0;
3017 }
3018
3019 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3020 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3021 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003022 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003023 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3024 return LY_EVALID;
3025 }
3026 }
3027
3028 /* check status */
3029 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3030 (*key)->flags, (*key)->module, (*key)->name));
3031
3032 /* mark leaf as unique */
3033 (*key)->flags |= LYS_UNIQUE;
3034
3035 /* next unique value in line */
3036 keystr = delim;
3037 }
3038 /* next unique definition */
3039 }
3040
3041 return LY_SUCCESS;
3042}
3043
3044/**
3045 * @brief Compile parsed list node information.
3046 * @param[in] ctx Compile context
3047 * @param[in] pnode Parsed list node.
3048 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3049 * is enriched with the list-specific information.
3050 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3051 */
3052static LY_ERR
3053lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3054{
3055 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3056 struct lysc_node_list *list = (struct lysc_node_list *)node;
3057 struct lysp_node *child_p;
3058 struct lysc_node_leaf *key, *prev_key = NULL;
3059 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003060 const char *keystr, *delim;
3061 LY_ERR ret = LY_SUCCESS;
3062
3063 list->min = list_p->min;
3064 if (list->min) {
3065 list->flags |= LYS_MAND_TRUE;
3066 }
3067 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3068
3069 LY_LIST_FOR(list_p->child, child_p) {
3070 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3071 }
3072
Michal Vasko5347e3a2020-11-03 17:14:57 +01003073 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02003074 if (list_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003075 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003076 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003077 }
3078
3079 /* keys */
3080 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003081 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003082 return LY_EVALID;
3083 }
3084
3085 /* find all the keys (must be direct children) */
3086 keystr = list_p->key;
3087 if (!keystr) {
3088 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003089 list->flags &= ~LYS_ORDBY_SYSTEM;
3090 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003091 }
3092 while (keystr) {
3093 delim = strpbrk(keystr, " \t\n");
3094 if (delim) {
3095 len = delim - keystr;
3096 while (isspace(*delim)) {
3097 ++delim;
3098 }
3099 } else {
3100 len = strlen(keystr);
3101 }
3102
3103 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003104 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3105 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003106 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003107 return LY_EVALID;
3108 }
3109 /* keys must be unique */
3110 if (key->flags & LYS_KEY) {
3111 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003112 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003113 return LY_EVALID;
3114 }
3115
Radek Krejcia6016992021-03-03 10:13:41 +01003116 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003117 /* key must have the same config flag as the list itself */
3118 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003119 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003120 return LY_EVALID;
3121 }
3122 if (ctx->pmod->version < LYS_VERSION_1_1) {
3123 /* YANG 1.0 denies key to be of empty type */
3124 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003125 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003126 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3127 return LY_EVALID;
3128 }
3129 } else {
3130 /* when and if-feature are illegal on list keys */
3131 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003132 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003133 "List's key must not have any \"when\" statement.");
3134 return LY_EVALID;
3135 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003136 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003137 }
3138
3139 /* check status */
3140 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3141 key->flags, key->module, key->name));
3142
3143 /* ignore default values of the key */
3144 if (key->dflt) {
3145 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3146 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3147 free(key->dflt);
3148 key->dflt = NULL;
3149 }
3150 /* mark leaf as key */
3151 key->flags |= LYS_KEY;
3152
3153 /* move it to the correct position */
3154 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3155 /* fix links in closest previous siblings of the key */
3156 if (key->next) {
3157 key->next->prev = key->prev;
3158 } else {
3159 /* last child */
3160 list->child->prev = key->prev;
3161 }
3162 if (key->prev->next) {
3163 key->prev->next = key->next;
3164 }
3165 /* fix links in the key */
3166 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003167 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003168 key->next = prev_key->next;
3169 } else {
3170 key->prev = list->child->prev;
3171 key->next = list->child;
3172 }
3173 /* fix links in closes future siblings of the key */
3174 if (prev_key) {
3175 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003176 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003177 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003178 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003179 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003180 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003181 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003182 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003183 }
3184 /* fix links in parent */
3185 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003186 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003187 }
3188 }
3189
3190 /* next key value */
3191 prev_key = key;
3192 keystr = delim;
3193 lysc_update_path(ctx, NULL, NULL);
3194 }
3195
3196 /* uniques */
3197 if (list_p->uniques) {
3198 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3199 }
3200
Radek Krejci2a9fc652021-01-22 17:44:34 +01003201 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3202 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3203 LY_CHECK_GOTO(ret, done);
3204 }
3205 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3206 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3207 LY_CHECK_GOTO(ret, done);
3208 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003209
3210 /* checks */
3211 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003212 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003213 list->min, list->max);
3214 return LY_EVALID;
3215 }
3216
3217done:
3218 return ret;
3219}
3220
3221/**
3222 * @brief Do some checks and set the default choice's case.
3223 *
3224 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3225 *
3226 * @param[in] ctx Compile context.
3227 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3228 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3229 * @return LY_ERR value.
3230 */
3231static LY_ERR
3232lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3233{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003234 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003235 const struct lys_module *mod;
3236 const char *prefix = NULL, *name;
3237 size_t prefix_len = 0;
3238
3239 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3240 name = strchr(dflt->str, ':');
3241 if (name) {
3242 prefix = dflt->str;
3243 prefix_len = name - prefix;
3244 ++name;
3245 } else {
3246 name = dflt->str;
3247 }
3248 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003249 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003250 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003251 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003252 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003253 return LY_EVALID;
3254 }
3255 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003256 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003257 }
3258
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003259 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 +02003260 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003261 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003262 "Default case \"%s\" not found.", dflt->str);
3263 return LY_EVALID;
3264 }
3265
3266 /* no mandatory nodes directly under the default case */
3267 LY_LIST_FOR(ch->dflt->child, iter) {
3268 if (iter->parent != (struct lysc_node *)ch->dflt) {
3269 break;
3270 }
3271 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003272 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003273 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3274 return LY_EVALID;
3275 }
3276 }
3277
3278 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003279 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003280 return LY_EVALID;
3281 }
3282
3283 ch->dflt->flags |= LYS_SET_DFLT;
3284 return LY_SUCCESS;
3285}
3286
3287LY_ERR
3288lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3289 struct ly_set *child_set)
3290{
3291 LY_ERR ret = LY_SUCCESS;
3292 struct lysp_node *child_p_next = child_p->next;
3293 struct lysp_node_case *cs_p;
aPiecekaa320c92021-05-21 07:34:24 +02003294 struct lysc_node_case *compiled_case;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003295
3296 if (child_p->nodetype == LYS_CASE) {
3297 /* standard case under choice */
3298 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3299 } else {
3300 /* we need the implicit case first, so create a fake parsed case */
3301 cs_p = calloc(1, sizeof *cs_p);
3302 cs_p->nodetype = LYS_CASE;
3303 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3304 cs_p->child = child_p;
3305
3306 /* make the child the only case child */
3307 child_p->next = NULL;
3308
3309 /* compile it normally */
3310 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3311
3312free_fake_node:
3313 /* free the fake parsed node and correct pointers back */
aPiecek082c7dc2021-05-20 08:55:07 +02003314
aPiecekaa320c92021-05-21 07:34:24 +02003315 /* get last case node */
3316 compiled_case = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases->prev;
3317
aPiecek082c7dc2021-05-20 08:55:07 +02003318 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
aPiecekaa320c92021-05-21 07:34:24 +02003319 /* Compiled case node cannot point to his corresponding parsed node
3320 * because it exists temporarily. Therefore, it must be set to NULL.
3321 */
aPiecek082c7dc2021-05-20 08:55:07 +02003322 assert(compiled_case->priv == cs_p);
aPiecek082c7dc2021-05-20 08:55:07 +02003323 compiled_case->priv = NULL;
3324 }
aPiecekaa320c92021-05-21 07:34:24 +02003325
3326 /* The status is copied from his child and not from his parent as usual. */
3327 if (compiled_case->child) {
3328 compiled_case->flags &= ~LYS_STATUS_MASK;
3329 compiled_case->flags |= LYS_STATUS_MASK & compiled_case->child->flags;
3330 }
3331
3332 cs_p->child = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003333 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3334 child_p->next = child_p_next;
3335 }
3336
3337 return ret;
3338}
3339
3340/**
3341 * @brief Compile parsed choice node information.
3342 *
3343 * @param[in] ctx Compile context
3344 * @param[in] pnode Parsed choice node.
3345 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3346 * is enriched with the choice-specific information.
3347 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3348 */
3349static LY_ERR
3350lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3351{
3352 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3353 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3354 struct lysp_node *child_p;
3355 LY_ERR ret = LY_SUCCESS;
3356
3357 assert(node->nodetype == LYS_CHOICE);
3358
3359 LY_LIST_FOR(ch_p->child, child_p) {
3360 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3361 }
3362
3363 /* default branch */
3364 if (ch_p->dflt.str) {
3365 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3366 }
3367
3368 return ret;
3369}
3370
3371/**
3372 * @brief Compile parsed anydata or anyxml node information.
3373 * @param[in] ctx Compile context
3374 * @param[in] pnode Parsed anydata or anyxml node.
3375 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3376 * is enriched with the any-specific information.
3377 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3378 */
3379static LY_ERR
3380lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3381{
3382 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3383 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003384 LY_ERR ret = LY_SUCCESS;
3385
Michal Vasko5347e3a2020-11-03 17:14:57 +01003386 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02003387 if (any_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003388 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003389 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003390 LY_CHECK_GOTO(ret, done);
3391 }
3392
Radek Krejci91531e12021-02-08 19:57:54 +01003393 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003394 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003395 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3396 }
3397done:
3398 return ret;
3399}
3400
3401/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003402 * @brief Prepare the case structure in choice node for the new data node.
3403 *
3404 * 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
3405 * created in the choice when the first child was processed.
3406 *
3407 * @param[in] ctx Compile context.
3408 * @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,
3409 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3410 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3411 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3412 * @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,
3413 * it is linked from the case structure only in case it is its first child.
3414 */
3415static LY_ERR
3416lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3417{
3418 struct lysp_node *child_p;
3419 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3420
3421 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3422 /* we have to add an implicit case node into the parent choice */
3423 } else if (pnode->nodetype == LYS_CASE) {
3424 /* explicit parent case */
3425 LY_LIST_FOR(cs_p->child, child_p) {
3426 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3427 }
3428 } else {
3429 LOGINT_RET(ctx->ctx);
3430 }
3431
3432 return LY_SUCCESS;
3433}
3434
3435void
3436lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3437{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003438 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003439
3440 if (add) { /* set flag */
3441 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3442 parent = parent->parent) {
3443 parent->flags |= LYS_MAND_TRUE;
3444 }
3445 } else { /* unset flag */
3446 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003447 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003448 if (iter->flags & LYS_MAND_TRUE) {
3449 /* there is another mandatory node */
3450 return;
3451 }
3452 }
3453 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3454 parent->flags &= ~LYS_MAND_TRUE;
3455 }
3456 }
3457}
3458
3459/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003460 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003461 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003462 * @param[in] name Name of the grouping to find,
3463 * @return NULL when there is no grouping with the specified name
3464 * @return Pointer to the grouping of the specified @p name.
3465 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003466static struct lysp_node_grp *
3467match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003468{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003469 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003470
Radek Krejci2a9fc652021-01-22 17:44:34 +01003471 LY_LIST_FOR(grps, grp) {
3472 if (!strcmp(grp->name, name)) {
3473 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003474 }
3475 }
3476
3477 return NULL;
3478}
3479
3480/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003481 * @brief Find grouping for a uses.
3482 *
3483 * @param[in] ctx Compile context.
3484 * @param[in] uses_p Parsed uses node.
3485 * @param[out] gpr_p Found grouping on success.
3486 * @param[out] grp_pmod Module of @p grp_p on success.
3487 * @return LY_ERR value.
3488 */
3489static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003490lys_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 +02003491 struct lysp_module **grp_pmod)
3492{
3493 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003494 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003495 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003496 const char *id, *name, *prefix, *local_pref;
3497 size_t prefix_len, name_len;
3498 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003499 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003500
3501 *grp_p = NULL;
3502 *grp_pmod = NULL;
3503
3504 /* search for the grouping definition */
3505 id = uses_p->name;
3506 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3507 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3508 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3509 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003510 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003511 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003512 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3513 found = ctx->pmod;
3514 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003515 }
3516 }
3517 } else {
3518 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003519 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003520 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003521 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003522 "Invalid prefix used for grouping reference.", uses_p->name);
3523 return LY_EVALID;
3524 }
3525 pmod = mod->parsed;
3526 }
3527
3528 if (!found) {
3529 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003530 if ((grp = match_grouping(pmod->groupings, name))) {
3531 found = pmod;
3532 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003533 /* ... and all the submodules */
3534 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003535 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3536 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003537 break;
3538 }
3539 }
3540 }
3541 }
3542 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003543 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003544 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3545 return LY_EVALID;
3546 }
3547
Michal Vasko7c565922021-06-10 14:58:27 +02003548 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003549 /* remember that the grouping is instantiated to avoid its standalone validation */
3550 grp->flags |= LYS_USED_GRP;
3551 }
3552
3553 *grp_p = grp;
3554 *grp_pmod = found;
3555 return LY_SUCCESS;
3556}
3557
3558/**
3559 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3560 * If present, also apply uses's modificators.
3561 *
3562 * @param[in] ctx Compile context
3563 * @param[in] uses_p Parsed uses schema node.
3564 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3565 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3566 * the compile context.
3567 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3568 */
3569static LY_ERR
3570lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3571{
3572 struct lysp_node *pnode;
3573 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003574 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003575 uint32_t i, grp_stack_count;
3576 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3577 LY_ERR ret = LY_SUCCESS;
3578 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003579 struct ly_set uses_child_set = {0};
3580
3581 /* find the referenced grouping */
3582 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3583
3584 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3585 grp_stack_count = ctx->groupings.count;
3586 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3587 if (grp_stack_count == ctx->groupings.count) {
3588 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003589 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003590 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3591 return LY_EVALID;
3592 }
3593
3594 /* check status */
3595 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3596 LY_CHECK_GOTO(ret, cleanup);
3597
3598 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3599 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3600 LY_CHECK_GOTO(ret, cleanup);
3601
3602 /* switch context's parsed module being processed */
3603 ctx->pmod = grp_mod;
3604
3605 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003606 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003607 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3608 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 +02003609 LY_CHECK_GOTO(ret, cleanup);
3610 }
3611
3612 if (child_set) {
3613 /* add these children to our compiled child_set as well since uses is a schema-only node */
3614 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3615 }
3616
3617 if (uses_p->when) {
3618 /* pass uses's when to all the data children */
3619 for (i = 0; i < uses_child_set.count; ++i) {
3620 child = uses_child_set.snodes[i];
3621
Michal Vasko72244882021-01-12 15:21:05 +01003622 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 +02003623 LY_CHECK_GOTO(ret, cleanup);
3624 }
3625 }
3626
3627 /* compile actions */
3628 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003629 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003630 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3631 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003632 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003633 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3634 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003635 ret = LY_EVALID;
3636 goto cleanup;
3637 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003638 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3639 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3640 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3641 LY_CHECK_GOTO(ret, cleanup);
3642 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003643
3644 if (uses_p->when) {
3645 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003646 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3647 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003648 LY_CHECK_GOTO(ret, cleanup);
3649 }
3650 }
3651 }
3652
3653 /* compile notifications */
3654 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003655 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003656 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3657 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003658 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003659 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3660 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003661 ret = LY_EVALID;
3662 goto cleanup;
3663 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003664
3665 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3666 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3667 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3668 LY_CHECK_GOTO(ret, cleanup);
3669 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003670
3671 if (uses_p->when) {
3672 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003673 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3674 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 +02003675 LY_CHECK_GOTO(ret, cleanup);
3676 }
3677 }
3678 }
3679
3680 /* check that all augments were applied */
3681 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003682 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3683 /* augment of some parent uses, irrelevant now */
3684 continue;
3685 }
3686
3687 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003688 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3689 ret = LY_ENOTFOUND;
3690 }
3691 LY_CHECK_GOTO(ret, cleanup);
3692
3693 /* check that all refines were applied */
3694 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003695 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3696 /* refine of some paretn uses, irrelevant now */
3697 continue;
3698 }
3699
3700 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003701 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3702 ret = LY_ENOTFOUND;
3703 }
3704 LY_CHECK_GOTO(ret, cleanup);
3705
3706cleanup:
3707 /* reload previous context's parsed module being processed */
3708 ctx->pmod = mod_old;
3709
3710 /* remove the grouping from the stack for circular groupings dependency check */
3711 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3712 assert(ctx->groupings.count == grp_stack_count);
3713
3714 ly_set_erase(&uses_child_set, NULL);
3715 return ret;
3716}
3717
3718static int
3719lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3720{
3721 struct lysp_node *iter;
3722 int len = 0;
3723
3724 *path = NULL;
3725 for (iter = node; iter && len >= 0; iter = iter->parent) {
3726 char *s = *path;
3727 char *id;
3728
3729 switch (iter->nodetype) {
3730 case LYS_USES:
3731 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3732 break;
3733 case LYS_GROUPING:
3734 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3735 break;
3736 case LYS_AUGMENT:
3737 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3738 break;
3739 default:
3740 id = strdup(iter->name);
3741 break;
3742 }
3743
3744 if (!iter->parent) {
3745 /* print prefix */
3746 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3747 } else {
3748 /* prefix is the same as in parent */
3749 len = asprintf(path, "/%s%s", id, s ? s : "");
3750 }
3751 free(s);
3752 free(id);
3753 }
3754
3755 if (len < 0) {
3756 free(*path);
3757 *path = NULL;
3758 } else if (len == 0) {
3759 *path = strdup("/");
3760 len = 1;
3761 }
3762 return len;
3763}
3764
3765LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003766lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003767{
3768 LY_ERR ret;
3769 char *path;
3770 int len;
3771
3772 struct lysp_node_uses fake_uses = {
3773 .parent = pnode,
3774 .nodetype = LYS_USES,
3775 .flags = 0, .next = NULL,
3776 .name = grp->name,
3777 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3778 .refines = NULL, .augments = NULL
3779 };
3780 struct lysc_node_container fake_container = {
3781 .nodetype = LYS_CONTAINER,
3782 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3783 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003784 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003785 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003786 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003787 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003788 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3789 };
3790
3791 if (grp->parent) {
3792 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3793 }
3794
3795 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3796 if (len < 0) {
3797 LOGMEM(ctx->ctx);
3798 return LY_EMEM;
3799 }
3800 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3801 ctx->path_len = (uint32_t)len;
3802 free(path);
3803
3804 lysc_update_path(ctx, NULL, "{grouping}");
3805 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003806 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003807 lysc_update_path(ctx, NULL, NULL);
3808 lysc_update_path(ctx, NULL, NULL);
3809
3810 ctx->path_len = 1;
3811 ctx->path[1] = '\0';
3812
3813 /* cleanup */
3814 lysc_node_container_free(ctx->ctx, &fake_container);
3815
3816 return ret;
3817}
3818
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003819LY_ERR
3820lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3821 struct ly_set *child_set)
3822{
3823 LY_ERR ret = LY_SUCCESS;
3824 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02003825 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003826
3827 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3828
3829 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003830 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003831 } else {
3832 lysc_update_path(ctx, NULL, "{uses}");
3833 lysc_update_path(ctx, NULL, pnode->name);
3834 }
3835
3836 switch (pnode->nodetype) {
3837 case LYS_CONTAINER:
3838 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3839 node_compile_spec = lys_compile_node_container;
3840 break;
3841 case LYS_LEAF:
3842 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3843 node_compile_spec = lys_compile_node_leaf;
3844 break;
3845 case LYS_LIST:
3846 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3847 node_compile_spec = lys_compile_node_list;
3848 break;
3849 case LYS_LEAFLIST:
3850 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3851 node_compile_spec = lys_compile_node_leaflist;
3852 break;
3853 case LYS_CHOICE:
3854 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3855 node_compile_spec = lys_compile_node_choice;
3856 break;
3857 case LYS_CASE:
3858 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3859 node_compile_spec = lys_compile_node_case;
3860 break;
3861 case LYS_ANYXML:
3862 case LYS_ANYDATA:
3863 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3864 node_compile_spec = lys_compile_node_any;
3865 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003866 case LYS_RPC:
3867 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02003868 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003869 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3870 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003871 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003872 return LY_EVALID;
3873 }
3874 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3875 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02003876 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003877 break;
3878 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02003879 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003880 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3881 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003882 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003883 return LY_EVALID;
3884 }
3885 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3886 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02003887 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003888 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003889 case LYS_USES:
3890 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3891 lysc_update_path(ctx, NULL, NULL);
3892 lysc_update_path(ctx, NULL, NULL);
3893 return ret;
3894 default:
3895 LOGINT(ctx->ctx);
3896 return LY_EINT;
3897 }
3898 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3899
Radek Krejci2a9fc652021-01-22 17:44:34 +01003900 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003901
Michal Vasko7c565922021-06-10 14:58:27 +02003902 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003903 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003904 return ret;
3905}