blob: 8ebf744ac69481ce0801582611f080deccf82a43 [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 */
53#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010056 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020057 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
58 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010060 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejciec4da802019-05-02 13:02:41 +020066#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010067 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020068 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
69 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
70 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010071 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020072 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010073 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
74 } \
75 }
76
Radek Krejci0935f412019-08-20 16:15:18 +020077#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
78 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020079 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
80 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 +020081 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010082 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 +020083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010088 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020089 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
90 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
91 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010092 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020093 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010094 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
95 } \
96 }
97
Radek Krejciec4da802019-05-02 13:02:41 +020098#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010099 if (MEMBER_P) { \
100 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
101 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200102 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100103 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
104 }
105
Radek Krejciec4da802019-05-02 13:02:41 +0200106#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100107 if (MEMBER_P) { \
108 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200109 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100110 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200111 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100112 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
113 }
114
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100115#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100116 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200117 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100118 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100119 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
120 return LY_EVALID; \
121 } \
122 } \
123 }
124
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100125#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
126 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200127 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100128 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
129 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
130 return LY_EVALID; \
131 } \
132 } \
133 }
134
135struct lysc_ext *
136lysc_ext_dup(struct lysc_ext *orig)
137{
138 ++orig->refcount;
139 return orig;
140}
141
Radek Krejci19a96102018-11-15 13:38:09 +0100142static struct lysc_ext_instance *
143lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
144{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100145 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100146 (void) ctx;
147 (void) orig;
148 return NULL;
149}
150
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200151static LY_ERR
152lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +0200153 struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200154{
155 struct lysc_incomplete_dflt *r;
156 uint32_t i;
157
158 for (i = 0; i < ctx->dflts.count; ++i) {
159 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
160 if (r->leaf == leaf) {
161 /* just replace the default */
162 r->dflt = dflt;
163 return LY_SUCCESS;
164 }
165 }
166
167 r = malloc(sizeof *r);
168 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
169 r->leaf = leaf;
170 r->dflt = dflt;
171 r->dflts = NULL;
172 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200173 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200174
175 return LY_SUCCESS;
176}
177
Radek Krejcib56c7502019-02-13 14:19:54 +0100178/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200179 * @brief Add record into the compile context's list of incomplete default values.
180 * @param[in] ctx Compile context with the incomplete default values list.
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200181 * @param[in] term Term context node with the default value.
182 * @param[in] value String default value.
183 * @param[in] val_len Length of @p value.
Radek Krejci474f9b82019-07-24 11:36:37 +0200184 * @param[in] dflt_mod Module of the default value definition to store in the record.
185 * @return LY_EMEM in case of memory allocation failure.
186 * @return LY_SUCCESS
187 */
188static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200189lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts,
Radek Krejci0f969882020-08-21 16:56:47 +0200190 struct lys_module *dflt_mod)
Radek Krejci474f9b82019-07-24 11:36:37 +0200191{
192 struct lysc_incomplete_dflt *r;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200193 uint32_t i;
194
195 for (i = 0; i < ctx->dflts.count; ++i) {
196 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
197 if (r->llist == llist) {
198 /* just replace the defaults */
199 r->dflts = dflts;
200 return LY_SUCCESS;
201 }
202 }
203
Radek Krejci474f9b82019-07-24 11:36:37 +0200204 r = malloc(sizeof *r);
205 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200206 r->llist = llist;
207 r->dflt = NULL;
208 r->dflts = dflts;
Radek Krejci474f9b82019-07-24 11:36:37 +0200209 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200210 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Radek Krejci474f9b82019-07-24 11:36:37 +0200211
212 return LY_SUCCESS;
213}
214
215/**
216 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
217 * @param[in] ctx Compile context with the incomplete default values list.
218 * @param[in] dflt Incomplete default values identifying the record to remove.
219 */
220static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200221lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term)
Radek Krejci474f9b82019-07-24 11:36:37 +0200222{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200223 uint32_t u;
Radek Krejci474f9b82019-07-24 11:36:37 +0200224 struct lysc_incomplete_dflt *r;
225
226 for (u = 0; u < ctx->dflts.count; ++u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200227 r = ctx->dflts.objs[u];
228 if (r->leaf == (struct lysc_node_leaf *)term) {
Radek Krejci474f9b82019-07-24 11:36:37 +0200229 free(ctx->dflts.objs[u]);
230 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
231 --ctx->dflts.count;
232 return;
233 }
234 }
235}
236
Radek Krejci0e59c312019-08-15 15:34:15 +0200237void
Radek Krejci327de162019-06-14 12:52:07 +0200238lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
239{
240 int len;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200241 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
Radek Krejci327de162019-06-14 12:52:07 +0200242
243 if (!name) {
244 /* removing last path segment */
245 if (ctx->path[ctx->path_len - 1] == '}') {
Michal Vaskod989ba02020-08-24 10:59:24 +0200246 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200247 if (ctx->path[ctx->path_len] == '=') {
248 ctx->path[ctx->path_len++] = '}';
249 } else {
250 /* not a top-level special tag, remove also preceiding '/' */
251 goto remove_nodelevel;
252 }
253 } else {
254remove_nodelevel:
Michal Vaskod989ba02020-08-24 10:59:24 +0200255 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200256 if (ctx->path_len == 0) {
257 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200258 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200259 }
260 }
261 /* set new terminating NULL-byte */
262 ctx->path[ctx->path_len] = '\0';
263 } else {
264 if (ctx->path_len > 1) {
265 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
266 /* extension of the special tag */
267 nextlevel = 2;
268 --ctx->path_len;
269 } else {
270 /* there is already some path, so add next level */
271 nextlevel = 1;
272 }
273 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
274
275 if (nextlevel != 2) {
276 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
277 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200278 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200279 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200280 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 +0200281 }
282 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200283 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200284 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200285 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
Radek Krejciacc79042019-07-25 14:14:57 +0200286 /* output truncated */
287 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
288 } else {
289 ctx->path_len += len;
290 }
Radek Krejci327de162019-06-14 12:52:07 +0200291 }
292}
293
294/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100295 * @brief Duplicate the compiled pattern structure.
296 *
297 * Instead of duplicating memory, the reference counter in the @p orig is increased.
298 *
299 * @param[in] orig The pattern structure to duplicate.
300 * @return The duplicated structure to use.
301 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200302static struct lysc_pattern *
Radek Krejci19a96102018-11-15 13:38:09 +0100303lysc_pattern_dup(struct lysc_pattern *orig)
304{
305 ++orig->refcount;
306 return orig;
307}
308
Radek Krejcib56c7502019-02-13 14:19:54 +0100309/**
310 * @brief Duplicate the array of compiled patterns.
311 *
312 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
313 *
314 * @param[in] ctx Libyang context for logging.
315 * @param[in] orig The patterns sized array to duplicate.
316 * @return New sized array as a copy of @p orig.
317 * @return NULL in case of memory allocation error.
318 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200319static struct lysc_pattern **
Radek Krejci19a96102018-11-15 13:38:09 +0100320lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
321{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100322 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200323 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100324
Radek Krejcib56c7502019-02-13 14:19:54 +0100325 assert(orig);
326
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200327 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100328 LY_ARRAY_FOR(orig, u) {
329 dup[u] = lysc_pattern_dup(orig[u]);
330 LY_ARRAY_INCREMENT(dup);
331 }
332 return dup;
333}
334
Radek Krejcib56c7502019-02-13 14:19:54 +0100335/**
336 * @brief Duplicate compiled range structure.
337 *
338 * @param[in] ctx Libyang context for logging.
339 * @param[in] orig The range structure to be duplicated.
340 * @return New compiled range structure as a copy of @p orig.
341 * @return NULL in case of memory allocation error.
342 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200343struct lysc_range *
Radek Krejci19a96102018-11-15 13:38:09 +0100344lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
345{
346 struct lysc_range *dup;
347 LY_ERR ret;
348
Radek Krejcib56c7502019-02-13 14:19:54 +0100349 assert(orig);
350
Radek Krejci19a96102018-11-15 13:38:09 +0100351 dup = calloc(1, sizeof *dup);
352 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
353 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200354 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
355 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
356 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100357 }
358 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
359 DUP_STRING(ctx, orig->emsg, dup->emsg);
360 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
361
362 return dup;
363cleanup:
364 free(dup);
365 (void) ret; /* set but not used due to the return type */
366 return NULL;
367}
368
Radek Krejcib56c7502019-02-13 14:19:54 +0100369/**
370 * @brief Stack for processing if-feature expressions.
371 */
Radek Krejci19a96102018-11-15 13:38:09 +0100372struct iff_stack {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200373 size_t size; /**< number of items in the stack */
374 size_t index; /**< first empty item */
375 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100376};
377
Radek Krejcib56c7502019-02-13 14:19:54 +0100378/**
379 * @brief Add @ref ifftokens into the stack.
380 * @param[in] stack The if-feature stack to use.
381 * @param[in] value One of the @ref ifftokens to store in the stack.
382 * @return LY_EMEM in case of memory allocation error
383 * @return LY_ESUCCESS if the value successfully stored.
384 */
Radek Krejci19a96102018-11-15 13:38:09 +0100385static LY_ERR
386iff_stack_push(struct iff_stack *stack, uint8_t value)
387{
388 if (stack->index == stack->size) {
389 stack->size += 4;
390 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
391 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
392 }
393 stack->stack[stack->index++] = value;
394 return LY_SUCCESS;
395}
396
Radek Krejcib56c7502019-02-13 14:19:54 +0100397/**
398 * @brief Get (and remove) the last item form the stack.
399 * @param[in] stack The if-feature stack to use.
400 * @return The value from the top of the stack.
401 */
Radek Krejci19a96102018-11-15 13:38:09 +0100402static uint8_t
403iff_stack_pop(struct iff_stack *stack)
404{
Radek Krejcib56c7502019-02-13 14:19:54 +0100405 assert(stack && stack->index);
406
Radek Krejci19a96102018-11-15 13:38:09 +0100407 stack->index--;
408 return stack->stack[stack->index];
409}
410
Radek Krejcib56c7502019-02-13 14:19:54 +0100411/**
412 * @brief Clean up the stack.
413 * @param[in] stack The if-feature stack to use.
414 */
Radek Krejci19a96102018-11-15 13:38:09 +0100415static void
416iff_stack_clean(struct iff_stack *stack)
417{
418 stack->size = 0;
419 free(stack->stack);
420}
421
Radek Krejcib56c7502019-02-13 14:19:54 +0100422/**
423 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
424 * (libyang format of the if-feature expression).
425 * @param[in,out] list The 2bits array to modify.
426 * @param[in] op The operand (@ref ifftokens) to store.
427 * @param[in] pos Position (0-based) where to store the given @p op.
428 */
Radek Krejci19a96102018-11-15 13:38:09 +0100429static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200430iff_setop(uint8_t *list, uint8_t op, size_t pos)
Radek Krejci19a96102018-11-15 13:38:09 +0100431{
432 uint8_t *item;
433 uint8_t mask = 3;
434
Radek Krejci19a96102018-11-15 13:38:09 +0100435 assert(op <= 3); /* max 2 bits */
436
437 item = &list[pos / 4];
438 mask = mask << 2 * (pos % 4);
439 *item = (*item) & ~mask;
440 *item = (*item) | (op << 2 * (pos % 4));
441}
442
Radek Krejcib56c7502019-02-13 14:19:54 +0100443#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
444#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100445
Radek Krejci0af46292019-01-11 16:02:31 +0100446/**
447 * @brief Find a feature of the given name and referenced in the given module.
448 *
449 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
450 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
451 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
452 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
453 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
454 * assigned till that time will be still valid.
455 *
456 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
457 * @param[in] name Name of the feature including possible prefix.
458 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
459 * @return Pointer to the feature structure if found, NULL otherwise.
460 */
Radek Krejci19a96102018-11-15 13:38:09 +0100461static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100462lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100463{
464 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200465 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100466 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100467
Radek Krejci120d8542020-08-12 09:29:16 +0200468 assert(mod);
469
Radek Krejci19a96102018-11-15 13:38:09 +0100470 for (i = 0; i < len; ++i) {
471 if (name[i] == ':') {
472 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100473 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100474 LY_CHECK_RET(!mod, NULL);
475
476 name = &name[i + 1];
477 len = len - i - 1;
478 }
479 }
480
481 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100482 if (mod->implemented) {
483 /* module is implemented so there is already the compiled schema */
484 flist = mod->compiled->features;
485 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200486 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100487 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200488 LY_ARRAY_FOR(flist, u) {
489 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200490 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100491 return f;
492 }
493 }
494
495 return NULL;
496}
497
Michal Vasko8d544252020-03-02 10:19:52 +0100498/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100499 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
500 */
501static LY_ERR
502lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
503{
504 LY_ERR ret = LY_SUCCESS;
505
506 if (!ext_p->compiled) {
507 lysc_update_path(ctx, NULL, "{extension}");
508 lysc_update_path(ctx, NULL, ext_p->name);
509
510 /* compile the extension definition */
511 ext_p->compiled = calloc(1, sizeof **ext);
512 ext_p->compiled->refcount = 1;
513 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
514 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
515 ext_p->compiled->module = (struct lys_module *)ext_mod;
516 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
517
518 lysc_update_path(ctx, NULL, NULL);
519 lysc_update_path(ctx, NULL, NULL);
520
521 /* find extension definition plugin */
522 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
523 }
524
525 *ext = lysc_ext_dup(ext_p->compiled);
526
527done:
528 return ret;
529}
530
531/**
Michal Vasko8d544252020-03-02 10:19:52 +0100532 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
533 *
534 * @param[in] ctx Compilation context.
535 * @param[in] ext_p Parsed extension instance.
536 * @param[in,out] ext Prepared compiled extension instance.
537 * @param[in] parent Extension instance parent.
538 * @param[in] parent_type Extension instance parent type.
539 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
540 */
Radek Krejci19a96102018-11-15 13:38:09 +0100541static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100542lys_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 +0200543 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100544{
Radek Krejci7c960162019-09-18 14:16:12 +0200545 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100546 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200547 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200548 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200549 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100550
551 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
552 ext->insubstmt = ext_p->insubstmt;
553 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200554 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200555 ext->parent = parent;
556 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100557
Michal Vasko22df3f02020-08-24 13:29:22 +0200558 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200559
Radek Krejci19a96102018-11-15 13:38:09 +0100560 /* get module where the extension definition should be placed */
Radek Krejci1e008d22020-08-17 11:37:37 +0200561 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
Radek Krejci7c960162019-09-18 14:16:12 +0200562 if (ext_p->yin) {
563 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
564 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
565 * YANG (import) prefix must be done here. */
566 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
567 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
568 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200569 } else {
570 assert(ctx->mod_def->parsed);
571 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
572 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200573 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200574 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 +0200575 ret = LY_EMEM, cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200576 prefixed_name = lydict_insert_zc(ctx->ctx, s);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200577 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200578 break;
579 }
580 }
581 }
582 if (!prefixed_name) {
583 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
584 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
585 goto cleanup;
586 }
587 } else {
588 prefixed_name = ext_p->name;
589 }
590 lysc_update_path(ctx, NULL, prefixed_name);
591
Michal Vasko8d544252020-03-02 10:19:52 +0100592 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200593 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100594 if (!ext_mod) {
595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
596 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
597 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);
602 goto cleanup;
603 }
Radek Krejci7c960162019-09-18 14:16:12 +0200604 }
605 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200606
Michal Vasko5fe75f12020-03-02 13:52:37 +0100607 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200608 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
609 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100610 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200611 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100612 break;
613 }
614 }
Radek Krejci7c960162019-09-18 14:16:12 +0200615 if (!ext->def) {
616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
617 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
618 goto cleanup;
619 }
Radek Krejci0935f412019-08-20 16:15:18 +0200620
Radek Krejcif56e2a42019-09-09 14:15:25 +0200621 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
622 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
623 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200624 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200625 /* Schema was parsed from YIN and an argument is expected, ... */
626 struct lysp_stmt *stmt = NULL;
627
628 if (ext->def->flags & LYS_YINELEM_TRUE) {
629 /* ... argument was the first XML child element */
630 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
631 /* TODO check namespace of the statement */
632 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
633 stmt = ext_p->child;
634 }
635 }
636 } else {
637 /* ... argument was one of the XML attributes which are represented as child stmt
638 * with LYS_YIN_ATTR flag */
639 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
640 if (!strcmp(stmt->stmt, ext->def->argument)) {
641 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200642 break;
643 }
644 }
645 }
646 if (!stmt) {
647 /* missing extension's argument */
648 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200649 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
650 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200651
652 }
653 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
654 stmt->flags |= LYS_YIN_ARGUMENT;
655 }
Radek Krejci7c960162019-09-18 14:16:12 +0200656 if (prefixed_name != ext_p->name) {
657 lydict_remove(ctx->ctx, ext_p->name);
658 ext_p->name = prefixed_name;
659 if (!ext_p->argument && ext->argument) {
660 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
661 }
662 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200663
Radek Krejci0935f412019-08-20 16:15:18 +0200664 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200665 if (ext->argument) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200666 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
Radek Krejciad5963b2019-09-06 16:03:05 +0200667 }
Radek Krejci7c960162019-09-18 14:16:12 +0200668 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200669 if (ext->argument) {
670 lysc_update_path(ctx, NULL, NULL);
671 }
Radek Krejci0935f412019-08-20 16:15:18 +0200672 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200673 ext_p->compiled = ext;
674
Radek Krejci7c960162019-09-18 14:16:12 +0200675 ret = LY_SUCCESS;
676cleanup:
677 if (prefixed_name && prefixed_name != ext_p->name) {
678 lydict_remove(ctx->ctx, prefixed_name);
679 }
680
Radek Krejcif56e2a42019-09-09 14:15:25 +0200681 lysc_update_path(ctx, NULL, NULL);
682 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200683
Radek Krejci7c960162019-09-18 14:16:12 +0200684 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200685}
686
687/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100688 * @brief Compile information from the if-feature statement
689 * @param[in] ctx Compile context.
690 * @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 +0100691 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
692 * @return LY_ERR value.
693 */
Radek Krejci19a96102018-11-15 13:38:09 +0100694static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200695lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100696{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200697 LY_ERR rc = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100698 const char *c = *value;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200699 int64_t i, j;
700 int8_t op_len, last_not = 0, checkversion = 0;
701 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci19a96102018-11-15 13:38:09 +0100702 uint8_t op;
703 struct iff_stack stack = {0, 0, NULL};
704 struct lysc_feature *f;
705
706 assert(c);
707
708 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200709 for (i = j = 0; c[i]; i++) {
Radek Krejci19a96102018-11-15 13:38:09 +0100710 if (c[i] == '(') {
711 j++;
712 checkversion = 1;
713 continue;
714 } else if (c[i] == ')') {
715 j--;
716 continue;
717 } else if (isspace(c[i])) {
718 checkversion = 1;
719 continue;
720 }
721
Radek Krejci1deb5be2020-08-26 16:43:36 +0200722 if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
723 uint64_t spaces;
724 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++);
725 if (c[i + op_len + spaces] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100726 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
727 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
728 return LY_EVALID;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200729 } else if (!isspace(c[i + op_len])) {
Radek Krejci19a96102018-11-15 13:38:09 +0100730 /* feature name starting with the not/and/or */
731 last_not = 0;
732 f_size++;
733 } else if (c[i] == 'n') { /* not operation */
734 if (last_not) {
735 /* double not */
736 expr_size = expr_size - 2;
737 last_not = 0;
738 } else {
739 last_not = 1;
740 }
741 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200742 if (f_exp != f_size) {
743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200744 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, op_len, &c[i]);
Radek Krejci6788abc2019-06-14 13:56:49 +0200745 return LY_EVALID;
746 }
Radek Krejci19a96102018-11-15 13:38:09 +0100747 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200748
Radek Krejci19a96102018-11-15 13:38:09 +0100749 /* not a not operation */
750 last_not = 0;
751 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200752 i += op_len;
Radek Krejci19a96102018-11-15 13:38:09 +0100753 } else {
754 f_size++;
755 last_not = 0;
756 }
757 expr_size++;
758
759 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200760 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100761 i--;
762 break;
763 }
764 i++;
765 }
766 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200767 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100768 /* not matching count of ( and ) */
769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
770 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
771 return LY_EVALID;
772 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200773 if (f_exp != f_size) {
774 /* features do not match the needed arguments for the logical operations */
775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
776 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
777 "the required number of operands for the operations.", *value);
778 return LY_EVALID;
779 }
Radek Krejci19a96102018-11-15 13:38:09 +0100780
781 if (checkversion || expr_size > 1) {
782 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100783 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100784 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
785 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
786 return LY_EVALID;
787 }
788 }
789
790 /* allocate the memory */
791 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
792 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
793 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200794 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
Radek Krejci19a96102018-11-15 13:38:09 +0100795
796 stack.size = expr_size;
797 f_size--; expr_size--; /* used as indexes from now */
798
799 for (i--; i >= 0; i--) {
800 if (c[i] == ')') {
801 /* push it on stack */
802 iff_stack_push(&stack, LYS_IFF_RP);
803 continue;
804 } else if (c[i] == '(') {
805 /* pop from the stack into result all operators until ) */
Michal Vaskod989ba02020-08-24 10:59:24 +0200806 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
Radek Krejci19a96102018-11-15 13:38:09 +0100807 iff_setop(iff->expr, op, expr_size--);
808 }
809 continue;
810 } else if (isspace(c[i])) {
811 continue;
812 }
813
814 /* end of operator or operand -> find beginning and get what is it */
815 j = i + 1;
816 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
817 i--;
818 }
819 i++; /* go back by one step */
820
821 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
822 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
823 /* double not */
824 iff_stack_pop(&stack);
825 } else {
826 /* not has the highest priority, so do not pop from the stack
827 * as in case of AND and OR */
828 iff_stack_push(&stack, LYS_IFF_NOT);
829 }
830 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
831 /* as for OR - pop from the stack all operators with the same or higher
832 * priority and store them to the result, then push the AND to the stack */
833 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
834 op = iff_stack_pop(&stack);
835 iff_setop(iff->expr, op, expr_size--);
836 }
837 iff_stack_push(&stack, LYS_IFF_AND);
838 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
839 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
840 op = iff_stack_pop(&stack);
841 iff_setop(iff->expr, op, expr_size--);
842 }
843 iff_stack_push(&stack, LYS_IFF_OR);
844 } else {
845 /* feature name, length is j - i */
846
847 /* add it to the expression */
848 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
849
850 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100851 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
852 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
853 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
854 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100855 iff->features[f_size] = f;
856 LY_ARRAY_INCREMENT(iff->features);
857 f_size--;
858 }
859 }
860 while (stack.index) {
861 op = iff_stack_pop(&stack);
862 iff_setop(iff->expr, op, expr_size--);
863 }
864
865 if (++expr_size || ++f_size) {
866 /* not all expected operators and operands found */
867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
868 "Invalid value \"%s\" of if-feature - processing error.", *value);
869 rc = LY_EINT;
870 } else {
871 rc = LY_SUCCESS;
872 }
873
874error:
875 /* cleanup */
876 iff_stack_clean(&stack);
877
878 return rc;
879}
880
Radek Krejcib56c7502019-02-13 14:19:54 +0100881/**
Michal Vasko175012e2019-11-06 15:49:14 +0100882 * @brief Get the XPath context node for the given schema node.
883 * @param[in] start The schema node where the XPath expression appears.
884 * @return The context node to evaluate XPath expression in given schema node.
885 * @return NULL in case the context node is the root node.
886 */
887static struct lysc_node *
888lysc_xpath_context(struct lysc_node *start)
889{
Michal Vasko1bf09392020-03-27 12:38:10 +0100890 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 +0200891 start = start->parent) {}
Michal Vasko175012e2019-11-06 15:49:14 +0100892 return start;
893}
894
895/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100896 * @brief Compile information from the when statement
897 * @param[in] ctx Compile context.
898 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100899 * @param[in] flags Flags of the node with the "when" defiition.
900 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100901 * @param[out] when Pointer where to store pointer to the created compiled when structure.
902 * @return LY_ERR value.
903 */
Radek Krejci19a96102018-11-15 13:38:09 +0100904static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100905lys_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 +0100906{
Radek Krejci19a96102018-11-15 13:38:09 +0100907 LY_ERR ret = LY_SUCCESS;
908
Radek Krejci00b874b2019-02-12 10:54:50 +0100909 *when = calloc(1, sizeof **when);
910 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200911 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200912 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100913 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100914 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
915 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
916 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200917 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100918 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100919
920done:
921 return ret;
922}
923
Radek Krejcib56c7502019-02-13 14:19:54 +0100924/**
925 * @brief Compile information from the must statement
926 * @param[in] ctx Compile context.
927 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100928 * @param[in,out] must Prepared (empty) compiled must structure to fill.
929 * @return LY_ERR value.
930 */
Radek Krejci19a96102018-11-15 13:38:09 +0100931static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200932lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100933{
Radek Krejci19a96102018-11-15 13:38:09 +0100934 LY_ERR ret = LY_SUCCESS;
935
Michal Vasko004d3152020-06-11 19:59:22 +0200936 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100937 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200938 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100939 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
940 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100941 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
942 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200943 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100944
945done:
946 return ret;
947}
948
Radek Krejcib56c7502019-02-13 14:19:54 +0100949/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200950 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100951 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200952 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100953 * @return LY_ERR value.
954 */
Radek Krejci19a96102018-11-15 13:38:09 +0100955static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200956lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100957{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200958 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100959 LY_ERR ret = LY_SUCCESS;
960
Radek Krejci7f2a5362018-11-28 13:05:37 +0100961 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
962 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100963 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200964 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100965 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200966 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200967 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200968 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200969 LOGINT(ctx->ctx);
970 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200971 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 +0200972 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200973 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200974 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200975 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200976 mod = NULL;
977 }
Radek Krejci19a96102018-11-15 13:38:09 +0100978 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200979 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100980 }
981 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200982 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 +0100983 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 +0200984 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100985 return LY_ENOTFOUND;
986 }
987 }
Radek Krejci19a96102018-11-15 13:38:09 +0100988 }
989
Radek Krejci19a96102018-11-15 13:38:09 +0100990 return ret;
991}
992
Michal Vasko33ff9422020-07-03 09:50:39 +0200993LY_ERR
994lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +0200995 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100996{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200997 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200998 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +0100999 LY_ERR ret = LY_SUCCESS;
1000
Michal Vasko33ff9422020-07-03 09:50:39 +02001001 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +02001002
Michal Vasko33ff9422020-07-03 09:50:39 +02001003 if (!ctx_sc) {
1004 context.ctx = ctx;
1005 context.mod = module;
Radek Krejci120d8542020-08-12 09:29:16 +02001006 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +02001007 context.path_len = 1;
1008 context.path[0] = '/';
1009 ctx_sc = &context;
1010 }
Radek Krejci19a96102018-11-15 13:38:09 +01001011
Michal Vasko33ff9422020-07-03 09:50:39 +02001012 if (!identities_p) {
1013 return LY_SUCCESS;
1014 }
1015 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001016 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02001017 }
1018
1019 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001020 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001021 LY_ARRAY_FOR(identities_p, u) {
1022 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
1023
1024 LY_ARRAY_INCREMENT(*identities);
1025 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
1026 DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
1027 DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
1028 DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
1029 (*identities)[offset + u].module = ctx_sc->mod;
1030 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
1031 lys_compile_iffeature, ret, done);
1032 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
1033 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
1034 LYEXT_PAR_IDENT, ret, done);
1035 (*identities)[offset + u].flags = identities_p[u].flags;
1036
1037 lysc_update_path(ctx_sc, NULL, NULL);
1038 }
1039 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001040done:
1041 return ret;
1042}
1043
Radek Krejcib56c7502019-02-13 14:19:54 +01001044/**
1045 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1046 *
1047 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1048 *
1049 * @param[in] ctx Compile context for logging.
1050 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1051 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1052 * @return LY_SUCCESS if everything is ok.
1053 * @return LY_EVALID if the identity is derived from itself.
1054 */
Radek Krejci38222632019-02-12 16:55:05 +01001055static LY_ERR
1056lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1057{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001058 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001059 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001060 struct ly_set recursion = {0};
1061 struct lysc_ident *drv;
1062
1063 if (!derived) {
1064 return LY_SUCCESS;
1065 }
1066
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001067 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001068 if (ident == derived[u]) {
1069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1070 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001071 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001072 goto cleanup;
1073 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001074 ret = ly_set_add(&recursion, derived[u], 0, NULL);
1075 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001076 }
1077
1078 for (v = 0; v < recursion.count; ++v) {
1079 drv = recursion.objs[v];
1080 if (!drv->derived) {
1081 continue;
1082 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001083 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001084 if (ident == drv->derived[u]) {
1085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1086 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001087 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001088 goto cleanup;
1089 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001090 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
1091 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001092 }
1093 }
Radek Krejci38222632019-02-12 16:55:05 +01001094
1095cleanup:
1096 ly_set_erase(&recursion, NULL);
1097 return ret;
1098}
1099
Radek Krejcia3045382018-11-22 14:30:31 +01001100/**
1101 * @brief Find and process the referenced base identities from another identity or identityref
1102 *
Radek Krejciaca74032019-06-04 08:53:06 +02001103 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001104 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1105 * to distinguish these two use cases.
1106 *
1107 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1108 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001109 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001110 * @param[in] bases Array of bases of identityref to fill in.
1111 * @return LY_ERR value.
1112 */
Radek Krejci19a96102018-11-15 13:38:09 +01001113static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001114lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001115 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001116{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001117 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001118 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001119 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001120 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001121
1122 assert(ident || bases);
1123
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001124 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1126 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1127 return LY_EVALID;
1128 }
1129
Michal Vasko33ff9422020-07-03 09:50:39 +02001130 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001131 s = strchr(bases_p[u], ':');
1132 if (s) {
1133 /* prefixed identity */
1134 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001135 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001136 } else {
1137 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001138 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001139 }
1140 if (!mod) {
1141 if (ident) {
1142 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1143 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1144 } else {
1145 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1146 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1147 }
1148 return LY_EVALID;
1149 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001150
Radek Krejci555cb5b2018-11-16 14:54:33 +01001151 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001152 if (mod->compiled) {
1153 identities = mod->compiled->identities;
1154 } else {
1155 identities = mod->dis_identities;
1156 }
1157 LY_ARRAY_FOR(identities, v) {
1158 if (!strcmp(name, identities[v].name)) {
1159 if (ident) {
1160 if (ident == &identities[v]) {
1161 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1162 "Identity \"%s\" is derived from itself.", ident->name);
1163 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001164 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001165 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1166 /* we have match! store the backlink */
1167 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1168 *idref = ident;
1169 } else {
1170 /* we have match! store the found identity */
1171 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1172 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001173 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001174 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001175 }
1176 }
1177 if (!idref || !(*idref)) {
1178 if (ident) {
1179 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1180 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1181 } else {
1182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1183 "Unable to find base (%s) of identityref.", bases_p[u]);
1184 }
1185 return LY_EVALID;
1186 }
1187 }
1188 return LY_SUCCESS;
1189}
1190
Radek Krejcia3045382018-11-22 14:30:31 +01001191/**
1192 * @brief For the given array of identities, set the backlinks from all their base identities.
1193 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1194 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1195 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1196 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1197 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001198static LY_ERR
1199lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1200{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001201 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001202
Michal Vasko33ff9422020-07-03 09:50:39 +02001203 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001204 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001205 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001206 continue;
1207 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001208 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001209 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 +02001210 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001211 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001212 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001213 return LY_SUCCESS;
1214}
1215
Radek Krejci0af46292019-01-11 16:02:31 +01001216LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001217lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001218 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001219{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001220 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001221 struct lysc_ctx context = {0};
1222
Radek Krejci327de162019-06-14 12:52:07 +02001223 assert(ctx_sc || ctx);
1224
1225 if (!ctx_sc) {
1226 context.ctx = ctx;
1227 context.mod = module;
1228 context.path_len = 1;
1229 context.path[0] = '/';
1230 ctx_sc = &context;
1231 }
Radek Krejci0af46292019-01-11 16:02:31 +01001232
1233 if (!features_p) {
1234 return LY_SUCCESS;
1235 }
1236 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001237 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001238 }
1239
Radek Krejci327de162019-06-14 12:52:07 +02001240 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001241 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001242 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001243 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1244
Radek Krejci0af46292019-01-11 16:02:31 +01001245 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001246 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001247 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1248 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1249 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001250 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001251 (*features)[offset + u].module = ctx_sc->mod;
1252
1253 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001254 }
Radek Krejci327de162019-06-14 12:52:07 +02001255 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001256
1257 return LY_SUCCESS;
1258}
1259
Radek Krejcia3045382018-11-22 14:30:31 +01001260/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001261 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001262 *
1263 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1264 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001265 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001266 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1267 * being currently processed).
1268 * @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 +01001269 * @return LY_SUCCESS if everything is ok.
1270 * @return LY_EVALID if the feature references indirectly itself.
1271 */
1272static LY_ERR
1273lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1274{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001275 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001276 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001277 struct ly_set recursion = {0};
1278 struct lysc_feature *drv;
1279
1280 if (!depfeatures) {
1281 return LY_SUCCESS;
1282 }
1283
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001284 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001285 if (feature == depfeatures[u]) {
1286 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1287 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001288 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001289 goto cleanup;
1290 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001291 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
1292 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001293 }
1294
1295 for (v = 0; v < recursion.count; ++v) {
1296 drv = recursion.objs[v];
1297 if (!drv->depfeatures) {
1298 continue;
1299 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001300 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001301 if (feature == drv->depfeatures[u]) {
1302 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1303 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001304 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001305 goto cleanup;
1306 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001307 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
1308 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001309 }
1310 }
Radek Krejci09a1fc52019-02-13 10:55:17 +01001311
1312cleanup:
1313 ly_set_erase(&recursion, NULL);
1314 return ret;
1315}
1316
1317/**
Radek Krejci0af46292019-01-11 16:02:31 +01001318 * @brief Create pre-compiled features array.
1319 *
1320 * See lys_feature_precompile() for more details.
1321 *
Radek Krejcia3045382018-11-22 14:30:31 +01001322 * @param[in] ctx Compile context.
1323 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001324 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001325 * @return LY_ERR value.
1326 */
Radek Krejci19a96102018-11-15 13:38:09 +01001327static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001328lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001329{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001330 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001331 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001332 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001333
Radek Krejci0af46292019-01-11 16:02:31 +01001334 /* find the preprecompiled feature */
1335 LY_ARRAY_FOR(features, x) {
1336 if (strcmp(features[x].name, feature_p->name)) {
1337 continue;
1338 }
1339 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001340 lysc_update_path(ctx, NULL, "{feature}");
1341 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001342
Radek Krejci0af46292019-01-11 16:02:31 +01001343 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001344 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001345 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001346 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001347 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001348 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001349 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001350 /* check for circular dependency - direct reference first,... */
1351 if (feature == feature->iffeatures[u].features[v]) {
1352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1353 "Feature \"%s\" is referenced from itself.", feature->name);
1354 return LY_EVALID;
1355 }
1356 /* ... and indirect circular reference */
1357 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1358
Radek Krejci0af46292019-01-11 16:02:31 +01001359 /* add itself into the dependants list */
1360 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1361 *df = feature;
1362 }
Radek Krejci19a96102018-11-15 13:38:09 +01001363 }
Radek Krejci19a96102018-11-15 13:38:09 +01001364 }
1365 }
Radek Krejci327de162019-06-14 12:52:07 +02001366 lysc_update_path(ctx, NULL, NULL);
1367 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001368 done:
1369 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001370 }
Radek Krejci0af46292019-01-11 16:02:31 +01001371
1372 LOGINT(ctx->ctx);
1373 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001374}
1375
Radek Krejcib56c7502019-02-13 14:19:54 +01001376/**
1377 * @brief Revert compiled list of features back to the precompiled state.
1378 *
1379 * 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 +02001380 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001381 *
1382 * @param[in] ctx Compilation context.
1383 * @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 +02001384 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001385 */
Radek Krejci95710c92019-02-11 15:49:55 +01001386static void
1387lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1388{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001389 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001390
Radek Krejcia46012b2020-08-12 15:41:04 +02001391 if (mod->compiled) {
1392 /* keep the dis_features list until the complete lys_module is freed */
1393 mod->dis_features = mod->compiled->features;
1394 mod->compiled->features = NULL;
1395 }
Radek Krejci95710c92019-02-11 15:49:55 +01001396
Michal Vasko33ff9422020-07-03 09:50:39 +02001397 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001398 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001399 LY_ARRAY_FOR(mod->dis_features, u) {
1400 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1401 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001402 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001403 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1404 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001405
Michal Vasko33ff9422020-07-03 09:50:39 +02001406 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1407 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001408 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001409 LY_ARRAY_FREE(mod->dis_features[u].exts);
1410 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001411 }
1412}
1413
Radek Krejcia3045382018-11-22 14:30:31 +01001414/**
1415 * @brief Validate and normalize numeric value from a range definition.
1416 * @param[in] ctx Compile context.
1417 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1418 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1419 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1420 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1421 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1422 * @param[in] value String value of the range boundary.
1423 * @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.
1424 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1425 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1426 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001427LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001428range_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 +01001429{
Radek Krejci6cba4292018-11-15 17:33:29 +01001430 size_t fraction = 0, size;
1431
Radek Krejci19a96102018-11-15 13:38:09 +01001432 *len = 0;
1433
1434 assert(value);
1435 /* parse value */
1436 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1437 return LY_EVALID;
1438 }
1439
1440 if ((value[*len] == '-') || (value[*len] == '+')) {
1441 ++(*len);
1442 }
1443
1444 while (isdigit(value[*len])) {
1445 ++(*len);
1446 }
1447
1448 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001449 if (basetype == LY_TYPE_DEC64) {
1450 goto decimal;
1451 } else {
1452 *valcopy = strndup(value, *len);
1453 return LY_SUCCESS;
1454 }
Radek Krejci19a96102018-11-15 13:38:09 +01001455 }
1456 fraction = *len;
1457
1458 ++(*len);
1459 while (isdigit(value[*len])) {
1460 ++(*len);
1461 }
1462
Radek Krejci6cba4292018-11-15 17:33:29 +01001463 if (basetype == LY_TYPE_DEC64) {
1464decimal:
1465 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001466 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001467 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1468 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1469 *len, value, frdigits);
1470 return LY_EINVAL;
1471 }
1472 if (fraction) {
1473 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1474 } else {
1475 size = (*len) + frdigits + 1;
1476 }
1477 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001478 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1479
Radek Krejci6cba4292018-11-15 17:33:29 +01001480 (*valcopy)[size - 1] = '\0';
1481 if (fraction) {
1482 memcpy(&(*valcopy)[0], &value[0], fraction);
1483 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1484 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1485 } else {
1486 memcpy(&(*valcopy)[0], &value[0], *len);
1487 memset(&(*valcopy)[*len], '0', frdigits);
1488 }
Radek Krejci19a96102018-11-15 13:38:09 +01001489 }
1490 return LY_SUCCESS;
1491}
1492
Radek Krejcia3045382018-11-22 14:30:31 +01001493/**
1494 * @brief Check that values in range are in ascendant order.
1495 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001496 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1497 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001498 * @param[in] value Current value to check.
1499 * @param[in] prev_value The last seen value.
1500 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1501 */
Radek Krejci19a96102018-11-15 13:38:09 +01001502static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001503range_part_check_ascendancy(uint8_t unsigned_value, uint8_t max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001504{
1505 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001506 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001507 return LY_EEXIST;
1508 }
1509 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001510 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001511 return LY_EEXIST;
1512 }
1513 }
1514 return LY_SUCCESS;
1515}
1516
Radek Krejcia3045382018-11-22 14:30:31 +01001517/**
1518 * @brief Set min/max value of the range part.
1519 * @param[in] ctx Compile context.
1520 * @param[in] part Range part structure to fill.
1521 * @param[in] max Flag to distinguish if storing min or max value.
1522 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1523 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1524 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1525 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1526 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001527 * @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 +01001528 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1529 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1530 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1531 * frdigits value), LY_EMEM.
1532 */
Radek Krejci19a96102018-11-15 13:38:09 +01001533static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001534range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, uint8_t max, int64_t prev, LY_DATA_TYPE basetype,
1535 uint8_t first, uint8_t length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001536{
1537 LY_ERR ret = LY_SUCCESS;
1538 char *valcopy = NULL;
1539 size_t len;
1540
1541 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001542 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001543 LY_CHECK_GOTO(ret, finalize);
1544 }
1545 if (!valcopy && base_range) {
1546 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001547 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001548 } else {
1549 part->min_64 = base_range->parts[0].min_64;
1550 }
1551 if (!first) {
1552 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1553 }
1554 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001555 }
1556
1557 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001558 case LY_TYPE_INT8: /* range */
1559 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001560 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 +01001561 } else if (max) {
1562 part->max_64 = INT64_C(127);
1563 } else {
1564 part->min_64 = INT64_C(-128);
1565 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001566 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001567 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001568 }
1569 break;
1570 case LY_TYPE_INT16: /* range */
1571 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001572 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 +01001573 } else if (max) {
1574 part->max_64 = INT64_C(32767);
1575 } else {
1576 part->min_64 = INT64_C(-32768);
1577 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001578 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001579 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001580 }
1581 break;
1582 case LY_TYPE_INT32: /* range */
1583 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001584 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 +01001585 } else if (max) {
1586 part->max_64 = INT64_C(2147483647);
1587 } else {
1588 part->min_64 = INT64_C(-2147483648);
1589 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001590 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001591 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001592 }
1593 break;
1594 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001595 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001596 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001597 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001598 max ? &part->max_64 : &part->min_64);
1599 } else if (max) {
1600 part->max_64 = INT64_C(9223372036854775807);
1601 } else {
1602 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1603 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001604 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001605 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001606 }
1607 break;
1608 case LY_TYPE_UINT8: /* range */
1609 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001610 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 +01001611 } else if (max) {
1612 part->max_u64 = UINT64_C(255);
1613 } else {
1614 part->min_u64 = UINT64_C(0);
1615 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001616 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001617 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001618 }
1619 break;
1620 case LY_TYPE_UINT16: /* range */
1621 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001622 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 +01001623 } else if (max) {
1624 part->max_u64 = UINT64_C(65535);
1625 } else {
1626 part->min_u64 = UINT64_C(0);
1627 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001628 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001629 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001630 }
1631 break;
1632 case LY_TYPE_UINT32: /* range */
1633 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001634 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 +01001635 } else if (max) {
1636 part->max_u64 = UINT64_C(4294967295);
1637 } else {
1638 part->min_u64 = UINT64_C(0);
1639 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001640 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001641 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001642 }
1643 break;
1644 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001645 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001646 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001647 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001648 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 +01001649 } else if (max) {
1650 part->max_u64 = UINT64_C(18446744073709551615);
1651 } else {
1652 part->min_u64 = UINT64_C(0);
1653 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001654 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001655 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001656 }
1657 break;
1658 default:
1659 LOGINT(ctx->ctx);
1660 ret = LY_EINT;
1661 }
1662
Radek Krejci5969f272018-11-23 10:03:58 +01001663finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001664 if (ret == LY_EDENIED) {
1665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1666 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1667 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1668 } else if (ret == LY_EVALID) {
1669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1670 "Invalid %s restriction - invalid value \"%s\".",
1671 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1672 } else if (ret == LY_EEXIST) {
1673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1674 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001675 length_restr ? "length" : "range",
Radek Krejci0f969882020-08-21 16:56:47 +02001676 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001677 } else if (!ret && value) {
1678 *value = *value + len;
1679 }
1680 free(valcopy);
1681 return ret;
1682}
1683
Radek Krejcia3045382018-11-22 14:30:31 +01001684/**
1685 * @brief Compile the parsed range restriction.
1686 * @param[in] ctx Compile context.
1687 * @param[in] range_p Parsed range structure to compile.
1688 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1689 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1690 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1691 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1692 * range restriction must be more restrictive than the base_range.
1693 * @param[in,out] range Pointer to the created current range structure.
1694 * @return LY_ERR value.
1695 */
Radek Krejci19a96102018-11-15 13:38:09 +01001696static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001697lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, uint8_t length_restr,
Radek Krejci0f969882020-08-21 16:56:47 +02001698 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001699{
1700 LY_ERR ret = LY_EVALID;
1701 const char *expr;
1702 struct lysc_range_part *parts = NULL, *part;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001703 uint8_t range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001704 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001705
1706 assert(range);
1707 assert(range_p);
1708
1709 expr = range_p->arg;
Michal Vaskod989ba02020-08-24 10:59:24 +02001710 while (1) {
Radek Krejci19a96102018-11-15 13:38:09 +01001711 if (isspace(*expr)) {
1712 ++expr;
1713 } else if (*expr == '\0') {
1714 if (range_expected) {
1715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1716 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1717 length_restr ? "length" : "range", range_p->arg);
1718 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001719 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1721 "Invalid %s restriction - unexpected end of the expression (%s).",
1722 length_restr ? "length" : "range", range_p->arg);
1723 goto cleanup;
1724 }
1725 parts_done++;
1726 break;
1727 } else if (!strncmp(expr, "min", 3)) {
1728 if (parts) {
1729 /* min cannot be used elsewhere than in the first part */
1730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1731 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1732 expr - range_p->arg, range_p->arg);
1733 goto cleanup;
1734 }
1735 expr += 3;
1736
1737 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001738 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 +01001739 part->max_64 = part->min_64;
1740 } else if (*expr == '|') {
1741 if (!parts || range_expected) {
1742 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1743 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1744 goto cleanup;
1745 }
1746 expr++;
1747 parts_done++;
1748 /* process next part of the expression */
1749 } else if (!strncmp(expr, "..", 2)) {
1750 expr += 2;
1751 while (isspace(*expr)) {
1752 expr++;
1753 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001754 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001755 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1756 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1757 goto cleanup;
1758 }
1759 /* continue expecting the upper boundary */
1760 range_expected = 1;
1761 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1762 /* number */
1763 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001764 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001765 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 +01001766 range_expected = 0;
1767 } else {
1768 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001769 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 +01001770 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001771 part->max_64 = part->min_64;
1772 }
1773
1774 /* continue with possible another expression part */
1775 } else if (!strncmp(expr, "max", 3)) {
1776 expr += 3;
1777 while (isspace(*expr)) {
1778 expr++;
1779 }
1780 if (*expr != '\0') {
1781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1782 length_restr ? "length" : "range", expr);
1783 goto cleanup;
1784 }
1785 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001786 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001787 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 +01001788 range_expected = 0;
1789 } else {
1790 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001791 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 +01001792 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001793 part->min_64 = part->max_64;
1794 }
1795 } else {
1796 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1797 length_restr ? "length" : "range", expr);
1798 goto cleanup;
1799 }
1800 }
1801
1802 /* check with the previous range/length restriction */
1803 if (base_range) {
1804 switch (basetype) {
1805 case LY_TYPE_BINARY:
1806 case LY_TYPE_UINT8:
1807 case LY_TYPE_UINT16:
1808 case LY_TYPE_UINT32:
1809 case LY_TYPE_UINT64:
1810 case LY_TYPE_STRING:
1811 uns = 1;
1812 break;
1813 case LY_TYPE_DEC64:
1814 case LY_TYPE_INT8:
1815 case LY_TYPE_INT16:
1816 case LY_TYPE_INT32:
1817 case LY_TYPE_INT64:
1818 uns = 0;
1819 break;
1820 default:
1821 LOGINT(ctx->ctx);
1822 ret = LY_EINT;
1823 goto cleanup;
1824 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001825 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001826 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1827 goto baseerror;
1828 }
1829 /* current lower bound is not lower than the base */
1830 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1831 /* base has single value */
1832 if (base_range->parts[v].min_64 == parts[u].min_64) {
1833 /* both lower bounds are the same */
1834 if (parts[u].min_64 != parts[u].max_64) {
1835 /* current continues with a range */
1836 goto baseerror;
1837 } else {
1838 /* equal single values, move both forward */
1839 ++v;
1840 continue;
1841 }
1842 } else {
1843 /* base is single value lower than current range, so the
1844 * value from base range is removed in the current,
1845 * move only base and repeat checking */
1846 ++v;
1847 --u;
1848 continue;
1849 }
1850 } else {
1851 /* base is the range */
1852 if (parts[u].min_64 == parts[u].max_64) {
1853 /* current is a single value */
1854 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1855 /* current is behind the base range, so base range is omitted,
1856 * move the base and keep the current for further check */
1857 ++v;
1858 --u;
1859 } /* else it is within the base range, so move the current, but keep the base */
1860 continue;
1861 } else {
1862 /* both are ranges - check the higher bound, the lower was already checked */
1863 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1864 /* higher bound is higher than the current higher bound */
1865 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1866 /* but the current lower bound is also higher, so the base range is omitted,
1867 * continue with the same current, but move the base */
1868 --u;
1869 ++v;
1870 continue;
1871 }
1872 /* current range starts within the base range but end behind it */
1873 goto baseerror;
1874 } else {
1875 /* current range is smaller than the base,
1876 * move current, but stay with the base */
1877 continue;
1878 }
1879 }
1880 }
1881 }
1882 if (u != parts_done) {
1883baseerror:
1884 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1885 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1886 length_restr ? "length" : "range", range_p->arg);
1887 goto cleanup;
1888 }
1889 }
1890
1891 if (!(*range)) {
1892 *range = calloc(1, sizeof **range);
1893 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1894 }
1895
Radek Krejcic8b31002019-01-08 10:24:45 +01001896 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001897 if (range_p->eapptag) {
1898 lydict_remove(ctx->ctx, (*range)->eapptag);
1899 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1900 }
1901 if (range_p->emsg) {
1902 lydict_remove(ctx->ctx, (*range)->emsg);
1903 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1904 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001905 if (range_p->dsc) {
1906 lydict_remove(ctx->ctx, (*range)->dsc);
1907 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1908 }
1909 if (range_p->ref) {
1910 lydict_remove(ctx->ctx, (*range)->ref);
1911 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1912 }
Radek Krejci19a96102018-11-15 13:38:09 +01001913 /* extensions are taken only from the last range by the caller */
1914
1915 (*range)->parts = parts;
1916 parts = NULL;
1917 ret = LY_SUCCESS;
1918cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001919 LY_ARRAY_FREE(parts);
1920
1921 return ret;
1922}
1923
1924/**
1925 * @brief Checks pattern syntax.
1926 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001927 * @param[in] ctx Context.
1928 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001929 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001930 * @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 +01001931 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001932 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001933LY_ERR
1934lys_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 +01001935{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001936 size_t idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001937 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001938 int err_code;
1939 const char *orig_ptr;
1940 PCRE2_SIZE err_offset;
1941 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001942#define URANGE_LEN 19
1943 char *ublock2urange[][2] = {
1944 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1945 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1946 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1947 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1948 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1949 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1950 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1951 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1952 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1953 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1954 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1955 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1956 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1957 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1958 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1959 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1960 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1961 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1962 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1963 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1964 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1965 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1966 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1967 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1968 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1969 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1970 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1971 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1972 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1973 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1974 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1975 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1976 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1977 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1978 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1979 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1980 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1981 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1982 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1983 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1984 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1985 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1986 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1987 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1988 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1989 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1990 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1991 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1992 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1993 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1994 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1995 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1996 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1997 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1998 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1999 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
2000 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
2001 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
2002 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
2003 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
2004 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
2005 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
2006 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
2007 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
2008 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
2009 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
2010 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
2011 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
2012 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
2013 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
2014 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
2015 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
2016 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
2017 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
2018 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
2019 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
2020 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
2021 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
2022 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
2023 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
2024 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
2025 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
2026 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
2027 {NULL, NULL}
2028 };
2029
2030 /* adjust the expression to a Perl equivalent
2031 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
2032
Michal Vasko40a00082020-05-27 15:20:01 +02002033 /* allocate space for the transformed pattern */
2034 size = strlen(pattern) + 1;
2035 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002036 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002037 perl_regex[0] = '\0';
2038
Michal Vasko40a00082020-05-27 15:20:01 +02002039 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
2040 brack = 0;
2041 idx = 0;
2042 orig_ptr = pattern;
2043 while (orig_ptr[0]) {
2044 switch (orig_ptr[0]) {
2045 case '$':
2046 case '^':
2047 if (!brack) {
2048 /* make space for the extra character */
2049 ++size;
2050 perl_regex = ly_realloc(perl_regex, size);
2051 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002052
Michal Vasko40a00082020-05-27 15:20:01 +02002053 /* print escape slash */
2054 perl_regex[idx] = '\\';
2055 ++idx;
2056 }
2057 break;
2058 case '[':
2059 /* must not be escaped */
2060 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2061 ++brack;
2062 }
2063 break;
2064 case ']':
2065 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2066 /* pattern was checked and compiled already */
2067 assert(brack);
2068 --brack;
2069 }
2070 break;
2071 default:
2072 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002073 }
Michal Vasko40a00082020-05-27 15:20:01 +02002074
2075 /* copy char */
2076 perl_regex[idx] = orig_ptr[0];
2077
2078 ++idx;
2079 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002080 }
Michal Vasko40a00082020-05-27 15:20:01 +02002081 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002082
2083 /* substitute Unicode Character Blocks with exact Character Ranges */
2084 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2085 start = ptr - perl_regex;
2086
2087 ptr = strchr(ptr, '}');
2088 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002089 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002090 pattern, perl_regex + start + 2, "unterminated character property");
2091 free(perl_regex);
2092 return LY_EVALID;
2093 }
2094 end = (ptr - perl_regex) + 1;
2095
2096 /* need more space */
2097 if (end - start < URANGE_LEN) {
2098 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002100 }
2101
2102 /* find our range */
2103 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2104 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2105 break;
2106 }
2107 }
2108 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002110 pattern, perl_regex + start + 5, "unknown block name");
2111 free(perl_regex);
2112 return LY_EVALID;
2113 }
2114
2115 /* 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 +02002116 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002117 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002118 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002119 }
2120 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002121 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002122 }
2123 }
Michal Vasko40a00082020-05-27 15:20:01 +02002124 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002125 /* skip brackets */
2126 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2127 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2128 } else {
2129 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2130 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2131 }
2132 }
2133
2134 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002135 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2136 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002137 &err_code, &err_offset, NULL);
2138 if (!code_local) {
2139 PCRE2_UCHAR err_msg[256] = {0};
2140 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002142 free(perl_regex);
2143 return LY_EVALID;
2144 }
2145 free(perl_regex);
2146
Radek Krejci54579462019-04-30 12:47:06 +02002147 if (code) {
2148 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002149 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002150 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002151 }
2152
2153 return LY_SUCCESS;
2154
2155#undef URANGE_LEN
2156}
2157
Radek Krejcia3045382018-11-22 14:30:31 +01002158/**
2159 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2160 * @param[in] ctx Compile context.
2161 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002162 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2163 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2164 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2165 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2166 */
Radek Krejci19a96102018-11-15 13:38:09 +01002167static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002168lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci0f969882020-08-21 16:56:47 +02002169 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
Radek Krejci19a96102018-11-15 13:38:09 +01002170{
2171 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002172 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002173 LY_ERR ret = LY_SUCCESS;
2174
2175 /* first, copy the patterns from the base type */
2176 if (base_patterns) {
2177 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2178 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2179 }
2180
2181 LY_ARRAY_FOR(patterns_p, u) {
2182 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2183 *pattern = calloc(1, sizeof **pattern);
2184 ++(*pattern)->refcount;
2185
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002187 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002188
2189 if (patterns_p[u].arg[0] == 0x15) {
2190 (*pattern)->inverted = 1;
2191 }
Radek Krejci54579462019-04-30 12:47:06 +02002192 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002193 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2194 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002195 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2196 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002197 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002198 }
2199done:
2200 return ret;
2201}
2202
Radek Krejcia3045382018-11-22 14:30:31 +01002203/**
2204 * @brief map of the possible restrictions combination for the specific built-in type.
2205 */
Radek Krejci19a96102018-11-15 13:38:09 +01002206static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2207 0 /* LY_TYPE_UNKNOWN */,
2208 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002209 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2210 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2211 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2212 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2213 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002214 LYS_SET_BIT /* LY_TYPE_BITS */,
2215 0 /* LY_TYPE_BOOL */,
2216 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2217 0 /* LY_TYPE_EMPTY */,
2218 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2219 LYS_SET_BASE /* LY_TYPE_IDENT */,
2220 LYS_SET_REQINST /* LY_TYPE_INST */,
2221 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002222 LYS_SET_TYPE /* LY_TYPE_UNION */,
2223 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002224 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002225 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002226 LYS_SET_RANGE /* LY_TYPE_INT64 */
2227};
2228
2229/**
2230 * @brief stringification of the YANG built-in data types
2231 */
Michal Vasko22df3f02020-08-24 13:29:22 +02002232const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
Radek Krejci5969f272018-11-23 10:03:58 +01002233 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
Radek Krejci0f969882020-08-21 16:56:47 +02002234 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"};
Radek Krejci19a96102018-11-15 13:38:09 +01002235
Radek Krejcia3045382018-11-22 14:30:31 +01002236/**
2237 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2238 * @param[in] ctx Compile context.
2239 * @param[in] enums_p Array of the parsed enum structures to compile.
2240 * @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 +01002241 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2242 * @param[out] enums Newly created array of the compiled enums information for the current type.
2243 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2244 */
Radek Krejci19a96102018-11-15 13:38:09 +01002245static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002246lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci0f969882020-08-21 16:56:47 +02002247 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002248{
2249 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002250 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002251 int32_t value = 0;
2252 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002253 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002254
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002255 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2257 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2258 return LY_EVALID;
2259 }
2260
2261 LY_ARRAY_FOR(enums_p, u) {
2262 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2263 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002264 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2265 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002266 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002267 if (base_enums) {
2268 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2269 LY_ARRAY_FOR(base_enums, v) {
2270 if (!strcmp(e->name, base_enums[v].name)) {
2271 break;
2272 }
2273 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002274 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2276 "Invalid %s - derived type adds new item \"%s\".",
2277 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2278 return LY_EVALID;
2279 }
2280 match = v;
2281 }
2282
2283 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002284 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002285 if (enums_p[u].flags & LYS_SET_VALUE) {
2286 e->value = (int32_t)enums_p[u].value;
2287 if (!u || e->value >= value) {
2288 value = e->value + 1;
2289 }
2290 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002291 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002292 if (e->value == (*enums)[v].value) {
2293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2294 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2295 e->value, e->name, (*enums)[v].name);
2296 return LY_EVALID;
2297 }
2298 }
2299 } else if (base_enums) {
2300 /* inherit the assigned value */
2301 e->value = base_enums[match].value;
2302 if (!u || e->value >= value) {
2303 value = e->value + 1;
2304 }
2305 } else {
2306 /* assign value automatically */
2307 if (u && value == INT32_MIN) {
2308 /* counter overflow */
2309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2310 "Invalid enumeration - it is not possible to auto-assign enum value for "
2311 "\"%s\" since the highest value is already 2147483647.", e->name);
2312 return LY_EVALID;
2313 }
2314 e->value = value++;
2315 }
2316 } else { /* LY_TYPE_BITS */
2317 if (enums_p[u].flags & LYS_SET_VALUE) {
2318 e->value = (int32_t)enums_p[u].value;
2319 if (!u || (uint32_t)e->value >= position) {
2320 position = (uint32_t)e->value + 1;
2321 }
2322 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002323 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002324 if (e->value == (*enums)[v].value) {
2325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2326 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek Krejci0f969882020-08-21 16:56:47 +02002327 (uint32_t)e->value, e->name, (*enums)[v].name);
Radek Krejci19a96102018-11-15 13:38:09 +01002328 return LY_EVALID;
2329 }
2330 }
2331 } else if (base_enums) {
2332 /* inherit the assigned value */
2333 e->value = base_enums[match].value;
2334 if (!u || (uint32_t)e->value >= position) {
2335 position = (uint32_t)e->value + 1;
2336 }
2337 } else {
2338 /* assign value automatically */
2339 if (u && position == 0) {
2340 /* counter overflow */
2341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2342 "Invalid bits - it is not possible to auto-assign bit position for "
2343 "\"%s\" since the highest value is already 4294967295.", e->name);
2344 return LY_EVALID;
2345 }
2346 e->value = position++;
2347 }
2348 }
2349
2350 if (base_enums) {
2351 /* the assigned values must not change from the derived type */
2352 if (e->value != base_enums[match].value) {
2353 if (basetype == LY_TYPE_ENUM) {
2354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2355 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2356 e->name, base_enums[match].value, e->value);
2357 } else {
2358 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2359 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2360 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2361 }
2362 return LY_EVALID;
2363 }
2364 }
2365
Radek Krejciec4da802019-05-02 13:02:41 +02002366 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002367 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 +01002368
2369 if (basetype == LY_TYPE_BITS) {
2370 /* keep bits ordered by position */
Radek Krejci1e008d22020-08-17 11:37:37 +02002371 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
Radek Krejci19a96102018-11-15 13:38:09 +01002372 if (v != u) {
2373 memcpy(&storage, e, sizeof *e);
2374 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2375 memcpy(&(*enums)[v], &storage, sizeof storage);
2376 }
2377 }
2378 }
2379
2380done:
2381 return ret;
2382}
2383
Radek Krejcia3045382018-11-22 14:30:31 +01002384/**
2385 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2386 *
2387 * path-arg = absolute-path / relative-path
2388 * absolute-path = 1*("/" (node-identifier *path-predicate))
2389 * relative-path = 1*(".." "/") descendant-path
2390 *
2391 * @param[in,out] path Path to parse.
2392 * @param[out] prefix Prefix of the token, NULL if there is not any.
2393 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2394 * @param[out] name Name of the token.
2395 * @param[out] nam_len Length of the name.
2396 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2397 * must not be changed between consecutive calls. -1 if the
2398 * path is absolute.
2399 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2400 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2401 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002402LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002403lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
Radek Krejci1deb5be2020-08-26 16:43:36 +02002404 int32_t *parent_times, uint8_t *has_predicate)
Radek Krejcia3045382018-11-22 14:30:31 +01002405{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002406 int32_t par_times = 0;
Radek Krejcia3045382018-11-22 14:30:31 +01002407
2408 assert(path && *path);
2409 assert(parent_times);
2410 assert(prefix);
2411 assert(prefix_len);
2412 assert(name);
2413 assert(name_len);
2414 assert(has_predicate);
2415
2416 *prefix = NULL;
2417 *prefix_len = 0;
2418 *name = NULL;
2419 *name_len = 0;
2420 *has_predicate = 0;
2421
2422 if (!*parent_times) {
2423 if (!strncmp(*path, "..", 2)) {
2424 *path += 2;
2425 ++par_times;
2426 while (!strncmp(*path, "/..", 3)) {
2427 *path += 3;
2428 ++par_times;
2429 }
2430 }
2431 if (par_times) {
2432 *parent_times = par_times;
2433 } else {
2434 *parent_times = -1;
2435 }
2436 }
2437
2438 if (**path != '/') {
2439 return LY_EINVAL;
2440 }
2441 /* skip '/' */
2442 ++(*path);
2443
2444 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002445 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002446
2447 if ((**path == '/' && (*path)[1]) || !**path) {
2448 /* path continues by another token or this is the last token */
2449 return LY_SUCCESS;
2450 } else if ((*path)[0] != '[') {
2451 /* unexpected character */
2452 return LY_EINVAL;
2453 } else {
2454 /* predicate starting with [ */
2455 *has_predicate = 1;
2456 return LY_SUCCESS;
2457 }
2458}
2459
2460/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002461 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2462 *
2463 * The set of features used for target must be a subset of features used for the leafref.
2464 * This is not a perfect, we should compare the truth tables but it could require too much resources
2465 * and RFC 7950 does not require it explicitely, so we simplify that.
2466 *
2467 * @param[in] refnode The leafref node.
2468 * @param[in] target Tha target node of the leafref.
2469 * @return LY_SUCCESS or LY_EVALID;
2470 */
2471static LY_ERR
2472lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2473{
2474 LY_ERR ret = LY_EVALID;
2475 const struct lysc_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002476 LY_ARRAY_COUNT_TYPE u, v;
2477 uint32_t count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002478 struct ly_set features = {0};
2479
2480 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002481 if (iter->iffeatures) {
2482 LY_ARRAY_FOR(iter->iffeatures, u) {
2483 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002484 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002485 }
2486 }
2487 }
2488 }
2489
2490 /* we should have, in features set, a superset of features applicable to the target node.
Radek Krejciba03a5a2020-08-27 14:40:41 +02002491 * If the feature is not present, we don;t have a subset of features applicable
Radek Krejci58d171e2018-11-23 13:50:55 +01002492 * to the leafref itself. */
2493 count = features.count;
2494 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002495 if (iter->iffeatures) {
2496 LY_ARRAY_FOR(iter->iffeatures, u) {
2497 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002498 if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
2499 /* feature not present */
Radek Krejci58d171e2018-11-23 13:50:55 +01002500 goto cleanup;
2501 }
2502 }
2503 }
2504 }
2505 }
2506 ret = LY_SUCCESS;
2507
2508cleanup:
2509 ly_set_erase(&features, NULL);
2510 return ret;
2511}
2512
Michal Vasko004d3152020-06-11 19:59:22 +02002513static 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 +02002514 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2515 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod);
Radek Krejcia3045382018-11-22 14:30:31 +01002516
Radek Krejcia3045382018-11-22 14:30:31 +01002517/**
2518 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2519 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002520 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2521 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2522 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2523 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002524 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002525 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002526 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002527 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2528 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2529 * @param[out] type Newly created type structure with the filled information about the type.
2530 * @return LY_ERR value.
2531 */
Radek Krejci19a96102018-11-15 13:38:09 +01002532static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002533lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002534 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2535 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2536 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002537{
2538 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002539 struct lysc_type_bin *bin;
2540 struct lysc_type_num *num;
2541 struct lysc_type_str *str;
2542 struct lysc_type_bits *bits;
2543 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002544 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002545 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002546 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002547 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002548
2549 switch (basetype) {
2550 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +02002551 bin = (struct lysc_type_bin *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002552
2553 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002554 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002555 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002556 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002557 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002558 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 +01002559 }
2560 }
2561
2562 if (tpdfname) {
2563 type_p->compiled = *type;
2564 *type = calloc(1, sizeof(struct lysc_type_bin));
2565 }
2566 break;
2567 case LY_TYPE_BITS:
2568 /* RFC 7950 9.7 - bits */
Michal Vasko22df3f02020-08-24 13:29:22 +02002569 bits = (struct lysc_type_bits *)(*type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002570 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002571 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002572 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
2573 (struct lysc_type_bitenum_item **)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002574 }
2575
Radek Krejci555cb5b2018-11-16 14:54:33 +01002576 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002577 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002578 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002580 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002581 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002582 free(*type);
2583 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002584 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002585 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002586 }
2587
2588 if (tpdfname) {
2589 type_p->compiled = *type;
2590 *type = calloc(1, sizeof(struct lysc_type_bits));
2591 }
2592 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002593 case LY_TYPE_DEC64:
Radek Krejci115a74d2020-08-14 22:18:12 +02002594 dec = (struct lysc_type_dec *)(*type);
Radek Krejci6cba4292018-11-15 17:33:29 +01002595
2596 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002597 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002598 if (!type_p->fraction_digits) {
2599 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002601 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002603 free(*type);
2604 *type = NULL;
2605 }
2606 return LY_EVALID;
2607 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002608 dec->fraction_digits = type_p->fraction_digits;
2609 } else {
2610 if (type_p->fraction_digits) {
2611 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
2612 if (tpdfname) {
2613 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2614 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
2615 tpdfname);
2616 } else {
2617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2618 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
2619 free(*type);
2620 *type = NULL;
2621 }
2622 return LY_EVALID;
Radek Krejci6cba4292018-11-15 17:33:29 +01002623 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002624 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
Radek Krejci6cba4292018-11-15 17:33:29 +01002625 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002626
2627 /* RFC 7950 9.2.4 - range */
2628 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002629 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
Radek Krejci115a74d2020-08-14 22:18:12 +02002630 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002631 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002632 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 +01002633 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002634 }
2635
2636 if (tpdfname) {
2637 type_p->compiled = *type;
2638 *type = calloc(1, sizeof(struct lysc_type_dec));
2639 }
2640 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002641 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002642 str = (struct lysc_type_str *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002643
2644 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002645 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002646 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002647 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002648 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002649 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 +01002650 }
Michal Vasko22df3f02020-08-24 13:29:22 +02002651 } else if (base && ((struct lysc_type_str *)base)->length) {
2652 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002653 }
2654
2655 /* RFC 7950 9.4.5 - pattern */
2656 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002657 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Michal Vasko22df3f02020-08-24 13:29:22 +02002658 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
2659 } else if (base && ((struct lysc_type_str *)base)->patterns) {
2660 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002661 }
2662
2663 if (tpdfname) {
2664 type_p->compiled = *type;
2665 *type = calloc(1, sizeof(struct lysc_type_str));
2666 }
2667 break;
2668 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +02002669 enumeration = (struct lysc_type_enum *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002670
2671 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002672 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002673 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002674 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002675 }
2676
Radek Krejci555cb5b2018-11-16 14:54:33 +01002677 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002678 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002679 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002681 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002682 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002683 free(*type);
2684 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002685 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002686 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002687 }
2688
2689 if (tpdfname) {
2690 type_p->compiled = *type;
2691 *type = calloc(1, sizeof(struct lysc_type_enum));
2692 }
2693 break;
2694 case LY_TYPE_INT8:
2695 case LY_TYPE_UINT8:
2696 case LY_TYPE_INT16:
2697 case LY_TYPE_UINT16:
2698 case LY_TYPE_INT32:
2699 case LY_TYPE_UINT32:
2700 case LY_TYPE_INT64:
2701 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +02002702 num = (struct lysc_type_num *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002703
2704 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002705 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002706 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002707 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002708 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002709 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 +01002710 }
2711 }
2712
2713 if (tpdfname) {
2714 type_p->compiled = *type;
2715 *type = calloc(1, sizeof(struct lysc_type_num));
2716 }
2717 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002718 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002719 idref = (struct lysc_type_identityref *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002720
2721 /* RFC 7950 9.10.2 - base */
2722 if (type_p->bases) {
2723 if (base) {
2724 /* only the directly derived identityrefs can contain base specification */
2725 if (tpdfname) {
2726 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002727 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002728 tpdfname);
2729 } else {
2730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002731 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002732 free(*type);
2733 *type = NULL;
2734 }
2735 return LY_EVALID;
2736 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002737 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002738 }
2739
2740 if (!base && !type_p->flags) {
2741 /* type derived from identityref built-in type must contain at least one base */
2742 if (tpdfname) {
2743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2744 } else {
2745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2746 free(*type);
2747 *type = NULL;
2748 }
2749 return LY_EVALID;
2750 }
2751
2752 if (tpdfname) {
2753 type_p->compiled = *type;
2754 *type = calloc(1, sizeof(struct lysc_type_identityref));
2755 }
2756 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002757 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002758 lref = (struct lysc_type_leafref *)*type;
Michal Vasko004d3152020-06-11 19:59:22 +02002759
Radek Krejcia3045382018-11-22 14:30:31 +01002760 /* RFC 7950 9.9.3 - require-instance */
2761 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002762 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002763 if (tpdfname) {
2764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2765 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2766 } else {
2767 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2768 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2769 free(*type);
2770 *type = NULL;
2771 }
2772 return LY_EVALID;
2773 }
Michal Vasko004d3152020-06-11 19:59:22 +02002774 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002775 } else if (base) {
2776 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002777 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002778 } else {
2779 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002780 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002781 }
2782 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002783 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2784 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002785 } else if (base) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002786 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path);
2787 lref->path_context = ((struct lysc_type_leafref *)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002788 } else if (tpdfname) {
2789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2790 return LY_EVALID;
2791 } else {
2792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2793 free(*type);
2794 *type = NULL;
2795 return LY_EVALID;
2796 }
2797 if (tpdfname) {
2798 type_p->compiled = *type;
2799 *type = calloc(1, sizeof(struct lysc_type_leafref));
2800 }
2801 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002802 case LY_TYPE_INST:
2803 /* RFC 7950 9.9.3 - require-instance */
2804 if (type_p->flags & LYS_SET_REQINST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002805 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
Radek Krejci16c0f822018-11-16 10:46:10 +01002806 } else {
2807 /* default is true */
Michal Vasko22df3f02020-08-24 13:29:22 +02002808 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
Radek Krejci16c0f822018-11-16 10:46:10 +01002809 }
2810
2811 if (tpdfname) {
2812 type_p->compiled = *type;
2813 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2814 }
2815 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002816 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +02002817 un = (struct lysc_type_union *)(*type);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002818
2819 /* RFC 7950 7.4 - type */
2820 if (type_p->types) {
2821 if (base) {
2822 /* only the directly derived union can contain types specification */
2823 if (tpdfname) {
2824 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2825 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2826 tpdfname);
2827 } else {
2828 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2829 "Invalid type substatement for the type not directly derived from union built-in type.");
2830 free(*type);
2831 *type = NULL;
2832 }
2833 return LY_EVALID;
2834 }
2835 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002836 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2837 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002838 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002839 &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002840 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2841 /* add space for additional types from the union subtype */
2842 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vasko22df3f02020-08-24 13:29:22 +02002843 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
2844 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002845
2846 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002847 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002848 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2849 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2850 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
Michal Vasko22df3f02020-08-24 13:29:22 +02002851 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 +02002852 lref = (struct lysc_type_leafref *)un->types[u + additional];
2853
2854 lref->basetype = LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02002855 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path);
Michal Vasko004d3152020-06-11 19:59:22 +02002856 lref->refcount = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +02002857 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
2858 lref->path_context = ((struct lysc_type_leafref *)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002859 /* TODO extensions */
2860
2861 } else {
2862 un->types[u + additional] = un_aux->types[v];
2863 ++un_aux->types[v]->refcount;
2864 }
2865 ++additional;
2866 LY_ARRAY_INCREMENT(un->types);
2867 }
2868 /* compensate u increment in main loop */
2869 --additional;
2870
2871 /* free the replaced union subtype */
Michal Vasko22df3f02020-08-24 13:29:22 +02002872 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002873 } else {
2874 LY_ARRAY_INCREMENT(un->types);
2875 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002876 }
2877 }
2878
2879 if (!base && !type_p->flags) {
2880 /* type derived from union built-in type must contain at least one type */
2881 if (tpdfname) {
2882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2883 } else {
2884 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2885 free(*type);
2886 *type = NULL;
2887 }
2888 return LY_EVALID;
2889 }
2890
2891 if (tpdfname) {
2892 type_p->compiled = *type;
2893 *type = calloc(1, sizeof(struct lysc_type_union));
2894 }
2895 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002896 case LY_TYPE_BOOL:
2897 case LY_TYPE_EMPTY:
2898 case LY_TYPE_UNKNOWN: /* just to complete switch */
2899 break;
2900 }
2901 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2902done:
2903 return ret;
2904}
2905
Radek Krejcia3045382018-11-22 14:30:31 +01002906/**
2907 * @brief Compile information about the leaf/leaf-list's type.
2908 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002909 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2910 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2911 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2912 * @param[in] context_name Name of the context node or referencing typedef for logging.
2913 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002914 * @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 +01002915 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002916 * @param[out] dflt Default value for the type.
2917 * @param[out] dflt_mod Local module for the default value.
Radek Krejcia3045382018-11-22 14:30:31 +01002918 * @return LY_ERR value.
2919 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002920static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002921lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002922 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2923 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod)
Radek Krejci19a96102018-11-15 13:38:09 +01002924{
2925 LY_ERR ret = LY_SUCCESS;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002926 uint8_t dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002927 struct type_context {
2928 const struct lysp_tpdf *tpdf;
2929 struct lysp_node *node;
2930 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002931 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002932 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002933 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002934 struct ly_set tpdf_chain = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002935
2936 assert((dflt && dflt_mod) || (!dflt && !dflt_mod));
Radek Krejci19a96102018-11-15 13:38:09 +01002937
2938 (*type) = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002939 if (dflt) {
2940 *dflt = NULL;
2941 *dflt_mod = NULL;
2942 }
Radek Krejci19a96102018-11-15 13:38:09 +01002943
2944 tctx = calloc(1, sizeof *tctx);
2945 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002946 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002947 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2948 ret == LY_SUCCESS;
2949 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2950 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2951 if (basetype) {
2952 break;
2953 }
2954
2955 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002956 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2957 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002958 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2959
Radek Krejcicdfecd92018-11-26 11:27:32 +01002960 if (units && !*units) {
2961 /* inherit units */
2962 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2963 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002964 if (dflt && !*dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002965 /* inherit default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002966 *dflt = tctx->tpdf->dflt;
2967 *dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002968 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002969 if (dummyloops && (!units || *units) && dflt && *dflt) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002970 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002971 break;
2972 }
2973
Radek Krejci19a96102018-11-15 13:38:09 +01002974 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002975 /* it is not necessary to continue, the rest of the chain was already compiled,
2976 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002977 basetype = tctx->tpdf->type.compiled->basetype;
Radek Krejciba03a5a2020-08-27 14:40:41 +02002978 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
2979 LY_CHECK_GOTO(ret, cleanup);
2980
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002981 if ((units && !*units) || (dflt && !*dflt)) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002982 dummyloops = 1;
2983 goto preparenext;
2984 } else {
2985 tctx = NULL;
2986 break;
2987 }
Radek Krejci19a96102018-11-15 13:38:09 +01002988 }
2989
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002990 /* circular typedef reference detection */
Radek Krejci1deb5be2020-08-26 16:43:36 +02002991 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002992 /* local part */
Michal Vasko22df3f02020-08-24 13:29:22 +02002993 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002994 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2995 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2996 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2997 free(tctx);
2998 ret = LY_EVALID;
2999 goto cleanup;
3000 }
3001 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003002 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003003 /* global part for unions corner case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003004 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003005 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3006 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3007 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3008 free(tctx);
3009 ret = LY_EVALID;
3010 goto cleanup;
3011 }
3012 }
3013
Radek Krejci19a96102018-11-15 13:38:09 +01003014 /* store information for the following processing */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003015 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
3016 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003017
Radek Krejcicdfecd92018-11-26 11:27:32 +01003018preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003019 /* prepare next loop */
3020 tctx_prev = tctx;
3021 tctx = calloc(1, sizeof *tctx);
3022 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3023 }
3024 free(tctx);
3025
3026 /* allocate type according to the basetype */
3027 switch (basetype) {
3028 case LY_TYPE_BINARY:
3029 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003030 break;
3031 case LY_TYPE_BITS:
3032 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003033 break;
3034 case LY_TYPE_BOOL:
3035 case LY_TYPE_EMPTY:
3036 *type = calloc(1, sizeof(struct lysc_type));
3037 break;
3038 case LY_TYPE_DEC64:
3039 *type = calloc(1, sizeof(struct lysc_type_dec));
3040 break;
3041 case LY_TYPE_ENUM:
3042 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003043 break;
3044 case LY_TYPE_IDENT:
3045 *type = calloc(1, sizeof(struct lysc_type_identityref));
3046 break;
3047 case LY_TYPE_INST:
3048 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3049 break;
3050 case LY_TYPE_LEAFREF:
3051 *type = calloc(1, sizeof(struct lysc_type_leafref));
3052 break;
3053 case LY_TYPE_STRING:
3054 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003055 break;
3056 case LY_TYPE_UNION:
3057 *type = calloc(1, sizeof(struct lysc_type_union));
3058 break;
3059 case LY_TYPE_INT8:
3060 case LY_TYPE_UINT8:
3061 case LY_TYPE_INT16:
3062 case LY_TYPE_UINT16:
3063 case LY_TYPE_INT32:
3064 case LY_TYPE_UINT32:
3065 case LY_TYPE_INT64:
3066 case LY_TYPE_UINT64:
3067 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003068 break;
3069 case LY_TYPE_UNKNOWN:
3070 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3071 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3072 ret = LY_EVALID;
3073 goto cleanup;
3074 }
3075 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003076 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3078 ly_data_type2str[basetype]);
3079 free(*type);
3080 (*type) = NULL;
3081 ret = LY_EVALID;
3082 goto cleanup;
3083 }
3084
3085 /* get restrictions from the referred typedefs */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003086 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003087 tctx = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003088
3089 /* remember the typedef context for circular check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003090 ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
3091 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003092
Radek Krejci43699232018-11-23 14:59:46 +01003093 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003094 base = tctx->tpdf->type.compiled;
3095 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003096 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003097 /* no change, just use the type information from the base */
Michal Vasko22df3f02020-08-24 13:29:22 +02003098 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 +01003099 ++base->refcount;
3100 continue;
3101 }
3102
3103 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003104 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3106 tctx->tpdf->name, ly_data_type2str[basetype]);
3107 ret = LY_EVALID;
3108 goto cleanup;
3109 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3110 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3111 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3112 tctx->tpdf->name, tctx->tpdf->dflt);
3113 ret = LY_EVALID;
3114 goto cleanup;
3115 }
3116
Radek Krejci19a96102018-11-15 13:38:09 +01003117 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003118 /* TODO user type plugins */
3119 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003120 prev_type = *type;
Michal Vasko22df3f02020-08-24 13:29:22 +02003121 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 +01003122 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003123 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003124 LY_CHECK_GOTO(ret, cleanup);
3125 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003126 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003127 /* remove the processed typedef contexts from the stack for circular check */
3128 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003129
Radek Krejcic5c27e52018-11-15 14:38:11 +01003130 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003131 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003132 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003133 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003134 /* TODO user type plugins */
3135 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003136 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003137 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 +01003138 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003139 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003140 /* no specific restriction in leaf's type definition, copy from the base */
3141 free(*type);
3142 (*type) = base;
3143 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003144 }
3145
Radek Krejci0935f412019-08-20 16:15:18 +02003146 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003147
3148cleanup:
3149 ly_set_erase(&tpdf_chain, free);
3150 return ret;
3151}
3152
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003153/**
3154 * @brief Compile status information of the given node.
3155 *
3156 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3157 * has the status correctly set during the compilation.
3158 *
3159 * @param[in] ctx Compile context
3160 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3161 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3162 * the compatibility with the parent's status value.
3163 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3164 * @return LY_ERR value.
3165 */
3166static LY_ERR
3167lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3168{
3169 /* status - it is not inherited by specification, but it does not make sense to have
3170 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3171 if (!((*node_flags) & LYS_STATUS_MASK)) {
3172 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3173 if ((parent_flags & 0x3) != 0x3) {
3174 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3175 * combination of bits (0x3) which marks the uses_status value */
3176 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
Radek Krejci0f969882020-08-21 16:56:47 +02003177 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003178 }
3179 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3180 } else {
3181 (*node_flags) |= LYS_STATUS_CURR;
3182 }
3183 } else if (parent_flags & LYS_STATUS_MASK) {
3184 /* check status compatibility with the parent */
3185 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3186 if ((*node_flags) & LYS_STATUS_CURR) {
3187 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3188 "A \"current\" status is in conflict with the parent's \"%s\" status.",
Radek Krejci0f969882020-08-21 16:56:47 +02003189 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003190 } else { /* LYS_STATUS_DEPRC */
3191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3192 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3193 }
3194 return LY_EVALID;
3195 }
3196 }
3197 return LY_SUCCESS;
3198}
3199
Radek Krejci8cce8532019-03-05 11:27:45 +01003200/**
3201 * @brief Check uniqness of the node/action/notification name.
3202 *
3203 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3204 * structures, but they share the namespace so we need to check their name collisions.
3205 *
3206 * @param[in] ctx Compile context.
3207 * @param[in] children List (linked list) of data nodes to go through.
3208 * @param[in] actions List (sized array) of actions or RPCs to go through.
3209 * @param[in] notifs List (sized array) of Notifications to go through.
3210 * @param[in] name Name of the item to find in the given lists.
3211 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3212 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3213 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3214 */
3215static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003216lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
Radek Krejci0f969882020-08-21 16:56:47 +02003217 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003218{
3219 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003220 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003221
3222 LY_LIST_FOR(children, iter) {
3223 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3224 goto error;
3225 }
3226 }
3227 LY_ARRAY_FOR(actions, u) {
3228 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3229 goto error;
3230 }
3231 }
3232 LY_ARRAY_FOR(notifs, u) {
3233 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3234 goto error;
3235 }
3236 }
3237 return LY_SUCCESS;
3238error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003240 return LY_EEXIST;
3241}
3242
Radek Krejciec4da802019-05-02 13:02:41 +02003243static 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 +01003244
Radek Krejcia3045382018-11-22 14:30:31 +01003245/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003246 * @brief Compile parsed RPC/action schema node information.
3247 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003248 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003249 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003250 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3251 * @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).
3252 * Zero means no uses, non-zero value with no status bit set mean the default status.
3253 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3254 */
3255static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003256lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003257 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003258{
3259 LY_ERR ret = LY_SUCCESS;
3260 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003261 LY_ARRAY_COUNT_TYPE u;
3262 uint32_t opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003263
Radek Krejci327de162019-06-14 12:52:07 +02003264 lysc_update_path(ctx, parent, action_p->name);
3265
Radek Krejci8cce8532019-03-05 11:27:45 +01003266 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3267 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3268 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3269 action_p->name, action)) {
3270 return LY_EVALID;
3271 }
3272
Radek Krejciec4da802019-05-02 13:02:41 +02003273 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003275 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003276 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003277 return LY_EVALID;
3278 }
3279
Michal Vasko1bf09392020-03-27 12:38:10 +01003280 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003281 action->module = ctx->mod;
3282 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003283 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003284 action->sp = action_p;
3285 }
3286 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3287
3288 /* status - it is not inherited by specification, but it does not make sense to have
3289 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003290 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003291
3292 DUP_STRING(ctx->ctx, action_p->name, action->name);
3293 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3294 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003295 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003296 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003297
3298 /* input */
Michal Vasko22df3f02020-08-24 13:29:22 +02003299 lysc_update_path(ctx, (struct lysc_node *)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003300 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003301 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 +02003302 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003303 LY_LIST_FOR(action_p->input.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003304 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003305 }
Radek Krejci327de162019-06-14 12:52:07 +02003306 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003307 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003308
3309 /* output */
Michal Vasko22df3f02020-08-24 13:29:22 +02003310 lysc_update_path(ctx, (struct lysc_node *)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003311 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003312 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 +02003313 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003314 LY_LIST_FOR(action_p->output.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003315 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003316 }
Radek Krejci327de162019-06-14 12:52:07 +02003317 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003318 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003319
3320 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3321 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003322 ret = ly_set_add(&ctx->xpath, action, 0, NULL);
3323 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003324 }
3325
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003326cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003327 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003328 return ret;
3329}
3330
3331/**
Radek Krejci43981a32019-04-12 09:44:11 +02003332 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003333 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003334 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003335 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3336 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3337 * @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 +02003338 * Zero means no uses, non-zero value with no status bit set mean the default status.
3339 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3340 */
3341static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003342lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003343 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
Radek Krejcifc11bd72019-04-11 16:00:05 +02003344{
3345 LY_ERR ret = LY_SUCCESS;
3346 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003347 LY_ARRAY_COUNT_TYPE u;
3348 uint32_t opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003349
Radek Krejci327de162019-06-14 12:52:07 +02003350 lysc_update_path(ctx, parent, notif_p->name);
3351
Radek Krejcifc11bd72019-04-11 16:00:05 +02003352 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3353 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3354 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3355 notif_p->name, notif)) {
3356 return LY_EVALID;
3357 }
3358
Radek Krejciec4da802019-05-02 13:02:41 +02003359 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003360 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3361 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003362 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003363 return LY_EVALID;
3364 }
3365
3366 notif->nodetype = LYS_NOTIF;
3367 notif->module = ctx->mod;
3368 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003369 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003370 notif->sp = notif_p;
3371 }
3372 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3373
3374 /* status - it is not inherited by specification, but it does not make sense to have
3375 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003376 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
3377 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003378
3379 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3380 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3381 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003382 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003383 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003384 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3385 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003386 ret = ly_set_add(&ctx->xpath, notif, 0, NULL);
3387 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003388 }
Radek Krejci0935f412019-08-20 16:15:18 +02003389 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003390
Radek Krejciec4da802019-05-02 13:02:41 +02003391 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003392 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003393 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status);
3394 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003395 }
3396
Radek Krejci327de162019-06-14 12:52:07 +02003397 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003398cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003399 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003400 return ret;
3401}
3402
3403/**
Radek Krejcia3045382018-11-22 14:30:31 +01003404 * @brief Compile parsed container node information.
3405 * @param[in] ctx Compile context
3406 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003407 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3408 * is enriched with the container-specific information.
3409 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3410 */
Radek Krejci19a96102018-11-15 13:38:09 +01003411static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003412lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003413{
Michal Vasko22df3f02020-08-24 13:29:22 +02003414 struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p;
3415 struct lysc_node_container *cont = (struct lysc_node_container *)node;
Radek Krejci19a96102018-11-15 13:38:09 +01003416 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003417 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003418 LY_ERR ret = LY_SUCCESS;
3419
Radek Krejcife909632019-02-12 15:34:42 +01003420 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003421 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003422 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003423 } else if (cont_p->musts) {
3424 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003425 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3426 cont->flags |= LYS_PRESENCE;
3427 } else if (cont_p->when) {
3428 /* container with a when condition */
3429 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 +02003430 cont->flags |= LYS_PRESENCE;
3431 } else if (cont_p->parent) {
3432 if (cont_p->parent->nodetype == LYS_CHOICE) {
3433 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003434 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 +02003435 cont_p->name, cont_p->parent->name);
3436 cont->flags |= LYS_PRESENCE;
3437 } else if ((cont_p->parent->nodetype == LYS_CASE)
3438 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3439 /* 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 +02003440 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 +02003441 cont_p->name, cont_p->parent->name);
3442 cont->flags |= LYS_PRESENCE;
3443 }
Radek Krejcife909632019-02-12 15:34:42 +01003444 }
3445
Michal Vaskoba417ac2020-08-06 14:48:20 +02003446 /* more cases when the container has meaning but is kept NP for convenience:
3447 * - when condition
3448 * - direct child action/notification
3449 */
3450
Radek Krejci19a96102018-11-15 13:38:09 +01003451 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003452 ret = lys_compile_node(ctx, child_p, node, 0);
3453 LY_CHECK_GOTO(ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003454 }
3455
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003456 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003457 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3458 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003459 ret = ly_set_add(&ctx->xpath, cont, 0, NULL);
3460 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003461 }
Radek Krejciec4da802019-05-02 13:02:41 +02003462 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3463 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003464
3465done:
3466 return ret;
3467}
3468
Radek Krejci33f72892019-02-21 10:36:58 +01003469/*
3470 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3471 * @param[in] ctx Compile context.
3472 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3473 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003474 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3475 * @return LY_ERR value.
3476 */
3477static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003478lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003479 struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003480{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003481 const char *dflt;
3482 struct lys_module *dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003483
Radek Krejciec4da802019-05-02 13:02:41 +02003484 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 +02003485 leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod));
3486
3487 /* store default value, if any */
3488 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
3489 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, dflt, dflt_mod));
Radek Krejci33f72892019-02-21 10:36:58 +01003490 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003491
Radek Krejci33f72892019-02-21 10:36:58 +01003492 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3493 /* 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 +02003494 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003495 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003496 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003497 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
3498 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
Radek Krejci33f72892019-02-21 10:36:58 +01003499 /* 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 +02003500 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003501 }
3502 }
3503 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003504 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3506 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3507 return LY_EVALID;
3508 }
3509 }
3510
Radek Krejci33f72892019-02-21 10:36:58 +01003511 return LY_SUCCESS;
3512}
3513
Radek Krejcia3045382018-11-22 14:30:31 +01003514/**
3515 * @brief Compile parsed leaf node information.
3516 * @param[in] ctx Compile context
3517 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003518 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3519 * is enriched with the leaf-specific information.
3520 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3521 */
Radek Krejci19a96102018-11-15 13:38:09 +01003522static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003523lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003524{
Michal Vasko22df3f02020-08-24 13:29:22 +02003525 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p;
3526 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003527 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003528 LY_ERR ret = LY_SUCCESS;
3529
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003530 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003531 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3532 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003533 ret = ly_set_add(&ctx->xpath, leaf, 0, NULL);
3534 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003535 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003536 if (leaf_p->units) {
3537 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3538 leaf->flags |= LYS_SET_UNITS;
3539 }
Radek Krejcia1911222019-07-22 17:24:50 +02003540
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003541 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003542 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3543 LY_CHECK_GOTO(ret, done);
Radek Krejcia1911222019-07-22 17:24:50 +02003544
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003545 /* store/update default value */
Radek Krejciccd20f12019-02-15 14:12:27 +01003546 if (leaf_p->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003547 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, leaf_p->dflt, ctx->mod_def));
Radek Krejci76b3e962018-12-14 17:01:25 +01003548 leaf->flags |= LYS_SET_DFLT;
3549 }
Radek Krejci43699232018-11-23 14:59:46 +01003550
Radek Krejci19a96102018-11-15 13:38:09 +01003551done:
3552 return ret;
3553}
3554
Radek Krejcia3045382018-11-22 14:30:31 +01003555/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003556 * @brief Compile parsed leaf-list node information.
3557 * @param[in] ctx Compile context
3558 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003559 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3560 * is enriched with the leaf-list-specific information.
3561 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3562 */
3563static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003564lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003565{
Michal Vasko22df3f02020-08-24 13:29:22 +02003566 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p;
3567 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003568 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003569 LY_ERR ret = LY_SUCCESS;
3570
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003571 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003572 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3573 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003574 ret = ly_set_add(&ctx->xpath, llist, 0, NULL);
3575 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003576 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003577 if (llist_p->units) {
3578 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3579 llist->flags |= LYS_SET_UNITS;
3580 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003581
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003582 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003583 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist);
3584 LY_CHECK_GOTO(ret, done);
Michal Vasko6a044b22020-01-15 12:25:39 +01003585
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003586 /* store/update default values */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003587 if (llist_p->dflts) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003588 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, llist_p->dflts, ctx->mod_def), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003589 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003590 }
3591
3592 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003593 if (llist->min) {
3594 llist->flags |= LYS_MAND_TRUE;
3595 }
Radek Krejcib7408632018-11-28 17:12:11 +01003596 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003597
Radek Krejci0e5d8382018-11-28 16:37:53 +01003598done:
3599 return ret;
3600}
3601
3602/**
Radek Krejci7af64242019-02-18 13:07:53 +01003603 * @brief Compile information about list's uniques.
3604 * @param[in] ctx Compile context.
3605 * @param[in] context_module Module where the prefixes are going to be resolved.
3606 * @param[in] uniques Sized array list of unique statements.
3607 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3608 * @return LY_ERR value.
3609 */
3610static LY_ERR
3611lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3612{
3613 LY_ERR ret = LY_SUCCESS;
3614 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003615 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003616 const char *keystr, *delim;
3617 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003618 LY_ARRAY_COUNT_TYPE v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003619 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003620 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003621
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003622 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003623 config = -1;
3624 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3625 keystr = uniques[v];
3626 while (keystr) {
3627 delim = strpbrk(keystr, " \t\n");
3628 if (delim) {
3629 len = delim - keystr;
3630 while (isspace(*delim)) {
3631 ++delim;
3632 }
3633 } else {
3634 len = strlen(keystr);
3635 }
3636
3637 /* unique node must be present */
3638 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko22df3f02020-08-24 13:29:22 +02003639 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0,
3640 (const struct lysc_node **)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003641 if (ret != LY_SUCCESS) {
3642 if (ret == LY_EDENIED) {
3643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003644 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003645 len, keystr, lys_nodetype2str((*key)->nodetype));
3646 }
3647 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003648 } else if (flags) {
3649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3650 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003651 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003652 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003653 }
3654
3655 /* all referenced leafs must be of the same config type */
3656 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003658 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003659 return LY_EVALID;
3660 } else if ((*key)->flags & LYS_CONFIG_W) {
3661 config = 1;
3662 } else { /* LYS_CONFIG_R */
3663 config = 0;
3664 }
3665
Michal Vasko14654712020-02-06 08:35:21 +01003666 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3667 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3668 if (parent->nodetype == LYS_LIST) {
3669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3670 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3671 return LY_EVALID;
3672 }
3673 }
3674
Radek Krejci7af64242019-02-18 13:07:53 +01003675 /* check status */
3676 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0f969882020-08-21 16:56:47 +02003677 (*key)->flags, (*key)->module, (*key)->name));
Radek Krejci7af64242019-02-18 13:07:53 +01003678
3679 /* mark leaf as unique */
3680 (*key)->flags |= LYS_UNIQUE;
3681
3682 /* next unique value in line */
3683 keystr = delim;
3684 }
3685 /* next unique definition */
3686 }
3687
3688 return LY_SUCCESS;
3689}
3690
3691/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003692 * @brief Compile parsed list node information.
3693 * @param[in] ctx Compile context
3694 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003695 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3696 * is enriched with the list-specific information.
3697 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3698 */
3699static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003700lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003701{
Michal Vasko22df3f02020-08-24 13:29:22 +02003702 struct lysp_node_list *list_p = (struct lysp_node_list *)node_p;
3703 struct lysc_node_list *list = (struct lysc_node_list *)node;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003704 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003705 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003706 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003707 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003708 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003709 LY_ERR ret = LY_SUCCESS;
3710
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003711 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003712 if (list->min) {
3713 list->flags |= LYS_MAND_TRUE;
3714 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003715 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3716
3717 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003718 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003719 }
3720
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003721 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003722 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3723 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003724 LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL));
Michal Vasko5d8756a2019-11-07 15:21:00 +01003725 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003726
3727 /* keys */
3728 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3730 return LY_EVALID;
3731 }
3732
3733 /* find all the keys (must be direct children) */
3734 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003735 if (!keystr) {
3736 /* keyless list */
3737 list->flags |= LYS_KEYLESS;
3738 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003739 while (keystr) {
3740 delim = strpbrk(keystr, " \t\n");
3741 if (delim) {
3742 len = delim - keystr;
3743 while (isspace(*delim)) {
3744 ++delim;
3745 }
3746 } else {
3747 len = strlen(keystr);
3748 }
3749
3750 /* key node must be present */
Michal Vasko22df3f02020-08-24 13:29:22 +02003751 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 +02003752 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3754 "The list's key \"%.*s\" not found.", len, keystr);
3755 return LY_EVALID;
3756 }
3757 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003758 if (key->flags & LYS_KEY) {
3759 /* the node was already marked as a key */
3760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3761 "Duplicated key identifier \"%.*s\".", len, keystr);
3762 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003763 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003764
Michal Vasko22df3f02020-08-24 13:29:22 +02003765 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003766 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003767 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3769 return LY_EVALID;
3770 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003771 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003772 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003773 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003775 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003776 return LY_EVALID;
3777 }
3778 } else {
3779 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003780 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003782 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003783 return LY_EVALID;
3784 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003785 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003787 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003788 return LY_EVALID;
3789 }
3790 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003791
3792 /* check status */
3793 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003794 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003795
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003796 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003797 if (key->dflt) {
3798 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3799 lysc_type_free(ctx->ctx, key->dflt->realtype);
3800 free(key->dflt);
3801 key->dflt = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003802 }
3803 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003804 key->flags |= LYS_KEY;
3805
3806 /* move it to the correct position */
Michal Vasko22df3f02020-08-24 13:29:22 +02003807 if ((prev_key && (struct lysc_node *)prev_key != key->prev) || (!prev_key && key->prev->next)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02003808 /* fix links in closest previous siblings of the key */
3809 if (key->next) {
3810 key->next->prev = key->prev;
3811 } else {
3812 /* last child */
3813 list->child->prev = key->prev;
3814 }
3815 if (key->prev->next) {
3816 key->prev->next = key->next;
3817 }
3818 /* fix links in the key */
3819 if (prev_key) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003820 key->prev = (struct lysc_node *)prev_key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003821 key->next = prev_key->next;
3822 } else {
3823 key->prev = list->child->prev;
3824 key->next = list->child;
3825 }
3826 /* fix links in closes future siblings of the key */
3827 if (prev_key) {
3828 if (prev_key->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003829 prev_key->next->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003830 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003831 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003832 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003833 prev_key->next = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003834 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003835 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003836 }
3837 /* fix links in parent */
3838 if (!key->prev->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003839 list->child = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003840 }
3841 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003842
3843 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003844 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003845 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003846 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003847 }
3848
3849 /* uniques */
3850 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003851 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003852 }
3853
Radek Krejciec4da802019-05-02 13:02:41 +02003854 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3855 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003856
3857done:
3858 return ret;
3859}
3860
Radek Krejcib56c7502019-02-13 14:19:54 +01003861/**
3862 * @brief Do some checks and set the default choice's case.
3863 *
3864 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3865 *
3866 * @param[in] ctx Compile context.
3867 * @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,
3868 * not the reference to the imported module.
3869 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3870 * @return LY_ERR value.
3871 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003872static LY_ERR
3873lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3874{
Michal Vasko22df3f02020-08-24 13:29:22 +02003875 struct lysc_node *iter, *node = (struct lysc_node *)ch;
Radek Krejci76b3e962018-12-14 17:01:25 +01003876 const char *prefix = NULL, *name;
3877 size_t prefix_len = 0;
3878
3879 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3880 name = strchr(dflt, ':');
3881 if (name) {
3882 prefix = dflt;
3883 prefix_len = name - prefix;
3884 ++name;
3885 } else {
3886 name = dflt;
3887 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003888 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003889 /* prefixed default case make sense only for the prefix of the schema itself */
3890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3891 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3892 prefix_len, prefix);
3893 return LY_EVALID;
3894 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003895 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 +01003896 if (!ch->dflt) {
3897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3898 "Default case \"%s\" not found.", dflt);
3899 return LY_EVALID;
3900 }
3901 /* no mandatory nodes directly under the default case */
3902 LY_LIST_FOR(ch->dflt->child, iter) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003903 if (iter->parent != (struct lysc_node *)ch->dflt) {
Radek Krejcife13da42019-02-15 14:51:01 +01003904 break;
3905 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003906 if (iter->flags & LYS_MAND_TRUE) {
3907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3908 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3909 return LY_EVALID;
3910 }
3911 }
Radek Krejci01342af2019-01-03 15:18:08 +01003912 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003913 return LY_SUCCESS;
3914}
3915
Radek Krejciccd20f12019-02-15 14:12:27 +01003916static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003917lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003918{
3919 struct lys_module *mod;
3920 const char *prefix = NULL, *name;
3921 size_t prefix_len = 0;
3922 struct lysc_node_case *cs;
3923 struct lysc_node *node;
3924
3925 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3926 name = strchr(dflt, ':');
3927 if (name) {
3928 prefix = dflt;
3929 prefix_len = name - prefix;
3930 ++name;
3931 } else {
3932 name = dflt;
3933 }
3934 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3935 if (prefix) {
3936 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003938 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3939 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003940 return LY_EVALID;
3941 }
3942 } else {
3943 mod = ctx->mod;
3944 }
3945 /* get the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003946 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 +01003947 if (!cs) {
3948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003949 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003950 return LY_EVALID;
3951 }
3952
3953 /* check that there is no mandatory node */
3954 LY_LIST_FOR(cs->child, node) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003955 if (node->parent != (struct lysc_node *)cs) {
Radek Krejcife13da42019-02-15 14:51:01 +01003956 break;
3957 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003958 if (node->flags & LYS_MAND_TRUE) {
3959 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003960 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3961 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003962 return LY_EVALID;
3963 }
3964 }
3965
3966 /* set the default case in choice */
3967 ch->dflt = cs;
3968 cs->flags |= LYS_SET_DFLT;
3969
3970 return LY_SUCCESS;
3971}
3972
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003973/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003974 * @brief Compile parsed choice node information.
3975 * @param[in] ctx Compile context
3976 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003977 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003978 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003979 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3980 */
3981static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003982lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003983{
Michal Vasko22df3f02020-08-24 13:29:22 +02003984 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p;
3985 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
Radek Krejci056d0a82018-12-06 16:57:25 +01003986 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003987 LY_ERR ret = LY_SUCCESS;
3988
Radek Krejci056d0a82018-12-06 16:57:25 +01003989 LY_LIST_FOR(ch_p->child, child_p) {
3990 if (child_p->nodetype == LYS_CASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003991 LY_LIST_FOR(((struct lysp_node_case *)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003992 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003993 }
3994 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003995 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003996 }
3997 }
3998
3999 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004000 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004001 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004002 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004003
Radek Krejci9800fb82018-12-13 14:26:23 +01004004 return ret;
4005}
4006
4007/**
4008 * @brief Compile parsed anydata or anyxml node information.
4009 * @param[in] ctx Compile context
4010 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004011 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4012 * is enriched with the any-specific information.
4013 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4014 */
4015static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004016lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004017{
Michal Vasko22df3f02020-08-24 13:29:22 +02004018 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p;
4019 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004020 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9800fb82018-12-13 14:26:23 +01004021 LY_ERR ret = LY_SUCCESS;
4022
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004023 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004024 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4025 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004026 ret = ly_set_add(&ctx->xpath, any, 0, NULL);
4027 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004028 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004029
4030 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004031 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4032 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004033 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004034done:
4035 return ret;
4036}
4037
Radek Krejcib56c7502019-02-13 14:19:54 +01004038/**
Michal Vasko795b3752020-07-13 15:24:27 +02004039 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4040 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004041 *
4042 * @param[in] ctx Compile context
4043 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4044 * the choice itself is expected instead of a specific case node.
4045 * @param[in] node Schema node to connect into the list.
4046 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004047 * 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 +01004048 */
4049static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004050lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004051{
Michal Vasko795b3752020-07-13 15:24:27 +02004052 struct lysc_node **children, *start, *end;
4053 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004054
4055 if (node->nodetype == LYS_CASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004056 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
Radek Krejci056d0a82018-12-06 16:57:25 +01004057 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004058 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004059 }
4060 if (children) {
4061 if (!(*children)) {
4062 /* first child */
4063 *children = node;
4064 } else if (*children != node) {
4065 /* by the condition in previous branch we cover the choice/case children
4066 * - the children list is shared by the choice and the the first case, in addition
4067 * the first child of each case must be referenced from the case node. So the node is
4068 * actually always already inserted in case it is the first children - so here such
4069 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004070 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4071 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4072 /* some augments are already connected and we are connecting new ones,
4073 * keep module name order and insert the node into the children list */
4074 end = (*children);
4075 do {
4076 end = end->prev;
4077 mod = end->module;
4078 while (end->prev->module == mod) {
4079 end = end->prev;
4080 }
Radek Krejcif6a11002020-08-21 13:29:07 +02004081 } 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 +02004082
4083 /* we have the last existing node after our node, easily get the first before and connect it */
4084 start = end->prev;
4085 start->next = node;
4086 node->next = end;
4087 end->prev = node;
4088 node->prev = start;
4089 } else {
4090 /* insert at the end of the parent's children list */
4091 (*children)->prev->next = node;
4092 node->prev = (*children)->prev;
4093 (*children)->prev = node;
4094 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004095
4096 /* check the name uniqueness */
4097 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4098 lysc_node_notifs(parent), node->name, node)) {
4099 return LY_EEXIST;
4100 }
4101 }
4102 }
4103 return LY_SUCCESS;
4104}
4105
Radek Krejci95710c92019-02-11 15:49:55 +01004106/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004107 * @brief Prepare the case structure in choice node for the new data node.
4108 *
4109 * 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
4110 * created in the choice when the first child was processed.
4111 *
4112 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004113 * @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 +02004114 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004115 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4116 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4117 * @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,
4118 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004119 */
Michal Vasko22df3f02020-08-24 13:29:22 +02004120static struct lysc_node_case *
Radek Krejciec4da802019-05-02 13:02:41 +02004121lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child)
Radek Krejci056d0a82018-12-06 16:57:25 +01004122{
4123 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004124 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004125 struct lysc_when **when;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004126 LY_ARRAY_COUNT_TYPE u;
Radek Krejci056d0a82018-12-06 16:57:25 +01004127 LY_ERR ret;
4128
Radek Krejci95710c92019-02-11 15:49:55 +01004129#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004130 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004131 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004132 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4133 return NULL; \
4134 } \
4135 }
4136
Radek Krejci39424802020-08-12 09:31:00 +02004137 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci95710c92019-02-11 15:49:55 +01004138 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004139
4140 /* we have to add an implicit case node into the parent choice */
4141 cs = calloc(1, sizeof(struct lysc_node_case));
4142 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02004143 cs->parent = (struct lysc_node *)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004144 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004145 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004146 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4147 /* the case is already present since the child is not its first children */
Michal Vasko22df3f02020-08-24 13:29:22 +02004148 return (struct lysc_node_case *)ch->cases->prev;
Radek Krejci056d0a82018-12-06 16:57:25 +01004149 }
Radek Krejci95710c92019-02-11 15:49:55 +01004150 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004151
4152 /* explicit parent case is not present (this is its first child) */
4153 cs = calloc(1, sizeof(struct lysc_node_case));
4154 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02004155 cs->parent = (struct lysc_node *)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004156 cs->flags = LYS_STATUS_MASK & node_p->flags;
4157 cs->sp = node_p;
4158
Radek Krejcib1b59152019-01-07 13:21:56 +01004159 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
Radek Krejcibae745f2019-04-09 16:28:00 +02004160 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004161
4162 if (node_p->when) {
4163 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004164 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004165 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004166
4167 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4168 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004169 ret = ly_set_add(&ctx->xpath, cs, 0, NULL);
4170 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004171 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004172 }
Radek Krejciec4da802019-05-02 13:02:41 +02004173 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004174 } else {
4175 LOGINT(ctx->ctx);
4176 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004177 }
4178 cs->module = ctx->mod;
Michal Vasko22df3f02020-08-24 13:29:22 +02004179 cs->prev = (struct lysc_node *)cs;
Radek Krejci056d0a82018-12-06 16:57:25 +01004180 cs->nodetype = LYS_CASE;
Michal Vasko22df3f02020-08-24 13:29:22 +02004181 lys_compile_node_connect(ctx, (struct lysc_node *)ch, (struct lysc_node *)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004182 cs->child = child;
4183
4184 return cs;
4185error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004186 if (cs) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004187 lysc_node_free(ctx->ctx, (struct lysc_node *)cs);
Radek Krejci1b1e9252019-04-24 08:45:50 +02004188 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004189 return NULL;
4190
4191#undef UNIQUE_CHECK
4192}
4193
Radek Krejcib56c7502019-02-13 14:19:54 +01004194/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004195 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004196 *
4197 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004198 * @param[in] node Target node where the config is supposed to be changed.
4199 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004200 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4201 * 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 +01004202 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4203 * @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 +01004204 * @return LY_ERR value.
4205 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004206static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004207lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci1deb5be2020-08-26 16:43:36 +02004208 uint8_t inheriting, uint8_t refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004209{
4210 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004211 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004212
4213 if (config == (node->flags & LYS_CONFIG_MASK)) {
4214 /* nothing to do */
4215 return LY_SUCCESS;
4216 }
4217
4218 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004219 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004220 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004222 "Invalid %s of config - configuration node cannot be child of any state data node.",
4223 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004224 return LY_EVALID;
4225 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004226 node->flags |= LYS_SET_CONFIG;
4227 } else {
4228 if (node->flags & LYS_SET_CONFIG) {
4229 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4230 /* setting config flags, but have node with explicit config true */
4231 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004232 "Invalid %s of config - configuration node cannot be child of any state data node.",
4233 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004234 return LY_EVALID;
4235 }
4236 /* do not change config on nodes where the config is explicitely set, this does not apply to
4237 * nodes, which are being changed explicitly (targets of refine or deviation) */
4238 return LY_SUCCESS;
4239 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004240 }
4241 node->flags &= ~LYS_CONFIG_MASK;
4242 node->flags |= config;
4243
4244 /* inherit the change into the children */
Michal Vasko22df3f02020-08-24 13:29:22 +02004245 LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004246 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004247 }
4248
Radek Krejci76b3e962018-12-14 17:01:25 +01004249 return LY_SUCCESS;
4250}
4251
Radek Krejcib56c7502019-02-13 14:19:54 +01004252/**
4253 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4254 *
4255 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4256 * the flag to such parents from a mandatory children.
4257 *
4258 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4259 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4260 * (mandatory children was removed).
4261 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02004262static void
4263lys_compile_mandatory_parents(struct lysc_node *parent, uint8_t add)
Radek Krejcife909632019-02-12 15:34:42 +01004264{
4265 struct lysc_node *iter;
4266
4267 if (add) { /* set flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004268 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
Radek Krejcife909632019-02-12 15:34:42 +01004269 parent = parent->parent) {
4270 parent->flags |= LYS_MAND_TRUE;
4271 }
4272 } else { /* unset flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004273 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004274 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004275 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004276 /* there is another mandatory node */
4277 return;
4278 }
4279 }
4280 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4281 parent->flags &= ~LYS_MAND_TRUE;
4282 }
4283 }
4284}
4285
Radek Krejci056d0a82018-12-06 16:57:25 +01004286/**
Radek Krejci3641f562019-02-13 15:38:40 +01004287 * @brief Internal sorting process for the lys_compile_augment_sort().
4288 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4289 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4290 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4291 */
4292static void
4293lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4294{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004295 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004296 size_t len;
4297
4298 len = strlen(aug_p->nodeid);
4299 LY_ARRAY_FOR(result, v) {
4300 if (strlen(result[v]->nodeid) <= len) {
4301 continue;
4302 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004303 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004304 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004305 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004306 break;
4307 }
4308 }
4309 result[v] = aug_p;
4310 LY_ARRAY_INCREMENT(result);
4311}
4312
4313/**
4314 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4315 *
4316 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4317 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4318 *
4319 * @param[in] ctx Compile context.
4320 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4321 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4322 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4323 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4324 * @return LY_ERR value.
4325 */
4326LY_ERR
4327lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4328{
4329 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004330 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004331
4332 assert(augments);
4333
4334 /* get count of the augments in module and all its submodules */
4335 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004336 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004337 }
4338 LY_ARRAY_FOR(inc_p, u) {
4339 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004340 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004341 }
4342 }
4343
4344 if (!count) {
4345 *augments = NULL;
4346 return LY_SUCCESS;
4347 }
4348 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4349
4350 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4351 * together, so there can be even /z/y betwwen them. */
4352 LY_ARRAY_FOR(aug_p, u) {
4353 lys_compile_augment_sort_(&aug_p[u], result);
4354 }
4355 LY_ARRAY_FOR(inc_p, u) {
4356 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4357 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4358 }
4359 }
4360
4361 *augments = result;
4362 return LY_SUCCESS;
4363}
4364
4365/**
4366 * @brief Compile the parsed augment connecting it into its target.
4367 *
4368 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4369 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4370 * are already implemented and compiled.
4371 *
4372 * @param[in] ctx Compile context.
4373 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004374 * @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
4375 * children in case of the augmenting uses data.
4376 * @return LY_SUCCESS on success.
4377 * @return LY_EVALID on failure.
4378 */
4379LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004380lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004381{
Michal Vaskoe6143202020-07-03 13:02:08 +02004382 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004383 struct lysp_node *node_p, *case_node_p;
4384 struct lysc_node *target; /* target target of the augment */
4385 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004386 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004387 struct lys_module **aug_mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004388 uint8_t allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004389 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004390 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004391 uint32_t opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004392
Radek Krejci327de162019-06-14 12:52:07 +02004393 lysc_update_path(ctx, NULL, "{augment}");
4394 lysc_update_path(ctx, NULL, aug_p->nodeid);
4395
Michal Vaskocb18c7f2020-08-24 09:22:28 +02004396 ret = lysc_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004397 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Michal Vasko22df3f02020-08-24 13:29:22 +02004398 1, (const struct lysc_node **)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004399 if (ret != LY_SUCCESS) {
4400 if (ret == LY_EDENIED) {
4401 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4402 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4403 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4404 }
4405 return LY_EVALID;
4406 }
4407
4408 /* check for mandatory nodes
4409 * - new cases augmenting some choice can have mandatory nodes
4410 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4411 */
Radek Krejci733988a2019-02-15 15:12:44 +01004412 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004413 allow_mandatory = 1;
4414 }
4415
4416 when_shared = NULL;
4417 LY_LIST_FOR(aug_p->child, node_p) {
4418 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4419 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004420 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004421 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4422 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004423 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004424 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4425 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004426 return LY_EVALID;
4427 }
4428
4429 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004430 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004431 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004432 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004433 } else {
4434 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004435 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004436 }
4437 }
Radek Krejciec4da802019-05-02 13:02:41 +02004438 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004439
Radek Krejcife13da42019-02-15 14:51:01 +01004440 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4441 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004442 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004443 /* 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 +02004444 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 +01004445 } else if (target->nodetype == LYS_CHOICE) {
4446 /* 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 +02004447 node = ((struct lysc_node_choice *)target)->cases->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004448 } else {
4449 /* the compiled node is the last child of the target */
Michal Vasko22df3f02020-08-24 13:29:22 +02004450 node = (struct lysc_node *)lysc_node_children(target, flags);
Radek Krejci7c7783d2020-04-08 15:34:39 +02004451 if (!node) {
4452 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4453 break;
4454 }
4455 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004456 }
4457
Radek Krejci733988a2019-02-15 15:12:44 +01004458 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004459 node->flags &= ~LYS_MAND_TRUE;
4460 lys_compile_mandatory_parents(target, 0);
4461 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004462 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004463 return LY_EVALID;
4464 }
4465
4466 /* pass augment's when to all the children */
4467 if (aug_p->when) {
4468 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4469 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004470 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004471 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004472
4473 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4474 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004475 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4476 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004477 }
4478
Radek Krejci3641f562019-02-13 15:38:40 +01004479 when_shared = *when;
4480 } else {
4481 ++when_shared->refcount;
4482 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004483
4484 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4485 /* in this case check "when" again for all children because of dummy node check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004486 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4487 LY_CHECK_GOTO(ret, error);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004488 }
Radek Krejci3641f562019-02-13 15:38:40 +01004489 }
4490 }
4491 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004492
Radek Krejciec4da802019-05-02 13:02:41 +02004493 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004494 switch (target->nodetype) {
4495 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004496 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004497 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004498 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004499 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004500 break;
4501 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004502 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004503 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004504 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004505 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004506 break;
4507 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004508 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004509 if (aug_p->actions) {
4510 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004511 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4512 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004513 return LY_EVALID;
4514 }
4515 if (aug_p->notifs) {
4516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004517 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004518 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004519 return LY_EVALID;
4520 }
4521 }
Radek Krejci3641f562019-02-13 15:38:40 +01004522
Michal Vaskoe6143202020-07-03 13:02:08 +02004523 /* add this module into the target module augmented_by, if not there already */
4524 rc = LY_SUCCESS;
4525 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4526 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4527 rc = LY_EEXIST;
4528 break;
4529 }
4530 }
4531 if (!rc) {
4532 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4533 *aug_mod = ctx->mod;
4534 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004535
Radek Krejci327de162019-06-14 12:52:07 +02004536 lysc_update_path(ctx, NULL, NULL);
4537 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004538
Radek Krejci3641f562019-02-13 15:38:40 +01004539error:
Radek Krejciec4da802019-05-02 13:02:41 +02004540 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004541 return ret;
4542}
4543
4544/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004545 * @brief Apply refined or deviated mandatory flag to the target node.
4546 *
4547 * @param[in] ctx Compile context.
4548 * @param[in] node Target node where the mandatory property is supposed to be changed.
4549 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004550 * @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 +01004551 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4552 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4553 * 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 +01004554 * @return LY_ERR value.
4555 */
4556static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004557lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, uint8_t refine_flag)
Radek Krejcif1421c22019-02-19 13:05:20 +01004558{
4559 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4560 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004561 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4562 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004563 return LY_EVALID;
4564 }
4565
4566 if (mandatory_flag & LYS_MAND_TRUE) {
4567 /* check if node has default value */
4568 if (node->nodetype & LYS_LEAF) {
4569 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004570 if (refine_flag) {
4571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004572 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004573 return LY_EVALID;
4574 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004575 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004576 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004577 if (refine_flag) {
4578 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004579 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004580 return LY_EVALID;
4581 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004582 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004583 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004584 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 +01004585 return LY_EVALID;
4586 }
4587
4588 node->flags &= ~LYS_MAND_FALSE;
4589 node->flags |= LYS_MAND_TRUE;
4590 lys_compile_mandatory_parents(node->parent, 1);
4591 } else {
4592 /* make mandatory false */
4593 node->flags &= ~LYS_MAND_TRUE;
4594 node->flags |= LYS_MAND_FALSE;
4595 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcif1421c22019-02-19 13:05:20 +01004596 }
4597 return LY_SUCCESS;
4598}
4599
4600/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004601 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4602 * If present, also apply uses's modificators.
4603 *
4604 * @param[in] ctx Compile context
4605 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004606 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4607 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4608 * the compile context.
4609 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4610 */
4611static LY_ERR
Radek Krejcic6b4f442020-08-12 14:45:18 +02004612lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p)
Radek Krejcie86bf772018-12-14 11:39:53 +01004613{
4614 struct lysp_node *node_p;
Radek Krejcic6b4f442020-08-12 14:45:18 +02004615 struct lysc_node *node, *child, *iter;
Radek Krejci12fb9142019-01-08 09:45:30 +01004616 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004617 struct lysc_node_container context_node_fake =
Radek Krejci0f969882020-08-21 16:56:47 +02004618 {.nodetype = LYS_CONTAINER,
4619 .module = ctx->mod,
4620 .flags = parent ? parent->flags : 0,
4621 .child = NULL, .next = NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +02004622 .prev = (struct lysc_node *)&context_node_fake,
Radek Krejci0f969882020-08-21 16:56:47 +02004623 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004624 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004625 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004626 uint32_t grp_stack_count;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004627 uint8_t found = 0;
Radek Krejcie86bf772018-12-14 11:39:53 +01004628 const char *id, *name, *prefix;
4629 size_t prefix_len, name_len;
4630 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004631 struct lysp_refine *rfn;
Radek Krejciba03a5a2020-08-27 14:40:41 +02004632 LY_ERR rc, ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004633 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004634 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004635 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004636 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004637 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004638 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004639 struct lysc_notif **notifs = NULL;
4640 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004641
4642 /* search for the grouping definition */
Radek Krejcie86bf772018-12-14 11:39:53 +01004643 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004644 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004645 if (prefix) {
4646 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4647 if (!mod) {
4648 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004649 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004650 return LY_EVALID;
4651 }
4652 } else {
4653 mod = ctx->mod_def;
4654 }
4655 if (mod == ctx->mod_def) {
4656 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004657 grp = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004658 LY_ARRAY_FOR(grp, u) {
4659 if (!strcmp(grp[u].name, name)) {
4660 grp = &grp[u];
4661 found = 1;
4662 break;
4663 }
4664 }
4665 }
4666 }
4667 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004668 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004669 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004670 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004671 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004672 if (!strcmp(grp[u].name, name)) {
4673 grp = &grp[u];
4674 found = 1;
4675 }
4676 }
4677 }
4678 if (!found && mod->parsed->includes) {
4679 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004680 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004681 grp = mod->parsed->includes[u].submodule->groupings;
4682 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004683 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004684 if (!strcmp(grp[v].name, name)) {
4685 grp = &grp[v];
4686 found = 1;
4687 }
4688 }
4689 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004690 }
4691 }
4692 }
4693 if (!found) {
4694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4695 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4696 return LY_EVALID;
4697 }
4698
4699 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4700 grp_stack_count = ctx->groupings.count;
Radek Krejciba03a5a2020-08-27 14:40:41 +02004701 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
Radek Krejcie86bf772018-12-14 11:39:53 +01004702 if (grp_stack_count == ctx->groupings.count) {
4703 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4704 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4705 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4706 return LY_EVALID;
4707 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004708 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4709 /* remember that the grouping is instantiated to avoid its standalone validation */
4710 grp->flags |= LYS_USED_GRP;
4711 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004712
4713 /* switch context's mod_def */
4714 mod_old = ctx->mod_def;
4715 ctx->mod_def = mod;
4716
4717 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004718 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup);
Radek Krejcie86bf772018-12-14 11:39:53 +01004719
Radek Krejcic6b4f442020-08-12 14:45:18 +02004720 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
4721 * applu refine and augment only to the nodes from the uses */
4722 if (parent) {
4723 if (parent->nodetype == LYS_CASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004724 child = (struct lysc_node *)lysc_node_children(parent->parent, ctx->options & LYSC_OPT_RPC_MASK);
Radek Krejcic6b4f442020-08-12 14:45:18 +02004725 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02004726 child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
Radek Krejcic6b4f442020-08-12 14:45:18 +02004727 }
4728 } else if (ctx->mod->compiled->data) {
4729 child = ctx->mod->compiled->data;
4730 } else {
4731 child = NULL;
4732 }
4733 /* remember the last child */
4734 if (child) {
4735 child = child->prev;
4736 }
4737
Radek Krejcifc11bd72019-04-11 16:00:05 +02004738 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004739 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004740 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
Radek Krejciec4da802019-05-02 13:02:41 +02004741 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01004742 }
Radek Krejcic6b4f442020-08-12 14:45:18 +02004743
4744 /* split the children and add the uses's data into the fake context node */
4745 if (child) {
4746 context_node_fake.child = child->next;
4747 } else if (parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004748 context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
Radek Krejcic6b4f442020-08-12 14:45:18 +02004749 } else if (ctx->mod->compiled->data) {
4750 context_node_fake.child = ctx->mod->compiled->data;
4751 }
4752 if (context_node_fake.child) {
4753 /* remember child as the last data node added by grouping to fix the list later */
4754 child = context_node_fake.child->prev;
4755 context_node_fake.child->prev = NULL;
4756 }
4757
Radek Krejci00b874b2019-02-12 10:54:50 +01004758 when_shared = NULL;
Radek Krejcic6b4f442020-08-12 14:45:18 +02004759 LY_LIST_FOR(context_node_fake.child, iter) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004760 iter->parent = (struct lysc_node *)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004761
Radek Krejcifc11bd72019-04-11 16:00:05 +02004762 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004763 if (uses_p->when) {
Radek Krejcic6b4f442020-08-12 14:45:18 +02004764 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004765 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004766 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4767
4768 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4769 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004770 rc = ly_set_add(&ctx->xpath, iter, 0, NULL);
4771 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01004772 }
4773
Radek Krejci00b874b2019-02-12 10:54:50 +01004774 when_shared = *when;
4775 } else {
4776 ++when_shared->refcount;
4777 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004778
4779 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4780 /* in this case check "when" again for all children because of dummy node check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004781 rc = ly_set_add(&ctx->xpath, iter, 0, NULL);
4782 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004783 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004784 }
4785 }
Radek Krejci01342af2019-01-03 15:18:08 +01004786 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004787
Radek Krejcifc11bd72019-04-11 16:00:05 +02004788 /* compile actions */
4789 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4790 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004791 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004792 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004793 if (*actions && (uses_p->augments || uses_p->refines)) {
4794 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004795 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4796 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4797 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004798 }
4799 }
4800
4801 /* compile notifications */
4802 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4803 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004804 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004805 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004806 if (*notifs && (uses_p->augments || uses_p->refines)) {
4807 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004808 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4809 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4810 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004811 }
4812 }
4813
Radek Krejci3641f562019-02-13 15:38:40 +01004814 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004815 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004816 LY_ARRAY_FOR(augments, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004817 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004818 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004819
Radek Krejcif0089082019-01-07 16:42:01 +01004820 /* reload previous context's mod_def */
4821 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004822 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004823
Radek Krejci76b3e962018-12-14 17:01:25 +01004824 /* apply refine */
4825 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004826 lysc_update_path(ctx, NULL, rfn->nodeid);
4827
Michal Vasko22df3f02020-08-24 13:29:22 +02004828 LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node *)&context_node_fake, ctx->mod,
4829 0, 0, (const struct lysc_node **)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004830 cleanup);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004831 rc = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL);
4832 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004833
4834 /* default value */
4835 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004836 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004837 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci0f969882020-08-21 16:56:47 +02004838 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE " default values.",
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004839 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004840 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004841 }
4842 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4843 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004844 "Invalid refine of default - %s cannot hold default value(s).",
4845 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004846 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004847 }
4848 if (node->nodetype == LYS_LEAF) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004849 /* postpone default compilation when the tree is complete */
4850 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0],
4851 ctx->mod_def), cleanup);
4852 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004853 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004854 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004855 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4856 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004857 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004858 }
Radek Krejcia1911222019-07-22 17:24:50 +02004859
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004860 /* postpone default compilation when the tree is complete */
4861 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts,
4862 ctx->mod_def), cleanup);
4863 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004864 } else if (node->nodetype == LYS_CHOICE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004865 if (((struct lysc_node_choice *)node)->dflt) {
Radek Krejci01342af2019-01-03 15:18:08 +01004866 /* unset LYS_SET_DFLT from the current default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02004867 ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT;
Radek Krejci01342af2019-01-03 15:18:08 +01004868 }
Michal Vasko22df3f02020-08-24 13:29:22 +02004869 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004870 }
4871 }
4872
Radek Krejci12fb9142019-01-08 09:45:30 +01004873 /* description */
4874 if (rfn->dsc) {
4875 FREE_STRING(ctx->ctx, node->dsc);
4876 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4877 }
4878
4879 /* reference */
4880 if (rfn->ref) {
4881 FREE_STRING(ctx->ctx, node->ref);
4882 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4883 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004884
4885 /* config */
4886 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004887 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004888 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004889 } else {
4890 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004891 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004892 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004893 }
4894
4895 /* mandatory */
4896 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004897 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004898 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004899
4900 /* presence */
4901 if (rfn->presence) {
4902 if (node->nodetype != LYS_CONTAINER) {
4903 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004904 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4905 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004906 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004907 }
4908 node->flags |= LYS_PRESENCE;
4909 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004910
4911 /* must */
4912 if (rfn->musts) {
4913 switch (node->nodetype) {
4914 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02004915 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 +01004916 break;
4917 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004918 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 +01004919 break;
4920 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004921 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 +01004922 break;
4923 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004924 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 +01004925 break;
4926 case LYS_ANYXML:
4927 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02004928 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 +01004929 break;
4930 default:
4931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004932 "Invalid refine of must statement - %s cannot hold any must statement.",
4933 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004934 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004935 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02004936 rc = ly_set_add(&ctx->xpath, node, 0, NULL);
4937 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004938 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004939
4940 /* min/max-elements */
4941 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4942 switch (node->nodetype) {
4943 case LYS_LEAFLIST:
4944 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004945 ((struct lysc_node_leaflist *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004946 }
4947 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004948 ((struct lysc_node_leaflist *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004949 if (rfn->min) {
4950 node->flags |= LYS_MAND_TRUE;
4951 lys_compile_mandatory_parents(node->parent, 1);
4952 } else {
4953 node->flags &= ~LYS_MAND_TRUE;
4954 lys_compile_mandatory_parents(node->parent, 0);
4955 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004956 }
4957 break;
4958 case LYS_LIST:
4959 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004960 ((struct lysc_node_list *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004961 }
4962 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004963 ((struct lysc_node_list *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004964 if (rfn->min) {
4965 node->flags |= LYS_MAND_TRUE;
4966 lys_compile_mandatory_parents(node->parent, 1);
4967 } else {
4968 node->flags &= ~LYS_MAND_TRUE;
4969 lys_compile_mandatory_parents(node->parent, 0);
4970 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004971 }
4972 break;
4973 default:
4974 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004975 "Invalid refine of %s statement - %s cannot hold this statement.",
Radek Krejci0f969882020-08-21 16:56:47 +02004976 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004977 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004978 }
4979 }
Radek Krejcif0089082019-01-07 16:42:01 +01004980
4981 /* if-feature */
4982 if (rfn->iffeatures) {
4983 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004984 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004985 }
Radek Krejci327de162019-06-14 12:52:07 +02004986
4987 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004988 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004989
Radek Krejcif2271f12019-01-07 16:42:23 +01004990 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004991 for (uint32_t i = 0; i < refined.count; ++i) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004992 node = (struct lysc_node *)refined.objs[i];
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004993 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02004994 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004995
4996 /* check possible conflict with default value (default added, mandatory left true) */
4997 if ((node->flags & LYS_MAND_TRUE) &&
Michal Vasko22df3f02020-08-24 13:29:22 +02004998 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) ||
Radek Krejcif2271f12019-01-07 16:42:23 +01004999 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005001 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005002 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005003 }
5004
5005 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5006 if (node->nodetype == LYS_LIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005007 min = ((struct lysc_node_list *)node)->min;
5008 max = ((struct lysc_node_list *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01005009 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02005010 min = ((struct lysc_node_leaflist *)node)->min;
5011 max = ((struct lysc_node_leaflist *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01005012 }
5013 if (min > max) {
5014 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005015 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
Radek Krejci0f969882020-08-21 16:56:47 +02005016 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005017 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005018 }
5019 }
5020 }
5021
Radek Krejcic6b4f442020-08-12 14:45:18 +02005022 if (first_p) {
5023 *first_p = context_node_fake.child;
5024 }
Radek Krejci327de162019-06-14 12:52:07 +02005025 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005026 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005027
5028cleanup:
5029 /* fix connection of the children nodes from fake context node back into the parent */
5030 if (context_node_fake.child) {
5031 context_node_fake.child->prev = child;
5032 }
5033 LY_LIST_FOR(context_node_fake.child, child) {
5034 child->parent = parent;
5035 }
5036
5037 if (uses_p->augments || uses_p->refines) {
5038 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005039 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005040 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005041 LY_ARRAY_FREE(context_node_fake.actions);
5042 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005043 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005044 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005045 LY_ARRAY_FREE(context_node_fake.notifs);
5046 }
5047 }
5048
Radek Krejcie86bf772018-12-14 11:39:53 +01005049 /* reload previous context's mod_def */
5050 ctx->mod_def = mod_old;
5051 /* remove the grouping from the stack for circular groupings dependency check */
5052 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5053 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005054 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005055 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005056
5057 return ret;
5058}
5059
Radek Krejci327de162019-06-14 12:52:07 +02005060static int
5061lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5062{
5063 struct lysp_node *iter;
5064 int len = 0;
5065
5066 *path = NULL;
5067 for (iter = node; iter && len >= 0; iter = iter->parent) {
5068 char *s = *path;
5069 char *id;
5070
5071 switch (iter->nodetype) {
5072 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005073 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005074 break;
5075 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005076 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005077 break;
5078 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005079 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005080 break;
5081 default:
5082 id = strdup(iter->name);
5083 break;
5084 }
5085
5086 if (!iter->parent) {
5087 /* print prefix */
5088 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5089 } else {
5090 /* prefix is the same as in parent */
5091 len = asprintf(path, "/%s%s", id, s ? s : "");
5092 }
5093 free(s);
5094 free(id);
5095 }
5096
5097 if (len < 0) {
5098 free(*path);
5099 *path = NULL;
5100 } else if (len == 0) {
5101 *path = strdup("/");
5102 len = 1;
5103 }
5104 return len;
5105}
5106
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005107/**
5108 * @brief Validate groupings that were defined but not directly used in the schema itself.
5109 *
5110 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5111 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5112 */
5113static LY_ERR
5114lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5115{
5116 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005117 char *path;
5118 int len;
5119
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005120 struct lysp_node_uses fake_uses = {
5121 .parent = node_p,
5122 .nodetype = LYS_USES,
5123 .flags = 0, .next = NULL,
5124 .name = grp->name,
5125 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5126 .refines = NULL, .augments = NULL
5127 };
5128 struct lysc_node_container fake_container = {
5129 .nodetype = LYS_CONTAINER,
5130 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5131 .module = ctx->mod,
5132 .sp = NULL, .parent = NULL, .next = NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +02005133 .prev = (struct lysc_node *)&fake_container,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005134 .name = "fake",
5135 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5136 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5137 };
5138
5139 if (grp->parent) {
5140 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5141 }
Radek Krejci327de162019-06-14 12:52:07 +02005142
5143 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5144 if (len < 0) {
5145 LOGMEM(ctx->ctx);
5146 return LY_EMEM;
5147 }
5148 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005149 ctx->path_len = (uint32_t)len;
Radek Krejci327de162019-06-14 12:52:07 +02005150 free(path);
5151
5152 lysc_update_path(ctx, NULL, "{grouping}");
5153 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02005154 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005155 lysc_update_path(ctx, NULL, NULL);
5156 lysc_update_path(ctx, NULL, NULL);
5157
5158 ctx->path_len = 1;
5159 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005160
5161 /* cleanup */
5162 lysc_node_container_free(ctx->ctx, &fake_container);
5163
5164 return ret;
5165}
Radek Krejcife909632019-02-12 15:34:42 +01005166
Radek Krejcie86bf772018-12-14 11:39:53 +01005167/**
Radek Krejcia3045382018-11-22 14:30:31 +01005168 * @brief Compile parsed schema node information.
5169 * @param[in] ctx Compile context
5170 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005171 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5172 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5173 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005174 * @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).
5175 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005176 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5177 */
Radek Krejci19a96102018-11-15 13:38:09 +01005178static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005179lys_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 +01005180{
Radek Krejci1c54f462020-05-12 17:25:34 +02005181 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005182 struct lysc_node *node = NULL;
5183 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005184 struct lysc_when **when;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005185 LY_ARRAY_COUNT_TYPE u;
Michal Vasko22df3f02020-08-24 13:29:22 +02005186 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
Radek Krejci19a96102018-11-15 13:38:09 +01005187
Radek Krejci327de162019-06-14 12:52:07 +02005188 if (node_p->nodetype != LYS_USES) {
5189 lysc_update_path(ctx, parent, node_p->name);
5190 } else {
5191 lysc_update_path(ctx, NULL, "{uses}");
5192 lysc_update_path(ctx, NULL, node_p->name);
5193 }
5194
Radek Krejci19a96102018-11-15 13:38:09 +01005195 switch (node_p->nodetype) {
5196 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02005197 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
Radek Krejci19a96102018-11-15 13:38:09 +01005198 node_compile_spec = lys_compile_node_container;
5199 break;
5200 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005201 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
Radek Krejci19a96102018-11-15 13:38:09 +01005202 node_compile_spec = lys_compile_node_leaf;
5203 break;
5204 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005205 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005206 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005207 break;
5208 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005209 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005210 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005211 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005212 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02005213 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005214 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005215 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005216 case LYS_ANYXML:
5217 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005218 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005219 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005220 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005221 case LYS_USES:
Radek Krejcic6b4f442020-08-12 14:45:18 +02005222 if (parent && parent->nodetype == LYS_CHOICE) {
5223 assert(node_p->parent->nodetype == LYS_CASE);
5224 lysc_update_path(ctx, parent, node_p->parent->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02005225 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice *)parent, NULL);
Radek Krejcic6b4f442020-08-12 14:45:18 +02005226 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
5227 }
5228
Michal Vasko22df3f02020-08-24 13:29:22 +02005229 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, cs ? (struct lysc_node *)cs : parent, &node);
Radek Krejcic6b4f442020-08-12 14:45:18 +02005230 if (cs) {
5231 cs->child = node;
5232 lysc_update_path(ctx, NULL, NULL);
5233 }
Radek Krejci327de162019-06-14 12:52:07 +02005234 lysc_update_path(ctx, NULL, NULL);
5235 lysc_update_path(ctx, NULL, NULL);
5236 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005237 default:
5238 LOGINT(ctx->ctx);
5239 return LY_EINT;
5240 }
5241 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5242 node->nodetype = node_p->nodetype;
5243 node->module = ctx->mod;
5244 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005245 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005246
5247 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005248 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005249 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005250 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005251 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5252 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005253 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005254 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005255 node->flags |= LYS_CONFIG_R;
5256 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005257 /* config not explicitely set, inherit it from parent */
5258 if (parent) {
5259 node->flags |= parent->flags & LYS_CONFIG_MASK;
5260 } else {
5261 /* default is config true */
5262 node->flags |= LYS_CONFIG_W;
5263 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005264 } else {
5265 /* config set explicitely */
5266 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005267 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005268 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5269 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5270 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005271 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005272 goto error;
5273 }
Radek Krejci19a96102018-11-15 13:38:09 +01005274
Radek Krejcia6d57732018-11-29 13:40:37 +01005275 /* *list ordering */
5276 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5277 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005278 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci0f969882020-08-21 16:56:47 +02005279 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5280 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005281 node->flags &= ~LYS_ORDBY_MASK;
5282 node->flags |= LYS_ORDBY_SYSTEM;
5283 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5284 /* default ordering is system */
5285 node->flags |= LYS_ORDBY_SYSTEM;
5286 }
5287 }
5288
Radek Krejci19a96102018-11-15 13:38:09 +01005289 /* status - it is not inherited by specification, but it does not make sense to have
5290 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005291 if (!parent || parent->nodetype != LYS_CHOICE) {
5292 /* in case of choice/case's children, postpone the check to the moment we know if
5293 * the parent is choice (parent here) or some case (so we have to get its flags to check) */
Radek Krejci1c54f462020-05-12 17:25:34 +02005294 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 +01005295 }
5296
Radek Krejciec4da802019-05-02 13:02:41 +02005297 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005298 node->sp = node_p;
5299 }
5300 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005301 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5302 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005303 if (node_p->when) {
5304 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005305 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005306
5307 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5308 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02005309 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
5310 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005311 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005312 }
Radek Krejciec4da802019-05-02 13:02:41 +02005313 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005314
5315 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005316 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005317
Radek Krejci0935f412019-08-20 16:15:18 +02005318 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5319
Radek Krejcife909632019-02-12 15:34:42 +01005320 /* inherit LYS_MAND_TRUE in parent containers */
5321 if (node->flags & LYS_MAND_TRUE) {
5322 lys_compile_mandatory_parents(parent, 1);
5323 }
5324
Radek Krejci327de162019-06-14 12:52:07 +02005325 lysc_update_path(ctx, NULL, NULL);
5326
Radek Krejci19a96102018-11-15 13:38:09 +01005327 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005328 if (parent) {
5329 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005330 if (node_p->parent->nodetype == LYS_CASE) {
5331 lysc_update_path(ctx, parent, node_p->parent->name);
5332 } else {
5333 lysc_update_path(ctx, parent, node->name);
5334 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005335 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice *)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005336 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejci0f969882020-08-21 16:56:47 +02005337 if (uses_status) {}
Radek Krejci056d0a82018-12-06 16:57:25 +01005338 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005339 * it directly gets the same status flags as the choice;
5340 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005341 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Michal Vasko22df3f02020-08-24 13:29:22 +02005342 node->parent = (struct lysc_node *)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005343 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005344 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005345 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005346 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005347 }
Radek Krejciec4da802019-05-02 13:02:41 +02005348 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
Radek Krejci327de162019-06-14 12:52:07 +02005349
5350 if (parent->nodetype == LYS_CHOICE) {
5351 lysc_update_path(ctx, NULL, NULL);
5352 }
Radek Krejci19a96102018-11-15 13:38:09 +01005353 } else {
5354 /* top-level element */
5355 if (!ctx->mod->compiled->data) {
5356 ctx->mod->compiled->data = node;
5357 } else {
5358 /* insert at the end of the module's top-level nodes list */
5359 ctx->mod->compiled->data->prev->next = node;
5360 node->prev = ctx->mod->compiled->data->prev;
5361 ctx->mod->compiled->data->prev = node;
5362 }
Radek Krejci327de162019-06-14 12:52:07 +02005363 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005364 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5365 ctx->mod->compiled->notifs, node->name, node)) {
5366 return LY_EVALID;
5367 }
Radek Krejci19a96102018-11-15 13:38:09 +01005368 }
Radek Krejci327de162019-06-14 12:52:07 +02005369 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005370
5371 return LY_SUCCESS;
5372
5373error:
5374 lysc_node_free(ctx->ctx, node);
5375 return ret;
5376}
5377
Radek Krejciccd20f12019-02-15 14:12:27 +01005378static void
5379lysc_disconnect(struct lysc_node *node)
5380{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005381 struct lysc_node *parent, *child, *prev = NULL, *next;
5382 struct lysc_node_case *cs = NULL;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005383 struct lysc_module *modc = node->module->compiled;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005384 uint8_t remove_cs = 0;
Radek Krejciccd20f12019-02-15 14:12:27 +01005385
5386 parent = node->parent;
5387
5388 /* parent's first child */
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005389 if (parent && parent->nodetype == LYS_CHOICE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005390 cs = (struct lysc_node_case *)node;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005391 } else if (parent && parent->nodetype == LYS_CASE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005392 /* disconnecting some node in a case */
Michal Vasko22df3f02020-08-24 13:29:22 +02005393 cs = (struct lysc_node_case *)parent;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005394 parent = cs->parent;
Michal Vasko22df3f02020-08-24 13:29:22 +02005395 for (child = cs->child; child && child->parent == (struct lysc_node *)cs; child = child->next) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005396 if (child == node) {
5397 if (cs->child == child) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005398 if (!child->next || child->next->parent != (struct lysc_node *)cs) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005399 /* case with a single child -> remove also the case */
5400 child->parent = NULL;
5401 remove_cs = 1;
5402 } else {
5403 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005404 }
5405 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005406 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005407 }
5408 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005409 if (!remove_cs) {
5410 cs = NULL;
5411 }
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005412 } else if (parent && lysc_node_children(parent, node->flags) == node) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005413 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005414 } else if (modc->data == node) {
5415 modc->data = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005416 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005417 if (cs) {
5418 if (remove_cs) {
5419 /* cs has only one child which is being also removed */
Michal Vasko22df3f02020-08-24 13:29:22 +02005420 lysc_disconnect((struct lysc_node *)cs);
5421 lysc_node_free(cs->module->ctx, (struct lysc_node *)cs);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005422 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02005423 if (((struct lysc_node_choice *)parent)->dflt == cs) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005424 /* default case removed */
Michal Vasko22df3f02020-08-24 13:29:22 +02005425 ((struct lysc_node_choice *)parent)->dflt = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005426 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005427 if (((struct lysc_node_choice *)parent)->cases == cs) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005428 /* first case removed */
Michal Vasko22df3f02020-08-24 13:29:22 +02005429 ((struct lysc_node_choice *)parent)->cases = (struct lysc_node_case *)cs->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005430 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005431 if (cs->child) {
5432 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
Michal Vasko22df3f02020-08-24 13:29:22 +02005433 if (cs->child->prev->parent != (struct lysc_node *)cs) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005434 prev = cs->child->prev;
5435 } /* else all the children are under a single case */
5436 LY_LIST_FOR_SAFE(cs->child, next, child) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005437 if (child->parent != (struct lysc_node *)cs) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005438 break;
5439 }
5440 lysc_node_free(node->module->ctx, child);
5441 }
5442 if (prev) {
5443 if (prev->next) {
5444 prev->next = child;
5445 }
5446 if (child) {
5447 child->prev = prev;
5448 } else {
5449 /* link from the first child under the cases */
Michal Vasko22df3f02020-08-24 13:29:22 +02005450 ((struct lysc_node_choice *)cs->parent)->cases->child->prev = prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005451 }
5452 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005453 }
5454 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005455 }
5456
5457 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005458 if (node->prev->next) {
5459 node->prev->next = node->next;
5460 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005461 if (node->next) {
5462 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005463 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005464 if (parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005465 child = (struct lysc_node *)lysc_node_children(parent, node->flags);
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005466 } else {
5467 child = modc->data;
5468 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005469 if (child) {
5470 child->prev = node->prev;
5471 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005472 } else if (((struct lysc_node_choice *)parent)->cases) {
5473 ((struct lysc_node_choice *)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005474 }
5475}
5476
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005477struct lysc_deviation {
5478 const char *nodeid;
5479 struct lysc_node *target; /* target node of the deviation */
Michal Vasko22df3f02020-08-24 13:29:22 +02005480 struct lysp_deviate **deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02005481 uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005482 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5483};
5484
Michal Vaskoccc062a2020-08-13 08:34:50 +02005485/* MACROS for deviates checking */
5486#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5487 if (!(target->nodetype & (NODETYPES))) { \
5488 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5489 goto cleanup; \
5490 }
5491
5492#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5493 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5495 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5496 goto cleanup; \
5497 }
5498
5499#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5500 if (!((TYPE)target)->MEMBER || COND) { \
5501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5502 goto cleanup; \
5503 }
5504
5505/**
5506 * @brief Apply deviate add.
5507 *
5508 * @param[in] ctx Compile context.
5509 * @param[in] target Deviation target.
5510 * @param[in] dev_flags Internal deviation flags.
5511 * @param[in] d Deviate add to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02005512 * @return LY_ERR value.
5513 */
5514static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005515lys_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 +02005516{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005517 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005518 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5519 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005520 LY_ARRAY_COUNT_TYPE x;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005521
5522#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5523 if (((TYPE)target)->MEMBER COND) { \
5524 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5525 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5526 PROPERTY, ((TYPE)target)->MEMBER); \
5527 goto cleanup; \
5528 }
5529
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005530#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005531 if (((TYPE)target)->MEMBER && (COND)) { \
Radek Krejci1deb5be2020-08-26 16:43:36 +02005532 uint8_t dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005533 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005534 ctx->mod_def, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5536 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5537 if (dynamic_) {free((void*)val_);} \
5538 goto cleanup; \
5539 }
5540
5541#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5542 if (((TYPE)target)->MEMBER && (COND)) { \
5543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5544 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5545 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5546 goto cleanup; \
5547 }
5548
5549 /* [units-stmt] */
5550 if (d->units) {
5551 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5552 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5553
5554 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5555 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units);
5556 }
5557
5558 /* *must-stmt */
5559 if (d->musts) {
5560 switch (target->nodetype) {
5561 case LYS_CONTAINER:
5562 case LYS_LIST:
5563 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5564 x, lys_compile_must, ret, cleanup);
5565 break;
5566 case LYS_LEAF:
5567 case LYS_LEAFLIST:
5568 case LYS_ANYDATA:
5569 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5570 x, lys_compile_must, ret, cleanup);
5571 break;
5572 case LYS_NOTIF:
5573 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5574 x, lys_compile_must, ret, cleanup);
5575 break;
5576 case LYS_RPC:
5577 case LYS_ACTION:
5578 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5579 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5580 x, lys_compile_must, ret, cleanup);
5581 break;
5582 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5583 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5584 x, lys_compile_must, ret, cleanup);
5585 break;
5586 }
Radek Krejci0f969882020-08-21 16:56:47 +02005587 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005588 default:
5589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5590 lys_nodetype2str(target->nodetype), "add", "must");
5591 goto cleanup;
5592 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02005593 ret = ly_set_add(&ctx->xpath, target, 0, NULL);
5594 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005595 }
5596
5597 /* *unique-stmt */
5598 if (d->uniques) {
5599 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5600 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5601 }
5602
5603 /* *default-stmt */
5604 if (d->dflts) {
5605 switch (target->nodetype) {
5606 case LYS_LEAF:
5607 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005608 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005609 if (leaf->dflt) {
5610 /* first, remove the default value taken from the type */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005611 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5612 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005613 free(leaf->dflt);
5614 leaf->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005615 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005616
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005617 /* store the default value in unres */
Michal Vasko31a82d52020-08-21 08:11:48 +02005618 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005619 target->flags |= LYS_SET_DFLT;
5620 break;
5621 case LYS_LEAFLIST:
5622 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5623 /* first, remove the default value taken from the type */
5624 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005625 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5626 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5627 free(llist->dflts[x]);
5628 }
5629 LY_ARRAY_FREE(llist->dflts);
5630 llist->dflts = 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 values in unres */
5634 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005635 target->flags |= LYS_SET_DFLT;
5636 break;
5637 case LYS_CHOICE:
5638 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5639 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5640 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5641 * to allow making the default case even the augmented case from the deviating module */
5642 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5643 goto cleanup;
5644 }
5645 break;
5646 default:
5647 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5648 lys_nodetype2str(target->nodetype), "add", "default");
5649 goto cleanup;
5650 }
5651 }
5652
5653 /* [config-stmt] */
5654 if (d->flags & LYS_CONFIG_MASK) {
5655 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5657 lys_nodetype2str(target->nodetype), "add", "config");
5658 goto cleanup;
5659 }
5660 if (dev_flags) {
5661 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5662 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5663 }
5664 if (target->flags & LYS_SET_CONFIG) {
5665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5666 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5667 target->flags & LYS_CONFIG_W ? "true" : "false");
5668 goto cleanup;
5669 }
5670 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5671 }
5672
5673 /* [mandatory-stmt] */
5674 if (d->flags & LYS_MAND_MASK) {
5675 if (target->flags & LYS_MAND_MASK) {
5676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5677 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5678 target->flags & LYS_MAND_TRUE ? "true" : "false");
5679 goto cleanup;
5680 }
5681 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5682 }
5683
5684 /* [min-elements-stmt] */
5685 if (d->flags & LYS_SET_MIN) {
5686 if (target->nodetype == LYS_LEAFLIST) {
5687 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5688 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005689 ((struct lysc_node_leaflist *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005690 } else if (target->nodetype == LYS_LIST) {
5691 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5692 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005693 ((struct lysc_node_list *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005694 } else {
5695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5696 lys_nodetype2str(target->nodetype), "add", "min-elements");
5697 goto cleanup;
5698 }
5699 if (d->min) {
5700 target->flags |= LYS_MAND_TRUE;
5701 }
5702 }
5703
5704 /* [max-elements-stmt] */
5705 if (d->flags & LYS_SET_MAX) {
5706 if (target->nodetype == LYS_LEAFLIST) {
5707 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5708 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005709 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005710 } else if (target->nodetype == LYS_LIST) {
5711 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5712 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005713 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005714 } else {
5715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5716 lys_nodetype2str(target->nodetype), "add", "max-elements");
5717 goto cleanup;
5718 }
5719 }
5720
5721 ret = LY_SUCCESS;
5722
5723cleanup:
5724 return ret;
5725}
5726
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005727static LY_ERR
5728lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt)
5729{
5730 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005731 uint8_t dyn = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005732 const char *orig_dflt;
5733 uint32_t i;
5734
5735 if (target->module != ctx->mod) {
5736 /* foreign deviation */
5737 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt);
5738
5739 /* check that the value matches */
5740 orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5741 if (strcmp(orig_dflt, dflt)) {
5742 goto error;
5743 }
5744 if (dyn) {
5745 free((char *)orig_dflt);
5746 }
5747
5748 /* remove the default specification */
5749 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5750 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5751 free(leaf->dflt);
5752 leaf->dflt = NULL;
5753 } else {
5754 /* local deviation */
5755 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt);
5756
5757 /* find the incomplete default */
5758 orig_dflt = NULL;
5759 for (i = 0; i < ctx->dflts.count; ++i) {
5760 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
5761 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5762 break;
5763 }
5764 }
5765 LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT);
5766
5767 /* check that the value matches */
5768 if (strcmp(orig_dflt, dflt)) {
5769 goto error;
5770 }
5771
5772 /* update the list of incomplete default values */
5773 lysc_incomplete_dflt_remove(ctx, target);
5774 }
5775
5776 target->flags &= ~LYS_SET_DFLT;
5777 return LY_SUCCESS;
5778
5779error:
5780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5781 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5782 dflt, orig_dflt);
5783 if (dyn) {
5784 free((char *)orig_dflt);
5785 }
5786cleanup:
5787 return LY_EVALID;
5788}
5789
5790static LY_ERR
5791lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts)
5792{
5793 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005794 uint8_t dyn = 0, found;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005795 const char *orig_dflt, **orig_dflts;
5796 uint32_t i;
5797 LY_ARRAY_COUNT_TYPE x, y;
5798
5799 if (target->module != ctx->mod) {
5800 /* foreign deviation */
5801 DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]);
5802 LY_ARRAY_FOR(dflts, x) {
5803 found = 0;
5804 LY_ARRAY_FOR(llist->dflts, y) {
5805 orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5806 if (!strcmp(orig_dflt, dflts[x])) {
5807 if (dyn) {
5808 free((char *)orig_dflt);
5809 }
5810 found = 1;
5811 break;
5812 }
5813 if (dyn) {
5814 free((char *)orig_dflt);
5815 }
5816 }
5817 if (!found) {
5818 goto error;
5819 }
5820
5821 /* update compiled default values */
5822 LY_ARRAY_DECREMENT(llist->dflts);
5823 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
5824 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
5825 free(llist->dflts[y]);
5826 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
5827 }
5828 if (!LY_ARRAY_COUNT(llist->dflts)) {
5829 LY_ARRAY_FREE(llist->dflts);
5830 llist->dflts = NULL;
5831 llist->flags &= ~LYS_SET_DFLT;
5832 }
5833 } else {
5834 /* local deviation */
5835 orig_dflt = NULL;
5836 orig_dflts = NULL;
5837 for (i = 0; i < ctx->dflts.count; ++i) {
5838 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) {
5839 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5840 orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts;
5841 break;
5842 }
5843 }
5844 LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT);
5845
5846 if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) {
5847 /* TODO it is not currently possible to remove just one default value from incomplete defaults array */
5848 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5849 "Local deviation deleting leaf-list defaults is not supported.");
5850 return LY_EVALID;
5851 }
5852
5853 LY_ARRAY_FOR(dflts, x) {
5854 found = 0;
5855 if (orig_dflts) {
5856 LY_ARRAY_FOR(orig_dflts, y) {
5857 if (!strcmp(orig_dflts[y], dflts[x])) {
5858 found = 1;
5859 break;
5860 }
5861 }
5862 } else if (!strcmp(orig_dflt, dflts[x])) {
5863 found = 1;
5864 }
5865 if (!found) {
5866 goto error;
5867 }
5868
5869 /* update the list of incomplete default values */
5870 lysc_incomplete_dflt_remove(ctx, target);
5871 }
5872
5873 llist->flags &= ~LYS_SET_DFLT;
5874 }
5875
5876 return LY_SUCCESS;
5877
5878error:
5879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
5880 "which does not match any of the target's property values.", dflts[x]);
5881cleanup:
5882 return LY_EVALID;
5883}
5884
Michal Vaskoccc062a2020-08-13 08:34:50 +02005885/**
5886 * @brief Apply deviate delete.
5887 *
5888 * @param[in] ctx Compile context.
5889 * @param[in] target Deviation target.
5890 * @param[in] dev_flags Internal deviation flags.
5891 * @param[in] d Deviate delete to apply.
5892 * @return LY_ERR value.
5893 */
5894static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005895lys_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 +02005896{
5897 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005898 struct lysc_node_list *list = (struct lysc_node_list *)target;
5899 LY_ARRAY_COUNT_TYPE x, y, z;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005900 size_t prefix_len, name_len;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005901 const char *prefix, *name, *nodeid;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005902 struct lys_module *mod;
5903
5904#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5905 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5906 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5907 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5908 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5909 } \
5910 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5912 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5913 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5914 goto cleanup; \
5915 } \
5916 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5917 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5918 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5919 &((TYPE)target)->ARRAY_TRG[y + 1], \
5920 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5921 } \
5922 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5923 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5924 ((TYPE)target)->ARRAY_TRG = NULL; \
5925 }
5926
5927 /* [units-stmt] */
5928 if (d->units) {
5929 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Michal Vasko22df3f02020-08-24 13:29:22 +02005930 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units);
5931 if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5933 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02005934 d->units, ((struct lysc_node_leaf *)target)->units);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005935 goto cleanup;
5936 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005937 lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5938 ((struct lysc_node_leaf *)target)->units = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005939 }
5940
5941 /* *must-stmt */
5942 if (d->musts) {
5943 switch (target->nodetype) {
5944 case LYS_CONTAINER:
5945 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005946 DEV_DEL_ARRAY(struct lysc_node_container *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005947 break;
5948 case LYS_LEAF:
5949 case LYS_LEAFLIST:
5950 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005951 DEV_DEL_ARRAY(struct lysc_node_leaf *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005952 break;
5953 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005954 DEV_DEL_ARRAY(struct lysc_notif *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005955 break;
5956 case LYS_RPC:
5957 case LYS_ACTION:
5958 if (dev_flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005959 DEV_DEL_ARRAY(struct lysc_action *, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005960 break;
5961 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005962 DEV_DEL_ARRAY(struct lysc_action *, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005963 break;
5964 }
Radek Krejci0f969882020-08-21 16:56:47 +02005965 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005966 default:
5967 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5968 lys_nodetype2str(target->nodetype), "delete", "must");
5969 goto cleanup;
5970 }
5971 }
5972
5973 /* *unique-stmt */
5974 if (d->uniques) {
5975 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5976 LY_ARRAY_FOR(d->uniques, x) {
5977 LY_ARRAY_FOR(list->uniques, z) {
5978 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
5979 nodeid = strpbrk(name, " \t\n");
5980 if (nodeid) {
5981 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
5982 break;
5983 }
5984 while (isspace(*nodeid)) {
5985 ++nodeid;
5986 }
5987 } else {
5988 if (strcmp(name, list->uniques[z][y]->name)) {
5989 break;
5990 }
5991 }
5992 }
5993 if (!name) {
5994 /* complete match - remove the unique */
5995 LY_ARRAY_DECREMENT(list->uniques);
5996 LY_ARRAY_FREE(list->uniques[z]);
5997 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
5998 --z;
5999 break;
6000 }
6001 }
6002 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
6003 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6004 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6005 d->uniques[x]);
6006 goto cleanup;
6007 }
6008 }
6009 if (!LY_ARRAY_COUNT(list->uniques)) {
6010 LY_ARRAY_FREE(list->uniques);
6011 list->uniques = NULL;
6012 }
6013 }
6014
6015 /* *default-stmt */
6016 if (d->dflts) {
6017 switch (target->nodetype) {
6018 case LYS_LEAF:
6019 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006020 LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006021 break;
6022 case LYS_LEAFLIST:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006023 LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006024 break;
6025 case LYS_CHOICE:
6026 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vasko22df3f02020-08-24 13:29:22 +02006027 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "deleting", "default", d->dflts[0]);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006028 nodeid = d->dflts[0];
6029 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6030 if (prefix) {
6031 /* use module prefixes from the deviation module to match the module of the default case */
6032 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006033 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006034 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6035 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6036 goto cleanup;
6037 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006038 if (mod != ((struct lysc_node_choice *)target)->dflt->module) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006039 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006040 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6041 "The prefix does not match the default case's module.", d->dflts[0]);
6042 goto cleanup;
6043 }
6044 }
6045 /* else {
6046 * strictly, the default prefix would point to the deviation module, but the value should actually
6047 * match the default string in the original module (usually unprefixed), so in this case we do not check
6048 * the module of the default case, just matching its name */
Michal Vasko22df3f02020-08-24 13:29:22 +02006049 if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02006050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6051 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02006052 d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006053 goto cleanup;
6054 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006055 ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT;
6056 ((struct lysc_node_choice *)target)->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006057 break;
6058 default:
6059 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6060 lys_nodetype2str(target->nodetype), "delete", "default");
6061 goto cleanup;
6062 }
6063 }
6064
6065 ret = LY_SUCCESS;
6066
6067cleanup:
6068 return ret;
6069}
6070
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006071static LY_ERR
6072lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target)
6073{
6074 LY_ERR ret;
6075 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6076 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6077 struct ly_err_item *err = NULL;
6078 LY_ARRAY_COUNT_TYPE x;
6079 const char *dflt;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006080 uint8_t dyn;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006081
6082 if (target->module != ctx->mod) {
6083 /* foreign deviation */
6084 if (target->nodetype == LYS_LEAF) {
6085 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn);
6086 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6087 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6088
6089 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6090 LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err);
6091 if (dyn) {
6092 free((char *)dflt);
6093 }
6094 if (err) {
6095 ly_err_print(err);
6096 ctx->path[0] = '\0';
6097 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6099 "Invalid default - value does not fit the type (%s).", err->msg);
6100 ly_err_free(err);
6101 }
6102 LY_CHECK_RET(ret);
6103
6104 ++leaf->dflt->realtype->refcount;
6105 } else { /* LY_LEAFLIST */
6106 LY_ARRAY_FOR(llist->dflts, x) {
6107 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn);
6108 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6109 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6110
6111 ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6112 LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err);
6113 if (dyn) {
6114 free((char *)dflt);
6115 }
6116 if (err) {
6117 ly_err_print(err);
6118 ctx->path[0] = '\0';
6119 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6120 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6121 "Invalid default - value does not fit the type (%s).", err->msg);
6122 ly_err_free(err);
6123 }
6124 LY_CHECK_RET(ret);
6125
6126 ++llist->dflts[x]->realtype->refcount;
6127 }
6128 }
6129 } else {
6130 /* local deviation */
6131
6132 /* these default were not compiled yet, so they will use the new type automatically */
6133 }
6134
6135 return LY_SUCCESS;
6136}
6137
Michal Vaskoccc062a2020-08-13 08:34:50 +02006138/**
6139 * @brief Apply deviate replace.
6140 *
6141 * @param[in] ctx Compile context.
6142 * @param[in] target Deviation target.
6143 * @param[in] d Deviate replace to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02006144 * @return LY_ERR value.
6145 */
6146static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006147lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02006148{
6149 LY_ERR ret = LY_EVALID;
6150 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6151 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6152 LY_ARRAY_COUNT_TYPE x;
6153
6154#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6155 if (!(((TYPE)target)->MEMBER COND)) { \
6156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6157 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6158 goto cleanup; \
6159 }
6160
6161 /* [type-stmt] */
6162 if (d->type) {
6163 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6164 /* type is mandatory, so checking for its presence is not necessary */
6165 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6166
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006167 /* remove only default value inherited from the type */
6168 if (!(target->flags & LYS_SET_DFLT)) {
6169 if (target->module != ctx->mod) {
6170 /* foreign deviation - the target has default from the previous type, remove it */
6171 if (target->nodetype == LYS_LEAF) {
6172 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6173 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6174 free(leaf->dflt);
6175 leaf->dflt = NULL;
6176 } else { /* LYS_LEAFLIST */
6177 LY_ARRAY_FOR(llist->dflts, x) {
6178 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6179 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6180 free(llist->dflts[x]);
6181 }
6182 LY_ARRAY_FREE(llist->dflts);
6183 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006184 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006185 } else {
6186 /* local deviation */
6187 lysc_incomplete_dflt_remove(ctx, target);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006188 }
6189 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006190
6191 LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf));
6192
6193 if (target->flags & LYS_SET_DFLT) {
6194 /* the term default value(s) needs to be recompiled */
6195 LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006196 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006197 }
6198
6199 /* [units-stmt] */
6200 if (d->units) {
6201 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6202 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6203 units, "replacing", "units", d->units);
6204
6205 lydict_remove(ctx->ctx, leaf->units);
6206 DUP_STRING(ctx->ctx, d->units, leaf->units);
6207 }
6208
6209 /* [default-stmt] */
6210 if (d->dflt) {
6211 switch (target->nodetype) {
6212 case LYS_LEAF:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006213 if (target->module != ctx->mod) {
6214 /* foreign deviation */
6215 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing",
6216 "default", d->dflt);
6217
6218 /* remove the default specification */
6219 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6220 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6221 free(leaf->dflt);
6222 leaf->dflt = NULL;
6223 } else {
6224 /* local deviation */
6225 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing",
6226 "default", d->dflt);
6227 assert(!leaf->dflt);
6228 }
6229
6230 /* store the new default value */
6231 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006232 break;
6233 case LYS_CHOICE:
6234 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6235 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6236 goto cleanup;
6237 }
6238 break;
6239 default:
6240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6241 lys_nodetype2str(target->nodetype), "replace", "default");
6242 goto cleanup;
6243 }
6244 }
6245
6246 /* [config-stmt] */
6247 if (d->flags & LYS_CONFIG_MASK) {
6248 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6249 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6250 lys_nodetype2str(target->nodetype), "replace", "config");
6251 goto cleanup;
6252 }
6253 if (!(target->flags & LYS_SET_CONFIG)) {
6254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6255 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6256 goto cleanup;
6257 }
6258 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6259 }
6260
6261 /* [mandatory-stmt] */
6262 if (d->flags & LYS_MAND_MASK) {
6263 if (!(target->flags & LYS_MAND_MASK)) {
6264 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6265 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6266 goto cleanup;
6267 }
6268 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6269 }
6270
6271 /* [min-elements-stmt] */
6272 if (d->flags & LYS_SET_MIN) {
6273 if (target->nodetype == LYS_LEAFLIST) {
6274 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6275 /* change value */
6276 ((struct lysc_node_leaflist *)target)->min = d->min;
6277 } else if (target->nodetype == LYS_LIST) {
6278 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6279 /* change value */
6280 ((struct lysc_node_list *)target)->min = d->min;
6281 } else {
6282 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6283 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6284 goto cleanup;
6285 }
6286 if (d->min) {
6287 target->flags |= LYS_MAND_TRUE;
6288 }
6289 }
6290
6291 /* [max-elements-stmt] */
6292 if (d->flags & LYS_SET_MAX) {
6293 if (target->nodetype == LYS_LEAFLIST) {
6294 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6295 /* change value */
6296 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6297 } else if (target->nodetype == LYS_LIST) {
6298 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6299 /* change value */
6300 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6301 } else {
6302 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6303 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6304 goto cleanup;
6305 }
6306 }
6307
6308 ret = LY_SUCCESS;
6309
6310cleanup:
6311 return ret;
6312}
6313
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006314/**
6315 * @brief Apply all deviations of one target node.
6316 *
6317 * @param[in] ctx Compile context.
6318 * @param[in] dev Deviation structure to apply.
6319 * @return LY_ERR value.
6320 */
6321static LY_ERR
6322lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006323{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006324 LY_ERR ret = LY_EVALID;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006325 struct lysc_node *target = dev->target;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006326 struct lysc_action *rpcs;
6327 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006328 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006329 LY_ARRAY_COUNT_TYPE v, x;
Radek Krejci551b12c2019-02-19 16:11:21 +01006330 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006331
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006332 lysc_update_path(ctx, NULL, dev->nodeid);
6333
6334 /* not-supported */
6335 if (dev->not_supported) {
6336 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
Radek Krejci0f969882020-08-21 16:56:47 +02006337 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 +02006338 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6339 }
6340
6341#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6342 if (target->parent) { \
6343 ARRAY = (TYPE*)GETFUNC(target->parent); \
6344 } else { \
6345 ARRAY = target->module->compiled->ARRAY; \
6346 } \
6347 LY_ARRAY_FOR(ARRAY, x) { \
6348 if (&ARRAY[x] == (TYPE*)target) { break; } \
6349 } \
6350 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6351 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6352 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6353 LY_ARRAY_DECREMENT(ARRAY); \
6354 }
6355
6356 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6357 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6358 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006359 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6360 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6361 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6362 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006363 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6364 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006365 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6366 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6367 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6368 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006369 } else {
6370 /* remove RPC/action */
6371 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6372 }
6373 } else if (target->nodetype == LYS_NOTIF) {
6374 /* remove Notification */
6375 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6376 } else {
6377 /* remove the target node */
6378 lysc_disconnect(target);
6379 lysc_node_free(ctx->ctx, target);
6380 }
6381
6382 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6383 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6384 return LY_SUCCESS;
6385 }
6386
6387 /* list of deviates (not-supported is not present in the list) */
6388 LY_ARRAY_FOR(dev->deviates, v) {
6389 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006390 switch (d->mod) {
6391 case LYS_DEV_ADD:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006392 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006393 break;
6394 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006395 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006396 break;
6397 case LYS_DEV_REPLACE:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006398 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006399 break;
6400 default:
6401 LOGINT(ctx->ctx);
6402 goto cleanup;
6403 }
6404 }
6405
6406 /* final check when all deviations of a single target node are applied */
6407
6408 /* check min-max compatibility */
6409 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006410 min = ((struct lysc_node_leaflist *)target)->min;
6411 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006412 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006413 min = ((struct lysc_node_list *)target)->min;
6414 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006415 } else {
6416 min = max = 0;
6417 }
6418 if (min > max) {
6419 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6420 "after deviation: min value %u is bigger than max value %u.", min, max);
6421 goto cleanup;
6422 }
6423
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006424 /* check mandatory - default compatibility */
6425 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6426 && (target->flags & LYS_MAND_TRUE)) {
6427 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6428 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6429 goto cleanup;
Michal Vasko22df3f02020-08-24 13:29:22 +02006430 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006431 && (target->flags & LYS_MAND_TRUE)) {
6432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6433 goto cleanup;
6434 }
6435 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6436 /* mandatory node under a default case */
6437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6438 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6439 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6440 goto cleanup;
6441 }
6442
6443 /* success */
6444 ret = LY_SUCCESS;
6445
6446cleanup:
6447 lysc_update_path(ctx, NULL, NULL);
6448 return ret;
6449}
6450
6451LY_ERR
6452lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6453{
6454 LY_ERR ret = LY_EVALID;
6455 struct ly_set devs_p = {0};
6456 struct ly_set targets = {0};
6457 struct lysc_node *target; /* target target of the deviation */
6458 struct lysp_deviation *dev;
6459 struct lysp_deviate *d, **dp_new;
6460 LY_ARRAY_COUNT_TYPE u, v;
6461 struct lysc_deviation **devs = NULL;
6462 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006463 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006464
6465 /* get all deviations from the module and all its submodules ... */
6466 LY_ARRAY_FOR(mod_p->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006467 LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST, NULL));
Radek Krejciccd20f12019-02-15 14:12:27 +01006468 }
6469 LY_ARRAY_FOR(mod_p->includes, v) {
6470 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006471 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 +01006472 }
6473 }
6474 if (!devs_p.count) {
6475 /* nothing to do */
6476 return LY_SUCCESS;
6477 }
6478
Radek Krejci327de162019-06-14 12:52:07 +02006479 lysc_update_path(ctx, NULL, "{deviation}");
6480
Radek Krejciccd20f12019-02-15 14:12:27 +01006481 /* ... and group them by the target node */
6482 devs = calloc(devs_p.count, sizeof *devs);
6483 for (u = 0; u < devs_p.count; ++u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006484 uint32_t index;
6485
Radek Krejciccd20f12019-02-15 14:12:27 +01006486 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006487 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006488
6489 /* resolve the target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02006490 LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
Michal Vasko22df3f02020-08-24 13:29:22 +02006491 (const struct lysc_node **)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006492 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006493 /* move the target pointer to input/output to make them different from the action and
6494 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6495 * back to the RPC/action node due to a better compatibility and decision code in this function.
6496 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6497 if (flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006498 target = (struct lysc_node *)&((struct lysc_action *)target)->input;
Radek Krejcif538ce52019-03-05 10:46:14 +01006499 flags |= LYSC_OPT_INTERNAL;
6500 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006501 target = (struct lysc_node *)&((struct lysc_action *)target)->output;
Radek Krejcif538ce52019-03-05 10:46:14 +01006502 flags |= LYSC_OPT_INTERNAL;
6503 }
6504 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006505 /* insert into the set of targets with duplicity detection */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006506 ret = ly_set_add(&targets, target, 0, &index);
6507 LY_CHECK_GOTO(ret, cleanup);
6508 if (!devs[index]) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006509 /* new record */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006510 devs[index] = calloc(1, sizeof **devs);
6511 devs[index]->target = target;
6512 devs[index]->nodeid = dev->nodeid;
6513 devs[index]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006514 }
6515 /* add deviates into the deviation's list of deviates */
Michal Vasko5c38f362020-08-24 09:22:51 +02006516 LY_LIST_FOR(dev->deviates, d) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006517 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[index]->deviates, dp_new, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006518 *dp_new = d;
6519 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006520 devs[index]->not_supported = 1;
Radek Krejciccd20f12019-02-15 14:12:27 +01006521 }
6522 }
Radek Krejci327de162019-06-14 12:52:07 +02006523
6524 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006525 }
6526
Radek Krejciccd20f12019-02-15 14:12:27 +01006527 /* apply deviations */
6528 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006529 uint8_t match = 0;
6530
Radek Krejcif538ce52019-03-05 10:46:14 +01006531 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6532 /* fix the target pointer in case of RPC's/action's input/output */
6533 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006534 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006535 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006536 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006537 }
6538 }
6539
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006540 /* remember target module (the target node may be removed) */
6541 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006542
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006543 /* apply the deviation */
6544 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006545
Michal Vaskoe6143202020-07-03 13:02:08 +02006546 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006547 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6548 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006549 match = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006550 break;
6551 }
6552 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02006553 if (!match) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006554 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006555 *dev_mod = mod_p->mod;
6556 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006557 }
6558
Radek Krejci327de162019-06-14 12:52:07 +02006559 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006560 ret = LY_SUCCESS;
6561
6562cleanup:
6563 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6564 LY_ARRAY_FREE(devs[u]->deviates);
6565 free(devs[u]);
6566 }
6567 free(devs);
6568 ly_set_erase(&targets, NULL);
6569 ly_set_erase(&devs_p, NULL);
6570
6571 return ret;
6572}
6573
Radek Krejcib56c7502019-02-13 14:19:54 +01006574/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006575 * @brief Compile the given YANG submodule into the main module.
6576 * @param[in] ctx Compile context
6577 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006578 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6579 */
6580LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006581lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006582{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006583 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006584 LY_ERR ret = LY_SUCCESS;
6585 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006586 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006587 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006588 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006589
Michal Vasko33ff9422020-07-03 09:50:39 +02006590 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006591 /* features are compiled directly into the compiled module structure,
6592 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6593 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006594 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6595 LY_CHECK_GOTO(ret, error);
6596 }
Radek Krejci0af46292019-01-11 16:02:31 +01006597
Michal Vasko33ff9422020-07-03 09:50:39 +02006598 if (!mainmod->mod->dis_identities) {
6599 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6600 LY_CHECK_GOTO(ret, error);
6601 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006602
Radek Krejci474f9b82019-07-24 11:36:37 +02006603 /* data nodes */
6604 LY_LIST_FOR(submod->data, node_p) {
6605 ret = lys_compile_node(ctx, node_p, NULL, 0);
6606 LY_CHECK_GOTO(ret, error);
6607 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006608
Radek Krejciec4da802019-05-02 13:02:41 +02006609 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6610 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006611
Radek Krejcid05cbd92018-12-05 14:26:40 +01006612error:
6613 return ret;
6614}
6615
Radek Krejci335332a2019-09-05 13:03:35 +02006616static void *
6617lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6618{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006619 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci335332a2019-09-05 13:03:35 +02006620 if (substmts[u].stmt == stmt) {
6621 return substmts[u].storage;
6622 }
6623 }
6624 return NULL;
6625}
6626
6627LY_ERR
6628lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6629{
6630 LY_ERR ret = LY_EVALID, r;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006631 LY_ARRAY_COUNT_TYPE u;
Radek Krejci335332a2019-09-05 13:03:35 +02006632 struct lysp_stmt *stmt;
6633 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006634
6635 /* check for invalid substatements */
6636 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006637 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6638 continue;
6639 }
Radek Krejci335332a2019-09-05 13:03:35 +02006640 for (u = 0; substmts[u].stmt; ++u) {
6641 if (substmts[u].stmt == stmt->kw) {
6642 break;
6643 }
6644 }
6645 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6647 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006648 goto cleanup;
6649 }
Radek Krejci335332a2019-09-05 13:03:35 +02006650 }
6651
Radek Krejciad5963b2019-09-06 16:03:05 +02006652 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6653
Radek Krejci335332a2019-09-05 13:03:35 +02006654 /* keep order of the processing the same as the order in the defined substmts,
6655 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6656 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02006657 uint8_t stmt_present = 0;
Radek Krejciad5963b2019-09-06 16:03:05 +02006658
Radek Krejci335332a2019-09-05 13:03:35 +02006659 for (stmt = ext->child; stmt; stmt = stmt->next) {
6660 if (substmts[u].stmt != stmt->kw) {
6661 continue;
6662 }
6663
Radek Krejciad5963b2019-09-06 16:03:05 +02006664 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006665 if (substmts[u].storage) {
6666 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006667 case LY_STMT_STATUS:
6668 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6669 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6670 break;
6671 case LY_STMT_UNITS: {
6672 const char **units;
6673
6674 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6675 /* single item */
6676 if (*((const char **)substmts[u].storage)) {
6677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6678 goto cleanup;
6679 }
6680 units = (const char **)substmts[u].storage;
6681 } else {
6682 /* sized array */
6683 const char ***units_array = (const char ***)substmts[u].storage;
6684 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6685 }
6686 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6687 break;
6688 }
Radek Krejci335332a2019-09-05 13:03:35 +02006689 case LY_STMT_TYPE: {
6690 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6691 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6692
6693 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6694 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006695 if (*(struct lysc_type **)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6697 goto cleanup;
6698 }
6699 compiled = substmts[u].storage;
6700 } else {
6701 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006702 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006703 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006704 compiled = (void *)type;
Radek Krejci335332a2019-09-05 13:03:35 +02006705 }
6706
Radek Krejciad5963b2019-09-06 16:03:05 +02006707 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006708 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL,
6709 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006710 units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02006711 lysp_type_free(ctx->ctx, parsed);
6712 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006713 break;
6714 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006715 case LY_STMT_IF_FEATURE: {
6716 struct lysc_iffeature *iff = NULL;
6717
6718 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6719 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006720 if (((struct lysc_iffeature *)substmts[u].storage)->features) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006721 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6722 goto cleanup;
6723 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006724 iff = (struct lysc_iffeature *)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006725 } else {
6726 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006727 struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006728 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6729 }
6730 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6731 break;
6732 }
6733 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6734 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006735 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006736 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.",
6737 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006738 goto cleanup;
6739 }
6740 }
Radek Krejci335332a2019-09-05 13:03:35 +02006741 }
Radek Krejci335332a2019-09-05 13:03:35 +02006742
Radek Krejciad5963b2019-09-06 16:03:05 +02006743 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6745 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6746 goto cleanup;
6747 }
Radek Krejci335332a2019-09-05 13:03:35 +02006748 }
6749
6750 ret = LY_SUCCESS;
6751
6752cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006753 return ret;
6754}
6755
Michal Vasko175012e2019-11-06 15:49:14 +01006756/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006757 * @brief Check when for cyclic dependencies.
6758 * @param[in] set Set with all the referenced nodes.
6759 * @param[in] node Node whose "when" referenced nodes are in @p set.
6760 * @return LY_ERR value
6761 */
6762static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006763lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006764{
6765 struct lyxp_set tmp_set;
6766 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006767 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006768 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006769 struct lysc_when *when;
6770 LY_ERR ret = LY_SUCCESS;
6771
6772 memset(&tmp_set, 0, sizeof tmp_set);
6773
6774 /* prepare in_ctx of the set */
Michal Vaskod989ba02020-08-24 10:59:24 +02006775 for (i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006776 xp_scnode = &set->val.scnodes[i];
6777
Michal Vasko5c4e5892019-11-14 12:31:38 +01006778 if (xp_scnode->in_ctx != -1) {
6779 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006780 xp_scnode->in_ctx = 1;
6781 }
6782 }
6783
6784 for (i = 0; i < set->used; ++i) {
6785 xp_scnode = &set->val.scnodes[i];
6786 if (xp_scnode->in_ctx != 1) {
6787 /* already checked */
6788 continue;
6789 }
6790
Michal Vasko1bf09392020-03-27 12:38:10 +01006791 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006792 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006793 /* no when to check */
6794 xp_scnode->in_ctx = 0;
6795 continue;
6796 }
6797
6798 node = xp_scnode->scnode;
6799 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006800 LY_ARRAY_FOR(node->when, u) {
6801 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006802 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006803 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6804 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006805 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006806 goto cleanup;
6807 }
6808
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006809 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006810 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006811 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006812 /* try to find this node in our set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006813 int idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006814 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006815 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 +01006816 ret = LY_EVALID;
6817 goto cleanup;
6818 }
6819
6820 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006821 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006822 } else {
6823 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006824 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006825 }
6826 }
6827
6828 /* merge this set into the global when set */
6829 lyxp_set_scnode_merge(set, &tmp_set);
6830 }
6831
6832 /* check when of non-data parents as well */
6833 node = node->parent;
6834 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6835
Michal Vasko251f56e2019-11-14 16:06:47 +01006836 /* this node when was checked (xp_scnode could have been reallocd) */
6837 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006838 }
6839
6840cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006841 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006842 return ret;
6843}
6844
6845/**
Michal Vasko175012e2019-11-06 15:49:14 +01006846 * @brief Check when/must expressions of a node on a compiled schema tree.
6847 * @param[in] ctx Compile context.
6848 * @param[in] node Node to check.
6849 * @return LY_ERR value
6850 */
6851static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006852lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006853{
Michal Vasko175012e2019-11-06 15:49:14 +01006854 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006855 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006856 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006857 uint32_t opts;
6858 uint8_t input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006859 struct lysc_when **when = NULL;
6860 struct lysc_must *musts = NULL;
6861 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006862 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006863
6864 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006865 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006866 if (node->flags & LYS_CONFIG_R) {
6867 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6868 if (op) {
6869 /* we are actually in output */
6870 opts = LYXP_SCNODE_OUTPUT;
6871 }
6872 }
Michal Vasko175012e2019-11-06 15:49:14 +01006873
6874 switch (node->nodetype) {
6875 case LYS_CONTAINER:
6876 when = ((struct lysc_node_container *)node)->when;
6877 musts = ((struct lysc_node_container *)node)->musts;
6878 break;
6879 case LYS_CHOICE:
6880 when = ((struct lysc_node_choice *)node)->when;
6881 break;
6882 case LYS_LEAF:
6883 when = ((struct lysc_node_leaf *)node)->when;
6884 musts = ((struct lysc_node_leaf *)node)->musts;
6885 break;
6886 case LYS_LEAFLIST:
6887 when = ((struct lysc_node_leaflist *)node)->when;
6888 musts = ((struct lysc_node_leaflist *)node)->musts;
6889 break;
6890 case LYS_LIST:
6891 when = ((struct lysc_node_list *)node)->when;
6892 musts = ((struct lysc_node_list *)node)->musts;
6893 break;
6894 case LYS_ANYXML:
6895 case LYS_ANYDATA:
6896 when = ((struct lysc_node_anydata *)node)->when;
6897 musts = ((struct lysc_node_anydata *)node)->musts;
6898 break;
6899 case LYS_CASE:
6900 when = ((struct lysc_node_case *)node)->when;
6901 break;
6902 case LYS_NOTIF:
6903 musts = ((struct lysc_notif *)node)->musts;
6904 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006905 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006906 case LYS_ACTION:
6907 /* first process input musts */
6908 musts = ((struct lysc_action *)node)->input.musts;
6909 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006910 default:
6911 /* nothing to check */
6912 break;
6913 }
6914
Michal Vasko175012e2019-11-06 15:49:14 +01006915 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006916 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006917 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 +02006918 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006919 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006920 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006921 goto cleanup;
6922 }
6923
Michal Vaskodc052f32019-11-07 11:11:38 +01006924 ctx->path[0] = '\0';
6925 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006926 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006927 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006928 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6929 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006930
Michal Vaskoecd62de2019-11-13 12:35:11 +01006931 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006932 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006933 schema->name);
6934 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006935
6936 /* check dummy node accessing */
6937 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006938 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006939 ret = LY_EVALID;
6940 goto cleanup;
6941 }
Michal Vasko175012e2019-11-06 15:49:14 +01006942 }
6943 }
6944
Michal Vaskoecd62de2019-11-13 12:35:11 +01006945 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006946 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006947 LY_CHECK_GOTO(ret, cleanup);
6948
Michal Vaskod3678892020-05-21 10:06:58 +02006949 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006950 }
6951
Michal Vasko5d8756a2019-11-07 15:21:00 +01006952check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006953 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006954 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006955 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 +01006956 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006957 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006958 goto cleanup;
6959 }
6960
Michal Vaskodc052f32019-11-07 11:11:38 +01006961 ctx->path[0] = '\0';
6962 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006963 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006964 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006965 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006966 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006967 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6968 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006969 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006970 }
6971 }
6972
Michal Vaskod3678892020-05-21 10:06:58 +02006973 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006974 }
6975
Michal Vasko1bf09392020-03-27 12:38:10 +01006976 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006977 /* now check output musts */
6978 input_done = 1;
6979 musts = ((struct lysc_action *)node)->output.musts;
6980 opts = LYXP_SCNODE_OUTPUT;
6981 goto check_musts;
6982 }
6983
Michal Vasko175012e2019-11-06 15:49:14 +01006984cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006985 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006986 return ret;
6987}
6988
Michal Vasko8d544252020-03-02 10:19:52 +01006989static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006990lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6991{
Michal Vasko6b26e742020-07-17 15:02:10 +02006992 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02006993 struct ly_path *p;
6994 struct lysc_type *type;
6995
6996 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6997
6998 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006999 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
7000 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007001 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02007002
7003 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007004 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02007005 ly_path_free(node->module->ctx, p);
7006
7007 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7008 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7009 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
7010 lref->path->expr, lys_nodetype2str(target->nodetype));
7011 return LY_EVALID;
7012 }
7013
7014 /* check status */
7015 ctx->path[0] = '\0';
7016 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7017 ctx->path_len = strlen(ctx->path);
7018 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
7019 return LY_EVALID;
7020 }
7021 ctx->path_len = 1;
7022 ctx->path[1] = '\0';
7023
7024 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007025 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007026 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007027 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007028 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7029 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7030 return LY_EVALID;
7031 }
7032 }
7033
7034 /* store the target's type and check for circular chain of leafrefs */
7035 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7036 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7037 if (type == (struct lysc_type *)lref) {
7038 /* circular chain detected */
7039 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7040 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7041 return LY_EVALID;
7042 }
7043 }
7044
7045 /* check if leafref and its target are under common if-features */
7046 if (lys_compile_leafref_features_validate(node, target)) {
7047 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7048 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7049 " features applicable to the leafref itself.", lref->path->expr);
7050 return LY_EVALID;
7051 }
7052
7053 return LY_SUCCESS;
7054}
7055
7056static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007057lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7058{
7059 struct lysc_ext_instance *ext;
7060 struct lysp_ext_instance *ext_p = NULL;
7061 struct lysp_stmt *stmt;
7062 const struct lys_module *ext_mod;
7063 LY_ERR ret = LY_SUCCESS;
7064
7065 /* create the parsed extension instance manually */
7066 ext_p = calloc(1, sizeof *ext_p);
7067 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
7068 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
7069 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
7070 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7071 ext_p->insubstmt_index = 0;
7072
7073 stmt = calloc(1, sizeof *ext_p->child);
7074 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
7075 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
7076 stmt->kw = LY_STMT_TYPE;
7077 ext_p->child = stmt;
7078
7079 /* allocate new extension instance */
7080 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7081
7082 /* manually get extension definition module */
7083 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7084
7085 /* compile the extension instance */
7086 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7087
7088cleanup:
7089 lysp_ext_instance_free(ctx->ctx, ext_p);
7090 free(ext_p);
7091 return ret;
7092}
7093
Michal Vasko004d3152020-06-11 19:59:22 +02007094static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007095lys_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 +02007096 const struct lys_module *dflt_mod, struct lyd_value *storage)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007097{
7098 LY_ERR ret;
7099 struct ly_err_item *err = NULL;
7100
7101 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7102 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7103 if (err) {
7104 ly_err_print(err);
7105 ctx->path[0] = '\0';
7106 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7108 "Invalid default - value does not fit the type (%s).", err->msg);
7109 ly_err_free(err);
7110 }
7111 if (!ret) {
7112 ++storage->realtype->refcount;
7113 return LY_SUCCESS;
7114 }
7115 return ret;
7116}
7117
7118static LY_ERR
7119lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +02007120 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007121{
7122 LY_ERR ret;
7123
7124 assert(!leaf->dflt);
7125
7126 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7127 /* ignore default values for keys and mandatory leaves */
7128 return LY_SUCCESS;
7129 }
7130
7131 /* allocate the default value */
7132 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7133 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7134
7135 /* store the default value */
7136 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt);
7137 if (ret) {
7138 free(leaf->dflt);
7139 leaf->dflt = NULL;
7140 }
7141
7142 return ret;
7143}
7144
7145static LY_ERR
7146lys_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 +02007147 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007148{
7149 LY_ERR ret;
7150 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7151
7152 assert(dflt || dflts);
7153
7154 if (llist->dflts) {
7155 /* there were already some defaults and we are adding new by deviations */
7156 assert(dflts);
7157 orig_count = LY_ARRAY_COUNT(llist->dflts);
7158 } else {
7159 orig_count = 0;
7160 }
7161
7162 /* allocate new items */
7163 if (dflts) {
7164 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7165 } else {
7166 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7167 }
7168
7169 /* fill each new default value */
7170 if (dflts) {
7171 LY_ARRAY_FOR(dflts, u) {
7172 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
7173 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod,
7174 llist->dflts[orig_count + u]);
7175 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7176 LY_ARRAY_INCREMENT(llist->dflts);
7177 }
7178 } else {
7179 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
7180 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]);
7181 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7182 LY_ARRAY_INCREMENT(llist->dflts);
7183 }
7184
7185 /* check default value uniqueness */
7186 if (llist->flags & LYS_CONFIG_W) {
7187 /* configuration data values must be unique - so check the default values */
7188 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7189 for (v = 0; v < u; ++v) {
7190 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007191 uint8_t dynamic = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007192 const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic);
7193
7194 lysc_update_path(ctx, llist->parent, llist->name);
7195 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7196 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
7197 lysc_update_path(ctx, NULL, NULL);
7198 if (dynamic) {
7199 free((char *)val);
7200 }
7201 return LY_EVALID;
7202 }
7203 }
7204 }
7205 }
7206
7207 return LY_SUCCESS;
7208}
7209
7210static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007211lys_compile_unres(struct lysc_ctx *ctx)
7212{
7213 struct lysc_node *node;
7214 struct lysc_type *type, *typeiter;
7215 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007216 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007217 uint32_t i;
7218
7219 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7220 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7221 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7222 for (i = 0; i < ctx->leafrefs.count; ++i) {
7223 node = ctx->leafrefs.objs[i];
7224 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7225 type = ((struct lysc_node_leaf *)node)->type;
7226 if (type->basetype == LY_TYPE_LEAFREF) {
7227 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7228 } else if (type->basetype == LY_TYPE_UNION) {
7229 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7230 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7231 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7232 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7233 }
7234 }
7235 }
7236 }
7237 for (i = 0; i < ctx->leafrefs.count; ++i) {
7238 /* store pointer to the real type */
7239 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7240 if (type->basetype == LY_TYPE_LEAFREF) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007241 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007242 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007243 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7244 ((struct lysc_type_leafref *)type)->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007245 } else if (type->basetype == LY_TYPE_UNION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007246 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7247 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7248 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007249 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007250 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7251 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007252 }
7253 }
7254 }
7255 }
7256
7257 /* check xpath */
7258 for (i = 0; i < ctx->xpath.count; ++i) {
7259 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7260 }
7261
7262 /* finish incomplete default values compilation */
7263 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007264 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007265 if (r->leaf->nodetype == LYS_LEAF) {
7266 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod));
7267 } else {
7268 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 +02007269 }
Michal Vasko004d3152020-06-11 19:59:22 +02007270 }
7271
7272 return LY_SUCCESS;
7273}
7274
Radek Krejci19a96102018-11-15 13:38:09 +01007275LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007276lys_compile(struct lys_module **mod, uint32_t options)
Radek Krejci19a96102018-11-15 13:38:09 +01007277{
7278 struct lysc_ctx ctx = {0};
7279 struct lysc_module *mod_c;
7280 struct lysp_module *sp;
7281 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007282 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007283 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007284 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007285 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007286 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007287 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007288 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007289
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007290 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007291
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007292 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007293 /* just imported modules are not compiled */
7294 return LY_SUCCESS;
7295 }
7296
Radek Krejcia46012b2020-08-12 15:41:04 +02007297 compile_id = ++(*mod)->ctx->module_set_id;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007298 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007299
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007300 ctx.ctx = (*mod)->ctx;
7301 ctx.mod = *mod;
7302 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007303 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007304 ctx.path_len = 1;
7305 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007306
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007307 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7308 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7309 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007310
Michal Vasko7c8439f2020-08-05 13:25:19 +02007311 LY_ARRAY_FOR(sp->imports, u) {
7312 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7313 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01007314 LY_ARRAY_FOR(sp->includes, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02007315 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007316 }
Radek Krejci0935f412019-08-20 16:15:18 +02007317
7318 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007319 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007320 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007321 mod_c->features = (*mod)->dis_features;
7322 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007323 } else {
7324 /* features are compiled directly into the compiled module structure,
7325 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci327de162019-06-14 12:52:07 +02007326 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007327 LY_CHECK_GOTO(ret, error);
7328 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007329
Radek Krejci0af46292019-01-11 16:02:31 +01007330 /* finish feature compilation, not only for the main module, but also for the submodules.
7331 * Due to possible forward references, it must be done when all the features (including submodules)
7332 * are present. */
7333 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007334 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007335 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7336 }
Radek Krejci327de162019-06-14 12:52:07 +02007337 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007338 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007339 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007340 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007341 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007342 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7343 }
Radek Krejci327de162019-06-14 12:52:07 +02007344 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007345 }
Radek Krejci327de162019-06-14 12:52:07 +02007346 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007347
Michal Vasko33ff9422020-07-03 09:50:39 +02007348 /* identities, work similarly to features with the precompilation */
7349 if ((*mod)->dis_identities) {
7350 mod_c->identities = (*mod)->dis_identities;
7351 (*mod)->dis_identities = NULL;
7352 } else {
7353 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7354 LY_CHECK_GOTO(ret, error);
7355 }
Radek Krejci19a96102018-11-15 13:38:09 +01007356 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007357 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007358 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007359 lysc_update_path(&ctx, NULL, "{submodule}");
7360 LY_ARRAY_FOR(sp->includes, v) {
7361 if (sp->includes[v].submodule->identities) {
7362 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7363 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7364 LY_CHECK_GOTO(ret, error);
7365 lysc_update_path(&ctx, NULL, NULL);
7366 }
7367 }
Radek Krejci327de162019-06-14 12:52:07 +02007368 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007369
Radek Krejci95710c92019-02-11 15:49:55 +01007370 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007371 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007372 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007373 }
Radek Krejci95710c92019-02-11 15:49:55 +01007374
Radek Krejciec4da802019-05-02 13:02:41 +02007375 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7376 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007377
Radek Krejci95710c92019-02-11 15:49:55 +01007378 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007379 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007380 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007381 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007382 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007383
Radek Krejci474f9b82019-07-24 11:36:37 +02007384 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007385 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007386
Radek Krejci0935f412019-08-20 16:15:18 +02007387 /* extension instances TODO cover extension instances from submodules */
7388 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007389
Michal Vasko004d3152020-06-11 19:59:22 +02007390 /* finish compilation for all unresolved items in the context */
7391 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007392
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007393 /* validate non-instantiated groupings from the parsed schema,
7394 * without it we would accept even the schemas with invalid grouping specification */
7395 ctx.options |= LYSC_OPT_GROUPING;
7396 LY_ARRAY_FOR(sp->groupings, u) {
7397 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007398 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007399 }
7400 }
7401 LY_LIST_FOR(sp->data, node_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007402 grps = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007403 LY_ARRAY_FOR(grps, u) {
7404 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007405 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007406 }
7407 }
7408 }
7409
Radek Krejci474f9b82019-07-24 11:36:37 +02007410 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7411 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7412 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7413 }
7414
Michal Vasko8d544252020-03-02 10:19:52 +01007415#if 0
7416 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7417 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7418 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7419 * the anotation definitions available in the internal schema structure. */
7420 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7421 if (lyp_add_ietf_netconf_annotations(mod)) {
7422 lys_free(mod, NULL, 1, 1);
7423 return NULL;
7424 }
7425 }
7426#endif
7427
7428 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007429 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7430 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007431 }
7432
Radek Krejci1c0c3442019-07-23 16:08:47 +02007433 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007434 ly_set_erase(&ctx.xpath, NULL);
7435 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007436 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007437 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007438 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007439
Radek Krejciec4da802019-05-02 13:02:41 +02007440 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007441 lysp_module_free((*mod)->parsed);
7442 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007443 }
7444
Radek Krejciec4da802019-05-02 13:02:41 +02007445 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007446 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007447 for (i = 0; i < ctx.ctx->list.count; ++i) {
7448 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007449 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007450 m->implemented = 1;
7451 }
7452 }
7453 }
7454
Radek Krejci19a96102018-11-15 13:38:09 +01007455 return LY_SUCCESS;
7456
7457error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007458 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007459 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007460 ly_set_erase(&ctx.xpath, NULL);
7461 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007462 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007463 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007464 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007465 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007466 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007467
Radek Krejcia46012b2020-08-12 15:41:04 +02007468 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7469 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7470 * 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 +02007471 for (i = 0; i < ctx.ctx->list.count; ++i) {
7472 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007473 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007474 /* revert features list to the precompiled state */
7475 lys_feature_precompile_revert(&ctx, m);
7476 /* mark module as imported-only / not-implemented */
7477 m->implemented = 0;
7478 /* free the compiled version of the module */
7479 lysc_module_free(m->compiled, NULL);
7480 m->compiled = NULL;
7481 }
7482 }
7483
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007484 /* remove the module itself from the context and free it */
7485 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7486 lys_module_free(*mod, NULL);
7487 *mod = NULL;
7488
Radek Krejci19a96102018-11-15 13:38:09 +01007489 return ret;
7490}