blob: 61edac23f1ef1abbcf5cda903f2d0632adf31ade [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Michal Vasko7c8439f2020-08-05 13:25:19 +020039#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010041#include "tree_schema_internal.h"
42#include "xpath.h"
43
Michal Vasko5fe75f12020-03-02 13:52:37 +010044static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
Radek Krejci0f969882020-08-21 16:56:47 +020045 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
Michal Vasko5fe75f12020-03-02 13:52:37 +010046
Radek Krejci19a96102018-11-15 13:38:09 +010047/**
48 * @brief Duplicate string into dictionary
49 * @param[in] CTX libyang context of the dictionary.
50 * @param[in] ORIG String to duplicate.
51 * @param[out] DUP Where to store the result.
52 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020053#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
54
55#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
Radek Krejci19a96102018-11-15 13:38:09 +010056
Radek Krejciec4da802019-05-02 13:02:41 +020057#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010058 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020059 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
60 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
61 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020063 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010064 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
65 } \
66 }
67
Radek Krejciec4da802019-05-02 13:02:41 +020068#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010069 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020070 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
71 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
72 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010073 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020074 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010075 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
76 } \
77 }
78
Radek Krejci0935f412019-08-20 16:15:18 +020079#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
80 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020081 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
82 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020083 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010084 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020085 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
86 } \
87 }
88
Radek Krejciec4da802019-05-02 13:02:41 +020089#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010090 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020091 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
92 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
93 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010094 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020095 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010096 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
97 } \
98 }
99
Radek Krejciec4da802019-05-02 13:02:41 +0200100#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +0100101 if (MEMBER_P) { \
102 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
103 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200104 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100105 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
106 }
107
Radek Krejciec4da802019-05-02 13:02:41 +0200108#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 if (MEMBER_P) { \
110 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200111 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100112 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200113 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100114 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
115 }
116
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100117#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200119 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100120 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100121 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
122 return LY_EVALID; \
123 } \
124 } \
125 }
126
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
128 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200129 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100130 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
131 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
132 return LY_EVALID; \
133 } \
134 } \
135 }
136
137struct lysc_ext *
138lysc_ext_dup(struct lysc_ext *orig)
139{
140 ++orig->refcount;
141 return orig;
142}
143
Radek Krejci19a96102018-11-15 13:38:09 +0100144static struct lysc_ext_instance *
145lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
146{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100147 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100148 (void) ctx;
149 (void) orig;
150 return NULL;
151}
152
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200153static LY_ERR
154lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +0200155 struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200156{
157 struct lysc_incomplete_dflt *r;
158 uint32_t i;
159
160 for (i = 0; i < ctx->dflts.count; ++i) {
161 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
162 if (r->leaf == leaf) {
163 /* just replace the default */
164 r->dflt = dflt;
165 return LY_SUCCESS;
166 }
167 }
168
169 r = malloc(sizeof *r);
170 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
171 r->leaf = leaf;
172 r->dflt = dflt;
173 r->dflts = NULL;
174 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200175 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200176
177 return LY_SUCCESS;
178}
179
Radek Krejcib56c7502019-02-13 14:19:54 +0100180/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200181 * @brief Add record into the compile context's list of incomplete default values.
182 * @param[in] ctx Compile context with the incomplete default values list.
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200183 * @param[in] term Term context node with the default value.
184 * @param[in] value String default value.
185 * @param[in] val_len Length of @p value.
Radek Krejci474f9b82019-07-24 11:36:37 +0200186 * @param[in] dflt_mod Module of the default value definition to store in the record.
187 * @return LY_EMEM in case of memory allocation failure.
188 * @return LY_SUCCESS
189 */
190static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200191lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts,
Radek Krejci0f969882020-08-21 16:56:47 +0200192 struct lys_module *dflt_mod)
Radek Krejci474f9b82019-07-24 11:36:37 +0200193{
194 struct lysc_incomplete_dflt *r;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200195 uint32_t i;
196
197 for (i = 0; i < ctx->dflts.count; ++i) {
198 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
199 if (r->llist == llist) {
200 /* just replace the defaults */
201 r->dflts = dflts;
202 return LY_SUCCESS;
203 }
204 }
205
Radek Krejci474f9b82019-07-24 11:36:37 +0200206 r = malloc(sizeof *r);
207 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200208 r->llist = llist;
209 r->dflt = NULL;
210 r->dflts = dflts;
Radek Krejci474f9b82019-07-24 11:36:37 +0200211 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200212 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Radek Krejci474f9b82019-07-24 11:36:37 +0200213
214 return LY_SUCCESS;
215}
216
217/**
218 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
219 * @param[in] ctx Compile context with the incomplete default values list.
220 * @param[in] dflt Incomplete default values identifying the record to remove.
221 */
222static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200223lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term)
Radek Krejci474f9b82019-07-24 11:36:37 +0200224{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200225 uint32_t u;
Radek Krejci474f9b82019-07-24 11:36:37 +0200226 struct lysc_incomplete_dflt *r;
227
228 for (u = 0; u < ctx->dflts.count; ++u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200229 r = ctx->dflts.objs[u];
230 if (r->leaf == (struct lysc_node_leaf *)term) {
Radek Krejci474f9b82019-07-24 11:36:37 +0200231 free(ctx->dflts.objs[u]);
232 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
233 --ctx->dflts.count;
234 return;
235 }
236 }
237}
238
Radek Krejci0e59c312019-08-15 15:34:15 +0200239void
Radek Krejci327de162019-06-14 12:52:07 +0200240lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
241{
242 int len;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200243 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
Radek Krejci327de162019-06-14 12:52:07 +0200244
245 if (!name) {
246 /* removing last path segment */
247 if (ctx->path[ctx->path_len - 1] == '}') {
Michal Vaskod989ba02020-08-24 10:59:24 +0200248 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200249 if (ctx->path[ctx->path_len] == '=') {
250 ctx->path[ctx->path_len++] = '}';
251 } else {
252 /* not a top-level special tag, remove also preceiding '/' */
253 goto remove_nodelevel;
254 }
255 } else {
256remove_nodelevel:
Michal Vaskod989ba02020-08-24 10:59:24 +0200257 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200258 if (ctx->path_len == 0) {
259 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200260 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200261 }
262 }
263 /* set new terminating NULL-byte */
264 ctx->path[ctx->path_len] = '\0';
265 } else {
266 if (ctx->path_len > 1) {
267 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
268 /* extension of the special tag */
269 nextlevel = 2;
270 --ctx->path_len;
271 } else {
272 /* there is already some path, so add next level */
273 nextlevel = 1;
274 }
275 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
276
277 if (nextlevel != 2) {
278 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
279 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200280 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200281 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200282 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200283 }
284 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200285 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200286 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200287 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
Radek Krejciacc79042019-07-25 14:14:57 +0200288 /* output truncated */
289 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
290 } else {
291 ctx->path_len += len;
292 }
Radek Krejci327de162019-06-14 12:52:07 +0200293 }
294}
295
296/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100297 * @brief Duplicate the compiled pattern structure.
298 *
299 * Instead of duplicating memory, the reference counter in the @p orig is increased.
300 *
301 * @param[in] orig The pattern structure to duplicate.
302 * @return The duplicated structure to use.
303 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200304static struct lysc_pattern *
Radek Krejci19a96102018-11-15 13:38:09 +0100305lysc_pattern_dup(struct lysc_pattern *orig)
306{
307 ++orig->refcount;
308 return orig;
309}
310
Radek Krejcib56c7502019-02-13 14:19:54 +0100311/**
312 * @brief Duplicate the array of compiled patterns.
313 *
314 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
315 *
316 * @param[in] ctx Libyang context for logging.
317 * @param[in] orig The patterns sized array to duplicate.
318 * @return New sized array as a copy of @p orig.
319 * @return NULL in case of memory allocation error.
320 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200321static struct lysc_pattern **
Radek Krejci19a96102018-11-15 13:38:09 +0100322lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
323{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100324 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200325 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327 assert(orig);
328
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200329 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100330 LY_ARRAY_FOR(orig, u) {
331 dup[u] = lysc_pattern_dup(orig[u]);
332 LY_ARRAY_INCREMENT(dup);
333 }
334 return dup;
335}
336
Radek Krejcib56c7502019-02-13 14:19:54 +0100337/**
338 * @brief Duplicate compiled range structure.
339 *
340 * @param[in] ctx Libyang context for logging.
341 * @param[in] orig The range structure to be duplicated.
342 * @return New compiled range structure as a copy of @p orig.
343 * @return NULL in case of memory allocation error.
344 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200345struct lysc_range *
Radek Krejci19a96102018-11-15 13:38:09 +0100346lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
347{
348 struct lysc_range *dup;
349 LY_ERR ret;
350
Radek Krejcib56c7502019-02-13 14:19:54 +0100351 assert(orig);
352
Radek Krejci19a96102018-11-15 13:38:09 +0100353 dup = calloc(1, sizeof *dup);
354 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
355 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200356 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
357 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
358 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100359 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200360 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
361 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100362 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
363
364 return dup;
365cleanup:
366 free(dup);
367 (void) ret; /* set but not used due to the return type */
368 return NULL;
369}
370
Radek Krejcib56c7502019-02-13 14:19:54 +0100371/**
372 * @brief Stack for processing if-feature expressions.
373 */
Radek Krejci19a96102018-11-15 13:38:09 +0100374struct iff_stack {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200375 size_t size; /**< number of items in the stack */
376 size_t index; /**< first empty item */
377 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100378};
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Add @ref ifftokens into the stack.
382 * @param[in] stack The if-feature stack to use.
383 * @param[in] value One of the @ref ifftokens to store in the stack.
384 * @return LY_EMEM in case of memory allocation error
385 * @return LY_ESUCCESS if the value successfully stored.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static LY_ERR
388iff_stack_push(struct iff_stack *stack, uint8_t value)
389{
390 if (stack->index == stack->size) {
391 stack->size += 4;
392 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
393 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
394 }
395 stack->stack[stack->index++] = value;
396 return LY_SUCCESS;
397}
398
Radek Krejcib56c7502019-02-13 14:19:54 +0100399/**
400 * @brief Get (and remove) the last item form the stack.
401 * @param[in] stack The if-feature stack to use.
402 * @return The value from the top of the stack.
403 */
Radek Krejci19a96102018-11-15 13:38:09 +0100404static uint8_t
405iff_stack_pop(struct iff_stack *stack)
406{
Radek Krejcib56c7502019-02-13 14:19:54 +0100407 assert(stack && stack->index);
408
Radek Krejci19a96102018-11-15 13:38:09 +0100409 stack->index--;
410 return stack->stack[stack->index];
411}
412
Radek Krejcib56c7502019-02-13 14:19:54 +0100413/**
414 * @brief Clean up the stack.
415 * @param[in] stack The if-feature stack to use.
416 */
Radek Krejci19a96102018-11-15 13:38:09 +0100417static void
418iff_stack_clean(struct iff_stack *stack)
419{
420 stack->size = 0;
421 free(stack->stack);
422}
423
Radek Krejcib56c7502019-02-13 14:19:54 +0100424/**
425 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
426 * (libyang format of the if-feature expression).
427 * @param[in,out] list The 2bits array to modify.
428 * @param[in] op The operand (@ref ifftokens) to store.
429 * @param[in] pos Position (0-based) where to store the given @p op.
430 */
Radek Krejci19a96102018-11-15 13:38:09 +0100431static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200432iff_setop(uint8_t *list, uint8_t op, size_t pos)
Radek Krejci19a96102018-11-15 13:38:09 +0100433{
434 uint8_t *item;
435 uint8_t mask = 3;
436
Radek Krejci19a96102018-11-15 13:38:09 +0100437 assert(op <= 3); /* max 2 bits */
438
439 item = &list[pos / 4];
440 mask = mask << 2 * (pos % 4);
441 *item = (*item) & ~mask;
442 *item = (*item) | (op << 2 * (pos % 4));
443}
444
Radek Krejcib56c7502019-02-13 14:19:54 +0100445#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
446#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100447
Radek Krejci0af46292019-01-11 16:02:31 +0100448/**
449 * @brief Find a feature of the given name and referenced in the given module.
450 *
451 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
452 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
453 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
454 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
455 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
456 * assigned till that time will be still valid.
457 *
458 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
459 * @param[in] name Name of the feature including possible prefix.
460 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
461 * @return Pointer to the feature structure if found, NULL otherwise.
462 */
Radek Krejci19a96102018-11-15 13:38:09 +0100463static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100464lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100465{
466 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200467 LY_ARRAY_COUNT_TYPE u;
Radek Krejci14915cc2020-09-14 17:28:13 +0200468 struct lysc_feature *f;
Radek Krejci19a96102018-11-15 13:38:09 +0100469
Radek Krejci120d8542020-08-12 09:29:16 +0200470 assert(mod);
471
Radek Krejci19a96102018-11-15 13:38:09 +0100472 for (i = 0; i < len; ++i) {
473 if (name[i] == ':') {
474 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100475 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100476 LY_CHECK_RET(!mod, NULL);
477
478 name = &name[i + 1];
479 len = len - i - 1;
480 }
481 }
482
483 /* we have the correct module, get the feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200484 LY_ARRAY_FOR(mod->features, u) {
485 f = &mod->features[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200486 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100487 return f;
488 }
489 }
490
491 return NULL;
492}
493
Michal Vasko8d544252020-03-02 10:19:52 +0100494/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100495 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
496 */
497static LY_ERR
498lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
499{
500 LY_ERR ret = LY_SUCCESS;
501
502 if (!ext_p->compiled) {
503 lysc_update_path(ctx, NULL, "{extension}");
504 lysc_update_path(ctx, NULL, ext_p->name);
505
506 /* compile the extension definition */
507 ext_p->compiled = calloc(1, sizeof **ext);
508 ext_p->compiled->refcount = 1;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200509 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
510 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100511 ext_p->compiled->module = (struct lys_module *)ext_mod;
512 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
513
514 lysc_update_path(ctx, NULL, NULL);
515 lysc_update_path(ctx, NULL, NULL);
516
517 /* find extension definition plugin */
518 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
519 }
520
521 *ext = lysc_ext_dup(ext_p->compiled);
522
523done:
524 return ret;
525}
526
527/**
Michal Vasko8d544252020-03-02 10:19:52 +0100528 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
529 *
530 * @param[in] ctx Compilation context.
531 * @param[in] ext_p Parsed extension instance.
532 * @param[in,out] ext Prepared compiled extension instance.
533 * @param[in] parent Extension instance parent.
534 * @param[in] parent_type Extension instance parent type.
535 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
536 */
Radek Krejci19a96102018-11-15 13:38:09 +0100537static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100538lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200539 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100540{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200541 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100542 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200543 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200544 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200545 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100546
Radek Krejci011e4aa2020-09-04 15:22:31 +0200547 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
548 LY_CHECK_RET(ret);
549
Radek Krejci19a96102018-11-15 13:38:09 +0100550 ext->insubstmt = ext_p->insubstmt;
551 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200552 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200553 ext->parent = parent;
554 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100555
Michal Vasko22df3f02020-08-24 13:29:22 +0200556 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200557
Radek Krejci19a96102018-11-15 13:38:09 +0100558 /* get module where the extension definition should be placed */
Radek Krejci1e008d22020-08-17 11:37:37 +0200559 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
Radek Krejci7c960162019-09-18 14:16:12 +0200560 if (ext_p->yin) {
561 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
562 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
563 * YANG (import) prefix must be done here. */
564 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200565 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200566 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200567 } else {
568 assert(ctx->mod_def->parsed);
569 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
570 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200571 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200572 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1,
Radek Krejci200f1062020-07-11 22:51:03 +0200573 ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200574 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200575 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200576 break;
577 }
578 }
579 }
580 if (!prefixed_name) {
581 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
582 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200583 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200584 goto cleanup;
585 }
586 } else {
587 prefixed_name = ext_p->name;
588 }
589 lysc_update_path(ctx, NULL, prefixed_name);
590
Michal Vasko8d544252020-03-02 10:19:52 +0100591 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200592 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100593 if (!ext_mod) {
594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
595 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200596 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100597 goto cleanup;
598 } else if (!ext_mod->parsed->extensions) {
599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
600 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
601 prefixed_name, ext_mod->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200602 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100603 goto cleanup;
604 }
Radek Krejci7c960162019-09-18 14:16:12 +0200605 }
606 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200607
Michal Vasko5fe75f12020-03-02 13:52:37 +0100608 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200609 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
610 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100611 /* compile extension definition and assign it */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200612 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100613 break;
614 }
615 }
Radek Krejci7c960162019-09-18 14:16:12 +0200616 if (!ext->def) {
617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
618 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200619 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200620 goto cleanup;
621 }
Radek Krejci0935f412019-08-20 16:15:18 +0200622
Radek Krejcif56e2a42019-09-09 14:15:25 +0200623 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
624 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
625 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200626 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200627 /* Schema was parsed from YIN and an argument is expected, ... */
628 struct lysp_stmt *stmt = NULL;
629
630 if (ext->def->flags & LYS_YINELEM_TRUE) {
631 /* ... argument was the first XML child element */
632 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
633 /* TODO check namespace of the statement */
634 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
635 stmt = ext_p->child;
636 }
637 }
638 } else {
639 /* ... argument was one of the XML attributes which are represented as child stmt
640 * with LYS_YIN_ATTR flag */
641 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
642 if (!strcmp(stmt->stmt, ext->def->argument)) {
643 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200644 break;
645 }
646 }
647 }
648 if (!stmt) {
649 /* missing extension's argument */
650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200651 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200652 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200653 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200654
655 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200656 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200657 stmt->flags |= LYS_YIN_ARGUMENT;
658 }
Radek Krejci7c960162019-09-18 14:16:12 +0200659 if (prefixed_name != ext_p->name) {
660 lydict_remove(ctx->ctx, ext_p->name);
661 ext_p->name = prefixed_name;
662 if (!ext_p->argument && ext->argument) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200663 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200664 }
665 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200666
Radek Krejci0935f412019-08-20 16:15:18 +0200667 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200668 if (ext->argument) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200669 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
Radek Krejciad5963b2019-09-06 16:03:05 +0200670 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200671 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200672 if (ext->argument) {
673 lysc_update_path(ctx, NULL, NULL);
674 }
Radek Krejci0935f412019-08-20 16:15:18 +0200675 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200676 ext_p->compiled = ext;
677
Radek Krejci7c960162019-09-18 14:16:12 +0200678cleanup:
679 if (prefixed_name && prefixed_name != ext_p->name) {
680 lydict_remove(ctx->ctx, prefixed_name);
681 }
682
Radek Krejcif56e2a42019-09-09 14:15:25 +0200683 lysc_update_path(ctx, NULL, NULL);
684 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200685
Radek Krejci7c960162019-09-18 14:16:12 +0200686 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200687}
688
689/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100690 * @brief Compile information from the if-feature statement
691 * @param[in] ctx Compile context.
692 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100693 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
694 * @return LY_ERR value.
695 */
Radek Krejci19a96102018-11-15 13:38:09 +0100696static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200697lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100698{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200699 LY_ERR rc = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100700 const char *c = *value;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200701 int64_t i, j;
702 int8_t op_len, last_not = 0, checkversion = 0;
703 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci19a96102018-11-15 13:38:09 +0100704 uint8_t op;
705 struct iff_stack stack = {0, 0, NULL};
706 struct lysc_feature *f;
707
708 assert(c);
709
710 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200711 for (i = j = 0; c[i]; i++) {
Radek Krejci19a96102018-11-15 13:38:09 +0100712 if (c[i] == '(') {
713 j++;
714 checkversion = 1;
715 continue;
716 } else if (c[i] == ')') {
717 j--;
718 continue;
719 } else if (isspace(c[i])) {
720 checkversion = 1;
721 continue;
722 }
723
Radek Krejci1deb5be2020-08-26 16:43:36 +0200724 if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
725 uint64_t spaces;
726 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++);
727 if (c[i + op_len + spaces] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
729 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
730 return LY_EVALID;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200731 } else if (!isspace(c[i + op_len])) {
Radek Krejci19a96102018-11-15 13:38:09 +0100732 /* feature name starting with the not/and/or */
733 last_not = 0;
734 f_size++;
735 } else if (c[i] == 'n') { /* not operation */
736 if (last_not) {
737 /* double not */
738 expr_size = expr_size - 2;
739 last_not = 0;
740 } else {
741 last_not = 1;
742 }
743 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200744 if (f_exp != f_size) {
745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200746 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, op_len, &c[i]);
Radek Krejci6788abc2019-06-14 13:56:49 +0200747 return LY_EVALID;
748 }
Radek Krejci19a96102018-11-15 13:38:09 +0100749 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200750
Radek Krejci19a96102018-11-15 13:38:09 +0100751 /* not a not operation */
752 last_not = 0;
753 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200754 i += op_len;
Radek Krejci19a96102018-11-15 13:38:09 +0100755 } else {
756 f_size++;
757 last_not = 0;
758 }
759 expr_size++;
760
761 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200762 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100763 i--;
764 break;
765 }
766 i++;
767 }
768 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200769 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100770 /* not matching count of ( and ) */
771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
772 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
773 return LY_EVALID;
774 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200775 if (f_exp != f_size) {
776 /* features do not match the needed arguments for the logical operations */
777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
778 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
779 "the required number of operands for the operations.", *value);
780 return LY_EVALID;
781 }
Radek Krejci19a96102018-11-15 13:38:09 +0100782
783 if (checkversion || expr_size > 1) {
784 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100785 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
787 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
788 return LY_EVALID;
789 }
790 }
791
792 /* allocate the memory */
793 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
794 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
795 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200796 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
Radek Krejci19a96102018-11-15 13:38:09 +0100797
798 stack.size = expr_size;
799 f_size--; expr_size--; /* used as indexes from now */
800
801 for (i--; i >= 0; i--) {
802 if (c[i] == ')') {
803 /* push it on stack */
804 iff_stack_push(&stack, LYS_IFF_RP);
805 continue;
806 } else if (c[i] == '(') {
807 /* pop from the stack into result all operators until ) */
Michal Vaskod989ba02020-08-24 10:59:24 +0200808 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
Radek Krejci19a96102018-11-15 13:38:09 +0100809 iff_setop(iff->expr, op, expr_size--);
810 }
811 continue;
812 } else if (isspace(c[i])) {
813 continue;
814 }
815
816 /* end of operator or operand -> find beginning and get what is it */
817 j = i + 1;
818 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
819 i--;
820 }
821 i++; /* go back by one step */
822
823 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
824 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
825 /* double not */
826 iff_stack_pop(&stack);
827 } else {
828 /* not has the highest priority, so do not pop from the stack
829 * as in case of AND and OR */
830 iff_stack_push(&stack, LYS_IFF_NOT);
831 }
832 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
833 /* as for OR - pop from the stack all operators with the same or higher
834 * priority and store them to the result, then push the AND to the stack */
835 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
836 op = iff_stack_pop(&stack);
837 iff_setop(iff->expr, op, expr_size--);
838 }
839 iff_stack_push(&stack, LYS_IFF_AND);
840 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
841 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
842 op = iff_stack_pop(&stack);
843 iff_setop(iff->expr, op, expr_size--);
844 }
845 iff_stack_push(&stack, LYS_IFF_OR);
846 } else {
847 /* feature name, length is j - i */
848
849 /* add it to the expression */
850 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
851
852 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100853 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
854 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
855 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
856 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100857 iff->features[f_size] = f;
858 LY_ARRAY_INCREMENT(iff->features);
859 f_size--;
860 }
861 }
862 while (stack.index) {
863 op = iff_stack_pop(&stack);
864 iff_setop(iff->expr, op, expr_size--);
865 }
866
867 if (++expr_size || ++f_size) {
868 /* not all expected operators and operands found */
869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
870 "Invalid value \"%s\" of if-feature - processing error.", *value);
871 rc = LY_EINT;
872 } else {
873 rc = LY_SUCCESS;
874 }
875
876error:
877 /* cleanup */
878 iff_stack_clean(&stack);
879
880 return rc;
881}
882
Radek Krejcib56c7502019-02-13 14:19:54 +0100883/**
Michal Vasko175012e2019-11-06 15:49:14 +0100884 * @brief Get the XPath context node for the given schema node.
885 * @param[in] start The schema node where the XPath expression appears.
886 * @return The context node to evaluate XPath expression in given schema node.
887 * @return NULL in case the context node is the root node.
888 */
889static struct lysc_node *
890lysc_xpath_context(struct lysc_node *start)
891{
Michal Vasko1bf09392020-03-27 12:38:10 +0100892 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Radek Krejci1e008d22020-08-17 11:37:37 +0200893 start = start->parent) {}
Michal Vasko175012e2019-11-06 15:49:14 +0100894 return start;
895}
896
897/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100898 * @brief Compile information from the when statement
899 * @param[in] ctx Compile context.
900 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100901 * @param[in] flags Flags of the node with the "when" defiition.
902 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100903 * @param[out] when Pointer where to store pointer to the created compiled when structure.
904 * @return LY_ERR value.
905 */
Radek Krejci19a96102018-11-15 13:38:09 +0100906static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100907lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100908{
Radek Krejci19a96102018-11-15 13:38:09 +0100909 LY_ERR ret = LY_SUCCESS;
910
Radek Krejci00b874b2019-02-12 10:54:50 +0100911 *when = calloc(1, sizeof **when);
912 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200913 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200914 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100915 (*when)->context = lysc_xpath_context(node);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200916 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
917 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejci00b874b2019-02-12 10:54:50 +0100918 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200919 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100920 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100921
922done:
923 return ret;
924}
925
Radek Krejcib56c7502019-02-13 14:19:54 +0100926/**
927 * @brief Compile information from the must statement
928 * @param[in] ctx Compile context.
929 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100930 * @param[in,out] must Prepared (empty) compiled must structure to fill.
931 * @return LY_ERR value.
932 */
Radek Krejci19a96102018-11-15 13:38:09 +0100933static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200934lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100935{
Radek Krejci19a96102018-11-15 13:38:09 +0100936 LY_ERR ret = LY_SUCCESS;
937
Michal Vasko004d3152020-06-11 19:59:22 +0200938 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100939 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200940 must->module = ctx->mod_def;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200941 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
942 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
943 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
944 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200945 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100946
947done:
948 return ret;
949}
950
Radek Krejcib56c7502019-02-13 14:19:54 +0100951/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200952 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100953 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200954 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100955 * @return LY_ERR value.
956 */
Radek Krejci19a96102018-11-15 13:38:09 +0100957static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200958lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100959{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200960 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100961 LY_ERR ret = LY_SUCCESS;
962
Radek Krejci7f2a5362018-11-28 13:05:37 +0100963 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
964 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100965 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200966 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100967 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200968 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200969 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200970 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200971 LOGINT(ctx->ctx);
972 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200973 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
Michal Vasko3a41dff2020-07-15 14:30:28 +0200974 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200975 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200976 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200977 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200978 mod = NULL;
979 }
Radek Krejci19a96102018-11-15 13:38:09 +0100980 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200981 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100982 }
983 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200984 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100985 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200986 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100987 return LY_ENOTFOUND;
988 }
989 }
Radek Krejci19a96102018-11-15 13:38:09 +0100990 }
991
Radek Krejci19a96102018-11-15 13:38:09 +0100992 return ret;
993}
994
Michal Vasko33ff9422020-07-03 09:50:39 +0200995LY_ERR
996lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +0200997 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100998{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200999 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +02001000 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01001001 LY_ERR ret = LY_SUCCESS;
1002
Michal Vasko33ff9422020-07-03 09:50:39 +02001003 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +02001004
Michal Vasko33ff9422020-07-03 09:50:39 +02001005 if (!ctx_sc) {
1006 context.ctx = ctx;
1007 context.mod = module;
Radek Krejci120d8542020-08-12 09:29:16 +02001008 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +02001009 context.path_len = 1;
1010 context.path[0] = '/';
1011 ctx_sc = &context;
1012 }
Radek Krejci19a96102018-11-15 13:38:09 +01001013
Michal Vasko33ff9422020-07-03 09:50:39 +02001014 if (!identities_p) {
1015 return LY_SUCCESS;
1016 }
1017 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001018 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02001019 }
1020
1021 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001022 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001023 LY_ARRAY_FOR(identities_p, u) {
1024 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
1025
1026 LY_ARRAY_INCREMENT(*identities);
1027 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001028 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
1029 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
1030 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
Michal Vasko33ff9422020-07-03 09:50:39 +02001031 (*identities)[offset + u].module = ctx_sc->mod;
1032 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
1033 lys_compile_iffeature, ret, done);
1034 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
1035 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
1036 LYEXT_PAR_IDENT, ret, done);
1037 (*identities)[offset + u].flags = identities_p[u].flags;
1038
1039 lysc_update_path(ctx_sc, NULL, NULL);
1040 }
1041 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001042done:
1043 return ret;
1044}
1045
Radek Krejcib56c7502019-02-13 14:19:54 +01001046/**
1047 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1048 *
1049 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1050 *
1051 * @param[in] ctx Compile context for logging.
1052 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1053 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1054 * @return LY_SUCCESS if everything is ok.
1055 * @return LY_EVALID if the identity is derived from itself.
1056 */
Radek Krejci38222632019-02-12 16:55:05 +01001057static LY_ERR
1058lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1059{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001060 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001061 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001062 struct ly_set recursion = {0};
1063 struct lysc_ident *drv;
1064
1065 if (!derived) {
1066 return LY_SUCCESS;
1067 }
1068
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001069 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001070 if (ident == derived[u]) {
1071 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1072 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001073 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001074 goto cleanup;
1075 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001076 ret = ly_set_add(&recursion, derived[u], 0, NULL);
1077 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001078 }
1079
1080 for (v = 0; v < recursion.count; ++v) {
1081 drv = recursion.objs[v];
1082 if (!drv->derived) {
1083 continue;
1084 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001085 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001086 if (ident == drv->derived[u]) {
1087 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1088 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001089 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001090 goto cleanup;
1091 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001092 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
1093 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001094 }
1095 }
Radek Krejci38222632019-02-12 16:55:05 +01001096
1097cleanup:
1098 ly_set_erase(&recursion, NULL);
1099 return ret;
1100}
1101
Radek Krejcia3045382018-11-22 14:30:31 +01001102/**
1103 * @brief Find and process the referenced base identities from another identity or identityref
1104 *
Radek Krejciaca74032019-06-04 08:53:06 +02001105 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001106 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1107 * to distinguish these two use cases.
1108 *
1109 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1110 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001111 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001112 * @param[in] bases Array of bases of identityref to fill in.
1113 * @return LY_ERR value.
1114 */
Radek Krejci19a96102018-11-15 13:38:09 +01001115static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001116lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001117 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001118{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001119 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001120 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001121 struct lys_module *mod;
Radek Krejci80d281e2020-09-14 17:42:54 +02001122 struct lysc_ident **idref;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001123
1124 assert(ident || bases);
1125
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001126 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1128 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1129 return LY_EVALID;
1130 }
1131
Michal Vasko33ff9422020-07-03 09:50:39 +02001132 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001133 s = strchr(bases_p[u], ':');
1134 if (s) {
1135 /* prefixed identity */
1136 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001137 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001138 } else {
1139 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001140 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001141 }
1142 if (!mod) {
1143 if (ident) {
1144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1145 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1146 } else {
1147 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1148 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1149 }
1150 return LY_EVALID;
1151 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001152
Radek Krejci555cb5b2018-11-16 14:54:33 +01001153 idref = NULL;
Radek Krejci80d281e2020-09-14 17:42:54 +02001154 LY_ARRAY_FOR(mod->identities, v) {
1155 if (!strcmp(name, mod->identities[v].name)) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001156 if (ident) {
Radek Krejci80d281e2020-09-14 17:42:54 +02001157 if (ident == &mod->identities[v]) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001158 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1159 "Identity \"%s\" is derived from itself.", ident->name);
1160 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001161 }
Radek Krejci80d281e2020-09-14 17:42:54 +02001162 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
Michal Vasko33ff9422020-07-03 09:50:39 +02001163 /* we have match! store the backlink */
Radek Krejci80d281e2020-09-14 17:42:54 +02001164 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001165 *idref = ident;
1166 } else {
1167 /* we have match! store the found identity */
1168 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejci80d281e2020-09-14 17:42:54 +02001169 *idref = &mod->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001170 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001171 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001172 }
1173 }
1174 if (!idref || !(*idref)) {
1175 if (ident) {
1176 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1177 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1178 } else {
1179 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1180 "Unable to find base (%s) of identityref.", bases_p[u]);
1181 }
1182 return LY_EVALID;
1183 }
1184 }
1185 return LY_SUCCESS;
1186}
1187
Radek Krejcia3045382018-11-22 14:30:31 +01001188/**
1189 * @brief For the given array of identities, set the backlinks from all their base identities.
1190 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1191 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1192 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1193 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1194 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001195static LY_ERR
1196lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1197{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001198 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001199
Michal Vasko33ff9422020-07-03 09:50:39 +02001200 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001201 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001202 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001203 continue;
1204 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001205 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001206 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 +02001207 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001208 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001209 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001210 return LY_SUCCESS;
1211}
1212
Radek Krejci0af46292019-01-11 16:02:31 +01001213LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001214lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001215 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001216{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001217 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001218 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001219 struct lysc_ctx context = {0};
1220
Radek Krejci327de162019-06-14 12:52:07 +02001221 assert(ctx_sc || ctx);
1222
1223 if (!ctx_sc) {
1224 context.ctx = ctx;
1225 context.mod = module;
1226 context.path_len = 1;
1227 context.path[0] = '/';
1228 ctx_sc = &context;
1229 }
Radek Krejci0af46292019-01-11 16:02:31 +01001230
1231 if (!features_p) {
1232 return LY_SUCCESS;
1233 }
1234 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001235 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001236 }
1237
Radek Krejci327de162019-06-14 12:52:07 +02001238 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001239 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001240 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001241 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1242
Radek Krejci0af46292019-01-11 16:02:31 +01001243 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001244 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001245 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
1246 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
1247 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001248 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001249 (*features)[offset + u].module = ctx_sc->mod;
1250
1251 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001252 }
Radek Krejci327de162019-06-14 12:52:07 +02001253 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001254
Radek Krejci011e4aa2020-09-04 15:22:31 +02001255done:
1256 return ret;
Radek Krejci0af46292019-01-11 16:02:31 +01001257}
1258
Radek Krejcia3045382018-11-22 14:30:31 +01001259/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001260 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001261 *
1262 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1263 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001264 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001265 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1266 * being currently processed).
1267 * @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 +01001268 * @return LY_SUCCESS if everything is ok.
1269 * @return LY_EVALID if the feature references indirectly itself.
1270 */
1271static LY_ERR
1272lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1273{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001274 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001275 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001276 struct ly_set recursion = {0};
1277 struct lysc_feature *drv;
1278
1279 if (!depfeatures) {
1280 return LY_SUCCESS;
1281 }
1282
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001283 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001284 if (feature == depfeatures[u]) {
1285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1286 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001287 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001288 goto cleanup;
1289 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001290 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
1291 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001292 }
1293
1294 for (v = 0; v < recursion.count; ++v) {
1295 drv = recursion.objs[v];
1296 if (!drv->depfeatures) {
1297 continue;
1298 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001299 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001300 if (feature == drv->depfeatures[u]) {
1301 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1302 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001303 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001304 goto cleanup;
1305 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001306 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
1307 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001308 }
1309 }
Radek Krejci09a1fc52019-02-13 10:55:17 +01001310
1311cleanup:
1312 ly_set_erase(&recursion, NULL);
1313 return ret;
1314}
1315
1316/**
Radek Krejci0af46292019-01-11 16:02:31 +01001317 * @brief Create pre-compiled features array.
1318 *
1319 * See lys_feature_precompile() for more details.
1320 *
Radek Krejcia3045382018-11-22 14:30:31 +01001321 * @param[in] ctx Compile context.
1322 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001323 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001324 * @return LY_ERR value.
1325 */
Radek Krejci19a96102018-11-15 13:38:09 +01001326static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001327lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001328{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001329 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001330 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001331 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001332
Radek Krejci0af46292019-01-11 16:02:31 +01001333 /* find the preprecompiled feature */
1334 LY_ARRAY_FOR(features, x) {
1335 if (strcmp(features[x].name, feature_p->name)) {
1336 continue;
1337 }
1338 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001339 lysc_update_path(ctx, NULL, "{feature}");
1340 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001341
Radek Krejci0af46292019-01-11 16:02:31 +01001342 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001343 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001344 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001345 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001346 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001347 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001348 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001349 /* check for circular dependency - direct reference first,... */
1350 if (feature == feature->iffeatures[u].features[v]) {
1351 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1352 "Feature \"%s\" is referenced from itself.", feature->name);
1353 return LY_EVALID;
1354 }
1355 /* ... and indirect circular reference */
1356 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1357
Radek Krejci0af46292019-01-11 16:02:31 +01001358 /* add itself into the dependants list */
1359 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1360 *df = feature;
1361 }
Radek Krejci19a96102018-11-15 13:38:09 +01001362 }
Radek Krejci19a96102018-11-15 13:38:09 +01001363 }
1364 }
Radek Krejci327de162019-06-14 12:52:07 +02001365 lysc_update_path(ctx, NULL, NULL);
1366 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001367 done:
1368 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001369 }
Radek Krejci0af46292019-01-11 16:02:31 +01001370
1371 LOGINT(ctx->ctx);
1372 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001373}
1374
Radek Krejcib56c7502019-02-13 14:19:54 +01001375/**
1376 * @brief Revert compiled list of features back to the precompiled state.
1377 *
1378 * 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 +02001379 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001380 *
1381 * @param[in] ctx Compilation context.
1382 * @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 +02001383 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001384 */
Radek Krejci95710c92019-02-11 15:49:55 +01001385static void
1386lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1387{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001388 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001389
Michal Vasko33ff9422020-07-03 09:50:39 +02001390 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001391 * which may points into the data being freed here */
Radek Krejci14915cc2020-09-14 17:28:13 +02001392 LY_ARRAY_FOR(mod->features, u) {
1393 LY_ARRAY_FOR(mod->features[u].iffeatures, v) {
1394 lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001395 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001396 LY_ARRAY_FREE(mod->features[u].iffeatures);
1397 mod->features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001398
Radek Krejci14915cc2020-09-14 17:28:13 +02001399 LY_ARRAY_FOR(mod->features[u].exts, v) {
1400 lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001401 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001402 LY_ARRAY_FREE(mod->features[u].exts);
1403 mod->features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001404 }
1405}
1406
Radek Krejcia3045382018-11-22 14:30:31 +01001407/**
1408 * @brief Validate and normalize numeric value from a range definition.
1409 * @param[in] ctx Compile context.
1410 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1411 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1412 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1413 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1414 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1415 * @param[in] value String value of the range boundary.
1416 * @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.
1417 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1418 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1419 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001420LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001421range_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 +01001422{
Radek Krejci6cba4292018-11-15 17:33:29 +01001423 size_t fraction = 0, size;
1424
Radek Krejci19a96102018-11-15 13:38:09 +01001425 *len = 0;
1426
1427 assert(value);
1428 /* parse value */
1429 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1430 return LY_EVALID;
1431 }
1432
1433 if ((value[*len] == '-') || (value[*len] == '+')) {
1434 ++(*len);
1435 }
1436
1437 while (isdigit(value[*len])) {
1438 ++(*len);
1439 }
1440
1441 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001442 if (basetype == LY_TYPE_DEC64) {
1443 goto decimal;
1444 } else {
1445 *valcopy = strndup(value, *len);
1446 return LY_SUCCESS;
1447 }
Radek Krejci19a96102018-11-15 13:38:09 +01001448 }
1449 fraction = *len;
1450
1451 ++(*len);
1452 while (isdigit(value[*len])) {
1453 ++(*len);
1454 }
1455
Radek Krejci6cba4292018-11-15 17:33:29 +01001456 if (basetype == LY_TYPE_DEC64) {
1457decimal:
1458 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001459 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001460 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1461 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1462 *len, value, frdigits);
1463 return LY_EINVAL;
1464 }
1465 if (fraction) {
1466 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1467 } else {
1468 size = (*len) + frdigits + 1;
1469 }
1470 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001471 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1472
Radek Krejci6cba4292018-11-15 17:33:29 +01001473 (*valcopy)[size - 1] = '\0';
1474 if (fraction) {
1475 memcpy(&(*valcopy)[0], &value[0], fraction);
1476 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1477 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1478 } else {
1479 memcpy(&(*valcopy)[0], &value[0], *len);
1480 memset(&(*valcopy)[*len], '0', frdigits);
1481 }
Radek Krejci19a96102018-11-15 13:38:09 +01001482 }
1483 return LY_SUCCESS;
1484}
1485
Radek Krejcia3045382018-11-22 14:30:31 +01001486/**
1487 * @brief Check that values in range are in ascendant order.
1488 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001489 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1490 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001491 * @param[in] value Current value to check.
1492 * @param[in] prev_value The last seen value.
1493 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1494 */
Radek Krejci19a96102018-11-15 13:38:09 +01001495static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001496range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001497{
1498 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001499 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001500 return LY_EEXIST;
1501 }
1502 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001503 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001504 return LY_EEXIST;
1505 }
1506 }
1507 return LY_SUCCESS;
1508}
1509
Radek Krejcia3045382018-11-22 14:30:31 +01001510/**
1511 * @brief Set min/max value of the range part.
1512 * @param[in] ctx Compile context.
1513 * @param[in] part Range part structure to fill.
1514 * @param[in] max Flag to distinguish if storing min or max value.
1515 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1516 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1517 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1518 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1519 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001520 * @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 +01001521 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1522 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1523 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1524 * frdigits value), LY_EMEM.
1525 */
Radek Krejci19a96102018-11-15 13:38:09 +01001526static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001527range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
1528 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 +01001529{
1530 LY_ERR ret = LY_SUCCESS;
1531 char *valcopy = NULL;
1532 size_t len;
1533
1534 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001535 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001536 LY_CHECK_GOTO(ret, finalize);
1537 }
1538 if (!valcopy && base_range) {
1539 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001540 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001541 } else {
1542 part->min_64 = base_range->parts[0].min_64;
1543 }
1544 if (!first) {
1545 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1546 }
1547 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001548 }
1549
1550 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001551 case LY_TYPE_INT8: /* range */
1552 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001553 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 +01001554 } else if (max) {
1555 part->max_64 = INT64_C(127);
1556 } else {
1557 part->min_64 = INT64_C(-128);
1558 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001559 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001560 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001561 }
1562 break;
1563 case LY_TYPE_INT16: /* range */
1564 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001565 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 +01001566 } else if (max) {
1567 part->max_64 = INT64_C(32767);
1568 } else {
1569 part->min_64 = INT64_C(-32768);
1570 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001571 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001572 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001573 }
1574 break;
1575 case LY_TYPE_INT32: /* range */
1576 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001577 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 +01001578 } else if (max) {
1579 part->max_64 = INT64_C(2147483647);
1580 } else {
1581 part->min_64 = INT64_C(-2147483648);
1582 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001583 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001584 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001585 }
1586 break;
1587 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001588 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001589 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001590 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001591 max ? &part->max_64 : &part->min_64);
1592 } else if (max) {
1593 part->max_64 = INT64_C(9223372036854775807);
1594 } else {
1595 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1596 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001597 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001598 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001599 }
1600 break;
1601 case LY_TYPE_UINT8: /* range */
1602 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001603 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 +01001604 } else if (max) {
1605 part->max_u64 = UINT64_C(255);
1606 } else {
1607 part->min_u64 = UINT64_C(0);
1608 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001609 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001610 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001611 }
1612 break;
1613 case LY_TYPE_UINT16: /* range */
1614 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001615 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 +01001616 } else if (max) {
1617 part->max_u64 = UINT64_C(65535);
1618 } else {
1619 part->min_u64 = UINT64_C(0);
1620 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001621 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001622 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001623 }
1624 break;
1625 case LY_TYPE_UINT32: /* range */
1626 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001627 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 +01001628 } else if (max) {
1629 part->max_u64 = UINT64_C(4294967295);
1630 } else {
1631 part->min_u64 = UINT64_C(0);
1632 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001633 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001634 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001635 }
1636 break;
1637 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001638 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001639 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001640 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001641 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 +01001642 } else if (max) {
1643 part->max_u64 = UINT64_C(18446744073709551615);
1644 } else {
1645 part->min_u64 = UINT64_C(0);
1646 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001647 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001648 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001649 }
1650 break;
1651 default:
1652 LOGINT(ctx->ctx);
1653 ret = LY_EINT;
1654 }
1655
Radek Krejci5969f272018-11-23 10:03:58 +01001656finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001657 if (ret == LY_EDENIED) {
1658 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1659 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1660 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1661 } else if (ret == LY_EVALID) {
1662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1663 "Invalid %s restriction - invalid value \"%s\".",
1664 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1665 } else if (ret == LY_EEXIST) {
1666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1667 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001668 length_restr ? "length" : "range",
Radek Krejci0f969882020-08-21 16:56:47 +02001669 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001670 } else if (!ret && value) {
1671 *value = *value + len;
1672 }
1673 free(valcopy);
1674 return ret;
1675}
1676
Radek Krejcia3045382018-11-22 14:30:31 +01001677/**
1678 * @brief Compile the parsed range restriction.
1679 * @param[in] ctx Compile context.
1680 * @param[in] range_p Parsed range structure to compile.
1681 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1682 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1683 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1684 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1685 * range restriction must be more restrictive than the base_range.
1686 * @param[in,out] range Pointer to the created current range structure.
1687 * @return LY_ERR value.
1688 */
Radek Krejci19a96102018-11-15 13:38:09 +01001689static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001690lys_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 +02001691 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001692{
1693 LY_ERR ret = LY_EVALID;
1694 const char *expr;
1695 struct lysc_range_part *parts = NULL, *part;
Radek Krejci857189e2020-09-01 13:26:36 +02001696 ly_bool range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001697 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001698
1699 assert(range);
1700 assert(range_p);
1701
1702 expr = range_p->arg;
Michal Vaskod989ba02020-08-24 10:59:24 +02001703 while (1) {
Radek Krejci19a96102018-11-15 13:38:09 +01001704 if (isspace(*expr)) {
1705 ++expr;
1706 } else if (*expr == '\0') {
1707 if (range_expected) {
1708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1709 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1710 length_restr ? "length" : "range", range_p->arg);
1711 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001712 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1714 "Invalid %s restriction - unexpected end of the expression (%s).",
1715 length_restr ? "length" : "range", range_p->arg);
1716 goto cleanup;
1717 }
1718 parts_done++;
1719 break;
1720 } else if (!strncmp(expr, "min", 3)) {
1721 if (parts) {
1722 /* min cannot be used elsewhere than in the first part */
1723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1724 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1725 expr - range_p->arg, range_p->arg);
1726 goto cleanup;
1727 }
1728 expr += 3;
1729
1730 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001731 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 +01001732 part->max_64 = part->min_64;
1733 } else if (*expr == '|') {
1734 if (!parts || range_expected) {
1735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1736 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1737 goto cleanup;
1738 }
1739 expr++;
1740 parts_done++;
1741 /* process next part of the expression */
1742 } else if (!strncmp(expr, "..", 2)) {
1743 expr += 2;
1744 while (isspace(*expr)) {
1745 expr++;
1746 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001747 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1749 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1750 goto cleanup;
1751 }
1752 /* continue expecting the upper boundary */
1753 range_expected = 1;
1754 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1755 /* number */
1756 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001757 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001758 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 +01001759 range_expected = 0;
1760 } else {
1761 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001762 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 +01001763 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001764 part->max_64 = part->min_64;
1765 }
1766
1767 /* continue with possible another expression part */
1768 } else if (!strncmp(expr, "max", 3)) {
1769 expr += 3;
1770 while (isspace(*expr)) {
1771 expr++;
1772 }
1773 if (*expr != '\0') {
1774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1775 length_restr ? "length" : "range", expr);
1776 goto cleanup;
1777 }
1778 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001779 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001780 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 +01001781 range_expected = 0;
1782 } else {
1783 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001784 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 +01001785 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001786 part->min_64 = part->max_64;
1787 }
1788 } else {
1789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1790 length_restr ? "length" : "range", expr);
1791 goto cleanup;
1792 }
1793 }
1794
1795 /* check with the previous range/length restriction */
1796 if (base_range) {
1797 switch (basetype) {
1798 case LY_TYPE_BINARY:
1799 case LY_TYPE_UINT8:
1800 case LY_TYPE_UINT16:
1801 case LY_TYPE_UINT32:
1802 case LY_TYPE_UINT64:
1803 case LY_TYPE_STRING:
1804 uns = 1;
1805 break;
1806 case LY_TYPE_DEC64:
1807 case LY_TYPE_INT8:
1808 case LY_TYPE_INT16:
1809 case LY_TYPE_INT32:
1810 case LY_TYPE_INT64:
1811 uns = 0;
1812 break;
1813 default:
1814 LOGINT(ctx->ctx);
1815 ret = LY_EINT;
1816 goto cleanup;
1817 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001818 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001819 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1820 goto baseerror;
1821 }
1822 /* current lower bound is not lower than the base */
1823 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1824 /* base has single value */
1825 if (base_range->parts[v].min_64 == parts[u].min_64) {
1826 /* both lower bounds are the same */
1827 if (parts[u].min_64 != parts[u].max_64) {
1828 /* current continues with a range */
1829 goto baseerror;
1830 } else {
1831 /* equal single values, move both forward */
1832 ++v;
1833 continue;
1834 }
1835 } else {
1836 /* base is single value lower than current range, so the
1837 * value from base range is removed in the current,
1838 * move only base and repeat checking */
1839 ++v;
1840 --u;
1841 continue;
1842 }
1843 } else {
1844 /* base is the range */
1845 if (parts[u].min_64 == parts[u].max_64) {
1846 /* current is a single value */
1847 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1848 /* current is behind the base range, so base range is omitted,
1849 * move the base and keep the current for further check */
1850 ++v;
1851 --u;
1852 } /* else it is within the base range, so move the current, but keep the base */
1853 continue;
1854 } else {
1855 /* both are ranges - check the higher bound, the lower was already checked */
1856 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1857 /* higher bound is higher than the current higher bound */
1858 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1859 /* but the current lower bound is also higher, so the base range is omitted,
1860 * continue with the same current, but move the base */
1861 --u;
1862 ++v;
1863 continue;
1864 }
1865 /* current range starts within the base range but end behind it */
1866 goto baseerror;
1867 } else {
1868 /* current range is smaller than the base,
1869 * move current, but stay with the base */
1870 continue;
1871 }
1872 }
1873 }
1874 }
1875 if (u != parts_done) {
1876baseerror:
1877 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1878 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1879 length_restr ? "length" : "range", range_p->arg);
1880 goto cleanup;
1881 }
1882 }
1883
1884 if (!(*range)) {
1885 *range = calloc(1, sizeof **range);
1886 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1887 }
1888
Radek Krejcic8b31002019-01-08 10:24:45 +01001889 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001890 if (range_p->eapptag) {
1891 lydict_remove(ctx->ctx, (*range)->eapptag);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001892 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001893 }
1894 if (range_p->emsg) {
1895 lydict_remove(ctx->ctx, (*range)->emsg);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001896 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001897 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001898 if (range_p->dsc) {
1899 lydict_remove(ctx->ctx, (*range)->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001900 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001901 }
1902 if (range_p->ref) {
1903 lydict_remove(ctx->ctx, (*range)->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001904 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001905 }
Radek Krejci19a96102018-11-15 13:38:09 +01001906 /* extensions are taken only from the last range by the caller */
1907
1908 (*range)->parts = parts;
1909 parts = NULL;
1910 ret = LY_SUCCESS;
1911cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001912 LY_ARRAY_FREE(parts);
1913
1914 return ret;
1915}
1916
1917/**
1918 * @brief Checks pattern syntax.
1919 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001920 * @param[in] ctx Context.
1921 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001922 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001923 * @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 +01001924 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001925 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001926LY_ERR
1927lys_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 +01001928{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001929 size_t idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001930 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001931 int err_code;
1932 const char *orig_ptr;
1933 PCRE2_SIZE err_offset;
1934 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001935#define URANGE_LEN 19
1936 char *ublock2urange[][2] = {
1937 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1938 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1939 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1940 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1941 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1942 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1943 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1944 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1945 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1946 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1947 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1948 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1949 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1950 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1951 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1952 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1953 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1954 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1955 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1956 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1957 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1958 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1959 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1960 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1961 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1962 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1963 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1964 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1965 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1966 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1967 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1968 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1969 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1970 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1971 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1972 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1973 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1974 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1975 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1976 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1977 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1978 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1979 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1980 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1981 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1982 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1983 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1984 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1985 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1986 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1987 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1988 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1989 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1990 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1991 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1992 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1993 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1994 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1995 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1996 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1997 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1998 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1999 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
2000 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
2001 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
2002 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
2003 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
2004 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
2005 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
2006 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
2007 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
2008 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
2009 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
2010 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
2011 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
2012 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
2013 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
2014 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
2015 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
2016 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
2017 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
2018 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
2019 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
2020 {NULL, NULL}
2021 };
2022
2023 /* adjust the expression to a Perl equivalent
2024 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
2025
Michal Vasko40a00082020-05-27 15:20:01 +02002026 /* allocate space for the transformed pattern */
2027 size = strlen(pattern) + 1;
2028 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002030 perl_regex[0] = '\0';
2031
Michal Vasko40a00082020-05-27 15:20:01 +02002032 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
2033 brack = 0;
2034 idx = 0;
2035 orig_ptr = pattern;
2036 while (orig_ptr[0]) {
2037 switch (orig_ptr[0]) {
2038 case '$':
2039 case '^':
2040 if (!brack) {
2041 /* make space for the extra character */
2042 ++size;
2043 perl_regex = ly_realloc(perl_regex, size);
2044 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002045
Michal Vasko40a00082020-05-27 15:20:01 +02002046 /* print escape slash */
2047 perl_regex[idx] = '\\';
2048 ++idx;
2049 }
2050 break;
2051 case '[':
2052 /* must not be escaped */
2053 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2054 ++brack;
2055 }
2056 break;
2057 case ']':
2058 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2059 /* pattern was checked and compiled already */
2060 assert(brack);
2061 --brack;
2062 }
2063 break;
2064 default:
2065 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002066 }
Michal Vasko40a00082020-05-27 15:20:01 +02002067
2068 /* copy char */
2069 perl_regex[idx] = orig_ptr[0];
2070
2071 ++idx;
2072 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002073 }
Michal Vasko40a00082020-05-27 15:20:01 +02002074 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002075
2076 /* substitute Unicode Character Blocks with exact Character Ranges */
2077 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2078 start = ptr - perl_regex;
2079
2080 ptr = strchr(ptr, '}');
2081 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002082 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002083 pattern, perl_regex + start + 2, "unterminated character property");
2084 free(perl_regex);
2085 return LY_EVALID;
2086 }
2087 end = (ptr - perl_regex) + 1;
2088
2089 /* need more space */
2090 if (end - start < URANGE_LEN) {
2091 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002093 }
2094
2095 /* find our range */
2096 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2097 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2098 break;
2099 }
2100 }
2101 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002103 pattern, perl_regex + start + 5, "unknown block name");
2104 free(perl_regex);
2105 return LY_EVALID;
2106 }
2107
2108 /* 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 +02002109 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002110 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002111 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002112 }
2113 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002114 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002115 }
2116 }
Michal Vasko40a00082020-05-27 15:20:01 +02002117 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002118 /* skip brackets */
2119 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2120 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2121 } else {
2122 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2123 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2124 }
2125 }
2126
2127 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002128 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2129 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002130 &err_code, &err_offset, NULL);
2131 if (!code_local) {
2132 PCRE2_UCHAR err_msg[256] = {0};
2133 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002135 free(perl_regex);
2136 return LY_EVALID;
2137 }
2138 free(perl_regex);
2139
Radek Krejci54579462019-04-30 12:47:06 +02002140 if (code) {
2141 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002142 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002143 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002144 }
2145
2146 return LY_SUCCESS;
2147
2148#undef URANGE_LEN
2149}
2150
Radek Krejcia3045382018-11-22 14:30:31 +01002151/**
2152 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2153 * @param[in] ctx Compile context.
2154 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002155 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2156 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2157 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2158 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2159 */
Radek Krejci19a96102018-11-15 13:38:09 +01002160static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002161lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci0f969882020-08-21 16:56:47 +02002162 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
Radek Krejci19a96102018-11-15 13:38:09 +01002163{
2164 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002165 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002166 LY_ERR ret = LY_SUCCESS;
2167
2168 /* first, copy the patterns from the base type */
2169 if (base_patterns) {
2170 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2171 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2172 }
2173
2174 LY_ARRAY_FOR(patterns_p, u) {
2175 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2176 *pattern = calloc(1, sizeof **pattern);
2177 ++(*pattern)->refcount;
2178
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002180 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002181
2182 if (patterns_p[u].arg[0] == 0x15) {
2183 (*pattern)->inverted = 1;
2184 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002185 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done);
2186 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
2187 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
2188 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
2189 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002190 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002191 }
2192done:
2193 return ret;
2194}
2195
Radek Krejcia3045382018-11-22 14:30:31 +01002196/**
2197 * @brief map of the possible restrictions combination for the specific built-in type.
2198 */
Radek Krejci19a96102018-11-15 13:38:09 +01002199static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2200 0 /* LY_TYPE_UNKNOWN */,
2201 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002202 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2203 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2204 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2205 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2206 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002207 LYS_SET_BIT /* LY_TYPE_BITS */,
2208 0 /* LY_TYPE_BOOL */,
2209 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2210 0 /* LY_TYPE_EMPTY */,
2211 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2212 LYS_SET_BASE /* LY_TYPE_IDENT */,
2213 LYS_SET_REQINST /* LY_TYPE_INST */,
2214 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002215 LYS_SET_TYPE /* LY_TYPE_UNION */,
2216 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002217 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002218 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002219 LYS_SET_RANGE /* LY_TYPE_INT64 */
2220};
2221
2222/**
2223 * @brief stringification of the YANG built-in data types
2224 */
Michal Vasko22df3f02020-08-24 13:29:22 +02002225const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
Radek Krejci5969f272018-11-23 10:03:58 +01002226 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
Radek Krejci0f969882020-08-21 16:56:47 +02002227 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"};
Radek Krejci19a96102018-11-15 13:38:09 +01002228
Radek Krejcia3045382018-11-22 14:30:31 +01002229/**
2230 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2231 * @param[in] ctx Compile context.
2232 * @param[in] enums_p Array of the parsed enum structures to compile.
2233 * @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 +01002234 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2235 * @param[out] enums Newly created array of the compiled enums information for the current type.
2236 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2237 */
Radek Krejci19a96102018-11-15 13:38:09 +01002238static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002239lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci0f969882020-08-21 16:56:47 +02002240 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002241{
2242 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002243 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002244 int32_t value = 0;
2245 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002246 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002247
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002248 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002249 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2250 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2251 return LY_EVALID;
2252 }
2253
2254 LY_ARRAY_FOR(enums_p, u) {
2255 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002256 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
2257 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done);
2258 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Radek Krejci693262f2019-04-29 15:23:20 +02002259 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002260 if (base_enums) {
2261 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2262 LY_ARRAY_FOR(base_enums, v) {
2263 if (!strcmp(e->name, base_enums[v].name)) {
2264 break;
2265 }
2266 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002267 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002268 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2269 "Invalid %s - derived type adds new item \"%s\".",
2270 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2271 return LY_EVALID;
2272 }
2273 match = v;
2274 }
2275
2276 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002277 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002278 if (enums_p[u].flags & LYS_SET_VALUE) {
2279 e->value = (int32_t)enums_p[u].value;
2280 if (!u || e->value >= value) {
2281 value = e->value + 1;
2282 }
2283 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002284 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002285 if (e->value == (*enums)[v].value) {
2286 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2287 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2288 e->value, e->name, (*enums)[v].name);
2289 return LY_EVALID;
2290 }
2291 }
2292 } else if (base_enums) {
2293 /* inherit the assigned value */
2294 e->value = base_enums[match].value;
2295 if (!u || e->value >= value) {
2296 value = e->value + 1;
2297 }
2298 } else {
2299 /* assign value automatically */
2300 if (u && value == INT32_MIN) {
2301 /* counter overflow */
2302 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2303 "Invalid enumeration - it is not possible to auto-assign enum value for "
2304 "\"%s\" since the highest value is already 2147483647.", e->name);
2305 return LY_EVALID;
2306 }
2307 e->value = value++;
2308 }
2309 } else { /* LY_TYPE_BITS */
2310 if (enums_p[u].flags & LYS_SET_VALUE) {
2311 e->value = (int32_t)enums_p[u].value;
2312 if (!u || (uint32_t)e->value >= position) {
2313 position = (uint32_t)e->value + 1;
2314 }
2315 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002316 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002317 if (e->value == (*enums)[v].value) {
2318 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2319 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek Krejci0f969882020-08-21 16:56:47 +02002320 (uint32_t)e->value, e->name, (*enums)[v].name);
Radek Krejci19a96102018-11-15 13:38:09 +01002321 return LY_EVALID;
2322 }
2323 }
2324 } else if (base_enums) {
2325 /* inherit the assigned value */
2326 e->value = base_enums[match].value;
2327 if (!u || (uint32_t)e->value >= position) {
2328 position = (uint32_t)e->value + 1;
2329 }
2330 } else {
2331 /* assign value automatically */
2332 if (u && position == 0) {
2333 /* counter overflow */
2334 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2335 "Invalid bits - it is not possible to auto-assign bit position for "
2336 "\"%s\" since the highest value is already 4294967295.", e->name);
2337 return LY_EVALID;
2338 }
2339 e->value = position++;
2340 }
2341 }
2342
2343 if (base_enums) {
2344 /* the assigned values must not change from the derived type */
2345 if (e->value != base_enums[match].value) {
2346 if (basetype == LY_TYPE_ENUM) {
2347 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2348 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2349 e->name, base_enums[match].value, e->value);
2350 } else {
2351 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2352 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2353 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2354 }
2355 return LY_EVALID;
2356 }
2357 }
2358
Radek Krejciec4da802019-05-02 13:02:41 +02002359 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002360 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 +01002361
2362 if (basetype == LY_TYPE_BITS) {
2363 /* keep bits ordered by position */
Radek Krejci1e008d22020-08-17 11:37:37 +02002364 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
Radek Krejci19a96102018-11-15 13:38:09 +01002365 if (v != u) {
2366 memcpy(&storage, e, sizeof *e);
2367 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2368 memcpy(&(*enums)[v], &storage, sizeof storage);
2369 }
2370 }
2371 }
2372
2373done:
2374 return ret;
2375}
2376
Radek Krejcia3045382018-11-22 14:30:31 +01002377/**
2378 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2379 *
2380 * path-arg = absolute-path / relative-path
2381 * absolute-path = 1*("/" (node-identifier *path-predicate))
2382 * relative-path = 1*(".." "/") descendant-path
2383 *
2384 * @param[in,out] path Path to parse.
2385 * @param[out] prefix Prefix of the token, NULL if there is not any.
2386 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2387 * @param[out] name Name of the token.
2388 * @param[out] nam_len Length of the name.
2389 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2390 * must not be changed between consecutive calls. -1 if the
2391 * path is absolute.
2392 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2393 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2394 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002395LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002396lys_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 +02002397 int32_t *parent_times, ly_bool *has_predicate)
Radek Krejcia3045382018-11-22 14:30:31 +01002398{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002399 int32_t par_times = 0;
Radek Krejcia3045382018-11-22 14:30:31 +01002400
2401 assert(path && *path);
2402 assert(parent_times);
2403 assert(prefix);
2404 assert(prefix_len);
2405 assert(name);
2406 assert(name_len);
2407 assert(has_predicate);
2408
2409 *prefix = NULL;
2410 *prefix_len = 0;
2411 *name = NULL;
2412 *name_len = 0;
2413 *has_predicate = 0;
2414
2415 if (!*parent_times) {
2416 if (!strncmp(*path, "..", 2)) {
2417 *path += 2;
2418 ++par_times;
2419 while (!strncmp(*path, "/..", 3)) {
2420 *path += 3;
2421 ++par_times;
2422 }
2423 }
2424 if (par_times) {
2425 *parent_times = par_times;
2426 } else {
2427 *parent_times = -1;
2428 }
2429 }
2430
2431 if (**path != '/') {
2432 return LY_EINVAL;
2433 }
2434 /* skip '/' */
2435 ++(*path);
2436
2437 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002438 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002439
2440 if ((**path == '/' && (*path)[1]) || !**path) {
2441 /* path continues by another token or this is the last token */
2442 return LY_SUCCESS;
2443 } else if ((*path)[0] != '[') {
2444 /* unexpected character */
2445 return LY_EINVAL;
2446 } else {
2447 /* predicate starting with [ */
2448 *has_predicate = 1;
2449 return LY_SUCCESS;
2450 }
2451}
2452
2453/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002454 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2455 *
2456 * The set of features used for target must be a subset of features used for the leafref.
2457 * This is not a perfect, we should compare the truth tables but it could require too much resources
2458 * and RFC 7950 does not require it explicitely, so we simplify that.
2459 *
2460 * @param[in] refnode The leafref node.
2461 * @param[in] target Tha target node of the leafref.
2462 * @return LY_SUCCESS or LY_EVALID;
2463 */
2464static LY_ERR
2465lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2466{
2467 LY_ERR ret = LY_EVALID;
2468 const struct lysc_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002469 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci58d171e2018-11-23 13:50:55 +01002470 struct ly_set features = {0};
2471
2472 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002473 if (iter->iffeatures) {
2474 LY_ARRAY_FOR(iter->iffeatures, u) {
2475 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002476 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002477 }
2478 }
2479 }
2480 }
2481
2482 /* we should have, in features set, a superset of features applicable to the target node.
Radek Krejciba03a5a2020-08-27 14:40:41 +02002483 * If the feature is not present, we don;t have a subset of features applicable
Radek Krejci58d171e2018-11-23 13:50:55 +01002484 * to the leafref itself. */
Radek Krejci58d171e2018-11-23 13:50:55 +01002485 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002486 if (iter->iffeatures) {
2487 LY_ARRAY_FOR(iter->iffeatures, u) {
2488 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002489 if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
2490 /* feature not present */
Radek Krejci58d171e2018-11-23 13:50:55 +01002491 goto cleanup;
2492 }
2493 }
2494 }
2495 }
2496 }
2497 ret = LY_SUCCESS;
2498
2499cleanup:
2500 ly_set_erase(&features, NULL);
2501 return ret;
2502}
2503
Michal Vasko004d3152020-06-11 19:59:22 +02002504static 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 +02002505 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2506 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod);
Radek Krejcia3045382018-11-22 14:30:31 +01002507
Radek Krejcia3045382018-11-22 14:30:31 +01002508/**
2509 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2510 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002511 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2512 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2513 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2514 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002515 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002516 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002517 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002518 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2519 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2520 * @param[out] type Newly created type structure with the filled information about the type.
2521 * @return LY_ERR value.
2522 */
Radek Krejci19a96102018-11-15 13:38:09 +01002523static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002524lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002525 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2526 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2527 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002528{
2529 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002530 struct lysc_type_bin *bin;
2531 struct lysc_type_num *num;
2532 struct lysc_type_str *str;
2533 struct lysc_type_bits *bits;
2534 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002535 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002536 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002537 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002538 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002539
2540 switch (basetype) {
2541 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +02002542 bin = (struct lysc_type_bin *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002543
2544 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002545 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002546 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002547 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002548 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002549 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 +01002550 }
2551 }
2552
2553 if (tpdfname) {
2554 type_p->compiled = *type;
2555 *type = calloc(1, sizeof(struct lysc_type_bin));
2556 }
2557 break;
2558 case LY_TYPE_BITS:
2559 /* RFC 7950 9.7 - bits */
Michal Vasko22df3f02020-08-24 13:29:22 +02002560 bits = (struct lysc_type_bits *)(*type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002561 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002562 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002563 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
2564 (struct lysc_type_bitenum_item **)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002565 }
2566
Radek Krejci555cb5b2018-11-16 14:54:33 +01002567 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002568 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002569 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002570 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002571 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002572 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002573 free(*type);
2574 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002575 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002576 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002577 }
2578
2579 if (tpdfname) {
2580 type_p->compiled = *type;
2581 *type = calloc(1, sizeof(struct lysc_type_bits));
2582 }
2583 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002584 case LY_TYPE_DEC64:
Radek Krejci115a74d2020-08-14 22:18:12 +02002585 dec = (struct lysc_type_dec *)(*type);
Radek Krejci6cba4292018-11-15 17:33:29 +01002586
2587 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002588 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002589 if (!type_p->fraction_digits) {
2590 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002592 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002594 free(*type);
2595 *type = NULL;
2596 }
2597 return LY_EVALID;
2598 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002599 dec->fraction_digits = type_p->fraction_digits;
2600 } else {
2601 if (type_p->fraction_digits) {
2602 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
2603 if (tpdfname) {
2604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2605 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
2606 tpdfname);
2607 } else {
2608 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2609 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
2610 free(*type);
2611 *type = NULL;
2612 }
2613 return LY_EVALID;
Radek Krejci6cba4292018-11-15 17:33:29 +01002614 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002615 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
Radek Krejci6cba4292018-11-15 17:33:29 +01002616 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002617
2618 /* RFC 7950 9.2.4 - range */
2619 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002620 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
Radek Krejci115a74d2020-08-14 22:18:12 +02002621 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002622 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002623 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 +01002624 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002625 }
2626
2627 if (tpdfname) {
2628 type_p->compiled = *type;
2629 *type = calloc(1, sizeof(struct lysc_type_dec));
2630 }
2631 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002632 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002633 str = (struct lysc_type_str *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002634
2635 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002636 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002637 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002638 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002639 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002640 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 +01002641 }
Michal Vasko22df3f02020-08-24 13:29:22 +02002642 } else if (base && ((struct lysc_type_str *)base)->length) {
2643 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002644 }
2645
2646 /* RFC 7950 9.4.5 - pattern */
2647 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002648 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Michal Vasko22df3f02020-08-24 13:29:22 +02002649 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
2650 } else if (base && ((struct lysc_type_str *)base)->patterns) {
2651 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002652 }
2653
2654 if (tpdfname) {
2655 type_p->compiled = *type;
2656 *type = calloc(1, sizeof(struct lysc_type_str));
2657 }
2658 break;
2659 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +02002660 enumeration = (struct lysc_type_enum *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002661
2662 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002663 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002664 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002665 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002666 }
2667
Radek Krejci555cb5b2018-11-16 14:54:33 +01002668 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002669 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002670 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002672 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002674 free(*type);
2675 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002676 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002677 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002678 }
2679
2680 if (tpdfname) {
2681 type_p->compiled = *type;
2682 *type = calloc(1, sizeof(struct lysc_type_enum));
2683 }
2684 break;
2685 case LY_TYPE_INT8:
2686 case LY_TYPE_UINT8:
2687 case LY_TYPE_INT16:
2688 case LY_TYPE_UINT16:
2689 case LY_TYPE_INT32:
2690 case LY_TYPE_UINT32:
2691 case LY_TYPE_INT64:
2692 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +02002693 num = (struct lysc_type_num *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002694
2695 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002696 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002697 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002698 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002699 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002700 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 +01002701 }
2702 }
2703
2704 if (tpdfname) {
2705 type_p->compiled = *type;
2706 *type = calloc(1, sizeof(struct lysc_type_num));
2707 }
2708 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002709 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002710 idref = (struct lysc_type_identityref *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002711
2712 /* RFC 7950 9.10.2 - base */
2713 if (type_p->bases) {
2714 if (base) {
2715 /* only the directly derived identityrefs can contain base specification */
2716 if (tpdfname) {
2717 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002718 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002719 tpdfname);
2720 } else {
2721 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002722 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002723 free(*type);
2724 *type = NULL;
2725 }
2726 return LY_EVALID;
2727 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002728 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002729 }
2730
2731 if (!base && !type_p->flags) {
2732 /* type derived from identityref built-in type must contain at least one base */
2733 if (tpdfname) {
2734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2735 } else {
2736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2737 free(*type);
2738 *type = NULL;
2739 }
2740 return LY_EVALID;
2741 }
2742
2743 if (tpdfname) {
2744 type_p->compiled = *type;
2745 *type = calloc(1, sizeof(struct lysc_type_identityref));
2746 }
2747 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002748 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002749 lref = (struct lysc_type_leafref *)*type;
Michal Vasko004d3152020-06-11 19:59:22 +02002750
Radek Krejcia3045382018-11-22 14:30:31 +01002751 /* RFC 7950 9.9.3 - require-instance */
2752 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002753 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002754 if (tpdfname) {
2755 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2756 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2757 } else {
2758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2759 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2760 free(*type);
2761 *type = NULL;
2762 }
2763 return LY_EVALID;
2764 }
Michal Vasko004d3152020-06-11 19:59:22 +02002765 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002766 } else if (base) {
2767 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002768 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002769 } else {
2770 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002771 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002772 }
2773 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002774 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2775 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002776 } else if (base) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002777 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path);
2778 lref->path_context = ((struct lysc_type_leafref *)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002779 } else if (tpdfname) {
2780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2781 return LY_EVALID;
2782 } else {
2783 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2784 free(*type);
2785 *type = NULL;
2786 return LY_EVALID;
2787 }
2788 if (tpdfname) {
2789 type_p->compiled = *type;
2790 *type = calloc(1, sizeof(struct lysc_type_leafref));
2791 }
2792 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002793 case LY_TYPE_INST:
2794 /* RFC 7950 9.9.3 - require-instance */
2795 if (type_p->flags & LYS_SET_REQINST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002796 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
Radek Krejci16c0f822018-11-16 10:46:10 +01002797 } else {
2798 /* default is true */
Michal Vasko22df3f02020-08-24 13:29:22 +02002799 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
Radek Krejci16c0f822018-11-16 10:46:10 +01002800 }
2801
2802 if (tpdfname) {
2803 type_p->compiled = *type;
2804 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2805 }
2806 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002807 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +02002808 un = (struct lysc_type_union *)(*type);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002809
2810 /* RFC 7950 7.4 - type */
2811 if (type_p->types) {
2812 if (base) {
2813 /* only the directly derived union can contain types specification */
2814 if (tpdfname) {
2815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2816 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2817 tpdfname);
2818 } else {
2819 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2820 "Invalid type substatement for the type not directly derived from union built-in type.");
2821 free(*type);
2822 *type = NULL;
2823 }
2824 return LY_EVALID;
2825 }
2826 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002827 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2828 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002829 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002830 &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002831 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2832 /* add space for additional types from the union subtype */
2833 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vasko22df3f02020-08-24 13:29:22 +02002834 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
2835 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002836
2837 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002838 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002839 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2840 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2841 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
Michal Vasko22df3f02020-08-24 13:29:22 +02002842 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 +02002843 lref = (struct lysc_type_leafref *)un->types[u + additional];
2844
2845 lref->basetype = LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02002846 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path);
Michal Vasko004d3152020-06-11 19:59:22 +02002847 lref->refcount = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +02002848 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
2849 lref->path_context = ((struct lysc_type_leafref *)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002850 /* TODO extensions */
2851
2852 } else {
2853 un->types[u + additional] = un_aux->types[v];
2854 ++un_aux->types[v]->refcount;
2855 }
2856 ++additional;
2857 LY_ARRAY_INCREMENT(un->types);
2858 }
2859 /* compensate u increment in main loop */
2860 --additional;
2861
2862 /* free the replaced union subtype */
Michal Vasko22df3f02020-08-24 13:29:22 +02002863 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002864 } else {
2865 LY_ARRAY_INCREMENT(un->types);
2866 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002867 }
2868 }
2869
2870 if (!base && !type_p->flags) {
2871 /* type derived from union built-in type must contain at least one type */
2872 if (tpdfname) {
2873 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2874 } else {
2875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2876 free(*type);
2877 *type = NULL;
2878 }
2879 return LY_EVALID;
2880 }
2881
2882 if (tpdfname) {
2883 type_p->compiled = *type;
2884 *type = calloc(1, sizeof(struct lysc_type_union));
2885 }
2886 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002887 case LY_TYPE_BOOL:
2888 case LY_TYPE_EMPTY:
2889 case LY_TYPE_UNKNOWN: /* just to complete switch */
2890 break;
2891 }
2892 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2893done:
2894 return ret;
2895}
2896
Radek Krejcia3045382018-11-22 14:30:31 +01002897/**
2898 * @brief Compile information about the leaf/leaf-list's type.
2899 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002900 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2901 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2902 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2903 * @param[in] context_name Name of the context node or referencing typedef for logging.
2904 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002905 * @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 +01002906 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002907 * @param[out] dflt Default value for the type.
2908 * @param[out] dflt_mod Local module for the default value.
Radek Krejcia3045382018-11-22 14:30:31 +01002909 * @return LY_ERR value.
2910 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002911static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002912lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002913 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2914 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod)
Radek Krejci19a96102018-11-15 13:38:09 +01002915{
2916 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02002917 ly_bool dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002918 struct type_context {
2919 const struct lysp_tpdf *tpdf;
2920 struct lysp_node *node;
2921 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002922 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002923 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002924 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002925 struct ly_set tpdf_chain = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002926
2927 assert((dflt && dflt_mod) || (!dflt && !dflt_mod));
Radek Krejci19a96102018-11-15 13:38:09 +01002928
2929 (*type) = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002930 if (dflt) {
2931 *dflt = NULL;
2932 *dflt_mod = NULL;
2933 }
Radek Krejci19a96102018-11-15 13:38:09 +01002934
2935 tctx = calloc(1, sizeof *tctx);
2936 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002937 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002938 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2939 ret == LY_SUCCESS;
2940 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2941 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2942 if (basetype) {
2943 break;
2944 }
2945
2946 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002947 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2948 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci87e25252020-09-15 13:28:31 +02002949 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01002950
Radek Krejcicdfecd92018-11-26 11:27:32 +01002951 if (units && !*units) {
2952 /* inherit units */
Radek Krejci87e25252020-09-15 13:28:31 +02002953 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2954 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002955 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002956 if (dflt && !*dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002957 /* inherit default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002958 *dflt = tctx->tpdf->dflt;
2959 *dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002960 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002961 if (dummyloops && (!units || *units) && dflt && *dflt) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002962 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002963 break;
2964 }
2965
Radek Krejci19a96102018-11-15 13:38:09 +01002966 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002967 /* it is not necessary to continue, the rest of the chain was already compiled,
2968 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002969 basetype = tctx->tpdf->type.compiled->basetype;
Radek Krejciba03a5a2020-08-27 14:40:41 +02002970 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02002971 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejciba03a5a2020-08-27 14:40:41 +02002972
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002973 if ((units && !*units) || (dflt && !*dflt)) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002974 dummyloops = 1;
2975 goto preparenext;
2976 } else {
2977 tctx = NULL;
2978 break;
2979 }
Radek Krejci19a96102018-11-15 13:38:09 +01002980 }
2981
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002982 /* circular typedef reference detection */
Radek Krejci1deb5be2020-08-26 16:43:36 +02002983 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002984 /* local part */
Michal Vasko22df3f02020-08-24 13:29:22 +02002985 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002986 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2988 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2989 free(tctx);
2990 ret = LY_EVALID;
2991 goto cleanup;
2992 }
2993 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02002994 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002995 /* global part for unions corner case */
Michal Vasko22df3f02020-08-24 13:29:22 +02002996 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002997 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2999 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3000 free(tctx);
3001 ret = LY_EVALID;
3002 goto cleanup;
3003 }
3004 }
3005
Radek Krejci19a96102018-11-15 13:38:09 +01003006 /* store information for the following processing */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003007 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02003008 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003009
Radek Krejcicdfecd92018-11-26 11:27:32 +01003010preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003011 /* prepare next loop */
3012 tctx_prev = tctx;
3013 tctx = calloc(1, sizeof *tctx);
3014 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3015 }
3016 free(tctx);
3017
3018 /* allocate type according to the basetype */
3019 switch (basetype) {
3020 case LY_TYPE_BINARY:
3021 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003022 break;
3023 case LY_TYPE_BITS:
3024 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003025 break;
3026 case LY_TYPE_BOOL:
3027 case LY_TYPE_EMPTY:
3028 *type = calloc(1, sizeof(struct lysc_type));
3029 break;
3030 case LY_TYPE_DEC64:
3031 *type = calloc(1, sizeof(struct lysc_type_dec));
3032 break;
3033 case LY_TYPE_ENUM:
3034 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003035 break;
3036 case LY_TYPE_IDENT:
3037 *type = calloc(1, sizeof(struct lysc_type_identityref));
3038 break;
3039 case LY_TYPE_INST:
3040 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3041 break;
3042 case LY_TYPE_LEAFREF:
3043 *type = calloc(1, sizeof(struct lysc_type_leafref));
3044 break;
3045 case LY_TYPE_STRING:
3046 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003047 break;
3048 case LY_TYPE_UNION:
3049 *type = calloc(1, sizeof(struct lysc_type_union));
3050 break;
3051 case LY_TYPE_INT8:
3052 case LY_TYPE_UINT8:
3053 case LY_TYPE_INT16:
3054 case LY_TYPE_UINT16:
3055 case LY_TYPE_INT32:
3056 case LY_TYPE_UINT32:
3057 case LY_TYPE_INT64:
3058 case LY_TYPE_UINT64:
3059 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003060 break;
3061 case LY_TYPE_UNKNOWN:
3062 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3063 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3064 ret = LY_EVALID;
3065 goto cleanup;
3066 }
3067 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003068 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3070 ly_data_type2str[basetype]);
3071 free(*type);
3072 (*type) = NULL;
3073 ret = LY_EVALID;
3074 goto cleanup;
3075 }
3076
3077 /* get restrictions from the referred typedefs */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003078 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003079 tctx = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003080
3081 /* remember the typedef context for circular check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003082 ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
3083 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003084
Radek Krejci43699232018-11-23 14:59:46 +01003085 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003086 base = tctx->tpdf->type.compiled;
3087 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003088 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003089 /* no change, just use the type information from the base */
Michal Vasko22df3f02020-08-24 13:29:22 +02003090 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 +01003091 ++base->refcount;
3092 continue;
3093 }
3094
3095 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003096 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3097 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3098 tctx->tpdf->name, ly_data_type2str[basetype]);
3099 ret = LY_EVALID;
3100 goto cleanup;
3101 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3102 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3103 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3104 tctx->tpdf->name, tctx->tpdf->dflt);
3105 ret = LY_EVALID;
3106 goto cleanup;
3107 }
3108
Radek Krejci19a96102018-11-15 13:38:09 +01003109 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003110 /* TODO user type plugins */
3111 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003112 prev_type = *type;
Michal Vasko22df3f02020-08-24 13:29:22 +02003113 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 +01003114 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003115 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003116 LY_CHECK_GOTO(ret, cleanup);
3117 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003118 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003119 /* remove the processed typedef contexts from the stack for circular check */
3120 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003121
Radek Krejcic5c27e52018-11-15 14:38:11 +01003122 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003123 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003124 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003125 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003126 /* TODO user type plugins */
3127 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003128 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003129 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 +01003130 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003131 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003132 /* no specific restriction in leaf's type definition, copy from the base */
3133 free(*type);
3134 (*type) = base;
3135 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003136 }
3137
Radek Krejci0935f412019-08-20 16:15:18 +02003138 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003139
3140cleanup:
3141 ly_set_erase(&tpdf_chain, free);
3142 return ret;
3143}
3144
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003145/**
3146 * @brief Compile status information of the given node.
3147 *
3148 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3149 * has the status correctly set during the compilation.
3150 *
3151 * @param[in] ctx Compile context
3152 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3153 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3154 * the compatibility with the parent's status value.
3155 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3156 * @return LY_ERR value.
3157 */
3158static LY_ERR
3159lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3160{
3161 /* status - it is not inherited by specification, but it does not make sense to have
3162 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3163 if (!((*node_flags) & LYS_STATUS_MASK)) {
3164 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3165 if ((parent_flags & 0x3) != 0x3) {
3166 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3167 * combination of bits (0x3) which marks the uses_status value */
3168 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
Radek Krejci0f969882020-08-21 16:56:47 +02003169 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003170 }
3171 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3172 } else {
3173 (*node_flags) |= LYS_STATUS_CURR;
3174 }
3175 } else if (parent_flags & LYS_STATUS_MASK) {
3176 /* check status compatibility with the parent */
3177 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3178 if ((*node_flags) & LYS_STATUS_CURR) {
3179 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3180 "A \"current\" status is in conflict with the parent's \"%s\" status.",
Radek Krejci0f969882020-08-21 16:56:47 +02003181 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003182 } else { /* LYS_STATUS_DEPRC */
3183 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3184 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3185 }
3186 return LY_EVALID;
3187 }
3188 }
3189 return LY_SUCCESS;
3190}
3191
Radek Krejci8cce8532019-03-05 11:27:45 +01003192/**
3193 * @brief Check uniqness of the node/action/notification name.
3194 *
3195 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3196 * structures, but they share the namespace so we need to check their name collisions.
3197 *
3198 * @param[in] ctx Compile context.
Michal Vasko20424b42020-08-31 12:29:38 +02003199 * @param[in] parent Parent of the nodes to check, can be NULL.
Radek Krejci8cce8532019-03-05 11:27:45 +01003200 * @param[in] name Name of the item to find in the given lists.
Michal Vasko20424b42020-08-31 12:29:38 +02003201 * @param[in] exclude Node that was just added that should be excluded from the name checking.
Radek Krejci8cce8532019-03-05 11:27:45 +01003202 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3203 */
3204static LY_ERR
Michal Vasko20424b42020-08-31 12:29:38 +02003205lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
3206 const struct lysc_node *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003207{
Michal Vasko20424b42020-08-31 12:29:38 +02003208 const struct lysc_node *iter, *iter2;
3209 const struct lysc_action *actions;
3210 const struct lysc_notif *notifs;
3211 uint32_t getnext_flags;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003212 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003213
Michal Vasko20424b42020-08-31 12:29:38 +02003214#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
3215
3216 if (exclude->nodetype == LYS_CASE) {
3217 /* check restricted only to all the cases */
3218 assert(parent->nodetype == LYS_CHOICE);
3219 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
3220 if (CHECK_NODE(iter, exclude, name)) {
3221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
3222 return LY_EEXIST;
3223 }
3224 }
3225
3226 return LY_SUCCESS;
3227 }
3228
3229 /* no reason for our parent to be choice anymore */
3230 assert(!parent || (parent->nodetype != LYS_CHOICE));
3231
3232 if (parent && (parent->nodetype == LYS_CASE)) {
3233 /* move to the first data definition parent */
3234 parent = lysc_data_parent(parent);
3235 }
3236
3237 getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE;
3238 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
3239 getnext_flags |= LYS_GETNEXT_OUTPUT;
3240 }
3241
3242 iter = NULL;
3243 while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) {
3244 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003245 goto error;
3246 }
Michal Vasko20424b42020-08-31 12:29:38 +02003247
3248 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
3249 if (iter->nodetype == LYS_CHOICE) {
3250 iter2 = NULL;
3251 while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) {
3252 if (CHECK_NODE(iter2, exclude, name)) {
3253 goto error;
3254 }
3255 }
3256 }
Radek Krejci8cce8532019-03-05 11:27:45 +01003257 }
Michal Vasko20424b42020-08-31 12:29:38 +02003258
3259 actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003260 LY_ARRAY_FOR(actions, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003261 if (CHECK_NODE(&actions[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003262 goto error;
3263 }
3264 }
Michal Vasko20424b42020-08-31 12:29:38 +02003265
3266 notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003267 LY_ARRAY_FOR(notifs, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003268 if (CHECK_NODE(&notifs[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003269 goto error;
3270 }
3271 }
3272 return LY_SUCCESS;
Michal Vasko20424b42020-08-31 12:29:38 +02003273
Radek Krejci8cce8532019-03-05 11:27:45 +01003274error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003276 return LY_EEXIST;
Michal Vasko20424b42020-08-31 12:29:38 +02003277
3278#undef CHECK_NODE
Radek Krejci8cce8532019-03-05 11:27:45 +01003279}
3280
Radek Krejciec4da802019-05-02 13:02:41 +02003281static 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 +01003282
Radek Krejcia3045382018-11-22 14:30:31 +01003283/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003284 * @brief Compile parsed RPC/action schema node information.
3285 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003286 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003287 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003288 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3289 * @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).
3290 * Zero means no uses, non-zero value with no status bit set mean the default status.
3291 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3292 */
3293static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003294lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003295 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003296{
3297 LY_ERR ret = LY_SUCCESS;
3298 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003299 LY_ARRAY_COUNT_TYPE u;
3300 uint32_t opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003301
Radek Krejci327de162019-06-14 12:52:07 +02003302 lysc_update_path(ctx, parent, action_p->name);
3303
Michal Vasko20424b42020-08-31 12:29:38 +02003304 /* member needed for uniqueness check lys_getnext() */
3305 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
3306 action->module = ctx->mod;
3307 action->parent = parent;
3308
3309 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action));
Radek Krejci8cce8532019-03-05 11:27:45 +01003310
Radek Krejciec4da802019-05-02 13:02:41 +02003311 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003313 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003314 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003315 return LY_EVALID;
3316 }
3317
Radek Krejciec4da802019-05-02 13:02:41 +02003318 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003319 action->sp = action_p;
3320 }
3321 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3322
3323 /* status - it is not inherited by specification, but it does not make sense to have
3324 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003325 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003326
Radek Krejci011e4aa2020-09-04 15:22:31 +02003327 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
3328 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
3329 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003330 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003331 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003332
3333 /* input */
Michal Vasko22df3f02020-08-24 13:29:22 +02003334 lysc_update_path(ctx, (struct lysc_node *)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003335 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003336 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 +02003337 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003338 LY_LIST_FOR(action_p->input.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003339 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003340 }
Radek Krejci327de162019-06-14 12:52:07 +02003341 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003342 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003343
3344 /* output */
Michal Vasko22df3f02020-08-24 13:29:22 +02003345 lysc_update_path(ctx, (struct lysc_node *)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003346 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003347 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 +02003348 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003349 LY_LIST_FOR(action_p->output.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003350 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003351 }
Radek Krejci327de162019-06-14 12:52:07 +02003352 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003353 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003354
3355 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3356 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003357 ret = ly_set_add(&ctx->xpath, action, 0, NULL);
3358 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003359 }
3360
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003361cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003362 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003363 return ret;
3364}
3365
3366/**
Radek Krejci43981a32019-04-12 09:44:11 +02003367 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003368 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003369 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003370 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3371 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3372 * @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 +02003373 * Zero means no uses, non-zero value with no status bit set mean the default status.
3374 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3375 */
3376static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003377lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003378 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
Radek Krejcifc11bd72019-04-11 16:00:05 +02003379{
3380 LY_ERR ret = LY_SUCCESS;
3381 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003382 LY_ARRAY_COUNT_TYPE u;
3383 uint32_t opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003384
Radek Krejci327de162019-06-14 12:52:07 +02003385 lysc_update_path(ctx, parent, notif_p->name);
3386
Michal Vasko20424b42020-08-31 12:29:38 +02003387 /* member needed for uniqueness check lys_getnext() */
3388 notif->nodetype = LYS_NOTIF;
3389 notif->module = ctx->mod;
3390 notif->parent = parent;
3391
3392 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003393
Radek Krejciec4da802019-05-02 13:02:41 +02003394 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3396 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003397 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003398 return LY_EVALID;
3399 }
3400
Radek Krejciec4da802019-05-02 13:02:41 +02003401 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003402 notif->sp = notif_p;
3403 }
3404 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3405
3406 /* status - it is not inherited by specification, but it does not make sense to have
3407 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003408 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
3409 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003410
Radek Krejci011e4aa2020-09-04 15:22:31 +02003411 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
3412 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
3413 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003414 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003415 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003416 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3417 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003418 ret = ly_set_add(&ctx->xpath, notif, 0, NULL);
3419 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003420 }
Radek Krejci0935f412019-08-20 16:15:18 +02003421 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003422
Radek Krejciec4da802019-05-02 13:02:41 +02003423 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003424 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003425 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status);
3426 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003427 }
3428
Radek Krejci327de162019-06-14 12:52:07 +02003429 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003430cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003431 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003432 return ret;
3433}
3434
3435/**
Radek Krejcia3045382018-11-22 14:30:31 +01003436 * @brief Compile parsed container node information.
3437 * @param[in] ctx Compile context
3438 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003439 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3440 * is enriched with the container-specific information.
3441 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3442 */
Radek Krejci19a96102018-11-15 13:38:09 +01003443static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003444lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003445{
Michal Vasko22df3f02020-08-24 13:29:22 +02003446 struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p;
3447 struct lysc_node_container *cont = (struct lysc_node_container *)node;
Radek Krejci19a96102018-11-15 13:38:09 +01003448 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003449 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003450 LY_ERR ret = LY_SUCCESS;
3451
Radek Krejcife909632019-02-12 15:34:42 +01003452 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003453 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003454 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003455 } else if (cont_p->musts) {
3456 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003457 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3458 cont->flags |= LYS_PRESENCE;
3459 } else if (cont_p->when) {
3460 /* container with a when condition */
3461 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 +02003462 cont->flags |= LYS_PRESENCE;
3463 } else if (cont_p->parent) {
3464 if (cont_p->parent->nodetype == LYS_CHOICE) {
3465 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003466 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 +02003467 cont_p->name, cont_p->parent->name);
3468 cont->flags |= LYS_PRESENCE;
3469 } else if ((cont_p->parent->nodetype == LYS_CASE)
3470 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3471 /* 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 +02003472 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 +02003473 cont_p->name, cont_p->parent->name);
3474 cont->flags |= LYS_PRESENCE;
3475 }
Radek Krejcife909632019-02-12 15:34:42 +01003476 }
3477
Michal Vaskoba417ac2020-08-06 14:48:20 +02003478 /* more cases when the container has meaning but is kept NP for convenience:
3479 * - when condition
3480 * - direct child action/notification
3481 */
3482
Radek Krejci19a96102018-11-15 13:38:09 +01003483 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003484 ret = lys_compile_node(ctx, child_p, node, 0);
3485 LY_CHECK_GOTO(ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003486 }
3487
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003488 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003489 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3490 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003491 ret = ly_set_add(&ctx->xpath, cont, 0, NULL);
3492 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003493 }
Radek Krejciec4da802019-05-02 13:02:41 +02003494 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3495 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003496
3497done:
3498 return ret;
3499}
3500
Radek Krejci33f72892019-02-21 10:36:58 +01003501/*
3502 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3503 * @param[in] ctx Compile context.
3504 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3505 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003506 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3507 * @return LY_ERR value.
3508 */
3509static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003510lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003511 struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003512{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003513 const char *dflt;
3514 struct lys_module *dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003515
Radek Krejciec4da802019-05-02 13:02:41 +02003516 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 +02003517 leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod));
3518
3519 /* store default value, if any */
3520 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
3521 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, dflt, dflt_mod));
Radek Krejci33f72892019-02-21 10:36:58 +01003522 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003523
Radek Krejci33f72892019-02-21 10:36:58 +01003524 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3525 /* 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 +02003526 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003527 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003528 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003529 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
3530 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
Radek Krejci33f72892019-02-21 10:36:58 +01003531 /* 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 +02003532 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003533 }
3534 }
3535 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003536 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3537 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3538 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3539 return LY_EVALID;
3540 }
3541 }
3542
Radek Krejci33f72892019-02-21 10:36:58 +01003543 return LY_SUCCESS;
3544}
3545
Radek Krejcia3045382018-11-22 14:30:31 +01003546/**
3547 * @brief Compile parsed leaf node information.
3548 * @param[in] ctx Compile context
3549 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003550 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3551 * is enriched with the leaf-specific information.
3552 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3553 */
Radek Krejci19a96102018-11-15 13:38:09 +01003554static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003555lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003556{
Michal Vasko22df3f02020-08-24 13:29:22 +02003557 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p;
3558 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003559 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003560 LY_ERR ret = LY_SUCCESS;
3561
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003562 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003563 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3564 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003565 ret = ly_set_add(&ctx->xpath, leaf, 0, NULL);
3566 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003567 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003568 if (leaf_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003569 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003570 leaf->flags |= LYS_SET_UNITS;
3571 }
Radek Krejcia1911222019-07-22 17:24:50 +02003572
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003573 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003574 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3575 LY_CHECK_GOTO(ret, done);
Radek Krejcia1911222019-07-22 17:24:50 +02003576
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003577 /* store/update default value */
Radek Krejciccd20f12019-02-15 14:12:27 +01003578 if (leaf_p->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003579 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, leaf_p->dflt, ctx->mod_def));
Radek Krejci76b3e962018-12-14 17:01:25 +01003580 leaf->flags |= LYS_SET_DFLT;
3581 }
Radek Krejci43699232018-11-23 14:59:46 +01003582
Radek Krejci19a96102018-11-15 13:38:09 +01003583done:
3584 return ret;
3585}
3586
Radek Krejcia3045382018-11-22 14:30:31 +01003587/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003588 * @brief Compile parsed leaf-list node information.
3589 * @param[in] ctx Compile context
3590 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003591 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3592 * is enriched with the leaf-list-specific information.
3593 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3594 */
3595static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003596lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003597{
Michal Vasko22df3f02020-08-24 13:29:22 +02003598 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p;
3599 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003600 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003601 LY_ERR ret = LY_SUCCESS;
3602
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003603 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003604 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3605 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003606 ret = ly_set_add(&ctx->xpath, llist, 0, NULL);
3607 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003608 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003609 if (llist_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003610 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003611 llist->flags |= LYS_SET_UNITS;
3612 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003613
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003614 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003615 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist);
3616 LY_CHECK_GOTO(ret, done);
Michal Vasko6a044b22020-01-15 12:25:39 +01003617
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003618 /* store/update default values */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003619 if (llist_p->dflts) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003620 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, llist_p->dflts, ctx->mod_def), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003621 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003622 }
3623
3624 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003625 if (llist->min) {
3626 llist->flags |= LYS_MAND_TRUE;
3627 }
Radek Krejcib7408632018-11-28 17:12:11 +01003628 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003629
Radek Krejci0e5d8382018-11-28 16:37:53 +01003630done:
3631 return ret;
3632}
3633
3634/**
Radek Krejci7af64242019-02-18 13:07:53 +01003635 * @brief Compile information about list's uniques.
3636 * @param[in] ctx Compile context.
3637 * @param[in] context_module Module where the prefixes are going to be resolved.
3638 * @param[in] uniques Sized array list of unique statements.
3639 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3640 * @return LY_ERR value.
3641 */
3642static LY_ERR
3643lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3644{
3645 LY_ERR ret = LY_SUCCESS;
3646 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003647 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003648 const char *keystr, *delim;
3649 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003650 LY_ARRAY_COUNT_TYPE v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003651 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003652 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003653
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003654 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003655 config = -1;
3656 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3657 keystr = uniques[v];
3658 while (keystr) {
3659 delim = strpbrk(keystr, " \t\n");
3660 if (delim) {
3661 len = delim - keystr;
3662 while (isspace(*delim)) {
3663 ++delim;
3664 }
3665 } else {
3666 len = strlen(keystr);
3667 }
3668
3669 /* unique node must be present */
3670 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko22df3f02020-08-24 13:29:22 +02003671 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0,
3672 (const struct lysc_node **)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003673 if (ret != LY_SUCCESS) {
3674 if (ret == LY_EDENIED) {
3675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003676 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003677 len, keystr, lys_nodetype2str((*key)->nodetype));
3678 }
3679 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003680 } else if (flags) {
3681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3682 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003683 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003684 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003685 }
3686
3687 /* all referenced leafs must be of the same config type */
3688 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003690 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003691 return LY_EVALID;
3692 } else if ((*key)->flags & LYS_CONFIG_W) {
3693 config = 1;
3694 } else { /* LYS_CONFIG_R */
3695 config = 0;
3696 }
3697
Michal Vasko14654712020-02-06 08:35:21 +01003698 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3699 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3700 if (parent->nodetype == LYS_LIST) {
3701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3702 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3703 return LY_EVALID;
3704 }
3705 }
3706
Radek Krejci7af64242019-02-18 13:07:53 +01003707 /* check status */
3708 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0f969882020-08-21 16:56:47 +02003709 (*key)->flags, (*key)->module, (*key)->name));
Radek Krejci7af64242019-02-18 13:07:53 +01003710
3711 /* mark leaf as unique */
3712 (*key)->flags |= LYS_UNIQUE;
3713
3714 /* next unique value in line */
3715 keystr = delim;
3716 }
3717 /* next unique definition */
3718 }
3719
3720 return LY_SUCCESS;
3721}
3722
3723/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003724 * @brief Compile parsed list node information.
3725 * @param[in] ctx Compile context
3726 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3728 * is enriched with the list-specific information.
3729 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3730 */
3731static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003732lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003733{
Michal Vasko22df3f02020-08-24 13:29:22 +02003734 struct lysp_node_list *list_p = (struct lysp_node_list *)node_p;
3735 struct lysc_node_list *list = (struct lysc_node_list *)node;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003736 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003737 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003738 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003739 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003740 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003741 LY_ERR ret = LY_SUCCESS;
3742
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003743 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003744 if (list->min) {
3745 list->flags |= LYS_MAND_TRUE;
3746 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003747 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3748
3749 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003750 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003751 }
3752
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003753 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003754 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3755 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003756 LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL));
Michal Vasko5d8756a2019-11-07 15:21:00 +01003757 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003758
3759 /* keys */
3760 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3762 return LY_EVALID;
3763 }
3764
3765 /* find all the keys (must be direct children) */
3766 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003767 if (!keystr) {
3768 /* keyless list */
3769 list->flags |= LYS_KEYLESS;
3770 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003771 while (keystr) {
3772 delim = strpbrk(keystr, " \t\n");
3773 if (delim) {
3774 len = delim - keystr;
3775 while (isspace(*delim)) {
3776 ++delim;
3777 }
3778 } else {
3779 len = strlen(keystr);
3780 }
3781
3782 /* key node must be present */
Michal Vasko22df3f02020-08-24 13:29:22 +02003783 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 +02003784 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3786 "The list's key \"%.*s\" not found.", len, keystr);
3787 return LY_EVALID;
3788 }
3789 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003790 if (key->flags & LYS_KEY) {
3791 /* the node was already marked as a key */
3792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3793 "Duplicated key identifier \"%.*s\".", len, keystr);
3794 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003795 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003796
Michal Vasko22df3f02020-08-24 13:29:22 +02003797 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003798 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003799 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3801 return LY_EVALID;
3802 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003803 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003804 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003805 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003806 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003807 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003808 return LY_EVALID;
3809 }
3810 } else {
3811 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003812 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003813 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003814 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003815 return LY_EVALID;
3816 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003817 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003818 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003819 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003820 return LY_EVALID;
3821 }
3822 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003823
3824 /* check status */
3825 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003826 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003827
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003828 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003829 if (key->dflt) {
3830 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3831 lysc_type_free(ctx->ctx, key->dflt->realtype);
3832 free(key->dflt);
3833 key->dflt = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003834 }
3835 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003836 key->flags |= LYS_KEY;
3837
3838 /* move it to the correct position */
Michal Vasko22df3f02020-08-24 13:29:22 +02003839 if ((prev_key && (struct lysc_node *)prev_key != key->prev) || (!prev_key && key->prev->next)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02003840 /* fix links in closest previous siblings of the key */
3841 if (key->next) {
3842 key->next->prev = key->prev;
3843 } else {
3844 /* last child */
3845 list->child->prev = key->prev;
3846 }
3847 if (key->prev->next) {
3848 key->prev->next = key->next;
3849 }
3850 /* fix links in the key */
3851 if (prev_key) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003852 key->prev = (struct lysc_node *)prev_key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003853 key->next = prev_key->next;
3854 } else {
3855 key->prev = list->child->prev;
3856 key->next = list->child;
3857 }
3858 /* fix links in closes future siblings of the key */
3859 if (prev_key) {
3860 if (prev_key->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003861 prev_key->next->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003862 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003863 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003864 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003865 prev_key->next = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003866 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003867 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003868 }
3869 /* fix links in parent */
3870 if (!key->prev->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003871 list->child = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003872 }
3873 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003874
3875 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003876 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003877 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003878 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003879 }
3880
3881 /* uniques */
3882 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003883 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003884 }
3885
Radek Krejciec4da802019-05-02 13:02:41 +02003886 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3887 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003888
3889done:
3890 return ret;
3891}
3892
Radek Krejcib56c7502019-02-13 14:19:54 +01003893/**
3894 * @brief Do some checks and set the default choice's case.
3895 *
3896 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3897 *
3898 * @param[in] ctx Compile context.
3899 * @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,
3900 * not the reference to the imported module.
3901 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3902 * @return LY_ERR value.
3903 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003904static LY_ERR
3905lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3906{
Michal Vasko22df3f02020-08-24 13:29:22 +02003907 struct lysc_node *iter, *node = (struct lysc_node *)ch;
Radek Krejci76b3e962018-12-14 17:01:25 +01003908 const char *prefix = NULL, *name;
3909 size_t prefix_len = 0;
3910
3911 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3912 name = strchr(dflt, ':');
3913 if (name) {
3914 prefix = dflt;
3915 prefix_len = name - prefix;
3916 ++name;
3917 } else {
3918 name = dflt;
3919 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003920 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003921 /* prefixed default case make sense only for the prefix of the schema itself */
3922 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3923 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3924 prefix_len, prefix);
3925 return LY_EVALID;
3926 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003927 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 +01003928 if (!ch->dflt) {
3929 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3930 "Default case \"%s\" not found.", dflt);
3931 return LY_EVALID;
3932 }
3933 /* no mandatory nodes directly under the default case */
3934 LY_LIST_FOR(ch->dflt->child, iter) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003935 if (iter->parent != (struct lysc_node *)ch->dflt) {
Radek Krejcife13da42019-02-15 14:51:01 +01003936 break;
3937 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003938 if (iter->flags & LYS_MAND_TRUE) {
3939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3940 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3941 return LY_EVALID;
3942 }
3943 }
Radek Krejci01342af2019-01-03 15:18:08 +01003944 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003945 return LY_SUCCESS;
3946}
3947
Radek Krejciccd20f12019-02-15 14:12:27 +01003948static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003949lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003950{
3951 struct lys_module *mod;
3952 const char *prefix = NULL, *name;
3953 size_t prefix_len = 0;
3954 struct lysc_node_case *cs;
3955 struct lysc_node *node;
3956
3957 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3958 name = strchr(dflt, ':');
3959 if (name) {
3960 prefix = dflt;
3961 prefix_len = name - prefix;
3962 ++name;
3963 } else {
3964 name = dflt;
3965 }
3966 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3967 if (prefix) {
3968 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3969 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003970 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3971 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003972 return LY_EVALID;
3973 }
3974 } else {
3975 mod = ctx->mod;
3976 }
3977 /* get the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003978 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 +01003979 if (!cs) {
3980 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003981 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003982 return LY_EVALID;
3983 }
3984
3985 /* check that there is no mandatory node */
3986 LY_LIST_FOR(cs->child, node) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003987 if (node->parent != (struct lysc_node *)cs) {
Radek Krejcife13da42019-02-15 14:51:01 +01003988 break;
3989 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003990 if (node->flags & LYS_MAND_TRUE) {
3991 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003992 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3993 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003994 return LY_EVALID;
3995 }
3996 }
3997
3998 /* set the default case in choice */
3999 ch->dflt = cs;
4000 cs->flags |= LYS_SET_DFLT;
4001
4002 return LY_SUCCESS;
4003}
4004
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004005/**
Michal Vasko20424b42020-08-31 12:29:38 +02004006 * @brief Compile choice children.
4007 *
4008 * @param[in] ctx Compile context
4009 * @param[in] child_p Parsed choice children nodes.
4010 * @param[in] node Compiled choice node to compile and add children to.
4011 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4012 */
4013static LY_ERR
4014lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node)
4015{
4016 LY_ERR ret = LY_SUCCESS;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004017 struct lysp_node *child_p_next = child_p->next;
Michal Vasko20424b42020-08-31 12:29:38 +02004018 struct lysp_node_case *cs_p;
4019
4020 if (child_p->nodetype == LYS_CASE) {
4021 /* standard case under choice */
4022 ret = lys_compile_node(ctx, child_p, node, 0);
4023 } else {
4024 /* we need the implicit case first, so create a fake parsed case */
4025 cs_p = calloc(1, sizeof *cs_p);
4026 cs_p->nodetype = LYS_CASE;
Radek Krejci87e25252020-09-15 13:28:31 +02004027 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
Michal Vasko20424b42020-08-31 12:29:38 +02004028 cs_p->child = child_p;
4029
4030 /* make the child the only case child */
Michal Vasko20424b42020-08-31 12:29:38 +02004031 child_p->next = NULL;
4032
4033 /* compile it normally */
4034 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0);
4035
Radek Krejci87e25252020-09-15 13:28:31 +02004036free_fake_node:
Michal Vasko20424b42020-08-31 12:29:38 +02004037 /* free the fake parsed node and correct pointers back */
4038 cs_p->child = NULL;
4039 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
Radek Krejci8f5fad22020-09-15 16:50:54 +02004040 child_p->next = child_p_next;
Michal Vasko20424b42020-08-31 12:29:38 +02004041 }
4042
4043 return ret;
4044}
4045
4046/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004047 * @brief Compile parsed choice node information.
Michal Vasko20424b42020-08-31 12:29:38 +02004048 *
Radek Krejci056d0a82018-12-06 16:57:25 +01004049 * @param[in] ctx Compile context
4050 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004051 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004052 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004053 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4054 */
4055static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004056lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004057{
Michal Vasko22df3f02020-08-24 13:29:22 +02004058 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p;
4059 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
Michal Vasko20424b42020-08-31 12:29:38 +02004060 struct lysp_node *child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004061 LY_ERR ret = LY_SUCCESS;
4062
Michal Vasko20424b42020-08-31 12:29:38 +02004063 assert(node->nodetype == LYS_CHOICE);
4064
Radek Krejci056d0a82018-12-06 16:57:25 +01004065 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko20424b42020-08-31 12:29:38 +02004066 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node));
Radek Krejci056d0a82018-12-06 16:57:25 +01004067 }
4068
4069 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004070 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004071 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004072 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004073
Radek Krejci9800fb82018-12-13 14:26:23 +01004074 return ret;
4075}
4076
4077/**
4078 * @brief Compile parsed anydata or anyxml node information.
4079 * @param[in] ctx Compile context
4080 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004081 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4082 * is enriched with the any-specific information.
4083 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4084 */
4085static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004086lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004087{
Michal Vasko22df3f02020-08-24 13:29:22 +02004088 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p;
4089 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004090 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9800fb82018-12-13 14:26:23 +01004091 LY_ERR ret = LY_SUCCESS;
4092
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004093 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004094 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4095 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004096 ret = ly_set_add(&ctx->xpath, any, 0, NULL);
4097 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004098 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004099
4100 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004101 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4102 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004103 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004104done:
4105 return ret;
4106}
4107
Radek Krejcib56c7502019-02-13 14:19:54 +01004108/**
Michal Vasko795b3752020-07-13 15:24:27 +02004109 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4110 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004111 *
4112 * @param[in] ctx Compile context
4113 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4114 * the choice itself is expected instead of a specific case node.
4115 * @param[in] node Schema node to connect into the list.
4116 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004117 * 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 +01004118 */
4119static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004120lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004121{
Michal Vasko795b3752020-07-13 15:24:27 +02004122 struct lysc_node **children, *start, *end;
4123 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004124
Michal Vasko20424b42020-08-31 12:29:38 +02004125 node->parent = parent;
4126
4127 if (parent) {
4128 if (parent->nodetype == LYS_CHOICE) {
4129 assert(node->nodetype == LYS_CASE);
4130 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
4131 } else {
4132 children = lysc_node_children_p(parent, ctx->options);
4133 }
4134 assert(children);
4135
Radek Krejci056d0a82018-12-06 16:57:25 +01004136 if (!(*children)) {
4137 /* first child */
4138 *children = node;
4139 } else if (*children != node) {
4140 /* by the condition in previous branch we cover the choice/case children
4141 * - the children list is shared by the choice and the the first case, in addition
4142 * the first child of each case must be referenced from the case node. So the node is
4143 * actually always already inserted in case it is the first children - so here such
4144 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004145 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4146 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4147 /* some augments are already connected and we are connecting new ones,
4148 * keep module name order and insert the node into the children list */
4149 end = (*children);
4150 do {
4151 end = end->prev;
4152 mod = end->module;
4153 while (end->prev->module == mod) {
4154 end = end->prev;
4155 }
Radek Krejcif6a11002020-08-21 13:29:07 +02004156 } 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 +02004157
4158 /* we have the last existing node after our node, easily get the first before and connect it */
4159 start = end->prev;
4160 start->next = node;
4161 node->next = end;
4162 end->prev = node;
4163 node->prev = start;
4164 } else {
4165 /* insert at the end of the parent's children list */
4166 (*children)->prev->next = node;
4167 node->prev = (*children)->prev;
4168 (*children)->prev = node;
4169 }
Michal Vasko20424b42020-08-31 12:29:38 +02004170 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004171
Michal Vasko20424b42020-08-31 12:29:38 +02004172 /* check the name uniqueness (even for an only child, it may be in case) */
4173 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
4174 return LY_EEXIST;
4175 }
4176 } else {
4177 /* top-level element */
4178 if (!ctx->mod->compiled->data) {
4179 ctx->mod->compiled->data = node;
4180 } else {
4181 /* insert at the end of the module's top-level nodes list */
4182 ctx->mod->compiled->data->prev->next = node;
4183 node->prev = ctx->mod->compiled->data->prev;
4184 ctx->mod->compiled->data->prev = node;
4185 }
4186
4187 /* check the name uniqueness on top-level */
4188 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
4189 return LY_EEXIST;
Radek Krejci056d0a82018-12-06 16:57:25 +01004190 }
4191 }
Michal Vasko20424b42020-08-31 12:29:38 +02004192
Radek Krejci056d0a82018-12-06 16:57:25 +01004193 return LY_SUCCESS;
4194}
4195
Radek Krejci95710c92019-02-11 15:49:55 +01004196/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004197 * @brief Prepare the case structure in choice node for the new data node.
4198 *
4199 * 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
4200 * created in the choice when the first child was processed.
4201 *
4202 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004203 * @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 +02004204 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004205 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4206 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4207 * @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,
4208 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004209 */
Michal Vasko20424b42020-08-31 12:29:38 +02004210static LY_ERR
4211lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004212{
Michal Vasko20424b42020-08-31 12:29:38 +02004213 struct lysp_node *child_p;
4214 struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004215
Radek Krejci39424802020-08-12 09:31:00 +02004216 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004217 /* we have to add an implicit case node into the parent choice */
Radek Krejci95710c92019-02-11 15:49:55 +01004218 } else if (node_p->nodetype == LYS_CASE) {
Michal Vasko20424b42020-08-31 12:29:38 +02004219 /* explicit parent case */
4220 LY_LIST_FOR(cs_p->child, child_p) {
4221 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004222 }
Radek Krejci95710c92019-02-11 15:49:55 +01004223 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004224 LOGINT_RET(ctx->ctx);
Radek Krejci056d0a82018-12-06 16:57:25 +01004225 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004226
Michal Vasko20424b42020-08-31 12:29:38 +02004227 return LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01004228}
4229
Radek Krejcib56c7502019-02-13 14:19:54 +01004230/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004231 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004232 *
4233 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004234 * @param[in] node Target node where the config is supposed to be changed.
4235 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004236 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4237 * 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 +01004238 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4239 * @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 +01004240 * @return LY_ERR value.
4241 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004242static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004243lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci857189e2020-09-01 13:26:36 +02004244 ly_bool inheriting, ly_bool refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004245{
4246 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004247 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004248
4249 if (config == (node->flags & LYS_CONFIG_MASK)) {
4250 /* nothing to do */
4251 return LY_SUCCESS;
4252 }
4253
4254 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004255 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004256 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004258 "Invalid %s of config - configuration node cannot be child of any state data node.",
4259 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004260 return LY_EVALID;
4261 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004262 node->flags |= LYS_SET_CONFIG;
4263 } else {
4264 if (node->flags & LYS_SET_CONFIG) {
4265 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4266 /* setting config flags, but have node with explicit config true */
4267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004268 "Invalid %s of config - configuration node cannot be child of any state data node.",
4269 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004270 return LY_EVALID;
4271 }
4272 /* do not change config on nodes where the config is explicitely set, this does not apply to
4273 * nodes, which are being changed explicitly (targets of refine or deviation) */
4274 return LY_SUCCESS;
4275 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004276 }
4277 node->flags &= ~LYS_CONFIG_MASK;
4278 node->flags |= config;
4279
4280 /* inherit the change into the children */
Michal Vasko22df3f02020-08-24 13:29:22 +02004281 LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004282 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004283 }
4284
Radek Krejci76b3e962018-12-14 17:01:25 +01004285 return LY_SUCCESS;
4286}
4287
Radek Krejcib56c7502019-02-13 14:19:54 +01004288/**
4289 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4290 *
4291 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4292 * the flag to such parents from a mandatory children.
4293 *
4294 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4295 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4296 * (mandatory children was removed).
4297 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02004298static void
Radek Krejci857189e2020-09-01 13:26:36 +02004299lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
Radek Krejcife909632019-02-12 15:34:42 +01004300{
4301 struct lysc_node *iter;
4302
4303 if (add) { /* set flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004304 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
Radek Krejcife909632019-02-12 15:34:42 +01004305 parent = parent->parent) {
4306 parent->flags |= LYS_MAND_TRUE;
4307 }
4308 } else { /* unset flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004309 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004310 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004311 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004312 /* there is another mandatory node */
4313 return;
4314 }
4315 }
4316 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4317 parent->flags &= ~LYS_MAND_TRUE;
4318 }
4319 }
4320}
4321
Radek Krejci056d0a82018-12-06 16:57:25 +01004322/**
Radek Krejci3641f562019-02-13 15:38:40 +01004323 * @brief Internal sorting process for the lys_compile_augment_sort().
4324 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4325 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4326 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4327 */
4328static void
4329lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4330{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004331 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004332 size_t len;
4333
4334 len = strlen(aug_p->nodeid);
4335 LY_ARRAY_FOR(result, v) {
4336 if (strlen(result[v]->nodeid) <= len) {
4337 continue;
4338 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004339 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004340 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004341 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004342 break;
4343 }
4344 }
4345 result[v] = aug_p;
4346 LY_ARRAY_INCREMENT(result);
4347}
4348
4349/**
4350 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4351 *
4352 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4353 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4354 *
4355 * @param[in] ctx Compile context.
4356 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4357 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4358 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4359 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4360 * @return LY_ERR value.
4361 */
4362LY_ERR
4363lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4364{
4365 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004366 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004367
4368 assert(augments);
4369
4370 /* get count of the augments in module and all its submodules */
4371 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004372 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004373 }
4374 LY_ARRAY_FOR(inc_p, u) {
4375 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004376 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004377 }
4378 }
4379
4380 if (!count) {
4381 *augments = NULL;
4382 return LY_SUCCESS;
4383 }
4384 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4385
4386 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4387 * together, so there can be even /z/y betwwen them. */
4388 LY_ARRAY_FOR(aug_p, u) {
4389 lys_compile_augment_sort_(&aug_p[u], result);
4390 }
4391 LY_ARRAY_FOR(inc_p, u) {
4392 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4393 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4394 }
4395 }
4396
4397 *augments = result;
4398 return LY_SUCCESS;
4399}
4400
4401/**
4402 * @brief Compile the parsed augment connecting it into its target.
4403 *
4404 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4405 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4406 * are already implemented and compiled.
4407 *
4408 * @param[in] ctx Compile context.
4409 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004410 * @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
4411 * children in case of the augmenting uses data.
4412 * @return LY_SUCCESS on success.
4413 * @return LY_EVALID on failure.
4414 */
4415LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004416lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004417{
Michal Vaskoe6143202020-07-03 13:02:08 +02004418 LY_ERR ret = LY_SUCCESS, rc;
Michal Vasko20424b42020-08-31 12:29:38 +02004419 struct lysp_node *node_p;
Radek Krejci3641f562019-02-13 15:38:40 +01004420 struct lysc_node *target; /* target target of the augment */
4421 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004422 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004423 struct lys_module **aug_mod;
Radek Krejci857189e2020-09-01 13:26:36 +02004424 ly_bool allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004425 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004426 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004427 uint32_t opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004428
Radek Krejci327de162019-06-14 12:52:07 +02004429 lysc_update_path(ctx, NULL, "{augment}");
4430 lysc_update_path(ctx, NULL, aug_p->nodeid);
4431
Michal Vaskocb18c7f2020-08-24 09:22:28 +02004432 ret = lysc_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004433 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Michal Vasko22df3f02020-08-24 13:29:22 +02004434 1, (const struct lysc_node **)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004435 if (ret != LY_SUCCESS) {
4436 if (ret == LY_EDENIED) {
4437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4438 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4439 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4440 }
4441 return LY_EVALID;
4442 }
4443
4444 /* check for mandatory nodes
4445 * - new cases augmenting some choice can have mandatory nodes
4446 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4447 */
Radek Krejci733988a2019-02-15 15:12:44 +01004448 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004449 allow_mandatory = 1;
4450 }
4451
4452 when_shared = NULL;
4453 LY_LIST_FOR(aug_p->child, node_p) {
4454 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4455 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004456 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004457 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4458 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004460 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4461 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004462 return LY_EVALID;
4463 }
4464
4465 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004466 ctx->options |= flags;
Michal Vasko20424b42020-08-31 12:29:38 +02004467 if (target->nodetype == LYS_CHOICE) {
4468 LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target));
Radek Krejci3641f562019-02-13 15:38:40 +01004469 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004470 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004471 }
Radek Krejciec4da802019-05-02 13:02:41 +02004472 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004473
Radek Krejcife13da42019-02-15 14:51:01 +01004474 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4475 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004476 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004477 /* 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 +02004478 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 +01004479 } else if (target->nodetype == LYS_CHOICE) {
4480 /* 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 +02004481 node = ((struct lysc_node_choice *)target)->cases->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004482 } else {
4483 /* the compiled node is the last child of the target */
Michal Vasko22df3f02020-08-24 13:29:22 +02004484 node = (struct lysc_node *)lysc_node_children(target, flags);
Radek Krejci7c7783d2020-04-08 15:34:39 +02004485 if (!node) {
4486 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4487 break;
4488 }
4489 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004490 }
4491
Radek Krejci733988a2019-02-15 15:12:44 +01004492 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004493 node->flags &= ~LYS_MAND_TRUE;
4494 lys_compile_mandatory_parents(target, 0);
4495 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004496 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004497 return LY_EVALID;
4498 }
4499
4500 /* pass augment's when to all the children */
4501 if (aug_p->when) {
4502 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4503 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004504 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004505 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004506
4507 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4508 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004509 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4510 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004511 }
4512
Radek Krejci3641f562019-02-13 15:38:40 +01004513 when_shared = *when;
4514 } else {
4515 ++when_shared->refcount;
4516 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004517
4518 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4519 /* in this case check "when" again for all children because of dummy node check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004520 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4521 LY_CHECK_GOTO(ret, error);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004522 }
Radek Krejci3641f562019-02-13 15:38:40 +01004523 }
4524 }
4525 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004526
Radek Krejciec4da802019-05-02 13:02:41 +02004527 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004528 switch (target->nodetype) {
4529 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004530 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004531 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004532 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004533 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004534 break;
4535 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004536 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004537 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004538 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004539 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004540 break;
4541 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004542 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004543 if (aug_p->actions) {
4544 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004545 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4546 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004547 return LY_EVALID;
4548 }
4549 if (aug_p->notifs) {
4550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004551 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004552 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004553 return LY_EVALID;
4554 }
4555 }
Radek Krejci3641f562019-02-13 15:38:40 +01004556
Michal Vaskoe6143202020-07-03 13:02:08 +02004557 /* add this module into the target module augmented_by, if not there already */
4558 rc = LY_SUCCESS;
4559 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4560 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4561 rc = LY_EEXIST;
4562 break;
4563 }
4564 }
4565 if (!rc) {
4566 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4567 *aug_mod = ctx->mod;
4568 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004569
Radek Krejci327de162019-06-14 12:52:07 +02004570 lysc_update_path(ctx, NULL, NULL);
4571 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004572
Radek Krejci3641f562019-02-13 15:38:40 +01004573error:
Radek Krejciec4da802019-05-02 13:02:41 +02004574 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004575 return ret;
4576}
4577
4578/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004579 * @brief Apply refined or deviated mandatory flag to the target node.
4580 *
4581 * @param[in] ctx Compile context.
4582 * @param[in] node Target node where the mandatory property is supposed to be changed.
4583 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004584 * @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 +01004585 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4586 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4587 * 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 +01004588 * @return LY_ERR value.
4589 */
4590static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004591lys_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 +01004592{
4593 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004595 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4596 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004597 return LY_EVALID;
4598 }
4599
4600 if (mandatory_flag & LYS_MAND_TRUE) {
4601 /* check if node has default value */
4602 if (node->nodetype & LYS_LEAF) {
4603 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004604 if (refine_flag) {
4605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004606 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004607 return LY_EVALID;
4608 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004609 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004610 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004611 if (refine_flag) {
4612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004613 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004614 return LY_EVALID;
4615 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004616 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004617 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004618 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 +01004619 return LY_EVALID;
4620 }
4621
4622 node->flags &= ~LYS_MAND_FALSE;
4623 node->flags |= LYS_MAND_TRUE;
4624 lys_compile_mandatory_parents(node->parent, 1);
4625 } else {
4626 /* make mandatory false */
4627 node->flags &= ~LYS_MAND_TRUE;
4628 node->flags |= LYS_MAND_FALSE;
4629 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcif1421c22019-02-19 13:05:20 +01004630 }
4631 return LY_SUCCESS;
4632}
4633
4634/**
Michal Vasko601ddb32020-08-31 16:25:35 +02004635 * @brief Find grouping for a uses.
Radek Krejcie86bf772018-12-14 11:39:53 +01004636 *
Michal Vasko601ddb32020-08-31 16:25:35 +02004637 * @param[in] ctx Compile context.
4638 * @param[in] uses_p Parsed uses node.
4639 * @param[out] gpr_p Found grouping on success.
4640 * @param[out] grp_mod Module of @p grp_p on success.
4641 * @return LY_ERR value.
Radek Krejcie86bf772018-12-14 11:39:53 +01004642 */
4643static LY_ERR
Michal Vasko601ddb32020-08-31 16:25:35 +02004644lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
4645 struct lys_module **grp_mod)
Radek Krejcie86bf772018-12-14 11:39:53 +01004646{
4647 struct lysp_node *node_p;
Michal Vasko601ddb32020-08-31 16:25:35 +02004648 struct lysp_grp *grp;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004649 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +02004650 ly_bool found = 0;
Radek Krejcie86bf772018-12-14 11:39:53 +01004651 const char *id, *name, *prefix;
4652 size_t prefix_len, name_len;
Michal Vasko601ddb32020-08-31 16:25:35 +02004653 struct lys_module *mod;
4654
4655 *grp_p = NULL;
4656 *grp_mod = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004657
4658 /* search for the grouping definition */
Radek Krejcie86bf772018-12-14 11:39:53 +01004659 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004660 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004661 if (prefix) {
4662 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4663 if (!mod) {
4664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004665 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004666 return LY_EVALID;
4667 }
4668 } else {
4669 mod = ctx->mod_def;
4670 }
4671 if (mod == ctx->mod_def) {
4672 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004673 grp = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004674 LY_ARRAY_FOR(grp, u) {
4675 if (!strcmp(grp[u].name, name)) {
4676 grp = &grp[u];
4677 found = 1;
4678 break;
4679 }
4680 }
4681 }
4682 }
4683 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004684 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004685 grp = mod->parsed->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004686 LY_ARRAY_FOR(grp, u) {
4687 if (!strcmp(grp[u].name, name)) {
4688 grp = &grp[u];
4689 found = 1;
4690 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004691 }
4692 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004693 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004694 /* ... and all the submodules */
Michal Vasko601ddb32020-08-31 16:25:35 +02004695 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004696 grp = mod->parsed->includes[u].submodule->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004697 LY_ARRAY_FOR(grp, v) {
4698 if (!strcmp(grp[v].name, name)) {
4699 grp = &grp[v];
4700 found = 1;
4701 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004702 }
4703 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004704 if (found) {
4705 break;
4706 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004707 }
4708 }
4709 }
4710 if (!found) {
4711 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4712 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4713 return LY_EVALID;
4714 }
4715
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004716 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4717 /* remember that the grouping is instantiated to avoid its standalone validation */
4718 grp->flags |= LYS_USED_GRP;
4719 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004720
Michal Vasko601ddb32020-08-31 16:25:35 +02004721 *grp_p = grp;
4722 *grp_mod = mod;
4723 return LY_SUCCESS;
4724}
Radek Krejcie86bf772018-12-14 11:39:53 +01004725
Michal Vasko601ddb32020-08-31 16:25:35 +02004726static LY_ERR
4727lys_compile_refines(struct lysc_ctx *ctx, struct lysp_refine *refines, const struct lysc_node *context_node)
4728{
4729 struct lysc_node *node;
4730 LY_ARRAY_COUNT_TYPE u;
4731 struct lysp_refine *rfn;
4732 LY_ERR ret = LY_SUCCESS;
4733 uint32_t min, max;
4734 uint16_t flags;
4735 struct ly_set refined = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01004736
Radek Krejci327de162019-06-14 12:52:07 +02004737 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004738
Radek Krejci76b3e962018-12-14 17:01:25 +01004739 /* apply refine */
Michal Vasko601ddb32020-08-31 16:25:35 +02004740 LY_ARRAY_FOR(refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004741 lysc_update_path(ctx, NULL, rfn->nodeid);
4742
Michal Vasko601ddb32020-08-31 16:25:35 +02004743 ret = lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, context_node, ctx->mod,
Michal Vasko20424b42020-08-31 12:29:38 +02004744 0, 0, (const struct lysc_node **)&node, &flags);
4745 LY_CHECK_GOTO(ret, cleanup);
4746 ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL);
4747 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004748
4749 /* default value */
4750 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004751 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci0f969882020-08-21 16:56:47 +02004753 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE " default values.",
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004754 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Michal Vasko20424b42020-08-31 12:29:38 +02004755 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004756 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004757 }
4758 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004760 "Invalid refine of default - %s cannot hold default value(s).",
4761 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004762 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004763 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004764 }
4765 if (node->nodetype == LYS_LEAF) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004766 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004767 ret = lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], ctx->mod_def);
4768 LY_CHECK_GOTO(ret, cleanup);
4769
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004770 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004771 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004772 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4774 "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 +02004775 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004776 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004777 }
Radek Krejcia1911222019-07-22 17:24:50 +02004778
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004779 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004780 ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def);
4781 LY_CHECK_GOTO(ret, cleanup);
4782
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004783 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004784 } else if (node->nodetype == LYS_CHOICE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004785 if (((struct lysc_node_choice *)node)->dflt) {
Radek Krejci01342af2019-01-03 15:18:08 +01004786 /* unset LYS_SET_DFLT from the current default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02004787 ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT;
Radek Krejci01342af2019-01-03 15:18:08 +01004788 }
Michal Vasko20424b42020-08-31 12:29:38 +02004789 ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node);
4790 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004791 }
4792 }
4793
Radek Krejci12fb9142019-01-08 09:45:30 +01004794 /* description */
4795 if (rfn->dsc) {
4796 FREE_STRING(ctx->ctx, node->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004797 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->dsc, 0, &node->dsc), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004798 }
4799
4800 /* reference */
4801 if (rfn->ref) {
4802 FREE_STRING(ctx->ctx, node->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004803 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->ref, 0, &node->ref), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004804 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004805
4806 /* config */
4807 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004808 if (!flags) {
Michal Vasko20424b42020-08-31 12:29:38 +02004809 ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1);
4810 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004811 } else {
4812 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004813 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004814 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004815 }
4816
4817 /* mandatory */
4818 if (rfn->flags & LYS_MAND_MASK) {
Michal Vasko20424b42020-08-31 12:29:38 +02004819 ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1);
4820 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004821 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004822
4823 /* presence */
4824 if (rfn->presence) {
4825 if (node->nodetype != LYS_CONTAINER) {
4826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004827 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4828 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004829 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004830 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004831 }
4832 node->flags |= LYS_PRESENCE;
4833 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004834
4835 /* must */
4836 if (rfn->musts) {
4837 switch (node->nodetype) {
4838 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02004839 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 +01004840 break;
4841 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004842 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 +01004843 break;
4844 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004845 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 +01004846 break;
4847 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004848 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 +01004849 break;
4850 case LYS_ANYXML:
4851 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02004852 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 +01004853 break;
4854 default:
4855 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004856 "Invalid refine of must statement - %s cannot hold any must statement.",
4857 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004858 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004859 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004860 }
Michal Vasko20424b42020-08-31 12:29:38 +02004861 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4862 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004863 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004864
4865 /* min/max-elements */
4866 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4867 switch (node->nodetype) {
4868 case LYS_LEAFLIST:
4869 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004870 ((struct lysc_node_leaflist *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004871 }
4872 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004873 ((struct lysc_node_leaflist *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004874 if (rfn->min) {
4875 node->flags |= LYS_MAND_TRUE;
4876 lys_compile_mandatory_parents(node->parent, 1);
4877 } else {
4878 node->flags &= ~LYS_MAND_TRUE;
4879 lys_compile_mandatory_parents(node->parent, 0);
4880 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004881 }
4882 break;
4883 case LYS_LIST:
4884 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004885 ((struct lysc_node_list *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004886 }
4887 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004888 ((struct lysc_node_list *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004889 if (rfn->min) {
4890 node->flags |= LYS_MAND_TRUE;
4891 lys_compile_mandatory_parents(node->parent, 1);
4892 } else {
4893 node->flags &= ~LYS_MAND_TRUE;
4894 lys_compile_mandatory_parents(node->parent, 0);
4895 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004896 }
4897 break;
4898 default:
4899 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004900 "Invalid refine of %s statement - %s cannot hold this statement.",
Radek Krejci0f969882020-08-21 16:56:47 +02004901 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004902 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004903 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004904 }
4905 }
Radek Krejcif0089082019-01-07 16:42:01 +01004906
4907 /* if-feature */
4908 if (rfn->iffeatures) {
4909 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004910 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004911 }
Radek Krejci327de162019-06-14 12:52:07 +02004912
4913 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004914 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004915
Radek Krejcif2271f12019-01-07 16:42:23 +01004916 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004917 for (uint32_t i = 0; i < refined.count; ++i) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004918 node = (struct lysc_node *)refined.objs[i];
Michal Vasko601ddb32020-08-31 16:25:35 +02004919 rfn = &refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02004920 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004921
4922 /* check possible conflict with default value (default added, mandatory left true) */
4923 if ((node->flags & LYS_MAND_TRUE) &&
Michal Vasko22df3f02020-08-24 13:29:22 +02004924 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) ||
Radek Krejcif2271f12019-01-07 16:42:23 +01004925 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004927 "Invalid refine of default - the node is mandatory.");
Michal Vasko20424b42020-08-31 12:29:38 +02004928 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004929 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004930 }
4931
4932 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4933 if (node->nodetype == LYS_LIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004934 min = ((struct lysc_node_list *)node)->min;
4935 max = ((struct lysc_node_list *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004936 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02004937 min = ((struct lysc_node_leaflist *)node)->min;
4938 max = ((struct lysc_node_leaflist *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004939 }
4940 if (min > max) {
4941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004942 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
Radek Krejci0f969882020-08-21 16:56:47 +02004943 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Michal Vasko20424b42020-08-31 12:29:38 +02004944 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004945 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004946 }
4947 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004948
4949 lysc_update_path(ctx, NULL, NULL);
Radek Krejcif2271f12019-01-07 16:42:23 +01004950 }
4951
Michal Vasko601ddb32020-08-31 16:25:35 +02004952cleanup:
4953 ly_set_erase(&refined, NULL);
4954 lysc_update_path(ctx, NULL, NULL);
4955 return ret;
4956}
4957
4958/**
4959 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4960 * If present, also apply uses's modificators.
4961 *
4962 * @param[in] ctx Compile context
4963 * @param[in] uses_p Parsed uses schema node.
4964 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4965 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4966 * the compile context.
4967 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4968 */
4969static LY_ERR
4970lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p)
4971{
4972 struct lysp_node *node_p;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004973 struct lysc_node *child = NULL, *iter;
Michal Vasko601ddb32020-08-31 16:25:35 +02004974 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
4975 struct lysc_node_container context_node_fake =
4976 {.nodetype = LYS_CONTAINER,
4977 .module = ctx->mod,
4978 .flags = parent ? parent->flags : 0,
4979 .child = NULL, .next = NULL,
4980 .prev = (struct lysc_node *)&context_node_fake,
4981 .actions = NULL, .notifs = NULL};
4982 struct lysp_grp *grp = NULL;
4983 LY_ARRAY_COUNT_TYPE u;
4984 uint32_t grp_stack_count;
4985 struct lys_module *grp_mod, *mod_old;
4986 LY_ERR ret = LY_SUCCESS;
4987 struct lysc_when **when, *when_shared;
4988 struct lysp_augment **augments = NULL;
4989 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
4990 struct lysc_notif **notifs = NULL;
4991 struct lysc_action **actions = NULL;
4992
4993 /* find the referenced grouping */
4994 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
4995
4996 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4997 grp_stack_count = ctx->groupings.count;
4998 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
4999 if (grp_stack_count == ctx->groupings.count) {
5000 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
5001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5002 "Grouping \"%s\" references itself through a uses statement.", grp->name);
5003 return LY_EVALID;
5004 }
5005
5006 /* switch context's mod_def */
5007 mod_old = ctx->mod_def;
5008 ctx->mod_def = grp_mod;
5009
5010 /* check status */
5011 ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, grp_mod, grp->name);
5012 LY_CHECK_GOTO(ret, cleanup);
5013
5014 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
5015 * applu refine and augment only to the nodes from the uses */
5016 if (parent) {
5017 child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5018 } else if (ctx->mod->compiled->data) {
5019 child = ctx->mod->compiled->data;
5020 } else {
5021 child = NULL;
5022 }
5023 /* remember the last child */
5024 if (child) {
5025 child = child->prev;
5026 }
5027
5028 /* compile data nodes */
5029 LY_LIST_FOR(grp->data, node_p) {
5030 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
5031 ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3);
5032 LY_CHECK_GOTO(ret, cleanup);
5033 }
5034
5035 /* split the children and add the uses's data into the fake context node */
5036 if (child) {
5037 context_node_fake.child = child->next;
5038 } else if (parent) {
5039 context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5040 } else if (ctx->mod->compiled->data) {
5041 context_node_fake.child = ctx->mod->compiled->data;
5042 }
5043 if (context_node_fake.child) {
5044 /* remember child as the last data node added by grouping to fix the list later */
5045 child = context_node_fake.child->prev;
5046 context_node_fake.child->prev = NULL;
5047 }
5048
5049 when_shared = NULL;
5050 LY_LIST_FOR(context_node_fake.child, iter) {
5051 iter->parent = (struct lysc_node *)&context_node_fake;
5052
5053 /* pass uses's when to all the data children, actions and notifications are ignored */
5054 if (uses_p->when) {
5055 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
5056 if (!when_shared) {
5057 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when);
5058 LY_CHECK_GOTO(ret, cleanup);
5059
5060 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5061 /* do not check "when" semantics in a grouping */
5062 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5063 LY_CHECK_GOTO(ret, cleanup);
5064 }
5065
5066 when_shared = *when;
5067 } else {
5068 ++when_shared->refcount;
5069 (*when) = when_shared;
5070
5071 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5072 /* in this case check "when" again for all children because of dummy node check */
5073 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5074 LY_CHECK_GOTO(ret, cleanup);
5075 }
5076 }
5077 }
5078 }
5079
5080 /* compile actions */
5081 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5082 if (actions) {
5083 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
5084 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
5085 if (*actions && (uses_p->augments || uses_p->refines)) {
5086 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5087 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
5088 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
5089 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
5090 }
5091 }
5092
5093 /* compile notifications */
5094 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5095 if (notifs) {
5096 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
5097 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
5098 if (*notifs && (uses_p->augments || uses_p->refines)) {
5099 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5100 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
5101 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
5102 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
5103 }
5104 }
5105
5106 /* sort and apply augments */
5107 ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments);
5108 LY_CHECK_GOTO(ret, cleanup);
5109 LY_ARRAY_FOR(augments, u) {
5110 ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake);
5111 LY_CHECK_GOTO(ret, cleanup);
5112 }
5113
5114 /* reload previous context's mod_def */
5115 ctx->mod_def = mod_old;
5116
5117 /* apply all refines */
5118 ret = lys_compile_refines(ctx, uses_p->refines, (struct lysc_node *)&context_node_fake);
5119 LY_CHECK_GOTO(ret, cleanup);
5120
Radek Krejcic6b4f442020-08-12 14:45:18 +02005121 if (first_p) {
5122 *first_p = context_node_fake.child;
5123 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005124
5125cleanup:
5126 /* fix connection of the children nodes from fake context node back into the parent */
5127 if (context_node_fake.child) {
5128 context_node_fake.child->prev = child;
5129 }
5130 LY_LIST_FOR(context_node_fake.child, child) {
5131 child->parent = parent;
5132 }
5133
5134 if (uses_p->augments || uses_p->refines) {
5135 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005136 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005137 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005138 LY_ARRAY_FREE(context_node_fake.actions);
5139 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005140 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005141 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005142 LY_ARRAY_FREE(context_node_fake.notifs);
5143 }
5144 }
5145
Radek Krejcie86bf772018-12-14 11:39:53 +01005146 /* reload previous context's mod_def */
5147 ctx->mod_def = mod_old;
5148 /* remove the grouping from the stack for circular groupings dependency check */
5149 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5150 assert(ctx->groupings.count == grp_stack_count);
Radek Krejci3641f562019-02-13 15:38:40 +01005151 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005152
5153 return ret;
5154}
5155
Radek Krejci327de162019-06-14 12:52:07 +02005156static int
5157lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5158{
5159 struct lysp_node *iter;
5160 int len = 0;
5161
5162 *path = NULL;
5163 for (iter = node; iter && len >= 0; iter = iter->parent) {
5164 char *s = *path;
5165 char *id;
5166
5167 switch (iter->nodetype) {
5168 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005169 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005170 break;
5171 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005172 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005173 break;
5174 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005175 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005176 break;
5177 default:
5178 id = strdup(iter->name);
5179 break;
5180 }
5181
5182 if (!iter->parent) {
5183 /* print prefix */
5184 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5185 } else {
5186 /* prefix is the same as in parent */
5187 len = asprintf(path, "/%s%s", id, s ? s : "");
5188 }
5189 free(s);
5190 free(id);
5191 }
5192
5193 if (len < 0) {
5194 free(*path);
5195 *path = NULL;
5196 } else if (len == 0) {
5197 *path = strdup("/");
5198 len = 1;
5199 }
5200 return len;
5201}
5202
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005203/**
5204 * @brief Validate groupings that were defined but not directly used in the schema itself.
5205 *
5206 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5207 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5208 */
5209static LY_ERR
5210lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5211{
5212 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005213 char *path;
5214 int len;
5215
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005216 struct lysp_node_uses fake_uses = {
5217 .parent = node_p,
5218 .nodetype = LYS_USES,
5219 .flags = 0, .next = NULL,
5220 .name = grp->name,
5221 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5222 .refines = NULL, .augments = NULL
5223 };
5224 struct lysc_node_container fake_container = {
5225 .nodetype = LYS_CONTAINER,
5226 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5227 .module = ctx->mod,
5228 .sp = NULL, .parent = NULL, .next = NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +02005229 .prev = (struct lysc_node *)&fake_container,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005230 .name = "fake",
5231 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5232 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5233 };
5234
5235 if (grp->parent) {
5236 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5237 }
Radek Krejci327de162019-06-14 12:52:07 +02005238
5239 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5240 if (len < 0) {
5241 LOGMEM(ctx->ctx);
5242 return LY_EMEM;
5243 }
5244 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005245 ctx->path_len = (uint32_t)len;
Radek Krejci327de162019-06-14 12:52:07 +02005246 free(path);
5247
5248 lysc_update_path(ctx, NULL, "{grouping}");
5249 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02005250 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005251 lysc_update_path(ctx, NULL, NULL);
5252 lysc_update_path(ctx, NULL, NULL);
5253
5254 ctx->path_len = 1;
5255 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005256
5257 /* cleanup */
5258 lysc_node_container_free(ctx->ctx, &fake_container);
5259
5260 return ret;
5261}
Radek Krejcife909632019-02-12 15:34:42 +01005262
Radek Krejcie86bf772018-12-14 11:39:53 +01005263/**
Michal Vasko20424b42020-08-31 12:29:38 +02005264 * @brief Set config flags for a node.
5265 *
5266 * @param[in] ctx Compile context.
5267 * @param[in] node Compiled node config to set.
5268 * @param[in] parent Parent of @p node.
5269 * @return LY_ERR value.
5270 */
5271static LY_ERR
5272lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
5273{
5274 if (node->nodetype == LYS_CASE) {
5275 /* case never has any config */
5276 assert(!(node->flags & LYS_CONFIG_MASK));
5277 return LY_SUCCESS;
5278 }
5279
5280 /* adjust parent to always get the ancestor with config */
5281 if (parent && (parent->nodetype == LYS_CASE)) {
5282 parent = parent->parent;
5283 assert(parent);
5284 }
5285
5286 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
5287 /* ignore config statements inside RPC/action data */
5288 node->flags &= ~LYS_CONFIG_MASK;
5289 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5290 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
5291 /* ignore config statements inside Notification data */
5292 node->flags &= ~LYS_CONFIG_MASK;
5293 node->flags |= LYS_CONFIG_R;
5294 } else if (!(node->flags & LYS_CONFIG_MASK)) {
5295 /* config not explicitely set, inherit it from parent */
5296 if (parent) {
5297 node->flags |= parent->flags & LYS_CONFIG_MASK;
5298 } else {
5299 /* default is config true */
5300 node->flags |= LYS_CONFIG_W;
5301 }
5302 } else {
5303 /* config set explicitely */
5304 node->flags |= LYS_SET_CONFIG;
5305 }
5306
5307 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5308 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5309 "Configuration node cannot be child of any state data node.");
5310 return LY_EVALID;
5311 }
5312
5313 return LY_SUCCESS;
5314}
5315
5316/**
Radek Krejcia3045382018-11-22 14:30:31 +01005317 * @brief Compile parsed schema node information.
5318 * @param[in] ctx Compile context
5319 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005320 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5321 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5322 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005323 * @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).
5324 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005325 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5326 */
Radek Krejci19a96102018-11-15 13:38:09 +01005327static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005328lys_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 +01005329{
Radek Krejci1c54f462020-05-12 17:25:34 +02005330 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005331 struct lysc_node *node = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005332 struct lysc_when **when;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005333 LY_ARRAY_COUNT_TYPE u;
Michal Vasko22df3f02020-08-24 13:29:22 +02005334 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
Radek Krejci19a96102018-11-15 13:38:09 +01005335
Radek Krejci327de162019-06-14 12:52:07 +02005336 if (node_p->nodetype != LYS_USES) {
5337 lysc_update_path(ctx, parent, node_p->name);
5338 } else {
5339 lysc_update_path(ctx, NULL, "{uses}");
5340 lysc_update_path(ctx, NULL, node_p->name);
5341 }
5342
Radek Krejci19a96102018-11-15 13:38:09 +01005343 switch (node_p->nodetype) {
5344 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02005345 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
Radek Krejci19a96102018-11-15 13:38:09 +01005346 node_compile_spec = lys_compile_node_container;
5347 break;
5348 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005349 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
Radek Krejci19a96102018-11-15 13:38:09 +01005350 node_compile_spec = lys_compile_node_leaf;
5351 break;
5352 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005353 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005354 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005355 break;
5356 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005357 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005358 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005359 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005360 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02005361 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005362 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005363 break;
Michal Vasko20424b42020-08-31 12:29:38 +02005364 case LYS_CASE:
5365 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
5366 node_compile_spec = lys_compile_node_case;
5367 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005368 case LYS_ANYXML:
5369 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005370 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005371 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005372 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005373 case LYS_USES:
Michal Vasko9e8de2d2020-09-01 08:17:10 +02005374 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, parent, &node);
Radek Krejci327de162019-06-14 12:52:07 +02005375 lysc_update_path(ctx, NULL, NULL);
5376 lysc_update_path(ctx, NULL, NULL);
5377 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005378 default:
5379 LOGINT(ctx->ctx);
5380 return LY_EINT;
5381 }
5382 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5383 node->nodetype = node_p->nodetype;
5384 node->module = ctx->mod;
5385 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005386 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005387
5388 /* config */
Michal Vasko20424b42020-08-31 12:29:38 +02005389 ret = lys_compile_config(ctx, node, parent);
5390 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005391
Michal Vasko20424b42020-08-31 12:29:38 +02005392 /* list ordering */
Radek Krejcia6d57732018-11-29 13:40:37 +01005393 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5394 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005395 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci0f969882020-08-21 16:56:47 +02005396 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5397 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005398 node->flags &= ~LYS_ORDBY_MASK;
5399 node->flags |= LYS_ORDBY_SYSTEM;
5400 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5401 /* default ordering is system */
5402 node->flags |= LYS_ORDBY_SYSTEM;
5403 }
5404 }
5405
Radek Krejci19a96102018-11-15 13:38:09 +01005406 /* status - it is not inherited by specification, but it does not make sense to have
5407 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko20424b42020-08-31 12:29:38 +02005408 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 +01005409
Radek Krejciec4da802019-05-02 13:02:41 +02005410 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005411 node->sp = node_p;
5412 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02005413 DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error);
5414 DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error);
5415 DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error);
Radek Krejci00b874b2019-02-12 10:54:50 +01005416 if (node_p->when) {
5417 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005418 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005419
5420 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5421 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02005422 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
5423 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005424 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005425 }
Radek Krejciec4da802019-05-02 13:02:41 +02005426 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005427
Michal Vasko20424b42020-08-31 12:29:38 +02005428 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
5429 LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node));
5430
Radek Krejci19a96102018-11-15 13:38:09 +01005431 /* nodetype-specific part */
Michal Vasko20424b42020-08-31 12:29:38 +02005432 LY_CHECK_RET(node_compile_spec(ctx, node_p, node));
Radek Krejci19a96102018-11-15 13:38:09 +01005433
Michal Vasko20424b42020-08-31 12:29:38 +02005434 /* final compilation tasks that require the node to be connected */
5435 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, done);
Radek Krejcife909632019-02-12 15:34:42 +01005436 if (node->flags & LYS_MAND_TRUE) {
Michal Vasko20424b42020-08-31 12:29:38 +02005437 /* inherit LYS_MAND_TRUE in parent containers */
Radek Krejcife909632019-02-12 15:34:42 +01005438 lys_compile_mandatory_parents(parent, 1);
5439 }
5440
Radek Krejci327de162019-06-14 12:52:07 +02005441 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005442 return LY_SUCCESS;
5443
5444error:
5445 lysc_node_free(ctx->ctx, node);
Michal Vasko20424b42020-08-31 12:29:38 +02005446done:
Radek Krejci19a96102018-11-15 13:38:09 +01005447 return ret;
5448}
5449
Radek Krejciccd20f12019-02-15 14:12:27 +01005450static void
Michal Vasko20424b42020-08-31 12:29:38 +02005451lysc_node_unlink(struct lysc_node *node)
Radek Krejciccd20f12019-02-15 14:12:27 +01005452{
Michal Vasko20424b42020-08-31 12:29:38 +02005453 struct lysc_node *parent, *child;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005454 struct lysc_module *modc = node->module->compiled;
Radek Krejciccd20f12019-02-15 14:12:27 +01005455
5456 parent = node->parent;
5457
5458 /* parent's first child */
Michal Vasko20424b42020-08-31 12:29:38 +02005459 if (parent && lysc_node_children(parent, node->flags) == node) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005460 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005461 } else if (modc->data == node) {
5462 modc->data = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005463 }
Michal Vasko20424b42020-08-31 12:29:38 +02005464
5465 /* special choice case unlinking */
5466 if (parent && parent->nodetype == LYS_CHOICE) {
5467 if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) {
5468 /* default case removed */
5469 ((struct lysc_node_choice *)parent)->dflt = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005470 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005471 }
5472
5473 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005474 if (node->prev->next) {
5475 node->prev->next = node->next;
5476 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005477 if (node->next) {
5478 node->next->prev = node->prev;
Michal Vasko20424b42020-08-31 12:29:38 +02005479 } else {
5480 /* last child */
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005481 if (parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005482 child = (struct lysc_node *)lysc_node_children(parent, node->flags);
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005483 } else {
5484 child = modc->data;
5485 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005486 if (child) {
5487 child->prev = node->prev;
5488 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005489 }
5490}
5491
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005492struct lysc_deviation {
5493 const char *nodeid;
5494 struct lysc_node *target; /* target node of the deviation */
Michal Vasko22df3f02020-08-24 13:29:22 +02005495 struct lysp_deviate **deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02005496 uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */
Radek Krejci857189e2020-09-01 13:26:36 +02005497 ly_bool not_supported; /* flag if deviates contains not-supported deviate */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005498};
5499
Michal Vaskoccc062a2020-08-13 08:34:50 +02005500/* MACROS for deviates checking */
5501#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5502 if (!(target->nodetype & (NODETYPES))) { \
5503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5504 goto cleanup; \
5505 }
5506
5507#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5508 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5509 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5510 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5511 goto cleanup; \
5512 }
5513
5514#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5515 if (!((TYPE)target)->MEMBER || COND) { \
5516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5517 goto cleanup; \
5518 }
5519
5520/**
5521 * @brief Apply deviate add.
5522 *
5523 * @param[in] ctx Compile context.
5524 * @param[in] target Deviation target.
5525 * @param[in] dev_flags Internal deviation flags.
5526 * @param[in] d Deviate add to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02005527 * @return LY_ERR value.
5528 */
5529static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005530lys_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 +02005531{
Radek Krejci011e4aa2020-09-04 15:22:31 +02005532 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005533 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5534 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005535 LY_ARRAY_COUNT_TYPE x;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005536
5537#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5538 if (((TYPE)target)->MEMBER COND) { \
5539 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5540 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5541 PROPERTY, ((TYPE)target)->MEMBER); \
5542 goto cleanup; \
5543 }
5544
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005545#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005546 if (((TYPE)target)->MEMBER && (COND)) { \
Radek Krejci857189e2020-09-01 13:26:36 +02005547 ly_bool dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005548 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005549 ctx->mod_def, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5551 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5552 if (dynamic_) {free((void*)val_);} \
5553 goto cleanup; \
5554 }
5555
5556#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5557 if (((TYPE)target)->MEMBER && (COND)) { \
5558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5559 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5560 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5561 goto cleanup; \
5562 }
5563
5564 /* [units-stmt] */
5565 if (d->units) {
5566 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5567 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5568
5569 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02005570 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc);
5571 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005572 }
5573
5574 /* *must-stmt */
5575 if (d->musts) {
5576 switch (target->nodetype) {
5577 case LYS_CONTAINER:
5578 case LYS_LIST:
5579 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5580 x, lys_compile_must, ret, cleanup);
5581 break;
5582 case LYS_LEAF:
5583 case LYS_LEAFLIST:
5584 case LYS_ANYDATA:
5585 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5586 x, lys_compile_must, ret, cleanup);
5587 break;
5588 case LYS_NOTIF:
5589 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5590 x, lys_compile_must, ret, cleanup);
5591 break;
5592 case LYS_RPC:
5593 case LYS_ACTION:
5594 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5595 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5596 x, lys_compile_must, ret, cleanup);
5597 break;
5598 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5599 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5600 x, lys_compile_must, ret, cleanup);
5601 break;
5602 }
Radek Krejci0f969882020-08-21 16:56:47 +02005603 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005604 default:
5605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5606 lys_nodetype2str(target->nodetype), "add", "must");
5607 goto cleanup;
5608 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02005609 ret = ly_set_add(&ctx->xpath, target, 0, NULL);
5610 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005611 }
5612
5613 /* *unique-stmt */
5614 if (d->uniques) {
5615 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5616 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5617 }
5618
5619 /* *default-stmt */
5620 if (d->dflts) {
5621 switch (target->nodetype) {
5622 case LYS_LEAF:
5623 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005624 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005625 if (leaf->dflt) {
5626 /* first, remove the default value taken from the type */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005627 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5628 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005629 free(leaf->dflt);
5630 leaf->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005631 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005632
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005633 /* store the default value in unres */
Michal Vasko31a82d52020-08-21 08:11:48 +02005634 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005635 target->flags |= LYS_SET_DFLT;
5636 break;
5637 case LYS_LEAFLIST:
5638 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5639 /* first, remove the default value taken from the type */
5640 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005641 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5642 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5643 free(llist->dflts[x]);
5644 }
5645 LY_ARRAY_FREE(llist->dflts);
5646 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005647 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005648
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005649 /* store the default values in unres */
5650 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005651 target->flags |= LYS_SET_DFLT;
5652 break;
5653 case LYS_CHOICE:
5654 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5655 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5656 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5657 * to allow making the default case even the augmented case from the deviating module */
5658 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5659 goto cleanup;
5660 }
5661 break;
5662 default:
5663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5664 lys_nodetype2str(target->nodetype), "add", "default");
5665 goto cleanup;
5666 }
5667 }
5668
5669 /* [config-stmt] */
5670 if (d->flags & LYS_CONFIG_MASK) {
5671 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5673 lys_nodetype2str(target->nodetype), "add", "config");
5674 goto cleanup;
5675 }
5676 if (dev_flags) {
5677 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5678 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5679 }
5680 if (target->flags & LYS_SET_CONFIG) {
5681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5682 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5683 target->flags & LYS_CONFIG_W ? "true" : "false");
5684 goto cleanup;
5685 }
5686 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5687 }
5688
5689 /* [mandatory-stmt] */
5690 if (d->flags & LYS_MAND_MASK) {
5691 if (target->flags & LYS_MAND_MASK) {
5692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5693 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5694 target->flags & LYS_MAND_TRUE ? "true" : "false");
5695 goto cleanup;
5696 }
5697 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5698 }
5699
5700 /* [min-elements-stmt] */
5701 if (d->flags & LYS_SET_MIN) {
5702 if (target->nodetype == LYS_LEAFLIST) {
5703 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5704 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005705 ((struct lysc_node_leaflist *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005706 } else if (target->nodetype == LYS_LIST) {
5707 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5708 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005709 ((struct lysc_node_list *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005710 } else {
5711 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5712 lys_nodetype2str(target->nodetype), "add", "min-elements");
5713 goto cleanup;
5714 }
5715 if (d->min) {
5716 target->flags |= LYS_MAND_TRUE;
5717 }
5718 }
5719
5720 /* [max-elements-stmt] */
5721 if (d->flags & LYS_SET_MAX) {
5722 if (target->nodetype == LYS_LEAFLIST) {
5723 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5724 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005725 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005726 } else if (target->nodetype == LYS_LIST) {
5727 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5728 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005729 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005730 } else {
5731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5732 lys_nodetype2str(target->nodetype), "add", "max-elements");
5733 goto cleanup;
5734 }
5735 }
5736
5737 ret = LY_SUCCESS;
5738
5739cleanup:
5740 return ret;
5741}
5742
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005743static LY_ERR
5744lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt)
5745{
5746 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005747 ly_bool dyn = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005748 const char *orig_dflt;
5749 uint32_t i;
5750
5751 if (target->module != ctx->mod) {
5752 /* foreign deviation */
5753 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt);
5754
5755 /* check that the value matches */
5756 orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5757 if (strcmp(orig_dflt, dflt)) {
5758 goto error;
5759 }
5760 if (dyn) {
5761 free((char *)orig_dflt);
5762 }
5763
5764 /* remove the default specification */
5765 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5766 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5767 free(leaf->dflt);
5768 leaf->dflt = NULL;
5769 } else {
5770 /* local deviation */
5771 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt);
5772
5773 /* find the incomplete default */
5774 orig_dflt = NULL;
5775 for (i = 0; i < ctx->dflts.count; ++i) {
5776 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
5777 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5778 break;
5779 }
5780 }
5781 LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT);
5782
5783 /* check that the value matches */
5784 if (strcmp(orig_dflt, dflt)) {
5785 goto error;
5786 }
5787
5788 /* update the list of incomplete default values */
5789 lysc_incomplete_dflt_remove(ctx, target);
5790 }
5791
5792 target->flags &= ~LYS_SET_DFLT;
5793 return LY_SUCCESS;
5794
5795error:
5796 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5797 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5798 dflt, orig_dflt);
5799 if (dyn) {
5800 free((char *)orig_dflt);
5801 }
5802cleanup:
5803 return LY_EVALID;
5804}
5805
5806static LY_ERR
5807lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts)
5808{
5809 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005810 ly_bool dyn = 0, found;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005811 const char *orig_dflt, **orig_dflts;
5812 uint32_t i;
5813 LY_ARRAY_COUNT_TYPE x, y;
5814
5815 if (target->module != ctx->mod) {
5816 /* foreign deviation */
5817 DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]);
5818 LY_ARRAY_FOR(dflts, x) {
5819 found = 0;
5820 LY_ARRAY_FOR(llist->dflts, y) {
5821 orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5822 if (!strcmp(orig_dflt, dflts[x])) {
5823 if (dyn) {
5824 free((char *)orig_dflt);
5825 }
5826 found = 1;
5827 break;
5828 }
5829 if (dyn) {
5830 free((char *)orig_dflt);
5831 }
5832 }
5833 if (!found) {
5834 goto error;
5835 }
5836
5837 /* update compiled default values */
5838 LY_ARRAY_DECREMENT(llist->dflts);
5839 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
5840 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
5841 free(llist->dflts[y]);
5842 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
5843 }
5844 if (!LY_ARRAY_COUNT(llist->dflts)) {
5845 LY_ARRAY_FREE(llist->dflts);
5846 llist->dflts = NULL;
5847 llist->flags &= ~LYS_SET_DFLT;
5848 }
5849 } else {
5850 /* local deviation */
5851 orig_dflt = NULL;
5852 orig_dflts = NULL;
5853 for (i = 0; i < ctx->dflts.count; ++i) {
5854 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) {
5855 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5856 orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts;
5857 break;
5858 }
5859 }
5860 LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT);
5861
5862 if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) {
5863 /* TODO it is not currently possible to remove just one default value from incomplete defaults array */
5864 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5865 "Local deviation deleting leaf-list defaults is not supported.");
5866 return LY_EVALID;
5867 }
5868
5869 LY_ARRAY_FOR(dflts, x) {
5870 found = 0;
5871 if (orig_dflts) {
5872 LY_ARRAY_FOR(orig_dflts, y) {
5873 if (!strcmp(orig_dflts[y], dflts[x])) {
5874 found = 1;
5875 break;
5876 }
5877 }
5878 } else if (!strcmp(orig_dflt, dflts[x])) {
5879 found = 1;
5880 }
5881 if (!found) {
5882 goto error;
5883 }
5884
5885 /* update the list of incomplete default values */
5886 lysc_incomplete_dflt_remove(ctx, target);
5887 }
5888
5889 llist->flags &= ~LYS_SET_DFLT;
5890 }
5891
5892 return LY_SUCCESS;
5893
5894error:
5895 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
5896 "which does not match any of the target's property values.", dflts[x]);
5897cleanup:
5898 return LY_EVALID;
5899}
5900
Michal Vaskoccc062a2020-08-13 08:34:50 +02005901/**
5902 * @brief Apply deviate delete.
5903 *
5904 * @param[in] ctx Compile context.
5905 * @param[in] target Deviation target.
5906 * @param[in] dev_flags Internal deviation flags.
5907 * @param[in] d Deviate delete to apply.
5908 * @return LY_ERR value.
5909 */
5910static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005911lys_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 +02005912{
5913 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005914 struct lysc_node_list *list = (struct lysc_node_list *)target;
5915 LY_ARRAY_COUNT_TYPE x, y, z;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005916 size_t prefix_len, name_len;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005917 const char *prefix, *name, *nodeid;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005918 struct lys_module *mod;
5919
5920#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5921 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5922 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5923 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5924 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5925 } \
5926 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5928 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5929 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5930 goto cleanup; \
5931 } \
5932 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5933 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5934 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5935 &((TYPE)target)->ARRAY_TRG[y + 1], \
5936 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5937 } \
5938 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5939 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5940 ((TYPE)target)->ARRAY_TRG = NULL; \
5941 }
5942
5943 /* [units-stmt] */
5944 if (d->units) {
5945 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Michal Vasko22df3f02020-08-24 13:29:22 +02005946 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units);
5947 if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5949 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02005950 d->units, ((struct lysc_node_leaf *)target)->units);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005951 goto cleanup;
5952 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005953 lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5954 ((struct lysc_node_leaf *)target)->units = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005955 }
5956
5957 /* *must-stmt */
5958 if (d->musts) {
5959 switch (target->nodetype) {
5960 case LYS_CONTAINER:
5961 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005962 DEV_DEL_ARRAY(struct lysc_node_container *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005963 break;
5964 case LYS_LEAF:
5965 case LYS_LEAFLIST:
5966 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005967 DEV_DEL_ARRAY(struct lysc_node_leaf *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005968 break;
5969 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005970 DEV_DEL_ARRAY(struct lysc_notif *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005971 break;
5972 case LYS_RPC:
5973 case LYS_ACTION:
5974 if (dev_flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005975 DEV_DEL_ARRAY(struct lysc_action *, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005976 break;
5977 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005978 DEV_DEL_ARRAY(struct lysc_action *, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005979 break;
5980 }
Radek Krejci0f969882020-08-21 16:56:47 +02005981 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005982 default:
5983 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5984 lys_nodetype2str(target->nodetype), "delete", "must");
5985 goto cleanup;
5986 }
5987 }
5988
5989 /* *unique-stmt */
5990 if (d->uniques) {
5991 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5992 LY_ARRAY_FOR(d->uniques, x) {
5993 LY_ARRAY_FOR(list->uniques, z) {
5994 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
5995 nodeid = strpbrk(name, " \t\n");
5996 if (nodeid) {
5997 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
5998 break;
5999 }
6000 while (isspace(*nodeid)) {
6001 ++nodeid;
6002 }
6003 } else {
6004 if (strcmp(name, list->uniques[z][y]->name)) {
6005 break;
6006 }
6007 }
6008 }
6009 if (!name) {
6010 /* complete match - remove the unique */
6011 LY_ARRAY_DECREMENT(list->uniques);
6012 LY_ARRAY_FREE(list->uniques[z]);
6013 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
6014 --z;
6015 break;
6016 }
6017 }
6018 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
6019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6020 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6021 d->uniques[x]);
6022 goto cleanup;
6023 }
6024 }
6025 if (!LY_ARRAY_COUNT(list->uniques)) {
6026 LY_ARRAY_FREE(list->uniques);
6027 list->uniques = NULL;
6028 }
6029 }
6030
6031 /* *default-stmt */
6032 if (d->dflts) {
6033 switch (target->nodetype) {
6034 case LYS_LEAF:
6035 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006036 LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006037 break;
6038 case LYS_LEAFLIST:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006039 LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006040 break;
6041 case LYS_CHOICE:
6042 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vasko22df3f02020-08-24 13:29:22 +02006043 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "deleting", "default", d->dflts[0]);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006044 nodeid = d->dflts[0];
6045 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6046 if (prefix) {
6047 /* use module prefixes from the deviation module to match the module of the default case */
6048 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006049 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006050 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6051 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6052 goto cleanup;
6053 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006054 if (mod != ((struct lysc_node_choice *)target)->dflt->module) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006055 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006056 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6057 "The prefix does not match the default case's module.", d->dflts[0]);
6058 goto cleanup;
6059 }
6060 }
6061 /* else {
6062 * strictly, the default prefix would point to the deviation module, but the value should actually
6063 * match the default string in the original module (usually unprefixed), so in this case we do not check
6064 * the module of the default case, just matching its name */
Michal Vasko22df3f02020-08-24 13:29:22 +02006065 if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02006066 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6067 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02006068 d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006069 goto cleanup;
6070 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006071 ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT;
6072 ((struct lysc_node_choice *)target)->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006073 break;
6074 default:
6075 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6076 lys_nodetype2str(target->nodetype), "delete", "default");
6077 goto cleanup;
6078 }
6079 }
6080
6081 ret = LY_SUCCESS;
6082
6083cleanup:
6084 return ret;
6085}
6086
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006087static LY_ERR
6088lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target)
6089{
6090 LY_ERR ret;
6091 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6092 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6093 struct ly_err_item *err = NULL;
6094 LY_ARRAY_COUNT_TYPE x;
6095 const char *dflt;
Radek Krejci857189e2020-09-01 13:26:36 +02006096 ly_bool dyn;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006097
6098 if (target->module != ctx->mod) {
6099 /* foreign deviation */
6100 if (target->nodetype == LYS_LEAF) {
6101 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn);
6102 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6103 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6104
6105 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6106 LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err);
6107 if (dyn) {
6108 free((char *)dflt);
6109 }
6110 if (err) {
6111 ly_err_print(err);
6112 ctx->path[0] = '\0';
6113 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6115 "Invalid default - value does not fit the type (%s).", err->msg);
6116 ly_err_free(err);
6117 }
6118 LY_CHECK_RET(ret);
6119
6120 ++leaf->dflt->realtype->refcount;
6121 } else { /* LY_LEAFLIST */
6122 LY_ARRAY_FOR(llist->dflts, x) {
6123 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn);
6124 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6125 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6126
6127 ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6128 LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err);
6129 if (dyn) {
6130 free((char *)dflt);
6131 }
6132 if (err) {
6133 ly_err_print(err);
6134 ctx->path[0] = '\0';
6135 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6136 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6137 "Invalid default - value does not fit the type (%s).", err->msg);
6138 ly_err_free(err);
6139 }
6140 LY_CHECK_RET(ret);
6141
6142 ++llist->dflts[x]->realtype->refcount;
6143 }
6144 }
6145 } else {
6146 /* local deviation */
6147
6148 /* these default were not compiled yet, so they will use the new type automatically */
6149 }
6150
6151 return LY_SUCCESS;
6152}
6153
Michal Vaskoccc062a2020-08-13 08:34:50 +02006154/**
6155 * @brief Apply deviate replace.
6156 *
6157 * @param[in] ctx Compile context.
6158 * @param[in] target Deviation target.
6159 * @param[in] d Deviate replace to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02006160 * @return LY_ERR value.
6161 */
6162static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006163lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02006164{
Radek Krejci011e4aa2020-09-04 15:22:31 +02006165 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006166 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6167 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6168 LY_ARRAY_COUNT_TYPE x;
6169
6170#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6171 if (!(((TYPE)target)->MEMBER COND)) { \
6172 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6173 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6174 goto cleanup; \
6175 }
6176
6177 /* [type-stmt] */
6178 if (d->type) {
6179 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6180 /* type is mandatory, so checking for its presence is not necessary */
6181 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6182
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006183 /* remove only default value inherited from the type */
6184 if (!(target->flags & LYS_SET_DFLT)) {
6185 if (target->module != ctx->mod) {
6186 /* foreign deviation - the target has default from the previous type, remove it */
6187 if (target->nodetype == LYS_LEAF) {
6188 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6189 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6190 free(leaf->dflt);
6191 leaf->dflt = NULL;
6192 } else { /* LYS_LEAFLIST */
6193 LY_ARRAY_FOR(llist->dflts, x) {
6194 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6195 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6196 free(llist->dflts[x]);
6197 }
6198 LY_ARRAY_FREE(llist->dflts);
6199 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006200 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006201 } else {
6202 /* local deviation */
6203 lysc_incomplete_dflt_remove(ctx, target);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006204 }
6205 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006206
6207 LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf));
6208
6209 if (target->flags & LYS_SET_DFLT) {
6210 /* the term default value(s) needs to be recompiled */
6211 LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006212 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006213 }
6214
6215 /* [units-stmt] */
6216 if (d->units) {
6217 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6218 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6219 units, "replacing", "units", d->units);
6220
6221 lydict_remove(ctx->ctx, leaf->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02006222 DUP_STRING(ctx->ctx, d->units, leaf->units, rc);
6223 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006224 }
6225
6226 /* [default-stmt] */
6227 if (d->dflt) {
6228 switch (target->nodetype) {
6229 case LYS_LEAF:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006230 if (target->module != ctx->mod) {
6231 /* foreign deviation */
6232 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing",
6233 "default", d->dflt);
6234
6235 /* remove the default specification */
6236 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6237 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6238 free(leaf->dflt);
6239 leaf->dflt = NULL;
6240 } else {
6241 /* local deviation */
6242 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing",
6243 "default", d->dflt);
6244 assert(!leaf->dflt);
6245 }
6246
6247 /* store the new default value */
6248 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006249 break;
6250 case LYS_CHOICE:
6251 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6252 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6253 goto cleanup;
6254 }
6255 break;
6256 default:
6257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6258 lys_nodetype2str(target->nodetype), "replace", "default");
6259 goto cleanup;
6260 }
6261 }
6262
6263 /* [config-stmt] */
6264 if (d->flags & LYS_CONFIG_MASK) {
6265 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6267 lys_nodetype2str(target->nodetype), "replace", "config");
6268 goto cleanup;
6269 }
6270 if (!(target->flags & LYS_SET_CONFIG)) {
6271 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6272 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6273 goto cleanup;
6274 }
6275 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6276 }
6277
6278 /* [mandatory-stmt] */
6279 if (d->flags & LYS_MAND_MASK) {
6280 if (!(target->flags & LYS_MAND_MASK)) {
6281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6282 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6283 goto cleanup;
6284 }
6285 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6286 }
6287
6288 /* [min-elements-stmt] */
6289 if (d->flags & LYS_SET_MIN) {
6290 if (target->nodetype == LYS_LEAFLIST) {
6291 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6292 /* change value */
6293 ((struct lysc_node_leaflist *)target)->min = d->min;
6294 } else if (target->nodetype == LYS_LIST) {
6295 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6296 /* change value */
6297 ((struct lysc_node_list *)target)->min = d->min;
6298 } else {
6299 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6300 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6301 goto cleanup;
6302 }
6303 if (d->min) {
6304 target->flags |= LYS_MAND_TRUE;
6305 }
6306 }
6307
6308 /* [max-elements-stmt] */
6309 if (d->flags & LYS_SET_MAX) {
6310 if (target->nodetype == LYS_LEAFLIST) {
6311 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6312 /* change value */
6313 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6314 } else if (target->nodetype == LYS_LIST) {
6315 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6316 /* change value */
6317 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6318 } else {
6319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6320 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6321 goto cleanup;
6322 }
6323 }
6324
6325 ret = LY_SUCCESS;
6326
6327cleanup:
6328 return ret;
6329}
6330
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006331/**
6332 * @brief Apply all deviations of one target node.
6333 *
6334 * @param[in] ctx Compile context.
6335 * @param[in] dev Deviation structure to apply.
6336 * @return LY_ERR value.
6337 */
6338static LY_ERR
6339lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006340{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006341 LY_ERR ret = LY_EVALID;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006342 struct lysc_node *target = dev->target;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006343 struct lysc_action *rpcs;
6344 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006345 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006346 LY_ARRAY_COUNT_TYPE v, x;
Radek Krejci551b12c2019-02-19 16:11:21 +01006347 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006348
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006349 lysc_update_path(ctx, NULL, dev->nodeid);
6350
6351 /* not-supported */
6352 if (dev->not_supported) {
6353 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
Radek Krejci0f969882020-08-21 16:56:47 +02006354 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 +02006355 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6356 }
6357
6358#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6359 if (target->parent) { \
6360 ARRAY = (TYPE*)GETFUNC(target->parent); \
6361 } else { \
6362 ARRAY = target->module->compiled->ARRAY; \
6363 } \
6364 LY_ARRAY_FOR(ARRAY, x) { \
6365 if (&ARRAY[x] == (TYPE*)target) { break; } \
6366 } \
6367 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6368 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6369 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6370 LY_ARRAY_DECREMENT(ARRAY); \
6371 }
6372
6373 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6374 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6375 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006376 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6377 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6378 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6379 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006380 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6381 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006382 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6383 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6384 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6385 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006386 } else {
6387 /* remove RPC/action */
6388 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6389 }
6390 } else if (target->nodetype == LYS_NOTIF) {
6391 /* remove Notification */
6392 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6393 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02006394 if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) {
6395 /* remove the target node with its parent case node because it is the only node of the case */
6396 lysc_node_unlink(target->parent);
6397 lysc_node_free(ctx->ctx, target->parent);
6398 } else {
6399 /* remove the target node */
6400 lysc_node_unlink(target);
6401 lysc_node_free(ctx->ctx, target);
6402 }
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006403 }
6404
6405 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6406 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6407 return LY_SUCCESS;
6408 }
6409
6410 /* list of deviates (not-supported is not present in the list) */
6411 LY_ARRAY_FOR(dev->deviates, v) {
6412 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006413 switch (d->mod) {
6414 case LYS_DEV_ADD:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006415 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006416 break;
6417 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006418 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006419 break;
6420 case LYS_DEV_REPLACE:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006421 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006422 break;
6423 default:
6424 LOGINT(ctx->ctx);
6425 goto cleanup;
6426 }
6427 }
6428
6429 /* final check when all deviations of a single target node are applied */
6430
6431 /* check min-max compatibility */
6432 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006433 min = ((struct lysc_node_leaflist *)target)->min;
6434 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006435 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006436 min = ((struct lysc_node_list *)target)->min;
6437 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006438 } else {
6439 min = max = 0;
6440 }
6441 if (min > max) {
6442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6443 "after deviation: min value %u is bigger than max value %u.", min, max);
6444 goto cleanup;
6445 }
6446
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006447 /* check mandatory - default compatibility */
6448 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6449 && (target->flags & LYS_MAND_TRUE)) {
6450 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6451 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6452 goto cleanup;
Michal Vasko22df3f02020-08-24 13:29:22 +02006453 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006454 && (target->flags & LYS_MAND_TRUE)) {
6455 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6456 goto cleanup;
6457 }
6458 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6459 /* mandatory node under a default case */
6460 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6461 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6462 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6463 goto cleanup;
6464 }
6465
6466 /* success */
6467 ret = LY_SUCCESS;
6468
6469cleanup:
6470 lysc_update_path(ctx, NULL, NULL);
6471 return ret;
6472}
6473
6474LY_ERR
6475lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6476{
6477 LY_ERR ret = LY_EVALID;
6478 struct ly_set devs_p = {0};
6479 struct ly_set targets = {0};
6480 struct lysc_node *target; /* target target of the deviation */
6481 struct lysp_deviation *dev;
6482 struct lysp_deviate *d, **dp_new;
6483 LY_ARRAY_COUNT_TYPE u, v;
6484 struct lysc_deviation **devs = NULL;
6485 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006486 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006487
6488 /* get all deviations from the module and all its submodules ... */
6489 LY_ARRAY_FOR(mod_p->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006490 LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST, NULL));
Radek Krejciccd20f12019-02-15 14:12:27 +01006491 }
6492 LY_ARRAY_FOR(mod_p->includes, v) {
6493 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006494 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 +01006495 }
6496 }
6497 if (!devs_p.count) {
6498 /* nothing to do */
6499 return LY_SUCCESS;
6500 }
6501
Radek Krejci327de162019-06-14 12:52:07 +02006502 lysc_update_path(ctx, NULL, "{deviation}");
6503
Radek Krejciccd20f12019-02-15 14:12:27 +01006504 /* ... and group them by the target node */
6505 devs = calloc(devs_p.count, sizeof *devs);
6506 for (u = 0; u < devs_p.count; ++u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006507 uint32_t index;
6508
Radek Krejciccd20f12019-02-15 14:12:27 +01006509 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006510 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006511
6512 /* resolve the target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02006513 LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
Michal Vasko22df3f02020-08-24 13:29:22 +02006514 (const struct lysc_node **)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006515 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006516 /* move the target pointer to input/output to make them different from the action and
6517 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6518 * back to the RPC/action node due to a better compatibility and decision code in this function.
6519 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6520 if (flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006521 target = (struct lysc_node *)&((struct lysc_action *)target)->input;
Radek Krejcif538ce52019-03-05 10:46:14 +01006522 flags |= LYSC_OPT_INTERNAL;
6523 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006524 target = (struct lysc_node *)&((struct lysc_action *)target)->output;
Radek Krejcif538ce52019-03-05 10:46:14 +01006525 flags |= LYSC_OPT_INTERNAL;
6526 }
6527 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006528 /* insert into the set of targets with duplicity detection */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006529 ret = ly_set_add(&targets, target, 0, &index);
6530 LY_CHECK_GOTO(ret, cleanup);
6531 if (!devs[index]) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006532 /* new record */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006533 devs[index] = calloc(1, sizeof **devs);
6534 devs[index]->target = target;
6535 devs[index]->nodeid = dev->nodeid;
6536 devs[index]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006537 }
6538 /* add deviates into the deviation's list of deviates */
Michal Vasko5c38f362020-08-24 09:22:51 +02006539 LY_LIST_FOR(dev->deviates, d) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006540 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[index]->deviates, dp_new, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006541 *dp_new = d;
6542 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006543 devs[index]->not_supported = 1;
Radek Krejciccd20f12019-02-15 14:12:27 +01006544 }
6545 }
Radek Krejci327de162019-06-14 12:52:07 +02006546
6547 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006548 }
6549
Radek Krejciccd20f12019-02-15 14:12:27 +01006550 /* apply deviations */
6551 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006552 ly_bool match = 0;
Radek Krejciba03a5a2020-08-27 14:40:41 +02006553
Radek Krejcif538ce52019-03-05 10:46:14 +01006554 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6555 /* fix the target pointer in case of RPC's/action's input/output */
6556 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006557 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006558 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006559 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006560 }
6561 }
6562
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006563 /* remember target module (the target node may be removed) */
6564 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006565
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006566 /* apply the deviation */
6567 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006568
Michal Vaskoe6143202020-07-03 13:02:08 +02006569 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006570 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6571 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006572 match = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006573 break;
6574 }
6575 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02006576 if (!match) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006577 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006578 *dev_mod = mod_p->mod;
6579 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006580 }
6581
Radek Krejci327de162019-06-14 12:52:07 +02006582 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006583 ret = LY_SUCCESS;
6584
6585cleanup:
6586 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6587 LY_ARRAY_FREE(devs[u]->deviates);
6588 free(devs[u]);
6589 }
6590 free(devs);
6591 ly_set_erase(&targets, NULL);
6592 ly_set_erase(&devs_p, NULL);
6593
6594 return ret;
6595}
6596
Radek Krejcib56c7502019-02-13 14:19:54 +01006597/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006598 * @brief Compile the given YANG submodule into the main module.
6599 * @param[in] ctx Compile context
6600 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006601 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6602 */
6603LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006604lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006605{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006606 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006607 LY_ERR ret = LY_SUCCESS;
6608 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006609 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006610 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006611 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006612
Radek Krejci14915cc2020-09-14 17:28:13 +02006613 /* features are compiled directly into the compiled module structure,
6614 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6615 * The features compilation is finished in the main module (lys_compile()). */
6616 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->mod->features);
6617 LY_CHECK_GOTO(ret, error);
Radek Krejci0af46292019-01-11 16:02:31 +01006618
Radek Krejci80d281e2020-09-14 17:42:54 +02006619 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->mod->identities);
6620 LY_CHECK_GOTO(ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006621
Radek Krejci474f9b82019-07-24 11:36:37 +02006622 /* data nodes */
6623 LY_LIST_FOR(submod->data, node_p) {
6624 ret = lys_compile_node(ctx, node_p, NULL, 0);
6625 LY_CHECK_GOTO(ret, error);
6626 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006627
Radek Krejciec4da802019-05-02 13:02:41 +02006628 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6629 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006630
Radek Krejcid05cbd92018-12-05 14:26:40 +01006631error:
6632 return ret;
6633}
6634
Radek Krejci335332a2019-09-05 13:03:35 +02006635static void *
6636lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6637{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006638 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci335332a2019-09-05 13:03:35 +02006639 if (substmts[u].stmt == stmt) {
6640 return substmts[u].storage;
6641 }
6642 }
6643 return NULL;
6644}
6645
6646LY_ERR
6647lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6648{
6649 LY_ERR ret = LY_EVALID, r;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006650 LY_ARRAY_COUNT_TYPE u;
Radek Krejci335332a2019-09-05 13:03:35 +02006651 struct lysp_stmt *stmt;
6652 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006653
6654 /* check for invalid substatements */
6655 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006656 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6657 continue;
6658 }
Radek Krejci335332a2019-09-05 13:03:35 +02006659 for (u = 0; substmts[u].stmt; ++u) {
6660 if (substmts[u].stmt == stmt->kw) {
6661 break;
6662 }
6663 }
6664 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6666 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006667 goto cleanup;
6668 }
Radek Krejci335332a2019-09-05 13:03:35 +02006669 }
6670
Radek Krejciad5963b2019-09-06 16:03:05 +02006671 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6672
Radek Krejci335332a2019-09-05 13:03:35 +02006673 /* keep order of the processing the same as the order in the defined substmts,
6674 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6675 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006676 ly_bool stmt_present = 0;
Radek Krejciad5963b2019-09-06 16:03:05 +02006677
Radek Krejci335332a2019-09-05 13:03:35 +02006678 for (stmt = ext->child; stmt; stmt = stmt->next) {
6679 if (substmts[u].stmt != stmt->kw) {
6680 continue;
6681 }
6682
Radek Krejciad5963b2019-09-06 16:03:05 +02006683 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006684 if (substmts[u].storage) {
6685 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006686 case LY_STMT_STATUS:
6687 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6688 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6689 break;
6690 case LY_STMT_UNITS: {
6691 const char **units;
6692
6693 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6694 /* single item */
6695 if (*((const char **)substmts[u].storage)) {
6696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6697 goto cleanup;
6698 }
6699 units = (const char **)substmts[u].storage;
6700 } else {
6701 /* sized array */
6702 const char ***units_array = (const char ***)substmts[u].storage;
6703 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6704 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02006705 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
6706 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +02006707 break;
6708 }
Radek Krejci335332a2019-09-05 13:03:35 +02006709 case LY_STMT_TYPE: {
6710 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6711 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6712
6713 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6714 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006715 if (*(struct lysc_type **)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6717 goto cleanup;
6718 }
6719 compiled = substmts[u].storage;
6720 } else {
6721 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006722 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006723 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006724 compiled = (void *)type;
Radek Krejci335332a2019-09-05 13:03:35 +02006725 }
6726
Radek Krejciad5963b2019-09-06 16:03:05 +02006727 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006728 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL,
6729 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006730 units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02006731 lysp_type_free(ctx->ctx, parsed);
6732 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006733 break;
6734 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006735 case LY_STMT_IF_FEATURE: {
6736 struct lysc_iffeature *iff = NULL;
6737
6738 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6739 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006740 if (((struct lysc_iffeature *)substmts[u].storage)->features) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6742 goto cleanup;
6743 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006744 iff = (struct lysc_iffeature *)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006745 } else {
6746 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006747 struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006748 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6749 }
6750 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6751 break;
6752 }
6753 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6754 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006755 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006756 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.",
6757 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006758 goto cleanup;
6759 }
6760 }
Radek Krejci335332a2019-09-05 13:03:35 +02006761 }
Radek Krejci335332a2019-09-05 13:03:35 +02006762
Radek Krejciad5963b2019-09-06 16:03:05 +02006763 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6765 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6766 goto cleanup;
6767 }
Radek Krejci335332a2019-09-05 13:03:35 +02006768 }
6769
6770 ret = LY_SUCCESS;
6771
6772cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006773 return ret;
6774}
6775
Michal Vasko175012e2019-11-06 15:49:14 +01006776/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006777 * @brief Check when for cyclic dependencies.
6778 * @param[in] set Set with all the referenced nodes.
6779 * @param[in] node Node whose "when" referenced nodes are in @p set.
6780 * @return LY_ERR value
6781 */
6782static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006783lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006784{
6785 struct lyxp_set tmp_set;
6786 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006787 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006788 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006789 struct lysc_when *when;
6790 LY_ERR ret = LY_SUCCESS;
6791
6792 memset(&tmp_set, 0, sizeof tmp_set);
6793
6794 /* prepare in_ctx of the set */
Michal Vaskod989ba02020-08-24 10:59:24 +02006795 for (i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006796 xp_scnode = &set->val.scnodes[i];
6797
Michal Vasko5c4e5892019-11-14 12:31:38 +01006798 if (xp_scnode->in_ctx != -1) {
6799 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006800 xp_scnode->in_ctx = 1;
6801 }
6802 }
6803
6804 for (i = 0; i < set->used; ++i) {
6805 xp_scnode = &set->val.scnodes[i];
6806 if (xp_scnode->in_ctx != 1) {
6807 /* already checked */
6808 continue;
6809 }
6810
Michal Vasko1bf09392020-03-27 12:38:10 +01006811 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006812 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006813 /* no when to check */
6814 xp_scnode->in_ctx = 0;
6815 continue;
6816 }
6817
6818 node = xp_scnode->scnode;
6819 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006820 LY_ARRAY_FOR(node->when, u) {
6821 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006822 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006823 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6824 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006825 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006826 goto cleanup;
6827 }
6828
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006829 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006830 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006831 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006832 /* try to find this node in our set */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006833 uint32_t idx;
6834 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 +01006835 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 +01006836 ret = LY_EVALID;
6837 goto cleanup;
6838 }
6839
6840 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006841 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006842 } else {
6843 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006844 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006845 }
6846 }
6847
6848 /* merge this set into the global when set */
6849 lyxp_set_scnode_merge(set, &tmp_set);
6850 }
6851
6852 /* check when of non-data parents as well */
6853 node = node->parent;
6854 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6855
Michal Vasko251f56e2019-11-14 16:06:47 +01006856 /* this node when was checked (xp_scnode could have been reallocd) */
6857 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006858 }
6859
6860cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006861 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006862 return ret;
6863}
6864
6865/**
Michal Vasko175012e2019-11-06 15:49:14 +01006866 * @brief Check when/must expressions of a node on a compiled schema tree.
6867 * @param[in] ctx Compile context.
6868 * @param[in] node Node to check.
6869 * @return LY_ERR value
6870 */
6871static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006872lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006873{
Michal Vasko175012e2019-11-06 15:49:14 +01006874 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006875 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006876 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006877 uint32_t opts;
Radek Krejci857189e2020-09-01 13:26:36 +02006878 ly_bool input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006879 struct lysc_when **when = NULL;
6880 struct lysc_must *musts = NULL;
6881 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006882 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006883
6884 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006885 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006886 if (node->flags & LYS_CONFIG_R) {
6887 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6888 if (op) {
6889 /* we are actually in output */
6890 opts = LYXP_SCNODE_OUTPUT;
6891 }
6892 }
Michal Vasko175012e2019-11-06 15:49:14 +01006893
6894 switch (node->nodetype) {
6895 case LYS_CONTAINER:
6896 when = ((struct lysc_node_container *)node)->when;
6897 musts = ((struct lysc_node_container *)node)->musts;
6898 break;
6899 case LYS_CHOICE:
6900 when = ((struct lysc_node_choice *)node)->when;
6901 break;
6902 case LYS_LEAF:
6903 when = ((struct lysc_node_leaf *)node)->when;
6904 musts = ((struct lysc_node_leaf *)node)->musts;
6905 break;
6906 case LYS_LEAFLIST:
6907 when = ((struct lysc_node_leaflist *)node)->when;
6908 musts = ((struct lysc_node_leaflist *)node)->musts;
6909 break;
6910 case LYS_LIST:
6911 when = ((struct lysc_node_list *)node)->when;
6912 musts = ((struct lysc_node_list *)node)->musts;
6913 break;
6914 case LYS_ANYXML:
6915 case LYS_ANYDATA:
6916 when = ((struct lysc_node_anydata *)node)->when;
6917 musts = ((struct lysc_node_anydata *)node)->musts;
6918 break;
6919 case LYS_CASE:
6920 when = ((struct lysc_node_case *)node)->when;
6921 break;
6922 case LYS_NOTIF:
6923 musts = ((struct lysc_notif *)node)->musts;
6924 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006925 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006926 case LYS_ACTION:
6927 /* first process input musts */
6928 musts = ((struct lysc_action *)node)->input.musts;
6929 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006930 default:
6931 /* nothing to check */
6932 break;
6933 }
6934
Michal Vasko175012e2019-11-06 15:49:14 +01006935 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006936 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006937 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 +02006938 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006939 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006940 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006941 goto cleanup;
6942 }
6943
Michal Vaskodc052f32019-11-07 11:11:38 +01006944 ctx->path[0] = '\0';
6945 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006946 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006947 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006948 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6949 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006950
Michal Vaskoecd62de2019-11-13 12:35:11 +01006951 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006952 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006953 schema->name);
6954 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006955
6956 /* check dummy node accessing */
6957 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006958 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006959 ret = LY_EVALID;
6960 goto cleanup;
6961 }
Michal Vasko175012e2019-11-06 15:49:14 +01006962 }
6963 }
6964
Michal Vaskoecd62de2019-11-13 12:35:11 +01006965 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006966 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006967 LY_CHECK_GOTO(ret, cleanup);
6968
Michal Vaskod3678892020-05-21 10:06:58 +02006969 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006970 }
6971
Michal Vasko5d8756a2019-11-07 15:21:00 +01006972check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006973 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006974 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006975 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 +01006976 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006977 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006978 goto cleanup;
6979 }
6980
Michal Vaskodc052f32019-11-07 11:11:38 +01006981 ctx->path[0] = '\0';
6982 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006983 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006984 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006985 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006986 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006987 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6988 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006989 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006990 }
6991 }
6992
Michal Vaskod3678892020-05-21 10:06:58 +02006993 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006994 }
6995
Michal Vasko1bf09392020-03-27 12:38:10 +01006996 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006997 /* now check output musts */
6998 input_done = 1;
6999 musts = ((struct lysc_action *)node)->output.musts;
7000 opts = LYXP_SCNODE_OUTPUT;
7001 goto check_musts;
7002 }
7003
Michal Vasko175012e2019-11-06 15:49:14 +01007004cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007005 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007006 return ret;
7007}
7008
Michal Vasko8d544252020-03-02 10:19:52 +01007009static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007010lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
7011{
Michal Vasko6b26e742020-07-17 15:02:10 +02007012 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02007013 struct ly_path *p;
7014 struct lysc_type *type;
7015
7016 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7017
7018 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02007019 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
7020 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007021 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02007022
7023 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007024 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02007025 ly_path_free(node->module->ctx, p);
7026
7027 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7028 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7029 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
7030 lref->path->expr, lys_nodetype2str(target->nodetype));
7031 return LY_EVALID;
7032 }
7033
7034 /* check status */
7035 ctx->path[0] = '\0';
7036 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7037 ctx->path_len = strlen(ctx->path);
7038 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
7039 return LY_EVALID;
7040 }
7041 ctx->path_len = 1;
7042 ctx->path[1] = '\0';
7043
7044 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007045 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007046 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007047 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007048 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7049 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7050 return LY_EVALID;
7051 }
7052 }
7053
7054 /* store the target's type and check for circular chain of leafrefs */
7055 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7056 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7057 if (type == (struct lysc_type *)lref) {
7058 /* circular chain detected */
7059 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7060 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7061 return LY_EVALID;
7062 }
7063 }
7064
7065 /* check if leafref and its target are under common if-features */
7066 if (lys_compile_leafref_features_validate(node, target)) {
7067 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7068 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7069 " features applicable to the leafref itself.", lref->path->expr);
7070 return LY_EVALID;
7071 }
7072
7073 return LY_SUCCESS;
7074}
7075
7076static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007077lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7078{
7079 struct lysc_ext_instance *ext;
7080 struct lysp_ext_instance *ext_p = NULL;
7081 struct lysp_stmt *stmt;
7082 const struct lys_module *ext_mod;
7083 LY_ERR ret = LY_SUCCESS;
7084
7085 /* create the parsed extension instance manually */
7086 ext_p = calloc(1, sizeof *ext_p);
7087 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007088 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
7089 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007090 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7091 ext_p->insubstmt_index = 0;
7092
Radek Krejci87e25252020-09-15 13:28:31 +02007093 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
7094 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007095 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
7096 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007097 stmt->kw = LY_STMT_TYPE;
Michal Vasko8d544252020-03-02 10:19:52 +01007098
7099 /* allocate new extension instance */
7100 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7101
7102 /* manually get extension definition module */
7103 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7104
7105 /* compile the extension instance */
7106 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7107
7108cleanup:
7109 lysp_ext_instance_free(ctx->ctx, ext_p);
7110 free(ext_p);
7111 return ret;
7112}
7113
Michal Vasko004d3152020-06-11 19:59:22 +02007114static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007115lys_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 +02007116 const struct lys_module *dflt_mod, struct lyd_value *storage)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007117{
7118 LY_ERR ret;
7119 struct ly_err_item *err = NULL;
7120
7121 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7122 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7123 if (err) {
7124 ly_err_print(err);
7125 ctx->path[0] = '\0';
7126 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7128 "Invalid default - value does not fit the type (%s).", err->msg);
7129 ly_err_free(err);
7130 }
7131 if (!ret) {
7132 ++storage->realtype->refcount;
7133 return LY_SUCCESS;
7134 }
7135 return ret;
7136}
7137
7138static LY_ERR
7139lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +02007140 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007141{
7142 LY_ERR ret;
7143
7144 assert(!leaf->dflt);
7145
7146 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7147 /* ignore default values for keys and mandatory leaves */
7148 return LY_SUCCESS;
7149 }
7150
7151 /* allocate the default value */
7152 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7153 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7154
7155 /* store the default value */
7156 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt);
7157 if (ret) {
7158 free(leaf->dflt);
7159 leaf->dflt = NULL;
7160 }
7161
7162 return ret;
7163}
7164
7165static LY_ERR
7166lys_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 +02007167 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007168{
7169 LY_ERR ret;
7170 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7171
7172 assert(dflt || dflts);
7173
7174 if (llist->dflts) {
7175 /* there were already some defaults and we are adding new by deviations */
7176 assert(dflts);
7177 orig_count = LY_ARRAY_COUNT(llist->dflts);
7178 } else {
7179 orig_count = 0;
7180 }
7181
7182 /* allocate new items */
7183 if (dflts) {
7184 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7185 } else {
7186 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7187 }
7188
7189 /* fill each new default value */
7190 if (dflts) {
7191 LY_ARRAY_FOR(dflts, u) {
7192 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
7193 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod,
7194 llist->dflts[orig_count + u]);
7195 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7196 LY_ARRAY_INCREMENT(llist->dflts);
7197 }
7198 } else {
7199 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
7200 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]);
7201 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7202 LY_ARRAY_INCREMENT(llist->dflts);
7203 }
7204
7205 /* check default value uniqueness */
7206 if (llist->flags & LYS_CONFIG_W) {
7207 /* configuration data values must be unique - so check the default values */
7208 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7209 for (v = 0; v < u; ++v) {
7210 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejci857189e2020-09-01 13:26:36 +02007211 ly_bool dynamic = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007212 const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic);
7213
7214 lysc_update_path(ctx, llist->parent, llist->name);
7215 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7216 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
7217 lysc_update_path(ctx, NULL, NULL);
7218 if (dynamic) {
7219 free((char *)val);
7220 }
7221 return LY_EVALID;
7222 }
7223 }
7224 }
7225 }
7226
7227 return LY_SUCCESS;
7228}
7229
7230static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007231lys_compile_unres(struct lysc_ctx *ctx)
7232{
7233 struct lysc_node *node;
7234 struct lysc_type *type, *typeiter;
7235 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007236 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007237 uint32_t i;
7238
7239 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7240 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7241 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7242 for (i = 0; i < ctx->leafrefs.count; ++i) {
7243 node = ctx->leafrefs.objs[i];
7244 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7245 type = ((struct lysc_node_leaf *)node)->type;
7246 if (type->basetype == LY_TYPE_LEAFREF) {
7247 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7248 } else if (type->basetype == LY_TYPE_UNION) {
7249 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7250 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7251 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7252 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7253 }
7254 }
7255 }
7256 }
7257 for (i = 0; i < ctx->leafrefs.count; ++i) {
7258 /* store pointer to the real type */
7259 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7260 if (type->basetype == LY_TYPE_LEAFREF) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007261 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007262 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007263 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7264 ((struct lysc_type_leafref *)type)->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007265 } else if (type->basetype == LY_TYPE_UNION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007266 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7267 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7268 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007269 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007270 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7271 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007272 }
7273 }
7274 }
7275 }
7276
7277 /* check xpath */
7278 for (i = 0; i < ctx->xpath.count; ++i) {
7279 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7280 }
7281
7282 /* finish incomplete default values compilation */
7283 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007284 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007285 if (r->leaf->nodetype == LYS_LEAF) {
7286 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod));
7287 } else {
7288 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 +02007289 }
Michal Vasko004d3152020-06-11 19:59:22 +02007290 }
7291
7292 return LY_SUCCESS;
7293}
7294
Radek Krejci19a96102018-11-15 13:38:09 +01007295LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02007296lys_compile(struct lys_module *mod, uint32_t options)
Radek Krejci19a96102018-11-15 13:38:09 +01007297{
7298 struct lysc_ctx ctx = {0};
7299 struct lysc_module *mod_c;
7300 struct lysp_module *sp;
7301 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007302 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007303 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007304 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007305 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007306 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007307 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007308 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007309
Michal Vasko7a0b0762020-09-02 16:37:01 +02007310 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007311
Michal Vasko7a0b0762020-09-02 16:37:01 +02007312 if (!mod->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007313 /* just imported modules are not compiled */
7314 return LY_SUCCESS;
7315 }
7316
Michal Vasko7a0b0762020-09-02 16:37:01 +02007317 compile_id = ++mod->ctx->module_set_id;
7318 sp = mod->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007319
Michal Vasko7a0b0762020-09-02 16:37:01 +02007320 ctx.ctx = mod->ctx;
7321 ctx.mod = mod;
7322 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007323 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007324 ctx.path_len = 1;
7325 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007326
Michal Vasko7a0b0762020-09-02 16:37:01 +02007327 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
7328 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7329 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007330
Michal Vasko7c8439f2020-08-05 13:25:19 +02007331 LY_ARRAY_FOR(sp->imports, u) {
7332 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7333 }
Radek Krejci0935f412019-08-20 16:15:18 +02007334
Radek Krejci14915cc2020-09-14 17:28:13 +02007335 /* features precompilation */
7336 if (!mod->features && sp->features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007337 /* features are compiled directly into the compiled module structure,
7338 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci14915cc2020-09-14 17:28:13 +02007339 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007340 LY_CHECK_GOTO(ret, error);
Radek Krejci14915cc2020-09-14 17:28:13 +02007341 } /* else the features are already precompiled */
Michal Vasko33ff9422020-07-03 09:50:39 +02007342
Radek Krejci14915cc2020-09-14 17:28:13 +02007343 /* similarly, identities precompilation */
Radek Krejci80d281e2020-09-14 17:42:54 +02007344 if (!mod->identities && sp->identities) {
7345 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod->identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02007346 LY_CHECK_GOTO(ret, error);
7347 }
Radek Krejci14915cc2020-09-14 17:28:13 +02007348
7349 /* compile submodules
7350 * - must be between features/identities precompilation and finishing their compilation to cover features/identities from
7351 * submodules */
7352 LY_ARRAY_FOR(sp->includes, u) {
7353 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
7354 }
7355
7356 /* finish feature compilation, not only for the main module, but also for the submodules.
7357 * Due to possible forward references, it must be done when all the features (including submodules)
7358 * are present. */
7359 LY_ARRAY_FOR(sp->features, u) {
7360 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod->features);
7361 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7362 }
7363 lysc_update_path(&ctx, NULL, "{submodule}");
7364 LY_ARRAY_FOR(sp->includes, v) {
7365 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7366 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
7367 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod->features);
7368 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7369 }
7370 lysc_update_path(&ctx, NULL, NULL);
7371 }
7372 lysc_update_path(&ctx, NULL, NULL);
7373
7374 /* identities, work similarly to features with the precompilation */
Radek Krejci19a96102018-11-15 13:38:09 +01007375 if (sp->identities) {
Radek Krejci80d281e2020-09-14 17:42:54 +02007376 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007377 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007378 lysc_update_path(&ctx, NULL, "{submodule}");
7379 LY_ARRAY_FOR(sp->includes, v) {
7380 if (sp->includes[v].submodule->identities) {
7381 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci80d281e2020-09-14 17:42:54 +02007382 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod->identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02007383 LY_CHECK_GOTO(ret, error);
7384 lysc_update_path(&ctx, NULL, NULL);
7385 }
7386 }
Radek Krejci327de162019-06-14 12:52:07 +02007387 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007388
Radek Krejci95710c92019-02-11 15:49:55 +01007389 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007390 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007391 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007392 }
Radek Krejci95710c92019-02-11 15:49:55 +01007393
Radek Krejciec4da802019-05-02 13:02:41 +02007394 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7395 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007396
Radek Krejci95710c92019-02-11 15:49:55 +01007397 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007398 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007399 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007400 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007401 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007402
Radek Krejci474f9b82019-07-24 11:36:37 +02007403 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007404 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007405
Radek Krejci0935f412019-08-20 16:15:18 +02007406 /* extension instances TODO cover extension instances from submodules */
7407 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007408
Michal Vasko004d3152020-06-11 19:59:22 +02007409 /* finish compilation for all unresolved items in the context */
7410 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007411
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007412 /* validate non-instantiated groupings from the parsed schema,
7413 * without it we would accept even the schemas with invalid grouping specification */
7414 ctx.options |= LYSC_OPT_GROUPING;
7415 LY_ARRAY_FOR(sp->groupings, u) {
7416 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007417 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007418 }
7419 }
7420 LY_LIST_FOR(sp->data, node_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007421 grps = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007422 LY_ARRAY_FOR(grps, u) {
7423 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007424 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007425 }
7426 }
7427 }
7428
Radek Krejci474f9b82019-07-24 11:36:37 +02007429 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7430 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7431 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7432 }
7433
Michal Vasko8d544252020-03-02 10:19:52 +01007434#if 0
7435 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7436 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7437 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7438 * the anotation definitions available in the internal schema structure. */
7439 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7440 if (lyp_add_ietf_netconf_annotations(mod)) {
7441 lys_free(mod, NULL, 1, 1);
7442 return NULL;
7443 }
7444 }
7445#endif
7446
7447 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007448 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
7449 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007450 }
7451
Radek Krejci1c0c3442019-07-23 16:08:47 +02007452 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007453 ly_set_erase(&ctx.xpath, NULL);
7454 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007455 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007456 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007457 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007458
Radek Krejciec4da802019-05-02 13:02:41 +02007459 if (ctx.options & LYSC_OPT_FREE_SP) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02007460 lysp_module_free(mod->parsed);
7461 mod->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007462 }
7463
Radek Krejciec4da802019-05-02 13:02:41 +02007464 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007465 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007466 for (i = 0; i < ctx.ctx->list.count; ++i) {
7467 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007468 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007469 m->implemented = 1;
7470 }
7471 }
7472 }
7473
Radek Krejci19a96102018-11-15 13:38:09 +01007474 return LY_SUCCESS;
7475
7476error:
Michal Vasko7a0b0762020-09-02 16:37:01 +02007477 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007478 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007479 ly_set_erase(&ctx.xpath, NULL);
7480 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007481 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007482 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007483 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007484 lysc_module_free(mod_c, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02007485 mod->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007486
Radek Krejcia46012b2020-08-12 15:41:04 +02007487 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7488 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7489 * 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 +02007490 for (i = 0; i < ctx.ctx->list.count; ++i) {
7491 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007492 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007493 /* revert features list to the precompiled state */
7494 lys_feature_precompile_revert(&ctx, m);
7495 /* mark module as imported-only / not-implemented */
7496 m->implemented = 0;
7497 /* free the compiled version of the module */
7498 lysc_module_free(m->compiled, NULL);
7499 m->compiled = NULL;
7500 }
7501 }
7502
Radek Krejci19a96102018-11-15 13:38:09 +01007503 return ret;
7504}