blob: 331fc29d8896323dbc492138d79fa246f36ffc42 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Michal Vasko7c8439f2020-08-05 13:25:19 +020039#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010041#include "tree_schema_internal.h"
42#include "xpath.h"
43
Michal Vasko5fe75f12020-03-02 13:52:37 +010044static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
Radek Krejci0f969882020-08-21 16:56:47 +020045 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
Michal Vasko5fe75f12020-03-02 13:52:37 +010046
Radek Krejci19a96102018-11-15 13:38:09 +010047/**
48 * @brief Duplicate string into dictionary
49 * @param[in] CTX libyang context of the dictionary.
50 * @param[in] ORIG String to duplicate.
51 * @param[out] DUP Where to store the result.
52 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020053#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
54
55#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
Radek Krejci19a96102018-11-15 13:38:09 +010056
Radek Krejciec4da802019-05-02 13:02:41 +020057#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010058 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020059 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
60 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
61 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020063 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010064 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
65 } \
66 }
67
Radek Krejciec4da802019-05-02 13:02:41 +020068#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010069 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020070 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
71 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
72 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010073 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020074 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010075 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
76 } \
77 }
78
Radek Krejci0935f412019-08-20 16:15:18 +020079#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
80 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020081 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
82 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020083 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010084 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020085 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
86 } \
87 }
88
Radek Krejciec4da802019-05-02 13:02:41 +020089#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010090 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020091 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
92 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
93 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010094 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020095 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010096 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
97 } \
98 }
99
Radek Krejciec4da802019-05-02 13:02:41 +0200100#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +0100101 if (MEMBER_P) { \
102 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
103 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200104 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100105 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
106 }
107
Radek Krejciec4da802019-05-02 13:02:41 +0200108#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 if (MEMBER_P) { \
110 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200111 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100112 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200113 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100114 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
115 }
116
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100117#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200119 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100120 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100121 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
122 return LY_EVALID; \
123 } \
124 } \
125 }
126
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
128 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200129 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100130 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
131 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
132 return LY_EVALID; \
133 } \
134 } \
135 }
136
137struct lysc_ext *
138lysc_ext_dup(struct lysc_ext *orig)
139{
140 ++orig->refcount;
141 return orig;
142}
143
Radek Krejci19a96102018-11-15 13:38:09 +0100144static struct lysc_ext_instance *
145lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
146{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100147 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100148 (void) ctx;
149 (void) orig;
150 return NULL;
151}
152
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200153static LY_ERR
154lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +0200155 struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200156{
157 struct lysc_incomplete_dflt *r;
158 uint32_t i;
159
160 for (i = 0; i < ctx->dflts.count; ++i) {
161 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
162 if (r->leaf == leaf) {
163 /* just replace the default */
164 r->dflt = dflt;
165 return LY_SUCCESS;
166 }
167 }
168
169 r = malloc(sizeof *r);
170 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
171 r->leaf = leaf;
172 r->dflt = dflt;
173 r->dflts = NULL;
174 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200175 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200176
177 return LY_SUCCESS;
178}
179
Radek Krejcib56c7502019-02-13 14:19:54 +0100180/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200181 * @brief Add record into the compile context's list of incomplete default values.
182 * @param[in] ctx Compile context with the incomplete default values list.
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200183 * @param[in] term Term context node with the default value.
184 * @param[in] value String default value.
185 * @param[in] val_len Length of @p value.
Radek Krejci474f9b82019-07-24 11:36:37 +0200186 * @param[in] dflt_mod Module of the default value definition to store in the record.
187 * @return LY_EMEM in case of memory allocation failure.
188 * @return LY_SUCCESS
189 */
190static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200191lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts,
Radek Krejci0f969882020-08-21 16:56:47 +0200192 struct lys_module *dflt_mod)
Radek Krejci474f9b82019-07-24 11:36:37 +0200193{
194 struct lysc_incomplete_dflt *r;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200195 uint32_t i;
196
197 for (i = 0; i < ctx->dflts.count; ++i) {
198 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
199 if (r->llist == llist) {
200 /* just replace the defaults */
201 r->dflts = dflts;
202 return LY_SUCCESS;
203 }
204 }
205
Radek Krejci474f9b82019-07-24 11:36:37 +0200206 r = malloc(sizeof *r);
207 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200208 r->llist = llist;
209 r->dflt = NULL;
210 r->dflts = dflts;
Radek Krejci474f9b82019-07-24 11:36:37 +0200211 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200212 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Radek Krejci474f9b82019-07-24 11:36:37 +0200213
214 return LY_SUCCESS;
215}
216
217/**
218 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
219 * @param[in] ctx Compile context with the incomplete default values list.
220 * @param[in] dflt Incomplete default values identifying the record to remove.
221 */
222static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200223lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term)
Radek Krejci474f9b82019-07-24 11:36:37 +0200224{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200225 uint32_t u;
Radek Krejci474f9b82019-07-24 11:36:37 +0200226 struct lysc_incomplete_dflt *r;
227
228 for (u = 0; u < ctx->dflts.count; ++u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200229 r = ctx->dflts.objs[u];
230 if (r->leaf == (struct lysc_node_leaf *)term) {
Radek Krejci474f9b82019-07-24 11:36:37 +0200231 free(ctx->dflts.objs[u]);
232 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
233 --ctx->dflts.count;
234 return;
235 }
236 }
237}
238
Radek Krejci0e59c312019-08-15 15:34:15 +0200239void
Radek Krejci327de162019-06-14 12:52:07 +0200240lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
241{
242 int len;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200243 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
Radek Krejci327de162019-06-14 12:52:07 +0200244
245 if (!name) {
246 /* removing last path segment */
247 if (ctx->path[ctx->path_len - 1] == '}') {
Michal Vaskod989ba02020-08-24 10:59:24 +0200248 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200249 if (ctx->path[ctx->path_len] == '=') {
250 ctx->path[ctx->path_len++] = '}';
251 } else {
252 /* not a top-level special tag, remove also preceiding '/' */
253 goto remove_nodelevel;
254 }
255 } else {
256remove_nodelevel:
Michal Vaskod989ba02020-08-24 10:59:24 +0200257 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200258 if (ctx->path_len == 0) {
259 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200260 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200261 }
262 }
263 /* set new terminating NULL-byte */
264 ctx->path[ctx->path_len] = '\0';
265 } else {
266 if (ctx->path_len > 1) {
267 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
268 /* extension of the special tag */
269 nextlevel = 2;
270 --ctx->path_len;
271 } else {
272 /* there is already some path, so add next level */
273 nextlevel = 1;
274 }
275 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
276
277 if (nextlevel != 2) {
278 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
279 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200280 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200281 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200282 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200283 }
284 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200285 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200286 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200287 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
Radek Krejciacc79042019-07-25 14:14:57 +0200288 /* output truncated */
289 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
290 } else {
291 ctx->path_len += len;
292 }
Radek Krejci327de162019-06-14 12:52:07 +0200293 }
294}
295
296/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100297 * @brief Duplicate the compiled pattern structure.
298 *
299 * Instead of duplicating memory, the reference counter in the @p orig is increased.
300 *
301 * @param[in] orig The pattern structure to duplicate.
302 * @return The duplicated structure to use.
303 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200304static struct lysc_pattern *
Radek Krejci19a96102018-11-15 13:38:09 +0100305lysc_pattern_dup(struct lysc_pattern *orig)
306{
307 ++orig->refcount;
308 return orig;
309}
310
Radek Krejcib56c7502019-02-13 14:19:54 +0100311/**
312 * @brief Duplicate the array of compiled patterns.
313 *
314 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
315 *
316 * @param[in] ctx Libyang context for logging.
317 * @param[in] orig The patterns sized array to duplicate.
318 * @return New sized array as a copy of @p orig.
319 * @return NULL in case of memory allocation error.
320 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200321static struct lysc_pattern **
Radek Krejci19a96102018-11-15 13:38:09 +0100322lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
323{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100324 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200325 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327 assert(orig);
328
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200329 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100330 LY_ARRAY_FOR(orig, u) {
331 dup[u] = lysc_pattern_dup(orig[u]);
332 LY_ARRAY_INCREMENT(dup);
333 }
334 return dup;
335}
336
Radek Krejcib56c7502019-02-13 14:19:54 +0100337/**
338 * @brief Duplicate compiled range structure.
339 *
340 * @param[in] ctx Libyang context for logging.
341 * @param[in] orig The range structure to be duplicated.
342 * @return New compiled range structure as a copy of @p orig.
343 * @return NULL in case of memory allocation error.
344 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200345struct lysc_range *
Radek Krejci19a96102018-11-15 13:38:09 +0100346lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
347{
348 struct lysc_range *dup;
349 LY_ERR ret;
350
Radek Krejcib56c7502019-02-13 14:19:54 +0100351 assert(orig);
352
Radek Krejci19a96102018-11-15 13:38:09 +0100353 dup = calloc(1, sizeof *dup);
354 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
355 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200356 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
357 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
358 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100359 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200360 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
361 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100362 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
363
364 return dup;
365cleanup:
366 free(dup);
367 (void) ret; /* set but not used due to the return type */
368 return NULL;
369}
370
Radek Krejcib56c7502019-02-13 14:19:54 +0100371/**
372 * @brief Stack for processing if-feature expressions.
373 */
Radek Krejci19a96102018-11-15 13:38:09 +0100374struct iff_stack {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200375 size_t size; /**< number of items in the stack */
376 size_t index; /**< first empty item */
377 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100378};
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Add @ref ifftokens into the stack.
382 * @param[in] stack The if-feature stack to use.
383 * @param[in] value One of the @ref ifftokens to store in the stack.
384 * @return LY_EMEM in case of memory allocation error
385 * @return LY_ESUCCESS if the value successfully stored.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static LY_ERR
388iff_stack_push(struct iff_stack *stack, uint8_t value)
389{
390 if (stack->index == stack->size) {
391 stack->size += 4;
392 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
393 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
394 }
395 stack->stack[stack->index++] = value;
396 return LY_SUCCESS;
397}
398
Radek Krejcib56c7502019-02-13 14:19:54 +0100399/**
400 * @brief Get (and remove) the last item form the stack.
401 * @param[in] stack The if-feature stack to use.
402 * @return The value from the top of the stack.
403 */
Radek Krejci19a96102018-11-15 13:38:09 +0100404static uint8_t
405iff_stack_pop(struct iff_stack *stack)
406{
Radek Krejcib56c7502019-02-13 14:19:54 +0100407 assert(stack && stack->index);
408
Radek Krejci19a96102018-11-15 13:38:09 +0100409 stack->index--;
410 return stack->stack[stack->index];
411}
412
Radek Krejcib56c7502019-02-13 14:19:54 +0100413/**
414 * @brief Clean up the stack.
415 * @param[in] stack The if-feature stack to use.
416 */
Radek Krejci19a96102018-11-15 13:38:09 +0100417static void
418iff_stack_clean(struct iff_stack *stack)
419{
420 stack->size = 0;
421 free(stack->stack);
422}
423
Radek Krejcib56c7502019-02-13 14:19:54 +0100424/**
425 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
426 * (libyang format of the if-feature expression).
427 * @param[in,out] list The 2bits array to modify.
428 * @param[in] op The operand (@ref ifftokens) to store.
429 * @param[in] pos Position (0-based) where to store the given @p op.
430 */
Radek Krejci19a96102018-11-15 13:38:09 +0100431static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200432iff_setop(uint8_t *list, uint8_t op, size_t pos)
Radek Krejci19a96102018-11-15 13:38:09 +0100433{
434 uint8_t *item;
435 uint8_t mask = 3;
436
Radek Krejci19a96102018-11-15 13:38:09 +0100437 assert(op <= 3); /* max 2 bits */
438
439 item = &list[pos / 4];
440 mask = mask << 2 * (pos % 4);
441 *item = (*item) & ~mask;
442 *item = (*item) | (op << 2 * (pos % 4));
443}
444
Radek Krejcib56c7502019-02-13 14:19:54 +0100445#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
446#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100447
Radek Krejci0af46292019-01-11 16:02:31 +0100448/**
449 * @brief Find a feature of the given name and referenced in the given module.
450 *
451 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
452 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
453 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
454 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
455 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
456 * assigned till that time will be still valid.
457 *
458 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
459 * @param[in] name Name of the feature including possible prefix.
460 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
461 * @return Pointer to the feature structure if found, NULL otherwise.
462 */
Radek Krejci19a96102018-11-15 13:38:09 +0100463static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100464lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100465{
466 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200467 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100468 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100469
Radek Krejci120d8542020-08-12 09:29:16 +0200470 assert(mod);
471
Radek Krejci19a96102018-11-15 13:38:09 +0100472 for (i = 0; i < len; ++i) {
473 if (name[i] == ':') {
474 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100475 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100476 LY_CHECK_RET(!mod, NULL);
477
478 name = &name[i + 1];
479 len = len - i - 1;
480 }
481 }
482
483 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100484 if (mod->implemented) {
485 /* module is implemented so there is already the compiled schema */
486 flist = mod->compiled->features;
487 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200488 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100489 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200490 LY_ARRAY_FOR(flist, u) {
491 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200492 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100493 return f;
494 }
495 }
496
497 return NULL;
498}
499
Michal Vasko8d544252020-03-02 10:19:52 +0100500/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100501 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
502 */
503static LY_ERR
504lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
505{
506 LY_ERR ret = LY_SUCCESS;
507
508 if (!ext_p->compiled) {
509 lysc_update_path(ctx, NULL, "{extension}");
510 lysc_update_path(ctx, NULL, ext_p->name);
511
512 /* compile the extension definition */
513 ext_p->compiled = calloc(1, sizeof **ext);
514 ext_p->compiled->refcount = 1;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200515 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
516 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100517 ext_p->compiled->module = (struct lys_module *)ext_mod;
518 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
519
520 lysc_update_path(ctx, NULL, NULL);
521 lysc_update_path(ctx, NULL, NULL);
522
523 /* find extension definition plugin */
524 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
525 }
526
527 *ext = lysc_ext_dup(ext_p->compiled);
528
529done:
530 return ret;
531}
532
533/**
Michal Vasko8d544252020-03-02 10:19:52 +0100534 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
535 *
536 * @param[in] ctx Compilation context.
537 * @param[in] ext_p Parsed extension instance.
538 * @param[in,out] ext Prepared compiled extension instance.
539 * @param[in] parent Extension instance parent.
540 * @param[in] parent_type Extension instance parent type.
541 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
542 */
Radek Krejci19a96102018-11-15 13:38:09 +0100543static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100544lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200545 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100546{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200547 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100548 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200549 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200550 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200551 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100552
Radek Krejci011e4aa2020-09-04 15:22:31 +0200553 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
554 LY_CHECK_RET(ret);
555
Radek Krejci19a96102018-11-15 13:38:09 +0100556 ext->insubstmt = ext_p->insubstmt;
557 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200558 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200559 ext->parent = parent;
560 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100561
Michal Vasko22df3f02020-08-24 13:29:22 +0200562 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200563
Radek Krejci19a96102018-11-15 13:38:09 +0100564 /* get module where the extension definition should be placed */
Radek Krejci1e008d22020-08-17 11:37:37 +0200565 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
Radek Krejci7c960162019-09-18 14:16:12 +0200566 if (ext_p->yin) {
567 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
568 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
569 * YANG (import) prefix must be done here. */
570 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200571 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200572 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200573 } else {
574 assert(ctx->mod_def->parsed);
575 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
576 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200577 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200578 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1,
Radek Krejci200f1062020-07-11 22:51:03 +0200579 ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200580 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200581 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200582 break;
583 }
584 }
585 }
586 if (!prefixed_name) {
587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
588 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200589 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200590 goto cleanup;
591 }
592 } else {
593 prefixed_name = ext_p->name;
594 }
595 lysc_update_path(ctx, NULL, prefixed_name);
596
Michal Vasko8d544252020-03-02 10:19:52 +0100597 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200598 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100599 if (!ext_mod) {
600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
601 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200602 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100603 goto cleanup;
604 } else if (!ext_mod->parsed->extensions) {
605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
606 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
607 prefixed_name, ext_mod->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200608 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100609 goto cleanup;
610 }
Radek Krejci7c960162019-09-18 14:16:12 +0200611 }
612 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200613
Michal Vasko5fe75f12020-03-02 13:52:37 +0100614 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200615 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
616 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100617 /* compile extension definition and assign it */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200618 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100619 break;
620 }
621 }
Radek Krejci7c960162019-09-18 14:16:12 +0200622 if (!ext->def) {
623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
624 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200625 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200626 goto cleanup;
627 }
Radek Krejci0935f412019-08-20 16:15:18 +0200628
Radek Krejcif56e2a42019-09-09 14:15:25 +0200629 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
630 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
631 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200632 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200633 /* Schema was parsed from YIN and an argument is expected, ... */
634 struct lysp_stmt *stmt = NULL;
635
636 if (ext->def->flags & LYS_YINELEM_TRUE) {
637 /* ... argument was the first XML child element */
638 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
639 /* TODO check namespace of the statement */
640 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
641 stmt = ext_p->child;
642 }
643 }
644 } else {
645 /* ... argument was one of the XML attributes which are represented as child stmt
646 * with LYS_YIN_ATTR flag */
647 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
648 if (!strcmp(stmt->stmt, ext->def->argument)) {
649 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200650 break;
651 }
652 }
653 }
654 if (!stmt) {
655 /* missing extension's argument */
656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200657 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200658 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200659 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200660
661 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200662 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200663 stmt->flags |= LYS_YIN_ARGUMENT;
664 }
Radek Krejci7c960162019-09-18 14:16:12 +0200665 if (prefixed_name != ext_p->name) {
666 lydict_remove(ctx->ctx, ext_p->name);
667 ext_p->name = prefixed_name;
668 if (!ext_p->argument && ext->argument) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200669 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200670 }
671 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200672
Radek Krejci0935f412019-08-20 16:15:18 +0200673 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200674 if (ext->argument) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200675 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
Radek Krejciad5963b2019-09-06 16:03:05 +0200676 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200677 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200678 if (ext->argument) {
679 lysc_update_path(ctx, NULL, NULL);
680 }
Radek Krejci0935f412019-08-20 16:15:18 +0200681 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200682 ext_p->compiled = ext;
683
Radek Krejci7c960162019-09-18 14:16:12 +0200684cleanup:
685 if (prefixed_name && prefixed_name != ext_p->name) {
686 lydict_remove(ctx->ctx, prefixed_name);
687 }
688
Radek Krejcif56e2a42019-09-09 14:15:25 +0200689 lysc_update_path(ctx, NULL, NULL);
690 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200691
Radek Krejci7c960162019-09-18 14:16:12 +0200692 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200693}
694
695/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100696 * @brief Compile information from the if-feature statement
697 * @param[in] ctx Compile context.
698 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100699 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
700 * @return LY_ERR value.
701 */
Radek Krejci19a96102018-11-15 13:38:09 +0100702static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200703lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100704{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200705 LY_ERR rc = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100706 const char *c = *value;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200707 int64_t i, j;
708 int8_t op_len, last_not = 0, checkversion = 0;
709 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci19a96102018-11-15 13:38:09 +0100710 uint8_t op;
711 struct iff_stack stack = {0, 0, NULL};
712 struct lysc_feature *f;
713
714 assert(c);
715
716 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200717 for (i = j = 0; c[i]; i++) {
Radek Krejci19a96102018-11-15 13:38:09 +0100718 if (c[i] == '(') {
719 j++;
720 checkversion = 1;
721 continue;
722 } else if (c[i] == ')') {
723 j--;
724 continue;
725 } else if (isspace(c[i])) {
726 checkversion = 1;
727 continue;
728 }
729
Radek Krejci1deb5be2020-08-26 16:43:36 +0200730 if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
731 uint64_t spaces;
732 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++);
733 if (c[i + op_len + spaces] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
735 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
736 return LY_EVALID;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200737 } else if (!isspace(c[i + op_len])) {
Radek Krejci19a96102018-11-15 13:38:09 +0100738 /* feature name starting with the not/and/or */
739 last_not = 0;
740 f_size++;
741 } else if (c[i] == 'n') { /* not operation */
742 if (last_not) {
743 /* double not */
744 expr_size = expr_size - 2;
745 last_not = 0;
746 } else {
747 last_not = 1;
748 }
749 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200750 if (f_exp != f_size) {
751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200752 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, op_len, &c[i]);
Radek Krejci6788abc2019-06-14 13:56:49 +0200753 return LY_EVALID;
754 }
Radek Krejci19a96102018-11-15 13:38:09 +0100755 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200756
Radek Krejci19a96102018-11-15 13:38:09 +0100757 /* not a not operation */
758 last_not = 0;
759 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200760 i += op_len;
Radek Krejci19a96102018-11-15 13:38:09 +0100761 } else {
762 f_size++;
763 last_not = 0;
764 }
765 expr_size++;
766
767 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200768 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100769 i--;
770 break;
771 }
772 i++;
773 }
774 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200775 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100776 /* not matching count of ( and ) */
777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
778 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
779 return LY_EVALID;
780 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200781 if (f_exp != f_size) {
782 /* features do not match the needed arguments for the logical operations */
783 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
784 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
785 "the required number of operands for the operations.", *value);
786 return LY_EVALID;
787 }
Radek Krejci19a96102018-11-15 13:38:09 +0100788
789 if (checkversion || expr_size > 1) {
790 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
793 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
794 return LY_EVALID;
795 }
796 }
797
798 /* allocate the memory */
799 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
800 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
801 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200802 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
Radek Krejci19a96102018-11-15 13:38:09 +0100803
804 stack.size = expr_size;
805 f_size--; expr_size--; /* used as indexes from now */
806
807 for (i--; i >= 0; i--) {
808 if (c[i] == ')') {
809 /* push it on stack */
810 iff_stack_push(&stack, LYS_IFF_RP);
811 continue;
812 } else if (c[i] == '(') {
813 /* pop from the stack into result all operators until ) */
Michal Vaskod989ba02020-08-24 10:59:24 +0200814 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
Radek Krejci19a96102018-11-15 13:38:09 +0100815 iff_setop(iff->expr, op, expr_size--);
816 }
817 continue;
818 } else if (isspace(c[i])) {
819 continue;
820 }
821
822 /* end of operator or operand -> find beginning and get what is it */
823 j = i + 1;
824 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
825 i--;
826 }
827 i++; /* go back by one step */
828
829 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
830 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
831 /* double not */
832 iff_stack_pop(&stack);
833 } else {
834 /* not has the highest priority, so do not pop from the stack
835 * as in case of AND and OR */
836 iff_stack_push(&stack, LYS_IFF_NOT);
837 }
838 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
839 /* as for OR - pop from the stack all operators with the same or higher
840 * priority and store them to the result, then push the AND to the stack */
841 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
842 op = iff_stack_pop(&stack);
843 iff_setop(iff->expr, op, expr_size--);
844 }
845 iff_stack_push(&stack, LYS_IFF_AND);
846 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
847 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
848 op = iff_stack_pop(&stack);
849 iff_setop(iff->expr, op, expr_size--);
850 }
851 iff_stack_push(&stack, LYS_IFF_OR);
852 } else {
853 /* feature name, length is j - i */
854
855 /* add it to the expression */
856 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
857
858 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100859 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
860 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
861 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
862 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100863 iff->features[f_size] = f;
864 LY_ARRAY_INCREMENT(iff->features);
865 f_size--;
866 }
867 }
868 while (stack.index) {
869 op = iff_stack_pop(&stack);
870 iff_setop(iff->expr, op, expr_size--);
871 }
872
873 if (++expr_size || ++f_size) {
874 /* not all expected operators and operands found */
875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
876 "Invalid value \"%s\" of if-feature - processing error.", *value);
877 rc = LY_EINT;
878 } else {
879 rc = LY_SUCCESS;
880 }
881
882error:
883 /* cleanup */
884 iff_stack_clean(&stack);
885
886 return rc;
887}
888
Radek Krejcib56c7502019-02-13 14:19:54 +0100889/**
Michal Vasko175012e2019-11-06 15:49:14 +0100890 * @brief Get the XPath context node for the given schema node.
891 * @param[in] start The schema node where the XPath expression appears.
892 * @return The context node to evaluate XPath expression in given schema node.
893 * @return NULL in case the context node is the root node.
894 */
895static struct lysc_node *
896lysc_xpath_context(struct lysc_node *start)
897{
Michal Vasko1bf09392020-03-27 12:38:10 +0100898 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Radek Krejci1e008d22020-08-17 11:37:37 +0200899 start = start->parent) {}
Michal Vasko175012e2019-11-06 15:49:14 +0100900 return start;
901}
902
903/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100904 * @brief Compile information from the when statement
905 * @param[in] ctx Compile context.
906 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100907 * @param[in] flags Flags of the node with the "when" defiition.
908 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100909 * @param[out] when Pointer where to store pointer to the created compiled when structure.
910 * @return LY_ERR value.
911 */
Radek Krejci19a96102018-11-15 13:38:09 +0100912static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100913lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100914{
Radek Krejci19a96102018-11-15 13:38:09 +0100915 LY_ERR ret = LY_SUCCESS;
916
Radek Krejci00b874b2019-02-12 10:54:50 +0100917 *when = calloc(1, sizeof **when);
918 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200919 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200920 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100921 (*when)->context = lysc_xpath_context(node);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200922 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
923 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejci00b874b2019-02-12 10:54:50 +0100924 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200925 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100926 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100927
928done:
929 return ret;
930}
931
Radek Krejcib56c7502019-02-13 14:19:54 +0100932/**
933 * @brief Compile information from the must statement
934 * @param[in] ctx Compile context.
935 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100936 * @param[in,out] must Prepared (empty) compiled must structure to fill.
937 * @return LY_ERR value.
938 */
Radek Krejci19a96102018-11-15 13:38:09 +0100939static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200940lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100941{
Radek Krejci19a96102018-11-15 13:38:09 +0100942 LY_ERR ret = LY_SUCCESS;
943
Michal Vasko004d3152020-06-11 19:59:22 +0200944 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100945 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200946 must->module = ctx->mod_def;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200947 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
948 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
949 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
950 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200951 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100952
953done:
954 return ret;
955}
956
Radek Krejcib56c7502019-02-13 14:19:54 +0100957/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200958 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100959 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200960 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100961 * @return LY_ERR value.
962 */
Radek Krejci19a96102018-11-15 13:38:09 +0100963static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200964lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100965{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200966 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100967 LY_ERR ret = LY_SUCCESS;
968
Radek Krejci7f2a5362018-11-28 13:05:37 +0100969 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
970 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100971 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200972 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100973 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200974 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200975 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200976 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200977 LOGINT(ctx->ctx);
978 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200979 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
Michal Vasko3a41dff2020-07-15 14:30:28 +0200980 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200981 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200982 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200983 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200984 mod = NULL;
985 }
Radek Krejci19a96102018-11-15 13:38:09 +0100986 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200987 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100988 }
989 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200990 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100991 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200992 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100993 return LY_ENOTFOUND;
994 }
995 }
Radek Krejci19a96102018-11-15 13:38:09 +0100996 }
997
Radek Krejci19a96102018-11-15 13:38:09 +0100998 return ret;
999}
1000
Michal Vasko33ff9422020-07-03 09:50:39 +02001001LY_ERR
1002lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001003 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +01001004{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001005 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +02001006 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01001007 LY_ERR ret = LY_SUCCESS;
1008
Michal Vasko33ff9422020-07-03 09:50:39 +02001009 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +02001010
Michal Vasko33ff9422020-07-03 09:50:39 +02001011 if (!ctx_sc) {
1012 context.ctx = ctx;
1013 context.mod = module;
Radek Krejci120d8542020-08-12 09:29:16 +02001014 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +02001015 context.path_len = 1;
1016 context.path[0] = '/';
1017 ctx_sc = &context;
1018 }
Radek Krejci19a96102018-11-15 13:38:09 +01001019
Michal Vasko33ff9422020-07-03 09:50:39 +02001020 if (!identities_p) {
1021 return LY_SUCCESS;
1022 }
1023 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001024 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02001025 }
1026
1027 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001028 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001029 LY_ARRAY_FOR(identities_p, u) {
1030 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
1031
1032 LY_ARRAY_INCREMENT(*identities);
1033 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001034 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
1035 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
1036 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
Michal Vasko33ff9422020-07-03 09:50:39 +02001037 (*identities)[offset + u].module = ctx_sc->mod;
1038 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
1039 lys_compile_iffeature, ret, done);
1040 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
1041 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
1042 LYEXT_PAR_IDENT, ret, done);
1043 (*identities)[offset + u].flags = identities_p[u].flags;
1044
1045 lysc_update_path(ctx_sc, NULL, NULL);
1046 }
1047 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001048done:
1049 return ret;
1050}
1051
Radek Krejcib56c7502019-02-13 14:19:54 +01001052/**
1053 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1054 *
1055 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1056 *
1057 * @param[in] ctx Compile context for logging.
1058 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1059 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1060 * @return LY_SUCCESS if everything is ok.
1061 * @return LY_EVALID if the identity is derived from itself.
1062 */
Radek Krejci38222632019-02-12 16:55:05 +01001063static LY_ERR
1064lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1065{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001066 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001067 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001068 struct ly_set recursion = {0};
1069 struct lysc_ident *drv;
1070
1071 if (!derived) {
1072 return LY_SUCCESS;
1073 }
1074
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001075 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001076 if (ident == derived[u]) {
1077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1078 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001079 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001080 goto cleanup;
1081 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001082 ret = ly_set_add(&recursion, derived[u], 0, NULL);
1083 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001084 }
1085
1086 for (v = 0; v < recursion.count; ++v) {
1087 drv = recursion.objs[v];
1088 if (!drv->derived) {
1089 continue;
1090 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001091 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001092 if (ident == drv->derived[u]) {
1093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1094 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001095 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001096 goto cleanup;
1097 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001098 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
1099 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001100 }
1101 }
Radek Krejci38222632019-02-12 16:55:05 +01001102
1103cleanup:
1104 ly_set_erase(&recursion, NULL);
1105 return ret;
1106}
1107
Radek Krejcia3045382018-11-22 14:30:31 +01001108/**
1109 * @brief Find and process the referenced base identities from another identity or identityref
1110 *
Radek Krejciaca74032019-06-04 08:53:06 +02001111 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001112 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1113 * to distinguish these two use cases.
1114 *
1115 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1116 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001117 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001118 * @param[in] bases Array of bases of identityref to fill in.
1119 * @return LY_ERR value.
1120 */
Radek Krejci19a96102018-11-15 13:38:09 +01001121static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001122lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001123 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001124{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001125 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001126 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001127 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001128 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001129
1130 assert(ident || bases);
1131
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001132 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1134 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1135 return LY_EVALID;
1136 }
1137
Michal Vasko33ff9422020-07-03 09:50:39 +02001138 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001139 s = strchr(bases_p[u], ':');
1140 if (s) {
1141 /* prefixed identity */
1142 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001143 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001144 } else {
1145 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001146 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001147 }
1148 if (!mod) {
1149 if (ident) {
1150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1151 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1152 } else {
1153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1154 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1155 }
1156 return LY_EVALID;
1157 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001158
Radek Krejci555cb5b2018-11-16 14:54:33 +01001159 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001160 if (mod->compiled) {
1161 identities = mod->compiled->identities;
1162 } else {
1163 identities = mod->dis_identities;
1164 }
1165 LY_ARRAY_FOR(identities, v) {
1166 if (!strcmp(name, identities[v].name)) {
1167 if (ident) {
1168 if (ident == &identities[v]) {
1169 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1170 "Identity \"%s\" is derived from itself.", ident->name);
1171 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001172 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001173 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1174 /* we have match! store the backlink */
1175 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1176 *idref = ident;
1177 } else {
1178 /* we have match! store the found identity */
1179 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1180 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001181 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001182 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001183 }
1184 }
1185 if (!idref || !(*idref)) {
1186 if (ident) {
1187 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1188 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1189 } else {
1190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1191 "Unable to find base (%s) of identityref.", bases_p[u]);
1192 }
1193 return LY_EVALID;
1194 }
1195 }
1196 return LY_SUCCESS;
1197}
1198
Radek Krejcia3045382018-11-22 14:30:31 +01001199/**
1200 * @brief For the given array of identities, set the backlinks from all their base identities.
1201 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1202 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1203 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1204 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1205 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001206static LY_ERR
1207lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1208{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001209 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001210
Michal Vasko33ff9422020-07-03 09:50:39 +02001211 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001212 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001213 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001214 continue;
1215 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001216 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001217 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module, idents_p[u].bases, &idents[u], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001218 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001219 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001220 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001221 return LY_SUCCESS;
1222}
1223
Radek Krejci0af46292019-01-11 16:02:31 +01001224LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001225lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001226 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001227{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001228 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001229 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001230 struct lysc_ctx context = {0};
1231
Radek Krejci327de162019-06-14 12:52:07 +02001232 assert(ctx_sc || ctx);
1233
1234 if (!ctx_sc) {
1235 context.ctx = ctx;
1236 context.mod = module;
1237 context.path_len = 1;
1238 context.path[0] = '/';
1239 ctx_sc = &context;
1240 }
Radek Krejci0af46292019-01-11 16:02:31 +01001241
1242 if (!features_p) {
1243 return LY_SUCCESS;
1244 }
1245 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001246 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001247 }
1248
Radek Krejci327de162019-06-14 12:52:07 +02001249 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001250 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001251 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001252 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1253
Radek Krejci0af46292019-01-11 16:02:31 +01001254 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001255 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001256 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
1257 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
1258 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001259 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001260 (*features)[offset + u].module = ctx_sc->mod;
1261
1262 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001263 }
Radek Krejci327de162019-06-14 12:52:07 +02001264 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001265
Radek Krejci011e4aa2020-09-04 15:22:31 +02001266done:
1267 return ret;
Radek Krejci0af46292019-01-11 16:02:31 +01001268}
1269
Radek Krejcia3045382018-11-22 14:30:31 +01001270/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001271 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001272 *
1273 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1274 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001275 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001276 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1277 * being currently processed).
1278 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +01001279 * @return LY_SUCCESS if everything is ok.
1280 * @return LY_EVALID if the feature references indirectly itself.
1281 */
1282static LY_ERR
1283lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1284{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001285 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001286 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001287 struct ly_set recursion = {0};
1288 struct lysc_feature *drv;
1289
1290 if (!depfeatures) {
1291 return LY_SUCCESS;
1292 }
1293
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001294 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001295 if (feature == depfeatures[u]) {
1296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1297 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001298 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001299 goto cleanup;
1300 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001301 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
1302 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001303 }
1304
1305 for (v = 0; v < recursion.count; ++v) {
1306 drv = recursion.objs[v];
1307 if (!drv->depfeatures) {
1308 continue;
1309 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001310 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001311 if (feature == drv->depfeatures[u]) {
1312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1313 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001314 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001315 goto cleanup;
1316 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001317 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
1318 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001319 }
1320 }
Radek Krejci09a1fc52019-02-13 10:55:17 +01001321
1322cleanup:
1323 ly_set_erase(&recursion, NULL);
1324 return ret;
1325}
1326
1327/**
Radek Krejci0af46292019-01-11 16:02:31 +01001328 * @brief Create pre-compiled features array.
1329 *
1330 * See lys_feature_precompile() for more details.
1331 *
Radek Krejcia3045382018-11-22 14:30:31 +01001332 * @param[in] ctx Compile context.
1333 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001334 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001335 * @return LY_ERR value.
1336 */
Radek Krejci19a96102018-11-15 13:38:09 +01001337static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001338lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001339{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001340 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001341 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001342 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001343
Radek Krejci0af46292019-01-11 16:02:31 +01001344 /* find the preprecompiled feature */
1345 LY_ARRAY_FOR(features, x) {
1346 if (strcmp(features[x].name, feature_p->name)) {
1347 continue;
1348 }
1349 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001350 lysc_update_path(ctx, NULL, "{feature}");
1351 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001352
Radek Krejci0af46292019-01-11 16:02:31 +01001353 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001354 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001355 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001356 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001357 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001358 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001359 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001360 /* check for circular dependency - direct reference first,... */
1361 if (feature == feature->iffeatures[u].features[v]) {
1362 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1363 "Feature \"%s\" is referenced from itself.", feature->name);
1364 return LY_EVALID;
1365 }
1366 /* ... and indirect circular reference */
1367 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1368
Radek Krejci0af46292019-01-11 16:02:31 +01001369 /* add itself into the dependants list */
1370 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1371 *df = feature;
1372 }
Radek Krejci19a96102018-11-15 13:38:09 +01001373 }
Radek Krejci19a96102018-11-15 13:38:09 +01001374 }
1375 }
Radek Krejci327de162019-06-14 12:52:07 +02001376 lysc_update_path(ctx, NULL, NULL);
1377 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001378 done:
1379 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001380 }
Radek Krejci0af46292019-01-11 16:02:31 +01001381
1382 LOGINT(ctx->ctx);
1383 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001384}
1385
Radek Krejcib56c7502019-02-13 14:19:54 +01001386/**
1387 * @brief Revert compiled list of features back to the precompiled state.
1388 *
1389 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
Michal Vasko33ff9422020-07-03 09:50:39 +02001390 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001391 *
1392 * @param[in] ctx Compilation context.
1393 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
Michal Vasko33ff9422020-07-03 09:50:39 +02001394 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001395 */
Radek Krejci95710c92019-02-11 15:49:55 +01001396static void
1397lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1398{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001399 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001400
Radek Krejcia46012b2020-08-12 15:41:04 +02001401 if (mod->compiled) {
1402 /* keep the dis_features list until the complete lys_module is freed */
1403 mod->dis_features = mod->compiled->features;
1404 mod->compiled->features = NULL;
1405 }
Radek Krejci95710c92019-02-11 15:49:55 +01001406
Michal Vasko33ff9422020-07-03 09:50:39 +02001407 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001408 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001409 LY_ARRAY_FOR(mod->dis_features, u) {
1410 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1411 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001412 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001413 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1414 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001415
Michal Vasko33ff9422020-07-03 09:50:39 +02001416 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1417 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001418 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001419 LY_ARRAY_FREE(mod->dis_features[u].exts);
1420 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001421 }
1422}
1423
Radek Krejcia3045382018-11-22 14:30:31 +01001424/**
1425 * @brief Validate and normalize numeric value from a range definition.
1426 * @param[in] ctx Compile context.
1427 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1428 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1429 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1430 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1431 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1432 * @param[in] value String value of the range boundary.
1433 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
1434 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1435 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1436 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001437LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001438range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001439{
Radek Krejci6cba4292018-11-15 17:33:29 +01001440 size_t fraction = 0, size;
1441
Radek Krejci19a96102018-11-15 13:38:09 +01001442 *len = 0;
1443
1444 assert(value);
1445 /* parse value */
1446 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1447 return LY_EVALID;
1448 }
1449
1450 if ((value[*len] == '-') || (value[*len] == '+')) {
1451 ++(*len);
1452 }
1453
1454 while (isdigit(value[*len])) {
1455 ++(*len);
1456 }
1457
1458 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001459 if (basetype == LY_TYPE_DEC64) {
1460 goto decimal;
1461 } else {
1462 *valcopy = strndup(value, *len);
1463 return LY_SUCCESS;
1464 }
Radek Krejci19a96102018-11-15 13:38:09 +01001465 }
1466 fraction = *len;
1467
1468 ++(*len);
1469 while (isdigit(value[*len])) {
1470 ++(*len);
1471 }
1472
Radek Krejci6cba4292018-11-15 17:33:29 +01001473 if (basetype == LY_TYPE_DEC64) {
1474decimal:
1475 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001476 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1478 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1479 *len, value, frdigits);
1480 return LY_EINVAL;
1481 }
1482 if (fraction) {
1483 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1484 } else {
1485 size = (*len) + frdigits + 1;
1486 }
1487 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001488 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1489
Radek Krejci6cba4292018-11-15 17:33:29 +01001490 (*valcopy)[size - 1] = '\0';
1491 if (fraction) {
1492 memcpy(&(*valcopy)[0], &value[0], fraction);
1493 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1494 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1495 } else {
1496 memcpy(&(*valcopy)[0], &value[0], *len);
1497 memset(&(*valcopy)[*len], '0', frdigits);
1498 }
Radek Krejci19a96102018-11-15 13:38:09 +01001499 }
1500 return LY_SUCCESS;
1501}
1502
Radek Krejcia3045382018-11-22 14:30:31 +01001503/**
1504 * @brief Check that values in range are in ascendant order.
1505 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001506 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1507 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001508 * @param[in] value Current value to check.
1509 * @param[in] prev_value The last seen value.
1510 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1511 */
Radek Krejci19a96102018-11-15 13:38:09 +01001512static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001513range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001514{
1515 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001516 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001517 return LY_EEXIST;
1518 }
1519 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001520 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001521 return LY_EEXIST;
1522 }
1523 }
1524 return LY_SUCCESS;
1525}
1526
Radek Krejcia3045382018-11-22 14:30:31 +01001527/**
1528 * @brief Set min/max value of the range part.
1529 * @param[in] ctx Compile context.
1530 * @param[in] part Range part structure to fill.
1531 * @param[in] max Flag to distinguish if storing min or max value.
1532 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1533 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1534 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1535 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1536 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001537 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +01001538 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1539 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1540 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1541 * frdigits value), LY_EMEM.
1542 */
Radek Krejci19a96102018-11-15 13:38:09 +01001543static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001544range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
1545 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001546{
1547 LY_ERR ret = LY_SUCCESS;
1548 char *valcopy = NULL;
1549 size_t len;
1550
1551 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001552 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001553 LY_CHECK_GOTO(ret, finalize);
1554 }
1555 if (!valcopy && base_range) {
1556 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001557 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001558 } else {
1559 part->min_64 = base_range->parts[0].min_64;
1560 }
1561 if (!first) {
1562 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1563 }
1564 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001565 }
1566
1567 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001568 case LY_TYPE_INT8: /* range */
1569 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001570 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001571 } else if (max) {
1572 part->max_64 = INT64_C(127);
1573 } else {
1574 part->min_64 = INT64_C(-128);
1575 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001576 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001577 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001578 }
1579 break;
1580 case LY_TYPE_INT16: /* range */
1581 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001582 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001583 } else if (max) {
1584 part->max_64 = INT64_C(32767);
1585 } else {
1586 part->min_64 = INT64_C(-32768);
1587 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001588 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001589 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001590 }
1591 break;
1592 case LY_TYPE_INT32: /* range */
1593 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001594 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001595 } else if (max) {
1596 part->max_64 = INT64_C(2147483647);
1597 } else {
1598 part->min_64 = INT64_C(-2147483648);
1599 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001600 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001601 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001602 }
1603 break;
1604 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001605 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001606 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001607 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001608 max ? &part->max_64 : &part->min_64);
1609 } else if (max) {
1610 part->max_64 = INT64_C(9223372036854775807);
1611 } else {
1612 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1613 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001614 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001615 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001616 }
1617 break;
1618 case LY_TYPE_UINT8: /* range */
1619 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001620 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001621 } else if (max) {
1622 part->max_u64 = UINT64_C(255);
1623 } else {
1624 part->min_u64 = UINT64_C(0);
1625 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001626 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001627 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001628 }
1629 break;
1630 case LY_TYPE_UINT16: /* range */
1631 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001632 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001633 } else if (max) {
1634 part->max_u64 = UINT64_C(65535);
1635 } else {
1636 part->min_u64 = UINT64_C(0);
1637 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001638 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001639 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001640 }
1641 break;
1642 case LY_TYPE_UINT32: /* range */
1643 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001644 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001645 } else if (max) {
1646 part->max_u64 = UINT64_C(4294967295);
1647 } else {
1648 part->min_u64 = UINT64_C(0);
1649 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001650 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001651 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001652 }
1653 break;
1654 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001655 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001656 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001657 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001658 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001659 } else if (max) {
1660 part->max_u64 = UINT64_C(18446744073709551615);
1661 } else {
1662 part->min_u64 = UINT64_C(0);
1663 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001664 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001665 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001666 }
1667 break;
1668 default:
1669 LOGINT(ctx->ctx);
1670 ret = LY_EINT;
1671 }
1672
Radek Krejci5969f272018-11-23 10:03:58 +01001673finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001674 if (ret == LY_EDENIED) {
1675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1676 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1677 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1678 } else if (ret == LY_EVALID) {
1679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1680 "Invalid %s restriction - invalid value \"%s\".",
1681 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1682 } else if (ret == LY_EEXIST) {
1683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1684 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001685 length_restr ? "length" : "range",
Radek Krejci0f969882020-08-21 16:56:47 +02001686 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001687 } else if (!ret && value) {
1688 *value = *value + len;
1689 }
1690 free(valcopy);
1691 return ret;
1692}
1693
Radek Krejcia3045382018-11-22 14:30:31 +01001694/**
1695 * @brief Compile the parsed range restriction.
1696 * @param[in] ctx Compile context.
1697 * @param[in] range_p Parsed range structure to compile.
1698 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1699 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1700 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1701 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1702 * range restriction must be more restrictive than the base_range.
1703 * @param[in,out] range Pointer to the created current range structure.
1704 * @return LY_ERR value.
1705 */
Radek Krejci19a96102018-11-15 13:38:09 +01001706static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001707lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
Radek Krejci0f969882020-08-21 16:56:47 +02001708 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001709{
1710 LY_ERR ret = LY_EVALID;
1711 const char *expr;
1712 struct lysc_range_part *parts = NULL, *part;
Radek Krejci857189e2020-09-01 13:26:36 +02001713 ly_bool range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001714 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001715
1716 assert(range);
1717 assert(range_p);
1718
1719 expr = range_p->arg;
Michal Vaskod989ba02020-08-24 10:59:24 +02001720 while (1) {
Radek Krejci19a96102018-11-15 13:38:09 +01001721 if (isspace(*expr)) {
1722 ++expr;
1723 } else if (*expr == '\0') {
1724 if (range_expected) {
1725 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1726 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1727 length_restr ? "length" : "range", range_p->arg);
1728 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001729 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1731 "Invalid %s restriction - unexpected end of the expression (%s).",
1732 length_restr ? "length" : "range", range_p->arg);
1733 goto cleanup;
1734 }
1735 parts_done++;
1736 break;
1737 } else if (!strncmp(expr, "min", 3)) {
1738 if (parts) {
1739 /* min cannot be used elsewhere than in the first part */
1740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1741 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1742 expr - range_p->arg, range_p->arg);
1743 goto cleanup;
1744 }
1745 expr += 3;
1746
1747 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001748 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001749 part->max_64 = part->min_64;
1750 } else if (*expr == '|') {
1751 if (!parts || range_expected) {
1752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1753 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1754 goto cleanup;
1755 }
1756 expr++;
1757 parts_done++;
1758 /* process next part of the expression */
1759 } else if (!strncmp(expr, "..", 2)) {
1760 expr += 2;
1761 while (isspace(*expr)) {
1762 expr++;
1763 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001764 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1766 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1767 goto cleanup;
1768 }
1769 /* continue expecting the upper boundary */
1770 range_expected = 1;
1771 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1772 /* number */
1773 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001774 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001775 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001776 range_expected = 0;
1777 } else {
1778 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001779 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001780 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001781 part->max_64 = part->min_64;
1782 }
1783
1784 /* continue with possible another expression part */
1785 } else if (!strncmp(expr, "max", 3)) {
1786 expr += 3;
1787 while (isspace(*expr)) {
1788 expr++;
1789 }
1790 if (*expr != '\0') {
1791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1792 length_restr ? "length" : "range", expr);
1793 goto cleanup;
1794 }
1795 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001796 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001797 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001798 range_expected = 0;
1799 } else {
1800 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001801 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001802 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001803 part->min_64 = part->max_64;
1804 }
1805 } else {
1806 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1807 length_restr ? "length" : "range", expr);
1808 goto cleanup;
1809 }
1810 }
1811
1812 /* check with the previous range/length restriction */
1813 if (base_range) {
1814 switch (basetype) {
1815 case LY_TYPE_BINARY:
1816 case LY_TYPE_UINT8:
1817 case LY_TYPE_UINT16:
1818 case LY_TYPE_UINT32:
1819 case LY_TYPE_UINT64:
1820 case LY_TYPE_STRING:
1821 uns = 1;
1822 break;
1823 case LY_TYPE_DEC64:
1824 case LY_TYPE_INT8:
1825 case LY_TYPE_INT16:
1826 case LY_TYPE_INT32:
1827 case LY_TYPE_INT64:
1828 uns = 0;
1829 break;
1830 default:
1831 LOGINT(ctx->ctx);
1832 ret = LY_EINT;
1833 goto cleanup;
1834 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001835 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001836 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1837 goto baseerror;
1838 }
1839 /* current lower bound is not lower than the base */
1840 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1841 /* base has single value */
1842 if (base_range->parts[v].min_64 == parts[u].min_64) {
1843 /* both lower bounds are the same */
1844 if (parts[u].min_64 != parts[u].max_64) {
1845 /* current continues with a range */
1846 goto baseerror;
1847 } else {
1848 /* equal single values, move both forward */
1849 ++v;
1850 continue;
1851 }
1852 } else {
1853 /* base is single value lower than current range, so the
1854 * value from base range is removed in the current,
1855 * move only base and repeat checking */
1856 ++v;
1857 --u;
1858 continue;
1859 }
1860 } else {
1861 /* base is the range */
1862 if (parts[u].min_64 == parts[u].max_64) {
1863 /* current is a single value */
1864 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1865 /* current is behind the base range, so base range is omitted,
1866 * move the base and keep the current for further check */
1867 ++v;
1868 --u;
1869 } /* else it is within the base range, so move the current, but keep the base */
1870 continue;
1871 } else {
1872 /* both are ranges - check the higher bound, the lower was already checked */
1873 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1874 /* higher bound is higher than the current higher bound */
1875 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1876 /* but the current lower bound is also higher, so the base range is omitted,
1877 * continue with the same current, but move the base */
1878 --u;
1879 ++v;
1880 continue;
1881 }
1882 /* current range starts within the base range but end behind it */
1883 goto baseerror;
1884 } else {
1885 /* current range is smaller than the base,
1886 * move current, but stay with the base */
1887 continue;
1888 }
1889 }
1890 }
1891 }
1892 if (u != parts_done) {
1893baseerror:
1894 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1895 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1896 length_restr ? "length" : "range", range_p->arg);
1897 goto cleanup;
1898 }
1899 }
1900
1901 if (!(*range)) {
1902 *range = calloc(1, sizeof **range);
1903 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1904 }
1905
Radek Krejcic8b31002019-01-08 10:24:45 +01001906 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001907 if (range_p->eapptag) {
1908 lydict_remove(ctx->ctx, (*range)->eapptag);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001909 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001910 }
1911 if (range_p->emsg) {
1912 lydict_remove(ctx->ctx, (*range)->emsg);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001913 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001914 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001915 if (range_p->dsc) {
1916 lydict_remove(ctx->ctx, (*range)->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001917 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001918 }
1919 if (range_p->ref) {
1920 lydict_remove(ctx->ctx, (*range)->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001921 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001922 }
Radek Krejci19a96102018-11-15 13:38:09 +01001923 /* extensions are taken only from the last range by the caller */
1924
1925 (*range)->parts = parts;
1926 parts = NULL;
1927 ret = LY_SUCCESS;
1928cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001929 LY_ARRAY_FREE(parts);
1930
1931 return ret;
1932}
1933
1934/**
1935 * @brief Checks pattern syntax.
1936 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937 * @param[in] ctx Context.
1938 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001939 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001940 * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
Radek Krejcia3045382018-11-22 14:30:31 +01001941 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001942 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943LY_ERR
1944lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001945{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001946 size_t idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001947 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001948 int err_code;
1949 const char *orig_ptr;
1950 PCRE2_SIZE err_offset;
1951 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001952#define URANGE_LEN 19
1953 char *ublock2urange[][2] = {
1954 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1955 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1956 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1957 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1958 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1959 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1960 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1961 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1962 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1963 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1964 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1965 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1966 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1967 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1968 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1969 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1970 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1971 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1972 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1973 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1974 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1975 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1976 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1977 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1978 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1979 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1980 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1981 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1982 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1983 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1984 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1985 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1986 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1987 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1988 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1989 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1990 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1991 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1992 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1993 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1994 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1995 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1996 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1997 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1998 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1999 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
2000 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
2001 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
2002 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
2003 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
2004 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
2005 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
2006 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
2007 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
2008 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
2009 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
2010 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
2011 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
2012 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
2013 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
2014 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
2015 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
2016 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
2017 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
2018 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
2019 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
2020 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
2021 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
2022 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
2023 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
2024 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
2025 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
2026 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
2027 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
2028 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
2029 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
2030 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
2031 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
2032 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
2033 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
2034 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
2035 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
2036 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
2037 {NULL, NULL}
2038 };
2039
2040 /* adjust the expression to a Perl equivalent
2041 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
2042
Michal Vasko40a00082020-05-27 15:20:01 +02002043 /* allocate space for the transformed pattern */
2044 size = strlen(pattern) + 1;
2045 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002047 perl_regex[0] = '\0';
2048
Michal Vasko40a00082020-05-27 15:20:01 +02002049 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
2050 brack = 0;
2051 idx = 0;
2052 orig_ptr = pattern;
2053 while (orig_ptr[0]) {
2054 switch (orig_ptr[0]) {
2055 case '$':
2056 case '^':
2057 if (!brack) {
2058 /* make space for the extra character */
2059 ++size;
2060 perl_regex = ly_realloc(perl_regex, size);
2061 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002062
Michal Vasko40a00082020-05-27 15:20:01 +02002063 /* print escape slash */
2064 perl_regex[idx] = '\\';
2065 ++idx;
2066 }
2067 break;
2068 case '[':
2069 /* must not be escaped */
2070 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2071 ++brack;
2072 }
2073 break;
2074 case ']':
2075 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2076 /* pattern was checked and compiled already */
2077 assert(brack);
2078 --brack;
2079 }
2080 break;
2081 default:
2082 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002083 }
Michal Vasko40a00082020-05-27 15:20:01 +02002084
2085 /* copy char */
2086 perl_regex[idx] = orig_ptr[0];
2087
2088 ++idx;
2089 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002090 }
Michal Vasko40a00082020-05-27 15:20:01 +02002091 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002092
2093 /* substitute Unicode Character Blocks with exact Character Ranges */
2094 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2095 start = ptr - perl_regex;
2096
2097 ptr = strchr(ptr, '}');
2098 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002100 pattern, perl_regex + start + 2, "unterminated character property");
2101 free(perl_regex);
2102 return LY_EVALID;
2103 }
2104 end = (ptr - perl_regex) + 1;
2105
2106 /* need more space */
2107 if (end - start < URANGE_LEN) {
2108 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002110 }
2111
2112 /* find our range */
2113 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2114 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2115 break;
2116 }
2117 }
2118 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002120 pattern, perl_regex + start + 5, "unknown block name");
2121 free(perl_regex);
2122 return LY_EVALID;
2123 }
2124
2125 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
Michal Vasko40a00082020-05-27 15:20:01 +02002126 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002127 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002128 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002129 }
2130 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002131 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002132 }
2133 }
Michal Vasko40a00082020-05-27 15:20:01 +02002134 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002135 /* skip brackets */
2136 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2137 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2138 } else {
2139 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2140 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2141 }
2142 }
2143
2144 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002145 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2146 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002147 &err_code, &err_offset, NULL);
2148 if (!code_local) {
2149 PCRE2_UCHAR err_msg[256] = {0};
2150 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002152 free(perl_regex);
2153 return LY_EVALID;
2154 }
2155 free(perl_regex);
2156
Radek Krejci54579462019-04-30 12:47:06 +02002157 if (code) {
2158 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002159 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002160 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002161 }
2162
2163 return LY_SUCCESS;
2164
2165#undef URANGE_LEN
2166}
2167
Radek Krejcia3045382018-11-22 14:30:31 +01002168/**
2169 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2170 * @param[in] ctx Compile context.
2171 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002172 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2173 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2174 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2175 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2176 */
Radek Krejci19a96102018-11-15 13:38:09 +01002177static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002178lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci0f969882020-08-21 16:56:47 +02002179 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
Radek Krejci19a96102018-11-15 13:38:09 +01002180{
2181 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002182 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002183 LY_ERR ret = LY_SUCCESS;
2184
2185 /* first, copy the patterns from the base type */
2186 if (base_patterns) {
2187 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2188 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2189 }
2190
2191 LY_ARRAY_FOR(patterns_p, u) {
2192 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2193 *pattern = calloc(1, sizeof **pattern);
2194 ++(*pattern)->refcount;
2195
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002197 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002198
2199 if (patterns_p[u].arg[0] == 0x15) {
2200 (*pattern)->inverted = 1;
2201 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002202 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done);
2203 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
2204 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
2205 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
2206 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002207 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002208 }
2209done:
2210 return ret;
2211}
2212
Radek Krejcia3045382018-11-22 14:30:31 +01002213/**
2214 * @brief map of the possible restrictions combination for the specific built-in type.
2215 */
Radek Krejci19a96102018-11-15 13:38:09 +01002216static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2217 0 /* LY_TYPE_UNKNOWN */,
2218 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002219 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2220 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2221 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2222 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2223 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002224 LYS_SET_BIT /* LY_TYPE_BITS */,
2225 0 /* LY_TYPE_BOOL */,
2226 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2227 0 /* LY_TYPE_EMPTY */,
2228 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2229 LYS_SET_BASE /* LY_TYPE_IDENT */,
2230 LYS_SET_REQINST /* LY_TYPE_INST */,
2231 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002232 LYS_SET_TYPE /* LY_TYPE_UNION */,
2233 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002234 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002235 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002236 LYS_SET_RANGE /* LY_TYPE_INT64 */
2237};
2238
2239/**
2240 * @brief stringification of the YANG built-in data types
2241 */
Michal Vasko22df3f02020-08-24 13:29:22 +02002242const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
Radek Krejci5969f272018-11-23 10:03:58 +01002243 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
Radek Krejci0f969882020-08-21 16:56:47 +02002244 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"};
Radek Krejci19a96102018-11-15 13:38:09 +01002245
Radek Krejcia3045382018-11-22 14:30:31 +01002246/**
2247 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2248 * @param[in] ctx Compile context.
2249 * @param[in] enums_p Array of the parsed enum structures to compile.
2250 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
Radek Krejcia3045382018-11-22 14:30:31 +01002251 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2252 * @param[out] enums Newly created array of the compiled enums information for the current type.
2253 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2254 */
Radek Krejci19a96102018-11-15 13:38:09 +01002255static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002256lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci0f969882020-08-21 16:56:47 +02002257 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002258{
2259 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002260 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002261 int32_t value = 0;
2262 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002263 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002264
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002265 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2267 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2268 return LY_EVALID;
2269 }
2270
2271 LY_ARRAY_FOR(enums_p, u) {
2272 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002273 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
2274 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done);
2275 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Radek Krejci693262f2019-04-29 15:23:20 +02002276 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002277 if (base_enums) {
2278 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2279 LY_ARRAY_FOR(base_enums, v) {
2280 if (!strcmp(e->name, base_enums[v].name)) {
2281 break;
2282 }
2283 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002284 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2286 "Invalid %s - derived type adds new item \"%s\".",
2287 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2288 return LY_EVALID;
2289 }
2290 match = v;
2291 }
2292
2293 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002294 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002295 if (enums_p[u].flags & LYS_SET_VALUE) {
2296 e->value = (int32_t)enums_p[u].value;
2297 if (!u || e->value >= value) {
2298 value = e->value + 1;
2299 }
2300 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002301 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002302 if (e->value == (*enums)[v].value) {
2303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2304 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2305 e->value, e->name, (*enums)[v].name);
2306 return LY_EVALID;
2307 }
2308 }
2309 } else if (base_enums) {
2310 /* inherit the assigned value */
2311 e->value = base_enums[match].value;
2312 if (!u || e->value >= value) {
2313 value = e->value + 1;
2314 }
2315 } else {
2316 /* assign value automatically */
2317 if (u && value == INT32_MIN) {
2318 /* counter overflow */
2319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2320 "Invalid enumeration - it is not possible to auto-assign enum value for "
2321 "\"%s\" since the highest value is already 2147483647.", e->name);
2322 return LY_EVALID;
2323 }
2324 e->value = value++;
2325 }
2326 } else { /* LY_TYPE_BITS */
2327 if (enums_p[u].flags & LYS_SET_VALUE) {
2328 e->value = (int32_t)enums_p[u].value;
2329 if (!u || (uint32_t)e->value >= position) {
2330 position = (uint32_t)e->value + 1;
2331 }
2332 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002333 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002334 if (e->value == (*enums)[v].value) {
2335 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2336 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek Krejci0f969882020-08-21 16:56:47 +02002337 (uint32_t)e->value, e->name, (*enums)[v].name);
Radek Krejci19a96102018-11-15 13:38:09 +01002338 return LY_EVALID;
2339 }
2340 }
2341 } else if (base_enums) {
2342 /* inherit the assigned value */
2343 e->value = base_enums[match].value;
2344 if (!u || (uint32_t)e->value >= position) {
2345 position = (uint32_t)e->value + 1;
2346 }
2347 } else {
2348 /* assign value automatically */
2349 if (u && position == 0) {
2350 /* counter overflow */
2351 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2352 "Invalid bits - it is not possible to auto-assign bit position for "
2353 "\"%s\" since the highest value is already 4294967295.", e->name);
2354 return LY_EVALID;
2355 }
2356 e->value = position++;
2357 }
2358 }
2359
2360 if (base_enums) {
2361 /* the assigned values must not change from the derived type */
2362 if (e->value != base_enums[match].value) {
2363 if (basetype == LY_TYPE_ENUM) {
2364 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2365 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2366 e->name, base_enums[match].value, e->value);
2367 } else {
2368 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2369 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2370 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2371 }
2372 return LY_EVALID;
2373 }
2374 }
2375
Radek Krejciec4da802019-05-02 13:02:41 +02002376 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002377 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002378
2379 if (basetype == LY_TYPE_BITS) {
2380 /* keep bits ordered by position */
Radek Krejci1e008d22020-08-17 11:37:37 +02002381 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
Radek Krejci19a96102018-11-15 13:38:09 +01002382 if (v != u) {
2383 memcpy(&storage, e, sizeof *e);
2384 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2385 memcpy(&(*enums)[v], &storage, sizeof storage);
2386 }
2387 }
2388 }
2389
2390done:
2391 return ret;
2392}
2393
Radek Krejcia3045382018-11-22 14:30:31 +01002394/**
2395 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2396 *
2397 * path-arg = absolute-path / relative-path
2398 * absolute-path = 1*("/" (node-identifier *path-predicate))
2399 * relative-path = 1*(".." "/") descendant-path
2400 *
2401 * @param[in,out] path Path to parse.
2402 * @param[out] prefix Prefix of the token, NULL if there is not any.
2403 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2404 * @param[out] name Name of the token.
2405 * @param[out] nam_len Length of the name.
2406 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2407 * must not be changed between consecutive calls. -1 if the
2408 * path is absolute.
2409 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2410 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2411 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002412LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002413lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
Radek Krejci857189e2020-09-01 13:26:36 +02002414 int32_t *parent_times, ly_bool *has_predicate)
Radek Krejcia3045382018-11-22 14:30:31 +01002415{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002416 int32_t par_times = 0;
Radek Krejcia3045382018-11-22 14:30:31 +01002417
2418 assert(path && *path);
2419 assert(parent_times);
2420 assert(prefix);
2421 assert(prefix_len);
2422 assert(name);
2423 assert(name_len);
2424 assert(has_predicate);
2425
2426 *prefix = NULL;
2427 *prefix_len = 0;
2428 *name = NULL;
2429 *name_len = 0;
2430 *has_predicate = 0;
2431
2432 if (!*parent_times) {
2433 if (!strncmp(*path, "..", 2)) {
2434 *path += 2;
2435 ++par_times;
2436 while (!strncmp(*path, "/..", 3)) {
2437 *path += 3;
2438 ++par_times;
2439 }
2440 }
2441 if (par_times) {
2442 *parent_times = par_times;
2443 } else {
2444 *parent_times = -1;
2445 }
2446 }
2447
2448 if (**path != '/') {
2449 return LY_EINVAL;
2450 }
2451 /* skip '/' */
2452 ++(*path);
2453
2454 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002455 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002456
2457 if ((**path == '/' && (*path)[1]) || !**path) {
2458 /* path continues by another token or this is the last token */
2459 return LY_SUCCESS;
2460 } else if ((*path)[0] != '[') {
2461 /* unexpected character */
2462 return LY_EINVAL;
2463 } else {
2464 /* predicate starting with [ */
2465 *has_predicate = 1;
2466 return LY_SUCCESS;
2467 }
2468}
2469
2470/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002471 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2472 *
2473 * The set of features used for target must be a subset of features used for the leafref.
2474 * This is not a perfect, we should compare the truth tables but it could require too much resources
2475 * and RFC 7950 does not require it explicitely, so we simplify that.
2476 *
2477 * @param[in] refnode The leafref node.
2478 * @param[in] target Tha target node of the leafref.
2479 * @return LY_SUCCESS or LY_EVALID;
2480 */
2481static LY_ERR
2482lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2483{
2484 LY_ERR ret = LY_EVALID;
2485 const struct lysc_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002486 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci58d171e2018-11-23 13:50:55 +01002487 struct ly_set features = {0};
2488
2489 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002490 if (iter->iffeatures) {
2491 LY_ARRAY_FOR(iter->iffeatures, u) {
2492 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002493 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002494 }
2495 }
2496 }
2497 }
2498
2499 /* we should have, in features set, a superset of features applicable to the target node.
Radek Krejciba03a5a2020-08-27 14:40:41 +02002500 * If the feature is not present, we don;t have a subset of features applicable
Radek Krejci58d171e2018-11-23 13:50:55 +01002501 * to the leafref itself. */
Radek Krejci58d171e2018-11-23 13:50:55 +01002502 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002503 if (iter->iffeatures) {
2504 LY_ARRAY_FOR(iter->iffeatures, u) {
2505 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002506 if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
2507 /* feature not present */
Radek Krejci58d171e2018-11-23 13:50:55 +01002508 goto cleanup;
2509 }
2510 }
2511 }
2512 }
2513 }
2514 ret = LY_SUCCESS;
2515
2516cleanup:
2517 ly_set_erase(&features, NULL);
2518 return ret;
2519}
2520
Michal Vasko004d3152020-06-11 19:59:22 +02002521static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002522 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2523 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod);
Radek Krejcia3045382018-11-22 14:30:31 +01002524
Radek Krejcia3045382018-11-22 14:30:31 +01002525/**
2526 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2527 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002528 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2529 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2530 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2531 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002532 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002533 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002534 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002535 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2536 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2537 * @param[out] type Newly created type structure with the filled information about the type.
2538 * @return LY_ERR value.
2539 */
Radek Krejci19a96102018-11-15 13:38:09 +01002540static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002541lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002542 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2543 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2544 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002545{
2546 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002547 struct lysc_type_bin *bin;
2548 struct lysc_type_num *num;
2549 struct lysc_type_str *str;
2550 struct lysc_type_bits *bits;
2551 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002552 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002553 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002554 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002555 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002556
2557 switch (basetype) {
2558 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +02002559 bin = (struct lysc_type_bin *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002560
2561 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002562 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002563 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002564 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002565 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002566 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002567 }
2568 }
2569
2570 if (tpdfname) {
2571 type_p->compiled = *type;
2572 *type = calloc(1, sizeof(struct lysc_type_bin));
2573 }
2574 break;
2575 case LY_TYPE_BITS:
2576 /* RFC 7950 9.7 - bits */
Michal Vasko22df3f02020-08-24 13:29:22 +02002577 bits = (struct lysc_type_bits *)(*type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002578 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002579 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002580 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
2581 (struct lysc_type_bitenum_item **)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002582 }
2583
Radek Krejci555cb5b2018-11-16 14:54:33 +01002584 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002585 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002586 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002588 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002590 free(*type);
2591 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002592 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002593 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002594 }
2595
2596 if (tpdfname) {
2597 type_p->compiled = *type;
2598 *type = calloc(1, sizeof(struct lysc_type_bits));
2599 }
2600 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002601 case LY_TYPE_DEC64:
Radek Krejci115a74d2020-08-14 22:18:12 +02002602 dec = (struct lysc_type_dec *)(*type);
Radek Krejci6cba4292018-11-15 17:33:29 +01002603
2604 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002605 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002606 if (!type_p->fraction_digits) {
2607 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002608 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002609 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002610 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002611 free(*type);
2612 *type = NULL;
2613 }
2614 return LY_EVALID;
2615 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002616 dec->fraction_digits = type_p->fraction_digits;
2617 } else {
2618 if (type_p->fraction_digits) {
2619 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
2620 if (tpdfname) {
2621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2622 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
2623 tpdfname);
2624 } else {
2625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2626 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
2627 free(*type);
2628 *type = NULL;
2629 }
2630 return LY_EVALID;
Radek Krejci6cba4292018-11-15 17:33:29 +01002631 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002632 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
Radek Krejci6cba4292018-11-15 17:33:29 +01002633 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002634
2635 /* RFC 7950 9.2.4 - range */
2636 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002637 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
Radek Krejci115a74d2020-08-14 22:18:12 +02002638 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002639 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002640 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002641 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002642 }
2643
2644 if (tpdfname) {
2645 type_p->compiled = *type;
2646 *type = calloc(1, sizeof(struct lysc_type_dec));
2647 }
2648 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002649 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002650 str = (struct lysc_type_str *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002651
2652 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002653 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002654 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002655 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002656 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002657 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002658 }
Michal Vasko22df3f02020-08-24 13:29:22 +02002659 } else if (base && ((struct lysc_type_str *)base)->length) {
2660 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002661 }
2662
2663 /* RFC 7950 9.4.5 - pattern */
2664 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002665 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Michal Vasko22df3f02020-08-24 13:29:22 +02002666 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
2667 } else if (base && ((struct lysc_type_str *)base)->patterns) {
2668 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002669 }
2670
2671 if (tpdfname) {
2672 type_p->compiled = *type;
2673 *type = calloc(1, sizeof(struct lysc_type_str));
2674 }
2675 break;
2676 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +02002677 enumeration = (struct lysc_type_enum *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002678
2679 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002680 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002681 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002682 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002683 }
2684
Radek Krejci555cb5b2018-11-16 14:54:33 +01002685 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002686 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002687 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002689 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002691 free(*type);
2692 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002693 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002694 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002695 }
2696
2697 if (tpdfname) {
2698 type_p->compiled = *type;
2699 *type = calloc(1, sizeof(struct lysc_type_enum));
2700 }
2701 break;
2702 case LY_TYPE_INT8:
2703 case LY_TYPE_UINT8:
2704 case LY_TYPE_INT16:
2705 case LY_TYPE_UINT16:
2706 case LY_TYPE_INT32:
2707 case LY_TYPE_UINT32:
2708 case LY_TYPE_INT64:
2709 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +02002710 num = (struct lysc_type_num *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002711
2712 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002713 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002714 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002715 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002716 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002717 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002718 }
2719 }
2720
2721 if (tpdfname) {
2722 type_p->compiled = *type;
2723 *type = calloc(1, sizeof(struct lysc_type_num));
2724 }
2725 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002726 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002727 idref = (struct lysc_type_identityref *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002728
2729 /* RFC 7950 9.10.2 - base */
2730 if (type_p->bases) {
2731 if (base) {
2732 /* only the directly derived identityrefs can contain base specification */
2733 if (tpdfname) {
2734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002735 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002736 tpdfname);
2737 } else {
2738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002739 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002740 free(*type);
2741 *type = NULL;
2742 }
2743 return LY_EVALID;
2744 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002745 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002746 }
2747
2748 if (!base && !type_p->flags) {
2749 /* type derived from identityref built-in type must contain at least one base */
2750 if (tpdfname) {
2751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2752 } else {
2753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2754 free(*type);
2755 *type = NULL;
2756 }
2757 return LY_EVALID;
2758 }
2759
2760 if (tpdfname) {
2761 type_p->compiled = *type;
2762 *type = calloc(1, sizeof(struct lysc_type_identityref));
2763 }
2764 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002765 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002766 lref = (struct lysc_type_leafref *)*type;
Michal Vasko004d3152020-06-11 19:59:22 +02002767
Radek Krejcia3045382018-11-22 14:30:31 +01002768 /* RFC 7950 9.9.3 - require-instance */
2769 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002770 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002771 if (tpdfname) {
2772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2773 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2774 } else {
2775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2776 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2777 free(*type);
2778 *type = NULL;
2779 }
2780 return LY_EVALID;
2781 }
Michal Vasko004d3152020-06-11 19:59:22 +02002782 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002783 } else if (base) {
2784 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002785 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002786 } else {
2787 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002788 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002789 }
2790 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002791 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2792 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002793 } else if (base) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002794 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path);
2795 lref->path_context = ((struct lysc_type_leafref *)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002796 } else if (tpdfname) {
2797 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2798 return LY_EVALID;
2799 } else {
2800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2801 free(*type);
2802 *type = NULL;
2803 return LY_EVALID;
2804 }
2805 if (tpdfname) {
2806 type_p->compiled = *type;
2807 *type = calloc(1, sizeof(struct lysc_type_leafref));
2808 }
2809 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002810 case LY_TYPE_INST:
2811 /* RFC 7950 9.9.3 - require-instance */
2812 if (type_p->flags & LYS_SET_REQINST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002813 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
Radek Krejci16c0f822018-11-16 10:46:10 +01002814 } else {
2815 /* default is true */
Michal Vasko22df3f02020-08-24 13:29:22 +02002816 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
Radek Krejci16c0f822018-11-16 10:46:10 +01002817 }
2818
2819 if (tpdfname) {
2820 type_p->compiled = *type;
2821 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2822 }
2823 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002824 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +02002825 un = (struct lysc_type_union *)(*type);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002826
2827 /* RFC 7950 7.4 - type */
2828 if (type_p->types) {
2829 if (base) {
2830 /* only the directly derived union can contain types specification */
2831 if (tpdfname) {
2832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2833 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2834 tpdfname);
2835 } else {
2836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2837 "Invalid type substatement for the type not directly derived from union built-in type.");
2838 free(*type);
2839 *type = NULL;
2840 }
2841 return LY_EVALID;
2842 }
2843 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002844 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2845 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002846 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002847 &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002848 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2849 /* add space for additional types from the union subtype */
2850 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vasko22df3f02020-08-24 13:29:22 +02002851 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
2852 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002853
2854 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002855 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002856 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2857 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2858 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
Michal Vasko22df3f02020-08-24 13:29:22 +02002859 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx); lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +02002860 lref = (struct lysc_type_leafref *)un->types[u + additional];
2861
2862 lref->basetype = LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02002863 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path);
Michal Vasko004d3152020-06-11 19:59:22 +02002864 lref->refcount = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +02002865 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
2866 lref->path_context = ((struct lysc_type_leafref *)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002867 /* TODO extensions */
2868
2869 } else {
2870 un->types[u + additional] = un_aux->types[v];
2871 ++un_aux->types[v]->refcount;
2872 }
2873 ++additional;
2874 LY_ARRAY_INCREMENT(un->types);
2875 }
2876 /* compensate u increment in main loop */
2877 --additional;
2878
2879 /* free the replaced union subtype */
Michal Vasko22df3f02020-08-24 13:29:22 +02002880 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002881 } else {
2882 LY_ARRAY_INCREMENT(un->types);
2883 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002884 }
2885 }
2886
2887 if (!base && !type_p->flags) {
2888 /* type derived from union built-in type must contain at least one type */
2889 if (tpdfname) {
2890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2891 } else {
2892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2893 free(*type);
2894 *type = NULL;
2895 }
2896 return LY_EVALID;
2897 }
2898
2899 if (tpdfname) {
2900 type_p->compiled = *type;
2901 *type = calloc(1, sizeof(struct lysc_type_union));
2902 }
2903 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002904 case LY_TYPE_BOOL:
2905 case LY_TYPE_EMPTY:
2906 case LY_TYPE_UNKNOWN: /* just to complete switch */
2907 break;
2908 }
2909 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2910done:
2911 return ret;
2912}
2913
Radek Krejcia3045382018-11-22 14:30:31 +01002914/**
2915 * @brief Compile information about the leaf/leaf-list's type.
2916 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002917 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2918 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2919 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2920 * @param[in] context_name Name of the context node or referencing typedef for logging.
2921 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002922 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002923 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002924 * @param[out] dflt Default value for the type.
2925 * @param[out] dflt_mod Local module for the default value.
Radek Krejcia3045382018-11-22 14:30:31 +01002926 * @return LY_ERR value.
2927 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002928static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002929lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002930 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2931 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod)
Radek Krejci19a96102018-11-15 13:38:09 +01002932{
2933 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02002934 ly_bool dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002935 struct type_context {
2936 const struct lysp_tpdf *tpdf;
2937 struct lysp_node *node;
2938 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002939 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002940 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002941 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002942 struct ly_set tpdf_chain = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002943
2944 assert((dflt && dflt_mod) || (!dflt && !dflt_mod));
Radek Krejci19a96102018-11-15 13:38:09 +01002945
2946 (*type) = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002947 if (dflt) {
2948 *dflt = NULL;
2949 *dflt_mod = NULL;
2950 }
Radek Krejci19a96102018-11-15 13:38:09 +01002951
2952 tctx = calloc(1, sizeof *tctx);
2953 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002954 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002955 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2956 ret == LY_SUCCESS;
2957 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2958 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2959 if (basetype) {
2960 break;
2961 }
2962
2963 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002964 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2965 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci87e25252020-09-15 13:28:31 +02002966 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01002967
Radek Krejcicdfecd92018-11-26 11:27:32 +01002968 if (units && !*units) {
2969 /* inherit units */
Radek Krejci87e25252020-09-15 13:28:31 +02002970 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2971 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002972 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002973 if (dflt && !*dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002974 /* inherit default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002975 *dflt = tctx->tpdf->dflt;
2976 *dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002977 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002978 if (dummyloops && (!units || *units) && dflt && *dflt) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002979 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002980 break;
2981 }
2982
Radek Krejci19a96102018-11-15 13:38:09 +01002983 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002984 /* it is not necessary to continue, the rest of the chain was already compiled,
2985 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002986 basetype = tctx->tpdf->type.compiled->basetype;
Radek Krejciba03a5a2020-08-27 14:40:41 +02002987 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02002988 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejciba03a5a2020-08-27 14:40:41 +02002989
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002990 if ((units && !*units) || (dflt && !*dflt)) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002991 dummyloops = 1;
2992 goto preparenext;
2993 } else {
2994 tctx = NULL;
2995 break;
2996 }
Radek Krejci19a96102018-11-15 13:38:09 +01002997 }
2998
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002999 /* circular typedef reference detection */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003000 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003001 /* local part */
Michal Vasko22df3f02020-08-24 13:29:22 +02003002 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003003 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3004 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3005 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3006 free(tctx);
3007 ret = LY_EVALID;
3008 goto cleanup;
3009 }
3010 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003011 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003012 /* global part for unions corner case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003013 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003014 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3015 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3016 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3017 free(tctx);
3018 ret = LY_EVALID;
3019 goto cleanup;
3020 }
3021 }
3022
Radek Krejci19a96102018-11-15 13:38:09 +01003023 /* store information for the following processing */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003024 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02003025 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003026
Radek Krejcicdfecd92018-11-26 11:27:32 +01003027preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003028 /* prepare next loop */
3029 tctx_prev = tctx;
3030 tctx = calloc(1, sizeof *tctx);
3031 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3032 }
3033 free(tctx);
3034
3035 /* allocate type according to the basetype */
3036 switch (basetype) {
3037 case LY_TYPE_BINARY:
3038 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003039 break;
3040 case LY_TYPE_BITS:
3041 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003042 break;
3043 case LY_TYPE_BOOL:
3044 case LY_TYPE_EMPTY:
3045 *type = calloc(1, sizeof(struct lysc_type));
3046 break;
3047 case LY_TYPE_DEC64:
3048 *type = calloc(1, sizeof(struct lysc_type_dec));
3049 break;
3050 case LY_TYPE_ENUM:
3051 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003052 break;
3053 case LY_TYPE_IDENT:
3054 *type = calloc(1, sizeof(struct lysc_type_identityref));
3055 break;
3056 case LY_TYPE_INST:
3057 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3058 break;
3059 case LY_TYPE_LEAFREF:
3060 *type = calloc(1, sizeof(struct lysc_type_leafref));
3061 break;
3062 case LY_TYPE_STRING:
3063 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003064 break;
3065 case LY_TYPE_UNION:
3066 *type = calloc(1, sizeof(struct lysc_type_union));
3067 break;
3068 case LY_TYPE_INT8:
3069 case LY_TYPE_UINT8:
3070 case LY_TYPE_INT16:
3071 case LY_TYPE_UINT16:
3072 case LY_TYPE_INT32:
3073 case LY_TYPE_UINT32:
3074 case LY_TYPE_INT64:
3075 case LY_TYPE_UINT64:
3076 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003077 break;
3078 case LY_TYPE_UNKNOWN:
3079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3080 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3081 ret = LY_EVALID;
3082 goto cleanup;
3083 }
3084 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003085 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3087 ly_data_type2str[basetype]);
3088 free(*type);
3089 (*type) = NULL;
3090 ret = LY_EVALID;
3091 goto cleanup;
3092 }
3093
3094 /* get restrictions from the referred typedefs */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003095 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003096 tctx = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003097
3098 /* remember the typedef context for circular check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003099 ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
3100 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003101
Radek Krejci43699232018-11-23 14:59:46 +01003102 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003103 base = tctx->tpdf->type.compiled;
3104 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003105 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003106 /* no change, just use the type information from the base */
Michal Vasko22df3f02020-08-24 13:29:22 +02003107 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
Radek Krejci19a96102018-11-15 13:38:09 +01003108 ++base->refcount;
3109 continue;
3110 }
3111
3112 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003113 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3115 tctx->tpdf->name, ly_data_type2str[basetype]);
3116 ret = LY_EVALID;
3117 goto cleanup;
3118 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3120 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3121 tctx->tpdf->name, tctx->tpdf->dflt);
3122 ret = LY_EVALID;
3123 goto cleanup;
3124 }
3125
Radek Krejci19a96102018-11-15 13:38:09 +01003126 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003127 /* TODO user type plugins */
3128 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003129 prev_type = *type;
Michal Vasko22df3f02020-08-24 13:29:22 +02003130 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf *)tctx->tpdf)->type,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003131 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003132 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003133 LY_CHECK_GOTO(ret, cleanup);
3134 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003135 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003136 /* remove the processed typedef contexts from the stack for circular check */
3137 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003138
Radek Krejcic5c27e52018-11-15 14:38:11 +01003139 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003140 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003141 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003142 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003143 /* TODO user type plugins */
3144 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003145 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003146 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003147 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003148 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003149 /* no specific restriction in leaf's type definition, copy from the base */
3150 free(*type);
3151 (*type) = base;
3152 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003153 }
3154
Radek Krejci0935f412019-08-20 16:15:18 +02003155 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003156
3157cleanup:
3158 ly_set_erase(&tpdf_chain, free);
3159 return ret;
3160}
3161
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003162/**
3163 * @brief Compile status information of the given node.
3164 *
3165 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3166 * has the status correctly set during the compilation.
3167 *
3168 * @param[in] ctx Compile context
3169 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3170 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3171 * the compatibility with the parent's status value.
3172 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3173 * @return LY_ERR value.
3174 */
3175static LY_ERR
3176lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3177{
3178 /* status - it is not inherited by specification, but it does not make sense to have
3179 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3180 if (!((*node_flags) & LYS_STATUS_MASK)) {
3181 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3182 if ((parent_flags & 0x3) != 0x3) {
3183 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3184 * combination of bits (0x3) which marks the uses_status value */
3185 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
Radek Krejci0f969882020-08-21 16:56:47 +02003186 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003187 }
3188 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3189 } else {
3190 (*node_flags) |= LYS_STATUS_CURR;
3191 }
3192 } else if (parent_flags & LYS_STATUS_MASK) {
3193 /* check status compatibility with the parent */
3194 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3195 if ((*node_flags) & LYS_STATUS_CURR) {
3196 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3197 "A \"current\" status is in conflict with the parent's \"%s\" status.",
Radek Krejci0f969882020-08-21 16:56:47 +02003198 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003199 } else { /* LYS_STATUS_DEPRC */
3200 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3201 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3202 }
3203 return LY_EVALID;
3204 }
3205 }
3206 return LY_SUCCESS;
3207}
3208
Radek Krejci8cce8532019-03-05 11:27:45 +01003209/**
3210 * @brief Check uniqness of the node/action/notification name.
3211 *
3212 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3213 * structures, but they share the namespace so we need to check their name collisions.
3214 *
3215 * @param[in] ctx Compile context.
Michal Vasko20424b42020-08-31 12:29:38 +02003216 * @param[in] parent Parent of the nodes to check, can be NULL.
Radek Krejci8cce8532019-03-05 11:27:45 +01003217 * @param[in] name Name of the item to find in the given lists.
Michal Vasko20424b42020-08-31 12:29:38 +02003218 * @param[in] exclude Node that was just added that should be excluded from the name checking.
Radek Krejci8cce8532019-03-05 11:27:45 +01003219 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3220 */
3221static LY_ERR
Michal Vasko20424b42020-08-31 12:29:38 +02003222lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
3223 const struct lysc_node *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003224{
Michal Vasko20424b42020-08-31 12:29:38 +02003225 const struct lysc_node *iter, *iter2;
3226 const struct lysc_action *actions;
3227 const struct lysc_notif *notifs;
3228 uint32_t getnext_flags;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003229 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003230
Michal Vasko20424b42020-08-31 12:29:38 +02003231#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
3232
3233 if (exclude->nodetype == LYS_CASE) {
3234 /* check restricted only to all the cases */
3235 assert(parent->nodetype == LYS_CHOICE);
3236 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
3237 if (CHECK_NODE(iter, exclude, name)) {
3238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
3239 return LY_EEXIST;
3240 }
3241 }
3242
3243 return LY_SUCCESS;
3244 }
3245
3246 /* no reason for our parent to be choice anymore */
3247 assert(!parent || (parent->nodetype != LYS_CHOICE));
3248
3249 if (parent && (parent->nodetype == LYS_CASE)) {
3250 /* move to the first data definition parent */
3251 parent = lysc_data_parent(parent);
3252 }
3253
3254 getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE;
3255 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
3256 getnext_flags |= LYS_GETNEXT_OUTPUT;
3257 }
3258
3259 iter = NULL;
3260 while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) {
3261 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003262 goto error;
3263 }
Michal Vasko20424b42020-08-31 12:29:38 +02003264
3265 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
3266 if (iter->nodetype == LYS_CHOICE) {
3267 iter2 = NULL;
3268 while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) {
3269 if (CHECK_NODE(iter2, exclude, name)) {
3270 goto error;
3271 }
3272 }
3273 }
Radek Krejci8cce8532019-03-05 11:27:45 +01003274 }
Michal Vasko20424b42020-08-31 12:29:38 +02003275
3276 actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003277 LY_ARRAY_FOR(actions, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003278 if (CHECK_NODE(&actions[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003279 goto error;
3280 }
3281 }
Michal Vasko20424b42020-08-31 12:29:38 +02003282
3283 notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003284 LY_ARRAY_FOR(notifs, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003285 if (CHECK_NODE(&notifs[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003286 goto error;
3287 }
3288 }
3289 return LY_SUCCESS;
Michal Vasko20424b42020-08-31 12:29:38 +02003290
Radek Krejci8cce8532019-03-05 11:27:45 +01003291error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003293 return LY_EEXIST;
Michal Vasko20424b42020-08-31 12:29:38 +02003294
3295#undef CHECK_NODE
Radek Krejci8cce8532019-03-05 11:27:45 +01003296}
3297
Radek Krejciec4da802019-05-02 13:02:41 +02003298static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status);
Radek Krejci19a96102018-11-15 13:38:09 +01003299
Radek Krejcia3045382018-11-22 14:30:31 +01003300/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003301 * @brief Compile parsed RPC/action schema node information.
3302 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003303 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003304 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003305 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3306 * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags).
3307 * Zero means no uses, non-zero value with no status bit set mean the default status.
3308 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3309 */
3310static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003311lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003312 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003313{
3314 LY_ERR ret = LY_SUCCESS;
3315 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003316 LY_ARRAY_COUNT_TYPE u;
3317 uint32_t opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003318
Radek Krejci327de162019-06-14 12:52:07 +02003319 lysc_update_path(ctx, parent, action_p->name);
3320
Michal Vasko20424b42020-08-31 12:29:38 +02003321 /* member needed for uniqueness check lys_getnext() */
3322 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
3323 action->module = ctx->mod;
3324 action->parent = parent;
3325
3326 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action));
Radek Krejci8cce8532019-03-05 11:27:45 +01003327
Radek Krejciec4da802019-05-02 13:02:41 +02003328 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003329 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003330 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003331 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003332 return LY_EVALID;
3333 }
3334
Radek Krejciec4da802019-05-02 13:02:41 +02003335 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003336 action->sp = action_p;
3337 }
3338 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3339
3340 /* status - it is not inherited by specification, but it does not make sense to have
3341 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003342 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003343
Radek Krejci011e4aa2020-09-04 15:22:31 +02003344 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
3345 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
3346 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003347 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003348 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003349
3350 /* input */
Michal Vasko22df3f02020-08-24 13:29:22 +02003351 lysc_update_path(ctx, (struct lysc_node *)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003352 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003353 COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003354 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003355 LY_LIST_FOR(action_p->input.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003356 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003357 }
Radek Krejci327de162019-06-14 12:52:07 +02003358 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003359 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003360
3361 /* output */
Michal Vasko22df3f02020-08-24 13:29:22 +02003362 lysc_update_path(ctx, (struct lysc_node *)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003363 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003364 COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003365 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003366 LY_LIST_FOR(action_p->output.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003367 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003368 }
Radek Krejci327de162019-06-14 12:52:07 +02003369 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003370 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003371
3372 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3373 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003374 ret = ly_set_add(&ctx->xpath, action, 0, NULL);
3375 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003376 }
3377
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003378cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003379 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003380 return ret;
3381}
3382
3383/**
Radek Krejci43981a32019-04-12 09:44:11 +02003384 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003385 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003386 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003387 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3388 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3389 * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags).
Radek Krejcifc11bd72019-04-11 16:00:05 +02003390 * Zero means no uses, non-zero value with no status bit set mean the default status.
3391 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3392 */
3393static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003394lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003395 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
Radek Krejcifc11bd72019-04-11 16:00:05 +02003396{
3397 LY_ERR ret = LY_SUCCESS;
3398 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003399 LY_ARRAY_COUNT_TYPE u;
3400 uint32_t opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003401
Radek Krejci327de162019-06-14 12:52:07 +02003402 lysc_update_path(ctx, parent, notif_p->name);
3403
Michal Vasko20424b42020-08-31 12:29:38 +02003404 /* member needed for uniqueness check lys_getnext() */
3405 notif->nodetype = LYS_NOTIF;
3406 notif->module = ctx->mod;
3407 notif->parent = parent;
3408
3409 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003410
Radek Krejciec4da802019-05-02 13:02:41 +02003411 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003412 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3413 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003414 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003415 return LY_EVALID;
3416 }
3417
Radek Krejciec4da802019-05-02 13:02:41 +02003418 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003419 notif->sp = notif_p;
3420 }
3421 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3422
3423 /* status - it is not inherited by specification, but it does not make sense to have
3424 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003425 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
3426 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003427
Radek Krejci011e4aa2020-09-04 15:22:31 +02003428 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
3429 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
3430 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003431 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003432 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003433 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3434 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003435 ret = ly_set_add(&ctx->xpath, notif, 0, NULL);
3436 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003437 }
Radek Krejci0935f412019-08-20 16:15:18 +02003438 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003439
Radek Krejciec4da802019-05-02 13:02:41 +02003440 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003441 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003442 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status);
3443 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003444 }
3445
Radek Krejci327de162019-06-14 12:52:07 +02003446 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003447cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003448 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003449 return ret;
3450}
3451
3452/**
Radek Krejcia3045382018-11-22 14:30:31 +01003453 * @brief Compile parsed container node information.
3454 * @param[in] ctx Compile context
3455 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003456 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3457 * is enriched with the container-specific information.
3458 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3459 */
Radek Krejci19a96102018-11-15 13:38:09 +01003460static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003461lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003462{
Michal Vasko22df3f02020-08-24 13:29:22 +02003463 struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p;
3464 struct lysc_node_container *cont = (struct lysc_node_container *)node;
Radek Krejci19a96102018-11-15 13:38:09 +01003465 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003466 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003467 LY_ERR ret = LY_SUCCESS;
3468
Radek Krejcife909632019-02-12 15:34:42 +01003469 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003470 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003471 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003472 } else if (cont_p->musts) {
3473 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003474 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3475 cont->flags |= LYS_PRESENCE;
3476 } else if (cont_p->when) {
3477 /* container with a when condition */
3478 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"when\" condition.", cont_p->name);
Michal Vaskoba417ac2020-08-06 14:48:20 +02003479 cont->flags |= LYS_PRESENCE;
3480 } else if (cont_p->parent) {
3481 if (cont_p->parent->nodetype == LYS_CHOICE) {
3482 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003483 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
Michal Vaskoba417ac2020-08-06 14:48:20 +02003484 cont_p->name, cont_p->parent->name);
3485 cont->flags |= LYS_PRESENCE;
3486 } else if ((cont_p->parent->nodetype == LYS_CASE)
3487 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3488 /* container is the only node in a case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003489 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
Michal Vaskoba417ac2020-08-06 14:48:20 +02003490 cont_p->name, cont_p->parent->name);
3491 cont->flags |= LYS_PRESENCE;
3492 }
Radek Krejcife909632019-02-12 15:34:42 +01003493 }
3494
Michal Vaskoba417ac2020-08-06 14:48:20 +02003495 /* more cases when the container has meaning but is kept NP for convenience:
3496 * - when condition
3497 * - direct child action/notification
3498 */
3499
Radek Krejci19a96102018-11-15 13:38:09 +01003500 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003501 ret = lys_compile_node(ctx, child_p, node, 0);
3502 LY_CHECK_GOTO(ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003503 }
3504
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003505 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003506 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3507 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003508 ret = ly_set_add(&ctx->xpath, cont, 0, NULL);
3509 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003510 }
Radek Krejciec4da802019-05-02 13:02:41 +02003511 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3512 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003513
3514done:
3515 return ret;
3516}
3517
Radek Krejci33f72892019-02-21 10:36:58 +01003518/*
3519 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3520 * @param[in] ctx Compile context.
3521 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3522 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003523 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3524 * @return LY_ERR value.
3525 */
3526static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003527lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003528 struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003529{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003530 const char *dflt;
3531 struct lys_module *dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003532
Radek Krejciec4da802019-05-02 13:02:41 +02003533 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003534 leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod));
3535
3536 /* store default value, if any */
3537 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
3538 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, dflt, dflt_mod));
Radek Krejci33f72892019-02-21 10:36:58 +01003539 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003540
Radek Krejci33f72892019-02-21 10:36:58 +01003541 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3542 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003543 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003544 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003545 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003546 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
3547 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
Radek Krejci33f72892019-02-21 10:36:58 +01003548 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003549 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003550 }
3551 }
3552 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003553 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3555 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3556 return LY_EVALID;
3557 }
3558 }
3559
Radek Krejci33f72892019-02-21 10:36:58 +01003560 return LY_SUCCESS;
3561}
3562
Radek Krejcia3045382018-11-22 14:30:31 +01003563/**
3564 * @brief Compile parsed leaf node information.
3565 * @param[in] ctx Compile context
3566 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003567 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3568 * is enriched with the leaf-specific information.
3569 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3570 */
Radek Krejci19a96102018-11-15 13:38:09 +01003571static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003572lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003573{
Michal Vasko22df3f02020-08-24 13:29:22 +02003574 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p;
3575 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003576 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003577 LY_ERR ret = LY_SUCCESS;
3578
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003579 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003580 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3581 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003582 ret = ly_set_add(&ctx->xpath, leaf, 0, NULL);
3583 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003584 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003585 if (leaf_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003586 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003587 leaf->flags |= LYS_SET_UNITS;
3588 }
Radek Krejcia1911222019-07-22 17:24:50 +02003589
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003590 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003591 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3592 LY_CHECK_GOTO(ret, done);
Radek Krejcia1911222019-07-22 17:24:50 +02003593
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003594 /* store/update default value */
Radek Krejciccd20f12019-02-15 14:12:27 +01003595 if (leaf_p->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003596 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, leaf_p->dflt, ctx->mod_def));
Radek Krejci76b3e962018-12-14 17:01:25 +01003597 leaf->flags |= LYS_SET_DFLT;
3598 }
Radek Krejci43699232018-11-23 14:59:46 +01003599
Radek Krejci19a96102018-11-15 13:38:09 +01003600done:
3601 return ret;
3602}
3603
Radek Krejcia3045382018-11-22 14:30:31 +01003604/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003605 * @brief Compile parsed leaf-list node information.
3606 * @param[in] ctx Compile context
3607 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003608 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3609 * is enriched with the leaf-list-specific information.
3610 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3611 */
3612static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003613lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003614{
Michal Vasko22df3f02020-08-24 13:29:22 +02003615 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p;
3616 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003617 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003618 LY_ERR ret = LY_SUCCESS;
3619
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003620 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003621 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3622 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003623 ret = ly_set_add(&ctx->xpath, llist, 0, NULL);
3624 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003625 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003626 if (llist_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003627 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003628 llist->flags |= LYS_SET_UNITS;
3629 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003630
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003631 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003632 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist);
3633 LY_CHECK_GOTO(ret, done);
Michal Vasko6a044b22020-01-15 12:25:39 +01003634
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003635 /* store/update default values */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003636 if (llist_p->dflts) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003637 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, llist_p->dflts, ctx->mod_def), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003638 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003639 }
3640
3641 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003642 if (llist->min) {
3643 llist->flags |= LYS_MAND_TRUE;
3644 }
Radek Krejcib7408632018-11-28 17:12:11 +01003645 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003646
Radek Krejci0e5d8382018-11-28 16:37:53 +01003647done:
3648 return ret;
3649}
3650
3651/**
Radek Krejci7af64242019-02-18 13:07:53 +01003652 * @brief Compile information about list's uniques.
3653 * @param[in] ctx Compile context.
3654 * @param[in] context_module Module where the prefixes are going to be resolved.
3655 * @param[in] uniques Sized array list of unique statements.
3656 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3657 * @return LY_ERR value.
3658 */
3659static LY_ERR
3660lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3661{
3662 LY_ERR ret = LY_SUCCESS;
3663 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003664 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003665 const char *keystr, *delim;
3666 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003667 LY_ARRAY_COUNT_TYPE v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003668 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003669 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003670
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003671 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003672 config = -1;
3673 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3674 keystr = uniques[v];
3675 while (keystr) {
3676 delim = strpbrk(keystr, " \t\n");
3677 if (delim) {
3678 len = delim - keystr;
3679 while (isspace(*delim)) {
3680 ++delim;
3681 }
3682 } else {
3683 len = strlen(keystr);
3684 }
3685
3686 /* unique node must be present */
3687 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko22df3f02020-08-24 13:29:22 +02003688 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0,
3689 (const struct lysc_node **)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003690 if (ret != LY_SUCCESS) {
3691 if (ret == LY_EDENIED) {
3692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003693 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003694 len, keystr, lys_nodetype2str((*key)->nodetype));
3695 }
3696 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003697 } else if (flags) {
3698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3699 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003700 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003701 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003702 }
3703
3704 /* all referenced leafs must be of the same config type */
3705 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003707 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003708 return LY_EVALID;
3709 } else if ((*key)->flags & LYS_CONFIG_W) {
3710 config = 1;
3711 } else { /* LYS_CONFIG_R */
3712 config = 0;
3713 }
3714
Michal Vasko14654712020-02-06 08:35:21 +01003715 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3716 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3717 if (parent->nodetype == LYS_LIST) {
3718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3719 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3720 return LY_EVALID;
3721 }
3722 }
3723
Radek Krejci7af64242019-02-18 13:07:53 +01003724 /* check status */
3725 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0f969882020-08-21 16:56:47 +02003726 (*key)->flags, (*key)->module, (*key)->name));
Radek Krejci7af64242019-02-18 13:07:53 +01003727
3728 /* mark leaf as unique */
3729 (*key)->flags |= LYS_UNIQUE;
3730
3731 /* next unique value in line */
3732 keystr = delim;
3733 }
3734 /* next unique definition */
3735 }
3736
3737 return LY_SUCCESS;
3738}
3739
3740/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003741 * @brief Compile parsed list node information.
3742 * @param[in] ctx Compile context
3743 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003744 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3745 * is enriched with the list-specific information.
3746 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3747 */
3748static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003749lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003750{
Michal Vasko22df3f02020-08-24 13:29:22 +02003751 struct lysp_node_list *list_p = (struct lysp_node_list *)node_p;
3752 struct lysc_node_list *list = (struct lysc_node_list *)node;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003753 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003754 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003755 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003756 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003757 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003758 LY_ERR ret = LY_SUCCESS;
3759
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003760 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003761 if (list->min) {
3762 list->flags |= LYS_MAND_TRUE;
3763 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003764 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3765
3766 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003767 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003768 }
3769
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003770 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003771 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3772 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003773 LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL));
Michal Vasko5d8756a2019-11-07 15:21:00 +01003774 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003775
3776 /* keys */
3777 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3779 return LY_EVALID;
3780 }
3781
3782 /* find all the keys (must be direct children) */
3783 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003784 if (!keystr) {
3785 /* keyless list */
3786 list->flags |= LYS_KEYLESS;
3787 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003788 while (keystr) {
3789 delim = strpbrk(keystr, " \t\n");
3790 if (delim) {
3791 len = delim - keystr;
3792 while (isspace(*delim)) {
3793 ++delim;
3794 }
3795 } else {
3796 len = strlen(keystr);
3797 }
3798
3799 /* key node must be present */
Michal Vasko22df3f02020-08-24 13:29:22 +02003800 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003801 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3803 "The list's key \"%.*s\" not found.", len, keystr);
3804 return LY_EVALID;
3805 }
3806 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003807 if (key->flags & LYS_KEY) {
3808 /* the node was already marked as a key */
3809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3810 "Duplicated key identifier \"%.*s\".", len, keystr);
3811 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003812 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003813
Michal Vasko22df3f02020-08-24 13:29:22 +02003814 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003815 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003816 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003817 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3818 return LY_EVALID;
3819 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003820 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003821 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003822 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003823 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003824 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003825 return LY_EVALID;
3826 }
3827 } else {
3828 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003829 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003830 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003831 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003832 return LY_EVALID;
3833 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003834 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003836 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003837 return LY_EVALID;
3838 }
3839 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003840
3841 /* check status */
3842 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003843 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003844
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003845 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003846 if (key->dflt) {
3847 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3848 lysc_type_free(ctx->ctx, key->dflt->realtype);
3849 free(key->dflt);
3850 key->dflt = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003851 }
3852 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003853 key->flags |= LYS_KEY;
3854
3855 /* move it to the correct position */
Michal Vasko22df3f02020-08-24 13:29:22 +02003856 if ((prev_key && (struct lysc_node *)prev_key != key->prev) || (!prev_key && key->prev->next)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02003857 /* fix links in closest previous siblings of the key */
3858 if (key->next) {
3859 key->next->prev = key->prev;
3860 } else {
3861 /* last child */
3862 list->child->prev = key->prev;
3863 }
3864 if (key->prev->next) {
3865 key->prev->next = key->next;
3866 }
3867 /* fix links in the key */
3868 if (prev_key) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003869 key->prev = (struct lysc_node *)prev_key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003870 key->next = prev_key->next;
3871 } else {
3872 key->prev = list->child->prev;
3873 key->next = list->child;
3874 }
3875 /* fix links in closes future siblings of the key */
3876 if (prev_key) {
3877 if (prev_key->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003878 prev_key->next->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003879 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003880 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003881 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003882 prev_key->next = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003883 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003884 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003885 }
3886 /* fix links in parent */
3887 if (!key->prev->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003888 list->child = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003889 }
3890 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003891
3892 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003893 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003894 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003895 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003896 }
3897
3898 /* uniques */
3899 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003900 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003901 }
3902
Radek Krejciec4da802019-05-02 13:02:41 +02003903 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3904 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003905
3906done:
3907 return ret;
3908}
3909
Radek Krejcib56c7502019-02-13 14:19:54 +01003910/**
3911 * @brief Do some checks and set the default choice's case.
3912 *
3913 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3914 *
3915 * @param[in] ctx Compile context.
3916 * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself,
3917 * not the reference to the imported module.
3918 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3919 * @return LY_ERR value.
3920 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003921static LY_ERR
3922lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3923{
Michal Vasko22df3f02020-08-24 13:29:22 +02003924 struct lysc_node *iter, *node = (struct lysc_node *)ch;
Radek Krejci76b3e962018-12-14 17:01:25 +01003925 const char *prefix = NULL, *name;
3926 size_t prefix_len = 0;
3927
3928 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3929 name = strchr(dflt, ':');
3930 if (name) {
3931 prefix = dflt;
3932 prefix_len = name - prefix;
3933 ++name;
3934 } else {
3935 name = dflt;
3936 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003937 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003938 /* prefixed default case make sense only for the prefix of the schema itself */
3939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3940 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3941 prefix_len, prefix);
3942 return LY_EVALID;
3943 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003944 ch->dflt = (struct lysc_node_case *)lys_find_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejci76b3e962018-12-14 17:01:25 +01003945 if (!ch->dflt) {
3946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3947 "Default case \"%s\" not found.", dflt);
3948 return LY_EVALID;
3949 }
3950 /* no mandatory nodes directly under the default case */
3951 LY_LIST_FOR(ch->dflt->child, iter) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003952 if (iter->parent != (struct lysc_node *)ch->dflt) {
Radek Krejcife13da42019-02-15 14:51:01 +01003953 break;
3954 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003955 if (iter->flags & LYS_MAND_TRUE) {
3956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3957 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3958 return LY_EVALID;
3959 }
3960 }
Radek Krejci01342af2019-01-03 15:18:08 +01003961 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003962 return LY_SUCCESS;
3963}
3964
Radek Krejciccd20f12019-02-15 14:12:27 +01003965static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003966lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003967{
3968 struct lys_module *mod;
3969 const char *prefix = NULL, *name;
3970 size_t prefix_len = 0;
3971 struct lysc_node_case *cs;
3972 struct lysc_node *node;
3973
3974 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3975 name = strchr(dflt, ':');
3976 if (name) {
3977 prefix = dflt;
3978 prefix_len = name - prefix;
3979 ++name;
3980 } else {
3981 name = dflt;
3982 }
3983 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3984 if (prefix) {
3985 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003987 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3988 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003989 return LY_EVALID;
3990 }
3991 } else {
3992 mod = ctx->mod;
3993 }
3994 /* get the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003995 cs = (struct lysc_node_case *)lys_find_child((struct lysc_node *)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejciccd20f12019-02-15 14:12:27 +01003996 if (!cs) {
3997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003998 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003999 return LY_EVALID;
4000 }
4001
4002 /* check that there is no mandatory node */
4003 LY_LIST_FOR(cs->child, node) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004004 if (node->parent != (struct lysc_node *)cs) {
Radek Krejcife13da42019-02-15 14:51:01 +01004005 break;
4006 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004007 if (node->flags & LYS_MAND_TRUE) {
4008 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004009 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4010 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004011 return LY_EVALID;
4012 }
4013 }
4014
4015 /* set the default case in choice */
4016 ch->dflt = cs;
4017 cs->flags |= LYS_SET_DFLT;
4018
4019 return LY_SUCCESS;
4020}
4021
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004022/**
Michal Vasko20424b42020-08-31 12:29:38 +02004023 * @brief Compile choice children.
4024 *
4025 * @param[in] ctx Compile context
4026 * @param[in] child_p Parsed choice children nodes.
4027 * @param[in] node Compiled choice node to compile and add children to.
4028 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4029 */
4030static LY_ERR
4031lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node)
4032{
4033 LY_ERR ret = LY_SUCCESS;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004034 struct lysp_node *child_p_next = child_p->next;
Michal Vasko20424b42020-08-31 12:29:38 +02004035 struct lysp_node_case *cs_p;
4036
4037 if (child_p->nodetype == LYS_CASE) {
4038 /* standard case under choice */
4039 ret = lys_compile_node(ctx, child_p, node, 0);
4040 } else {
4041 /* we need the implicit case first, so create a fake parsed case */
4042 cs_p = calloc(1, sizeof *cs_p);
4043 cs_p->nodetype = LYS_CASE;
Radek Krejci87e25252020-09-15 13:28:31 +02004044 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
Michal Vasko20424b42020-08-31 12:29:38 +02004045 cs_p->child = child_p;
4046
4047 /* make the child the only case child */
Michal Vasko20424b42020-08-31 12:29:38 +02004048 child_p->next = NULL;
4049
4050 /* compile it normally */
4051 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0);
4052
Radek Krejci87e25252020-09-15 13:28:31 +02004053free_fake_node:
Michal Vasko20424b42020-08-31 12:29:38 +02004054 /* free the fake parsed node and correct pointers back */
4055 cs_p->child = NULL;
4056 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
Radek Krejci8f5fad22020-09-15 16:50:54 +02004057 child_p->next = child_p_next;
Michal Vasko20424b42020-08-31 12:29:38 +02004058 }
4059
4060 return ret;
4061}
4062
4063/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004064 * @brief Compile parsed choice node information.
Michal Vasko20424b42020-08-31 12:29:38 +02004065 *
Radek Krejci056d0a82018-12-06 16:57:25 +01004066 * @param[in] ctx Compile context
4067 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004068 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004069 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004070 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4071 */
4072static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004073lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004074{
Michal Vasko22df3f02020-08-24 13:29:22 +02004075 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p;
4076 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
Michal Vasko20424b42020-08-31 12:29:38 +02004077 struct lysp_node *child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004078 LY_ERR ret = LY_SUCCESS;
4079
Michal Vasko20424b42020-08-31 12:29:38 +02004080 assert(node->nodetype == LYS_CHOICE);
4081
Radek Krejci056d0a82018-12-06 16:57:25 +01004082 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko20424b42020-08-31 12:29:38 +02004083 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node));
Radek Krejci056d0a82018-12-06 16:57:25 +01004084 }
4085
4086 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004087 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004088 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004089 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004090
Radek Krejci9800fb82018-12-13 14:26:23 +01004091 return ret;
4092}
4093
4094/**
4095 * @brief Compile parsed anydata or anyxml node information.
4096 * @param[in] ctx Compile context
4097 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004098 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4099 * is enriched with the any-specific information.
4100 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4101 */
4102static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004103lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004104{
Michal Vasko22df3f02020-08-24 13:29:22 +02004105 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p;
4106 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004107 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9800fb82018-12-13 14:26:23 +01004108 LY_ERR ret = LY_SUCCESS;
4109
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004110 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004111 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4112 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004113 ret = ly_set_add(&ctx->xpath, any, 0, NULL);
4114 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004115 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004116
4117 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004118 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4119 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004120 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004121done:
4122 return ret;
4123}
4124
Radek Krejcib56c7502019-02-13 14:19:54 +01004125/**
Michal Vasko795b3752020-07-13 15:24:27 +02004126 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4127 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004128 *
4129 * @param[in] ctx Compile context
4130 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4131 * the choice itself is expected instead of a specific case node.
4132 * @param[in] node Schema node to connect into the list.
4133 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004134 * 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 +01004135 */
4136static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004137lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004138{
Michal Vasko795b3752020-07-13 15:24:27 +02004139 struct lysc_node **children, *start, *end;
4140 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004141
Michal Vasko20424b42020-08-31 12:29:38 +02004142 node->parent = parent;
4143
4144 if (parent) {
4145 if (parent->nodetype == LYS_CHOICE) {
4146 assert(node->nodetype == LYS_CASE);
4147 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
4148 } else {
4149 children = lysc_node_children_p(parent, ctx->options);
4150 }
4151 assert(children);
4152
Radek Krejci056d0a82018-12-06 16:57:25 +01004153 if (!(*children)) {
4154 /* first child */
4155 *children = node;
4156 } else if (*children != node) {
4157 /* by the condition in previous branch we cover the choice/case children
4158 * - the children list is shared by the choice and the the first case, in addition
4159 * the first child of each case must be referenced from the case node. So the node is
4160 * actually always already inserted in case it is the first children - so here such
4161 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004162 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4163 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4164 /* some augments are already connected and we are connecting new ones,
4165 * keep module name order and insert the node into the children list */
4166 end = (*children);
4167 do {
4168 end = end->prev;
4169 mod = end->module;
4170 while (end->prev->module == mod) {
4171 end = end->prev;
4172 }
Radek Krejcif6a11002020-08-21 13:29:07 +02004173 } 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 +02004174
4175 /* we have the last existing node after our node, easily get the first before and connect it */
4176 start = end->prev;
4177 start->next = node;
4178 node->next = end;
4179 end->prev = node;
4180 node->prev = start;
4181 } else {
4182 /* insert at the end of the parent's children list */
4183 (*children)->prev->next = node;
4184 node->prev = (*children)->prev;
4185 (*children)->prev = node;
4186 }
Michal Vasko20424b42020-08-31 12:29:38 +02004187 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004188
Michal Vasko20424b42020-08-31 12:29:38 +02004189 /* check the name uniqueness (even for an only child, it may be in case) */
4190 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
4191 return LY_EEXIST;
4192 }
4193 } else {
4194 /* top-level element */
4195 if (!ctx->mod->compiled->data) {
4196 ctx->mod->compiled->data = node;
4197 } else {
4198 /* insert at the end of the module's top-level nodes list */
4199 ctx->mod->compiled->data->prev->next = node;
4200 node->prev = ctx->mod->compiled->data->prev;
4201 ctx->mod->compiled->data->prev = node;
4202 }
4203
4204 /* check the name uniqueness on top-level */
4205 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
4206 return LY_EEXIST;
Radek Krejci056d0a82018-12-06 16:57:25 +01004207 }
4208 }
Michal Vasko20424b42020-08-31 12:29:38 +02004209
Radek Krejci056d0a82018-12-06 16:57:25 +01004210 return LY_SUCCESS;
4211}
4212
Radek Krejci95710c92019-02-11 15:49:55 +01004213/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004214 * @brief Prepare the case structure in choice node for the new data node.
4215 *
4216 * 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
4217 * created in the choice when the first child was processed.
4218 *
4219 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004220 * @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 +02004221 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004222 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4223 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4224 * @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,
4225 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004226 */
Michal Vasko20424b42020-08-31 12:29:38 +02004227static LY_ERR
4228lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004229{
Michal Vasko20424b42020-08-31 12:29:38 +02004230 struct lysp_node *child_p;
4231 struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004232
Radek Krejci39424802020-08-12 09:31:00 +02004233 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004234 /* we have to add an implicit case node into the parent choice */
Radek Krejci95710c92019-02-11 15:49:55 +01004235 } else if (node_p->nodetype == LYS_CASE) {
Michal Vasko20424b42020-08-31 12:29:38 +02004236 /* explicit parent case */
4237 LY_LIST_FOR(cs_p->child, child_p) {
4238 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004239 }
Radek Krejci95710c92019-02-11 15:49:55 +01004240 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004241 LOGINT_RET(ctx->ctx);
Radek Krejci056d0a82018-12-06 16:57:25 +01004242 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004243
Michal Vasko20424b42020-08-31 12:29:38 +02004244 return LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01004245}
4246
Radek Krejcib56c7502019-02-13 14:19:54 +01004247/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004248 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004249 *
4250 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004251 * @param[in] node Target node where the config is supposed to be changed.
4252 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004253 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4254 * 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 +01004255 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4256 * @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 +01004257 * @return LY_ERR value.
4258 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004259static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004260lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci857189e2020-09-01 13:26:36 +02004261 ly_bool inheriting, ly_bool refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004262{
4263 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004264 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004265
4266 if (config == (node->flags & LYS_CONFIG_MASK)) {
4267 /* nothing to do */
4268 return LY_SUCCESS;
4269 }
4270
4271 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004272 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004273 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004275 "Invalid %s of config - configuration node cannot be child of any state data node.",
4276 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004277 return LY_EVALID;
4278 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004279 node->flags |= LYS_SET_CONFIG;
4280 } else {
4281 if (node->flags & LYS_SET_CONFIG) {
4282 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4283 /* setting config flags, but have node with explicit config true */
4284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004285 "Invalid %s of config - configuration node cannot be child of any state data node.",
4286 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004287 return LY_EVALID;
4288 }
4289 /* do not change config on nodes where the config is explicitely set, this does not apply to
4290 * nodes, which are being changed explicitly (targets of refine or deviation) */
4291 return LY_SUCCESS;
4292 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004293 }
4294 node->flags &= ~LYS_CONFIG_MASK;
4295 node->flags |= config;
4296
4297 /* inherit the change into the children */
Michal Vasko22df3f02020-08-24 13:29:22 +02004298 LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004299 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004300 }
4301
Radek Krejci76b3e962018-12-14 17:01:25 +01004302 return LY_SUCCESS;
4303}
4304
Radek Krejcib56c7502019-02-13 14:19:54 +01004305/**
4306 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4307 *
4308 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4309 * the flag to such parents from a mandatory children.
4310 *
4311 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4312 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4313 * (mandatory children was removed).
4314 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02004315static void
Radek Krejci857189e2020-09-01 13:26:36 +02004316lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
Radek Krejcife909632019-02-12 15:34:42 +01004317{
4318 struct lysc_node *iter;
4319
4320 if (add) { /* set flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004321 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
Radek Krejcife909632019-02-12 15:34:42 +01004322 parent = parent->parent) {
4323 parent->flags |= LYS_MAND_TRUE;
4324 }
4325 } else { /* unset flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004326 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004327 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004328 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004329 /* there is another mandatory node */
4330 return;
4331 }
4332 }
4333 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4334 parent->flags &= ~LYS_MAND_TRUE;
4335 }
4336 }
4337}
4338
Radek Krejci056d0a82018-12-06 16:57:25 +01004339/**
Radek Krejci3641f562019-02-13 15:38:40 +01004340 * @brief Internal sorting process for the lys_compile_augment_sort().
4341 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4342 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4343 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4344 */
4345static void
4346lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4347{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004348 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004349 size_t len;
4350
4351 len = strlen(aug_p->nodeid);
4352 LY_ARRAY_FOR(result, v) {
4353 if (strlen(result[v]->nodeid) <= len) {
4354 continue;
4355 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004356 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004357 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004358 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004359 break;
4360 }
4361 }
4362 result[v] = aug_p;
4363 LY_ARRAY_INCREMENT(result);
4364}
4365
4366/**
4367 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4368 *
4369 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4370 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4371 *
4372 * @param[in] ctx Compile context.
4373 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4374 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4375 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4376 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4377 * @return LY_ERR value.
4378 */
4379LY_ERR
4380lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4381{
4382 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004383 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004384
4385 assert(augments);
4386
4387 /* get count of the augments in module and all its submodules */
4388 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004389 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004390 }
4391 LY_ARRAY_FOR(inc_p, u) {
4392 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004393 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004394 }
4395 }
4396
4397 if (!count) {
4398 *augments = NULL;
4399 return LY_SUCCESS;
4400 }
4401 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4402
4403 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4404 * together, so there can be even /z/y betwwen them. */
4405 LY_ARRAY_FOR(aug_p, u) {
4406 lys_compile_augment_sort_(&aug_p[u], result);
4407 }
4408 LY_ARRAY_FOR(inc_p, u) {
4409 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4410 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4411 }
4412 }
4413
4414 *augments = result;
4415 return LY_SUCCESS;
4416}
4417
4418/**
4419 * @brief Compile the parsed augment connecting it into its target.
4420 *
4421 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4422 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4423 * are already implemented and compiled.
4424 *
4425 * @param[in] ctx Compile context.
4426 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004427 * @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
4428 * children in case of the augmenting uses data.
4429 * @return LY_SUCCESS on success.
4430 * @return LY_EVALID on failure.
4431 */
4432LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004433lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004434{
Michal Vaskoe6143202020-07-03 13:02:08 +02004435 LY_ERR ret = LY_SUCCESS, rc;
Michal Vasko20424b42020-08-31 12:29:38 +02004436 struct lysp_node *node_p;
Radek Krejci3641f562019-02-13 15:38:40 +01004437 struct lysc_node *target; /* target target of the augment */
4438 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004439 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004440 struct lys_module **aug_mod;
Radek Krejci857189e2020-09-01 13:26:36 +02004441 ly_bool allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004442 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004443 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004444 uint32_t opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004445
Radek Krejci327de162019-06-14 12:52:07 +02004446 lysc_update_path(ctx, NULL, "{augment}");
4447 lysc_update_path(ctx, NULL, aug_p->nodeid);
4448
Michal Vaskocb18c7f2020-08-24 09:22:28 +02004449 ret = lysc_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004450 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Michal Vasko22df3f02020-08-24 13:29:22 +02004451 1, (const struct lysc_node **)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004452 if (ret != LY_SUCCESS) {
4453 if (ret == LY_EDENIED) {
4454 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4455 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4456 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4457 }
4458 return LY_EVALID;
4459 }
4460
4461 /* check for mandatory nodes
4462 * - new cases augmenting some choice can have mandatory nodes
4463 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4464 */
Radek Krejci733988a2019-02-15 15:12:44 +01004465 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004466 allow_mandatory = 1;
4467 }
4468
4469 when_shared = NULL;
4470 LY_LIST_FOR(aug_p->child, node_p) {
4471 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4472 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004473 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004474 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4475 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004477 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4478 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004479 return LY_EVALID;
4480 }
4481
4482 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004483 ctx->options |= flags;
Michal Vasko20424b42020-08-31 12:29:38 +02004484 if (target->nodetype == LYS_CHOICE) {
4485 LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target));
Radek Krejci3641f562019-02-13 15:38:40 +01004486 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004487 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004488 }
Radek Krejciec4da802019-05-02 13:02:41 +02004489 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004490
Radek Krejcife13da42019-02-15 14:51:01 +01004491 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4492 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004493 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004494 /* 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 +02004495 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 +01004496 } else if (target->nodetype == LYS_CHOICE) {
4497 /* 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 +02004498 node = ((struct lysc_node_choice *)target)->cases->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004499 } else {
4500 /* the compiled node is the last child of the target */
Michal Vasko22df3f02020-08-24 13:29:22 +02004501 node = (struct lysc_node *)lysc_node_children(target, flags);
Radek Krejci7c7783d2020-04-08 15:34:39 +02004502 if (!node) {
4503 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4504 break;
4505 }
4506 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004507 }
4508
Radek Krejci733988a2019-02-15 15:12:44 +01004509 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004510 node->flags &= ~LYS_MAND_TRUE;
4511 lys_compile_mandatory_parents(target, 0);
4512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004513 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004514 return LY_EVALID;
4515 }
4516
4517 /* pass augment's when to all the children */
4518 if (aug_p->when) {
4519 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4520 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004521 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004522 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004523
4524 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4525 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004526 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4527 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004528 }
4529
Radek Krejci3641f562019-02-13 15:38:40 +01004530 when_shared = *when;
4531 } else {
4532 ++when_shared->refcount;
4533 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004534
4535 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4536 /* in this case check "when" again for all children because of dummy node check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004537 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4538 LY_CHECK_GOTO(ret, error);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004539 }
Radek Krejci3641f562019-02-13 15:38:40 +01004540 }
4541 }
4542 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004543
Radek Krejciec4da802019-05-02 13:02:41 +02004544 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004545 switch (target->nodetype) {
4546 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004547 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004548 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004549 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004550 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004551 break;
4552 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004553 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004554 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004555 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004556 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004557 break;
4558 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004559 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004560 if (aug_p->actions) {
4561 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004562 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4563 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004564 return LY_EVALID;
4565 }
4566 if (aug_p->notifs) {
4567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004568 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004569 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004570 return LY_EVALID;
4571 }
4572 }
Radek Krejci3641f562019-02-13 15:38:40 +01004573
Michal Vaskoe6143202020-07-03 13:02:08 +02004574 /* add this module into the target module augmented_by, if not there already */
4575 rc = LY_SUCCESS;
4576 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4577 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4578 rc = LY_EEXIST;
4579 break;
4580 }
4581 }
4582 if (!rc) {
4583 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4584 *aug_mod = ctx->mod;
4585 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004586
Radek Krejci327de162019-06-14 12:52:07 +02004587 lysc_update_path(ctx, NULL, NULL);
4588 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004589
Radek Krejci3641f562019-02-13 15:38:40 +01004590error:
Radek Krejciec4da802019-05-02 13:02:41 +02004591 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004592 return ret;
4593}
4594
4595/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004596 * @brief Apply refined or deviated mandatory flag to the target node.
4597 *
4598 * @param[in] ctx Compile context.
4599 * @param[in] node Target node where the mandatory property is supposed to be changed.
4600 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004601 * @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 +01004602 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4603 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4604 * 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 +01004605 * @return LY_ERR value.
4606 */
4607static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004608lys_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 +01004609{
4610 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4611 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004612 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4613 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004614 return LY_EVALID;
4615 }
4616
4617 if (mandatory_flag & LYS_MAND_TRUE) {
4618 /* check if node has default value */
4619 if (node->nodetype & LYS_LEAF) {
4620 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004621 if (refine_flag) {
4622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004623 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004624 return LY_EVALID;
4625 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004626 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004627 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004628 if (refine_flag) {
4629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004630 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004631 return LY_EVALID;
4632 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004633 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004634 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004635 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 +01004636 return LY_EVALID;
4637 }
4638
4639 node->flags &= ~LYS_MAND_FALSE;
4640 node->flags |= LYS_MAND_TRUE;
4641 lys_compile_mandatory_parents(node->parent, 1);
4642 } else {
4643 /* make mandatory false */
4644 node->flags &= ~LYS_MAND_TRUE;
4645 node->flags |= LYS_MAND_FALSE;
4646 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcif1421c22019-02-19 13:05:20 +01004647 }
4648 return LY_SUCCESS;
4649}
4650
4651/**
Michal Vasko601ddb32020-08-31 16:25:35 +02004652 * @brief Find grouping for a uses.
Radek Krejcie86bf772018-12-14 11:39:53 +01004653 *
Michal Vasko601ddb32020-08-31 16:25:35 +02004654 * @param[in] ctx Compile context.
4655 * @param[in] uses_p Parsed uses node.
4656 * @param[out] gpr_p Found grouping on success.
4657 * @param[out] grp_mod Module of @p grp_p on success.
4658 * @return LY_ERR value.
Radek Krejcie86bf772018-12-14 11:39:53 +01004659 */
4660static LY_ERR
Michal Vasko601ddb32020-08-31 16:25:35 +02004661lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
4662 struct lys_module **grp_mod)
Radek Krejcie86bf772018-12-14 11:39:53 +01004663{
4664 struct lysp_node *node_p;
Michal Vasko601ddb32020-08-31 16:25:35 +02004665 struct lysp_grp *grp;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004666 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +02004667 ly_bool found = 0;
Radek Krejcie86bf772018-12-14 11:39:53 +01004668 const char *id, *name, *prefix;
4669 size_t prefix_len, name_len;
Michal Vasko601ddb32020-08-31 16:25:35 +02004670 struct lys_module *mod;
4671
4672 *grp_p = NULL;
4673 *grp_mod = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004674
4675 /* search for the grouping definition */
Radek Krejcie86bf772018-12-14 11:39:53 +01004676 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004677 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004678 if (prefix) {
4679 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4680 if (!mod) {
4681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004682 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004683 return LY_EVALID;
4684 }
4685 } else {
4686 mod = ctx->mod_def;
4687 }
4688 if (mod == ctx->mod_def) {
4689 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004690 grp = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004691 LY_ARRAY_FOR(grp, u) {
4692 if (!strcmp(grp[u].name, name)) {
4693 grp = &grp[u];
4694 found = 1;
4695 break;
4696 }
4697 }
4698 }
4699 }
4700 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004701 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004702 grp = mod->parsed->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004703 LY_ARRAY_FOR(grp, u) {
4704 if (!strcmp(grp[u].name, name)) {
4705 grp = &grp[u];
4706 found = 1;
4707 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004708 }
4709 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004710 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004711 /* ... and all the submodules */
Michal Vasko601ddb32020-08-31 16:25:35 +02004712 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004713 grp = mod->parsed->includes[u].submodule->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004714 LY_ARRAY_FOR(grp, v) {
4715 if (!strcmp(grp[v].name, name)) {
4716 grp = &grp[v];
4717 found = 1;
4718 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004719 }
4720 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004721 if (found) {
4722 break;
4723 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004724 }
4725 }
4726 }
4727 if (!found) {
4728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4729 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4730 return LY_EVALID;
4731 }
4732
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004733 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4734 /* remember that the grouping is instantiated to avoid its standalone validation */
4735 grp->flags |= LYS_USED_GRP;
4736 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004737
Michal Vasko601ddb32020-08-31 16:25:35 +02004738 *grp_p = grp;
4739 *grp_mod = mod;
4740 return LY_SUCCESS;
4741}
Radek Krejcie86bf772018-12-14 11:39:53 +01004742
Michal Vasko601ddb32020-08-31 16:25:35 +02004743static LY_ERR
4744lys_compile_refines(struct lysc_ctx *ctx, struct lysp_refine *refines, const struct lysc_node *context_node)
4745{
4746 struct lysc_node *node;
4747 LY_ARRAY_COUNT_TYPE u;
4748 struct lysp_refine *rfn;
4749 LY_ERR ret = LY_SUCCESS;
4750 uint32_t min, max;
4751 uint16_t flags;
4752 struct ly_set refined = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01004753
Radek Krejci327de162019-06-14 12:52:07 +02004754 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004755
Radek Krejci76b3e962018-12-14 17:01:25 +01004756 /* apply refine */
Michal Vasko601ddb32020-08-31 16:25:35 +02004757 LY_ARRAY_FOR(refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004758 lysc_update_path(ctx, NULL, rfn->nodeid);
4759
Michal Vasko601ddb32020-08-31 16:25:35 +02004760 ret = lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, context_node, ctx->mod,
Michal Vasko20424b42020-08-31 12:29:38 +02004761 0, 0, (const struct lysc_node **)&node, &flags);
4762 LY_CHECK_GOTO(ret, cleanup);
4763 ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL);
4764 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004765
4766 /* default value */
4767 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004768 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci0f969882020-08-21 16:56:47 +02004770 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE " default values.",
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004771 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Michal Vasko20424b42020-08-31 12:29:38 +02004772 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004773 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004774 }
4775 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4776 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004777 "Invalid refine of default - %s cannot hold default value(s).",
4778 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004779 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004780 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004781 }
4782 if (node->nodetype == LYS_LEAF) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004783 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004784 ret = lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], ctx->mod_def);
4785 LY_CHECK_GOTO(ret, cleanup);
4786
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004787 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004788 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004789 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004790 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4791 "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 +02004792 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004793 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004794 }
Radek Krejcia1911222019-07-22 17:24:50 +02004795
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004796 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004797 ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def);
4798 LY_CHECK_GOTO(ret, cleanup);
4799
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004800 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004801 } else if (node->nodetype == LYS_CHOICE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004802 if (((struct lysc_node_choice *)node)->dflt) {
Radek Krejci01342af2019-01-03 15:18:08 +01004803 /* unset LYS_SET_DFLT from the current default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02004804 ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT;
Radek Krejci01342af2019-01-03 15:18:08 +01004805 }
Michal Vasko20424b42020-08-31 12:29:38 +02004806 ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node);
4807 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004808 }
4809 }
4810
Radek Krejci12fb9142019-01-08 09:45:30 +01004811 /* description */
4812 if (rfn->dsc) {
4813 FREE_STRING(ctx->ctx, node->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004814 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->dsc, 0, &node->dsc), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004815 }
4816
4817 /* reference */
4818 if (rfn->ref) {
4819 FREE_STRING(ctx->ctx, node->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004820 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->ref, 0, &node->ref), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004821 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004822
4823 /* config */
4824 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004825 if (!flags) {
Michal Vasko20424b42020-08-31 12:29:38 +02004826 ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1);
4827 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004828 } else {
4829 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004830 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004831 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004832 }
4833
4834 /* mandatory */
4835 if (rfn->flags & LYS_MAND_MASK) {
Michal Vasko20424b42020-08-31 12:29:38 +02004836 ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1);
4837 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004838 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004839
4840 /* presence */
4841 if (rfn->presence) {
4842 if (node->nodetype != LYS_CONTAINER) {
4843 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004844 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4845 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004846 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004847 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004848 }
4849 node->flags |= LYS_PRESENCE;
4850 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004851
4852 /* must */
4853 if (rfn->musts) {
4854 switch (node->nodetype) {
4855 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02004856 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 +01004857 break;
4858 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004859 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 +01004860 break;
4861 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004862 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 +01004863 break;
4864 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004865 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 +01004866 break;
4867 case LYS_ANYXML:
4868 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02004869 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 +01004870 break;
4871 default:
4872 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004873 "Invalid refine of must statement - %s cannot hold any must statement.",
4874 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004875 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004876 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004877 }
Michal Vasko20424b42020-08-31 12:29:38 +02004878 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4879 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004880 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004881
4882 /* min/max-elements */
4883 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4884 switch (node->nodetype) {
4885 case LYS_LEAFLIST:
4886 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004887 ((struct lysc_node_leaflist *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004888 }
4889 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004890 ((struct lysc_node_leaflist *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004891 if (rfn->min) {
4892 node->flags |= LYS_MAND_TRUE;
4893 lys_compile_mandatory_parents(node->parent, 1);
4894 } else {
4895 node->flags &= ~LYS_MAND_TRUE;
4896 lys_compile_mandatory_parents(node->parent, 0);
4897 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004898 }
4899 break;
4900 case LYS_LIST:
4901 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004902 ((struct lysc_node_list *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004903 }
4904 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004905 ((struct lysc_node_list *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004906 if (rfn->min) {
4907 node->flags |= LYS_MAND_TRUE;
4908 lys_compile_mandatory_parents(node->parent, 1);
4909 } else {
4910 node->flags &= ~LYS_MAND_TRUE;
4911 lys_compile_mandatory_parents(node->parent, 0);
4912 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004913 }
4914 break;
4915 default:
4916 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004917 "Invalid refine of %s statement - %s cannot hold this statement.",
Radek Krejci0f969882020-08-21 16:56:47 +02004918 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004919 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004920 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004921 }
4922 }
Radek Krejcif0089082019-01-07 16:42:01 +01004923
4924 /* if-feature */
4925 if (rfn->iffeatures) {
4926 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004927 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004928 }
Radek Krejci327de162019-06-14 12:52:07 +02004929
4930 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004931 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004932
Radek Krejcif2271f12019-01-07 16:42:23 +01004933 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004934 for (uint32_t i = 0; i < refined.count; ++i) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004935 node = (struct lysc_node *)refined.objs[i];
Michal Vasko601ddb32020-08-31 16:25:35 +02004936 rfn = &refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02004937 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004938
4939 /* check possible conflict with default value (default added, mandatory left true) */
4940 if ((node->flags & LYS_MAND_TRUE) &&
Michal Vasko22df3f02020-08-24 13:29:22 +02004941 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) ||
Radek Krejcif2271f12019-01-07 16:42:23 +01004942 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004944 "Invalid refine of default - the node is mandatory.");
Michal Vasko20424b42020-08-31 12:29:38 +02004945 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004946 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004947 }
4948
4949 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4950 if (node->nodetype == LYS_LIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004951 min = ((struct lysc_node_list *)node)->min;
4952 max = ((struct lysc_node_list *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004953 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02004954 min = ((struct lysc_node_leaflist *)node)->min;
4955 max = ((struct lysc_node_leaflist *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004956 }
4957 if (min > max) {
4958 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004959 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
Radek Krejci0f969882020-08-21 16:56:47 +02004960 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Michal Vasko20424b42020-08-31 12:29:38 +02004961 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004962 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004963 }
4964 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004965
4966 lysc_update_path(ctx, NULL, NULL);
Radek Krejcif2271f12019-01-07 16:42:23 +01004967 }
4968
Michal Vasko601ddb32020-08-31 16:25:35 +02004969cleanup:
4970 ly_set_erase(&refined, NULL);
4971 lysc_update_path(ctx, NULL, NULL);
4972 return ret;
4973}
4974
4975/**
4976 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4977 * If present, also apply uses's modificators.
4978 *
4979 * @param[in] ctx Compile context
4980 * @param[in] uses_p Parsed uses schema node.
4981 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4982 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4983 * the compile context.
4984 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4985 */
4986static LY_ERR
4987lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p)
4988{
4989 struct lysp_node *node_p;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004990 struct lysc_node *child = NULL, *iter;
Michal Vasko601ddb32020-08-31 16:25:35 +02004991 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
4992 struct lysc_node_container context_node_fake =
4993 {.nodetype = LYS_CONTAINER,
4994 .module = ctx->mod,
4995 .flags = parent ? parent->flags : 0,
4996 .child = NULL, .next = NULL,
4997 .prev = (struct lysc_node *)&context_node_fake,
4998 .actions = NULL, .notifs = NULL};
4999 struct lysp_grp *grp = NULL;
5000 LY_ARRAY_COUNT_TYPE u;
5001 uint32_t grp_stack_count;
5002 struct lys_module *grp_mod, *mod_old;
5003 LY_ERR ret = LY_SUCCESS;
5004 struct lysc_when **when, *when_shared;
5005 struct lysp_augment **augments = NULL;
5006 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
5007 struct lysc_notif **notifs = NULL;
5008 struct lysc_action **actions = NULL;
5009
5010 /* find the referenced grouping */
5011 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
5012
5013 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
5014 grp_stack_count = ctx->groupings.count;
5015 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
5016 if (grp_stack_count == ctx->groupings.count) {
5017 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
5018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5019 "Grouping \"%s\" references itself through a uses statement.", grp->name);
5020 return LY_EVALID;
5021 }
5022
5023 /* switch context's mod_def */
5024 mod_old = ctx->mod_def;
5025 ctx->mod_def = grp_mod;
5026
5027 /* check status */
5028 ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, grp_mod, grp->name);
5029 LY_CHECK_GOTO(ret, cleanup);
5030
5031 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
5032 * applu refine and augment only to the nodes from the uses */
5033 if (parent) {
5034 child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5035 } else if (ctx->mod->compiled->data) {
5036 child = ctx->mod->compiled->data;
5037 } else {
5038 child = NULL;
5039 }
5040 /* remember the last child */
5041 if (child) {
5042 child = child->prev;
5043 }
5044
5045 /* compile data nodes */
5046 LY_LIST_FOR(grp->data, node_p) {
5047 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
5048 ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3);
5049 LY_CHECK_GOTO(ret, cleanup);
5050 }
5051
5052 /* split the children and add the uses's data into the fake context node */
5053 if (child) {
5054 context_node_fake.child = child->next;
5055 } else if (parent) {
5056 context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5057 } else if (ctx->mod->compiled->data) {
5058 context_node_fake.child = ctx->mod->compiled->data;
5059 }
5060 if (context_node_fake.child) {
5061 /* remember child as the last data node added by grouping to fix the list later */
5062 child = context_node_fake.child->prev;
5063 context_node_fake.child->prev = NULL;
5064 }
5065
5066 when_shared = NULL;
5067 LY_LIST_FOR(context_node_fake.child, iter) {
5068 iter->parent = (struct lysc_node *)&context_node_fake;
5069
5070 /* pass uses's when to all the data children, actions and notifications are ignored */
5071 if (uses_p->when) {
5072 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
5073 if (!when_shared) {
5074 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when);
5075 LY_CHECK_GOTO(ret, cleanup);
5076
5077 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5078 /* do not check "when" semantics in a grouping */
5079 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5080 LY_CHECK_GOTO(ret, cleanup);
5081 }
5082
5083 when_shared = *when;
5084 } else {
5085 ++when_shared->refcount;
5086 (*when) = when_shared;
5087
5088 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5089 /* in this case check "when" again for all children because of dummy node check */
5090 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5091 LY_CHECK_GOTO(ret, cleanup);
5092 }
5093 }
5094 }
5095 }
5096
5097 /* compile actions */
5098 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5099 if (actions) {
5100 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
5101 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
5102 if (*actions && (uses_p->augments || uses_p->refines)) {
5103 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5104 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
5105 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
5106 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
5107 }
5108 }
5109
5110 /* compile notifications */
5111 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5112 if (notifs) {
5113 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
5114 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
5115 if (*notifs && (uses_p->augments || uses_p->refines)) {
5116 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5117 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
5118 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
5119 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
5120 }
5121 }
5122
5123 /* sort and apply augments */
5124 ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments);
5125 LY_CHECK_GOTO(ret, cleanup);
5126 LY_ARRAY_FOR(augments, u) {
5127 ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake);
5128 LY_CHECK_GOTO(ret, cleanup);
5129 }
5130
5131 /* reload previous context's mod_def */
5132 ctx->mod_def = mod_old;
5133
5134 /* apply all refines */
5135 ret = lys_compile_refines(ctx, uses_p->refines, (struct lysc_node *)&context_node_fake);
5136 LY_CHECK_GOTO(ret, cleanup);
5137
Radek Krejcic6b4f442020-08-12 14:45:18 +02005138 if (first_p) {
5139 *first_p = context_node_fake.child;
5140 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005141
5142cleanup:
5143 /* fix connection of the children nodes from fake context node back into the parent */
5144 if (context_node_fake.child) {
5145 context_node_fake.child->prev = child;
5146 }
5147 LY_LIST_FOR(context_node_fake.child, child) {
5148 child->parent = parent;
5149 }
5150
5151 if (uses_p->augments || uses_p->refines) {
5152 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005153 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005154 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005155 LY_ARRAY_FREE(context_node_fake.actions);
5156 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005157 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005158 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005159 LY_ARRAY_FREE(context_node_fake.notifs);
5160 }
5161 }
5162
Radek Krejcie86bf772018-12-14 11:39:53 +01005163 /* reload previous context's mod_def */
5164 ctx->mod_def = mod_old;
5165 /* remove the grouping from the stack for circular groupings dependency check */
5166 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5167 assert(ctx->groupings.count == grp_stack_count);
Radek Krejci3641f562019-02-13 15:38:40 +01005168 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005169
5170 return ret;
5171}
5172
Radek Krejci327de162019-06-14 12:52:07 +02005173static int
5174lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5175{
5176 struct lysp_node *iter;
5177 int len = 0;
5178
5179 *path = NULL;
5180 for (iter = node; iter && len >= 0; iter = iter->parent) {
5181 char *s = *path;
5182 char *id;
5183
5184 switch (iter->nodetype) {
5185 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005186 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005187 break;
5188 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005189 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005190 break;
5191 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005192 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005193 break;
5194 default:
5195 id = strdup(iter->name);
5196 break;
5197 }
5198
5199 if (!iter->parent) {
5200 /* print prefix */
5201 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5202 } else {
5203 /* prefix is the same as in parent */
5204 len = asprintf(path, "/%s%s", id, s ? s : "");
5205 }
5206 free(s);
5207 free(id);
5208 }
5209
5210 if (len < 0) {
5211 free(*path);
5212 *path = NULL;
5213 } else if (len == 0) {
5214 *path = strdup("/");
5215 len = 1;
5216 }
5217 return len;
5218}
5219
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005220/**
5221 * @brief Validate groupings that were defined but not directly used in the schema itself.
5222 *
5223 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5224 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5225 */
5226static LY_ERR
5227lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5228{
5229 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005230 char *path;
5231 int len;
5232
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005233 struct lysp_node_uses fake_uses = {
5234 .parent = node_p,
5235 .nodetype = LYS_USES,
5236 .flags = 0, .next = NULL,
5237 .name = grp->name,
5238 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5239 .refines = NULL, .augments = NULL
5240 };
5241 struct lysc_node_container fake_container = {
5242 .nodetype = LYS_CONTAINER,
5243 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5244 .module = ctx->mod,
5245 .sp = NULL, .parent = NULL, .next = NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +02005246 .prev = (struct lysc_node *)&fake_container,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005247 .name = "fake",
5248 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5249 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5250 };
5251
5252 if (grp->parent) {
5253 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5254 }
Radek Krejci327de162019-06-14 12:52:07 +02005255
5256 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5257 if (len < 0) {
5258 LOGMEM(ctx->ctx);
5259 return LY_EMEM;
5260 }
5261 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005262 ctx->path_len = (uint32_t)len;
Radek Krejci327de162019-06-14 12:52:07 +02005263 free(path);
5264
5265 lysc_update_path(ctx, NULL, "{grouping}");
5266 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02005267 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005268 lysc_update_path(ctx, NULL, NULL);
5269 lysc_update_path(ctx, NULL, NULL);
5270
5271 ctx->path_len = 1;
5272 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005273
5274 /* cleanup */
5275 lysc_node_container_free(ctx->ctx, &fake_container);
5276
5277 return ret;
5278}
Radek Krejcife909632019-02-12 15:34:42 +01005279
Radek Krejcie86bf772018-12-14 11:39:53 +01005280/**
Michal Vasko20424b42020-08-31 12:29:38 +02005281 * @brief Set config flags for a node.
5282 *
5283 * @param[in] ctx Compile context.
5284 * @param[in] node Compiled node config to set.
5285 * @param[in] parent Parent of @p node.
5286 * @return LY_ERR value.
5287 */
5288static LY_ERR
5289lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
5290{
5291 if (node->nodetype == LYS_CASE) {
5292 /* case never has any config */
5293 assert(!(node->flags & LYS_CONFIG_MASK));
5294 return LY_SUCCESS;
5295 }
5296
5297 /* adjust parent to always get the ancestor with config */
5298 if (parent && (parent->nodetype == LYS_CASE)) {
5299 parent = parent->parent;
5300 assert(parent);
5301 }
5302
5303 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
5304 /* ignore config statements inside RPC/action data */
5305 node->flags &= ~LYS_CONFIG_MASK;
5306 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5307 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
5308 /* ignore config statements inside Notification data */
5309 node->flags &= ~LYS_CONFIG_MASK;
5310 node->flags |= LYS_CONFIG_R;
5311 } else if (!(node->flags & LYS_CONFIG_MASK)) {
5312 /* config not explicitely set, inherit it from parent */
5313 if (parent) {
5314 node->flags |= parent->flags & LYS_CONFIG_MASK;
5315 } else {
5316 /* default is config true */
5317 node->flags |= LYS_CONFIG_W;
5318 }
5319 } else {
5320 /* config set explicitely */
5321 node->flags |= LYS_SET_CONFIG;
5322 }
5323
5324 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5326 "Configuration node cannot be child of any state data node.");
5327 return LY_EVALID;
5328 }
5329
5330 return LY_SUCCESS;
5331}
5332
5333/**
Radek Krejcia3045382018-11-22 14:30:31 +01005334 * @brief Compile parsed schema node information.
5335 * @param[in] ctx Compile context
5336 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005337 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5338 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5339 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005340 * @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).
5341 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005342 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5343 */
Radek Krejci19a96102018-11-15 13:38:09 +01005344static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005345lys_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 +01005346{
Radek Krejci1c54f462020-05-12 17:25:34 +02005347 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005348 struct lysc_node *node = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005349 struct lysc_when **when;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005350 LY_ARRAY_COUNT_TYPE u;
Michal Vasko22df3f02020-08-24 13:29:22 +02005351 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
Radek Krejci19a96102018-11-15 13:38:09 +01005352
Radek Krejci327de162019-06-14 12:52:07 +02005353 if (node_p->nodetype != LYS_USES) {
5354 lysc_update_path(ctx, parent, node_p->name);
5355 } else {
5356 lysc_update_path(ctx, NULL, "{uses}");
5357 lysc_update_path(ctx, NULL, node_p->name);
5358 }
5359
Radek Krejci19a96102018-11-15 13:38:09 +01005360 switch (node_p->nodetype) {
5361 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02005362 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
Radek Krejci19a96102018-11-15 13:38:09 +01005363 node_compile_spec = lys_compile_node_container;
5364 break;
5365 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005366 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
Radek Krejci19a96102018-11-15 13:38:09 +01005367 node_compile_spec = lys_compile_node_leaf;
5368 break;
5369 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005370 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005371 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005372 break;
5373 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005374 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005375 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005376 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005377 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02005378 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005379 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005380 break;
Michal Vasko20424b42020-08-31 12:29:38 +02005381 case LYS_CASE:
5382 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
5383 node_compile_spec = lys_compile_node_case;
5384 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005385 case LYS_ANYXML:
5386 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005387 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005388 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005389 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005390 case LYS_USES:
Michal Vasko9e8de2d2020-09-01 08:17:10 +02005391 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, parent, &node);
Radek Krejci327de162019-06-14 12:52:07 +02005392 lysc_update_path(ctx, NULL, NULL);
5393 lysc_update_path(ctx, NULL, NULL);
5394 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005395 default:
5396 LOGINT(ctx->ctx);
5397 return LY_EINT;
5398 }
5399 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5400 node->nodetype = node_p->nodetype;
5401 node->module = ctx->mod;
5402 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005403 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005404
5405 /* config */
Michal Vasko20424b42020-08-31 12:29:38 +02005406 ret = lys_compile_config(ctx, node, parent);
5407 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005408
Michal Vasko20424b42020-08-31 12:29:38 +02005409 /* list ordering */
Radek Krejcia6d57732018-11-29 13:40:37 +01005410 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5411 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005412 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci0f969882020-08-21 16:56:47 +02005413 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5414 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005415 node->flags &= ~LYS_ORDBY_MASK;
5416 node->flags |= LYS_ORDBY_SYSTEM;
5417 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5418 /* default ordering is system */
5419 node->flags |= LYS_ORDBY_SYSTEM;
5420 }
5421 }
5422
Radek Krejci19a96102018-11-15 13:38:09 +01005423 /* status - it is not inherited by specification, but it does not make sense to have
5424 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko20424b42020-08-31 12:29:38 +02005425 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 +01005426
Radek Krejciec4da802019-05-02 13:02:41 +02005427 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005428 node->sp = node_p;
5429 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02005430 DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error);
5431 DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error);
5432 DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error);
Radek Krejci00b874b2019-02-12 10:54:50 +01005433 if (node_p->when) {
5434 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005435 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005436
5437 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5438 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02005439 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
5440 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005441 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005442 }
Radek Krejciec4da802019-05-02 13:02:41 +02005443 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005444
Michal Vasko20424b42020-08-31 12:29:38 +02005445 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
5446 LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node));
5447
Radek Krejci19a96102018-11-15 13:38:09 +01005448 /* nodetype-specific part */
Michal Vasko20424b42020-08-31 12:29:38 +02005449 LY_CHECK_RET(node_compile_spec(ctx, node_p, node));
Radek Krejci19a96102018-11-15 13:38:09 +01005450
Michal Vasko20424b42020-08-31 12:29:38 +02005451 /* final compilation tasks that require the node to be connected */
5452 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, done);
Radek Krejcife909632019-02-12 15:34:42 +01005453 if (node->flags & LYS_MAND_TRUE) {
Michal Vasko20424b42020-08-31 12:29:38 +02005454 /* inherit LYS_MAND_TRUE in parent containers */
Radek Krejcife909632019-02-12 15:34:42 +01005455 lys_compile_mandatory_parents(parent, 1);
5456 }
5457
Radek Krejci327de162019-06-14 12:52:07 +02005458 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005459 return LY_SUCCESS;
5460
5461error:
5462 lysc_node_free(ctx->ctx, node);
Michal Vasko20424b42020-08-31 12:29:38 +02005463done:
Radek Krejci19a96102018-11-15 13:38:09 +01005464 return ret;
5465}
5466
Radek Krejciccd20f12019-02-15 14:12:27 +01005467static void
Michal Vasko20424b42020-08-31 12:29:38 +02005468lysc_node_unlink(struct lysc_node *node)
Radek Krejciccd20f12019-02-15 14:12:27 +01005469{
Michal Vasko20424b42020-08-31 12:29:38 +02005470 struct lysc_node *parent, *child;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005471 struct lysc_module *modc = node->module->compiled;
Radek Krejciccd20f12019-02-15 14:12:27 +01005472
5473 parent = node->parent;
5474
5475 /* parent's first child */
Michal Vasko20424b42020-08-31 12:29:38 +02005476 if (parent && lysc_node_children(parent, node->flags) == node) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005477 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005478 } else if (modc->data == node) {
5479 modc->data = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005480 }
Michal Vasko20424b42020-08-31 12:29:38 +02005481
5482 /* special choice case unlinking */
5483 if (parent && parent->nodetype == LYS_CHOICE) {
5484 if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) {
5485 /* default case removed */
5486 ((struct lysc_node_choice *)parent)->dflt = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005487 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005488 }
5489
5490 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005491 if (node->prev->next) {
5492 node->prev->next = node->next;
5493 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005494 if (node->next) {
5495 node->next->prev = node->prev;
Michal Vasko20424b42020-08-31 12:29:38 +02005496 } else {
5497 /* last child */
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005498 if (parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005499 child = (struct lysc_node *)lysc_node_children(parent, node->flags);
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005500 } else {
5501 child = modc->data;
5502 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005503 if (child) {
5504 child->prev = node->prev;
5505 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005506 }
5507}
5508
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005509struct lysc_deviation {
5510 const char *nodeid;
5511 struct lysc_node *target; /* target node of the deviation */
Michal Vasko22df3f02020-08-24 13:29:22 +02005512 struct lysp_deviate **deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02005513 uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */
Radek Krejci857189e2020-09-01 13:26:36 +02005514 ly_bool not_supported; /* flag if deviates contains not-supported deviate */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005515};
5516
Michal Vaskoccc062a2020-08-13 08:34:50 +02005517/* MACROS for deviates checking */
5518#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5519 if (!(target->nodetype & (NODETYPES))) { \
5520 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5521 goto cleanup; \
5522 }
5523
5524#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5525 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5527 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5528 goto cleanup; \
5529 }
5530
5531#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5532 if (!((TYPE)target)->MEMBER || COND) { \
5533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5534 goto cleanup; \
5535 }
5536
5537/**
5538 * @brief Apply deviate add.
5539 *
5540 * @param[in] ctx Compile context.
5541 * @param[in] target Deviation target.
5542 * @param[in] dev_flags Internal deviation flags.
5543 * @param[in] d Deviate add to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02005544 * @return LY_ERR value.
5545 */
5546static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005547lys_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 +02005548{
Radek Krejci011e4aa2020-09-04 15:22:31 +02005549 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005550 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5551 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005552 LY_ARRAY_COUNT_TYPE x;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005553
5554#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5555 if (((TYPE)target)->MEMBER COND) { \
5556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5557 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5558 PROPERTY, ((TYPE)target)->MEMBER); \
5559 goto cleanup; \
5560 }
5561
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005562#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005563 if (((TYPE)target)->MEMBER && (COND)) { \
Radek Krejci857189e2020-09-01 13:26:36 +02005564 ly_bool dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005565 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005566 ctx->mod_def, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5568 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5569 if (dynamic_) {free((void*)val_);} \
5570 goto cleanup; \
5571 }
5572
5573#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5574 if (((TYPE)target)->MEMBER && (COND)) { \
5575 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5576 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5577 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5578 goto cleanup; \
5579 }
5580
5581 /* [units-stmt] */
5582 if (d->units) {
5583 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5584 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5585
5586 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02005587 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc);
5588 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005589 }
5590
5591 /* *must-stmt */
5592 if (d->musts) {
5593 switch (target->nodetype) {
5594 case LYS_CONTAINER:
5595 case LYS_LIST:
5596 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5597 x, lys_compile_must, ret, cleanup);
5598 break;
5599 case LYS_LEAF:
5600 case LYS_LEAFLIST:
5601 case LYS_ANYDATA:
5602 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5603 x, lys_compile_must, ret, cleanup);
5604 break;
5605 case LYS_NOTIF:
5606 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5607 x, lys_compile_must, ret, cleanup);
5608 break;
5609 case LYS_RPC:
5610 case LYS_ACTION:
5611 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5612 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5613 x, lys_compile_must, ret, cleanup);
5614 break;
5615 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5616 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5617 x, lys_compile_must, ret, cleanup);
5618 break;
5619 }
Radek Krejci0f969882020-08-21 16:56:47 +02005620 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005621 default:
5622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5623 lys_nodetype2str(target->nodetype), "add", "must");
5624 goto cleanup;
5625 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02005626 ret = ly_set_add(&ctx->xpath, target, 0, NULL);
5627 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005628 }
5629
5630 /* *unique-stmt */
5631 if (d->uniques) {
5632 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5633 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5634 }
5635
5636 /* *default-stmt */
5637 if (d->dflts) {
5638 switch (target->nodetype) {
5639 case LYS_LEAF:
5640 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005641 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005642 if (leaf->dflt) {
5643 /* first, remove the default value taken from the type */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005644 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5645 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005646 free(leaf->dflt);
5647 leaf->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005648 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005649
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005650 /* store the default value in unres */
Michal Vasko31a82d52020-08-21 08:11:48 +02005651 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005652 target->flags |= LYS_SET_DFLT;
5653 break;
5654 case LYS_LEAFLIST:
5655 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5656 /* first, remove the default value taken from the type */
5657 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005658 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5659 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5660 free(llist->dflts[x]);
5661 }
5662 LY_ARRAY_FREE(llist->dflts);
5663 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005664 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005665
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005666 /* store the default values in unres */
5667 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005668 target->flags |= LYS_SET_DFLT;
5669 break;
5670 case LYS_CHOICE:
5671 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5672 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5673 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5674 * to allow making the default case even the augmented case from the deviating module */
5675 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5676 goto cleanup;
5677 }
5678 break;
5679 default:
5680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5681 lys_nodetype2str(target->nodetype), "add", "default");
5682 goto cleanup;
5683 }
5684 }
5685
5686 /* [config-stmt] */
5687 if (d->flags & LYS_CONFIG_MASK) {
5688 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5690 lys_nodetype2str(target->nodetype), "add", "config");
5691 goto cleanup;
5692 }
5693 if (dev_flags) {
5694 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5695 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5696 }
5697 if (target->flags & LYS_SET_CONFIG) {
5698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5699 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5700 target->flags & LYS_CONFIG_W ? "true" : "false");
5701 goto cleanup;
5702 }
5703 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5704 }
5705
5706 /* [mandatory-stmt] */
5707 if (d->flags & LYS_MAND_MASK) {
5708 if (target->flags & LYS_MAND_MASK) {
5709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5710 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5711 target->flags & LYS_MAND_TRUE ? "true" : "false");
5712 goto cleanup;
5713 }
5714 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5715 }
5716
5717 /* [min-elements-stmt] */
5718 if (d->flags & LYS_SET_MIN) {
5719 if (target->nodetype == LYS_LEAFLIST) {
5720 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5721 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005722 ((struct lysc_node_leaflist *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005723 } else if (target->nodetype == LYS_LIST) {
5724 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5725 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005726 ((struct lysc_node_list *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005727 } else {
5728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5729 lys_nodetype2str(target->nodetype), "add", "min-elements");
5730 goto cleanup;
5731 }
5732 if (d->min) {
5733 target->flags |= LYS_MAND_TRUE;
5734 }
5735 }
5736
5737 /* [max-elements-stmt] */
5738 if (d->flags & LYS_SET_MAX) {
5739 if (target->nodetype == LYS_LEAFLIST) {
5740 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5741 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005742 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005743 } else if (target->nodetype == LYS_LIST) {
5744 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5745 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005746 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005747 } else {
5748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5749 lys_nodetype2str(target->nodetype), "add", "max-elements");
5750 goto cleanup;
5751 }
5752 }
5753
5754 ret = LY_SUCCESS;
5755
5756cleanup:
5757 return ret;
5758}
5759
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005760static LY_ERR
5761lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt)
5762{
5763 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005764 ly_bool dyn = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005765 const char *orig_dflt;
5766 uint32_t i;
5767
5768 if (target->module != ctx->mod) {
5769 /* foreign deviation */
5770 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt);
5771
5772 /* check that the value matches */
5773 orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5774 if (strcmp(orig_dflt, dflt)) {
5775 goto error;
5776 }
5777 if (dyn) {
5778 free((char *)orig_dflt);
5779 }
5780
5781 /* remove the default specification */
5782 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5783 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5784 free(leaf->dflt);
5785 leaf->dflt = NULL;
5786 } else {
5787 /* local deviation */
5788 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt);
5789
5790 /* find the incomplete default */
5791 orig_dflt = NULL;
5792 for (i = 0; i < ctx->dflts.count; ++i) {
5793 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
5794 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5795 break;
5796 }
5797 }
5798 LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT);
5799
5800 /* check that the value matches */
5801 if (strcmp(orig_dflt, dflt)) {
5802 goto error;
5803 }
5804
5805 /* update the list of incomplete default values */
5806 lysc_incomplete_dflt_remove(ctx, target);
5807 }
5808
5809 target->flags &= ~LYS_SET_DFLT;
5810 return LY_SUCCESS;
5811
5812error:
5813 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5814 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5815 dflt, orig_dflt);
5816 if (dyn) {
5817 free((char *)orig_dflt);
5818 }
5819cleanup:
5820 return LY_EVALID;
5821}
5822
5823static LY_ERR
5824lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts)
5825{
5826 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005827 ly_bool dyn = 0, found;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005828 const char *orig_dflt, **orig_dflts;
5829 uint32_t i;
5830 LY_ARRAY_COUNT_TYPE x, y;
5831
5832 if (target->module != ctx->mod) {
5833 /* foreign deviation */
5834 DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]);
5835 LY_ARRAY_FOR(dflts, x) {
5836 found = 0;
5837 LY_ARRAY_FOR(llist->dflts, y) {
5838 orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5839 if (!strcmp(orig_dflt, dflts[x])) {
5840 if (dyn) {
5841 free((char *)orig_dflt);
5842 }
5843 found = 1;
5844 break;
5845 }
5846 if (dyn) {
5847 free((char *)orig_dflt);
5848 }
5849 }
5850 if (!found) {
5851 goto error;
5852 }
5853
5854 /* update compiled default values */
5855 LY_ARRAY_DECREMENT(llist->dflts);
5856 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
5857 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
5858 free(llist->dflts[y]);
5859 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
5860 }
5861 if (!LY_ARRAY_COUNT(llist->dflts)) {
5862 LY_ARRAY_FREE(llist->dflts);
5863 llist->dflts = NULL;
5864 llist->flags &= ~LYS_SET_DFLT;
5865 }
5866 } else {
5867 /* local deviation */
5868 orig_dflt = NULL;
5869 orig_dflts = NULL;
5870 for (i = 0; i < ctx->dflts.count; ++i) {
5871 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) {
5872 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5873 orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts;
5874 break;
5875 }
5876 }
5877 LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT);
5878
5879 if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) {
5880 /* TODO it is not currently possible to remove just one default value from incomplete defaults array */
5881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5882 "Local deviation deleting leaf-list defaults is not supported.");
5883 return LY_EVALID;
5884 }
5885
5886 LY_ARRAY_FOR(dflts, x) {
5887 found = 0;
5888 if (orig_dflts) {
5889 LY_ARRAY_FOR(orig_dflts, y) {
5890 if (!strcmp(orig_dflts[y], dflts[x])) {
5891 found = 1;
5892 break;
5893 }
5894 }
5895 } else if (!strcmp(orig_dflt, dflts[x])) {
5896 found = 1;
5897 }
5898 if (!found) {
5899 goto error;
5900 }
5901
5902 /* update the list of incomplete default values */
5903 lysc_incomplete_dflt_remove(ctx, target);
5904 }
5905
5906 llist->flags &= ~LYS_SET_DFLT;
5907 }
5908
5909 return LY_SUCCESS;
5910
5911error:
5912 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
5913 "which does not match any of the target's property values.", dflts[x]);
5914cleanup:
5915 return LY_EVALID;
5916}
5917
Michal Vaskoccc062a2020-08-13 08:34:50 +02005918/**
5919 * @brief Apply deviate delete.
5920 *
5921 * @param[in] ctx Compile context.
5922 * @param[in] target Deviation target.
5923 * @param[in] dev_flags Internal deviation flags.
5924 * @param[in] d Deviate delete to apply.
5925 * @return LY_ERR value.
5926 */
5927static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005928lys_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 +02005929{
5930 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005931 struct lysc_node_list *list = (struct lysc_node_list *)target;
5932 LY_ARRAY_COUNT_TYPE x, y, z;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005933 size_t prefix_len, name_len;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005934 const char *prefix, *name, *nodeid;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005935 struct lys_module *mod;
5936
5937#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5938 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5939 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5940 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5941 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5942 } \
5943 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5945 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5946 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5947 goto cleanup; \
5948 } \
5949 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5950 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5951 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5952 &((TYPE)target)->ARRAY_TRG[y + 1], \
5953 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5954 } \
5955 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5956 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5957 ((TYPE)target)->ARRAY_TRG = NULL; \
5958 }
5959
5960 /* [units-stmt] */
5961 if (d->units) {
5962 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Michal Vasko22df3f02020-08-24 13:29:22 +02005963 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units);
5964 if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005965 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5966 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02005967 d->units, ((struct lysc_node_leaf *)target)->units);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005968 goto cleanup;
5969 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005970 lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5971 ((struct lysc_node_leaf *)target)->units = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005972 }
5973
5974 /* *must-stmt */
5975 if (d->musts) {
5976 switch (target->nodetype) {
5977 case LYS_CONTAINER:
5978 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005979 DEV_DEL_ARRAY(struct lysc_node_container *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005980 break;
5981 case LYS_LEAF:
5982 case LYS_LEAFLIST:
5983 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005984 DEV_DEL_ARRAY(struct lysc_node_leaf *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005985 break;
5986 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005987 DEV_DEL_ARRAY(struct lysc_notif *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005988 break;
5989 case LYS_RPC:
5990 case LYS_ACTION:
5991 if (dev_flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005992 DEV_DEL_ARRAY(struct lysc_action *, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005993 break;
5994 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005995 DEV_DEL_ARRAY(struct lysc_action *, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005996 break;
5997 }
Radek Krejci0f969882020-08-21 16:56:47 +02005998 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005999 default:
6000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6001 lys_nodetype2str(target->nodetype), "delete", "must");
6002 goto cleanup;
6003 }
6004 }
6005
6006 /* *unique-stmt */
6007 if (d->uniques) {
6008 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6009 LY_ARRAY_FOR(d->uniques, x) {
6010 LY_ARRAY_FOR(list->uniques, z) {
6011 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
6012 nodeid = strpbrk(name, " \t\n");
6013 if (nodeid) {
6014 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
6015 break;
6016 }
6017 while (isspace(*nodeid)) {
6018 ++nodeid;
6019 }
6020 } else {
6021 if (strcmp(name, list->uniques[z][y]->name)) {
6022 break;
6023 }
6024 }
6025 }
6026 if (!name) {
6027 /* complete match - remove the unique */
6028 LY_ARRAY_DECREMENT(list->uniques);
6029 LY_ARRAY_FREE(list->uniques[z]);
6030 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
6031 --z;
6032 break;
6033 }
6034 }
6035 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
6036 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6037 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6038 d->uniques[x]);
6039 goto cleanup;
6040 }
6041 }
6042 if (!LY_ARRAY_COUNT(list->uniques)) {
6043 LY_ARRAY_FREE(list->uniques);
6044 list->uniques = NULL;
6045 }
6046 }
6047
6048 /* *default-stmt */
6049 if (d->dflts) {
6050 switch (target->nodetype) {
6051 case LYS_LEAF:
6052 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006053 LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006054 break;
6055 case LYS_LEAFLIST:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006056 LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006057 break;
6058 case LYS_CHOICE:
6059 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vasko22df3f02020-08-24 13:29:22 +02006060 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "deleting", "default", d->dflts[0]);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006061 nodeid = d->dflts[0];
6062 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6063 if (prefix) {
6064 /* use module prefixes from the deviation module to match the module of the default case */
6065 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006066 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006067 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6068 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6069 goto cleanup;
6070 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006071 if (mod != ((struct lysc_node_choice *)target)->dflt->module) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006072 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006073 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6074 "The prefix does not match the default case's module.", d->dflts[0]);
6075 goto cleanup;
6076 }
6077 }
6078 /* else {
6079 * strictly, the default prefix would point to the deviation module, but the value should actually
6080 * match the default string in the original module (usually unprefixed), so in this case we do not check
6081 * the module of the default case, just matching its name */
Michal Vasko22df3f02020-08-24 13:29:22 +02006082 if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02006083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6084 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02006085 d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006086 goto cleanup;
6087 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006088 ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT;
6089 ((struct lysc_node_choice *)target)->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006090 break;
6091 default:
6092 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6093 lys_nodetype2str(target->nodetype), "delete", "default");
6094 goto cleanup;
6095 }
6096 }
6097
6098 ret = LY_SUCCESS;
6099
6100cleanup:
6101 return ret;
6102}
6103
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006104static LY_ERR
6105lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target)
6106{
6107 LY_ERR ret;
6108 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6109 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6110 struct ly_err_item *err = NULL;
6111 LY_ARRAY_COUNT_TYPE x;
6112 const char *dflt;
Radek Krejci857189e2020-09-01 13:26:36 +02006113 ly_bool dyn;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006114
6115 if (target->module != ctx->mod) {
6116 /* foreign deviation */
6117 if (target->nodetype == LYS_LEAF) {
6118 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn);
6119 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6120 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6121
6122 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6123 LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err);
6124 if (dyn) {
6125 free((char *)dflt);
6126 }
6127 if (err) {
6128 ly_err_print(err);
6129 ctx->path[0] = '\0';
6130 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6131 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6132 "Invalid default - value does not fit the type (%s).", err->msg);
6133 ly_err_free(err);
6134 }
6135 LY_CHECK_RET(ret);
6136
6137 ++leaf->dflt->realtype->refcount;
6138 } else { /* LY_LEAFLIST */
6139 LY_ARRAY_FOR(llist->dflts, x) {
6140 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn);
6141 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6142 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6143
6144 ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6145 LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err);
6146 if (dyn) {
6147 free((char *)dflt);
6148 }
6149 if (err) {
6150 ly_err_print(err);
6151 ctx->path[0] = '\0';
6152 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6154 "Invalid default - value does not fit the type (%s).", err->msg);
6155 ly_err_free(err);
6156 }
6157 LY_CHECK_RET(ret);
6158
6159 ++llist->dflts[x]->realtype->refcount;
6160 }
6161 }
6162 } else {
6163 /* local deviation */
6164
6165 /* these default were not compiled yet, so they will use the new type automatically */
6166 }
6167
6168 return LY_SUCCESS;
6169}
6170
Michal Vaskoccc062a2020-08-13 08:34:50 +02006171/**
6172 * @brief Apply deviate replace.
6173 *
6174 * @param[in] ctx Compile context.
6175 * @param[in] target Deviation target.
6176 * @param[in] d Deviate replace to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02006177 * @return LY_ERR value.
6178 */
6179static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006180lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02006181{
Radek Krejci011e4aa2020-09-04 15:22:31 +02006182 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006183 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6184 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6185 LY_ARRAY_COUNT_TYPE x;
6186
6187#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6188 if (!(((TYPE)target)->MEMBER COND)) { \
6189 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6190 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6191 goto cleanup; \
6192 }
6193
6194 /* [type-stmt] */
6195 if (d->type) {
6196 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6197 /* type is mandatory, so checking for its presence is not necessary */
6198 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6199
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006200 /* remove only default value inherited from the type */
6201 if (!(target->flags & LYS_SET_DFLT)) {
6202 if (target->module != ctx->mod) {
6203 /* foreign deviation - the target has default from the previous type, remove it */
6204 if (target->nodetype == LYS_LEAF) {
6205 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6206 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6207 free(leaf->dflt);
6208 leaf->dflt = NULL;
6209 } else { /* LYS_LEAFLIST */
6210 LY_ARRAY_FOR(llist->dflts, x) {
6211 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6212 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6213 free(llist->dflts[x]);
6214 }
6215 LY_ARRAY_FREE(llist->dflts);
6216 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006217 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006218 } else {
6219 /* local deviation */
6220 lysc_incomplete_dflt_remove(ctx, target);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006221 }
6222 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006223
6224 LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf));
6225
6226 if (target->flags & LYS_SET_DFLT) {
6227 /* the term default value(s) needs to be recompiled */
6228 LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006229 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006230 }
6231
6232 /* [units-stmt] */
6233 if (d->units) {
6234 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6235 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6236 units, "replacing", "units", d->units);
6237
6238 lydict_remove(ctx->ctx, leaf->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02006239 DUP_STRING(ctx->ctx, d->units, leaf->units, rc);
6240 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006241 }
6242
6243 /* [default-stmt] */
6244 if (d->dflt) {
6245 switch (target->nodetype) {
6246 case LYS_LEAF:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006247 if (target->module != ctx->mod) {
6248 /* foreign deviation */
6249 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing",
6250 "default", d->dflt);
6251
6252 /* remove the default specification */
6253 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6254 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6255 free(leaf->dflt);
6256 leaf->dflt = NULL;
6257 } else {
6258 /* local deviation */
6259 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing",
6260 "default", d->dflt);
6261 assert(!leaf->dflt);
6262 }
6263
6264 /* store the new default value */
6265 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006266 break;
6267 case LYS_CHOICE:
6268 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6269 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6270 goto cleanup;
6271 }
6272 break;
6273 default:
6274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6275 lys_nodetype2str(target->nodetype), "replace", "default");
6276 goto cleanup;
6277 }
6278 }
6279
6280 /* [config-stmt] */
6281 if (d->flags & LYS_CONFIG_MASK) {
6282 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6283 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6284 lys_nodetype2str(target->nodetype), "replace", "config");
6285 goto cleanup;
6286 }
6287 if (!(target->flags & LYS_SET_CONFIG)) {
6288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6289 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6290 goto cleanup;
6291 }
6292 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6293 }
6294
6295 /* [mandatory-stmt] */
6296 if (d->flags & LYS_MAND_MASK) {
6297 if (!(target->flags & LYS_MAND_MASK)) {
6298 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6299 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6300 goto cleanup;
6301 }
6302 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6303 }
6304
6305 /* [min-elements-stmt] */
6306 if (d->flags & LYS_SET_MIN) {
6307 if (target->nodetype == LYS_LEAFLIST) {
6308 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6309 /* change value */
6310 ((struct lysc_node_leaflist *)target)->min = d->min;
6311 } else if (target->nodetype == LYS_LIST) {
6312 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6313 /* change value */
6314 ((struct lysc_node_list *)target)->min = d->min;
6315 } else {
6316 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6317 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6318 goto cleanup;
6319 }
6320 if (d->min) {
6321 target->flags |= LYS_MAND_TRUE;
6322 }
6323 }
6324
6325 /* [max-elements-stmt] */
6326 if (d->flags & LYS_SET_MAX) {
6327 if (target->nodetype == LYS_LEAFLIST) {
6328 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6329 /* change value */
6330 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6331 } else if (target->nodetype == LYS_LIST) {
6332 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6333 /* change value */
6334 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6335 } else {
6336 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6337 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6338 goto cleanup;
6339 }
6340 }
6341
6342 ret = LY_SUCCESS;
6343
6344cleanup:
6345 return ret;
6346}
6347
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006348/**
6349 * @brief Apply all deviations of one target node.
6350 *
6351 * @param[in] ctx Compile context.
6352 * @param[in] dev Deviation structure to apply.
6353 * @return LY_ERR value.
6354 */
6355static LY_ERR
6356lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006357{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006358 LY_ERR ret = LY_EVALID;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006359 struct lysc_node *target = dev->target;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006360 struct lysc_action *rpcs;
6361 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006362 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006363 LY_ARRAY_COUNT_TYPE v, x;
Radek Krejci551b12c2019-02-19 16:11:21 +01006364 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006365
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006366 lysc_update_path(ctx, NULL, dev->nodeid);
6367
6368 /* not-supported */
6369 if (dev->not_supported) {
6370 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
Radek Krejci0f969882020-08-21 16:56:47 +02006371 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 +02006372 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6373 }
6374
6375#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6376 if (target->parent) { \
6377 ARRAY = (TYPE*)GETFUNC(target->parent); \
6378 } else { \
6379 ARRAY = target->module->compiled->ARRAY; \
6380 } \
6381 LY_ARRAY_FOR(ARRAY, x) { \
6382 if (&ARRAY[x] == (TYPE*)target) { break; } \
6383 } \
6384 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6385 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6386 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6387 LY_ARRAY_DECREMENT(ARRAY); \
6388 }
6389
6390 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6391 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6392 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006393 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6394 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6395 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6396 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006397 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6398 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006399 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6400 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6401 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6402 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006403 } else {
6404 /* remove RPC/action */
6405 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6406 }
6407 } else if (target->nodetype == LYS_NOTIF) {
6408 /* remove Notification */
6409 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6410 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02006411 if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) {
6412 /* remove the target node with its parent case node because it is the only node of the case */
6413 lysc_node_unlink(target->parent);
6414 lysc_node_free(ctx->ctx, target->parent);
6415 } else {
6416 /* remove the target node */
6417 lysc_node_unlink(target);
6418 lysc_node_free(ctx->ctx, target);
6419 }
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006420 }
6421
6422 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6423 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6424 return LY_SUCCESS;
6425 }
6426
6427 /* list of deviates (not-supported is not present in the list) */
6428 LY_ARRAY_FOR(dev->deviates, v) {
6429 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006430 switch (d->mod) {
6431 case LYS_DEV_ADD:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006432 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006433 break;
6434 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006435 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006436 break;
6437 case LYS_DEV_REPLACE:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006438 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006439 break;
6440 default:
6441 LOGINT(ctx->ctx);
6442 goto cleanup;
6443 }
6444 }
6445
6446 /* final check when all deviations of a single target node are applied */
6447
6448 /* check min-max compatibility */
6449 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006450 min = ((struct lysc_node_leaflist *)target)->min;
6451 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006452 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006453 min = ((struct lysc_node_list *)target)->min;
6454 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006455 } else {
6456 min = max = 0;
6457 }
6458 if (min > max) {
6459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6460 "after deviation: min value %u is bigger than max value %u.", min, max);
6461 goto cleanup;
6462 }
6463
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006464 /* check mandatory - default compatibility */
6465 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6466 && (target->flags & LYS_MAND_TRUE)) {
6467 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6468 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6469 goto cleanup;
Michal Vasko22df3f02020-08-24 13:29:22 +02006470 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006471 && (target->flags & LYS_MAND_TRUE)) {
6472 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6473 goto cleanup;
6474 }
6475 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6476 /* mandatory node under a default case */
6477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6478 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6479 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6480 goto cleanup;
6481 }
6482
6483 /* success */
6484 ret = LY_SUCCESS;
6485
6486cleanup:
6487 lysc_update_path(ctx, NULL, NULL);
6488 return ret;
6489}
6490
6491LY_ERR
6492lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6493{
6494 LY_ERR ret = LY_EVALID;
6495 struct ly_set devs_p = {0};
6496 struct ly_set targets = {0};
6497 struct lysc_node *target; /* target target of the deviation */
6498 struct lysp_deviation *dev;
6499 struct lysp_deviate *d, **dp_new;
6500 LY_ARRAY_COUNT_TYPE u, v;
6501 struct lysc_deviation **devs = NULL;
6502 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006503 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006504
6505 /* get all deviations from the module and all its submodules ... */
6506 LY_ARRAY_FOR(mod_p->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006507 LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST, NULL));
Radek Krejciccd20f12019-02-15 14:12:27 +01006508 }
6509 LY_ARRAY_FOR(mod_p->includes, v) {
6510 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006511 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 +01006512 }
6513 }
6514 if (!devs_p.count) {
6515 /* nothing to do */
6516 return LY_SUCCESS;
6517 }
6518
Radek Krejci327de162019-06-14 12:52:07 +02006519 lysc_update_path(ctx, NULL, "{deviation}");
6520
Radek Krejciccd20f12019-02-15 14:12:27 +01006521 /* ... and group them by the target node */
6522 devs = calloc(devs_p.count, sizeof *devs);
6523 for (u = 0; u < devs_p.count; ++u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006524 uint32_t index;
6525
Radek Krejciccd20f12019-02-15 14:12:27 +01006526 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006527 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006528
6529 /* resolve the target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02006530 LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
Michal Vasko22df3f02020-08-24 13:29:22 +02006531 (const struct lysc_node **)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006532 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006533 /* move the target pointer to input/output to make them different from the action and
6534 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6535 * back to the RPC/action node due to a better compatibility and decision code in this function.
6536 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6537 if (flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006538 target = (struct lysc_node *)&((struct lysc_action *)target)->input;
Radek Krejcif538ce52019-03-05 10:46:14 +01006539 flags |= LYSC_OPT_INTERNAL;
6540 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006541 target = (struct lysc_node *)&((struct lysc_action *)target)->output;
Radek Krejcif538ce52019-03-05 10:46:14 +01006542 flags |= LYSC_OPT_INTERNAL;
6543 }
6544 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006545 /* insert into the set of targets with duplicity detection */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006546 ret = ly_set_add(&targets, target, 0, &index);
6547 LY_CHECK_GOTO(ret, cleanup);
6548 if (!devs[index]) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006549 /* new record */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006550 devs[index] = calloc(1, sizeof **devs);
6551 devs[index]->target = target;
6552 devs[index]->nodeid = dev->nodeid;
6553 devs[index]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006554 }
6555 /* add deviates into the deviation's list of deviates */
Michal Vasko5c38f362020-08-24 09:22:51 +02006556 LY_LIST_FOR(dev->deviates, d) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006557 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[index]->deviates, dp_new, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006558 *dp_new = d;
6559 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006560 devs[index]->not_supported = 1;
Radek Krejciccd20f12019-02-15 14:12:27 +01006561 }
6562 }
Radek Krejci327de162019-06-14 12:52:07 +02006563
6564 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006565 }
6566
Radek Krejciccd20f12019-02-15 14:12:27 +01006567 /* apply deviations */
6568 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006569 ly_bool match = 0;
Radek Krejciba03a5a2020-08-27 14:40:41 +02006570
Radek Krejcif538ce52019-03-05 10:46:14 +01006571 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6572 /* fix the target pointer in case of RPC's/action's input/output */
6573 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006574 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006575 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006576 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006577 }
6578 }
6579
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006580 /* remember target module (the target node may be removed) */
6581 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006582
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006583 /* apply the deviation */
6584 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006585
Michal Vaskoe6143202020-07-03 13:02:08 +02006586 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006587 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6588 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006589 match = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006590 break;
6591 }
6592 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02006593 if (!match) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006594 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006595 *dev_mod = mod_p->mod;
6596 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006597 }
6598
Radek Krejci327de162019-06-14 12:52:07 +02006599 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006600 ret = LY_SUCCESS;
6601
6602cleanup:
6603 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6604 LY_ARRAY_FREE(devs[u]->deviates);
6605 free(devs[u]);
6606 }
6607 free(devs);
6608 ly_set_erase(&targets, NULL);
6609 ly_set_erase(&devs_p, NULL);
6610
6611 return ret;
6612}
6613
Radek Krejcib56c7502019-02-13 14:19:54 +01006614/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006615 * @brief Compile the given YANG submodule into the main module.
6616 * @param[in] ctx Compile context
6617 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006618 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6619 */
6620LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006621lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006622{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006623 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006624 LY_ERR ret = LY_SUCCESS;
6625 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006626 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006627 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006628 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006629
Michal Vasko33ff9422020-07-03 09:50:39 +02006630 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006631 /* features are compiled directly into the compiled module structure,
6632 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6633 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006634 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6635 LY_CHECK_GOTO(ret, error);
6636 }
Radek Krejci0af46292019-01-11 16:02:31 +01006637
Michal Vasko33ff9422020-07-03 09:50:39 +02006638 if (!mainmod->mod->dis_identities) {
6639 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6640 LY_CHECK_GOTO(ret, error);
6641 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006642
Radek Krejci474f9b82019-07-24 11:36:37 +02006643 /* data nodes */
6644 LY_LIST_FOR(submod->data, node_p) {
6645 ret = lys_compile_node(ctx, node_p, NULL, 0);
6646 LY_CHECK_GOTO(ret, error);
6647 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006648
Radek Krejciec4da802019-05-02 13:02:41 +02006649 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6650 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006651
Radek Krejcid05cbd92018-12-05 14:26:40 +01006652error:
6653 return ret;
6654}
6655
Radek Krejci335332a2019-09-05 13:03:35 +02006656static void *
6657lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6658{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006659 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci335332a2019-09-05 13:03:35 +02006660 if (substmts[u].stmt == stmt) {
6661 return substmts[u].storage;
6662 }
6663 }
6664 return NULL;
6665}
6666
6667LY_ERR
6668lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6669{
6670 LY_ERR ret = LY_EVALID, r;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006671 LY_ARRAY_COUNT_TYPE u;
Radek Krejci335332a2019-09-05 13:03:35 +02006672 struct lysp_stmt *stmt;
6673 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006674
6675 /* check for invalid substatements */
6676 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006677 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6678 continue;
6679 }
Radek Krejci335332a2019-09-05 13:03:35 +02006680 for (u = 0; substmts[u].stmt; ++u) {
6681 if (substmts[u].stmt == stmt->kw) {
6682 break;
6683 }
6684 }
6685 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6687 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006688 goto cleanup;
6689 }
Radek Krejci335332a2019-09-05 13:03:35 +02006690 }
6691
Radek Krejciad5963b2019-09-06 16:03:05 +02006692 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6693
Radek Krejci335332a2019-09-05 13:03:35 +02006694 /* keep order of the processing the same as the order in the defined substmts,
6695 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6696 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006697 ly_bool stmt_present = 0;
Radek Krejciad5963b2019-09-06 16:03:05 +02006698
Radek Krejci335332a2019-09-05 13:03:35 +02006699 for (stmt = ext->child; stmt; stmt = stmt->next) {
6700 if (substmts[u].stmt != stmt->kw) {
6701 continue;
6702 }
6703
Radek Krejciad5963b2019-09-06 16:03:05 +02006704 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006705 if (substmts[u].storage) {
6706 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006707 case LY_STMT_STATUS:
6708 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6709 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6710 break;
6711 case LY_STMT_UNITS: {
6712 const char **units;
6713
6714 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6715 /* single item */
6716 if (*((const char **)substmts[u].storage)) {
6717 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6718 goto cleanup;
6719 }
6720 units = (const char **)substmts[u].storage;
6721 } else {
6722 /* sized array */
6723 const char ***units_array = (const char ***)substmts[u].storage;
6724 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6725 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02006726 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
6727 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +02006728 break;
6729 }
Radek Krejci335332a2019-09-05 13:03:35 +02006730 case LY_STMT_TYPE: {
6731 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6732 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6733
6734 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6735 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006736 if (*(struct lysc_type **)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6738 goto cleanup;
6739 }
6740 compiled = substmts[u].storage;
6741 } else {
6742 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006743 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006744 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006745 compiled = (void *)type;
Radek Krejci335332a2019-09-05 13:03:35 +02006746 }
6747
Radek Krejciad5963b2019-09-06 16:03:05 +02006748 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006749 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL,
6750 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006751 units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02006752 lysp_type_free(ctx->ctx, parsed);
6753 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006754 break;
6755 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006756 case LY_STMT_IF_FEATURE: {
6757 struct lysc_iffeature *iff = NULL;
6758
6759 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6760 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006761 if (((struct lysc_iffeature *)substmts[u].storage)->features) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006762 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6763 goto cleanup;
6764 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006765 iff = (struct lysc_iffeature *)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006766 } else {
6767 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006768 struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006769 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6770 }
6771 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6772 break;
6773 }
6774 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6775 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006776 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006777 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.",
6778 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006779 goto cleanup;
6780 }
6781 }
Radek Krejci335332a2019-09-05 13:03:35 +02006782 }
Radek Krejci335332a2019-09-05 13:03:35 +02006783
Radek Krejciad5963b2019-09-06 16:03:05 +02006784 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6786 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6787 goto cleanup;
6788 }
Radek Krejci335332a2019-09-05 13:03:35 +02006789 }
6790
6791 ret = LY_SUCCESS;
6792
6793cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006794 return ret;
6795}
6796
Michal Vasko175012e2019-11-06 15:49:14 +01006797/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006798 * @brief Check when for cyclic dependencies.
6799 * @param[in] set Set with all the referenced nodes.
6800 * @param[in] node Node whose "when" referenced nodes are in @p set.
6801 * @return LY_ERR value
6802 */
6803static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006804lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006805{
6806 struct lyxp_set tmp_set;
6807 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006808 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006809 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006810 struct lysc_when *when;
6811 LY_ERR ret = LY_SUCCESS;
6812
6813 memset(&tmp_set, 0, sizeof tmp_set);
6814
6815 /* prepare in_ctx of the set */
Michal Vaskod989ba02020-08-24 10:59:24 +02006816 for (i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006817 xp_scnode = &set->val.scnodes[i];
6818
Michal Vasko5c4e5892019-11-14 12:31:38 +01006819 if (xp_scnode->in_ctx != -1) {
6820 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006821 xp_scnode->in_ctx = 1;
6822 }
6823 }
6824
6825 for (i = 0; i < set->used; ++i) {
6826 xp_scnode = &set->val.scnodes[i];
6827 if (xp_scnode->in_ctx != 1) {
6828 /* already checked */
6829 continue;
6830 }
6831
Michal Vasko1bf09392020-03-27 12:38:10 +01006832 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006833 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006834 /* no when to check */
6835 xp_scnode->in_ctx = 0;
6836 continue;
6837 }
6838
6839 node = xp_scnode->scnode;
6840 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006841 LY_ARRAY_FOR(node->when, u) {
6842 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006843 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006844 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6845 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006846 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006847 goto cleanup;
6848 }
6849
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006850 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006851 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006852 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006853 /* try to find this node in our set */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006854 uint32_t idx;
6855 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 +01006856 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 +01006857 ret = LY_EVALID;
6858 goto cleanup;
6859 }
6860
6861 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006862 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006863 } else {
6864 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006865 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006866 }
6867 }
6868
6869 /* merge this set into the global when set */
6870 lyxp_set_scnode_merge(set, &tmp_set);
6871 }
6872
6873 /* check when of non-data parents as well */
6874 node = node->parent;
6875 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6876
Michal Vasko251f56e2019-11-14 16:06:47 +01006877 /* this node when was checked (xp_scnode could have been reallocd) */
6878 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006879 }
6880
6881cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006882 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006883 return ret;
6884}
6885
6886/**
Michal Vasko175012e2019-11-06 15:49:14 +01006887 * @brief Check when/must expressions of a node on a compiled schema tree.
6888 * @param[in] ctx Compile context.
6889 * @param[in] node Node to check.
6890 * @return LY_ERR value
6891 */
6892static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006893lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006894{
Michal Vasko175012e2019-11-06 15:49:14 +01006895 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006896 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006897 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006898 uint32_t opts;
Radek Krejci857189e2020-09-01 13:26:36 +02006899 ly_bool input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006900 struct lysc_when **when = NULL;
6901 struct lysc_must *musts = NULL;
6902 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006903 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006904
6905 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006906 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006907 if (node->flags & LYS_CONFIG_R) {
6908 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6909 if (op) {
6910 /* we are actually in output */
6911 opts = LYXP_SCNODE_OUTPUT;
6912 }
6913 }
Michal Vasko175012e2019-11-06 15:49:14 +01006914
6915 switch (node->nodetype) {
6916 case LYS_CONTAINER:
6917 when = ((struct lysc_node_container *)node)->when;
6918 musts = ((struct lysc_node_container *)node)->musts;
6919 break;
6920 case LYS_CHOICE:
6921 when = ((struct lysc_node_choice *)node)->when;
6922 break;
6923 case LYS_LEAF:
6924 when = ((struct lysc_node_leaf *)node)->when;
6925 musts = ((struct lysc_node_leaf *)node)->musts;
6926 break;
6927 case LYS_LEAFLIST:
6928 when = ((struct lysc_node_leaflist *)node)->when;
6929 musts = ((struct lysc_node_leaflist *)node)->musts;
6930 break;
6931 case LYS_LIST:
6932 when = ((struct lysc_node_list *)node)->when;
6933 musts = ((struct lysc_node_list *)node)->musts;
6934 break;
6935 case LYS_ANYXML:
6936 case LYS_ANYDATA:
6937 when = ((struct lysc_node_anydata *)node)->when;
6938 musts = ((struct lysc_node_anydata *)node)->musts;
6939 break;
6940 case LYS_CASE:
6941 when = ((struct lysc_node_case *)node)->when;
6942 break;
6943 case LYS_NOTIF:
6944 musts = ((struct lysc_notif *)node)->musts;
6945 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006946 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006947 case LYS_ACTION:
6948 /* first process input musts */
6949 musts = ((struct lysc_action *)node)->input.musts;
6950 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006951 default:
6952 /* nothing to check */
6953 break;
6954 }
6955
Michal Vasko175012e2019-11-06 15:49:14 +01006956 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006957 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006958 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 +02006959 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006960 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006961 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006962 goto cleanup;
6963 }
6964
Michal Vaskodc052f32019-11-07 11:11:38 +01006965 ctx->path[0] = '\0';
6966 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006967 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006968 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006969 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6970 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006971
Michal Vaskoecd62de2019-11-13 12:35:11 +01006972 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006973 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006974 schema->name);
6975 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006976
6977 /* check dummy node accessing */
6978 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006979 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006980 ret = LY_EVALID;
6981 goto cleanup;
6982 }
Michal Vasko175012e2019-11-06 15:49:14 +01006983 }
6984 }
6985
Michal Vaskoecd62de2019-11-13 12:35:11 +01006986 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006987 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006988 LY_CHECK_GOTO(ret, cleanup);
6989
Michal Vaskod3678892020-05-21 10:06:58 +02006990 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006991 }
6992
Michal Vasko5d8756a2019-11-07 15:21:00 +01006993check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006994 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006995 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006996 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 +01006997 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006998 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006999 goto cleanup;
7000 }
7001
Michal Vaskodc052f32019-11-07 11:11:38 +01007002 ctx->path[0] = '\0';
7003 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007004 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007005 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007006 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01007007 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007008 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
7009 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01007010 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01007011 }
7012 }
7013
Michal Vaskod3678892020-05-21 10:06:58 +02007014 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007015 }
7016
Michal Vasko1bf09392020-03-27 12:38:10 +01007017 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01007018 /* now check output musts */
7019 input_done = 1;
7020 musts = ((struct lysc_action *)node)->output.musts;
7021 opts = LYXP_SCNODE_OUTPUT;
7022 goto check_musts;
7023 }
7024
Michal Vasko175012e2019-11-06 15:49:14 +01007025cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007026 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007027 return ret;
7028}
7029
Michal Vasko8d544252020-03-02 10:19:52 +01007030static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007031lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
7032{
Michal Vasko6b26e742020-07-17 15:02:10 +02007033 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02007034 struct ly_path *p;
7035 struct lysc_type *type;
7036
7037 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7038
7039 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02007040 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
7041 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007042 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02007043
7044 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007045 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02007046 ly_path_free(node->module->ctx, p);
7047
7048 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7049 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7050 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
7051 lref->path->expr, lys_nodetype2str(target->nodetype));
7052 return LY_EVALID;
7053 }
7054
7055 /* check status */
7056 ctx->path[0] = '\0';
7057 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7058 ctx->path_len = strlen(ctx->path);
7059 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
7060 return LY_EVALID;
7061 }
7062 ctx->path_len = 1;
7063 ctx->path[1] = '\0';
7064
7065 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007066 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007067 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007068 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007069 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7070 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7071 return LY_EVALID;
7072 }
7073 }
7074
7075 /* store the target's type and check for circular chain of leafrefs */
7076 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7077 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7078 if (type == (struct lysc_type *)lref) {
7079 /* circular chain detected */
7080 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7081 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7082 return LY_EVALID;
7083 }
7084 }
7085
7086 /* check if leafref and its target are under common if-features */
7087 if (lys_compile_leafref_features_validate(node, target)) {
7088 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7089 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7090 " features applicable to the leafref itself.", lref->path->expr);
7091 return LY_EVALID;
7092 }
7093
7094 return LY_SUCCESS;
7095}
7096
7097static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007098lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7099{
7100 struct lysc_ext_instance *ext;
7101 struct lysp_ext_instance *ext_p = NULL;
7102 struct lysp_stmt *stmt;
7103 const struct lys_module *ext_mod;
7104 LY_ERR ret = LY_SUCCESS;
7105
7106 /* create the parsed extension instance manually */
7107 ext_p = calloc(1, sizeof *ext_p);
7108 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007109 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
7110 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007111 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7112 ext_p->insubstmt_index = 0;
7113
Radek Krejci87e25252020-09-15 13:28:31 +02007114 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
7115 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007116 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
7117 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007118 stmt->kw = LY_STMT_TYPE;
Michal Vasko8d544252020-03-02 10:19:52 +01007119
7120 /* allocate new extension instance */
7121 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7122
7123 /* manually get extension definition module */
7124 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7125
7126 /* compile the extension instance */
7127 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7128
7129cleanup:
7130 lysp_ext_instance_free(ctx->ctx, ext_p);
7131 free(ext_p);
7132 return ret;
7133}
7134
Michal Vasko004d3152020-06-11 19:59:22 +02007135static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007136lys_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 +02007137 const struct lys_module *dflt_mod, struct lyd_value *storage)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007138{
7139 LY_ERR ret;
7140 struct ly_err_item *err = NULL;
7141
7142 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7143 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7144 if (err) {
7145 ly_err_print(err);
7146 ctx->path[0] = '\0';
7147 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7148 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7149 "Invalid default - value does not fit the type (%s).", err->msg);
7150 ly_err_free(err);
7151 }
7152 if (!ret) {
7153 ++storage->realtype->refcount;
7154 return LY_SUCCESS;
7155 }
7156 return ret;
7157}
7158
7159static LY_ERR
7160lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +02007161 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007162{
7163 LY_ERR ret;
7164
7165 assert(!leaf->dflt);
7166
7167 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7168 /* ignore default values for keys and mandatory leaves */
7169 return LY_SUCCESS;
7170 }
7171
7172 /* allocate the default value */
7173 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7174 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7175
7176 /* store the default value */
7177 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt);
7178 if (ret) {
7179 free(leaf->dflt);
7180 leaf->dflt = NULL;
7181 }
7182
7183 return ret;
7184}
7185
7186static LY_ERR
7187lys_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 +02007188 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007189{
7190 LY_ERR ret;
7191 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7192
7193 assert(dflt || dflts);
7194
7195 if (llist->dflts) {
7196 /* there were already some defaults and we are adding new by deviations */
7197 assert(dflts);
7198 orig_count = LY_ARRAY_COUNT(llist->dflts);
7199 } else {
7200 orig_count = 0;
7201 }
7202
7203 /* allocate new items */
7204 if (dflts) {
7205 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7206 } else {
7207 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7208 }
7209
7210 /* fill each new default value */
7211 if (dflts) {
7212 LY_ARRAY_FOR(dflts, u) {
7213 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
7214 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod,
7215 llist->dflts[orig_count + u]);
7216 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7217 LY_ARRAY_INCREMENT(llist->dflts);
7218 }
7219 } else {
7220 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
7221 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]);
7222 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7223 LY_ARRAY_INCREMENT(llist->dflts);
7224 }
7225
7226 /* check default value uniqueness */
7227 if (llist->flags & LYS_CONFIG_W) {
7228 /* configuration data values must be unique - so check the default values */
7229 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7230 for (v = 0; v < u; ++v) {
7231 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejci857189e2020-09-01 13:26:36 +02007232 ly_bool dynamic = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007233 const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic);
7234
7235 lysc_update_path(ctx, llist->parent, llist->name);
7236 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7237 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
7238 lysc_update_path(ctx, NULL, NULL);
7239 if (dynamic) {
7240 free((char *)val);
7241 }
7242 return LY_EVALID;
7243 }
7244 }
7245 }
7246 }
7247
7248 return LY_SUCCESS;
7249}
7250
7251static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007252lys_compile_unres(struct lysc_ctx *ctx)
7253{
7254 struct lysc_node *node;
7255 struct lysc_type *type, *typeiter;
7256 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007257 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007258 uint32_t i;
7259
7260 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7261 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7262 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7263 for (i = 0; i < ctx->leafrefs.count; ++i) {
7264 node = ctx->leafrefs.objs[i];
7265 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7266 type = ((struct lysc_node_leaf *)node)->type;
7267 if (type->basetype == LY_TYPE_LEAFREF) {
7268 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7269 } else if (type->basetype == LY_TYPE_UNION) {
7270 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7271 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7272 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7273 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7274 }
7275 }
7276 }
7277 }
7278 for (i = 0; i < ctx->leafrefs.count; ++i) {
7279 /* store pointer to the real type */
7280 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7281 if (type->basetype == LY_TYPE_LEAFREF) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007282 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007283 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007284 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7285 ((struct lysc_type_leafref *)type)->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007286 } else if (type->basetype == LY_TYPE_UNION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007287 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7288 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7289 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007290 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007291 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7292 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007293 }
7294 }
7295 }
7296 }
7297
7298 /* check xpath */
7299 for (i = 0; i < ctx->xpath.count; ++i) {
7300 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7301 }
7302
7303 /* finish incomplete default values compilation */
7304 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007305 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007306 if (r->leaf->nodetype == LYS_LEAF) {
7307 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod));
7308 } else {
7309 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 +02007310 }
Michal Vasko004d3152020-06-11 19:59:22 +02007311 }
7312
7313 return LY_SUCCESS;
7314}
7315
Radek Krejci19a96102018-11-15 13:38:09 +01007316LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02007317lys_compile(struct lys_module *mod, uint32_t options)
Radek Krejci19a96102018-11-15 13:38:09 +01007318{
7319 struct lysc_ctx ctx = {0};
7320 struct lysc_module *mod_c;
7321 struct lysp_module *sp;
7322 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007323 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007324 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007325 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007326 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007327 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007328 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007329 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007330
Michal Vasko7a0b0762020-09-02 16:37:01 +02007331 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007332
Michal Vasko7a0b0762020-09-02 16:37:01 +02007333 if (!mod->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007334 /* just imported modules are not compiled */
7335 return LY_SUCCESS;
7336 }
7337
Michal Vasko7a0b0762020-09-02 16:37:01 +02007338 compile_id = ++mod->ctx->module_set_id;
7339 sp = mod->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007340
Michal Vasko7a0b0762020-09-02 16:37:01 +02007341 ctx.ctx = mod->ctx;
7342 ctx.mod = mod;
7343 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007344 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007345 ctx.path_len = 1;
7346 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007347
Michal Vasko7a0b0762020-09-02 16:37:01 +02007348 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
7349 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7350 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007351
Michal Vasko7c8439f2020-08-05 13:25:19 +02007352 LY_ARRAY_FOR(sp->imports, u) {
7353 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7354 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01007355 LY_ARRAY_FOR(sp->includes, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02007356 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007357 }
Radek Krejci0935f412019-08-20 16:15:18 +02007358
7359 /* features */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007360 if (mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007361 /* there is already precompiled array of features */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007362 mod_c->features = mod->dis_features;
7363 mod->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007364 } else {
7365 /* features are compiled directly into the compiled module structure,
7366 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci327de162019-06-14 12:52:07 +02007367 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007368 LY_CHECK_GOTO(ret, error);
7369 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007370
Radek Krejci0af46292019-01-11 16:02:31 +01007371 /* finish feature compilation, not only for the main module, but also for the submodules.
7372 * Due to possible forward references, it must be done when all the features (including submodules)
7373 * are present. */
7374 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007375 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007376 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7377 }
Radek Krejci327de162019-06-14 12:52:07 +02007378 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007379 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007380 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007381 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007382 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007383 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7384 }
Radek Krejci327de162019-06-14 12:52:07 +02007385 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007386 }
Radek Krejci327de162019-06-14 12:52:07 +02007387 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007388
Michal Vasko33ff9422020-07-03 09:50:39 +02007389 /* identities, work similarly to features with the precompilation */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007390 if (mod->dis_identities) {
7391 mod_c->identities = mod->dis_identities;
7392 mod->dis_identities = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02007393 } else {
7394 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7395 LY_CHECK_GOTO(ret, error);
7396 }
Radek Krejci19a96102018-11-15 13:38:09 +01007397 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007398 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007399 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007400 lysc_update_path(&ctx, NULL, "{submodule}");
7401 LY_ARRAY_FOR(sp->includes, v) {
7402 if (sp->includes[v].submodule->identities) {
7403 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7404 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7405 LY_CHECK_GOTO(ret, error);
7406 lysc_update_path(&ctx, NULL, NULL);
7407 }
7408 }
Radek Krejci327de162019-06-14 12:52:07 +02007409 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007410
Radek Krejci95710c92019-02-11 15:49:55 +01007411 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007412 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007413 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007414 }
Radek Krejci95710c92019-02-11 15:49:55 +01007415
Radek Krejciec4da802019-05-02 13:02:41 +02007416 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7417 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007418
Radek Krejci95710c92019-02-11 15:49:55 +01007419 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007420 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007421 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007422 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007423 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007424
Radek Krejci474f9b82019-07-24 11:36:37 +02007425 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007426 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007427
Radek Krejci0935f412019-08-20 16:15:18 +02007428 /* extension instances TODO cover extension instances from submodules */
7429 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007430
Michal Vasko004d3152020-06-11 19:59:22 +02007431 /* finish compilation for all unresolved items in the context */
7432 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007433
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007434 /* validate non-instantiated groupings from the parsed schema,
7435 * without it we would accept even the schemas with invalid grouping specification */
7436 ctx.options |= LYSC_OPT_GROUPING;
7437 LY_ARRAY_FOR(sp->groupings, u) {
7438 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007439 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007440 }
7441 }
7442 LY_LIST_FOR(sp->data, node_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007443 grps = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007444 LY_ARRAY_FOR(grps, u) {
7445 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007446 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007447 }
7448 }
7449 }
7450
Radek Krejci474f9b82019-07-24 11:36:37 +02007451 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7452 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7453 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7454 }
7455
Michal Vasko8d544252020-03-02 10:19:52 +01007456#if 0
7457 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7458 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7459 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7460 * the anotation definitions available in the internal schema structure. */
7461 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7462 if (lyp_add_ietf_netconf_annotations(mod)) {
7463 lys_free(mod, NULL, 1, 1);
7464 return NULL;
7465 }
7466 }
7467#endif
7468
7469 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007470 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
7471 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007472 }
7473
Radek Krejci1c0c3442019-07-23 16:08:47 +02007474 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007475 ly_set_erase(&ctx.xpath, NULL);
7476 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007477 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007478 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007479 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007480
Radek Krejciec4da802019-05-02 13:02:41 +02007481 if (ctx.options & LYSC_OPT_FREE_SP) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02007482 lysp_module_free(mod->parsed);
7483 mod->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007484 }
7485
Radek Krejciec4da802019-05-02 13:02:41 +02007486 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007487 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007488 for (i = 0; i < ctx.ctx->list.count; ++i) {
7489 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007490 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007491 m->implemented = 1;
7492 }
7493 }
7494 }
7495
Radek Krejci19a96102018-11-15 13:38:09 +01007496 return LY_SUCCESS;
7497
7498error:
Michal Vasko7a0b0762020-09-02 16:37:01 +02007499 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007500 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007501 ly_set_erase(&ctx.xpath, NULL);
7502 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007503 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007504 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007505 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007506 lysc_module_free(mod_c, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02007507 mod->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007508
Radek Krejcia46012b2020-08-12 15:41:04 +02007509 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7510 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7511 * 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 +02007512 for (i = 0; i < ctx.ctx->list.count; ++i) {
7513 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007514 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007515 /* revert features list to the precompiled state */
7516 lys_feature_precompile_revert(&ctx, m);
7517 /* mark module as imported-only / not-implemented */
7518 m->implemented = 0;
7519 /* free the compiled version of the module */
7520 lysc_module_free(m->compiled, NULL);
7521 m->compiled = NULL;
7522 }
7523 }
7524
Radek Krejci19a96102018-11-15 13:38:09 +01007525 return ret;
7526}