blob: d7f08726d01292634015a777a49f318cf8c26544 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Michal Vasko7c8439f2020-08-05 13:25:19 +020039#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010041#include "tree_schema_internal.h"
42#include "xpath.h"
43
Michal Vasko5fe75f12020-03-02 13:52:37 +010044static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
Radek Krejci0f969882020-08-21 16:56:47 +020045 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
Michal Vasko5fe75f12020-03-02 13:52:37 +010046
Radek Krejci19a96102018-11-15 13:38:09 +010047/**
48 * @brief Duplicate string into dictionary
49 * @param[in] CTX libyang context of the dictionary.
50 * @param[in] ORIG String to duplicate.
51 * @param[out] DUP Where to store the result.
52 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020053#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
54
55#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
Radek Krejci19a96102018-11-15 13:38:09 +010056
Radek Krejciec4da802019-05-02 13:02:41 +020057#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010058 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020059 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
60 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
61 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020063 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010064 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
65 } \
66 }
67
Radek Krejciec4da802019-05-02 13:02:41 +020068#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010069 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020070 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
71 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
72 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010073 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020074 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010075 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
76 } \
77 }
78
Radek Krejci0935f412019-08-20 16:15:18 +020079#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
80 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020081 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
82 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020083 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010084 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020085 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
86 } \
87 }
88
Radek Krejciec4da802019-05-02 13:02:41 +020089#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010090 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020091 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
92 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
93 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010094 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020095 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010096 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
97 } \
98 }
99
Radek Krejciec4da802019-05-02 13:02:41 +0200100#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +0100101 if (MEMBER_P) { \
102 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
103 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200104 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100105 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
106 }
107
Radek Krejciec4da802019-05-02 13:02:41 +0200108#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 if (MEMBER_P) { \
110 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200111 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100112 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200113 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100114 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
115 }
116
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100117#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200119 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100120 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100121 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
122 return LY_EVALID; \
123 } \
124 } \
125 }
126
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
128 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200129 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100130 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
131 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
132 return LY_EVALID; \
133 } \
134 } \
135 }
136
137struct lysc_ext *
138lysc_ext_dup(struct lysc_ext *orig)
139{
140 ++orig->refcount;
141 return orig;
142}
143
Radek Krejci19a96102018-11-15 13:38:09 +0100144static struct lysc_ext_instance *
145lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
146{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100147 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100148 (void) ctx;
149 (void) orig;
150 return NULL;
151}
152
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200153static LY_ERR
154lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +0200155 struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200156{
157 struct lysc_incomplete_dflt *r;
158 uint32_t i;
159
160 for (i = 0; i < ctx->dflts.count; ++i) {
161 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
162 if (r->leaf == leaf) {
163 /* just replace the default */
164 r->dflt = dflt;
165 return LY_SUCCESS;
166 }
167 }
168
169 r = malloc(sizeof *r);
170 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
171 r->leaf = leaf;
172 r->dflt = dflt;
173 r->dflts = NULL;
174 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200175 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200176
177 return LY_SUCCESS;
178}
179
Radek Krejcib56c7502019-02-13 14:19:54 +0100180/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200181 * @brief Add record into the compile context's list of incomplete default values.
182 * @param[in] ctx Compile context with the incomplete default values list.
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200183 * @param[in] term Term context node with the default value.
184 * @param[in] value String default value.
185 * @param[in] val_len Length of @p value.
Radek Krejci474f9b82019-07-24 11:36:37 +0200186 * @param[in] dflt_mod Module of the default value definition to store in the record.
187 * @return LY_EMEM in case of memory allocation failure.
188 * @return LY_SUCCESS
189 */
190static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200191lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts,
Radek Krejci0f969882020-08-21 16:56:47 +0200192 struct lys_module *dflt_mod)
Radek Krejci474f9b82019-07-24 11:36:37 +0200193{
194 struct lysc_incomplete_dflt *r;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200195 uint32_t i;
196
197 for (i = 0; i < ctx->dflts.count; ++i) {
198 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
199 if (r->llist == llist) {
200 /* just replace the defaults */
201 r->dflts = dflts;
202 return LY_SUCCESS;
203 }
204 }
205
Radek Krejci474f9b82019-07-24 11:36:37 +0200206 r = malloc(sizeof *r);
207 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200208 r->llist = llist;
209 r->dflt = NULL;
210 r->dflts = dflts;
Radek Krejci474f9b82019-07-24 11:36:37 +0200211 r->dflt_mod = dflt_mod;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200212 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
Radek Krejci474f9b82019-07-24 11:36:37 +0200213
214 return LY_SUCCESS;
215}
216
217/**
218 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
219 * @param[in] ctx Compile context with the incomplete default values list.
220 * @param[in] dflt Incomplete default values identifying the record to remove.
221 */
222static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200223lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term)
Radek Krejci474f9b82019-07-24 11:36:37 +0200224{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200225 uint32_t u;
Radek Krejci474f9b82019-07-24 11:36:37 +0200226 struct lysc_incomplete_dflt *r;
227
228 for (u = 0; u < ctx->dflts.count; ++u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200229 r = ctx->dflts.objs[u];
230 if (r->leaf == (struct lysc_node_leaf *)term) {
Radek Krejci474f9b82019-07-24 11:36:37 +0200231 free(ctx->dflts.objs[u]);
232 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
233 --ctx->dflts.count;
234 return;
235 }
236 }
237}
238
Radek Krejci0e59c312019-08-15 15:34:15 +0200239void
Radek Krejci327de162019-06-14 12:52:07 +0200240lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
241{
242 int len;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200243 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
Radek Krejci327de162019-06-14 12:52:07 +0200244
245 if (!name) {
246 /* removing last path segment */
247 if (ctx->path[ctx->path_len - 1] == '}') {
Michal Vaskod989ba02020-08-24 10:59:24 +0200248 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200249 if (ctx->path[ctx->path_len] == '=') {
250 ctx->path[ctx->path_len++] = '}';
251 } else {
252 /* not a top-level special tag, remove also preceiding '/' */
253 goto remove_nodelevel;
254 }
255 } else {
256remove_nodelevel:
Michal Vaskod989ba02020-08-24 10:59:24 +0200257 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200258 if (ctx->path_len == 0) {
259 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200260 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200261 }
262 }
263 /* set new terminating NULL-byte */
264 ctx->path[ctx->path_len] = '\0';
265 } else {
266 if (ctx->path_len > 1) {
267 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
268 /* extension of the special tag */
269 nextlevel = 2;
270 --ctx->path_len;
271 } else {
272 /* there is already some path, so add next level */
273 nextlevel = 1;
274 }
275 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
276
277 if (nextlevel != 2) {
278 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
279 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200280 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200281 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200282 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200283 }
284 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200285 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200286 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200287 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
Radek Krejciacc79042019-07-25 14:14:57 +0200288 /* output truncated */
289 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
290 } else {
291 ctx->path_len += len;
292 }
Radek Krejci327de162019-06-14 12:52:07 +0200293 }
294}
295
296/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100297 * @brief Duplicate the compiled pattern structure.
298 *
299 * Instead of duplicating memory, the reference counter in the @p orig is increased.
300 *
301 * @param[in] orig The pattern structure to duplicate.
302 * @return The duplicated structure to use.
303 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200304static struct lysc_pattern *
Radek Krejci19a96102018-11-15 13:38:09 +0100305lysc_pattern_dup(struct lysc_pattern *orig)
306{
307 ++orig->refcount;
308 return orig;
309}
310
Radek Krejcib56c7502019-02-13 14:19:54 +0100311/**
312 * @brief Duplicate the array of compiled patterns.
313 *
314 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
315 *
316 * @param[in] ctx Libyang context for logging.
317 * @param[in] orig The patterns sized array to duplicate.
318 * @return New sized array as a copy of @p orig.
319 * @return NULL in case of memory allocation error.
320 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200321static struct lysc_pattern **
Radek Krejci19a96102018-11-15 13:38:09 +0100322lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
323{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100324 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200325 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327 assert(orig);
328
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200329 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100330 LY_ARRAY_FOR(orig, u) {
331 dup[u] = lysc_pattern_dup(orig[u]);
332 LY_ARRAY_INCREMENT(dup);
333 }
334 return dup;
335}
336
Radek Krejcib56c7502019-02-13 14:19:54 +0100337/**
338 * @brief Duplicate compiled range structure.
339 *
340 * @param[in] ctx Libyang context for logging.
341 * @param[in] orig The range structure to be duplicated.
342 * @return New compiled range structure as a copy of @p orig.
343 * @return NULL in case of memory allocation error.
344 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200345struct lysc_range *
Radek Krejci19a96102018-11-15 13:38:09 +0100346lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
347{
348 struct lysc_range *dup;
349 LY_ERR ret;
350
Radek Krejcib56c7502019-02-13 14:19:54 +0100351 assert(orig);
352
Radek Krejci19a96102018-11-15 13:38:09 +0100353 dup = calloc(1, sizeof *dup);
354 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
355 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200356 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
357 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
358 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100359 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200360 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
361 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100362 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
363
364 return dup;
365cleanup:
366 free(dup);
367 (void) ret; /* set but not used due to the return type */
368 return NULL;
369}
370
Radek Krejcib56c7502019-02-13 14:19:54 +0100371/**
372 * @brief Stack for processing if-feature expressions.
373 */
Radek Krejci19a96102018-11-15 13:38:09 +0100374struct iff_stack {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200375 size_t size; /**< number of items in the stack */
376 size_t index; /**< first empty item */
377 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100378};
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Add @ref ifftokens into the stack.
382 * @param[in] stack The if-feature stack to use.
383 * @param[in] value One of the @ref ifftokens to store in the stack.
384 * @return LY_EMEM in case of memory allocation error
385 * @return LY_ESUCCESS if the value successfully stored.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static LY_ERR
388iff_stack_push(struct iff_stack *stack, uint8_t value)
389{
390 if (stack->index == stack->size) {
391 stack->size += 4;
392 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
393 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
394 }
395 stack->stack[stack->index++] = value;
396 return LY_SUCCESS;
397}
398
Radek Krejcib56c7502019-02-13 14:19:54 +0100399/**
400 * @brief Get (and remove) the last item form the stack.
401 * @param[in] stack The if-feature stack to use.
402 * @return The value from the top of the stack.
403 */
Radek Krejci19a96102018-11-15 13:38:09 +0100404static uint8_t
405iff_stack_pop(struct iff_stack *stack)
406{
Radek Krejcib56c7502019-02-13 14:19:54 +0100407 assert(stack && stack->index);
408
Radek Krejci19a96102018-11-15 13:38:09 +0100409 stack->index--;
410 return stack->stack[stack->index];
411}
412
Radek Krejcib56c7502019-02-13 14:19:54 +0100413/**
414 * @brief Clean up the stack.
415 * @param[in] stack The if-feature stack to use.
416 */
Radek Krejci19a96102018-11-15 13:38:09 +0100417static void
418iff_stack_clean(struct iff_stack *stack)
419{
420 stack->size = 0;
421 free(stack->stack);
422}
423
Radek Krejcib56c7502019-02-13 14:19:54 +0100424/**
425 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
426 * (libyang format of the if-feature expression).
427 * @param[in,out] list The 2bits array to modify.
428 * @param[in] op The operand (@ref ifftokens) to store.
429 * @param[in] pos Position (0-based) where to store the given @p op.
430 */
Radek Krejci19a96102018-11-15 13:38:09 +0100431static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200432iff_setop(uint8_t *list, uint8_t op, size_t pos)
Radek Krejci19a96102018-11-15 13:38:09 +0100433{
434 uint8_t *item;
435 uint8_t mask = 3;
436
Radek Krejci19a96102018-11-15 13:38:09 +0100437 assert(op <= 3); /* max 2 bits */
438
439 item = &list[pos / 4];
440 mask = mask << 2 * (pos % 4);
441 *item = (*item) & ~mask;
442 *item = (*item) | (op << 2 * (pos % 4));
443}
444
Radek Krejcib56c7502019-02-13 14:19:54 +0100445#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
446#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100447
Radek Krejci0af46292019-01-11 16:02:31 +0100448/**
449 * @brief Find a feature of the given name and referenced in the given module.
450 *
451 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
452 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
453 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
454 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
455 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
456 * assigned till that time will be still valid.
457 *
458 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
459 * @param[in] name Name of the feature including possible prefix.
460 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
461 * @return Pointer to the feature structure if found, NULL otherwise.
462 */
Radek Krejci19a96102018-11-15 13:38:09 +0100463static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100464lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100465{
466 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200467 LY_ARRAY_COUNT_TYPE u;
Radek Krejci14915cc2020-09-14 17:28:13 +0200468 struct lysc_feature *f;
Radek Krejci19a96102018-11-15 13:38:09 +0100469
Radek Krejci120d8542020-08-12 09:29:16 +0200470 assert(mod);
471
Radek Krejci19a96102018-11-15 13:38:09 +0100472 for (i = 0; i < len; ++i) {
473 if (name[i] == ':') {
474 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100475 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100476 LY_CHECK_RET(!mod, NULL);
477
478 name = &name[i + 1];
479 len = len - i - 1;
480 }
481 }
482
483 /* we have the correct module, get the feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200484 LY_ARRAY_FOR(mod->features, u) {
485 f = &mod->features[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200486 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100487 return f;
488 }
489 }
490
491 return NULL;
492}
493
Michal Vasko8d544252020-03-02 10:19:52 +0100494/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100495 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
496 */
497static LY_ERR
498lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
499{
500 LY_ERR ret = LY_SUCCESS;
501
502 if (!ext_p->compiled) {
503 lysc_update_path(ctx, NULL, "{extension}");
504 lysc_update_path(ctx, NULL, ext_p->name);
505
506 /* compile the extension definition */
507 ext_p->compiled = calloc(1, sizeof **ext);
508 ext_p->compiled->refcount = 1;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200509 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
510 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100511 ext_p->compiled->module = (struct lys_module *)ext_mod;
512 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
513
514 lysc_update_path(ctx, NULL, NULL);
515 lysc_update_path(ctx, NULL, NULL);
516
517 /* find extension definition plugin */
518 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
519 }
520
521 *ext = lysc_ext_dup(ext_p->compiled);
522
523done:
524 return ret;
525}
526
527/**
Michal Vasko8d544252020-03-02 10:19:52 +0100528 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
529 *
530 * @param[in] ctx Compilation context.
531 * @param[in] ext_p Parsed extension instance.
532 * @param[in,out] ext Prepared compiled extension instance.
533 * @param[in] parent Extension instance parent.
534 * @param[in] parent_type Extension instance parent type.
535 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
536 */
Radek Krejci19a96102018-11-15 13:38:09 +0100537static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100538lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200539 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100540{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200541 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100542 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200543 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200544 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200545 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100546
Radek Krejci011e4aa2020-09-04 15:22:31 +0200547 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
548 LY_CHECK_RET(ret);
549
Radek Krejci19a96102018-11-15 13:38:09 +0100550 ext->insubstmt = ext_p->insubstmt;
551 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200552 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200553 ext->parent = parent;
554 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100555
Michal Vasko22df3f02020-08-24 13:29:22 +0200556 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200557
Radek Krejci19a96102018-11-15 13:38:09 +0100558 /* get module where the extension definition should be placed */
Radek Krejci1e008d22020-08-17 11:37:37 +0200559 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
Radek Krejci7c960162019-09-18 14:16:12 +0200560 if (ext_p->yin) {
561 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
562 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
563 * YANG (import) prefix must be done here. */
564 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200565 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200566 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200567 } else {
568 assert(ctx->mod_def->parsed);
569 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
570 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200571 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200572 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1,
Radek Krejci200f1062020-07-11 22:51:03 +0200573 ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200574 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200575 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200576 break;
577 }
578 }
579 }
580 if (!prefixed_name) {
581 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
582 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200583 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200584 goto cleanup;
585 }
586 } else {
587 prefixed_name = ext_p->name;
588 }
589 lysc_update_path(ctx, NULL, prefixed_name);
590
Michal Vasko8d544252020-03-02 10:19:52 +0100591 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200592 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100593 if (!ext_mod) {
594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
595 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200596 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100597 goto cleanup;
598 } else if (!ext_mod->parsed->extensions) {
599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
600 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
601 prefixed_name, ext_mod->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200602 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100603 goto cleanup;
604 }
Radek Krejci7c960162019-09-18 14:16:12 +0200605 }
606 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200607
Michal Vasko5fe75f12020-03-02 13:52:37 +0100608 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200609 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
610 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100611 /* compile extension definition and assign it */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200612 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100613 break;
614 }
615 }
Radek Krejci7c960162019-09-18 14:16:12 +0200616 if (!ext->def) {
617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
618 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200619 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200620 goto cleanup;
621 }
Radek Krejci0935f412019-08-20 16:15:18 +0200622
Radek Krejcif56e2a42019-09-09 14:15:25 +0200623 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
624 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
625 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200626 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200627 /* Schema was parsed from YIN and an argument is expected, ... */
628 struct lysp_stmt *stmt = NULL;
629
630 if (ext->def->flags & LYS_YINELEM_TRUE) {
631 /* ... argument was the first XML child element */
632 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
633 /* TODO check namespace of the statement */
634 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
635 stmt = ext_p->child;
636 }
637 }
638 } else {
639 /* ... argument was one of the XML attributes which are represented as child stmt
640 * with LYS_YIN_ATTR flag */
641 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
642 if (!strcmp(stmt->stmt, ext->def->argument)) {
643 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200644 break;
645 }
646 }
647 }
648 if (!stmt) {
649 /* missing extension's argument */
650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200651 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200652 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200653 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200654
655 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200656 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200657 stmt->flags |= LYS_YIN_ARGUMENT;
658 }
Radek Krejci7c960162019-09-18 14:16:12 +0200659 if (prefixed_name != ext_p->name) {
660 lydict_remove(ctx->ctx, ext_p->name);
661 ext_p->name = prefixed_name;
662 if (!ext_p->argument && ext->argument) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200663 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200664 }
665 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200666
Radek Krejci0935f412019-08-20 16:15:18 +0200667 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200668 if (ext->argument) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200669 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
Radek Krejciad5963b2019-09-06 16:03:05 +0200670 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200671 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200672 if (ext->argument) {
673 lysc_update_path(ctx, NULL, NULL);
674 }
Radek Krejci0935f412019-08-20 16:15:18 +0200675 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200676 ext_p->compiled = ext;
677
Radek Krejci7c960162019-09-18 14:16:12 +0200678cleanup:
679 if (prefixed_name && prefixed_name != ext_p->name) {
680 lydict_remove(ctx->ctx, prefixed_name);
681 }
682
Radek Krejcif56e2a42019-09-09 14:15:25 +0200683 lysc_update_path(ctx, NULL, NULL);
684 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200685
Radek Krejci7c960162019-09-18 14:16:12 +0200686 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200687}
688
689/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100690 * @brief Compile information from the if-feature statement
691 * @param[in] ctx Compile context.
692 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100693 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
694 * @return LY_ERR value.
695 */
Radek Krejci19a96102018-11-15 13:38:09 +0100696static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200697lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100698{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200699 LY_ERR rc = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100700 const char *c = *value;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200701 int64_t i, j;
702 int8_t op_len, last_not = 0, checkversion = 0;
703 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci19a96102018-11-15 13:38:09 +0100704 uint8_t op;
705 struct iff_stack stack = {0, 0, NULL};
706 struct lysc_feature *f;
707
708 assert(c);
709
710 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200711 for (i = j = 0; c[i]; i++) {
Radek Krejci19a96102018-11-15 13:38:09 +0100712 if (c[i] == '(') {
713 j++;
714 checkversion = 1;
715 continue;
716 } else if (c[i] == ')') {
717 j--;
718 continue;
719 } else if (isspace(c[i])) {
720 checkversion = 1;
721 continue;
722 }
723
Radek Krejci1deb5be2020-08-26 16:43:36 +0200724 if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
725 uint64_t spaces;
726 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++);
727 if (c[i + op_len + spaces] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
729 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
730 return LY_EVALID;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200731 } else if (!isspace(c[i + op_len])) {
Radek Krejci19a96102018-11-15 13:38:09 +0100732 /* feature name starting with the not/and/or */
733 last_not = 0;
734 f_size++;
735 } else if (c[i] == 'n') { /* not operation */
736 if (last_not) {
737 /* double not */
738 expr_size = expr_size - 2;
739 last_not = 0;
740 } else {
741 last_not = 1;
742 }
743 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200744 if (f_exp != f_size) {
745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200746 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, op_len, &c[i]);
Radek Krejci6788abc2019-06-14 13:56:49 +0200747 return LY_EVALID;
748 }
Radek Krejci19a96102018-11-15 13:38:09 +0100749 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200750
Radek Krejci19a96102018-11-15 13:38:09 +0100751 /* not a not operation */
752 last_not = 0;
753 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200754 i += op_len;
Radek Krejci19a96102018-11-15 13:38:09 +0100755 } else {
756 f_size++;
757 last_not = 0;
758 }
759 expr_size++;
760
761 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200762 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100763 i--;
764 break;
765 }
766 i++;
767 }
768 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200769 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100770 /* not matching count of ( and ) */
771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
772 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
773 return LY_EVALID;
774 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200775 if (f_exp != f_size) {
776 /* features do not match the needed arguments for the logical operations */
777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
778 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
779 "the required number of operands for the operations.", *value);
780 return LY_EVALID;
781 }
Radek Krejci19a96102018-11-15 13:38:09 +0100782
783 if (checkversion || expr_size > 1) {
784 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100785 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
787 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
788 return LY_EVALID;
789 }
790 }
791
792 /* allocate the memory */
793 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
794 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
795 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200796 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
Radek Krejci19a96102018-11-15 13:38:09 +0100797
798 stack.size = expr_size;
799 f_size--; expr_size--; /* used as indexes from now */
800
801 for (i--; i >= 0; i--) {
802 if (c[i] == ')') {
803 /* push it on stack */
804 iff_stack_push(&stack, LYS_IFF_RP);
805 continue;
806 } else if (c[i] == '(') {
807 /* pop from the stack into result all operators until ) */
Michal Vaskod989ba02020-08-24 10:59:24 +0200808 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
Radek Krejci19a96102018-11-15 13:38:09 +0100809 iff_setop(iff->expr, op, expr_size--);
810 }
811 continue;
812 } else if (isspace(c[i])) {
813 continue;
814 }
815
816 /* end of operator or operand -> find beginning and get what is it */
817 j = i + 1;
818 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
819 i--;
820 }
821 i++; /* go back by one step */
822
823 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
824 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
825 /* double not */
826 iff_stack_pop(&stack);
827 } else {
828 /* not has the highest priority, so do not pop from the stack
829 * as in case of AND and OR */
830 iff_stack_push(&stack, LYS_IFF_NOT);
831 }
832 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
833 /* as for OR - pop from the stack all operators with the same or higher
834 * priority and store them to the result, then push the AND to the stack */
835 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
836 op = iff_stack_pop(&stack);
837 iff_setop(iff->expr, op, expr_size--);
838 }
839 iff_stack_push(&stack, LYS_IFF_AND);
840 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
841 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
842 op = iff_stack_pop(&stack);
843 iff_setop(iff->expr, op, expr_size--);
844 }
845 iff_stack_push(&stack, LYS_IFF_OR);
846 } else {
847 /* feature name, length is j - i */
848
849 /* add it to the expression */
850 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
851
852 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100853 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
854 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
855 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
856 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100857 iff->features[f_size] = f;
858 LY_ARRAY_INCREMENT(iff->features);
859 f_size--;
860 }
861 }
862 while (stack.index) {
863 op = iff_stack_pop(&stack);
864 iff_setop(iff->expr, op, expr_size--);
865 }
866
867 if (++expr_size || ++f_size) {
868 /* not all expected operators and operands found */
869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
870 "Invalid value \"%s\" of if-feature - processing error.", *value);
871 rc = LY_EINT;
872 } else {
873 rc = LY_SUCCESS;
874 }
875
876error:
877 /* cleanup */
878 iff_stack_clean(&stack);
879
880 return rc;
881}
882
Radek Krejcib56c7502019-02-13 14:19:54 +0100883/**
Michal Vasko175012e2019-11-06 15:49:14 +0100884 * @brief Get the XPath context node for the given schema node.
885 * @param[in] start The schema node where the XPath expression appears.
886 * @return The context node to evaluate XPath expression in given schema node.
887 * @return NULL in case the context node is the root node.
888 */
889static struct lysc_node *
890lysc_xpath_context(struct lysc_node *start)
891{
Michal Vasko1bf09392020-03-27 12:38:10 +0100892 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Radek Krejci1e008d22020-08-17 11:37:37 +0200893 start = start->parent) {}
Michal Vasko175012e2019-11-06 15:49:14 +0100894 return start;
895}
896
897/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100898 * @brief Compile information from the when statement
899 * @param[in] ctx Compile context.
900 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100901 * @param[in] flags Flags of the node with the "when" defiition.
902 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100903 * @param[out] when Pointer where to store pointer to the created compiled when structure.
904 * @return LY_ERR value.
905 */
Radek Krejci19a96102018-11-15 13:38:09 +0100906static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100907lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100908{
Radek Krejci19a96102018-11-15 13:38:09 +0100909 LY_ERR ret = LY_SUCCESS;
910
Radek Krejci00b874b2019-02-12 10:54:50 +0100911 *when = calloc(1, sizeof **when);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200912 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci00b874b2019-02-12 10:54:50 +0100913 (*when)->refcount = 1;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200914 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejcia0f704a2019-09-09 16:12:23 +0200915 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100916 (*when)->context = lysc_xpath_context(node);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200917 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
918 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200919 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100920 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100921
922done:
923 return ret;
924}
925
Radek Krejcib56c7502019-02-13 14:19:54 +0100926/**
927 * @brief Compile information from the must statement
928 * @param[in] ctx Compile context.
929 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100930 * @param[in,out] must Prepared (empty) compiled must structure to fill.
931 * @return LY_ERR value.
932 */
Radek Krejci19a96102018-11-15 13:38:09 +0100933static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200934lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100935{
Radek Krejci19a96102018-11-15 13:38:09 +0100936 LY_ERR ret = LY_SUCCESS;
937
Radek Krejcif03a9e22020-09-18 20:09:31 +0200938 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1, &must->cond));
Radek Krejci462cf252019-07-24 09:49:08 +0200939 must->module = ctx->mod_def;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200940 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
941 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
942 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
943 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200944 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100945
946done:
947 return ret;
948}
949
Radek Krejcib56c7502019-02-13 14:19:54 +0100950/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200951 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100952 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200953 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100954 * @return LY_ERR value.
955 */
Radek Krejci19a96102018-11-15 13:38:09 +0100956static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200957lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100958{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200959 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100960 LY_ERR ret = LY_SUCCESS;
961
Radek Krejci7f2a5362018-11-28 13:05:37 +0100962 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
963 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100964 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200965 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100966 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200967 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200968 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200969 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200970 LOGINT(ctx->ctx);
971 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200972 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
Michal Vasko3a41dff2020-07-15 14:30:28 +0200973 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200974 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200975 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200976 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200977 mod = NULL;
978 }
Radek Krejci19a96102018-11-15 13:38:09 +0100979 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200980 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100981 }
982 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200983 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100984 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200985 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100986 return LY_ENOTFOUND;
987 }
988 }
Radek Krejci19a96102018-11-15 13:38:09 +0100989 }
990
Radek Krejci19a96102018-11-15 13:38:09 +0100991 return ret;
992}
993
Michal Vasko33ff9422020-07-03 09:50:39 +0200994LY_ERR
995lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +0200996 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100997{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200998 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200999 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01001000 LY_ERR ret = LY_SUCCESS;
1001
Michal Vasko33ff9422020-07-03 09:50:39 +02001002 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +02001003
Michal Vasko33ff9422020-07-03 09:50:39 +02001004 if (!ctx_sc) {
1005 context.ctx = ctx;
1006 context.mod = module;
Radek Krejci120d8542020-08-12 09:29:16 +02001007 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +02001008 context.path_len = 1;
1009 context.path[0] = '/';
1010 ctx_sc = &context;
1011 }
Radek Krejci19a96102018-11-15 13:38:09 +01001012
Michal Vasko33ff9422020-07-03 09:50:39 +02001013 if (!identities_p) {
1014 return LY_SUCCESS;
1015 }
1016 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001017 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02001018 }
1019
1020 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001021 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001022 LY_ARRAY_FOR(identities_p, u) {
1023 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
1024
1025 LY_ARRAY_INCREMENT(*identities);
1026 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001027 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
1028 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
1029 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
Michal Vasko33ff9422020-07-03 09:50:39 +02001030 (*identities)[offset + u].module = ctx_sc->mod;
1031 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
1032 lys_compile_iffeature, ret, done);
1033 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
1034 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
1035 LYEXT_PAR_IDENT, ret, done);
1036 (*identities)[offset + u].flags = identities_p[u].flags;
1037
1038 lysc_update_path(ctx_sc, NULL, NULL);
1039 }
1040 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001041done:
1042 return ret;
1043}
1044
Radek Krejcib56c7502019-02-13 14:19:54 +01001045/**
1046 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1047 *
1048 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1049 *
1050 * @param[in] ctx Compile context for logging.
1051 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1052 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1053 * @return LY_SUCCESS if everything is ok.
1054 * @return LY_EVALID if the identity is derived from itself.
1055 */
Radek Krejci38222632019-02-12 16:55:05 +01001056static LY_ERR
1057lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1058{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001059 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001060 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001061 struct ly_set recursion = {0};
1062 struct lysc_ident *drv;
1063
1064 if (!derived) {
1065 return LY_SUCCESS;
1066 }
1067
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001068 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001069 if (ident == derived[u]) {
1070 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1071 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001072 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001073 goto cleanup;
1074 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001075 ret = ly_set_add(&recursion, derived[u], 0, NULL);
1076 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001077 }
1078
1079 for (v = 0; v < recursion.count; ++v) {
1080 drv = recursion.objs[v];
1081 if (!drv->derived) {
1082 continue;
1083 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001084 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001085 if (ident == drv->derived[u]) {
1086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1087 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001088 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001089 goto cleanup;
1090 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001091 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
1092 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001093 }
1094 }
Radek Krejci38222632019-02-12 16:55:05 +01001095
1096cleanup:
1097 ly_set_erase(&recursion, NULL);
1098 return ret;
1099}
1100
Radek Krejcia3045382018-11-22 14:30:31 +01001101/**
1102 * @brief Find and process the referenced base identities from another identity or identityref
1103 *
Radek Krejciaca74032019-06-04 08:53:06 +02001104 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001105 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1106 * to distinguish these two use cases.
1107 *
1108 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1109 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001110 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001111 * @param[in] bases Array of bases of identityref to fill in.
1112 * @return LY_ERR value.
1113 */
Radek Krejci19a96102018-11-15 13:38:09 +01001114static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001115lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001116 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001117{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001118 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001119 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001120 struct lys_module *mod;
Radek Krejci80d281e2020-09-14 17:42:54 +02001121 struct lysc_ident **idref;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001122
1123 assert(ident || bases);
1124
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001125 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1127 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1128 return LY_EVALID;
1129 }
1130
Michal Vasko33ff9422020-07-03 09:50:39 +02001131 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001132 s = strchr(bases_p[u], ':');
1133 if (s) {
1134 /* prefixed identity */
1135 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001136 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001137 } else {
1138 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001139 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001140 }
1141 if (!mod) {
1142 if (ident) {
1143 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1144 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1145 } else {
1146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1147 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1148 }
1149 return LY_EVALID;
1150 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001151
Radek Krejci555cb5b2018-11-16 14:54:33 +01001152 idref = NULL;
Radek Krejci80d281e2020-09-14 17:42:54 +02001153 LY_ARRAY_FOR(mod->identities, v) {
1154 if (!strcmp(name, mod->identities[v].name)) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001155 if (ident) {
Radek Krejci80d281e2020-09-14 17:42:54 +02001156 if (ident == &mod->identities[v]) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1158 "Identity \"%s\" is derived from itself.", ident->name);
1159 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001160 }
Radek Krejci80d281e2020-09-14 17:42:54 +02001161 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
Michal Vasko33ff9422020-07-03 09:50:39 +02001162 /* we have match! store the backlink */
Radek Krejci80d281e2020-09-14 17:42:54 +02001163 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001164 *idref = ident;
1165 } else {
1166 /* we have match! store the found identity */
1167 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejci80d281e2020-09-14 17:42:54 +02001168 *idref = &mod->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001169 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001170 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001171 }
1172 }
1173 if (!idref || !(*idref)) {
1174 if (ident) {
1175 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1176 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1177 } else {
1178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1179 "Unable to find base (%s) of identityref.", bases_p[u]);
1180 }
1181 return LY_EVALID;
1182 }
1183 }
1184 return LY_SUCCESS;
1185}
1186
Radek Krejcia3045382018-11-22 14:30:31 +01001187/**
1188 * @brief For the given array of identities, set the backlinks from all their base identities.
1189 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1190 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1191 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1192 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1193 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001194static LY_ERR
1195lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1196{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001197 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001198
Michal Vasko33ff9422020-07-03 09:50:39 +02001199 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001200 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001201 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001202 continue;
1203 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001204 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001205 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module, idents_p[u].bases, &idents[u], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001206 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001207 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001208 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001209 return LY_SUCCESS;
1210}
1211
Radek Krejci0af46292019-01-11 16:02:31 +01001212LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001213lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001214 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001215{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001216 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001217 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001218 struct lysc_ctx context = {0};
1219
Radek Krejci327de162019-06-14 12:52:07 +02001220 assert(ctx_sc || ctx);
1221
1222 if (!ctx_sc) {
1223 context.ctx = ctx;
1224 context.mod = module;
1225 context.path_len = 1;
1226 context.path[0] = '/';
1227 ctx_sc = &context;
1228 }
Radek Krejci0af46292019-01-11 16:02:31 +01001229
1230 if (!features_p) {
1231 return LY_SUCCESS;
1232 }
1233 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001234 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001235 }
1236
Radek Krejci327de162019-06-14 12:52:07 +02001237 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001238 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001239 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001240 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1241
Radek Krejci0af46292019-01-11 16:02:31 +01001242 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001243 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001244 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
1245 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
1246 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001247 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001248 (*features)[offset + u].module = ctx_sc->mod;
1249
1250 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001251 }
Radek Krejci327de162019-06-14 12:52:07 +02001252 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001253
Radek Krejci011e4aa2020-09-04 15:22:31 +02001254done:
1255 return ret;
Radek Krejci0af46292019-01-11 16:02:31 +01001256}
1257
Radek Krejcia3045382018-11-22 14:30:31 +01001258/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001259 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001260 *
1261 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1262 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001263 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001264 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1265 * being currently processed).
1266 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +01001267 * @return LY_SUCCESS if everything is ok.
1268 * @return LY_EVALID if the feature references indirectly itself.
1269 */
1270static LY_ERR
1271lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1272{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001273 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001274 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001275 struct ly_set recursion = {0};
1276 struct lysc_feature *drv;
1277
1278 if (!depfeatures) {
1279 return LY_SUCCESS;
1280 }
1281
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001282 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001283 if (feature == depfeatures[u]) {
1284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1285 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001286 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001287 goto cleanup;
1288 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001289 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
1290 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001291 }
1292
1293 for (v = 0; v < recursion.count; ++v) {
1294 drv = recursion.objs[v];
1295 if (!drv->depfeatures) {
1296 continue;
1297 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001298 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001299 if (feature == drv->depfeatures[u]) {
1300 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1301 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001302 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001303 goto cleanup;
1304 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001305 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
1306 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001307 }
1308 }
Radek Krejci09a1fc52019-02-13 10:55:17 +01001309
1310cleanup:
1311 ly_set_erase(&recursion, NULL);
1312 return ret;
1313}
1314
1315/**
Radek Krejci0af46292019-01-11 16:02:31 +01001316 * @brief Create pre-compiled features array.
1317 *
1318 * See lys_feature_precompile() for more details.
1319 *
Radek Krejcia3045382018-11-22 14:30:31 +01001320 * @param[in] ctx Compile context.
1321 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001322 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001323 * @return LY_ERR value.
1324 */
Radek Krejci19a96102018-11-15 13:38:09 +01001325static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001326lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001327{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001328 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001329 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001330 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001331
Radek Krejci0af46292019-01-11 16:02:31 +01001332 /* find the preprecompiled feature */
1333 LY_ARRAY_FOR(features, x) {
1334 if (strcmp(features[x].name, feature_p->name)) {
1335 continue;
1336 }
1337 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001338 lysc_update_path(ctx, NULL, "{feature}");
1339 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001340
Radek Krejci0af46292019-01-11 16:02:31 +01001341 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001342 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001343 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001344 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001345 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001346 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001347 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001348 /* check for circular dependency - direct reference first,... */
1349 if (feature == feature->iffeatures[u].features[v]) {
1350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1351 "Feature \"%s\" is referenced from itself.", feature->name);
1352 return LY_EVALID;
1353 }
1354 /* ... and indirect circular reference */
1355 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1356
Radek Krejci0af46292019-01-11 16:02:31 +01001357 /* add itself into the dependants list */
1358 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1359 *df = feature;
1360 }
Radek Krejci19a96102018-11-15 13:38:09 +01001361 }
Radek Krejci19a96102018-11-15 13:38:09 +01001362 }
1363 }
Radek Krejci327de162019-06-14 12:52:07 +02001364 lysc_update_path(ctx, NULL, NULL);
1365 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001366 done:
1367 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001368 }
Radek Krejci0af46292019-01-11 16:02:31 +01001369
1370 LOGINT(ctx->ctx);
1371 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001372}
1373
Radek Krejcib56c7502019-02-13 14:19:54 +01001374/**
1375 * @brief Revert compiled list of features back to the precompiled state.
1376 *
1377 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
Michal Vasko33ff9422020-07-03 09:50:39 +02001378 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001379 *
1380 * @param[in] ctx Compilation context.
1381 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
Michal Vasko33ff9422020-07-03 09:50:39 +02001382 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001383 */
Radek Krejci95710c92019-02-11 15:49:55 +01001384static void
1385lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1386{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001387 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001388
Michal Vasko33ff9422020-07-03 09:50:39 +02001389 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001390 * which may points into the data being freed here */
Radek Krejci14915cc2020-09-14 17:28:13 +02001391 LY_ARRAY_FOR(mod->features, u) {
1392 LY_ARRAY_FOR(mod->features[u].iffeatures, v) {
1393 lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001394 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001395 LY_ARRAY_FREE(mod->features[u].iffeatures);
1396 mod->features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001397
Radek Krejci14915cc2020-09-14 17:28:13 +02001398 LY_ARRAY_FOR(mod->features[u].exts, v) {
1399 lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001400 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001401 LY_ARRAY_FREE(mod->features[u].exts);
1402 mod->features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001403 }
1404}
1405
Radek Krejcia3045382018-11-22 14:30:31 +01001406/**
1407 * @brief Validate and normalize numeric value from a range definition.
1408 * @param[in] ctx Compile context.
1409 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1410 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1411 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1412 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1413 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1414 * @param[in] value String value of the range boundary.
1415 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
1416 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1417 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1418 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001419LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001420range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001421{
Radek Krejci6cba4292018-11-15 17:33:29 +01001422 size_t fraction = 0, size;
1423
Radek Krejci19a96102018-11-15 13:38:09 +01001424 *len = 0;
1425
1426 assert(value);
1427 /* parse value */
1428 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1429 return LY_EVALID;
1430 }
1431
1432 if ((value[*len] == '-') || (value[*len] == '+')) {
1433 ++(*len);
1434 }
1435
1436 while (isdigit(value[*len])) {
1437 ++(*len);
1438 }
1439
1440 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001441 if (basetype == LY_TYPE_DEC64) {
1442 goto decimal;
1443 } else {
1444 *valcopy = strndup(value, *len);
1445 return LY_SUCCESS;
1446 }
Radek Krejci19a96102018-11-15 13:38:09 +01001447 }
1448 fraction = *len;
1449
1450 ++(*len);
1451 while (isdigit(value[*len])) {
1452 ++(*len);
1453 }
1454
Radek Krejci6cba4292018-11-15 17:33:29 +01001455 if (basetype == LY_TYPE_DEC64) {
1456decimal:
1457 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001458 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1460 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1461 *len, value, frdigits);
1462 return LY_EINVAL;
1463 }
1464 if (fraction) {
1465 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1466 } else {
1467 size = (*len) + frdigits + 1;
1468 }
1469 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001470 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1471
Radek Krejci6cba4292018-11-15 17:33:29 +01001472 (*valcopy)[size - 1] = '\0';
1473 if (fraction) {
1474 memcpy(&(*valcopy)[0], &value[0], fraction);
1475 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1476 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1477 } else {
1478 memcpy(&(*valcopy)[0], &value[0], *len);
1479 memset(&(*valcopy)[*len], '0', frdigits);
1480 }
Radek Krejci19a96102018-11-15 13:38:09 +01001481 }
1482 return LY_SUCCESS;
1483}
1484
Radek Krejcia3045382018-11-22 14:30:31 +01001485/**
1486 * @brief Check that values in range are in ascendant order.
1487 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001488 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1489 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001490 * @param[in] value Current value to check.
1491 * @param[in] prev_value The last seen value.
1492 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1493 */
Radek Krejci19a96102018-11-15 13:38:09 +01001494static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001495range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001496{
1497 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001498 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001499 return LY_EEXIST;
1500 }
1501 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001502 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001503 return LY_EEXIST;
1504 }
1505 }
1506 return LY_SUCCESS;
1507}
1508
Radek Krejcia3045382018-11-22 14:30:31 +01001509/**
1510 * @brief Set min/max value of the range part.
1511 * @param[in] ctx Compile context.
1512 * @param[in] part Range part structure to fill.
1513 * @param[in] max Flag to distinguish if storing min or max value.
1514 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1515 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1516 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1517 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1518 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001519 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +01001520 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1521 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1522 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1523 * frdigits value), LY_EMEM.
1524 */
Radek Krejci19a96102018-11-15 13:38:09 +01001525static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001526range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
1527 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001528{
1529 LY_ERR ret = LY_SUCCESS;
1530 char *valcopy = NULL;
1531 size_t len;
1532
1533 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001534 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001535 LY_CHECK_GOTO(ret, finalize);
1536 }
1537 if (!valcopy && base_range) {
1538 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001539 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001540 } else {
1541 part->min_64 = base_range->parts[0].min_64;
1542 }
1543 if (!first) {
1544 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1545 }
1546 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001547 }
1548
1549 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001550 case LY_TYPE_INT8: /* range */
1551 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001552 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001553 } else if (max) {
1554 part->max_64 = INT64_C(127);
1555 } else {
1556 part->min_64 = INT64_C(-128);
1557 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001558 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001559 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001560 }
1561 break;
1562 case LY_TYPE_INT16: /* range */
1563 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001564 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001565 } else if (max) {
1566 part->max_64 = INT64_C(32767);
1567 } else {
1568 part->min_64 = INT64_C(-32768);
1569 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001570 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001571 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001572 }
1573 break;
1574 case LY_TYPE_INT32: /* range */
1575 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001576 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001577 } else if (max) {
1578 part->max_64 = INT64_C(2147483647);
1579 } else {
1580 part->min_64 = INT64_C(-2147483648);
1581 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001582 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001583 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001584 }
1585 break;
1586 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001587 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001588 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001589 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001590 max ? &part->max_64 : &part->min_64);
1591 } else if (max) {
1592 part->max_64 = INT64_C(9223372036854775807);
1593 } else {
1594 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1595 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001596 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001597 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001598 }
1599 break;
1600 case LY_TYPE_UINT8: /* range */
1601 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001602 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001603 } else if (max) {
1604 part->max_u64 = UINT64_C(255);
1605 } else {
1606 part->min_u64 = UINT64_C(0);
1607 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001608 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001609 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001610 }
1611 break;
1612 case LY_TYPE_UINT16: /* range */
1613 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001614 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001615 } else if (max) {
1616 part->max_u64 = UINT64_C(65535);
1617 } else {
1618 part->min_u64 = UINT64_C(0);
1619 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001620 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001621 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001622 }
1623 break;
1624 case LY_TYPE_UINT32: /* range */
1625 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001626 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001627 } else if (max) {
1628 part->max_u64 = UINT64_C(4294967295);
1629 } else {
1630 part->min_u64 = UINT64_C(0);
1631 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001632 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001633 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001634 }
1635 break;
1636 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001637 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001638 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001639 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001640 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001641 } else if (max) {
1642 part->max_u64 = UINT64_C(18446744073709551615);
1643 } else {
1644 part->min_u64 = UINT64_C(0);
1645 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001646 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001647 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001648 }
1649 break;
1650 default:
1651 LOGINT(ctx->ctx);
1652 ret = LY_EINT;
1653 }
1654
Radek Krejci5969f272018-11-23 10:03:58 +01001655finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001656 if (ret == LY_EDENIED) {
1657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1658 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1659 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1660 } else if (ret == LY_EVALID) {
1661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1662 "Invalid %s restriction - invalid value \"%s\".",
1663 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1664 } else if (ret == LY_EEXIST) {
1665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1666 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001667 length_restr ? "length" : "range",
Radek Krejci0f969882020-08-21 16:56:47 +02001668 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001669 } else if (!ret && value) {
1670 *value = *value + len;
1671 }
1672 free(valcopy);
1673 return ret;
1674}
1675
Radek Krejcia3045382018-11-22 14:30:31 +01001676/**
1677 * @brief Compile the parsed range restriction.
1678 * @param[in] ctx Compile context.
1679 * @param[in] range_p Parsed range structure to compile.
1680 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1681 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1682 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1683 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1684 * range restriction must be more restrictive than the base_range.
1685 * @param[in,out] range Pointer to the created current range structure.
1686 * @return LY_ERR value.
1687 */
Radek Krejci19a96102018-11-15 13:38:09 +01001688static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001689lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
Radek Krejci0f969882020-08-21 16:56:47 +02001690 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001691{
1692 LY_ERR ret = LY_EVALID;
1693 const char *expr;
1694 struct lysc_range_part *parts = NULL, *part;
Radek Krejci857189e2020-09-01 13:26:36 +02001695 ly_bool range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001696 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001697
1698 assert(range);
1699 assert(range_p);
1700
1701 expr = range_p->arg;
Michal Vaskod989ba02020-08-24 10:59:24 +02001702 while (1) {
Radek Krejci19a96102018-11-15 13:38:09 +01001703 if (isspace(*expr)) {
1704 ++expr;
1705 } else if (*expr == '\0') {
1706 if (range_expected) {
1707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1708 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1709 length_restr ? "length" : "range", range_p->arg);
1710 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001711 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1713 "Invalid %s restriction - unexpected end of the expression (%s).",
1714 length_restr ? "length" : "range", range_p->arg);
1715 goto cleanup;
1716 }
1717 parts_done++;
1718 break;
1719 } else if (!strncmp(expr, "min", 3)) {
1720 if (parts) {
1721 /* min cannot be used elsewhere than in the first part */
1722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1723 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1724 expr - range_p->arg, range_p->arg);
1725 goto cleanup;
1726 }
1727 expr += 3;
1728
1729 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001730 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001731 part->max_64 = part->min_64;
1732 } else if (*expr == '|') {
1733 if (!parts || range_expected) {
1734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1735 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1736 goto cleanup;
1737 }
1738 expr++;
1739 parts_done++;
1740 /* process next part of the expression */
1741 } else if (!strncmp(expr, "..", 2)) {
1742 expr += 2;
1743 while (isspace(*expr)) {
1744 expr++;
1745 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001746 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1748 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1749 goto cleanup;
1750 }
1751 /* continue expecting the upper boundary */
1752 range_expected = 1;
1753 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1754 /* number */
1755 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001756 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001757 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001758 range_expected = 0;
1759 } else {
1760 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001761 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001762 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001763 part->max_64 = part->min_64;
1764 }
1765
1766 /* continue with possible another expression part */
1767 } else if (!strncmp(expr, "max", 3)) {
1768 expr += 3;
1769 while (isspace(*expr)) {
1770 expr++;
1771 }
1772 if (*expr != '\0') {
1773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1774 length_restr ? "length" : "range", expr);
1775 goto cleanup;
1776 }
1777 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001778 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001779 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001780 range_expected = 0;
1781 } else {
1782 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001783 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001784 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001785 part->min_64 = part->max_64;
1786 }
1787 } else {
1788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1789 length_restr ? "length" : "range", expr);
1790 goto cleanup;
1791 }
1792 }
1793
1794 /* check with the previous range/length restriction */
1795 if (base_range) {
1796 switch (basetype) {
1797 case LY_TYPE_BINARY:
1798 case LY_TYPE_UINT8:
1799 case LY_TYPE_UINT16:
1800 case LY_TYPE_UINT32:
1801 case LY_TYPE_UINT64:
1802 case LY_TYPE_STRING:
1803 uns = 1;
1804 break;
1805 case LY_TYPE_DEC64:
1806 case LY_TYPE_INT8:
1807 case LY_TYPE_INT16:
1808 case LY_TYPE_INT32:
1809 case LY_TYPE_INT64:
1810 uns = 0;
1811 break;
1812 default:
1813 LOGINT(ctx->ctx);
1814 ret = LY_EINT;
1815 goto cleanup;
1816 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001817 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001818 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1819 goto baseerror;
1820 }
1821 /* current lower bound is not lower than the base */
1822 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1823 /* base has single value */
1824 if (base_range->parts[v].min_64 == parts[u].min_64) {
1825 /* both lower bounds are the same */
1826 if (parts[u].min_64 != parts[u].max_64) {
1827 /* current continues with a range */
1828 goto baseerror;
1829 } else {
1830 /* equal single values, move both forward */
1831 ++v;
1832 continue;
1833 }
1834 } else {
1835 /* base is single value lower than current range, so the
1836 * value from base range is removed in the current,
1837 * move only base and repeat checking */
1838 ++v;
1839 --u;
1840 continue;
1841 }
1842 } else {
1843 /* base is the range */
1844 if (parts[u].min_64 == parts[u].max_64) {
1845 /* current is a single value */
1846 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1847 /* current is behind the base range, so base range is omitted,
1848 * move the base and keep the current for further check */
1849 ++v;
1850 --u;
1851 } /* else it is within the base range, so move the current, but keep the base */
1852 continue;
1853 } else {
1854 /* both are ranges - check the higher bound, the lower was already checked */
1855 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1856 /* higher bound is higher than the current higher bound */
1857 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1858 /* but the current lower bound is also higher, so the base range is omitted,
1859 * continue with the same current, but move the base */
1860 --u;
1861 ++v;
1862 continue;
1863 }
1864 /* current range starts within the base range but end behind it */
1865 goto baseerror;
1866 } else {
1867 /* current range is smaller than the base,
1868 * move current, but stay with the base */
1869 continue;
1870 }
1871 }
1872 }
1873 }
1874 if (u != parts_done) {
1875baseerror:
1876 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1877 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1878 length_restr ? "length" : "range", range_p->arg);
1879 goto cleanup;
1880 }
1881 }
1882
1883 if (!(*range)) {
1884 *range = calloc(1, sizeof **range);
1885 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1886 }
1887
Radek Krejcic8b31002019-01-08 10:24:45 +01001888 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001889 if (range_p->eapptag) {
1890 lydict_remove(ctx->ctx, (*range)->eapptag);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001891 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001892 }
1893 if (range_p->emsg) {
1894 lydict_remove(ctx->ctx, (*range)->emsg);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001895 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001896 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001897 if (range_p->dsc) {
1898 lydict_remove(ctx->ctx, (*range)->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001899 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001900 }
1901 if (range_p->ref) {
1902 lydict_remove(ctx->ctx, (*range)->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001903 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001904 }
Radek Krejci19a96102018-11-15 13:38:09 +01001905 /* extensions are taken only from the last range by the caller */
1906
1907 (*range)->parts = parts;
1908 parts = NULL;
1909 ret = LY_SUCCESS;
1910cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001911 LY_ARRAY_FREE(parts);
1912
1913 return ret;
1914}
1915
1916/**
1917 * @brief Checks pattern syntax.
1918 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001919 * @param[in] ctx Context.
1920 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001921 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001922 * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
Radek Krejcia3045382018-11-22 14:30:31 +01001923 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001924 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925LY_ERR
1926lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001927{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001928 size_t idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001929 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001930 int err_code;
1931 const char *orig_ptr;
1932 PCRE2_SIZE err_offset;
1933 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001934#define URANGE_LEN 19
1935 char *ublock2urange[][2] = {
1936 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1937 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1938 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1939 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1940 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1941 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1942 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1943 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1944 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1945 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1946 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1947 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1948 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1949 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1950 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1951 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1952 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1953 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1954 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1955 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1956 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1957 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1958 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1959 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1960 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1961 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1962 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1963 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1964 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1965 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1966 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1967 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1968 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1969 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1970 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1971 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1972 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1973 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1974 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1975 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1976 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1977 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1978 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1979 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1980 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1981 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1982 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1983 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1984 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1985 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1986 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1987 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1988 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1989 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1990 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1991 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1992 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1993 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1994 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1995 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1996 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1997 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1998 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1999 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
2000 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
2001 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
2002 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
2003 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
2004 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
2005 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
2006 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
2007 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
2008 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
2009 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
2010 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
2011 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
2012 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
2013 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
2014 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
2015 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
2016 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
2017 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
2018 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
2019 {NULL, NULL}
2020 };
2021
2022 /* adjust the expression to a Perl equivalent
2023 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
2024
Michal Vasko40a00082020-05-27 15:20:01 +02002025 /* allocate space for the transformed pattern */
2026 size = strlen(pattern) + 1;
2027 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002029 perl_regex[0] = '\0';
2030
Michal Vasko40a00082020-05-27 15:20:01 +02002031 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
2032 brack = 0;
2033 idx = 0;
2034 orig_ptr = pattern;
2035 while (orig_ptr[0]) {
2036 switch (orig_ptr[0]) {
2037 case '$':
2038 case '^':
2039 if (!brack) {
2040 /* make space for the extra character */
2041 ++size;
2042 perl_regex = ly_realloc(perl_regex, size);
2043 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002044
Michal Vasko40a00082020-05-27 15:20:01 +02002045 /* print escape slash */
2046 perl_regex[idx] = '\\';
2047 ++idx;
2048 }
2049 break;
2050 case '[':
2051 /* must not be escaped */
2052 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2053 ++brack;
2054 }
2055 break;
2056 case ']':
2057 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2058 /* pattern was checked and compiled already */
2059 assert(brack);
2060 --brack;
2061 }
2062 break;
2063 default:
2064 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002065 }
Michal Vasko40a00082020-05-27 15:20:01 +02002066
2067 /* copy char */
2068 perl_regex[idx] = orig_ptr[0];
2069
2070 ++idx;
2071 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002072 }
Michal Vasko40a00082020-05-27 15:20:01 +02002073 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002074
2075 /* substitute Unicode Character Blocks with exact Character Ranges */
2076 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2077 start = ptr - perl_regex;
2078
2079 ptr = strchr(ptr, '}');
2080 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002082 pattern, perl_regex + start + 2, "unterminated character property");
2083 free(perl_regex);
2084 return LY_EVALID;
2085 }
2086 end = (ptr - perl_regex) + 1;
2087
2088 /* need more space */
2089 if (end - start < URANGE_LEN) {
2090 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002092 }
2093
2094 /* find our range */
2095 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2096 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2097 break;
2098 }
2099 }
2100 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002102 pattern, perl_regex + start + 5, "unknown block name");
2103 free(perl_regex);
2104 return LY_EVALID;
2105 }
2106
2107 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
Michal Vasko40a00082020-05-27 15:20:01 +02002108 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002109 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002110 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002111 }
2112 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002113 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002114 }
2115 }
Michal Vasko40a00082020-05-27 15:20:01 +02002116 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002117 /* skip brackets */
2118 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2119 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2120 } else {
2121 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2122 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2123 }
2124 }
2125
2126 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002127 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2128 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002129 &err_code, &err_offset, NULL);
2130 if (!code_local) {
2131 PCRE2_UCHAR err_msg[256] = {0};
2132 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002134 free(perl_regex);
2135 return LY_EVALID;
2136 }
2137 free(perl_regex);
2138
Radek Krejci54579462019-04-30 12:47:06 +02002139 if (code) {
2140 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002141 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002142 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002143 }
2144
2145 return LY_SUCCESS;
2146
2147#undef URANGE_LEN
2148}
2149
Radek Krejcia3045382018-11-22 14:30:31 +01002150/**
2151 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2152 * @param[in] ctx Compile context.
2153 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002154 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2155 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2156 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2157 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2158 */
Radek Krejci19a96102018-11-15 13:38:09 +01002159static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002160lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci0f969882020-08-21 16:56:47 +02002161 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
Radek Krejci19a96102018-11-15 13:38:09 +01002162{
2163 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002164 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002165 LY_ERR ret = LY_SUCCESS;
2166
2167 /* first, copy the patterns from the base type */
2168 if (base_patterns) {
2169 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2170 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2171 }
2172
2173 LY_ARRAY_FOR(patterns_p, u) {
2174 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2175 *pattern = calloc(1, sizeof **pattern);
2176 ++(*pattern)->refcount;
2177
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002179 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002180
2181 if (patterns_p[u].arg[0] == 0x15) {
2182 (*pattern)->inverted = 1;
2183 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002184 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done);
2185 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
2186 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
2187 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
2188 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002189 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002190 }
2191done:
2192 return ret;
2193}
2194
Radek Krejcia3045382018-11-22 14:30:31 +01002195/**
2196 * @brief map of the possible restrictions combination for the specific built-in type.
2197 */
Radek Krejci19a96102018-11-15 13:38:09 +01002198static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2199 0 /* LY_TYPE_UNKNOWN */,
2200 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002201 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2202 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2203 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2204 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2205 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002206 LYS_SET_BIT /* LY_TYPE_BITS */,
2207 0 /* LY_TYPE_BOOL */,
2208 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2209 0 /* LY_TYPE_EMPTY */,
2210 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2211 LYS_SET_BASE /* LY_TYPE_IDENT */,
2212 LYS_SET_REQINST /* LY_TYPE_INST */,
2213 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002214 LYS_SET_TYPE /* LY_TYPE_UNION */,
2215 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002216 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002217 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002218 LYS_SET_RANGE /* LY_TYPE_INT64 */
2219};
2220
2221/**
2222 * @brief stringification of the YANG built-in data types
2223 */
Michal Vasko22df3f02020-08-24 13:29:22 +02002224const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
Radek Krejci5969f272018-11-23 10:03:58 +01002225 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
Radek Krejci0f969882020-08-21 16:56:47 +02002226 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"};
Radek Krejci19a96102018-11-15 13:38:09 +01002227
Radek Krejcia3045382018-11-22 14:30:31 +01002228/**
2229 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2230 * @param[in] ctx Compile context.
2231 * @param[in] enums_p Array of the parsed enum structures to compile.
2232 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
Radek Krejcia3045382018-11-22 14:30:31 +01002233 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2234 * @param[out] enums Newly created array of the compiled enums information for the current type.
2235 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2236 */
Radek Krejci19a96102018-11-15 13:38:09 +01002237static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002238lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci0f969882020-08-21 16:56:47 +02002239 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002240{
2241 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002242 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002243 int32_t value = 0;
2244 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002245 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002246
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002247 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002248 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2249 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2250 return LY_EVALID;
2251 }
2252
2253 LY_ARRAY_FOR(enums_p, u) {
2254 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002255 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
2256 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done);
2257 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Radek Krejci693262f2019-04-29 15:23:20 +02002258 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002259 if (base_enums) {
2260 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2261 LY_ARRAY_FOR(base_enums, v) {
2262 if (!strcmp(e->name, base_enums[v].name)) {
2263 break;
2264 }
2265 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002266 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2268 "Invalid %s - derived type adds new item \"%s\".",
2269 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2270 return LY_EVALID;
2271 }
2272 match = v;
2273 }
2274
2275 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002276 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002277 if (enums_p[u].flags & LYS_SET_VALUE) {
2278 e->value = (int32_t)enums_p[u].value;
2279 if (!u || e->value >= value) {
2280 value = e->value + 1;
2281 }
2282 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002283 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002284 if (e->value == (*enums)[v].value) {
2285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2286 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2287 e->value, e->name, (*enums)[v].name);
2288 return LY_EVALID;
2289 }
2290 }
2291 } else if (base_enums) {
2292 /* inherit the assigned value */
2293 e->value = base_enums[match].value;
2294 if (!u || e->value >= value) {
2295 value = e->value + 1;
2296 }
2297 } else {
2298 /* assign value automatically */
2299 if (u && value == INT32_MIN) {
2300 /* counter overflow */
2301 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2302 "Invalid enumeration - it is not possible to auto-assign enum value for "
2303 "\"%s\" since the highest value is already 2147483647.", e->name);
2304 return LY_EVALID;
2305 }
2306 e->value = value++;
2307 }
2308 } else { /* LY_TYPE_BITS */
2309 if (enums_p[u].flags & LYS_SET_VALUE) {
2310 e->value = (int32_t)enums_p[u].value;
2311 if (!u || (uint32_t)e->value >= position) {
2312 position = (uint32_t)e->value + 1;
2313 }
2314 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002315 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002316 if (e->value == (*enums)[v].value) {
2317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2318 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek Krejci0f969882020-08-21 16:56:47 +02002319 (uint32_t)e->value, e->name, (*enums)[v].name);
Radek Krejci19a96102018-11-15 13:38:09 +01002320 return LY_EVALID;
2321 }
2322 }
2323 } else if (base_enums) {
2324 /* inherit the assigned value */
2325 e->value = base_enums[match].value;
2326 if (!u || (uint32_t)e->value >= position) {
2327 position = (uint32_t)e->value + 1;
2328 }
2329 } else {
2330 /* assign value automatically */
2331 if (u && position == 0) {
2332 /* counter overflow */
2333 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2334 "Invalid bits - it is not possible to auto-assign bit position for "
2335 "\"%s\" since the highest value is already 4294967295.", e->name);
2336 return LY_EVALID;
2337 }
2338 e->value = position++;
2339 }
2340 }
2341
2342 if (base_enums) {
2343 /* the assigned values must not change from the derived type */
2344 if (e->value != base_enums[match].value) {
2345 if (basetype == LY_TYPE_ENUM) {
2346 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2347 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2348 e->name, base_enums[match].value, e->value);
2349 } else {
2350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2351 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2352 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2353 }
2354 return LY_EVALID;
2355 }
2356 }
2357
Radek Krejciec4da802019-05-02 13:02:41 +02002358 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002359 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002360
2361 if (basetype == LY_TYPE_BITS) {
2362 /* keep bits ordered by position */
Radek Krejci1e008d22020-08-17 11:37:37 +02002363 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
Radek Krejci19a96102018-11-15 13:38:09 +01002364 if (v != u) {
2365 memcpy(&storage, e, sizeof *e);
2366 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2367 memcpy(&(*enums)[v], &storage, sizeof storage);
2368 }
2369 }
2370 }
2371
2372done:
2373 return ret;
2374}
2375
Radek Krejcia3045382018-11-22 14:30:31 +01002376/**
2377 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2378 *
2379 * path-arg = absolute-path / relative-path
2380 * absolute-path = 1*("/" (node-identifier *path-predicate))
2381 * relative-path = 1*(".." "/") descendant-path
2382 *
2383 * @param[in,out] path Path to parse.
2384 * @param[out] prefix Prefix of the token, NULL if there is not any.
2385 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2386 * @param[out] name Name of the token.
2387 * @param[out] nam_len Length of the name.
2388 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2389 * must not be changed between consecutive calls. -1 if the
2390 * path is absolute.
2391 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2392 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2393 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002394LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002395lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
Radek Krejci857189e2020-09-01 13:26:36 +02002396 int32_t *parent_times, ly_bool *has_predicate)
Radek Krejcia3045382018-11-22 14:30:31 +01002397{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002398 int32_t par_times = 0;
Radek Krejcia3045382018-11-22 14:30:31 +01002399
2400 assert(path && *path);
2401 assert(parent_times);
2402 assert(prefix);
2403 assert(prefix_len);
2404 assert(name);
2405 assert(name_len);
2406 assert(has_predicate);
2407
2408 *prefix = NULL;
2409 *prefix_len = 0;
2410 *name = NULL;
2411 *name_len = 0;
2412 *has_predicate = 0;
2413
2414 if (!*parent_times) {
2415 if (!strncmp(*path, "..", 2)) {
2416 *path += 2;
2417 ++par_times;
2418 while (!strncmp(*path, "/..", 3)) {
2419 *path += 3;
2420 ++par_times;
2421 }
2422 }
2423 if (par_times) {
2424 *parent_times = par_times;
2425 } else {
2426 *parent_times = -1;
2427 }
2428 }
2429
2430 if (**path != '/') {
2431 return LY_EINVAL;
2432 }
2433 /* skip '/' */
2434 ++(*path);
2435
2436 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002437 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002438
2439 if ((**path == '/' && (*path)[1]) || !**path) {
2440 /* path continues by another token or this is the last token */
2441 return LY_SUCCESS;
2442 } else if ((*path)[0] != '[') {
2443 /* unexpected character */
2444 return LY_EINVAL;
2445 } else {
2446 /* predicate starting with [ */
2447 *has_predicate = 1;
2448 return LY_SUCCESS;
2449 }
2450}
2451
2452/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002453 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2454 *
2455 * The set of features used for target must be a subset of features used for the leafref.
2456 * This is not a perfect, we should compare the truth tables but it could require too much resources
2457 * and RFC 7950 does not require it explicitely, so we simplify that.
2458 *
2459 * @param[in] refnode The leafref node.
2460 * @param[in] target Tha target node of the leafref.
2461 * @return LY_SUCCESS or LY_EVALID;
2462 */
2463static LY_ERR
2464lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2465{
2466 LY_ERR ret = LY_EVALID;
2467 const struct lysc_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002468 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci58d171e2018-11-23 13:50:55 +01002469 struct ly_set features = {0};
2470
2471 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002472 if (iter->iffeatures) {
2473 LY_ARRAY_FOR(iter->iffeatures, u) {
2474 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002475 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002476 }
2477 }
2478 }
2479 }
2480
2481 /* we should have, in features set, a superset of features applicable to the target node.
Radek Krejciba03a5a2020-08-27 14:40:41 +02002482 * If the feature is not present, we don;t have a subset of features applicable
Radek Krejci58d171e2018-11-23 13:50:55 +01002483 * to the leafref itself. */
Radek Krejci58d171e2018-11-23 13:50:55 +01002484 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002485 if (iter->iffeatures) {
2486 LY_ARRAY_FOR(iter->iffeatures, u) {
2487 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002488 if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
2489 /* feature not present */
Radek Krejci58d171e2018-11-23 13:50:55 +01002490 goto cleanup;
2491 }
2492 }
2493 }
2494 }
2495 }
2496 ret = LY_SUCCESS;
2497
2498cleanup:
2499 ly_set_erase(&features, NULL);
2500 return ret;
2501}
2502
Michal Vasko004d3152020-06-11 19:59:22 +02002503static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002504 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2505 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod);
Radek Krejcia3045382018-11-22 14:30:31 +01002506
Radek Krejcia3045382018-11-22 14:30:31 +01002507/**
2508 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2509 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002510 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2511 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2512 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2513 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002514 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002515 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002516 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002517 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2518 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2519 * @param[out] type Newly created type structure with the filled information about the type.
2520 * @return LY_ERR value.
2521 */
Radek Krejci19a96102018-11-15 13:38:09 +01002522static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002523lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Michal Vasko1734be92020-09-22 08:55:10 +02002524 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, struct lys_module *module,
2525 LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002526{
2527 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002528 struct lysc_type_bin *bin;
2529 struct lysc_type_num *num;
2530 struct lysc_type_str *str;
2531 struct lysc_type_bits *bits;
2532 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002533 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002534 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002535 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002536 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002537
2538 switch (basetype) {
2539 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +02002540 bin = (struct lysc_type_bin *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002541
2542 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002543 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002544 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002545 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002546 if (!tpdfname) {
Michal Vasko1734be92020-09-22 08:55:10 +02002547 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, cleanup);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002548 }
2549 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002550 break;
2551 case LY_TYPE_BITS:
2552 /* RFC 7950 9.7 - bits */
Michal Vasko22df3f02020-08-24 13:29:22 +02002553 bits = (struct lysc_type_bits *)(*type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002554 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002555 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002556 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
2557 (struct lysc_type_bitenum_item **)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002558 }
2559
Radek Krejci555cb5b2018-11-16 14:54:33 +01002560 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002561 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002562 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002563 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002564 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002565 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejcic5c27e52018-11-15 14:38:11 +01002566 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002567 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002568 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002569 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002570 case LY_TYPE_DEC64:
Radek Krejci115a74d2020-08-14 22:18:12 +02002571 dec = (struct lysc_type_dec *)(*type);
Radek Krejci6cba4292018-11-15 17:33:29 +01002572
2573 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002574 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002575 if (!type_p->fraction_digits) {
2576 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002578 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002580 }
2581 return LY_EVALID;
2582 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002583 dec->fraction_digits = type_p->fraction_digits;
2584 } else {
2585 if (type_p->fraction_digits) {
2586 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
2587 if (tpdfname) {
2588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2589 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
2590 tpdfname);
2591 } else {
2592 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2593 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci115a74d2020-08-14 22:18:12 +02002594 }
2595 return LY_EVALID;
Radek Krejci6cba4292018-11-15 17:33:29 +01002596 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002597 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
Radek Krejci6cba4292018-11-15 17:33:29 +01002598 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002599
2600 /* RFC 7950 9.2.4 - range */
2601 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002602 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
Radek Krejci115a74d2020-08-14 22:18:12 +02002603 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002604 if (!tpdfname) {
Michal Vasko1734be92020-09-22 08:55:10 +02002605 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, cleanup);
Radek Krejci6cba4292018-11-15 17:33:29 +01002606 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002607 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002608 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002609 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002610 str = (struct lysc_type_str *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002611
2612 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002613 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002614 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002615 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002616 if (!tpdfname) {
Michal Vasko1734be92020-09-22 08:55:10 +02002617 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, cleanup);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002618 }
Michal Vasko22df3f02020-08-24 13:29:22 +02002619 } else if (base && ((struct lysc_type_str *)base)->length) {
2620 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002621 }
2622
2623 /* RFC 7950 9.4.5 - pattern */
2624 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002625 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Michal Vasko22df3f02020-08-24 13:29:22 +02002626 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
2627 } else if (base && ((struct lysc_type_str *)base)->patterns) {
2628 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002629 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002630 break;
2631 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +02002632 enumeration = (struct lysc_type_enum *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002633
2634 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002635 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002636 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002637 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002638 }
2639
Radek Krejci555cb5b2018-11-16 14:54:33 +01002640 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002641 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002642 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002644 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejcic5c27e52018-11-15 14:38:11 +01002646 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002647 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002648 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002649 break;
2650 case LY_TYPE_INT8:
2651 case LY_TYPE_UINT8:
2652 case LY_TYPE_INT16:
2653 case LY_TYPE_UINT16:
2654 case LY_TYPE_INT32:
2655 case LY_TYPE_UINT32:
2656 case LY_TYPE_INT64:
2657 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +02002658 num = (struct lysc_type_num *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002659
2660 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002661 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002662 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002663 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002664 if (!tpdfname) {
Michal Vasko1734be92020-09-22 08:55:10 +02002665 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, cleanup);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002666 }
2667 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002668 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002669 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002670 idref = (struct lysc_type_identityref *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002671
2672 /* RFC 7950 9.10.2 - base */
2673 if (type_p->bases) {
2674 if (base) {
2675 /* only the directly derived identityrefs can contain base specification */
2676 if (tpdfname) {
2677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002678 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002679 tpdfname);
2680 } else {
2681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002682 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002683 }
2684 return LY_EVALID;
2685 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002686 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002687 }
2688
2689 if (!base && !type_p->flags) {
2690 /* type derived from identityref built-in type must contain at least one base */
2691 if (tpdfname) {
2692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2693 } else {
2694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002695 }
2696 return LY_EVALID;
2697 }
Radek Krejci555cb5b2018-11-16 14:54:33 +01002698 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002699 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002700 lref = (struct lysc_type_leafref *)*type;
Michal Vasko004d3152020-06-11 19:59:22 +02002701
Radek Krejcia3045382018-11-22 14:30:31 +01002702 /* RFC 7950 9.9.3 - require-instance */
2703 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002704 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002705 if (tpdfname) {
2706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2707 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2708 } else {
2709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2710 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002711 }
2712 return LY_EVALID;
2713 }
Michal Vasko004d3152020-06-11 19:59:22 +02002714 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002715 } else if (base) {
2716 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002717 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002718 } else {
2719 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002720 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002721 }
2722 if (type_p->path) {
Michal Vasko1734be92020-09-22 08:55:10 +02002723 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Michal Vasko004d3152020-06-11 19:59:22 +02002724 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002725 } else if (base) {
Michal Vasko1734be92020-09-22 08:55:10 +02002726 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Michal Vasko22df3f02020-08-24 13:29:22 +02002727 lref->path_context = ((struct lysc_type_leafref *)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002728 } else if (tpdfname) {
2729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2730 return LY_EVALID;
2731 } else {
2732 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Radek Krejcia3045382018-11-22 14:30:31 +01002733 return LY_EVALID;
2734 }
Radek Krejcia3045382018-11-22 14:30:31 +01002735 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002736 case LY_TYPE_INST:
2737 /* RFC 7950 9.9.3 - require-instance */
2738 if (type_p->flags & LYS_SET_REQINST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002739 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
Radek Krejci16c0f822018-11-16 10:46:10 +01002740 } else {
2741 /* default is true */
Michal Vasko22df3f02020-08-24 13:29:22 +02002742 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
Radek Krejci16c0f822018-11-16 10:46:10 +01002743 }
Radek Krejci16c0f822018-11-16 10:46:10 +01002744 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002745 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +02002746 un = (struct lysc_type_union *)(*type);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002747
2748 /* RFC 7950 7.4 - type */
2749 if (type_p->types) {
2750 if (base) {
2751 /* only the directly derived union can contain types specification */
2752 if (tpdfname) {
2753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2754 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2755 tpdfname);
2756 } else {
2757 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2758 "Invalid type substatement for the type not directly derived from union built-in type.");
Radek Krejcicdfecd92018-11-26 11:27:32 +01002759 }
2760 return LY_EVALID;
2761 }
2762 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002763 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2764 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002765 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002766 &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002767 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2768 /* add space for additional types from the union subtype */
2769 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vasko22df3f02020-08-24 13:29:22 +02002770 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
2771 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002772
2773 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002774 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002775 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2776 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2777 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
Michal Vasko22df3f02020-08-24 13:29:22 +02002778 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 +02002779 lref = (struct lysc_type_leafref *)un->types[u + additional];
2780
2781 lref->basetype = LY_TYPE_LEAFREF;
Michal Vasko1734be92020-09-22 08:55:10 +02002782 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path));
Michal Vasko004d3152020-06-11 19:59:22 +02002783 lref->refcount = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +02002784 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
2785 lref->path_context = ((struct lysc_type_leafref *)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002786 /* TODO extensions */
2787
2788 } else {
2789 un->types[u + additional] = un_aux->types[v];
2790 ++un_aux->types[v]->refcount;
2791 }
2792 ++additional;
2793 LY_ARRAY_INCREMENT(un->types);
2794 }
2795 /* compensate u increment in main loop */
2796 --additional;
2797
2798 /* free the replaced union subtype */
Michal Vasko22df3f02020-08-24 13:29:22 +02002799 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002800 } else {
2801 LY_ARRAY_INCREMENT(un->types);
2802 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002803 }
2804 }
2805
2806 if (!base && !type_p->flags) {
2807 /* type derived from union built-in type must contain at least one type */
2808 if (tpdfname) {
2809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2810 } else {
2811 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Radek Krejcicdfecd92018-11-26 11:27:32 +01002812 }
2813 return LY_EVALID;
2814 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002815 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002816 case LY_TYPE_BOOL:
2817 case LY_TYPE_EMPTY:
2818 case LY_TYPE_UNKNOWN: /* just to complete switch */
2819 break;
2820 }
Michal Vasko1734be92020-09-22 08:55:10 +02002821
2822 if (tpdfname) {
2823 switch (basetype) {
2824 case LY_TYPE_BINARY:
2825 type_p->compiled = *type;
2826 *type = calloc(1, sizeof(struct lysc_type_bin));
2827 break;
2828 case LY_TYPE_BITS:
2829 type_p->compiled = *type;
2830 *type = calloc(1, sizeof(struct lysc_type_bits));
2831 break;
2832 case LY_TYPE_DEC64:
2833 type_p->compiled = *type;
2834 *type = calloc(1, sizeof(struct lysc_type_dec));
2835 break;
2836 case LY_TYPE_STRING:
2837 type_p->compiled = *type;
2838 *type = calloc(1, sizeof(struct lysc_type_str));
2839 break;
2840 case LY_TYPE_ENUM:
2841 type_p->compiled = *type;
2842 *type = calloc(1, sizeof(struct lysc_type_enum));
2843 break;
2844 case LY_TYPE_INT8:
2845 case LY_TYPE_UINT8:
2846 case LY_TYPE_INT16:
2847 case LY_TYPE_UINT16:
2848 case LY_TYPE_INT32:
2849 case LY_TYPE_UINT32:
2850 case LY_TYPE_INT64:
2851 case LY_TYPE_UINT64:
2852 type_p->compiled = *type;
2853 *type = calloc(1, sizeof(struct lysc_type_num));
2854 break;
2855 case LY_TYPE_IDENT:
2856 type_p->compiled = *type;
2857 *type = calloc(1, sizeof(struct lysc_type_identityref));
2858 break;
2859 case LY_TYPE_LEAFREF:
2860 type_p->compiled = *type;
2861 *type = calloc(1, sizeof(struct lysc_type_leafref));
2862 break;
2863 case LY_TYPE_INST:
2864 type_p->compiled = *type;
2865 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2866 break;
2867 case LY_TYPE_UNION:
2868 type_p->compiled = *type;
2869 *type = calloc(1, sizeof(struct lysc_type_union));
2870 break;
2871 case LY_TYPE_BOOL:
2872 case LY_TYPE_EMPTY:
2873 case LY_TYPE_UNKNOWN: /* just to complete switch */
2874 break;
2875 }
2876 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002877 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko1734be92020-09-22 08:55:10 +02002878
2879cleanup:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002880 return ret;
2881}
2882
Radek Krejcia3045382018-11-22 14:30:31 +01002883/**
2884 * @brief Compile information about the leaf/leaf-list's type.
2885 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002886 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2887 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2888 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2889 * @param[in] context_name Name of the context node or referencing typedef for logging.
2890 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002891 * @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 +01002892 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002893 * @param[out] dflt Default value for the type.
2894 * @param[out] dflt_mod Local module for the default value.
Radek Krejcia3045382018-11-22 14:30:31 +01002895 * @return LY_ERR value.
2896 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002897static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002898lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002899 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2900 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod)
Radek Krejci19a96102018-11-15 13:38:09 +01002901{
2902 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02002903 ly_bool dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002904 struct type_context {
2905 const struct lysp_tpdf *tpdf;
2906 struct lysp_node *node;
2907 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002908 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002909 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002910 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002911 struct ly_set tpdf_chain = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002912
2913 assert((dflt && dflt_mod) || (!dflt && !dflt_mod));
Radek Krejci19a96102018-11-15 13:38:09 +01002914
2915 (*type) = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002916 if (dflt) {
2917 *dflt = NULL;
2918 *dflt_mod = NULL;
2919 }
Radek Krejci19a96102018-11-15 13:38:09 +01002920
2921 tctx = calloc(1, sizeof *tctx);
2922 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002923 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002924 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2925 ret == LY_SUCCESS;
2926 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2927 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2928 if (basetype) {
2929 break;
2930 }
2931
2932 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002933 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2934 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci87e25252020-09-15 13:28:31 +02002935 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01002936
Radek Krejcicdfecd92018-11-26 11:27:32 +01002937 if (units && !*units) {
2938 /* inherit units */
Radek Krejci87e25252020-09-15 13:28:31 +02002939 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2940 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002941 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002942 if (dflt && !*dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002943 /* inherit default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002944 *dflt = tctx->tpdf->dflt;
2945 *dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002946 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002947 if (dummyloops && (!units || *units) && dflt && *dflt) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002948 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002949 break;
2950 }
2951
Radek Krejci19a96102018-11-15 13:38:09 +01002952 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002953 /* it is not necessary to continue, the rest of the chain was already compiled,
2954 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002955 basetype = tctx->tpdf->type.compiled->basetype;
Radek Krejciba03a5a2020-08-27 14:40:41 +02002956 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02002957 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejciba03a5a2020-08-27 14:40:41 +02002958
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002959 if ((units && !*units) || (dflt && !*dflt)) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002960 dummyloops = 1;
2961 goto preparenext;
2962 } else {
2963 tctx = NULL;
2964 break;
2965 }
Radek Krejci19a96102018-11-15 13:38:09 +01002966 }
2967
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002968 /* circular typedef reference detection */
Radek Krejci1deb5be2020-08-26 16:43:36 +02002969 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002970 /* local part */
Michal Vasko22df3f02020-08-24 13:29:22 +02002971 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002972 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2974 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2975 free(tctx);
2976 ret = LY_EVALID;
2977 goto cleanup;
2978 }
2979 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02002980 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002981 /* global part for unions corner case */
Michal Vasko22df3f02020-08-24 13:29:22 +02002982 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002983 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2984 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2985 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2986 free(tctx);
2987 ret = LY_EVALID;
2988 goto cleanup;
2989 }
2990 }
2991
Radek Krejci19a96102018-11-15 13:38:09 +01002992 /* store information for the following processing */
Radek Krejciba03a5a2020-08-27 14:40:41 +02002993 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02002994 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01002995
Radek Krejcicdfecd92018-11-26 11:27:32 +01002996preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002997 /* prepare next loop */
2998 tctx_prev = tctx;
2999 tctx = calloc(1, sizeof *tctx);
3000 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3001 }
3002 free(tctx);
3003
3004 /* allocate type according to the basetype */
3005 switch (basetype) {
3006 case LY_TYPE_BINARY:
3007 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003008 break;
3009 case LY_TYPE_BITS:
3010 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003011 break;
3012 case LY_TYPE_BOOL:
3013 case LY_TYPE_EMPTY:
3014 *type = calloc(1, sizeof(struct lysc_type));
3015 break;
3016 case LY_TYPE_DEC64:
3017 *type = calloc(1, sizeof(struct lysc_type_dec));
3018 break;
3019 case LY_TYPE_ENUM:
3020 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003021 break;
3022 case LY_TYPE_IDENT:
3023 *type = calloc(1, sizeof(struct lysc_type_identityref));
3024 break;
3025 case LY_TYPE_INST:
3026 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3027 break;
3028 case LY_TYPE_LEAFREF:
3029 *type = calloc(1, sizeof(struct lysc_type_leafref));
3030 break;
3031 case LY_TYPE_STRING:
3032 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003033 break;
3034 case LY_TYPE_UNION:
3035 *type = calloc(1, sizeof(struct lysc_type_union));
3036 break;
3037 case LY_TYPE_INT8:
3038 case LY_TYPE_UINT8:
3039 case LY_TYPE_INT16:
3040 case LY_TYPE_UINT16:
3041 case LY_TYPE_INT32:
3042 case LY_TYPE_UINT32:
3043 case LY_TYPE_INT64:
3044 case LY_TYPE_UINT64:
3045 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003046 break;
3047 case LY_TYPE_UNKNOWN:
3048 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3049 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3050 ret = LY_EVALID;
3051 goto cleanup;
3052 }
3053 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003054 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003055 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3056 ly_data_type2str[basetype]);
3057 free(*type);
3058 (*type) = NULL;
3059 ret = LY_EVALID;
3060 goto cleanup;
3061 }
3062
3063 /* get restrictions from the referred typedefs */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003064 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003065 tctx = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003066
3067 /* remember the typedef context for circular check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003068 ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
3069 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003070
Radek Krejci43699232018-11-23 14:59:46 +01003071 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003072 base = tctx->tpdf->type.compiled;
3073 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003074 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003075 /* no change, just use the type information from the base */
Michal Vasko22df3f02020-08-24 13:29:22 +02003076 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 +01003077 ++base->refcount;
3078 continue;
3079 }
3080
3081 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003082 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3084 tctx->tpdf->name, ly_data_type2str[basetype]);
3085 ret = LY_EVALID;
3086 goto cleanup;
3087 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3088 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3089 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3090 tctx->tpdf->name, tctx->tpdf->dflt);
3091 ret = LY_EVALID;
3092 goto cleanup;
3093 }
3094
Radek Krejci19a96102018-11-15 13:38:09 +01003095 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003096 /* TODO user type plugins */
3097 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003098 prev_type = *type;
Michal Vasko22df3f02020-08-24 13:29:22 +02003099 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 +01003100 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003101 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003102 LY_CHECK_GOTO(ret, cleanup);
3103 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003104 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003105 /* remove the processed typedef contexts from the stack for circular check */
3106 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003107
Radek Krejcic5c27e52018-11-15 14:38:11 +01003108 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003109 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003110 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003111 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003112 /* TODO user type plugins */
3113 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003114 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003115 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 +01003116 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003117 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003118 /* no specific restriction in leaf's type definition, copy from the base */
3119 free(*type);
3120 (*type) = base;
3121 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003122 }
3123
Radek Krejci0935f412019-08-20 16:15:18 +02003124 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003125
3126cleanup:
3127 ly_set_erase(&tpdf_chain, free);
3128 return ret;
3129}
3130
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003131/**
3132 * @brief Compile status information of the given node.
3133 *
3134 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3135 * has the status correctly set during the compilation.
3136 *
3137 * @param[in] ctx Compile context
3138 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3139 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3140 * the compatibility with the parent's status value.
3141 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3142 * @return LY_ERR value.
3143 */
3144static LY_ERR
3145lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3146{
3147 /* status - it is not inherited by specification, but it does not make sense to have
3148 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3149 if (!((*node_flags) & LYS_STATUS_MASK)) {
3150 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3151 if ((parent_flags & 0x3) != 0x3) {
3152 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3153 * combination of bits (0x3) which marks the uses_status value */
3154 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
Radek Krejci0f969882020-08-21 16:56:47 +02003155 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003156 }
3157 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3158 } else {
3159 (*node_flags) |= LYS_STATUS_CURR;
3160 }
3161 } else if (parent_flags & LYS_STATUS_MASK) {
3162 /* check status compatibility with the parent */
3163 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3164 if ((*node_flags) & LYS_STATUS_CURR) {
3165 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3166 "A \"current\" status is in conflict with the parent's \"%s\" status.",
Radek Krejci0f969882020-08-21 16:56:47 +02003167 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003168 } else { /* LYS_STATUS_DEPRC */
3169 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3170 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3171 }
3172 return LY_EVALID;
3173 }
3174 }
3175 return LY_SUCCESS;
3176}
3177
Radek Krejci8cce8532019-03-05 11:27:45 +01003178/**
3179 * @brief Check uniqness of the node/action/notification name.
3180 *
3181 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3182 * structures, but they share the namespace so we need to check their name collisions.
3183 *
3184 * @param[in] ctx Compile context.
Michal Vasko20424b42020-08-31 12:29:38 +02003185 * @param[in] parent Parent of the nodes to check, can be NULL.
Radek Krejci8cce8532019-03-05 11:27:45 +01003186 * @param[in] name Name of the item to find in the given lists.
Michal Vasko20424b42020-08-31 12:29:38 +02003187 * @param[in] exclude Node that was just added that should be excluded from the name checking.
Radek Krejci8cce8532019-03-05 11:27:45 +01003188 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3189 */
3190static LY_ERR
Michal Vasko20424b42020-08-31 12:29:38 +02003191lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
3192 const struct lysc_node *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003193{
Michal Vasko20424b42020-08-31 12:29:38 +02003194 const struct lysc_node *iter, *iter2;
3195 const struct lysc_action *actions;
3196 const struct lysc_notif *notifs;
3197 uint32_t getnext_flags;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003198 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003199
Michal Vasko20424b42020-08-31 12:29:38 +02003200#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
3201
3202 if (exclude->nodetype == LYS_CASE) {
3203 /* check restricted only to all the cases */
3204 assert(parent->nodetype == LYS_CHOICE);
3205 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
3206 if (CHECK_NODE(iter, exclude, name)) {
3207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
3208 return LY_EEXIST;
3209 }
3210 }
3211
3212 return LY_SUCCESS;
3213 }
3214
3215 /* no reason for our parent to be choice anymore */
3216 assert(!parent || (parent->nodetype != LYS_CHOICE));
3217
3218 if (parent && (parent->nodetype == LYS_CASE)) {
3219 /* move to the first data definition parent */
3220 parent = lysc_data_parent(parent);
3221 }
3222
3223 getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE;
3224 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
3225 getnext_flags |= LYS_GETNEXT_OUTPUT;
3226 }
3227
3228 iter = NULL;
3229 while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) {
3230 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003231 goto error;
3232 }
Michal Vasko20424b42020-08-31 12:29:38 +02003233
3234 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
3235 if (iter->nodetype == LYS_CHOICE) {
3236 iter2 = NULL;
3237 while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) {
3238 if (CHECK_NODE(iter2, exclude, name)) {
3239 goto error;
3240 }
3241 }
3242 }
Radek Krejci8cce8532019-03-05 11:27:45 +01003243 }
Michal Vasko20424b42020-08-31 12:29:38 +02003244
3245 actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003246 LY_ARRAY_FOR(actions, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003247 if (CHECK_NODE(&actions[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003248 goto error;
3249 }
3250 }
Michal Vasko20424b42020-08-31 12:29:38 +02003251
3252 notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003253 LY_ARRAY_FOR(notifs, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003254 if (CHECK_NODE(&notifs[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003255 goto error;
3256 }
3257 }
3258 return LY_SUCCESS;
Michal Vasko20424b42020-08-31 12:29:38 +02003259
Radek Krejci8cce8532019-03-05 11:27:45 +01003260error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003262 return LY_EEXIST;
Michal Vasko20424b42020-08-31 12:29:38 +02003263
3264#undef CHECK_NODE
Radek Krejci8cce8532019-03-05 11:27:45 +01003265}
3266
Radek Krejciec4da802019-05-02 13:02:41 +02003267static 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 +01003268
Radek Krejcia3045382018-11-22 14:30:31 +01003269/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003270 * @brief Compile parsed RPC/action schema node information.
3271 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003272 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003273 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003274 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3275 * @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).
3276 * Zero means no uses, non-zero value with no status bit set mean the default status.
3277 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3278 */
3279static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003280lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003281 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003282{
3283 LY_ERR ret = LY_SUCCESS;
3284 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003285 LY_ARRAY_COUNT_TYPE u;
3286 uint32_t opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003287
Radek Krejci327de162019-06-14 12:52:07 +02003288 lysc_update_path(ctx, parent, action_p->name);
3289
Michal Vasko20424b42020-08-31 12:29:38 +02003290 /* member needed for uniqueness check lys_getnext() */
3291 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
3292 action->module = ctx->mod;
3293 action->parent = parent;
3294
3295 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action));
Radek Krejci8cce8532019-03-05 11:27:45 +01003296
Radek Krejciec4da802019-05-02 13:02:41 +02003297 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003298 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003299 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003300 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003301 return LY_EVALID;
3302 }
3303
Radek Krejciec4da802019-05-02 13:02:41 +02003304 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003305 action->sp = action_p;
3306 }
3307 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3308
3309 /* status - it is not inherited by specification, but it does not make sense to have
3310 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003311 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003312
Radek Krejci011e4aa2020-09-04 15:22:31 +02003313 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
3314 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
3315 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003316 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003317 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003318
3319 /* input */
Michal Vasko22df3f02020-08-24 13:29:22 +02003320 lysc_update_path(ctx, (struct lysc_node *)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003321 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003322 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 +02003323 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003324 LY_LIST_FOR(action_p->input.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003325 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003326 }
Radek Krejci327de162019-06-14 12:52:07 +02003327 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003328 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003329
3330 /* output */
Michal Vasko22df3f02020-08-24 13:29:22 +02003331 lysc_update_path(ctx, (struct lysc_node *)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003332 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003333 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 +02003334 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003335 LY_LIST_FOR(action_p->output.data, child_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003336 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003337 }
Radek Krejci327de162019-06-14 12:52:07 +02003338 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003339 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003340
3341 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3342 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003343 ret = ly_set_add(&ctx->xpath, action, 0, NULL);
3344 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003345 }
3346
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003347cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003348 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003349 return ret;
3350}
3351
3352/**
Radek Krejci43981a32019-04-12 09:44:11 +02003353 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003354 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003355 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003356 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3357 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3358 * @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 +02003359 * Zero means no uses, non-zero value with no status bit set mean the default status.
3360 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3361 */
3362static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003363lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003364 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
Radek Krejcifc11bd72019-04-11 16:00:05 +02003365{
3366 LY_ERR ret = LY_SUCCESS;
3367 struct lysp_node *child_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003368 LY_ARRAY_COUNT_TYPE u;
3369 uint32_t opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003370
Radek Krejci327de162019-06-14 12:52:07 +02003371 lysc_update_path(ctx, parent, notif_p->name);
3372
Michal Vasko20424b42020-08-31 12:29:38 +02003373 /* member needed for uniqueness check lys_getnext() */
3374 notif->nodetype = LYS_NOTIF;
3375 notif->module = ctx->mod;
3376 notif->parent = parent;
3377
3378 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003379
Radek Krejciec4da802019-05-02 13:02:41 +02003380 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3382 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003383 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003384 return LY_EVALID;
3385 }
3386
Radek Krejciec4da802019-05-02 13:02:41 +02003387 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003388 notif->sp = notif_p;
3389 }
3390 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3391
3392 /* status - it is not inherited by specification, but it does not make sense to have
3393 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003394 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
3395 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003396
Radek Krejci011e4aa2020-09-04 15:22:31 +02003397 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
3398 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
3399 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003400 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003401 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003402 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3403 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003404 ret = ly_set_add(&ctx->xpath, notif, 0, NULL);
3405 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003406 }
Radek Krejci0935f412019-08-20 16:15:18 +02003407 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003408
Radek Krejciec4da802019-05-02 13:02:41 +02003409 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003410 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003411 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status);
3412 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003413 }
3414
Radek Krejci327de162019-06-14 12:52:07 +02003415 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003416cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003417 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003418 return ret;
3419}
3420
3421/**
Radek Krejcia3045382018-11-22 14:30:31 +01003422 * @brief Compile parsed container node information.
3423 * @param[in] ctx Compile context
3424 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003425 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3426 * is enriched with the container-specific information.
3427 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3428 */
Radek Krejci19a96102018-11-15 13:38:09 +01003429static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003430lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003431{
Michal Vasko22df3f02020-08-24 13:29:22 +02003432 struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p;
3433 struct lysc_node_container *cont = (struct lysc_node_container *)node;
Radek Krejci19a96102018-11-15 13:38:09 +01003434 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003435 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003436 LY_ERR ret = LY_SUCCESS;
3437
Radek Krejcife909632019-02-12 15:34:42 +01003438 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003439 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003440 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003441 } else if (cont_p->musts) {
3442 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003443 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3444 cont->flags |= LYS_PRESENCE;
3445 } else if (cont_p->when) {
3446 /* container with a when condition */
3447 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 +02003448 cont->flags |= LYS_PRESENCE;
3449 } else if (cont_p->parent) {
3450 if (cont_p->parent->nodetype == LYS_CHOICE) {
3451 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003452 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 +02003453 cont_p->name, cont_p->parent->name);
3454 cont->flags |= LYS_PRESENCE;
3455 } else if ((cont_p->parent->nodetype == LYS_CASE)
3456 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3457 /* 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 +02003458 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 +02003459 cont_p->name, cont_p->parent->name);
3460 cont->flags |= LYS_PRESENCE;
3461 }
Radek Krejcife909632019-02-12 15:34:42 +01003462 }
3463
Michal Vaskoba417ac2020-08-06 14:48:20 +02003464 /* more cases when the container has meaning but is kept NP for convenience:
3465 * - when condition
3466 * - direct child action/notification
3467 */
3468
Radek Krejci19a96102018-11-15 13:38:09 +01003469 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02003470 ret = lys_compile_node(ctx, child_p, node, 0);
3471 LY_CHECK_GOTO(ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003472 }
3473
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003474 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003475 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3476 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003477 ret = ly_set_add(&ctx->xpath, cont, 0, NULL);
3478 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003479 }
Radek Krejciec4da802019-05-02 13:02:41 +02003480 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3481 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003482
3483done:
3484 return ret;
3485}
3486
Radek Krejci33f72892019-02-21 10:36:58 +01003487/*
3488 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3489 * @param[in] ctx Compile context.
3490 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3491 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003492 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3493 * @return LY_ERR value.
3494 */
3495static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003496lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003497 struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003498{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003499 const char *dflt;
3500 struct lys_module *dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003501
Radek Krejciec4da802019-05-02 13:02:41 +02003502 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 +02003503 leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod));
3504
3505 /* store default value, if any */
3506 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
3507 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, dflt, dflt_mod));
Radek Krejci33f72892019-02-21 10:36:58 +01003508 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003509
Radek Krejci33f72892019-02-21 10:36:58 +01003510 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3511 /* 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 +02003512 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003513 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003514 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003515 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
3516 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
Radek Krejci33f72892019-02-21 10:36:58 +01003517 /* 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 +02003518 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003519 }
3520 }
3521 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003522 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3523 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3524 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3525 return LY_EVALID;
3526 }
3527 }
3528
Radek Krejci33f72892019-02-21 10:36:58 +01003529 return LY_SUCCESS;
3530}
3531
Radek Krejcia3045382018-11-22 14:30:31 +01003532/**
3533 * @brief Compile parsed leaf node information.
3534 * @param[in] ctx Compile context
3535 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003536 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3537 * is enriched with the leaf-specific information.
3538 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3539 */
Radek Krejci19a96102018-11-15 13:38:09 +01003540static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003541lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003542{
Michal Vasko22df3f02020-08-24 13:29:22 +02003543 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p;
3544 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003545 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003546 LY_ERR ret = LY_SUCCESS;
3547
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003548 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003549 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3550 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003551 ret = ly_set_add(&ctx->xpath, leaf, 0, NULL);
3552 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003553 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003554 if (leaf_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003555 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003556 leaf->flags |= LYS_SET_UNITS;
3557 }
Radek Krejcia1911222019-07-22 17:24:50 +02003558
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003559 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003560 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3561 LY_CHECK_GOTO(ret, done);
Radek Krejcia1911222019-07-22 17:24:50 +02003562
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003563 /* store/update default value */
Radek Krejciccd20f12019-02-15 14:12:27 +01003564 if (leaf_p->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003565 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, leaf_p->dflt, ctx->mod_def));
Radek Krejci76b3e962018-12-14 17:01:25 +01003566 leaf->flags |= LYS_SET_DFLT;
3567 }
Radek Krejci43699232018-11-23 14:59:46 +01003568
Radek Krejci19a96102018-11-15 13:38:09 +01003569done:
3570 return ret;
3571}
3572
Radek Krejcia3045382018-11-22 14:30:31 +01003573/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003574 * @brief Compile parsed leaf-list node information.
3575 * @param[in] ctx Compile context
3576 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003577 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3578 * is enriched with the leaf-list-specific information.
3579 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3580 */
3581static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003582lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003583{
Michal Vasko22df3f02020-08-24 13:29:22 +02003584 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p;
3585 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003586 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003587 LY_ERR ret = LY_SUCCESS;
3588
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003589 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003590 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3591 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003592 ret = ly_set_add(&ctx->xpath, llist, 0, NULL);
3593 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003594 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003595 if (llist_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003596 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003597 llist->flags |= LYS_SET_UNITS;
3598 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003599
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003600 /* compile type */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003601 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist);
3602 LY_CHECK_GOTO(ret, done);
Michal Vasko6a044b22020-01-15 12:25:39 +01003603
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003604 /* store/update default values */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003605 if (llist_p->dflts) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003606 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, llist_p->dflts, ctx->mod_def), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003607 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003608 }
3609
3610 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003611 if (llist->min) {
3612 llist->flags |= LYS_MAND_TRUE;
3613 }
Radek Krejcib7408632018-11-28 17:12:11 +01003614 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003615
Radek Krejci0e5d8382018-11-28 16:37:53 +01003616done:
3617 return ret;
3618}
3619
3620/**
Radek Krejci7af64242019-02-18 13:07:53 +01003621 * @brief Compile information about list's uniques.
3622 * @param[in] ctx Compile context.
3623 * @param[in] context_module Module where the prefixes are going to be resolved.
3624 * @param[in] uniques Sized array list of unique statements.
3625 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3626 * @return LY_ERR value.
3627 */
3628static LY_ERR
3629lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3630{
3631 LY_ERR ret = LY_SUCCESS;
3632 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003633 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003634 const char *keystr, *delim;
3635 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003636 LY_ARRAY_COUNT_TYPE v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003637 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003638 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003639
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003640 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003641 config = -1;
3642 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3643 keystr = uniques[v];
3644 while (keystr) {
3645 delim = strpbrk(keystr, " \t\n");
3646 if (delim) {
3647 len = delim - keystr;
3648 while (isspace(*delim)) {
3649 ++delim;
3650 }
3651 } else {
3652 len = strlen(keystr);
3653 }
3654
3655 /* unique node must be present */
3656 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko22df3f02020-08-24 13:29:22 +02003657 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0,
3658 (const struct lysc_node **)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003659 if (ret != LY_SUCCESS) {
3660 if (ret == LY_EDENIED) {
3661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003662 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003663 len, keystr, lys_nodetype2str((*key)->nodetype));
3664 }
3665 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003666 } else if (flags) {
3667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3668 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003669 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003670 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003671 }
3672
3673 /* all referenced leafs must be of the same config type */
3674 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003676 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003677 return LY_EVALID;
3678 } else if ((*key)->flags & LYS_CONFIG_W) {
3679 config = 1;
3680 } else { /* LYS_CONFIG_R */
3681 config = 0;
3682 }
3683
Michal Vasko14654712020-02-06 08:35:21 +01003684 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3685 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3686 if (parent->nodetype == LYS_LIST) {
3687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3688 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3689 return LY_EVALID;
3690 }
3691 }
3692
Radek Krejci7af64242019-02-18 13:07:53 +01003693 /* check status */
3694 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0f969882020-08-21 16:56:47 +02003695 (*key)->flags, (*key)->module, (*key)->name));
Radek Krejci7af64242019-02-18 13:07:53 +01003696
3697 /* mark leaf as unique */
3698 (*key)->flags |= LYS_UNIQUE;
3699
3700 /* next unique value in line */
3701 keystr = delim;
3702 }
3703 /* next unique definition */
3704 }
3705
3706 return LY_SUCCESS;
3707}
3708
3709/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003710 * @brief Compile parsed list node information.
3711 * @param[in] ctx Compile context
3712 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003713 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3714 * is enriched with the list-specific information.
3715 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3716 */
3717static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003718lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003719{
Michal Vasko22df3f02020-08-24 13:29:22 +02003720 struct lysp_node_list *list_p = (struct lysp_node_list *)node_p;
3721 struct lysc_node_list *list = (struct lysc_node_list *)node;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003722 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003723 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003724 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003725 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003726 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 LY_ERR ret = LY_SUCCESS;
3728
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003729 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003730 if (list->min) {
3731 list->flags |= LYS_MAND_TRUE;
3732 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003733 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3734
3735 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003736 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003737 }
3738
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003739 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003740 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3741 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003742 LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL));
Michal Vasko5d8756a2019-11-07 15:21:00 +01003743 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003744
3745 /* keys */
3746 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3748 return LY_EVALID;
3749 }
3750
3751 /* find all the keys (must be direct children) */
3752 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003753 if (!keystr) {
3754 /* keyless list */
3755 list->flags |= LYS_KEYLESS;
3756 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003757 while (keystr) {
3758 delim = strpbrk(keystr, " \t\n");
3759 if (delim) {
3760 len = delim - keystr;
3761 while (isspace(*delim)) {
3762 ++delim;
3763 }
3764 } else {
3765 len = strlen(keystr);
3766 }
3767
3768 /* key node must be present */
Michal Vasko22df3f02020-08-24 13:29:22 +02003769 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 +02003770 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3772 "The list's key \"%.*s\" not found.", len, keystr);
3773 return LY_EVALID;
3774 }
3775 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003776 if (key->flags & LYS_KEY) {
3777 /* the node was already marked as a key */
3778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3779 "Duplicated key identifier \"%.*s\".", len, keystr);
3780 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003781 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003782
Michal Vasko22df3f02020-08-24 13:29:22 +02003783 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003784 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003785 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3787 return LY_EVALID;
3788 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003789 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003790 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003791 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003793 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003794 return LY_EVALID;
3795 }
3796 } else {
3797 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003798 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003800 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003801 return LY_EVALID;
3802 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003803 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003804 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003805 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003806 return LY_EVALID;
3807 }
3808 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003809
3810 /* check status */
3811 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003812 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003813
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003814 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003815 if (key->dflt) {
3816 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3817 lysc_type_free(ctx->ctx, key->dflt->realtype);
3818 free(key->dflt);
3819 key->dflt = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003820 }
3821 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003822 key->flags |= LYS_KEY;
3823
3824 /* move it to the correct position */
Michal Vasko22df3f02020-08-24 13:29:22 +02003825 if ((prev_key && (struct lysc_node *)prev_key != key->prev) || (!prev_key && key->prev->next)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02003826 /* fix links in closest previous siblings of the key */
3827 if (key->next) {
3828 key->next->prev = key->prev;
3829 } else {
3830 /* last child */
3831 list->child->prev = key->prev;
3832 }
3833 if (key->prev->next) {
3834 key->prev->next = key->next;
3835 }
3836 /* fix links in the key */
3837 if (prev_key) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003838 key->prev = (struct lysc_node *)prev_key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003839 key->next = prev_key->next;
3840 } else {
3841 key->prev = list->child->prev;
3842 key->next = list->child;
3843 }
3844 /* fix links in closes future siblings of the key */
3845 if (prev_key) {
3846 if (prev_key->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003847 prev_key->next->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003848 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003849 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003850 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003851 prev_key->next = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003852 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003853 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003854 }
3855 /* fix links in parent */
3856 if (!key->prev->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003857 list->child = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003858 }
3859 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003860
3861 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003862 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003863 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003864 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003865 }
3866
3867 /* uniques */
3868 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003869 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003870 }
3871
Radek Krejciec4da802019-05-02 13:02:41 +02003872 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3873 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003874
3875done:
3876 return ret;
3877}
3878
Radek Krejcib56c7502019-02-13 14:19:54 +01003879/**
3880 * @brief Do some checks and set the default choice's case.
3881 *
3882 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3883 *
3884 * @param[in] ctx Compile context.
3885 * @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,
3886 * not the reference to the imported module.
3887 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3888 * @return LY_ERR value.
3889 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003890static LY_ERR
3891lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3892{
Michal Vasko22df3f02020-08-24 13:29:22 +02003893 struct lysc_node *iter, *node = (struct lysc_node *)ch;
Radek Krejci76b3e962018-12-14 17:01:25 +01003894 const char *prefix = NULL, *name;
3895 size_t prefix_len = 0;
3896
3897 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3898 name = strchr(dflt, ':');
3899 if (name) {
3900 prefix = dflt;
3901 prefix_len = name - prefix;
3902 ++name;
3903 } else {
3904 name = dflt;
3905 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003906 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003907 /* prefixed default case make sense only for the prefix of the schema itself */
3908 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3909 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3910 prefix_len, prefix);
3911 return LY_EVALID;
3912 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003913 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 +01003914 if (!ch->dflt) {
3915 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3916 "Default case \"%s\" not found.", dflt);
3917 return LY_EVALID;
3918 }
3919 /* no mandatory nodes directly under the default case */
3920 LY_LIST_FOR(ch->dflt->child, iter) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003921 if (iter->parent != (struct lysc_node *)ch->dflt) {
Radek Krejcife13da42019-02-15 14:51:01 +01003922 break;
3923 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003924 if (iter->flags & LYS_MAND_TRUE) {
3925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3926 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3927 return LY_EVALID;
3928 }
3929 }
Radek Krejci01342af2019-01-03 15:18:08 +01003930 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003931 return LY_SUCCESS;
3932}
3933
Radek Krejciccd20f12019-02-15 14:12:27 +01003934static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003935lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003936{
3937 struct lys_module *mod;
3938 const char *prefix = NULL, *name;
3939 size_t prefix_len = 0;
3940 struct lysc_node_case *cs;
3941 struct lysc_node *node;
3942
3943 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3944 name = strchr(dflt, ':');
3945 if (name) {
3946 prefix = dflt;
3947 prefix_len = name - prefix;
3948 ++name;
3949 } else {
3950 name = dflt;
3951 }
3952 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3953 if (prefix) {
3954 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003956 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3957 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003958 return LY_EVALID;
3959 }
3960 } else {
3961 mod = ctx->mod;
3962 }
3963 /* get the default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003964 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 +01003965 if (!cs) {
3966 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003967 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003968 return LY_EVALID;
3969 }
3970
3971 /* check that there is no mandatory node */
3972 LY_LIST_FOR(cs->child, node) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003973 if (node->parent != (struct lysc_node *)cs) {
Radek Krejcife13da42019-02-15 14:51:01 +01003974 break;
3975 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003976 if (node->flags & LYS_MAND_TRUE) {
3977 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003978 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3979 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003980 return LY_EVALID;
3981 }
3982 }
3983
3984 /* set the default case in choice */
3985 ch->dflt = cs;
3986 cs->flags |= LYS_SET_DFLT;
3987
3988 return LY_SUCCESS;
3989}
3990
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003991/**
Michal Vasko20424b42020-08-31 12:29:38 +02003992 * @brief Compile choice children.
3993 *
3994 * @param[in] ctx Compile context
3995 * @param[in] child_p Parsed choice children nodes.
3996 * @param[in] node Compiled choice node to compile and add children to.
3997 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3998 */
3999static LY_ERR
4000lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node)
4001{
4002 LY_ERR ret = LY_SUCCESS;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004003 struct lysp_node *child_p_next = child_p->next;
Michal Vasko20424b42020-08-31 12:29:38 +02004004 struct lysp_node_case *cs_p;
4005
4006 if (child_p->nodetype == LYS_CASE) {
4007 /* standard case under choice */
4008 ret = lys_compile_node(ctx, child_p, node, 0);
4009 } else {
4010 /* we need the implicit case first, so create a fake parsed case */
4011 cs_p = calloc(1, sizeof *cs_p);
4012 cs_p->nodetype = LYS_CASE;
Radek Krejci87e25252020-09-15 13:28:31 +02004013 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
Michal Vasko20424b42020-08-31 12:29:38 +02004014 cs_p->child = child_p;
4015
4016 /* make the child the only case child */
Michal Vasko20424b42020-08-31 12:29:38 +02004017 child_p->next = NULL;
4018
4019 /* compile it normally */
4020 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0);
4021
Radek Krejci87e25252020-09-15 13:28:31 +02004022free_fake_node:
Michal Vasko20424b42020-08-31 12:29:38 +02004023 /* free the fake parsed node and correct pointers back */
4024 cs_p->child = NULL;
4025 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
Radek Krejci8f5fad22020-09-15 16:50:54 +02004026 child_p->next = child_p_next;
Michal Vasko20424b42020-08-31 12:29:38 +02004027 }
4028
4029 return ret;
4030}
4031
4032/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004033 * @brief Compile parsed choice node information.
Michal Vasko20424b42020-08-31 12:29:38 +02004034 *
Radek Krejci056d0a82018-12-06 16:57:25 +01004035 * @param[in] ctx Compile context
4036 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004037 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004038 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004039 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4040 */
4041static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004042lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004043{
Michal Vasko22df3f02020-08-24 13:29:22 +02004044 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p;
4045 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
Michal Vasko20424b42020-08-31 12:29:38 +02004046 struct lysp_node *child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004047 LY_ERR ret = LY_SUCCESS;
4048
Michal Vasko20424b42020-08-31 12:29:38 +02004049 assert(node->nodetype == LYS_CHOICE);
4050
Radek Krejci056d0a82018-12-06 16:57:25 +01004051 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko20424b42020-08-31 12:29:38 +02004052 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node));
Radek Krejci056d0a82018-12-06 16:57:25 +01004053 }
4054
4055 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004056 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004057 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004058 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004059
Radek Krejci9800fb82018-12-13 14:26:23 +01004060 return ret;
4061}
4062
4063/**
4064 * @brief Compile parsed anydata or anyxml node information.
4065 * @param[in] ctx Compile context
4066 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004067 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4068 * is enriched with the any-specific information.
4069 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4070 */
4071static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004072lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004073{
Michal Vasko22df3f02020-08-24 13:29:22 +02004074 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p;
4075 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004076 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9800fb82018-12-13 14:26:23 +01004077 LY_ERR ret = LY_SUCCESS;
4078
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004079 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004080 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4081 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004082 ret = ly_set_add(&ctx->xpath, any, 0, NULL);
4083 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004084 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004085
4086 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004087 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4088 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004089 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004090done:
4091 return ret;
4092}
4093
Radek Krejcib56c7502019-02-13 14:19:54 +01004094/**
Michal Vasko795b3752020-07-13 15:24:27 +02004095 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4096 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004097 *
4098 * @param[in] ctx Compile context
4099 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4100 * the choice itself is expected instead of a specific case node.
4101 * @param[in] node Schema node to connect into the list.
4102 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004103 * 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 +01004104 */
4105static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004106lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004107{
Michal Vasko795b3752020-07-13 15:24:27 +02004108 struct lysc_node **children, *start, *end;
4109 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004110
Michal Vasko20424b42020-08-31 12:29:38 +02004111 node->parent = parent;
4112
4113 if (parent) {
4114 if (parent->nodetype == LYS_CHOICE) {
4115 assert(node->nodetype == LYS_CASE);
4116 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
4117 } else {
4118 children = lysc_node_children_p(parent, ctx->options);
4119 }
4120 assert(children);
4121
Radek Krejci056d0a82018-12-06 16:57:25 +01004122 if (!(*children)) {
4123 /* first child */
4124 *children = node;
4125 } else if (*children != node) {
4126 /* by the condition in previous branch we cover the choice/case children
4127 * - the children list is shared by the choice and the the first case, in addition
4128 * the first child of each case must be referenced from the case node. So the node is
4129 * actually always already inserted in case it is the first children - so here such
4130 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004131 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4132 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4133 /* some augments are already connected and we are connecting new ones,
4134 * keep module name order and insert the node into the children list */
4135 end = (*children);
4136 do {
4137 end = end->prev;
4138 mod = end->module;
4139 while (end->prev->module == mod) {
4140 end = end->prev;
4141 }
Radek Krejcif6a11002020-08-21 13:29:07 +02004142 } 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 +02004143
4144 /* we have the last existing node after our node, easily get the first before and connect it */
4145 start = end->prev;
4146 start->next = node;
4147 node->next = end;
4148 end->prev = node;
4149 node->prev = start;
4150 } else {
4151 /* insert at the end of the parent's children list */
4152 (*children)->prev->next = node;
4153 node->prev = (*children)->prev;
4154 (*children)->prev = node;
4155 }
Michal Vasko20424b42020-08-31 12:29:38 +02004156 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004157
Michal Vasko20424b42020-08-31 12:29:38 +02004158 /* check the name uniqueness (even for an only child, it may be in case) */
4159 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
4160 return LY_EEXIST;
4161 }
4162 } else {
4163 /* top-level element */
4164 if (!ctx->mod->compiled->data) {
4165 ctx->mod->compiled->data = node;
4166 } else {
4167 /* insert at the end of the module's top-level nodes list */
4168 ctx->mod->compiled->data->prev->next = node;
4169 node->prev = ctx->mod->compiled->data->prev;
4170 ctx->mod->compiled->data->prev = node;
4171 }
4172
4173 /* check the name uniqueness on top-level */
4174 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
4175 return LY_EEXIST;
Radek Krejci056d0a82018-12-06 16:57:25 +01004176 }
4177 }
Michal Vasko20424b42020-08-31 12:29:38 +02004178
Radek Krejci056d0a82018-12-06 16:57:25 +01004179 return LY_SUCCESS;
4180}
4181
Radek Krejci95710c92019-02-11 15:49:55 +01004182/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004183 * @brief Prepare the case structure in choice node for the new data node.
4184 *
4185 * 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
4186 * created in the choice when the first child was processed.
4187 *
4188 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004189 * @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 +02004190 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004191 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4192 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4193 * @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,
4194 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004195 */
Michal Vasko20424b42020-08-31 12:29:38 +02004196static LY_ERR
4197lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004198{
Michal Vasko20424b42020-08-31 12:29:38 +02004199 struct lysp_node *child_p;
4200 struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004201
Radek Krejci39424802020-08-12 09:31:00 +02004202 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004203 /* we have to add an implicit case node into the parent choice */
Radek Krejci95710c92019-02-11 15:49:55 +01004204 } else if (node_p->nodetype == LYS_CASE) {
Michal Vasko20424b42020-08-31 12:29:38 +02004205 /* explicit parent case */
4206 LY_LIST_FOR(cs_p->child, child_p) {
4207 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004208 }
Radek Krejci95710c92019-02-11 15:49:55 +01004209 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004210 LOGINT_RET(ctx->ctx);
Radek Krejci056d0a82018-12-06 16:57:25 +01004211 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004212
Michal Vasko20424b42020-08-31 12:29:38 +02004213 return LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01004214}
4215
Radek Krejcib56c7502019-02-13 14:19:54 +01004216/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004217 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004218 *
4219 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004220 * @param[in] node Target node where the config is supposed to be changed.
4221 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004222 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4223 * 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 +01004224 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4225 * @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 +01004226 * @return LY_ERR value.
4227 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004228static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004229lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci857189e2020-09-01 13:26:36 +02004230 ly_bool inheriting, ly_bool refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004231{
4232 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004233 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004234
4235 if (config == (node->flags & LYS_CONFIG_MASK)) {
4236 /* nothing to do */
4237 return LY_SUCCESS;
4238 }
4239
4240 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004241 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004242 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004244 "Invalid %s of config - configuration node cannot be child of any state data node.",
4245 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004246 return LY_EVALID;
4247 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004248 node->flags |= LYS_SET_CONFIG;
4249 } else {
4250 if (node->flags & LYS_SET_CONFIG) {
4251 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4252 /* setting config flags, but have node with explicit config true */
4253 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004254 "Invalid %s of config - configuration node cannot be child of any state data node.",
4255 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004256 return LY_EVALID;
4257 }
4258 /* do not change config on nodes where the config is explicitely set, this does not apply to
4259 * nodes, which are being changed explicitly (targets of refine or deviation) */
4260 return LY_SUCCESS;
4261 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004262 }
4263 node->flags &= ~LYS_CONFIG_MASK;
4264 node->flags |= config;
4265
4266 /* inherit the change into the children */
Michal Vasko22df3f02020-08-24 13:29:22 +02004267 LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004268 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004269 }
4270
Radek Krejci76b3e962018-12-14 17:01:25 +01004271 return LY_SUCCESS;
4272}
4273
Radek Krejcib56c7502019-02-13 14:19:54 +01004274/**
4275 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4276 *
4277 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4278 * the flag to such parents from a mandatory children.
4279 *
4280 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4281 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4282 * (mandatory children was removed).
4283 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02004284static void
Radek Krejci857189e2020-09-01 13:26:36 +02004285lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
Radek Krejcife909632019-02-12 15:34:42 +01004286{
4287 struct lysc_node *iter;
4288
4289 if (add) { /* set flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004290 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
Radek Krejcife909632019-02-12 15:34:42 +01004291 parent = parent->parent) {
4292 parent->flags |= LYS_MAND_TRUE;
4293 }
4294 } else { /* unset flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004295 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004296 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004297 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004298 /* there is another mandatory node */
4299 return;
4300 }
4301 }
4302 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4303 parent->flags &= ~LYS_MAND_TRUE;
4304 }
4305 }
4306}
4307
Radek Krejci056d0a82018-12-06 16:57:25 +01004308/**
Radek Krejci3641f562019-02-13 15:38:40 +01004309 * @brief Internal sorting process for the lys_compile_augment_sort().
4310 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4311 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4312 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4313 */
4314static void
4315lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4316{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004317 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004318 size_t len;
4319
4320 len = strlen(aug_p->nodeid);
4321 LY_ARRAY_FOR(result, v) {
4322 if (strlen(result[v]->nodeid) <= len) {
4323 continue;
4324 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004325 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004326 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004327 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004328 break;
4329 }
4330 }
4331 result[v] = aug_p;
4332 LY_ARRAY_INCREMENT(result);
4333}
4334
4335/**
4336 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4337 *
4338 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4339 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4340 *
4341 * @param[in] ctx Compile context.
4342 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4343 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4344 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4345 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4346 * @return LY_ERR value.
4347 */
4348LY_ERR
4349lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4350{
4351 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004352 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004353
4354 assert(augments);
4355
4356 /* get count of the augments in module and all its submodules */
4357 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004358 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004359 }
4360 LY_ARRAY_FOR(inc_p, u) {
4361 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004362 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004363 }
4364 }
4365
4366 if (!count) {
4367 *augments = NULL;
4368 return LY_SUCCESS;
4369 }
4370 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4371
4372 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4373 * together, so there can be even /z/y betwwen them. */
4374 LY_ARRAY_FOR(aug_p, u) {
4375 lys_compile_augment_sort_(&aug_p[u], result);
4376 }
4377 LY_ARRAY_FOR(inc_p, u) {
4378 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4379 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4380 }
4381 }
4382
4383 *augments = result;
4384 return LY_SUCCESS;
4385}
4386
4387/**
4388 * @brief Compile the parsed augment connecting it into its target.
4389 *
4390 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4391 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4392 * are already implemented and compiled.
4393 *
4394 * @param[in] ctx Compile context.
4395 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004396 * @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
4397 * children in case of the augmenting uses data.
4398 * @return LY_SUCCESS on success.
4399 * @return LY_EVALID on failure.
4400 */
4401LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004402lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004403{
Michal Vaskoe6143202020-07-03 13:02:08 +02004404 LY_ERR ret = LY_SUCCESS, rc;
Michal Vasko20424b42020-08-31 12:29:38 +02004405 struct lysp_node *node_p;
Radek Krejci3641f562019-02-13 15:38:40 +01004406 struct lysc_node *target; /* target target of the augment */
4407 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004408 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004409 struct lys_module **aug_mod;
Radek Krejci857189e2020-09-01 13:26:36 +02004410 ly_bool allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004411 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004412 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004413 uint32_t opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004414
Radek Krejci327de162019-06-14 12:52:07 +02004415 lysc_update_path(ctx, NULL, "{augment}");
4416 lysc_update_path(ctx, NULL, aug_p->nodeid);
4417
Michal Vaskocb18c7f2020-08-24 09:22:28 +02004418 ret = lysc_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004419 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Michal Vasko22df3f02020-08-24 13:29:22 +02004420 1, (const struct lysc_node **)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004421 if (ret != LY_SUCCESS) {
4422 if (ret == LY_EDENIED) {
4423 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4424 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4425 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4426 }
4427 return LY_EVALID;
4428 }
4429
4430 /* check for mandatory nodes
4431 * - new cases augmenting some choice can have mandatory nodes
4432 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4433 */
Radek Krejci733988a2019-02-15 15:12:44 +01004434 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004435 allow_mandatory = 1;
4436 }
4437
4438 when_shared = NULL;
4439 LY_LIST_FOR(aug_p->child, node_p) {
4440 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4441 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004442 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004443 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4444 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004445 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004446 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4447 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004448 return LY_EVALID;
4449 }
4450
4451 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004452 ctx->options |= flags;
Michal Vasko20424b42020-08-31 12:29:38 +02004453 if (target->nodetype == LYS_CHOICE) {
4454 LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target));
Radek Krejci3641f562019-02-13 15:38:40 +01004455 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004456 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004457 }
Radek Krejciec4da802019-05-02 13:02:41 +02004458 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004459
Radek Krejcife13da42019-02-15 14:51:01 +01004460 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4461 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004462 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004463 /* 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 +02004464 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 +01004465 } else if (target->nodetype == LYS_CHOICE) {
4466 /* 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 +02004467 node = ((struct lysc_node_choice *)target)->cases->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004468 } else {
4469 /* the compiled node is the last child of the target */
Michal Vasko22df3f02020-08-24 13:29:22 +02004470 node = (struct lysc_node *)lysc_node_children(target, flags);
Radek Krejci7c7783d2020-04-08 15:34:39 +02004471 if (!node) {
4472 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4473 break;
4474 }
4475 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004476 }
4477
Radek Krejci733988a2019-02-15 15:12:44 +01004478 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004479 node->flags &= ~LYS_MAND_TRUE;
4480 lys_compile_mandatory_parents(target, 0);
4481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004482 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004483 return LY_EVALID;
4484 }
4485
4486 /* pass augment's when to all the children */
4487 if (aug_p->when) {
4488 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4489 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004490 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004491 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004492
4493 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4494 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004495 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4496 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004497 }
4498
Radek Krejci3641f562019-02-13 15:38:40 +01004499 when_shared = *when;
4500 } else {
4501 ++when_shared->refcount;
4502 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004503
4504 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4505 /* in this case check "when" again for all children because of dummy node check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004506 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4507 LY_CHECK_GOTO(ret, error);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004508 }
Radek Krejci3641f562019-02-13 15:38:40 +01004509 }
4510 }
4511 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004512
Radek Krejciec4da802019-05-02 13:02:41 +02004513 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004514 switch (target->nodetype) {
4515 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004516 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004517 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004518 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004519 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004520 break;
4521 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004522 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004523 u, lys_compile_action, 0, ret, error);
Michal Vasko22df3f02020-08-24 13:29:22 +02004524 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004525 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004526 break;
4527 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004528 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004529 if (aug_p->actions) {
4530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004531 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4532 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004533 return LY_EVALID;
4534 }
4535 if (aug_p->notifs) {
4536 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004537 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004538 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004539 return LY_EVALID;
4540 }
4541 }
Radek Krejci3641f562019-02-13 15:38:40 +01004542
Michal Vaskoe6143202020-07-03 13:02:08 +02004543 /* add this module into the target module augmented_by, if not there already */
4544 rc = LY_SUCCESS;
4545 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4546 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4547 rc = LY_EEXIST;
4548 break;
4549 }
4550 }
4551 if (!rc) {
4552 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4553 *aug_mod = ctx->mod;
4554 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004555
Radek Krejci327de162019-06-14 12:52:07 +02004556 lysc_update_path(ctx, NULL, NULL);
4557 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004558
Radek Krejci3641f562019-02-13 15:38:40 +01004559error:
Radek Krejciec4da802019-05-02 13:02:41 +02004560 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004561 return ret;
4562}
4563
4564/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004565 * @brief Apply refined or deviated mandatory flag to the target node.
4566 *
4567 * @param[in] ctx Compile context.
4568 * @param[in] node Target node where the mandatory property is supposed to be changed.
4569 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004570 * @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 +01004571 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4572 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4573 * 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 +01004574 * @return LY_ERR value.
4575 */
4576static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004577lys_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 +01004578{
4579 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4580 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004581 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4582 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004583 return LY_EVALID;
4584 }
4585
4586 if (mandatory_flag & LYS_MAND_TRUE) {
4587 /* check if node has default value */
4588 if (node->nodetype & LYS_LEAF) {
4589 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004590 if (refine_flag) {
4591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004592 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004593 return LY_EVALID;
4594 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004595 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004596 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004597 if (refine_flag) {
4598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004599 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004600 return LY_EVALID;
4601 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004602 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004603 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004604 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 +01004605 return LY_EVALID;
4606 }
4607
4608 node->flags &= ~LYS_MAND_FALSE;
4609 node->flags |= LYS_MAND_TRUE;
4610 lys_compile_mandatory_parents(node->parent, 1);
4611 } else {
4612 /* make mandatory false */
4613 node->flags &= ~LYS_MAND_TRUE;
4614 node->flags |= LYS_MAND_FALSE;
4615 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcif1421c22019-02-19 13:05:20 +01004616 }
4617 return LY_SUCCESS;
4618}
4619
4620/**
Michal Vasko601ddb32020-08-31 16:25:35 +02004621 * @brief Find grouping for a uses.
Radek Krejcie86bf772018-12-14 11:39:53 +01004622 *
Michal Vasko601ddb32020-08-31 16:25:35 +02004623 * @param[in] ctx Compile context.
4624 * @param[in] uses_p Parsed uses node.
4625 * @param[out] gpr_p Found grouping on success.
4626 * @param[out] grp_mod Module of @p grp_p on success.
4627 * @return LY_ERR value.
Radek Krejcie86bf772018-12-14 11:39:53 +01004628 */
4629static LY_ERR
Michal Vasko601ddb32020-08-31 16:25:35 +02004630lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
4631 struct lys_module **grp_mod)
Radek Krejcie86bf772018-12-14 11:39:53 +01004632{
4633 struct lysp_node *node_p;
Michal Vasko601ddb32020-08-31 16:25:35 +02004634 struct lysp_grp *grp;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004635 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +02004636 ly_bool found = 0;
Radek Krejcie86bf772018-12-14 11:39:53 +01004637 const char *id, *name, *prefix;
4638 size_t prefix_len, name_len;
Michal Vasko601ddb32020-08-31 16:25:35 +02004639 struct lys_module *mod;
4640
4641 *grp_p = NULL;
4642 *grp_mod = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004643
4644 /* search for the grouping definition */
Radek Krejcie86bf772018-12-14 11:39:53 +01004645 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004646 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004647 if (prefix) {
4648 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4649 if (!mod) {
4650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004651 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004652 return LY_EVALID;
4653 }
4654 } else {
4655 mod = ctx->mod_def;
4656 }
4657 if (mod == ctx->mod_def) {
4658 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004659 grp = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004660 LY_ARRAY_FOR(grp, u) {
4661 if (!strcmp(grp[u].name, name)) {
4662 grp = &grp[u];
4663 found = 1;
4664 break;
4665 }
4666 }
4667 }
4668 }
4669 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004670 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004671 grp = mod->parsed->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004672 LY_ARRAY_FOR(grp, u) {
4673 if (!strcmp(grp[u].name, name)) {
4674 grp = &grp[u];
4675 found = 1;
4676 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004677 }
4678 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004679 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004680 /* ... and all the submodules */
Michal Vasko601ddb32020-08-31 16:25:35 +02004681 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004682 grp = mod->parsed->includes[u].submodule->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004683 LY_ARRAY_FOR(grp, v) {
4684 if (!strcmp(grp[v].name, name)) {
4685 grp = &grp[v];
4686 found = 1;
4687 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004688 }
4689 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004690 if (found) {
4691 break;
4692 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004693 }
4694 }
4695 }
4696 if (!found) {
4697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4698 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4699 return LY_EVALID;
4700 }
4701
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004702 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4703 /* remember that the grouping is instantiated to avoid its standalone validation */
4704 grp->flags |= LYS_USED_GRP;
4705 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004706
Michal Vasko601ddb32020-08-31 16:25:35 +02004707 *grp_p = grp;
4708 *grp_mod = mod;
4709 return LY_SUCCESS;
4710}
Radek Krejcie86bf772018-12-14 11:39:53 +01004711
Michal Vasko601ddb32020-08-31 16:25:35 +02004712static LY_ERR
4713lys_compile_refines(struct lysc_ctx *ctx, struct lysp_refine *refines, const struct lysc_node *context_node)
4714{
4715 struct lysc_node *node;
4716 LY_ARRAY_COUNT_TYPE u;
4717 struct lysp_refine *rfn;
4718 LY_ERR ret = LY_SUCCESS;
4719 uint32_t min, max;
4720 uint16_t flags;
4721 struct ly_set refined = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01004722
Radek Krejci327de162019-06-14 12:52:07 +02004723 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004724
Radek Krejci76b3e962018-12-14 17:01:25 +01004725 /* apply refine */
Michal Vasko601ddb32020-08-31 16:25:35 +02004726 LY_ARRAY_FOR(refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004727 lysc_update_path(ctx, NULL, rfn->nodeid);
4728
Michal Vasko601ddb32020-08-31 16:25:35 +02004729 ret = lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, context_node, ctx->mod,
Michal Vasko20424b42020-08-31 12:29:38 +02004730 0, 0, (const struct lysc_node **)&node, &flags);
4731 LY_CHECK_GOTO(ret, cleanup);
4732 ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL);
4733 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004734
4735 /* default value */
4736 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004737 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci0f969882020-08-21 16:56:47 +02004739 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE " default values.",
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004740 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Michal Vasko20424b42020-08-31 12:29:38 +02004741 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004742 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004743 }
4744 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004746 "Invalid refine of default - %s cannot hold default value(s).",
4747 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004748 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004749 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004750 }
4751 if (node->nodetype == LYS_LEAF) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004752 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004753 ret = lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], ctx->mod_def);
4754 LY_CHECK_GOTO(ret, cleanup);
4755
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004756 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004757 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004758 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4760 "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 +02004761 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004762 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004763 }
Radek Krejcia1911222019-07-22 17:24:50 +02004764
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004765 /* postpone default compilation when the tree is complete */
Michal Vasko20424b42020-08-31 12:29:38 +02004766 ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def);
4767 LY_CHECK_GOTO(ret, cleanup);
4768
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004769 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004770 } else if (node->nodetype == LYS_CHOICE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004771 if (((struct lysc_node_choice *)node)->dflt) {
Radek Krejci01342af2019-01-03 15:18:08 +01004772 /* unset LYS_SET_DFLT from the current default case */
Michal Vasko22df3f02020-08-24 13:29:22 +02004773 ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT;
Radek Krejci01342af2019-01-03 15:18:08 +01004774 }
Michal Vasko20424b42020-08-31 12:29:38 +02004775 ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node);
4776 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004777 }
4778 }
4779
Radek Krejci12fb9142019-01-08 09:45:30 +01004780 /* description */
4781 if (rfn->dsc) {
4782 FREE_STRING(ctx->ctx, node->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004783 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->dsc, 0, &node->dsc), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004784 }
4785
4786 /* reference */
4787 if (rfn->ref) {
4788 FREE_STRING(ctx->ctx, node->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02004789 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->ref, 0, &node->ref), cleanup);
Radek Krejci12fb9142019-01-08 09:45:30 +01004790 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004791
4792 /* config */
4793 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004794 if (!flags) {
Michal Vasko20424b42020-08-31 12:29:38 +02004795 ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1);
4796 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004797 } else {
4798 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004799 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004800 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004801 }
4802
4803 /* mandatory */
4804 if (rfn->flags & LYS_MAND_MASK) {
Michal Vasko20424b42020-08-31 12:29:38 +02004805 ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1);
4806 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004807 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004808
4809 /* presence */
4810 if (rfn->presence) {
4811 if (node->nodetype != LYS_CONTAINER) {
4812 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004813 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4814 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004815 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004816 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004817 }
4818 node->flags |= LYS_PRESENCE;
4819 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004820
4821 /* must */
4822 if (rfn->musts) {
4823 switch (node->nodetype) {
4824 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02004825 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 +01004826 break;
4827 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004828 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 +01004829 break;
4830 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02004831 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 +01004832 break;
4833 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02004834 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 +01004835 break;
4836 case LYS_ANYXML:
4837 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02004838 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 +01004839 break;
4840 default:
4841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004842 "Invalid refine of must statement - %s cannot hold any must statement.",
4843 lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004844 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004845 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004846 }
Michal Vasko20424b42020-08-31 12:29:38 +02004847 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
4848 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004849 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004850
4851 /* min/max-elements */
4852 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4853 switch (node->nodetype) {
4854 case LYS_LEAFLIST:
4855 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004856 ((struct lysc_node_leaflist *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004857 }
4858 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004859 ((struct lysc_node_leaflist *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004860 if (rfn->min) {
4861 node->flags |= LYS_MAND_TRUE;
4862 lys_compile_mandatory_parents(node->parent, 1);
4863 } else {
4864 node->flags &= ~LYS_MAND_TRUE;
4865 lys_compile_mandatory_parents(node->parent, 0);
4866 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004867 }
4868 break;
4869 case LYS_LIST:
4870 if (rfn->flags & LYS_SET_MAX) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004871 ((struct lysc_node_list *)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004872 }
4873 if (rfn->flags & LYS_SET_MIN) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004874 ((struct lysc_node_list *)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004875 if (rfn->min) {
4876 node->flags |= LYS_MAND_TRUE;
4877 lys_compile_mandatory_parents(node->parent, 1);
4878 } else {
4879 node->flags &= ~LYS_MAND_TRUE;
4880 lys_compile_mandatory_parents(node->parent, 0);
4881 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004882 }
4883 break;
4884 default:
4885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004886 "Invalid refine of %s statement - %s cannot hold this statement.",
Radek Krejci0f969882020-08-21 16:56:47 +02004887 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Michal Vasko20424b42020-08-31 12:29:38 +02004888 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004889 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004890 }
4891 }
Radek Krejcif0089082019-01-07 16:42:01 +01004892
4893 /* if-feature */
4894 if (rfn->iffeatures) {
4895 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004896 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004897 }
Radek Krejci327de162019-06-14 12:52:07 +02004898
4899 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004900 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004901
Radek Krejcif2271f12019-01-07 16:42:23 +01004902 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004903 for (uint32_t i = 0; i < refined.count; ++i) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004904 node = (struct lysc_node *)refined.objs[i];
Michal Vasko601ddb32020-08-31 16:25:35 +02004905 rfn = &refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02004906 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004907
4908 /* check possible conflict with default value (default added, mandatory left true) */
4909 if ((node->flags & LYS_MAND_TRUE) &&
Michal Vasko22df3f02020-08-24 13:29:22 +02004910 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) ||
Radek Krejcif2271f12019-01-07 16:42:23 +01004911 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4912 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004913 "Invalid refine of default - the node is mandatory.");
Michal Vasko20424b42020-08-31 12:29:38 +02004914 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004915 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004916 }
4917
4918 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4919 if (node->nodetype == LYS_LIST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004920 min = ((struct lysc_node_list *)node)->min;
4921 max = ((struct lysc_node_list *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004922 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02004923 min = ((struct lysc_node_leaflist *)node)->min;
4924 max = ((struct lysc_node_leaflist *)node)->max;
Radek Krejcif2271f12019-01-07 16:42:23 +01004925 }
4926 if (min > max) {
4927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004928 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
Radek Krejci0f969882020-08-21 16:56:47 +02004929 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Michal Vasko20424b42020-08-31 12:29:38 +02004930 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004931 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004932 }
4933 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004934
4935 lysc_update_path(ctx, NULL, NULL);
Radek Krejcif2271f12019-01-07 16:42:23 +01004936 }
4937
Michal Vasko601ddb32020-08-31 16:25:35 +02004938cleanup:
4939 ly_set_erase(&refined, NULL);
4940 lysc_update_path(ctx, NULL, NULL);
4941 return ret;
4942}
4943
4944/**
4945 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4946 * If present, also apply uses's modificators.
4947 *
4948 * @param[in] ctx Compile context
4949 * @param[in] uses_p Parsed uses schema node.
4950 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4951 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4952 * the compile context.
4953 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4954 */
4955static LY_ERR
4956lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p)
4957{
4958 struct lysp_node *node_p;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004959 struct lysc_node *child = NULL, *iter;
Michal Vasko601ddb32020-08-31 16:25:35 +02004960 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
4961 struct lysc_node_container context_node_fake =
4962 {.nodetype = LYS_CONTAINER,
4963 .module = ctx->mod,
4964 .flags = parent ? parent->flags : 0,
4965 .child = NULL, .next = NULL,
4966 .prev = (struct lysc_node *)&context_node_fake,
4967 .actions = NULL, .notifs = NULL};
4968 struct lysp_grp *grp = NULL;
4969 LY_ARRAY_COUNT_TYPE u;
4970 uint32_t grp_stack_count;
4971 struct lys_module *grp_mod, *mod_old;
4972 LY_ERR ret = LY_SUCCESS;
4973 struct lysc_when **when, *when_shared;
4974 struct lysp_augment **augments = NULL;
4975 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
4976 struct lysc_notif **notifs = NULL;
4977 struct lysc_action **actions = NULL;
4978
4979 /* find the referenced grouping */
4980 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
4981
4982 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4983 grp_stack_count = ctx->groupings.count;
4984 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
4985 if (grp_stack_count == ctx->groupings.count) {
4986 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4988 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4989 return LY_EVALID;
4990 }
4991
4992 /* switch context's mod_def */
4993 mod_old = ctx->mod_def;
4994 ctx->mod_def = grp_mod;
4995
4996 /* check status */
4997 ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, grp_mod, grp->name);
4998 LY_CHECK_GOTO(ret, cleanup);
4999
5000 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
5001 * applu refine and augment only to the nodes from the uses */
5002 if (parent) {
5003 child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5004 } else if (ctx->mod->compiled->data) {
5005 child = ctx->mod->compiled->data;
5006 } else {
5007 child = NULL;
5008 }
5009 /* remember the last child */
5010 if (child) {
5011 child = child->prev;
5012 }
5013
5014 /* compile data nodes */
5015 LY_LIST_FOR(grp->data, node_p) {
5016 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
5017 ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3);
5018 LY_CHECK_GOTO(ret, cleanup);
5019 }
5020
5021 /* split the children and add the uses's data into the fake context node */
5022 if (child) {
5023 context_node_fake.child = child->next;
5024 } else if (parent) {
5025 context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5026 } else if (ctx->mod->compiled->data) {
5027 context_node_fake.child = ctx->mod->compiled->data;
5028 }
5029 if (context_node_fake.child) {
5030 /* remember child as the last data node added by grouping to fix the list later */
5031 child = context_node_fake.child->prev;
5032 context_node_fake.child->prev = NULL;
5033 }
5034
5035 when_shared = NULL;
5036 LY_LIST_FOR(context_node_fake.child, iter) {
5037 iter->parent = (struct lysc_node *)&context_node_fake;
5038
5039 /* pass uses's when to all the data children, actions and notifications are ignored */
5040 if (uses_p->when) {
5041 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
5042 if (!when_shared) {
5043 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when);
5044 LY_CHECK_GOTO(ret, cleanup);
5045
5046 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5047 /* do not check "when" semantics in a grouping */
5048 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5049 LY_CHECK_GOTO(ret, cleanup);
5050 }
5051
5052 when_shared = *when;
5053 } else {
5054 ++when_shared->refcount;
5055 (*when) = when_shared;
5056
5057 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5058 /* in this case check "when" again for all children because of dummy node check */
5059 ret = ly_set_add(&ctx->xpath, iter, 0, NULL);
5060 LY_CHECK_GOTO(ret, cleanup);
5061 }
5062 }
5063 }
5064 }
5065
5066 /* compile actions */
5067 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5068 if (actions) {
5069 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
5070 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
5071 if (*actions && (uses_p->augments || uses_p->refines)) {
5072 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5073 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
5074 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
5075 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
5076 }
5077 }
5078
5079 /* compile notifications */
5080 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5081 if (notifs) {
5082 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
5083 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
5084 if (*notifs && (uses_p->augments || uses_p->refines)) {
5085 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5086 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
5087 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
5088 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
5089 }
5090 }
5091
5092 /* sort and apply augments */
5093 ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments);
5094 LY_CHECK_GOTO(ret, cleanup);
5095 LY_ARRAY_FOR(augments, u) {
5096 ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake);
5097 LY_CHECK_GOTO(ret, cleanup);
5098 }
5099
5100 /* reload previous context's mod_def */
5101 ctx->mod_def = mod_old;
5102
5103 /* apply all refines */
5104 ret = lys_compile_refines(ctx, uses_p->refines, (struct lysc_node *)&context_node_fake);
5105 LY_CHECK_GOTO(ret, cleanup);
5106
Radek Krejcic6b4f442020-08-12 14:45:18 +02005107 if (first_p) {
5108 *first_p = context_node_fake.child;
5109 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005110
5111cleanup:
5112 /* fix connection of the children nodes from fake context node back into the parent */
5113 if (context_node_fake.child) {
5114 context_node_fake.child->prev = child;
5115 }
5116 LY_LIST_FOR(context_node_fake.child, child) {
5117 child->parent = parent;
5118 }
5119
5120 if (uses_p->augments || uses_p->refines) {
5121 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005122 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005123 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005124 LY_ARRAY_FREE(context_node_fake.actions);
5125 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005126 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005127 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005128 LY_ARRAY_FREE(context_node_fake.notifs);
5129 }
5130 }
5131
Radek Krejcie86bf772018-12-14 11:39:53 +01005132 /* reload previous context's mod_def */
5133 ctx->mod_def = mod_old;
5134 /* remove the grouping from the stack for circular groupings dependency check */
5135 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5136 assert(ctx->groupings.count == grp_stack_count);
Radek Krejci3641f562019-02-13 15:38:40 +01005137 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005138
5139 return ret;
5140}
5141
Radek Krejci327de162019-06-14 12:52:07 +02005142static int
5143lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5144{
5145 struct lysp_node *iter;
5146 int len = 0;
5147
5148 *path = NULL;
5149 for (iter = node; iter && len >= 0; iter = iter->parent) {
5150 char *s = *path;
5151 char *id;
5152
5153 switch (iter->nodetype) {
5154 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005155 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005156 break;
5157 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005158 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005159 break;
5160 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005161 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005162 break;
5163 default:
5164 id = strdup(iter->name);
5165 break;
5166 }
5167
5168 if (!iter->parent) {
5169 /* print prefix */
5170 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5171 } else {
5172 /* prefix is the same as in parent */
5173 len = asprintf(path, "/%s%s", id, s ? s : "");
5174 }
5175 free(s);
5176 free(id);
5177 }
5178
5179 if (len < 0) {
5180 free(*path);
5181 *path = NULL;
5182 } else if (len == 0) {
5183 *path = strdup("/");
5184 len = 1;
5185 }
5186 return len;
5187}
5188
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005189/**
5190 * @brief Validate groupings that were defined but not directly used in the schema itself.
5191 *
5192 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5193 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5194 */
5195static LY_ERR
5196lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5197{
5198 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005199 char *path;
5200 int len;
5201
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005202 struct lysp_node_uses fake_uses = {
5203 .parent = node_p,
5204 .nodetype = LYS_USES,
5205 .flags = 0, .next = NULL,
5206 .name = grp->name,
5207 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5208 .refines = NULL, .augments = NULL
5209 };
5210 struct lysc_node_container fake_container = {
5211 .nodetype = LYS_CONTAINER,
5212 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5213 .module = ctx->mod,
5214 .sp = NULL, .parent = NULL, .next = NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +02005215 .prev = (struct lysc_node *)&fake_container,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005216 .name = "fake",
5217 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5218 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5219 };
5220
5221 if (grp->parent) {
5222 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5223 }
Radek Krejci327de162019-06-14 12:52:07 +02005224
5225 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5226 if (len < 0) {
5227 LOGMEM(ctx->ctx);
5228 return LY_EMEM;
5229 }
5230 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005231 ctx->path_len = (uint32_t)len;
Radek Krejci327de162019-06-14 12:52:07 +02005232 free(path);
5233
5234 lysc_update_path(ctx, NULL, "{grouping}");
5235 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02005236 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005237 lysc_update_path(ctx, NULL, NULL);
5238 lysc_update_path(ctx, NULL, NULL);
5239
5240 ctx->path_len = 1;
5241 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005242
5243 /* cleanup */
5244 lysc_node_container_free(ctx->ctx, &fake_container);
5245
5246 return ret;
5247}
Radek Krejcife909632019-02-12 15:34:42 +01005248
Radek Krejcie86bf772018-12-14 11:39:53 +01005249/**
Michal Vasko20424b42020-08-31 12:29:38 +02005250 * @brief Set config flags for a node.
5251 *
5252 * @param[in] ctx Compile context.
5253 * @param[in] node Compiled node config to set.
5254 * @param[in] parent Parent of @p node.
5255 * @return LY_ERR value.
5256 */
5257static LY_ERR
5258lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
5259{
5260 if (node->nodetype == LYS_CASE) {
5261 /* case never has any config */
5262 assert(!(node->flags & LYS_CONFIG_MASK));
5263 return LY_SUCCESS;
5264 }
5265
5266 /* adjust parent to always get the ancestor with config */
5267 if (parent && (parent->nodetype == LYS_CASE)) {
5268 parent = parent->parent;
5269 assert(parent);
5270 }
5271
5272 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
5273 /* ignore config statements inside RPC/action data */
5274 node->flags &= ~LYS_CONFIG_MASK;
5275 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5276 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
5277 /* ignore config statements inside Notification data */
5278 node->flags &= ~LYS_CONFIG_MASK;
5279 node->flags |= LYS_CONFIG_R;
5280 } else if (!(node->flags & LYS_CONFIG_MASK)) {
5281 /* config not explicitely set, inherit it from parent */
5282 if (parent) {
5283 node->flags |= parent->flags & LYS_CONFIG_MASK;
5284 } else {
5285 /* default is config true */
5286 node->flags |= LYS_CONFIG_W;
5287 }
5288 } else {
5289 /* config set explicitely */
5290 node->flags |= LYS_SET_CONFIG;
5291 }
5292
5293 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5294 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5295 "Configuration node cannot be child of any state data node.");
5296 return LY_EVALID;
5297 }
5298
5299 return LY_SUCCESS;
5300}
5301
5302/**
Radek Krejcia3045382018-11-22 14:30:31 +01005303 * @brief Compile parsed schema node information.
5304 * @param[in] ctx Compile context
5305 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005306 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5307 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5308 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005309 * @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).
5310 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005311 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5312 */
Radek Krejci19a96102018-11-15 13:38:09 +01005313static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005314lys_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 +01005315{
Radek Krejci1c54f462020-05-12 17:25:34 +02005316 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005317 struct lysc_node *node = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005318 struct lysc_when **when;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005319 LY_ARRAY_COUNT_TYPE u;
Michal Vasko22df3f02020-08-24 13:29:22 +02005320 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
Radek Krejci19a96102018-11-15 13:38:09 +01005321
Radek Krejci327de162019-06-14 12:52:07 +02005322 if (node_p->nodetype != LYS_USES) {
5323 lysc_update_path(ctx, parent, node_p->name);
5324 } else {
5325 lysc_update_path(ctx, NULL, "{uses}");
5326 lysc_update_path(ctx, NULL, node_p->name);
5327 }
5328
Radek Krejci19a96102018-11-15 13:38:09 +01005329 switch (node_p->nodetype) {
5330 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02005331 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
Radek Krejci19a96102018-11-15 13:38:09 +01005332 node_compile_spec = lys_compile_node_container;
5333 break;
5334 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005335 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
Radek Krejci19a96102018-11-15 13:38:09 +01005336 node_compile_spec = lys_compile_node_leaf;
5337 break;
5338 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005339 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005340 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005341 break;
5342 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005343 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005344 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005345 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005346 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02005347 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005348 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005349 break;
Michal Vasko20424b42020-08-31 12:29:38 +02005350 case LYS_CASE:
5351 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
5352 node_compile_spec = lys_compile_node_case;
5353 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005354 case LYS_ANYXML:
5355 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005356 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005357 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005358 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005359 case LYS_USES:
Michal Vasko9e8de2d2020-09-01 08:17:10 +02005360 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, parent, &node);
Radek Krejci327de162019-06-14 12:52:07 +02005361 lysc_update_path(ctx, NULL, NULL);
5362 lysc_update_path(ctx, NULL, NULL);
5363 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005364 default:
5365 LOGINT(ctx->ctx);
5366 return LY_EINT;
5367 }
5368 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5369 node->nodetype = node_p->nodetype;
5370 node->module = ctx->mod;
5371 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005372 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005373
5374 /* config */
Michal Vasko20424b42020-08-31 12:29:38 +02005375 ret = lys_compile_config(ctx, node, parent);
5376 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005377
Michal Vasko20424b42020-08-31 12:29:38 +02005378 /* list ordering */
Radek Krejcia6d57732018-11-29 13:40:37 +01005379 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5380 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005381 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci0f969882020-08-21 16:56:47 +02005382 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5383 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005384 node->flags &= ~LYS_ORDBY_MASK;
5385 node->flags |= LYS_ORDBY_SYSTEM;
5386 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5387 /* default ordering is system */
5388 node->flags |= LYS_ORDBY_SYSTEM;
5389 }
5390 }
5391
Radek Krejci19a96102018-11-15 13:38:09 +01005392 /* status - it is not inherited by specification, but it does not make sense to have
5393 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko20424b42020-08-31 12:29:38 +02005394 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 +01005395
Radek Krejciec4da802019-05-02 13:02:41 +02005396 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005397 node->sp = node_p;
5398 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02005399 DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error);
5400 DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error);
5401 DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error);
Radek Krejci00b874b2019-02-12 10:54:50 +01005402 if (node_p->when) {
5403 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005404 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005405
5406 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5407 /* do not check "when" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02005408 ret = ly_set_add(&ctx->xpath, node, 0, NULL);
5409 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005410 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005411 }
Radek Krejciec4da802019-05-02 13:02:41 +02005412 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005413
Michal Vasko20424b42020-08-31 12:29:38 +02005414 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
5415 LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node));
5416
Radek Krejci19a96102018-11-15 13:38:09 +01005417 /* nodetype-specific part */
Michal Vasko20424b42020-08-31 12:29:38 +02005418 LY_CHECK_RET(node_compile_spec(ctx, node_p, node));
Radek Krejci19a96102018-11-15 13:38:09 +01005419
Michal Vasko20424b42020-08-31 12:29:38 +02005420 /* final compilation tasks that require the node to be connected */
5421 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, done);
Radek Krejcife909632019-02-12 15:34:42 +01005422 if (node->flags & LYS_MAND_TRUE) {
Michal Vasko20424b42020-08-31 12:29:38 +02005423 /* inherit LYS_MAND_TRUE in parent containers */
Radek Krejcife909632019-02-12 15:34:42 +01005424 lys_compile_mandatory_parents(parent, 1);
5425 }
5426
Radek Krejci327de162019-06-14 12:52:07 +02005427 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005428 return LY_SUCCESS;
5429
5430error:
5431 lysc_node_free(ctx->ctx, node);
Michal Vasko20424b42020-08-31 12:29:38 +02005432done:
Radek Krejci19a96102018-11-15 13:38:09 +01005433 return ret;
5434}
5435
Radek Krejciccd20f12019-02-15 14:12:27 +01005436static void
Michal Vasko20424b42020-08-31 12:29:38 +02005437lysc_node_unlink(struct lysc_node *node)
Radek Krejciccd20f12019-02-15 14:12:27 +01005438{
Michal Vasko20424b42020-08-31 12:29:38 +02005439 struct lysc_node *parent, *child;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005440 struct lysc_module *modc = node->module->compiled;
Radek Krejciccd20f12019-02-15 14:12:27 +01005441
5442 parent = node->parent;
5443
5444 /* parent's first child */
Michal Vasko20424b42020-08-31 12:29:38 +02005445 if (parent && lysc_node_children(parent, node->flags) == node) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005446 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005447 } else if (modc->data == node) {
5448 modc->data = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005449 }
Michal Vasko20424b42020-08-31 12:29:38 +02005450
5451 /* special choice case unlinking */
5452 if (parent && parent->nodetype == LYS_CHOICE) {
5453 if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) {
5454 /* default case removed */
5455 ((struct lysc_node_choice *)parent)->dflt = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005456 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005457 }
5458
5459 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005460 if (node->prev->next) {
5461 node->prev->next = node->next;
5462 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005463 if (node->next) {
5464 node->next->prev = node->prev;
Michal Vasko20424b42020-08-31 12:29:38 +02005465 } else {
5466 /* last child */
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005467 if (parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005468 child = (struct lysc_node *)lysc_node_children(parent, node->flags);
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005469 } else {
5470 child = modc->data;
5471 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005472 if (child) {
5473 child->prev = node->prev;
5474 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005475 }
5476}
5477
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005478struct lysc_deviation {
5479 const char *nodeid;
5480 struct lysc_node *target; /* target node of the deviation */
Michal Vasko22df3f02020-08-24 13:29:22 +02005481 struct lysp_deviate **deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02005482 uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */
Radek Krejci857189e2020-09-01 13:26:36 +02005483 ly_bool not_supported; /* flag if deviates contains not-supported deviate */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005484};
5485
Michal Vaskoccc062a2020-08-13 08:34:50 +02005486/* MACROS for deviates checking */
5487#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5488 if (!(target->nodetype & (NODETYPES))) { \
5489 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5490 goto cleanup; \
5491 }
5492
5493#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5494 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5495 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5496 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5497 goto cleanup; \
5498 }
5499
5500#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5501 if (!((TYPE)target)->MEMBER || COND) { \
5502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5503 goto cleanup; \
5504 }
5505
5506/**
5507 * @brief Apply deviate add.
5508 *
5509 * @param[in] ctx Compile context.
5510 * @param[in] target Deviation target.
5511 * @param[in] dev_flags Internal deviation flags.
5512 * @param[in] d Deviate add to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02005513 * @return LY_ERR value.
5514 */
5515static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005516lys_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 +02005517{
Radek Krejci011e4aa2020-09-04 15:22:31 +02005518 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005519 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5520 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005521 LY_ARRAY_COUNT_TYPE x;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005522
5523#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5524 if (((TYPE)target)->MEMBER COND) { \
5525 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5526 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5527 PROPERTY, ((TYPE)target)->MEMBER); \
5528 goto cleanup; \
5529 }
5530
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005531#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005532 if (((TYPE)target)->MEMBER && (COND)) { \
Radek Krejci857189e2020-09-01 13:26:36 +02005533 ly_bool dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005534 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005535 ctx->mod_def, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005536 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5537 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5538 if (dynamic_) {free((void*)val_);} \
5539 goto cleanup; \
5540 }
5541
5542#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5543 if (((TYPE)target)->MEMBER && (COND)) { \
5544 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5545 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5546 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5547 goto cleanup; \
5548 }
5549
5550 /* [units-stmt] */
5551 if (d->units) {
5552 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5553 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5554
5555 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02005556 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc);
5557 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005558 }
5559
5560 /* *must-stmt */
5561 if (d->musts) {
5562 switch (target->nodetype) {
5563 case LYS_CONTAINER:
5564 case LYS_LIST:
5565 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5566 x, lys_compile_must, ret, cleanup);
5567 break;
5568 case LYS_LEAF:
5569 case LYS_LEAFLIST:
5570 case LYS_ANYDATA:
5571 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5572 x, lys_compile_must, ret, cleanup);
5573 break;
5574 case LYS_NOTIF:
5575 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5576 x, lys_compile_must, ret, cleanup);
5577 break;
5578 case LYS_RPC:
5579 case LYS_ACTION:
5580 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5581 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5582 x, lys_compile_must, ret, cleanup);
5583 break;
5584 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5585 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5586 x, lys_compile_must, ret, cleanup);
5587 break;
5588 }
Radek Krejci0f969882020-08-21 16:56:47 +02005589 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005590 default:
5591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5592 lys_nodetype2str(target->nodetype), "add", "must");
5593 goto cleanup;
5594 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02005595 ret = ly_set_add(&ctx->xpath, target, 0, NULL);
5596 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005597 }
5598
5599 /* *unique-stmt */
5600 if (d->uniques) {
5601 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5602 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5603 }
5604
5605 /* *default-stmt */
5606 if (d->dflts) {
5607 switch (target->nodetype) {
5608 case LYS_LEAF:
5609 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005610 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005611 if (leaf->dflt) {
5612 /* first, remove the default value taken from the type */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005613 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5614 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005615 free(leaf->dflt);
5616 leaf->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005617 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005618
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005619 /* store the default value in unres */
Michal Vasko31a82d52020-08-21 08:11:48 +02005620 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005621 target->flags |= LYS_SET_DFLT;
5622 break;
5623 case LYS_LEAFLIST:
5624 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5625 /* first, remove the default value taken from the type */
5626 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005627 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5628 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5629 free(llist->dflts[x]);
5630 }
5631 LY_ARRAY_FREE(llist->dflts);
5632 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005633 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005634
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005635 /* store the default values in unres */
5636 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005637 target->flags |= LYS_SET_DFLT;
5638 break;
5639 case LYS_CHOICE:
5640 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5641 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5642 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5643 * to allow making the default case even the augmented case from the deviating module */
5644 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5645 goto cleanup;
5646 }
5647 break;
5648 default:
5649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5650 lys_nodetype2str(target->nodetype), "add", "default");
5651 goto cleanup;
5652 }
5653 }
5654
5655 /* [config-stmt] */
5656 if (d->flags & LYS_CONFIG_MASK) {
5657 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5658 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5659 lys_nodetype2str(target->nodetype), "add", "config");
5660 goto cleanup;
5661 }
5662 if (dev_flags) {
5663 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5664 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5665 }
5666 if (target->flags & LYS_SET_CONFIG) {
5667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5668 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5669 target->flags & LYS_CONFIG_W ? "true" : "false");
5670 goto cleanup;
5671 }
5672 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5673 }
5674
5675 /* [mandatory-stmt] */
5676 if (d->flags & LYS_MAND_MASK) {
5677 if (target->flags & LYS_MAND_MASK) {
5678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5679 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5680 target->flags & LYS_MAND_TRUE ? "true" : "false");
5681 goto cleanup;
5682 }
5683 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5684 }
5685
5686 /* [min-elements-stmt] */
5687 if (d->flags & LYS_SET_MIN) {
5688 if (target->nodetype == LYS_LEAFLIST) {
5689 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5690 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005691 ((struct lysc_node_leaflist *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005692 } else if (target->nodetype == LYS_LIST) {
5693 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5694 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005695 ((struct lysc_node_list *)target)->min = d->min;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005696 } else {
5697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5698 lys_nodetype2str(target->nodetype), "add", "min-elements");
5699 goto cleanup;
5700 }
5701 if (d->min) {
5702 target->flags |= LYS_MAND_TRUE;
5703 }
5704 }
5705
5706 /* [max-elements-stmt] */
5707 if (d->flags & LYS_SET_MAX) {
5708 if (target->nodetype == LYS_LEAFLIST) {
5709 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5710 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005711 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005712 } else if (target->nodetype == LYS_LIST) {
5713 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5714 /* change value */
Michal Vasko22df3f02020-08-24 13:29:22 +02005715 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005716 } else {
5717 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5718 lys_nodetype2str(target->nodetype), "add", "max-elements");
5719 goto cleanup;
5720 }
5721 }
5722
5723 ret = LY_SUCCESS;
5724
5725cleanup:
5726 return ret;
5727}
5728
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005729static LY_ERR
5730lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt)
5731{
5732 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005733 ly_bool dyn = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005734 const char *orig_dflt;
5735 uint32_t i;
5736
5737 if (target->module != ctx->mod) {
5738 /* foreign deviation */
5739 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt);
5740
5741 /* check that the value matches */
5742 orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5743 if (strcmp(orig_dflt, dflt)) {
5744 goto error;
5745 }
5746 if (dyn) {
5747 free((char *)orig_dflt);
5748 }
5749
5750 /* remove the default specification */
5751 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5752 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5753 free(leaf->dflt);
5754 leaf->dflt = NULL;
5755 } else {
5756 /* local deviation */
5757 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt);
5758
5759 /* find the incomplete default */
5760 orig_dflt = NULL;
5761 for (i = 0; i < ctx->dflts.count; ++i) {
5762 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
5763 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5764 break;
5765 }
5766 }
5767 LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT);
5768
5769 /* check that the value matches */
5770 if (strcmp(orig_dflt, dflt)) {
5771 goto error;
5772 }
5773
5774 /* update the list of incomplete default values */
5775 lysc_incomplete_dflt_remove(ctx, target);
5776 }
5777
5778 target->flags &= ~LYS_SET_DFLT;
5779 return LY_SUCCESS;
5780
5781error:
5782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5783 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5784 dflt, orig_dflt);
5785 if (dyn) {
5786 free((char *)orig_dflt);
5787 }
5788cleanup:
5789 return LY_EVALID;
5790}
5791
5792static LY_ERR
5793lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts)
5794{
5795 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Radek Krejci857189e2020-09-01 13:26:36 +02005796 ly_bool dyn = 0, found;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005797 const char *orig_dflt, **orig_dflts;
5798 uint32_t i;
5799 LY_ARRAY_COUNT_TYPE x, y;
5800
5801 if (target->module != ctx->mod) {
5802 /* foreign deviation */
5803 DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]);
5804 LY_ARRAY_FOR(dflts, x) {
5805 found = 0;
5806 LY_ARRAY_FOR(llist->dflts, y) {
5807 orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5808 if (!strcmp(orig_dflt, dflts[x])) {
5809 if (dyn) {
5810 free((char *)orig_dflt);
5811 }
5812 found = 1;
5813 break;
5814 }
5815 if (dyn) {
5816 free((char *)orig_dflt);
5817 }
5818 }
5819 if (!found) {
5820 goto error;
5821 }
5822
5823 /* update compiled default values */
5824 LY_ARRAY_DECREMENT(llist->dflts);
5825 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
5826 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
5827 free(llist->dflts[y]);
5828 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
5829 }
5830 if (!LY_ARRAY_COUNT(llist->dflts)) {
5831 LY_ARRAY_FREE(llist->dflts);
5832 llist->dflts = NULL;
5833 llist->flags &= ~LYS_SET_DFLT;
5834 }
5835 } else {
5836 /* local deviation */
5837 orig_dflt = NULL;
5838 orig_dflts = NULL;
5839 for (i = 0; i < ctx->dflts.count; ++i) {
5840 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) {
5841 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5842 orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts;
5843 break;
5844 }
5845 }
5846 LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT);
5847
5848 if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) {
5849 /* TODO it is not currently possible to remove just one default value from incomplete defaults array */
5850 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5851 "Local deviation deleting leaf-list defaults is not supported.");
5852 return LY_EVALID;
5853 }
5854
5855 LY_ARRAY_FOR(dflts, x) {
5856 found = 0;
5857 if (orig_dflts) {
5858 LY_ARRAY_FOR(orig_dflts, y) {
5859 if (!strcmp(orig_dflts[y], dflts[x])) {
5860 found = 1;
5861 break;
5862 }
5863 }
5864 } else if (!strcmp(orig_dflt, dflts[x])) {
5865 found = 1;
5866 }
5867 if (!found) {
5868 goto error;
5869 }
5870
5871 /* update the list of incomplete default values */
5872 lysc_incomplete_dflt_remove(ctx, target);
5873 }
5874
5875 llist->flags &= ~LYS_SET_DFLT;
5876 }
5877
5878 return LY_SUCCESS;
5879
5880error:
5881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
5882 "which does not match any of the target's property values.", dflts[x]);
5883cleanup:
5884 return LY_EVALID;
5885}
5886
Michal Vaskoccc062a2020-08-13 08:34:50 +02005887/**
5888 * @brief Apply deviate delete.
5889 *
5890 * @param[in] ctx Compile context.
5891 * @param[in] target Deviation target.
5892 * @param[in] dev_flags Internal deviation flags.
5893 * @param[in] d Deviate delete to apply.
5894 * @return LY_ERR value.
5895 */
5896static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005897lys_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 +02005898{
5899 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005900 struct lysc_node_list *list = (struct lysc_node_list *)target;
5901 LY_ARRAY_COUNT_TYPE x, y, z;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005902 size_t prefix_len, name_len;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005903 const char *prefix, *name, *nodeid;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005904 struct lys_module *mod;
5905
5906#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5907 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5908 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5909 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5910 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5911 } \
5912 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5913 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5914 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5915 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5916 goto cleanup; \
5917 } \
5918 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5919 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5920 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5921 &((TYPE)target)->ARRAY_TRG[y + 1], \
5922 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5923 } \
5924 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5925 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5926 ((TYPE)target)->ARRAY_TRG = NULL; \
5927 }
5928
5929 /* [units-stmt] */
5930 if (d->units) {
5931 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Michal Vasko22df3f02020-08-24 13:29:22 +02005932 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units);
5933 if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5935 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02005936 d->units, ((struct lysc_node_leaf *)target)->units);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005937 goto cleanup;
5938 }
Michal Vasko22df3f02020-08-24 13:29:22 +02005939 lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5940 ((struct lysc_node_leaf *)target)->units = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005941 }
5942
5943 /* *must-stmt */
5944 if (d->musts) {
5945 switch (target->nodetype) {
5946 case LYS_CONTAINER:
5947 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02005948 DEV_DEL_ARRAY(struct lysc_node_container *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005949 break;
5950 case LYS_LEAF:
5951 case LYS_LEAFLIST:
5952 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02005953 DEV_DEL_ARRAY(struct lysc_node_leaf *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005954 break;
5955 case LYS_NOTIF:
Michal Vasko22df3f02020-08-24 13:29:22 +02005956 DEV_DEL_ARRAY(struct lysc_notif *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005957 break;
5958 case LYS_RPC:
5959 case LYS_ACTION:
5960 if (dev_flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005961 DEV_DEL_ARRAY(struct lysc_action *, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005962 break;
5963 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02005964 DEV_DEL_ARRAY(struct lysc_action *, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Michal Vaskoccc062a2020-08-13 08:34:50 +02005965 break;
5966 }
Radek Krejci0f969882020-08-21 16:56:47 +02005967 /* fall through */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005968 default:
5969 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5970 lys_nodetype2str(target->nodetype), "delete", "must");
5971 goto cleanup;
5972 }
5973 }
5974
5975 /* *unique-stmt */
5976 if (d->uniques) {
5977 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5978 LY_ARRAY_FOR(d->uniques, x) {
5979 LY_ARRAY_FOR(list->uniques, z) {
5980 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
5981 nodeid = strpbrk(name, " \t\n");
5982 if (nodeid) {
5983 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
5984 break;
5985 }
5986 while (isspace(*nodeid)) {
5987 ++nodeid;
5988 }
5989 } else {
5990 if (strcmp(name, list->uniques[z][y]->name)) {
5991 break;
5992 }
5993 }
5994 }
5995 if (!name) {
5996 /* complete match - remove the unique */
5997 LY_ARRAY_DECREMENT(list->uniques);
5998 LY_ARRAY_FREE(list->uniques[z]);
5999 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
6000 --z;
6001 break;
6002 }
6003 }
6004 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
6005 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6006 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6007 d->uniques[x]);
6008 goto cleanup;
6009 }
6010 }
6011 if (!LY_ARRAY_COUNT(list->uniques)) {
6012 LY_ARRAY_FREE(list->uniques);
6013 list->uniques = NULL;
6014 }
6015 }
6016
6017 /* *default-stmt */
6018 if (d->dflts) {
6019 switch (target->nodetype) {
6020 case LYS_LEAF:
6021 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006022 LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006023 break;
6024 case LYS_LEAFLIST:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006025 LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006026 break;
6027 case LYS_CHOICE:
6028 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vasko22df3f02020-08-24 13:29:22 +02006029 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "deleting", "default", d->dflts[0]);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006030 nodeid = d->dflts[0];
6031 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6032 if (prefix) {
6033 /* use module prefixes from the deviation module to match the module of the default case */
6034 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006035 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006036 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6037 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6038 goto cleanup;
6039 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006040 if (mod != ((struct lysc_node_choice *)target)->dflt->module) {
Michal Vaskod989ba02020-08-24 10:59:24 +02006041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoccc062a2020-08-13 08:34:50 +02006042 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6043 "The prefix does not match the default case's module.", d->dflts[0]);
6044 goto cleanup;
6045 }
6046 }
6047 /* else {
6048 * strictly, the default prefix would point to the deviation module, but the value should actually
6049 * match the default string in the original module (usually unprefixed), so in this case we do not check
6050 * the module of the default case, just matching its name */
Michal Vasko22df3f02020-08-24 13:29:22 +02006051 if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02006052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6053 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
Michal Vasko22df3f02020-08-24 13:29:22 +02006054 d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006055 goto cleanup;
6056 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006057 ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT;
6058 ((struct lysc_node_choice *)target)->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006059 break;
6060 default:
6061 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6062 lys_nodetype2str(target->nodetype), "delete", "default");
6063 goto cleanup;
6064 }
6065 }
6066
6067 ret = LY_SUCCESS;
6068
6069cleanup:
6070 return ret;
6071}
6072
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006073static LY_ERR
6074lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target)
6075{
6076 LY_ERR ret;
6077 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6078 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6079 struct ly_err_item *err = NULL;
6080 LY_ARRAY_COUNT_TYPE x;
6081 const char *dflt;
Radek Krejci857189e2020-09-01 13:26:36 +02006082 ly_bool dyn;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006083
6084 if (target->module != ctx->mod) {
6085 /* foreign deviation */
6086 if (target->nodetype == LYS_LEAF) {
6087 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn);
6088 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6089 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6090
6091 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6092 LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err);
6093 if (dyn) {
6094 free((char *)dflt);
6095 }
6096 if (err) {
6097 ly_err_print(err);
6098 ctx->path[0] = '\0';
6099 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6100 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6101 "Invalid default - value does not fit the type (%s).", err->msg);
6102 ly_err_free(err);
6103 }
6104 LY_CHECK_RET(ret);
6105
6106 ++leaf->dflt->realtype->refcount;
6107 } else { /* LY_LEAFLIST */
6108 LY_ARRAY_FOR(llist->dflts, x) {
6109 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn);
6110 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6111 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6112
6113 ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6114 LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err);
6115 if (dyn) {
6116 free((char *)dflt);
6117 }
6118 if (err) {
6119 ly_err_print(err);
6120 ctx->path[0] = '\0';
6121 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6122 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6123 "Invalid default - value does not fit the type (%s).", err->msg);
6124 ly_err_free(err);
6125 }
6126 LY_CHECK_RET(ret);
6127
6128 ++llist->dflts[x]->realtype->refcount;
6129 }
6130 }
6131 } else {
6132 /* local deviation */
6133
6134 /* these default were not compiled yet, so they will use the new type automatically */
6135 }
6136
6137 return LY_SUCCESS;
6138}
6139
Michal Vaskoccc062a2020-08-13 08:34:50 +02006140/**
6141 * @brief Apply deviate replace.
6142 *
6143 * @param[in] ctx Compile context.
6144 * @param[in] target Deviation target.
6145 * @param[in] d Deviate replace to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02006146 * @return LY_ERR value.
6147 */
6148static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006149lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02006150{
Radek Krejci011e4aa2020-09-04 15:22:31 +02006151 LY_ERR ret = LY_EVALID, rc = LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006152 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6153 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6154 LY_ARRAY_COUNT_TYPE x;
6155
6156#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6157 if (!(((TYPE)target)->MEMBER COND)) { \
6158 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6159 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6160 goto cleanup; \
6161 }
6162
6163 /* [type-stmt] */
6164 if (d->type) {
6165 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6166 /* type is mandatory, so checking for its presence is not necessary */
6167 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6168
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006169 /* remove only default value inherited from the type */
6170 if (!(target->flags & LYS_SET_DFLT)) {
6171 if (target->module != ctx->mod) {
6172 /* foreign deviation - the target has default from the previous type, remove it */
6173 if (target->nodetype == LYS_LEAF) {
6174 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6175 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6176 free(leaf->dflt);
6177 leaf->dflt = NULL;
6178 } else { /* LYS_LEAFLIST */
6179 LY_ARRAY_FOR(llist->dflts, x) {
6180 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6181 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6182 free(llist->dflts[x]);
6183 }
6184 LY_ARRAY_FREE(llist->dflts);
6185 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006186 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006187 } else {
6188 /* local deviation */
6189 lysc_incomplete_dflt_remove(ctx, target);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006190 }
6191 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006192
6193 LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf));
6194
6195 if (target->flags & LYS_SET_DFLT) {
6196 /* the term default value(s) needs to be recompiled */
6197 LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006198 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006199 }
6200
6201 /* [units-stmt] */
6202 if (d->units) {
6203 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6204 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6205 units, "replacing", "units", d->units);
6206
6207 lydict_remove(ctx->ctx, leaf->units);
Radek Krejci011e4aa2020-09-04 15:22:31 +02006208 DUP_STRING(ctx->ctx, d->units, leaf->units, rc);
6209 LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006210 }
6211
6212 /* [default-stmt] */
6213 if (d->dflt) {
6214 switch (target->nodetype) {
6215 case LYS_LEAF:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006216 if (target->module != ctx->mod) {
6217 /* foreign deviation */
6218 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing",
6219 "default", d->dflt);
6220
6221 /* remove the default specification */
6222 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6223 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6224 free(leaf->dflt);
6225 leaf->dflt = NULL;
6226 } else {
6227 /* local deviation */
6228 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing",
6229 "default", d->dflt);
6230 assert(!leaf->dflt);
6231 }
6232
6233 /* store the new default value */
6234 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006235 break;
6236 case LYS_CHOICE:
6237 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6238 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6239 goto cleanup;
6240 }
6241 break;
6242 default:
6243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6244 lys_nodetype2str(target->nodetype), "replace", "default");
6245 goto cleanup;
6246 }
6247 }
6248
6249 /* [config-stmt] */
6250 if (d->flags & LYS_CONFIG_MASK) {
6251 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6252 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6253 lys_nodetype2str(target->nodetype), "replace", "config");
6254 goto cleanup;
6255 }
6256 if (!(target->flags & LYS_SET_CONFIG)) {
6257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6258 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6259 goto cleanup;
6260 }
6261 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6262 }
6263
6264 /* [mandatory-stmt] */
6265 if (d->flags & LYS_MAND_MASK) {
6266 if (!(target->flags & LYS_MAND_MASK)) {
6267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6268 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6269 goto cleanup;
6270 }
6271 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6272 }
6273
6274 /* [min-elements-stmt] */
6275 if (d->flags & LYS_SET_MIN) {
6276 if (target->nodetype == LYS_LEAFLIST) {
6277 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6278 /* change value */
6279 ((struct lysc_node_leaflist *)target)->min = d->min;
6280 } else if (target->nodetype == LYS_LIST) {
6281 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6282 /* change value */
6283 ((struct lysc_node_list *)target)->min = d->min;
6284 } else {
6285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6286 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6287 goto cleanup;
6288 }
6289 if (d->min) {
6290 target->flags |= LYS_MAND_TRUE;
6291 }
6292 }
6293
6294 /* [max-elements-stmt] */
6295 if (d->flags & LYS_SET_MAX) {
6296 if (target->nodetype == LYS_LEAFLIST) {
6297 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6298 /* change value */
6299 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6300 } else if (target->nodetype == LYS_LIST) {
6301 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6302 /* change value */
6303 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6304 } else {
6305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6306 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6307 goto cleanup;
6308 }
6309 }
6310
6311 ret = LY_SUCCESS;
6312
6313cleanup:
6314 return ret;
6315}
6316
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006317/**
6318 * @brief Apply all deviations of one target node.
6319 *
6320 * @param[in] ctx Compile context.
6321 * @param[in] dev Deviation structure to apply.
6322 * @return LY_ERR value.
6323 */
6324static LY_ERR
6325lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006326{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006327 LY_ERR ret = LY_EVALID;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006328 struct lysc_node *target = dev->target;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006329 struct lysc_action *rpcs;
6330 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006331 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006332 LY_ARRAY_COUNT_TYPE v, x;
Radek Krejci551b12c2019-02-19 16:11:21 +01006333 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006334
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006335 lysc_update_path(ctx, NULL, dev->nodeid);
6336
6337 /* not-supported */
6338 if (dev->not_supported) {
6339 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
Radek Krejci0f969882020-08-21 16:56:47 +02006340 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 +02006341 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6342 }
6343
6344#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6345 if (target->parent) { \
6346 ARRAY = (TYPE*)GETFUNC(target->parent); \
6347 } else { \
6348 ARRAY = target->module->compiled->ARRAY; \
6349 } \
6350 LY_ARRAY_FOR(ARRAY, x) { \
6351 if (&ARRAY[x] == (TYPE*)target) { break; } \
6352 } \
6353 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6354 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6355 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6356 LY_ARRAY_DECREMENT(ARRAY); \
6357 }
6358
6359 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6360 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6361 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006362 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6363 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6364 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6365 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006366 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6367 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006368 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6369 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6370 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6371 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006372 } else {
6373 /* remove RPC/action */
6374 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6375 }
6376 } else if (target->nodetype == LYS_NOTIF) {
6377 /* remove Notification */
6378 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6379 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02006380 if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) {
6381 /* remove the target node with its parent case node because it is the only node of the case */
6382 lysc_node_unlink(target->parent);
6383 lysc_node_free(ctx->ctx, target->parent);
6384 } else {
6385 /* remove the target node */
6386 lysc_node_unlink(target);
6387 lysc_node_free(ctx->ctx, target);
6388 }
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006389 }
6390
6391 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6392 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6393 return LY_SUCCESS;
6394 }
6395
6396 /* list of deviates (not-supported is not present in the list) */
6397 LY_ARRAY_FOR(dev->deviates, v) {
6398 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006399 switch (d->mod) {
6400 case LYS_DEV_ADD:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006401 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006402 break;
6403 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006404 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006405 break;
6406 case LYS_DEV_REPLACE:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006407 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006408 break;
6409 default:
6410 LOGINT(ctx->ctx);
6411 goto cleanup;
6412 }
6413 }
6414
6415 /* final check when all deviations of a single target node are applied */
6416
6417 /* check min-max compatibility */
6418 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006419 min = ((struct lysc_node_leaflist *)target)->min;
6420 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006421 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006422 min = ((struct lysc_node_list *)target)->min;
6423 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006424 } else {
6425 min = max = 0;
6426 }
6427 if (min > max) {
6428 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6429 "after deviation: min value %u is bigger than max value %u.", min, max);
6430 goto cleanup;
6431 }
6432
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006433 /* check mandatory - default compatibility */
6434 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6435 && (target->flags & LYS_MAND_TRUE)) {
6436 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6437 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6438 goto cleanup;
Michal Vasko22df3f02020-08-24 13:29:22 +02006439 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006440 && (target->flags & LYS_MAND_TRUE)) {
6441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6442 goto cleanup;
6443 }
6444 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6445 /* mandatory node under a default case */
6446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6447 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6448 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6449 goto cleanup;
6450 }
6451
6452 /* success */
6453 ret = LY_SUCCESS;
6454
6455cleanup:
6456 lysc_update_path(ctx, NULL, NULL);
6457 return ret;
6458}
6459
6460LY_ERR
6461lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6462{
6463 LY_ERR ret = LY_EVALID;
6464 struct ly_set devs_p = {0};
6465 struct ly_set targets = {0};
6466 struct lysc_node *target; /* target target of the deviation */
6467 struct lysp_deviation *dev;
6468 struct lysp_deviate *d, **dp_new;
6469 LY_ARRAY_COUNT_TYPE u, v;
6470 struct lysc_deviation **devs = NULL;
6471 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006472 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006473
6474 /* get all deviations from the module and all its submodules ... */
6475 LY_ARRAY_FOR(mod_p->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006476 LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST, NULL));
Radek Krejciccd20f12019-02-15 14:12:27 +01006477 }
6478 LY_ARRAY_FOR(mod_p->includes, v) {
6479 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006480 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 +01006481 }
6482 }
6483 if (!devs_p.count) {
6484 /* nothing to do */
6485 return LY_SUCCESS;
6486 }
6487
Radek Krejci327de162019-06-14 12:52:07 +02006488 lysc_update_path(ctx, NULL, "{deviation}");
6489
Radek Krejciccd20f12019-02-15 14:12:27 +01006490 /* ... and group them by the target node */
6491 devs = calloc(devs_p.count, sizeof *devs);
6492 for (u = 0; u < devs_p.count; ++u) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006493 uint32_t index;
6494
Radek Krejciccd20f12019-02-15 14:12:27 +01006495 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006496 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006497
6498 /* resolve the target */
Michal Vaskocb18c7f2020-08-24 09:22:28 +02006499 LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
Michal Vasko22df3f02020-08-24 13:29:22 +02006500 (const struct lysc_node **)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006501 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006502 /* move the target pointer to input/output to make them different from the action and
6503 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6504 * back to the RPC/action node due to a better compatibility and decision code in this function.
6505 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6506 if (flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006507 target = (struct lysc_node *)&((struct lysc_action *)target)->input;
Radek Krejcif538ce52019-03-05 10:46:14 +01006508 flags |= LYSC_OPT_INTERNAL;
6509 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko22df3f02020-08-24 13:29:22 +02006510 target = (struct lysc_node *)&((struct lysc_action *)target)->output;
Radek Krejcif538ce52019-03-05 10:46:14 +01006511 flags |= LYSC_OPT_INTERNAL;
6512 }
6513 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006514 /* insert into the set of targets with duplicity detection */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006515 ret = ly_set_add(&targets, target, 0, &index);
6516 LY_CHECK_GOTO(ret, cleanup);
6517 if (!devs[index]) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006518 /* new record */
Radek Krejciba03a5a2020-08-27 14:40:41 +02006519 devs[index] = calloc(1, sizeof **devs);
6520 devs[index]->target = target;
6521 devs[index]->nodeid = dev->nodeid;
6522 devs[index]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006523 }
6524 /* add deviates into the deviation's list of deviates */
Michal Vasko5c38f362020-08-24 09:22:51 +02006525 LY_LIST_FOR(dev->deviates, d) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006526 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[index]->deviates, dp_new, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006527 *dp_new = d;
6528 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006529 devs[index]->not_supported = 1;
Radek Krejciccd20f12019-02-15 14:12:27 +01006530 }
6531 }
Radek Krejci327de162019-06-14 12:52:07 +02006532
6533 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006534 }
6535
Radek Krejciccd20f12019-02-15 14:12:27 +01006536 /* apply deviations */
6537 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006538 ly_bool match = 0;
Radek Krejciba03a5a2020-08-27 14:40:41 +02006539
Radek Krejcif538ce52019-03-05 10:46:14 +01006540 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6541 /* fix the target pointer in case of RPC's/action's input/output */
6542 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006543 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006544 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006545 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006546 }
6547 }
6548
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006549 /* remember target module (the target node may be removed) */
6550 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006551
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006552 /* apply the deviation */
6553 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006554
Michal Vaskoe6143202020-07-03 13:02:08 +02006555 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006556 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6557 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02006558 match = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006559 break;
6560 }
6561 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02006562 if (!match) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006563 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006564 *dev_mod = mod_p->mod;
6565 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006566 }
6567
Radek Krejci327de162019-06-14 12:52:07 +02006568 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006569 ret = LY_SUCCESS;
6570
6571cleanup:
6572 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6573 LY_ARRAY_FREE(devs[u]->deviates);
6574 free(devs[u]);
6575 }
6576 free(devs);
6577 ly_set_erase(&targets, NULL);
6578 ly_set_erase(&devs_p, NULL);
6579
6580 return ret;
6581}
6582
Radek Krejcib56c7502019-02-13 14:19:54 +01006583/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006584 * @brief Compile the given YANG submodule into the main module.
6585 * @param[in] ctx Compile context
6586 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006587 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6588 */
6589LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006590lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006591{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006592 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006593 LY_ERR ret = LY_SUCCESS;
6594 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006595 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006596 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006597 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006598
Radek Krejci14915cc2020-09-14 17:28:13 +02006599 /* features are compiled directly into the compiled module structure,
6600 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6601 * The features compilation is finished in the main module (lys_compile()). */
6602 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->mod->features);
6603 LY_CHECK_GOTO(ret, error);
Radek Krejci0af46292019-01-11 16:02:31 +01006604
Radek Krejci80d281e2020-09-14 17:42:54 +02006605 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->mod->identities);
6606 LY_CHECK_GOTO(ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006607
Radek Krejci474f9b82019-07-24 11:36:37 +02006608 /* data nodes */
6609 LY_LIST_FOR(submod->data, node_p) {
6610 ret = lys_compile_node(ctx, node_p, NULL, 0);
6611 LY_CHECK_GOTO(ret, error);
6612 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006613
Radek Krejciec4da802019-05-02 13:02:41 +02006614 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6615 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006616
Radek Krejcid05cbd92018-12-05 14:26:40 +01006617error:
6618 return ret;
6619}
6620
Radek Krejci335332a2019-09-05 13:03:35 +02006621static void *
6622lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6623{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006624 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci335332a2019-09-05 13:03:35 +02006625 if (substmts[u].stmt == stmt) {
6626 return substmts[u].storage;
6627 }
6628 }
6629 return NULL;
6630}
6631
6632LY_ERR
6633lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6634{
6635 LY_ERR ret = LY_EVALID, r;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006636 LY_ARRAY_COUNT_TYPE u;
Radek Krejci335332a2019-09-05 13:03:35 +02006637 struct lysp_stmt *stmt;
6638 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006639
6640 /* check for invalid substatements */
6641 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006642 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6643 continue;
6644 }
Radek Krejci335332a2019-09-05 13:03:35 +02006645 for (u = 0; substmts[u].stmt; ++u) {
6646 if (substmts[u].stmt == stmt->kw) {
6647 break;
6648 }
6649 }
6650 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6652 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006653 goto cleanup;
6654 }
Radek Krejci335332a2019-09-05 13:03:35 +02006655 }
6656
Radek Krejciad5963b2019-09-06 16:03:05 +02006657 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6658
Radek Krejci335332a2019-09-05 13:03:35 +02006659 /* keep order of the processing the same as the order in the defined substmts,
6660 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6661 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02006662 ly_bool stmt_present = 0;
Radek Krejciad5963b2019-09-06 16:03:05 +02006663
Radek Krejci335332a2019-09-05 13:03:35 +02006664 for (stmt = ext->child; stmt; stmt = stmt->next) {
6665 if (substmts[u].stmt != stmt->kw) {
6666 continue;
6667 }
6668
Radek Krejciad5963b2019-09-06 16:03:05 +02006669 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006670 if (substmts[u].storage) {
6671 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006672 case LY_STMT_STATUS:
6673 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6674 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6675 break;
6676 case LY_STMT_UNITS: {
6677 const char **units;
6678
6679 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6680 /* single item */
6681 if (*((const char **)substmts[u].storage)) {
6682 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6683 goto cleanup;
6684 }
6685 units = (const char **)substmts[u].storage;
6686 } else {
6687 /* sized array */
6688 const char ***units_array = (const char ***)substmts[u].storage;
6689 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6690 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02006691 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
6692 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +02006693 break;
6694 }
Radek Krejci335332a2019-09-05 13:03:35 +02006695 case LY_STMT_TYPE: {
6696 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6697 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6698
6699 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6700 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006701 if (*(struct lysc_type **)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006702 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6703 goto cleanup;
6704 }
6705 compiled = substmts[u].storage;
6706 } else {
6707 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006708 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006709 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006710 compiled = (void *)type;
Radek Krejci335332a2019-09-05 13:03:35 +02006711 }
6712
Radek Krejciad5963b2019-09-06 16:03:05 +02006713 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02006714 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL,
6715 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006716 units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02006717 lysp_type_free(ctx->ctx, parsed);
6718 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006719 break;
6720 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006721 case LY_STMT_IF_FEATURE: {
6722 struct lysc_iffeature *iff = NULL;
6723
6724 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6725 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02006726 if (((struct lysc_iffeature *)substmts[u].storage)->features) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006727 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6728 goto cleanup;
6729 }
Michal Vasko22df3f02020-08-24 13:29:22 +02006730 iff = (struct lysc_iffeature *)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006731 } else {
6732 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02006733 struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02006734 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6735 }
6736 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6737 break;
6738 }
6739 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6740 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006741 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006742 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.",
6743 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006744 goto cleanup;
6745 }
6746 }
Radek Krejci335332a2019-09-05 13:03:35 +02006747 }
Radek Krejci335332a2019-09-05 13:03:35 +02006748
Radek Krejciad5963b2019-09-06 16:03:05 +02006749 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6751 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6752 goto cleanup;
6753 }
Radek Krejci335332a2019-09-05 13:03:35 +02006754 }
6755
6756 ret = LY_SUCCESS;
6757
6758cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006759 return ret;
6760}
6761
Michal Vasko175012e2019-11-06 15:49:14 +01006762/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006763 * @brief Check when for cyclic dependencies.
6764 * @param[in] set Set with all the referenced nodes.
6765 * @param[in] node Node whose "when" referenced nodes are in @p set.
6766 * @return LY_ERR value
6767 */
6768static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006769lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006770{
6771 struct lyxp_set tmp_set;
6772 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006773 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006774 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006775 struct lysc_when *when;
6776 LY_ERR ret = LY_SUCCESS;
6777
6778 memset(&tmp_set, 0, sizeof tmp_set);
6779
6780 /* prepare in_ctx of the set */
Michal Vaskod989ba02020-08-24 10:59:24 +02006781 for (i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006782 xp_scnode = &set->val.scnodes[i];
6783
Michal Vasko5c4e5892019-11-14 12:31:38 +01006784 if (xp_scnode->in_ctx != -1) {
6785 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006786 xp_scnode->in_ctx = 1;
6787 }
6788 }
6789
6790 for (i = 0; i < set->used; ++i) {
6791 xp_scnode = &set->val.scnodes[i];
6792 if (xp_scnode->in_ctx != 1) {
6793 /* already checked */
6794 continue;
6795 }
6796
Michal Vasko1bf09392020-03-27 12:38:10 +01006797 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006798 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006799 /* no when to check */
6800 xp_scnode->in_ctx = 0;
6801 continue;
6802 }
6803
6804 node = xp_scnode->scnode;
6805 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006806 LY_ARRAY_FOR(node->when, u) {
6807 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006808 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006809 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6810 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006811 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006812 goto cleanup;
6813 }
6814
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006815 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006816 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006817 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006818 /* try to find this node in our set */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006819 uint32_t idx;
6820 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 +01006821 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 +01006822 ret = LY_EVALID;
6823 goto cleanup;
6824 }
6825
6826 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006827 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006828 } else {
6829 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006830 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006831 }
6832 }
6833
6834 /* merge this set into the global when set */
6835 lyxp_set_scnode_merge(set, &tmp_set);
6836 }
6837
6838 /* check when of non-data parents as well */
6839 node = node->parent;
6840 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6841
Michal Vasko251f56e2019-11-14 16:06:47 +01006842 /* this node when was checked (xp_scnode could have been reallocd) */
6843 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006844 }
6845
6846cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006847 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006848 return ret;
6849}
6850
6851/**
Michal Vasko175012e2019-11-06 15:49:14 +01006852 * @brief Check when/must expressions of a node on a compiled schema tree.
6853 * @param[in] ctx Compile context.
6854 * @param[in] node Node to check.
6855 * @return LY_ERR value
6856 */
6857static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006858lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006859{
Michal Vasko175012e2019-11-06 15:49:14 +01006860 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006861 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006862 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006863 uint32_t opts;
Radek Krejci857189e2020-09-01 13:26:36 +02006864 ly_bool input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006865 struct lysc_when **when = NULL;
6866 struct lysc_must *musts = NULL;
6867 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006868 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006869
6870 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006871 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006872 if (node->flags & LYS_CONFIG_R) {
6873 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6874 if (op) {
6875 /* we are actually in output */
6876 opts = LYXP_SCNODE_OUTPUT;
6877 }
6878 }
Michal Vasko175012e2019-11-06 15:49:14 +01006879
6880 switch (node->nodetype) {
6881 case LYS_CONTAINER:
6882 when = ((struct lysc_node_container *)node)->when;
6883 musts = ((struct lysc_node_container *)node)->musts;
6884 break;
6885 case LYS_CHOICE:
6886 when = ((struct lysc_node_choice *)node)->when;
6887 break;
6888 case LYS_LEAF:
6889 when = ((struct lysc_node_leaf *)node)->when;
6890 musts = ((struct lysc_node_leaf *)node)->musts;
6891 break;
6892 case LYS_LEAFLIST:
6893 when = ((struct lysc_node_leaflist *)node)->when;
6894 musts = ((struct lysc_node_leaflist *)node)->musts;
6895 break;
6896 case LYS_LIST:
6897 when = ((struct lysc_node_list *)node)->when;
6898 musts = ((struct lysc_node_list *)node)->musts;
6899 break;
6900 case LYS_ANYXML:
6901 case LYS_ANYDATA:
6902 when = ((struct lysc_node_anydata *)node)->when;
6903 musts = ((struct lysc_node_anydata *)node)->musts;
6904 break;
6905 case LYS_CASE:
6906 when = ((struct lysc_node_case *)node)->when;
6907 break;
6908 case LYS_NOTIF:
6909 musts = ((struct lysc_notif *)node)->musts;
6910 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006911 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006912 case LYS_ACTION:
6913 /* first process input musts */
6914 musts = ((struct lysc_action *)node)->input.musts;
6915 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006916 default:
6917 /* nothing to check */
6918 break;
6919 }
6920
Michal Vasko175012e2019-11-06 15:49:14 +01006921 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006922 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006923 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 +02006924 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006925 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006926 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006927 goto cleanup;
6928 }
6929
Michal Vaskodc052f32019-11-07 11:11:38 +01006930 ctx->path[0] = '\0';
6931 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006932 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006933 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006934 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6935 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006936
Michal Vaskoecd62de2019-11-13 12:35:11 +01006937 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006938 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006939 schema->name);
6940 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006941
6942 /* check dummy node accessing */
6943 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006944 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006945 ret = LY_EVALID;
6946 goto cleanup;
6947 }
Michal Vasko175012e2019-11-06 15:49:14 +01006948 }
6949 }
6950
Michal Vaskoecd62de2019-11-13 12:35:11 +01006951 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006952 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006953 LY_CHECK_GOTO(ret, cleanup);
6954
Michal Vaskod3678892020-05-21 10:06:58 +02006955 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006956 }
6957
Michal Vasko5d8756a2019-11-07 15:21:00 +01006958check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006959 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006960 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006961 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 +01006962 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006963 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006964 goto cleanup;
6965 }
6966
Michal Vaskodc052f32019-11-07 11:11:38 +01006967 ctx->path[0] = '\0';
6968 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006969 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006970 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006971 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +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, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6974 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006975 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006976 }
6977 }
6978
Michal Vaskod3678892020-05-21 10:06:58 +02006979 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006980 }
6981
Michal Vasko1bf09392020-03-27 12:38:10 +01006982 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006983 /* now check output musts */
6984 input_done = 1;
6985 musts = ((struct lysc_action *)node)->output.musts;
6986 opts = LYXP_SCNODE_OUTPUT;
6987 goto check_musts;
6988 }
6989
Michal Vasko175012e2019-11-06 15:49:14 +01006990cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006991 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006992 return ret;
6993}
6994
Michal Vasko8d544252020-03-02 10:19:52 +01006995static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006996lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6997{
Michal Vasko6b26e742020-07-17 15:02:10 +02006998 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02006999 struct ly_path *p;
7000 struct lysc_type *type;
7001
7002 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7003
7004 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02007005 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
7006 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007007 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02007008
7009 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007010 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02007011 ly_path_free(node->module->ctx, p);
7012
7013 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7014 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7015 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
7016 lref->path->expr, lys_nodetype2str(target->nodetype));
7017 return LY_EVALID;
7018 }
7019
7020 /* check status */
7021 ctx->path[0] = '\0';
7022 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7023 ctx->path_len = strlen(ctx->path);
7024 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
7025 return LY_EVALID;
7026 }
7027 ctx->path_len = 1;
7028 ctx->path[1] = '\0';
7029
7030 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007031 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007032 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007033 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007034 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7035 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7036 return LY_EVALID;
7037 }
7038 }
7039
7040 /* store the target's type and check for circular chain of leafrefs */
7041 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7042 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7043 if (type == (struct lysc_type *)lref) {
7044 /* circular chain detected */
7045 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7046 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7047 return LY_EVALID;
7048 }
7049 }
7050
7051 /* check if leafref and its target are under common if-features */
7052 if (lys_compile_leafref_features_validate(node, target)) {
7053 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7054 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7055 " features applicable to the leafref itself.", lref->path->expr);
7056 return LY_EVALID;
7057 }
7058
7059 return LY_SUCCESS;
7060}
7061
7062static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007063lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7064{
7065 struct lysc_ext_instance *ext;
7066 struct lysp_ext_instance *ext_p = NULL;
7067 struct lysp_stmt *stmt;
7068 const struct lys_module *ext_mod;
7069 LY_ERR ret = LY_SUCCESS;
7070
7071 /* create the parsed extension instance manually */
7072 ext_p = calloc(1, sizeof *ext_p);
7073 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007074 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
7075 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007076 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7077 ext_p->insubstmt_index = 0;
7078
Radek Krejci87e25252020-09-15 13:28:31 +02007079 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
7080 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007081 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
7082 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007083 stmt->kw = LY_STMT_TYPE;
Michal Vasko8d544252020-03-02 10:19:52 +01007084
7085 /* allocate new extension instance */
7086 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7087
7088 /* manually get extension definition module */
7089 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7090
7091 /* compile the extension instance */
7092 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7093
7094cleanup:
7095 lysp_ext_instance_free(ctx->ctx, ext_p);
7096 free(ext_p);
7097 return ret;
7098}
7099
Michal Vasko004d3152020-06-11 19:59:22 +02007100static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007101lys_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 +02007102 const struct lys_module *dflt_mod, struct lyd_value *storage)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007103{
7104 LY_ERR ret;
7105 struct ly_err_item *err = NULL;
7106
7107 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7108 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7109 if (err) {
7110 ly_err_print(err);
7111 ctx->path[0] = '\0';
7112 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7114 "Invalid default - value does not fit the type (%s).", err->msg);
7115 ly_err_free(err);
7116 }
7117 if (!ret) {
7118 ++storage->realtype->refcount;
7119 return LY_SUCCESS;
7120 }
7121 return ret;
7122}
7123
7124static LY_ERR
7125lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +02007126 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007127{
7128 LY_ERR ret;
7129
7130 assert(!leaf->dflt);
7131
7132 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7133 /* ignore default values for keys and mandatory leaves */
7134 return LY_SUCCESS;
7135 }
7136
7137 /* allocate the default value */
7138 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7139 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7140
7141 /* store the default value */
7142 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt);
7143 if (ret) {
7144 free(leaf->dflt);
7145 leaf->dflt = NULL;
7146 }
7147
7148 return ret;
7149}
7150
7151static LY_ERR
7152lys_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 +02007153 const struct lys_module *dflt_mod)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007154{
7155 LY_ERR ret;
7156 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7157
7158 assert(dflt || dflts);
7159
7160 if (llist->dflts) {
7161 /* there were already some defaults and we are adding new by deviations */
7162 assert(dflts);
7163 orig_count = LY_ARRAY_COUNT(llist->dflts);
7164 } else {
7165 orig_count = 0;
7166 }
7167
7168 /* allocate new items */
7169 if (dflts) {
7170 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7171 } else {
7172 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7173 }
7174
7175 /* fill each new default value */
7176 if (dflts) {
7177 LY_ARRAY_FOR(dflts, u) {
7178 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
7179 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod,
7180 llist->dflts[orig_count + u]);
7181 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7182 LY_ARRAY_INCREMENT(llist->dflts);
7183 }
7184 } else {
7185 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
7186 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]);
7187 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7188 LY_ARRAY_INCREMENT(llist->dflts);
7189 }
7190
7191 /* check default value uniqueness */
7192 if (llist->flags & LYS_CONFIG_W) {
7193 /* configuration data values must be unique - so check the default values */
7194 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7195 for (v = 0; v < u; ++v) {
7196 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejci857189e2020-09-01 13:26:36 +02007197 ly_bool dynamic = 0;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007198 const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic);
7199
7200 lysc_update_path(ctx, llist->parent, llist->name);
7201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7202 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
7203 lysc_update_path(ctx, NULL, NULL);
7204 if (dynamic) {
7205 free((char *)val);
7206 }
7207 return LY_EVALID;
7208 }
7209 }
7210 }
7211 }
7212
7213 return LY_SUCCESS;
7214}
7215
7216static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007217lys_compile_unres(struct lysc_ctx *ctx)
7218{
7219 struct lysc_node *node;
7220 struct lysc_type *type, *typeiter;
7221 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007222 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007223 uint32_t i;
7224
7225 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7226 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7227 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7228 for (i = 0; i < ctx->leafrefs.count; ++i) {
7229 node = ctx->leafrefs.objs[i];
7230 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7231 type = ((struct lysc_node_leaf *)node)->type;
7232 if (type->basetype == LY_TYPE_LEAFREF) {
7233 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7234 } else if (type->basetype == LY_TYPE_UNION) {
7235 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7236 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7237 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7238 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7239 }
7240 }
7241 }
7242 }
7243 for (i = 0; i < ctx->leafrefs.count; ++i) {
7244 /* store pointer to the real type */
7245 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7246 if (type->basetype == LY_TYPE_LEAFREF) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007247 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007248 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007249 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7250 ((struct lysc_type_leafref *)type)->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007251 } else if (type->basetype == LY_TYPE_UNION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007252 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7253 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7254 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007255 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007256 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7257 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007258 }
7259 }
7260 }
7261 }
7262
7263 /* check xpath */
7264 for (i = 0; i < ctx->xpath.count; ++i) {
7265 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7266 }
7267
7268 /* finish incomplete default values compilation */
7269 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007270 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007271 if (r->leaf->nodetype == LYS_LEAF) {
7272 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod));
7273 } else {
7274 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 +02007275 }
Michal Vasko004d3152020-06-11 19:59:22 +02007276 }
7277
7278 return LY_SUCCESS;
7279}
7280
Radek Krejci19a96102018-11-15 13:38:09 +01007281LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02007282lys_compile(struct lys_module *mod, uint32_t options)
Radek Krejci19a96102018-11-15 13:38:09 +01007283{
7284 struct lysc_ctx ctx = {0};
7285 struct lysc_module *mod_c;
7286 struct lysp_module *sp;
7287 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007288 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007289 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007290 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007291 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007292 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007293 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007294 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007295
Michal Vasko7a0b0762020-09-02 16:37:01 +02007296 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007297
Michal Vasko7a0b0762020-09-02 16:37:01 +02007298 if (!mod->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007299 /* just imported modules are not compiled */
7300 return LY_SUCCESS;
7301 }
7302
Michal Vasko7a0b0762020-09-02 16:37:01 +02007303 compile_id = ++mod->ctx->module_set_id;
7304 sp = mod->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007305
Michal Vasko7a0b0762020-09-02 16:37:01 +02007306 ctx.ctx = mod->ctx;
7307 ctx.mod = mod;
7308 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007309 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007310 ctx.path_len = 1;
7311 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007312
Michal Vasko7a0b0762020-09-02 16:37:01 +02007313 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
7314 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7315 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007316
Michal Vasko7c8439f2020-08-05 13:25:19 +02007317 LY_ARRAY_FOR(sp->imports, u) {
7318 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7319 }
Radek Krejci0935f412019-08-20 16:15:18 +02007320
Radek Krejci14915cc2020-09-14 17:28:13 +02007321 /* features precompilation */
7322 if (!mod->features && sp->features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007323 /* features are compiled directly into the compiled module structure,
7324 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci14915cc2020-09-14 17:28:13 +02007325 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007326 LY_CHECK_GOTO(ret, error);
Radek Krejci14915cc2020-09-14 17:28:13 +02007327 } /* else the features are already precompiled */
Michal Vasko33ff9422020-07-03 09:50:39 +02007328
Radek Krejci14915cc2020-09-14 17:28:13 +02007329 /* similarly, identities precompilation */
Radek Krejci80d281e2020-09-14 17:42:54 +02007330 if (!mod->identities && sp->identities) {
7331 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod->identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02007332 LY_CHECK_GOTO(ret, error);
7333 }
Radek Krejci14915cc2020-09-14 17:28:13 +02007334
7335 /* compile submodules
7336 * - must be between features/identities precompilation and finishing their compilation to cover features/identities from
7337 * submodules */
7338 LY_ARRAY_FOR(sp->includes, u) {
7339 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
7340 }
7341
7342 /* finish feature compilation, not only for the main module, but also for the submodules.
7343 * Due to possible forward references, it must be done when all the features (including submodules)
7344 * are present. */
7345 LY_ARRAY_FOR(sp->features, u) {
7346 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod->features);
7347 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7348 }
7349 lysc_update_path(&ctx, NULL, "{submodule}");
7350 LY_ARRAY_FOR(sp->includes, v) {
7351 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7352 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
7353 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod->features);
7354 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7355 }
7356 lysc_update_path(&ctx, NULL, NULL);
7357 }
7358 lysc_update_path(&ctx, NULL, NULL);
7359
7360 /* identities, work similarly to features with the precompilation */
Radek Krejci19a96102018-11-15 13:38:09 +01007361 if (sp->identities) {
Radek Krejci80d281e2020-09-14 17:42:54 +02007362 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007363 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007364 lysc_update_path(&ctx, NULL, "{submodule}");
7365 LY_ARRAY_FOR(sp->includes, v) {
7366 if (sp->includes[v].submodule->identities) {
7367 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci80d281e2020-09-14 17:42:54 +02007368 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod->identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02007369 LY_CHECK_GOTO(ret, error);
7370 lysc_update_path(&ctx, NULL, NULL);
7371 }
7372 }
Radek Krejci327de162019-06-14 12:52:07 +02007373 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007374
Radek Krejci95710c92019-02-11 15:49:55 +01007375 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007376 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007377 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007378 }
Radek Krejci95710c92019-02-11 15:49:55 +01007379
Radek Krejciec4da802019-05-02 13:02:41 +02007380 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7381 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007382
Radek Krejci95710c92019-02-11 15:49:55 +01007383 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007384 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007385 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007386 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007387 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007388
Radek Krejci474f9b82019-07-24 11:36:37 +02007389 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007390 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007391
Radek Krejci0935f412019-08-20 16:15:18 +02007392 /* extension instances TODO cover extension instances from submodules */
7393 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007394
Michal Vasko004d3152020-06-11 19:59:22 +02007395 /* finish compilation for all unresolved items in the context */
7396 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007397
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007398 /* validate non-instantiated groupings from the parsed schema,
7399 * without it we would accept even the schemas with invalid grouping specification */
7400 ctx.options |= LYSC_OPT_GROUPING;
7401 LY_ARRAY_FOR(sp->groupings, u) {
7402 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007403 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007404 }
7405 }
7406 LY_LIST_FOR(sp->data, node_p) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007407 grps = (struct lysp_grp *)lysp_node_groupings(node_p);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007408 LY_ARRAY_FOR(grps, u) {
7409 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007410 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007411 }
7412 }
7413 }
7414
Radek Krejci474f9b82019-07-24 11:36:37 +02007415 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7416 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7417 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7418 }
7419
Michal Vasko8d544252020-03-02 10:19:52 +01007420#if 0
7421 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7422 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7423 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7424 * the anotation definitions available in the internal schema structure. */
7425 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7426 if (lyp_add_ietf_netconf_annotations(mod)) {
7427 lys_free(mod, NULL, 1, 1);
7428 return NULL;
7429 }
7430 }
7431#endif
7432
7433 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Michal Vasko7a0b0762020-09-02 16:37:01 +02007434 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
7435 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007436 }
7437
Radek Krejci1c0c3442019-07-23 16:08:47 +02007438 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007439 ly_set_erase(&ctx.xpath, NULL);
7440 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007441 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007442 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007443 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007444
Radek Krejciec4da802019-05-02 13:02:41 +02007445 if (ctx.options & LYSC_OPT_FREE_SP) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02007446 lysp_module_free(mod->parsed);
7447 mod->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007448 }
7449
Radek Krejciec4da802019-05-02 13:02:41 +02007450 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007451 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007452 for (i = 0; i < ctx.ctx->list.count; ++i) {
7453 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007454 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007455 m->implemented = 1;
7456 }
7457 }
7458 }
7459
Radek Krejci19a96102018-11-15 13:38:09 +01007460 return LY_SUCCESS;
7461
7462error:
Michal Vasko7a0b0762020-09-02 16:37:01 +02007463 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007464 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007465 ly_set_erase(&ctx.xpath, NULL);
7466 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007467 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007468 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007469 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007470 lysc_module_free(mod_c, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02007471 mod->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007472
Radek Krejcia46012b2020-08-12 15:41:04 +02007473 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7474 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7475 * 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 +02007476 for (i = 0; i < ctx.ctx->list.count; ++i) {
7477 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007478 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007479 /* revert features list to the precompiled state */
7480 lys_feature_precompile_revert(&ctx, m);
7481 /* mark module as imported-only / not-implemented */
7482 m->implemented = 0;
7483 /* free the compiled version of the module */
7484 lysc_module_free(m->compiled, NULL);
7485 m->compiled = NULL;
7486 }
7487 }
7488
Radek Krejci19a96102018-11-15 13:38:09 +01007489 return ret;
7490}