blob: 720351a29689dd02b4683f5be27b58e3b1890631 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Michal Vasko7c8439f2020-08-05 13:25:19 +020039#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010041#include "tree_schema_internal.h"
42#include "xpath.h"
43
Michal Vasko5fe75f12020-03-02 13:52:37 +010044static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
Radek Krejci0f969882020-08-21 16:56:47 +020045 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
Michal Vasko5fe75f12020-03-02 13:52:37 +010046
Radek Krejci19a96102018-11-15 13:38:09 +010047/**
48 * @brief Duplicate string into dictionary
49 * @param[in] CTX libyang context of the dictionary.
50 * @param[in] ORIG String to duplicate.
51 * @param[out] DUP Where to store the result.
52 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020053#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
54
55#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
Radek Krejci19a96102018-11-15 13:38:09 +010056
Radek Krejciec4da802019-05-02 13:02:41 +020057#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010058 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020059 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
60 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
61 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020063 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010064 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
65 } \
66 }
67
Radek Krejciec4da802019-05-02 13:02:41 +020068#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010069 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020070 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
71 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
72 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010073 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020074 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010075 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
76 } \
77 }
78
Radek Krejci0935f412019-08-20 16:15:18 +020079#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
80 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020081 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
82 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020083 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010084 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020085 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
86 } \
87 }
88
Radek Krejciec4da802019-05-02 13:02:41 +020089#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010090 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020091 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
92 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
93 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010094 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020095 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010096 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
97 } \
98 }
99
Radek Krejciec4da802019-05-02 13:02:41 +0200100#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +0100101 if (MEMBER_P) { \
102 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
103 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200104 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100105 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
106 }
107
Radek Krejciec4da802019-05-02 13:02:41 +0200108#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 if (MEMBER_P) { \
110 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200111 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100112 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200113 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100114 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
115 }
116
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100117#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200119 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100120 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100121 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
122 return LY_EVALID; \
123 } \
124 } \
125 }
126
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
128 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200129 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100130 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
131 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
132 return LY_EVALID; \
133 } \
134 } \
135 }
136
137struct lysc_ext *
138lysc_ext_dup(struct lysc_ext *orig)
139{
140 ++orig->refcount;
141 return orig;
142}
143
Radek Krejci19a96102018-11-15 13:38:09 +0100144static struct lysc_ext_instance *
145lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
146{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100147 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100148 (void) ctx;
149 (void) orig;
150 return NULL;
151}
152
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200153static LY_ERR
154lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +0200155 struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200156{
157 struct lysc_incomplete_dflt *r;
158 uint32_t i;
159
160 for (i = 0; i < ctx->dflts.count; ++i) {
161 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
162 if (r->leaf == leaf) {
163 /* just replace the default */
164 r->dflt = dflt;
165 return LY_SUCCESS;
166 }
167 }
168
169 r = malloc(sizeof *r);
170 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
171 r->leaf = leaf;
172 r->dflt = dflt;
173 r->dflts = NULL;
174 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200175 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200176
177 return LY_SUCCESS;
178}
179
Radek Krejcib56c7502019-02-13 14:19:54 +0100180/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200181 * @brief Add record into the compile context's list of incomplete default values.
182 * @param[in] ctx Compile context with the incomplete default values list.
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200183 * @param[in] term Term context node with the default value.
184 * @param[in] value String default value.
185 * @param[in] val_len Length of @p value.
Radek Krejci474f9b82019-07-24 11:36:37 +0200186 * @param[in] dflt_mod Module of the default value definition to store in the record.
187 * @return LY_EMEM in case of memory allocation failure.
188 * @return LY_SUCCESS
189 */
190static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200191lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts,
Radek Krejci0f969882020-08-21 16:56:47 +0200192 struct lys_module *dflt_mod)
Radek Krejci474f9b82019-07-24 11:36:37 +0200193{
194 struct lysc_incomplete_dflt *r;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200195 uint32_t i;
196
197 for (i = 0; i < ctx->dflts.count; ++i) {
198 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
199 if (r->llist == llist) {
200 /* just replace the defaults */
201 r->dflts = dflts;
202 return LY_SUCCESS;
203 }
204 }
205
Radek Krejci474f9b82019-07-24 11:36:37 +0200206 r = malloc(sizeof *r);
207 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200208 r->llist = llist;
209 r->dflt = NULL;
210 r->dflts = dflts;
Radek Krejci474f9b82019-07-24 11:36:37 +0200211 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200212 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Radek Krejci474f9b82019-07-24 11:36:37 +0200213
214 return LY_SUCCESS;
215}
216
217/**
218 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
219 * @param[in] ctx Compile context with the incomplete default values list.
220 * @param[in] dflt Incomplete default values identifying the record to remove.
221 */
222static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200223lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term)
Radek Krejci474f9b82019-07-24 11:36:37 +0200224{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200225 uint32_t u;
Radek Krejci474f9b82019-07-24 11:36:37 +0200226 struct lysc_incomplete_dflt *r;
227
228 for (u = 0; u < ctx->dflts.count; ++u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200229 r = ctx->dflts.objs[u];
230 if (r->leaf == (struct lysc_node_leaf *)term) {
Radek Krejci474f9b82019-07-24 11:36:37 +0200231 free(ctx->dflts.objs[u]);
232 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
233 --ctx->dflts.count;
234 return;
235 }
236 }
237}
238
Radek Krejci0e59c312019-08-15 15:34:15 +0200239void
Radek Krejci327de162019-06-14 12:52:07 +0200240lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
241{
242 int len;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200243 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
Radek Krejci327de162019-06-14 12:52:07 +0200244
245 if (!name) {
246 /* removing last path segment */
247 if (ctx->path[ctx->path_len - 1] == '}') {
Michal Vaskod989ba02020-08-24 10:59:24 +0200248 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200249 if (ctx->path[ctx->path_len] == '=') {
250 ctx->path[ctx->path_len++] = '}';
251 } else {
252 /* not a top-level special tag, remove also preceiding '/' */
253 goto remove_nodelevel;
254 }
255 } else {
256remove_nodelevel:
Michal Vaskod989ba02020-08-24 10:59:24 +0200257 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200258 if (ctx->path_len == 0) {
259 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200260 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200261 }
262 }
263 /* set new terminating NULL-byte */
264 ctx->path[ctx->path_len] = '\0';
265 } else {
266 if (ctx->path_len > 1) {
267 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
268 /* extension of the special tag */
269 nextlevel = 2;
270 --ctx->path_len;
271 } else {
272 /* there is already some path, so add next level */
273 nextlevel = 1;
274 }
275 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
276
277 if (nextlevel != 2) {
278 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
279 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200280 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200281 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200282 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200283 }
284 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200285 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200286 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200287 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
Radek Krejciacc79042019-07-25 14:14:57 +0200288 /* output truncated */
289 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
290 } else {
291 ctx->path_len += len;
292 }
Radek Krejci327de162019-06-14 12:52:07 +0200293 }
294}
295
296/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100297 * @brief Duplicate the compiled pattern structure.
298 *
299 * Instead of duplicating memory, the reference counter in the @p orig is increased.
300 *
301 * @param[in] orig The pattern structure to duplicate.
302 * @return The duplicated structure to use.
303 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200304static struct lysc_pattern *
Radek Krejci19a96102018-11-15 13:38:09 +0100305lysc_pattern_dup(struct lysc_pattern *orig)
306{
307 ++orig->refcount;
308 return orig;
309}
310
Radek Krejcib56c7502019-02-13 14:19:54 +0100311/**
312 * @brief Duplicate the array of compiled patterns.
313 *
314 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
315 *
316 * @param[in] ctx Libyang context for logging.
317 * @param[in] orig The patterns sized array to duplicate.
318 * @return New sized array as a copy of @p orig.
319 * @return NULL in case of memory allocation error.
320 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200321static struct lysc_pattern **
Radek Krejci19a96102018-11-15 13:38:09 +0100322lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
323{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100324 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200325 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327 assert(orig);
328
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200329 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100330 LY_ARRAY_FOR(orig, u) {
331 dup[u] = lysc_pattern_dup(orig[u]);
332 LY_ARRAY_INCREMENT(dup);
333 }
334 return dup;
335}
336
Radek Krejcib56c7502019-02-13 14:19:54 +0100337/**
338 * @brief Duplicate compiled range structure.
339 *
340 * @param[in] ctx Libyang context for logging.
341 * @param[in] orig The range structure to be duplicated.
342 * @return New compiled range structure as a copy of @p orig.
343 * @return NULL in case of memory allocation error.
344 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200345struct lysc_range *
Radek Krejci19a96102018-11-15 13:38:09 +0100346lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
347{
348 struct lysc_range *dup;
349 LY_ERR ret;
350
Radek Krejcib56c7502019-02-13 14:19:54 +0100351 assert(orig);
352
Radek Krejci19a96102018-11-15 13:38:09 +0100353 dup = calloc(1, sizeof *dup);
354 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
355 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200356 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
357 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
358 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100359 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200360 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
361 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100362 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
363
364 return dup;
365cleanup:
366 free(dup);
367 (void) ret; /* set but not used due to the return type */
368 return NULL;
369}
370
Radek Krejcib56c7502019-02-13 14:19:54 +0100371/**
372 * @brief Stack for processing if-feature expressions.
373 */
Radek Krejci19a96102018-11-15 13:38:09 +0100374struct iff_stack {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200375 size_t size; /**< number of items in the stack */
376 size_t index; /**< first empty item */
377 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100378};
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Add @ref ifftokens into the stack.
382 * @param[in] stack The if-feature stack to use.
383 * @param[in] value One of the @ref ifftokens to store in the stack.
384 * @return LY_EMEM in case of memory allocation error
385 * @return LY_ESUCCESS if the value successfully stored.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static LY_ERR
388iff_stack_push(struct iff_stack *stack, uint8_t value)
389{
390 if (stack->index == stack->size) {
391 stack->size += 4;
392 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
393 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
394 }
395 stack->stack[stack->index++] = value;
396 return LY_SUCCESS;
397}
398
Radek Krejcib56c7502019-02-13 14:19:54 +0100399/**
400 * @brief Get (and remove) the last item form the stack.
401 * @param[in] stack The if-feature stack to use.
402 * @return The value from the top of the stack.
403 */
Radek Krejci19a96102018-11-15 13:38:09 +0100404static uint8_t
405iff_stack_pop(struct iff_stack *stack)
406{
Radek Krejcib56c7502019-02-13 14:19:54 +0100407 assert(stack && stack->index);
408
Radek Krejci19a96102018-11-15 13:38:09 +0100409 stack->index--;
410 return stack->stack[stack->index];
411}
412
Radek Krejcib56c7502019-02-13 14:19:54 +0100413/**
414 * @brief Clean up the stack.
415 * @param[in] stack The if-feature stack to use.
416 */
Radek Krejci19a96102018-11-15 13:38:09 +0100417static void
418iff_stack_clean(struct iff_stack *stack)
419{
420 stack->size = 0;
421 free(stack->stack);
422}
423
Radek Krejcib56c7502019-02-13 14:19:54 +0100424/**
425 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
426 * (libyang format of the if-feature expression).
427 * @param[in,out] list The 2bits array to modify.
428 * @param[in] op The operand (@ref ifftokens) to store.
429 * @param[in] pos Position (0-based) where to store the given @p op.
430 */
Radek Krejci19a96102018-11-15 13:38:09 +0100431static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200432iff_setop(uint8_t *list, uint8_t op, size_t pos)
Radek Krejci19a96102018-11-15 13:38:09 +0100433{
434 uint8_t *item;
435 uint8_t mask = 3;
436
Radek Krejci19a96102018-11-15 13:38:09 +0100437 assert(op <= 3); /* max 2 bits */
438
439 item = &list[pos / 4];
440 mask = mask << 2 * (pos % 4);
441 *item = (*item) & ~mask;
442 *item = (*item) | (op << 2 * (pos % 4));
443}
444
Radek Krejcib56c7502019-02-13 14:19:54 +0100445#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
446#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100447
Radek Krejci0af46292019-01-11 16:02:31 +0100448/**
449 * @brief Find a feature of the given name and referenced in the given module.
450 *
451 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
452 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
453 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
454 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
455 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
456 * assigned till that time will be still valid.
457 *
458 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
459 * @param[in] name Name of the feature including possible prefix.
460 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
461 * @return Pointer to the feature structure if found, NULL otherwise.
462 */
Radek Krejci19a96102018-11-15 13:38:09 +0100463static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100464lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100465{
466 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200467 LY_ARRAY_COUNT_TYPE u;
Radek Krejci14915cc2020-09-14 17:28:13 +0200468 struct lysc_feature *f;
Radek Krejci19a96102018-11-15 13:38:09 +0100469
Radek Krejci120d8542020-08-12 09:29:16 +0200470 assert(mod);
471
Radek Krejci19a96102018-11-15 13:38:09 +0100472 for (i = 0; i < len; ++i) {
473 if (name[i] == ':') {
474 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100475 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100476 LY_CHECK_RET(!mod, NULL);
477
478 name = &name[i + 1];
479 len = len - i - 1;
480 }
481 }
482
483 /* we have the correct module, get the feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200484 LY_ARRAY_FOR(mod->features, u) {
485 f = &mod->features[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200486 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100487 return f;
488 }
489 }
490
491 return NULL;
492}
493
Michal Vasko8d544252020-03-02 10:19:52 +0100494/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100495 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
496 */
497static LY_ERR
498lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
499{
500 LY_ERR ret = LY_SUCCESS;
501
502 if (!ext_p->compiled) {
503 lysc_update_path(ctx, NULL, "{extension}");
504 lysc_update_path(ctx, NULL, ext_p->name);
505
506 /* compile the extension definition */
507 ext_p->compiled = calloc(1, sizeof **ext);
508 ext_p->compiled->refcount = 1;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200509 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
510 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100511 ext_p->compiled->module = (struct lys_module *)ext_mod;
512 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
513
514 lysc_update_path(ctx, NULL, NULL);
515 lysc_update_path(ctx, NULL, NULL);
516
517 /* find extension definition plugin */
518 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
519 }
520
521 *ext = lysc_ext_dup(ext_p->compiled);
522
523done:
524 return ret;
525}
526
527/**
Michal Vasko8d544252020-03-02 10:19:52 +0100528 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
529 *
530 * @param[in] ctx Compilation context.
531 * @param[in] ext_p Parsed extension instance.
532 * @param[in,out] ext Prepared compiled extension instance.
533 * @param[in] parent Extension instance parent.
534 * @param[in] parent_type Extension instance parent type.
535 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
536 */
Radek Krejci19a96102018-11-15 13:38:09 +0100537static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100538lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200539 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100540{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200541 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100542 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200543 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200544 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200545 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100546
Radek Krejci011e4aa2020-09-04 15:22:31 +0200547 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
548 LY_CHECK_RET(ret);
549
Radek Krejci19a96102018-11-15 13:38:09 +0100550 ext->insubstmt = ext_p->insubstmt;
551 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200552 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200553 ext->parent = parent;
554 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100555
Michal Vasko22df3f02020-08-24 13:29:22 +0200556 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200557
Radek Krejci19a96102018-11-15 13:38:09 +0100558 /* get module where the extension definition should be placed */
Radek Krejci1e008d22020-08-17 11:37:37 +0200559 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
Radek Krejci7c960162019-09-18 14:16:12 +0200560 if (ext_p->yin) {
561 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
562 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
563 * YANG (import) prefix must be done here. */
564 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200565 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200566 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200567 } else {
568 assert(ctx->mod_def->parsed);
569 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
570 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200571 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200572 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1,
Radek Krejci200f1062020-07-11 22:51:03 +0200573 ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200574 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200575 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200576 break;
577 }
578 }
579 }
580 if (!prefixed_name) {
581 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
582 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200583 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200584 goto cleanup;
585 }
586 } else {
587 prefixed_name = ext_p->name;
588 }
589 lysc_update_path(ctx, NULL, prefixed_name);
590
Michal Vasko8d544252020-03-02 10:19:52 +0100591 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200592 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100593 if (!ext_mod) {
594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
595 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200596 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100597 goto cleanup;
598 } else if (!ext_mod->parsed->extensions) {
599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
600 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
601 prefixed_name, ext_mod->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200602 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100603 goto cleanup;
604 }
Radek Krejci7c960162019-09-18 14:16:12 +0200605 }
606 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200607
Michal Vasko5fe75f12020-03-02 13:52:37 +0100608 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200609 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
610 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100611 /* compile extension definition and assign it */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200612 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100613 break;
614 }
615 }
Radek Krejci7c960162019-09-18 14:16:12 +0200616 if (!ext->def) {
617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
618 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200619 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200620 goto cleanup;
621 }
Radek Krejci0935f412019-08-20 16:15:18 +0200622
Radek Krejcif56e2a42019-09-09 14:15:25 +0200623 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
624 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
625 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200626 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200627 /* Schema was parsed from YIN and an argument is expected, ... */
628 struct lysp_stmt *stmt = NULL;
629
630 if (ext->def->flags & LYS_YINELEM_TRUE) {
631 /* ... argument was the first XML child element */
632 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
633 /* TODO check namespace of the statement */
634 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
635 stmt = ext_p->child;
636 }
637 }
638 } else {
639 /* ... argument was one of the XML attributes which are represented as child stmt
640 * with LYS_YIN_ATTR flag */
641 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
642 if (!strcmp(stmt->stmt, ext->def->argument)) {
643 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200644 break;
645 }
646 }
647 }
648 if (!stmt) {
649 /* missing extension's argument */
650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200651 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200652 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200653 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200654
655 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200656 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200657 stmt->flags |= LYS_YIN_ARGUMENT;
658 }
Radek Krejci7c960162019-09-18 14:16:12 +0200659 if (prefixed_name != ext_p->name) {
660 lydict_remove(ctx->ctx, ext_p->name);
661 ext_p->name = prefixed_name;
662 if (!ext_p->argument && ext->argument) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200663 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200664 }
665 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200666
Radek Krejci0935f412019-08-20 16:15:18 +0200667 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200668 if (ext->argument) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200669 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
Radek Krejciad5963b2019-09-06 16:03:05 +0200670 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200671 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200672 if (ext->argument) {
673 lysc_update_path(ctx, NULL, NULL);
674 }
Radek Krejci0935f412019-08-20 16:15:18 +0200675 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200676 ext_p->compiled = ext;
677
Radek Krejci7c960162019-09-18 14:16:12 +0200678cleanup:
679 if (prefixed_name && prefixed_name != ext_p->name) {
680 lydict_remove(ctx->ctx, prefixed_name);
681 }
682
Radek Krejcif56e2a42019-09-09 14:15:25 +0200683 lysc_update_path(ctx, NULL, NULL);
684 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200685
Radek Krejci7c960162019-09-18 14:16:12 +0200686 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200687}
688
689/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100690 * @brief Compile information from the if-feature statement
691 * @param[in] ctx Compile context.
692 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100693 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
694 * @return LY_ERR value.
695 */
Radek Krejci19a96102018-11-15 13:38:09 +0100696static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200697lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100698{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200699 LY_ERR rc = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100700 const char *c = *value;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200701 int64_t i, j;
702 int8_t op_len, last_not = 0, checkversion = 0;
703 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci19a96102018-11-15 13:38:09 +0100704 uint8_t op;
705 struct iff_stack stack = {0, 0, NULL};
706 struct lysc_feature *f;
707
708 assert(c);
709
710 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200711 for (i = j = 0; c[i]; i++) {
Radek Krejci19a96102018-11-15 13:38:09 +0100712 if (c[i] == '(') {
713 j++;
714 checkversion = 1;
715 continue;
716 } else if (c[i] == ')') {
717 j--;
718 continue;
719 } else if (isspace(c[i])) {
720 checkversion = 1;
721 continue;
722 }
723
Radek Krejci1deb5be2020-08-26 16:43:36 +0200724 if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
725 uint64_t spaces;
726 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++);
727 if (c[i + op_len + spaces] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
729 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
730 return LY_EVALID;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200731 } else if (!isspace(c[i + op_len])) {
Radek Krejci19a96102018-11-15 13:38:09 +0100732 /* feature name starting with the not/and/or */
733 last_not = 0;
734 f_size++;
735 } else if (c[i] == 'n') { /* not operation */
736 if (last_not) {
737 /* double not */
738 expr_size = expr_size - 2;
739 last_not = 0;
740 } else {
741 last_not = 1;
742 }
743 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200744 if (f_exp != f_size) {
745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200746 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, op_len, &c[i]);
Radek Krejci6788abc2019-06-14 13:56:49 +0200747 return LY_EVALID;
748 }
Radek Krejci19a96102018-11-15 13:38:09 +0100749 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200750
Radek Krejci19a96102018-11-15 13:38:09 +0100751 /* not a not operation */
752 last_not = 0;
753 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200754 i += op_len;
Radek Krejci19a96102018-11-15 13:38:09 +0100755 } else {
756 f_size++;
757 last_not = 0;
758 }
759 expr_size++;
760
761 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200762 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100763 i--;
764 break;
765 }
766 i++;
767 }
768 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200769 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100770 /* not matching count of ( and ) */
771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
772 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
773 return LY_EVALID;
774 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200775 if (f_exp != f_size) {
776 /* features do not match the needed arguments for the logical operations */
777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
778 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
779 "the required number of operands for the operations.", *value);
780 return LY_EVALID;
781 }
Radek Krejci19a96102018-11-15 13:38:09 +0100782
783 if (checkversion || expr_size > 1) {
784 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100785 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
787 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
788 return LY_EVALID;
789 }
790 }
791
792 /* allocate the memory */
793 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
794 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
795 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200796 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
Radek Krejci19a96102018-11-15 13:38:09 +0100797
798 stack.size = expr_size;
799 f_size--; expr_size--; /* used as indexes from now */
800
801 for (i--; i >= 0; i--) {
802 if (c[i] == ')') {
803 /* push it on stack */
804 iff_stack_push(&stack, LYS_IFF_RP);
805 continue;
806 } else if (c[i] == '(') {
807 /* pop from the stack into result all operators until ) */
Michal Vaskod989ba02020-08-24 10:59:24 +0200808 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
Radek Krejci19a96102018-11-15 13:38:09 +0100809 iff_setop(iff->expr, op, expr_size--);
810 }
811 continue;
812 } else if (isspace(c[i])) {
813 continue;
814 }
815
816 /* end of operator or operand -> find beginning and get what is it */
817 j = i + 1;
818 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
819 i--;
820 }
821 i++; /* go back by one step */
822
823 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
824 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
825 /* double not */
826 iff_stack_pop(&stack);
827 } else {
828 /* not has the highest priority, so do not pop from the stack
829 * as in case of AND and OR */
830 iff_stack_push(&stack, LYS_IFF_NOT);
831 }
832 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
833 /* as for OR - pop from the stack all operators with the same or higher
834 * priority and store them to the result, then push the AND to the stack */
835 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
836 op = iff_stack_pop(&stack);
837 iff_setop(iff->expr, op, expr_size--);
838 }
839 iff_stack_push(&stack, LYS_IFF_AND);
840 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
841 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
842 op = iff_stack_pop(&stack);
843 iff_setop(iff->expr, op, expr_size--);
844 }
845 iff_stack_push(&stack, LYS_IFF_OR);
846 } else {
847 /* feature name, length is j - i */
848
849 /* add it to the expression */
850 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
851
852 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100853 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
854 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
855 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
856 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100857 iff->features[f_size] = f;
858 LY_ARRAY_INCREMENT(iff->features);
859 f_size--;
860 }
861 }
862 while (stack.index) {
863 op = iff_stack_pop(&stack);
864 iff_setop(iff->expr, op, expr_size--);
865 }
866
867 if (++expr_size || ++f_size) {
868 /* not all expected operators and operands found */
869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
870 "Invalid value \"%s\" of if-feature - processing error.", *value);
871 rc = LY_EINT;
872 } else {
873 rc = LY_SUCCESS;
874 }
875
876error:
877 /* cleanup */
878 iff_stack_clean(&stack);
879
880 return rc;
881}
882
Radek Krejcib56c7502019-02-13 14:19:54 +0100883/**
Michal Vasko175012e2019-11-06 15:49:14 +0100884 * @brief Get the XPath context node for the given schema node.
885 * @param[in] start The schema node where the XPath expression appears.
886 * @return The context node to evaluate XPath expression in given schema node.
887 * @return NULL in case the context node is the root node.
888 */
889static struct lysc_node *
890lysc_xpath_context(struct lysc_node *start)
891{
Michal Vasko1bf09392020-03-27 12:38:10 +0100892 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Radek Krejci1e008d22020-08-17 11:37:37 +0200893 start = start->parent) {}
Michal Vasko175012e2019-11-06 15:49:14 +0100894 return start;
895}
896
897/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100898 * @brief Compile information from the when statement
899 * @param[in] ctx Compile context.
900 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100901 * @param[in] flags Flags of the node with the "when" defiition.
902 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100903 * @param[out] when Pointer where to store pointer to the created compiled when structure.
904 * @return LY_ERR value.
905 */
Radek Krejci19a96102018-11-15 13:38:09 +0100906static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100907lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100908{
Radek Krejci19a96102018-11-15 13:38:09 +0100909 LY_ERR ret = LY_SUCCESS;
910
Radek Krejci00b874b2019-02-12 10:54:50 +0100911 *when = calloc(1, sizeof **when);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200912 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci00b874b2019-02-12 10:54:50 +0100913 (*when)->refcount = 1;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200914 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejcia0f704a2019-09-09 16:12:23 +0200915 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100916 (*when)->context = lysc_xpath_context(node);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200917 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
918 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200919 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100920 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100921
922done:
923 return ret;
924}
925
Radek Krejcib56c7502019-02-13 14:19:54 +0100926/**
927 * @brief Compile information from the must statement
928 * @param[in] ctx Compile context.
929 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100930 * @param[in,out] must Prepared (empty) compiled must structure to fill.
931 * @return LY_ERR value.
932 */
Radek Krejci19a96102018-11-15 13:38:09 +0100933static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200934lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100935{
Radek Krejci19a96102018-11-15 13:38:09 +0100936 LY_ERR ret = LY_SUCCESS;
937
Radek Krejcif03a9e22020-09-18 20:09:31 +0200938 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1, &must->cond));
Radek Krejci462cf252019-07-24 09:49:08 +0200939 must->module = ctx->mod_def;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200940 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
941 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
942 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
943 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200944 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100945
946done:
947 return ret;
948}
949
Radek Krejcib56c7502019-02-13 14:19:54 +0100950/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200951 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100952 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200953 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100954 * @return LY_ERR value.
955 */
Radek Krejci19a96102018-11-15 13:38:09 +0100956static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200957lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100958{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200959 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100960 LY_ERR ret = LY_SUCCESS;
961
Radek Krejci7f2a5362018-11-28 13:05:37 +0100962 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
963 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100964 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200965 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100966 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200967 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200968 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200969 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200970 LOGINT(ctx->ctx);
971 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200972 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 +0200973 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200974 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200975 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200976 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200977 mod = NULL;
978 }
Radek Krejci19a96102018-11-15 13:38:09 +0100979 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200980 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100981 }
982 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200983 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 +0100984 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 +0200985 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100986 return LY_ENOTFOUND;
987 }
988 }
Radek Krejci19a96102018-11-15 13:38:09 +0100989 }
990
Radek Krejci19a96102018-11-15 13:38:09 +0100991 return ret;
992}
993
Michal Vasko33ff9422020-07-03 09:50:39 +0200994LY_ERR
995lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +0200996 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100997{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200998 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200999 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01001000 LY_ERR ret = LY_SUCCESS;
1001
Michal Vasko33ff9422020-07-03 09:50:39 +02001002 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +02001003
Michal Vasko33ff9422020-07-03 09:50:39 +02001004 if (!ctx_sc) {
1005 context.ctx = ctx;
1006 context.mod = module;
Radek Krejci120d8542020-08-12 09:29:16 +02001007 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +02001008 context.path_len = 1;
1009 context.path[0] = '/';
1010 ctx_sc = &context;
1011 }
Radek Krejci19a96102018-11-15 13:38:09 +01001012
Michal Vasko33ff9422020-07-03 09:50:39 +02001013 if (!identities_p) {
1014 return LY_SUCCESS;
1015 }
1016 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001017 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02001018 }
1019
1020 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001021 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001022 LY_ARRAY_FOR(identities_p, u) {
1023 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
1024
1025 LY_ARRAY_INCREMENT(*identities);
1026 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001027 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
1028 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
1029 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
Michal Vasko33ff9422020-07-03 09:50:39 +02001030 (*identities)[offset + u].module = ctx_sc->mod;
1031 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
1032 lys_compile_iffeature, ret, done);
1033 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
1034 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
1035 LYEXT_PAR_IDENT, ret, done);
1036 (*identities)[offset + u].flags = identities_p[u].flags;
1037
1038 lysc_update_path(ctx_sc, NULL, NULL);
1039 }
1040 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001041done:
1042 return ret;
1043}
1044
Radek Krejcib56c7502019-02-13 14:19:54 +01001045/**
1046 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1047 *
1048 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1049 *
1050 * @param[in] ctx Compile context for logging.
1051 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1052 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1053 * @return LY_SUCCESS if everything is ok.
1054 * @return LY_EVALID if the identity is derived from itself.
1055 */
Radek Krejci38222632019-02-12 16:55:05 +01001056static LY_ERR
1057lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1058{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001059 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001060 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001061 struct ly_set recursion = {0};
1062 struct lysc_ident *drv;
1063
1064 if (!derived) {
1065 return LY_SUCCESS;
1066 }
1067
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001068 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001069 if (ident == derived[u]) {
1070 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1071 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001072 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001073 goto cleanup;
1074 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001075 ret = ly_set_add(&recursion, derived[u], 0, NULL);
1076 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001077 }
1078
1079 for (v = 0; v < recursion.count; ++v) {
1080 drv = recursion.objs[v];
1081 if (!drv->derived) {
1082 continue;
1083 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001084 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001085 if (ident == drv->derived[u]) {
1086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1087 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001088 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001089 goto cleanup;
1090 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001091 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
1092 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001093 }
1094 }
Radek Krejci38222632019-02-12 16:55:05 +01001095
1096cleanup:
1097 ly_set_erase(&recursion, NULL);
1098 return ret;
1099}
1100
Radek Krejcia3045382018-11-22 14:30:31 +01001101/**
1102 * @brief Find and process the referenced base identities from another identity or identityref
1103 *
Radek Krejciaca74032019-06-04 08:53:06 +02001104 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001105 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1106 * to distinguish these two use cases.
1107 *
1108 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1109 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001110 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001111 * @param[in] bases Array of bases of identityref to fill in.
1112 * @return LY_ERR value.
1113 */
Radek Krejci19a96102018-11-15 13:38:09 +01001114static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001115lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001116 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001117{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001118 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001119 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001120 struct lys_module *mod;
Radek Krejci80d281e2020-09-14 17:42:54 +02001121 struct lysc_ident **idref;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001122
1123 assert(ident || bases);
1124
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001125 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1127 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1128 return LY_EVALID;
1129 }
1130
Michal Vasko33ff9422020-07-03 09:50:39 +02001131 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001132 s = strchr(bases_p[u], ':');
1133 if (s) {
1134 /* prefixed identity */
1135 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001136 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001137 } else {
1138 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001139 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001140 }
1141 if (!mod) {
1142 if (ident) {
1143 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1144 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1145 } else {
1146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1147 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1148 }
1149 return LY_EVALID;
1150 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001151
Radek Krejci555cb5b2018-11-16 14:54:33 +01001152 idref = NULL;
Radek Krejci80d281e2020-09-14 17:42:54 +02001153 LY_ARRAY_FOR(mod->identities, v) {
1154 if (!strcmp(name, mod->identities[v].name)) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001155 if (ident) {
Radek Krejci80d281e2020-09-14 17:42:54 +02001156 if (ident == &mod->identities[v]) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1158 "Identity \"%s\" is derived from itself.", ident->name);
1159 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001160 }
Radek Krejci80d281e2020-09-14 17:42:54 +02001161 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
Michal Vasko33ff9422020-07-03 09:50:39 +02001162 /* we have match! store the backlink */
Radek Krejci80d281e2020-09-14 17:42:54 +02001163 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001164 *idref = ident;
1165 } else {
1166 /* we have match! store the found identity */
1167 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejci80d281e2020-09-14 17:42:54 +02001168 *idref = &mod->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001169 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001170 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001171 }
1172 }
1173 if (!idref || !(*idref)) {
1174 if (ident) {
1175 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1176 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1177 } else {
1178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1179 "Unable to find base (%s) of identityref.", bases_p[u]);
1180 }
1181 return LY_EVALID;
1182 }
1183 }
1184 return LY_SUCCESS;
1185}
1186
Radek Krejcia3045382018-11-22 14:30:31 +01001187/**
1188 * @brief For the given array of identities, set the backlinks from all their base identities.
1189 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1190 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1191 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1192 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1193 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001194static LY_ERR
1195lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1196{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001197 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001198
Michal Vasko33ff9422020-07-03 09:50:39 +02001199 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001200 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001201 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001202 continue;
1203 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001204 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001205 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 +02001206 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001207 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001208 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001209 return LY_SUCCESS;
1210}
1211
Radek Krejci0af46292019-01-11 16:02:31 +01001212LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001213lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001214 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001215{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001216 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001217 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001218 struct lysc_ctx context = {0};
1219
Radek Krejci327de162019-06-14 12:52:07 +02001220 assert(ctx_sc || ctx);
1221
1222 if (!ctx_sc) {
1223 context.ctx = ctx;
1224 context.mod = module;
1225 context.path_len = 1;
1226 context.path[0] = '/';
1227 ctx_sc = &context;
1228 }
Radek Krejci0af46292019-01-11 16:02:31 +01001229
1230 if (!features_p) {
1231 return LY_SUCCESS;
1232 }
1233 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001234 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001235 }
1236
Radek Krejci327de162019-06-14 12:52:07 +02001237 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001238 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001239 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001240 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1241
Radek Krejci0af46292019-01-11 16:02:31 +01001242 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001243 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001244 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
1245 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
1246 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001247 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001248 (*features)[offset + u].module = ctx_sc->mod;
1249
1250 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001251 }
Radek Krejci327de162019-06-14 12:52:07 +02001252 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001253
Radek Krejci011e4aa2020-09-04 15:22:31 +02001254done:
1255 return ret;
Radek Krejci0af46292019-01-11 16:02:31 +01001256}
1257
Radek Krejcia3045382018-11-22 14:30:31 +01001258/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001259 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001260 *
1261 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1262 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001263 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001264 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1265 * being currently processed).
1266 * @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 +01001267 * @return LY_SUCCESS if everything is ok.
1268 * @return LY_EVALID if the feature references indirectly itself.
1269 */
1270static LY_ERR
1271lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1272{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001273 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001274 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001275 struct ly_set recursion = {0};
1276 struct lysc_feature *drv;
1277
1278 if (!depfeatures) {
1279 return LY_SUCCESS;
1280 }
1281
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001282 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001283 if (feature == depfeatures[u]) {
1284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1285 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001286 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001287 goto cleanup;
1288 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001289 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
1290 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001291 }
1292
1293 for (v = 0; v < recursion.count; ++v) {
1294 drv = recursion.objs[v];
1295 if (!drv->depfeatures) {
1296 continue;
1297 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001298 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001299 if (feature == drv->depfeatures[u]) {
1300 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1301 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001302 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001303 goto cleanup;
1304 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001305 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
1306 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001307 }
1308 }
Radek Krejci09a1fc52019-02-13 10:55:17 +01001309
1310cleanup:
1311 ly_set_erase(&recursion, NULL);
1312 return ret;
1313}
1314
1315/**
Radek Krejci0af46292019-01-11 16:02:31 +01001316 * @brief Create pre-compiled features array.
1317 *
1318 * See lys_feature_precompile() for more details.
1319 *
Radek Krejcia3045382018-11-22 14:30:31 +01001320 * @param[in] ctx Compile context.
1321 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001322 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001323 * @return LY_ERR value.
1324 */
Radek Krejci19a96102018-11-15 13:38:09 +01001325static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001326lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001327{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001328 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001329 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001330 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001331
Radek Krejci0af46292019-01-11 16:02:31 +01001332 /* find the preprecompiled feature */
1333 LY_ARRAY_FOR(features, x) {
1334 if (strcmp(features[x].name, feature_p->name)) {
1335 continue;
1336 }
1337 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001338 lysc_update_path(ctx, NULL, "{feature}");
1339 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001340
Radek Krejci0af46292019-01-11 16:02:31 +01001341 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001342 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001343 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001344 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001345 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001346 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001347 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001348 /* check for circular dependency - direct reference first,... */
1349 if (feature == feature->iffeatures[u].features[v]) {
1350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1351 "Feature \"%s\" is referenced from itself.", feature->name);
1352 return LY_EVALID;
1353 }
1354 /* ... and indirect circular reference */
1355 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1356
Radek Krejci0af46292019-01-11 16:02:31 +01001357 /* add itself into the dependants list */
1358 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1359 *df = feature;
1360 }
Radek Krejci19a96102018-11-15 13:38:09 +01001361 }
Radek Krejci19a96102018-11-15 13:38:09 +01001362 }
1363 }
Radek Krejci327de162019-06-14 12:52:07 +02001364 lysc_update_path(ctx, NULL, NULL);
1365 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001366 done:
1367 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001368 }
Radek Krejci0af46292019-01-11 16:02:31 +01001369
1370 LOGINT(ctx->ctx);
1371 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001372}
1373
Radek Krejcib56c7502019-02-13 14:19:54 +01001374/**
1375 * @brief Revert compiled list of features back to the precompiled state.
1376 *
1377 * 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 +02001378 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001379 *
1380 * @param[in] ctx Compilation context.
1381 * @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 +02001382 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001383 */
Radek Krejci95710c92019-02-11 15:49:55 +01001384static void
1385lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1386{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001387 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001388
Michal Vasko33ff9422020-07-03 09:50:39 +02001389 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001390 * which may points into the data being freed here */
Radek Krejci14915cc2020-09-14 17:28:13 +02001391 LY_ARRAY_FOR(mod->features, u) {
1392 LY_ARRAY_FOR(mod->features[u].iffeatures, v) {
1393 lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001394 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001395 LY_ARRAY_FREE(mod->features[u].iffeatures);
1396 mod->features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001397
Radek Krejci14915cc2020-09-14 17:28:13 +02001398 LY_ARRAY_FOR(mod->features[u].exts, v) {
1399 lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001400 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001401 LY_ARRAY_FREE(mod->features[u].exts);
1402 mod->features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001403 }
1404}
1405
Radek Krejcia3045382018-11-22 14:30:31 +01001406/**
1407 * @brief Validate and normalize numeric value from a range definition.
1408 * @param[in] ctx Compile context.
1409 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1410 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1411 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1412 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1413 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1414 * @param[in] value String value of the range boundary.
1415 * @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.
1416 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1417 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1418 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001419LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001420range_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 +01001421{
Radek Krejci6cba4292018-11-15 17:33:29 +01001422 size_t fraction = 0, size;
1423
Radek Krejci19a96102018-11-15 13:38:09 +01001424 *len = 0;
1425
1426 assert(value);
1427 /* parse value */
1428 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1429 return LY_EVALID;
1430 }
1431
1432 if ((value[*len] == '-') || (value[*len] == '+')) {
1433 ++(*len);
1434 }
1435
1436 while (isdigit(value[*len])) {
1437 ++(*len);
1438 }
1439
1440 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001441 if (basetype == LY_TYPE_DEC64) {
1442 goto decimal;
1443 } else {
1444 *valcopy = strndup(value, *len);
1445 return LY_SUCCESS;
1446 }
Radek Krejci19a96102018-11-15 13:38:09 +01001447 }
1448 fraction = *len;
1449
1450 ++(*len);
1451 while (isdigit(value[*len])) {
1452 ++(*len);
1453 }
1454
Radek Krejci6cba4292018-11-15 17:33:29 +01001455 if (basetype == LY_TYPE_DEC64) {
1456decimal:
1457 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001458 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1460 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1461 *len, value, frdigits);
1462 return LY_EINVAL;
1463 }
1464 if (fraction) {
1465 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1466 } else {
1467 size = (*len) + frdigits + 1;
1468 }
1469 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001470 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1471
Radek Krejci6cba4292018-11-15 17:33:29 +01001472 (*valcopy)[size - 1] = '\0';
1473 if (fraction) {
1474 memcpy(&(*valcopy)[0], &value[0], fraction);
1475 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1476 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1477 } else {
1478 memcpy(&(*valcopy)[0], &value[0], *len);
1479 memset(&(*valcopy)[*len], '0', frdigits);
1480 }
Radek Krejci19a96102018-11-15 13:38:09 +01001481 }
1482 return LY_SUCCESS;
1483}
1484
Radek Krejcia3045382018-11-22 14:30:31 +01001485/**
1486 * @brief Check that values in range are in ascendant order.
1487 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001488 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1489 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001490 * @param[in] value Current value to check.
1491 * @param[in] prev_value The last seen value.
1492 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1493 */
Radek Krejci19a96102018-11-15 13:38:09 +01001494static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001495range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001496{
1497 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001498 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001499 return LY_EEXIST;
1500 }
1501 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001502 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001503 return LY_EEXIST;
1504 }
1505 }
1506 return LY_SUCCESS;
1507}
1508
Radek Krejcia3045382018-11-22 14:30:31 +01001509/**
1510 * @brief Set min/max value of the range part.
1511 * @param[in] ctx Compile context.
1512 * @param[in] part Range part structure to fill.
1513 * @param[in] max Flag to distinguish if storing min or max value.
1514 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1515 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1516 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1517 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1518 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001519 * @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 +01001520 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1521 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1522 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1523 * frdigits value), LY_EMEM.
1524 */
Radek Krejci19a96102018-11-15 13:38:09 +01001525static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001526range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
1527 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001528{
1529 LY_ERR ret = LY_SUCCESS;
1530 char *valcopy = NULL;
1531 size_t len;
1532
1533 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001534 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001535 LY_CHECK_GOTO(ret, finalize);
1536 }
1537 if (!valcopy && base_range) {
1538 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001539 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001540 } else {
1541 part->min_64 = base_range->parts[0].min_64;
1542 }
1543 if (!first) {
1544 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1545 }
1546 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001547 }
1548
1549 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001550 case LY_TYPE_INT8: /* range */
1551 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001552 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 +01001553 } else if (max) {
1554 part->max_64 = INT64_C(127);
1555 } else {
1556 part->min_64 = INT64_C(-128);
1557 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001558 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001559 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001560 }
1561 break;
1562 case LY_TYPE_INT16: /* range */
1563 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001564 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 +01001565 } else if (max) {
1566 part->max_64 = INT64_C(32767);
1567 } else {
1568 part->min_64 = INT64_C(-32768);
1569 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001570 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001571 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001572 }
1573 break;
1574 case LY_TYPE_INT32: /* range */
1575 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001576 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 +01001577 } else if (max) {
1578 part->max_64 = INT64_C(2147483647);
1579 } else {
1580 part->min_64 = INT64_C(-2147483648);
1581 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001582 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001583 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001584 }
1585 break;
1586 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001587 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001588 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001589 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001590 max ? &part->max_64 : &part->min_64);
1591 } else if (max) {
1592 part->max_64 = INT64_C(9223372036854775807);
1593 } else {
1594 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1595 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001596 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001597 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001598 }
1599 break;
1600 case LY_TYPE_UINT8: /* range */
1601 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001602 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 +01001603 } else if (max) {
1604 part->max_u64 = UINT64_C(255);
1605 } else {
1606 part->min_u64 = UINT64_C(0);
1607 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001608 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001609 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001610 }
1611 break;
1612 case LY_TYPE_UINT16: /* range */
1613 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001614 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 +01001615 } else if (max) {
1616 part->max_u64 = UINT64_C(65535);
1617 } else {
1618 part->min_u64 = UINT64_C(0);
1619 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001620 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001621 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001622 }
1623 break;
1624 case LY_TYPE_UINT32: /* range */
1625 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001626 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 +01001627 } else if (max) {
1628 part->max_u64 = UINT64_C(4294967295);
1629 } else {
1630 part->min_u64 = UINT64_C(0);
1631 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001632 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001633 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001634 }
1635 break;
1636 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001637 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001638 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001639 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001640 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 +01001641 } else if (max) {
1642 part->max_u64 = UINT64_C(18446744073709551615);
1643 } else {
1644 part->min_u64 = UINT64_C(0);
1645 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001646 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001647 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001648 }
1649 break;
1650 default:
1651 LOGINT(ctx->ctx);
1652 ret = LY_EINT;
1653 }
1654
Radek Krejci5969f272018-11-23 10:03:58 +01001655finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001656 if (ret == LY_EDENIED) {
1657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1658 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1659 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1660 } else if (ret == LY_EVALID) {
1661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1662 "Invalid %s restriction - invalid value \"%s\".",
1663 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1664 } else if (ret == LY_EEXIST) {
1665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1666 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001667 length_restr ? "length" : "range",
Radek Krejci0f969882020-08-21 16:56:47 +02001668 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001669 } else if (!ret && value) {
1670 *value = *value + len;
1671 }
1672 free(valcopy);
1673 return ret;
1674}
1675
Radek Krejcia3045382018-11-22 14:30:31 +01001676/**
1677 * @brief Compile the parsed range restriction.
1678 * @param[in] ctx Compile context.
1679 * @param[in] range_p Parsed range structure to compile.
1680 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1681 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1682 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1683 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1684 * range restriction must be more restrictive than the base_range.
1685 * @param[in,out] range Pointer to the created current range structure.
1686 * @return LY_ERR value.
1687 */
Radek Krejci19a96102018-11-15 13:38:09 +01001688static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001689lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
Radek Krejci0f969882020-08-21 16:56:47 +02001690 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001691{
1692 LY_ERR ret = LY_EVALID;
1693 const char *expr;
1694 struct lysc_range_part *parts = NULL, *part;
Radek Krejci857189e2020-09-01 13:26:36 +02001695 ly_bool range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001696 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001697
1698 assert(range);
1699 assert(range_p);
1700
1701 expr = range_p->arg;
Michal Vaskod989ba02020-08-24 10:59:24 +02001702 while (1) {
Radek Krejci19a96102018-11-15 13:38:09 +01001703 if (isspace(*expr)) {
1704 ++expr;
1705 } else if (*expr == '\0') {
1706 if (range_expected) {
1707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1708 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1709 length_restr ? "length" : "range", range_p->arg);
1710 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001711 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1713 "Invalid %s restriction - unexpected end of the expression (%s).",
1714 length_restr ? "length" : "range", range_p->arg);
1715 goto cleanup;
1716 }
1717 parts_done++;
1718 break;
1719 } else if (!strncmp(expr, "min", 3)) {
1720 if (parts) {
1721 /* min cannot be used elsewhere than in the first part */
1722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1723 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1724 expr - range_p->arg, range_p->arg);
1725 goto cleanup;
1726 }
1727 expr += 3;
1728
1729 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001730 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 +01001731 part->max_64 = part->min_64;
1732 } else if (*expr == '|') {
1733 if (!parts || range_expected) {
1734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1735 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1736 goto cleanup;
1737 }
1738 expr++;
1739 parts_done++;
1740 /* process next part of the expression */
1741 } else if (!strncmp(expr, "..", 2)) {
1742 expr += 2;
1743 while (isspace(*expr)) {
1744 expr++;
1745 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001746 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1748 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1749 goto cleanup;
1750 }
1751 /* continue expecting the upper boundary */
1752 range_expected = 1;
1753 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1754 /* number */
1755 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001756 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001757 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 +01001758 range_expected = 0;
1759 } else {
1760 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001761 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 +01001762 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001763 part->max_64 = part->min_64;
1764 }
1765
1766 /* continue with possible another expression part */
1767 } else if (!strncmp(expr, "max", 3)) {
1768 expr += 3;
1769 while (isspace(*expr)) {
1770 expr++;
1771 }
1772 if (*expr != '\0') {
1773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1774 length_restr ? "length" : "range", expr);
1775 goto cleanup;
1776 }
1777 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001778 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001779 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 +01001780 range_expected = 0;
1781 } else {
1782 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001783 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 +01001784 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001785 part->min_64 = part->max_64;
1786 }
1787 } else {
1788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1789 length_restr ? "length" : "range", expr);
1790 goto cleanup;
1791 }
1792 }
1793
1794 /* check with the previous range/length restriction */
1795 if (base_range) {
1796 switch (basetype) {
1797 case LY_TYPE_BINARY:
1798 case LY_TYPE_UINT8:
1799 case LY_TYPE_UINT16:
1800 case LY_TYPE_UINT32:
1801 case LY_TYPE_UINT64:
1802 case LY_TYPE_STRING:
1803 uns = 1;
1804 break;
1805 case LY_TYPE_DEC64:
1806 case LY_TYPE_INT8:
1807 case LY_TYPE_INT16:
1808 case LY_TYPE_INT32:
1809 case LY_TYPE_INT64:
1810 uns = 0;
1811 break;
1812 default:
1813 LOGINT(ctx->ctx);
1814 ret = LY_EINT;
1815 goto cleanup;
1816 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001817 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001818 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1819 goto baseerror;
1820 }
1821 /* current lower bound is not lower than the base */
1822 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1823 /* base has single value */
1824 if (base_range->parts[v].min_64 == parts[u].min_64) {
1825 /* both lower bounds are the same */
1826 if (parts[u].min_64 != parts[u].max_64) {
1827 /* current continues with a range */
1828 goto baseerror;
1829 } else {
1830 /* equal single values, move both forward */
1831 ++v;
1832 continue;
1833 }
1834 } else {
1835 /* base is single value lower than current range, so the
1836 * value from base range is removed in the current,
1837 * move only base and repeat checking */
1838 ++v;
1839 --u;
1840 continue;
1841 }
1842 } else {
1843 /* base is the range */
1844 if (parts[u].min_64 == parts[u].max_64) {
1845 /* current is a single value */
1846 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1847 /* current is behind the base range, so base range is omitted,
1848 * move the base and keep the current for further check */
1849 ++v;
1850 --u;
1851 } /* else it is within the base range, so move the current, but keep the base */
1852 continue;
1853 } else {
1854 /* both are ranges - check the higher bound, the lower was already checked */
1855 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1856 /* higher bound is higher than the current higher bound */
1857 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1858 /* but the current lower bound is also higher, so the base range is omitted,
1859 * continue with the same current, but move the base */
1860 --u;
1861 ++v;
1862 continue;
1863 }
1864 /* current range starts within the base range but end behind it */
1865 goto baseerror;
1866 } else {
1867 /* current range is smaller than the base,
1868 * move current, but stay with the base */
1869 continue;
1870 }
1871 }
1872 }
1873 }
1874 if (u != parts_done) {
1875baseerror:
1876 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1877 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1878 length_restr ? "length" : "range", range_p->arg);
1879 goto cleanup;
1880 }
1881 }
1882
1883 if (!(*range)) {
1884 *range = calloc(1, sizeof **range);
1885 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1886 }
1887
Radek Krejcic8b31002019-01-08 10:24:45 +01001888 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001889 if (range_p->eapptag) {
1890 lydict_remove(ctx->ctx, (*range)->eapptag);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001891 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001892 }
1893 if (range_p->emsg) {
1894 lydict_remove(ctx->ctx, (*range)->emsg);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001895 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001896 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001897 if (range_p->dsc) {
1898 lydict_remove(ctx->ctx, (*range)->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001899 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001900 }
1901 if (range_p->ref) {
1902 lydict_remove(ctx->ctx, (*range)->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001903 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001904 }
Radek Krejci19a96102018-11-15 13:38:09 +01001905 /* extensions are taken only from the last range by the caller */
1906
1907 (*range)->parts = parts;
1908 parts = NULL;
1909 ret = LY_SUCCESS;
1910cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001911 LY_ARRAY_FREE(parts);
1912
1913 return ret;
1914}
1915
1916/**
1917 * @brief Checks pattern syntax.
1918 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001919 * @param[in] ctx Context.
1920 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001921 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001922 * @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 +01001923 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001924 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925LY_ERR
1926lys_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 +01001927{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001928 size_t idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001929 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001930 int err_code;
1931 const char *orig_ptr;
1932 PCRE2_SIZE err_offset;
1933 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001934#define URANGE_LEN 19
1935 char *ublock2urange[][2] = {
1936 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1937 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1938 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1939 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1940 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1941 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1942 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1943 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1944 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1945 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1946 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1947 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1948 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1949 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1950 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1951 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1952 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1953 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1954 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1955 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1956 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1957 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1958 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1959 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1960 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1961 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1962 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1963 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1964 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1965 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1966 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1967 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1968 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1969 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1970 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1971 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1972 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1973 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1974 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1975 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1976 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1977 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1978 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1979 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1980 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1981 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1982 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1983 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1984 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1985 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1986 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1987 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1988 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1989 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1990 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1991 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1992 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1993 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1994 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1995 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1996 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1997 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1998 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1999 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
2000 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
2001 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
2002 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
2003 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
2004 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
2005 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
2006 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
2007 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
2008 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
2009 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
2010 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
2011 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
2012 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
2013 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
2014 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
2015 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
2016 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
2017 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
2018 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
2019 {NULL, NULL}
2020 };
2021
2022 /* adjust the expression to a Perl equivalent
2023 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
2024
Michal Vasko40a00082020-05-27 15:20:01 +02002025 /* allocate space for the transformed pattern */
2026 size = strlen(pattern) + 1;
2027 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002029 perl_regex[0] = '\0';
2030
Michal Vasko40a00082020-05-27 15:20:01 +02002031 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
2032 brack = 0;
2033 idx = 0;
2034 orig_ptr = pattern;
2035 while (orig_ptr[0]) {
2036 switch (orig_ptr[0]) {
2037 case '$':
2038 case '^':
2039 if (!brack) {
2040 /* make space for the extra character */
2041 ++size;
2042 perl_regex = ly_realloc(perl_regex, size);
2043 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002044
Michal Vasko40a00082020-05-27 15:20:01 +02002045 /* print escape slash */
2046 perl_regex[idx] = '\\';
2047 ++idx;
2048 }
2049 break;
2050 case '[':
2051 /* must not be escaped */
2052 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2053 ++brack;
2054 }
2055 break;
2056 case ']':
2057 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2058 /* pattern was checked and compiled already */
2059 assert(brack);
2060 --brack;
2061 }
2062 break;
2063 default:
2064 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002065 }
Michal Vasko40a00082020-05-27 15:20:01 +02002066
2067 /* copy char */
2068 perl_regex[idx] = orig_ptr[0];
2069
2070 ++idx;
2071 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002072 }
Michal Vasko40a00082020-05-27 15:20:01 +02002073 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002074
2075 /* substitute Unicode Character Blocks with exact Character Ranges */
2076 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2077 start = ptr - perl_regex;
2078
2079 ptr = strchr(ptr, '}');
2080 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002082 pattern, perl_regex + start + 2, "unterminated character property");
2083 free(perl_regex);
2084 return LY_EVALID;
2085 }
2086 end = (ptr - perl_regex) + 1;
2087
2088 /* need more space */
2089 if (end - start < URANGE_LEN) {
2090 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002092 }
2093
2094 /* find our range */
2095 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2096 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2097 break;
2098 }
2099 }
2100 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002102 pattern, perl_regex + start + 5, "unknown block name");
2103 free(perl_regex);
2104 return LY_EVALID;
2105 }
2106
2107 /* 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 +02002108 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002109 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002110 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002111 }
2112 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002113 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002114 }
2115 }
Michal Vasko40a00082020-05-27 15:20:01 +02002116 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002117 /* skip brackets */
2118 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2119 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2120 } else {
2121 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2122 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2123 }
2124 }
2125
2126 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002127 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2128 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002129 &err_code, &err_offset, NULL);
2130 if (!code_local) {
2131 PCRE2_UCHAR err_msg[256] = {0};
2132 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002134 free(perl_regex);
2135 return LY_EVALID;
2136 }
2137 free(perl_regex);
2138
Radek Krejci54579462019-04-30 12:47:06 +02002139 if (code) {
2140 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002141 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002142 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002143 }
2144
2145 return LY_SUCCESS;
2146
2147#undef URANGE_LEN
2148}
2149
Radek Krejcia3045382018-11-22 14:30:31 +01002150/**
2151 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2152 * @param[in] ctx Compile context.
2153 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002154 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2155 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2156 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2157 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2158 */
Radek Krejci19a96102018-11-15 13:38:09 +01002159static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002160lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci0f969882020-08-21 16:56:47 +02002161 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
Radek Krejci19a96102018-11-15 13:38:09 +01002162{
2163 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002164 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002165 LY_ERR ret = LY_SUCCESS;
2166
2167 /* first, copy the patterns from the base type */
2168 if (base_patterns) {
2169 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2170 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2171 }
2172
2173 LY_ARRAY_FOR(patterns_p, u) {
2174 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2175 *pattern = calloc(1, sizeof **pattern);
2176 ++(*pattern)->refcount;
2177
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002179 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002180
2181 if (patterns_p[u].arg[0] == 0x15) {
2182 (*pattern)->inverted = 1;
2183 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002184 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done);
2185 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
2186 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
2187 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
2188 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002189 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002190 }
2191done:
2192 return ret;
2193}
2194
Radek Krejcia3045382018-11-22 14:30:31 +01002195/**
2196 * @brief map of the possible restrictions combination for the specific built-in type.
2197 */
Radek Krejci19a96102018-11-15 13:38:09 +01002198static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2199 0 /* LY_TYPE_UNKNOWN */,
2200 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002201 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2202 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2203 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2204 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2205 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002206 LYS_SET_BIT /* LY_TYPE_BITS */,
2207 0 /* LY_TYPE_BOOL */,
2208 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2209 0 /* LY_TYPE_EMPTY */,
2210 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2211 LYS_SET_BASE /* LY_TYPE_IDENT */,
2212 LYS_SET_REQINST /* LY_TYPE_INST */,
2213 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002214 LYS_SET_TYPE /* LY_TYPE_UNION */,
2215 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002216 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002217 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002218 LYS_SET_RANGE /* LY_TYPE_INT64 */
2219};
2220
2221/**
2222 * @brief stringification of the YANG built-in data types
2223 */
Michal Vasko22df3f02020-08-24 13:29:22 +02002224const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
Radek Krejci5969f272018-11-23 10:03:58 +01002225 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
Radek Krejci0f969882020-08-21 16:56:47 +02002226 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"};
Radek Krejci19a96102018-11-15 13:38:09 +01002227
Radek Krejcia3045382018-11-22 14:30:31 +01002228/**
2229 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2230 * @param[in] ctx Compile context.
2231 * @param[in] enums_p Array of the parsed enum structures to compile.
2232 * @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 +01002233 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2234 * @param[out] enums Newly created array of the compiled enums information for the current type.
2235 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2236 */
Radek Krejci19a96102018-11-15 13:38:09 +01002237static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002238lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci0f969882020-08-21 16:56:47 +02002239 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002240{
2241 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002242 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002243 int32_t value = 0;
2244 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002245 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002246
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002247 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002248 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2249 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2250 return LY_EVALID;
2251 }
2252
2253 LY_ARRAY_FOR(enums_p, u) {
2254 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002255 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
2256 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done);
2257 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Radek Krejci693262f2019-04-29 15:23:20 +02002258 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002259 if (base_enums) {
2260 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2261 LY_ARRAY_FOR(base_enums, v) {
2262 if (!strcmp(e->name, base_enums[v].name)) {
2263 break;
2264 }
2265 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002266 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2268 "Invalid %s - derived type adds new item \"%s\".",
2269 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2270 return LY_EVALID;
2271 }
2272 match = v;
2273 }
2274
2275 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002276 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002277 if (enums_p[u].flags & LYS_SET_VALUE) {
2278 e->value = (int32_t)enums_p[u].value;
2279 if (!u || e->value >= value) {
2280 value = e->value + 1;
2281 }
2282 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002283 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002284 if (e->value == (*enums)[v].value) {
2285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2286 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2287 e->value, e->name, (*enums)[v].name);
2288 return LY_EVALID;
2289 }
2290 }
2291 } else if (base_enums) {
2292 /* inherit the assigned value */
2293 e->value = base_enums[match].value;
2294 if (!u || e->value >= value) {
2295 value = e->value + 1;
2296 }
2297 } else {
2298 /* assign value automatically */
2299 if (u && value == INT32_MIN) {
2300 /* counter overflow */
2301 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2302 "Invalid enumeration - it is not possible to auto-assign enum value for "
2303 "\"%s\" since the highest value is already 2147483647.", e->name);
2304 return LY_EVALID;
2305 }
2306 e->value = value++;
2307 }
2308 } else { /* LY_TYPE_BITS */
2309 if (enums_p[u].flags & LYS_SET_VALUE) {
2310 e->value = (int32_t)enums_p[u].value;
2311 if (!u || (uint32_t)e->value >= position) {
2312 position = (uint32_t)e->value + 1;
2313 }
2314 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002315 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002316 if (e->value == (*enums)[v].value) {
2317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2318 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek Krejci0f969882020-08-21 16:56:47 +02002319 (uint32_t)e->value, e->name, (*enums)[v].name);
Radek Krejci19a96102018-11-15 13:38:09 +01002320 return LY_EVALID;
2321 }
2322 }
2323 } else if (base_enums) {
2324 /* inherit the assigned value */
2325 e->value = base_enums[match].value;
2326 if (!u || (uint32_t)e->value >= position) {
2327 position = (uint32_t)e->value + 1;
2328 }
2329 } else {
2330 /* assign value automatically */
2331 if (u && position == 0) {
2332 /* counter overflow */
2333 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2334 "Invalid bits - it is not possible to auto-assign bit position for "
2335 "\"%s\" since the highest value is already 4294967295.", e->name);
2336 return LY_EVALID;
2337 }
2338 e->value = position++;
2339 }
2340 }
2341
2342 if (base_enums) {
2343 /* the assigned values must not change from the derived type */
2344 if (e->value != base_enums[match].value) {
2345 if (basetype == LY_TYPE_ENUM) {
2346 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2347 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2348 e->name, base_enums[match].value, e->value);
2349 } else {
2350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2351 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2352 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2353 }
2354 return LY_EVALID;
2355 }
2356 }
2357
Radek Krejciec4da802019-05-02 13:02:41 +02002358 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002359 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 +01002360
2361 if (basetype == LY_TYPE_BITS) {
2362 /* keep bits ordered by position */
Radek Krejci1e008d22020-08-17 11:37:37 +02002363 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
Radek Krejci19a96102018-11-15 13:38:09 +01002364 if (v != u) {
2365 memcpy(&storage, e, sizeof *e);
2366 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2367 memcpy(&(*enums)[v], &storage, sizeof storage);
2368 }
2369 }
2370 }
2371
2372done:
2373 return ret;
2374}
2375
Radek Krejcia3045382018-11-22 14:30:31 +01002376/**
2377 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2378 *
2379 * path-arg = absolute-path / relative-path
2380 * absolute-path = 1*("/" (node-identifier *path-predicate))
2381 * relative-path = 1*(".." "/") descendant-path
2382 *
2383 * @param[in,out] path Path to parse.
2384 * @param[out] prefix Prefix of the token, NULL if there is not any.
2385 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2386 * @param[out] name Name of the token.
2387 * @param[out] nam_len Length of the name.
2388 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2389 * must not be changed between consecutive calls. -1 if the
2390 * path is absolute.
2391 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2392 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2393 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002394LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002395lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
Radek Krejci857189e2020-09-01 13:26:36 +02002396 int32_t *parent_times, ly_bool *has_predicate)
Radek Krejcia3045382018-11-22 14:30:31 +01002397{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002398 int32_t par_times = 0;
Radek Krejcia3045382018-11-22 14:30:31 +01002399
2400 assert(path && *path);
2401 assert(parent_times);
2402 assert(prefix);
2403 assert(prefix_len);
2404 assert(name);
2405 assert(name_len);
2406 assert(has_predicate);
2407
2408 *prefix = NULL;
2409 *prefix_len = 0;
2410 *name = NULL;
2411 *name_len = 0;
2412 *has_predicate = 0;
2413
2414 if (!*parent_times) {
2415 if (!strncmp(*path, "..", 2)) {
2416 *path += 2;
2417 ++par_times;
2418 while (!strncmp(*path, "/..", 3)) {
2419 *path += 3;
2420 ++par_times;
2421 }
2422 }
2423 if (par_times) {
2424 *parent_times = par_times;
2425 } else {
2426 *parent_times = -1;
2427 }
2428 }
2429
2430 if (**path != '/') {
2431 return LY_EINVAL;
2432 }
2433 /* skip '/' */
2434 ++(*path);
2435
2436 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002437 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002438
2439 if ((**path == '/' && (*path)[1]) || !**path) {
2440 /* path continues by another token or this is the last token */
2441 return LY_SUCCESS;
2442 } else if ((*path)[0] != '[') {
2443 /* unexpected character */
2444 return LY_EINVAL;
2445 } else {
2446 /* predicate starting with [ */
2447 *has_predicate = 1;
2448 return LY_SUCCESS;
2449 }
2450}
2451
2452/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002453 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2454 *
2455 * The set of features used for target must be a subset of features used for the leafref.
2456 * This is not a perfect, we should compare the truth tables but it could require too much resources
2457 * and RFC 7950 does not require it explicitely, so we simplify that.
2458 *
2459 * @param[in] refnode The leafref node.
2460 * @param[in] target Tha target node of the leafref.
2461 * @return LY_SUCCESS or LY_EVALID;
2462 */
2463static LY_ERR
2464lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2465{
2466 LY_ERR ret = LY_EVALID;
2467 const struct lysc_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002468 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci58d171e2018-11-23 13:50:55 +01002469 struct ly_set features = {0};
2470
2471 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002472 if (iter->iffeatures) {
2473 LY_ARRAY_FOR(iter->iffeatures, u) {
2474 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002475 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002476 }
2477 }
2478 }
2479 }
2480
2481 /* we should have, in features set, a superset of features applicable to the target node.
Radek Krejciba03a5a2020-08-27 14:40:41 +02002482 * If the feature is not present, we don;t have a subset of features applicable
Radek Krejci58d171e2018-11-23 13:50:55 +01002483 * to the leafref itself. */
Radek Krejci58d171e2018-11-23 13:50:55 +01002484 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002485 if (iter->iffeatures) {
2486 LY_ARRAY_FOR(iter->iffeatures, u) {
2487 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002488 if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
2489 /* feature not present */
Radek Krejci58d171e2018-11-23 13:50:55 +01002490 goto cleanup;
2491 }
2492 }
2493 }
2494 }
2495 }
2496 ret = LY_SUCCESS;
2497
2498cleanup:
2499 ly_set_erase(&features, NULL);
2500 return ret;
2501}
2502
Michal Vasko004d3152020-06-11 19:59:22 +02002503static 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 +02002504 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2505 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod);
Radek Krejcia3045382018-11-22 14:30:31 +01002506
Radek Krejcia3045382018-11-22 14:30:31 +01002507/**
2508 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2509 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002510 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2511 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2512 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2513 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002514 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002515 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002516 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002517 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2518 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2519 * @param[out] type Newly created type structure with the filled information about the type.
2520 * @return LY_ERR value.
2521 */
Radek Krejci19a96102018-11-15 13:38:09 +01002522static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002523lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002524 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2525 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2526 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002527{
2528 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002529 struct lysc_type_bin *bin;
2530 struct lysc_type_num *num;
2531 struct lysc_type_str *str;
2532 struct lysc_type_bits *bits;
2533 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002534 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002535 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002536 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002537 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002538
2539 switch (basetype) {
2540 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +02002541 bin = (struct lysc_type_bin *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002542
2543 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002544 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002545 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002546 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002547 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002548 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 +01002549 }
2550 }
2551
2552 if (tpdfname) {
2553 type_p->compiled = *type;
2554 *type = calloc(1, sizeof(struct lysc_type_bin));
2555 }
2556 break;
2557 case LY_TYPE_BITS:
2558 /* RFC 7950 9.7 - bits */
Michal Vasko22df3f02020-08-24 13:29:22 +02002559 bits = (struct lysc_type_bits *)(*type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002560 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002561 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002562 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
2563 (struct lysc_type_bitenum_item **)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002564 }
2565
Radek Krejci555cb5b2018-11-16 14:54:33 +01002566 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002567 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002568 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002569 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002570 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002572 free(*type);
2573 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002574 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002575 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002576 }
2577
2578 if (tpdfname) {
2579 type_p->compiled = *type;
2580 *type = calloc(1, sizeof(struct lysc_type_bits));
2581 }
2582 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002583 case LY_TYPE_DEC64:
Radek Krejci115a74d2020-08-14 22:18:12 +02002584 dec = (struct lysc_type_dec *)(*type);
Radek Krejci6cba4292018-11-15 17:33:29 +01002585
2586 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002587 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002588 if (!type_p->fraction_digits) {
2589 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002591 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002592 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002593 free(*type);
2594 *type = NULL;
2595 }
2596 return LY_EVALID;
2597 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002598 dec->fraction_digits = type_p->fraction_digits;
2599 } else {
2600 if (type_p->fraction_digits) {
2601 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
2602 if (tpdfname) {
2603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2604 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
2605 tpdfname);
2606 } else {
2607 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2608 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
2609 free(*type);
2610 *type = NULL;
2611 }
2612 return LY_EVALID;
Radek Krejci6cba4292018-11-15 17:33:29 +01002613 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002614 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
Radek Krejci6cba4292018-11-15 17:33:29 +01002615 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002616
2617 /* RFC 7950 9.2.4 - range */
2618 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002619 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
Radek Krejci115a74d2020-08-14 22:18:12 +02002620 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002621 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002622 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 +01002623 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002624 }
2625
2626 if (tpdfname) {
2627 type_p->compiled = *type;
2628 *type = calloc(1, sizeof(struct lysc_type_dec));
2629 }
2630 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002631 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002632 str = (struct lysc_type_str *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002633
2634 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002635 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002636 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002637 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002638 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002639 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 +01002640 }
Michal Vasko22df3f02020-08-24 13:29:22 +02002641 } else if (base && ((struct lysc_type_str *)base)->length) {
2642 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002643 }
2644
2645 /* RFC 7950 9.4.5 - pattern */
2646 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002647 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Michal Vasko22df3f02020-08-24 13:29:22 +02002648 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
2649 } else if (base && ((struct lysc_type_str *)base)->patterns) {
2650 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002651 }
2652
2653 if (tpdfname) {
2654 type_p->compiled = *type;
2655 *type = calloc(1, sizeof(struct lysc_type_str));
2656 }
2657 break;
2658 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +02002659 enumeration = (struct lysc_type_enum *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002660
2661 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002662 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002663 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002664 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002665 }
2666
Radek Krejci555cb5b2018-11-16 14:54:33 +01002667 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002668 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002669 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002671 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002673 free(*type);
2674 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002675 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002676 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002677 }
2678
2679 if (tpdfname) {
2680 type_p->compiled = *type;
2681 *type = calloc(1, sizeof(struct lysc_type_enum));
2682 }
2683 break;
2684 case LY_TYPE_INT8:
2685 case LY_TYPE_UINT8:
2686 case LY_TYPE_INT16:
2687 case LY_TYPE_UINT16:
2688 case LY_TYPE_INT32:
2689 case LY_TYPE_UINT32:
2690 case LY_TYPE_INT64:
2691 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +02002692 num = (struct lysc_type_num *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002693
2694 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002695 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002696 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002697 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002698 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002699 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 +01002700 }
2701 }
2702
2703 if (tpdfname) {
2704 type_p->compiled = *type;
2705 *type = calloc(1, sizeof(struct lysc_type_num));
2706 }
2707 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002708 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002709 idref = (struct lysc_type_identityref *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002710
2711 /* RFC 7950 9.10.2 - base */
2712 if (type_p->bases) {
2713 if (base) {
2714 /* only the directly derived identityrefs can contain base specification */
2715 if (tpdfname) {
2716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002717 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002718 tpdfname);
2719 } else {
2720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002721 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002722 free(*type);
2723 *type = NULL;
2724 }
2725 return LY_EVALID;
2726 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002727 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002728 }
2729
2730 if (!base && !type_p->flags) {
2731 /* type derived from identityref built-in type must contain at least one base */
2732 if (tpdfname) {
2733 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2734 } else {
2735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2736 free(*type);
2737 *type = NULL;
2738 }
2739 return LY_EVALID;
2740 }
2741
2742 if (tpdfname) {
2743 type_p->compiled = *type;
2744 *type = calloc(1, sizeof(struct lysc_type_identityref));
2745 }
2746 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002747 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002748 lref = (struct lysc_type_leafref *)*type;
Michal Vasko004d3152020-06-11 19:59:22 +02002749
Radek Krejcia3045382018-11-22 14:30:31 +01002750 /* RFC 7950 9.9.3 - require-instance */
2751 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002752 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002753 if (tpdfname) {
2754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2755 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2756 } else {
2757 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2758 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2759 free(*type);
2760 *type = NULL;
2761 }
2762 return LY_EVALID;
2763 }
Michal Vasko004d3152020-06-11 19:59:22 +02002764 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002765 } else if (base) {
2766 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002767 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002768 } else {
2769 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002770 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002771 }
2772 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002773 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2774 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002775 } else if (base) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002776 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path);
2777 lref->path_context = ((struct lysc_type_leafref *)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002778 } else if (tpdfname) {
2779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2780 return LY_EVALID;
2781 } else {
2782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2783 free(*type);
2784 *type = NULL;
2785 return LY_EVALID;
2786 }
2787 if (tpdfname) {
2788 type_p->compiled = *type;
2789 *type = calloc(1, sizeof(struct lysc_type_leafref));
2790 }
2791 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002792 case LY_TYPE_INST:
2793 /* RFC 7950 9.9.3 - require-instance */
2794 if (type_p->flags & LYS_SET_REQINST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002795 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
Radek Krejci16c0f822018-11-16 10:46:10 +01002796 } else {
2797 /* default is true */
Michal Vasko22df3f02020-08-24 13:29:22 +02002798 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
Radek Krejci16c0f822018-11-16 10:46:10 +01002799 }
2800
2801 if (tpdfname) {
2802 type_p->compiled = *type;
2803 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2804 }
2805 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002806 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +02002807 un = (struct lysc_type_union *)(*type);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002808
2809 /* RFC 7950 7.4 - type */
2810 if (type_p->types) {
2811 if (base) {
2812 /* only the directly derived union can contain types specification */
2813 if (tpdfname) {
2814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2815 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2816 tpdfname);
2817 } else {
2818 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2819 "Invalid type substatement for the type not directly derived from union built-in type.");
2820 free(*type);
2821 *type = NULL;
2822 }
2823 return LY_EVALID;
2824 }
2825 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002826 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2827 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002828 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002829 &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002830 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2831 /* add space for additional types from the union subtype */
2832 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vasko22df3f02020-08-24 13:29:22 +02002833 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
2834 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002835
2836 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002837 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002838 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2839 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2840 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
Michal Vasko22df3f02020-08-24 13:29:22 +02002841 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 +02002842 lref = (struct lysc_type_leafref *)un->types[u + additional];
2843
2844 lref->basetype = LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02002845 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path);
Michal Vasko004d3152020-06-11 19:59:22 +02002846 lref->refcount = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +02002847 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
2848 lref->path_context = ((struct lysc_type_leafref *)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002849 /* TODO extensions */
2850
2851 } else {
2852 un->types[u + additional] = un_aux->types[v];
2853 ++un_aux->types[v]->refcount;
2854 }
2855 ++additional;
2856 LY_ARRAY_INCREMENT(un->types);
2857 }
2858 /* compensate u increment in main loop */
2859 --additional;
2860
2861 /* free the replaced union subtype */
Michal Vasko22df3f02020-08-24 13:29:22 +02002862 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002863 } else {
2864 LY_ARRAY_INCREMENT(un->types);
2865 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002866 }
2867 }
2868
2869 if (!base && !type_p->flags) {
2870 /* type derived from union built-in type must contain at least one type */
2871 if (tpdfname) {
2872 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2873 } else {
2874 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2875 free(*type);
2876 *type = NULL;
2877 }
2878 return LY_EVALID;
2879 }
2880
2881 if (tpdfname) {
2882 type_p->compiled = *type;
2883 *type = calloc(1, sizeof(struct lysc_type_union));
2884 }
2885 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002886 case LY_TYPE_BOOL:
2887 case LY_TYPE_EMPTY:
2888 case LY_TYPE_UNKNOWN: /* just to complete switch */
2889 break;
2890 }
2891 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2892done:
2893 return ret;
2894}
2895
Radek Krejcia3045382018-11-22 14:30:31 +01002896/**
2897 * @brief Compile information about the leaf/leaf-list's type.
2898 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002899 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2900 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2901 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2902 * @param[in] context_name Name of the context node or referencing typedef for logging.
2903 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002904 * @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 +01002905 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002906 * @param[out] dflt Default value for the type.
2907 * @param[out] dflt_mod Local module for the default value.
Radek Krejcia3045382018-11-22 14:30:31 +01002908 * @return LY_ERR value.
2909 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002910static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002911lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002912 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2913 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod)
Radek Krejci19a96102018-11-15 13:38:09 +01002914{
2915 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02002916 ly_bool dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002917 struct type_context {
2918 const struct lysp_tpdf *tpdf;
2919 struct lysp_node *node;
2920 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002921 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002922 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002923 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002924 struct ly_set tpdf_chain = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002925
2926 assert((dflt && dflt_mod) || (!dflt && !dflt_mod));
Radek Krejci19a96102018-11-15 13:38:09 +01002927
2928 (*type) = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002929 if (dflt) {
2930 *dflt = NULL;
2931 *dflt_mod = NULL;
2932 }
Radek Krejci19a96102018-11-15 13:38:09 +01002933
2934 tctx = calloc(1, sizeof *tctx);
2935 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002936 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002937 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2938 ret == LY_SUCCESS;
2939 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2940 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2941 if (basetype) {
2942 break;
2943 }
2944
2945 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002946 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2947 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci87e25252020-09-15 13:28:31 +02002948 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01002949
Radek Krejcicdfecd92018-11-26 11:27:32 +01002950 if (units && !*units) {
2951 /* inherit units */
Radek Krejci87e25252020-09-15 13:28:31 +02002952 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2953 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002954 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002955 if (dflt && !*dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002956 /* inherit default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002957 *dflt = tctx->tpdf->dflt;
2958 *dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002959 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002960 if (dummyloops && (!units || *units) && dflt && *dflt) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002961 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002962 break;
2963 }
2964
Radek Krejci19a96102018-11-15 13:38:09 +01002965 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002966 /* it is not necessary to continue, the rest of the chain was already compiled,
2967 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002968 basetype = tctx->tpdf->type.compiled->basetype;
Radek Krejciba03a5a2020-08-27 14:40:41 +02002969 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02002970 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejciba03a5a2020-08-27 14:40:41 +02002971
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002972 if ((units && !*units) || (dflt && !*dflt)) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002973 dummyloops = 1;
2974 goto preparenext;
2975 } else {
2976 tctx = NULL;
2977 break;
2978 }
Radek Krejci19a96102018-11-15 13:38:09 +01002979 }
2980
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002981 /* circular typedef reference detection */
Radek Krejci1deb5be2020-08-26 16:43:36 +02002982 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002983 /* local part */
Michal Vasko22df3f02020-08-24 13:29:22 +02002984 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002985 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2987 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2988 free(tctx);
2989 ret = LY_EVALID;
2990 goto cleanup;
2991 }
2992 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02002993 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002994 /* global part for unions corner case */
Michal Vasko22df3f02020-08-24 13:29:22 +02002995 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002996 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2998 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2999 free(tctx);
3000 ret = LY_EVALID;
3001 goto cleanup;
3002 }
3003 }
3004
Radek Krejci19a96102018-11-15 13:38:09 +01003005 /* store information for the following processing */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003006 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02003007 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003008
Radek Krejcicdfecd92018-11-26 11:27:32 +01003009preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003010 /* prepare next loop */
3011 tctx_prev = tctx;
3012 tctx = calloc(1, sizeof *tctx);
3013 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3014 }
3015 free(tctx);
3016
3017 /* allocate type according to the basetype */
3018 switch (basetype) {
3019 case LY_TYPE_BINARY:
3020 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003021 break;
3022 case LY_TYPE_BITS:
3023 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003024 break;
3025 case LY_TYPE_BOOL:
3026 case LY_TYPE_EMPTY:
3027 *type = calloc(1, sizeof(struct lysc_type));
3028 break;
3029 case LY_TYPE_DEC64:
3030 *type = calloc(1, sizeof(struct lysc_type_dec));
3031 break;
3032 case LY_TYPE_ENUM:
3033 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003034 break;
3035 case LY_TYPE_IDENT:
3036 *type = calloc(1, sizeof(struct lysc_type_identityref));
3037 break;
3038 case LY_TYPE_INST:
3039 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3040 break;
3041 case LY_TYPE_LEAFREF:
3042 *type = calloc(1, sizeof(struct lysc_type_leafref));
3043 break;
3044 case LY_TYPE_STRING:
3045 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003046 break;
3047 case LY_TYPE_UNION:
3048 *type = calloc(1, sizeof(struct lysc_type_union));
3049 break;
3050 case LY_TYPE_INT8:
3051 case LY_TYPE_UINT8:
3052 case LY_TYPE_INT16:
3053 case LY_TYPE_UINT16:
3054 case LY_TYPE_INT32:
3055 case LY_TYPE_UINT32:
3056 case LY_TYPE_INT64:
3057 case LY_TYPE_UINT64:
3058 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003059 break;
3060 case LY_TYPE_UNKNOWN:
3061 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3062 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3063 ret = LY_EVALID;
3064 goto cleanup;
3065 }
3066 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003067 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003068 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3069 ly_data_type2str[basetype]);
3070 free(*type);
3071 (*type) = NULL;
3072 ret = LY_EVALID;
3073 goto cleanup;
3074 }
3075
3076 /* get restrictions from the referred typedefs */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003077 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003078 tctx = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003079
3080 /* remember the typedef context for circular check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003081 ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
3082 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003083
Radek Krejci43699232018-11-23 14:59:46 +01003084 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003085 base = tctx->tpdf->type.compiled;
3086 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003087 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003088 /* no change, just use the type information from the base */
Michal Vasko22df3f02020-08-24 13:29:22 +02003089 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 +01003090 ++base->refcount;
3091 continue;
3092 }
3093
3094 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003095 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3096 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3097 tctx->tpdf->name, ly_data_type2str[basetype]);
3098 ret = LY_EVALID;
3099 goto cleanup;
3100 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3102 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3103 tctx->tpdf->name, tctx->tpdf->dflt);
3104 ret = LY_EVALID;
3105 goto cleanup;
3106 }
3107
Radek Krejci19a96102018-11-15 13:38:09 +01003108 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003109 /* TODO user type plugins */
3110 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003111 prev_type = *type;
Michal Vasko22df3f02020-08-24 13:29:22 +02003112 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 +01003113 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003114 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003115 LY_CHECK_GOTO(ret, cleanup);
3116 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003117 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003118 /* remove the processed typedef contexts from the stack for circular check */
3119 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003120
Radek Krejcic5c27e52018-11-15 14:38:11 +01003121 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003122 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003123 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003124 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003125 /* TODO user type plugins */
3126 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003127 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003128 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 +01003129 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003130 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003131 /* no specific restriction in leaf's type definition, copy from the base */
3132 free(*type);
3133 (*type) = base;
3134 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003135 }
3136
Radek Krejci0935f412019-08-20 16:15:18 +02003137 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003138
3139cleanup:
3140 ly_set_erase(&tpdf_chain, free);
3141 return ret;
3142}
3143
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003144/**
3145 * @brief Compile status information of the given node.
3146 *
3147 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3148 * has the status correctly set during the compilation.
3149 *
3150 * @param[in] ctx Compile context
3151 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3152 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3153 * the compatibility with the parent's status value.
3154 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3155 * @return LY_ERR value.
3156 */
3157static LY_ERR
3158lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3159{
3160 /* status - it is not inherited by specification, but it does not make sense to have
3161 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3162 if (!((*node_flags) & LYS_STATUS_MASK)) {
3163 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3164 if ((parent_flags & 0x3) != 0x3) {
3165 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3166 * combination of bits (0x3) which marks the uses_status value */
3167 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
Radek Krejci0f969882020-08-21 16:56:47 +02003168 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003169 }
3170 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3171 } else {
3172 (*node_flags) |= LYS_STATUS_CURR;
3173 }
3174 } else if (parent_flags & LYS_STATUS_MASK) {
3175 /* check status compatibility with the parent */
3176 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3177 if ((*node_flags) & LYS_STATUS_CURR) {
3178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3179 "A \"current\" status is in conflict with the parent's \"%s\" status.",
Radek Krejci0f969882020-08-21 16:56:47 +02003180 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003181 } else { /* LYS_STATUS_DEPRC */
3182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3183 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3184 }
3185 return LY_EVALID;
3186 }
3187 }
3188 return LY_SUCCESS;
3189}
3190
Radek Krejci8cce8532019-03-05 11:27:45 +01003191/**
3192 * @brief Check uniqness of the node/action/notification name.
3193 *
3194 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3195 * structures, but they share the namespace so we need to check their name collisions.
3196 *
3197 * @param[in] ctx Compile context.
Michal Vasko20424b42020-08-31 12:29:38 +02003198 * @param[in] parent Parent of the nodes to check, can be NULL.
Radek Krejci8cce8532019-03-05 11:27:45 +01003199 * @param[in] name Name of the item to find in the given lists.
Michal Vasko20424b42020-08-31 12:29:38 +02003200 * @param[in] exclude Node that was just added that should be excluded from the name checking.
Radek Krejci8cce8532019-03-05 11:27:45 +01003201 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3202 */
3203static LY_ERR
Michal Vasko20424b42020-08-31 12:29:38 +02003204lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
3205 const struct lysc_node *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003206{
Michal Vasko20424b42020-08-31 12:29:38 +02003207 const struct lysc_node *iter, *iter2;
3208 const struct lysc_action *actions;
3209 const struct lysc_notif *notifs;
3210 uint32_t getnext_flags;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003211 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003212
Michal Vasko20424b42020-08-31 12:29:38 +02003213#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
3214
3215 if (exclude->nodetype == LYS_CASE) {
3216 /* check restricted only to all the cases */
3217 assert(parent->nodetype == LYS_CHOICE);
3218 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
3219 if (CHECK_NODE(iter, exclude, name)) {
3220 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
3221 return LY_EEXIST;
3222 }
3223 }
3224
3225 return LY_SUCCESS;
3226 }
3227
3228 /* no reason for our parent to be choice anymore */
3229 assert(!parent || (parent->nodetype != LYS_CHOICE));
3230
3231 if (parent && (parent->nodetype == LYS_CASE)) {
3232 /* move to the first data definition parent */
3233 parent = lysc_data_parent(parent);
3234 }
3235
3236 getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE;
3237 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
3238 getnext_flags |= LYS_GETNEXT_OUTPUT;
3239 }
3240
3241 iter = NULL;
3242 while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) {
3243 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003244 goto error;
3245 }
Michal Vasko20424b42020-08-31 12:29:38 +02003246
3247 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
3248 if (iter->nodetype == LYS_CHOICE) {
3249 iter2 = NULL;
3250 while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) {
3251 if (CHECK_NODE(iter2, exclude, name)) {
3252 goto error;
3253 }
3254 }
3255 }
Radek Krejci8cce8532019-03-05 11:27:45 +01003256 }
Michal Vasko20424b42020-08-31 12:29:38 +02003257
3258 actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003259 LY_ARRAY_FOR(actions, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003260 if (CHECK_NODE(&actions[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003261 goto error;
3262 }
3263 }
Michal Vasko20424b42020-08-31 12:29:38 +02003264
3265 notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003266 LY_ARRAY_FOR(notifs, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003267 if (CHECK_NODE(&notifs[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003268 goto error;
3269 }
3270 }
3271 return LY_SUCCESS;
Michal Vasko20424b42020-08-31 12:29:38 +02003272
Radek Krejci8cce8532019-03-05 11:27:45 +01003273error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003275 return LY_EEXIST;
Michal Vasko20424b42020-08-31 12:29:38 +02003276
3277#undef CHECK_NODE
Radek Krejci8cce8532019-03-05 11:27:45 +01003278}
3279
Radek Krejciec4da802019-05-02 13:02:41 +02003280static 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 +01003281
Radek Krejcia3045382018-11-22 14:30:31 +01003282/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003283 * @brief Compile parsed RPC/action schema node information.
3284 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003285 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003286 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003287 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3288 * @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).
3289 * Zero means no uses, non-zero value with no status bit set mean the default status.
3290 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3291 */
3292static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003293lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003294 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003295{
3296 LY_ERR ret = LY_SUCCESS;
3297 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003298 LY_ARRAY_COUNT_TYPE u;
3299 uint32_t opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003300
Radek Krejci327de162019-06-14 12:52:07 +02003301 lysc_update_path(ctx, parent, action_p->name);
3302
Michal Vasko20424b42020-08-31 12:29:38 +02003303 /* member needed for uniqueness check lys_getnext() */
3304 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
3305 action->module = ctx->mod;
3306 action->parent = parent;
3307
3308 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action));
Radek Krejci8cce8532019-03-05 11:27:45 +01003309
Radek Krejciec4da802019-05-02 13:02:41 +02003310 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003311 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003312 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003313 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003314 return LY_EVALID;
3315 }
3316
Radek Krejciec4da802019-05-02 13:02:41 +02003317 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003318 action->sp = action_p;
3319 }
3320 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3321
3322 /* status - it is not inherited by specification, but it does not make sense to have
3323 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003324 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003325
Radek Krejci011e4aa2020-09-04 15:22:31 +02003326 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
3327 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
3328 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003329 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003330 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003331
3332 /* input */
Michal Vasko22df3f02020-08-24 13:29:22 +02003333 lysc_update_path(ctx, (struct lysc_node *)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003334 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003335 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 +02003336 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003337 LY_LIST_FOR(action_p->input.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003338 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003339 }
Radek Krejci327de162019-06-14 12:52:07 +02003340 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003341 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003342
3343 /* output */
Michal Vasko22df3f02020-08-24 13:29:22 +02003344 lysc_update_path(ctx, (struct lysc_node *)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003345 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003346 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 +02003347 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003348 LY_LIST_FOR(action_p->output.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003349 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003350 }
Radek Krejci327de162019-06-14 12:52:07 +02003351 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003352 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003353
3354 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3355 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003356 ret = ly_set_add(&ctx->xpath, action, 0, NULL);
3357 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003358 }
3359
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003360cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003361 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003362 return ret;
3363}
3364
3365/**
Radek Krejci43981a32019-04-12 09:44:11 +02003366 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003367 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003368 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003369 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3370 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3371 * @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 +02003372 * Zero means no uses, non-zero value with no status bit set mean the default status.
3373 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3374 */
3375static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003376lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003377 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
Radek Krejcifc11bd72019-04-11 16:00:05 +02003378{
3379 LY_ERR ret = LY_SUCCESS;
3380 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003381 LY_ARRAY_COUNT_TYPE u;
3382 uint32_t opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003383
Radek Krejci327de162019-06-14 12:52:07 +02003384 lysc_update_path(ctx, parent, notif_p->name);
3385
Michal Vasko20424b42020-08-31 12:29:38 +02003386 /* member needed for uniqueness check lys_getnext() */
3387 notif->nodetype = LYS_NOTIF;
3388 notif->module = ctx->mod;
3389 notif->parent = parent;
3390
3391 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003392
Radek Krejciec4da802019-05-02 13:02:41 +02003393 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003394 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3395 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003396 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003397 return LY_EVALID;
3398 }
3399
Radek Krejciec4da802019-05-02 13:02:41 +02003400 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003401 notif->sp = notif_p;
3402 }
3403 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3404
3405 /* status - it is not inherited by specification, but it does not make sense to have
3406 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003407 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
3408 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003409
Radek Krejci011e4aa2020-09-04 15:22:31 +02003410 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
3411 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
3412 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003413 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003414 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003415 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3416 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003417 ret = ly_set_add(&ctx->xpath, notif, 0, NULL);
3418 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003419 }
Radek Krejci0935f412019-08-20 16:15:18 +02003420 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003421
Radek Krejciec4da802019-05-02 13:02:41 +02003422 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003423 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003424 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status);
3425 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003426 }
3427
Radek Krejci327de162019-06-14 12:52:07 +02003428 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003429cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003430 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003431 return ret;
3432}
3433
3434/**
Radek Krejcia3045382018-11-22 14:30:31 +01003435 * @brief Compile parsed container node information.
3436 * @param[in] ctx Compile context
3437 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003438 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3439 * is enriched with the container-specific information.
3440 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3441 */
Radek Krejci19a96102018-11-15 13:38:09 +01003442static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003443lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003444{
Michal Vasko22df3f02020-08-24 13:29:22 +02003445 struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p;
3446 struct lysc_node_container *cont = (struct lysc_node_container *)node;
Radek Krejci19a96102018-11-15 13:38:09 +01003447 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003448 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003449 LY_ERR ret = LY_SUCCESS;
3450
Radek Krejcife909632019-02-12 15:34:42 +01003451 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003452 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003453 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003454 } else if (cont_p->musts) {
3455 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003456 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3457 cont->flags |= LYS_PRESENCE;
3458 } else if (cont_p->when) {
3459 /* container with a when condition */
3460 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 +02003461 cont->flags |= LYS_PRESENCE;
3462 } else if (cont_p->parent) {
3463 if (cont_p->parent->nodetype == LYS_CHOICE) {
3464 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003465 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 +02003466 cont_p->name, cont_p->parent->name);
3467 cont->flags |= LYS_PRESENCE;
3468 } else if ((cont_p->parent->nodetype == LYS_CASE)
3469 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3470 /* 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 +02003471 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
Michal Vaskoba417ac2020-08-06 14:48:20 +02003472 cont_p->name, cont_p->parent->name);
3473 cont->flags |= LYS_PRESENCE;
3474 }
Radek Krejcife909632019-02-12 15:34:42 +01003475 }
3476
Michal Vaskoba417ac2020-08-06 14:48:20 +02003477 /* more cases when the container has meaning but is kept NP for convenience:
3478 * - when condition
3479 * - direct child action/notification
3480 */
3481
Radek Krejci19a96102018-11-15 13:38:09 +01003482 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003483 ret = lys_compile_node(ctx, child_p, node, 0);
3484 LY_CHECK_GOTO(ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003485 }
3486
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003487 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003488 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3489 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003490 ret = ly_set_add(&ctx->xpath, cont, 0, NULL);
3491 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003492 }
Radek Krejciec4da802019-05-02 13:02:41 +02003493 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3494 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003495
3496done:
3497 return ret;
3498}
3499
Radek Krejci33f72892019-02-21 10:36:58 +01003500/*
3501 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3502 * @param[in] ctx Compile context.
3503 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3504 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003505 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3506 * @return LY_ERR value.
3507 */
3508static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003509lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003510 struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003511{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003512 const char *dflt;
3513 struct lys_module *dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003514
Radek Krejciec4da802019-05-02 13:02:41 +02003515 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 +02003516 leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod));
3517
3518 /* store default value, if any */
3519 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
3520 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, dflt, dflt_mod));
Radek Krejci33f72892019-02-21 10:36:58 +01003521 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003522
Radek Krejci33f72892019-02-21 10:36:58 +01003523 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3524 /* 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 +02003525 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003526 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003527 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003528 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
3529 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
Radek Krejci33f72892019-02-21 10:36:58 +01003530 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003531 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003532 }
3533 }
3534 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003535 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3536 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3537 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3538 return LY_EVALID;
3539 }
3540 }
3541
Radek Krejci33f72892019-02-21 10:36:58 +01003542 return LY_SUCCESS;
3543}
3544
Radek Krejcia3045382018-11-22 14:30:31 +01003545/**
3546 * @brief Compile parsed leaf node information.
3547 * @param[in] ctx Compile context
3548 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003549 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3550 * is enriched with the leaf-specific information.
3551 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3552 */
Radek Krejci19a96102018-11-15 13:38:09 +01003553static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003554lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003555{
Michal Vasko22df3f02020-08-24 13:29:22 +02003556 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p;
3557 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003558 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003559 LY_ERR ret = LY_SUCCESS;
3560
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003561 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003562 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3563 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003564 ret = ly_set_add(&ctx->xpath, leaf, 0, NULL);
3565 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003566 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003567 if (leaf_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003568 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003569 leaf->flags |= LYS_SET_UNITS;
3570 }
Radek Krejcia1911222019-07-22 17:24:50 +02003571
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003572 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003573 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3574 LY_CHECK_GOTO(ret, done);
Radek Krejcia1911222019-07-22 17:24:50 +02003575
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003576 /* store/update default value */
Radek Krejciccd20f12019-02-15 14:12:27 +01003577 if (leaf_p->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003578 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, leaf_p->dflt, ctx->mod_def));
Radek Krejci76b3e962018-12-14 17:01:25 +01003579 leaf->flags |= LYS_SET_DFLT;
3580 }
Radek Krejci43699232018-11-23 14:59:46 +01003581
Radek Krejci19a96102018-11-15 13:38:09 +01003582done:
3583 return ret;
3584}
3585
Radek Krejcia3045382018-11-22 14:30:31 +01003586/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003587 * @brief Compile parsed leaf-list node information.
3588 * @param[in] ctx Compile context
3589 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003590 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3591 * is enriched with the leaf-list-specific information.
3592 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3593 */
3594static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003595lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003596{
Michal Vasko22df3f02020-08-24 13:29:22 +02003597 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p;
3598 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003599 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003600 LY_ERR ret = LY_SUCCESS;
3601
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003602 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003603 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3604 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003605 ret = ly_set_add(&ctx->xpath, llist, 0, NULL);
3606 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003607 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003608 if (llist_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003609 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003610 llist->flags |= LYS_SET_UNITS;
3611 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003612
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003613 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003614 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist);
3615 LY_CHECK_GOTO(ret, done);
Michal Vasko6a044b22020-01-15 12:25:39 +01003616
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003617 /* store/update default values */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003618 if (llist_p->dflts) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003619 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, llist_p->dflts, ctx->mod_def), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003620 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003621 }
3622
3623 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003624 if (llist->min) {
3625 llist->flags |= LYS_MAND_TRUE;
3626 }
Radek Krejcib7408632018-11-28 17:12:11 +01003627 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003628
Radek Krejci0e5d8382018-11-28 16:37:53 +01003629done:
3630 return ret;
3631}
3632
3633/**
Radek Krejci7af64242019-02-18 13:07:53 +01003634 * @brief Compile information about list's uniques.
3635 * @param[in] ctx Compile context.
3636 * @param[in] context_module Module where the prefixes are going to be resolved.
3637 * @param[in] uniques Sized array list of unique statements.
3638 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3639 * @return LY_ERR value.
3640 */
3641static LY_ERR
3642lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3643{
3644 LY_ERR ret = LY_SUCCESS;
3645 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003646 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003647 const char *keystr, *delim;
3648 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003649 LY_ARRAY_COUNT_TYPE v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003650 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003651 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003652
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003653 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003654 config = -1;
3655 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3656 keystr = uniques[v];
3657 while (keystr) {
3658 delim = strpbrk(keystr, " \t\n");
3659 if (delim) {
3660 len = delim - keystr;
3661 while (isspace(*delim)) {
3662 ++delim;
3663 }
3664 } else {
3665 len = strlen(keystr);
3666 }
3667
3668 /* unique node must be present */
3669 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko22df3f02020-08-24 13:29:22 +02003670 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0,
3671 (const struct lysc_node **)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003672 if (ret != LY_SUCCESS) {
3673 if (ret == LY_EDENIED) {
3674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003675 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003676 len, keystr, lys_nodetype2str((*key)->nodetype));
3677 }
3678 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003679 } else if (flags) {
3680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3681 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003682 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003683 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003684 }
3685
3686 /* all referenced leafs must be of the same config type */
3687 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003689 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003690 return LY_EVALID;
3691 } else if ((*key)->flags & LYS_CONFIG_W) {
3692 config = 1;
3693 } else { /* LYS_CONFIG_R */
3694 config = 0;
3695 }
3696
Michal Vasko14654712020-02-06 08:35:21 +01003697 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3698 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3699 if (parent->nodetype == LYS_LIST) {
3700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3701 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3702 return LY_EVALID;
3703 }
3704 }
3705
Radek Krejci7af64242019-02-18 13:07:53 +01003706 /* check status */
3707 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0f969882020-08-21 16:56:47 +02003708 (*key)->flags, (*key)->module, (*key)->name));
Radek Krejci7af64242019-02-18 13:07:53 +01003709
3710 /* mark leaf as unique */
3711 (*key)->flags |= LYS_UNIQUE;
3712
3713 /* next unique value in line */
3714 keystr = delim;
3715 }
3716 /* next unique definition */
3717 }
3718
3719 return LY_SUCCESS;
3720}
3721
3722/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003723 * @brief Compile parsed list node information.
3724 * @param[in] ctx Compile context
3725 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003726 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3727 * is enriched with the list-specific information.
3728 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3729 */
3730static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003731lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003732{
Michal Vasko22df3f02020-08-24 13:29:22 +02003733 struct lysp_node_list *list_p = (struct lysp_node_list *)node_p;
3734 struct lysc_node_list *list = (struct lysc_node_list *)node;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003735 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003736 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003737 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003738 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003739 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003740 LY_ERR ret = LY_SUCCESS;
3741
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003742 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003743 if (list->min) {
3744 list->flags |= LYS_MAND_TRUE;
3745 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003746 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3747
3748 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003749 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003750 }
3751
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003752 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003753 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3754 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003755 LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL));
Michal Vasko5d8756a2019-11-07 15:21:00 +01003756 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003757
3758 /* keys */
3759 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3761 return LY_EVALID;
3762 }
3763
3764 /* find all the keys (must be direct children) */
3765 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003766 if (!keystr) {
3767 /* keyless list */
3768 list->flags |= LYS_KEYLESS;
3769 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003770 while (keystr) {
3771 delim = strpbrk(keystr, " \t\n");
3772 if (delim) {
3773 len = delim - keystr;
3774 while (isspace(*delim)) {
3775 ++delim;
3776 }
3777 } else {
3778 len = strlen(keystr);
3779 }
3780
3781 /* key node must be present */
Michal Vasko22df3f02020-08-24 13:29:22 +02003782 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 +02003783 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003784 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3785 "The list's key \"%.*s\" not found.", len, keystr);
3786 return LY_EVALID;
3787 }
3788 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003789 if (key->flags & LYS_KEY) {
3790 /* the node was already marked as a key */
3791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3792 "Duplicated key identifier \"%.*s\".", len, keystr);
3793 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003794 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003795
Michal Vasko22df3f02020-08-24 13:29:22 +02003796 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003797 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003798 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3800 return LY_EVALID;
3801 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003802 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003803 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003804 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003806 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003807 return LY_EVALID;
3808 }
3809 } else {
3810 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003811 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003812 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003813 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003814 return LY_EVALID;
3815 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003816 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003817 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003818 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003819 return LY_EVALID;
3820 }
3821 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003822
3823 /* check status */
3824 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003825 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003826
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003827 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003828 if (key->dflt) {
3829 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3830 lysc_type_free(ctx->ctx, key->dflt->realtype);
3831 free(key->dflt);
3832 key->dflt = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003833 }
3834 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003835 key->flags |= LYS_KEY;
3836
3837 /* move it to the correct position */
Michal Vasko22df3f02020-08-24 13:29:22 +02003838 if ((prev_key && (struct lysc_node *)prev_key != key->prev) || (!prev_key && key->prev->next)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02003839 /* fix links in closest previous siblings of the key */
3840 if (key->next) {
3841 key->next->prev = key->prev;
3842 } else {
3843 /* last child */
3844 list->child->prev = key->prev;
3845 }
3846 if (key->prev->next) {
3847 key->prev->next = key->next;
3848 }
3849 /* fix links in the key */
3850 if (prev_key) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003851 key->prev = (struct lysc_node *)prev_key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003852 key->next = prev_key->next;
3853 } else {
3854 key->prev = list->child->prev;
3855 key->next = list->child;
3856 }
3857 /* fix links in closes future siblings of the key */
3858 if (prev_key) {
3859 if (prev_key->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003860 prev_key->next->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003861 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003862 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003863 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003864 prev_key->next = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003865 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003866 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003867 }
3868 /* fix links in parent */
3869 if (!key->prev->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003870 list->child = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003871 }
3872 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003873
3874 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003875 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003876 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003877 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003878 }
3879
3880 /* uniques */
3881 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003882 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003883 }
3884
Radek Krejciec4da802019-05-02 13:02:41 +02003885 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3886 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003887
3888done:
3889 return ret;
3890}
3891
Radek Krejcib56c7502019-02-13 14:19:54 +01003892/**
3893 * @brief Do some checks and set the default choice's case.
3894 *
3895 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3896 *
3897 * @param[in] ctx Compile context.
3898 * @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,
3899 * not the reference to the imported module.
3900 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3901 * @return LY_ERR value.
3902 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003903static LY_ERR
3904lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3905{
Michal Vasko22df3f02020-08-24 13:29:22 +02003906 struct lysc_node *iter, *node = (struct lysc_node *)ch;
Radek Krejci76b3e962018-12-14 17:01:25 +01003907 const char *prefix = NULL, *name;
3908 size_t prefix_len = 0;
3909
3910 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3911 name = strchr(dflt, ':');
3912 if (name) {
3913 prefix = dflt;
3914 prefix_len = name - prefix;
3915 ++name;
3916 } else {
3917 name = dflt;
3918 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003919 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003920 /* prefixed default case make sense only for the prefix of the schema itself */
3921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3922 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3923 prefix_len, prefix);
3924 return LY_EVALID;
3925 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003926 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 +01003927 if (!ch->dflt) {
3928 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3929 "Default case \"%s\" not found.", dflt);
3930 return LY_EVALID;
3931 }
3932 /* no mandatory nodes directly under the default case */
3933 LY_LIST_FOR(ch->dflt->child, iter) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003934 if (iter->parent != (struct lysc_node *)ch->dflt) {
Radek Krejcife13da42019-02-15 14:51:01 +01003935 break;
3936 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003937 if (iter->flags & LYS_MAND_TRUE) {
3938 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3939 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3940 return LY_EVALID;
3941 }
3942 }
Radek Krejci01342af2019-01-03 15:18:08 +01003943 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003944 return LY_SUCCESS;
3945}
3946
Radek Krejciccd20f12019-02-15 14:12:27 +01003947static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003948lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003949{
3950 struct lys_module *mod;
3951 const char *prefix = NULL, *name;
3952 size_t prefix_len = 0;
3953 struct lysc_node_case *cs;
3954 struct lysc_node *node;
3955
3956 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3957 name = strchr(dflt, ':');
3958 if (name) {
3959 prefix = dflt;
3960 prefix_len = name - prefix;
3961 ++name;
3962 } else {
3963 name = dflt;
3964 }
3965 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3966 if (prefix) {
3967 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3968 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003969 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3970 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003971 return LY_EVALID;
3972 }
3973 } else {
3974 mod = ctx->mod;
3975 }
3976 /* get the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003977 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 +01003978 if (!cs) {
3979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003980 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003981 return LY_EVALID;
3982 }
3983
3984 /* check that there is no mandatory node */
3985 LY_LIST_FOR(cs->child, node) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003986 if (node->parent != (struct lysc_node *)cs) {
Radek Krejcife13da42019-02-15 14:51:01 +01003987 break;
3988 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003989 if (node->flags & LYS_MAND_TRUE) {
3990 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003991 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3992 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003993 return LY_EVALID;
3994 }
3995 }
3996
3997 /* set the default case in choice */
3998 ch->dflt = cs;
3999 cs->flags |= LYS_SET_DFLT;
4000
4001 return LY_SUCCESS;
4002}
4003
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004004/**
Michal Vasko20424b42020-08-31 12:29:38 +02004005 * @brief Compile choice children.
4006 *
4007 * @param[in] ctx Compile context
4008 * @param[in] child_p Parsed choice children nodes.
4009 * @param[in] node Compiled choice node to compile and add children to.
4010 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4011 */
4012static LY_ERR
4013lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node)
4014{
4015 LY_ERR ret = LY_SUCCESS;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004016 struct lysp_node *child_p_next = child_p->next;
Michal Vasko20424b42020-08-31 12:29:38 +02004017 struct lysp_node_case *cs_p;
4018
4019 if (child_p->nodetype == LYS_CASE) {
4020 /* standard case under choice */
4021 ret = lys_compile_node(ctx, child_p, node, 0);
4022 } else {
4023 /* we need the implicit case first, so create a fake parsed case */
4024 cs_p = calloc(1, sizeof *cs_p);
4025 cs_p->nodetype = LYS_CASE;
Radek Krejci87e25252020-09-15 13:28:31 +02004026 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
Michal Vasko20424b42020-08-31 12:29:38 +02004027 cs_p->child = child_p;
4028
4029 /* make the child the only case child */
Michal Vasko20424b42020-08-31 12:29:38 +02004030 child_p->next = NULL;
4031
4032 /* compile it normally */
4033 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0);
4034
Radek Krejci87e25252020-09-15 13:28:31 +02004035free_fake_node:
Michal Vasko20424b42020-08-31 12:29:38 +02004036 /* free the fake parsed node and correct pointers back */
4037 cs_p->child = NULL;
4038 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
Radek Krejci8f5fad22020-09-15 16:50:54 +02004039 child_p->next = child_p_next;
Michal Vasko20424b42020-08-31 12:29:38 +02004040 }
4041
4042 return ret;
4043}
4044
4045/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004046 * @brief Compile parsed choice node information.
Michal Vasko20424b42020-08-31 12:29:38 +02004047 *
Radek Krejci056d0a82018-12-06 16:57:25 +01004048 * @param[in] ctx Compile context
4049 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004050 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004051 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004052 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4053 */
4054static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004055lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004056{
Michal Vasko22df3f02020-08-24 13:29:22 +02004057 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p;
4058 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
Michal Vasko20424b42020-08-31 12:29:38 +02004059 struct lysp_node *child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004060 LY_ERR ret = LY_SUCCESS;
4061
Michal Vasko20424b42020-08-31 12:29:38 +02004062 assert(node->nodetype == LYS_CHOICE);
4063
Radek Krejci056d0a82018-12-06 16:57:25 +01004064 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko20424b42020-08-31 12:29:38 +02004065 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node));
Radek Krejci056d0a82018-12-06 16:57:25 +01004066 }
4067
4068 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004069 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004070 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004071 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004072
Radek Krejci9800fb82018-12-13 14:26:23 +01004073 return ret;
4074}
4075
4076/**
4077 * @brief Compile parsed anydata or anyxml node information.
4078 * @param[in] ctx Compile context
4079 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004080 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4081 * is enriched with the any-specific information.
4082 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4083 */
4084static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004085lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004086{
Michal Vasko22df3f02020-08-24 13:29:22 +02004087 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p;
4088 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004089 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9800fb82018-12-13 14:26:23 +01004090 LY_ERR ret = LY_SUCCESS;
4091
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004092 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004093 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4094 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004095 ret = ly_set_add(&ctx->xpath, any, 0, NULL);
4096 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004097 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004098
4099 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004100 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4101 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004102 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004103done:
4104 return ret;
4105}
4106
Radek Krejcib56c7502019-02-13 14:19:54 +01004107/**
Michal Vasko795b3752020-07-13 15:24:27 +02004108 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4109 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004110 *
4111 * @param[in] ctx Compile context
4112 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4113 * the choice itself is expected instead of a specific case node.
4114 * @param[in] node Schema node to connect into the list.
4115 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004116 * 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 +01004117 */
4118static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004119lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004120{
Michal Vasko795b3752020-07-13 15:24:27 +02004121 struct lysc_node **children, *start, *end;
4122 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004123
Michal Vasko20424b42020-08-31 12:29:38 +02004124 node->parent = parent;
4125
4126 if (parent) {
4127 if (parent->nodetype == LYS_CHOICE) {
4128 assert(node->nodetype == LYS_CASE);
4129 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
4130 } else {
4131 children = lysc_node_children_p(parent, ctx->options);
4132 }
4133 assert(children);
4134
Radek Krejci056d0a82018-12-06 16:57:25 +01004135 if (!(*children)) {
4136 /* first child */
4137 *children = node;
4138 } else if (*children != node) {
4139 /* by the condition in previous branch we cover the choice/case children
4140 * - the children list is shared by the choice and the the first case, in addition
4141 * the first child of each case must be referenced from the case node. So the node is
4142 * actually always already inserted in case it is the first children - so here such
4143 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004144 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4145 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4146 /* some augments are already connected and we are connecting new ones,
4147 * keep module name order and insert the node into the children list */
4148 end = (*children);
4149 do {
4150 end = end->prev;
4151 mod = end->module;
4152 while (end->prev->module == mod) {
4153 end = end->prev;
4154 }
Radek Krejcif6a11002020-08-21 13:29:07 +02004155 } 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 +02004156
4157 /* we have the last existing node after our node, easily get the first before and connect it */
4158 start = end->prev;
4159 start->next = node;
4160 node->next = end;
4161 end->prev = node;
4162 node->prev = start;
4163 } else {
4164 /* insert at the end of the parent's children list */
4165 (*children)->prev->next = node;
4166 node->prev = (*children)->prev;
4167 (*children)->prev = node;
4168 }
Michal Vasko20424b42020-08-31 12:29:38 +02004169 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004170
Michal Vasko20424b42020-08-31 12:29:38 +02004171 /* check the name uniqueness (even for an only child, it may be in case) */
4172 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
4173 return LY_EEXIST;
4174 }
4175 } else {
4176 /* top-level element */
4177 if (!ctx->mod->compiled->data) {
4178 ctx->mod->compiled->data = node;
4179 } else {
4180 /* insert at the end of the module's top-level nodes list */
4181 ctx->mod->compiled->data->prev->next = node;
4182 node->prev = ctx->mod->compiled->data->prev;
4183 ctx->mod->compiled->data->prev = node;
4184 }
4185
4186 /* check the name uniqueness on top-level */
4187 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
4188 return LY_EEXIST;
Radek Krejci056d0a82018-12-06 16:57:25 +01004189 }
4190 }
Michal Vasko20424b42020-08-31 12:29:38 +02004191
Radek Krejci056d0a82018-12-06 16:57:25 +01004192 return LY_SUCCESS;
4193}
4194
Radek Krejci95710c92019-02-11 15:49:55 +01004195/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004196 * @brief Prepare the case structure in choice node for the new data node.
4197 *
4198 * 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
4199 * created in the choice when the first child was processed.
4200 *
4201 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004202 * @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 +02004203 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004204 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4205 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4206 * @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,
4207 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004208 */
Michal Vasko20424b42020-08-31 12:29:38 +02004209static LY_ERR
4210lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004211{
Michal Vasko20424b42020-08-31 12:29:38 +02004212 struct lysp_node *child_p;
4213 struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004214
Radek Krejci39424802020-08-12 09:31:00 +02004215 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004216 /* we have to add an implicit case node into the parent choice */
Radek Krejci95710c92019-02-11 15:49:55 +01004217 } else if (node_p->nodetype == LYS_CASE) {
Michal Vasko20424b42020-08-31 12:29:38 +02004218 /* explicit parent case */
4219 LY_LIST_FOR(cs_p->child, child_p) {
4220 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004221 }
Radek Krejci95710c92019-02-11 15:49:55 +01004222 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004223 LOGINT_RET(ctx->ctx);
Radek Krejci056d0a82018-12-06 16:57:25 +01004224 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004225
Michal Vasko20424b42020-08-31 12:29:38 +02004226 return LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01004227}
4228
Radek Krejcib56c7502019-02-13 14:19:54 +01004229/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004230 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004231 *
4232 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004233 * @param[in] node Target node where the config is supposed to be changed.
4234 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004235 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4236 * 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 +01004237 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4238 * @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 +01004239 * @return LY_ERR value.
4240 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004241static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004242lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci857189e2020-09-01 13:26:36 +02004243 ly_bool inheriting, ly_bool refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004244{
4245 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004246 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004247
4248 if (config == (node->flags & LYS_CONFIG_MASK)) {
4249 /* nothing to do */
4250 return LY_SUCCESS;
4251 }
4252
4253 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004254 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004255 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004257 "Invalid %s of config - configuration node cannot be child of any state data node.",
4258 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004259 return LY_EVALID;
4260 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004261 node->flags |= LYS_SET_CONFIG;
4262 } else {
4263 if (node->flags & LYS_SET_CONFIG) {
4264 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4265 /* setting config flags, but have node with explicit config true */
4266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004267 "Invalid %s of config - configuration node cannot be child of any state data node.",
4268 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004269 return LY_EVALID;
4270 }
4271 /* do not change config on nodes where the config is explicitely set, this does not apply to
4272 * nodes, which are being changed explicitly (targets of refine or deviation) */
4273 return LY_SUCCESS;
4274 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004275 }
4276 node->flags &= ~LYS_CONFIG_MASK;
4277 node->flags |= config;
4278
4279 /* inherit the change into the children */
Michal Vasko22df3f02020-08-24 13:29:22 +02004280 LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004281 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004282 }
4283
Radek Krejci76b3e962018-12-14 17:01:25 +01004284 return LY_SUCCESS;
4285}
4286
Radek Krejcib56c7502019-02-13 14:19:54 +01004287/**
4288 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4289 *
4290 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4291 * the flag to such parents from a mandatory children.
4292 *
4293 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4294 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4295 * (mandatory children was removed).
4296 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02004297static void
Radek Krejci857189e2020-09-01 13:26:36 +02004298lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
Radek Krejcife909632019-02-12 15:34:42 +01004299{
4300 struct lysc_node *iter;
4301
4302 if (add) { /* set flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004303 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
Radek Krejcife909632019-02-12 15:34:42 +01004304 parent = parent->parent) {
4305 parent->flags |= LYS_MAND_TRUE;
4306 }
4307 } else { /* unset flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004308 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004309 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004310 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004311 /* there is another mandatory node */
4312 return;
4313 }
4314 }
4315 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4316 parent->flags &= ~LYS_MAND_TRUE;
4317 }
4318 }
4319}
4320
Radek Krejci056d0a82018-12-06 16:57:25 +01004321/**
Radek Krejci3641f562019-02-13 15:38:40 +01004322 * @brief Internal sorting process for the lys_compile_augment_sort().
4323 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4324 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4325 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4326 */
4327static void
4328lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4329{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004330 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004331 size_t len;
4332
4333 len = strlen(aug_p->nodeid);
4334 LY_ARRAY_FOR(result, v) {
4335 if (strlen(result[v]->nodeid) <= len) {
4336 continue;
4337 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004338 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004339 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004340 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004341 break;
4342 }
4343 }
4344 result[v] = aug_p;
4345 LY_ARRAY_INCREMENT(result);
4346}
4347
4348/**
4349 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4350 *
4351 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4352 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4353 *
4354 * @param[in] ctx Compile context.
4355 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4356 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4357 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4358 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4359 * @return LY_ERR value.
4360 */
4361LY_ERR
4362lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4363{
4364 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004365 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004366
4367 assert(augments);
4368
4369 /* get count of the augments in module and all its submodules */
4370 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004371 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004372 }
4373 LY_ARRAY_FOR(inc_p, u) {
4374 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004375 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004376 }
4377 }
4378
4379 if (!count) {
4380 *augments = NULL;
4381 return LY_SUCCESS;
4382 }
4383 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4384
4385 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4386 * together, so there can be even /z/y betwwen them. */
4387 LY_ARRAY_FOR(aug_p, u) {
4388 lys_compile_augment_sort_(&aug_p[u], result);
4389 }
4390 LY_ARRAY_FOR(inc_p, u) {
4391 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4392 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4393 }
4394 }
4395
4396 *augments = result;
4397 return LY_SUCCESS;
4398}
4399
4400/**
4401 * @brief Compile the parsed augment connecting it into its target.
4402 *
4403 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4404 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4405 * are already implemented and compiled.
4406 *
4407 * @param[in] ctx Compile context.
4408 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004409 * @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
4410 * children in case of the augmenting uses data.
4411 * @return LY_SUCCESS on success.
4412 * @return LY_EVALID on failure.
4413 */
4414LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004415lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004416{
Michal Vaskoe6143202020-07-03 13:02:08 +02004417 LY_ERR ret = LY_SUCCESS, rc;
Michal Vasko20424b42020-08-31 12:29:38 +02004418 struct lysp_node *node_p;
Radek Krejci3641f562019-02-13 15:38:40 +01004419 struct lysc_node *target; /* target target of the augment */
4420 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004421 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004422 struct lys_module **aug_mod;
Radek Krejci857189e2020-09-01 13:26:36 +02004423 ly_bool allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004424 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004425 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004426 uint32_t opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004427
Radek Krejci327de162019-06-14 12:52:07 +02004428 lysc_update_path(ctx, NULL, "{augment}");
4429 lysc_update_path(ctx, NULL, aug_p->nodeid);
4430
Michal Vaskocb18c7f2020-08-24 09:22:28 +02004431 ret = lysc_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004432 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Michal Vasko22df3f02020-08-24 13:29:22 +02004433 1, (const struct lysc_node **)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004434 if (ret != LY_SUCCESS) {
4435 if (ret == LY_EDENIED) {
4436 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4437 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4438 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4439 }
4440 return LY_EVALID;
4441 }
4442
4443 /* check for mandatory nodes
4444 * - new cases augmenting some choice can have mandatory nodes
4445 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4446 */
Radek Krejci733988a2019-02-15 15:12:44 +01004447 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004448 allow_mandatory = 1;
4449 }
4450
4451 when_shared = NULL;
4452 LY_LIST_FOR(aug_p->child, node_p) {
4453 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4454 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004455 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004456 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4457 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004458 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004459 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4460 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004461 return LY_EVALID;
4462 }
4463
4464 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004465 ctx->options |= flags;
Michal Vasko20424b42020-08-31 12:29:38 +02004466 if (target->nodetype == LYS_CHOICE) {
4467 LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target));
Radek Krejci3641f562019-02-13 15:38:40 +01004468 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004469 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004470 }
Radek Krejciec4da802019-05-02 13:02:41 +02004471 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004472
Radek Krejcife13da42019-02-15 14:51:01 +01004473 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4474 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004475 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004476 /* 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 +02004477 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 +01004478 } else if (target->nodetype == LYS_CHOICE) {
4479 /* 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 +02004480 node = ((struct lysc_node_choice *)target)->cases->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004481 } else {
4482 /* the compiled node is the last child of the target */
Michal Vasko22df3f02020-08-24 13:29:22 +02004483 node = (struct lysc_node *)lysc_node_children(target, flags);
Radek Krejci7c7783d2020-04-08 15:34:39 +02004484 if (!node) {
4485 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4486 break;
4487 }
4488 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004489 }
4490
Radek Krejci733988a2019-02-15 15:12:44 +01004491 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004492 node->flags &= ~LYS_MAND_TRUE;
4493 lys_compile_mandatory_parents(target, 0);
4494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004495 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004496 return LY_EVALID;
4497 }
4498
4499 /* pass augment's when to all the children */
4500 if (aug_p->when) {
4501 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4502 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004503 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004504 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004505
4506 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4507 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004508 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4509 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004510 }
4511
Radek Krejci3641f562019-02-13 15:38:40 +01004512 when_shared = *when;
4513 } else {
4514 ++when_shared->refcount;
4515 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004516
4517 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4518 /* in this case check "when" again for all children because of dummy node check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004519 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4520 LY_CHECK_GOTO(ret, error);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004521 }
Radek Krejci3641f562019-02-13 15:38:40 +01004522 }
4523 }
4524 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004525
Radek Krejciec4da802019-05-02 13:02:41 +02004526 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004527 switch (target->nodetype) {
4528 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004529 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004530 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004531 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004532 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004533 break;
4534 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004535 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004536 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004537 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004538 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004539 break;
4540 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004541 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004542 if (aug_p->actions) {
4543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004544 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4545 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004546 return LY_EVALID;
4547 }
4548 if (aug_p->notifs) {
4549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004550 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004551 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004552 return LY_EVALID;
4553 }
4554 }
Radek Krejci3641f562019-02-13 15:38:40 +01004555
Michal Vaskoe6143202020-07-03 13:02:08 +02004556 /* add this module into the target module augmented_by, if not there already */
4557 rc = LY_SUCCESS;
4558 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4559 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4560 rc = LY_EEXIST;
4561 break;
4562 }
4563 }
4564 if (!rc) {
4565 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4566 *aug_mod = ctx->mod;
4567 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004568
Radek Krejci327de162019-06-14 12:52:07 +02004569 lysc_update_path(ctx, NULL, NULL);
4570 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004571
Radek Krejci3641f562019-02-13 15:38:40 +01004572error:
Radek Krejciec4da802019-05-02 13:02:41 +02004573 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004574 return ret;
4575}
4576
4577/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004578 * @brief Apply refined or deviated mandatory flag to the target node.
4579 *
4580 * @param[in] ctx Compile context.
4581 * @param[in] node Target node where the mandatory property is supposed to be changed.
4582 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004583 * @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 +01004584 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4585 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4586 * 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 +01004587 * @return LY_ERR value.
4588 */
4589static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004590lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, ly_bool refine_flag)
Radek Krejcif1421c22019-02-19 13:05:20 +01004591{
4592 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004594 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4595 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004596 return LY_EVALID;
4597 }
4598
4599 if (mandatory_flag & LYS_MAND_TRUE) {
4600 /* check if node has default value */
4601 if (node->nodetype & LYS_LEAF) {
4602 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004603 if (refine_flag) {
4604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004605 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004606 return LY_EVALID;
4607 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004608 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004609 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004610 if (refine_flag) {
4611 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004612 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004613 return LY_EVALID;
4614 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004615 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004616 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004617 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 +01004618 return LY_EVALID;
4619 }
4620
4621 node->flags &= ~LYS_MAND_FALSE;
4622 node->flags |= LYS_MAND_TRUE;
4623 lys_compile_mandatory_parents(node->parent, 1);
4624 } else {
4625 /* make mandatory false */
4626 node->flags &= ~LYS_MAND_TRUE;
4627 node->flags |= LYS_MAND_FALSE;
4628 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcif1421c22019-02-19 13:05:20 +01004629 }
4630 return LY_SUCCESS;
4631}
4632
4633/**
Michal Vasko601ddb32020-08-31 16:25:35 +02004634 * @brief Find grouping for a uses.
Radek Krejcie86bf772018-12-14 11:39:53 +01004635 *
Michal Vasko601ddb32020-08-31 16:25:35 +02004636 * @param[in] ctx Compile context.
4637 * @param[in] uses_p Parsed uses node.
4638 * @param[out] gpr_p Found grouping on success.
4639 * @param[out] grp_mod Module of @p grp_p on success.
4640 * @return LY_ERR value.
Radek Krejcie86bf772018-12-14 11:39:53 +01004641 */
4642static LY_ERR
Michal Vasko601ddb32020-08-31 16:25:35 +02004643lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
4644 struct lys_module **grp_mod)
Radek Krejcie86bf772018-12-14 11:39:53 +01004645{
4646 struct lysp_node *node_p;
Michal Vasko601ddb32020-08-31 16:25:35 +02004647 struct lysp_grp *grp;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004648 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +02004649 ly_bool found = 0;
Radek Krejcie86bf772018-12-14 11:39:53 +01004650 const char *id, *name, *prefix;
4651 size_t prefix_len, name_len;
Michal Vasko601ddb32020-08-31 16:25:35 +02004652 struct lys_module *mod;
4653
4654 *grp_p = NULL;
4655 *grp_mod = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004656
4657 /* search for the grouping definition */
Radek Krejcie86bf772018-12-14 11:39:53 +01004658 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004659 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004660 if (prefix) {
4661 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4662 if (!mod) {
4663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004664 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004665 return LY_EVALID;
4666 }
4667 } else {
4668 mod = ctx->mod_def;
4669 }
4670 if (mod == ctx->mod_def) {
4671 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004672 grp = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004673 LY_ARRAY_FOR(grp, u) {
4674 if (!strcmp(grp[u].name, name)) {
4675 grp = &grp[u];
4676 found = 1;
4677 break;
4678 }
4679 }
4680 }
4681 }
4682 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004683 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004684 grp = mod->parsed->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004685 LY_ARRAY_FOR(grp, u) {
4686 if (!strcmp(grp[u].name, name)) {
4687 grp = &grp[u];
4688 found = 1;
4689 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004690 }
4691 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004692 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004693 /* ... and all the submodules */
Michal Vasko601ddb32020-08-31 16:25:35 +02004694 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004695 grp = mod->parsed->includes[u].submodule->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004696 LY_ARRAY_FOR(grp, v) {
4697 if (!strcmp(grp[v].name, name)) {
4698 grp = &grp[v];
4699 found = 1;
4700 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004701 }
4702 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004703 if (found) {
4704 break;
4705 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004706 }
4707 }
4708 }
4709 if (!found) {
4710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4711 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4712 return LY_EVALID;
4713 }
4714
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004715 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4716 /* remember that the grouping is instantiated to avoid its standalone validation */
4717 grp->flags |= LYS_USED_GRP;
4718 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004719
Michal Vasko601ddb32020-08-31 16:25:35 +02004720 *grp_p = grp;
4721 *grp_mod = mod;
4722 return LY_SUCCESS;
4723}
Radek Krejcie86bf772018-12-14 11:39:53 +01004724
Michal Vasko601ddb32020-08-31 16:25:35 +02004725static LY_ERR
4726lys_compile_refines(struct lysc_ctx *ctx, struct lysp_refine *refines, const struct lysc_node *context_node)
4727{
4728 struct lysc_node *node;
4729 LY_ARRAY_COUNT_TYPE u;
4730 struct lysp_refine *rfn;
4731 LY_ERR ret = LY_SUCCESS;
4732 uint32_t min, max;
4733 uint16_t flags;
4734 struct ly_set refined = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01004735
Radek Krejci327de162019-06-14 12:52:07 +02004736 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004737
Radek Krejci76b3e962018-12-14 17:01:25 +01004738 /* apply refine */
Michal Vasko601ddb32020-08-31 16:25:35 +02004739 LY_ARRAY_FOR(refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004740 lysc_update_path(ctx, NULL, rfn->nodeid);
4741
Michal Vasko601ddb32020-08-31 16:25:35 +02004742 ret = lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, context_node, ctx->mod,
Michal Vasko20424b42020-08-31 12:29:38 +02004743 0, 0, (const struct lysc_node **)&node, &flags);
4744 LY_CHECK_GOTO(ret, cleanup);
4745 ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL);
4746 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004747
4748 /* default value */
4749 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004750 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci0f969882020-08-21 16:56:47 +02004752 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE " default values.",
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004753 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Michal Vasko20424b42020-08-31 12:29:38 +02004754 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004755 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004756 }
4757 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004759 "Invalid refine of default - %s cannot hold default value(s).",
4760 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004761 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004762 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004763 }
4764 if (node->nodetype == LYS_LEAF) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004765 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004766 ret = lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], ctx->mod_def);
4767 LY_CHECK_GOTO(ret, cleanup);
4768
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004769 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004770 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004771 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4773 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
Michal Vasko20424b42020-08-31 12:29:38 +02004774 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004775 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004776 }
Radek Krejcia1911222019-07-22 17:24:50 +02004777
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004778 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004779 ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def);
4780 LY_CHECK_GOTO(ret, cleanup);
4781
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004782 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004783 } else if (node->nodetype == LYS_CHOICE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004784 if (((struct lysc_node_choice *)node)->dflt) {
Radek Krejci01342af2019-01-03 15:18:08 +01004785 /* unset LYS_SET_DFLT from the current default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02004786 ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT;
Radek Krejci01342af2019-01-03 15:18:08 +01004787 }
Michal Vasko20424b42020-08-31 12:29:38 +02004788 ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node);
4789 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004790 }
4791 }
4792
Radek Krejci12fb9142019-01-08 09:45:30 +01004793 /* description */
4794 if (rfn->dsc) {
4795 FREE_STRING(ctx->ctx, node->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004796 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->dsc, 0, &node->dsc), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004797 }
4798
4799 /* reference */
4800 if (rfn->ref) {
4801 FREE_STRING(ctx->ctx, node->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004802 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->ref, 0, &node->ref), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004803 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004804
4805 /* config */
4806 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004807 if (!flags) {
Michal Vasko20424b42020-08-31 12:29:38 +02004808 ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1);
4809 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004810 } else {
4811 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004812 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004813 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004814 }
4815
4816 /* mandatory */
4817 if (rfn->flags & LYS_MAND_MASK) {
Michal Vasko20424b42020-08-31 12:29:38 +02004818 ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1);
4819 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004820 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004821
4822 /* presence */
4823 if (rfn->presence) {
4824 if (node->nodetype != LYS_CONTAINER) {
4825 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004826 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4827 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004828 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004829 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004830 }
4831 node->flags |= LYS_PRESENCE;
4832 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004833
4834 /* must */
4835 if (rfn->musts) {
4836 switch (node->nodetype) {
4837 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02004838 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 +01004839 break;
4840 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004841 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 +01004842 break;
4843 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004844 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 +01004845 break;
4846 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004847 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 +01004848 break;
4849 case LYS_ANYXML:
4850 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02004851 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 +01004852 break;
4853 default:
4854 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004855 "Invalid refine of must statement - %s cannot hold any must statement.",
4856 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004857 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004858 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004859 }
Michal Vasko20424b42020-08-31 12:29:38 +02004860 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4861 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004862 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004863
4864 /* min/max-elements */
4865 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4866 switch (node->nodetype) {
4867 case LYS_LEAFLIST:
4868 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004869 ((struct lysc_node_leaflist *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004870 }
4871 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004872 ((struct lysc_node_leaflist *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004873 if (rfn->min) {
4874 node->flags |= LYS_MAND_TRUE;
4875 lys_compile_mandatory_parents(node->parent, 1);
4876 } else {
4877 node->flags &= ~LYS_MAND_TRUE;
4878 lys_compile_mandatory_parents(node->parent, 0);
4879 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004880 }
4881 break;
4882 case LYS_LIST:
4883 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004884 ((struct lysc_node_list *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004885 }
4886 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004887 ((struct lysc_node_list *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004888 if (rfn->min) {
4889 node->flags |= LYS_MAND_TRUE;
4890 lys_compile_mandatory_parents(node->parent, 1);
4891 } else {
4892 node->flags &= ~LYS_MAND_TRUE;
4893 lys_compile_mandatory_parents(node->parent, 0);
4894 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004895 }
4896 break;
4897 default:
4898 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004899 "Invalid refine of %s statement - %s cannot hold this statement.",
Radek Krejci0f969882020-08-21 16:56:47 +02004900 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004901 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004902 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004903 }
4904 }
Radek Krejcif0089082019-01-07 16:42:01 +01004905
4906 /* if-feature */
4907 if (rfn->iffeatures) {
4908 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004909 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004910 }
Radek Krejci327de162019-06-14 12:52:07 +02004911
4912 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004913 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004914
Radek Krejcif2271f12019-01-07 16:42:23 +01004915 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004916 for (uint32_t i = 0; i < refined.count; ++i) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004917 node = (struct lysc_node *)refined.objs[i];
Michal Vasko601ddb32020-08-31 16:25:35 +02004918 rfn = &refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02004919 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004920
4921 /* check possible conflict with default value (default added, mandatory left true) */
4922 if ((node->flags & LYS_MAND_TRUE) &&
Michal Vasko22df3f02020-08-24 13:29:22 +02004923 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) ||
Radek Krejcif2271f12019-01-07 16:42:23 +01004924 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004926 "Invalid refine of default - the node is mandatory.");
Michal Vasko20424b42020-08-31 12:29:38 +02004927 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004928 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004929 }
4930
4931 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4932 if (node->nodetype == LYS_LIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004933 min = ((struct lysc_node_list *)node)->min;
4934 max = ((struct lysc_node_list *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004935 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02004936 min = ((struct lysc_node_leaflist *)node)->min;
4937 max = ((struct lysc_node_leaflist *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004938 }
4939 if (min > max) {
4940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004941 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
Radek Krejci0f969882020-08-21 16:56:47 +02004942 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Michal Vasko20424b42020-08-31 12:29:38 +02004943 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004944 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004945 }
4946 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004947
4948 lysc_update_path(ctx, NULL, NULL);
Radek Krejcif2271f12019-01-07 16:42:23 +01004949 }
4950
Michal Vasko601ddb32020-08-31 16:25:35 +02004951cleanup:
4952 ly_set_erase(&refined, NULL);
4953 lysc_update_path(ctx, NULL, NULL);
4954 return ret;
4955}
4956
4957/**
4958 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4959 * If present, also apply uses's modificators.
4960 *
4961 * @param[in] ctx Compile context
4962 * @param[in] uses_p Parsed uses schema node.
4963 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4964 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4965 * the compile context.
4966 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4967 */
4968static LY_ERR
4969lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p)
4970{
4971 struct lysp_node *node_p;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004972 struct lysc_node *child = NULL, *iter;
Michal Vasko601ddb32020-08-31 16:25:35 +02004973 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
4974 struct lysc_node_container context_node_fake =
4975 {.nodetype = LYS_CONTAINER,
4976 .module = ctx->mod,
4977 .flags = parent ? parent->flags : 0,
4978 .child = NULL, .next = NULL,
4979 .prev = (struct lysc_node *)&context_node_fake,
4980 .actions = NULL, .notifs = NULL};
4981 struct lysp_grp *grp = NULL;
4982 LY_ARRAY_COUNT_TYPE u;
4983 uint32_t grp_stack_count;
4984 struct lys_module *grp_mod, *mod_old;
4985 LY_ERR ret = LY_SUCCESS;
4986 struct lysc_when **when, *when_shared;
4987 struct lysp_augment **augments = NULL;
4988 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
4989 struct lysc_notif **notifs = NULL;
4990 struct lysc_action **actions = NULL;
4991
4992 /* find the referenced grouping */
4993 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
4994
4995 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4996 grp_stack_count = ctx->groupings.count;
4997 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
4998 if (grp_stack_count == ctx->groupings.count) {
4999 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
5000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5001 "Grouping \"%s\" references itself through a uses statement.", grp->name);
5002 return LY_EVALID;
5003 }
5004
5005 /* switch context's mod_def */
5006 mod_old = ctx->mod_def;
5007 ctx->mod_def = grp_mod;
5008
5009 /* check status */
5010 ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, grp_mod, grp->name);
5011 LY_CHECK_GOTO(ret, cleanup);
5012
5013 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
5014 * applu refine and augment only to the nodes from the uses */
5015 if (parent) {
5016 child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5017 } else if (ctx->mod->compiled->data) {
5018 child = ctx->mod->compiled->data;
5019 } else {
5020 child = NULL;
5021 }
5022 /* remember the last child */
5023 if (child) {
5024 child = child->prev;
5025 }
5026
5027 /* compile data nodes */
5028 LY_LIST_FOR(grp->data, node_p) {
5029 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
5030 ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3);
5031 LY_CHECK_GOTO(ret, cleanup);
5032 }
5033
5034 /* split the children and add the uses's data into the fake context node */
5035 if (child) {
5036 context_node_fake.child = child->next;
5037 } else if (parent) {
5038 context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5039 } else if (ctx->mod->compiled->data) {
5040 context_node_fake.child = ctx->mod->compiled->data;
5041 }
5042 if (context_node_fake.child) {
5043 /* remember child as the last data node added by grouping to fix the list later */
5044 child = context_node_fake.child->prev;
5045 context_node_fake.child->prev = NULL;
5046 }
5047
5048 when_shared = NULL;
5049 LY_LIST_FOR(context_node_fake.child, iter) {
5050 iter->parent = (struct lysc_node *)&context_node_fake;
5051
5052 /* pass uses's when to all the data children, actions and notifications are ignored */
5053 if (uses_p->when) {
5054 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
5055 if (!when_shared) {
5056 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when);
5057 LY_CHECK_GOTO(ret, cleanup);
5058
5059 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5060 /* do not check "when" semantics in a grouping */
5061 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5062 LY_CHECK_GOTO(ret, cleanup);
5063 }
5064
5065 when_shared = *when;
5066 } else {
5067 ++when_shared->refcount;
5068 (*when) = when_shared;
5069
5070 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5071 /* in this case check "when" again for all children because of dummy node check */
5072 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5073 LY_CHECK_GOTO(ret, cleanup);
5074 }
5075 }
5076 }
5077 }
5078
5079 /* compile actions */
5080 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5081 if (actions) {
5082 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
5083 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
5084 if (*actions && (uses_p->augments || uses_p->refines)) {
5085 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5086 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
5087 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
5088 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
5089 }
5090 }
5091
5092 /* compile notifications */
5093 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5094 if (notifs) {
5095 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
5096 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
5097 if (*notifs && (uses_p->augments || uses_p->refines)) {
5098 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5099 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
5100 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
5101 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
5102 }
5103 }
5104
5105 /* sort and apply augments */
5106 ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments);
5107 LY_CHECK_GOTO(ret, cleanup);
5108 LY_ARRAY_FOR(augments, u) {
5109 ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake);
5110 LY_CHECK_GOTO(ret, cleanup);
5111 }
5112
5113 /* reload previous context's mod_def */
5114 ctx->mod_def = mod_old;
5115
5116 /* apply all refines */
5117 ret = lys_compile_refines(ctx, uses_p->refines, (struct lysc_node *)&context_node_fake);
5118 LY_CHECK_GOTO(ret, cleanup);
5119
Radek Krejcic6b4f442020-08-12 14:45:18 +02005120 if (first_p) {
5121 *first_p = context_node_fake.child;
5122 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005123
5124cleanup:
5125 /* fix connection of the children nodes from fake context node back into the parent */
5126 if (context_node_fake.child) {
5127 context_node_fake.child->prev = child;
5128 }
5129 LY_LIST_FOR(context_node_fake.child, child) {
5130 child->parent = parent;
5131 }
5132
5133 if (uses_p->augments || uses_p->refines) {
5134 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005135 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005136 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005137 LY_ARRAY_FREE(context_node_fake.actions);
5138 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005139 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005140 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005141 LY_ARRAY_FREE(context_node_fake.notifs);
5142 }
5143 }
5144
Radek Krejcie86bf772018-12-14 11:39:53 +01005145 /* reload previous context's mod_def */
5146 ctx->mod_def = mod_old;
5147 /* remove the grouping from the stack for circular groupings dependency check */
5148 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5149 assert(ctx->groupings.count == grp_stack_count);
Radek Krejci3641f562019-02-13 15:38:40 +01005150 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005151
5152 return ret;
5153}
5154
Radek Krejci327de162019-06-14 12:52:07 +02005155static int
5156lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5157{
5158 struct lysp_node *iter;
5159 int len = 0;
5160
5161 *path = NULL;
5162 for (iter = node; iter && len >= 0; iter = iter->parent) {
5163 char *s = *path;
5164 char *id;
5165
5166 switch (iter->nodetype) {
5167 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005168 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005169 break;
5170 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005171 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005172 break;
5173 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005174 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005175 break;
5176 default:
5177 id = strdup(iter->name);
5178 break;
5179 }
5180
5181 if (!iter->parent) {
5182 /* print prefix */
5183 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5184 } else {
5185 /* prefix is the same as in parent */
5186 len = asprintf(path, "/%s%s", id, s ? s : "");
5187 }
5188 free(s);
5189 free(id);
5190 }
5191
5192 if (len < 0) {
5193 free(*path);
5194 *path = NULL;
5195 } else if (len == 0) {
5196 *path = strdup("/");
5197 len = 1;
5198 }
5199 return len;
5200}
5201
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005202/**
5203 * @brief Validate groupings that were defined but not directly used in the schema itself.
5204 *
5205 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5206 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5207 */
5208static LY_ERR
5209lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5210{
5211 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005212 char *path;
5213 int len;
5214
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005215 struct lysp_node_uses fake_uses = {
5216 .parent = node_p,
5217 .nodetype = LYS_USES,
5218 .flags = 0, .next = NULL,
5219 .name = grp->name,
5220 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5221 .refines = NULL, .augments = NULL
5222 };
5223 struct lysc_node_container fake_container = {
5224 .nodetype = LYS_CONTAINER,
5225 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5226 .module = ctx->mod,
5227 .sp = NULL, .parent = NULL, .next = NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +02005228 .prev = (struct lysc_node *)&fake_container,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005229 .name = "fake",
5230 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5231 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5232 };
5233
5234 if (grp->parent) {
5235 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5236 }
Radek Krejci327de162019-06-14 12:52:07 +02005237
5238 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5239 if (len < 0) {
5240 LOGMEM(ctx->ctx);
5241 return LY_EMEM;
5242 }
5243 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005244 ctx->path_len = (uint32_t)len;
Radek Krejci327de162019-06-14 12:52:07 +02005245 free(path);
5246
5247 lysc_update_path(ctx, NULL, "{grouping}");
5248 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02005249 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005250 lysc_update_path(ctx, NULL, NULL);
5251 lysc_update_path(ctx, NULL, NULL);
5252
5253 ctx->path_len = 1;
5254 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005255
5256 /* cleanup */
5257 lysc_node_container_free(ctx->ctx, &fake_container);
5258
5259 return ret;
5260}
Radek Krejcife909632019-02-12 15:34:42 +01005261
Radek Krejcie86bf772018-12-14 11:39:53 +01005262/**
Michal Vasko20424b42020-08-31 12:29:38 +02005263 * @brief Set config flags for a node.
5264 *
5265 * @param[in] ctx Compile context.
5266 * @param[in] node Compiled node config to set.
5267 * @param[in] parent Parent of @p node.
5268 * @return LY_ERR value.
5269 */
5270static LY_ERR
5271lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
5272{
5273 if (node->nodetype == LYS_CASE) {
5274 /* case never has any config */
5275 assert(!(node->flags & LYS_CONFIG_MASK));
5276 return LY_SUCCESS;
5277 }
5278
5279 /* adjust parent to always get the ancestor with config */
5280 if (parent && (parent->nodetype == LYS_CASE)) {
5281 parent = parent->parent;
5282 assert(parent);
5283 }
5284
5285 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
5286 /* ignore config statements inside RPC/action data */
5287 node->flags &= ~LYS_CONFIG_MASK;
5288 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5289 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
5290 /* ignore config statements inside Notification data */
5291 node->flags &= ~LYS_CONFIG_MASK;
5292 node->flags |= LYS_CONFIG_R;
5293 } else if (!(node->flags & LYS_CONFIG_MASK)) {
5294 /* config not explicitely set, inherit it from parent */
5295 if (parent) {
5296 node->flags |= parent->flags & LYS_CONFIG_MASK;
5297 } else {
5298 /* default is config true */
5299 node->flags |= LYS_CONFIG_W;
5300 }
5301 } else {
5302 /* config set explicitely */
5303 node->flags |= LYS_SET_CONFIG;
5304 }
5305
5306 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5307 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5308 "Configuration node cannot be child of any state data node.");
5309 return LY_EVALID;
5310 }
5311
5312 return LY_SUCCESS;
5313}
5314
5315/**
Radek Krejcia3045382018-11-22 14:30:31 +01005316 * @brief Compile parsed schema node information.
5317 * @param[in] ctx Compile context
5318 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005319 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5320 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5321 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005322 * @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).
5323 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005324 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5325 */
Radek Krejci19a96102018-11-15 13:38:09 +01005326static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005327lys_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 +01005328{
Radek Krejci1c54f462020-05-12 17:25:34 +02005329 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005330 struct lysc_node *node = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005331 struct lysc_when **when;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005332 LY_ARRAY_COUNT_TYPE u;
Michal Vasko22df3f02020-08-24 13:29:22 +02005333 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
Radek Krejci19a96102018-11-15 13:38:09 +01005334
Radek Krejci327de162019-06-14 12:52:07 +02005335 if (node_p->nodetype != LYS_USES) {
5336 lysc_update_path(ctx, parent, node_p->name);
5337 } else {
5338 lysc_update_path(ctx, NULL, "{uses}");
5339 lysc_update_path(ctx, NULL, node_p->name);
5340 }
5341
Radek Krejci19a96102018-11-15 13:38:09 +01005342 switch (node_p->nodetype) {
5343 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02005344 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
Radek Krejci19a96102018-11-15 13:38:09 +01005345 node_compile_spec = lys_compile_node_container;
5346 break;
5347 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005348 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
Radek Krejci19a96102018-11-15 13:38:09 +01005349 node_compile_spec = lys_compile_node_leaf;
5350 break;
5351 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005352 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005353 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005354 break;
5355 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005356 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005357 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005358 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005359 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02005360 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005361 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005362 break;
Michal Vasko20424b42020-08-31 12:29:38 +02005363 case LYS_CASE:
5364 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
5365 node_compile_spec = lys_compile_node_case;
5366 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005367 case LYS_ANYXML:
5368 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005369 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005370 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005371 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005372 case LYS_USES:
Michal Vasko9e8de2d2020-09-01 08:17:10 +02005373 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, parent, &node);
Radek Krejci327de162019-06-14 12:52:07 +02005374 lysc_update_path(ctx, NULL, NULL);
5375 lysc_update_path(ctx, NULL, NULL);
5376 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005377 default:
5378 LOGINT(ctx->ctx);
5379 return LY_EINT;
5380 }
5381 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5382 node->nodetype = node_p->nodetype;
5383 node->module = ctx->mod;
5384 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005385 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005386
5387 /* config */
Michal Vasko20424b42020-08-31 12:29:38 +02005388 ret = lys_compile_config(ctx, node, parent);
5389 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005390
Michal Vasko20424b42020-08-31 12:29:38 +02005391 /* list ordering */
Radek Krejcia6d57732018-11-29 13:40:37 +01005392 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5393 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005394 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci0f969882020-08-21 16:56:47 +02005395 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5396 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005397 node->flags &= ~LYS_ORDBY_MASK;
5398 node->flags |= LYS_ORDBY_SYSTEM;
5399 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5400 /* default ordering is system */
5401 node->flags |= LYS_ORDBY_SYSTEM;
5402 }
5403 }
5404
Radek Krejci19a96102018-11-15 13:38:09 +01005405 /* status - it is not inherited by specification, but it does not make sense to have
5406 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko20424b42020-08-31 12:29:38 +02005407 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 +01005408
Radek Krejciec4da802019-05-02 13:02:41 +02005409 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005410 node->sp = node_p;
5411 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02005412 DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error);
5413 DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error);
5414 DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error);
Radek Krejci00b874b2019-02-12 10:54:50 +01005415 if (node_p->when) {
5416 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005417 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005418
5419 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5420 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02005421 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
5422 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005423 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005424 }
Radek Krejciec4da802019-05-02 13:02:41 +02005425 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005426
Michal Vasko20424b42020-08-31 12:29:38 +02005427 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
5428 LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node));
5429
Radek Krejci19a96102018-11-15 13:38:09 +01005430 /* nodetype-specific part */
Michal Vasko20424b42020-08-31 12:29:38 +02005431 LY_CHECK_RET(node_compile_spec(ctx, node_p, node));
Radek Krejci19a96102018-11-15 13:38:09 +01005432
Michal Vasko20424b42020-08-31 12:29:38 +02005433 /* final compilation tasks that require the node to be connected */
5434 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, done);
Radek Krejcife909632019-02-12 15:34:42 +01005435 if (node->flags & LYS_MAND_TRUE) {
Michal Vasko20424b42020-08-31 12:29:38 +02005436 /* inherit LYS_MAND_TRUE in parent containers */
Radek Krejcife909632019-02-12 15:34:42 +01005437 lys_compile_mandatory_parents(parent, 1);
5438 }
5439
Radek Krejci327de162019-06-14 12:52:07 +02005440 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005441 return LY_SUCCESS;
5442
5443error:
5444 lysc_node_free(ctx->ctx, node);
Michal Vasko20424b42020-08-31 12:29:38 +02005445done:
Radek Krejci19a96102018-11-15 13:38:09 +01005446 return ret;
5447}
5448
Radek Krejciccd20f12019-02-15 14:12:27 +01005449static void
Michal Vasko20424b42020-08-31 12:29:38 +02005450lysc_node_unlink(struct lysc_node *node)
Radek Krejciccd20f12019-02-15 14:12:27 +01005451{
Michal Vasko20424b42020-08-31 12:29:38 +02005452 struct lysc_node *parent, *child;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005453 struct lysc_module *modc = node->module->compiled;
Radek Krejciccd20f12019-02-15 14:12:27 +01005454
5455 parent = node->parent;
5456
5457 /* parent's first child */
Michal Vasko20424b42020-08-31 12:29:38 +02005458 if (parent && lysc_node_children(parent, node->flags) == node) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005459 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005460 } else if (modc->data == node) {
5461 modc->data = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005462 }
Michal Vasko20424b42020-08-31 12:29:38 +02005463
5464 /* special choice case unlinking */
5465 if (parent && parent->nodetype == LYS_CHOICE) {
5466 if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) {
5467 /* default case removed */
5468 ((struct lysc_node_choice *)parent)->dflt = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005469 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005470 }
5471
5472 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005473 if (node->prev->next) {
5474 node->prev->next = node->next;
5475 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005476 if (node->next) {
5477 node->next->prev = node->prev;
Michal Vasko20424b42020-08-31 12:29:38 +02005478 } else {
5479 /* last child */
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005480 if (parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005481 child = (struct lysc_node *)lysc_node_children(parent, node->flags);
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005482 } else {
5483 child = modc->data;
5484 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005485 if (child) {
5486 child->prev = node->prev;
5487 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005488 }
5489}
5490
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005491struct lysc_deviation {
5492 const char *nodeid;
5493 struct lysc_node *target; /* target node of the deviation */
Michal Vasko22df3f02020-08-24 13:29:22 +02005494 struct lysp_deviate **deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02005495 uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */
Radek Krejci857189e2020-09-01 13:26:36 +02005496 ly_bool not_supported; /* flag if deviates contains not-supported deviate */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005497};
5498
Michal Vaskoccc062a2020-08-13 08:34:50 +02005499/* MACROS for deviates checking */
5500#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5501 if (!(target->nodetype & (NODETYPES))) { \
5502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5503 goto cleanup; \
5504 }
5505
5506#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5507 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5509 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5510 goto cleanup; \
5511 }
5512
5513#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5514 if (!((TYPE)target)->MEMBER || COND) { \
5515 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5516 goto cleanup; \
5517 }
5518
5519/**
5520 * @brief Apply deviate add.
5521 *
5522 * @param[in] ctx Compile context.
5523 * @param[in] target Deviation target.
5524 * @param[in] dev_flags Internal deviation flags.
5525 * @param[in] d Deviate add to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02005526 * @return LY_ERR value.
5527 */
5528static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005529lys_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 +02005530{
Radek Krejci011e4aa2020-09-04 15:22:31 +02005531 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005532 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5533 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005534 LY_ARRAY_COUNT_TYPE x;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005535
5536#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5537 if (((TYPE)target)->MEMBER COND) { \
5538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5539 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5540 PROPERTY, ((TYPE)target)->MEMBER); \
5541 goto cleanup; \
5542 }
5543
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005544#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005545 if (((TYPE)target)->MEMBER && (COND)) { \
Radek Krejci857189e2020-09-01 13:26:36 +02005546 ly_bool dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005547 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005548 ctx->mod_def, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5550 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5551 if (dynamic_) {free((void*)val_);} \
5552 goto cleanup; \
5553 }
5554
5555#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5556 if (((TYPE)target)->MEMBER && (COND)) { \
5557 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5558 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5559 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5560 goto cleanup; \
5561 }
5562
5563 /* [units-stmt] */
5564 if (d->units) {
5565 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5566 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5567
5568 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02005569 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc);
5570 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005571 }
5572
5573 /* *must-stmt */
5574 if (d->musts) {
5575 switch (target->nodetype) {
5576 case LYS_CONTAINER:
5577 case LYS_LIST:
5578 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5579 x, lys_compile_must, ret, cleanup);
5580 break;
5581 case LYS_LEAF:
5582 case LYS_LEAFLIST:
5583 case LYS_ANYDATA:
5584 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5585 x, lys_compile_must, ret, cleanup);
5586 break;
5587 case LYS_NOTIF:
5588 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5589 x, lys_compile_must, ret, cleanup);
5590 break;
5591 case LYS_RPC:
5592 case LYS_ACTION:
5593 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5594 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5595 x, lys_compile_must, ret, cleanup);
5596 break;
5597 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5598 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5599 x, lys_compile_must, ret, cleanup);
5600 break;
5601 }
Radek Krejci0f969882020-08-21 16:56:47 +02005602 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005603 default:
5604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5605 lys_nodetype2str(target->nodetype), "add", "must");
5606 goto cleanup;
5607 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02005608 ret = ly_set_add(&ctx->xpath, target, 0, NULL);
5609 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005610 }
5611
5612 /* *unique-stmt */
5613 if (d->uniques) {
5614 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5615 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5616 }
5617
5618 /* *default-stmt */
5619 if (d->dflts) {
5620 switch (target->nodetype) {
5621 case LYS_LEAF:
5622 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005623 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005624 if (leaf->dflt) {
5625 /* first, remove the default value taken from the type */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005626 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5627 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005628 free(leaf->dflt);
5629 leaf->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005630 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005631
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005632 /* store the default value in unres */
Michal Vasko31a82d52020-08-21 08:11:48 +02005633 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005634 target->flags |= LYS_SET_DFLT;
5635 break;
5636 case LYS_LEAFLIST:
5637 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5638 /* first, remove the default value taken from the type */
5639 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005640 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5641 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5642 free(llist->dflts[x]);
5643 }
5644 LY_ARRAY_FREE(llist->dflts);
5645 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005646 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005647
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005648 /* store the default values in unres */
5649 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005650 target->flags |= LYS_SET_DFLT;
5651 break;
5652 case LYS_CHOICE:
5653 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5654 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5655 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5656 * to allow making the default case even the augmented case from the deviating module */
5657 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5658 goto cleanup;
5659 }
5660 break;
5661 default:
5662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5663 lys_nodetype2str(target->nodetype), "add", "default");
5664 goto cleanup;
5665 }
5666 }
5667
5668 /* [config-stmt] */
5669 if (d->flags & LYS_CONFIG_MASK) {
5670 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5672 lys_nodetype2str(target->nodetype), "add", "config");
5673 goto cleanup;
5674 }
5675 if (dev_flags) {
5676 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5677 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5678 }
5679 if (target->flags & LYS_SET_CONFIG) {
5680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5681 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5682 target->flags & LYS_CONFIG_W ? "true" : "false");
5683 goto cleanup;
5684 }
5685 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5686 }
5687
5688 /* [mandatory-stmt] */
5689 if (d->flags & LYS_MAND_MASK) {
5690 if (target->flags & LYS_MAND_MASK) {
5691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5692 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5693 target->flags & LYS_MAND_TRUE ? "true" : "false");
5694 goto cleanup;
5695 }
5696 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5697 }
5698
5699 /* [min-elements-stmt] */
5700 if (d->flags & LYS_SET_MIN) {
5701 if (target->nodetype == LYS_LEAFLIST) {
5702 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5703 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005704 ((struct lysc_node_leaflist *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005705 } else if (target->nodetype == LYS_LIST) {
5706 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5707 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005708 ((struct lysc_node_list *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005709 } else {
5710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5711 lys_nodetype2str(target->nodetype), "add", "min-elements");
5712 goto cleanup;
5713 }
5714 if (d->min) {
5715 target->flags |= LYS_MAND_TRUE;
5716 }
5717 }
5718
5719 /* [max-elements-stmt] */
5720 if (d->flags & LYS_SET_MAX) {
5721 if (target->nodetype == LYS_LEAFLIST) {
5722 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5723 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005724 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005725 } else if (target->nodetype == LYS_LIST) {
5726 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5727 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005728 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005729 } else {
5730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5731 lys_nodetype2str(target->nodetype), "add", "max-elements");
5732 goto cleanup;
5733 }
5734 }
5735
5736 ret = LY_SUCCESS;
5737
5738cleanup:
5739 return ret;
5740}
5741
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005742static LY_ERR
5743lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt)
5744{
5745 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005746 ly_bool dyn = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005747 const char *orig_dflt;
5748 uint32_t i;
5749
5750 if (target->module != ctx->mod) {
5751 /* foreign deviation */
5752 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt);
5753
5754 /* check that the value matches */
5755 orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5756 if (strcmp(orig_dflt, dflt)) {
5757 goto error;
5758 }
5759 if (dyn) {
5760 free((char *)orig_dflt);
5761 }
5762
5763 /* remove the default specification */
5764 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5765 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5766 free(leaf->dflt);
5767 leaf->dflt = NULL;
5768 } else {
5769 /* local deviation */
5770 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt);
5771
5772 /* find the incomplete default */
5773 orig_dflt = NULL;
5774 for (i = 0; i < ctx->dflts.count; ++i) {
5775 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
5776 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5777 break;
5778 }
5779 }
5780 LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT);
5781
5782 /* check that the value matches */
5783 if (strcmp(orig_dflt, dflt)) {
5784 goto error;
5785 }
5786
5787 /* update the list of incomplete default values */
5788 lysc_incomplete_dflt_remove(ctx, target);
5789 }
5790
5791 target->flags &= ~LYS_SET_DFLT;
5792 return LY_SUCCESS;
5793
5794error:
5795 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5796 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5797 dflt, orig_dflt);
5798 if (dyn) {
5799 free((char *)orig_dflt);
5800 }
5801cleanup:
5802 return LY_EVALID;
5803}
5804
5805static LY_ERR
5806lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts)
5807{
5808 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005809 ly_bool dyn = 0, found;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005810 const char *orig_dflt, **orig_dflts;
5811 uint32_t i;
5812 LY_ARRAY_COUNT_TYPE x, y;
5813
5814 if (target->module != ctx->mod) {
5815 /* foreign deviation */
5816 DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]);
5817 LY_ARRAY_FOR(dflts, x) {
5818 found = 0;
5819 LY_ARRAY_FOR(llist->dflts, y) {
5820 orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5821 if (!strcmp(orig_dflt, dflts[x])) {
5822 if (dyn) {
5823 free((char *)orig_dflt);
5824 }
5825 found = 1;
5826 break;
5827 }
5828 if (dyn) {
5829 free((char *)orig_dflt);
5830 }
5831 }
5832 if (!found) {
5833 goto error;
5834 }
5835
5836 /* update compiled default values */
5837 LY_ARRAY_DECREMENT(llist->dflts);
5838 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
5839 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
5840 free(llist->dflts[y]);
5841 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
5842 }
5843 if (!LY_ARRAY_COUNT(llist->dflts)) {
5844 LY_ARRAY_FREE(llist->dflts);
5845 llist->dflts = NULL;
5846 llist->flags &= ~LYS_SET_DFLT;
5847 }
5848 } else {
5849 /* local deviation */
5850 orig_dflt = NULL;
5851 orig_dflts = NULL;
5852 for (i = 0; i < ctx->dflts.count; ++i) {
5853 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) {
5854 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5855 orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts;
5856 break;
5857 }
5858 }
5859 LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT);
5860
5861 if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) {
5862 /* TODO it is not currently possible to remove just one default value from incomplete defaults array */
5863 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5864 "Local deviation deleting leaf-list defaults is not supported.");
5865 return LY_EVALID;
5866 }
5867
5868 LY_ARRAY_FOR(dflts, x) {
5869 found = 0;
5870 if (orig_dflts) {
5871 LY_ARRAY_FOR(orig_dflts, y) {
5872 if (!strcmp(orig_dflts[y], dflts[x])) {
5873 found = 1;
5874 break;
5875 }
5876 }
5877 } else if (!strcmp(orig_dflt, dflts[x])) {
5878 found = 1;
5879 }
5880 if (!found) {
5881 goto error;
5882 }
5883
5884 /* update the list of incomplete default values */
5885 lysc_incomplete_dflt_remove(ctx, target);
5886 }
5887
5888 llist->flags &= ~LYS_SET_DFLT;
5889 }
5890
5891 return LY_SUCCESS;
5892
5893error:
5894 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
5895 "which does not match any of the target's property values.", dflts[x]);
5896cleanup:
5897 return LY_EVALID;
5898}
5899
Michal Vaskoccc062a2020-08-13 08:34:50 +02005900/**
5901 * @brief Apply deviate delete.
5902 *
5903 * @param[in] ctx Compile context.
5904 * @param[in] target Deviation target.
5905 * @param[in] dev_flags Internal deviation flags.
5906 * @param[in] d Deviate delete to apply.
5907 * @return LY_ERR value.
5908 */
5909static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005910lys_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 +02005911{
5912 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005913 struct lysc_node_list *list = (struct lysc_node_list *)target;
5914 LY_ARRAY_COUNT_TYPE x, y, z;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005915 size_t prefix_len, name_len;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005916 const char *prefix, *name, *nodeid;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005917 struct lys_module *mod;
5918
5919#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5920 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5921 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5922 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5923 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5924 } \
5925 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5927 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5928 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5929 goto cleanup; \
5930 } \
5931 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5932 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5933 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5934 &((TYPE)target)->ARRAY_TRG[y + 1], \
5935 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5936 } \
5937 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5938 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5939 ((TYPE)target)->ARRAY_TRG = NULL; \
5940 }
5941
5942 /* [units-stmt] */
5943 if (d->units) {
5944 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Michal Vasko22df3f02020-08-24 13:29:22 +02005945 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units);
5946 if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5948 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02005949 d->units, ((struct lysc_node_leaf *)target)->units);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005950 goto cleanup;
5951 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005952 lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5953 ((struct lysc_node_leaf *)target)->units = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005954 }
5955
5956 /* *must-stmt */
5957 if (d->musts) {
5958 switch (target->nodetype) {
5959 case LYS_CONTAINER:
5960 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005961 DEV_DEL_ARRAY(struct lysc_node_container *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005962 break;
5963 case LYS_LEAF:
5964 case LYS_LEAFLIST:
5965 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005966 DEV_DEL_ARRAY(struct lysc_node_leaf *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005967 break;
5968 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005969 DEV_DEL_ARRAY(struct lysc_notif *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005970 break;
5971 case LYS_RPC:
5972 case LYS_ACTION:
5973 if (dev_flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005974 DEV_DEL_ARRAY(struct lysc_action *, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005975 break;
5976 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005977 DEV_DEL_ARRAY(struct lysc_action *, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005978 break;
5979 }
Radek Krejci0f969882020-08-21 16:56:47 +02005980 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005981 default:
5982 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5983 lys_nodetype2str(target->nodetype), "delete", "must");
5984 goto cleanup;
5985 }
5986 }
5987
5988 /* *unique-stmt */
5989 if (d->uniques) {
5990 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5991 LY_ARRAY_FOR(d->uniques, x) {
5992 LY_ARRAY_FOR(list->uniques, z) {
5993 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
5994 nodeid = strpbrk(name, " \t\n");
5995 if (nodeid) {
5996 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
5997 break;
5998 }
5999 while (isspace(*nodeid)) {
6000 ++nodeid;
6001 }
6002 } else {
6003 if (strcmp(name, list->uniques[z][y]->name)) {
6004 break;
6005 }
6006 }
6007 }
6008 if (!name) {
6009 /* complete match - remove the unique */
6010 LY_ARRAY_DECREMENT(list->uniques);
6011 LY_ARRAY_FREE(list->uniques[z]);
6012 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
6013 --z;
6014 break;
6015 }
6016 }
6017 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
6018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6019 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6020 d->uniques[x]);
6021 goto cleanup;
6022 }
6023 }
6024 if (!LY_ARRAY_COUNT(list->uniques)) {
6025 LY_ARRAY_FREE(list->uniques);
6026 list->uniques = NULL;
6027 }
6028 }
6029
6030 /* *default-stmt */
6031 if (d->dflts) {
6032 switch (target->nodetype) {
6033 case LYS_LEAF:
6034 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006035 LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006036 break;
6037 case LYS_LEAFLIST:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006038 LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006039 break;
6040 case LYS_CHOICE:
6041 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vasko22df3f02020-08-24 13:29:22 +02006042 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "deleting", "default", d->dflts[0]);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006043 nodeid = d->dflts[0];
6044 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6045 if (prefix) {
6046 /* use module prefixes from the deviation module to match the module of the default case */
6047 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006048 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006049 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6050 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6051 goto cleanup;
6052 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006053 if (mod != ((struct lysc_node_choice *)target)->dflt->module) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006054 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006055 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6056 "The prefix does not match the default case's module.", d->dflts[0]);
6057 goto cleanup;
6058 }
6059 }
6060 /* else {
6061 * strictly, the default prefix would point to the deviation module, but the value should actually
6062 * match the default string in the original module (usually unprefixed), so in this case we do not check
6063 * the module of the default case, just matching its name */
Michal Vasko22df3f02020-08-24 13:29:22 +02006064 if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02006065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6066 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02006067 d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006068 goto cleanup;
6069 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006070 ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT;
6071 ((struct lysc_node_choice *)target)->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006072 break;
6073 default:
6074 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6075 lys_nodetype2str(target->nodetype), "delete", "default");
6076 goto cleanup;
6077 }
6078 }
6079
6080 ret = LY_SUCCESS;
6081
6082cleanup:
6083 return ret;
6084}
6085
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006086static LY_ERR
6087lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target)
6088{
6089 LY_ERR ret;
6090 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6091 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6092 struct ly_err_item *err = NULL;
6093 LY_ARRAY_COUNT_TYPE x;
6094 const char *dflt;
Radek Krejci857189e2020-09-01 13:26:36 +02006095 ly_bool dyn;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006096
6097 if (target->module != ctx->mod) {
6098 /* foreign deviation */
6099 if (target->nodetype == LYS_LEAF) {
6100 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn);
6101 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6102 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6103
6104 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6105 LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err);
6106 if (dyn) {
6107 free((char *)dflt);
6108 }
6109 if (err) {
6110 ly_err_print(err);
6111 ctx->path[0] = '\0';
6112 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6114 "Invalid default - value does not fit the type (%s).", err->msg);
6115 ly_err_free(err);
6116 }
6117 LY_CHECK_RET(ret);
6118
6119 ++leaf->dflt->realtype->refcount;
6120 } else { /* LY_LEAFLIST */
6121 LY_ARRAY_FOR(llist->dflts, x) {
6122 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn);
6123 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6124 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6125
6126 ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6127 LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err);
6128 if (dyn) {
6129 free((char *)dflt);
6130 }
6131 if (err) {
6132 ly_err_print(err);
6133 ctx->path[0] = '\0';
6134 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6135 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6136 "Invalid default - value does not fit the type (%s).", err->msg);
6137 ly_err_free(err);
6138 }
6139 LY_CHECK_RET(ret);
6140
6141 ++llist->dflts[x]->realtype->refcount;
6142 }
6143 }
6144 } else {
6145 /* local deviation */
6146
6147 /* these default were not compiled yet, so they will use the new type automatically */
6148 }
6149
6150 return LY_SUCCESS;
6151}
6152
Michal Vaskoccc062a2020-08-13 08:34:50 +02006153/**
6154 * @brief Apply deviate replace.
6155 *
6156 * @param[in] ctx Compile context.
6157 * @param[in] target Deviation target.
6158 * @param[in] d Deviate replace to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02006159 * @return LY_ERR value.
6160 */
6161static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006162lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02006163{
Radek Krejci011e4aa2020-09-04 15:22:31 +02006164 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006165 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6166 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6167 LY_ARRAY_COUNT_TYPE x;
6168
6169#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6170 if (!(((TYPE)target)->MEMBER COND)) { \
6171 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6172 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6173 goto cleanup; \
6174 }
6175
6176 /* [type-stmt] */
6177 if (d->type) {
6178 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6179 /* type is mandatory, so checking for its presence is not necessary */
6180 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6181
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006182 /* remove only default value inherited from the type */
6183 if (!(target->flags & LYS_SET_DFLT)) {
6184 if (target->module != ctx->mod) {
6185 /* foreign deviation - the target has default from the previous type, remove it */
6186 if (target->nodetype == LYS_LEAF) {
6187 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6188 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6189 free(leaf->dflt);
6190 leaf->dflt = NULL;
6191 } else { /* LYS_LEAFLIST */
6192 LY_ARRAY_FOR(llist->dflts, x) {
6193 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6194 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6195 free(llist->dflts[x]);
6196 }
6197 LY_ARRAY_FREE(llist->dflts);
6198 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006199 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006200 } else {
6201 /* local deviation */
6202 lysc_incomplete_dflt_remove(ctx, target);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006203 }
6204 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006205
6206 LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf));
6207
6208 if (target->flags & LYS_SET_DFLT) {
6209 /* the term default value(s) needs to be recompiled */
6210 LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006211 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006212 }
6213
6214 /* [units-stmt] */
6215 if (d->units) {
6216 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6217 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6218 units, "replacing", "units", d->units);
6219
6220 lydict_remove(ctx->ctx, leaf->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02006221 DUP_STRING(ctx->ctx, d->units, leaf->units, rc);
6222 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006223 }
6224
6225 /* [default-stmt] */
6226 if (d->dflt) {
6227 switch (target->nodetype) {
6228 case LYS_LEAF:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006229 if (target->module != ctx->mod) {
6230 /* foreign deviation */
6231 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing",
6232 "default", d->dflt);
6233
6234 /* remove the default specification */
6235 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6236 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6237 free(leaf->dflt);
6238 leaf->dflt = NULL;
6239 } else {
6240 /* local deviation */
6241 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing",
6242 "default", d->dflt);
6243 assert(!leaf->dflt);
6244 }
6245
6246 /* store the new default value */
6247 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006248 break;
6249 case LYS_CHOICE:
6250 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6251 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6252 goto cleanup;
6253 }
6254 break;
6255 default:
6256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6257 lys_nodetype2str(target->nodetype), "replace", "default");
6258 goto cleanup;
6259 }
6260 }
6261
6262 /* [config-stmt] */
6263 if (d->flags & LYS_CONFIG_MASK) {
6264 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6265 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6266 lys_nodetype2str(target->nodetype), "replace", "config");
6267 goto cleanup;
6268 }
6269 if (!(target->flags & LYS_SET_CONFIG)) {
6270 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6271 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6272 goto cleanup;
6273 }
6274 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6275 }
6276
6277 /* [mandatory-stmt] */
6278 if (d->flags & LYS_MAND_MASK) {
6279 if (!(target->flags & LYS_MAND_MASK)) {
6280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6281 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6282 goto cleanup;
6283 }
6284 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6285 }
6286
6287 /* [min-elements-stmt] */
6288 if (d->flags & LYS_SET_MIN) {
6289 if (target->nodetype == LYS_LEAFLIST) {
6290 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6291 /* change value */
6292 ((struct lysc_node_leaflist *)target)->min = d->min;
6293 } else if (target->nodetype == LYS_LIST) {
6294 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6295 /* change value */
6296 ((struct lysc_node_list *)target)->min = d->min;
6297 } else {
6298 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6299 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6300 goto cleanup;
6301 }
6302 if (d->min) {
6303 target->flags |= LYS_MAND_TRUE;
6304 }
6305 }
6306
6307 /* [max-elements-stmt] */
6308 if (d->flags & LYS_SET_MAX) {
6309 if (target->nodetype == LYS_LEAFLIST) {
6310 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6311 /* change value */
6312 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6313 } else if (target->nodetype == LYS_LIST) {
6314 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6315 /* change value */
6316 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6317 } else {
6318 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6319 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6320 goto cleanup;
6321 }
6322 }
6323
6324 ret = LY_SUCCESS;
6325
6326cleanup:
6327 return ret;
6328}
6329
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006330/**
6331 * @brief Apply all deviations of one target node.
6332 *
6333 * @param[in] ctx Compile context.
6334 * @param[in] dev Deviation structure to apply.
6335 * @return LY_ERR value.
6336 */
6337static LY_ERR
6338lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006339{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006340 LY_ERR ret = LY_EVALID;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006341 struct lysc_node *target = dev->target;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006342 struct lysc_action *rpcs;
6343 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006344 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006345 LY_ARRAY_COUNT_TYPE v, x;
Radek Krejci551b12c2019-02-19 16:11:21 +01006346 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006347
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006348 lysc_update_path(ctx, NULL, dev->nodeid);
6349
6350 /* not-supported */
6351 if (dev->not_supported) {
6352 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
Radek Krejci0f969882020-08-21 16:56:47 +02006353 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 +02006354 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6355 }
6356
6357#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6358 if (target->parent) { \
6359 ARRAY = (TYPE*)GETFUNC(target->parent); \
6360 } else { \
6361 ARRAY = target->module->compiled->ARRAY; \
6362 } \
6363 LY_ARRAY_FOR(ARRAY, x) { \
6364 if (&ARRAY[x] == (TYPE*)target) { break; } \
6365 } \
6366 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6367 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6368 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6369 LY_ARRAY_DECREMENT(ARRAY); \
6370 }
6371
6372 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6373 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6374 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006375 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6376 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6377 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6378 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006379 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6380 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006381 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6382 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6383 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6384 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006385 } else {
6386 /* remove RPC/action */
6387 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6388 }
6389 } else if (target->nodetype == LYS_NOTIF) {
6390 /* remove Notification */
6391 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6392 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02006393 if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) {
6394 /* remove the target node with its parent case node because it is the only node of the case */
6395 lysc_node_unlink(target->parent);
6396 lysc_node_free(ctx->ctx, target->parent);
6397 } else {
6398 /* remove the target node */
6399 lysc_node_unlink(target);
6400 lysc_node_free(ctx->ctx, target);
6401 }
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006402 }
6403
6404 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6405 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6406 return LY_SUCCESS;
6407 }
6408
6409 /* list of deviates (not-supported is not present in the list) */
6410 LY_ARRAY_FOR(dev->deviates, v) {
6411 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006412 switch (d->mod) {
6413 case LYS_DEV_ADD:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006414 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006415 break;
6416 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006417 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006418 break;
6419 case LYS_DEV_REPLACE:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006420 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006421 break;
6422 default:
6423 LOGINT(ctx->ctx);
6424 goto cleanup;
6425 }
6426 }
6427
6428 /* final check when all deviations of a single target node are applied */
6429
6430 /* check min-max compatibility */
6431 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006432 min = ((struct lysc_node_leaflist *)target)->min;
6433 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006434 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006435 min = ((struct lysc_node_list *)target)->min;
6436 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006437 } else {
6438 min = max = 0;
6439 }
6440 if (min > max) {
6441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6442 "after deviation: min value %u is bigger than max value %u.", min, max);
6443 goto cleanup;
6444 }
6445
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006446 /* check mandatory - default compatibility */
6447 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6448 && (target->flags & LYS_MAND_TRUE)) {
6449 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6450 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6451 goto cleanup;
Michal Vasko22df3f02020-08-24 13:29:22 +02006452 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006453 && (target->flags & LYS_MAND_TRUE)) {
6454 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6455 goto cleanup;
6456 }
6457 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6458 /* mandatory node under a default case */
6459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6460 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6461 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6462 goto cleanup;
6463 }
6464
6465 /* success */
6466 ret = LY_SUCCESS;
6467
6468cleanup:
6469 lysc_update_path(ctx, NULL, NULL);
6470 return ret;
6471}
6472
6473LY_ERR
6474lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6475{
6476 LY_ERR ret = LY_EVALID;
6477 struct ly_set devs_p = {0};
6478 struct ly_set targets = {0};
6479 struct lysc_node *target; /* target target of the deviation */
6480 struct lysp_deviation *dev;
6481 struct lysp_deviate *d, **dp_new;
6482 LY_ARRAY_COUNT_TYPE u, v;
6483 struct lysc_deviation **devs = NULL;
6484 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006485 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006486
6487 /* get all deviations from the module and all its submodules ... */
6488 LY_ARRAY_FOR(mod_p->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006489 LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST, NULL));
Radek Krejciccd20f12019-02-15 14:12:27 +01006490 }
6491 LY_ARRAY_FOR(mod_p->includes, v) {
6492 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006493 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 +01006494 }
6495 }
6496 if (!devs_p.count) {
6497 /* nothing to do */
6498 return LY_SUCCESS;
6499 }
6500
Radek Krejci327de162019-06-14 12:52:07 +02006501 lysc_update_path(ctx, NULL, "{deviation}");
6502
Radek Krejciccd20f12019-02-15 14:12:27 +01006503 /* ... and group them by the target node */
6504 devs = calloc(devs_p.count, sizeof *devs);
6505 for (u = 0; u < devs_p.count; ++u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006506 uint32_t index;
6507
Radek Krejciccd20f12019-02-15 14:12:27 +01006508 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006509 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006510
6511 /* resolve the target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02006512 LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
Michal Vasko22df3f02020-08-24 13:29:22 +02006513 (const struct lysc_node **)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006514 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006515 /* move the target pointer to input/output to make them different from the action and
6516 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6517 * back to the RPC/action node due to a better compatibility and decision code in this function.
6518 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6519 if (flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006520 target = (struct lysc_node *)&((struct lysc_action *)target)->input;
Radek Krejcif538ce52019-03-05 10:46:14 +01006521 flags |= LYSC_OPT_INTERNAL;
6522 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006523 target = (struct lysc_node *)&((struct lysc_action *)target)->output;
Radek Krejcif538ce52019-03-05 10:46:14 +01006524 flags |= LYSC_OPT_INTERNAL;
6525 }
6526 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006527 /* insert into the set of targets with duplicity detection */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006528 ret = ly_set_add(&targets, target, 0, &index);
6529 LY_CHECK_GOTO(ret, cleanup);
6530 if (!devs[index]) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006531 /* new record */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006532 devs[index] = calloc(1, sizeof **devs);
6533 devs[index]->target = target;
6534 devs[index]->nodeid = dev->nodeid;
6535 devs[index]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006536 }
6537 /* add deviates into the deviation's list of deviates */
Michal Vasko5c38f362020-08-24 09:22:51 +02006538 LY_LIST_FOR(dev->deviates, d) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006539 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[index]->deviates, dp_new, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006540 *dp_new = d;
6541 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006542 devs[index]->not_supported = 1;
Radek Krejciccd20f12019-02-15 14:12:27 +01006543 }
6544 }
Radek Krejci327de162019-06-14 12:52:07 +02006545
6546 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006547 }
6548
Radek Krejciccd20f12019-02-15 14:12:27 +01006549 /* apply deviations */
6550 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006551 ly_bool match = 0;
Radek Krejciba03a5a2020-08-27 14:40:41 +02006552
Radek Krejcif538ce52019-03-05 10:46:14 +01006553 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6554 /* fix the target pointer in case of RPC's/action's input/output */
6555 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006556 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006557 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006558 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006559 }
6560 }
6561
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006562 /* remember target module (the target node may be removed) */
6563 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006564
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006565 /* apply the deviation */
6566 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006567
Michal Vaskoe6143202020-07-03 13:02:08 +02006568 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006569 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6570 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006571 match = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006572 break;
6573 }
6574 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02006575 if (!match) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006576 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006577 *dev_mod = mod_p->mod;
6578 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006579 }
6580
Radek Krejci327de162019-06-14 12:52:07 +02006581 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006582 ret = LY_SUCCESS;
6583
6584cleanup:
6585 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6586 LY_ARRAY_FREE(devs[u]->deviates);
6587 free(devs[u]);
6588 }
6589 free(devs);
6590 ly_set_erase(&targets, NULL);
6591 ly_set_erase(&devs_p, NULL);
6592
6593 return ret;
6594}
6595
Radek Krejcib56c7502019-02-13 14:19:54 +01006596/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006597 * @brief Compile the given YANG submodule into the main module.
6598 * @param[in] ctx Compile context
6599 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006600 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6601 */
6602LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006603lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006604{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006605 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006606 LY_ERR ret = LY_SUCCESS;
6607 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006608 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006609 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006610 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006611
Radek Krejci14915cc2020-09-14 17:28:13 +02006612 /* features are compiled directly into the compiled module structure,
6613 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6614 * The features compilation is finished in the main module (lys_compile()). */
6615 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->mod->features);
6616 LY_CHECK_GOTO(ret, error);
Radek Krejci0af46292019-01-11 16:02:31 +01006617
Radek Krejci80d281e2020-09-14 17:42:54 +02006618 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->mod->identities);
6619 LY_CHECK_GOTO(ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006620
Radek Krejci474f9b82019-07-24 11:36:37 +02006621 /* data nodes */
6622 LY_LIST_FOR(submod->data, node_p) {
6623 ret = lys_compile_node(ctx, node_p, NULL, 0);
6624 LY_CHECK_GOTO(ret, error);
6625 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006626
Radek Krejciec4da802019-05-02 13:02:41 +02006627 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6628 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006629
Radek Krejcid05cbd92018-12-05 14:26:40 +01006630error:
6631 return ret;
6632}
6633
Radek Krejci335332a2019-09-05 13:03:35 +02006634static void *
6635lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6636{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006637 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci335332a2019-09-05 13:03:35 +02006638 if (substmts[u].stmt == stmt) {
6639 return substmts[u].storage;
6640 }
6641 }
6642 return NULL;
6643}
6644
6645LY_ERR
6646lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6647{
6648 LY_ERR ret = LY_EVALID, r;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006649 LY_ARRAY_COUNT_TYPE u;
Radek Krejci335332a2019-09-05 13:03:35 +02006650 struct lysp_stmt *stmt;
6651 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006652
6653 /* check for invalid substatements */
6654 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006655 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6656 continue;
6657 }
Radek Krejci335332a2019-09-05 13:03:35 +02006658 for (u = 0; substmts[u].stmt; ++u) {
6659 if (substmts[u].stmt == stmt->kw) {
6660 break;
6661 }
6662 }
6663 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6665 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006666 goto cleanup;
6667 }
Radek Krejci335332a2019-09-05 13:03:35 +02006668 }
6669
Radek Krejciad5963b2019-09-06 16:03:05 +02006670 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6671
Radek Krejci335332a2019-09-05 13:03:35 +02006672 /* keep order of the processing the same as the order in the defined substmts,
6673 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6674 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006675 ly_bool stmt_present = 0;
Radek Krejciad5963b2019-09-06 16:03:05 +02006676
Radek Krejci335332a2019-09-05 13:03:35 +02006677 for (stmt = ext->child; stmt; stmt = stmt->next) {
6678 if (substmts[u].stmt != stmt->kw) {
6679 continue;
6680 }
6681
Radek Krejciad5963b2019-09-06 16:03:05 +02006682 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006683 if (substmts[u].storage) {
6684 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006685 case LY_STMT_STATUS:
6686 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6687 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6688 break;
6689 case LY_STMT_UNITS: {
6690 const char **units;
6691
6692 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6693 /* single item */
6694 if (*((const char **)substmts[u].storage)) {
6695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6696 goto cleanup;
6697 }
6698 units = (const char **)substmts[u].storage;
6699 } else {
6700 /* sized array */
6701 const char ***units_array = (const char ***)substmts[u].storage;
6702 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6703 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02006704 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
6705 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +02006706 break;
6707 }
Radek Krejci335332a2019-09-05 13:03:35 +02006708 case LY_STMT_TYPE: {
6709 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6710 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6711
6712 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6713 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006714 if (*(struct lysc_type **)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6716 goto cleanup;
6717 }
6718 compiled = substmts[u].storage;
6719 } else {
6720 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006721 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006722 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006723 compiled = (void *)type;
Radek Krejci335332a2019-09-05 13:03:35 +02006724 }
6725
Radek Krejciad5963b2019-09-06 16:03:05 +02006726 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006727 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL,
6728 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006729 units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02006730 lysp_type_free(ctx->ctx, parsed);
6731 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006732 break;
6733 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006734 case LY_STMT_IF_FEATURE: {
6735 struct lysc_iffeature *iff = NULL;
6736
6737 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6738 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006739 if (((struct lysc_iffeature *)substmts[u].storage)->features) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6741 goto cleanup;
6742 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006743 iff = (struct lysc_iffeature *)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006744 } else {
6745 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006746 struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006747 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6748 }
6749 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6750 break;
6751 }
6752 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6753 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006754 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006755 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.",
6756 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006757 goto cleanup;
6758 }
6759 }
Radek Krejci335332a2019-09-05 13:03:35 +02006760 }
Radek Krejci335332a2019-09-05 13:03:35 +02006761
Radek Krejciad5963b2019-09-06 16:03:05 +02006762 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6764 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6765 goto cleanup;
6766 }
Radek Krejci335332a2019-09-05 13:03:35 +02006767 }
6768
6769 ret = LY_SUCCESS;
6770
6771cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006772 return ret;
6773}
6774
Michal Vasko175012e2019-11-06 15:49:14 +01006775/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006776 * @brief Check when for cyclic dependencies.
6777 * @param[in] set Set with all the referenced nodes.
6778 * @param[in] node Node whose "when" referenced nodes are in @p set.
6779 * @return LY_ERR value
6780 */
6781static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006782lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006783{
6784 struct lyxp_set tmp_set;
6785 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006786 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006787 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006788 struct lysc_when *when;
6789 LY_ERR ret = LY_SUCCESS;
6790
6791 memset(&tmp_set, 0, sizeof tmp_set);
6792
6793 /* prepare in_ctx of the set */
Michal Vaskod989ba02020-08-24 10:59:24 +02006794 for (i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006795 xp_scnode = &set->val.scnodes[i];
6796
Michal Vasko5c4e5892019-11-14 12:31:38 +01006797 if (xp_scnode->in_ctx != -1) {
6798 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006799 xp_scnode->in_ctx = 1;
6800 }
6801 }
6802
6803 for (i = 0; i < set->used; ++i) {
6804 xp_scnode = &set->val.scnodes[i];
6805 if (xp_scnode->in_ctx != 1) {
6806 /* already checked */
6807 continue;
6808 }
6809
Michal Vasko1bf09392020-03-27 12:38:10 +01006810 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006811 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006812 /* no when to check */
6813 xp_scnode->in_ctx = 0;
6814 continue;
6815 }
6816
6817 node = xp_scnode->scnode;
6818 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006819 LY_ARRAY_FOR(node->when, u) {
6820 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006821 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006822 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6823 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006824 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006825 goto cleanup;
6826 }
6827
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006828 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006829 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006830 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006831 /* try to find this node in our set */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006832 uint32_t idx;
6833 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006834 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 +01006835 ret = LY_EVALID;
6836 goto cleanup;
6837 }
6838
6839 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006840 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006841 } else {
6842 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006843 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006844 }
6845 }
6846
6847 /* merge this set into the global when set */
6848 lyxp_set_scnode_merge(set, &tmp_set);
6849 }
6850
6851 /* check when of non-data parents as well */
6852 node = node->parent;
6853 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6854
Michal Vasko251f56e2019-11-14 16:06:47 +01006855 /* this node when was checked (xp_scnode could have been reallocd) */
6856 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006857 }
6858
6859cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006860 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006861 return ret;
6862}
6863
6864/**
Michal Vasko175012e2019-11-06 15:49:14 +01006865 * @brief Check when/must expressions of a node on a compiled schema tree.
6866 * @param[in] ctx Compile context.
6867 * @param[in] node Node to check.
6868 * @return LY_ERR value
6869 */
6870static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006871lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006872{
Michal Vasko175012e2019-11-06 15:49:14 +01006873 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006874 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006875 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006876 uint32_t opts;
Radek Krejci857189e2020-09-01 13:26:36 +02006877 ly_bool input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006878 struct lysc_when **when = NULL;
6879 struct lysc_must *musts = NULL;
6880 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006881 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006882
6883 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006884 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006885 if (node->flags & LYS_CONFIG_R) {
6886 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6887 if (op) {
6888 /* we are actually in output */
6889 opts = LYXP_SCNODE_OUTPUT;
6890 }
6891 }
Michal Vasko175012e2019-11-06 15:49:14 +01006892
6893 switch (node->nodetype) {
6894 case LYS_CONTAINER:
6895 when = ((struct lysc_node_container *)node)->when;
6896 musts = ((struct lysc_node_container *)node)->musts;
6897 break;
6898 case LYS_CHOICE:
6899 when = ((struct lysc_node_choice *)node)->when;
6900 break;
6901 case LYS_LEAF:
6902 when = ((struct lysc_node_leaf *)node)->when;
6903 musts = ((struct lysc_node_leaf *)node)->musts;
6904 break;
6905 case LYS_LEAFLIST:
6906 when = ((struct lysc_node_leaflist *)node)->when;
6907 musts = ((struct lysc_node_leaflist *)node)->musts;
6908 break;
6909 case LYS_LIST:
6910 when = ((struct lysc_node_list *)node)->when;
6911 musts = ((struct lysc_node_list *)node)->musts;
6912 break;
6913 case LYS_ANYXML:
6914 case LYS_ANYDATA:
6915 when = ((struct lysc_node_anydata *)node)->when;
6916 musts = ((struct lysc_node_anydata *)node)->musts;
6917 break;
6918 case LYS_CASE:
6919 when = ((struct lysc_node_case *)node)->when;
6920 break;
6921 case LYS_NOTIF:
6922 musts = ((struct lysc_notif *)node)->musts;
6923 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006924 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006925 case LYS_ACTION:
6926 /* first process input musts */
6927 musts = ((struct lysc_action *)node)->input.musts;
6928 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006929 default:
6930 /* nothing to check */
6931 break;
6932 }
6933
Michal Vasko175012e2019-11-06 15:49:14 +01006934 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006935 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006936 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 +02006937 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006938 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006939 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006940 goto cleanup;
6941 }
6942
Michal Vaskodc052f32019-11-07 11:11:38 +01006943 ctx->path[0] = '\0';
6944 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006945 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006946 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006947 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6948 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006949
Michal Vaskoecd62de2019-11-13 12:35:11 +01006950 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006951 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006952 schema->name);
6953 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006954
6955 /* check dummy node accessing */
6956 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006957 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006958 ret = LY_EVALID;
6959 goto cleanup;
6960 }
Michal Vasko175012e2019-11-06 15:49:14 +01006961 }
6962 }
6963
Michal Vaskoecd62de2019-11-13 12:35:11 +01006964 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006965 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006966 LY_CHECK_GOTO(ret, cleanup);
6967
Michal Vaskod3678892020-05-21 10:06:58 +02006968 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006969 }
6970
Michal Vasko5d8756a2019-11-07 15:21:00 +01006971check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006972 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006973 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006974 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 +01006975 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006976 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006977 goto cleanup;
6978 }
6979
Michal Vaskodc052f32019-11-07 11:11:38 +01006980 ctx->path[0] = '\0';
6981 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006982 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006983 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006984 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006985 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006986 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6987 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006988 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006989 }
6990 }
6991
Michal Vaskod3678892020-05-21 10:06:58 +02006992 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006993 }
6994
Michal Vasko1bf09392020-03-27 12:38:10 +01006995 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006996 /* now check output musts */
6997 input_done = 1;
6998 musts = ((struct lysc_action *)node)->output.musts;
6999 opts = LYXP_SCNODE_OUTPUT;
7000 goto check_musts;
7001 }
7002
Michal Vasko175012e2019-11-06 15:49:14 +01007003cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007004 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007005 return ret;
7006}
7007
Michal Vasko8d544252020-03-02 10:19:52 +01007008static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007009lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
7010{
Michal Vasko6b26e742020-07-17 15:02:10 +02007011 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02007012 struct ly_path *p;
7013 struct lysc_type *type;
7014
7015 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7016
7017 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02007018 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
7019 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007020 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02007021
7022 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007023 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02007024 ly_path_free(node->module->ctx, p);
7025
7026 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7027 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7028 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
7029 lref->path->expr, lys_nodetype2str(target->nodetype));
7030 return LY_EVALID;
7031 }
7032
7033 /* check status */
7034 ctx->path[0] = '\0';
7035 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7036 ctx->path_len = strlen(ctx->path);
7037 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
7038 return LY_EVALID;
7039 }
7040 ctx->path_len = 1;
7041 ctx->path[1] = '\0';
7042
7043 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007044 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007045 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007046 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007047 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7048 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7049 return LY_EVALID;
7050 }
7051 }
7052
7053 /* store the target's type and check for circular chain of leafrefs */
7054 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7055 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7056 if (type == (struct lysc_type *)lref) {
7057 /* circular chain detected */
7058 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7059 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7060 return LY_EVALID;
7061 }
7062 }
7063
7064 /* check if leafref and its target are under common if-features */
7065 if (lys_compile_leafref_features_validate(node, target)) {
7066 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7067 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7068 " features applicable to the leafref itself.", lref->path->expr);
7069 return LY_EVALID;
7070 }
7071
7072 return LY_SUCCESS;
7073}
7074
7075static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007076lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7077{
7078 struct lysc_ext_instance *ext;
7079 struct lysp_ext_instance *ext_p = NULL;
7080 struct lysp_stmt *stmt;
7081 const struct lys_module *ext_mod;
7082 LY_ERR ret = LY_SUCCESS;
7083
7084 /* create the parsed extension instance manually */
7085 ext_p = calloc(1, sizeof *ext_p);
7086 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007087 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
7088 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007089 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7090 ext_p->insubstmt_index = 0;
7091
Radek Krejci87e25252020-09-15 13:28:31 +02007092 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
7093 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007094 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
7095 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007096 stmt->kw = LY_STMT_TYPE;
Michal Vasko8d544252020-03-02 10:19:52 +01007097
7098 /* allocate new extension instance */
7099 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7100
7101 /* manually get extension definition module */
7102 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7103
7104 /* compile the extension instance */
7105 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7106
7107cleanup:
7108 lysp_ext_instance_free(ctx->ctx, ext_p);
7109 free(ext_p);
7110 return ret;
7111}
7112
Michal Vasko004d3152020-06-11 19:59:22 +02007113static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007114lys_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 +02007115 const struct lys_module *dflt_mod, struct lyd_value *storage)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007116{
7117 LY_ERR ret;
7118 struct ly_err_item *err = NULL;
7119
7120 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7121 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7122 if (err) {
7123 ly_err_print(err);
7124 ctx->path[0] = '\0';
7125 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7127 "Invalid default - value does not fit the type (%s).", err->msg);
7128 ly_err_free(err);
7129 }
7130 if (!ret) {
7131 ++storage->realtype->refcount;
7132 return LY_SUCCESS;
7133 }
7134 return ret;
7135}
7136
7137static LY_ERR
7138lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +02007139 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007140{
7141 LY_ERR ret;
7142
7143 assert(!leaf->dflt);
7144
7145 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7146 /* ignore default values for keys and mandatory leaves */
7147 return LY_SUCCESS;
7148 }
7149
7150 /* allocate the default value */
7151 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7152 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7153
7154 /* store the default value */
7155 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt);
7156 if (ret) {
7157 free(leaf->dflt);
7158 leaf->dflt = NULL;
7159 }
7160
7161 return ret;
7162}
7163
7164static LY_ERR
7165lys_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 +02007166 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007167{
7168 LY_ERR ret;
7169 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7170
7171 assert(dflt || dflts);
7172
7173 if (llist->dflts) {
7174 /* there were already some defaults and we are adding new by deviations */
7175 assert(dflts);
7176 orig_count = LY_ARRAY_COUNT(llist->dflts);
7177 } else {
7178 orig_count = 0;
7179 }
7180
7181 /* allocate new items */
7182 if (dflts) {
7183 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7184 } else {
7185 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7186 }
7187
7188 /* fill each new default value */
7189 if (dflts) {
7190 LY_ARRAY_FOR(dflts, u) {
7191 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
7192 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod,
7193 llist->dflts[orig_count + u]);
7194 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7195 LY_ARRAY_INCREMENT(llist->dflts);
7196 }
7197 } else {
7198 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
7199 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]);
7200 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7201 LY_ARRAY_INCREMENT(llist->dflts);
7202 }
7203
7204 /* check default value uniqueness */
7205 if (llist->flags & LYS_CONFIG_W) {
7206 /* configuration data values must be unique - so check the default values */
7207 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7208 for (v = 0; v < u; ++v) {
7209 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejci857189e2020-09-01 13:26:36 +02007210 ly_bool dynamic = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007211 const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic);
7212
7213 lysc_update_path(ctx, llist->parent, llist->name);
7214 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7215 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
7216 lysc_update_path(ctx, NULL, NULL);
7217 if (dynamic) {
7218 free((char *)val);
7219 }
7220 return LY_EVALID;
7221 }
7222 }
7223 }
7224 }
7225
7226 return LY_SUCCESS;
7227}
7228
7229static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007230lys_compile_unres(struct lysc_ctx *ctx)
7231{
7232 struct lysc_node *node;
7233 struct lysc_type *type, *typeiter;
7234 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007235 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007236 uint32_t i;
7237
7238 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7239 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7240 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7241 for (i = 0; i < ctx->leafrefs.count; ++i) {
7242 node = ctx->leafrefs.objs[i];
7243 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7244 type = ((struct lysc_node_leaf *)node)->type;
7245 if (type->basetype == LY_TYPE_LEAFREF) {
7246 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7247 } else if (type->basetype == LY_TYPE_UNION) {
7248 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7249 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7250 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7251 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7252 }
7253 }
7254 }
7255 }
7256 for (i = 0; i < ctx->leafrefs.count; ++i) {
7257 /* store pointer to the real type */
7258 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7259 if (type->basetype == LY_TYPE_LEAFREF) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007260 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007261 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007262 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7263 ((struct lysc_type_leafref *)type)->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007264 } else if (type->basetype == LY_TYPE_UNION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007265 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7266 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7267 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007268 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007269 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7270 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007271 }
7272 }
7273 }
7274 }
7275
7276 /* check xpath */
7277 for (i = 0; i < ctx->xpath.count; ++i) {
7278 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7279 }
7280
7281 /* finish incomplete default values compilation */
7282 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007283 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007284 if (r->leaf->nodetype == LYS_LEAF) {
7285 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod));
7286 } else {
7287 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 +02007288 }
Michal Vasko004d3152020-06-11 19:59:22 +02007289 }
7290
7291 return LY_SUCCESS;
7292}
7293
Radek Krejci19a96102018-11-15 13:38:09 +01007294LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02007295lys_compile(struct lys_module *mod, uint32_t options)
Radek Krejci19a96102018-11-15 13:38:09 +01007296{
7297 struct lysc_ctx ctx = {0};
7298 struct lysc_module *mod_c;
7299 struct lysp_module *sp;
7300 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007301 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007302 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007303 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007304 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007305 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007306 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007307 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007308
Michal Vasko7a0b0762020-09-02 16:37:01 +02007309 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007310
Michal Vasko7a0b0762020-09-02 16:37:01 +02007311 if (!mod->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007312 /* just imported modules are not compiled */
7313 return LY_SUCCESS;
7314 }
7315
Michal Vasko7a0b0762020-09-02 16:37:01 +02007316 compile_id = ++mod->ctx->module_set_id;
7317 sp = mod->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007318
Michal Vasko7a0b0762020-09-02 16:37:01 +02007319 ctx.ctx = mod->ctx;
7320 ctx.mod = mod;
7321 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007322 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007323 ctx.path_len = 1;
7324 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007325
Michal Vasko7a0b0762020-09-02 16:37:01 +02007326 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
7327 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7328 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007329
Michal Vasko7c8439f2020-08-05 13:25:19 +02007330 LY_ARRAY_FOR(sp->imports, u) {
7331 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7332 }
Radek Krejci0935f412019-08-20 16:15:18 +02007333
Radek Krejci14915cc2020-09-14 17:28:13 +02007334 /* features precompilation */
7335 if (!mod->features && sp->features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007336 /* features are compiled directly into the compiled module structure,
7337 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci14915cc2020-09-14 17:28:13 +02007338 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007339 LY_CHECK_GOTO(ret, error);
Radek Krejci14915cc2020-09-14 17:28:13 +02007340 } /* else the features are already precompiled */
Michal Vasko33ff9422020-07-03 09:50:39 +02007341
Radek Krejci14915cc2020-09-14 17:28:13 +02007342 /* similarly, identities precompilation */
Radek Krejci80d281e2020-09-14 17:42:54 +02007343 if (!mod->identities && sp->identities) {
7344 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod->identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02007345 LY_CHECK_GOTO(ret, error);
7346 }
Radek Krejci14915cc2020-09-14 17:28:13 +02007347
7348 /* compile submodules
7349 * - must be between features/identities precompilation and finishing their compilation to cover features/identities from
7350 * submodules */
7351 LY_ARRAY_FOR(sp->includes, u) {
7352 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
7353 }
7354
7355 /* finish feature compilation, not only for the main module, but also for the submodules.
7356 * Due to possible forward references, it must be done when all the features (including submodules)
7357 * are present. */
7358 LY_ARRAY_FOR(sp->features, u) {
7359 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod->features);
7360 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7361 }
7362 lysc_update_path(&ctx, NULL, "{submodule}");
7363 LY_ARRAY_FOR(sp->includes, v) {
7364 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7365 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
7366 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod->features);
7367 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7368 }
7369 lysc_update_path(&ctx, NULL, NULL);
7370 }
7371 lysc_update_path(&ctx, NULL, NULL);
7372
7373 /* identities, work similarly to features with the precompilation */
Radek Krejci19a96102018-11-15 13:38:09 +01007374 if (sp->identities) {
Radek Krejci80d281e2020-09-14 17:42:54 +02007375 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007376 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007377 lysc_update_path(&ctx, NULL, "{submodule}");
7378 LY_ARRAY_FOR(sp->includes, v) {
7379 if (sp->includes[v].submodule->identities) {
7380 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci80d281e2020-09-14 17:42:54 +02007381 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod->identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02007382 LY_CHECK_GOTO(ret, error);
7383 lysc_update_path(&ctx, NULL, NULL);
7384 }
7385 }
Radek Krejci327de162019-06-14 12:52:07 +02007386 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007387
Radek Krejci95710c92019-02-11 15:49:55 +01007388 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007389 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007390 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007391 }
Radek Krejci95710c92019-02-11 15:49:55 +01007392
Radek Krejciec4da802019-05-02 13:02:41 +02007393 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7394 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007395
Radek Krejci95710c92019-02-11 15:49:55 +01007396 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007397 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007398 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007399 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007400 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007401
Radek Krejci474f9b82019-07-24 11:36:37 +02007402 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007403 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007404
Radek Krejci0935f412019-08-20 16:15:18 +02007405 /* extension instances TODO cover extension instances from submodules */
7406 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007407
Michal Vasko004d3152020-06-11 19:59:22 +02007408 /* finish compilation for all unresolved items in the context */
7409 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007410
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007411 /* validate non-instantiated groupings from the parsed schema,
7412 * without it we would accept even the schemas with invalid grouping specification */
7413 ctx.options |= LYSC_OPT_GROUPING;
7414 LY_ARRAY_FOR(sp->groupings, u) {
7415 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007416 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007417 }
7418 }
7419 LY_LIST_FOR(sp->data, node_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007420 grps = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007421 LY_ARRAY_FOR(grps, u) {
7422 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007423 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007424 }
7425 }
7426 }
7427
Radek Krejci474f9b82019-07-24 11:36:37 +02007428 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7429 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7430 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7431 }
7432
Michal Vasko8d544252020-03-02 10:19:52 +01007433#if 0
7434 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7435 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7436 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7437 * the anotation definitions available in the internal schema structure. */
7438 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7439 if (lyp_add_ietf_netconf_annotations(mod)) {
7440 lys_free(mod, NULL, 1, 1);
7441 return NULL;
7442 }
7443 }
7444#endif
7445
7446 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007447 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
7448 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007449 }
7450
Radek Krejci1c0c3442019-07-23 16:08:47 +02007451 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007452 ly_set_erase(&ctx.xpath, NULL);
7453 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007454 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007455 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007456 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007457
Radek Krejciec4da802019-05-02 13:02:41 +02007458 if (ctx.options & LYSC_OPT_FREE_SP) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02007459 lysp_module_free(mod->parsed);
7460 mod->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007461 }
7462
Radek Krejciec4da802019-05-02 13:02:41 +02007463 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007464 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007465 for (i = 0; i < ctx.ctx->list.count; ++i) {
7466 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007467 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007468 m->implemented = 1;
7469 }
7470 }
7471 }
7472
Radek Krejci19a96102018-11-15 13:38:09 +01007473 return LY_SUCCESS;
7474
7475error:
Michal Vasko7a0b0762020-09-02 16:37:01 +02007476 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007477 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007478 ly_set_erase(&ctx.xpath, NULL);
7479 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007480 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007481 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007482 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007483 lysc_module_free(mod_c, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02007484 mod->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007485
Radek Krejcia46012b2020-08-12 15:41:04 +02007486 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7487 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7488 * 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 +02007489 for (i = 0; i < ctx.ctx->list.count; ++i) {
7490 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007491 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007492 /* revert features list to the precompiled state */
7493 lys_feature_precompile_revert(&ctx, m);
7494 /* mark module as imported-only / not-implemented */
7495 m->implemented = 0;
7496 /* free the compiled version of the module */
7497 lysc_module_free(m->compiled, NULL);
7498 m->compiled = NULL;
7499 }
7500 }
7501
Radek Krejci19a96102018-11-15 13:38:09 +01007502 return ret;
7503}