blob: 481889a342e82cd20cd3f997e2ef75be508c5fad [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 Krejci0af46292019-01-11 16:02:31 +0100468 struct lysc_feature *f, *flist;
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 Krejci0af46292019-01-11 16:02:31 +0100484 if (mod->implemented) {
485 /* module is implemented so there is already the compiled schema */
486 flist = mod->compiled->features;
487 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200488 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100489 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200490 LY_ARRAY_FOR(flist, u) {
491 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200492 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100493 return f;
494 }
495 }
496
497 return NULL;
498}
499
Michal Vasko8d544252020-03-02 10:19:52 +0100500/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100501 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
502 */
503static LY_ERR
504lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
505{
506 LY_ERR ret = LY_SUCCESS;
507
508 if (!ext_p->compiled) {
509 lysc_update_path(ctx, NULL, "{extension}");
510 lysc_update_path(ctx, NULL, ext_p->name);
511
512 /* compile the extension definition */
513 ext_p->compiled = calloc(1, sizeof **ext);
514 ext_p->compiled->refcount = 1;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200515 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
516 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100517 ext_p->compiled->module = (struct lys_module *)ext_mod;
518 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
519
520 lysc_update_path(ctx, NULL, NULL);
521 lysc_update_path(ctx, NULL, NULL);
522
523 /* find extension definition plugin */
524 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
525 }
526
527 *ext = lysc_ext_dup(ext_p->compiled);
528
529done:
530 return ret;
531}
532
533/**
Michal Vasko8d544252020-03-02 10:19:52 +0100534 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
535 *
536 * @param[in] ctx Compilation context.
537 * @param[in] ext_p Parsed extension instance.
538 * @param[in,out] ext Prepared compiled extension instance.
539 * @param[in] parent Extension instance parent.
540 * @param[in] parent_type Extension instance parent type.
541 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
542 */
Radek Krejci19a96102018-11-15 13:38:09 +0100543static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100544lys_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 +0200545 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100546{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200547 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100548 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200549 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200550 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200551 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100552
Radek Krejci011e4aa2020-09-04 15:22:31 +0200553 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
554 LY_CHECK_RET(ret);
555
Radek Krejci19a96102018-11-15 13:38:09 +0100556 ext->insubstmt = ext_p->insubstmt;
557 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200558 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200559 ext->parent = parent;
560 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100561
Michal Vasko22df3f02020-08-24 13:29:22 +0200562 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200563
Radek Krejci19a96102018-11-15 13:38:09 +0100564 /* get module where the extension definition should be placed */
Radek Krejci1e008d22020-08-17 11:37:37 +0200565 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
Radek Krejci7c960162019-09-18 14:16:12 +0200566 if (ext_p->yin) {
567 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
568 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
569 * YANG (import) prefix must be done here. */
570 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200571 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200572 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200573 } else {
574 assert(ctx->mod_def->parsed);
575 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
576 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200577 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200578 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 +0200579 ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200580 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200581 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200582 break;
583 }
584 }
585 }
586 if (!prefixed_name) {
587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
588 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200589 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200590 goto cleanup;
591 }
592 } else {
593 prefixed_name = ext_p->name;
594 }
595 lysc_update_path(ctx, NULL, prefixed_name);
596
Michal Vasko8d544252020-03-02 10:19:52 +0100597 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200598 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100599 if (!ext_mod) {
600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
601 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200602 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100603 goto cleanup;
604 } else if (!ext_mod->parsed->extensions) {
605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
606 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
607 prefixed_name, ext_mod->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200608 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100609 goto cleanup;
610 }
Radek Krejci7c960162019-09-18 14:16:12 +0200611 }
612 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200613
Michal Vasko5fe75f12020-03-02 13:52:37 +0100614 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200615 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
616 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100617 /* compile extension definition and assign it */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200618 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 +0100619 break;
620 }
621 }
Radek Krejci7c960162019-09-18 14:16:12 +0200622 if (!ext->def) {
623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
624 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200625 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200626 goto cleanup;
627 }
Radek Krejci0935f412019-08-20 16:15:18 +0200628
Radek Krejcif56e2a42019-09-09 14:15:25 +0200629 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
630 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
631 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200632 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200633 /* Schema was parsed from YIN and an argument is expected, ... */
634 struct lysp_stmt *stmt = NULL;
635
636 if (ext->def->flags & LYS_YINELEM_TRUE) {
637 /* ... argument was the first XML child element */
638 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
639 /* TODO check namespace of the statement */
640 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
641 stmt = ext_p->child;
642 }
643 }
644 } else {
645 /* ... argument was one of the XML attributes which are represented as child stmt
646 * with LYS_YIN_ATTR flag */
647 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
648 if (!strcmp(stmt->stmt, ext->def->argument)) {
649 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200650 break;
651 }
652 }
653 }
654 if (!stmt) {
655 /* missing extension's argument */
656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200657 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200658 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200659 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200660
661 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200662 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200663 stmt->flags |= LYS_YIN_ARGUMENT;
664 }
Radek Krejci7c960162019-09-18 14:16:12 +0200665 if (prefixed_name != ext_p->name) {
666 lydict_remove(ctx->ctx, ext_p->name);
667 ext_p->name = prefixed_name;
668 if (!ext_p->argument && ext->argument) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200669 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200670 }
671 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200672
Radek Krejci0935f412019-08-20 16:15:18 +0200673 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200674 if (ext->argument) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200675 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
Radek Krejciad5963b2019-09-06 16:03:05 +0200676 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200677 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200678 if (ext->argument) {
679 lysc_update_path(ctx, NULL, NULL);
680 }
Radek Krejci0935f412019-08-20 16:15:18 +0200681 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200682 ext_p->compiled = ext;
683
Radek Krejci7c960162019-09-18 14:16:12 +0200684cleanup:
685 if (prefixed_name && prefixed_name != ext_p->name) {
686 lydict_remove(ctx->ctx, prefixed_name);
687 }
688
Radek Krejcif56e2a42019-09-09 14:15:25 +0200689 lysc_update_path(ctx, NULL, NULL);
690 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200691
Radek Krejci7c960162019-09-18 14:16:12 +0200692 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200693}
694
695/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100696 * @brief Compile information from the if-feature statement
697 * @param[in] ctx Compile context.
698 * @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 +0100699 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
700 * @return LY_ERR value.
701 */
Radek Krejci19a96102018-11-15 13:38:09 +0100702static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200703lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100704{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200705 LY_ERR rc = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100706 const char *c = *value;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200707 int64_t i, j;
708 int8_t op_len, last_not = 0, checkversion = 0;
709 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci19a96102018-11-15 13:38:09 +0100710 uint8_t op;
711 struct iff_stack stack = {0, 0, NULL};
712 struct lysc_feature *f;
713
714 assert(c);
715
716 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200717 for (i = j = 0; c[i]; i++) {
Radek Krejci19a96102018-11-15 13:38:09 +0100718 if (c[i] == '(') {
719 j++;
720 checkversion = 1;
721 continue;
722 } else if (c[i] == ')') {
723 j--;
724 continue;
725 } else if (isspace(c[i])) {
726 checkversion = 1;
727 continue;
728 }
729
Radek Krejci1deb5be2020-08-26 16:43:36 +0200730 if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
731 uint64_t spaces;
732 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++);
733 if (c[i + op_len + spaces] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
735 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
736 return LY_EVALID;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200737 } else if (!isspace(c[i + op_len])) {
Radek Krejci19a96102018-11-15 13:38:09 +0100738 /* feature name starting with the not/and/or */
739 last_not = 0;
740 f_size++;
741 } else if (c[i] == 'n') { /* not operation */
742 if (last_not) {
743 /* double not */
744 expr_size = expr_size - 2;
745 last_not = 0;
746 } else {
747 last_not = 1;
748 }
749 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200750 if (f_exp != f_size) {
751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200752 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, op_len, &c[i]);
Radek Krejci6788abc2019-06-14 13:56:49 +0200753 return LY_EVALID;
754 }
Radek Krejci19a96102018-11-15 13:38:09 +0100755 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200756
Radek Krejci19a96102018-11-15 13:38:09 +0100757 /* not a not operation */
758 last_not = 0;
759 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200760 i += op_len;
Radek Krejci19a96102018-11-15 13:38:09 +0100761 } else {
762 f_size++;
763 last_not = 0;
764 }
765 expr_size++;
766
767 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200768 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100769 i--;
770 break;
771 }
772 i++;
773 }
774 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200775 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100776 /* not matching count of ( and ) */
777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
778 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
779 return LY_EVALID;
780 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200781 if (f_exp != f_size) {
782 /* features do not match the needed arguments for the logical operations */
783 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
784 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
785 "the required number of operands for the operations.", *value);
786 return LY_EVALID;
787 }
Radek Krejci19a96102018-11-15 13:38:09 +0100788
789 if (checkversion || expr_size > 1) {
790 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
793 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
794 return LY_EVALID;
795 }
796 }
797
798 /* allocate the memory */
799 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
800 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
801 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200802 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
Radek Krejci19a96102018-11-15 13:38:09 +0100803
804 stack.size = expr_size;
805 f_size--; expr_size--; /* used as indexes from now */
806
807 for (i--; i >= 0; i--) {
808 if (c[i] == ')') {
809 /* push it on stack */
810 iff_stack_push(&stack, LYS_IFF_RP);
811 continue;
812 } else if (c[i] == '(') {
813 /* pop from the stack into result all operators until ) */
Michal Vaskod989ba02020-08-24 10:59:24 +0200814 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
Radek Krejci19a96102018-11-15 13:38:09 +0100815 iff_setop(iff->expr, op, expr_size--);
816 }
817 continue;
818 } else if (isspace(c[i])) {
819 continue;
820 }
821
822 /* end of operator or operand -> find beginning and get what is it */
823 j = i + 1;
824 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
825 i--;
826 }
827 i++; /* go back by one step */
828
829 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
830 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
831 /* double not */
832 iff_stack_pop(&stack);
833 } else {
834 /* not has the highest priority, so do not pop from the stack
835 * as in case of AND and OR */
836 iff_stack_push(&stack, LYS_IFF_NOT);
837 }
838 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
839 /* as for OR - pop from the stack all operators with the same or higher
840 * priority and store them to the result, then push the AND to the stack */
841 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
842 op = iff_stack_pop(&stack);
843 iff_setop(iff->expr, op, expr_size--);
844 }
845 iff_stack_push(&stack, LYS_IFF_AND);
846 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
847 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
848 op = iff_stack_pop(&stack);
849 iff_setop(iff->expr, op, expr_size--);
850 }
851 iff_stack_push(&stack, LYS_IFF_OR);
852 } else {
853 /* feature name, length is j - i */
854
855 /* add it to the expression */
856 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
857
858 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100859 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
860 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
861 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
862 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100863 iff->features[f_size] = f;
864 LY_ARRAY_INCREMENT(iff->features);
865 f_size--;
866 }
867 }
868 while (stack.index) {
869 op = iff_stack_pop(&stack);
870 iff_setop(iff->expr, op, expr_size--);
871 }
872
873 if (++expr_size || ++f_size) {
874 /* not all expected operators and operands found */
875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
876 "Invalid value \"%s\" of if-feature - processing error.", *value);
877 rc = LY_EINT;
878 } else {
879 rc = LY_SUCCESS;
880 }
881
882error:
883 /* cleanup */
884 iff_stack_clean(&stack);
885
886 return rc;
887}
888
Radek Krejcib56c7502019-02-13 14:19:54 +0100889/**
Michal Vasko175012e2019-11-06 15:49:14 +0100890 * @brief Get the XPath context node for the given schema node.
891 * @param[in] start The schema node where the XPath expression appears.
892 * @return The context node to evaluate XPath expression in given schema node.
893 * @return NULL in case the context node is the root node.
894 */
895static struct lysc_node *
896lysc_xpath_context(struct lysc_node *start)
897{
Michal Vasko1bf09392020-03-27 12:38:10 +0100898 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 +0200899 start = start->parent) {}
Michal Vasko175012e2019-11-06 15:49:14 +0100900 return start;
901}
902
903/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100904 * @brief Compile information from the when statement
905 * @param[in] ctx Compile context.
906 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100907 * @param[in] flags Flags of the node with the "when" defiition.
908 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100909 * @param[out] when Pointer where to store pointer to the created compiled when structure.
910 * @return LY_ERR value.
911 */
Radek Krejci19a96102018-11-15 13:38:09 +0100912static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100913lys_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 +0100914{
Radek Krejci19a96102018-11-15 13:38:09 +0100915 LY_ERR ret = LY_SUCCESS;
916
Radek Krejci00b874b2019-02-12 10:54:50 +0100917 *when = calloc(1, sizeof **when);
918 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200919 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200920 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100921 (*when)->context = lysc_xpath_context(node);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200922 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
923 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejci00b874b2019-02-12 10:54:50 +0100924 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200925 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100926 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100927
928done:
929 return ret;
930}
931
Radek Krejcib56c7502019-02-13 14:19:54 +0100932/**
933 * @brief Compile information from the must statement
934 * @param[in] ctx Compile context.
935 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100936 * @param[in,out] must Prepared (empty) compiled must structure to fill.
937 * @return LY_ERR value.
938 */
Radek Krejci19a96102018-11-15 13:38:09 +0100939static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200940lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100941{
Radek Krejci19a96102018-11-15 13:38:09 +0100942 LY_ERR ret = LY_SUCCESS;
943
Michal Vasko004d3152020-06-11 19:59:22 +0200944 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100945 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200946 must->module = ctx->mod_def;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200947 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
948 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
949 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
950 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200951 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100952
953done:
954 return ret;
955}
956
Radek Krejcib56c7502019-02-13 14:19:54 +0100957/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200958 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100959 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200960 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100961 * @return LY_ERR value.
962 */
Radek Krejci19a96102018-11-15 13:38:09 +0100963static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200964lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100965{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200966 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100967 LY_ERR ret = LY_SUCCESS;
968
Radek Krejci7f2a5362018-11-28 13:05:37 +0100969 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
970 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100971 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200972 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100973 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200974 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200975 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200976 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200977 LOGINT(ctx->ctx);
978 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200979 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 +0200980 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200981 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200982 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200983 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200984 mod = NULL;
985 }
Radek Krejci19a96102018-11-15 13:38:09 +0100986 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200987 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100988 }
989 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200990 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 +0100991 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 +0200992 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100993 return LY_ENOTFOUND;
994 }
995 }
Radek Krejci19a96102018-11-15 13:38:09 +0100996 }
997
Radek Krejci19a96102018-11-15 13:38:09 +0100998 return ret;
999}
1000
Michal Vasko33ff9422020-07-03 09:50:39 +02001001LY_ERR
1002lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001003 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +01001004{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001005 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +02001006 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01001007 LY_ERR ret = LY_SUCCESS;
1008
Michal Vasko33ff9422020-07-03 09:50:39 +02001009 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +02001010
Michal Vasko33ff9422020-07-03 09:50:39 +02001011 if (!ctx_sc) {
1012 context.ctx = ctx;
1013 context.mod = module;
Radek Krejci120d8542020-08-12 09:29:16 +02001014 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +02001015 context.path_len = 1;
1016 context.path[0] = '/';
1017 ctx_sc = &context;
1018 }
Radek Krejci19a96102018-11-15 13:38:09 +01001019
Michal Vasko33ff9422020-07-03 09:50:39 +02001020 if (!identities_p) {
1021 return LY_SUCCESS;
1022 }
1023 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001024 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02001025 }
1026
1027 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001028 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001029 LY_ARRAY_FOR(identities_p, u) {
1030 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
1031
1032 LY_ARRAY_INCREMENT(*identities);
1033 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001034 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
1035 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
1036 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
Michal Vasko33ff9422020-07-03 09:50:39 +02001037 (*identities)[offset + u].module = ctx_sc->mod;
1038 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
1039 lys_compile_iffeature, ret, done);
1040 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
1041 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
1042 LYEXT_PAR_IDENT, ret, done);
1043 (*identities)[offset + u].flags = identities_p[u].flags;
1044
1045 lysc_update_path(ctx_sc, NULL, NULL);
1046 }
1047 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001048done:
1049 return ret;
1050}
1051
Radek Krejcib56c7502019-02-13 14:19:54 +01001052/**
1053 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1054 *
1055 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1056 *
1057 * @param[in] ctx Compile context for logging.
1058 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1059 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1060 * @return LY_SUCCESS if everything is ok.
1061 * @return LY_EVALID if the identity is derived from itself.
1062 */
Radek Krejci38222632019-02-12 16:55:05 +01001063static LY_ERR
1064lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1065{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001066 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001067 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001068 struct ly_set recursion = {0};
1069 struct lysc_ident *drv;
1070
1071 if (!derived) {
1072 return LY_SUCCESS;
1073 }
1074
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001075 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001076 if (ident == derived[u]) {
1077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1078 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001079 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001080 goto cleanup;
1081 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001082 ret = ly_set_add(&recursion, derived[u], 0, NULL);
1083 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001084 }
1085
1086 for (v = 0; v < recursion.count; ++v) {
1087 drv = recursion.objs[v];
1088 if (!drv->derived) {
1089 continue;
1090 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001091 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001092 if (ident == drv->derived[u]) {
1093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1094 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001095 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001096 goto cleanup;
1097 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001098 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
1099 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001100 }
1101 }
Radek Krejci38222632019-02-12 16:55:05 +01001102
1103cleanup:
1104 ly_set_erase(&recursion, NULL);
1105 return ret;
1106}
1107
Radek Krejcia3045382018-11-22 14:30:31 +01001108/**
1109 * @brief Find and process the referenced base identities from another identity or identityref
1110 *
Radek Krejciaca74032019-06-04 08:53:06 +02001111 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001112 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1113 * to distinguish these two use cases.
1114 *
1115 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1116 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001117 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001118 * @param[in] bases Array of bases of identityref to fill in.
1119 * @return LY_ERR value.
1120 */
Radek Krejci19a96102018-11-15 13:38:09 +01001121static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001122lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001123 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001124{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001125 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001126 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001127 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001128 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001129
1130 assert(ident || bases);
1131
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001132 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1134 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1135 return LY_EVALID;
1136 }
1137
Michal Vasko33ff9422020-07-03 09:50:39 +02001138 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001139 s = strchr(bases_p[u], ':');
1140 if (s) {
1141 /* prefixed identity */
1142 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001143 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001144 } else {
1145 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001146 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001147 }
1148 if (!mod) {
1149 if (ident) {
1150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1151 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1152 } else {
1153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1154 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1155 }
1156 return LY_EVALID;
1157 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001158
Radek Krejci555cb5b2018-11-16 14:54:33 +01001159 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001160 if (mod->compiled) {
1161 identities = mod->compiled->identities;
1162 } else {
1163 identities = mod->dis_identities;
1164 }
1165 LY_ARRAY_FOR(identities, v) {
1166 if (!strcmp(name, identities[v].name)) {
1167 if (ident) {
1168 if (ident == &identities[v]) {
1169 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1170 "Identity \"%s\" is derived from itself.", ident->name);
1171 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001172 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001173 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1174 /* we have match! store the backlink */
1175 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1176 *idref = ident;
1177 } else {
1178 /* we have match! store the found identity */
1179 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1180 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001181 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001182 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001183 }
1184 }
1185 if (!idref || !(*idref)) {
1186 if (ident) {
1187 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1188 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1189 } else {
1190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1191 "Unable to find base (%s) of identityref.", bases_p[u]);
1192 }
1193 return LY_EVALID;
1194 }
1195 }
1196 return LY_SUCCESS;
1197}
1198
Radek Krejcia3045382018-11-22 14:30:31 +01001199/**
1200 * @brief For the given array of identities, set the backlinks from all their base identities.
1201 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1202 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1203 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1204 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1205 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001206static LY_ERR
1207lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1208{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001209 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001210
Michal Vasko33ff9422020-07-03 09:50:39 +02001211 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001212 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001213 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001214 continue;
1215 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001216 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001217 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 +02001218 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001219 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001220 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001221 return LY_SUCCESS;
1222}
1223
Radek Krejci0af46292019-01-11 16:02:31 +01001224LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001225lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001226 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001227{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001228 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001229 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001230 struct lysc_ctx context = {0};
1231
Radek Krejci327de162019-06-14 12:52:07 +02001232 assert(ctx_sc || ctx);
1233
1234 if (!ctx_sc) {
1235 context.ctx = ctx;
1236 context.mod = module;
1237 context.path_len = 1;
1238 context.path[0] = '/';
1239 ctx_sc = &context;
1240 }
Radek Krejci0af46292019-01-11 16:02:31 +01001241
1242 if (!features_p) {
1243 return LY_SUCCESS;
1244 }
1245 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001246 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001247 }
1248
Radek Krejci327de162019-06-14 12:52:07 +02001249 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001250 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001251 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001252 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1253
Radek Krejci0af46292019-01-11 16:02:31 +01001254 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001255 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001256 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
1257 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
1258 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001259 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001260 (*features)[offset + u].module = ctx_sc->mod;
1261
1262 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001263 }
Radek Krejci327de162019-06-14 12:52:07 +02001264 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001265
Radek Krejci011e4aa2020-09-04 15:22:31 +02001266done:
1267 return ret;
Radek Krejci0af46292019-01-11 16:02:31 +01001268}
1269
Radek Krejcia3045382018-11-22 14:30:31 +01001270/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001271 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001272 *
1273 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1274 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001275 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001276 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1277 * being currently processed).
1278 * @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 +01001279 * @return LY_SUCCESS if everything is ok.
1280 * @return LY_EVALID if the feature references indirectly itself.
1281 */
1282static LY_ERR
1283lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1284{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001285 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001286 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001287 struct ly_set recursion = {0};
1288 struct lysc_feature *drv;
1289
1290 if (!depfeatures) {
1291 return LY_SUCCESS;
1292 }
1293
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001294 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001295 if (feature == depfeatures[u]) {
1296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1297 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001298 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001299 goto cleanup;
1300 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001301 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
1302 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001303 }
1304
1305 for (v = 0; v < recursion.count; ++v) {
1306 drv = recursion.objs[v];
1307 if (!drv->depfeatures) {
1308 continue;
1309 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001310 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001311 if (feature == drv->depfeatures[u]) {
1312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1313 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001314 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001315 goto cleanup;
1316 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001317 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
1318 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001319 }
1320 }
Radek Krejci09a1fc52019-02-13 10:55:17 +01001321
1322cleanup:
1323 ly_set_erase(&recursion, NULL);
1324 return ret;
1325}
1326
1327/**
Radek Krejci0af46292019-01-11 16:02:31 +01001328 * @brief Create pre-compiled features array.
1329 *
1330 * See lys_feature_precompile() for more details.
1331 *
Radek Krejcia3045382018-11-22 14:30:31 +01001332 * @param[in] ctx Compile context.
1333 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001334 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001335 * @return LY_ERR value.
1336 */
Radek Krejci19a96102018-11-15 13:38:09 +01001337static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001338lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001339{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001340 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001341 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001342 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001343
Radek Krejci0af46292019-01-11 16:02:31 +01001344 /* find the preprecompiled feature */
1345 LY_ARRAY_FOR(features, x) {
1346 if (strcmp(features[x].name, feature_p->name)) {
1347 continue;
1348 }
1349 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001350 lysc_update_path(ctx, NULL, "{feature}");
1351 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001352
Radek Krejci0af46292019-01-11 16:02:31 +01001353 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001354 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001355 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001356 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001357 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001358 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001359 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001360 /* check for circular dependency - direct reference first,... */
1361 if (feature == feature->iffeatures[u].features[v]) {
1362 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1363 "Feature \"%s\" is referenced from itself.", feature->name);
1364 return LY_EVALID;
1365 }
1366 /* ... and indirect circular reference */
1367 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1368
Radek Krejci0af46292019-01-11 16:02:31 +01001369 /* add itself into the dependants list */
1370 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1371 *df = feature;
1372 }
Radek Krejci19a96102018-11-15 13:38:09 +01001373 }
Radek Krejci19a96102018-11-15 13:38:09 +01001374 }
1375 }
Radek Krejci327de162019-06-14 12:52:07 +02001376 lysc_update_path(ctx, NULL, NULL);
1377 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001378 done:
1379 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001380 }
Radek Krejci0af46292019-01-11 16:02:31 +01001381
1382 LOGINT(ctx->ctx);
1383 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001384}
1385
Radek Krejcib56c7502019-02-13 14:19:54 +01001386/**
1387 * @brief Revert compiled list of features back to the precompiled state.
1388 *
1389 * 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 +02001390 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001391 *
1392 * @param[in] ctx Compilation context.
1393 * @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 +02001394 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001395 */
Radek Krejci95710c92019-02-11 15:49:55 +01001396static void
1397lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1398{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001399 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001400
Radek Krejcia46012b2020-08-12 15:41:04 +02001401 if (mod->compiled) {
1402 /* keep the dis_features list until the complete lys_module is freed */
1403 mod->dis_features = mod->compiled->features;
1404 mod->compiled->features = NULL;
1405 }
Radek Krejci95710c92019-02-11 15:49:55 +01001406
Michal Vasko33ff9422020-07-03 09:50:39 +02001407 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001408 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001409 LY_ARRAY_FOR(mod->dis_features, u) {
1410 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1411 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001412 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001413 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1414 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001415
Michal Vasko33ff9422020-07-03 09:50:39 +02001416 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1417 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001418 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001419 LY_ARRAY_FREE(mod->dis_features[u].exts);
1420 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001421 }
1422}
1423
Radek Krejcia3045382018-11-22 14:30:31 +01001424/**
1425 * @brief Validate and normalize numeric value from a range definition.
1426 * @param[in] ctx Compile context.
1427 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1428 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1429 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1430 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1431 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1432 * @param[in] value String value of the range boundary.
1433 * @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.
1434 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1435 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1436 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001437LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001438range_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 +01001439{
Radek Krejci6cba4292018-11-15 17:33:29 +01001440 size_t fraction = 0, size;
1441
Radek Krejci19a96102018-11-15 13:38:09 +01001442 *len = 0;
1443
1444 assert(value);
1445 /* parse value */
1446 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1447 return LY_EVALID;
1448 }
1449
1450 if ((value[*len] == '-') || (value[*len] == '+')) {
1451 ++(*len);
1452 }
1453
1454 while (isdigit(value[*len])) {
1455 ++(*len);
1456 }
1457
1458 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001459 if (basetype == LY_TYPE_DEC64) {
1460 goto decimal;
1461 } else {
1462 *valcopy = strndup(value, *len);
1463 return LY_SUCCESS;
1464 }
Radek Krejci19a96102018-11-15 13:38:09 +01001465 }
1466 fraction = *len;
1467
1468 ++(*len);
1469 while (isdigit(value[*len])) {
1470 ++(*len);
1471 }
1472
Radek Krejci6cba4292018-11-15 17:33:29 +01001473 if (basetype == LY_TYPE_DEC64) {
1474decimal:
1475 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001476 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1478 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1479 *len, value, frdigits);
1480 return LY_EINVAL;
1481 }
1482 if (fraction) {
1483 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1484 } else {
1485 size = (*len) + frdigits + 1;
1486 }
1487 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001488 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1489
Radek Krejci6cba4292018-11-15 17:33:29 +01001490 (*valcopy)[size - 1] = '\0';
1491 if (fraction) {
1492 memcpy(&(*valcopy)[0], &value[0], fraction);
1493 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1494 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1495 } else {
1496 memcpy(&(*valcopy)[0], &value[0], *len);
1497 memset(&(*valcopy)[*len], '0', frdigits);
1498 }
Radek Krejci19a96102018-11-15 13:38:09 +01001499 }
1500 return LY_SUCCESS;
1501}
1502
Radek Krejcia3045382018-11-22 14:30:31 +01001503/**
1504 * @brief Check that values in range are in ascendant order.
1505 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001506 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1507 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001508 * @param[in] value Current value to check.
1509 * @param[in] prev_value The last seen value.
1510 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1511 */
Radek Krejci19a96102018-11-15 13:38:09 +01001512static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001513range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001514{
1515 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001516 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001517 return LY_EEXIST;
1518 }
1519 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001520 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001521 return LY_EEXIST;
1522 }
1523 }
1524 return LY_SUCCESS;
1525}
1526
Radek Krejcia3045382018-11-22 14:30:31 +01001527/**
1528 * @brief Set min/max value of the range part.
1529 * @param[in] ctx Compile context.
1530 * @param[in] part Range part structure to fill.
1531 * @param[in] max Flag to distinguish if storing min or max value.
1532 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1533 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1534 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1535 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1536 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001537 * @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 +01001538 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1539 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1540 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1541 * frdigits value), LY_EMEM.
1542 */
Radek Krejci19a96102018-11-15 13:38:09 +01001543static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001544range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
1545 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 +01001546{
1547 LY_ERR ret = LY_SUCCESS;
1548 char *valcopy = NULL;
1549 size_t len;
1550
1551 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001552 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001553 LY_CHECK_GOTO(ret, finalize);
1554 }
1555 if (!valcopy && base_range) {
1556 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001557 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001558 } else {
1559 part->min_64 = base_range->parts[0].min_64;
1560 }
1561 if (!first) {
1562 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1563 }
1564 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001565 }
1566
1567 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001568 case LY_TYPE_INT8: /* range */
1569 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001570 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 +01001571 } else if (max) {
1572 part->max_64 = INT64_C(127);
1573 } else {
1574 part->min_64 = INT64_C(-128);
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_INT16: /* range */
1581 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001582 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 +01001583 } else if (max) {
1584 part->max_64 = INT64_C(32767);
1585 } else {
1586 part->min_64 = INT64_C(-32768);
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_INT32: /* range */
1593 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001594 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 +01001595 } else if (max) {
1596 part->max_64 = INT64_C(2147483647);
1597 } else {
1598 part->min_64 = INT64_C(-2147483648);
1599 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001600 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001601 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001602 }
1603 break;
1604 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001605 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001606 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001607 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001608 max ? &part->max_64 : &part->min_64);
1609 } else if (max) {
1610 part->max_64 = INT64_C(9223372036854775807);
1611 } else {
1612 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
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(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001616 }
1617 break;
1618 case LY_TYPE_UINT8: /* range */
1619 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001620 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 +01001621 } else if (max) {
1622 part->max_u64 = UINT64_C(255);
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_UINT16: /* range */
1631 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001632 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 +01001633 } else if (max) {
1634 part->max_u64 = UINT64_C(65535);
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_UINT32: /* range */
1643 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001644 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 +01001645 } else if (max) {
1646 part->max_u64 = UINT64_C(4294967295);
1647 } else {
1648 part->min_u64 = UINT64_C(0);
1649 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001650 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001651 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001652 }
1653 break;
1654 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001655 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001656 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001657 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001658 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 +01001659 } else if (max) {
1660 part->max_u64 = UINT64_C(18446744073709551615);
1661 } else {
1662 part->min_u64 = UINT64_C(0);
1663 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001664 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001665 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001666 }
1667 break;
1668 default:
1669 LOGINT(ctx->ctx);
1670 ret = LY_EINT;
1671 }
1672
Radek Krejci5969f272018-11-23 10:03:58 +01001673finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001674 if (ret == LY_EDENIED) {
1675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1676 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1677 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1678 } else if (ret == LY_EVALID) {
1679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1680 "Invalid %s restriction - invalid value \"%s\".",
1681 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1682 } else if (ret == LY_EEXIST) {
1683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1684 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001685 length_restr ? "length" : "range",
Radek Krejci0f969882020-08-21 16:56:47 +02001686 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001687 } else if (!ret && value) {
1688 *value = *value + len;
1689 }
1690 free(valcopy);
1691 return ret;
1692}
1693
Radek Krejcia3045382018-11-22 14:30:31 +01001694/**
1695 * @brief Compile the parsed range restriction.
1696 * @param[in] ctx Compile context.
1697 * @param[in] range_p Parsed range structure to compile.
1698 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1699 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1700 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1701 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1702 * range restriction must be more restrictive than the base_range.
1703 * @param[in,out] range Pointer to the created current range structure.
1704 * @return LY_ERR value.
1705 */
Radek Krejci19a96102018-11-15 13:38:09 +01001706static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001707lys_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 +02001708 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001709{
1710 LY_ERR ret = LY_EVALID;
1711 const char *expr;
1712 struct lysc_range_part *parts = NULL, *part;
Radek Krejci857189e2020-09-01 13:26:36 +02001713 ly_bool range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001714 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001715
1716 assert(range);
1717 assert(range_p);
1718
1719 expr = range_p->arg;
Michal Vaskod989ba02020-08-24 10:59:24 +02001720 while (1) {
Radek Krejci19a96102018-11-15 13:38:09 +01001721 if (isspace(*expr)) {
1722 ++expr;
1723 } else if (*expr == '\0') {
1724 if (range_expected) {
1725 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1726 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1727 length_restr ? "length" : "range", range_p->arg);
1728 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001729 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1731 "Invalid %s restriction - unexpected end of the expression (%s).",
1732 length_restr ? "length" : "range", range_p->arg);
1733 goto cleanup;
1734 }
1735 parts_done++;
1736 break;
1737 } else if (!strncmp(expr, "min", 3)) {
1738 if (parts) {
1739 /* min cannot be used elsewhere than in the first part */
1740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1741 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1742 expr - range_p->arg, range_p->arg);
1743 goto cleanup;
1744 }
1745 expr += 3;
1746
1747 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001748 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 +01001749 part->max_64 = part->min_64;
1750 } else if (*expr == '|') {
1751 if (!parts || range_expected) {
1752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1753 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1754 goto cleanup;
1755 }
1756 expr++;
1757 parts_done++;
1758 /* process next part of the expression */
1759 } else if (!strncmp(expr, "..", 2)) {
1760 expr += 2;
1761 while (isspace(*expr)) {
1762 expr++;
1763 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001764 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1766 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1767 goto cleanup;
1768 }
1769 /* continue expecting the upper boundary */
1770 range_expected = 1;
1771 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1772 /* number */
1773 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001774 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001775 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 +01001776 range_expected = 0;
1777 } else {
1778 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001779 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 +01001780 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001781 part->max_64 = part->min_64;
1782 }
1783
1784 /* continue with possible another expression part */
1785 } else if (!strncmp(expr, "max", 3)) {
1786 expr += 3;
1787 while (isspace(*expr)) {
1788 expr++;
1789 }
1790 if (*expr != '\0') {
1791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1792 length_restr ? "length" : "range", expr);
1793 goto cleanup;
1794 }
1795 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001796 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001797 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 +01001798 range_expected = 0;
1799 } else {
1800 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001801 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 +01001802 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001803 part->min_64 = part->max_64;
1804 }
1805 } else {
1806 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1807 length_restr ? "length" : "range", expr);
1808 goto cleanup;
1809 }
1810 }
1811
1812 /* check with the previous range/length restriction */
1813 if (base_range) {
1814 switch (basetype) {
1815 case LY_TYPE_BINARY:
1816 case LY_TYPE_UINT8:
1817 case LY_TYPE_UINT16:
1818 case LY_TYPE_UINT32:
1819 case LY_TYPE_UINT64:
1820 case LY_TYPE_STRING:
1821 uns = 1;
1822 break;
1823 case LY_TYPE_DEC64:
1824 case LY_TYPE_INT8:
1825 case LY_TYPE_INT16:
1826 case LY_TYPE_INT32:
1827 case LY_TYPE_INT64:
1828 uns = 0;
1829 break;
1830 default:
1831 LOGINT(ctx->ctx);
1832 ret = LY_EINT;
1833 goto cleanup;
1834 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001835 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001836 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1837 goto baseerror;
1838 }
1839 /* current lower bound is not lower than the base */
1840 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1841 /* base has single value */
1842 if (base_range->parts[v].min_64 == parts[u].min_64) {
1843 /* both lower bounds are the same */
1844 if (parts[u].min_64 != parts[u].max_64) {
1845 /* current continues with a range */
1846 goto baseerror;
1847 } else {
1848 /* equal single values, move both forward */
1849 ++v;
1850 continue;
1851 }
1852 } else {
1853 /* base is single value lower than current range, so the
1854 * value from base range is removed in the current,
1855 * move only base and repeat checking */
1856 ++v;
1857 --u;
1858 continue;
1859 }
1860 } else {
1861 /* base is the range */
1862 if (parts[u].min_64 == parts[u].max_64) {
1863 /* current is a single value */
1864 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1865 /* current is behind the base range, so base range is omitted,
1866 * move the base and keep the current for further check */
1867 ++v;
1868 --u;
1869 } /* else it is within the base range, so move the current, but keep the base */
1870 continue;
1871 } else {
1872 /* both are ranges - check the higher bound, the lower was already checked */
1873 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1874 /* higher bound is higher than the current higher bound */
1875 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1876 /* but the current lower bound is also higher, so the base range is omitted,
1877 * continue with the same current, but move the base */
1878 --u;
1879 ++v;
1880 continue;
1881 }
1882 /* current range starts within the base range but end behind it */
1883 goto baseerror;
1884 } else {
1885 /* current range is smaller than the base,
1886 * move current, but stay with the base */
1887 continue;
1888 }
1889 }
1890 }
1891 }
1892 if (u != parts_done) {
1893baseerror:
1894 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1895 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1896 length_restr ? "length" : "range", range_p->arg);
1897 goto cleanup;
1898 }
1899 }
1900
1901 if (!(*range)) {
1902 *range = calloc(1, sizeof **range);
1903 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1904 }
1905
Radek Krejcic8b31002019-01-08 10:24:45 +01001906 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001907 if (range_p->eapptag) {
1908 lydict_remove(ctx->ctx, (*range)->eapptag);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001909 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001910 }
1911 if (range_p->emsg) {
1912 lydict_remove(ctx->ctx, (*range)->emsg);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001913 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001914 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001915 if (range_p->dsc) {
1916 lydict_remove(ctx->ctx, (*range)->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001917 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001918 }
1919 if (range_p->ref) {
1920 lydict_remove(ctx->ctx, (*range)->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001921 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001922 }
Radek Krejci19a96102018-11-15 13:38:09 +01001923 /* extensions are taken only from the last range by the caller */
1924
1925 (*range)->parts = parts;
1926 parts = NULL;
1927 ret = LY_SUCCESS;
1928cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001929 LY_ARRAY_FREE(parts);
1930
1931 return ret;
1932}
1933
1934/**
1935 * @brief Checks pattern syntax.
1936 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937 * @param[in] ctx Context.
1938 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001939 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001940 * @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 +01001941 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001942 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943LY_ERR
1944lys_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 +01001945{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001946 size_t idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001947 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001948 int err_code;
1949 const char *orig_ptr;
1950 PCRE2_SIZE err_offset;
1951 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001952#define URANGE_LEN 19
1953 char *ublock2urange[][2] = {
1954 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1955 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1956 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1957 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1958 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1959 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1960 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1961 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1962 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1963 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1964 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1965 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1966 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1967 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1968 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1969 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1970 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1971 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1972 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1973 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1974 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1975 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1976 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1977 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1978 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1979 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1980 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1981 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1982 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1983 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1984 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1985 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1986 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1987 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1988 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1989 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1990 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1991 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1992 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1993 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1994 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1995 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1996 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1997 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1998 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1999 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
2000 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
2001 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
2002 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
2003 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
2004 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
2005 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
2006 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
2007 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
2008 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
2009 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
2010 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
2011 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
2012 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
2013 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
2014 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
2015 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
2016 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
2017 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
2018 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
2019 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
2020 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
2021 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
2022 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
2023 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
2024 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
2025 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
2026 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
2027 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
2028 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
2029 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
2030 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
2031 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
2032 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
2033 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
2034 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
2035 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
2036 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
2037 {NULL, NULL}
2038 };
2039
2040 /* adjust the expression to a Perl equivalent
2041 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
2042
Michal Vasko40a00082020-05-27 15:20:01 +02002043 /* allocate space for the transformed pattern */
2044 size = strlen(pattern) + 1;
2045 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002047 perl_regex[0] = '\0';
2048
Michal Vasko40a00082020-05-27 15:20:01 +02002049 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
2050 brack = 0;
2051 idx = 0;
2052 orig_ptr = pattern;
2053 while (orig_ptr[0]) {
2054 switch (orig_ptr[0]) {
2055 case '$':
2056 case '^':
2057 if (!brack) {
2058 /* make space for the extra character */
2059 ++size;
2060 perl_regex = ly_realloc(perl_regex, size);
2061 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002062
Michal Vasko40a00082020-05-27 15:20:01 +02002063 /* print escape slash */
2064 perl_regex[idx] = '\\';
2065 ++idx;
2066 }
2067 break;
2068 case '[':
2069 /* must not be escaped */
2070 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2071 ++brack;
2072 }
2073 break;
2074 case ']':
2075 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2076 /* pattern was checked and compiled already */
2077 assert(brack);
2078 --brack;
2079 }
2080 break;
2081 default:
2082 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002083 }
Michal Vasko40a00082020-05-27 15:20:01 +02002084
2085 /* copy char */
2086 perl_regex[idx] = orig_ptr[0];
2087
2088 ++idx;
2089 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002090 }
Michal Vasko40a00082020-05-27 15:20:01 +02002091 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002092
2093 /* substitute Unicode Character Blocks with exact Character Ranges */
2094 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2095 start = ptr - perl_regex;
2096
2097 ptr = strchr(ptr, '}');
2098 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002100 pattern, perl_regex + start + 2, "unterminated character property");
2101 free(perl_regex);
2102 return LY_EVALID;
2103 }
2104 end = (ptr - perl_regex) + 1;
2105
2106 /* need more space */
2107 if (end - start < URANGE_LEN) {
2108 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002110 }
2111
2112 /* find our range */
2113 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2114 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2115 break;
2116 }
2117 }
2118 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002120 pattern, perl_regex + start + 5, "unknown block name");
2121 free(perl_regex);
2122 return LY_EVALID;
2123 }
2124
2125 /* 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 +02002126 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002127 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002128 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002129 }
2130 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002131 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002132 }
2133 }
Michal Vasko40a00082020-05-27 15:20:01 +02002134 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002135 /* skip brackets */
2136 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2137 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2138 } else {
2139 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2140 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2141 }
2142 }
2143
2144 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002145 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2146 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002147 &err_code, &err_offset, NULL);
2148 if (!code_local) {
2149 PCRE2_UCHAR err_msg[256] = {0};
2150 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002152 free(perl_regex);
2153 return LY_EVALID;
2154 }
2155 free(perl_regex);
2156
Radek Krejci54579462019-04-30 12:47:06 +02002157 if (code) {
2158 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002159 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002160 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002161 }
2162
2163 return LY_SUCCESS;
2164
2165#undef URANGE_LEN
2166}
2167
Radek Krejcia3045382018-11-22 14:30:31 +01002168/**
2169 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2170 * @param[in] ctx Compile context.
2171 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002172 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2173 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2174 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2175 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2176 */
Radek Krejci19a96102018-11-15 13:38:09 +01002177static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002178lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci0f969882020-08-21 16:56:47 +02002179 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
Radek Krejci19a96102018-11-15 13:38:09 +01002180{
2181 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002182 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002183 LY_ERR ret = LY_SUCCESS;
2184
2185 /* first, copy the patterns from the base type */
2186 if (base_patterns) {
2187 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2188 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2189 }
2190
2191 LY_ARRAY_FOR(patterns_p, u) {
2192 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2193 *pattern = calloc(1, sizeof **pattern);
2194 ++(*pattern)->refcount;
2195
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002197 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002198
2199 if (patterns_p[u].arg[0] == 0x15) {
2200 (*pattern)->inverted = 1;
2201 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002202 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done);
2203 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
2204 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
2205 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
2206 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002207 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002208 }
2209done:
2210 return ret;
2211}
2212
Radek Krejcia3045382018-11-22 14:30:31 +01002213/**
2214 * @brief map of the possible restrictions combination for the specific built-in type.
2215 */
Radek Krejci19a96102018-11-15 13:38:09 +01002216static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2217 0 /* LY_TYPE_UNKNOWN */,
2218 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002219 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2220 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2221 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2222 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2223 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002224 LYS_SET_BIT /* LY_TYPE_BITS */,
2225 0 /* LY_TYPE_BOOL */,
2226 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2227 0 /* LY_TYPE_EMPTY */,
2228 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2229 LYS_SET_BASE /* LY_TYPE_IDENT */,
2230 LYS_SET_REQINST /* LY_TYPE_INST */,
2231 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002232 LYS_SET_TYPE /* LY_TYPE_UNION */,
2233 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002234 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002235 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002236 LYS_SET_RANGE /* LY_TYPE_INT64 */
2237};
2238
2239/**
2240 * @brief stringification of the YANG built-in data types
2241 */
Michal Vasko22df3f02020-08-24 13:29:22 +02002242const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
Radek Krejci5969f272018-11-23 10:03:58 +01002243 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
Radek Krejci0f969882020-08-21 16:56:47 +02002244 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"};
Radek Krejci19a96102018-11-15 13:38:09 +01002245
Radek Krejcia3045382018-11-22 14:30:31 +01002246/**
2247 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2248 * @param[in] ctx Compile context.
2249 * @param[in] enums_p Array of the parsed enum structures to compile.
2250 * @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 +01002251 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2252 * @param[out] enums Newly created array of the compiled enums information for the current type.
2253 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2254 */
Radek Krejci19a96102018-11-15 13:38:09 +01002255static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002256lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci0f969882020-08-21 16:56:47 +02002257 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002258{
2259 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002260 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002261 int32_t value = 0;
2262 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002263 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002264
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002265 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2267 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2268 return LY_EVALID;
2269 }
2270
2271 LY_ARRAY_FOR(enums_p, u) {
2272 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002273 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
2274 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done);
2275 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Radek Krejci693262f2019-04-29 15:23:20 +02002276 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002277 if (base_enums) {
2278 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2279 LY_ARRAY_FOR(base_enums, v) {
2280 if (!strcmp(e->name, base_enums[v].name)) {
2281 break;
2282 }
2283 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002284 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2286 "Invalid %s - derived type adds new item \"%s\".",
2287 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2288 return LY_EVALID;
2289 }
2290 match = v;
2291 }
2292
2293 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002294 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002295 if (enums_p[u].flags & LYS_SET_VALUE) {
2296 e->value = (int32_t)enums_p[u].value;
2297 if (!u || e->value >= value) {
2298 value = e->value + 1;
2299 }
2300 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002301 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002302 if (e->value == (*enums)[v].value) {
2303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2304 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2305 e->value, e->name, (*enums)[v].name);
2306 return LY_EVALID;
2307 }
2308 }
2309 } else if (base_enums) {
2310 /* inherit the assigned value */
2311 e->value = base_enums[match].value;
2312 if (!u || e->value >= value) {
2313 value = e->value + 1;
2314 }
2315 } else {
2316 /* assign value automatically */
2317 if (u && value == INT32_MIN) {
2318 /* counter overflow */
2319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2320 "Invalid enumeration - it is not possible to auto-assign enum value for "
2321 "\"%s\" since the highest value is already 2147483647.", e->name);
2322 return LY_EVALID;
2323 }
2324 e->value = value++;
2325 }
2326 } else { /* LY_TYPE_BITS */
2327 if (enums_p[u].flags & LYS_SET_VALUE) {
2328 e->value = (int32_t)enums_p[u].value;
2329 if (!u || (uint32_t)e->value >= position) {
2330 position = (uint32_t)e->value + 1;
2331 }
2332 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002333 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002334 if (e->value == (*enums)[v].value) {
2335 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2336 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek Krejci0f969882020-08-21 16:56:47 +02002337 (uint32_t)e->value, e->name, (*enums)[v].name);
Radek Krejci19a96102018-11-15 13:38:09 +01002338 return LY_EVALID;
2339 }
2340 }
2341 } else if (base_enums) {
2342 /* inherit the assigned value */
2343 e->value = base_enums[match].value;
2344 if (!u || (uint32_t)e->value >= position) {
2345 position = (uint32_t)e->value + 1;
2346 }
2347 } else {
2348 /* assign value automatically */
2349 if (u && position == 0) {
2350 /* counter overflow */
2351 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2352 "Invalid bits - it is not possible to auto-assign bit position for "
2353 "\"%s\" since the highest value is already 4294967295.", e->name);
2354 return LY_EVALID;
2355 }
2356 e->value = position++;
2357 }
2358 }
2359
2360 if (base_enums) {
2361 /* the assigned values must not change from the derived type */
2362 if (e->value != base_enums[match].value) {
2363 if (basetype == LY_TYPE_ENUM) {
2364 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2365 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2366 e->name, base_enums[match].value, e->value);
2367 } else {
2368 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2369 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2370 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2371 }
2372 return LY_EVALID;
2373 }
2374 }
2375
Radek Krejciec4da802019-05-02 13:02:41 +02002376 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002377 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 +01002378
2379 if (basetype == LY_TYPE_BITS) {
2380 /* keep bits ordered by position */
Radek Krejci1e008d22020-08-17 11:37:37 +02002381 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
Radek Krejci19a96102018-11-15 13:38:09 +01002382 if (v != u) {
2383 memcpy(&storage, e, sizeof *e);
2384 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2385 memcpy(&(*enums)[v], &storage, sizeof storage);
2386 }
2387 }
2388 }
2389
2390done:
2391 return ret;
2392}
2393
Radek Krejcia3045382018-11-22 14:30:31 +01002394/**
2395 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2396 *
2397 * path-arg = absolute-path / relative-path
2398 * absolute-path = 1*("/" (node-identifier *path-predicate))
2399 * relative-path = 1*(".." "/") descendant-path
2400 *
2401 * @param[in,out] path Path to parse.
2402 * @param[out] prefix Prefix of the token, NULL if there is not any.
2403 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2404 * @param[out] name Name of the token.
2405 * @param[out] nam_len Length of the name.
2406 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2407 * must not be changed between consecutive calls. -1 if the
2408 * path is absolute.
2409 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2410 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2411 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002412LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002413lys_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 +02002414 int32_t *parent_times, ly_bool *has_predicate)
Radek Krejcia3045382018-11-22 14:30:31 +01002415{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002416 int32_t par_times = 0;
Radek Krejcia3045382018-11-22 14:30:31 +01002417
2418 assert(path && *path);
2419 assert(parent_times);
2420 assert(prefix);
2421 assert(prefix_len);
2422 assert(name);
2423 assert(name_len);
2424 assert(has_predicate);
2425
2426 *prefix = NULL;
2427 *prefix_len = 0;
2428 *name = NULL;
2429 *name_len = 0;
2430 *has_predicate = 0;
2431
2432 if (!*parent_times) {
2433 if (!strncmp(*path, "..", 2)) {
2434 *path += 2;
2435 ++par_times;
2436 while (!strncmp(*path, "/..", 3)) {
2437 *path += 3;
2438 ++par_times;
2439 }
2440 }
2441 if (par_times) {
2442 *parent_times = par_times;
2443 } else {
2444 *parent_times = -1;
2445 }
2446 }
2447
2448 if (**path != '/') {
2449 return LY_EINVAL;
2450 }
2451 /* skip '/' */
2452 ++(*path);
2453
2454 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002455 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002456
2457 if ((**path == '/' && (*path)[1]) || !**path) {
2458 /* path continues by another token or this is the last token */
2459 return LY_SUCCESS;
2460 } else if ((*path)[0] != '[') {
2461 /* unexpected character */
2462 return LY_EINVAL;
2463 } else {
2464 /* predicate starting with [ */
2465 *has_predicate = 1;
2466 return LY_SUCCESS;
2467 }
2468}
2469
2470/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002471 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2472 *
2473 * The set of features used for target must be a subset of features used for the leafref.
2474 * This is not a perfect, we should compare the truth tables but it could require too much resources
2475 * and RFC 7950 does not require it explicitely, so we simplify that.
2476 *
2477 * @param[in] refnode The leafref node.
2478 * @param[in] target Tha target node of the leafref.
2479 * @return LY_SUCCESS or LY_EVALID;
2480 */
2481static LY_ERR
2482lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2483{
2484 LY_ERR ret = LY_EVALID;
2485 const struct lysc_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002486 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci58d171e2018-11-23 13:50:55 +01002487 struct ly_set features = {0};
2488
2489 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002490 if (iter->iffeatures) {
2491 LY_ARRAY_FOR(iter->iffeatures, u) {
2492 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002493 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002494 }
2495 }
2496 }
2497 }
2498
2499 /* we should have, in features set, a superset of features applicable to the target node.
Radek Krejciba03a5a2020-08-27 14:40:41 +02002500 * If the feature is not present, we don;t have a subset of features applicable
Radek Krejci58d171e2018-11-23 13:50:55 +01002501 * to the leafref itself. */
Radek Krejci58d171e2018-11-23 13:50:55 +01002502 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002503 if (iter->iffeatures) {
2504 LY_ARRAY_FOR(iter->iffeatures, u) {
2505 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002506 if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
2507 /* feature not present */
Radek Krejci58d171e2018-11-23 13:50:55 +01002508 goto cleanup;
2509 }
2510 }
2511 }
2512 }
2513 }
2514 ret = LY_SUCCESS;
2515
2516cleanup:
2517 ly_set_erase(&features, NULL);
2518 return ret;
2519}
2520
Michal Vasko004d3152020-06-11 19:59:22 +02002521static 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 +02002522 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2523 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod);
Radek Krejcia3045382018-11-22 14:30:31 +01002524
Radek Krejcia3045382018-11-22 14:30:31 +01002525/**
2526 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2527 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002528 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2529 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2530 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2531 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002532 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002533 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002534 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002535 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2536 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2537 * @param[out] type Newly created type structure with the filled information about the type.
2538 * @return LY_ERR value.
2539 */
Radek Krejci19a96102018-11-15 13:38:09 +01002540static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002541lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002542 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2543 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2544 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002545{
2546 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002547 struct lysc_type_bin *bin;
2548 struct lysc_type_num *num;
2549 struct lysc_type_str *str;
2550 struct lysc_type_bits *bits;
2551 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002552 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002553 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002554 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002555 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002556
2557 switch (basetype) {
2558 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +02002559 bin = (struct lysc_type_bin *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002560
2561 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002562 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002563 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002564 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002565 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002566 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 +01002567 }
2568 }
2569
2570 if (tpdfname) {
2571 type_p->compiled = *type;
2572 *type = calloc(1, sizeof(struct lysc_type_bin));
2573 }
2574 break;
2575 case LY_TYPE_BITS:
2576 /* RFC 7950 9.7 - bits */
Michal Vasko22df3f02020-08-24 13:29:22 +02002577 bits = (struct lysc_type_bits *)(*type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002578 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002579 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002580 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
2581 (struct lysc_type_bitenum_item **)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002582 }
2583
Radek Krejci555cb5b2018-11-16 14:54:33 +01002584 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002585 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002586 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002588 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002590 free(*type);
2591 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002592 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002593 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002594 }
2595
2596 if (tpdfname) {
2597 type_p->compiled = *type;
2598 *type = calloc(1, sizeof(struct lysc_type_bits));
2599 }
2600 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002601 case LY_TYPE_DEC64:
Radek Krejci115a74d2020-08-14 22:18:12 +02002602 dec = (struct lysc_type_dec *)(*type);
Radek Krejci6cba4292018-11-15 17:33:29 +01002603
2604 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002605 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002606 if (!type_p->fraction_digits) {
2607 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002608 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002609 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002610 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002611 free(*type);
2612 *type = NULL;
2613 }
2614 return LY_EVALID;
2615 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002616 dec->fraction_digits = type_p->fraction_digits;
2617 } else {
2618 if (type_p->fraction_digits) {
2619 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
2620 if (tpdfname) {
2621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2622 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
2623 tpdfname);
2624 } else {
2625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2626 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
2627 free(*type);
2628 *type = NULL;
2629 }
2630 return LY_EVALID;
Radek Krejci6cba4292018-11-15 17:33:29 +01002631 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002632 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
Radek Krejci6cba4292018-11-15 17:33:29 +01002633 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002634
2635 /* RFC 7950 9.2.4 - range */
2636 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002637 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
Radek Krejci115a74d2020-08-14 22:18:12 +02002638 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002639 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002640 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 +01002641 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002642 }
2643
2644 if (tpdfname) {
2645 type_p->compiled = *type;
2646 *type = calloc(1, sizeof(struct lysc_type_dec));
2647 }
2648 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002649 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002650 str = (struct lysc_type_str *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002651
2652 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002653 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002654 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002655 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002656 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002657 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 +01002658 }
Michal Vasko22df3f02020-08-24 13:29:22 +02002659 } else if (base && ((struct lysc_type_str *)base)->length) {
2660 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002661 }
2662
2663 /* RFC 7950 9.4.5 - pattern */
2664 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002665 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Michal Vasko22df3f02020-08-24 13:29:22 +02002666 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
2667 } else if (base && ((struct lysc_type_str *)base)->patterns) {
2668 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002669 }
2670
2671 if (tpdfname) {
2672 type_p->compiled = *type;
2673 *type = calloc(1, sizeof(struct lysc_type_str));
2674 }
2675 break;
2676 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +02002677 enumeration = (struct lysc_type_enum *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002678
2679 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002680 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002681 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002682 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002683 }
2684
Radek Krejci555cb5b2018-11-16 14:54:33 +01002685 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002686 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002687 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002689 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002691 free(*type);
2692 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002693 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002694 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002695 }
2696
2697 if (tpdfname) {
2698 type_p->compiled = *type;
2699 *type = calloc(1, sizeof(struct lysc_type_enum));
2700 }
2701 break;
2702 case LY_TYPE_INT8:
2703 case LY_TYPE_UINT8:
2704 case LY_TYPE_INT16:
2705 case LY_TYPE_UINT16:
2706 case LY_TYPE_INT32:
2707 case LY_TYPE_UINT32:
2708 case LY_TYPE_INT64:
2709 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +02002710 num = (struct lysc_type_num *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002711
2712 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002713 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002714 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002715 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002716 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002717 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 +01002718 }
2719 }
2720
2721 if (tpdfname) {
2722 type_p->compiled = *type;
2723 *type = calloc(1, sizeof(struct lysc_type_num));
2724 }
2725 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002726 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002727 idref = (struct lysc_type_identityref *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002728
2729 /* RFC 7950 9.10.2 - base */
2730 if (type_p->bases) {
2731 if (base) {
2732 /* only the directly derived identityrefs can contain base specification */
2733 if (tpdfname) {
2734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002735 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002736 tpdfname);
2737 } else {
2738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002739 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002740 free(*type);
2741 *type = NULL;
2742 }
2743 return LY_EVALID;
2744 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002745 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002746 }
2747
2748 if (!base && !type_p->flags) {
2749 /* type derived from identityref built-in type must contain at least one base */
2750 if (tpdfname) {
2751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2752 } else {
2753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2754 free(*type);
2755 *type = NULL;
2756 }
2757 return LY_EVALID;
2758 }
2759
2760 if (tpdfname) {
2761 type_p->compiled = *type;
2762 *type = calloc(1, sizeof(struct lysc_type_identityref));
2763 }
2764 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002765 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002766 lref = (struct lysc_type_leafref *)*type;
Michal Vasko004d3152020-06-11 19:59:22 +02002767
Radek Krejcia3045382018-11-22 14:30:31 +01002768 /* RFC 7950 9.9.3 - require-instance */
2769 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002770 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002771 if (tpdfname) {
2772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2773 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2774 } else {
2775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2776 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2777 free(*type);
2778 *type = NULL;
2779 }
2780 return LY_EVALID;
2781 }
Michal Vasko004d3152020-06-11 19:59:22 +02002782 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002783 } else if (base) {
2784 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002785 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002786 } else {
2787 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002788 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002789 }
2790 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002791 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2792 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002793 } else if (base) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002794 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path);
2795 lref->path_context = ((struct lysc_type_leafref *)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002796 } else if (tpdfname) {
2797 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2798 return LY_EVALID;
2799 } else {
2800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2801 free(*type);
2802 *type = NULL;
2803 return LY_EVALID;
2804 }
2805 if (tpdfname) {
2806 type_p->compiled = *type;
2807 *type = calloc(1, sizeof(struct lysc_type_leafref));
2808 }
2809 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002810 case LY_TYPE_INST:
2811 /* RFC 7950 9.9.3 - require-instance */
2812 if (type_p->flags & LYS_SET_REQINST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002813 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
Radek Krejci16c0f822018-11-16 10:46:10 +01002814 } else {
2815 /* default is true */
Michal Vasko22df3f02020-08-24 13:29:22 +02002816 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
Radek Krejci16c0f822018-11-16 10:46:10 +01002817 }
2818
2819 if (tpdfname) {
2820 type_p->compiled = *type;
2821 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2822 }
2823 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002824 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +02002825 un = (struct lysc_type_union *)(*type);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002826
2827 /* RFC 7950 7.4 - type */
2828 if (type_p->types) {
2829 if (base) {
2830 /* only the directly derived union can contain types specification */
2831 if (tpdfname) {
2832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2833 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2834 tpdfname);
2835 } else {
2836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2837 "Invalid type substatement for the type not directly derived from union built-in type.");
2838 free(*type);
2839 *type = NULL;
2840 }
2841 return LY_EVALID;
2842 }
2843 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002844 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2845 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002846 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002847 &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002848 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2849 /* add space for additional types from the union subtype */
2850 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vasko22df3f02020-08-24 13:29:22 +02002851 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
2852 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002853
2854 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002855 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002856 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2857 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2858 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
Michal Vasko22df3f02020-08-24 13:29:22 +02002859 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 +02002860 lref = (struct lysc_type_leafref *)un->types[u + additional];
2861
2862 lref->basetype = LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02002863 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path);
Michal Vasko004d3152020-06-11 19:59:22 +02002864 lref->refcount = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +02002865 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
2866 lref->path_context = ((struct lysc_type_leafref *)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002867 /* TODO extensions */
2868
2869 } else {
2870 un->types[u + additional] = un_aux->types[v];
2871 ++un_aux->types[v]->refcount;
2872 }
2873 ++additional;
2874 LY_ARRAY_INCREMENT(un->types);
2875 }
2876 /* compensate u increment in main loop */
2877 --additional;
2878
2879 /* free the replaced union subtype */
Michal Vasko22df3f02020-08-24 13:29:22 +02002880 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002881 } else {
2882 LY_ARRAY_INCREMENT(un->types);
2883 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002884 }
2885 }
2886
2887 if (!base && !type_p->flags) {
2888 /* type derived from union built-in type must contain at least one type */
2889 if (tpdfname) {
2890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2891 } else {
2892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2893 free(*type);
2894 *type = NULL;
2895 }
2896 return LY_EVALID;
2897 }
2898
2899 if (tpdfname) {
2900 type_p->compiled = *type;
2901 *type = calloc(1, sizeof(struct lysc_type_union));
2902 }
2903 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002904 case LY_TYPE_BOOL:
2905 case LY_TYPE_EMPTY:
2906 case LY_TYPE_UNKNOWN: /* just to complete switch */
2907 break;
2908 }
2909 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2910done:
2911 return ret;
2912}
2913
Radek Krejcia3045382018-11-22 14:30:31 +01002914/**
2915 * @brief Compile information about the leaf/leaf-list's type.
2916 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002917 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2918 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2919 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2920 * @param[in] context_name Name of the context node or referencing typedef for logging.
2921 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002922 * @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 +01002923 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002924 * @param[out] dflt Default value for the type.
2925 * @param[out] dflt_mod Local module for the default value.
Radek Krejcia3045382018-11-22 14:30:31 +01002926 * @return LY_ERR value.
2927 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002928static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002929lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002930 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2931 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod)
Radek Krejci19a96102018-11-15 13:38:09 +01002932{
2933 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02002934 ly_bool dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002935 struct type_context {
2936 const struct lysp_tpdf *tpdf;
2937 struct lysp_node *node;
2938 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002939 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002940 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002941 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002942 struct ly_set tpdf_chain = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002943
2944 assert((dflt && dflt_mod) || (!dflt && !dflt_mod));
Radek Krejci19a96102018-11-15 13:38:09 +01002945
2946 (*type) = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002947 if (dflt) {
2948 *dflt = NULL;
2949 *dflt_mod = NULL;
2950 }
Radek Krejci19a96102018-11-15 13:38:09 +01002951
2952 tctx = calloc(1, sizeof *tctx);
2953 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002954 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002955 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2956 ret == LY_SUCCESS;
2957 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2958 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2959 if (basetype) {
2960 break;
2961 }
2962
2963 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002964 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2965 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci87e25252020-09-15 13:28:31 +02002966 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01002967
Radek Krejcicdfecd92018-11-26 11:27:32 +01002968 if (units && !*units) {
2969 /* inherit units */
Radek Krejci87e25252020-09-15 13:28:31 +02002970 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2971 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002972 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002973 if (dflt && !*dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002974 /* inherit default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002975 *dflt = tctx->tpdf->dflt;
2976 *dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002977 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002978 if (dummyloops && (!units || *units) && dflt && *dflt) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002979 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002980 break;
2981 }
2982
Radek Krejci19a96102018-11-15 13:38:09 +01002983 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002984 /* it is not necessary to continue, the rest of the chain was already compiled,
2985 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002986 basetype = tctx->tpdf->type.compiled->basetype;
Radek Krejciba03a5a2020-08-27 14:40:41 +02002987 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02002988 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejciba03a5a2020-08-27 14:40:41 +02002989
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002990 if ((units && !*units) || (dflt && !*dflt)) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002991 dummyloops = 1;
2992 goto preparenext;
2993 } else {
2994 tctx = NULL;
2995 break;
2996 }
Radek Krejci19a96102018-11-15 13:38:09 +01002997 }
2998
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002999 /* circular typedef reference detection */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003000 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003001 /* local part */
Michal Vasko22df3f02020-08-24 13:29:22 +02003002 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003003 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3004 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3005 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3006 free(tctx);
3007 ret = LY_EVALID;
3008 goto cleanup;
3009 }
3010 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003011 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003012 /* global part for unions corner case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003013 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003014 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3015 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3016 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3017 free(tctx);
3018 ret = LY_EVALID;
3019 goto cleanup;
3020 }
3021 }
3022
Radek Krejci19a96102018-11-15 13:38:09 +01003023 /* store information for the following processing */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003024 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02003025 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003026
Radek Krejcicdfecd92018-11-26 11:27:32 +01003027preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003028 /* prepare next loop */
3029 tctx_prev = tctx;
3030 tctx = calloc(1, sizeof *tctx);
3031 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3032 }
3033 free(tctx);
3034
3035 /* allocate type according to the basetype */
3036 switch (basetype) {
3037 case LY_TYPE_BINARY:
3038 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003039 break;
3040 case LY_TYPE_BITS:
3041 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003042 break;
3043 case LY_TYPE_BOOL:
3044 case LY_TYPE_EMPTY:
3045 *type = calloc(1, sizeof(struct lysc_type));
3046 break;
3047 case LY_TYPE_DEC64:
3048 *type = calloc(1, sizeof(struct lysc_type_dec));
3049 break;
3050 case LY_TYPE_ENUM:
3051 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003052 break;
3053 case LY_TYPE_IDENT:
3054 *type = calloc(1, sizeof(struct lysc_type_identityref));
3055 break;
3056 case LY_TYPE_INST:
3057 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3058 break;
3059 case LY_TYPE_LEAFREF:
3060 *type = calloc(1, sizeof(struct lysc_type_leafref));
3061 break;
3062 case LY_TYPE_STRING:
3063 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003064 break;
3065 case LY_TYPE_UNION:
3066 *type = calloc(1, sizeof(struct lysc_type_union));
3067 break;
3068 case LY_TYPE_INT8:
3069 case LY_TYPE_UINT8:
3070 case LY_TYPE_INT16:
3071 case LY_TYPE_UINT16:
3072 case LY_TYPE_INT32:
3073 case LY_TYPE_UINT32:
3074 case LY_TYPE_INT64:
3075 case LY_TYPE_UINT64:
3076 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003077 break;
3078 case LY_TYPE_UNKNOWN:
3079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3080 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3081 ret = LY_EVALID;
3082 goto cleanup;
3083 }
3084 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003085 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3087 ly_data_type2str[basetype]);
3088 free(*type);
3089 (*type) = NULL;
3090 ret = LY_EVALID;
3091 goto cleanup;
3092 }
3093
3094 /* get restrictions from the referred typedefs */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003095 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003096 tctx = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003097
3098 /* remember the typedef context for circular check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003099 ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
3100 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003101
Radek Krejci43699232018-11-23 14:59:46 +01003102 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003103 base = tctx->tpdf->type.compiled;
3104 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003105 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003106 /* no change, just use the type information from the base */
Michal Vasko22df3f02020-08-24 13:29:22 +02003107 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 +01003108 ++base->refcount;
3109 continue;
3110 }
3111
3112 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003113 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3115 tctx->tpdf->name, ly_data_type2str[basetype]);
3116 ret = LY_EVALID;
3117 goto cleanup;
3118 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3120 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3121 tctx->tpdf->name, tctx->tpdf->dflt);
3122 ret = LY_EVALID;
3123 goto cleanup;
3124 }
3125
Radek Krejci19a96102018-11-15 13:38:09 +01003126 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003127 /* TODO user type plugins */
3128 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003129 prev_type = *type;
Michal Vasko22df3f02020-08-24 13:29:22 +02003130 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 +01003131 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003132 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003133 LY_CHECK_GOTO(ret, cleanup);
3134 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003135 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003136 /* remove the processed typedef contexts from the stack for circular check */
3137 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003138
Radek Krejcic5c27e52018-11-15 14:38:11 +01003139 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003140 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003141 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003142 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003143 /* TODO user type plugins */
3144 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003145 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003146 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 +01003147 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003148 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003149 /* no specific restriction in leaf's type definition, copy from the base */
3150 free(*type);
3151 (*type) = base;
3152 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003153 }
3154
Radek Krejci0935f412019-08-20 16:15:18 +02003155 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003156
3157cleanup:
3158 ly_set_erase(&tpdf_chain, free);
3159 return ret;
3160}
3161
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003162/**
3163 * @brief Compile status information of the given node.
3164 *
3165 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3166 * has the status correctly set during the compilation.
3167 *
3168 * @param[in] ctx Compile context
3169 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3170 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3171 * the compatibility with the parent's status value.
3172 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3173 * @return LY_ERR value.
3174 */
3175static LY_ERR
3176lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3177{
3178 /* status - it is not inherited by specification, but it does not make sense to have
3179 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3180 if (!((*node_flags) & LYS_STATUS_MASK)) {
3181 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3182 if ((parent_flags & 0x3) != 0x3) {
3183 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3184 * combination of bits (0x3) which marks the uses_status value */
3185 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
Radek Krejci0f969882020-08-21 16:56:47 +02003186 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003187 }
3188 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3189 } else {
3190 (*node_flags) |= LYS_STATUS_CURR;
3191 }
3192 } else if (parent_flags & LYS_STATUS_MASK) {
3193 /* check status compatibility with the parent */
3194 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3195 if ((*node_flags) & LYS_STATUS_CURR) {
3196 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3197 "A \"current\" status is in conflict with the parent's \"%s\" status.",
Radek Krejci0f969882020-08-21 16:56:47 +02003198 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003199 } else { /* LYS_STATUS_DEPRC */
3200 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3201 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3202 }
3203 return LY_EVALID;
3204 }
3205 }
3206 return LY_SUCCESS;
3207}
3208
Radek Krejci8cce8532019-03-05 11:27:45 +01003209/**
3210 * @brief Check uniqness of the node/action/notification name.
3211 *
3212 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3213 * structures, but they share the namespace so we need to check their name collisions.
3214 *
3215 * @param[in] ctx Compile context.
Michal Vasko20424b42020-08-31 12:29:38 +02003216 * @param[in] parent Parent of the nodes to check, can be NULL.
Radek Krejci8cce8532019-03-05 11:27:45 +01003217 * @param[in] name Name of the item to find in the given lists.
Michal Vasko20424b42020-08-31 12:29:38 +02003218 * @param[in] exclude Node that was just added that should be excluded from the name checking.
Radek Krejci8cce8532019-03-05 11:27:45 +01003219 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3220 */
3221static LY_ERR
Michal Vasko20424b42020-08-31 12:29:38 +02003222lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
3223 const struct lysc_node *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003224{
Michal Vasko20424b42020-08-31 12:29:38 +02003225 const struct lysc_node *iter, *iter2;
3226 const struct lysc_action *actions;
3227 const struct lysc_notif *notifs;
3228 uint32_t getnext_flags;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003229 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003230
Michal Vasko20424b42020-08-31 12:29:38 +02003231#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
3232
3233 if (exclude->nodetype == LYS_CASE) {
3234 /* check restricted only to all the cases */
3235 assert(parent->nodetype == LYS_CHOICE);
3236 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
3237 if (CHECK_NODE(iter, exclude, name)) {
3238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
3239 return LY_EEXIST;
3240 }
3241 }
3242
3243 return LY_SUCCESS;
3244 }
3245
3246 /* no reason for our parent to be choice anymore */
3247 assert(!parent || (parent->nodetype != LYS_CHOICE));
3248
3249 if (parent && (parent->nodetype == LYS_CASE)) {
3250 /* move to the first data definition parent */
3251 parent = lysc_data_parent(parent);
3252 }
3253
3254 getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE;
3255 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
3256 getnext_flags |= LYS_GETNEXT_OUTPUT;
3257 }
3258
3259 iter = NULL;
3260 while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) {
3261 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003262 goto error;
3263 }
Michal Vasko20424b42020-08-31 12:29:38 +02003264
3265 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
3266 if (iter->nodetype == LYS_CHOICE) {
3267 iter2 = NULL;
3268 while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) {
3269 if (CHECK_NODE(iter2, exclude, name)) {
3270 goto error;
3271 }
3272 }
3273 }
Radek Krejci8cce8532019-03-05 11:27:45 +01003274 }
Michal Vasko20424b42020-08-31 12:29:38 +02003275
3276 actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003277 LY_ARRAY_FOR(actions, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003278 if (CHECK_NODE(&actions[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003279 goto error;
3280 }
3281 }
Michal Vasko20424b42020-08-31 12:29:38 +02003282
3283 notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003284 LY_ARRAY_FOR(notifs, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003285 if (CHECK_NODE(&notifs[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003286 goto error;
3287 }
3288 }
3289 return LY_SUCCESS;
Michal Vasko20424b42020-08-31 12:29:38 +02003290
Radek Krejci8cce8532019-03-05 11:27:45 +01003291error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003293 return LY_EEXIST;
Michal Vasko20424b42020-08-31 12:29:38 +02003294
3295#undef CHECK_NODE
Radek Krejci8cce8532019-03-05 11:27:45 +01003296}
3297
Radek Krejciec4da802019-05-02 13:02:41 +02003298static 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 +01003299
Radek Krejcia3045382018-11-22 14:30:31 +01003300/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003301 * @brief Compile parsed RPC/action schema node information.
3302 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003303 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003304 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003305 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3306 * @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).
3307 * Zero means no uses, non-zero value with no status bit set mean the default status.
3308 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3309 */
3310static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003311lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003312 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003313{
3314 LY_ERR ret = LY_SUCCESS;
3315 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003316 LY_ARRAY_COUNT_TYPE u;
3317 uint32_t opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003318
Radek Krejci327de162019-06-14 12:52:07 +02003319 lysc_update_path(ctx, parent, action_p->name);
3320
Michal Vasko20424b42020-08-31 12:29:38 +02003321 /* member needed for uniqueness check lys_getnext() */
3322 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
3323 action->module = ctx->mod;
3324 action->parent = parent;
3325
3326 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action));
Radek Krejci8cce8532019-03-05 11:27:45 +01003327
Radek Krejciec4da802019-05-02 13:02:41 +02003328 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003329 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003330 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003331 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003332 return LY_EVALID;
3333 }
3334
Radek Krejciec4da802019-05-02 13:02:41 +02003335 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003336 action->sp = action_p;
3337 }
3338 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3339
3340 /* status - it is not inherited by specification, but it does not make sense to have
3341 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003342 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003343
Radek Krejci011e4aa2020-09-04 15:22:31 +02003344 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
3345 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
3346 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003347 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003348 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003349
3350 /* input */
Michal Vasko22df3f02020-08-24 13:29:22 +02003351 lysc_update_path(ctx, (struct lysc_node *)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003352 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003353 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 +02003354 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003355 LY_LIST_FOR(action_p->input.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003356 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003357 }
Radek Krejci327de162019-06-14 12:52:07 +02003358 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003359 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003360
3361 /* output */
Michal Vasko22df3f02020-08-24 13:29:22 +02003362 lysc_update_path(ctx, (struct lysc_node *)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003363 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003364 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 +02003365 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003366 LY_LIST_FOR(action_p->output.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003367 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003368 }
Radek Krejci327de162019-06-14 12:52:07 +02003369 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003370 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003371
3372 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3373 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003374 ret = ly_set_add(&ctx->xpath, action, 0, NULL);
3375 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003376 }
3377
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003378cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003379 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003380 return ret;
3381}
3382
3383/**
Radek Krejci43981a32019-04-12 09:44:11 +02003384 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003385 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003386 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003387 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3388 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3389 * @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 +02003390 * Zero means no uses, non-zero value with no status bit set mean the default status.
3391 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3392 */
3393static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003394lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003395 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
Radek Krejcifc11bd72019-04-11 16:00:05 +02003396{
3397 LY_ERR ret = LY_SUCCESS;
3398 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003399 LY_ARRAY_COUNT_TYPE u;
3400 uint32_t opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003401
Radek Krejci327de162019-06-14 12:52:07 +02003402 lysc_update_path(ctx, parent, notif_p->name);
3403
Michal Vasko20424b42020-08-31 12:29:38 +02003404 /* member needed for uniqueness check lys_getnext() */
3405 notif->nodetype = LYS_NOTIF;
3406 notif->module = ctx->mod;
3407 notif->parent = parent;
3408
3409 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003410
Radek Krejciec4da802019-05-02 13:02:41 +02003411 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003412 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3413 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003414 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003415 return LY_EVALID;
3416 }
3417
Radek Krejciec4da802019-05-02 13:02:41 +02003418 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003419 notif->sp = notif_p;
3420 }
3421 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3422
3423 /* status - it is not inherited by specification, but it does not make sense to have
3424 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003425 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
3426 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003427
Radek Krejci011e4aa2020-09-04 15:22:31 +02003428 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
3429 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
3430 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003431 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003432 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003433 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3434 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003435 ret = ly_set_add(&ctx->xpath, notif, 0, NULL);
3436 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003437 }
Radek Krejci0935f412019-08-20 16:15:18 +02003438 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003439
Radek Krejciec4da802019-05-02 13:02:41 +02003440 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003441 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003442 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status);
3443 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003444 }
3445
Radek Krejci327de162019-06-14 12:52:07 +02003446 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003447cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003448 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003449 return ret;
3450}
3451
3452/**
Radek Krejcia3045382018-11-22 14:30:31 +01003453 * @brief Compile parsed container node information.
3454 * @param[in] ctx Compile context
3455 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003456 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3457 * is enriched with the container-specific information.
3458 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3459 */
Radek Krejci19a96102018-11-15 13:38:09 +01003460static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003461lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003462{
Michal Vasko22df3f02020-08-24 13:29:22 +02003463 struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p;
3464 struct lysc_node_container *cont = (struct lysc_node_container *)node;
Radek Krejci19a96102018-11-15 13:38:09 +01003465 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003466 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003467 LY_ERR ret = LY_SUCCESS;
3468
Radek Krejcife909632019-02-12 15:34:42 +01003469 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003470 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003471 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003472 } else if (cont_p->musts) {
3473 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003474 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3475 cont->flags |= LYS_PRESENCE;
3476 } else if (cont_p->when) {
3477 /* container with a when condition */
3478 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 +02003479 cont->flags |= LYS_PRESENCE;
3480 } else if (cont_p->parent) {
3481 if (cont_p->parent->nodetype == LYS_CHOICE) {
3482 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003483 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 +02003484 cont_p->name, cont_p->parent->name);
3485 cont->flags |= LYS_PRESENCE;
3486 } else if ((cont_p->parent->nodetype == LYS_CASE)
3487 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3488 /* 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 +02003489 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 +02003490 cont_p->name, cont_p->parent->name);
3491 cont->flags |= LYS_PRESENCE;
3492 }
Radek Krejcife909632019-02-12 15:34:42 +01003493 }
3494
Michal Vaskoba417ac2020-08-06 14:48:20 +02003495 /* more cases when the container has meaning but is kept NP for convenience:
3496 * - when condition
3497 * - direct child action/notification
3498 */
3499
Radek Krejci19a96102018-11-15 13:38:09 +01003500 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003501 ret = lys_compile_node(ctx, child_p, node, 0);
3502 LY_CHECK_GOTO(ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003503 }
3504
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003505 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003506 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3507 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003508 ret = ly_set_add(&ctx->xpath, cont, 0, NULL);
3509 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003510 }
Radek Krejciec4da802019-05-02 13:02:41 +02003511 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3512 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003513
3514done:
3515 return ret;
3516}
3517
Radek Krejci33f72892019-02-21 10:36:58 +01003518/*
3519 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3520 * @param[in] ctx Compile context.
3521 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3522 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003523 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3524 * @return LY_ERR value.
3525 */
3526static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003527lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003528 struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003529{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003530 const char *dflt;
3531 struct lys_module *dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003532
Radek Krejciec4da802019-05-02 13:02:41 +02003533 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 +02003534 leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod));
3535
3536 /* store default value, if any */
3537 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
3538 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, dflt, dflt_mod));
Radek Krejci33f72892019-02-21 10:36:58 +01003539 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003540
Radek Krejci33f72892019-02-21 10:36:58 +01003541 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3542 /* 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 +02003543 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003544 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003545 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003546 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
3547 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
Radek Krejci33f72892019-02-21 10:36:58 +01003548 /* 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 +02003549 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003550 }
3551 }
3552 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003553 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3555 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3556 return LY_EVALID;
3557 }
3558 }
3559
Radek Krejci33f72892019-02-21 10:36:58 +01003560 return LY_SUCCESS;
3561}
3562
Radek Krejcia3045382018-11-22 14:30:31 +01003563/**
3564 * @brief Compile parsed leaf node information.
3565 * @param[in] ctx Compile context
3566 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003567 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3568 * is enriched with the leaf-specific information.
3569 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3570 */
Radek Krejci19a96102018-11-15 13:38:09 +01003571static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003572lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003573{
Michal Vasko22df3f02020-08-24 13:29:22 +02003574 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p;
3575 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003576 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003577 LY_ERR ret = LY_SUCCESS;
3578
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003579 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003580 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3581 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003582 ret = ly_set_add(&ctx->xpath, leaf, 0, NULL);
3583 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003584 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003585 if (leaf_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003586 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003587 leaf->flags |= LYS_SET_UNITS;
3588 }
Radek Krejcia1911222019-07-22 17:24:50 +02003589
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003590 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003591 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3592 LY_CHECK_GOTO(ret, done);
Radek Krejcia1911222019-07-22 17:24:50 +02003593
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003594 /* store/update default value */
Radek Krejciccd20f12019-02-15 14:12:27 +01003595 if (leaf_p->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003596 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, leaf_p->dflt, ctx->mod_def));
Radek Krejci76b3e962018-12-14 17:01:25 +01003597 leaf->flags |= LYS_SET_DFLT;
3598 }
Radek Krejci43699232018-11-23 14:59:46 +01003599
Radek Krejci19a96102018-11-15 13:38:09 +01003600done:
3601 return ret;
3602}
3603
Radek Krejcia3045382018-11-22 14:30:31 +01003604/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003605 * @brief Compile parsed leaf-list node information.
3606 * @param[in] ctx Compile context
3607 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003608 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3609 * is enriched with the leaf-list-specific information.
3610 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3611 */
3612static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003613lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003614{
Michal Vasko22df3f02020-08-24 13:29:22 +02003615 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p;
3616 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003617 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003618 LY_ERR ret = LY_SUCCESS;
3619
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003620 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003621 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3622 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003623 ret = ly_set_add(&ctx->xpath, llist, 0, NULL);
3624 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003625 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003626 if (llist_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003627 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003628 llist->flags |= LYS_SET_UNITS;
3629 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003630
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003631 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003632 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist);
3633 LY_CHECK_GOTO(ret, done);
Michal Vasko6a044b22020-01-15 12:25:39 +01003634
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003635 /* store/update default values */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003636 if (llist_p->dflts) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003637 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, llist_p->dflts, ctx->mod_def), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003638 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003639 }
3640
3641 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003642 if (llist->min) {
3643 llist->flags |= LYS_MAND_TRUE;
3644 }
Radek Krejcib7408632018-11-28 17:12:11 +01003645 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003646
Radek Krejci0e5d8382018-11-28 16:37:53 +01003647done:
3648 return ret;
3649}
3650
3651/**
Radek Krejci7af64242019-02-18 13:07:53 +01003652 * @brief Compile information about list's uniques.
3653 * @param[in] ctx Compile context.
3654 * @param[in] context_module Module where the prefixes are going to be resolved.
3655 * @param[in] uniques Sized array list of unique statements.
3656 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3657 * @return LY_ERR value.
3658 */
3659static LY_ERR
3660lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3661{
3662 LY_ERR ret = LY_SUCCESS;
3663 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003664 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003665 const char *keystr, *delim;
3666 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003667 LY_ARRAY_COUNT_TYPE v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003668 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003669 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003670
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003671 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003672 config = -1;
3673 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3674 keystr = uniques[v];
3675 while (keystr) {
3676 delim = strpbrk(keystr, " \t\n");
3677 if (delim) {
3678 len = delim - keystr;
3679 while (isspace(*delim)) {
3680 ++delim;
3681 }
3682 } else {
3683 len = strlen(keystr);
3684 }
3685
3686 /* unique node must be present */
3687 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko22df3f02020-08-24 13:29:22 +02003688 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0,
3689 (const struct lysc_node **)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003690 if (ret != LY_SUCCESS) {
3691 if (ret == LY_EDENIED) {
3692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003693 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003694 len, keystr, lys_nodetype2str((*key)->nodetype));
3695 }
3696 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003697 } else if (flags) {
3698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3699 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003700 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003701 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003702 }
3703
3704 /* all referenced leafs must be of the same config type */
3705 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003707 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003708 return LY_EVALID;
3709 } else if ((*key)->flags & LYS_CONFIG_W) {
3710 config = 1;
3711 } else { /* LYS_CONFIG_R */
3712 config = 0;
3713 }
3714
Michal Vasko14654712020-02-06 08:35:21 +01003715 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3716 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3717 if (parent->nodetype == LYS_LIST) {
3718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3719 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3720 return LY_EVALID;
3721 }
3722 }
3723
Radek Krejci7af64242019-02-18 13:07:53 +01003724 /* check status */
3725 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0f969882020-08-21 16:56:47 +02003726 (*key)->flags, (*key)->module, (*key)->name));
Radek Krejci7af64242019-02-18 13:07:53 +01003727
3728 /* mark leaf as unique */
3729 (*key)->flags |= LYS_UNIQUE;
3730
3731 /* next unique value in line */
3732 keystr = delim;
3733 }
3734 /* next unique definition */
3735 }
3736
3737 return LY_SUCCESS;
3738}
3739
3740/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003741 * @brief Compile parsed list node information.
3742 * @param[in] ctx Compile context
3743 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003744 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3745 * is enriched with the list-specific information.
3746 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3747 */
3748static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003749lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003750{
Michal Vasko22df3f02020-08-24 13:29:22 +02003751 struct lysp_node_list *list_p = (struct lysp_node_list *)node_p;
3752 struct lysc_node_list *list = (struct lysc_node_list *)node;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003753 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003754 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003755 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003756 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003757 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003758 LY_ERR ret = LY_SUCCESS;
3759
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003760 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003761 if (list->min) {
3762 list->flags |= LYS_MAND_TRUE;
3763 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003764 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3765
3766 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003767 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003768 }
3769
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003770 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003771 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3772 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003773 LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL));
Michal Vasko5d8756a2019-11-07 15:21:00 +01003774 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003775
3776 /* keys */
3777 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3779 return LY_EVALID;
3780 }
3781
3782 /* find all the keys (must be direct children) */
3783 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003784 if (!keystr) {
3785 /* keyless list */
3786 list->flags |= LYS_KEYLESS;
3787 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003788 while (keystr) {
3789 delim = strpbrk(keystr, " \t\n");
3790 if (delim) {
3791 len = delim - keystr;
3792 while (isspace(*delim)) {
3793 ++delim;
3794 }
3795 } else {
3796 len = strlen(keystr);
3797 }
3798
3799 /* key node must be present */
Michal Vasko22df3f02020-08-24 13:29:22 +02003800 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 +02003801 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3803 "The list's key \"%.*s\" not found.", len, keystr);
3804 return LY_EVALID;
3805 }
3806 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003807 if (key->flags & LYS_KEY) {
3808 /* the node was already marked as a key */
3809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3810 "Duplicated key identifier \"%.*s\".", len, keystr);
3811 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003812 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003813
Michal Vasko22df3f02020-08-24 13:29:22 +02003814 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003815 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003816 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003817 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3818 return LY_EVALID;
3819 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003820 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003821 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003822 if (key->type->basetype == LY_TYPE_EMPTY) {
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 cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003825 return LY_EVALID;
3826 }
3827 } else {
3828 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003829 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003830 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003831 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003832 return LY_EVALID;
3833 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003834 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003836 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003837 return LY_EVALID;
3838 }
3839 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003840
3841 /* check status */
3842 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003843 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003844
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003845 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003846 if (key->dflt) {
3847 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3848 lysc_type_free(ctx->ctx, key->dflt->realtype);
3849 free(key->dflt);
3850 key->dflt = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003851 }
3852 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003853 key->flags |= LYS_KEY;
3854
3855 /* move it to the correct position */
Michal Vasko22df3f02020-08-24 13:29:22 +02003856 if ((prev_key && (struct lysc_node *)prev_key != key->prev) || (!prev_key && key->prev->next)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02003857 /* fix links in closest previous siblings of the key */
3858 if (key->next) {
3859 key->next->prev = key->prev;
3860 } else {
3861 /* last child */
3862 list->child->prev = key->prev;
3863 }
3864 if (key->prev->next) {
3865 key->prev->next = key->next;
3866 }
3867 /* fix links in the key */
3868 if (prev_key) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003869 key->prev = (struct lysc_node *)prev_key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003870 key->next = prev_key->next;
3871 } else {
3872 key->prev = list->child->prev;
3873 key->next = list->child;
3874 }
3875 /* fix links in closes future siblings of the key */
3876 if (prev_key) {
3877 if (prev_key->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003878 prev_key->next->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003879 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003880 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003881 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003882 prev_key->next = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003883 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003884 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003885 }
3886 /* fix links in parent */
3887 if (!key->prev->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003888 list->child = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003889 }
3890 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003891
3892 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003893 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003894 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003895 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003896 }
3897
3898 /* uniques */
3899 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003900 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003901 }
3902
Radek Krejciec4da802019-05-02 13:02:41 +02003903 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3904 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003905
3906done:
3907 return ret;
3908}
3909
Radek Krejcib56c7502019-02-13 14:19:54 +01003910/**
3911 * @brief Do some checks and set the default choice's case.
3912 *
3913 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3914 *
3915 * @param[in] ctx Compile context.
3916 * @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,
3917 * not the reference to the imported module.
3918 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3919 * @return LY_ERR value.
3920 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003921static LY_ERR
3922lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3923{
Michal Vasko22df3f02020-08-24 13:29:22 +02003924 struct lysc_node *iter, *node = (struct lysc_node *)ch;
Radek Krejci76b3e962018-12-14 17:01:25 +01003925 const char *prefix = NULL, *name;
3926 size_t prefix_len = 0;
3927
3928 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3929 name = strchr(dflt, ':');
3930 if (name) {
3931 prefix = dflt;
3932 prefix_len = name - prefix;
3933 ++name;
3934 } else {
3935 name = dflt;
3936 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003937 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003938 /* prefixed default case make sense only for the prefix of the schema itself */
3939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3940 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3941 prefix_len, prefix);
3942 return LY_EVALID;
3943 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003944 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 +01003945 if (!ch->dflt) {
3946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3947 "Default case \"%s\" not found.", dflt);
3948 return LY_EVALID;
3949 }
3950 /* no mandatory nodes directly under the default case */
3951 LY_LIST_FOR(ch->dflt->child, iter) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003952 if (iter->parent != (struct lysc_node *)ch->dflt) {
Radek Krejcife13da42019-02-15 14:51:01 +01003953 break;
3954 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003955 if (iter->flags & LYS_MAND_TRUE) {
3956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3957 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3958 return LY_EVALID;
3959 }
3960 }
Radek Krejci01342af2019-01-03 15:18:08 +01003961 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003962 return LY_SUCCESS;
3963}
3964
Radek Krejciccd20f12019-02-15 14:12:27 +01003965static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003966lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003967{
3968 struct lys_module *mod;
3969 const char *prefix = NULL, *name;
3970 size_t prefix_len = 0;
3971 struct lysc_node_case *cs;
3972 struct lysc_node *node;
3973
3974 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3975 name = strchr(dflt, ':');
3976 if (name) {
3977 prefix = dflt;
3978 prefix_len = name - prefix;
3979 ++name;
3980 } else {
3981 name = dflt;
3982 }
3983 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3984 if (prefix) {
3985 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003987 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3988 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003989 return LY_EVALID;
3990 }
3991 } else {
3992 mod = ctx->mod;
3993 }
3994 /* get the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003995 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 +01003996 if (!cs) {
3997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003998 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003999 return LY_EVALID;
4000 }
4001
4002 /* check that there is no mandatory node */
4003 LY_LIST_FOR(cs->child, node) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004004 if (node->parent != (struct lysc_node *)cs) {
Radek Krejcife13da42019-02-15 14:51:01 +01004005 break;
4006 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004007 if (node->flags & LYS_MAND_TRUE) {
4008 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004009 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4010 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004011 return LY_EVALID;
4012 }
4013 }
4014
4015 /* set the default case in choice */
4016 ch->dflt = cs;
4017 cs->flags |= LYS_SET_DFLT;
4018
4019 return LY_SUCCESS;
4020}
4021
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004022/**
Michal Vasko20424b42020-08-31 12:29:38 +02004023 * @brief Compile choice children.
4024 *
4025 * @param[in] ctx Compile context
4026 * @param[in] child_p Parsed choice children nodes.
4027 * @param[in] node Compiled choice node to compile and add children to.
4028 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4029 */
4030static LY_ERR
4031lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node)
4032{
4033 LY_ERR ret = LY_SUCCESS;
4034 struct lysp_node *child_next_p;
4035 struct lysp_node_case *cs_p;
4036
4037 if (child_p->nodetype == LYS_CASE) {
4038 /* standard case under choice */
4039 ret = lys_compile_node(ctx, child_p, node, 0);
4040 } else {
4041 /* we need the implicit case first, so create a fake parsed case */
4042 cs_p = calloc(1, sizeof *cs_p);
4043 cs_p->nodetype = LYS_CASE;
Radek Krejci87e25252020-09-15 13:28:31 +02004044 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
Michal Vasko20424b42020-08-31 12:29:38 +02004045 cs_p->child = child_p;
4046
4047 /* make the child the only case child */
4048 child_next_p = child_p->next;
4049 child_p->next = NULL;
4050
4051 /* compile it normally */
4052 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0);
4053
Radek Krejci87e25252020-09-15 13:28:31 +02004054free_fake_node:
Michal Vasko20424b42020-08-31 12:29:38 +02004055 /* free the fake parsed node and correct pointers back */
4056 cs_p->child = NULL;
4057 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
4058 child_p->next = child_next_p;
4059 }
4060
4061 return ret;
4062}
4063
4064/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004065 * @brief Compile parsed choice node information.
Michal Vasko20424b42020-08-31 12:29:38 +02004066 *
Radek Krejci056d0a82018-12-06 16:57:25 +01004067 * @param[in] ctx Compile context
4068 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004069 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004070 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004071 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4072 */
4073static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004074lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004075{
Michal Vasko22df3f02020-08-24 13:29:22 +02004076 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p;
4077 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
Michal Vasko20424b42020-08-31 12:29:38 +02004078 struct lysp_node *child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004079 LY_ERR ret = LY_SUCCESS;
4080
Michal Vasko20424b42020-08-31 12:29:38 +02004081 assert(node->nodetype == LYS_CHOICE);
4082
Radek Krejci056d0a82018-12-06 16:57:25 +01004083 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko20424b42020-08-31 12:29:38 +02004084 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node));
Radek Krejci056d0a82018-12-06 16:57:25 +01004085 }
4086
4087 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004088 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004089 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004090 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004091
Radek Krejci9800fb82018-12-13 14:26:23 +01004092 return ret;
4093}
4094
4095/**
4096 * @brief Compile parsed anydata or anyxml node information.
4097 * @param[in] ctx Compile context
4098 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004099 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4100 * is enriched with the any-specific information.
4101 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4102 */
4103static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004104lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004105{
Michal Vasko22df3f02020-08-24 13:29:22 +02004106 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p;
4107 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004108 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9800fb82018-12-13 14:26:23 +01004109 LY_ERR ret = LY_SUCCESS;
4110
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004111 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004112 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4113 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004114 ret = ly_set_add(&ctx->xpath, any, 0, NULL);
4115 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004116 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004117
4118 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004119 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4120 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004121 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004122done:
4123 return ret;
4124}
4125
Radek Krejcib56c7502019-02-13 14:19:54 +01004126/**
Michal Vasko795b3752020-07-13 15:24:27 +02004127 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4128 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004129 *
4130 * @param[in] ctx Compile context
4131 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4132 * the choice itself is expected instead of a specific case node.
4133 * @param[in] node Schema node to connect into the list.
4134 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004135 * 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 +01004136 */
4137static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004138lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004139{
Michal Vasko795b3752020-07-13 15:24:27 +02004140 struct lysc_node **children, *start, *end;
4141 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004142
Michal Vasko20424b42020-08-31 12:29:38 +02004143 node->parent = parent;
4144
4145 if (parent) {
4146 if (parent->nodetype == LYS_CHOICE) {
4147 assert(node->nodetype == LYS_CASE);
4148 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
4149 } else {
4150 children = lysc_node_children_p(parent, ctx->options);
4151 }
4152 assert(children);
4153
Radek Krejci056d0a82018-12-06 16:57:25 +01004154 if (!(*children)) {
4155 /* first child */
4156 *children = node;
4157 } else if (*children != node) {
4158 /* by the condition in previous branch we cover the choice/case children
4159 * - the children list is shared by the choice and the the first case, in addition
4160 * the first child of each case must be referenced from the case node. So the node is
4161 * actually always already inserted in case it is the first children - so here such
4162 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004163 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4164 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4165 /* some augments are already connected and we are connecting new ones,
4166 * keep module name order and insert the node into the children list */
4167 end = (*children);
4168 do {
4169 end = end->prev;
4170 mod = end->module;
4171 while (end->prev->module == mod) {
4172 end = end->prev;
4173 }
Radek Krejcif6a11002020-08-21 13:29:07 +02004174 } 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 +02004175
4176 /* we have the last existing node after our node, easily get the first before and connect it */
4177 start = end->prev;
4178 start->next = node;
4179 node->next = end;
4180 end->prev = node;
4181 node->prev = start;
4182 } else {
4183 /* insert at the end of the parent's children list */
4184 (*children)->prev->next = node;
4185 node->prev = (*children)->prev;
4186 (*children)->prev = node;
4187 }
Michal Vasko20424b42020-08-31 12:29:38 +02004188 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004189
Michal Vasko20424b42020-08-31 12:29:38 +02004190 /* check the name uniqueness (even for an only child, it may be in case) */
4191 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
4192 return LY_EEXIST;
4193 }
4194 } else {
4195 /* top-level element */
4196 if (!ctx->mod->compiled->data) {
4197 ctx->mod->compiled->data = node;
4198 } else {
4199 /* insert at the end of the module's top-level nodes list */
4200 ctx->mod->compiled->data->prev->next = node;
4201 node->prev = ctx->mod->compiled->data->prev;
4202 ctx->mod->compiled->data->prev = node;
4203 }
4204
4205 /* check the name uniqueness on top-level */
4206 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
4207 return LY_EEXIST;
Radek Krejci056d0a82018-12-06 16:57:25 +01004208 }
4209 }
Michal Vasko20424b42020-08-31 12:29:38 +02004210
Radek Krejci056d0a82018-12-06 16:57:25 +01004211 return LY_SUCCESS;
4212}
4213
Radek Krejci95710c92019-02-11 15:49:55 +01004214/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004215 * @brief Prepare the case structure in choice node for the new data node.
4216 *
4217 * 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
4218 * created in the choice when the first child was processed.
4219 *
4220 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004221 * @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 +02004222 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004223 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4224 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4225 * @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,
4226 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004227 */
Michal Vasko20424b42020-08-31 12:29:38 +02004228static LY_ERR
4229lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004230{
Michal Vasko20424b42020-08-31 12:29:38 +02004231 struct lysp_node *child_p;
4232 struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004233
Radek Krejci39424802020-08-12 09:31:00 +02004234 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004235 /* we have to add an implicit case node into the parent choice */
Radek Krejci95710c92019-02-11 15:49:55 +01004236 } else if (node_p->nodetype == LYS_CASE) {
Michal Vasko20424b42020-08-31 12:29:38 +02004237 /* explicit parent case */
4238 LY_LIST_FOR(cs_p->child, child_p) {
4239 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004240 }
Radek Krejci95710c92019-02-11 15:49:55 +01004241 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004242 LOGINT_RET(ctx->ctx);
Radek Krejci056d0a82018-12-06 16:57:25 +01004243 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004244
Michal Vasko20424b42020-08-31 12:29:38 +02004245 return LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01004246}
4247
Radek Krejcib56c7502019-02-13 14:19:54 +01004248/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004249 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004250 *
4251 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004252 * @param[in] node Target node where the config is supposed to be changed.
4253 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004254 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4255 * 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 +01004256 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4257 * @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 +01004258 * @return LY_ERR value.
4259 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004260static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004261lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci857189e2020-09-01 13:26:36 +02004262 ly_bool inheriting, ly_bool refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004263{
4264 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004265 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004266
4267 if (config == (node->flags & LYS_CONFIG_MASK)) {
4268 /* nothing to do */
4269 return LY_SUCCESS;
4270 }
4271
4272 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004273 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004274 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004276 "Invalid %s of config - configuration node cannot be child of any state data node.",
4277 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004278 return LY_EVALID;
4279 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004280 node->flags |= LYS_SET_CONFIG;
4281 } else {
4282 if (node->flags & LYS_SET_CONFIG) {
4283 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4284 /* setting config flags, but have node with explicit config true */
4285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004286 "Invalid %s of config - configuration node cannot be child of any state data node.",
4287 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004288 return LY_EVALID;
4289 }
4290 /* do not change config on nodes where the config is explicitely set, this does not apply to
4291 * nodes, which are being changed explicitly (targets of refine or deviation) */
4292 return LY_SUCCESS;
4293 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004294 }
4295 node->flags &= ~LYS_CONFIG_MASK;
4296 node->flags |= config;
4297
4298 /* inherit the change into the children */
Michal Vasko22df3f02020-08-24 13:29:22 +02004299 LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004300 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004301 }
4302
Radek Krejci76b3e962018-12-14 17:01:25 +01004303 return LY_SUCCESS;
4304}
4305
Radek Krejcib56c7502019-02-13 14:19:54 +01004306/**
4307 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4308 *
4309 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4310 * the flag to such parents from a mandatory children.
4311 *
4312 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4313 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4314 * (mandatory children was removed).
4315 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02004316static void
Radek Krejci857189e2020-09-01 13:26:36 +02004317lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
Radek Krejcife909632019-02-12 15:34:42 +01004318{
4319 struct lysc_node *iter;
4320
4321 if (add) { /* set flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004322 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
Radek Krejcife909632019-02-12 15:34:42 +01004323 parent = parent->parent) {
4324 parent->flags |= LYS_MAND_TRUE;
4325 }
4326 } else { /* unset flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004327 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004328 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004329 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004330 /* there is another mandatory node */
4331 return;
4332 }
4333 }
4334 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4335 parent->flags &= ~LYS_MAND_TRUE;
4336 }
4337 }
4338}
4339
Radek Krejci056d0a82018-12-06 16:57:25 +01004340/**
Radek Krejci3641f562019-02-13 15:38:40 +01004341 * @brief Internal sorting process for the lys_compile_augment_sort().
4342 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4343 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4344 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4345 */
4346static void
4347lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4348{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004349 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004350 size_t len;
4351
4352 len = strlen(aug_p->nodeid);
4353 LY_ARRAY_FOR(result, v) {
4354 if (strlen(result[v]->nodeid) <= len) {
4355 continue;
4356 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004357 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004358 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004359 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004360 break;
4361 }
4362 }
4363 result[v] = aug_p;
4364 LY_ARRAY_INCREMENT(result);
4365}
4366
4367/**
4368 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4369 *
4370 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4371 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4372 *
4373 * @param[in] ctx Compile context.
4374 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4375 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4376 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4377 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4378 * @return LY_ERR value.
4379 */
4380LY_ERR
4381lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4382{
4383 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004384 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004385
4386 assert(augments);
4387
4388 /* get count of the augments in module and all its submodules */
4389 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004390 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004391 }
4392 LY_ARRAY_FOR(inc_p, u) {
4393 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004394 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004395 }
4396 }
4397
4398 if (!count) {
4399 *augments = NULL;
4400 return LY_SUCCESS;
4401 }
4402 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4403
4404 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4405 * together, so there can be even /z/y betwwen them. */
4406 LY_ARRAY_FOR(aug_p, u) {
4407 lys_compile_augment_sort_(&aug_p[u], result);
4408 }
4409 LY_ARRAY_FOR(inc_p, u) {
4410 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4411 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4412 }
4413 }
4414
4415 *augments = result;
4416 return LY_SUCCESS;
4417}
4418
4419/**
4420 * @brief Compile the parsed augment connecting it into its target.
4421 *
4422 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4423 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4424 * are already implemented and compiled.
4425 *
4426 * @param[in] ctx Compile context.
4427 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004428 * @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
4429 * children in case of the augmenting uses data.
4430 * @return LY_SUCCESS on success.
4431 * @return LY_EVALID on failure.
4432 */
4433LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004434lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004435{
Michal Vaskoe6143202020-07-03 13:02:08 +02004436 LY_ERR ret = LY_SUCCESS, rc;
Michal Vasko20424b42020-08-31 12:29:38 +02004437 struct lysp_node *node_p;
Radek Krejci3641f562019-02-13 15:38:40 +01004438 struct lysc_node *target; /* target target of the augment */
4439 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004440 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004441 struct lys_module **aug_mod;
Radek Krejci857189e2020-09-01 13:26:36 +02004442 ly_bool allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004443 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004444 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004445 uint32_t opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004446
Radek Krejci327de162019-06-14 12:52:07 +02004447 lysc_update_path(ctx, NULL, "{augment}");
4448 lysc_update_path(ctx, NULL, aug_p->nodeid);
4449
Michal Vaskocb18c7f2020-08-24 09:22:28 +02004450 ret = lysc_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004451 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Michal Vasko22df3f02020-08-24 13:29:22 +02004452 1, (const struct lysc_node **)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004453 if (ret != LY_SUCCESS) {
4454 if (ret == LY_EDENIED) {
4455 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4456 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4457 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4458 }
4459 return LY_EVALID;
4460 }
4461
4462 /* check for mandatory nodes
4463 * - new cases augmenting some choice can have mandatory nodes
4464 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4465 */
Radek Krejci733988a2019-02-15 15:12:44 +01004466 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004467 allow_mandatory = 1;
4468 }
4469
4470 when_shared = NULL;
4471 LY_LIST_FOR(aug_p->child, node_p) {
4472 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4473 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004474 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004475 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4476 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004478 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4479 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004480 return LY_EVALID;
4481 }
4482
4483 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004484 ctx->options |= flags;
Michal Vasko20424b42020-08-31 12:29:38 +02004485 if (target->nodetype == LYS_CHOICE) {
4486 LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target));
Radek Krejci3641f562019-02-13 15:38:40 +01004487 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004488 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004489 }
Radek Krejciec4da802019-05-02 13:02:41 +02004490 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004491
Radek Krejcife13da42019-02-15 14:51:01 +01004492 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4493 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004494 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004495 /* 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 +02004496 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 +01004497 } else if (target->nodetype == LYS_CHOICE) {
4498 /* 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 +02004499 node = ((struct lysc_node_choice *)target)->cases->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004500 } else {
4501 /* the compiled node is the last child of the target */
Michal Vasko22df3f02020-08-24 13:29:22 +02004502 node = (struct lysc_node *)lysc_node_children(target, flags);
Radek Krejci7c7783d2020-04-08 15:34:39 +02004503 if (!node) {
4504 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4505 break;
4506 }
4507 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004508 }
4509
Radek Krejci733988a2019-02-15 15:12:44 +01004510 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004511 node->flags &= ~LYS_MAND_TRUE;
4512 lys_compile_mandatory_parents(target, 0);
4513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004514 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004515 return LY_EVALID;
4516 }
4517
4518 /* pass augment's when to all the children */
4519 if (aug_p->when) {
4520 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4521 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004522 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004523 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004524
4525 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4526 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004527 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4528 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004529 }
4530
Radek Krejci3641f562019-02-13 15:38:40 +01004531 when_shared = *when;
4532 } else {
4533 ++when_shared->refcount;
4534 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004535
4536 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4537 /* in this case check "when" again for all children because of dummy node check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004538 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4539 LY_CHECK_GOTO(ret, error);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004540 }
Radek Krejci3641f562019-02-13 15:38:40 +01004541 }
4542 }
4543 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004544
Radek Krejciec4da802019-05-02 13:02:41 +02004545 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004546 switch (target->nodetype) {
4547 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004548 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004549 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004550 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004551 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004552 break;
4553 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004554 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004555 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004556 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004557 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004558 break;
4559 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004560 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004561 if (aug_p->actions) {
4562 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004563 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4564 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004565 return LY_EVALID;
4566 }
4567 if (aug_p->notifs) {
4568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004569 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004570 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004571 return LY_EVALID;
4572 }
4573 }
Radek Krejci3641f562019-02-13 15:38:40 +01004574
Michal Vaskoe6143202020-07-03 13:02:08 +02004575 /* add this module into the target module augmented_by, if not there already */
4576 rc = LY_SUCCESS;
4577 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4578 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4579 rc = LY_EEXIST;
4580 break;
4581 }
4582 }
4583 if (!rc) {
4584 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4585 *aug_mod = ctx->mod;
4586 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004587
Radek Krejci327de162019-06-14 12:52:07 +02004588 lysc_update_path(ctx, NULL, NULL);
4589 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004590
Radek Krejci3641f562019-02-13 15:38:40 +01004591error:
Radek Krejciec4da802019-05-02 13:02:41 +02004592 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004593 return ret;
4594}
4595
4596/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004597 * @brief Apply refined or deviated mandatory flag to the target node.
4598 *
4599 * @param[in] ctx Compile context.
4600 * @param[in] node Target node where the mandatory property is supposed to be changed.
4601 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004602 * @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 +01004603 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4604 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4605 * 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 +01004606 * @return LY_ERR value.
4607 */
4608static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004609lys_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 +01004610{
4611 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004613 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4614 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004615 return LY_EVALID;
4616 }
4617
4618 if (mandatory_flag & LYS_MAND_TRUE) {
4619 /* check if node has default value */
4620 if (node->nodetype & LYS_LEAF) {
4621 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004622 if (refine_flag) {
4623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004624 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004625 return LY_EVALID;
4626 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004627 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004628 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004629 if (refine_flag) {
4630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004631 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004632 return LY_EVALID;
4633 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004634 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004635 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004636 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 +01004637 return LY_EVALID;
4638 }
4639
4640 node->flags &= ~LYS_MAND_FALSE;
4641 node->flags |= LYS_MAND_TRUE;
4642 lys_compile_mandatory_parents(node->parent, 1);
4643 } else {
4644 /* make mandatory false */
4645 node->flags &= ~LYS_MAND_TRUE;
4646 node->flags |= LYS_MAND_FALSE;
4647 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcif1421c22019-02-19 13:05:20 +01004648 }
4649 return LY_SUCCESS;
4650}
4651
4652/**
Michal Vasko601ddb32020-08-31 16:25:35 +02004653 * @brief Find grouping for a uses.
Radek Krejcie86bf772018-12-14 11:39:53 +01004654 *
Michal Vasko601ddb32020-08-31 16:25:35 +02004655 * @param[in] ctx Compile context.
4656 * @param[in] uses_p Parsed uses node.
4657 * @param[out] gpr_p Found grouping on success.
4658 * @param[out] grp_mod Module of @p grp_p on success.
4659 * @return LY_ERR value.
Radek Krejcie86bf772018-12-14 11:39:53 +01004660 */
4661static LY_ERR
Michal Vasko601ddb32020-08-31 16:25:35 +02004662lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
4663 struct lys_module **grp_mod)
Radek Krejcie86bf772018-12-14 11:39:53 +01004664{
4665 struct lysp_node *node_p;
Michal Vasko601ddb32020-08-31 16:25:35 +02004666 struct lysp_grp *grp;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004667 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +02004668 ly_bool found = 0;
Radek Krejcie86bf772018-12-14 11:39:53 +01004669 const char *id, *name, *prefix;
4670 size_t prefix_len, name_len;
Michal Vasko601ddb32020-08-31 16:25:35 +02004671 struct lys_module *mod;
4672
4673 *grp_p = NULL;
4674 *grp_mod = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004675
4676 /* search for the grouping definition */
Radek Krejcie86bf772018-12-14 11:39:53 +01004677 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004678 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004679 if (prefix) {
4680 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4681 if (!mod) {
4682 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004683 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004684 return LY_EVALID;
4685 }
4686 } else {
4687 mod = ctx->mod_def;
4688 }
4689 if (mod == ctx->mod_def) {
4690 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004691 grp = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004692 LY_ARRAY_FOR(grp, u) {
4693 if (!strcmp(grp[u].name, name)) {
4694 grp = &grp[u];
4695 found = 1;
4696 break;
4697 }
4698 }
4699 }
4700 }
4701 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004702 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004703 grp = mod->parsed->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004704 LY_ARRAY_FOR(grp, u) {
4705 if (!strcmp(grp[u].name, name)) {
4706 grp = &grp[u];
4707 found = 1;
4708 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004709 }
4710 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004711 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004712 /* ... and all the submodules */
Michal Vasko601ddb32020-08-31 16:25:35 +02004713 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004714 grp = mod->parsed->includes[u].submodule->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004715 LY_ARRAY_FOR(grp, v) {
4716 if (!strcmp(grp[v].name, name)) {
4717 grp = &grp[v];
4718 found = 1;
4719 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004720 }
4721 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004722 if (found) {
4723 break;
4724 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004725 }
4726 }
4727 }
4728 if (!found) {
4729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4730 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4731 return LY_EVALID;
4732 }
4733
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004734 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4735 /* remember that the grouping is instantiated to avoid its standalone validation */
4736 grp->flags |= LYS_USED_GRP;
4737 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004738
Michal Vasko601ddb32020-08-31 16:25:35 +02004739 *grp_p = grp;
4740 *grp_mod = mod;
4741 return LY_SUCCESS;
4742}
Radek Krejcie86bf772018-12-14 11:39:53 +01004743
Michal Vasko601ddb32020-08-31 16:25:35 +02004744static LY_ERR
4745lys_compile_refines(struct lysc_ctx *ctx, struct lysp_refine *refines, const struct lysc_node *context_node)
4746{
4747 struct lysc_node *node;
4748 LY_ARRAY_COUNT_TYPE u;
4749 struct lysp_refine *rfn;
4750 LY_ERR ret = LY_SUCCESS;
4751 uint32_t min, max;
4752 uint16_t flags;
4753 struct ly_set refined = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01004754
Radek Krejci327de162019-06-14 12:52:07 +02004755 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004756
Radek Krejci76b3e962018-12-14 17:01:25 +01004757 /* apply refine */
Michal Vasko601ddb32020-08-31 16:25:35 +02004758 LY_ARRAY_FOR(refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004759 lysc_update_path(ctx, NULL, rfn->nodeid);
4760
Michal Vasko601ddb32020-08-31 16:25:35 +02004761 ret = lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, context_node, ctx->mod,
Michal Vasko20424b42020-08-31 12:29:38 +02004762 0, 0, (const struct lysc_node **)&node, &flags);
4763 LY_CHECK_GOTO(ret, cleanup);
4764 ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL);
4765 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004766
4767 /* default value */
4768 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004769 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004770 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci0f969882020-08-21 16:56:47 +02004771 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE " default values.",
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004772 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Michal Vasko20424b42020-08-31 12:29:38 +02004773 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004774 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004775 }
4776 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004778 "Invalid refine of default - %s cannot hold default value(s).",
4779 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004780 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004781 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004782 }
4783 if (node->nodetype == LYS_LEAF) {
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_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], 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_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004790 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4792 "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 +02004793 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004794 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004795 }
Radek Krejcia1911222019-07-22 17:24:50 +02004796
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004797 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004798 ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def);
4799 LY_CHECK_GOTO(ret, cleanup);
4800
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004801 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004802 } else if (node->nodetype == LYS_CHOICE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004803 if (((struct lysc_node_choice *)node)->dflt) {
Radek Krejci01342af2019-01-03 15:18:08 +01004804 /* unset LYS_SET_DFLT from the current default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02004805 ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT;
Radek Krejci01342af2019-01-03 15:18:08 +01004806 }
Michal Vasko20424b42020-08-31 12:29:38 +02004807 ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node);
4808 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004809 }
4810 }
4811
Radek Krejci12fb9142019-01-08 09:45:30 +01004812 /* description */
4813 if (rfn->dsc) {
4814 FREE_STRING(ctx->ctx, node->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004815 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->dsc, 0, &node->dsc), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004816 }
4817
4818 /* reference */
4819 if (rfn->ref) {
4820 FREE_STRING(ctx->ctx, node->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004821 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->ref, 0, &node->ref), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004822 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004823
4824 /* config */
4825 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004826 if (!flags) {
Michal Vasko20424b42020-08-31 12:29:38 +02004827 ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1);
4828 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004829 } else {
4830 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004831 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004832 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004833 }
4834
4835 /* mandatory */
4836 if (rfn->flags & LYS_MAND_MASK) {
Michal Vasko20424b42020-08-31 12:29:38 +02004837 ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1);
4838 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004839 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004840
4841 /* presence */
4842 if (rfn->presence) {
4843 if (node->nodetype != LYS_CONTAINER) {
4844 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004845 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4846 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004847 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004848 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004849 }
4850 node->flags |= LYS_PRESENCE;
4851 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004852
4853 /* must */
4854 if (rfn->musts) {
4855 switch (node->nodetype) {
4856 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02004857 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 +01004858 break;
4859 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004860 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 +01004861 break;
4862 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004863 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 +01004864 break;
4865 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004866 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 +01004867 break;
4868 case LYS_ANYXML:
4869 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02004870 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 +01004871 break;
4872 default:
4873 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004874 "Invalid refine of must statement - %s cannot hold any must statement.",
4875 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004876 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004877 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004878 }
Michal Vasko20424b42020-08-31 12:29:38 +02004879 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4880 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004881 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004882
4883 /* min/max-elements */
4884 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4885 switch (node->nodetype) {
4886 case LYS_LEAFLIST:
4887 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004888 ((struct lysc_node_leaflist *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004889 }
4890 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004891 ((struct lysc_node_leaflist *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004892 if (rfn->min) {
4893 node->flags |= LYS_MAND_TRUE;
4894 lys_compile_mandatory_parents(node->parent, 1);
4895 } else {
4896 node->flags &= ~LYS_MAND_TRUE;
4897 lys_compile_mandatory_parents(node->parent, 0);
4898 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004899 }
4900 break;
4901 case LYS_LIST:
4902 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004903 ((struct lysc_node_list *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004904 }
4905 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004906 ((struct lysc_node_list *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004907 if (rfn->min) {
4908 node->flags |= LYS_MAND_TRUE;
4909 lys_compile_mandatory_parents(node->parent, 1);
4910 } else {
4911 node->flags &= ~LYS_MAND_TRUE;
4912 lys_compile_mandatory_parents(node->parent, 0);
4913 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004914 }
4915 break;
4916 default:
4917 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004918 "Invalid refine of %s statement - %s cannot hold this statement.",
Radek Krejci0f969882020-08-21 16:56:47 +02004919 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004920 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004921 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004922 }
4923 }
Radek Krejcif0089082019-01-07 16:42:01 +01004924
4925 /* if-feature */
4926 if (rfn->iffeatures) {
4927 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004928 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004929 }
Radek Krejci327de162019-06-14 12:52:07 +02004930
4931 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004932 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004933
Radek Krejcif2271f12019-01-07 16:42:23 +01004934 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004935 for (uint32_t i = 0; i < refined.count; ++i) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004936 node = (struct lysc_node *)refined.objs[i];
Michal Vasko601ddb32020-08-31 16:25:35 +02004937 rfn = &refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02004938 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004939
4940 /* check possible conflict with default value (default added, mandatory left true) */
4941 if ((node->flags & LYS_MAND_TRUE) &&
Michal Vasko22df3f02020-08-24 13:29:22 +02004942 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) ||
Radek Krejcif2271f12019-01-07 16:42:23 +01004943 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004945 "Invalid refine of default - the node is mandatory.");
Michal Vasko20424b42020-08-31 12:29:38 +02004946 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004947 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004948 }
4949
4950 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4951 if (node->nodetype == LYS_LIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004952 min = ((struct lysc_node_list *)node)->min;
4953 max = ((struct lysc_node_list *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004954 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02004955 min = ((struct lysc_node_leaflist *)node)->min;
4956 max = ((struct lysc_node_leaflist *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004957 }
4958 if (min > max) {
4959 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004960 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
Radek Krejci0f969882020-08-21 16:56:47 +02004961 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Michal Vasko20424b42020-08-31 12:29:38 +02004962 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004963 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004964 }
4965 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004966
4967 lysc_update_path(ctx, NULL, NULL);
Radek Krejcif2271f12019-01-07 16:42:23 +01004968 }
4969
Michal Vasko601ddb32020-08-31 16:25:35 +02004970cleanup:
4971 ly_set_erase(&refined, NULL);
4972 lysc_update_path(ctx, NULL, NULL);
4973 return ret;
4974}
4975
4976/**
4977 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4978 * If present, also apply uses's modificators.
4979 *
4980 * @param[in] ctx Compile context
4981 * @param[in] uses_p Parsed uses schema node.
4982 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4983 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4984 * the compile context.
4985 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4986 */
4987static LY_ERR
4988lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p)
4989{
4990 struct lysp_node *node_p;
4991 struct lysc_node *child, *iter;
4992 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
4993 struct lysc_node_container context_node_fake =
4994 {.nodetype = LYS_CONTAINER,
4995 .module = ctx->mod,
4996 .flags = parent ? parent->flags : 0,
4997 .child = NULL, .next = NULL,
4998 .prev = (struct lysc_node *)&context_node_fake,
4999 .actions = NULL, .notifs = NULL};
5000 struct lysp_grp *grp = NULL;
5001 LY_ARRAY_COUNT_TYPE u;
5002 uint32_t grp_stack_count;
5003 struct lys_module *grp_mod, *mod_old;
5004 LY_ERR ret = LY_SUCCESS;
5005 struct lysc_when **when, *when_shared;
5006 struct lysp_augment **augments = NULL;
5007 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
5008 struct lysc_notif **notifs = NULL;
5009 struct lysc_action **actions = NULL;
5010
5011 /* find the referenced grouping */
5012 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
5013
5014 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
5015 grp_stack_count = ctx->groupings.count;
5016 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
5017 if (grp_stack_count == ctx->groupings.count) {
5018 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
5019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5020 "Grouping \"%s\" references itself through a uses statement.", grp->name);
5021 return LY_EVALID;
5022 }
5023
5024 /* switch context's mod_def */
5025 mod_old = ctx->mod_def;
5026 ctx->mod_def = grp_mod;
5027
5028 /* check status */
5029 ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, grp_mod, grp->name);
5030 LY_CHECK_GOTO(ret, cleanup);
5031
5032 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
5033 * applu refine and augment only to the nodes from the uses */
5034 if (parent) {
5035 child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5036 } else if (ctx->mod->compiled->data) {
5037 child = ctx->mod->compiled->data;
5038 } else {
5039 child = NULL;
5040 }
5041 /* remember the last child */
5042 if (child) {
5043 child = child->prev;
5044 }
5045
5046 /* compile data nodes */
5047 LY_LIST_FOR(grp->data, node_p) {
5048 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
5049 ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3);
5050 LY_CHECK_GOTO(ret, cleanup);
5051 }
5052
5053 /* split the children and add the uses's data into the fake context node */
5054 if (child) {
5055 context_node_fake.child = child->next;
5056 } else if (parent) {
5057 context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5058 } else if (ctx->mod->compiled->data) {
5059 context_node_fake.child = ctx->mod->compiled->data;
5060 }
5061 if (context_node_fake.child) {
5062 /* remember child as the last data node added by grouping to fix the list later */
5063 child = context_node_fake.child->prev;
5064 context_node_fake.child->prev = NULL;
5065 }
5066
5067 when_shared = NULL;
5068 LY_LIST_FOR(context_node_fake.child, iter) {
5069 iter->parent = (struct lysc_node *)&context_node_fake;
5070
5071 /* pass uses's when to all the data children, actions and notifications are ignored */
5072 if (uses_p->when) {
5073 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
5074 if (!when_shared) {
5075 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when);
5076 LY_CHECK_GOTO(ret, cleanup);
5077
5078 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5079 /* do not check "when" semantics in a grouping */
5080 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5081 LY_CHECK_GOTO(ret, cleanup);
5082 }
5083
5084 when_shared = *when;
5085 } else {
5086 ++when_shared->refcount;
5087 (*when) = when_shared;
5088
5089 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5090 /* in this case check "when" again for all children because of dummy node check */
5091 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5092 LY_CHECK_GOTO(ret, cleanup);
5093 }
5094 }
5095 }
5096 }
5097
5098 /* compile actions */
5099 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5100 if (actions) {
5101 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
5102 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
5103 if (*actions && (uses_p->augments || uses_p->refines)) {
5104 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5105 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
5106 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
5107 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
5108 }
5109 }
5110
5111 /* compile notifications */
5112 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5113 if (notifs) {
5114 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
5115 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
5116 if (*notifs && (uses_p->augments || uses_p->refines)) {
5117 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5118 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
5119 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
5120 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
5121 }
5122 }
5123
5124 /* sort and apply augments */
5125 ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments);
5126 LY_CHECK_GOTO(ret, cleanup);
5127 LY_ARRAY_FOR(augments, u) {
5128 ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake);
5129 LY_CHECK_GOTO(ret, cleanup);
5130 }
5131
5132 /* reload previous context's mod_def */
5133 ctx->mod_def = mod_old;
5134
5135 /* apply all refines */
5136 ret = lys_compile_refines(ctx, uses_p->refines, (struct lysc_node *)&context_node_fake);
5137 LY_CHECK_GOTO(ret, cleanup);
5138
Radek Krejcic6b4f442020-08-12 14:45:18 +02005139 if (first_p) {
5140 *first_p = context_node_fake.child;
5141 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005142
5143cleanup:
5144 /* fix connection of the children nodes from fake context node back into the parent */
5145 if (context_node_fake.child) {
5146 context_node_fake.child->prev = child;
5147 }
5148 LY_LIST_FOR(context_node_fake.child, child) {
5149 child->parent = parent;
5150 }
5151
5152 if (uses_p->augments || uses_p->refines) {
5153 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005154 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005155 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005156 LY_ARRAY_FREE(context_node_fake.actions);
5157 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005158 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005159 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005160 LY_ARRAY_FREE(context_node_fake.notifs);
5161 }
5162 }
5163
Radek Krejcie86bf772018-12-14 11:39:53 +01005164 /* reload previous context's mod_def */
5165 ctx->mod_def = mod_old;
5166 /* remove the grouping from the stack for circular groupings dependency check */
5167 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5168 assert(ctx->groupings.count == grp_stack_count);
Radek Krejci3641f562019-02-13 15:38:40 +01005169 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005170
5171 return ret;
5172}
5173
Radek Krejci327de162019-06-14 12:52:07 +02005174static int
5175lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5176{
5177 struct lysp_node *iter;
5178 int len = 0;
5179
5180 *path = NULL;
5181 for (iter = node; iter && len >= 0; iter = iter->parent) {
5182 char *s = *path;
5183 char *id;
5184
5185 switch (iter->nodetype) {
5186 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005187 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005188 break;
5189 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005190 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005191 break;
5192 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005193 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005194 break;
5195 default:
5196 id = strdup(iter->name);
5197 break;
5198 }
5199
5200 if (!iter->parent) {
5201 /* print prefix */
5202 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5203 } else {
5204 /* prefix is the same as in parent */
5205 len = asprintf(path, "/%s%s", id, s ? s : "");
5206 }
5207 free(s);
5208 free(id);
5209 }
5210
5211 if (len < 0) {
5212 free(*path);
5213 *path = NULL;
5214 } else if (len == 0) {
5215 *path = strdup("/");
5216 len = 1;
5217 }
5218 return len;
5219}
5220
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005221/**
5222 * @brief Validate groupings that were defined but not directly used in the schema itself.
5223 *
5224 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5225 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5226 */
5227static LY_ERR
5228lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5229{
5230 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005231 char *path;
5232 int len;
5233
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005234 struct lysp_node_uses fake_uses = {
5235 .parent = node_p,
5236 .nodetype = LYS_USES,
5237 .flags = 0, .next = NULL,
5238 .name = grp->name,
5239 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5240 .refines = NULL, .augments = NULL
5241 };
5242 struct lysc_node_container fake_container = {
5243 .nodetype = LYS_CONTAINER,
5244 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5245 .module = ctx->mod,
5246 .sp = NULL, .parent = NULL, .next = NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +02005247 .prev = (struct lysc_node *)&fake_container,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005248 .name = "fake",
5249 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5250 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5251 };
5252
5253 if (grp->parent) {
5254 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5255 }
Radek Krejci327de162019-06-14 12:52:07 +02005256
5257 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5258 if (len < 0) {
5259 LOGMEM(ctx->ctx);
5260 return LY_EMEM;
5261 }
5262 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005263 ctx->path_len = (uint32_t)len;
Radek Krejci327de162019-06-14 12:52:07 +02005264 free(path);
5265
5266 lysc_update_path(ctx, NULL, "{grouping}");
5267 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02005268 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005269 lysc_update_path(ctx, NULL, NULL);
5270 lysc_update_path(ctx, NULL, NULL);
5271
5272 ctx->path_len = 1;
5273 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005274
5275 /* cleanup */
5276 lysc_node_container_free(ctx->ctx, &fake_container);
5277
5278 return ret;
5279}
Radek Krejcife909632019-02-12 15:34:42 +01005280
Radek Krejcie86bf772018-12-14 11:39:53 +01005281/**
Michal Vasko20424b42020-08-31 12:29:38 +02005282 * @brief Set config flags for a node.
5283 *
5284 * @param[in] ctx Compile context.
5285 * @param[in] node Compiled node config to set.
5286 * @param[in] parent Parent of @p node.
5287 * @return LY_ERR value.
5288 */
5289static LY_ERR
5290lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
5291{
5292 if (node->nodetype == LYS_CASE) {
5293 /* case never has any config */
5294 assert(!(node->flags & LYS_CONFIG_MASK));
5295 return LY_SUCCESS;
5296 }
5297
5298 /* adjust parent to always get the ancestor with config */
5299 if (parent && (parent->nodetype == LYS_CASE)) {
5300 parent = parent->parent;
5301 assert(parent);
5302 }
5303
5304 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
5305 /* ignore config statements inside RPC/action data */
5306 node->flags &= ~LYS_CONFIG_MASK;
5307 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5308 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
5309 /* ignore config statements inside Notification data */
5310 node->flags &= ~LYS_CONFIG_MASK;
5311 node->flags |= LYS_CONFIG_R;
5312 } else if (!(node->flags & LYS_CONFIG_MASK)) {
5313 /* config not explicitely set, inherit it from parent */
5314 if (parent) {
5315 node->flags |= parent->flags & LYS_CONFIG_MASK;
5316 } else {
5317 /* default is config true */
5318 node->flags |= LYS_CONFIG_W;
5319 }
5320 } else {
5321 /* config set explicitely */
5322 node->flags |= LYS_SET_CONFIG;
5323 }
5324
5325 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5326 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5327 "Configuration node cannot be child of any state data node.");
5328 return LY_EVALID;
5329 }
5330
5331 return LY_SUCCESS;
5332}
5333
5334/**
Radek Krejcia3045382018-11-22 14:30:31 +01005335 * @brief Compile parsed schema node information.
5336 * @param[in] ctx Compile context
5337 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005338 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5339 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5340 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005341 * @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).
5342 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005343 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5344 */
Radek Krejci19a96102018-11-15 13:38:09 +01005345static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005346lys_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 +01005347{
Radek Krejci1c54f462020-05-12 17:25:34 +02005348 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005349 struct lysc_node *node = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005350 struct lysc_when **when;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005351 LY_ARRAY_COUNT_TYPE u;
Michal Vasko22df3f02020-08-24 13:29:22 +02005352 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
Radek Krejci19a96102018-11-15 13:38:09 +01005353
Radek Krejci327de162019-06-14 12:52:07 +02005354 if (node_p->nodetype != LYS_USES) {
5355 lysc_update_path(ctx, parent, node_p->name);
5356 } else {
5357 lysc_update_path(ctx, NULL, "{uses}");
5358 lysc_update_path(ctx, NULL, node_p->name);
5359 }
5360
Radek Krejci19a96102018-11-15 13:38:09 +01005361 switch (node_p->nodetype) {
5362 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02005363 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
Radek Krejci19a96102018-11-15 13:38:09 +01005364 node_compile_spec = lys_compile_node_container;
5365 break;
5366 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005367 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
Radek Krejci19a96102018-11-15 13:38:09 +01005368 node_compile_spec = lys_compile_node_leaf;
5369 break;
5370 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005371 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005372 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005373 break;
5374 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005375 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005376 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005377 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005378 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02005379 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005380 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005381 break;
Michal Vasko20424b42020-08-31 12:29:38 +02005382 case LYS_CASE:
5383 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
5384 node_compile_spec = lys_compile_node_case;
5385 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005386 case LYS_ANYXML:
5387 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005388 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005389 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005390 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005391 case LYS_USES:
Michal Vasko9e8de2d2020-09-01 08:17:10 +02005392 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, parent, &node);
Radek Krejci327de162019-06-14 12:52:07 +02005393 lysc_update_path(ctx, NULL, NULL);
5394 lysc_update_path(ctx, NULL, NULL);
5395 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005396 default:
5397 LOGINT(ctx->ctx);
5398 return LY_EINT;
5399 }
5400 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5401 node->nodetype = node_p->nodetype;
5402 node->module = ctx->mod;
5403 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005404 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005405
5406 /* config */
Michal Vasko20424b42020-08-31 12:29:38 +02005407 ret = lys_compile_config(ctx, node, parent);
5408 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005409
Michal Vasko20424b42020-08-31 12:29:38 +02005410 /* list ordering */
Radek Krejcia6d57732018-11-29 13:40:37 +01005411 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5412 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005413 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci0f969882020-08-21 16:56:47 +02005414 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5415 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005416 node->flags &= ~LYS_ORDBY_MASK;
5417 node->flags |= LYS_ORDBY_SYSTEM;
5418 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5419 /* default ordering is system */
5420 node->flags |= LYS_ORDBY_SYSTEM;
5421 }
5422 }
5423
Radek Krejci19a96102018-11-15 13:38:09 +01005424 /* status - it is not inherited by specification, but it does not make sense to have
5425 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko20424b42020-08-31 12:29:38 +02005426 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 +01005427
Radek Krejciec4da802019-05-02 13:02:41 +02005428 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005429 node->sp = node_p;
5430 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02005431 DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error);
5432 DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error);
5433 DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error);
Radek Krejci00b874b2019-02-12 10:54:50 +01005434 if (node_p->when) {
5435 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005436 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005437
5438 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5439 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02005440 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
5441 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005442 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005443 }
Radek Krejciec4da802019-05-02 13:02:41 +02005444 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005445
Michal Vasko20424b42020-08-31 12:29:38 +02005446 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
5447 LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node));
5448
Radek Krejci19a96102018-11-15 13:38:09 +01005449 /* nodetype-specific part */
Michal Vasko20424b42020-08-31 12:29:38 +02005450 LY_CHECK_RET(node_compile_spec(ctx, node_p, node));
Radek Krejci19a96102018-11-15 13:38:09 +01005451
Michal Vasko20424b42020-08-31 12:29:38 +02005452 /* final compilation tasks that require the node to be connected */
5453 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, done);
Radek Krejcife909632019-02-12 15:34:42 +01005454 if (node->flags & LYS_MAND_TRUE) {
Michal Vasko20424b42020-08-31 12:29:38 +02005455 /* inherit LYS_MAND_TRUE in parent containers */
Radek Krejcife909632019-02-12 15:34:42 +01005456 lys_compile_mandatory_parents(parent, 1);
5457 }
5458
Radek Krejci327de162019-06-14 12:52:07 +02005459 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005460 return LY_SUCCESS;
5461
5462error:
5463 lysc_node_free(ctx->ctx, node);
Michal Vasko20424b42020-08-31 12:29:38 +02005464done:
Radek Krejci19a96102018-11-15 13:38:09 +01005465 return ret;
5466}
5467
Radek Krejciccd20f12019-02-15 14:12:27 +01005468static void
Michal Vasko20424b42020-08-31 12:29:38 +02005469lysc_node_unlink(struct lysc_node *node)
Radek Krejciccd20f12019-02-15 14:12:27 +01005470{
Michal Vasko20424b42020-08-31 12:29:38 +02005471 struct lysc_node *parent, *child;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005472 struct lysc_module *modc = node->module->compiled;
Radek Krejciccd20f12019-02-15 14:12:27 +01005473
5474 parent = node->parent;
5475
5476 /* parent's first child */
Michal Vasko20424b42020-08-31 12:29:38 +02005477 if (parent && lysc_node_children(parent, node->flags) == node) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005478 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005479 } else if (modc->data == node) {
5480 modc->data = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005481 }
Michal Vasko20424b42020-08-31 12:29:38 +02005482
5483 /* special choice case unlinking */
5484 if (parent && parent->nodetype == LYS_CHOICE) {
5485 if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) {
5486 /* default case removed */
5487 ((struct lysc_node_choice *)parent)->dflt = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005488 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005489 }
5490
5491 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005492 if (node->prev->next) {
5493 node->prev->next = node->next;
5494 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005495 if (node->next) {
5496 node->next->prev = node->prev;
Michal Vasko20424b42020-08-31 12:29:38 +02005497 } else {
5498 /* last child */
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005499 if (parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005500 child = (struct lysc_node *)lysc_node_children(parent, node->flags);
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005501 } else {
5502 child = modc->data;
5503 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005504 if (child) {
5505 child->prev = node->prev;
5506 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005507 }
5508}
5509
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005510struct lysc_deviation {
5511 const char *nodeid;
5512 struct lysc_node *target; /* target node of the deviation */
Michal Vasko22df3f02020-08-24 13:29:22 +02005513 struct lysp_deviate **deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02005514 uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */
Radek Krejci857189e2020-09-01 13:26:36 +02005515 ly_bool not_supported; /* flag if deviates contains not-supported deviate */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005516};
5517
Michal Vaskoccc062a2020-08-13 08:34:50 +02005518/* MACROS for deviates checking */
5519#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5520 if (!(target->nodetype & (NODETYPES))) { \
5521 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5522 goto cleanup; \
5523 }
5524
5525#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5526 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5528 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5529 goto cleanup; \
5530 }
5531
5532#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5533 if (!((TYPE)target)->MEMBER || COND) { \
5534 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5535 goto cleanup; \
5536 }
5537
5538/**
5539 * @brief Apply deviate add.
5540 *
5541 * @param[in] ctx Compile context.
5542 * @param[in] target Deviation target.
5543 * @param[in] dev_flags Internal deviation flags.
5544 * @param[in] d Deviate add to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02005545 * @return LY_ERR value.
5546 */
5547static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005548lys_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 +02005549{
Radek Krejci011e4aa2020-09-04 15:22:31 +02005550 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005551 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5552 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005553 LY_ARRAY_COUNT_TYPE x;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005554
5555#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5556 if (((TYPE)target)->MEMBER COND) { \
5557 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5558 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5559 PROPERTY, ((TYPE)target)->MEMBER); \
5560 goto cleanup; \
5561 }
5562
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005563#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005564 if (((TYPE)target)->MEMBER && (COND)) { \
Radek Krejci857189e2020-09-01 13:26:36 +02005565 ly_bool dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005566 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005567 ctx->mod_def, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5569 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5570 if (dynamic_) {free((void*)val_);} \
5571 goto cleanup; \
5572 }
5573
5574#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5575 if (((TYPE)target)->MEMBER && (COND)) { \
5576 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5577 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5578 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5579 goto cleanup; \
5580 }
5581
5582 /* [units-stmt] */
5583 if (d->units) {
5584 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5585 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5586
5587 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02005588 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc);
5589 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005590 }
5591
5592 /* *must-stmt */
5593 if (d->musts) {
5594 switch (target->nodetype) {
5595 case LYS_CONTAINER:
5596 case LYS_LIST:
5597 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5598 x, lys_compile_must, ret, cleanup);
5599 break;
5600 case LYS_LEAF:
5601 case LYS_LEAFLIST:
5602 case LYS_ANYDATA:
5603 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5604 x, lys_compile_must, ret, cleanup);
5605 break;
5606 case LYS_NOTIF:
5607 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5608 x, lys_compile_must, ret, cleanup);
5609 break;
5610 case LYS_RPC:
5611 case LYS_ACTION:
5612 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5613 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5614 x, lys_compile_must, ret, cleanup);
5615 break;
5616 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5617 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5618 x, lys_compile_must, ret, cleanup);
5619 break;
5620 }
Radek Krejci0f969882020-08-21 16:56:47 +02005621 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005622 default:
5623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5624 lys_nodetype2str(target->nodetype), "add", "must");
5625 goto cleanup;
5626 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02005627 ret = ly_set_add(&ctx->xpath, target, 0, NULL);
5628 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005629 }
5630
5631 /* *unique-stmt */
5632 if (d->uniques) {
5633 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5634 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5635 }
5636
5637 /* *default-stmt */
5638 if (d->dflts) {
5639 switch (target->nodetype) {
5640 case LYS_LEAF:
5641 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005642 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005643 if (leaf->dflt) {
5644 /* first, remove the default value taken from the type */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005645 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5646 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005647 free(leaf->dflt);
5648 leaf->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005649 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005650
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005651 /* store the default value in unres */
Michal Vasko31a82d52020-08-21 08:11:48 +02005652 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005653 target->flags |= LYS_SET_DFLT;
5654 break;
5655 case LYS_LEAFLIST:
5656 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5657 /* first, remove the default value taken from the type */
5658 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005659 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5660 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5661 free(llist->dflts[x]);
5662 }
5663 LY_ARRAY_FREE(llist->dflts);
5664 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005665 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005666
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005667 /* store the default values in unres */
5668 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005669 target->flags |= LYS_SET_DFLT;
5670 break;
5671 case LYS_CHOICE:
5672 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5673 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5674 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5675 * to allow making the default case even the augmented case from the deviating module */
5676 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5677 goto cleanup;
5678 }
5679 break;
5680 default:
5681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5682 lys_nodetype2str(target->nodetype), "add", "default");
5683 goto cleanup;
5684 }
5685 }
5686
5687 /* [config-stmt] */
5688 if (d->flags & LYS_CONFIG_MASK) {
5689 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5691 lys_nodetype2str(target->nodetype), "add", "config");
5692 goto cleanup;
5693 }
5694 if (dev_flags) {
5695 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5696 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5697 }
5698 if (target->flags & LYS_SET_CONFIG) {
5699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5700 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5701 target->flags & LYS_CONFIG_W ? "true" : "false");
5702 goto cleanup;
5703 }
5704 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5705 }
5706
5707 /* [mandatory-stmt] */
5708 if (d->flags & LYS_MAND_MASK) {
5709 if (target->flags & LYS_MAND_MASK) {
5710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5711 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5712 target->flags & LYS_MAND_TRUE ? "true" : "false");
5713 goto cleanup;
5714 }
5715 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5716 }
5717
5718 /* [min-elements-stmt] */
5719 if (d->flags & LYS_SET_MIN) {
5720 if (target->nodetype == LYS_LEAFLIST) {
5721 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5722 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005723 ((struct lysc_node_leaflist *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005724 } else if (target->nodetype == LYS_LIST) {
5725 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5726 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005727 ((struct lysc_node_list *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005728 } else {
5729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5730 lys_nodetype2str(target->nodetype), "add", "min-elements");
5731 goto cleanup;
5732 }
5733 if (d->min) {
5734 target->flags |= LYS_MAND_TRUE;
5735 }
5736 }
5737
5738 /* [max-elements-stmt] */
5739 if (d->flags & LYS_SET_MAX) {
5740 if (target->nodetype == LYS_LEAFLIST) {
5741 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5742 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005743 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005744 } else if (target->nodetype == LYS_LIST) {
5745 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5746 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005747 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005748 } else {
5749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5750 lys_nodetype2str(target->nodetype), "add", "max-elements");
5751 goto cleanup;
5752 }
5753 }
5754
5755 ret = LY_SUCCESS;
5756
5757cleanup:
5758 return ret;
5759}
5760
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005761static LY_ERR
5762lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt)
5763{
5764 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005765 ly_bool dyn = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005766 const char *orig_dflt;
5767 uint32_t i;
5768
5769 if (target->module != ctx->mod) {
5770 /* foreign deviation */
5771 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt);
5772
5773 /* check that the value matches */
5774 orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5775 if (strcmp(orig_dflt, dflt)) {
5776 goto error;
5777 }
5778 if (dyn) {
5779 free((char *)orig_dflt);
5780 }
5781
5782 /* remove the default specification */
5783 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5784 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5785 free(leaf->dflt);
5786 leaf->dflt = NULL;
5787 } else {
5788 /* local deviation */
5789 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt);
5790
5791 /* find the incomplete default */
5792 orig_dflt = NULL;
5793 for (i = 0; i < ctx->dflts.count; ++i) {
5794 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
5795 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5796 break;
5797 }
5798 }
5799 LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT);
5800
5801 /* check that the value matches */
5802 if (strcmp(orig_dflt, dflt)) {
5803 goto error;
5804 }
5805
5806 /* update the list of incomplete default values */
5807 lysc_incomplete_dflt_remove(ctx, target);
5808 }
5809
5810 target->flags &= ~LYS_SET_DFLT;
5811 return LY_SUCCESS;
5812
5813error:
5814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5815 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5816 dflt, orig_dflt);
5817 if (dyn) {
5818 free((char *)orig_dflt);
5819 }
5820cleanup:
5821 return LY_EVALID;
5822}
5823
5824static LY_ERR
5825lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts)
5826{
5827 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005828 ly_bool dyn = 0, found;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005829 const char *orig_dflt, **orig_dflts;
5830 uint32_t i;
5831 LY_ARRAY_COUNT_TYPE x, y;
5832
5833 if (target->module != ctx->mod) {
5834 /* foreign deviation */
5835 DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]);
5836 LY_ARRAY_FOR(dflts, x) {
5837 found = 0;
5838 LY_ARRAY_FOR(llist->dflts, y) {
5839 orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5840 if (!strcmp(orig_dflt, dflts[x])) {
5841 if (dyn) {
5842 free((char *)orig_dflt);
5843 }
5844 found = 1;
5845 break;
5846 }
5847 if (dyn) {
5848 free((char *)orig_dflt);
5849 }
5850 }
5851 if (!found) {
5852 goto error;
5853 }
5854
5855 /* update compiled default values */
5856 LY_ARRAY_DECREMENT(llist->dflts);
5857 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
5858 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
5859 free(llist->dflts[y]);
5860 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
5861 }
5862 if (!LY_ARRAY_COUNT(llist->dflts)) {
5863 LY_ARRAY_FREE(llist->dflts);
5864 llist->dflts = NULL;
5865 llist->flags &= ~LYS_SET_DFLT;
5866 }
5867 } else {
5868 /* local deviation */
5869 orig_dflt = NULL;
5870 orig_dflts = NULL;
5871 for (i = 0; i < ctx->dflts.count; ++i) {
5872 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) {
5873 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5874 orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts;
5875 break;
5876 }
5877 }
5878 LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT);
5879
5880 if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) {
5881 /* TODO it is not currently possible to remove just one default value from incomplete defaults array */
5882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5883 "Local deviation deleting leaf-list defaults is not supported.");
5884 return LY_EVALID;
5885 }
5886
5887 LY_ARRAY_FOR(dflts, x) {
5888 found = 0;
5889 if (orig_dflts) {
5890 LY_ARRAY_FOR(orig_dflts, y) {
5891 if (!strcmp(orig_dflts[y], dflts[x])) {
5892 found = 1;
5893 break;
5894 }
5895 }
5896 } else if (!strcmp(orig_dflt, dflts[x])) {
5897 found = 1;
5898 }
5899 if (!found) {
5900 goto error;
5901 }
5902
5903 /* update the list of incomplete default values */
5904 lysc_incomplete_dflt_remove(ctx, target);
5905 }
5906
5907 llist->flags &= ~LYS_SET_DFLT;
5908 }
5909
5910 return LY_SUCCESS;
5911
5912error:
5913 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
5914 "which does not match any of the target's property values.", dflts[x]);
5915cleanup:
5916 return LY_EVALID;
5917}
5918
Michal Vaskoccc062a2020-08-13 08:34:50 +02005919/**
5920 * @brief Apply deviate delete.
5921 *
5922 * @param[in] ctx Compile context.
5923 * @param[in] target Deviation target.
5924 * @param[in] dev_flags Internal deviation flags.
5925 * @param[in] d Deviate delete to apply.
5926 * @return LY_ERR value.
5927 */
5928static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005929lys_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 +02005930{
5931 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005932 struct lysc_node_list *list = (struct lysc_node_list *)target;
5933 LY_ARRAY_COUNT_TYPE x, y, z;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005934 size_t prefix_len, name_len;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005935 const char *prefix, *name, *nodeid;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005936 struct lys_module *mod;
5937
5938#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5939 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5940 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5941 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5942 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5943 } \
5944 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5945 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5946 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5947 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5948 goto cleanup; \
5949 } \
5950 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5951 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5952 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5953 &((TYPE)target)->ARRAY_TRG[y + 1], \
5954 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5955 } \
5956 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5957 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5958 ((TYPE)target)->ARRAY_TRG = NULL; \
5959 }
5960
5961 /* [units-stmt] */
5962 if (d->units) {
5963 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Michal Vasko22df3f02020-08-24 13:29:22 +02005964 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units);
5965 if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005966 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5967 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02005968 d->units, ((struct lysc_node_leaf *)target)->units);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005969 goto cleanup;
5970 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005971 lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5972 ((struct lysc_node_leaf *)target)->units = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005973 }
5974
5975 /* *must-stmt */
5976 if (d->musts) {
5977 switch (target->nodetype) {
5978 case LYS_CONTAINER:
5979 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005980 DEV_DEL_ARRAY(struct lysc_node_container *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005981 break;
5982 case LYS_LEAF:
5983 case LYS_LEAFLIST:
5984 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005985 DEV_DEL_ARRAY(struct lysc_node_leaf *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005986 break;
5987 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005988 DEV_DEL_ARRAY(struct lysc_notif *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005989 break;
5990 case LYS_RPC:
5991 case LYS_ACTION:
5992 if (dev_flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005993 DEV_DEL_ARRAY(struct lysc_action *, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005994 break;
5995 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005996 DEV_DEL_ARRAY(struct lysc_action *, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005997 break;
5998 }
Radek Krejci0f969882020-08-21 16:56:47 +02005999 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02006000 default:
6001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6002 lys_nodetype2str(target->nodetype), "delete", "must");
6003 goto cleanup;
6004 }
6005 }
6006
6007 /* *unique-stmt */
6008 if (d->uniques) {
6009 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6010 LY_ARRAY_FOR(d->uniques, x) {
6011 LY_ARRAY_FOR(list->uniques, z) {
6012 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
6013 nodeid = strpbrk(name, " \t\n");
6014 if (nodeid) {
6015 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
6016 break;
6017 }
6018 while (isspace(*nodeid)) {
6019 ++nodeid;
6020 }
6021 } else {
6022 if (strcmp(name, list->uniques[z][y]->name)) {
6023 break;
6024 }
6025 }
6026 }
6027 if (!name) {
6028 /* complete match - remove the unique */
6029 LY_ARRAY_DECREMENT(list->uniques);
6030 LY_ARRAY_FREE(list->uniques[z]);
6031 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
6032 --z;
6033 break;
6034 }
6035 }
6036 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
6037 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6038 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6039 d->uniques[x]);
6040 goto cleanup;
6041 }
6042 }
6043 if (!LY_ARRAY_COUNT(list->uniques)) {
6044 LY_ARRAY_FREE(list->uniques);
6045 list->uniques = NULL;
6046 }
6047 }
6048
6049 /* *default-stmt */
6050 if (d->dflts) {
6051 switch (target->nodetype) {
6052 case LYS_LEAF:
6053 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006054 LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006055 break;
6056 case LYS_LEAFLIST:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006057 LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006058 break;
6059 case LYS_CHOICE:
6060 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vasko22df3f02020-08-24 13:29:22 +02006061 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "deleting", "default", d->dflts[0]);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006062 nodeid = d->dflts[0];
6063 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6064 if (prefix) {
6065 /* use module prefixes from the deviation module to match the module of the default case */
6066 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006068 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6069 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6070 goto cleanup;
6071 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006072 if (mod != ((struct lysc_node_choice *)target)->dflt->module) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006073 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006074 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6075 "The prefix does not match the default case's module.", d->dflts[0]);
6076 goto cleanup;
6077 }
6078 }
6079 /* else {
6080 * strictly, the default prefix would point to the deviation module, but the value should actually
6081 * match the default string in the original module (usually unprefixed), so in this case we do not check
6082 * the module of the default case, just matching its name */
Michal Vasko22df3f02020-08-24 13:29:22 +02006083 if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02006084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6085 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02006086 d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006087 goto cleanup;
6088 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006089 ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT;
6090 ((struct lysc_node_choice *)target)->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006091 break;
6092 default:
6093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6094 lys_nodetype2str(target->nodetype), "delete", "default");
6095 goto cleanup;
6096 }
6097 }
6098
6099 ret = LY_SUCCESS;
6100
6101cleanup:
6102 return ret;
6103}
6104
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006105static LY_ERR
6106lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target)
6107{
6108 LY_ERR ret;
6109 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6110 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6111 struct ly_err_item *err = NULL;
6112 LY_ARRAY_COUNT_TYPE x;
6113 const char *dflt;
Radek Krejci857189e2020-09-01 13:26:36 +02006114 ly_bool dyn;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006115
6116 if (target->module != ctx->mod) {
6117 /* foreign deviation */
6118 if (target->nodetype == LYS_LEAF) {
6119 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn);
6120 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6121 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6122
6123 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6124 LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err);
6125 if (dyn) {
6126 free((char *)dflt);
6127 }
6128 if (err) {
6129 ly_err_print(err);
6130 ctx->path[0] = '\0';
6131 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6132 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6133 "Invalid default - value does not fit the type (%s).", err->msg);
6134 ly_err_free(err);
6135 }
6136 LY_CHECK_RET(ret);
6137
6138 ++leaf->dflt->realtype->refcount;
6139 } else { /* LY_LEAFLIST */
6140 LY_ARRAY_FOR(llist->dflts, x) {
6141 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn);
6142 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6143 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6144
6145 ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6146 LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err);
6147 if (dyn) {
6148 free((char *)dflt);
6149 }
6150 if (err) {
6151 ly_err_print(err);
6152 ctx->path[0] = '\0';
6153 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6155 "Invalid default - value does not fit the type (%s).", err->msg);
6156 ly_err_free(err);
6157 }
6158 LY_CHECK_RET(ret);
6159
6160 ++llist->dflts[x]->realtype->refcount;
6161 }
6162 }
6163 } else {
6164 /* local deviation */
6165
6166 /* these default were not compiled yet, so they will use the new type automatically */
6167 }
6168
6169 return LY_SUCCESS;
6170}
6171
Michal Vaskoccc062a2020-08-13 08:34:50 +02006172/**
6173 * @brief Apply deviate replace.
6174 *
6175 * @param[in] ctx Compile context.
6176 * @param[in] target Deviation target.
6177 * @param[in] d Deviate replace to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02006178 * @return LY_ERR value.
6179 */
6180static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006181lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02006182{
Radek Krejci011e4aa2020-09-04 15:22:31 +02006183 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006184 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6185 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6186 LY_ARRAY_COUNT_TYPE x;
6187
6188#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6189 if (!(((TYPE)target)->MEMBER COND)) { \
6190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6191 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6192 goto cleanup; \
6193 }
6194
6195 /* [type-stmt] */
6196 if (d->type) {
6197 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6198 /* type is mandatory, so checking for its presence is not necessary */
6199 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6200
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006201 /* remove only default value inherited from the type */
6202 if (!(target->flags & LYS_SET_DFLT)) {
6203 if (target->module != ctx->mod) {
6204 /* foreign deviation - the target has default from the previous type, remove it */
6205 if (target->nodetype == LYS_LEAF) {
6206 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6207 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6208 free(leaf->dflt);
6209 leaf->dflt = NULL;
6210 } else { /* LYS_LEAFLIST */
6211 LY_ARRAY_FOR(llist->dflts, x) {
6212 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6213 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6214 free(llist->dflts[x]);
6215 }
6216 LY_ARRAY_FREE(llist->dflts);
6217 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006218 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006219 } else {
6220 /* local deviation */
6221 lysc_incomplete_dflt_remove(ctx, target);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006222 }
6223 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006224
6225 LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf));
6226
6227 if (target->flags & LYS_SET_DFLT) {
6228 /* the term default value(s) needs to be recompiled */
6229 LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006230 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006231 }
6232
6233 /* [units-stmt] */
6234 if (d->units) {
6235 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6236 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6237 units, "replacing", "units", d->units);
6238
6239 lydict_remove(ctx->ctx, leaf->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02006240 DUP_STRING(ctx->ctx, d->units, leaf->units, rc);
6241 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006242 }
6243
6244 /* [default-stmt] */
6245 if (d->dflt) {
6246 switch (target->nodetype) {
6247 case LYS_LEAF:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006248 if (target->module != ctx->mod) {
6249 /* foreign deviation */
6250 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing",
6251 "default", d->dflt);
6252
6253 /* remove the default specification */
6254 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6255 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6256 free(leaf->dflt);
6257 leaf->dflt = NULL;
6258 } else {
6259 /* local deviation */
6260 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing",
6261 "default", d->dflt);
6262 assert(!leaf->dflt);
6263 }
6264
6265 /* store the new default value */
6266 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006267 break;
6268 case LYS_CHOICE:
6269 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6270 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6271 goto cleanup;
6272 }
6273 break;
6274 default:
6275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6276 lys_nodetype2str(target->nodetype), "replace", "default");
6277 goto cleanup;
6278 }
6279 }
6280
6281 /* [config-stmt] */
6282 if (d->flags & LYS_CONFIG_MASK) {
6283 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6285 lys_nodetype2str(target->nodetype), "replace", "config");
6286 goto cleanup;
6287 }
6288 if (!(target->flags & LYS_SET_CONFIG)) {
6289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6290 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6291 goto cleanup;
6292 }
6293 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6294 }
6295
6296 /* [mandatory-stmt] */
6297 if (d->flags & LYS_MAND_MASK) {
6298 if (!(target->flags & LYS_MAND_MASK)) {
6299 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6300 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6301 goto cleanup;
6302 }
6303 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6304 }
6305
6306 /* [min-elements-stmt] */
6307 if (d->flags & LYS_SET_MIN) {
6308 if (target->nodetype == LYS_LEAFLIST) {
6309 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6310 /* change value */
6311 ((struct lysc_node_leaflist *)target)->min = d->min;
6312 } else if (target->nodetype == LYS_LIST) {
6313 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6314 /* change value */
6315 ((struct lysc_node_list *)target)->min = d->min;
6316 } else {
6317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6318 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6319 goto cleanup;
6320 }
6321 if (d->min) {
6322 target->flags |= LYS_MAND_TRUE;
6323 }
6324 }
6325
6326 /* [max-elements-stmt] */
6327 if (d->flags & LYS_SET_MAX) {
6328 if (target->nodetype == LYS_LEAFLIST) {
6329 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6330 /* change value */
6331 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6332 } else if (target->nodetype == LYS_LIST) {
6333 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6334 /* change value */
6335 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6336 } else {
6337 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6338 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6339 goto cleanup;
6340 }
6341 }
6342
6343 ret = LY_SUCCESS;
6344
6345cleanup:
6346 return ret;
6347}
6348
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006349/**
6350 * @brief Apply all deviations of one target node.
6351 *
6352 * @param[in] ctx Compile context.
6353 * @param[in] dev Deviation structure to apply.
6354 * @return LY_ERR value.
6355 */
6356static LY_ERR
6357lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006358{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006359 LY_ERR ret = LY_EVALID;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006360 struct lysc_node *target = dev->target;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006361 struct lysc_action *rpcs;
6362 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006363 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006364 LY_ARRAY_COUNT_TYPE v, x;
Radek Krejci551b12c2019-02-19 16:11:21 +01006365 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006366
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006367 lysc_update_path(ctx, NULL, dev->nodeid);
6368
6369 /* not-supported */
6370 if (dev->not_supported) {
6371 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
Radek Krejci0f969882020-08-21 16:56:47 +02006372 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 +02006373 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6374 }
6375
6376#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6377 if (target->parent) { \
6378 ARRAY = (TYPE*)GETFUNC(target->parent); \
6379 } else { \
6380 ARRAY = target->module->compiled->ARRAY; \
6381 } \
6382 LY_ARRAY_FOR(ARRAY, x) { \
6383 if (&ARRAY[x] == (TYPE*)target) { break; } \
6384 } \
6385 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6386 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6387 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6388 LY_ARRAY_DECREMENT(ARRAY); \
6389 }
6390
6391 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6392 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6393 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006394 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6395 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6396 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6397 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006398 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6399 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006400 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6401 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6402 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6403 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006404 } else {
6405 /* remove RPC/action */
6406 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6407 }
6408 } else if (target->nodetype == LYS_NOTIF) {
6409 /* remove Notification */
6410 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6411 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02006412 if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) {
6413 /* remove the target node with its parent case node because it is the only node of the case */
6414 lysc_node_unlink(target->parent);
6415 lysc_node_free(ctx->ctx, target->parent);
6416 } else {
6417 /* remove the target node */
6418 lysc_node_unlink(target);
6419 lysc_node_free(ctx->ctx, target);
6420 }
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006421 }
6422
6423 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6424 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6425 return LY_SUCCESS;
6426 }
6427
6428 /* list of deviates (not-supported is not present in the list) */
6429 LY_ARRAY_FOR(dev->deviates, v) {
6430 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006431 switch (d->mod) {
6432 case LYS_DEV_ADD:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006433 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006434 break;
6435 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006436 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006437 break;
6438 case LYS_DEV_REPLACE:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006439 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006440 break;
6441 default:
6442 LOGINT(ctx->ctx);
6443 goto cleanup;
6444 }
6445 }
6446
6447 /* final check when all deviations of a single target node are applied */
6448
6449 /* check min-max compatibility */
6450 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006451 min = ((struct lysc_node_leaflist *)target)->min;
6452 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006453 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006454 min = ((struct lysc_node_list *)target)->min;
6455 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006456 } else {
6457 min = max = 0;
6458 }
6459 if (min > max) {
6460 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6461 "after deviation: min value %u is bigger than max value %u.", min, max);
6462 goto cleanup;
6463 }
6464
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006465 /* check mandatory - default compatibility */
6466 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6467 && (target->flags & LYS_MAND_TRUE)) {
6468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6469 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6470 goto cleanup;
Michal Vasko22df3f02020-08-24 13:29:22 +02006471 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006472 && (target->flags & LYS_MAND_TRUE)) {
6473 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6474 goto cleanup;
6475 }
6476 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6477 /* mandatory node under a default case */
6478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6479 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6480 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6481 goto cleanup;
6482 }
6483
6484 /* success */
6485 ret = LY_SUCCESS;
6486
6487cleanup:
6488 lysc_update_path(ctx, NULL, NULL);
6489 return ret;
6490}
6491
6492LY_ERR
6493lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6494{
6495 LY_ERR ret = LY_EVALID;
6496 struct ly_set devs_p = {0};
6497 struct ly_set targets = {0};
6498 struct lysc_node *target; /* target target of the deviation */
6499 struct lysp_deviation *dev;
6500 struct lysp_deviate *d, **dp_new;
6501 LY_ARRAY_COUNT_TYPE u, v;
6502 struct lysc_deviation **devs = NULL;
6503 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006504 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006505
6506 /* get all deviations from the module and all its submodules ... */
6507 LY_ARRAY_FOR(mod_p->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006508 LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST, NULL));
Radek Krejciccd20f12019-02-15 14:12:27 +01006509 }
6510 LY_ARRAY_FOR(mod_p->includes, v) {
6511 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006512 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 +01006513 }
6514 }
6515 if (!devs_p.count) {
6516 /* nothing to do */
6517 return LY_SUCCESS;
6518 }
6519
Radek Krejci327de162019-06-14 12:52:07 +02006520 lysc_update_path(ctx, NULL, "{deviation}");
6521
Radek Krejciccd20f12019-02-15 14:12:27 +01006522 /* ... and group them by the target node */
6523 devs = calloc(devs_p.count, sizeof *devs);
6524 for (u = 0; u < devs_p.count; ++u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006525 uint32_t index;
6526
Radek Krejciccd20f12019-02-15 14:12:27 +01006527 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006528 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006529
6530 /* resolve the target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02006531 LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
Michal Vasko22df3f02020-08-24 13:29:22 +02006532 (const struct lysc_node **)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006533 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006534 /* move the target pointer to input/output to make them different from the action and
6535 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6536 * back to the RPC/action node due to a better compatibility and decision code in this function.
6537 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6538 if (flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006539 target = (struct lysc_node *)&((struct lysc_action *)target)->input;
Radek Krejcif538ce52019-03-05 10:46:14 +01006540 flags |= LYSC_OPT_INTERNAL;
6541 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006542 target = (struct lysc_node *)&((struct lysc_action *)target)->output;
Radek Krejcif538ce52019-03-05 10:46:14 +01006543 flags |= LYSC_OPT_INTERNAL;
6544 }
6545 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006546 /* insert into the set of targets with duplicity detection */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006547 ret = ly_set_add(&targets, target, 0, &index);
6548 LY_CHECK_GOTO(ret, cleanup);
6549 if (!devs[index]) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006550 /* new record */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006551 devs[index] = calloc(1, sizeof **devs);
6552 devs[index]->target = target;
6553 devs[index]->nodeid = dev->nodeid;
6554 devs[index]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006555 }
6556 /* add deviates into the deviation's list of deviates */
Michal Vasko5c38f362020-08-24 09:22:51 +02006557 LY_LIST_FOR(dev->deviates, d) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006558 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[index]->deviates, dp_new, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006559 *dp_new = d;
6560 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006561 devs[index]->not_supported = 1;
Radek Krejciccd20f12019-02-15 14:12:27 +01006562 }
6563 }
Radek Krejci327de162019-06-14 12:52:07 +02006564
6565 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006566 }
6567
Radek Krejciccd20f12019-02-15 14:12:27 +01006568 /* apply deviations */
6569 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006570 ly_bool match = 0;
Radek Krejciba03a5a2020-08-27 14:40:41 +02006571
Radek Krejcif538ce52019-03-05 10:46:14 +01006572 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6573 /* fix the target pointer in case of RPC's/action's input/output */
6574 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006575 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006576 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006577 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006578 }
6579 }
6580
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006581 /* remember target module (the target node may be removed) */
6582 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006583
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006584 /* apply the deviation */
6585 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006586
Michal Vaskoe6143202020-07-03 13:02:08 +02006587 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006588 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6589 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006590 match = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006591 break;
6592 }
6593 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02006594 if (!match) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006595 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006596 *dev_mod = mod_p->mod;
6597 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006598 }
6599
Radek Krejci327de162019-06-14 12:52:07 +02006600 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006601 ret = LY_SUCCESS;
6602
6603cleanup:
6604 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6605 LY_ARRAY_FREE(devs[u]->deviates);
6606 free(devs[u]);
6607 }
6608 free(devs);
6609 ly_set_erase(&targets, NULL);
6610 ly_set_erase(&devs_p, NULL);
6611
6612 return ret;
6613}
6614
Radek Krejcib56c7502019-02-13 14:19:54 +01006615/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006616 * @brief Compile the given YANG submodule into the main module.
6617 * @param[in] ctx Compile context
6618 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006619 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6620 */
6621LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006622lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006623{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006624 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006625 LY_ERR ret = LY_SUCCESS;
6626 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006627 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006628 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006629 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006630
Michal Vasko33ff9422020-07-03 09:50:39 +02006631 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006632 /* features are compiled directly into the compiled module structure,
6633 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6634 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006635 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6636 LY_CHECK_GOTO(ret, error);
6637 }
Radek Krejci0af46292019-01-11 16:02:31 +01006638
Michal Vasko33ff9422020-07-03 09:50:39 +02006639 if (!mainmod->mod->dis_identities) {
6640 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6641 LY_CHECK_GOTO(ret, error);
6642 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006643
Radek Krejci474f9b82019-07-24 11:36:37 +02006644 /* data nodes */
6645 LY_LIST_FOR(submod->data, node_p) {
6646 ret = lys_compile_node(ctx, node_p, NULL, 0);
6647 LY_CHECK_GOTO(ret, error);
6648 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006649
Radek Krejciec4da802019-05-02 13:02:41 +02006650 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6651 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006652
Radek Krejcid05cbd92018-12-05 14:26:40 +01006653error:
6654 return ret;
6655}
6656
Radek Krejci335332a2019-09-05 13:03:35 +02006657static void *
6658lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6659{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006660 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci335332a2019-09-05 13:03:35 +02006661 if (substmts[u].stmt == stmt) {
6662 return substmts[u].storage;
6663 }
6664 }
6665 return NULL;
6666}
6667
6668LY_ERR
6669lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6670{
6671 LY_ERR ret = LY_EVALID, r;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006672 LY_ARRAY_COUNT_TYPE u;
Radek Krejci335332a2019-09-05 13:03:35 +02006673 struct lysp_stmt *stmt;
6674 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006675
6676 /* check for invalid substatements */
6677 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006678 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6679 continue;
6680 }
Radek Krejci335332a2019-09-05 13:03:35 +02006681 for (u = 0; substmts[u].stmt; ++u) {
6682 if (substmts[u].stmt == stmt->kw) {
6683 break;
6684 }
6685 }
6686 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6688 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006689 goto cleanup;
6690 }
Radek Krejci335332a2019-09-05 13:03:35 +02006691 }
6692
Radek Krejciad5963b2019-09-06 16:03:05 +02006693 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6694
Radek Krejci335332a2019-09-05 13:03:35 +02006695 /* keep order of the processing the same as the order in the defined substmts,
6696 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6697 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006698 ly_bool stmt_present = 0;
Radek Krejciad5963b2019-09-06 16:03:05 +02006699
Radek Krejci335332a2019-09-05 13:03:35 +02006700 for (stmt = ext->child; stmt; stmt = stmt->next) {
6701 if (substmts[u].stmt != stmt->kw) {
6702 continue;
6703 }
6704
Radek Krejciad5963b2019-09-06 16:03:05 +02006705 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006706 if (substmts[u].storage) {
6707 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006708 case LY_STMT_STATUS:
6709 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6710 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6711 break;
6712 case LY_STMT_UNITS: {
6713 const char **units;
6714
6715 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6716 /* single item */
6717 if (*((const char **)substmts[u].storage)) {
6718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6719 goto cleanup;
6720 }
6721 units = (const char **)substmts[u].storage;
6722 } else {
6723 /* sized array */
6724 const char ***units_array = (const char ***)substmts[u].storage;
6725 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6726 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02006727 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
6728 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +02006729 break;
6730 }
Radek Krejci335332a2019-09-05 13:03:35 +02006731 case LY_STMT_TYPE: {
6732 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6733 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6734
6735 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6736 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006737 if (*(struct lysc_type **)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6739 goto cleanup;
6740 }
6741 compiled = substmts[u].storage;
6742 } else {
6743 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006744 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006745 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006746 compiled = (void *)type;
Radek Krejci335332a2019-09-05 13:03:35 +02006747 }
6748
Radek Krejciad5963b2019-09-06 16:03:05 +02006749 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006750 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL,
6751 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006752 units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02006753 lysp_type_free(ctx->ctx, parsed);
6754 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006755 break;
6756 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006757 case LY_STMT_IF_FEATURE: {
6758 struct lysc_iffeature *iff = NULL;
6759
6760 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6761 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006762 if (((struct lysc_iffeature *)substmts[u].storage)->features) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6764 goto cleanup;
6765 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006766 iff = (struct lysc_iffeature *)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006767 } else {
6768 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006769 struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006770 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6771 }
6772 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6773 break;
6774 }
6775 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6776 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006777 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006778 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.",
6779 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006780 goto cleanup;
6781 }
6782 }
Radek Krejci335332a2019-09-05 13:03:35 +02006783 }
Radek Krejci335332a2019-09-05 13:03:35 +02006784
Radek Krejciad5963b2019-09-06 16:03:05 +02006785 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6787 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6788 goto cleanup;
6789 }
Radek Krejci335332a2019-09-05 13:03:35 +02006790 }
6791
6792 ret = LY_SUCCESS;
6793
6794cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006795 return ret;
6796}
6797
Michal Vasko175012e2019-11-06 15:49:14 +01006798/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006799 * @brief Check when for cyclic dependencies.
6800 * @param[in] set Set with all the referenced nodes.
6801 * @param[in] node Node whose "when" referenced nodes are in @p set.
6802 * @return LY_ERR value
6803 */
6804static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006805lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006806{
6807 struct lyxp_set tmp_set;
6808 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006809 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006810 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006811 struct lysc_when *when;
6812 LY_ERR ret = LY_SUCCESS;
6813
6814 memset(&tmp_set, 0, sizeof tmp_set);
6815
6816 /* prepare in_ctx of the set */
Michal Vaskod989ba02020-08-24 10:59:24 +02006817 for (i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006818 xp_scnode = &set->val.scnodes[i];
6819
Michal Vasko5c4e5892019-11-14 12:31:38 +01006820 if (xp_scnode->in_ctx != -1) {
6821 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006822 xp_scnode->in_ctx = 1;
6823 }
6824 }
6825
6826 for (i = 0; i < set->used; ++i) {
6827 xp_scnode = &set->val.scnodes[i];
6828 if (xp_scnode->in_ctx != 1) {
6829 /* already checked */
6830 continue;
6831 }
6832
Michal Vasko1bf09392020-03-27 12:38:10 +01006833 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006834 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006835 /* no when to check */
6836 xp_scnode->in_ctx = 0;
6837 continue;
6838 }
6839
6840 node = xp_scnode->scnode;
6841 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006842 LY_ARRAY_FOR(node->when, u) {
6843 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006844 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006845 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6846 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006847 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006848 goto cleanup;
6849 }
6850
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006851 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006852 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006853 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006854 /* try to find this node in our set */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006855 uint32_t idx;
6856 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 +01006857 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 +01006858 ret = LY_EVALID;
6859 goto cleanup;
6860 }
6861
6862 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006863 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006864 } else {
6865 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006866 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006867 }
6868 }
6869
6870 /* merge this set into the global when set */
6871 lyxp_set_scnode_merge(set, &tmp_set);
6872 }
6873
6874 /* check when of non-data parents as well */
6875 node = node->parent;
6876 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6877
Michal Vasko251f56e2019-11-14 16:06:47 +01006878 /* this node when was checked (xp_scnode could have been reallocd) */
6879 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006880 }
6881
6882cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006883 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006884 return ret;
6885}
6886
6887/**
Michal Vasko175012e2019-11-06 15:49:14 +01006888 * @brief Check when/must expressions of a node on a compiled schema tree.
6889 * @param[in] ctx Compile context.
6890 * @param[in] node Node to check.
6891 * @return LY_ERR value
6892 */
6893static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006894lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006895{
Michal Vasko175012e2019-11-06 15:49:14 +01006896 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006897 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006898 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006899 uint32_t opts;
Radek Krejci857189e2020-09-01 13:26:36 +02006900 ly_bool input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006901 struct lysc_when **when = NULL;
6902 struct lysc_must *musts = NULL;
6903 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006904 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006905
6906 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006907 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006908 if (node->flags & LYS_CONFIG_R) {
6909 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6910 if (op) {
6911 /* we are actually in output */
6912 opts = LYXP_SCNODE_OUTPUT;
6913 }
6914 }
Michal Vasko175012e2019-11-06 15:49:14 +01006915
6916 switch (node->nodetype) {
6917 case LYS_CONTAINER:
6918 when = ((struct lysc_node_container *)node)->when;
6919 musts = ((struct lysc_node_container *)node)->musts;
6920 break;
6921 case LYS_CHOICE:
6922 when = ((struct lysc_node_choice *)node)->when;
6923 break;
6924 case LYS_LEAF:
6925 when = ((struct lysc_node_leaf *)node)->when;
6926 musts = ((struct lysc_node_leaf *)node)->musts;
6927 break;
6928 case LYS_LEAFLIST:
6929 when = ((struct lysc_node_leaflist *)node)->when;
6930 musts = ((struct lysc_node_leaflist *)node)->musts;
6931 break;
6932 case LYS_LIST:
6933 when = ((struct lysc_node_list *)node)->when;
6934 musts = ((struct lysc_node_list *)node)->musts;
6935 break;
6936 case LYS_ANYXML:
6937 case LYS_ANYDATA:
6938 when = ((struct lysc_node_anydata *)node)->when;
6939 musts = ((struct lysc_node_anydata *)node)->musts;
6940 break;
6941 case LYS_CASE:
6942 when = ((struct lysc_node_case *)node)->when;
6943 break;
6944 case LYS_NOTIF:
6945 musts = ((struct lysc_notif *)node)->musts;
6946 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006947 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006948 case LYS_ACTION:
6949 /* first process input musts */
6950 musts = ((struct lysc_action *)node)->input.musts;
6951 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006952 default:
6953 /* nothing to check */
6954 break;
6955 }
6956
Michal Vasko175012e2019-11-06 15:49:14 +01006957 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006958 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006959 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 +02006960 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006961 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006962 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006963 goto cleanup;
6964 }
6965
Michal Vaskodc052f32019-11-07 11:11:38 +01006966 ctx->path[0] = '\0';
6967 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006968 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006969 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006970 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6971 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006972
Michal Vaskoecd62de2019-11-13 12:35:11 +01006973 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006974 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006975 schema->name);
6976 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006977
6978 /* check dummy node accessing */
6979 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006980 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006981 ret = LY_EVALID;
6982 goto cleanup;
6983 }
Michal Vasko175012e2019-11-06 15:49:14 +01006984 }
6985 }
6986
Michal Vaskoecd62de2019-11-13 12:35:11 +01006987 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006988 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006989 LY_CHECK_GOTO(ret, cleanup);
6990
Michal Vaskod3678892020-05-21 10:06:58 +02006991 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006992 }
6993
Michal Vasko5d8756a2019-11-07 15:21:00 +01006994check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006995 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006996 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006997 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 +01006998 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006999 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007000 goto cleanup;
7001 }
7002
Michal Vaskodc052f32019-11-07 11:11:38 +01007003 ctx->path[0] = '\0';
7004 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007005 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007006 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007007 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01007008 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007009 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
7010 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01007011 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01007012 }
7013 }
7014
Michal Vaskod3678892020-05-21 10:06:58 +02007015 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007016 }
7017
Michal Vasko1bf09392020-03-27 12:38:10 +01007018 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01007019 /* now check output musts */
7020 input_done = 1;
7021 musts = ((struct lysc_action *)node)->output.musts;
7022 opts = LYXP_SCNODE_OUTPUT;
7023 goto check_musts;
7024 }
7025
Michal Vasko175012e2019-11-06 15:49:14 +01007026cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007027 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007028 return ret;
7029}
7030
Michal Vasko8d544252020-03-02 10:19:52 +01007031static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007032lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
7033{
Michal Vasko6b26e742020-07-17 15:02:10 +02007034 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02007035 struct ly_path *p;
7036 struct lysc_type *type;
7037
7038 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7039
7040 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02007041 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
7042 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007043 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02007044
7045 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007046 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02007047 ly_path_free(node->module->ctx, p);
7048
7049 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7050 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7051 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
7052 lref->path->expr, lys_nodetype2str(target->nodetype));
7053 return LY_EVALID;
7054 }
7055
7056 /* check status */
7057 ctx->path[0] = '\0';
7058 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7059 ctx->path_len = strlen(ctx->path);
7060 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
7061 return LY_EVALID;
7062 }
7063 ctx->path_len = 1;
7064 ctx->path[1] = '\0';
7065
7066 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007067 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007068 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007069 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007070 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7071 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7072 return LY_EVALID;
7073 }
7074 }
7075
7076 /* store the target's type and check for circular chain of leafrefs */
7077 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7078 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7079 if (type == (struct lysc_type *)lref) {
7080 /* circular chain detected */
7081 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7082 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7083 return LY_EVALID;
7084 }
7085 }
7086
7087 /* check if leafref and its target are under common if-features */
7088 if (lys_compile_leafref_features_validate(node, target)) {
7089 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7090 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7091 " features applicable to the leafref itself.", lref->path->expr);
7092 return LY_EVALID;
7093 }
7094
7095 return LY_SUCCESS;
7096}
7097
7098static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007099lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7100{
7101 struct lysc_ext_instance *ext;
7102 struct lysp_ext_instance *ext_p = NULL;
7103 struct lysp_stmt *stmt;
7104 const struct lys_module *ext_mod;
7105 LY_ERR ret = LY_SUCCESS;
7106
7107 /* create the parsed extension instance manually */
7108 ext_p = calloc(1, sizeof *ext_p);
7109 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007110 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
7111 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007112 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7113 ext_p->insubstmt_index = 0;
7114
Radek Krejci87e25252020-09-15 13:28:31 +02007115 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
7116 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007117 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
7118 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007119 stmt->kw = LY_STMT_TYPE;
Michal Vasko8d544252020-03-02 10:19:52 +01007120
7121 /* allocate new extension instance */
7122 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7123
7124 /* manually get extension definition module */
7125 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7126
7127 /* compile the extension instance */
7128 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7129
7130cleanup:
7131 lysp_ext_instance_free(ctx->ctx, ext_p);
7132 free(ext_p);
7133 return ret;
7134}
7135
Michal Vasko004d3152020-06-11 19:59:22 +02007136static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007137lys_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 +02007138 const struct lys_module *dflt_mod, struct lyd_value *storage)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007139{
7140 LY_ERR ret;
7141 struct ly_err_item *err = NULL;
7142
7143 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7144 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7145 if (err) {
7146 ly_err_print(err);
7147 ctx->path[0] = '\0';
7148 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7150 "Invalid default - value does not fit the type (%s).", err->msg);
7151 ly_err_free(err);
7152 }
7153 if (!ret) {
7154 ++storage->realtype->refcount;
7155 return LY_SUCCESS;
7156 }
7157 return ret;
7158}
7159
7160static LY_ERR
7161lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +02007162 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007163{
7164 LY_ERR ret;
7165
7166 assert(!leaf->dflt);
7167
7168 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7169 /* ignore default values for keys and mandatory leaves */
7170 return LY_SUCCESS;
7171 }
7172
7173 /* allocate the default value */
7174 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7175 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7176
7177 /* store the default value */
7178 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt);
7179 if (ret) {
7180 free(leaf->dflt);
7181 leaf->dflt = NULL;
7182 }
7183
7184 return ret;
7185}
7186
7187static LY_ERR
7188lys_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 +02007189 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007190{
7191 LY_ERR ret;
7192 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7193
7194 assert(dflt || dflts);
7195
7196 if (llist->dflts) {
7197 /* there were already some defaults and we are adding new by deviations */
7198 assert(dflts);
7199 orig_count = LY_ARRAY_COUNT(llist->dflts);
7200 } else {
7201 orig_count = 0;
7202 }
7203
7204 /* allocate new items */
7205 if (dflts) {
7206 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7207 } else {
7208 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7209 }
7210
7211 /* fill each new default value */
7212 if (dflts) {
7213 LY_ARRAY_FOR(dflts, u) {
7214 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
7215 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod,
7216 llist->dflts[orig_count + u]);
7217 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7218 LY_ARRAY_INCREMENT(llist->dflts);
7219 }
7220 } else {
7221 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
7222 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]);
7223 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7224 LY_ARRAY_INCREMENT(llist->dflts);
7225 }
7226
7227 /* check default value uniqueness */
7228 if (llist->flags & LYS_CONFIG_W) {
7229 /* configuration data values must be unique - so check the default values */
7230 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7231 for (v = 0; v < u; ++v) {
7232 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejci857189e2020-09-01 13:26:36 +02007233 ly_bool dynamic = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007234 const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic);
7235
7236 lysc_update_path(ctx, llist->parent, llist->name);
7237 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7238 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
7239 lysc_update_path(ctx, NULL, NULL);
7240 if (dynamic) {
7241 free((char *)val);
7242 }
7243 return LY_EVALID;
7244 }
7245 }
7246 }
7247 }
7248
7249 return LY_SUCCESS;
7250}
7251
7252static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007253lys_compile_unres(struct lysc_ctx *ctx)
7254{
7255 struct lysc_node *node;
7256 struct lysc_type *type, *typeiter;
7257 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007258 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007259 uint32_t i;
7260
7261 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7262 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7263 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7264 for (i = 0; i < ctx->leafrefs.count; ++i) {
7265 node = ctx->leafrefs.objs[i];
7266 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7267 type = ((struct lysc_node_leaf *)node)->type;
7268 if (type->basetype == LY_TYPE_LEAFREF) {
7269 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7270 } else if (type->basetype == LY_TYPE_UNION) {
7271 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7272 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7273 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7274 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7275 }
7276 }
7277 }
7278 }
7279 for (i = 0; i < ctx->leafrefs.count; ++i) {
7280 /* store pointer to the real type */
7281 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7282 if (type->basetype == LY_TYPE_LEAFREF) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007283 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007284 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007285 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7286 ((struct lysc_type_leafref *)type)->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007287 } else if (type->basetype == LY_TYPE_UNION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007288 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7289 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7290 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007291 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007292 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7293 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007294 }
7295 }
7296 }
7297 }
7298
7299 /* check xpath */
7300 for (i = 0; i < ctx->xpath.count; ++i) {
7301 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7302 }
7303
7304 /* finish incomplete default values compilation */
7305 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007306 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007307 if (r->leaf->nodetype == LYS_LEAF) {
7308 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod));
7309 } else {
7310 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 +02007311 }
Michal Vasko004d3152020-06-11 19:59:22 +02007312 }
7313
7314 return LY_SUCCESS;
7315}
7316
Radek Krejci19a96102018-11-15 13:38:09 +01007317LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02007318lys_compile(struct lys_module *mod, uint32_t options)
Radek Krejci19a96102018-11-15 13:38:09 +01007319{
7320 struct lysc_ctx ctx = {0};
7321 struct lysc_module *mod_c;
7322 struct lysp_module *sp;
7323 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007324 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007325 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007326 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007327 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007328 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007329 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007330 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007331
Michal Vasko7a0b0762020-09-02 16:37:01 +02007332 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007333
Michal Vasko7a0b0762020-09-02 16:37:01 +02007334 if (!mod->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007335 /* just imported modules are not compiled */
7336 return LY_SUCCESS;
7337 }
7338
Michal Vasko7a0b0762020-09-02 16:37:01 +02007339 compile_id = ++mod->ctx->module_set_id;
7340 sp = mod->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007341
Michal Vasko7a0b0762020-09-02 16:37:01 +02007342 ctx.ctx = mod->ctx;
7343 ctx.mod = mod;
7344 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007345 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007346 ctx.path_len = 1;
7347 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007348
Michal Vasko7a0b0762020-09-02 16:37:01 +02007349 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
7350 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7351 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007352
Michal Vasko7c8439f2020-08-05 13:25:19 +02007353 LY_ARRAY_FOR(sp->imports, u) {
7354 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7355 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01007356 LY_ARRAY_FOR(sp->includes, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02007357 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007358 }
Radek Krejci0935f412019-08-20 16:15:18 +02007359
7360 /* features */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007361 if (mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007362 /* there is already precompiled array of features */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007363 mod_c->features = mod->dis_features;
7364 mod->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007365 } else {
7366 /* features are compiled directly into the compiled module structure,
7367 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci327de162019-06-14 12:52:07 +02007368 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007369 LY_CHECK_GOTO(ret, error);
7370 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007371
Radek Krejci0af46292019-01-11 16:02:31 +01007372 /* finish feature compilation, not only for the main module, but also for the submodules.
7373 * Due to possible forward references, it must be done when all the features (including submodules)
7374 * are present. */
7375 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007376 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007377 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7378 }
Radek Krejci327de162019-06-14 12:52:07 +02007379 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007380 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007381 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007382 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007383 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007384 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7385 }
Radek Krejci327de162019-06-14 12:52:07 +02007386 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007387 }
Radek Krejci327de162019-06-14 12:52:07 +02007388 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007389
Michal Vasko33ff9422020-07-03 09:50:39 +02007390 /* identities, work similarly to features with the precompilation */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007391 if (mod->dis_identities) {
7392 mod_c->identities = mod->dis_identities;
7393 mod->dis_identities = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02007394 } else {
7395 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7396 LY_CHECK_GOTO(ret, error);
7397 }
Radek Krejci19a96102018-11-15 13:38:09 +01007398 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007399 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007400 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007401 lysc_update_path(&ctx, NULL, "{submodule}");
7402 LY_ARRAY_FOR(sp->includes, v) {
7403 if (sp->includes[v].submodule->identities) {
7404 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7405 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7406 LY_CHECK_GOTO(ret, error);
7407 lysc_update_path(&ctx, NULL, NULL);
7408 }
7409 }
Radek Krejci327de162019-06-14 12:52:07 +02007410 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007411
Radek Krejci95710c92019-02-11 15:49:55 +01007412 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007413 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007414 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007415 }
Radek Krejci95710c92019-02-11 15:49:55 +01007416
Radek Krejciec4da802019-05-02 13:02:41 +02007417 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7418 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007419
Radek Krejci95710c92019-02-11 15:49:55 +01007420 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007421 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007422 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007423 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007424 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007425
Radek Krejci474f9b82019-07-24 11:36:37 +02007426 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007427 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007428
Radek Krejci0935f412019-08-20 16:15:18 +02007429 /* extension instances TODO cover extension instances from submodules */
7430 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007431
Michal Vasko004d3152020-06-11 19:59:22 +02007432 /* finish compilation for all unresolved items in the context */
7433 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007434
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007435 /* validate non-instantiated groupings from the parsed schema,
7436 * without it we would accept even the schemas with invalid grouping specification */
7437 ctx.options |= LYSC_OPT_GROUPING;
7438 LY_ARRAY_FOR(sp->groupings, u) {
7439 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007440 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007441 }
7442 }
7443 LY_LIST_FOR(sp->data, node_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007444 grps = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007445 LY_ARRAY_FOR(grps, u) {
7446 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007447 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007448 }
7449 }
7450 }
7451
Radek Krejci474f9b82019-07-24 11:36:37 +02007452 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7453 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7454 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7455 }
7456
Michal Vasko8d544252020-03-02 10:19:52 +01007457#if 0
7458 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7459 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7460 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7461 * the anotation definitions available in the internal schema structure. */
7462 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7463 if (lyp_add_ietf_netconf_annotations(mod)) {
7464 lys_free(mod, NULL, 1, 1);
7465 return NULL;
7466 }
7467 }
7468#endif
7469
7470 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007471 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
7472 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007473 }
7474
Radek Krejci1c0c3442019-07-23 16:08:47 +02007475 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007476 ly_set_erase(&ctx.xpath, NULL);
7477 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007478 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007479 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007480 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007481
Radek Krejciec4da802019-05-02 13:02:41 +02007482 if (ctx.options & LYSC_OPT_FREE_SP) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02007483 lysp_module_free(mod->parsed);
7484 mod->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007485 }
7486
Radek Krejciec4da802019-05-02 13:02:41 +02007487 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007488 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007489 for (i = 0; i < ctx.ctx->list.count; ++i) {
7490 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007491 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007492 m->implemented = 1;
7493 }
7494 }
7495 }
7496
Radek Krejci19a96102018-11-15 13:38:09 +01007497 return LY_SUCCESS;
7498
7499error:
Michal Vasko7a0b0762020-09-02 16:37:01 +02007500 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007501 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007502 ly_set_erase(&ctx.xpath, NULL);
7503 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007504 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007505 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007506 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007507 lysc_module_free(mod_c, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02007508 mod->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007509
Radek Krejcia46012b2020-08-12 15:41:04 +02007510 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7511 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7512 * 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 +02007513 for (i = 0; i < ctx.ctx->list.count; ++i) {
7514 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007515 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007516 /* revert features list to the precompiled state */
7517 lys_feature_precompile_revert(&ctx, m);
7518 /* mark module as imported-only / not-implemented */
7519 m->implemented = 0;
7520 /* free the compiled version of the module */
7521 lysc_module_free(m->compiled, NULL);
7522 m->compiled = NULL;
7523 }
7524 }
7525
Radek Krejci19a96102018-11-15 13:38:09 +01007526 return ret;
7527}