blob: 9ee36b999fc3f33218b16c6d5774e633e0c5b2d1 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 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
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Michal Vasko7c8439f2020-08-05 13:25:19 +020039#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010041#include "tree_schema_internal.h"
42#include "xpath.h"
43
Michal Vasko5fe75f12020-03-02 13:52:37 +010044static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
Radek Krejci0f969882020-08-21 16:56:47 +020045 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
Michal Vasko5fe75f12020-03-02 13:52:37 +010046
Radek Krejci19a96102018-11-15 13:38:09 +010047/**
48 * @brief Duplicate string into dictionary
49 * @param[in] CTX libyang context of the dictionary.
50 * @param[in] ORIG String to duplicate.
51 * @param[out] DUP Where to store the result.
52 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020053#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
54
55#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
Radek Krejci19a96102018-11-15 13:38:09 +010056
Radek Krejciec4da802019-05-02 13:02:41 +020057#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010058 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020059 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
60 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
61 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020063 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010064 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
65 } \
66 }
67
Radek Krejciec4da802019-05-02 13:02:41 +020068#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010069 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020070 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
71 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
72 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010073 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020074 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010075 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
76 } \
77 }
78
Radek Krejci0935f412019-08-20 16:15:18 +020079#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
80 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020081 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
82 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020083 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010084 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020085 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
86 } \
87 }
88
Radek Krejciec4da802019-05-02 13:02:41 +020089#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010090 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020091 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
92 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
93 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010094 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020095 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010096 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
97 } \
98 }
99
Radek Krejciec4da802019-05-02 13:02:41 +0200100#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +0100101 if (MEMBER_P) { \
102 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
103 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200104 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100105 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
106 }
107
Radek Krejciec4da802019-05-02 13:02:41 +0200108#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 if (MEMBER_P) { \
110 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200111 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100112 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200113 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100114 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
115 }
116
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100117#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200119 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100120 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100121 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
122 return LY_EVALID; \
123 } \
124 } \
125 }
126
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
128 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200129 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100130 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
131 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
132 return LY_EVALID; \
133 } \
134 } \
135 }
136
137struct lysc_ext *
138lysc_ext_dup(struct lysc_ext *orig)
139{
140 ++orig->refcount;
141 return orig;
142}
143
Radek Krejci19a96102018-11-15 13:38:09 +0100144static struct lysc_ext_instance *
145lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
146{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100147 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100148 (void) ctx;
149 (void) orig;
150 return NULL;
151}
152
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200153static LY_ERR
154lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +0200155 struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200156{
157 struct lysc_incomplete_dflt *r;
158 uint32_t i;
159
160 for (i = 0; i < ctx->dflts.count; ++i) {
161 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
162 if (r->leaf == leaf) {
163 /* just replace the default */
164 r->dflt = dflt;
165 return LY_SUCCESS;
166 }
167 }
168
169 r = malloc(sizeof *r);
170 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
171 r->leaf = leaf;
172 r->dflt = dflt;
173 r->dflts = NULL;
174 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200175 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200176
177 return LY_SUCCESS;
178}
179
Radek Krejcib56c7502019-02-13 14:19:54 +0100180/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200181 * @brief Add record into the compile context's list of incomplete default values.
182 * @param[in] ctx Compile context with the incomplete default values list.
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200183 * @param[in] term Term context node with the default value.
184 * @param[in] value String default value.
185 * @param[in] val_len Length of @p value.
Radek Krejci474f9b82019-07-24 11:36:37 +0200186 * @param[in] dflt_mod Module of the default value definition to store in the record.
187 * @return LY_EMEM in case of memory allocation failure.
188 * @return LY_SUCCESS
189 */
190static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200191lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts,
Radek Krejci0f969882020-08-21 16:56:47 +0200192 struct lys_module *dflt_mod)
Radek Krejci474f9b82019-07-24 11:36:37 +0200193{
194 struct lysc_incomplete_dflt *r;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200195 uint32_t i;
196
197 for (i = 0; i < ctx->dflts.count; ++i) {
198 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
199 if (r->llist == llist) {
200 /* just replace the defaults */
201 r->dflts = dflts;
202 return LY_SUCCESS;
203 }
204 }
205
Radek Krejci474f9b82019-07-24 11:36:37 +0200206 r = malloc(sizeof *r);
207 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200208 r->llist = llist;
209 r->dflt = NULL;
210 r->dflts = dflts;
Radek Krejci474f9b82019-07-24 11:36:37 +0200211 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200212 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Radek Krejci474f9b82019-07-24 11:36:37 +0200213
214 return LY_SUCCESS;
215}
216
217/**
218 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
219 * @param[in] ctx Compile context with the incomplete default values list.
220 * @param[in] dflt Incomplete default values identifying the record to remove.
221 */
222static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200223lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term)
Radek Krejci474f9b82019-07-24 11:36:37 +0200224{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200225 uint32_t u;
Radek Krejci474f9b82019-07-24 11:36:37 +0200226 struct lysc_incomplete_dflt *r;
227
228 for (u = 0; u < ctx->dflts.count; ++u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200229 r = ctx->dflts.objs[u];
230 if (r->leaf == (struct lysc_node_leaf *)term) {
Radek Krejci474f9b82019-07-24 11:36:37 +0200231 free(ctx->dflts.objs[u]);
232 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
233 --ctx->dflts.count;
234 return;
235 }
236 }
237}
238
Radek Krejci0e59c312019-08-15 15:34:15 +0200239void
Radek Krejci327de162019-06-14 12:52:07 +0200240lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
241{
242 int len;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200243 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
Radek Krejci327de162019-06-14 12:52:07 +0200244
245 if (!name) {
246 /* removing last path segment */
247 if (ctx->path[ctx->path_len - 1] == '}') {
Michal Vaskod989ba02020-08-24 10:59:24 +0200248 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200249 if (ctx->path[ctx->path_len] == '=') {
250 ctx->path[ctx->path_len++] = '}';
251 } else {
252 /* not a top-level special tag, remove also preceiding '/' */
253 goto remove_nodelevel;
254 }
255 } else {
256remove_nodelevel:
Michal Vaskod989ba02020-08-24 10:59:24 +0200257 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200258 if (ctx->path_len == 0) {
259 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200260 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200261 }
262 }
263 /* set new terminating NULL-byte */
264 ctx->path[ctx->path_len] = '\0';
265 } else {
266 if (ctx->path_len > 1) {
267 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
268 /* extension of the special tag */
269 nextlevel = 2;
270 --ctx->path_len;
271 } else {
272 /* there is already some path, so add next level */
273 nextlevel = 1;
274 }
275 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
276
277 if (nextlevel != 2) {
278 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
279 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200280 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200281 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200282 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200283 }
284 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200285 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200286 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200287 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
Radek Krejciacc79042019-07-25 14:14:57 +0200288 /* output truncated */
289 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
290 } else {
291 ctx->path_len += len;
292 }
Radek Krejci327de162019-06-14 12:52:07 +0200293 }
294}
295
296/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100297 * @brief Duplicate the compiled pattern structure.
298 *
299 * Instead of duplicating memory, the reference counter in the @p orig is increased.
300 *
301 * @param[in] orig The pattern structure to duplicate.
302 * @return The duplicated structure to use.
303 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200304static struct lysc_pattern *
Radek Krejci19a96102018-11-15 13:38:09 +0100305lysc_pattern_dup(struct lysc_pattern *orig)
306{
307 ++orig->refcount;
308 return orig;
309}
310
Radek Krejcib56c7502019-02-13 14:19:54 +0100311/**
312 * @brief Duplicate the array of compiled patterns.
313 *
314 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
315 *
316 * @param[in] ctx Libyang context for logging.
317 * @param[in] orig The patterns sized array to duplicate.
318 * @return New sized array as a copy of @p orig.
319 * @return NULL in case of memory allocation error.
320 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200321static struct lysc_pattern **
Radek Krejci19a96102018-11-15 13:38:09 +0100322lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
323{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100324 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200325 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327 assert(orig);
328
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200329 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100330 LY_ARRAY_FOR(orig, u) {
331 dup[u] = lysc_pattern_dup(orig[u]);
332 LY_ARRAY_INCREMENT(dup);
333 }
334 return dup;
335}
336
Radek Krejcib56c7502019-02-13 14:19:54 +0100337/**
338 * @brief Duplicate compiled range structure.
339 *
340 * @param[in] ctx Libyang context for logging.
341 * @param[in] orig The range structure to be duplicated.
342 * @return New compiled range structure as a copy of @p orig.
343 * @return NULL in case of memory allocation error.
344 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200345struct lysc_range *
Radek Krejci19a96102018-11-15 13:38:09 +0100346lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
347{
348 struct lysc_range *dup;
349 LY_ERR ret;
350
Radek Krejcib56c7502019-02-13 14:19:54 +0100351 assert(orig);
352
Radek Krejci19a96102018-11-15 13:38:09 +0100353 dup = calloc(1, sizeof *dup);
354 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
355 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200356 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
357 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
358 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100359 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200360 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
361 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100362 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
363
364 return dup;
365cleanup:
366 free(dup);
367 (void) ret; /* set but not used due to the return type */
368 return NULL;
369}
370
Radek Krejcib56c7502019-02-13 14:19:54 +0100371/**
372 * @brief Stack for processing if-feature expressions.
373 */
Radek Krejci19a96102018-11-15 13:38:09 +0100374struct iff_stack {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200375 size_t size; /**< number of items in the stack */
376 size_t index; /**< first empty item */
377 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100378};
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Add @ref ifftokens into the stack.
382 * @param[in] stack The if-feature stack to use.
383 * @param[in] value One of the @ref ifftokens to store in the stack.
384 * @return LY_EMEM in case of memory allocation error
385 * @return LY_ESUCCESS if the value successfully stored.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static LY_ERR
388iff_stack_push(struct iff_stack *stack, uint8_t value)
389{
390 if (stack->index == stack->size) {
391 stack->size += 4;
392 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
393 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
394 }
395 stack->stack[stack->index++] = value;
396 return LY_SUCCESS;
397}
398
Radek Krejcib56c7502019-02-13 14:19:54 +0100399/**
400 * @brief Get (and remove) the last item form the stack.
401 * @param[in] stack The if-feature stack to use.
402 * @return The value from the top of the stack.
403 */
Radek Krejci19a96102018-11-15 13:38:09 +0100404static uint8_t
405iff_stack_pop(struct iff_stack *stack)
406{
Radek Krejcib56c7502019-02-13 14:19:54 +0100407 assert(stack && stack->index);
408
Radek Krejci19a96102018-11-15 13:38:09 +0100409 stack->index--;
410 return stack->stack[stack->index];
411}
412
Radek Krejcib56c7502019-02-13 14:19:54 +0100413/**
414 * @brief Clean up the stack.
415 * @param[in] stack The if-feature stack to use.
416 */
Radek Krejci19a96102018-11-15 13:38:09 +0100417static void
418iff_stack_clean(struct iff_stack *stack)
419{
420 stack->size = 0;
421 free(stack->stack);
422}
423
Radek Krejcib56c7502019-02-13 14:19:54 +0100424/**
425 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
426 * (libyang format of the if-feature expression).
427 * @param[in,out] list The 2bits array to modify.
428 * @param[in] op The operand (@ref ifftokens) to store.
429 * @param[in] pos Position (0-based) where to store the given @p op.
430 */
Radek Krejci19a96102018-11-15 13:38:09 +0100431static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200432iff_setop(uint8_t *list, uint8_t op, size_t pos)
Radek Krejci19a96102018-11-15 13:38:09 +0100433{
434 uint8_t *item;
435 uint8_t mask = 3;
436
Radek Krejci19a96102018-11-15 13:38:09 +0100437 assert(op <= 3); /* max 2 bits */
438
439 item = &list[pos / 4];
440 mask = mask << 2 * (pos % 4);
441 *item = (*item) & ~mask;
442 *item = (*item) | (op << 2 * (pos % 4));
443}
444
Radek Krejcib56c7502019-02-13 14:19:54 +0100445#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
446#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100447
Radek Krejci0af46292019-01-11 16:02:31 +0100448/**
449 * @brief Find a feature of the given name and referenced in the given module.
450 *
451 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
452 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
453 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
454 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
455 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
456 * assigned till that time will be still valid.
457 *
458 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
459 * @param[in] name Name of the feature including possible prefix.
460 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
461 * @return Pointer to the feature structure if found, NULL otherwise.
462 */
Radek Krejci19a96102018-11-15 13:38:09 +0100463static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100464lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100465{
466 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200467 LY_ARRAY_COUNT_TYPE u;
Radek Krejci14915cc2020-09-14 17:28:13 +0200468 struct lysc_feature *f;
Radek Krejci19a96102018-11-15 13:38:09 +0100469
Radek Krejci120d8542020-08-12 09:29:16 +0200470 assert(mod);
471
Radek Krejci19a96102018-11-15 13:38:09 +0100472 for (i = 0; i < len; ++i) {
473 if (name[i] == ':') {
474 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100475 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100476 LY_CHECK_RET(!mod, NULL);
477
478 name = &name[i + 1];
479 len = len - i - 1;
480 }
481 }
482
483 /* we have the correct module, get the feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200484 LY_ARRAY_FOR(mod->features, u) {
485 f = &mod->features[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200486 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100487 return f;
488 }
489 }
490
491 return NULL;
492}
493
Michal Vasko8d544252020-03-02 10:19:52 +0100494/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100495 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
496 */
497static LY_ERR
498lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
499{
500 LY_ERR ret = LY_SUCCESS;
501
502 if (!ext_p->compiled) {
503 lysc_update_path(ctx, NULL, "{extension}");
504 lysc_update_path(ctx, NULL, ext_p->name);
505
506 /* compile the extension definition */
507 ext_p->compiled = calloc(1, sizeof **ext);
508 ext_p->compiled->refcount = 1;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200509 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
510 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100511 ext_p->compiled->module = (struct lys_module *)ext_mod;
512 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
513
514 lysc_update_path(ctx, NULL, NULL);
515 lysc_update_path(ctx, NULL, NULL);
516
517 /* find extension definition plugin */
518 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
519 }
520
521 *ext = lysc_ext_dup(ext_p->compiled);
522
523done:
524 return ret;
525}
526
527/**
Michal Vasko8d544252020-03-02 10:19:52 +0100528 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
529 *
530 * @param[in] ctx Compilation context.
531 * @param[in] ext_p Parsed extension instance.
532 * @param[in,out] ext Prepared compiled extension instance.
533 * @param[in] parent Extension instance parent.
534 * @param[in] parent_type Extension instance parent type.
535 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
536 */
Radek Krejci19a96102018-11-15 13:38:09 +0100537static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100538lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200539 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100540{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200541 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100542 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200543 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200544 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200545 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100546
Radek Krejci011e4aa2020-09-04 15:22:31 +0200547 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
548 LY_CHECK_RET(ret);
549
Radek Krejci19a96102018-11-15 13:38:09 +0100550 ext->insubstmt = ext_p->insubstmt;
551 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200552 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200553 ext->parent = parent;
554 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100555
Michal Vasko22df3f02020-08-24 13:29:22 +0200556 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200557
Radek Krejci19a96102018-11-15 13:38:09 +0100558 /* get module where the extension definition should be placed */
Radek Krejci1e008d22020-08-17 11:37:37 +0200559 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
Radek Krejci7c960162019-09-18 14:16:12 +0200560 if (ext_p->yin) {
561 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
562 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
563 * YANG (import) prefix must be done here. */
564 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200565 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200566 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200567 } else {
568 assert(ctx->mod_def->parsed);
569 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
570 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200571 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200572 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1,
Radek Krejci200f1062020-07-11 22:51:03 +0200573 ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200574 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200575 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200576 break;
577 }
578 }
579 }
580 if (!prefixed_name) {
581 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
582 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200583 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200584 goto cleanup;
585 }
586 } else {
587 prefixed_name = ext_p->name;
588 }
589 lysc_update_path(ctx, NULL, prefixed_name);
590
Michal Vasko8d544252020-03-02 10:19:52 +0100591 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200592 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100593 if (!ext_mod) {
594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
595 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200596 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100597 goto cleanup;
598 } else if (!ext_mod->parsed->extensions) {
599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
600 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
601 prefixed_name, ext_mod->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200602 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100603 goto cleanup;
604 }
Radek Krejci7c960162019-09-18 14:16:12 +0200605 }
606 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200607
Michal Vasko5fe75f12020-03-02 13:52:37 +0100608 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200609 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
610 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100611 /* compile extension definition and assign it */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200612 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100613 break;
614 }
615 }
Radek Krejci7c960162019-09-18 14:16:12 +0200616 if (!ext->def) {
617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
618 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200619 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200620 goto cleanup;
621 }
Radek Krejci0935f412019-08-20 16:15:18 +0200622
Radek Krejcif56e2a42019-09-09 14:15:25 +0200623 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
624 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
625 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200626 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200627 /* Schema was parsed from YIN and an argument is expected, ... */
628 struct lysp_stmt *stmt = NULL;
629
630 if (ext->def->flags & LYS_YINELEM_TRUE) {
631 /* ... argument was the first XML child element */
632 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
633 /* TODO check namespace of the statement */
634 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
635 stmt = ext_p->child;
636 }
637 }
638 } else {
639 /* ... argument was one of the XML attributes which are represented as child stmt
640 * with LYS_YIN_ATTR flag */
641 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
642 if (!strcmp(stmt->stmt, ext->def->argument)) {
643 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200644 break;
645 }
646 }
647 }
648 if (!stmt) {
649 /* missing extension's argument */
650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200651 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200652 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200653 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200654
655 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200656 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200657 stmt->flags |= LYS_YIN_ARGUMENT;
658 }
Radek Krejci7c960162019-09-18 14:16:12 +0200659 if (prefixed_name != ext_p->name) {
660 lydict_remove(ctx->ctx, ext_p->name);
661 ext_p->name = prefixed_name;
662 if (!ext_p->argument && ext->argument) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200663 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200664 }
665 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200666
Radek Krejci0935f412019-08-20 16:15:18 +0200667 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200668 if (ext->argument) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200669 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
Radek Krejciad5963b2019-09-06 16:03:05 +0200670 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200671 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200672 if (ext->argument) {
673 lysc_update_path(ctx, NULL, NULL);
674 }
Radek Krejci0935f412019-08-20 16:15:18 +0200675 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200676 ext_p->compiled = ext;
677
Radek Krejci7c960162019-09-18 14:16:12 +0200678cleanup:
679 if (prefixed_name && prefixed_name != ext_p->name) {
680 lydict_remove(ctx->ctx, prefixed_name);
681 }
682
Radek Krejcif56e2a42019-09-09 14:15:25 +0200683 lysc_update_path(ctx, NULL, NULL);
684 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200685
Radek Krejci7c960162019-09-18 14:16:12 +0200686 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200687}
688
689/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100690 * @brief Compile information from the if-feature statement
691 * @param[in] ctx Compile context.
692 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100693 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
694 * @return LY_ERR value.
695 */
Radek Krejci19a96102018-11-15 13:38:09 +0100696static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200697lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100698{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200699 LY_ERR rc = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100700 const char *c = *value;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200701 int64_t i, j;
702 int8_t op_len, last_not = 0, checkversion = 0;
703 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci19a96102018-11-15 13:38:09 +0100704 uint8_t op;
705 struct iff_stack stack = {0, 0, NULL};
706 struct lysc_feature *f;
707
708 assert(c);
709
710 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200711 for (i = j = 0; c[i]; i++) {
Radek Krejci19a96102018-11-15 13:38:09 +0100712 if (c[i] == '(') {
713 j++;
714 checkversion = 1;
715 continue;
716 } else if (c[i] == ')') {
717 j--;
718 continue;
719 } else if (isspace(c[i])) {
720 checkversion = 1;
721 continue;
722 }
723
Radek Krejci1deb5be2020-08-26 16:43:36 +0200724 if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
725 uint64_t spaces;
726 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++);
727 if (c[i + op_len + spaces] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
729 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
730 return LY_EVALID;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200731 } else if (!isspace(c[i + op_len])) {
Radek Krejci19a96102018-11-15 13:38:09 +0100732 /* feature name starting with the not/and/or */
733 last_not = 0;
734 f_size++;
735 } else if (c[i] == 'n') { /* not operation */
736 if (last_not) {
737 /* double not */
738 expr_size = expr_size - 2;
739 last_not = 0;
740 } else {
741 last_not = 1;
742 }
743 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200744 if (f_exp != f_size) {
745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200746 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, op_len, &c[i]);
Radek Krejci6788abc2019-06-14 13:56:49 +0200747 return LY_EVALID;
748 }
Radek Krejci19a96102018-11-15 13:38:09 +0100749 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200750
Radek Krejci19a96102018-11-15 13:38:09 +0100751 /* not a not operation */
752 last_not = 0;
753 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200754 i += op_len;
Radek Krejci19a96102018-11-15 13:38:09 +0100755 } else {
756 f_size++;
757 last_not = 0;
758 }
759 expr_size++;
760
761 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200762 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100763 i--;
764 break;
765 }
766 i++;
767 }
768 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200769 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100770 /* not matching count of ( and ) */
771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
772 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
773 return LY_EVALID;
774 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200775 if (f_exp != f_size) {
776 /* features do not match the needed arguments for the logical operations */
777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
778 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
779 "the required number of operands for the operations.", *value);
780 return LY_EVALID;
781 }
Radek Krejci19a96102018-11-15 13:38:09 +0100782
783 if (checkversion || expr_size > 1) {
784 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100785 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
787 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
788 return LY_EVALID;
789 }
790 }
791
792 /* allocate the memory */
793 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
794 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
795 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200796 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
Radek Krejci19a96102018-11-15 13:38:09 +0100797
798 stack.size = expr_size;
799 f_size--; expr_size--; /* used as indexes from now */
800
801 for (i--; i >= 0; i--) {
802 if (c[i] == ')') {
803 /* push it on stack */
804 iff_stack_push(&stack, LYS_IFF_RP);
805 continue;
806 } else if (c[i] == '(') {
807 /* pop from the stack into result all operators until ) */
Michal Vaskod989ba02020-08-24 10:59:24 +0200808 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
Radek Krejci19a96102018-11-15 13:38:09 +0100809 iff_setop(iff->expr, op, expr_size--);
810 }
811 continue;
812 } else if (isspace(c[i])) {
813 continue;
814 }
815
816 /* end of operator or operand -> find beginning and get what is it */
817 j = i + 1;
818 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
819 i--;
820 }
821 i++; /* go back by one step */
822
823 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
824 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
825 /* double not */
826 iff_stack_pop(&stack);
827 } else {
828 /* not has the highest priority, so do not pop from the stack
829 * as in case of AND and OR */
830 iff_stack_push(&stack, LYS_IFF_NOT);
831 }
832 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
833 /* as for OR - pop from the stack all operators with the same or higher
834 * priority and store them to the result, then push the AND to the stack */
835 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
836 op = iff_stack_pop(&stack);
837 iff_setop(iff->expr, op, expr_size--);
838 }
839 iff_stack_push(&stack, LYS_IFF_AND);
840 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
841 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
842 op = iff_stack_pop(&stack);
843 iff_setop(iff->expr, op, expr_size--);
844 }
845 iff_stack_push(&stack, LYS_IFF_OR);
846 } else {
847 /* feature name, length is j - i */
848
849 /* add it to the expression */
850 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
851
852 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100853 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
854 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
855 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
856 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100857 iff->features[f_size] = f;
858 LY_ARRAY_INCREMENT(iff->features);
859 f_size--;
860 }
861 }
862 while (stack.index) {
863 op = iff_stack_pop(&stack);
864 iff_setop(iff->expr, op, expr_size--);
865 }
866
867 if (++expr_size || ++f_size) {
868 /* not all expected operators and operands found */
869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
870 "Invalid value \"%s\" of if-feature - processing error.", *value);
871 rc = LY_EINT;
872 } else {
873 rc = LY_SUCCESS;
874 }
875
876error:
877 /* cleanup */
878 iff_stack_clean(&stack);
879
880 return rc;
881}
882
Radek Krejcib56c7502019-02-13 14:19:54 +0100883/**
Michal Vasko175012e2019-11-06 15:49:14 +0100884 * @brief Get the XPath context node for the given schema node.
885 * @param[in] start The schema node where the XPath expression appears.
886 * @return The context node to evaluate XPath expression in given schema node.
887 * @return NULL in case the context node is the root node.
888 */
889static struct lysc_node *
890lysc_xpath_context(struct lysc_node *start)
891{
Michal Vasko1bf09392020-03-27 12:38:10 +0100892 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Radek Krejci1e008d22020-08-17 11:37:37 +0200893 start = start->parent) {}
Michal Vasko175012e2019-11-06 15:49:14 +0100894 return start;
895}
896
897/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100898 * @brief Compile information from the when statement
899 * @param[in] ctx Compile context.
900 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100901 * @param[in] flags Flags of the node with the "when" defiition.
902 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100903 * @param[out] when Pointer where to store pointer to the created compiled when structure.
904 * @return LY_ERR value.
905 */
Radek Krejci19a96102018-11-15 13:38:09 +0100906static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100907lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100908{
Radek Krejci19a96102018-11-15 13:38:09 +0100909 LY_ERR ret = LY_SUCCESS;
910
Radek Krejci00b874b2019-02-12 10:54:50 +0100911 *when = calloc(1, sizeof **when);
912 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200913 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200914 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100915 (*when)->context = lysc_xpath_context(node);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200916 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
917 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejci00b874b2019-02-12 10:54:50 +0100918 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200919 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100920 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100921
922done:
923 return ret;
924}
925
Radek Krejcib56c7502019-02-13 14:19:54 +0100926/**
927 * @brief Compile information from the must statement
928 * @param[in] ctx Compile context.
929 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100930 * @param[in,out] must Prepared (empty) compiled must structure to fill.
931 * @return LY_ERR value.
932 */
Radek Krejci19a96102018-11-15 13:38:09 +0100933static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200934lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100935{
Radek Krejci19a96102018-11-15 13:38:09 +0100936 LY_ERR ret = LY_SUCCESS;
937
Michal Vasko004d3152020-06-11 19:59:22 +0200938 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100939 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200940 must->module = ctx->mod_def;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200941 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
942 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
943 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
944 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200945 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100946
947done:
948 return ret;
949}
950
Radek Krejcib56c7502019-02-13 14:19:54 +0100951/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200952 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100953 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200954 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100955 * @return LY_ERR value.
956 */
Radek Krejci19a96102018-11-15 13:38:09 +0100957static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200958lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100959{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200960 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100961 LY_ERR ret = LY_SUCCESS;
962
Radek Krejci7f2a5362018-11-28 13:05:37 +0100963 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
964 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100965 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200966 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100967 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200968 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200969 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200970 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200971 LOGINT(ctx->ctx);
972 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200973 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
Michal Vasko3a41dff2020-07-15 14:30:28 +0200974 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200975 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200976 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200977 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200978 mod = NULL;
979 }
Radek Krejci19a96102018-11-15 13:38:09 +0100980 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200981 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100982 }
983 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200984 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100985 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200986 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100987 return LY_ENOTFOUND;
988 }
989 }
Radek Krejci19a96102018-11-15 13:38:09 +0100990 }
991
Radek Krejci19a96102018-11-15 13:38:09 +0100992 return ret;
993}
994
Michal Vasko33ff9422020-07-03 09:50:39 +0200995LY_ERR
996lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +0200997 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100998{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200999 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +02001000 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01001001 LY_ERR ret = LY_SUCCESS;
1002
Michal Vasko33ff9422020-07-03 09:50:39 +02001003 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +02001004
Michal Vasko33ff9422020-07-03 09:50:39 +02001005 if (!ctx_sc) {
1006 context.ctx = ctx;
1007 context.mod = module;
Radek Krejci120d8542020-08-12 09:29:16 +02001008 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +02001009 context.path_len = 1;
1010 context.path[0] = '/';
1011 ctx_sc = &context;
1012 }
Radek Krejci19a96102018-11-15 13:38:09 +01001013
Michal Vasko33ff9422020-07-03 09:50:39 +02001014 if (!identities_p) {
1015 return LY_SUCCESS;
1016 }
1017 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001018 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02001019 }
1020
1021 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001022 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001023 LY_ARRAY_FOR(identities_p, u) {
1024 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
1025
1026 LY_ARRAY_INCREMENT(*identities);
1027 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001028 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
1029 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
1030 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
Michal Vasko33ff9422020-07-03 09:50:39 +02001031 (*identities)[offset + u].module = ctx_sc->mod;
1032 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
1033 lys_compile_iffeature, ret, done);
1034 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
1035 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
1036 LYEXT_PAR_IDENT, ret, done);
1037 (*identities)[offset + u].flags = identities_p[u].flags;
1038
1039 lysc_update_path(ctx_sc, NULL, NULL);
1040 }
1041 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001042done:
1043 return ret;
1044}
1045
Radek Krejcib56c7502019-02-13 14:19:54 +01001046/**
1047 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1048 *
1049 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1050 *
1051 * @param[in] ctx Compile context for logging.
1052 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1053 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1054 * @return LY_SUCCESS if everything is ok.
1055 * @return LY_EVALID if the identity is derived from itself.
1056 */
Radek Krejci38222632019-02-12 16:55:05 +01001057static LY_ERR
1058lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1059{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001060 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001061 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001062 struct ly_set recursion = {0};
1063 struct lysc_ident *drv;
1064
1065 if (!derived) {
1066 return LY_SUCCESS;
1067 }
1068
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001069 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001070 if (ident == derived[u]) {
1071 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1072 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001073 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001074 goto cleanup;
1075 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001076 ret = ly_set_add(&recursion, derived[u], 0, NULL);
1077 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001078 }
1079
1080 for (v = 0; v < recursion.count; ++v) {
1081 drv = recursion.objs[v];
1082 if (!drv->derived) {
1083 continue;
1084 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001085 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001086 if (ident == drv->derived[u]) {
1087 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1088 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001089 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001090 goto cleanup;
1091 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001092 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
1093 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001094 }
1095 }
Radek Krejci38222632019-02-12 16:55:05 +01001096
1097cleanup:
1098 ly_set_erase(&recursion, NULL);
1099 return ret;
1100}
1101
Radek Krejcia3045382018-11-22 14:30:31 +01001102/**
1103 * @brief Find and process the referenced base identities from another identity or identityref
1104 *
Radek Krejciaca74032019-06-04 08:53:06 +02001105 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001106 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1107 * to distinguish these two use cases.
1108 *
1109 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1110 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001111 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001112 * @param[in] bases Array of bases of identityref to fill in.
1113 * @return LY_ERR value.
1114 */
Radek Krejci19a96102018-11-15 13:38:09 +01001115static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001116lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001117 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001118{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001119 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001120 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001121 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001122 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001123
1124 assert(ident || bases);
1125
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001126 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1128 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1129 return LY_EVALID;
1130 }
1131
Michal Vasko33ff9422020-07-03 09:50:39 +02001132 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001133 s = strchr(bases_p[u], ':');
1134 if (s) {
1135 /* prefixed identity */
1136 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001137 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001138 } else {
1139 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001140 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001141 }
1142 if (!mod) {
1143 if (ident) {
1144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1145 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1146 } else {
1147 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1148 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1149 }
1150 return LY_EVALID;
1151 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001152
Radek Krejci555cb5b2018-11-16 14:54:33 +01001153 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001154 if (mod->compiled) {
1155 identities = mod->compiled->identities;
1156 } else {
1157 identities = mod->dis_identities;
1158 }
1159 LY_ARRAY_FOR(identities, v) {
1160 if (!strcmp(name, identities[v].name)) {
1161 if (ident) {
1162 if (ident == &identities[v]) {
1163 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1164 "Identity \"%s\" is derived from itself.", ident->name);
1165 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001166 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001167 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1168 /* we have match! store the backlink */
1169 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1170 *idref = ident;
1171 } else {
1172 /* we have match! store the found identity */
1173 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1174 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001175 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001176 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001177 }
1178 }
1179 if (!idref || !(*idref)) {
1180 if (ident) {
1181 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1182 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1183 } else {
1184 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1185 "Unable to find base (%s) of identityref.", bases_p[u]);
1186 }
1187 return LY_EVALID;
1188 }
1189 }
1190 return LY_SUCCESS;
1191}
1192
Radek Krejcia3045382018-11-22 14:30:31 +01001193/**
1194 * @brief For the given array of identities, set the backlinks from all their base identities.
1195 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1196 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1197 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1198 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1199 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001200static LY_ERR
1201lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1202{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001203 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001204
Michal Vasko33ff9422020-07-03 09:50:39 +02001205 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001206 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001207 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001208 continue;
1209 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001210 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001211 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module, idents_p[u].bases, &idents[u], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001212 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001213 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001214 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001215 return LY_SUCCESS;
1216}
1217
Radek Krejci0af46292019-01-11 16:02:31 +01001218LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001219lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001220 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001221{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001222 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001223 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001224 struct lysc_ctx context = {0};
1225
Radek Krejci327de162019-06-14 12:52:07 +02001226 assert(ctx_sc || ctx);
1227
1228 if (!ctx_sc) {
1229 context.ctx = ctx;
1230 context.mod = module;
1231 context.path_len = 1;
1232 context.path[0] = '/';
1233 ctx_sc = &context;
1234 }
Radek Krejci0af46292019-01-11 16:02:31 +01001235
1236 if (!features_p) {
1237 return LY_SUCCESS;
1238 }
1239 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001240 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001241 }
1242
Radek Krejci327de162019-06-14 12:52:07 +02001243 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001244 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001245 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001246 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1247
Radek Krejci0af46292019-01-11 16:02:31 +01001248 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001249 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001250 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
1251 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
1252 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001253 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001254 (*features)[offset + u].module = ctx_sc->mod;
1255
1256 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001257 }
Radek Krejci327de162019-06-14 12:52:07 +02001258 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001259
Radek Krejci011e4aa2020-09-04 15:22:31 +02001260done:
1261 return ret;
Radek Krejci0af46292019-01-11 16:02:31 +01001262}
1263
Radek Krejcia3045382018-11-22 14:30:31 +01001264/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001265 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001266 *
1267 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1268 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001269 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001270 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1271 * being currently processed).
1272 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +01001273 * @return LY_SUCCESS if everything is ok.
1274 * @return LY_EVALID if the feature references indirectly itself.
1275 */
1276static LY_ERR
1277lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1278{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001279 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001280 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001281 struct ly_set recursion = {0};
1282 struct lysc_feature *drv;
1283
1284 if (!depfeatures) {
1285 return LY_SUCCESS;
1286 }
1287
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001288 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001289 if (feature == depfeatures[u]) {
1290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1291 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001292 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001293 goto cleanup;
1294 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001295 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
1296 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001297 }
1298
1299 for (v = 0; v < recursion.count; ++v) {
1300 drv = recursion.objs[v];
1301 if (!drv->depfeatures) {
1302 continue;
1303 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001304 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001305 if (feature == drv->depfeatures[u]) {
1306 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1307 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001308 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001309 goto cleanup;
1310 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001311 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
1312 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001313 }
1314 }
Radek Krejci09a1fc52019-02-13 10:55:17 +01001315
1316cleanup:
1317 ly_set_erase(&recursion, NULL);
1318 return ret;
1319}
1320
1321/**
Radek Krejci0af46292019-01-11 16:02:31 +01001322 * @brief Create pre-compiled features array.
1323 *
1324 * See lys_feature_precompile() for more details.
1325 *
Radek Krejcia3045382018-11-22 14:30:31 +01001326 * @param[in] ctx Compile context.
1327 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001328 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001329 * @return LY_ERR value.
1330 */
Radek Krejci19a96102018-11-15 13:38:09 +01001331static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001332lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001333{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001334 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001335 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001336 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001337
Radek Krejci0af46292019-01-11 16:02:31 +01001338 /* find the preprecompiled feature */
1339 LY_ARRAY_FOR(features, x) {
1340 if (strcmp(features[x].name, feature_p->name)) {
1341 continue;
1342 }
1343 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001344 lysc_update_path(ctx, NULL, "{feature}");
1345 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001346
Radek Krejci0af46292019-01-11 16:02:31 +01001347 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001348 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001349 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001350 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001351 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001352 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001353 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001354 /* check for circular dependency - direct reference first,... */
1355 if (feature == feature->iffeatures[u].features[v]) {
1356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1357 "Feature \"%s\" is referenced from itself.", feature->name);
1358 return LY_EVALID;
1359 }
1360 /* ... and indirect circular reference */
1361 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1362
Radek Krejci0af46292019-01-11 16:02:31 +01001363 /* add itself into the dependants list */
1364 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1365 *df = feature;
1366 }
Radek Krejci19a96102018-11-15 13:38:09 +01001367 }
Radek Krejci19a96102018-11-15 13:38:09 +01001368 }
1369 }
Radek Krejci327de162019-06-14 12:52:07 +02001370 lysc_update_path(ctx, NULL, NULL);
1371 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001372 done:
1373 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001374 }
Radek Krejci0af46292019-01-11 16:02:31 +01001375
1376 LOGINT(ctx->ctx);
1377 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001378}
1379
Radek Krejcib56c7502019-02-13 14:19:54 +01001380/**
1381 * @brief Revert compiled list of features back to the precompiled state.
1382 *
1383 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
Michal Vasko33ff9422020-07-03 09:50:39 +02001384 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001385 *
1386 * @param[in] ctx Compilation context.
1387 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
Michal Vasko33ff9422020-07-03 09:50:39 +02001388 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001389 */
Radek Krejci95710c92019-02-11 15:49:55 +01001390static void
1391lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1392{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001393 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001394
Michal Vasko33ff9422020-07-03 09:50:39 +02001395 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001396 * which may points into the data being freed here */
Radek Krejci14915cc2020-09-14 17:28:13 +02001397 LY_ARRAY_FOR(mod->features, u) {
1398 LY_ARRAY_FOR(mod->features[u].iffeatures, v) {
1399 lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001400 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001401 LY_ARRAY_FREE(mod->features[u].iffeatures);
1402 mod->features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001403
Radek Krejci14915cc2020-09-14 17:28:13 +02001404 LY_ARRAY_FOR(mod->features[u].exts, v) {
1405 lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001406 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001407 LY_ARRAY_FREE(mod->features[u].exts);
1408 mod->features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001409 }
1410}
1411
Radek Krejcia3045382018-11-22 14:30:31 +01001412/**
1413 * @brief Validate and normalize numeric value from a range definition.
1414 * @param[in] ctx Compile context.
1415 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1416 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1417 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1418 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1419 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1420 * @param[in] value String value of the range boundary.
1421 * @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.
1422 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1423 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1424 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001425LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001426range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001427{
Radek Krejci6cba4292018-11-15 17:33:29 +01001428 size_t fraction = 0, size;
1429
Radek Krejci19a96102018-11-15 13:38:09 +01001430 *len = 0;
1431
1432 assert(value);
1433 /* parse value */
1434 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1435 return LY_EVALID;
1436 }
1437
1438 if ((value[*len] == '-') || (value[*len] == '+')) {
1439 ++(*len);
1440 }
1441
1442 while (isdigit(value[*len])) {
1443 ++(*len);
1444 }
1445
1446 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001447 if (basetype == LY_TYPE_DEC64) {
1448 goto decimal;
1449 } else {
1450 *valcopy = strndup(value, *len);
1451 return LY_SUCCESS;
1452 }
Radek Krejci19a96102018-11-15 13:38:09 +01001453 }
1454 fraction = *len;
1455
1456 ++(*len);
1457 while (isdigit(value[*len])) {
1458 ++(*len);
1459 }
1460
Radek Krejci6cba4292018-11-15 17:33:29 +01001461 if (basetype == LY_TYPE_DEC64) {
1462decimal:
1463 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001464 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001465 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1466 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1467 *len, value, frdigits);
1468 return LY_EINVAL;
1469 }
1470 if (fraction) {
1471 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1472 } else {
1473 size = (*len) + frdigits + 1;
1474 }
1475 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001476 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1477
Radek Krejci6cba4292018-11-15 17:33:29 +01001478 (*valcopy)[size - 1] = '\0';
1479 if (fraction) {
1480 memcpy(&(*valcopy)[0], &value[0], fraction);
1481 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1482 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1483 } else {
1484 memcpy(&(*valcopy)[0], &value[0], *len);
1485 memset(&(*valcopy)[*len], '0', frdigits);
1486 }
Radek Krejci19a96102018-11-15 13:38:09 +01001487 }
1488 return LY_SUCCESS;
1489}
1490
Radek Krejcia3045382018-11-22 14:30:31 +01001491/**
1492 * @brief Check that values in range are in ascendant order.
1493 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001494 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1495 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001496 * @param[in] value Current value to check.
1497 * @param[in] prev_value The last seen value.
1498 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1499 */
Radek Krejci19a96102018-11-15 13:38:09 +01001500static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001501range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001502{
1503 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001504 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001505 return LY_EEXIST;
1506 }
1507 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001508 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001509 return LY_EEXIST;
1510 }
1511 }
1512 return LY_SUCCESS;
1513}
1514
Radek Krejcia3045382018-11-22 14:30:31 +01001515/**
1516 * @brief Set min/max value of the range part.
1517 * @param[in] ctx Compile context.
1518 * @param[in] part Range part structure to fill.
1519 * @param[in] max Flag to distinguish if storing min or max value.
1520 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1521 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1522 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1523 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1524 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001525 * @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.
Radek Krejcia3045382018-11-22 14:30:31 +01001526 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1527 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1528 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1529 * frdigits value), LY_EMEM.
1530 */
Radek Krejci19a96102018-11-15 13:38:09 +01001531static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001532range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
1533 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001534{
1535 LY_ERR ret = LY_SUCCESS;
1536 char *valcopy = NULL;
1537 size_t len;
1538
1539 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001540 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001541 LY_CHECK_GOTO(ret, finalize);
1542 }
1543 if (!valcopy && base_range) {
1544 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001545 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001546 } else {
1547 part->min_64 = base_range->parts[0].min_64;
1548 }
1549 if (!first) {
1550 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1551 }
1552 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001553 }
1554
1555 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001556 case LY_TYPE_INT8: /* range */
1557 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001558 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001559 } else if (max) {
1560 part->max_64 = INT64_C(127);
1561 } else {
1562 part->min_64 = INT64_C(-128);
1563 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001564 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001565 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001566 }
1567 break;
1568 case LY_TYPE_INT16: /* range */
1569 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001570 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001571 } else if (max) {
1572 part->max_64 = INT64_C(32767);
1573 } else {
1574 part->min_64 = INT64_C(-32768);
1575 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001576 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001577 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001578 }
1579 break;
1580 case LY_TYPE_INT32: /* range */
1581 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001582 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001583 } else if (max) {
1584 part->max_64 = INT64_C(2147483647);
1585 } else {
1586 part->min_64 = INT64_C(-2147483648);
1587 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001588 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001589 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001590 }
1591 break;
1592 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001593 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001594 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001595 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001596 max ? &part->max_64 : &part->min_64);
1597 } else if (max) {
1598 part->max_64 = INT64_C(9223372036854775807);
1599 } else {
1600 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1601 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001602 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001603 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001604 }
1605 break;
1606 case LY_TYPE_UINT8: /* range */
1607 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001608 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001609 } else if (max) {
1610 part->max_u64 = UINT64_C(255);
1611 } else {
1612 part->min_u64 = UINT64_C(0);
1613 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001614 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001615 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001616 }
1617 break;
1618 case LY_TYPE_UINT16: /* range */
1619 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001620 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001621 } else if (max) {
1622 part->max_u64 = UINT64_C(65535);
1623 } else {
1624 part->min_u64 = UINT64_C(0);
1625 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001626 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001627 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001628 }
1629 break;
1630 case LY_TYPE_UINT32: /* range */
1631 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001632 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001633 } else if (max) {
1634 part->max_u64 = UINT64_C(4294967295);
1635 } else {
1636 part->min_u64 = UINT64_C(0);
1637 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001638 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001639 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001640 }
1641 break;
1642 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001643 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001644 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001645 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001646 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001647 } else if (max) {
1648 part->max_u64 = UINT64_C(18446744073709551615);
1649 } else {
1650 part->min_u64 = UINT64_C(0);
1651 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001652 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001653 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001654 }
1655 break;
1656 default:
1657 LOGINT(ctx->ctx);
1658 ret = LY_EINT;
1659 }
1660
Radek Krejci5969f272018-11-23 10:03:58 +01001661finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001662 if (ret == LY_EDENIED) {
1663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1664 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1665 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1666 } else if (ret == LY_EVALID) {
1667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1668 "Invalid %s restriction - invalid value \"%s\".",
1669 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1670 } else if (ret == LY_EEXIST) {
1671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1672 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001673 length_restr ? "length" : "range",
Radek Krejci0f969882020-08-21 16:56:47 +02001674 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001675 } else if (!ret && value) {
1676 *value = *value + len;
1677 }
1678 free(valcopy);
1679 return ret;
1680}
1681
Radek Krejcia3045382018-11-22 14:30:31 +01001682/**
1683 * @brief Compile the parsed range restriction.
1684 * @param[in] ctx Compile context.
1685 * @param[in] range_p Parsed range structure to compile.
1686 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1687 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1688 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1689 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1690 * range restriction must be more restrictive than the base_range.
1691 * @param[in,out] range Pointer to the created current range structure.
1692 * @return LY_ERR value.
1693 */
Radek Krejci19a96102018-11-15 13:38:09 +01001694static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001695lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
Radek Krejci0f969882020-08-21 16:56:47 +02001696 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001697{
1698 LY_ERR ret = LY_EVALID;
1699 const char *expr;
1700 struct lysc_range_part *parts = NULL, *part;
Radek Krejci857189e2020-09-01 13:26:36 +02001701 ly_bool range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001702 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001703
1704 assert(range);
1705 assert(range_p);
1706
1707 expr = range_p->arg;
Michal Vaskod989ba02020-08-24 10:59:24 +02001708 while (1) {
Radek Krejci19a96102018-11-15 13:38:09 +01001709 if (isspace(*expr)) {
1710 ++expr;
1711 } else if (*expr == '\0') {
1712 if (range_expected) {
1713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1714 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1715 length_restr ? "length" : "range", range_p->arg);
1716 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001717 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1719 "Invalid %s restriction - unexpected end of the expression (%s).",
1720 length_restr ? "length" : "range", range_p->arg);
1721 goto cleanup;
1722 }
1723 parts_done++;
1724 break;
1725 } else if (!strncmp(expr, "min", 3)) {
1726 if (parts) {
1727 /* min cannot be used elsewhere than in the first part */
1728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1729 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1730 expr - range_p->arg, range_p->arg);
1731 goto cleanup;
1732 }
1733 expr += 3;
1734
1735 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001736 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001737 part->max_64 = part->min_64;
1738 } else if (*expr == '|') {
1739 if (!parts || range_expected) {
1740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1741 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1742 goto cleanup;
1743 }
1744 expr++;
1745 parts_done++;
1746 /* process next part of the expression */
1747 } else if (!strncmp(expr, "..", 2)) {
1748 expr += 2;
1749 while (isspace(*expr)) {
1750 expr++;
1751 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001752 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1754 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1755 goto cleanup;
1756 }
1757 /* continue expecting the upper boundary */
1758 range_expected = 1;
1759 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1760 /* number */
1761 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001762 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001763 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001764 range_expected = 0;
1765 } else {
1766 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001767 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001768 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001769 part->max_64 = part->min_64;
1770 }
1771
1772 /* continue with possible another expression part */
1773 } else if (!strncmp(expr, "max", 3)) {
1774 expr += 3;
1775 while (isspace(*expr)) {
1776 expr++;
1777 }
1778 if (*expr != '\0') {
1779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1780 length_restr ? "length" : "range", expr);
1781 goto cleanup;
1782 }
1783 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001784 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001785 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001786 range_expected = 0;
1787 } else {
1788 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001789 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001790 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001791 part->min_64 = part->max_64;
1792 }
1793 } else {
1794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1795 length_restr ? "length" : "range", expr);
1796 goto cleanup;
1797 }
1798 }
1799
1800 /* check with the previous range/length restriction */
1801 if (base_range) {
1802 switch (basetype) {
1803 case LY_TYPE_BINARY:
1804 case LY_TYPE_UINT8:
1805 case LY_TYPE_UINT16:
1806 case LY_TYPE_UINT32:
1807 case LY_TYPE_UINT64:
1808 case LY_TYPE_STRING:
1809 uns = 1;
1810 break;
1811 case LY_TYPE_DEC64:
1812 case LY_TYPE_INT8:
1813 case LY_TYPE_INT16:
1814 case LY_TYPE_INT32:
1815 case LY_TYPE_INT64:
1816 uns = 0;
1817 break;
1818 default:
1819 LOGINT(ctx->ctx);
1820 ret = LY_EINT;
1821 goto cleanup;
1822 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001823 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001824 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1825 goto baseerror;
1826 }
1827 /* current lower bound is not lower than the base */
1828 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1829 /* base has single value */
1830 if (base_range->parts[v].min_64 == parts[u].min_64) {
1831 /* both lower bounds are the same */
1832 if (parts[u].min_64 != parts[u].max_64) {
1833 /* current continues with a range */
1834 goto baseerror;
1835 } else {
1836 /* equal single values, move both forward */
1837 ++v;
1838 continue;
1839 }
1840 } else {
1841 /* base is single value lower than current range, so the
1842 * value from base range is removed in the current,
1843 * move only base and repeat checking */
1844 ++v;
1845 --u;
1846 continue;
1847 }
1848 } else {
1849 /* base is the range */
1850 if (parts[u].min_64 == parts[u].max_64) {
1851 /* current is a single value */
1852 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1853 /* current is behind the base range, so base range is omitted,
1854 * move the base and keep the current for further check */
1855 ++v;
1856 --u;
1857 } /* else it is within the base range, so move the current, but keep the base */
1858 continue;
1859 } else {
1860 /* both are ranges - check the higher bound, the lower was already checked */
1861 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1862 /* higher bound is higher than the current higher bound */
1863 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1864 /* but the current lower bound is also higher, so the base range is omitted,
1865 * continue with the same current, but move the base */
1866 --u;
1867 ++v;
1868 continue;
1869 }
1870 /* current range starts within the base range but end behind it */
1871 goto baseerror;
1872 } else {
1873 /* current range is smaller than the base,
1874 * move current, but stay with the base */
1875 continue;
1876 }
1877 }
1878 }
1879 }
1880 if (u != parts_done) {
1881baseerror:
1882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1883 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1884 length_restr ? "length" : "range", range_p->arg);
1885 goto cleanup;
1886 }
1887 }
1888
1889 if (!(*range)) {
1890 *range = calloc(1, sizeof **range);
1891 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1892 }
1893
Radek Krejcic8b31002019-01-08 10:24:45 +01001894 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001895 if (range_p->eapptag) {
1896 lydict_remove(ctx->ctx, (*range)->eapptag);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001897 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001898 }
1899 if (range_p->emsg) {
1900 lydict_remove(ctx->ctx, (*range)->emsg);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001901 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001902 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001903 if (range_p->dsc) {
1904 lydict_remove(ctx->ctx, (*range)->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001905 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001906 }
1907 if (range_p->ref) {
1908 lydict_remove(ctx->ctx, (*range)->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001909 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001910 }
Radek Krejci19a96102018-11-15 13:38:09 +01001911 /* extensions are taken only from the last range by the caller */
1912
1913 (*range)->parts = parts;
1914 parts = NULL;
1915 ret = LY_SUCCESS;
1916cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001917 LY_ARRAY_FREE(parts);
1918
1919 return ret;
1920}
1921
1922/**
1923 * @brief Checks pattern syntax.
1924 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925 * @param[in] ctx Context.
1926 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001927 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001928 * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
Radek Krejcia3045382018-11-22 14:30:31 +01001929 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001930 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931LY_ERR
1932lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001933{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001934 size_t idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001935 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001936 int err_code;
1937 const char *orig_ptr;
1938 PCRE2_SIZE err_offset;
1939 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001940#define URANGE_LEN 19
1941 char *ublock2urange[][2] = {
1942 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1943 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1944 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1945 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1946 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1947 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1948 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1949 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1950 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1951 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1952 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1953 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1954 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1955 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1956 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1957 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1958 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1959 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1960 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1961 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1962 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1963 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1964 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1965 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1966 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1967 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1968 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1969 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1970 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1971 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1972 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1973 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1974 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1975 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1976 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1977 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1978 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1979 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1980 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1981 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1982 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1983 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1984 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1985 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1986 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1987 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1988 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1989 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1990 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1991 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1992 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1993 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1994 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1995 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1996 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1997 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1998 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1999 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
2000 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
2001 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
2002 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
2003 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
2004 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
2005 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
2006 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
2007 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
2008 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
2009 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
2010 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
2011 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
2012 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
2013 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
2014 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
2015 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
2016 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
2017 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
2018 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
2019 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
2020 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
2021 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
2022 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
2023 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
2024 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
2025 {NULL, NULL}
2026 };
2027
2028 /* adjust the expression to a Perl equivalent
2029 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
2030
Michal Vasko40a00082020-05-27 15:20:01 +02002031 /* allocate space for the transformed pattern */
2032 size = strlen(pattern) + 1;
2033 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002035 perl_regex[0] = '\0';
2036
Michal Vasko40a00082020-05-27 15:20:01 +02002037 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
2038 brack = 0;
2039 idx = 0;
2040 orig_ptr = pattern;
2041 while (orig_ptr[0]) {
2042 switch (orig_ptr[0]) {
2043 case '$':
2044 case '^':
2045 if (!brack) {
2046 /* make space for the extra character */
2047 ++size;
2048 perl_regex = ly_realloc(perl_regex, size);
2049 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002050
Michal Vasko40a00082020-05-27 15:20:01 +02002051 /* print escape slash */
2052 perl_regex[idx] = '\\';
2053 ++idx;
2054 }
2055 break;
2056 case '[':
2057 /* must not be escaped */
2058 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2059 ++brack;
2060 }
2061 break;
2062 case ']':
2063 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2064 /* pattern was checked and compiled already */
2065 assert(brack);
2066 --brack;
2067 }
2068 break;
2069 default:
2070 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002071 }
Michal Vasko40a00082020-05-27 15:20:01 +02002072
2073 /* copy char */
2074 perl_regex[idx] = orig_ptr[0];
2075
2076 ++idx;
2077 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002078 }
Michal Vasko40a00082020-05-27 15:20:01 +02002079 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002080
2081 /* substitute Unicode Character Blocks with exact Character Ranges */
2082 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2083 start = ptr - perl_regex;
2084
2085 ptr = strchr(ptr, '}');
2086 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002088 pattern, perl_regex + start + 2, "unterminated character property");
2089 free(perl_regex);
2090 return LY_EVALID;
2091 }
2092 end = (ptr - perl_regex) + 1;
2093
2094 /* need more space */
2095 if (end - start < URANGE_LEN) {
2096 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002098 }
2099
2100 /* find our range */
2101 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2102 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2103 break;
2104 }
2105 }
2106 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002108 pattern, perl_regex + start + 5, "unknown block name");
2109 free(perl_regex);
2110 return LY_EVALID;
2111 }
2112
2113 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
Michal Vasko40a00082020-05-27 15:20:01 +02002114 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002115 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002116 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002117 }
2118 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002119 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002120 }
2121 }
Michal Vasko40a00082020-05-27 15:20:01 +02002122 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002123 /* skip brackets */
2124 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2125 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2126 } else {
2127 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2128 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2129 }
2130 }
2131
2132 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002133 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2134 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002135 &err_code, &err_offset, NULL);
2136 if (!code_local) {
2137 PCRE2_UCHAR err_msg[256] = {0};
2138 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002139 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002140 free(perl_regex);
2141 return LY_EVALID;
2142 }
2143 free(perl_regex);
2144
Radek Krejci54579462019-04-30 12:47:06 +02002145 if (code) {
2146 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002147 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002148 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002149 }
2150
2151 return LY_SUCCESS;
2152
2153#undef URANGE_LEN
2154}
2155
Radek Krejcia3045382018-11-22 14:30:31 +01002156/**
2157 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2158 * @param[in] ctx Compile context.
2159 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002160 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2161 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2162 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2163 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2164 */
Radek Krejci19a96102018-11-15 13:38:09 +01002165static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002166lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci0f969882020-08-21 16:56:47 +02002167 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
Radek Krejci19a96102018-11-15 13:38:09 +01002168{
2169 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002170 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002171 LY_ERR ret = LY_SUCCESS;
2172
2173 /* first, copy the patterns from the base type */
2174 if (base_patterns) {
2175 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2176 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2177 }
2178
2179 LY_ARRAY_FOR(patterns_p, u) {
2180 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2181 *pattern = calloc(1, sizeof **pattern);
2182 ++(*pattern)->refcount;
2183
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002185 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002186
2187 if (patterns_p[u].arg[0] == 0x15) {
2188 (*pattern)->inverted = 1;
2189 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002190 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done);
2191 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
2192 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
2193 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
2194 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002195 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002196 }
2197done:
2198 return ret;
2199}
2200
Radek Krejcia3045382018-11-22 14:30:31 +01002201/**
2202 * @brief map of the possible restrictions combination for the specific built-in type.
2203 */
Radek Krejci19a96102018-11-15 13:38:09 +01002204static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2205 0 /* LY_TYPE_UNKNOWN */,
2206 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002207 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2208 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2209 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2210 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2211 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002212 LYS_SET_BIT /* LY_TYPE_BITS */,
2213 0 /* LY_TYPE_BOOL */,
2214 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2215 0 /* LY_TYPE_EMPTY */,
2216 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2217 LYS_SET_BASE /* LY_TYPE_IDENT */,
2218 LYS_SET_REQINST /* LY_TYPE_INST */,
2219 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002220 LYS_SET_TYPE /* LY_TYPE_UNION */,
2221 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002222 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002223 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002224 LYS_SET_RANGE /* LY_TYPE_INT64 */
2225};
2226
2227/**
2228 * @brief stringification of the YANG built-in data types
2229 */
Michal Vasko22df3f02020-08-24 13:29:22 +02002230const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
Radek Krejci5969f272018-11-23 10:03:58 +01002231 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
Radek Krejci0f969882020-08-21 16:56:47 +02002232 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"};
Radek Krejci19a96102018-11-15 13:38:09 +01002233
Radek Krejcia3045382018-11-22 14:30:31 +01002234/**
2235 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2236 * @param[in] ctx Compile context.
2237 * @param[in] enums_p Array of the parsed enum structures to compile.
2238 * @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.
Radek Krejcia3045382018-11-22 14:30:31 +01002239 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2240 * @param[out] enums Newly created array of the compiled enums information for the current type.
2241 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2242 */
Radek Krejci19a96102018-11-15 13:38:09 +01002243static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002244lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci0f969882020-08-21 16:56:47 +02002245 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002246{
2247 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002248 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002249 int32_t value = 0;
2250 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002251 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002252
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002253 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2255 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2256 return LY_EVALID;
2257 }
2258
2259 LY_ARRAY_FOR(enums_p, u) {
2260 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002261 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
2262 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done);
2263 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Radek Krejci693262f2019-04-29 15:23:20 +02002264 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002265 if (base_enums) {
2266 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2267 LY_ARRAY_FOR(base_enums, v) {
2268 if (!strcmp(e->name, base_enums[v].name)) {
2269 break;
2270 }
2271 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002272 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002273 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2274 "Invalid %s - derived type adds new item \"%s\".",
2275 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2276 return LY_EVALID;
2277 }
2278 match = v;
2279 }
2280
2281 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002282 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002283 if (enums_p[u].flags & LYS_SET_VALUE) {
2284 e->value = (int32_t)enums_p[u].value;
2285 if (!u || e->value >= value) {
2286 value = e->value + 1;
2287 }
2288 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002289 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002290 if (e->value == (*enums)[v].value) {
2291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2292 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2293 e->value, e->name, (*enums)[v].name);
2294 return LY_EVALID;
2295 }
2296 }
2297 } else if (base_enums) {
2298 /* inherit the assigned value */
2299 e->value = base_enums[match].value;
2300 if (!u || e->value >= value) {
2301 value = e->value + 1;
2302 }
2303 } else {
2304 /* assign value automatically */
2305 if (u && value == INT32_MIN) {
2306 /* counter overflow */
2307 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2308 "Invalid enumeration - it is not possible to auto-assign enum value for "
2309 "\"%s\" since the highest value is already 2147483647.", e->name);
2310 return LY_EVALID;
2311 }
2312 e->value = value++;
2313 }
2314 } else { /* LY_TYPE_BITS */
2315 if (enums_p[u].flags & LYS_SET_VALUE) {
2316 e->value = (int32_t)enums_p[u].value;
2317 if (!u || (uint32_t)e->value >= position) {
2318 position = (uint32_t)e->value + 1;
2319 }
2320 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002321 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002322 if (e->value == (*enums)[v].value) {
2323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2324 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek Krejci0f969882020-08-21 16:56:47 +02002325 (uint32_t)e->value, e->name, (*enums)[v].name);
Radek Krejci19a96102018-11-15 13:38:09 +01002326 return LY_EVALID;
2327 }
2328 }
2329 } else if (base_enums) {
2330 /* inherit the assigned value */
2331 e->value = base_enums[match].value;
2332 if (!u || (uint32_t)e->value >= position) {
2333 position = (uint32_t)e->value + 1;
2334 }
2335 } else {
2336 /* assign value automatically */
2337 if (u && position == 0) {
2338 /* counter overflow */
2339 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2340 "Invalid bits - it is not possible to auto-assign bit position for "
2341 "\"%s\" since the highest value is already 4294967295.", e->name);
2342 return LY_EVALID;
2343 }
2344 e->value = position++;
2345 }
2346 }
2347
2348 if (base_enums) {
2349 /* the assigned values must not change from the derived type */
2350 if (e->value != base_enums[match].value) {
2351 if (basetype == LY_TYPE_ENUM) {
2352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2353 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2354 e->name, base_enums[match].value, e->value);
2355 } else {
2356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2357 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2358 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2359 }
2360 return LY_EVALID;
2361 }
2362 }
2363
Radek Krejciec4da802019-05-02 13:02:41 +02002364 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002365 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002366
2367 if (basetype == LY_TYPE_BITS) {
2368 /* keep bits ordered by position */
Radek Krejci1e008d22020-08-17 11:37:37 +02002369 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
Radek Krejci19a96102018-11-15 13:38:09 +01002370 if (v != u) {
2371 memcpy(&storage, e, sizeof *e);
2372 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2373 memcpy(&(*enums)[v], &storage, sizeof storage);
2374 }
2375 }
2376 }
2377
2378done:
2379 return ret;
2380}
2381
Radek Krejcia3045382018-11-22 14:30:31 +01002382/**
2383 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2384 *
2385 * path-arg = absolute-path / relative-path
2386 * absolute-path = 1*("/" (node-identifier *path-predicate))
2387 * relative-path = 1*(".." "/") descendant-path
2388 *
2389 * @param[in,out] path Path to parse.
2390 * @param[out] prefix Prefix of the token, NULL if there is not any.
2391 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2392 * @param[out] name Name of the token.
2393 * @param[out] nam_len Length of the name.
2394 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2395 * must not be changed between consecutive calls. -1 if the
2396 * path is absolute.
2397 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2398 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2399 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002400LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002401lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
Radek Krejci857189e2020-09-01 13:26:36 +02002402 int32_t *parent_times, ly_bool *has_predicate)
Radek Krejcia3045382018-11-22 14:30:31 +01002403{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002404 int32_t par_times = 0;
Radek Krejcia3045382018-11-22 14:30:31 +01002405
2406 assert(path && *path);
2407 assert(parent_times);
2408 assert(prefix);
2409 assert(prefix_len);
2410 assert(name);
2411 assert(name_len);
2412 assert(has_predicate);
2413
2414 *prefix = NULL;
2415 *prefix_len = 0;
2416 *name = NULL;
2417 *name_len = 0;
2418 *has_predicate = 0;
2419
2420 if (!*parent_times) {
2421 if (!strncmp(*path, "..", 2)) {
2422 *path += 2;
2423 ++par_times;
2424 while (!strncmp(*path, "/..", 3)) {
2425 *path += 3;
2426 ++par_times;
2427 }
2428 }
2429 if (par_times) {
2430 *parent_times = par_times;
2431 } else {
2432 *parent_times = -1;
2433 }
2434 }
2435
2436 if (**path != '/') {
2437 return LY_EINVAL;
2438 }
2439 /* skip '/' */
2440 ++(*path);
2441
2442 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002443 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002444
2445 if ((**path == '/' && (*path)[1]) || !**path) {
2446 /* path continues by another token or this is the last token */
2447 return LY_SUCCESS;
2448 } else if ((*path)[0] != '[') {
2449 /* unexpected character */
2450 return LY_EINVAL;
2451 } else {
2452 /* predicate starting with [ */
2453 *has_predicate = 1;
2454 return LY_SUCCESS;
2455 }
2456}
2457
2458/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002459 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2460 *
2461 * The set of features used for target must be a subset of features used for the leafref.
2462 * This is not a perfect, we should compare the truth tables but it could require too much resources
2463 * and RFC 7950 does not require it explicitely, so we simplify that.
2464 *
2465 * @param[in] refnode The leafref node.
2466 * @param[in] target Tha target node of the leafref.
2467 * @return LY_SUCCESS or LY_EVALID;
2468 */
2469static LY_ERR
2470lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2471{
2472 LY_ERR ret = LY_EVALID;
2473 const struct lysc_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002474 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci58d171e2018-11-23 13:50:55 +01002475 struct ly_set features = {0};
2476
2477 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002478 if (iter->iffeatures) {
2479 LY_ARRAY_FOR(iter->iffeatures, u) {
2480 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002481 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002482 }
2483 }
2484 }
2485 }
2486
2487 /* we should have, in features set, a superset of features applicable to the target node.
Radek Krejciba03a5a2020-08-27 14:40:41 +02002488 * If the feature is not present, we don;t have a subset of features applicable
Radek Krejci58d171e2018-11-23 13:50:55 +01002489 * to the leafref itself. */
Radek Krejci58d171e2018-11-23 13:50:55 +01002490 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002491 if (iter->iffeatures) {
2492 LY_ARRAY_FOR(iter->iffeatures, u) {
2493 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002494 if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
2495 /* feature not present */
Radek Krejci58d171e2018-11-23 13:50:55 +01002496 goto cleanup;
2497 }
2498 }
2499 }
2500 }
2501 }
2502 ret = LY_SUCCESS;
2503
2504cleanup:
2505 ly_set_erase(&features, NULL);
2506 return ret;
2507}
2508
Michal Vasko004d3152020-06-11 19:59:22 +02002509static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002510 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2511 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod);
Radek Krejcia3045382018-11-22 14:30:31 +01002512
Radek Krejcia3045382018-11-22 14:30:31 +01002513/**
2514 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2515 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002516 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2517 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2518 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2519 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002520 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002521 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002522 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002523 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2524 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2525 * @param[out] type Newly created type structure with the filled information about the type.
2526 * @return LY_ERR value.
2527 */
Radek Krejci19a96102018-11-15 13:38:09 +01002528static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002529lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002530 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2531 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2532 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002533{
2534 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002535 struct lysc_type_bin *bin;
2536 struct lysc_type_num *num;
2537 struct lysc_type_str *str;
2538 struct lysc_type_bits *bits;
2539 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002540 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002541 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002542 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002543 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002544
2545 switch (basetype) {
2546 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +02002547 bin = (struct lysc_type_bin *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002548
2549 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002550 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002551 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002552 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002553 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002554 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002555 }
2556 }
2557
2558 if (tpdfname) {
2559 type_p->compiled = *type;
2560 *type = calloc(1, sizeof(struct lysc_type_bin));
2561 }
2562 break;
2563 case LY_TYPE_BITS:
2564 /* RFC 7950 9.7 - bits */
Michal Vasko22df3f02020-08-24 13:29:22 +02002565 bits = (struct lysc_type_bits *)(*type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002566 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002567 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002568 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
2569 (struct lysc_type_bitenum_item **)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002570 }
2571
Radek Krejci555cb5b2018-11-16 14:54:33 +01002572 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002573 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002574 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002575 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002576 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002578 free(*type);
2579 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002580 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002581 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002582 }
2583
2584 if (tpdfname) {
2585 type_p->compiled = *type;
2586 *type = calloc(1, sizeof(struct lysc_type_bits));
2587 }
2588 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002589 case LY_TYPE_DEC64:
Radek Krejci115a74d2020-08-14 22:18:12 +02002590 dec = (struct lysc_type_dec *)(*type);
Radek Krejci6cba4292018-11-15 17:33:29 +01002591
2592 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002593 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002594 if (!type_p->fraction_digits) {
2595 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002597 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002599 free(*type);
2600 *type = NULL;
2601 }
2602 return LY_EVALID;
2603 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002604 dec->fraction_digits = type_p->fraction_digits;
2605 } else {
2606 if (type_p->fraction_digits) {
2607 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
2608 if (tpdfname) {
2609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2610 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
2611 tpdfname);
2612 } else {
2613 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2614 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
2615 free(*type);
2616 *type = NULL;
2617 }
2618 return LY_EVALID;
Radek Krejci6cba4292018-11-15 17:33:29 +01002619 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002620 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
Radek Krejci6cba4292018-11-15 17:33:29 +01002621 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002622
2623 /* RFC 7950 9.2.4 - range */
2624 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002625 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
Radek Krejci115a74d2020-08-14 22:18:12 +02002626 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002627 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002628 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002629 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002630 }
2631
2632 if (tpdfname) {
2633 type_p->compiled = *type;
2634 *type = calloc(1, sizeof(struct lysc_type_dec));
2635 }
2636 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002637 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002638 str = (struct lysc_type_str *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002639
2640 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002641 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002642 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002643 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002644 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002645 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002646 }
Michal Vasko22df3f02020-08-24 13:29:22 +02002647 } else if (base && ((struct lysc_type_str *)base)->length) {
2648 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002649 }
2650
2651 /* RFC 7950 9.4.5 - pattern */
2652 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002653 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Michal Vasko22df3f02020-08-24 13:29:22 +02002654 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
2655 } else if (base && ((struct lysc_type_str *)base)->patterns) {
2656 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002657 }
2658
2659 if (tpdfname) {
2660 type_p->compiled = *type;
2661 *type = calloc(1, sizeof(struct lysc_type_str));
2662 }
2663 break;
2664 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +02002665 enumeration = (struct lysc_type_enum *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002666
2667 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002668 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002669 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002670 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002671 }
2672
Radek Krejci555cb5b2018-11-16 14:54:33 +01002673 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002674 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002675 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002677 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002679 free(*type);
2680 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002681 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002682 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002683 }
2684
2685 if (tpdfname) {
2686 type_p->compiled = *type;
2687 *type = calloc(1, sizeof(struct lysc_type_enum));
2688 }
2689 break;
2690 case LY_TYPE_INT8:
2691 case LY_TYPE_UINT8:
2692 case LY_TYPE_INT16:
2693 case LY_TYPE_UINT16:
2694 case LY_TYPE_INT32:
2695 case LY_TYPE_UINT32:
2696 case LY_TYPE_INT64:
2697 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +02002698 num = (struct lysc_type_num *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002699
2700 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002701 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002702 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002703 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002704 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002705 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002706 }
2707 }
2708
2709 if (tpdfname) {
2710 type_p->compiled = *type;
2711 *type = calloc(1, sizeof(struct lysc_type_num));
2712 }
2713 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002714 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002715 idref = (struct lysc_type_identityref *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002716
2717 /* RFC 7950 9.10.2 - base */
2718 if (type_p->bases) {
2719 if (base) {
2720 /* only the directly derived identityrefs can contain base specification */
2721 if (tpdfname) {
2722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002723 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002724 tpdfname);
2725 } else {
2726 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002727 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002728 free(*type);
2729 *type = NULL;
2730 }
2731 return LY_EVALID;
2732 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002733 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002734 }
2735
2736 if (!base && !type_p->flags) {
2737 /* type derived from identityref built-in type must contain at least one base */
2738 if (tpdfname) {
2739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2740 } else {
2741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2742 free(*type);
2743 *type = NULL;
2744 }
2745 return LY_EVALID;
2746 }
2747
2748 if (tpdfname) {
2749 type_p->compiled = *type;
2750 *type = calloc(1, sizeof(struct lysc_type_identityref));
2751 }
2752 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002753 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002754 lref = (struct lysc_type_leafref *)*type;
Michal Vasko004d3152020-06-11 19:59:22 +02002755
Radek Krejcia3045382018-11-22 14:30:31 +01002756 /* RFC 7950 9.9.3 - require-instance */
2757 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002758 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002759 if (tpdfname) {
2760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2761 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2762 } else {
2763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2764 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2765 free(*type);
2766 *type = NULL;
2767 }
2768 return LY_EVALID;
2769 }
Michal Vasko004d3152020-06-11 19:59:22 +02002770 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002771 } else if (base) {
2772 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002773 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002774 } else {
2775 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002776 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002777 }
2778 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002779 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2780 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002781 } else if (base) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002782 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path);
2783 lref->path_context = ((struct lysc_type_leafref *)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002784 } else if (tpdfname) {
2785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2786 return LY_EVALID;
2787 } else {
2788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2789 free(*type);
2790 *type = NULL;
2791 return LY_EVALID;
2792 }
2793 if (tpdfname) {
2794 type_p->compiled = *type;
2795 *type = calloc(1, sizeof(struct lysc_type_leafref));
2796 }
2797 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002798 case LY_TYPE_INST:
2799 /* RFC 7950 9.9.3 - require-instance */
2800 if (type_p->flags & LYS_SET_REQINST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002801 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
Radek Krejci16c0f822018-11-16 10:46:10 +01002802 } else {
2803 /* default is true */
Michal Vasko22df3f02020-08-24 13:29:22 +02002804 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
Radek Krejci16c0f822018-11-16 10:46:10 +01002805 }
2806
2807 if (tpdfname) {
2808 type_p->compiled = *type;
2809 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2810 }
2811 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002812 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +02002813 un = (struct lysc_type_union *)(*type);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002814
2815 /* RFC 7950 7.4 - type */
2816 if (type_p->types) {
2817 if (base) {
2818 /* only the directly derived union can contain types specification */
2819 if (tpdfname) {
2820 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2821 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2822 tpdfname);
2823 } else {
2824 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2825 "Invalid type substatement for the type not directly derived from union built-in type.");
2826 free(*type);
2827 *type = NULL;
2828 }
2829 return LY_EVALID;
2830 }
2831 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002832 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2833 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002834 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002835 &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002836 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2837 /* add space for additional types from the union subtype */
2838 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vasko22df3f02020-08-24 13:29:22 +02002839 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
2840 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002841
2842 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002843 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002844 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2845 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2846 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
Michal Vasko22df3f02020-08-24 13:29:22 +02002847 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx); lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +02002848 lref = (struct lysc_type_leafref *)un->types[u + additional];
2849
2850 lref->basetype = LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02002851 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path);
Michal Vasko004d3152020-06-11 19:59:22 +02002852 lref->refcount = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +02002853 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
2854 lref->path_context = ((struct lysc_type_leafref *)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002855 /* TODO extensions */
2856
2857 } else {
2858 un->types[u + additional] = un_aux->types[v];
2859 ++un_aux->types[v]->refcount;
2860 }
2861 ++additional;
2862 LY_ARRAY_INCREMENT(un->types);
2863 }
2864 /* compensate u increment in main loop */
2865 --additional;
2866
2867 /* free the replaced union subtype */
Michal Vasko22df3f02020-08-24 13:29:22 +02002868 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002869 } else {
2870 LY_ARRAY_INCREMENT(un->types);
2871 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002872 }
2873 }
2874
2875 if (!base && !type_p->flags) {
2876 /* type derived from union built-in type must contain at least one type */
2877 if (tpdfname) {
2878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2879 } else {
2880 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2881 free(*type);
2882 *type = NULL;
2883 }
2884 return LY_EVALID;
2885 }
2886
2887 if (tpdfname) {
2888 type_p->compiled = *type;
2889 *type = calloc(1, sizeof(struct lysc_type_union));
2890 }
2891 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002892 case LY_TYPE_BOOL:
2893 case LY_TYPE_EMPTY:
2894 case LY_TYPE_UNKNOWN: /* just to complete switch */
2895 break;
2896 }
2897 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2898done:
2899 return ret;
2900}
2901
Radek Krejcia3045382018-11-22 14:30:31 +01002902/**
2903 * @brief Compile information about the leaf/leaf-list's type.
2904 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002905 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2906 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2907 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2908 * @param[in] context_name Name of the context node or referencing typedef for logging.
2909 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002910 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002911 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002912 * @param[out] dflt Default value for the type.
2913 * @param[out] dflt_mod Local module for the default value.
Radek Krejcia3045382018-11-22 14:30:31 +01002914 * @return LY_ERR value.
2915 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002916static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002917lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002918 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2919 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod)
Radek Krejci19a96102018-11-15 13:38:09 +01002920{
2921 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02002922 ly_bool dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002923 struct type_context {
2924 const struct lysp_tpdf *tpdf;
2925 struct lysp_node *node;
2926 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002927 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002928 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002929 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002930 struct ly_set tpdf_chain = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002931
2932 assert((dflt && dflt_mod) || (!dflt && !dflt_mod));
Radek Krejci19a96102018-11-15 13:38:09 +01002933
2934 (*type) = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002935 if (dflt) {
2936 *dflt = NULL;
2937 *dflt_mod = NULL;
2938 }
Radek Krejci19a96102018-11-15 13:38:09 +01002939
2940 tctx = calloc(1, sizeof *tctx);
2941 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002942 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002943 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2944 ret == LY_SUCCESS;
2945 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2946 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2947 if (basetype) {
2948 break;
2949 }
2950
2951 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002952 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2953 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci87e25252020-09-15 13:28:31 +02002954 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01002955
Radek Krejcicdfecd92018-11-26 11:27:32 +01002956 if (units && !*units) {
2957 /* inherit units */
Radek Krejci87e25252020-09-15 13:28:31 +02002958 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2959 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002960 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002961 if (dflt && !*dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002962 /* inherit default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002963 *dflt = tctx->tpdf->dflt;
2964 *dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002965 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002966 if (dummyloops && (!units || *units) && dflt && *dflt) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002967 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002968 break;
2969 }
2970
Radek Krejci19a96102018-11-15 13:38:09 +01002971 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002972 /* it is not necessary to continue, the rest of the chain was already compiled,
2973 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002974 basetype = tctx->tpdf->type.compiled->basetype;
Radek Krejciba03a5a2020-08-27 14:40:41 +02002975 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02002976 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejciba03a5a2020-08-27 14:40:41 +02002977
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002978 if ((units && !*units) || (dflt && !*dflt)) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002979 dummyloops = 1;
2980 goto preparenext;
2981 } else {
2982 tctx = NULL;
2983 break;
2984 }
Radek Krejci19a96102018-11-15 13:38:09 +01002985 }
2986
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002987 /* circular typedef reference detection */
Radek Krejci1deb5be2020-08-26 16:43:36 +02002988 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002989 /* local part */
Michal Vasko22df3f02020-08-24 13:29:22 +02002990 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002991 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2992 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2993 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2994 free(tctx);
2995 ret = LY_EVALID;
2996 goto cleanup;
2997 }
2998 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02002999 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003000 /* global part for unions corner case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003001 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003002 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3003 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3004 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3005 free(tctx);
3006 ret = LY_EVALID;
3007 goto cleanup;
3008 }
3009 }
3010
Radek Krejci19a96102018-11-15 13:38:09 +01003011 /* store information for the following processing */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003012 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02003013 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003014
Radek Krejcicdfecd92018-11-26 11:27:32 +01003015preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003016 /* prepare next loop */
3017 tctx_prev = tctx;
3018 tctx = calloc(1, sizeof *tctx);
3019 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3020 }
3021 free(tctx);
3022
3023 /* allocate type according to the basetype */
3024 switch (basetype) {
3025 case LY_TYPE_BINARY:
3026 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003027 break;
3028 case LY_TYPE_BITS:
3029 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003030 break;
3031 case LY_TYPE_BOOL:
3032 case LY_TYPE_EMPTY:
3033 *type = calloc(1, sizeof(struct lysc_type));
3034 break;
3035 case LY_TYPE_DEC64:
3036 *type = calloc(1, sizeof(struct lysc_type_dec));
3037 break;
3038 case LY_TYPE_ENUM:
3039 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003040 break;
3041 case LY_TYPE_IDENT:
3042 *type = calloc(1, sizeof(struct lysc_type_identityref));
3043 break;
3044 case LY_TYPE_INST:
3045 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3046 break;
3047 case LY_TYPE_LEAFREF:
3048 *type = calloc(1, sizeof(struct lysc_type_leafref));
3049 break;
3050 case LY_TYPE_STRING:
3051 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003052 break;
3053 case LY_TYPE_UNION:
3054 *type = calloc(1, sizeof(struct lysc_type_union));
3055 break;
3056 case LY_TYPE_INT8:
3057 case LY_TYPE_UINT8:
3058 case LY_TYPE_INT16:
3059 case LY_TYPE_UINT16:
3060 case LY_TYPE_INT32:
3061 case LY_TYPE_UINT32:
3062 case LY_TYPE_INT64:
3063 case LY_TYPE_UINT64:
3064 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003065 break;
3066 case LY_TYPE_UNKNOWN:
3067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3068 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3069 ret = LY_EVALID;
3070 goto cleanup;
3071 }
3072 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003073 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003074 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3075 ly_data_type2str[basetype]);
3076 free(*type);
3077 (*type) = NULL;
3078 ret = LY_EVALID;
3079 goto cleanup;
3080 }
3081
3082 /* get restrictions from the referred typedefs */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003083 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003084 tctx = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003085
3086 /* remember the typedef context for circular check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003087 ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
3088 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003089
Radek Krejci43699232018-11-23 14:59:46 +01003090 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003091 base = tctx->tpdf->type.compiled;
3092 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003093 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003094 /* no change, just use the type information from the base */
Michal Vasko22df3f02020-08-24 13:29:22 +02003095 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
Radek Krejci19a96102018-11-15 13:38:09 +01003096 ++base->refcount;
3097 continue;
3098 }
3099
3100 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003101 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3102 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3103 tctx->tpdf->name, ly_data_type2str[basetype]);
3104 ret = LY_EVALID;
3105 goto cleanup;
3106 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3108 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3109 tctx->tpdf->name, tctx->tpdf->dflt);
3110 ret = LY_EVALID;
3111 goto cleanup;
3112 }
3113
Radek Krejci19a96102018-11-15 13:38:09 +01003114 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003115 /* TODO user type plugins */
3116 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003117 prev_type = *type;
Michal Vasko22df3f02020-08-24 13:29:22 +02003118 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf *)tctx->tpdf)->type,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003119 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003120 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003121 LY_CHECK_GOTO(ret, cleanup);
3122 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003123 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003124 /* remove the processed typedef contexts from the stack for circular check */
3125 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003126
Radek Krejcic5c27e52018-11-15 14:38:11 +01003127 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003128 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003129 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003130 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003131 /* TODO user type plugins */
3132 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003133 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003134 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003135 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003136 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003137 /* no specific restriction in leaf's type definition, copy from the base */
3138 free(*type);
3139 (*type) = base;
3140 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003141 }
3142
Radek Krejci0935f412019-08-20 16:15:18 +02003143 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003144
3145cleanup:
3146 ly_set_erase(&tpdf_chain, free);
3147 return ret;
3148}
3149
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003150/**
3151 * @brief Compile status information of the given node.
3152 *
3153 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3154 * has the status correctly set during the compilation.
3155 *
3156 * @param[in] ctx Compile context
3157 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3158 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3159 * the compatibility with the parent's status value.
3160 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3161 * @return LY_ERR value.
3162 */
3163static LY_ERR
3164lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3165{
3166 /* status - it is not inherited by specification, but it does not make sense to have
3167 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3168 if (!((*node_flags) & LYS_STATUS_MASK)) {
3169 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3170 if ((parent_flags & 0x3) != 0x3) {
3171 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3172 * combination of bits (0x3) which marks the uses_status value */
3173 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
Radek Krejci0f969882020-08-21 16:56:47 +02003174 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003175 }
3176 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3177 } else {
3178 (*node_flags) |= LYS_STATUS_CURR;
3179 }
3180 } else if (parent_flags & LYS_STATUS_MASK) {
3181 /* check status compatibility with the parent */
3182 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3183 if ((*node_flags) & LYS_STATUS_CURR) {
3184 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3185 "A \"current\" status is in conflict with the parent's \"%s\" status.",
Radek Krejci0f969882020-08-21 16:56:47 +02003186 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003187 } else { /* LYS_STATUS_DEPRC */
3188 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3189 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3190 }
3191 return LY_EVALID;
3192 }
3193 }
3194 return LY_SUCCESS;
3195}
3196
Radek Krejci8cce8532019-03-05 11:27:45 +01003197/**
3198 * @brief Check uniqness of the node/action/notification name.
3199 *
3200 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3201 * structures, but they share the namespace so we need to check their name collisions.
3202 *
3203 * @param[in] ctx Compile context.
Michal Vasko20424b42020-08-31 12:29:38 +02003204 * @param[in] parent Parent of the nodes to check, can be NULL.
Radek Krejci8cce8532019-03-05 11:27:45 +01003205 * @param[in] name Name of the item to find in the given lists.
Michal Vasko20424b42020-08-31 12:29:38 +02003206 * @param[in] exclude Node that was just added that should be excluded from the name checking.
Radek Krejci8cce8532019-03-05 11:27:45 +01003207 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3208 */
3209static LY_ERR
Michal Vasko20424b42020-08-31 12:29:38 +02003210lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
3211 const struct lysc_node *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003212{
Michal Vasko20424b42020-08-31 12:29:38 +02003213 const struct lysc_node *iter, *iter2;
3214 const struct lysc_action *actions;
3215 const struct lysc_notif *notifs;
3216 uint32_t getnext_flags;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003217 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003218
Michal Vasko20424b42020-08-31 12:29:38 +02003219#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
3220
3221 if (exclude->nodetype == LYS_CASE) {
3222 /* check restricted only to all the cases */
3223 assert(parent->nodetype == LYS_CHOICE);
3224 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
3225 if (CHECK_NODE(iter, exclude, name)) {
3226 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
3227 return LY_EEXIST;
3228 }
3229 }
3230
3231 return LY_SUCCESS;
3232 }
3233
3234 /* no reason for our parent to be choice anymore */
3235 assert(!parent || (parent->nodetype != LYS_CHOICE));
3236
3237 if (parent && (parent->nodetype == LYS_CASE)) {
3238 /* move to the first data definition parent */
3239 parent = lysc_data_parent(parent);
3240 }
3241
3242 getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE;
3243 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
3244 getnext_flags |= LYS_GETNEXT_OUTPUT;
3245 }
3246
3247 iter = NULL;
3248 while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) {
3249 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003250 goto error;
3251 }
Michal Vasko20424b42020-08-31 12:29:38 +02003252
3253 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
3254 if (iter->nodetype == LYS_CHOICE) {
3255 iter2 = NULL;
3256 while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) {
3257 if (CHECK_NODE(iter2, exclude, name)) {
3258 goto error;
3259 }
3260 }
3261 }
Radek Krejci8cce8532019-03-05 11:27:45 +01003262 }
Michal Vasko20424b42020-08-31 12:29:38 +02003263
3264 actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003265 LY_ARRAY_FOR(actions, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003266 if (CHECK_NODE(&actions[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003267 goto error;
3268 }
3269 }
Michal Vasko20424b42020-08-31 12:29:38 +02003270
3271 notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003272 LY_ARRAY_FOR(notifs, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003273 if (CHECK_NODE(&notifs[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003274 goto error;
3275 }
3276 }
3277 return LY_SUCCESS;
Michal Vasko20424b42020-08-31 12:29:38 +02003278
Radek Krejci8cce8532019-03-05 11:27:45 +01003279error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003281 return LY_EEXIST;
Michal Vasko20424b42020-08-31 12:29:38 +02003282
3283#undef CHECK_NODE
Radek Krejci8cce8532019-03-05 11:27:45 +01003284}
3285
Radek Krejciec4da802019-05-02 13:02:41 +02003286static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status);
Radek Krejci19a96102018-11-15 13:38:09 +01003287
Radek Krejcia3045382018-11-22 14:30:31 +01003288/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003289 * @brief Compile parsed RPC/action schema node information.
3290 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003291 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003292 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003293 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3294 * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags).
3295 * Zero means no uses, non-zero value with no status bit set mean the default status.
3296 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3297 */
3298static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003299lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003300 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003301{
3302 LY_ERR ret = LY_SUCCESS;
3303 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003304 LY_ARRAY_COUNT_TYPE u;
3305 uint32_t opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003306
Radek Krejci327de162019-06-14 12:52:07 +02003307 lysc_update_path(ctx, parent, action_p->name);
3308
Michal Vasko20424b42020-08-31 12:29:38 +02003309 /* member needed for uniqueness check lys_getnext() */
3310 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
3311 action->module = ctx->mod;
3312 action->parent = parent;
3313
3314 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action));
Radek Krejci8cce8532019-03-05 11:27:45 +01003315
Radek Krejciec4da802019-05-02 13:02:41 +02003316 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003318 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003319 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003320 return LY_EVALID;
3321 }
3322
Radek Krejciec4da802019-05-02 13:02:41 +02003323 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003324 action->sp = action_p;
3325 }
3326 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3327
3328 /* status - it is not inherited by specification, but it does not make sense to have
3329 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003330 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003331
Radek Krejci011e4aa2020-09-04 15:22:31 +02003332 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
3333 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
3334 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003335 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003336 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003337
3338 /* input */
Michal Vasko22df3f02020-08-24 13:29:22 +02003339 lysc_update_path(ctx, (struct lysc_node *)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003340 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003341 COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003342 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003343 LY_LIST_FOR(action_p->input.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003344 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003345 }
Radek Krejci327de162019-06-14 12:52:07 +02003346 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003347 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003348
3349 /* output */
Michal Vasko22df3f02020-08-24 13:29:22 +02003350 lysc_update_path(ctx, (struct lysc_node *)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003351 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003352 COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003353 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003354 LY_LIST_FOR(action_p->output.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003355 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003356 }
Radek Krejci327de162019-06-14 12:52:07 +02003357 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003358 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003359
3360 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3361 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003362 ret = ly_set_add(&ctx->xpath, action, 0, NULL);
3363 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003364 }
3365
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003366cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003367 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003368 return ret;
3369}
3370
3371/**
Radek Krejci43981a32019-04-12 09:44:11 +02003372 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003373 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003374 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003375 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3376 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3377 * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags).
Radek Krejcifc11bd72019-04-11 16:00:05 +02003378 * Zero means no uses, non-zero value with no status bit set mean the default status.
3379 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3380 */
3381static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003382lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003383 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
Radek Krejcifc11bd72019-04-11 16:00:05 +02003384{
3385 LY_ERR ret = LY_SUCCESS;
3386 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003387 LY_ARRAY_COUNT_TYPE u;
3388 uint32_t opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003389
Radek Krejci327de162019-06-14 12:52:07 +02003390 lysc_update_path(ctx, parent, notif_p->name);
3391
Michal Vasko20424b42020-08-31 12:29:38 +02003392 /* member needed for uniqueness check lys_getnext() */
3393 notif->nodetype = LYS_NOTIF;
3394 notif->module = ctx->mod;
3395 notif->parent = parent;
3396
3397 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003398
Radek Krejciec4da802019-05-02 13:02:41 +02003399 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003400 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3401 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003402 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003403 return LY_EVALID;
3404 }
3405
Radek Krejciec4da802019-05-02 13:02:41 +02003406 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003407 notif->sp = notif_p;
3408 }
3409 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3410
3411 /* status - it is not inherited by specification, but it does not make sense to have
3412 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003413 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
3414 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003415
Radek Krejci011e4aa2020-09-04 15:22:31 +02003416 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
3417 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
3418 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003419 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003420 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003421 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3422 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003423 ret = ly_set_add(&ctx->xpath, notif, 0, NULL);
3424 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003425 }
Radek Krejci0935f412019-08-20 16:15:18 +02003426 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003427
Radek Krejciec4da802019-05-02 13:02:41 +02003428 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003429 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003430 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status);
3431 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003432 }
3433
Radek Krejci327de162019-06-14 12:52:07 +02003434 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003435cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003436 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003437 return ret;
3438}
3439
3440/**
Radek Krejcia3045382018-11-22 14:30:31 +01003441 * @brief Compile parsed container node information.
3442 * @param[in] ctx Compile context
3443 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003444 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3445 * is enriched with the container-specific information.
3446 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3447 */
Radek Krejci19a96102018-11-15 13:38:09 +01003448static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003449lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003450{
Michal Vasko22df3f02020-08-24 13:29:22 +02003451 struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p;
3452 struct lysc_node_container *cont = (struct lysc_node_container *)node;
Radek Krejci19a96102018-11-15 13:38:09 +01003453 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003454 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003455 LY_ERR ret = LY_SUCCESS;
3456
Radek Krejcife909632019-02-12 15:34:42 +01003457 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003458 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003459 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003460 } else if (cont_p->musts) {
3461 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003462 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3463 cont->flags |= LYS_PRESENCE;
3464 } else if (cont_p->when) {
3465 /* container with a when condition */
3466 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"when\" condition.", cont_p->name);
Michal Vaskoba417ac2020-08-06 14:48:20 +02003467 cont->flags |= LYS_PRESENCE;
3468 } else if (cont_p->parent) {
3469 if (cont_p->parent->nodetype == LYS_CHOICE) {
3470 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003471 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
Michal Vaskoba417ac2020-08-06 14:48:20 +02003472 cont_p->name, cont_p->parent->name);
3473 cont->flags |= LYS_PRESENCE;
3474 } else if ((cont_p->parent->nodetype == LYS_CASE)
3475 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3476 /* container is the only node in a case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003477 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
Michal Vaskoba417ac2020-08-06 14:48:20 +02003478 cont_p->name, cont_p->parent->name);
3479 cont->flags |= LYS_PRESENCE;
3480 }
Radek Krejcife909632019-02-12 15:34:42 +01003481 }
3482
Michal Vaskoba417ac2020-08-06 14:48:20 +02003483 /* more cases when the container has meaning but is kept NP for convenience:
3484 * - when condition
3485 * - direct child action/notification
3486 */
3487
Radek Krejci19a96102018-11-15 13:38:09 +01003488 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003489 ret = lys_compile_node(ctx, child_p, node, 0);
3490 LY_CHECK_GOTO(ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003491 }
3492
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003493 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003494 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3495 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003496 ret = ly_set_add(&ctx->xpath, cont, 0, NULL);
3497 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003498 }
Radek Krejciec4da802019-05-02 13:02:41 +02003499 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3500 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003501
3502done:
3503 return ret;
3504}
3505
Radek Krejci33f72892019-02-21 10:36:58 +01003506/*
3507 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3508 * @param[in] ctx Compile context.
3509 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3510 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003511 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3512 * @return LY_ERR value.
3513 */
3514static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003515lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003516 struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003517{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003518 const char *dflt;
3519 struct lys_module *dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003520
Radek Krejciec4da802019-05-02 13:02:41 +02003521 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003522 leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod));
3523
3524 /* store default value, if any */
3525 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
3526 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, dflt, dflt_mod));
Radek Krejci33f72892019-02-21 10:36:58 +01003527 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003528
Radek Krejci33f72892019-02-21 10:36:58 +01003529 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3530 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003531 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003532 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003533 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003534 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
3535 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
Radek Krejci33f72892019-02-21 10:36:58 +01003536 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003537 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003538 }
3539 }
3540 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003541 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3543 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3544 return LY_EVALID;
3545 }
3546 }
3547
Radek Krejci33f72892019-02-21 10:36:58 +01003548 return LY_SUCCESS;
3549}
3550
Radek Krejcia3045382018-11-22 14:30:31 +01003551/**
3552 * @brief Compile parsed leaf node information.
3553 * @param[in] ctx Compile context
3554 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003555 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3556 * is enriched with the leaf-specific information.
3557 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3558 */
Radek Krejci19a96102018-11-15 13:38:09 +01003559static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003560lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003561{
Michal Vasko22df3f02020-08-24 13:29:22 +02003562 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p;
3563 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003564 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003565 LY_ERR ret = LY_SUCCESS;
3566
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003567 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003568 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3569 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003570 ret = ly_set_add(&ctx->xpath, leaf, 0, NULL);
3571 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003572 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003573 if (leaf_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003574 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003575 leaf->flags |= LYS_SET_UNITS;
3576 }
Radek Krejcia1911222019-07-22 17:24:50 +02003577
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003578 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003579 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3580 LY_CHECK_GOTO(ret, done);
Radek Krejcia1911222019-07-22 17:24:50 +02003581
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003582 /* store/update default value */
Radek Krejciccd20f12019-02-15 14:12:27 +01003583 if (leaf_p->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003584 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, leaf_p->dflt, ctx->mod_def));
Radek Krejci76b3e962018-12-14 17:01:25 +01003585 leaf->flags |= LYS_SET_DFLT;
3586 }
Radek Krejci43699232018-11-23 14:59:46 +01003587
Radek Krejci19a96102018-11-15 13:38:09 +01003588done:
3589 return ret;
3590}
3591
Radek Krejcia3045382018-11-22 14:30:31 +01003592/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003593 * @brief Compile parsed leaf-list node information.
3594 * @param[in] ctx Compile context
3595 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003596 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3597 * is enriched with the leaf-list-specific information.
3598 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3599 */
3600static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003601lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003602{
Michal Vasko22df3f02020-08-24 13:29:22 +02003603 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p;
3604 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003605 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003606 LY_ERR ret = LY_SUCCESS;
3607
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003608 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003609 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3610 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003611 ret = ly_set_add(&ctx->xpath, llist, 0, NULL);
3612 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003613 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003614 if (llist_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003615 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003616 llist->flags |= LYS_SET_UNITS;
3617 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003618
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003619 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003620 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist);
3621 LY_CHECK_GOTO(ret, done);
Michal Vasko6a044b22020-01-15 12:25:39 +01003622
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003623 /* store/update default values */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003624 if (llist_p->dflts) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003625 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, llist_p->dflts, ctx->mod_def), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003626 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003627 }
3628
3629 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003630 if (llist->min) {
3631 llist->flags |= LYS_MAND_TRUE;
3632 }
Radek Krejcib7408632018-11-28 17:12:11 +01003633 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003634
Radek Krejci0e5d8382018-11-28 16:37:53 +01003635done:
3636 return ret;
3637}
3638
3639/**
Radek Krejci7af64242019-02-18 13:07:53 +01003640 * @brief Compile information about list's uniques.
3641 * @param[in] ctx Compile context.
3642 * @param[in] context_module Module where the prefixes are going to be resolved.
3643 * @param[in] uniques Sized array list of unique statements.
3644 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3645 * @return LY_ERR value.
3646 */
3647static LY_ERR
3648lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3649{
3650 LY_ERR ret = LY_SUCCESS;
3651 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003652 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003653 const char *keystr, *delim;
3654 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003655 LY_ARRAY_COUNT_TYPE v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003656 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003657 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003658
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003659 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003660 config = -1;
3661 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3662 keystr = uniques[v];
3663 while (keystr) {
3664 delim = strpbrk(keystr, " \t\n");
3665 if (delim) {
3666 len = delim - keystr;
3667 while (isspace(*delim)) {
3668 ++delim;
3669 }
3670 } else {
3671 len = strlen(keystr);
3672 }
3673
3674 /* unique node must be present */
3675 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko22df3f02020-08-24 13:29:22 +02003676 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0,
3677 (const struct lysc_node **)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003678 if (ret != LY_SUCCESS) {
3679 if (ret == LY_EDENIED) {
3680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003681 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003682 len, keystr, lys_nodetype2str((*key)->nodetype));
3683 }
3684 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003685 } else if (flags) {
3686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3687 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003688 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003689 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003690 }
3691
3692 /* all referenced leafs must be of the same config type */
3693 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003695 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003696 return LY_EVALID;
3697 } else if ((*key)->flags & LYS_CONFIG_W) {
3698 config = 1;
3699 } else { /* LYS_CONFIG_R */
3700 config = 0;
3701 }
3702
Michal Vasko14654712020-02-06 08:35:21 +01003703 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3704 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3705 if (parent->nodetype == LYS_LIST) {
3706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3707 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3708 return LY_EVALID;
3709 }
3710 }
3711
Radek Krejci7af64242019-02-18 13:07:53 +01003712 /* check status */
3713 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0f969882020-08-21 16:56:47 +02003714 (*key)->flags, (*key)->module, (*key)->name));
Radek Krejci7af64242019-02-18 13:07:53 +01003715
3716 /* mark leaf as unique */
3717 (*key)->flags |= LYS_UNIQUE;
3718
3719 /* next unique value in line */
3720 keystr = delim;
3721 }
3722 /* next unique definition */
3723 }
3724
3725 return LY_SUCCESS;
3726}
3727
3728/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003729 * @brief Compile parsed list node information.
3730 * @param[in] ctx Compile context
3731 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003732 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3733 * is enriched with the list-specific information.
3734 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3735 */
3736static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003737lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003738{
Michal Vasko22df3f02020-08-24 13:29:22 +02003739 struct lysp_node_list *list_p = (struct lysp_node_list *)node_p;
3740 struct lysc_node_list *list = (struct lysc_node_list *)node;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003741 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003742 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003743 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003744 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003745 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003746 LY_ERR ret = LY_SUCCESS;
3747
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003748 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003749 if (list->min) {
3750 list->flags |= LYS_MAND_TRUE;
3751 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003752 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3753
3754 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003755 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003756 }
3757
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003758 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003759 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3760 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003761 LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL));
Michal Vasko5d8756a2019-11-07 15:21:00 +01003762 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003763
3764 /* keys */
3765 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3766 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3767 return LY_EVALID;
3768 }
3769
3770 /* find all the keys (must be direct children) */
3771 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003772 if (!keystr) {
3773 /* keyless list */
3774 list->flags |= LYS_KEYLESS;
3775 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003776 while (keystr) {
3777 delim = strpbrk(keystr, " \t\n");
3778 if (delim) {
3779 len = delim - keystr;
3780 while (isspace(*delim)) {
3781 ++delim;
3782 }
3783 } else {
3784 len = strlen(keystr);
3785 }
3786
3787 /* key node must be present */
Michal Vasko22df3f02020-08-24 13:29:22 +02003788 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003789 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003790 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3791 "The list's key \"%.*s\" not found.", len, keystr);
3792 return LY_EVALID;
3793 }
3794 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003795 if (key->flags & LYS_KEY) {
3796 /* the node was already marked as a key */
3797 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3798 "Duplicated key identifier \"%.*s\".", len, keystr);
3799 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003800 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003801
Michal Vasko22df3f02020-08-24 13:29:22 +02003802 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003803 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003804 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3806 return LY_EVALID;
3807 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003808 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003809 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003810 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003811 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003812 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003813 return LY_EVALID;
3814 }
3815 } else {
3816 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003817 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003818 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003819 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003820 return LY_EVALID;
3821 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003822 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003823 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003824 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003825 return LY_EVALID;
3826 }
3827 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003828
3829 /* check status */
3830 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003831 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003832
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003833 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003834 if (key->dflt) {
3835 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3836 lysc_type_free(ctx->ctx, key->dflt->realtype);
3837 free(key->dflt);
3838 key->dflt = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003839 }
3840 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003841 key->flags |= LYS_KEY;
3842
3843 /* move it to the correct position */
Michal Vasko22df3f02020-08-24 13:29:22 +02003844 if ((prev_key && (struct lysc_node *)prev_key != key->prev) || (!prev_key && key->prev->next)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02003845 /* fix links in closest previous siblings of the key */
3846 if (key->next) {
3847 key->next->prev = key->prev;
3848 } else {
3849 /* last child */
3850 list->child->prev = key->prev;
3851 }
3852 if (key->prev->next) {
3853 key->prev->next = key->next;
3854 }
3855 /* fix links in the key */
3856 if (prev_key) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003857 key->prev = (struct lysc_node *)prev_key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003858 key->next = prev_key->next;
3859 } else {
3860 key->prev = list->child->prev;
3861 key->next = list->child;
3862 }
3863 /* fix links in closes future siblings of the key */
3864 if (prev_key) {
3865 if (prev_key->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003866 prev_key->next->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003867 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003868 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003869 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003870 prev_key->next = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003871 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003872 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003873 }
3874 /* fix links in parent */
3875 if (!key->prev->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003876 list->child = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003877 }
3878 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003879
3880 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003881 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003882 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003883 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003884 }
3885
3886 /* uniques */
3887 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003888 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003889 }
3890
Radek Krejciec4da802019-05-02 13:02:41 +02003891 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3892 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003893
3894done:
3895 return ret;
3896}
3897
Radek Krejcib56c7502019-02-13 14:19:54 +01003898/**
3899 * @brief Do some checks and set the default choice's case.
3900 *
3901 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3902 *
3903 * @param[in] ctx Compile context.
3904 * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself,
3905 * not the reference to the imported module.
3906 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3907 * @return LY_ERR value.
3908 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003909static LY_ERR
3910lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3911{
Michal Vasko22df3f02020-08-24 13:29:22 +02003912 struct lysc_node *iter, *node = (struct lysc_node *)ch;
Radek Krejci76b3e962018-12-14 17:01:25 +01003913 const char *prefix = NULL, *name;
3914 size_t prefix_len = 0;
3915
3916 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3917 name = strchr(dflt, ':');
3918 if (name) {
3919 prefix = dflt;
3920 prefix_len = name - prefix;
3921 ++name;
3922 } else {
3923 name = dflt;
3924 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003925 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003926 /* prefixed default case make sense only for the prefix of the schema itself */
3927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3928 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3929 prefix_len, prefix);
3930 return LY_EVALID;
3931 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003932 ch->dflt = (struct lysc_node_case *)lys_find_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejci76b3e962018-12-14 17:01:25 +01003933 if (!ch->dflt) {
3934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3935 "Default case \"%s\" not found.", dflt);
3936 return LY_EVALID;
3937 }
3938 /* no mandatory nodes directly under the default case */
3939 LY_LIST_FOR(ch->dflt->child, iter) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003940 if (iter->parent != (struct lysc_node *)ch->dflt) {
Radek Krejcife13da42019-02-15 14:51:01 +01003941 break;
3942 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003943 if (iter->flags & LYS_MAND_TRUE) {
3944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3945 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3946 return LY_EVALID;
3947 }
3948 }
Radek Krejci01342af2019-01-03 15:18:08 +01003949 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003950 return LY_SUCCESS;
3951}
3952
Radek Krejciccd20f12019-02-15 14:12:27 +01003953static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003954lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003955{
3956 struct lys_module *mod;
3957 const char *prefix = NULL, *name;
3958 size_t prefix_len = 0;
3959 struct lysc_node_case *cs;
3960 struct lysc_node *node;
3961
3962 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3963 name = strchr(dflt, ':');
3964 if (name) {
3965 prefix = dflt;
3966 prefix_len = name - prefix;
3967 ++name;
3968 } else {
3969 name = dflt;
3970 }
3971 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3972 if (prefix) {
3973 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3974 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003975 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3976 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003977 return LY_EVALID;
3978 }
3979 } else {
3980 mod = ctx->mod;
3981 }
3982 /* get the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003983 cs = (struct lysc_node_case *)lys_find_child((struct lysc_node *)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejciccd20f12019-02-15 14:12:27 +01003984 if (!cs) {
3985 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003986 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003987 return LY_EVALID;
3988 }
3989
3990 /* check that there is no mandatory node */
3991 LY_LIST_FOR(cs->child, node) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003992 if (node->parent != (struct lysc_node *)cs) {
Radek Krejcife13da42019-02-15 14:51:01 +01003993 break;
3994 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003995 if (node->flags & LYS_MAND_TRUE) {
3996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003997 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3998 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003999 return LY_EVALID;
4000 }
4001 }
4002
4003 /* set the default case in choice */
4004 ch->dflt = cs;
4005 cs->flags |= LYS_SET_DFLT;
4006
4007 return LY_SUCCESS;
4008}
4009
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004010/**
Michal Vasko20424b42020-08-31 12:29:38 +02004011 * @brief Compile choice children.
4012 *
4013 * @param[in] ctx Compile context
4014 * @param[in] child_p Parsed choice children nodes.
4015 * @param[in] node Compiled choice node to compile and add children to.
4016 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4017 */
4018static LY_ERR
4019lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node)
4020{
4021 LY_ERR ret = LY_SUCCESS;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004022 struct lysp_node *child_p_next = child_p->next;
Michal Vasko20424b42020-08-31 12:29:38 +02004023 struct lysp_node_case *cs_p;
4024
4025 if (child_p->nodetype == LYS_CASE) {
4026 /* standard case under choice */
4027 ret = lys_compile_node(ctx, child_p, node, 0);
4028 } else {
4029 /* we need the implicit case first, so create a fake parsed case */
4030 cs_p = calloc(1, sizeof *cs_p);
4031 cs_p->nodetype = LYS_CASE;
Radek Krejci87e25252020-09-15 13:28:31 +02004032 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
Michal Vasko20424b42020-08-31 12:29:38 +02004033 cs_p->child = child_p;
4034
4035 /* make the child the only case child */
Michal Vasko20424b42020-08-31 12:29:38 +02004036 child_p->next = NULL;
4037
4038 /* compile it normally */
4039 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0);
4040
Radek Krejci87e25252020-09-15 13:28:31 +02004041free_fake_node:
Michal Vasko20424b42020-08-31 12:29:38 +02004042 /* free the fake parsed node and correct pointers back */
4043 cs_p->child = NULL;
4044 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
Radek Krejci8f5fad22020-09-15 16:50:54 +02004045 child_p->next = child_p_next;
Michal Vasko20424b42020-08-31 12:29:38 +02004046 }
4047
4048 return ret;
4049}
4050
4051/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004052 * @brief Compile parsed choice node information.
Michal Vasko20424b42020-08-31 12:29:38 +02004053 *
Radek Krejci056d0a82018-12-06 16:57:25 +01004054 * @param[in] ctx Compile context
4055 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004056 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004057 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004058 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4059 */
4060static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004061lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004062{
Michal Vasko22df3f02020-08-24 13:29:22 +02004063 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p;
4064 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
Michal Vasko20424b42020-08-31 12:29:38 +02004065 struct lysp_node *child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004066 LY_ERR ret = LY_SUCCESS;
4067
Michal Vasko20424b42020-08-31 12:29:38 +02004068 assert(node->nodetype == LYS_CHOICE);
4069
Radek Krejci056d0a82018-12-06 16:57:25 +01004070 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko20424b42020-08-31 12:29:38 +02004071 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node));
Radek Krejci056d0a82018-12-06 16:57:25 +01004072 }
4073
4074 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004075 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004076 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004077 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004078
Radek Krejci9800fb82018-12-13 14:26:23 +01004079 return ret;
4080}
4081
4082/**
4083 * @brief Compile parsed anydata or anyxml node information.
4084 * @param[in] ctx Compile context
4085 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004086 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4087 * is enriched with the any-specific information.
4088 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4089 */
4090static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004091lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004092{
Michal Vasko22df3f02020-08-24 13:29:22 +02004093 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p;
4094 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004095 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9800fb82018-12-13 14:26:23 +01004096 LY_ERR ret = LY_SUCCESS;
4097
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004098 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004099 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4100 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004101 ret = ly_set_add(&ctx->xpath, any, 0, NULL);
4102 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004103 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004104
4105 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004106 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4107 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004108 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004109done:
4110 return ret;
4111}
4112
Radek Krejcib56c7502019-02-13 14:19:54 +01004113/**
Michal Vasko795b3752020-07-13 15:24:27 +02004114 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4115 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004116 *
4117 * @param[in] ctx Compile context
4118 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4119 * the choice itself is expected instead of a specific case node.
4120 * @param[in] node Schema node to connect into the list.
4121 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004122 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
Radek Krejci056d0a82018-12-06 16:57:25 +01004123 */
4124static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004125lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004126{
Michal Vasko795b3752020-07-13 15:24:27 +02004127 struct lysc_node **children, *start, *end;
4128 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004129
Michal Vasko20424b42020-08-31 12:29:38 +02004130 node->parent = parent;
4131
4132 if (parent) {
4133 if (parent->nodetype == LYS_CHOICE) {
4134 assert(node->nodetype == LYS_CASE);
4135 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
4136 } else {
4137 children = lysc_node_children_p(parent, ctx->options);
4138 }
4139 assert(children);
4140
Radek Krejci056d0a82018-12-06 16:57:25 +01004141 if (!(*children)) {
4142 /* first child */
4143 *children = node;
4144 } else if (*children != node) {
4145 /* by the condition in previous branch we cover the choice/case children
4146 * - the children list is shared by the choice and the the first case, in addition
4147 * the first child of each case must be referenced from the case node. So the node is
4148 * actually always already inserted in case it is the first children - so here such
4149 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004150 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4151 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4152 /* some augments are already connected and we are connecting new ones,
4153 * keep module name order and insert the node into the children list */
4154 end = (*children);
4155 do {
4156 end = end->prev;
4157 mod = end->module;
4158 while (end->prev->module == mod) {
4159 end = end->prev;
4160 }
Radek Krejcif6a11002020-08-21 13:29:07 +02004161 } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module) && (strcmp(mod->name, node->module->name) > 0));
Michal Vasko795b3752020-07-13 15:24:27 +02004162
4163 /* we have the last existing node after our node, easily get the first before and connect it */
4164 start = end->prev;
4165 start->next = node;
4166 node->next = end;
4167 end->prev = node;
4168 node->prev = start;
4169 } else {
4170 /* insert at the end of the parent's children list */
4171 (*children)->prev->next = node;
4172 node->prev = (*children)->prev;
4173 (*children)->prev = node;
4174 }
Michal Vasko20424b42020-08-31 12:29:38 +02004175 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004176
Michal Vasko20424b42020-08-31 12:29:38 +02004177 /* check the name uniqueness (even for an only child, it may be in case) */
4178 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
4179 return LY_EEXIST;
4180 }
4181 } else {
4182 /* top-level element */
4183 if (!ctx->mod->compiled->data) {
4184 ctx->mod->compiled->data = node;
4185 } else {
4186 /* insert at the end of the module's top-level nodes list */
4187 ctx->mod->compiled->data->prev->next = node;
4188 node->prev = ctx->mod->compiled->data->prev;
4189 ctx->mod->compiled->data->prev = node;
4190 }
4191
4192 /* check the name uniqueness on top-level */
4193 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
4194 return LY_EEXIST;
Radek Krejci056d0a82018-12-06 16:57:25 +01004195 }
4196 }
Michal Vasko20424b42020-08-31 12:29:38 +02004197
Radek Krejci056d0a82018-12-06 16:57:25 +01004198 return LY_SUCCESS;
4199}
4200
Radek Krejci95710c92019-02-11 15:49:55 +01004201/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004202 * @brief Prepare the case structure in choice node for the new data node.
4203 *
4204 * 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
4205 * created in the choice when the first child was processed.
4206 *
4207 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004208 * @param[in] node_p Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case,
Radek Krejci39424802020-08-12 09:31:00 +02004209 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004210 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4211 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4212 * @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,
4213 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004214 */
Michal Vasko20424b42020-08-31 12:29:38 +02004215static LY_ERR
4216lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004217{
Michal Vasko20424b42020-08-31 12:29:38 +02004218 struct lysp_node *child_p;
4219 struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004220
Radek Krejci39424802020-08-12 09:31:00 +02004221 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004222 /* we have to add an implicit case node into the parent choice */
Radek Krejci95710c92019-02-11 15:49:55 +01004223 } else if (node_p->nodetype == LYS_CASE) {
Michal Vasko20424b42020-08-31 12:29:38 +02004224 /* explicit parent case */
4225 LY_LIST_FOR(cs_p->child, child_p) {
4226 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004227 }
Radek Krejci95710c92019-02-11 15:49:55 +01004228 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004229 LOGINT_RET(ctx->ctx);
Radek Krejci056d0a82018-12-06 16:57:25 +01004230 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004231
Michal Vasko20424b42020-08-31 12:29:38 +02004232 return LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01004233}
4234
Radek Krejcib56c7502019-02-13 14:19:54 +01004235/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004236 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004237 *
4238 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004239 * @param[in] node Target node where the config is supposed to be changed.
4240 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004241 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4242 * done only on the node for which the refine was created. The function applies also recursively to apply the config change
Radek Krejci93dcc392019-02-19 10:43:38 +01004243 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4244 * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01004245 * @return LY_ERR value.
4246 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004247static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004248lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci857189e2020-09-01 13:26:36 +02004249 ly_bool inheriting, ly_bool refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004250{
4251 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004252 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004253
4254 if (config == (node->flags & LYS_CONFIG_MASK)) {
4255 /* nothing to do */
4256 return LY_SUCCESS;
4257 }
4258
4259 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004260 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004261 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004263 "Invalid %s of config - configuration node cannot be child of any state data node.",
4264 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004265 return LY_EVALID;
4266 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004267 node->flags |= LYS_SET_CONFIG;
4268 } else {
4269 if (node->flags & LYS_SET_CONFIG) {
4270 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4271 /* setting config flags, but have node with explicit config true */
4272 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004273 "Invalid %s of config - configuration node cannot be child of any state data node.",
4274 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004275 return LY_EVALID;
4276 }
4277 /* do not change config on nodes where the config is explicitely set, this does not apply to
4278 * nodes, which are being changed explicitly (targets of refine or deviation) */
4279 return LY_SUCCESS;
4280 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004281 }
4282 node->flags &= ~LYS_CONFIG_MASK;
4283 node->flags |= config;
4284
4285 /* inherit the change into the children */
Michal Vasko22df3f02020-08-24 13:29:22 +02004286 LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004287 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004288 }
4289
Radek Krejci76b3e962018-12-14 17:01:25 +01004290 return LY_SUCCESS;
4291}
4292
Radek Krejcib56c7502019-02-13 14:19:54 +01004293/**
4294 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4295 *
4296 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4297 * the flag to such parents from a mandatory children.
4298 *
4299 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4300 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4301 * (mandatory children was removed).
4302 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02004303static void
Radek Krejci857189e2020-09-01 13:26:36 +02004304lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
Radek Krejcife909632019-02-12 15:34:42 +01004305{
4306 struct lysc_node *iter;
4307
4308 if (add) { /* set flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004309 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
Radek Krejcife909632019-02-12 15:34:42 +01004310 parent = parent->parent) {
4311 parent->flags |= LYS_MAND_TRUE;
4312 }
4313 } else { /* unset flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004314 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004315 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004316 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004317 /* there is another mandatory node */
4318 return;
4319 }
4320 }
4321 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4322 parent->flags &= ~LYS_MAND_TRUE;
4323 }
4324 }
4325}
4326
Radek Krejci056d0a82018-12-06 16:57:25 +01004327/**
Radek Krejci3641f562019-02-13 15:38:40 +01004328 * @brief Internal sorting process for the lys_compile_augment_sort().
4329 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4330 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4331 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4332 */
4333static void
4334lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4335{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004336 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004337 size_t len;
4338
4339 len = strlen(aug_p->nodeid);
4340 LY_ARRAY_FOR(result, v) {
4341 if (strlen(result[v]->nodeid) <= len) {
4342 continue;
4343 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004344 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004345 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004346 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004347 break;
4348 }
4349 }
4350 result[v] = aug_p;
4351 LY_ARRAY_INCREMENT(result);
4352}
4353
4354/**
4355 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4356 *
4357 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4358 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4359 *
4360 * @param[in] ctx Compile context.
4361 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4362 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4363 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4364 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4365 * @return LY_ERR value.
4366 */
4367LY_ERR
4368lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4369{
4370 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004371 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004372
4373 assert(augments);
4374
4375 /* get count of the augments in module and all its submodules */
4376 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004377 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004378 }
4379 LY_ARRAY_FOR(inc_p, u) {
4380 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004381 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004382 }
4383 }
4384
4385 if (!count) {
4386 *augments = NULL;
4387 return LY_SUCCESS;
4388 }
4389 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4390
4391 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4392 * together, so there can be even /z/y betwwen them. */
4393 LY_ARRAY_FOR(aug_p, u) {
4394 lys_compile_augment_sort_(&aug_p[u], result);
4395 }
4396 LY_ARRAY_FOR(inc_p, u) {
4397 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4398 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4399 }
4400 }
4401
4402 *augments = result;
4403 return LY_SUCCESS;
4404}
4405
4406/**
4407 * @brief Compile the parsed augment connecting it into its target.
4408 *
4409 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4410 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4411 * are already implemented and compiled.
4412 *
4413 * @param[in] ctx Compile context.
4414 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004415 * @param[in] parent Parent node to provide the augment's context. It is NULL for the top level augments and a node holding uses's
4416 * children in case of the augmenting uses data.
4417 * @return LY_SUCCESS on success.
4418 * @return LY_EVALID on failure.
4419 */
4420LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004421lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004422{
Michal Vaskoe6143202020-07-03 13:02:08 +02004423 LY_ERR ret = LY_SUCCESS, rc;
Michal Vasko20424b42020-08-31 12:29:38 +02004424 struct lysp_node *node_p;
Radek Krejci3641f562019-02-13 15:38:40 +01004425 struct lysc_node *target; /* target target of the augment */
4426 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004427 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004428 struct lys_module **aug_mod;
Radek Krejci857189e2020-09-01 13:26:36 +02004429 ly_bool allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004430 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004431 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004432 uint32_t opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004433
Radek Krejci327de162019-06-14 12:52:07 +02004434 lysc_update_path(ctx, NULL, "{augment}");
4435 lysc_update_path(ctx, NULL, aug_p->nodeid);
4436
Michal Vaskocb18c7f2020-08-24 09:22:28 +02004437 ret = lysc_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004438 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Michal Vasko22df3f02020-08-24 13:29:22 +02004439 1, (const struct lysc_node **)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004440 if (ret != LY_SUCCESS) {
4441 if (ret == LY_EDENIED) {
4442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4443 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4444 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4445 }
4446 return LY_EVALID;
4447 }
4448
4449 /* check for mandatory nodes
4450 * - new cases augmenting some choice can have mandatory nodes
4451 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4452 */
Radek Krejci733988a2019-02-15 15:12:44 +01004453 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004454 allow_mandatory = 1;
4455 }
4456
4457 when_shared = NULL;
4458 LY_LIST_FOR(aug_p->child, node_p) {
4459 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4460 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004461 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004462 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4463 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004464 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004465 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4466 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004467 return LY_EVALID;
4468 }
4469
4470 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004471 ctx->options |= flags;
Michal Vasko20424b42020-08-31 12:29:38 +02004472 if (target->nodetype == LYS_CHOICE) {
4473 LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target));
Radek Krejci3641f562019-02-13 15:38:40 +01004474 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004475 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004476 }
Radek Krejciec4da802019-05-02 13:02:41 +02004477 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004478
Radek Krejcife13da42019-02-15 14:51:01 +01004479 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4480 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004481 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004482 /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */
Michal Vasko22df3f02020-08-24 13:29:22 +02004483 for (node = (struct lysc_node *)lysc_node_children(target, flags); node->next && node->next->parent == node->parent; node = node->next) {}
Radek Krejci3641f562019-02-13 15:38:40 +01004484 } else if (target->nodetype == LYS_CHOICE) {
4485 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
Michal Vasko22df3f02020-08-24 13:29:22 +02004486 node = ((struct lysc_node_choice *)target)->cases->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004487 } else {
4488 /* the compiled node is the last child of the target */
Michal Vasko22df3f02020-08-24 13:29:22 +02004489 node = (struct lysc_node *)lysc_node_children(target, flags);
Radek Krejci7c7783d2020-04-08 15:34:39 +02004490 if (!node) {
4491 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4492 break;
4493 }
4494 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004495 }
4496
Radek Krejci733988a2019-02-15 15:12:44 +01004497 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004498 node->flags &= ~LYS_MAND_TRUE;
4499 lys_compile_mandatory_parents(target, 0);
4500 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004501 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004502 return LY_EVALID;
4503 }
4504
4505 /* pass augment's when to all the children */
4506 if (aug_p->when) {
4507 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4508 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004509 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004510 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004511
4512 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4513 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004514 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4515 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004516 }
4517
Radek Krejci3641f562019-02-13 15:38:40 +01004518 when_shared = *when;
4519 } else {
4520 ++when_shared->refcount;
4521 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004522
4523 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4524 /* in this case check "when" again for all children because of dummy node check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004525 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4526 LY_CHECK_GOTO(ret, error);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004527 }
Radek Krejci3641f562019-02-13 15:38:40 +01004528 }
4529 }
4530 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004531
Radek Krejciec4da802019-05-02 13:02:41 +02004532 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004533 switch (target->nodetype) {
4534 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004535 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004536 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004537 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004538 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004539 break;
4540 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004541 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004542 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004543 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004544 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004545 break;
4546 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004547 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004548 if (aug_p->actions) {
4549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004550 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4551 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004552 return LY_EVALID;
4553 }
4554 if (aug_p->notifs) {
4555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004556 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004557 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004558 return LY_EVALID;
4559 }
4560 }
Radek Krejci3641f562019-02-13 15:38:40 +01004561
Michal Vaskoe6143202020-07-03 13:02:08 +02004562 /* add this module into the target module augmented_by, if not there already */
4563 rc = LY_SUCCESS;
4564 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4565 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4566 rc = LY_EEXIST;
4567 break;
4568 }
4569 }
4570 if (!rc) {
4571 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4572 *aug_mod = ctx->mod;
4573 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004574
Radek Krejci327de162019-06-14 12:52:07 +02004575 lysc_update_path(ctx, NULL, NULL);
4576 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004577
Radek Krejci3641f562019-02-13 15:38:40 +01004578error:
Radek Krejciec4da802019-05-02 13:02:41 +02004579 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004580 return ret;
4581}
4582
4583/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004584 * @brief Apply refined or deviated mandatory flag to the target node.
4585 *
4586 * @param[in] ctx Compile context.
4587 * @param[in] node Target node where the mandatory property is supposed to be changed.
4588 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004589 * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging).
Radek Krejci551b12c2019-02-19 16:11:21 +01004590 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4591 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4592 * the tests are skipped here, but they are supposed to be performed after all the deviations are applied.
Radek Krejcif1421c22019-02-19 13:05:20 +01004593 * @return LY_ERR value.
4594 */
4595static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004596lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, ly_bool refine_flag)
Radek Krejcif1421c22019-02-19 13:05:20 +01004597{
4598 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004600 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4601 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004602 return LY_EVALID;
4603 }
4604
4605 if (mandatory_flag & LYS_MAND_TRUE) {
4606 /* check if node has default value */
4607 if (node->nodetype & LYS_LEAF) {
4608 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004609 if (refine_flag) {
4610 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004611 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004612 return LY_EVALID;
4613 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004614 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004615 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004616 if (refine_flag) {
4617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004618 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004619 return LY_EVALID;
4620 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004621 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004622 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid refine of mandatory under the default case.");
Radek Krejcif1421c22019-02-19 13:05:20 +01004624 return LY_EVALID;
4625 }
4626
4627 node->flags &= ~LYS_MAND_FALSE;
4628 node->flags |= LYS_MAND_TRUE;
4629 lys_compile_mandatory_parents(node->parent, 1);
4630 } else {
4631 /* make mandatory false */
4632 node->flags &= ~LYS_MAND_TRUE;
4633 node->flags |= LYS_MAND_FALSE;
4634 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcif1421c22019-02-19 13:05:20 +01004635 }
4636 return LY_SUCCESS;
4637}
4638
4639/**
Michal Vasko601ddb32020-08-31 16:25:35 +02004640 * @brief Find grouping for a uses.
Radek Krejcie86bf772018-12-14 11:39:53 +01004641 *
Michal Vasko601ddb32020-08-31 16:25:35 +02004642 * @param[in] ctx Compile context.
4643 * @param[in] uses_p Parsed uses node.
4644 * @param[out] gpr_p Found grouping on success.
4645 * @param[out] grp_mod Module of @p grp_p on success.
4646 * @return LY_ERR value.
Radek Krejcie86bf772018-12-14 11:39:53 +01004647 */
4648static LY_ERR
Michal Vasko601ddb32020-08-31 16:25:35 +02004649lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
4650 struct lys_module **grp_mod)
Radek Krejcie86bf772018-12-14 11:39:53 +01004651{
4652 struct lysp_node *node_p;
Michal Vasko601ddb32020-08-31 16:25:35 +02004653 struct lysp_grp *grp;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004654 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +02004655 ly_bool found = 0;
Radek Krejcie86bf772018-12-14 11:39:53 +01004656 const char *id, *name, *prefix;
4657 size_t prefix_len, name_len;
Michal Vasko601ddb32020-08-31 16:25:35 +02004658 struct lys_module *mod;
4659
4660 *grp_p = NULL;
4661 *grp_mod = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004662
4663 /* search for the grouping definition */
Radek Krejcie86bf772018-12-14 11:39:53 +01004664 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004665 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004666 if (prefix) {
4667 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4668 if (!mod) {
4669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004670 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004671 return LY_EVALID;
4672 }
4673 } else {
4674 mod = ctx->mod_def;
4675 }
4676 if (mod == ctx->mod_def) {
4677 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004678 grp = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004679 LY_ARRAY_FOR(grp, u) {
4680 if (!strcmp(grp[u].name, name)) {
4681 grp = &grp[u];
4682 found = 1;
4683 break;
4684 }
4685 }
4686 }
4687 }
4688 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004689 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004690 grp = mod->parsed->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004691 LY_ARRAY_FOR(grp, u) {
4692 if (!strcmp(grp[u].name, name)) {
4693 grp = &grp[u];
4694 found = 1;
4695 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004696 }
4697 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004698 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004699 /* ... and all the submodules */
Michal Vasko601ddb32020-08-31 16:25:35 +02004700 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004701 grp = mod->parsed->includes[u].submodule->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004702 LY_ARRAY_FOR(grp, v) {
4703 if (!strcmp(grp[v].name, name)) {
4704 grp = &grp[v];
4705 found = 1;
4706 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004707 }
4708 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004709 if (found) {
4710 break;
4711 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004712 }
4713 }
4714 }
4715 if (!found) {
4716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4717 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4718 return LY_EVALID;
4719 }
4720
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004721 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4722 /* remember that the grouping is instantiated to avoid its standalone validation */
4723 grp->flags |= LYS_USED_GRP;
4724 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004725
Michal Vasko601ddb32020-08-31 16:25:35 +02004726 *grp_p = grp;
4727 *grp_mod = mod;
4728 return LY_SUCCESS;
4729}
Radek Krejcie86bf772018-12-14 11:39:53 +01004730
Michal Vasko601ddb32020-08-31 16:25:35 +02004731static LY_ERR
4732lys_compile_refines(struct lysc_ctx *ctx, struct lysp_refine *refines, const struct lysc_node *context_node)
4733{
4734 struct lysc_node *node;
4735 LY_ARRAY_COUNT_TYPE u;
4736 struct lysp_refine *rfn;
4737 LY_ERR ret = LY_SUCCESS;
4738 uint32_t min, max;
4739 uint16_t flags;
4740 struct ly_set refined = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01004741
Radek Krejci327de162019-06-14 12:52:07 +02004742 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004743
Radek Krejci76b3e962018-12-14 17:01:25 +01004744 /* apply refine */
Michal Vasko601ddb32020-08-31 16:25:35 +02004745 LY_ARRAY_FOR(refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004746 lysc_update_path(ctx, NULL, rfn->nodeid);
4747
Michal Vasko601ddb32020-08-31 16:25:35 +02004748 ret = lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, context_node, ctx->mod,
Michal Vasko20424b42020-08-31 12:29:38 +02004749 0, 0, (const struct lysc_node **)&node, &flags);
4750 LY_CHECK_GOTO(ret, cleanup);
4751 ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL);
4752 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004753
4754 /* default value */
4755 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004756 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004757 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci0f969882020-08-21 16:56:47 +02004758 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE " default values.",
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004759 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Michal Vasko20424b42020-08-31 12:29:38 +02004760 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004761 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004762 }
4763 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004765 "Invalid refine of default - %s cannot hold default value(s).",
4766 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004767 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004768 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004769 }
4770 if (node->nodetype == LYS_LEAF) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004771 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004772 ret = lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], ctx->mod_def);
4773 LY_CHECK_GOTO(ret, cleanup);
4774
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004775 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004776 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004777 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4779 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
Michal Vasko20424b42020-08-31 12:29:38 +02004780 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004781 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004782 }
Radek Krejcia1911222019-07-22 17:24:50 +02004783
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004784 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004785 ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def);
4786 LY_CHECK_GOTO(ret, cleanup);
4787
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004788 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004789 } else if (node->nodetype == LYS_CHOICE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004790 if (((struct lysc_node_choice *)node)->dflt) {
Radek Krejci01342af2019-01-03 15:18:08 +01004791 /* unset LYS_SET_DFLT from the current default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02004792 ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT;
Radek Krejci01342af2019-01-03 15:18:08 +01004793 }
Michal Vasko20424b42020-08-31 12:29:38 +02004794 ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node);
4795 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004796 }
4797 }
4798
Radek Krejci12fb9142019-01-08 09:45:30 +01004799 /* description */
4800 if (rfn->dsc) {
4801 FREE_STRING(ctx->ctx, node->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004802 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->dsc, 0, &node->dsc), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004803 }
4804
4805 /* reference */
4806 if (rfn->ref) {
4807 FREE_STRING(ctx->ctx, node->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004808 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->ref, 0, &node->ref), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004809 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004810
4811 /* config */
4812 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004813 if (!flags) {
Michal Vasko20424b42020-08-31 12:29:38 +02004814 ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1);
4815 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004816 } else {
4817 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004818 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004819 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004820 }
4821
4822 /* mandatory */
4823 if (rfn->flags & LYS_MAND_MASK) {
Michal Vasko20424b42020-08-31 12:29:38 +02004824 ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1);
4825 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004826 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004827
4828 /* presence */
4829 if (rfn->presence) {
4830 if (node->nodetype != LYS_CONTAINER) {
4831 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004832 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4833 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004834 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004835 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004836 }
4837 node->flags |= LYS_PRESENCE;
4838 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004839
4840 /* must */
4841 if (rfn->musts) {
4842 switch (node->nodetype) {
4843 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02004844 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf *)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004845 break;
4846 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004847 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist *)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004848 break;
4849 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004850 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list *)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004851 break;
4852 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004853 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container *)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004854 break;
4855 case LYS_ANYXML:
4856 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02004857 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata *)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004858 break;
4859 default:
4860 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004861 "Invalid refine of must statement - %s cannot hold any must statement.",
4862 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004863 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004864 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004865 }
Michal Vasko20424b42020-08-31 12:29:38 +02004866 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4867 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004868 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004869
4870 /* min/max-elements */
4871 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4872 switch (node->nodetype) {
4873 case LYS_LEAFLIST:
4874 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004875 ((struct lysc_node_leaflist *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004876 }
4877 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004878 ((struct lysc_node_leaflist *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004879 if (rfn->min) {
4880 node->flags |= LYS_MAND_TRUE;
4881 lys_compile_mandatory_parents(node->parent, 1);
4882 } else {
4883 node->flags &= ~LYS_MAND_TRUE;
4884 lys_compile_mandatory_parents(node->parent, 0);
4885 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004886 }
4887 break;
4888 case LYS_LIST:
4889 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004890 ((struct lysc_node_list *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004891 }
4892 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004893 ((struct lysc_node_list *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004894 if (rfn->min) {
4895 node->flags |= LYS_MAND_TRUE;
4896 lys_compile_mandatory_parents(node->parent, 1);
4897 } else {
4898 node->flags &= ~LYS_MAND_TRUE;
4899 lys_compile_mandatory_parents(node->parent, 0);
4900 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004901 }
4902 break;
4903 default:
4904 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004905 "Invalid refine of %s statement - %s cannot hold this statement.",
Radek Krejci0f969882020-08-21 16:56:47 +02004906 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004907 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004908 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004909 }
4910 }
Radek Krejcif0089082019-01-07 16:42:01 +01004911
4912 /* if-feature */
4913 if (rfn->iffeatures) {
4914 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004915 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004916 }
Radek Krejci327de162019-06-14 12:52:07 +02004917
4918 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004919 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004920
Radek Krejcif2271f12019-01-07 16:42:23 +01004921 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004922 for (uint32_t i = 0; i < refined.count; ++i) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004923 node = (struct lysc_node *)refined.objs[i];
Michal Vasko601ddb32020-08-31 16:25:35 +02004924 rfn = &refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02004925 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004926
4927 /* check possible conflict with default value (default added, mandatory left true) */
4928 if ((node->flags & LYS_MAND_TRUE) &&
Michal Vasko22df3f02020-08-24 13:29:22 +02004929 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) ||
Radek Krejcif2271f12019-01-07 16:42:23 +01004930 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004932 "Invalid refine of default - the node is mandatory.");
Michal Vasko20424b42020-08-31 12:29:38 +02004933 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004934 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004935 }
4936
4937 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4938 if (node->nodetype == LYS_LIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004939 min = ((struct lysc_node_list *)node)->min;
4940 max = ((struct lysc_node_list *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004941 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02004942 min = ((struct lysc_node_leaflist *)node)->min;
4943 max = ((struct lysc_node_leaflist *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004944 }
4945 if (min > max) {
4946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004947 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
Radek Krejci0f969882020-08-21 16:56:47 +02004948 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Michal Vasko20424b42020-08-31 12:29:38 +02004949 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004950 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004951 }
4952 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004953
4954 lysc_update_path(ctx, NULL, NULL);
Radek Krejcif2271f12019-01-07 16:42:23 +01004955 }
4956
Michal Vasko601ddb32020-08-31 16:25:35 +02004957cleanup:
4958 ly_set_erase(&refined, NULL);
4959 lysc_update_path(ctx, NULL, NULL);
4960 return ret;
4961}
4962
4963/**
4964 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4965 * If present, also apply uses's modificators.
4966 *
4967 * @param[in] ctx Compile context
4968 * @param[in] uses_p Parsed uses schema node.
4969 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4970 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4971 * the compile context.
4972 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4973 */
4974static LY_ERR
4975lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p)
4976{
4977 struct lysp_node *node_p;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004978 struct lysc_node *child = NULL, *iter;
Michal Vasko601ddb32020-08-31 16:25:35 +02004979 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
4980 struct lysc_node_container context_node_fake =
4981 {.nodetype = LYS_CONTAINER,
4982 .module = ctx->mod,
4983 .flags = parent ? parent->flags : 0,
4984 .child = NULL, .next = NULL,
4985 .prev = (struct lysc_node *)&context_node_fake,
4986 .actions = NULL, .notifs = NULL};
4987 struct lysp_grp *grp = NULL;
4988 LY_ARRAY_COUNT_TYPE u;
4989 uint32_t grp_stack_count;
4990 struct lys_module *grp_mod, *mod_old;
4991 LY_ERR ret = LY_SUCCESS;
4992 struct lysc_when **when, *when_shared;
4993 struct lysp_augment **augments = NULL;
4994 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
4995 struct lysc_notif **notifs = NULL;
4996 struct lysc_action **actions = NULL;
4997
4998 /* find the referenced grouping */
4999 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
5000
5001 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
5002 grp_stack_count = ctx->groupings.count;
5003 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
5004 if (grp_stack_count == ctx->groupings.count) {
5005 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
5006 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5007 "Grouping \"%s\" references itself through a uses statement.", grp->name);
5008 return LY_EVALID;
5009 }
5010
5011 /* switch context's mod_def */
5012 mod_old = ctx->mod_def;
5013 ctx->mod_def = grp_mod;
5014
5015 /* check status */
5016 ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, grp_mod, grp->name);
5017 LY_CHECK_GOTO(ret, cleanup);
5018
5019 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
5020 * applu refine and augment only to the nodes from the uses */
5021 if (parent) {
5022 child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5023 } else if (ctx->mod->compiled->data) {
5024 child = ctx->mod->compiled->data;
5025 } else {
5026 child = NULL;
5027 }
5028 /* remember the last child */
5029 if (child) {
5030 child = child->prev;
5031 }
5032
5033 /* compile data nodes */
5034 LY_LIST_FOR(grp->data, node_p) {
5035 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
5036 ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3);
5037 LY_CHECK_GOTO(ret, cleanup);
5038 }
5039
5040 /* split the children and add the uses's data into the fake context node */
5041 if (child) {
5042 context_node_fake.child = child->next;
5043 } else if (parent) {
5044 context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5045 } else if (ctx->mod->compiled->data) {
5046 context_node_fake.child = ctx->mod->compiled->data;
5047 }
5048 if (context_node_fake.child) {
5049 /* remember child as the last data node added by grouping to fix the list later */
5050 child = context_node_fake.child->prev;
5051 context_node_fake.child->prev = NULL;
5052 }
5053
5054 when_shared = NULL;
5055 LY_LIST_FOR(context_node_fake.child, iter) {
5056 iter->parent = (struct lysc_node *)&context_node_fake;
5057
5058 /* pass uses's when to all the data children, actions and notifications are ignored */
5059 if (uses_p->when) {
5060 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
5061 if (!when_shared) {
5062 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when);
5063 LY_CHECK_GOTO(ret, cleanup);
5064
5065 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5066 /* do not check "when" semantics in a grouping */
5067 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5068 LY_CHECK_GOTO(ret, cleanup);
5069 }
5070
5071 when_shared = *when;
5072 } else {
5073 ++when_shared->refcount;
5074 (*when) = when_shared;
5075
5076 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5077 /* in this case check "when" again for all children because of dummy node check */
5078 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5079 LY_CHECK_GOTO(ret, cleanup);
5080 }
5081 }
5082 }
5083 }
5084
5085 /* compile actions */
5086 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5087 if (actions) {
5088 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
5089 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
5090 if (*actions && (uses_p->augments || uses_p->refines)) {
5091 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5092 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
5093 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
5094 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
5095 }
5096 }
5097
5098 /* compile notifications */
5099 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5100 if (notifs) {
5101 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
5102 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
5103 if (*notifs && (uses_p->augments || uses_p->refines)) {
5104 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5105 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
5106 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
5107 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
5108 }
5109 }
5110
5111 /* sort and apply augments */
5112 ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments);
5113 LY_CHECK_GOTO(ret, cleanup);
5114 LY_ARRAY_FOR(augments, u) {
5115 ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake);
5116 LY_CHECK_GOTO(ret, cleanup);
5117 }
5118
5119 /* reload previous context's mod_def */
5120 ctx->mod_def = mod_old;
5121
5122 /* apply all refines */
5123 ret = lys_compile_refines(ctx, uses_p->refines, (struct lysc_node *)&context_node_fake);
5124 LY_CHECK_GOTO(ret, cleanup);
5125
Radek Krejcic6b4f442020-08-12 14:45:18 +02005126 if (first_p) {
5127 *first_p = context_node_fake.child;
5128 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005129
5130cleanup:
5131 /* fix connection of the children nodes from fake context node back into the parent */
5132 if (context_node_fake.child) {
5133 context_node_fake.child->prev = child;
5134 }
5135 LY_LIST_FOR(context_node_fake.child, child) {
5136 child->parent = parent;
5137 }
5138
5139 if (uses_p->augments || uses_p->refines) {
5140 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005141 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005142 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005143 LY_ARRAY_FREE(context_node_fake.actions);
5144 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005145 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005146 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005147 LY_ARRAY_FREE(context_node_fake.notifs);
5148 }
5149 }
5150
Radek Krejcie86bf772018-12-14 11:39:53 +01005151 /* reload previous context's mod_def */
5152 ctx->mod_def = mod_old;
5153 /* remove the grouping from the stack for circular groupings dependency check */
5154 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5155 assert(ctx->groupings.count == grp_stack_count);
Radek Krejci3641f562019-02-13 15:38:40 +01005156 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005157
5158 return ret;
5159}
5160
Radek Krejci327de162019-06-14 12:52:07 +02005161static int
5162lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5163{
5164 struct lysp_node *iter;
5165 int len = 0;
5166
5167 *path = NULL;
5168 for (iter = node; iter && len >= 0; iter = iter->parent) {
5169 char *s = *path;
5170 char *id;
5171
5172 switch (iter->nodetype) {
5173 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005174 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005175 break;
5176 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005177 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005178 break;
5179 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005180 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005181 break;
5182 default:
5183 id = strdup(iter->name);
5184 break;
5185 }
5186
5187 if (!iter->parent) {
5188 /* print prefix */
5189 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5190 } else {
5191 /* prefix is the same as in parent */
5192 len = asprintf(path, "/%s%s", id, s ? s : "");
5193 }
5194 free(s);
5195 free(id);
5196 }
5197
5198 if (len < 0) {
5199 free(*path);
5200 *path = NULL;
5201 } else if (len == 0) {
5202 *path = strdup("/");
5203 len = 1;
5204 }
5205 return len;
5206}
5207
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005208/**
5209 * @brief Validate groupings that were defined but not directly used in the schema itself.
5210 *
5211 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5212 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5213 */
5214static LY_ERR
5215lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5216{
5217 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005218 char *path;
5219 int len;
5220
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005221 struct lysp_node_uses fake_uses = {
5222 .parent = node_p,
5223 .nodetype = LYS_USES,
5224 .flags = 0, .next = NULL,
5225 .name = grp->name,
5226 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5227 .refines = NULL, .augments = NULL
5228 };
5229 struct lysc_node_container fake_container = {
5230 .nodetype = LYS_CONTAINER,
5231 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5232 .module = ctx->mod,
5233 .sp = NULL, .parent = NULL, .next = NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +02005234 .prev = (struct lysc_node *)&fake_container,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005235 .name = "fake",
5236 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5237 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5238 };
5239
5240 if (grp->parent) {
5241 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5242 }
Radek Krejci327de162019-06-14 12:52:07 +02005243
5244 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5245 if (len < 0) {
5246 LOGMEM(ctx->ctx);
5247 return LY_EMEM;
5248 }
5249 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005250 ctx->path_len = (uint32_t)len;
Radek Krejci327de162019-06-14 12:52:07 +02005251 free(path);
5252
5253 lysc_update_path(ctx, NULL, "{grouping}");
5254 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02005255 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005256 lysc_update_path(ctx, NULL, NULL);
5257 lysc_update_path(ctx, NULL, NULL);
5258
5259 ctx->path_len = 1;
5260 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005261
5262 /* cleanup */
5263 lysc_node_container_free(ctx->ctx, &fake_container);
5264
5265 return ret;
5266}
Radek Krejcife909632019-02-12 15:34:42 +01005267
Radek Krejcie86bf772018-12-14 11:39:53 +01005268/**
Michal Vasko20424b42020-08-31 12:29:38 +02005269 * @brief Set config flags for a node.
5270 *
5271 * @param[in] ctx Compile context.
5272 * @param[in] node Compiled node config to set.
5273 * @param[in] parent Parent of @p node.
5274 * @return LY_ERR value.
5275 */
5276static LY_ERR
5277lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
5278{
5279 if (node->nodetype == LYS_CASE) {
5280 /* case never has any config */
5281 assert(!(node->flags & LYS_CONFIG_MASK));
5282 return LY_SUCCESS;
5283 }
5284
5285 /* adjust parent to always get the ancestor with config */
5286 if (parent && (parent->nodetype == LYS_CASE)) {
5287 parent = parent->parent;
5288 assert(parent);
5289 }
5290
5291 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
5292 /* ignore config statements inside RPC/action data */
5293 node->flags &= ~LYS_CONFIG_MASK;
5294 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5295 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
5296 /* ignore config statements inside Notification data */
5297 node->flags &= ~LYS_CONFIG_MASK;
5298 node->flags |= LYS_CONFIG_R;
5299 } else if (!(node->flags & LYS_CONFIG_MASK)) {
5300 /* config not explicitely set, inherit it from parent */
5301 if (parent) {
5302 node->flags |= parent->flags & LYS_CONFIG_MASK;
5303 } else {
5304 /* default is config true */
5305 node->flags |= LYS_CONFIG_W;
5306 }
5307 } else {
5308 /* config set explicitely */
5309 node->flags |= LYS_SET_CONFIG;
5310 }
5311
5312 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5313 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5314 "Configuration node cannot be child of any state data node.");
5315 return LY_EVALID;
5316 }
5317
5318 return LY_SUCCESS;
5319}
5320
5321/**
Radek Krejcia3045382018-11-22 14:30:31 +01005322 * @brief Compile parsed schema node information.
5323 * @param[in] ctx Compile context
5324 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005325 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5326 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5327 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005328 * @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).
5329 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005330 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5331 */
Radek Krejci19a96102018-11-15 13:38:09 +01005332static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005333lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status)
Radek Krejci19a96102018-11-15 13:38:09 +01005334{
Radek Krejci1c54f462020-05-12 17:25:34 +02005335 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005336 struct lysc_node *node = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005337 struct lysc_when **when;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005338 LY_ARRAY_COUNT_TYPE u;
Michal Vasko22df3f02020-08-24 13:29:22 +02005339 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
Radek Krejci19a96102018-11-15 13:38:09 +01005340
Radek Krejci327de162019-06-14 12:52:07 +02005341 if (node_p->nodetype != LYS_USES) {
5342 lysc_update_path(ctx, parent, node_p->name);
5343 } else {
5344 lysc_update_path(ctx, NULL, "{uses}");
5345 lysc_update_path(ctx, NULL, node_p->name);
5346 }
5347
Radek Krejci19a96102018-11-15 13:38:09 +01005348 switch (node_p->nodetype) {
5349 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02005350 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
Radek Krejci19a96102018-11-15 13:38:09 +01005351 node_compile_spec = lys_compile_node_container;
5352 break;
5353 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005354 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
Radek Krejci19a96102018-11-15 13:38:09 +01005355 node_compile_spec = lys_compile_node_leaf;
5356 break;
5357 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005358 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005359 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005360 break;
5361 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005362 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005363 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005364 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005365 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02005366 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005367 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005368 break;
Michal Vasko20424b42020-08-31 12:29:38 +02005369 case LYS_CASE:
5370 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
5371 node_compile_spec = lys_compile_node_case;
5372 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005373 case LYS_ANYXML:
5374 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005375 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005376 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005377 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005378 case LYS_USES:
Michal Vasko9e8de2d2020-09-01 08:17:10 +02005379 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, parent, &node);
Radek Krejci327de162019-06-14 12:52:07 +02005380 lysc_update_path(ctx, NULL, NULL);
5381 lysc_update_path(ctx, NULL, NULL);
5382 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005383 default:
5384 LOGINT(ctx->ctx);
5385 return LY_EINT;
5386 }
5387 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5388 node->nodetype = node_p->nodetype;
5389 node->module = ctx->mod;
5390 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005391 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005392
5393 /* config */
Michal Vasko20424b42020-08-31 12:29:38 +02005394 ret = lys_compile_config(ctx, node, parent);
5395 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005396
Michal Vasko20424b42020-08-31 12:29:38 +02005397 /* list ordering */
Radek Krejcia6d57732018-11-29 13:40:37 +01005398 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5399 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005400 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci0f969882020-08-21 16:56:47 +02005401 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5402 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005403 node->flags &= ~LYS_ORDBY_MASK;
5404 node->flags |= LYS_ORDBY_SYSTEM;
5405 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5406 /* default ordering is system */
5407 node->flags |= LYS_ORDBY_SYSTEM;
5408 }
5409 }
5410
Radek Krejci19a96102018-11-15 13:38:09 +01005411 /* status - it is not inherited by specification, but it does not make sense to have
5412 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko20424b42020-08-31 12:29:38 +02005413 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005414
Radek Krejciec4da802019-05-02 13:02:41 +02005415 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005416 node->sp = node_p;
5417 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02005418 DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error);
5419 DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error);
5420 DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error);
Radek Krejci00b874b2019-02-12 10:54:50 +01005421 if (node_p->when) {
5422 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005423 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005424
5425 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5426 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02005427 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
5428 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005429 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005430 }
Radek Krejciec4da802019-05-02 13:02:41 +02005431 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005432
Michal Vasko20424b42020-08-31 12:29:38 +02005433 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
5434 LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node));
5435
Radek Krejci19a96102018-11-15 13:38:09 +01005436 /* nodetype-specific part */
Michal Vasko20424b42020-08-31 12:29:38 +02005437 LY_CHECK_RET(node_compile_spec(ctx, node_p, node));
Radek Krejci19a96102018-11-15 13:38:09 +01005438
Michal Vasko20424b42020-08-31 12:29:38 +02005439 /* final compilation tasks that require the node to be connected */
5440 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, done);
Radek Krejcife909632019-02-12 15:34:42 +01005441 if (node->flags & LYS_MAND_TRUE) {
Michal Vasko20424b42020-08-31 12:29:38 +02005442 /* inherit LYS_MAND_TRUE in parent containers */
Radek Krejcife909632019-02-12 15:34:42 +01005443 lys_compile_mandatory_parents(parent, 1);
5444 }
5445
Radek Krejci327de162019-06-14 12:52:07 +02005446 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005447 return LY_SUCCESS;
5448
5449error:
5450 lysc_node_free(ctx->ctx, node);
Michal Vasko20424b42020-08-31 12:29:38 +02005451done:
Radek Krejci19a96102018-11-15 13:38:09 +01005452 return ret;
5453}
5454
Radek Krejciccd20f12019-02-15 14:12:27 +01005455static void
Michal Vasko20424b42020-08-31 12:29:38 +02005456lysc_node_unlink(struct lysc_node *node)
Radek Krejciccd20f12019-02-15 14:12:27 +01005457{
Michal Vasko20424b42020-08-31 12:29:38 +02005458 struct lysc_node *parent, *child;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005459 struct lysc_module *modc = node->module->compiled;
Radek Krejciccd20f12019-02-15 14:12:27 +01005460
5461 parent = node->parent;
5462
5463 /* parent's first child */
Michal Vasko20424b42020-08-31 12:29:38 +02005464 if (parent && lysc_node_children(parent, node->flags) == node) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005465 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005466 } else if (modc->data == node) {
5467 modc->data = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005468 }
Michal Vasko20424b42020-08-31 12:29:38 +02005469
5470 /* special choice case unlinking */
5471 if (parent && parent->nodetype == LYS_CHOICE) {
5472 if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) {
5473 /* default case removed */
5474 ((struct lysc_node_choice *)parent)->dflt = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005475 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005476 }
5477
5478 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005479 if (node->prev->next) {
5480 node->prev->next = node->next;
5481 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005482 if (node->next) {
5483 node->next->prev = node->prev;
Michal Vasko20424b42020-08-31 12:29:38 +02005484 } else {
5485 /* last child */
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005486 if (parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005487 child = (struct lysc_node *)lysc_node_children(parent, node->flags);
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005488 } else {
5489 child = modc->data;
5490 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005491 if (child) {
5492 child->prev = node->prev;
5493 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005494 }
5495}
5496
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005497struct lysc_deviation {
5498 const char *nodeid;
5499 struct lysc_node *target; /* target node of the deviation */
Michal Vasko22df3f02020-08-24 13:29:22 +02005500 struct lysp_deviate **deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02005501 uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */
Radek Krejci857189e2020-09-01 13:26:36 +02005502 ly_bool not_supported; /* flag if deviates contains not-supported deviate */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005503};
5504
Michal Vaskoccc062a2020-08-13 08:34:50 +02005505/* MACROS for deviates checking */
5506#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5507 if (!(target->nodetype & (NODETYPES))) { \
5508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5509 goto cleanup; \
5510 }
5511
5512#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5513 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5515 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5516 goto cleanup; \
5517 }
5518
5519#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5520 if (!((TYPE)target)->MEMBER || COND) { \
5521 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5522 goto cleanup; \
5523 }
5524
5525/**
5526 * @brief Apply deviate add.
5527 *
5528 * @param[in] ctx Compile context.
5529 * @param[in] target Deviation target.
5530 * @param[in] dev_flags Internal deviation flags.
5531 * @param[in] d Deviate add to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02005532 * @return LY_ERR value.
5533 */
5534static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005535lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysc_node *target, uint32_t dev_flags, struct lysp_deviate_add *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02005536{
Radek Krejci011e4aa2020-09-04 15:22:31 +02005537 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005538 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5539 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005540 LY_ARRAY_COUNT_TYPE x;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005541
5542#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5543 if (((TYPE)target)->MEMBER COND) { \
5544 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5545 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5546 PROPERTY, ((TYPE)target)->MEMBER); \
5547 goto cleanup; \
5548 }
5549
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005550#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005551 if (((TYPE)target)->MEMBER && (COND)) { \
Radek Krejci857189e2020-09-01 13:26:36 +02005552 ly_bool dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005553 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005554 ctx->mod_def, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5556 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5557 if (dynamic_) {free((void*)val_);} \
5558 goto cleanup; \
5559 }
5560
5561#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5562 if (((TYPE)target)->MEMBER && (COND)) { \
5563 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5564 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5565 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5566 goto cleanup; \
5567 }
5568
5569 /* [units-stmt] */
5570 if (d->units) {
5571 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5572 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5573
5574 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02005575 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc);
5576 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005577 }
5578
5579 /* *must-stmt */
5580 if (d->musts) {
5581 switch (target->nodetype) {
5582 case LYS_CONTAINER:
5583 case LYS_LIST:
5584 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5585 x, lys_compile_must, ret, cleanup);
5586 break;
5587 case LYS_LEAF:
5588 case LYS_LEAFLIST:
5589 case LYS_ANYDATA:
5590 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5591 x, lys_compile_must, ret, cleanup);
5592 break;
5593 case LYS_NOTIF:
5594 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5595 x, lys_compile_must, ret, cleanup);
5596 break;
5597 case LYS_RPC:
5598 case LYS_ACTION:
5599 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5600 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5601 x, lys_compile_must, ret, cleanup);
5602 break;
5603 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5604 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5605 x, lys_compile_must, ret, cleanup);
5606 break;
5607 }
Radek Krejci0f969882020-08-21 16:56:47 +02005608 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005609 default:
5610 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5611 lys_nodetype2str(target->nodetype), "add", "must");
5612 goto cleanup;
5613 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02005614 ret = ly_set_add(&ctx->xpath, target, 0, NULL);
5615 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005616 }
5617
5618 /* *unique-stmt */
5619 if (d->uniques) {
5620 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5621 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5622 }
5623
5624 /* *default-stmt */
5625 if (d->dflts) {
5626 switch (target->nodetype) {
5627 case LYS_LEAF:
5628 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005629 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005630 if (leaf->dflt) {
5631 /* first, remove the default value taken from the type */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005632 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5633 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005634 free(leaf->dflt);
5635 leaf->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005636 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005637
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005638 /* store the default value in unres */
Michal Vasko31a82d52020-08-21 08:11:48 +02005639 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005640 target->flags |= LYS_SET_DFLT;
5641 break;
5642 case LYS_LEAFLIST:
5643 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5644 /* first, remove the default value taken from the type */
5645 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005646 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5647 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5648 free(llist->dflts[x]);
5649 }
5650 LY_ARRAY_FREE(llist->dflts);
5651 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005652 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005653
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005654 /* store the default values in unres */
5655 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005656 target->flags |= LYS_SET_DFLT;
5657 break;
5658 case LYS_CHOICE:
5659 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5660 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5661 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5662 * to allow making the default case even the augmented case from the deviating module */
5663 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5664 goto cleanup;
5665 }
5666 break;
5667 default:
5668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5669 lys_nodetype2str(target->nodetype), "add", "default");
5670 goto cleanup;
5671 }
5672 }
5673
5674 /* [config-stmt] */
5675 if (d->flags & LYS_CONFIG_MASK) {
5676 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5678 lys_nodetype2str(target->nodetype), "add", "config");
5679 goto cleanup;
5680 }
5681 if (dev_flags) {
5682 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5683 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5684 }
5685 if (target->flags & LYS_SET_CONFIG) {
5686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5687 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5688 target->flags & LYS_CONFIG_W ? "true" : "false");
5689 goto cleanup;
5690 }
5691 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5692 }
5693
5694 /* [mandatory-stmt] */
5695 if (d->flags & LYS_MAND_MASK) {
5696 if (target->flags & LYS_MAND_MASK) {
5697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5698 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5699 target->flags & LYS_MAND_TRUE ? "true" : "false");
5700 goto cleanup;
5701 }
5702 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5703 }
5704
5705 /* [min-elements-stmt] */
5706 if (d->flags & LYS_SET_MIN) {
5707 if (target->nodetype == LYS_LEAFLIST) {
5708 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5709 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005710 ((struct lysc_node_leaflist *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005711 } else if (target->nodetype == LYS_LIST) {
5712 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5713 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005714 ((struct lysc_node_list *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005715 } else {
5716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5717 lys_nodetype2str(target->nodetype), "add", "min-elements");
5718 goto cleanup;
5719 }
5720 if (d->min) {
5721 target->flags |= LYS_MAND_TRUE;
5722 }
5723 }
5724
5725 /* [max-elements-stmt] */
5726 if (d->flags & LYS_SET_MAX) {
5727 if (target->nodetype == LYS_LEAFLIST) {
5728 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5729 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005730 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005731 } else if (target->nodetype == LYS_LIST) {
5732 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5733 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005734 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005735 } else {
5736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5737 lys_nodetype2str(target->nodetype), "add", "max-elements");
5738 goto cleanup;
5739 }
5740 }
5741
5742 ret = LY_SUCCESS;
5743
5744cleanup:
5745 return ret;
5746}
5747
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005748static LY_ERR
5749lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt)
5750{
5751 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005752 ly_bool dyn = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005753 const char *orig_dflt;
5754 uint32_t i;
5755
5756 if (target->module != ctx->mod) {
5757 /* foreign deviation */
5758 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt);
5759
5760 /* check that the value matches */
5761 orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5762 if (strcmp(orig_dflt, dflt)) {
5763 goto error;
5764 }
5765 if (dyn) {
5766 free((char *)orig_dflt);
5767 }
5768
5769 /* remove the default specification */
5770 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5771 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5772 free(leaf->dflt);
5773 leaf->dflt = NULL;
5774 } else {
5775 /* local deviation */
5776 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt);
5777
5778 /* find the incomplete default */
5779 orig_dflt = NULL;
5780 for (i = 0; i < ctx->dflts.count; ++i) {
5781 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
5782 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5783 break;
5784 }
5785 }
5786 LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT);
5787
5788 /* check that the value matches */
5789 if (strcmp(orig_dflt, dflt)) {
5790 goto error;
5791 }
5792
5793 /* update the list of incomplete default values */
5794 lysc_incomplete_dflt_remove(ctx, target);
5795 }
5796
5797 target->flags &= ~LYS_SET_DFLT;
5798 return LY_SUCCESS;
5799
5800error:
5801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5802 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5803 dflt, orig_dflt);
5804 if (dyn) {
5805 free((char *)orig_dflt);
5806 }
5807cleanup:
5808 return LY_EVALID;
5809}
5810
5811static LY_ERR
5812lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts)
5813{
5814 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005815 ly_bool dyn = 0, found;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005816 const char *orig_dflt, **orig_dflts;
5817 uint32_t i;
5818 LY_ARRAY_COUNT_TYPE x, y;
5819
5820 if (target->module != ctx->mod) {
5821 /* foreign deviation */
5822 DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]);
5823 LY_ARRAY_FOR(dflts, x) {
5824 found = 0;
5825 LY_ARRAY_FOR(llist->dflts, y) {
5826 orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5827 if (!strcmp(orig_dflt, dflts[x])) {
5828 if (dyn) {
5829 free((char *)orig_dflt);
5830 }
5831 found = 1;
5832 break;
5833 }
5834 if (dyn) {
5835 free((char *)orig_dflt);
5836 }
5837 }
5838 if (!found) {
5839 goto error;
5840 }
5841
5842 /* update compiled default values */
5843 LY_ARRAY_DECREMENT(llist->dflts);
5844 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
5845 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
5846 free(llist->dflts[y]);
5847 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
5848 }
5849 if (!LY_ARRAY_COUNT(llist->dflts)) {
5850 LY_ARRAY_FREE(llist->dflts);
5851 llist->dflts = NULL;
5852 llist->flags &= ~LYS_SET_DFLT;
5853 }
5854 } else {
5855 /* local deviation */
5856 orig_dflt = NULL;
5857 orig_dflts = NULL;
5858 for (i = 0; i < ctx->dflts.count; ++i) {
5859 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) {
5860 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5861 orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts;
5862 break;
5863 }
5864 }
5865 LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT);
5866
5867 if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) {
5868 /* TODO it is not currently possible to remove just one default value from incomplete defaults array */
5869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5870 "Local deviation deleting leaf-list defaults is not supported.");
5871 return LY_EVALID;
5872 }
5873
5874 LY_ARRAY_FOR(dflts, x) {
5875 found = 0;
5876 if (orig_dflts) {
5877 LY_ARRAY_FOR(orig_dflts, y) {
5878 if (!strcmp(orig_dflts[y], dflts[x])) {
5879 found = 1;
5880 break;
5881 }
5882 }
5883 } else if (!strcmp(orig_dflt, dflts[x])) {
5884 found = 1;
5885 }
5886 if (!found) {
5887 goto error;
5888 }
5889
5890 /* update the list of incomplete default values */
5891 lysc_incomplete_dflt_remove(ctx, target);
5892 }
5893
5894 llist->flags &= ~LYS_SET_DFLT;
5895 }
5896
5897 return LY_SUCCESS;
5898
5899error:
5900 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
5901 "which does not match any of the target's property values.", dflts[x]);
5902cleanup:
5903 return LY_EVALID;
5904}
5905
Michal Vaskoccc062a2020-08-13 08:34:50 +02005906/**
5907 * @brief Apply deviate delete.
5908 *
5909 * @param[in] ctx Compile context.
5910 * @param[in] target Deviation target.
5911 * @param[in] dev_flags Internal deviation flags.
5912 * @param[in] d Deviate delete to apply.
5913 * @return LY_ERR value.
5914 */
5915static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005916lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysc_node *target, uint32_t dev_flags, struct lysp_deviate_del *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02005917{
5918 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005919 struct lysc_node_list *list = (struct lysc_node_list *)target;
5920 LY_ARRAY_COUNT_TYPE x, y, z;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005921 size_t prefix_len, name_len;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005922 const char *prefix, *name, *nodeid;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005923 struct lys_module *mod;
5924
5925#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5926 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5927 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5928 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5929 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5930 } \
5931 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5933 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5934 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5935 goto cleanup; \
5936 } \
5937 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5938 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5939 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5940 &((TYPE)target)->ARRAY_TRG[y + 1], \
5941 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5942 } \
5943 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5944 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5945 ((TYPE)target)->ARRAY_TRG = NULL; \
5946 }
5947
5948 /* [units-stmt] */
5949 if (d->units) {
5950 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Michal Vasko22df3f02020-08-24 13:29:22 +02005951 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units);
5952 if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005953 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5954 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02005955 d->units, ((struct lysc_node_leaf *)target)->units);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005956 goto cleanup;
5957 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005958 lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5959 ((struct lysc_node_leaf *)target)->units = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005960 }
5961
5962 /* *must-stmt */
5963 if (d->musts) {
5964 switch (target->nodetype) {
5965 case LYS_CONTAINER:
5966 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005967 DEV_DEL_ARRAY(struct lysc_node_container *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005968 break;
5969 case LYS_LEAF:
5970 case LYS_LEAFLIST:
5971 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005972 DEV_DEL_ARRAY(struct lysc_node_leaf *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005973 break;
5974 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005975 DEV_DEL_ARRAY(struct lysc_notif *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005976 break;
5977 case LYS_RPC:
5978 case LYS_ACTION:
5979 if (dev_flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005980 DEV_DEL_ARRAY(struct lysc_action *, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005981 break;
5982 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005983 DEV_DEL_ARRAY(struct lysc_action *, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005984 break;
5985 }
Radek Krejci0f969882020-08-21 16:56:47 +02005986 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005987 default:
5988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5989 lys_nodetype2str(target->nodetype), "delete", "must");
5990 goto cleanup;
5991 }
5992 }
5993
5994 /* *unique-stmt */
5995 if (d->uniques) {
5996 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5997 LY_ARRAY_FOR(d->uniques, x) {
5998 LY_ARRAY_FOR(list->uniques, z) {
5999 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
6000 nodeid = strpbrk(name, " \t\n");
6001 if (nodeid) {
6002 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
6003 break;
6004 }
6005 while (isspace(*nodeid)) {
6006 ++nodeid;
6007 }
6008 } else {
6009 if (strcmp(name, list->uniques[z][y]->name)) {
6010 break;
6011 }
6012 }
6013 }
6014 if (!name) {
6015 /* complete match - remove the unique */
6016 LY_ARRAY_DECREMENT(list->uniques);
6017 LY_ARRAY_FREE(list->uniques[z]);
6018 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
6019 --z;
6020 break;
6021 }
6022 }
6023 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
6024 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6025 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6026 d->uniques[x]);
6027 goto cleanup;
6028 }
6029 }
6030 if (!LY_ARRAY_COUNT(list->uniques)) {
6031 LY_ARRAY_FREE(list->uniques);
6032 list->uniques = NULL;
6033 }
6034 }
6035
6036 /* *default-stmt */
6037 if (d->dflts) {
6038 switch (target->nodetype) {
6039 case LYS_LEAF:
6040 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006041 LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006042 break;
6043 case LYS_LEAFLIST:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006044 LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006045 break;
6046 case LYS_CHOICE:
6047 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vasko22df3f02020-08-24 13:29:22 +02006048 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "deleting", "default", d->dflts[0]);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006049 nodeid = d->dflts[0];
6050 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6051 if (prefix) {
6052 /* use module prefixes from the deviation module to match the module of the default case */
6053 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006054 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006055 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6056 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6057 goto cleanup;
6058 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006059 if (mod != ((struct lysc_node_choice *)target)->dflt->module) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006060 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006061 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6062 "The prefix does not match the default case's module.", d->dflts[0]);
6063 goto cleanup;
6064 }
6065 }
6066 /* else {
6067 * strictly, the default prefix would point to the deviation module, but the value should actually
6068 * match the default string in the original module (usually unprefixed), so in this case we do not check
6069 * the module of the default case, just matching its name */
Michal Vasko22df3f02020-08-24 13:29:22 +02006070 if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02006071 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6072 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02006073 d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006074 goto cleanup;
6075 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006076 ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT;
6077 ((struct lysc_node_choice *)target)->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006078 break;
6079 default:
6080 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6081 lys_nodetype2str(target->nodetype), "delete", "default");
6082 goto cleanup;
6083 }
6084 }
6085
6086 ret = LY_SUCCESS;
6087
6088cleanup:
6089 return ret;
6090}
6091
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006092static LY_ERR
6093lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target)
6094{
6095 LY_ERR ret;
6096 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6097 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6098 struct ly_err_item *err = NULL;
6099 LY_ARRAY_COUNT_TYPE x;
6100 const char *dflt;
Radek Krejci857189e2020-09-01 13:26:36 +02006101 ly_bool dyn;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006102
6103 if (target->module != ctx->mod) {
6104 /* foreign deviation */
6105 if (target->nodetype == LYS_LEAF) {
6106 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn);
6107 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6108 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6109
6110 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6111 LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err);
6112 if (dyn) {
6113 free((char *)dflt);
6114 }
6115 if (err) {
6116 ly_err_print(err);
6117 ctx->path[0] = '\0';
6118 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6120 "Invalid default - value does not fit the type (%s).", err->msg);
6121 ly_err_free(err);
6122 }
6123 LY_CHECK_RET(ret);
6124
6125 ++leaf->dflt->realtype->refcount;
6126 } else { /* LY_LEAFLIST */
6127 LY_ARRAY_FOR(llist->dflts, x) {
6128 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn);
6129 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6130 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6131
6132 ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6133 LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err);
6134 if (dyn) {
6135 free((char *)dflt);
6136 }
6137 if (err) {
6138 ly_err_print(err);
6139 ctx->path[0] = '\0';
6140 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6141 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6142 "Invalid default - value does not fit the type (%s).", err->msg);
6143 ly_err_free(err);
6144 }
6145 LY_CHECK_RET(ret);
6146
6147 ++llist->dflts[x]->realtype->refcount;
6148 }
6149 }
6150 } else {
6151 /* local deviation */
6152
6153 /* these default were not compiled yet, so they will use the new type automatically */
6154 }
6155
6156 return LY_SUCCESS;
6157}
6158
Michal Vaskoccc062a2020-08-13 08:34:50 +02006159/**
6160 * @brief Apply deviate replace.
6161 *
6162 * @param[in] ctx Compile context.
6163 * @param[in] target Deviation target.
6164 * @param[in] d Deviate replace to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02006165 * @return LY_ERR value.
6166 */
6167static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006168lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02006169{
Radek Krejci011e4aa2020-09-04 15:22:31 +02006170 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006171 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6172 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6173 LY_ARRAY_COUNT_TYPE x;
6174
6175#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6176 if (!(((TYPE)target)->MEMBER COND)) { \
6177 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6178 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6179 goto cleanup; \
6180 }
6181
6182 /* [type-stmt] */
6183 if (d->type) {
6184 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6185 /* type is mandatory, so checking for its presence is not necessary */
6186 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6187
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006188 /* remove only default value inherited from the type */
6189 if (!(target->flags & LYS_SET_DFLT)) {
6190 if (target->module != ctx->mod) {
6191 /* foreign deviation - the target has default from the previous type, remove it */
6192 if (target->nodetype == LYS_LEAF) {
6193 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6194 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6195 free(leaf->dflt);
6196 leaf->dflt = NULL;
6197 } else { /* LYS_LEAFLIST */
6198 LY_ARRAY_FOR(llist->dflts, x) {
6199 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6200 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6201 free(llist->dflts[x]);
6202 }
6203 LY_ARRAY_FREE(llist->dflts);
6204 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006205 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006206 } else {
6207 /* local deviation */
6208 lysc_incomplete_dflt_remove(ctx, target);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006209 }
6210 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006211
6212 LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf));
6213
6214 if (target->flags & LYS_SET_DFLT) {
6215 /* the term default value(s) needs to be recompiled */
6216 LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006217 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006218 }
6219
6220 /* [units-stmt] */
6221 if (d->units) {
6222 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6223 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6224 units, "replacing", "units", d->units);
6225
6226 lydict_remove(ctx->ctx, leaf->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02006227 DUP_STRING(ctx->ctx, d->units, leaf->units, rc);
6228 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006229 }
6230
6231 /* [default-stmt] */
6232 if (d->dflt) {
6233 switch (target->nodetype) {
6234 case LYS_LEAF:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006235 if (target->module != ctx->mod) {
6236 /* foreign deviation */
6237 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing",
6238 "default", d->dflt);
6239
6240 /* remove the default specification */
6241 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6242 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6243 free(leaf->dflt);
6244 leaf->dflt = NULL;
6245 } else {
6246 /* local deviation */
6247 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing",
6248 "default", d->dflt);
6249 assert(!leaf->dflt);
6250 }
6251
6252 /* store the new default value */
6253 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006254 break;
6255 case LYS_CHOICE:
6256 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6257 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6258 goto cleanup;
6259 }
6260 break;
6261 default:
6262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6263 lys_nodetype2str(target->nodetype), "replace", "default");
6264 goto cleanup;
6265 }
6266 }
6267
6268 /* [config-stmt] */
6269 if (d->flags & LYS_CONFIG_MASK) {
6270 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6271 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6272 lys_nodetype2str(target->nodetype), "replace", "config");
6273 goto cleanup;
6274 }
6275 if (!(target->flags & LYS_SET_CONFIG)) {
6276 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6277 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6278 goto cleanup;
6279 }
6280 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6281 }
6282
6283 /* [mandatory-stmt] */
6284 if (d->flags & LYS_MAND_MASK) {
6285 if (!(target->flags & LYS_MAND_MASK)) {
6286 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6287 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6288 goto cleanup;
6289 }
6290 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6291 }
6292
6293 /* [min-elements-stmt] */
6294 if (d->flags & LYS_SET_MIN) {
6295 if (target->nodetype == LYS_LEAFLIST) {
6296 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6297 /* change value */
6298 ((struct lysc_node_leaflist *)target)->min = d->min;
6299 } else if (target->nodetype == LYS_LIST) {
6300 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6301 /* change value */
6302 ((struct lysc_node_list *)target)->min = d->min;
6303 } else {
6304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6305 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6306 goto cleanup;
6307 }
6308 if (d->min) {
6309 target->flags |= LYS_MAND_TRUE;
6310 }
6311 }
6312
6313 /* [max-elements-stmt] */
6314 if (d->flags & LYS_SET_MAX) {
6315 if (target->nodetype == LYS_LEAFLIST) {
6316 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6317 /* change value */
6318 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6319 } else if (target->nodetype == LYS_LIST) {
6320 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6321 /* change value */
6322 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6323 } else {
6324 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6325 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6326 goto cleanup;
6327 }
6328 }
6329
6330 ret = LY_SUCCESS;
6331
6332cleanup:
6333 return ret;
6334}
6335
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006336/**
6337 * @brief Apply all deviations of one target node.
6338 *
6339 * @param[in] ctx Compile context.
6340 * @param[in] dev Deviation structure to apply.
6341 * @return LY_ERR value.
6342 */
6343static LY_ERR
6344lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006345{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006346 LY_ERR ret = LY_EVALID;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006347 struct lysc_node *target = dev->target;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006348 struct lysc_action *rpcs;
6349 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006350 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006351 LY_ARRAY_COUNT_TYPE v, x;
Radek Krejci551b12c2019-02-19 16:11:21 +01006352 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006353
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006354 lysc_update_path(ctx, NULL, dev->nodeid);
6355
6356 /* not-supported */
6357 if (dev->not_supported) {
6358 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
Radek Krejci0f969882020-08-21 16:56:47 +02006359 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE ") deviates on node \"%s\" since the node is not-supported.",
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006360 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6361 }
6362
6363#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6364 if (target->parent) { \
6365 ARRAY = (TYPE*)GETFUNC(target->parent); \
6366 } else { \
6367 ARRAY = target->module->compiled->ARRAY; \
6368 } \
6369 LY_ARRAY_FOR(ARRAY, x) { \
6370 if (&ARRAY[x] == (TYPE*)target) { break; } \
6371 } \
6372 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6373 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6374 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6375 LY_ARRAY_DECREMENT(ARRAY); \
6376 }
6377
6378 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6379 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6380 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006381 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6382 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6383 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6384 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006385 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6386 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006387 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6388 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6389 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6390 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006391 } else {
6392 /* remove RPC/action */
6393 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6394 }
6395 } else if (target->nodetype == LYS_NOTIF) {
6396 /* remove Notification */
6397 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6398 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02006399 if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) {
6400 /* remove the target node with its parent case node because it is the only node of the case */
6401 lysc_node_unlink(target->parent);
6402 lysc_node_free(ctx->ctx, target->parent);
6403 } else {
6404 /* remove the target node */
6405 lysc_node_unlink(target);
6406 lysc_node_free(ctx->ctx, target);
6407 }
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006408 }
6409
6410 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6411 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6412 return LY_SUCCESS;
6413 }
6414
6415 /* list of deviates (not-supported is not present in the list) */
6416 LY_ARRAY_FOR(dev->deviates, v) {
6417 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006418 switch (d->mod) {
6419 case LYS_DEV_ADD:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006420 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006421 break;
6422 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006423 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006424 break;
6425 case LYS_DEV_REPLACE:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006426 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006427 break;
6428 default:
6429 LOGINT(ctx->ctx);
6430 goto cleanup;
6431 }
6432 }
6433
6434 /* final check when all deviations of a single target node are applied */
6435
6436 /* check min-max compatibility */
6437 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006438 min = ((struct lysc_node_leaflist *)target)->min;
6439 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006440 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006441 min = ((struct lysc_node_list *)target)->min;
6442 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006443 } else {
6444 min = max = 0;
6445 }
6446 if (min > max) {
6447 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6448 "after deviation: min value %u is bigger than max value %u.", min, max);
6449 goto cleanup;
6450 }
6451
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006452 /* check mandatory - default compatibility */
6453 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6454 && (target->flags & LYS_MAND_TRUE)) {
6455 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6456 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6457 goto cleanup;
Michal Vasko22df3f02020-08-24 13:29:22 +02006458 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006459 && (target->flags & LYS_MAND_TRUE)) {
6460 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6461 goto cleanup;
6462 }
6463 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6464 /* mandatory node under a default case */
6465 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6466 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6467 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6468 goto cleanup;
6469 }
6470
6471 /* success */
6472 ret = LY_SUCCESS;
6473
6474cleanup:
6475 lysc_update_path(ctx, NULL, NULL);
6476 return ret;
6477}
6478
6479LY_ERR
6480lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6481{
6482 LY_ERR ret = LY_EVALID;
6483 struct ly_set devs_p = {0};
6484 struct ly_set targets = {0};
6485 struct lysc_node *target; /* target target of the deviation */
6486 struct lysp_deviation *dev;
6487 struct lysp_deviate *d, **dp_new;
6488 LY_ARRAY_COUNT_TYPE u, v;
6489 struct lysc_deviation **devs = NULL;
6490 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006491 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006492
6493 /* get all deviations from the module and all its submodules ... */
6494 LY_ARRAY_FOR(mod_p->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006495 LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST, NULL));
Radek Krejciccd20f12019-02-15 14:12:27 +01006496 }
6497 LY_ARRAY_FOR(mod_p->includes, v) {
6498 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006499 LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST, NULL));
Radek Krejciccd20f12019-02-15 14:12:27 +01006500 }
6501 }
6502 if (!devs_p.count) {
6503 /* nothing to do */
6504 return LY_SUCCESS;
6505 }
6506
Radek Krejci327de162019-06-14 12:52:07 +02006507 lysc_update_path(ctx, NULL, "{deviation}");
6508
Radek Krejciccd20f12019-02-15 14:12:27 +01006509 /* ... and group them by the target node */
6510 devs = calloc(devs_p.count, sizeof *devs);
6511 for (u = 0; u < devs_p.count; ++u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006512 uint32_t index;
6513
Radek Krejciccd20f12019-02-15 14:12:27 +01006514 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006515 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006516
6517 /* resolve the target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02006518 LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
Michal Vasko22df3f02020-08-24 13:29:22 +02006519 (const struct lysc_node **)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006520 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006521 /* move the target pointer to input/output to make them different from the action and
6522 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6523 * back to the RPC/action node due to a better compatibility and decision code in this function.
6524 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6525 if (flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006526 target = (struct lysc_node *)&((struct lysc_action *)target)->input;
Radek Krejcif538ce52019-03-05 10:46:14 +01006527 flags |= LYSC_OPT_INTERNAL;
6528 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006529 target = (struct lysc_node *)&((struct lysc_action *)target)->output;
Radek Krejcif538ce52019-03-05 10:46:14 +01006530 flags |= LYSC_OPT_INTERNAL;
6531 }
6532 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006533 /* insert into the set of targets with duplicity detection */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006534 ret = ly_set_add(&targets, target, 0, &index);
6535 LY_CHECK_GOTO(ret, cleanup);
6536 if (!devs[index]) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006537 /* new record */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006538 devs[index] = calloc(1, sizeof **devs);
6539 devs[index]->target = target;
6540 devs[index]->nodeid = dev->nodeid;
6541 devs[index]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006542 }
6543 /* add deviates into the deviation's list of deviates */
Michal Vasko5c38f362020-08-24 09:22:51 +02006544 LY_LIST_FOR(dev->deviates, d) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006545 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[index]->deviates, dp_new, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006546 *dp_new = d;
6547 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006548 devs[index]->not_supported = 1;
Radek Krejciccd20f12019-02-15 14:12:27 +01006549 }
6550 }
Radek Krejci327de162019-06-14 12:52:07 +02006551
6552 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006553 }
6554
Radek Krejciccd20f12019-02-15 14:12:27 +01006555 /* apply deviations */
6556 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006557 ly_bool match = 0;
Radek Krejciba03a5a2020-08-27 14:40:41 +02006558
Radek Krejcif538ce52019-03-05 10:46:14 +01006559 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6560 /* fix the target pointer in case of RPC's/action's input/output */
6561 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006562 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006563 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006564 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006565 }
6566 }
6567
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006568 /* remember target module (the target node may be removed) */
6569 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006570
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006571 /* apply the deviation */
6572 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006573
Michal Vaskoe6143202020-07-03 13:02:08 +02006574 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006575 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6576 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006577 match = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006578 break;
6579 }
6580 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02006581 if (!match) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006582 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006583 *dev_mod = mod_p->mod;
6584 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006585 }
6586
Radek Krejci327de162019-06-14 12:52:07 +02006587 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006588 ret = LY_SUCCESS;
6589
6590cleanup:
6591 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6592 LY_ARRAY_FREE(devs[u]->deviates);
6593 free(devs[u]);
6594 }
6595 free(devs);
6596 ly_set_erase(&targets, NULL);
6597 ly_set_erase(&devs_p, NULL);
6598
6599 return ret;
6600}
6601
Radek Krejcib56c7502019-02-13 14:19:54 +01006602/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006603 * @brief Compile the given YANG submodule into the main module.
6604 * @param[in] ctx Compile context
6605 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006606 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6607 */
6608LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006609lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006610{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006611 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006612 LY_ERR ret = LY_SUCCESS;
6613 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006614 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006615 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006616 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006617
Radek Krejci14915cc2020-09-14 17:28:13 +02006618 /* features are compiled directly into the compiled module structure,
6619 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6620 * The features compilation is finished in the main module (lys_compile()). */
6621 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->mod->features);
6622 LY_CHECK_GOTO(ret, error);
Radek Krejci0af46292019-01-11 16:02:31 +01006623
Michal Vasko33ff9422020-07-03 09:50:39 +02006624 if (!mainmod->mod->dis_identities) {
6625 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6626 LY_CHECK_GOTO(ret, error);
6627 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006628
Radek Krejci474f9b82019-07-24 11:36:37 +02006629 /* data nodes */
6630 LY_LIST_FOR(submod->data, node_p) {
6631 ret = lys_compile_node(ctx, node_p, NULL, 0);
6632 LY_CHECK_GOTO(ret, error);
6633 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006634
Radek Krejciec4da802019-05-02 13:02:41 +02006635 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6636 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006637
Radek Krejcid05cbd92018-12-05 14:26:40 +01006638error:
6639 return ret;
6640}
6641
Radek Krejci335332a2019-09-05 13:03:35 +02006642static void *
6643lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6644{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006645 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci335332a2019-09-05 13:03:35 +02006646 if (substmts[u].stmt == stmt) {
6647 return substmts[u].storage;
6648 }
6649 }
6650 return NULL;
6651}
6652
6653LY_ERR
6654lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6655{
6656 LY_ERR ret = LY_EVALID, r;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006657 LY_ARRAY_COUNT_TYPE u;
Radek Krejci335332a2019-09-05 13:03:35 +02006658 struct lysp_stmt *stmt;
6659 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006660
6661 /* check for invalid substatements */
6662 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006663 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6664 continue;
6665 }
Radek Krejci335332a2019-09-05 13:03:35 +02006666 for (u = 0; substmts[u].stmt; ++u) {
6667 if (substmts[u].stmt == stmt->kw) {
6668 break;
6669 }
6670 }
6671 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6673 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006674 goto cleanup;
6675 }
Radek Krejci335332a2019-09-05 13:03:35 +02006676 }
6677
Radek Krejciad5963b2019-09-06 16:03:05 +02006678 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6679
Radek Krejci335332a2019-09-05 13:03:35 +02006680 /* keep order of the processing the same as the order in the defined substmts,
6681 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6682 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006683 ly_bool stmt_present = 0;
Radek Krejciad5963b2019-09-06 16:03:05 +02006684
Radek Krejci335332a2019-09-05 13:03:35 +02006685 for (stmt = ext->child; stmt; stmt = stmt->next) {
6686 if (substmts[u].stmt != stmt->kw) {
6687 continue;
6688 }
6689
Radek Krejciad5963b2019-09-06 16:03:05 +02006690 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006691 if (substmts[u].storage) {
6692 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006693 case LY_STMT_STATUS:
6694 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6695 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6696 break;
6697 case LY_STMT_UNITS: {
6698 const char **units;
6699
6700 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6701 /* single item */
6702 if (*((const char **)substmts[u].storage)) {
6703 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6704 goto cleanup;
6705 }
6706 units = (const char **)substmts[u].storage;
6707 } else {
6708 /* sized array */
6709 const char ***units_array = (const char ***)substmts[u].storage;
6710 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6711 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02006712 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
6713 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +02006714 break;
6715 }
Radek Krejci335332a2019-09-05 13:03:35 +02006716 case LY_STMT_TYPE: {
6717 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6718 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6719
6720 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6721 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006722 if (*(struct lysc_type **)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6724 goto cleanup;
6725 }
6726 compiled = substmts[u].storage;
6727 } else {
6728 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006729 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006730 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006731 compiled = (void *)type;
Radek Krejci335332a2019-09-05 13:03:35 +02006732 }
6733
Radek Krejciad5963b2019-09-06 16:03:05 +02006734 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006735 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL,
6736 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006737 units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02006738 lysp_type_free(ctx->ctx, parsed);
6739 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006740 break;
6741 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006742 case LY_STMT_IF_FEATURE: {
6743 struct lysc_iffeature *iff = NULL;
6744
6745 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6746 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006747 if (((struct lysc_iffeature *)substmts[u].storage)->features) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6749 goto cleanup;
6750 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006751 iff = (struct lysc_iffeature *)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006752 } else {
6753 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006754 struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006755 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6756 }
6757 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6758 break;
6759 }
6760 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6761 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006762 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.",
6764 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006765 goto cleanup;
6766 }
6767 }
Radek Krejci335332a2019-09-05 13:03:35 +02006768 }
Radek Krejci335332a2019-09-05 13:03:35 +02006769
Radek Krejciad5963b2019-09-06 16:03:05 +02006770 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6772 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6773 goto cleanup;
6774 }
Radek Krejci335332a2019-09-05 13:03:35 +02006775 }
6776
6777 ret = LY_SUCCESS;
6778
6779cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006780 return ret;
6781}
6782
Michal Vasko175012e2019-11-06 15:49:14 +01006783/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006784 * @brief Check when for cyclic dependencies.
6785 * @param[in] set Set with all the referenced nodes.
6786 * @param[in] node Node whose "when" referenced nodes are in @p set.
6787 * @return LY_ERR value
6788 */
6789static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006790lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006791{
6792 struct lyxp_set tmp_set;
6793 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006794 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006795 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006796 struct lysc_when *when;
6797 LY_ERR ret = LY_SUCCESS;
6798
6799 memset(&tmp_set, 0, sizeof tmp_set);
6800
6801 /* prepare in_ctx of the set */
Michal Vaskod989ba02020-08-24 10:59:24 +02006802 for (i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006803 xp_scnode = &set->val.scnodes[i];
6804
Michal Vasko5c4e5892019-11-14 12:31:38 +01006805 if (xp_scnode->in_ctx != -1) {
6806 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006807 xp_scnode->in_ctx = 1;
6808 }
6809 }
6810
6811 for (i = 0; i < set->used; ++i) {
6812 xp_scnode = &set->val.scnodes[i];
6813 if (xp_scnode->in_ctx != 1) {
6814 /* already checked */
6815 continue;
6816 }
6817
Michal Vasko1bf09392020-03-27 12:38:10 +01006818 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006819 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006820 /* no when to check */
6821 xp_scnode->in_ctx = 0;
6822 continue;
6823 }
6824
6825 node = xp_scnode->scnode;
6826 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006827 LY_ARRAY_FOR(node->when, u) {
6828 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006829 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006830 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6831 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006832 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006833 goto cleanup;
6834 }
6835
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006836 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006837 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006838 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006839 /* try to find this node in our set */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006840 uint32_t idx;
6841 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006842 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LY_VCODE_CIRC_WHEN, node->name, set->val.scnodes[idx].scnode->name);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006843 ret = LY_EVALID;
6844 goto cleanup;
6845 }
6846
6847 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006848 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006849 } else {
6850 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006851 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006852 }
6853 }
6854
6855 /* merge this set into the global when set */
6856 lyxp_set_scnode_merge(set, &tmp_set);
6857 }
6858
6859 /* check when of non-data parents as well */
6860 node = node->parent;
6861 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6862
Michal Vasko251f56e2019-11-14 16:06:47 +01006863 /* this node when was checked (xp_scnode could have been reallocd) */
6864 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006865 }
6866
6867cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006868 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006869 return ret;
6870}
6871
6872/**
Michal Vasko175012e2019-11-06 15:49:14 +01006873 * @brief Check when/must expressions of a node on a compiled schema tree.
6874 * @param[in] ctx Compile context.
6875 * @param[in] node Node to check.
6876 * @return LY_ERR value
6877 */
6878static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006879lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006880{
Michal Vasko175012e2019-11-06 15:49:14 +01006881 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006882 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006883 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006884 uint32_t opts;
Radek Krejci857189e2020-09-01 13:26:36 +02006885 ly_bool input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006886 struct lysc_when **when = NULL;
6887 struct lysc_must *musts = NULL;
6888 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006889 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006890
6891 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006892 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006893 if (node->flags & LYS_CONFIG_R) {
6894 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6895 if (op) {
6896 /* we are actually in output */
6897 opts = LYXP_SCNODE_OUTPUT;
6898 }
6899 }
Michal Vasko175012e2019-11-06 15:49:14 +01006900
6901 switch (node->nodetype) {
6902 case LYS_CONTAINER:
6903 when = ((struct lysc_node_container *)node)->when;
6904 musts = ((struct lysc_node_container *)node)->musts;
6905 break;
6906 case LYS_CHOICE:
6907 when = ((struct lysc_node_choice *)node)->when;
6908 break;
6909 case LYS_LEAF:
6910 when = ((struct lysc_node_leaf *)node)->when;
6911 musts = ((struct lysc_node_leaf *)node)->musts;
6912 break;
6913 case LYS_LEAFLIST:
6914 when = ((struct lysc_node_leaflist *)node)->when;
6915 musts = ((struct lysc_node_leaflist *)node)->musts;
6916 break;
6917 case LYS_LIST:
6918 when = ((struct lysc_node_list *)node)->when;
6919 musts = ((struct lysc_node_list *)node)->musts;
6920 break;
6921 case LYS_ANYXML:
6922 case LYS_ANYDATA:
6923 when = ((struct lysc_node_anydata *)node)->when;
6924 musts = ((struct lysc_node_anydata *)node)->musts;
6925 break;
6926 case LYS_CASE:
6927 when = ((struct lysc_node_case *)node)->when;
6928 break;
6929 case LYS_NOTIF:
6930 musts = ((struct lysc_notif *)node)->musts;
6931 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006932 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006933 case LYS_ACTION:
6934 /* first process input musts */
6935 musts = ((struct lysc_action *)node)->input.musts;
6936 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006937 default:
6938 /* nothing to check */
6939 break;
6940 }
6941
Michal Vasko175012e2019-11-06 15:49:14 +01006942 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006943 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006944 ret = lyxp_atomize(when[u]->cond, LY_PREF_SCHEMA, when[u]->module, when[u]->context ? when[u]->context : node,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006945 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006946 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006947 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006948 goto cleanup;
6949 }
6950
Michal Vaskodc052f32019-11-07 11:11:38 +01006951 ctx->path[0] = '\0';
6952 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006953 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006954 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006955 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6956 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006957
Michal Vaskoecd62de2019-11-13 12:35:11 +01006958 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006959 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006960 schema->name);
6961 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006962
6963 /* check dummy node accessing */
6964 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006965 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006966 ret = LY_EVALID;
6967 goto cleanup;
6968 }
Michal Vasko175012e2019-11-06 15:49:14 +01006969 }
6970 }
6971
Michal Vaskoecd62de2019-11-13 12:35:11 +01006972 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006973 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006974 LY_CHECK_GOTO(ret, cleanup);
6975
Michal Vaskod3678892020-05-21 10:06:58 +02006976 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006977 }
6978
Michal Vasko5d8756a2019-11-07 15:21:00 +01006979check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006980 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006981 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006982 ret = lyxp_atomize(musts[u].cond, LY_PREF_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006983 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006984 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006985 goto cleanup;
6986 }
6987
Michal Vaskodc052f32019-11-07 11:11:38 +01006988 ctx->path[0] = '\0';
6989 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006990 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006991 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006992 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006993 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006994 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6995 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006996 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006997 }
6998 }
6999
Michal Vaskod3678892020-05-21 10:06:58 +02007000 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007001 }
7002
Michal Vasko1bf09392020-03-27 12:38:10 +01007003 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01007004 /* now check output musts */
7005 input_done = 1;
7006 musts = ((struct lysc_action *)node)->output.musts;
7007 opts = LYXP_SCNODE_OUTPUT;
7008 goto check_musts;
7009 }
7010
Michal Vasko175012e2019-11-06 15:49:14 +01007011cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007012 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007013 return ret;
7014}
7015
Michal Vasko8d544252020-03-02 10:19:52 +01007016static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007017lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
7018{
Michal Vasko6b26e742020-07-17 15:02:10 +02007019 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02007020 struct ly_path *p;
7021 struct lysc_type *type;
7022
7023 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7024
7025 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02007026 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
7027 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007028 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02007029
7030 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007031 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02007032 ly_path_free(node->module->ctx, p);
7033
7034 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7035 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7036 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
7037 lref->path->expr, lys_nodetype2str(target->nodetype));
7038 return LY_EVALID;
7039 }
7040
7041 /* check status */
7042 ctx->path[0] = '\0';
7043 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7044 ctx->path_len = strlen(ctx->path);
7045 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
7046 return LY_EVALID;
7047 }
7048 ctx->path_len = 1;
7049 ctx->path[1] = '\0';
7050
7051 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007052 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007053 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007054 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007055 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7056 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7057 return LY_EVALID;
7058 }
7059 }
7060
7061 /* store the target's type and check for circular chain of leafrefs */
7062 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7063 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7064 if (type == (struct lysc_type *)lref) {
7065 /* circular chain detected */
7066 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7067 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7068 return LY_EVALID;
7069 }
7070 }
7071
7072 /* check if leafref and its target are under common if-features */
7073 if (lys_compile_leafref_features_validate(node, target)) {
7074 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7075 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7076 " features applicable to the leafref itself.", lref->path->expr);
7077 return LY_EVALID;
7078 }
7079
7080 return LY_SUCCESS;
7081}
7082
7083static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007084lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7085{
7086 struct lysc_ext_instance *ext;
7087 struct lysp_ext_instance *ext_p = NULL;
7088 struct lysp_stmt *stmt;
7089 const struct lys_module *ext_mod;
7090 LY_ERR ret = LY_SUCCESS;
7091
7092 /* create the parsed extension instance manually */
7093 ext_p = calloc(1, sizeof *ext_p);
7094 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007095 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
7096 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007097 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7098 ext_p->insubstmt_index = 0;
7099
Radek Krejci87e25252020-09-15 13:28:31 +02007100 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
7101 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007102 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
7103 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007104 stmt->kw = LY_STMT_TYPE;
Michal Vasko8d544252020-03-02 10:19:52 +01007105
7106 /* allocate new extension instance */
7107 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7108
7109 /* manually get extension definition module */
7110 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7111
7112 /* compile the extension instance */
7113 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7114
7115cleanup:
7116 lysp_ext_instance_free(ctx->ctx, ext_p);
7117 free(ext_p);
7118 return ret;
7119}
7120
Michal Vasko004d3152020-06-11 19:59:22 +02007121static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007122lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +02007123 const struct lys_module *dflt_mod, struct lyd_value *storage)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007124{
7125 LY_ERR ret;
7126 struct ly_err_item *err = NULL;
7127
7128 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7129 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7130 if (err) {
7131 ly_err_print(err);
7132 ctx->path[0] = '\0';
7133 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7135 "Invalid default - value does not fit the type (%s).", err->msg);
7136 ly_err_free(err);
7137 }
7138 if (!ret) {
7139 ++storage->realtype->refcount;
7140 return LY_SUCCESS;
7141 }
7142 return ret;
7143}
7144
7145static LY_ERR
7146lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +02007147 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007148{
7149 LY_ERR ret;
7150
7151 assert(!leaf->dflt);
7152
7153 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7154 /* ignore default values for keys and mandatory leaves */
7155 return LY_SUCCESS;
7156 }
7157
7158 /* allocate the default value */
7159 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7160 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7161
7162 /* store the default value */
7163 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt);
7164 if (ret) {
7165 free(leaf->dflt);
7166 leaf->dflt = NULL;
7167 }
7168
7169 return ret;
7170}
7171
7172static LY_ERR
7173lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char *dflt, const char **dflts,
Radek Krejci0f969882020-08-21 16:56:47 +02007174 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007175{
7176 LY_ERR ret;
7177 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7178
7179 assert(dflt || dflts);
7180
7181 if (llist->dflts) {
7182 /* there were already some defaults and we are adding new by deviations */
7183 assert(dflts);
7184 orig_count = LY_ARRAY_COUNT(llist->dflts);
7185 } else {
7186 orig_count = 0;
7187 }
7188
7189 /* allocate new items */
7190 if (dflts) {
7191 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7192 } else {
7193 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7194 }
7195
7196 /* fill each new default value */
7197 if (dflts) {
7198 LY_ARRAY_FOR(dflts, u) {
7199 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
7200 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod,
7201 llist->dflts[orig_count + u]);
7202 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7203 LY_ARRAY_INCREMENT(llist->dflts);
7204 }
7205 } else {
7206 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
7207 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]);
7208 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7209 LY_ARRAY_INCREMENT(llist->dflts);
7210 }
7211
7212 /* check default value uniqueness */
7213 if (llist->flags & LYS_CONFIG_W) {
7214 /* configuration data values must be unique - so check the default values */
7215 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7216 for (v = 0; v < u; ++v) {
7217 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejci857189e2020-09-01 13:26:36 +02007218 ly_bool dynamic = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007219 const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic);
7220
7221 lysc_update_path(ctx, llist->parent, llist->name);
7222 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7223 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
7224 lysc_update_path(ctx, NULL, NULL);
7225 if (dynamic) {
7226 free((char *)val);
7227 }
7228 return LY_EVALID;
7229 }
7230 }
7231 }
7232 }
7233
7234 return LY_SUCCESS;
7235}
7236
7237static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007238lys_compile_unres(struct lysc_ctx *ctx)
7239{
7240 struct lysc_node *node;
7241 struct lysc_type *type, *typeiter;
7242 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007243 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007244 uint32_t i;
7245
7246 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7247 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7248 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7249 for (i = 0; i < ctx->leafrefs.count; ++i) {
7250 node = ctx->leafrefs.objs[i];
7251 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7252 type = ((struct lysc_node_leaf *)node)->type;
7253 if (type->basetype == LY_TYPE_LEAFREF) {
7254 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7255 } else if (type->basetype == LY_TYPE_UNION) {
7256 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7257 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7258 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7259 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7260 }
7261 }
7262 }
7263 }
7264 for (i = 0; i < ctx->leafrefs.count; ++i) {
7265 /* store pointer to the real type */
7266 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7267 if (type->basetype == LY_TYPE_LEAFREF) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007268 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007269 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007270 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7271 ((struct lysc_type_leafref *)type)->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007272 } else if (type->basetype == LY_TYPE_UNION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007273 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7274 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7275 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007276 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007277 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7278 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007279 }
7280 }
7281 }
7282 }
7283
7284 /* check xpath */
7285 for (i = 0; i < ctx->xpath.count; ++i) {
7286 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7287 }
7288
7289 /* finish incomplete default values compilation */
7290 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007291 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007292 if (r->leaf->nodetype == LYS_LEAF) {
7293 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod));
7294 } else {
7295 LY_CHECK_RET(lys_compile_unres_llist_dflts(ctx, r->llist, r->dflt, r->dflts, r->dflt_mod));
Michal Vasko004d3152020-06-11 19:59:22 +02007296 }
Michal Vasko004d3152020-06-11 19:59:22 +02007297 }
7298
7299 return LY_SUCCESS;
7300}
7301
Radek Krejci19a96102018-11-15 13:38:09 +01007302LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02007303lys_compile(struct lys_module *mod, uint32_t options)
Radek Krejci19a96102018-11-15 13:38:09 +01007304{
7305 struct lysc_ctx ctx = {0};
7306 struct lysc_module *mod_c;
7307 struct lysp_module *sp;
7308 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007309 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007310 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007311 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007312 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007313 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007314 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007315 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007316
Michal Vasko7a0b0762020-09-02 16:37:01 +02007317 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007318
Michal Vasko7a0b0762020-09-02 16:37:01 +02007319 if (!mod->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007320 /* just imported modules are not compiled */
7321 return LY_SUCCESS;
7322 }
7323
Michal Vasko7a0b0762020-09-02 16:37:01 +02007324 compile_id = ++mod->ctx->module_set_id;
7325 sp = mod->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007326
Michal Vasko7a0b0762020-09-02 16:37:01 +02007327 ctx.ctx = mod->ctx;
7328 ctx.mod = mod;
7329 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007330 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007331 ctx.path_len = 1;
7332 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007333
Michal Vasko7a0b0762020-09-02 16:37:01 +02007334 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
7335 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7336 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007337
Michal Vasko7c8439f2020-08-05 13:25:19 +02007338 LY_ARRAY_FOR(sp->imports, u) {
7339 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7340 }
Radek Krejci0935f412019-08-20 16:15:18 +02007341
Radek Krejci14915cc2020-09-14 17:28:13 +02007342 /* features precompilation */
7343 if (!mod->features && sp->features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007344 /* features are compiled directly into the compiled module structure,
7345 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci14915cc2020-09-14 17:28:13 +02007346 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007347 LY_CHECK_GOTO(ret, error);
Radek Krejci14915cc2020-09-14 17:28:13 +02007348 } /* else the features are already precompiled */
Michal Vasko33ff9422020-07-03 09:50:39 +02007349
Radek Krejci14915cc2020-09-14 17:28:13 +02007350 /* similarly, identities precompilation */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007351 if (mod->dis_identities) {
7352 mod_c->identities = mod->dis_identities;
7353 mod->dis_identities = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02007354 } else {
7355 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7356 LY_CHECK_GOTO(ret, error);
7357 }
Radek Krejci14915cc2020-09-14 17:28:13 +02007358
7359 /* compile submodules
7360 * - must be between features/identities precompilation and finishing their compilation to cover features/identities from
7361 * submodules */
7362 LY_ARRAY_FOR(sp->includes, u) {
7363 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
7364 }
7365
7366 /* finish feature compilation, not only for the main module, but also for the submodules.
7367 * Due to possible forward references, it must be done when all the features (including submodules)
7368 * are present. */
7369 LY_ARRAY_FOR(sp->features, u) {
7370 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod->features);
7371 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7372 }
7373 lysc_update_path(&ctx, NULL, "{submodule}");
7374 LY_ARRAY_FOR(sp->includes, v) {
7375 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7376 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
7377 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod->features);
7378 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7379 }
7380 lysc_update_path(&ctx, NULL, NULL);
7381 }
7382 lysc_update_path(&ctx, NULL, NULL);
7383
7384 /* identities, work similarly to features with the precompilation */
Radek Krejci19a96102018-11-15 13:38:09 +01007385 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007386 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007387 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007388 lysc_update_path(&ctx, NULL, "{submodule}");
7389 LY_ARRAY_FOR(sp->includes, v) {
7390 if (sp->includes[v].submodule->identities) {
7391 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7392 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7393 LY_CHECK_GOTO(ret, error);
7394 lysc_update_path(&ctx, NULL, NULL);
7395 }
7396 }
Radek Krejci327de162019-06-14 12:52:07 +02007397 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007398
Radek Krejci95710c92019-02-11 15:49:55 +01007399 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007400 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007401 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007402 }
Radek Krejci95710c92019-02-11 15:49:55 +01007403
Radek Krejciec4da802019-05-02 13:02:41 +02007404 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7405 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007406
Radek Krejci95710c92019-02-11 15:49:55 +01007407 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007408 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007409 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007410 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007411 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007412
Radek Krejci474f9b82019-07-24 11:36:37 +02007413 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007414 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007415
Radek Krejci0935f412019-08-20 16:15:18 +02007416 /* extension instances TODO cover extension instances from submodules */
7417 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007418
Michal Vasko004d3152020-06-11 19:59:22 +02007419 /* finish compilation for all unresolved items in the context */
7420 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007421
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007422 /* validate non-instantiated groupings from the parsed schema,
7423 * without it we would accept even the schemas with invalid grouping specification */
7424 ctx.options |= LYSC_OPT_GROUPING;
7425 LY_ARRAY_FOR(sp->groupings, u) {
7426 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007427 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007428 }
7429 }
7430 LY_LIST_FOR(sp->data, node_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007431 grps = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007432 LY_ARRAY_FOR(grps, u) {
7433 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007434 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007435 }
7436 }
7437 }
7438
Radek Krejci474f9b82019-07-24 11:36:37 +02007439 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7440 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7441 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7442 }
7443
Michal Vasko8d544252020-03-02 10:19:52 +01007444#if 0
7445 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7446 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7447 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7448 * the anotation definitions available in the internal schema structure. */
7449 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7450 if (lyp_add_ietf_netconf_annotations(mod)) {
7451 lys_free(mod, NULL, 1, 1);
7452 return NULL;
7453 }
7454 }
7455#endif
7456
7457 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007458 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
7459 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007460 }
7461
Radek Krejci1c0c3442019-07-23 16:08:47 +02007462 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007463 ly_set_erase(&ctx.xpath, NULL);
7464 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007465 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007466 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007467 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007468
Radek Krejciec4da802019-05-02 13:02:41 +02007469 if (ctx.options & LYSC_OPT_FREE_SP) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02007470 lysp_module_free(mod->parsed);
7471 mod->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007472 }
7473
Radek Krejciec4da802019-05-02 13:02:41 +02007474 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007475 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007476 for (i = 0; i < ctx.ctx->list.count; ++i) {
7477 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007478 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007479 m->implemented = 1;
7480 }
7481 }
7482 }
7483
Radek Krejci19a96102018-11-15 13:38:09 +01007484 return LY_SUCCESS;
7485
7486error:
Michal Vasko7a0b0762020-09-02 16:37:01 +02007487 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007488 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007489 ly_set_erase(&ctx.xpath, NULL);
7490 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007491 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007492 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007493 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007494 lysc_module_free(mod_c, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02007495 mod->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007496
Radek Krejcia46012b2020-08-12 15:41:04 +02007497 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7498 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7499 * processed and we are going to get back to them via stack, so freeing them is not a good idea. */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007500 for (i = 0; i < ctx.ctx->list.count; ++i) {
7501 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007502 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007503 /* revert features list to the precompiled state */
7504 lys_feature_precompile_revert(&ctx, m);
7505 /* mark module as imported-only / not-implemented */
7506 m->implemented = 0;
7507 /* free the compiled version of the module */
7508 lysc_module_free(m->compiled, NULL);
7509 m->compiled = NULL;
7510 }
7511 }
7512
Radek Krejci19a96102018-11-15 13:38:09 +01007513 return ret;
7514}