blob: 5541dd5a9e637cae493f0a03b7231a29d5f7a226 [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,
45 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
46
Radek Krejci19a96102018-11-15 13:38:09 +010047/**
48 * @brief Duplicate string into dictionary
49 * @param[in] CTX libyang context of the dictionary.
50 * @param[in] ORIG String to duplicate.
51 * @param[out] DUP Where to store the result.
52 */
53#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010056 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020057 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
58 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010060 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejciec4da802019-05-02 13:02:41 +020066#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010067 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020068 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
69 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
70 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010071 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020072 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010073 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
74 } \
75 }
76
Radek Krejci0935f412019-08-20 16:15:18 +020077#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
78 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020079 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
80 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020081 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010082 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010088 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020089 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
90 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
91 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010092 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020093 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010094 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
95 } \
96 }
97
Radek Krejciec4da802019-05-02 13:02:41 +020098#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010099 if (MEMBER_P) { \
100 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
101 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200102 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100103 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
104 }
105
Radek Krejciec4da802019-05-02 13:02:41 +0200106#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100107 if (MEMBER_P) { \
108 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200109 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100110 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200111 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100112 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
113 }
114
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100115#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100116 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200117 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100118 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100119 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
120 return LY_EVALID; \
121 } \
122 } \
123 }
124
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100125#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
126 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200127 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100128 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
129 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
130 return LY_EVALID; \
131 } \
132 } \
133 }
134
135struct lysc_ext *
136lysc_ext_dup(struct lysc_ext *orig)
137{
138 ++orig->refcount;
139 return orig;
140}
141
Radek Krejci19a96102018-11-15 13:38:09 +0100142static struct lysc_ext_instance *
143lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
144{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100145 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100146 (void) ctx;
147 (void) orig;
148 return NULL;
149}
150
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200151static LY_ERR
152lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
153 struct lys_module *dflt_mod)
154{
155 struct lysc_incomplete_dflt *r;
156 uint32_t i;
157
158 for (i = 0; i < ctx->dflts.count; ++i) {
159 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
160 if (r->leaf == leaf) {
161 /* just replace the default */
162 r->dflt = dflt;
163 return LY_SUCCESS;
164 }
165 }
166
167 r = malloc(sizeof *r);
168 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
169 r->leaf = leaf;
170 r->dflt = dflt;
171 r->dflts = NULL;
172 r->dflt_mod = dflt_mod;
173 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
174
175 return LY_SUCCESS;
176}
177
Radek Krejcib56c7502019-02-13 14:19:54 +0100178/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200179 * @brief Add record into the compile context's list of incomplete default values.
180 * @param[in] ctx Compile context with the incomplete default values list.
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200181 * @param[in] term Term context node with the default value.
182 * @param[in] value String default value.
183 * @param[in] val_len Length of @p value.
Radek Krejci474f9b82019-07-24 11:36:37 +0200184 * @param[in] dflt_mod Module of the default value definition to store in the record.
185 * @return LY_EMEM in case of memory allocation failure.
186 * @return LY_SUCCESS
187 */
188static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200189lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts,
190 struct lys_module *dflt_mod)
Radek Krejci474f9b82019-07-24 11:36:37 +0200191{
192 struct lysc_incomplete_dflt *r;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200193 uint32_t i;
194
195 for (i = 0; i < ctx->dflts.count; ++i) {
196 r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i];
197 if (r->llist == llist) {
198 /* just replace the defaults */
199 r->dflts = dflts;
200 return LY_SUCCESS;
201 }
202 }
203
Radek Krejci474f9b82019-07-24 11:36:37 +0200204 r = malloc(sizeof *r);
205 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200206 r->llist = llist;
207 r->dflt = NULL;
208 r->dflts = dflts;
Radek Krejci474f9b82019-07-24 11:36:37 +0200209 r->dflt_mod = dflt_mod;
210 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
211
212 return LY_SUCCESS;
213}
214
215/**
216 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
217 * @param[in] ctx Compile context with the incomplete default values list.
218 * @param[in] dflt Incomplete default values identifying the record to remove.
219 */
220static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200221lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term)
Radek Krejci474f9b82019-07-24 11:36:37 +0200222{
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200223 uint32_t u;
Radek Krejci474f9b82019-07-24 11:36:37 +0200224 struct lysc_incomplete_dflt *r;
225
226 for (u = 0; u < ctx->dflts.count; ++u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200227 r = ctx->dflts.objs[u];
228 if (r->leaf == (struct lysc_node_leaf *)term) {
Radek Krejci474f9b82019-07-24 11:36:37 +0200229 free(ctx->dflts.objs[u]);
230 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
231 --ctx->dflts.count;
232 return;
233 }
234 }
235}
236
Radek Krejci0e59c312019-08-15 15:34:15 +0200237void
Radek Krejci327de162019-06-14 12:52:07 +0200238lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
239{
240 int len;
241 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
242
243 if (!name) {
244 /* removing last path segment */
245 if (ctx->path[ctx->path_len - 1] == '}') {
Radek Krejci1e008d22020-08-17 11:37:37 +0200246 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200247 if (ctx->path[ctx->path_len] == '=') {
248 ctx->path[ctx->path_len++] = '}';
249 } else {
250 /* not a top-level special tag, remove also preceiding '/' */
251 goto remove_nodelevel;
252 }
253 } else {
254remove_nodelevel:
Radek Krejci1e008d22020-08-17 11:37:37 +0200255 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200256 if (ctx->path_len == 0) {
257 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200258 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200259 }
260 }
261 /* set new terminating NULL-byte */
262 ctx->path[ctx->path_len] = '\0';
263 } else {
264 if (ctx->path_len > 1) {
265 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
266 /* extension of the special tag */
267 nextlevel = 2;
268 --ctx->path_len;
269 } else {
270 /* there is already some path, so add next level */
271 nextlevel = 1;
272 }
273 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
274
275 if (nextlevel != 2) {
276 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
277 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200278 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200279 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200280 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200281 }
282 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200283 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200284 }
Radek Krejciacc79042019-07-25 14:14:57 +0200285 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
286 /* output truncated */
287 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
288 } else {
289 ctx->path_len += len;
290 }
Radek Krejci327de162019-06-14 12:52:07 +0200291 }
292}
293
294/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100295 * @brief Duplicate the compiled pattern structure.
296 *
297 * Instead of duplicating memory, the reference counter in the @p orig is increased.
298 *
299 * @param[in] orig The pattern structure to duplicate.
300 * @return The duplicated structure to use.
301 */
Radek Krejci19a96102018-11-15 13:38:09 +0100302static struct lysc_pattern*
303lysc_pattern_dup(struct lysc_pattern *orig)
304{
305 ++orig->refcount;
306 return orig;
307}
308
Radek Krejcib56c7502019-02-13 14:19:54 +0100309/**
310 * @brief Duplicate the array of compiled patterns.
311 *
312 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
313 *
314 * @param[in] ctx Libyang context for logging.
315 * @param[in] orig The patterns sized array to duplicate.
316 * @return New sized array as a copy of @p orig.
317 * @return NULL in case of memory allocation error.
318 */
Radek Krejci19a96102018-11-15 13:38:09 +0100319static struct lysc_pattern**
320lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
321{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100322 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200323 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100324
Radek Krejcib56c7502019-02-13 14:19:54 +0100325 assert(orig);
326
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200327 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100328 LY_ARRAY_FOR(orig, u) {
329 dup[u] = lysc_pattern_dup(orig[u]);
330 LY_ARRAY_INCREMENT(dup);
331 }
332 return dup;
333}
334
Radek Krejcib56c7502019-02-13 14:19:54 +0100335/**
336 * @brief Duplicate compiled range structure.
337 *
338 * @param[in] ctx Libyang context for logging.
339 * @param[in] orig The range structure to be duplicated.
340 * @return New compiled range structure as a copy of @p orig.
341 * @return NULL in case of memory allocation error.
342 */
Radek Krejci19a96102018-11-15 13:38:09 +0100343struct lysc_range*
344lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
345{
346 struct lysc_range *dup;
347 LY_ERR ret;
348
Radek Krejcib56c7502019-02-13 14:19:54 +0100349 assert(orig);
350
Radek Krejci19a96102018-11-15 13:38:09 +0100351 dup = calloc(1, sizeof *dup);
352 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
353 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200354 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
355 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
356 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100357 }
358 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
359 DUP_STRING(ctx, orig->emsg, dup->emsg);
360 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
361
362 return dup;
363cleanup:
364 free(dup);
365 (void) ret; /* set but not used due to the return type */
366 return NULL;
367}
368
Radek Krejcib56c7502019-02-13 14:19:54 +0100369/**
370 * @brief Stack for processing if-feature expressions.
371 */
Radek Krejci19a96102018-11-15 13:38:09 +0100372struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100373 int size; /**< number of items in the stack */
374 int index; /**< first empty item */
375 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100376};
377
Radek Krejcib56c7502019-02-13 14:19:54 +0100378/**
379 * @brief Add @ref ifftokens into the stack.
380 * @param[in] stack The if-feature stack to use.
381 * @param[in] value One of the @ref ifftokens to store in the stack.
382 * @return LY_EMEM in case of memory allocation error
383 * @return LY_ESUCCESS if the value successfully stored.
384 */
Radek Krejci19a96102018-11-15 13:38:09 +0100385static LY_ERR
386iff_stack_push(struct iff_stack *stack, uint8_t value)
387{
388 if (stack->index == stack->size) {
389 stack->size += 4;
390 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
391 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
392 }
393 stack->stack[stack->index++] = value;
394 return LY_SUCCESS;
395}
396
Radek Krejcib56c7502019-02-13 14:19:54 +0100397/**
398 * @brief Get (and remove) the last item form the stack.
399 * @param[in] stack The if-feature stack to use.
400 * @return The value from the top of the stack.
401 */
Radek Krejci19a96102018-11-15 13:38:09 +0100402static uint8_t
403iff_stack_pop(struct iff_stack *stack)
404{
Radek Krejcib56c7502019-02-13 14:19:54 +0100405 assert(stack && stack->index);
406
Radek Krejci19a96102018-11-15 13:38:09 +0100407 stack->index--;
408 return stack->stack[stack->index];
409}
410
Radek Krejcib56c7502019-02-13 14:19:54 +0100411/**
412 * @brief Clean up the stack.
413 * @param[in] stack The if-feature stack to use.
414 */
Radek Krejci19a96102018-11-15 13:38:09 +0100415static void
416iff_stack_clean(struct iff_stack *stack)
417{
418 stack->size = 0;
419 free(stack->stack);
420}
421
Radek Krejcib56c7502019-02-13 14:19:54 +0100422/**
423 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
424 * (libyang format of the if-feature expression).
425 * @param[in,out] list The 2bits array to modify.
426 * @param[in] op The operand (@ref ifftokens) to store.
427 * @param[in] pos Position (0-based) where to store the given @p op.
428 */
Radek Krejci19a96102018-11-15 13:38:09 +0100429static void
430iff_setop(uint8_t *list, uint8_t op, int pos)
431{
432 uint8_t *item;
433 uint8_t mask = 3;
434
435 assert(pos >= 0);
436 assert(op <= 3); /* max 2 bits */
437
438 item = &list[pos / 4];
439 mask = mask << 2 * (pos % 4);
440 *item = (*item) & ~mask;
441 *item = (*item) | (op << 2 * (pos % 4));
442}
443
Radek Krejcib56c7502019-02-13 14:19:54 +0100444#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
445#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100446
Radek Krejci0af46292019-01-11 16:02:31 +0100447/**
448 * @brief Find a feature of the given name and referenced in the given module.
449 *
450 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
451 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
452 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
453 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
454 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
455 * assigned till that time will be still valid.
456 *
457 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
458 * @param[in] name Name of the feature including possible prefix.
459 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
460 * @return Pointer to the feature structure if found, NULL otherwise.
461 */
Radek Krejci19a96102018-11-15 13:38:09 +0100462static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100463lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100464{
465 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200466 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100467 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100468
Radek Krejci120d8542020-08-12 09:29:16 +0200469 assert(mod);
470
Radek Krejci19a96102018-11-15 13:38:09 +0100471 for (i = 0; i < len; ++i) {
472 if (name[i] == ':') {
473 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100474 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100475 LY_CHECK_RET(!mod, NULL);
476
477 name = &name[i + 1];
478 len = len - i - 1;
479 }
480 }
481
482 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100483 if (mod->implemented) {
484 /* module is implemented so there is already the compiled schema */
485 flist = mod->compiled->features;
486 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200487 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100488 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200489 LY_ARRAY_FOR(flist, u) {
490 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200491 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100492 return f;
493 }
494 }
495
496 return NULL;
497}
498
Michal Vasko8d544252020-03-02 10:19:52 +0100499/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100500 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
501 */
502static LY_ERR
503lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
504{
505 LY_ERR ret = LY_SUCCESS;
506
507 if (!ext_p->compiled) {
508 lysc_update_path(ctx, NULL, "{extension}");
509 lysc_update_path(ctx, NULL, ext_p->name);
510
511 /* compile the extension definition */
512 ext_p->compiled = calloc(1, sizeof **ext);
513 ext_p->compiled->refcount = 1;
514 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
515 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
516 ext_p->compiled->module = (struct lys_module *)ext_mod;
517 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
518
519 lysc_update_path(ctx, NULL, NULL);
520 lysc_update_path(ctx, NULL, NULL);
521
522 /* find extension definition plugin */
523 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
524 }
525
526 *ext = lysc_ext_dup(ext_p->compiled);
527
528done:
529 return ret;
530}
531
532/**
Michal Vasko8d544252020-03-02 10:19:52 +0100533 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
534 *
535 * @param[in] ctx Compilation context.
536 * @param[in] ext_p Parsed extension instance.
537 * @param[in,out] ext Prepared compiled extension instance.
538 * @param[in] parent Extension instance parent.
539 * @param[in] parent_type Extension instance parent type.
540 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
541 */
Radek Krejci19a96102018-11-15 13:38:09 +0100542static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100543lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
544 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100545{
Radek Krejci7c960162019-09-18 14:16:12 +0200546 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100547 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200548 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200549 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200550 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100551
552 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
553 ext->insubstmt = ext_p->insubstmt;
554 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200555 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200556 ext->parent = parent;
557 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100558
Radek Krejcif56e2a42019-09-09 14:15:25 +0200559 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200560
Radek Krejci19a96102018-11-15 13:38:09 +0100561 /* get module where the extension definition should be placed */
Radek Krejci1e008d22020-08-17 11:37:37 +0200562 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
Radek Krejci7c960162019-09-18 14:16:12 +0200563 if (ext_p->yin) {
564 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
565 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
566 * YANG (import) prefix must be done here. */
567 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
568 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
569 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200570 } else {
571 assert(ctx->mod_def->parsed);
572 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
573 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200574 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200575 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 +0200576 ret = LY_EMEM, cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200577 prefixed_name = lydict_insert_zc(ctx->ctx, s);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200578 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200579 break;
580 }
581 }
582 }
583 if (!prefixed_name) {
584 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
585 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
586 goto cleanup;
587 }
588 } else {
589 prefixed_name = ext_p->name;
590 }
591 lysc_update_path(ctx, NULL, prefixed_name);
592
Michal Vasko8d544252020-03-02 10:19:52 +0100593 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200594 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100595 if (!ext_mod) {
596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
597 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
598 goto cleanup;
599 } else if (!ext_mod->parsed->extensions) {
600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
601 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
602 prefixed_name, ext_mod->name);
603 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 Krejci7eb54ba2020-05-18 16:30:04 +0200612 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
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);
619 goto cleanup;
620 }
Radek Krejci0935f412019-08-20 16:15:18 +0200621
Radek Krejcif56e2a42019-09-09 14:15:25 +0200622 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
623 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
624 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200625 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200626 /* Schema was parsed from YIN and an argument is expected, ... */
627 struct lysp_stmt *stmt = NULL;
628
629 if (ext->def->flags & LYS_YINELEM_TRUE) {
630 /* ... argument was the first XML child element */
631 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
632 /* TODO check namespace of the statement */
633 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
634 stmt = ext_p->child;
635 }
636 }
637 } else {
638 /* ... argument was one of the XML attributes which are represented as child stmt
639 * with LYS_YIN_ATTR flag */
640 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
641 if (!strcmp(stmt->stmt, ext->def->argument)) {
642 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200643 break;
644 }
645 }
646 }
647 if (!stmt) {
648 /* missing extension's argument */
649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200650 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
651 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200652
653 }
654 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
655 stmt->flags |= LYS_YIN_ARGUMENT;
656 }
Radek Krejci7c960162019-09-18 14:16:12 +0200657 if (prefixed_name != ext_p->name) {
658 lydict_remove(ctx->ctx, ext_p->name);
659 ext_p->name = prefixed_name;
660 if (!ext_p->argument && ext->argument) {
661 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
662 }
663 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200664
Radek Krejci0935f412019-08-20 16:15:18 +0200665 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200666 if (ext->argument) {
667 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
668 }
Radek Krejci7c960162019-09-18 14:16:12 +0200669 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200670 if (ext->argument) {
671 lysc_update_path(ctx, NULL, NULL);
672 }
Radek Krejci0935f412019-08-20 16:15:18 +0200673 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200674 ext_p->compiled = ext;
675
Radek Krejci7c960162019-09-18 14:16:12 +0200676 ret = LY_SUCCESS;
677cleanup:
678 if (prefixed_name && prefixed_name != ext_p->name) {
679 lydict_remove(ctx->ctx, prefixed_name);
680 }
681
Radek Krejcif56e2a42019-09-09 14:15:25 +0200682 lysc_update_path(ctx, NULL, NULL);
683 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200684
Radek Krejci7c960162019-09-18 14:16:12 +0200685 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200686}
687
688/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100689 * @brief Compile information from the if-feature statement
690 * @param[in] ctx Compile context.
691 * @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 +0100692 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
693 * @return LY_ERR value.
694 */
Radek Krejci19a96102018-11-15 13:38:09 +0100695static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200696lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100697{
698 const char *c = *value;
699 int r, rc = EXIT_FAILURE;
700 int i, j, last_not, checkversion = 0;
701 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
702 uint8_t op;
703 struct iff_stack stack = {0, 0, NULL};
704 struct lysc_feature *f;
705
706 assert(c);
707
708 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
709 for (i = j = last_not = 0; c[i]; i++) {
710 if (c[i] == '(') {
711 j++;
712 checkversion = 1;
713 continue;
714 } else if (c[i] == ')') {
715 j--;
716 continue;
717 } else if (isspace(c[i])) {
718 checkversion = 1;
719 continue;
720 }
721
722 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200723 int sp;
724 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
725 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100726 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
727 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
728 return LY_EVALID;
729 } else if (!isspace(c[i + r])) {
730 /* feature name starting with the not/and/or */
731 last_not = 0;
732 f_size++;
733 } else if (c[i] == 'n') { /* not operation */
734 if (last_not) {
735 /* double not */
736 expr_size = expr_size - 2;
737 last_not = 0;
738 } else {
739 last_not = 1;
740 }
741 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200742 if (f_exp != f_size) {
743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
744 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
745 return LY_EVALID;
746 }
Radek Krejci19a96102018-11-15 13:38:09 +0100747 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200748
Radek Krejci19a96102018-11-15 13:38:09 +0100749 /* not a not operation */
750 last_not = 0;
751 }
752 i += r;
753 } else {
754 f_size++;
755 last_not = 0;
756 }
757 expr_size++;
758
759 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200760 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100761 i--;
762 break;
763 }
764 i++;
765 }
766 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200767 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100768 /* not matching count of ( and ) */
769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
770 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
771 return LY_EVALID;
772 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200773 if (f_exp != f_size) {
774 /* features do not match the needed arguments for the logical operations */
775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
776 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
777 "the required number of operands for the operations.", *value);
778 return LY_EVALID;
779 }
Radek Krejci19a96102018-11-15 13:38:09 +0100780
781 if (checkversion || expr_size > 1) {
782 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100783 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100784 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
785 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
786 return LY_EVALID;
787 }
788 }
789
790 /* allocate the memory */
791 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
792 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
793 stack.stack = malloc(expr_size * sizeof *stack.stack);
794 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
795
796 stack.size = expr_size;
797 f_size--; expr_size--; /* used as indexes from now */
798
799 for (i--; i >= 0; i--) {
800 if (c[i] == ')') {
801 /* push it on stack */
802 iff_stack_push(&stack, LYS_IFF_RP);
803 continue;
804 } else if (c[i] == '(') {
805 /* pop from the stack into result all operators until ) */
806 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
807 iff_setop(iff->expr, op, expr_size--);
808 }
809 continue;
810 } else if (isspace(c[i])) {
811 continue;
812 }
813
814 /* end of operator or operand -> find beginning and get what is it */
815 j = i + 1;
816 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
817 i--;
818 }
819 i++; /* go back by one step */
820
821 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
822 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
823 /* double not */
824 iff_stack_pop(&stack);
825 } else {
826 /* not has the highest priority, so do not pop from the stack
827 * as in case of AND and OR */
828 iff_stack_push(&stack, LYS_IFF_NOT);
829 }
830 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
831 /* as for OR - pop from the stack all operators with the same or higher
832 * priority and store them to the result, then push the AND to the stack */
833 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
834 op = iff_stack_pop(&stack);
835 iff_setop(iff->expr, op, expr_size--);
836 }
837 iff_stack_push(&stack, LYS_IFF_AND);
838 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
839 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
840 op = iff_stack_pop(&stack);
841 iff_setop(iff->expr, op, expr_size--);
842 }
843 iff_stack_push(&stack, LYS_IFF_OR);
844 } else {
845 /* feature name, length is j - i */
846
847 /* add it to the expression */
848 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
849
850 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100851 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
852 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
853 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
854 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100855 iff->features[f_size] = f;
856 LY_ARRAY_INCREMENT(iff->features);
857 f_size--;
858 }
859 }
860 while (stack.index) {
861 op = iff_stack_pop(&stack);
862 iff_setop(iff->expr, op, expr_size--);
863 }
864
865 if (++expr_size || ++f_size) {
866 /* not all expected operators and operands found */
867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
868 "Invalid value \"%s\" of if-feature - processing error.", *value);
869 rc = LY_EINT;
870 } else {
871 rc = LY_SUCCESS;
872 }
873
874error:
875 /* cleanup */
876 iff_stack_clean(&stack);
877
878 return rc;
879}
880
Radek Krejcib56c7502019-02-13 14:19:54 +0100881/**
Michal Vasko175012e2019-11-06 15:49:14 +0100882 * @brief Get the XPath context node for the given schema node.
883 * @param[in] start The schema node where the XPath expression appears.
884 * @return The context node to evaluate XPath expression in given schema node.
885 * @return NULL in case the context node is the root node.
886 */
887static struct lysc_node *
888lysc_xpath_context(struct lysc_node *start)
889{
Michal Vasko1bf09392020-03-27 12:38:10 +0100890 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Radek Krejci1e008d22020-08-17 11:37:37 +0200891 start = start->parent) {}
Michal Vasko175012e2019-11-06 15:49:14 +0100892 return start;
893}
894
895/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100896 * @brief Compile information from the when statement
897 * @param[in] ctx Compile context.
898 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100899 * @param[in] flags Flags of the node with the "when" defiition.
900 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100901 * @param[out] when Pointer where to store pointer to the created compiled when structure.
902 * @return LY_ERR value.
903 */
Radek Krejci19a96102018-11-15 13:38:09 +0100904static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100905lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100906{
Radek Krejci19a96102018-11-15 13:38:09 +0100907 LY_ERR ret = LY_SUCCESS;
908
Radek Krejci00b874b2019-02-12 10:54:50 +0100909 *when = calloc(1, sizeof **when);
910 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200911 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200912 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100913 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100914 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
915 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
916 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200917 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100918 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100919
920done:
921 return ret;
922}
923
Radek Krejcib56c7502019-02-13 14:19:54 +0100924/**
925 * @brief Compile information from the must statement
926 * @param[in] ctx Compile context.
927 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100928 * @param[in,out] must Prepared (empty) compiled must structure to fill.
929 * @return LY_ERR value.
930 */
Radek Krejci19a96102018-11-15 13:38:09 +0100931static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200932lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100933{
Radek Krejci19a96102018-11-15 13:38:09 +0100934 LY_ERR ret = LY_SUCCESS;
935
Michal Vasko004d3152020-06-11 19:59:22 +0200936 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100937 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200938 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100939 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
940 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100941 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
942 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200943 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100944
945done:
946 return ret;
947}
948
Radek Krejcib56c7502019-02-13 14:19:54 +0100949/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200950 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100951 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200952 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100953 * @return LY_ERR value.
954 */
Radek Krejci19a96102018-11-15 13:38:09 +0100955static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200956lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100957{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200958 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100959 LY_ERR ret = LY_SUCCESS;
960
Radek Krejci7f2a5362018-11-28 13:05:37 +0100961 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
962 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100963 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200964 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100965 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200966 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200967 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200968 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200969 LOGINT(ctx->ctx);
970 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200971 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
Michal Vasko3a41dff2020-07-15 14:30:28 +0200972 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200973 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200974 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200975 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200976 mod = NULL;
977 }
Radek Krejci19a96102018-11-15 13:38:09 +0100978 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200979 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100980 }
981 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200982 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100983 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200984 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100985 return LY_ENOTFOUND;
986 }
987 }
Radek Krejci19a96102018-11-15 13:38:09 +0100988 }
989
Radek Krejci19a96102018-11-15 13:38:09 +0100990 return ret;
991}
992
Michal Vasko33ff9422020-07-03 09:50:39 +0200993LY_ERR
994lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
995 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100996{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200997 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200998 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +0100999 LY_ERR ret = LY_SUCCESS;
1000
Michal Vasko33ff9422020-07-03 09:50:39 +02001001 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +02001002
Michal Vasko33ff9422020-07-03 09:50:39 +02001003 if (!ctx_sc) {
1004 context.ctx = ctx;
1005 context.mod = module;
Radek Krejci120d8542020-08-12 09:29:16 +02001006 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +02001007 context.path_len = 1;
1008 context.path[0] = '/';
1009 ctx_sc = &context;
1010 }
Radek Krejci19a96102018-11-15 13:38:09 +01001011
Michal Vasko33ff9422020-07-03 09:50:39 +02001012 if (!identities_p) {
1013 return LY_SUCCESS;
1014 }
1015 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001016 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02001017 }
1018
1019 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001020 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001021 LY_ARRAY_FOR(identities_p, u) {
1022 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
1023
1024 LY_ARRAY_INCREMENT(*identities);
1025 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
1026 DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
1027 DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
1028 DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
1029 (*identities)[offset + u].module = ctx_sc->mod;
1030 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
1031 lys_compile_iffeature, ret, done);
1032 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
1033 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
1034 LYEXT_PAR_IDENT, ret, done);
1035 (*identities)[offset + u].flags = identities_p[u].flags;
1036
1037 lysc_update_path(ctx_sc, NULL, NULL);
1038 }
1039 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001040done:
1041 return ret;
1042}
1043
Radek Krejcib56c7502019-02-13 14:19:54 +01001044/**
1045 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1046 *
1047 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1048 *
1049 * @param[in] ctx Compile context for logging.
1050 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1051 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1052 * @return LY_SUCCESS if everything is ok.
1053 * @return LY_EVALID if the identity is derived from itself.
1054 */
Radek Krejci38222632019-02-12 16:55:05 +01001055static LY_ERR
1056lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1057{
1058 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001059 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001060 struct ly_set recursion = {0};
1061 struct lysc_ident *drv;
1062
1063 if (!derived) {
1064 return LY_SUCCESS;
1065 }
1066
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001067 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001068 if (ident == derived[u]) {
1069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1070 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1071 goto cleanup;
1072 }
1073 ly_set_add(&recursion, derived[u], 0);
1074 }
1075
1076 for (v = 0; v < recursion.count; ++v) {
1077 drv = recursion.objs[v];
1078 if (!drv->derived) {
1079 continue;
1080 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001081 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001082 if (ident == drv->derived[u]) {
1083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1084 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1085 goto cleanup;
1086 }
1087 ly_set_add(&recursion, drv->derived[u], 0);
1088 }
1089 }
1090 ret = LY_SUCCESS;
1091
1092cleanup:
1093 ly_set_erase(&recursion, NULL);
1094 return ret;
1095}
1096
Radek Krejcia3045382018-11-22 14:30:31 +01001097/**
1098 * @brief Find and process the referenced base identities from another identity or identityref
1099 *
Radek Krejciaca74032019-06-04 08:53:06 +02001100 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001101 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1102 * to distinguish these two use cases.
1103 *
1104 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1105 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001106 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001107 * @param[in] bases Array of bases of identityref to fill in.
1108 * @return LY_ERR value.
1109 */
Radek Krejci19a96102018-11-15 13:38:09 +01001110static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001111lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
1112 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001113{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001114 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001115 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001116 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001117 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001118
1119 assert(ident || bases);
1120
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001121 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001122 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1123 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1124 return LY_EVALID;
1125 }
1126
Michal Vasko33ff9422020-07-03 09:50:39 +02001127 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001128 s = strchr(bases_p[u], ':');
1129 if (s) {
1130 /* prefixed identity */
1131 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001132 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001133 } else {
1134 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001135 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001136 }
1137 if (!mod) {
1138 if (ident) {
1139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1140 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1141 } else {
1142 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1143 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1144 }
1145 return LY_EVALID;
1146 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001147
Radek Krejci555cb5b2018-11-16 14:54:33 +01001148 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001149 if (mod->compiled) {
1150 identities = mod->compiled->identities;
1151 } else {
1152 identities = mod->dis_identities;
1153 }
1154 LY_ARRAY_FOR(identities, v) {
1155 if (!strcmp(name, identities[v].name)) {
1156 if (ident) {
1157 if (ident == &identities[v]) {
1158 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1159 "Identity \"%s\" is derived from itself.", ident->name);
1160 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001161 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001162 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1163 /* we have match! store the backlink */
1164 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1165 *idref = ident;
1166 } else {
1167 /* we have match! store the found identity */
1168 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1169 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001170 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001171 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001172 }
1173 }
1174 if (!idref || !(*idref)) {
1175 if (ident) {
1176 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1177 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1178 } else {
1179 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1180 "Unable to find base (%s) of identityref.", bases_p[u]);
1181 }
1182 return LY_EVALID;
1183 }
1184 }
1185 return LY_SUCCESS;
1186}
1187
Radek Krejcia3045382018-11-22 14:30:31 +01001188/**
1189 * @brief For the given array of identities, set the backlinks from all their base identities.
1190 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1191 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1192 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1193 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1194 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001195static LY_ERR
1196lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1197{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001198 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001199
Michal Vasko33ff9422020-07-03 09:50:39 +02001200 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001201 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001202 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001203 continue;
1204 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001205 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001206 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module, idents_p[u].bases, &idents[u], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001207 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001208 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001209 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001210 return LY_SUCCESS;
1211}
1212
Radek Krejci0af46292019-01-11 16:02:31 +01001213LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001214lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
1215 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001216{
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 Krejci327de162019-06-14 12:52:07 +02001244 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1245 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1246 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
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
1254 return LY_SUCCESS;
1255}
1256
Radek Krejcia3045382018-11-22 14:30:31 +01001257/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001258 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001259 *
1260 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1261 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001262 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001263 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1264 * being currently processed).
1265 * @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 +01001266 * @return LY_SUCCESS if everything is ok.
1267 * @return LY_EVALID if the feature references indirectly itself.
1268 */
1269static LY_ERR
1270lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1271{
1272 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001273 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001274 struct ly_set recursion = {0};
1275 struct lysc_feature *drv;
1276
1277 if (!depfeatures) {
1278 return LY_SUCCESS;
1279 }
1280
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001281 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001282 if (feature == depfeatures[u]) {
1283 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1284 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1285 goto cleanup;
1286 }
1287 ly_set_add(&recursion, depfeatures[u], 0);
1288 }
1289
1290 for (v = 0; v < recursion.count; ++v) {
1291 drv = recursion.objs[v];
1292 if (!drv->depfeatures) {
1293 continue;
1294 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001295 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001296 if (feature == drv->depfeatures[u]) {
1297 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1298 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1299 goto cleanup;
1300 }
1301 ly_set_add(&recursion, drv->depfeatures[u], 0);
1302 }
1303 }
1304 ret = LY_SUCCESS;
1305
1306cleanup:
1307 ly_set_erase(&recursion, NULL);
1308 return ret;
1309}
1310
1311/**
Radek Krejci0af46292019-01-11 16:02:31 +01001312 * @brief Create pre-compiled features array.
1313 *
1314 * See lys_feature_precompile() for more details.
1315 *
Radek Krejcia3045382018-11-22 14:30:31 +01001316 * @param[in] ctx Compile context.
1317 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001318 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001319 * @return LY_ERR value.
1320 */
Radek Krejci19a96102018-11-15 13:38:09 +01001321static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001322lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001323{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001324 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001325 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001326 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001327
Radek Krejci327de162019-06-14 12:52:07 +02001328
Radek Krejci0af46292019-01-11 16:02:31 +01001329 /* find the preprecompiled feature */
1330 LY_ARRAY_FOR(features, x) {
1331 if (strcmp(features[x].name, feature_p->name)) {
1332 continue;
1333 }
1334 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001335 lysc_update_path(ctx, NULL, "{feature}");
1336 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001337
Radek Krejci0af46292019-01-11 16:02:31 +01001338 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001339 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001340 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001341 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001342 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001343 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001344 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001345 /* check for circular dependency - direct reference first,... */
1346 if (feature == feature->iffeatures[u].features[v]) {
1347 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1348 "Feature \"%s\" is referenced from itself.", feature->name);
1349 return LY_EVALID;
1350 }
1351 /* ... and indirect circular reference */
1352 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1353
Radek Krejci0af46292019-01-11 16:02:31 +01001354 /* add itself into the dependants list */
1355 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1356 *df = feature;
1357 }
Radek Krejci19a96102018-11-15 13:38:09 +01001358 }
Radek Krejci19a96102018-11-15 13:38:09 +01001359 }
1360 }
Radek Krejci327de162019-06-14 12:52:07 +02001361 lysc_update_path(ctx, NULL, NULL);
1362 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001363 done:
1364 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001365 }
Radek Krejci0af46292019-01-11 16:02:31 +01001366
1367 LOGINT(ctx->ctx);
1368 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001369}
1370
Radek Krejcib56c7502019-02-13 14:19:54 +01001371/**
1372 * @brief Revert compiled list of features back to the precompiled state.
1373 *
1374 * 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 +02001375 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001376 *
1377 * @param[in] ctx Compilation context.
1378 * @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 +02001379 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001380 */
Radek Krejci95710c92019-02-11 15:49:55 +01001381static void
1382lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1383{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001384 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001385
Radek Krejcia46012b2020-08-12 15:41:04 +02001386 if (mod->compiled) {
1387 /* keep the dis_features list until the complete lys_module is freed */
1388 mod->dis_features = mod->compiled->features;
1389 mod->compiled->features = NULL;
1390 }
Radek Krejci95710c92019-02-11 15:49:55 +01001391
Michal Vasko33ff9422020-07-03 09:50:39 +02001392 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001393 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001394 LY_ARRAY_FOR(mod->dis_features, u) {
1395 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1396 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001397 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001398 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1399 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001400
Michal Vasko33ff9422020-07-03 09:50:39 +02001401 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1402 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001403 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001404 LY_ARRAY_FREE(mod->dis_features[u].exts);
1405 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001406 }
1407}
1408
Radek Krejcia3045382018-11-22 14:30:31 +01001409/**
1410 * @brief Validate and normalize numeric value from a range definition.
1411 * @param[in] ctx Compile context.
1412 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1413 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1414 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1415 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1416 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1417 * @param[in] value String value of the range boundary.
1418 * @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.
1419 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1420 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1421 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001422LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001423range_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 +01001424{
Radek Krejci6cba4292018-11-15 17:33:29 +01001425 size_t fraction = 0, size;
1426
Radek Krejci19a96102018-11-15 13:38:09 +01001427 *len = 0;
1428
1429 assert(value);
1430 /* parse value */
1431 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1432 return LY_EVALID;
1433 }
1434
1435 if ((value[*len] == '-') || (value[*len] == '+')) {
1436 ++(*len);
1437 }
1438
1439 while (isdigit(value[*len])) {
1440 ++(*len);
1441 }
1442
1443 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001444 if (basetype == LY_TYPE_DEC64) {
1445 goto decimal;
1446 } else {
1447 *valcopy = strndup(value, *len);
1448 return LY_SUCCESS;
1449 }
Radek Krejci19a96102018-11-15 13:38:09 +01001450 }
1451 fraction = *len;
1452
1453 ++(*len);
1454 while (isdigit(value[*len])) {
1455 ++(*len);
1456 }
1457
Radek Krejci6cba4292018-11-15 17:33:29 +01001458 if (basetype == LY_TYPE_DEC64) {
1459decimal:
1460 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001461 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001462 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1463 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1464 *len, value, frdigits);
1465 return LY_EINVAL;
1466 }
1467 if (fraction) {
1468 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1469 } else {
1470 size = (*len) + frdigits + 1;
1471 }
1472 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001473 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1474
Radek Krejci6cba4292018-11-15 17:33:29 +01001475 (*valcopy)[size - 1] = '\0';
1476 if (fraction) {
1477 memcpy(&(*valcopy)[0], &value[0], fraction);
1478 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1479 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1480 } else {
1481 memcpy(&(*valcopy)[0], &value[0], *len);
1482 memset(&(*valcopy)[*len], '0', frdigits);
1483 }
Radek Krejci19a96102018-11-15 13:38:09 +01001484 }
1485 return LY_SUCCESS;
1486}
1487
Radek Krejcia3045382018-11-22 14:30:31 +01001488/**
1489 * @brief Check that values in range are in ascendant order.
1490 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001491 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1492 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001493 * @param[in] value Current value to check.
1494 * @param[in] prev_value The last seen value.
1495 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1496 */
Radek Krejci19a96102018-11-15 13:38:09 +01001497static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001498range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001499{
1500 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001501 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001502 return LY_EEXIST;
1503 }
1504 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001505 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001506 return LY_EEXIST;
1507 }
1508 }
1509 return LY_SUCCESS;
1510}
1511
Radek Krejcia3045382018-11-22 14:30:31 +01001512/**
1513 * @brief Set min/max value of the range part.
1514 * @param[in] ctx Compile context.
1515 * @param[in] part Range part structure to fill.
1516 * @param[in] max Flag to distinguish if storing min or max value.
1517 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1518 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1519 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1520 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1521 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001522 * @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 +01001523 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1524 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1525 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1526 * frdigits value), LY_EMEM.
1527 */
Radek Krejci19a96102018-11-15 13:38:09 +01001528static LY_ERR
Michal Vaskoba417ac2020-08-06 14:48:20 +02001529range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype,
1530 int first, int length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001531{
1532 LY_ERR ret = LY_SUCCESS;
1533 char *valcopy = NULL;
1534 size_t len;
1535
1536 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001537 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001538 LY_CHECK_GOTO(ret, finalize);
1539 }
1540 if (!valcopy && base_range) {
1541 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001542 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001543 } else {
1544 part->min_64 = base_range->parts[0].min_64;
1545 }
1546 if (!first) {
1547 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1548 }
1549 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001550 }
1551
1552 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001553 case LY_TYPE_INT8: /* range */
1554 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001555 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 +01001556 } else if (max) {
1557 part->max_64 = INT64_C(127);
1558 } else {
1559 part->min_64 = INT64_C(-128);
1560 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001561 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001562 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001563 }
1564 break;
1565 case LY_TYPE_INT16: /* range */
1566 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001567 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 +01001568 } else if (max) {
1569 part->max_64 = INT64_C(32767);
1570 } else {
1571 part->min_64 = INT64_C(-32768);
1572 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001573 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001574 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001575 }
1576 break;
1577 case LY_TYPE_INT32: /* range */
1578 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001579 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 +01001580 } else if (max) {
1581 part->max_64 = INT64_C(2147483647);
1582 } else {
1583 part->min_64 = INT64_C(-2147483648);
1584 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001585 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001586 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001587 }
1588 break;
1589 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001590 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001591 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001592 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001593 max ? &part->max_64 : &part->min_64);
1594 } else if (max) {
1595 part->max_64 = INT64_C(9223372036854775807);
1596 } else {
1597 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1598 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001599 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001600 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001601 }
1602 break;
1603 case LY_TYPE_UINT8: /* range */
1604 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001605 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 +01001606 } else if (max) {
1607 part->max_u64 = UINT64_C(255);
1608 } else {
1609 part->min_u64 = UINT64_C(0);
1610 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001611 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001612 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001613 }
1614 break;
1615 case LY_TYPE_UINT16: /* range */
1616 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001617 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 +01001618 } else if (max) {
1619 part->max_u64 = UINT64_C(65535);
1620 } else {
1621 part->min_u64 = UINT64_C(0);
1622 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001623 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001624 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001625 }
1626 break;
1627 case LY_TYPE_UINT32: /* range */
1628 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001629 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 +01001630 } else if (max) {
1631 part->max_u64 = UINT64_C(4294967295);
1632 } else {
1633 part->min_u64 = UINT64_C(0);
1634 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001635 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001636 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001637 }
1638 break;
1639 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001640 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001641 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001642 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001643 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 +01001644 } else if (max) {
1645 part->max_u64 = UINT64_C(18446744073709551615);
1646 } else {
1647 part->min_u64 = UINT64_C(0);
1648 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001649 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001650 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001651 }
1652 break;
1653 default:
1654 LOGINT(ctx->ctx);
1655 ret = LY_EINT;
1656 }
1657
Radek Krejci5969f272018-11-23 10:03:58 +01001658finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001659 if (ret == LY_EDENIED) {
1660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1661 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1662 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1663 } else if (ret == LY_EVALID) {
1664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1665 "Invalid %s restriction - invalid value \"%s\".",
1666 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1667 } else if (ret == LY_EEXIST) {
1668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1669 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001670 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001671 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001672 } else if (!ret && value) {
1673 *value = *value + len;
1674 }
1675 free(valcopy);
1676 return ret;
1677}
1678
Radek Krejcia3045382018-11-22 14:30:31 +01001679/**
1680 * @brief Compile the parsed range restriction.
1681 * @param[in] ctx Compile context.
1682 * @param[in] range_p Parsed range structure to compile.
1683 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1684 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1685 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1686 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1687 * range restriction must be more restrictive than the base_range.
1688 * @param[in,out] range Pointer to the created current range structure.
1689 * @return LY_ERR value.
1690 */
Radek Krejci19a96102018-11-15 13:38:09 +01001691static LY_ERR
Michal Vaskoba417ac2020-08-06 14:48:20 +02001692lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr,
1693 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001694{
1695 LY_ERR ret = LY_EVALID;
1696 const char *expr;
1697 struct lysc_range_part *parts = NULL, *part;
1698 int range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001699 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001700
1701 assert(range);
1702 assert(range_p);
1703
1704 expr = range_p->arg;
1705 while(1) {
1706 if (isspace(*expr)) {
1707 ++expr;
1708 } else if (*expr == '\0') {
1709 if (range_expected) {
1710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1711 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1712 length_restr ? "length" : "range", range_p->arg);
1713 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001714 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1716 "Invalid %s restriction - unexpected end of the expression (%s).",
1717 length_restr ? "length" : "range", range_p->arg);
1718 goto cleanup;
1719 }
1720 parts_done++;
1721 break;
1722 } else if (!strncmp(expr, "min", 3)) {
1723 if (parts) {
1724 /* min cannot be used elsewhere than in the first part */
1725 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1726 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1727 expr - range_p->arg, range_p->arg);
1728 goto cleanup;
1729 }
1730 expr += 3;
1731
1732 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001733 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 +01001734 part->max_64 = part->min_64;
1735 } else if (*expr == '|') {
1736 if (!parts || range_expected) {
1737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1738 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1739 goto cleanup;
1740 }
1741 expr++;
1742 parts_done++;
1743 /* process next part of the expression */
1744 } else if (!strncmp(expr, "..", 2)) {
1745 expr += 2;
1746 while (isspace(*expr)) {
1747 expr++;
1748 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001749 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1751 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1752 goto cleanup;
1753 }
1754 /* continue expecting the upper boundary */
1755 range_expected = 1;
1756 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1757 /* number */
1758 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001759 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001760 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 +01001761 range_expected = 0;
1762 } else {
1763 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001764 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 +01001765 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001766 part->max_64 = part->min_64;
1767 }
1768
1769 /* continue with possible another expression part */
1770 } else if (!strncmp(expr, "max", 3)) {
1771 expr += 3;
1772 while (isspace(*expr)) {
1773 expr++;
1774 }
1775 if (*expr != '\0') {
1776 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1777 length_restr ? "length" : "range", expr);
1778 goto cleanup;
1779 }
1780 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001781 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001782 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 +01001783 range_expected = 0;
1784 } else {
1785 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001786 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 +01001787 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001788 part->min_64 = part->max_64;
1789 }
1790 } else {
1791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1792 length_restr ? "length" : "range", expr);
1793 goto cleanup;
1794 }
1795 }
1796
1797 /* check with the previous range/length restriction */
1798 if (base_range) {
1799 switch (basetype) {
1800 case LY_TYPE_BINARY:
1801 case LY_TYPE_UINT8:
1802 case LY_TYPE_UINT16:
1803 case LY_TYPE_UINT32:
1804 case LY_TYPE_UINT64:
1805 case LY_TYPE_STRING:
1806 uns = 1;
1807 break;
1808 case LY_TYPE_DEC64:
1809 case LY_TYPE_INT8:
1810 case LY_TYPE_INT16:
1811 case LY_TYPE_INT32:
1812 case LY_TYPE_INT64:
1813 uns = 0;
1814 break;
1815 default:
1816 LOGINT(ctx->ctx);
1817 ret = LY_EINT;
1818 goto cleanup;
1819 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001820 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001821 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1822 goto baseerror;
1823 }
1824 /* current lower bound is not lower than the base */
1825 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1826 /* base has single value */
1827 if (base_range->parts[v].min_64 == parts[u].min_64) {
1828 /* both lower bounds are the same */
1829 if (parts[u].min_64 != parts[u].max_64) {
1830 /* current continues with a range */
1831 goto baseerror;
1832 } else {
1833 /* equal single values, move both forward */
1834 ++v;
1835 continue;
1836 }
1837 } else {
1838 /* base is single value lower than current range, so the
1839 * value from base range is removed in the current,
1840 * move only base and repeat checking */
1841 ++v;
1842 --u;
1843 continue;
1844 }
1845 } else {
1846 /* base is the range */
1847 if (parts[u].min_64 == parts[u].max_64) {
1848 /* current is a single value */
1849 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1850 /* current is behind the base range, so base range is omitted,
1851 * move the base and keep the current for further check */
1852 ++v;
1853 --u;
1854 } /* else it is within the base range, so move the current, but keep the base */
1855 continue;
1856 } else {
1857 /* both are ranges - check the higher bound, the lower was already checked */
1858 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1859 /* higher bound is higher than the current higher bound */
1860 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1861 /* but the current lower bound is also higher, so the base range is omitted,
1862 * continue with the same current, but move the base */
1863 --u;
1864 ++v;
1865 continue;
1866 }
1867 /* current range starts within the base range but end behind it */
1868 goto baseerror;
1869 } else {
1870 /* current range is smaller than the base,
1871 * move current, but stay with the base */
1872 continue;
1873 }
1874 }
1875 }
1876 }
1877 if (u != parts_done) {
1878baseerror:
1879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1880 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1881 length_restr ? "length" : "range", range_p->arg);
1882 goto cleanup;
1883 }
1884 }
1885
1886 if (!(*range)) {
1887 *range = calloc(1, sizeof **range);
1888 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1889 }
1890
Radek Krejcic8b31002019-01-08 10:24:45 +01001891 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001892 if (range_p->eapptag) {
1893 lydict_remove(ctx->ctx, (*range)->eapptag);
1894 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1895 }
1896 if (range_p->emsg) {
1897 lydict_remove(ctx->ctx, (*range)->emsg);
1898 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1899 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001900 if (range_p->dsc) {
1901 lydict_remove(ctx->ctx, (*range)->dsc);
1902 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1903 }
1904 if (range_p->ref) {
1905 lydict_remove(ctx->ctx, (*range)->ref);
1906 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1907 }
Radek Krejci19a96102018-11-15 13:38:09 +01001908 /* extensions are taken only from the last range by the caller */
1909
1910 (*range)->parts = parts;
1911 parts = NULL;
1912 ret = LY_SUCCESS;
1913cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001914 LY_ARRAY_FREE(parts);
1915
1916 return ret;
1917}
1918
1919/**
1920 * @brief Checks pattern syntax.
1921 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922 * @param[in] ctx Context.
1923 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001924 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001925 * @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 +01001926 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001927 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001928LY_ERR
1929lys_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 +01001930{
Michal Vasko40a00082020-05-27 15:20:01 +02001931 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001932 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001933 int err_code;
1934 const char *orig_ptr;
1935 PCRE2_SIZE err_offset;
1936 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001937#define URANGE_LEN 19
1938 char *ublock2urange[][2] = {
1939 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1940 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1941 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1942 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1943 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1944 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1945 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1946 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1947 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1948 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1949 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1950 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1951 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1952 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1953 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1954 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1955 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1956 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1957 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1958 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1959 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1960 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1961 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1962 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1963 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1964 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1965 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1966 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1967 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1968 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1969 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1970 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1971 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1972 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1973 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1974 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1975 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1976 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1977 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1978 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1979 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1980 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1981 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1982 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1983 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1984 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1985 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1986 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1987 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1988 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1989 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1990 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1991 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1992 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1993 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1994 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1995 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1996 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1997 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1998 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1999 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
2000 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
2001 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
2002 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
2003 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
2004 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
2005 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
2006 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
2007 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
2008 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
2009 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
2010 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
2011 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
2012 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
2013 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
2014 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
2015 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
2016 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
2017 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
2018 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
2019 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
2020 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
2021 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
2022 {NULL, NULL}
2023 };
2024
2025 /* adjust the expression to a Perl equivalent
2026 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
2027
Michal Vasko40a00082020-05-27 15:20:01 +02002028 /* allocate space for the transformed pattern */
2029 size = strlen(pattern) + 1;
2030 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002031 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002032 perl_regex[0] = '\0';
2033
Michal Vasko40a00082020-05-27 15:20:01 +02002034 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
2035 brack = 0;
2036 idx = 0;
2037 orig_ptr = pattern;
2038 while (orig_ptr[0]) {
2039 switch (orig_ptr[0]) {
2040 case '$':
2041 case '^':
2042 if (!brack) {
2043 /* make space for the extra character */
2044 ++size;
2045 perl_regex = ly_realloc(perl_regex, size);
2046 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002047
Michal Vasko40a00082020-05-27 15:20:01 +02002048 /* print escape slash */
2049 perl_regex[idx] = '\\';
2050 ++idx;
2051 }
2052 break;
2053 case '[':
2054 /* must not be escaped */
2055 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2056 ++brack;
2057 }
2058 break;
2059 case ']':
2060 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2061 /* pattern was checked and compiled already */
2062 assert(brack);
2063 --brack;
2064 }
2065 break;
2066 default:
2067 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002068 }
Michal Vasko40a00082020-05-27 15:20:01 +02002069
2070 /* copy char */
2071 perl_regex[idx] = orig_ptr[0];
2072
2073 ++idx;
2074 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002075 }
Michal Vasko40a00082020-05-27 15:20:01 +02002076 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002077
2078 /* substitute Unicode Character Blocks with exact Character Ranges */
2079 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2080 start = ptr - perl_regex;
2081
2082 ptr = strchr(ptr, '}');
2083 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002085 pattern, perl_regex + start + 2, "unterminated character property");
2086 free(perl_regex);
2087 return LY_EVALID;
2088 }
2089 end = (ptr - perl_regex) + 1;
2090
2091 /* need more space */
2092 if (end - start < URANGE_LEN) {
2093 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002095 }
2096
2097 /* find our range */
2098 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2099 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2100 break;
2101 }
2102 }
2103 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002105 pattern, perl_regex + start + 5, "unknown block name");
2106 free(perl_regex);
2107 return LY_EVALID;
2108 }
2109
2110 /* 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 +02002111 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002112 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 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002116 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002117 }
2118 }
Michal Vasko40a00082020-05-27 15:20:01 +02002119 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002120 /* skip brackets */
2121 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2122 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2123 } else {
2124 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2125 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2126 }
2127 }
2128
2129 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002130 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2131 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002132 &err_code, &err_offset, NULL);
2133 if (!code_local) {
2134 PCRE2_UCHAR err_msg[256] = {0};
2135 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002137 free(perl_regex);
2138 return LY_EVALID;
2139 }
2140 free(perl_regex);
2141
Radek Krejci54579462019-04-30 12:47:06 +02002142 if (code) {
2143 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002144 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002145 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002146 }
2147
2148 return LY_SUCCESS;
2149
2150#undef URANGE_LEN
2151}
2152
Radek Krejcia3045382018-11-22 14:30:31 +01002153/**
2154 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2155 * @param[in] ctx Compile context.
2156 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002157 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2158 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2159 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2160 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2161 */
Radek Krejci19a96102018-11-15 13:38:09 +01002162static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002163lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002164 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2165{
2166 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002167 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002168 LY_ERR ret = LY_SUCCESS;
2169
2170 /* first, copy the patterns from the base type */
2171 if (base_patterns) {
2172 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2173 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2174 }
2175
2176 LY_ARRAY_FOR(patterns_p, u) {
2177 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2178 *pattern = calloc(1, sizeof **pattern);
2179 ++(*pattern)->refcount;
2180
Michal Vasko03ff5a72019-09-11 13:49:33 +02002181 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002182 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002183
2184 if (patterns_p[u].arg[0] == 0x15) {
2185 (*pattern)->inverted = 1;
2186 }
Radek Krejci54579462019-04-30 12:47:06 +02002187 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002188 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2189 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002190 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2191 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002192 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002193 }
2194done:
2195 return ret;
2196}
2197
Radek Krejcia3045382018-11-22 14:30:31 +01002198/**
2199 * @brief map of the possible restrictions combination for the specific built-in type.
2200 */
Radek Krejci19a96102018-11-15 13:38:09 +01002201static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2202 0 /* LY_TYPE_UNKNOWN */,
2203 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002204 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2205 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2206 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2207 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2208 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002209 LYS_SET_BIT /* LY_TYPE_BITS */,
2210 0 /* LY_TYPE_BOOL */,
2211 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2212 0 /* LY_TYPE_EMPTY */,
2213 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2214 LYS_SET_BASE /* LY_TYPE_IDENT */,
2215 LYS_SET_REQINST /* LY_TYPE_INST */,
2216 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002217 LYS_SET_TYPE /* LY_TYPE_UNION */,
2218 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002219 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002220 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002221 LYS_SET_RANGE /* LY_TYPE_INT64 */
2222};
2223
2224/**
2225 * @brief stringification of the YANG built-in data types
2226 */
2227const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2228 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2229 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002230};
2231
Radek Krejcia3045382018-11-22 14:30:31 +01002232/**
2233 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2234 * @param[in] ctx Compile context.
2235 * @param[in] enums_p Array of the parsed enum structures to compile.
2236 * @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 +01002237 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2238 * @param[out] enums Newly created array of the compiled enums information for the current type.
2239 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2240 */
Radek Krejci19a96102018-11-15 13:38:09 +01002241static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002242lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002243 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002244{
2245 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002246 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002247 int32_t value = 0;
2248 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002249 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002250
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002251 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002252 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2253 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2254 return LY_EVALID;
2255 }
2256
2257 LY_ARRAY_FOR(enums_p, u) {
2258 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2259 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002260 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2261 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002262 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002263 if (base_enums) {
2264 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2265 LY_ARRAY_FOR(base_enums, v) {
2266 if (!strcmp(e->name, base_enums[v].name)) {
2267 break;
2268 }
2269 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002270 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002271 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2272 "Invalid %s - derived type adds new item \"%s\".",
2273 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2274 return LY_EVALID;
2275 }
2276 match = v;
2277 }
2278
2279 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002280 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002281 if (enums_p[u].flags & LYS_SET_VALUE) {
2282 e->value = (int32_t)enums_p[u].value;
2283 if (!u || e->value >= value) {
2284 value = e->value + 1;
2285 }
2286 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002287 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002288 if (e->value == (*enums)[v].value) {
2289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2290 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2291 e->value, e->name, (*enums)[v].name);
2292 return LY_EVALID;
2293 }
2294 }
2295 } else if (base_enums) {
2296 /* inherit the assigned value */
2297 e->value = base_enums[match].value;
2298 if (!u || e->value >= value) {
2299 value = e->value + 1;
2300 }
2301 } else {
2302 /* assign value automatically */
2303 if (u && value == INT32_MIN) {
2304 /* counter overflow */
2305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2306 "Invalid enumeration - it is not possible to auto-assign enum value for "
2307 "\"%s\" since the highest value is already 2147483647.", e->name);
2308 return LY_EVALID;
2309 }
2310 e->value = value++;
2311 }
2312 } else { /* LY_TYPE_BITS */
2313 if (enums_p[u].flags & LYS_SET_VALUE) {
2314 e->value = (int32_t)enums_p[u].value;
2315 if (!u || (uint32_t)e->value >= position) {
2316 position = (uint32_t)e->value + 1;
2317 }
2318 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002319 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002320 if (e->value == (*enums)[v].value) {
2321 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2322 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2323 (uint32_t)e->value, e->name, (*enums)[v].name);
2324 return LY_EVALID;
2325 }
2326 }
2327 } else if (base_enums) {
2328 /* inherit the assigned value */
2329 e->value = base_enums[match].value;
2330 if (!u || (uint32_t)e->value >= position) {
2331 position = (uint32_t)e->value + 1;
2332 }
2333 } else {
2334 /* assign value automatically */
2335 if (u && position == 0) {
2336 /* counter overflow */
2337 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2338 "Invalid bits - it is not possible to auto-assign bit position for "
2339 "\"%s\" since the highest value is already 4294967295.", e->name);
2340 return LY_EVALID;
2341 }
2342 e->value = position++;
2343 }
2344 }
2345
2346 if (base_enums) {
2347 /* the assigned values must not change from the derived type */
2348 if (e->value != base_enums[match].value) {
2349 if (basetype == LY_TYPE_ENUM) {
2350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2351 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2352 e->name, base_enums[match].value, e->value);
2353 } else {
2354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2355 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2356 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2357 }
2358 return LY_EVALID;
2359 }
2360 }
2361
Radek Krejciec4da802019-05-02 13:02:41 +02002362 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002363 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 +01002364
2365 if (basetype == LY_TYPE_BITS) {
2366 /* keep bits ordered by position */
Radek Krejci1e008d22020-08-17 11:37:37 +02002367 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
Radek Krejci19a96102018-11-15 13:38:09 +01002368 if (v != u) {
2369 memcpy(&storage, e, sizeof *e);
2370 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2371 memcpy(&(*enums)[v], &storage, sizeof storage);
2372 }
2373 }
2374 }
2375
2376done:
2377 return ret;
2378}
2379
Radek Krejcia3045382018-11-22 14:30:31 +01002380/**
2381 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2382 *
2383 * path-arg = absolute-path / relative-path
2384 * absolute-path = 1*("/" (node-identifier *path-predicate))
2385 * relative-path = 1*(".." "/") descendant-path
2386 *
2387 * @param[in,out] path Path to parse.
2388 * @param[out] prefix Prefix of the token, NULL if there is not any.
2389 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2390 * @param[out] name Name of the token.
2391 * @param[out] nam_len Length of the name.
2392 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2393 * must not be changed between consecutive calls. -1 if the
2394 * path is absolute.
2395 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2396 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2397 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002398LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002399lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2400 int *parent_times, int *has_predicate)
2401{
2402 int par_times = 0;
2403
2404 assert(path && *path);
2405 assert(parent_times);
2406 assert(prefix);
2407 assert(prefix_len);
2408 assert(name);
2409 assert(name_len);
2410 assert(has_predicate);
2411
2412 *prefix = NULL;
2413 *prefix_len = 0;
2414 *name = NULL;
2415 *name_len = 0;
2416 *has_predicate = 0;
2417
2418 if (!*parent_times) {
2419 if (!strncmp(*path, "..", 2)) {
2420 *path += 2;
2421 ++par_times;
2422 while (!strncmp(*path, "/..", 3)) {
2423 *path += 3;
2424 ++par_times;
2425 }
2426 }
2427 if (par_times) {
2428 *parent_times = par_times;
2429 } else {
2430 *parent_times = -1;
2431 }
2432 }
2433
2434 if (**path != '/') {
2435 return LY_EINVAL;
2436 }
2437 /* skip '/' */
2438 ++(*path);
2439
2440 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002441 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002442
2443 if ((**path == '/' && (*path)[1]) || !**path) {
2444 /* path continues by another token or this is the last token */
2445 return LY_SUCCESS;
2446 } else if ((*path)[0] != '[') {
2447 /* unexpected character */
2448 return LY_EINVAL;
2449 } else {
2450 /* predicate starting with [ */
2451 *has_predicate = 1;
2452 return LY_SUCCESS;
2453 }
2454}
2455
2456/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002457 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2458 *
2459 * The set of features used for target must be a subset of features used for the leafref.
2460 * This is not a perfect, we should compare the truth tables but it could require too much resources
2461 * and RFC 7950 does not require it explicitely, so we simplify that.
2462 *
2463 * @param[in] refnode The leafref node.
2464 * @param[in] target Tha target node of the leafref.
2465 * @return LY_SUCCESS or LY_EVALID;
2466 */
2467static LY_ERR
2468lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2469{
2470 LY_ERR ret = LY_EVALID;
2471 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002472 LY_ARRAY_COUNT_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002473 struct ly_set features = {0};
2474
2475 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002476 if (iter->iffeatures) {
2477 LY_ARRAY_FOR(iter->iffeatures, u) {
2478 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2479 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002480 }
2481 }
2482 }
2483 }
2484
2485 /* we should have, in features set, a superset of features applicable to the target node.
2486 * So when adding features applicable to the target into the features set, we should not be
2487 * able to actually add any new feature, otherwise it is not a subset of features applicable
2488 * to the leafref itself. */
2489 count = features.count;
2490 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002491 if (iter->iffeatures) {
2492 LY_ARRAY_FOR(iter->iffeatures, u) {
2493 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2494 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002495 /* new feature was added (or LY_EMEM) */
2496 goto cleanup;
2497 }
2498 }
2499 }
2500 }
2501 }
2502 ret = LY_SUCCESS;
2503
2504cleanup:
2505 ly_set_erase(&features, NULL);
2506 return ret;
2507}
2508
Michal Vasko004d3152020-06-11 19:59:22 +02002509static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2510 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002511 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod);
Radek Krejcia3045382018-11-22 14:30:31 +01002512
Radek Krejcia3045382018-11-22 14:30:31 +01002513/**
2514 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2515 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002516 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2517 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2518 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2519 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002520 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002521 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002522 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002523 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2524 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2525 * @param[out] type Newly created type structure with the filled information about the type.
2526 * @return LY_ERR value.
2527 */
Radek Krejci19a96102018-11-15 13:38:09 +01002528static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002529lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2530 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2531 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2532 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002533{
2534 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002535 struct lysc_type_bin *bin;
2536 struct lysc_type_num *num;
2537 struct lysc_type_str *str;
2538 struct lysc_type_bits *bits;
2539 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002540 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002541 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002542 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002543 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002544
2545 switch (basetype) {
2546 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002547 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002548
2549 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002550 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002551 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2552 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002553 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002554 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002555 }
2556 }
2557
2558 if (tpdfname) {
2559 type_p->compiled = *type;
2560 *type = calloc(1, sizeof(struct lysc_type_bin));
2561 }
2562 break;
2563 case LY_TYPE_BITS:
2564 /* RFC 7950 9.7 - bits */
2565 bits = (struct lysc_type_bits*)(*type);
2566 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002567 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002568 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2569 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002570 }
2571
Radek Krejci555cb5b2018-11-16 14:54:33 +01002572 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002573 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002574 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002575 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002576 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002578 free(*type);
2579 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002580 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002581 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002582 }
2583
2584 if (tpdfname) {
2585 type_p->compiled = *type;
2586 *type = calloc(1, sizeof(struct lysc_type_bits));
2587 }
2588 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002589 case LY_TYPE_DEC64:
Radek Krejci115a74d2020-08-14 22:18:12 +02002590 dec = (struct lysc_type_dec *)(*type);
Radek Krejci6cba4292018-11-15 17:33:29 +01002591
2592 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002593 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002594 if (!type_p->fraction_digits) {
2595 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002597 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002599 free(*type);
2600 *type = NULL;
2601 }
2602 return LY_EVALID;
2603 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002604 dec->fraction_digits = type_p->fraction_digits;
2605 } else {
2606 if (type_p->fraction_digits) {
2607 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
2608 if (tpdfname) {
2609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2610 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
2611 tpdfname);
2612 } else {
2613 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2614 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
2615 free(*type);
2616 *type = NULL;
2617 }
2618 return LY_EVALID;
Radek Krejci6cba4292018-11-15 17:33:29 +01002619 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002620 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
Radek Krejci6cba4292018-11-15 17:33:29 +01002621 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002622
2623 /* RFC 7950 9.2.4 - range */
2624 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002625 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
Radek Krejci115a74d2020-08-14 22:18:12 +02002626 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002627 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002628 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002629 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002630 }
2631
2632 if (tpdfname) {
2633 type_p->compiled = *type;
2634 *type = calloc(1, sizeof(struct lysc_type_dec));
2635 }
2636 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002637 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002638 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002639
2640 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002641 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002642 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2643 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002644 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002645 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002646 }
2647 } else if (base && ((struct lysc_type_str*)base)->length) {
2648 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2649 }
2650
2651 /* RFC 7950 9.4.5 - pattern */
2652 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002653 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002654 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002655 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2656 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2657 }
2658
2659 if (tpdfname) {
2660 type_p->compiled = *type;
2661 *type = calloc(1, sizeof(struct lysc_type_str));
2662 }
2663 break;
2664 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002665 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002666
2667 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002668 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002669 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002670 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002671 }
2672
Radek Krejci555cb5b2018-11-16 14:54:33 +01002673 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002674 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002675 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002677 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002679 free(*type);
2680 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002681 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002682 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002683 }
2684
2685 if (tpdfname) {
2686 type_p->compiled = *type;
2687 *type = calloc(1, sizeof(struct lysc_type_enum));
2688 }
2689 break;
2690 case LY_TYPE_INT8:
2691 case LY_TYPE_UINT8:
2692 case LY_TYPE_INT16:
2693 case LY_TYPE_UINT16:
2694 case LY_TYPE_INT32:
2695 case LY_TYPE_UINT32:
2696 case LY_TYPE_INT64:
2697 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002698 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002699
2700 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002701 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002702 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2703 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002704 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002705 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002706 }
2707 }
2708
2709 if (tpdfname) {
2710 type_p->compiled = *type;
2711 *type = calloc(1, sizeof(struct lysc_type_num));
2712 }
2713 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002714 case LY_TYPE_IDENT:
2715 idref = (struct lysc_type_identityref*)(*type);
2716
2717 /* RFC 7950 9.10.2 - base */
2718 if (type_p->bases) {
2719 if (base) {
2720 /* only the directly derived identityrefs can contain base specification */
2721 if (tpdfname) {
2722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002723 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002724 tpdfname);
2725 } else {
2726 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002727 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002728 free(*type);
2729 *type = NULL;
2730 }
2731 return LY_EVALID;
2732 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002733 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002734 }
2735
2736 if (!base && !type_p->flags) {
2737 /* type derived from identityref built-in type must contain at least one base */
2738 if (tpdfname) {
2739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2740 } else {
2741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2742 free(*type);
2743 *type = NULL;
2744 }
2745 return LY_EVALID;
2746 }
2747
2748 if (tpdfname) {
2749 type_p->compiled = *type;
2750 *type = calloc(1, sizeof(struct lysc_type_identityref));
2751 }
2752 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002753 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002754 lref = (struct lysc_type_leafref*)*type;
2755
Radek Krejcia3045382018-11-22 14:30:31 +01002756 /* RFC 7950 9.9.3 - require-instance */
2757 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002758 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002759 if (tpdfname) {
2760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2761 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2762 } else {
2763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2764 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2765 free(*type);
2766 *type = NULL;
2767 }
2768 return LY_EVALID;
2769 }
Michal Vasko004d3152020-06-11 19:59:22 +02002770 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002771 } else if (base) {
2772 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002773 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002774 } else {
2775 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002776 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002777 }
2778 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002779 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2780 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002781 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002782 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2783 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002784 } else if (tpdfname) {
2785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2786 return LY_EVALID;
2787 } else {
2788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2789 free(*type);
2790 *type = NULL;
2791 return LY_EVALID;
2792 }
2793 if (tpdfname) {
2794 type_p->compiled = *type;
2795 *type = calloc(1, sizeof(struct lysc_type_leafref));
2796 }
2797 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002798 case LY_TYPE_INST:
2799 /* RFC 7950 9.9.3 - require-instance */
2800 if (type_p->flags & LYS_SET_REQINST) {
2801 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2802 } else {
2803 /* default is true */
2804 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2805 }
2806
2807 if (tpdfname) {
2808 type_p->compiled = *type;
2809 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2810 }
2811 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002812 case LY_TYPE_UNION:
2813 un = (struct lysc_type_union*)(*type);
2814
2815 /* RFC 7950 7.4 - type */
2816 if (type_p->types) {
2817 if (base) {
2818 /* only the directly derived union can contain types specification */
2819 if (tpdfname) {
2820 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2821 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2822 tpdfname);
2823 } else {
2824 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2825 "Invalid type substatement for the type not directly derived from union built-in type.");
2826 free(*type);
2827 *type = NULL;
2828 }
2829 return LY_EVALID;
2830 }
2831 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002832 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2833 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002834 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002835 &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002836 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2837 /* add space for additional types from the union subtype */
2838 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002839 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
Radek Krejcic4fa0292020-05-14 10:54:49 +02002840 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002841
2842 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002843 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002844 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2845 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2846 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2847 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 +02002848 lref = (struct lysc_type_leafref *)un->types[u + additional];
2849
2850 lref->basetype = LY_TYPE_LEAFREF;
2851 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2852 lref->refcount = 1;
2853 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2854 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002855 /* TODO extensions */
2856
2857 } else {
2858 un->types[u + additional] = un_aux->types[v];
2859 ++un_aux->types[v]->refcount;
2860 }
2861 ++additional;
2862 LY_ARRAY_INCREMENT(un->types);
2863 }
2864 /* compensate u increment in main loop */
2865 --additional;
2866
2867 /* free the replaced union subtype */
2868 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2869 } else {
2870 LY_ARRAY_INCREMENT(un->types);
2871 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002872 }
2873 }
2874
2875 if (!base && !type_p->flags) {
2876 /* type derived from union built-in type must contain at least one type */
2877 if (tpdfname) {
2878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2879 } else {
2880 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2881 free(*type);
2882 *type = NULL;
2883 }
2884 return LY_EVALID;
2885 }
2886
2887 if (tpdfname) {
2888 type_p->compiled = *type;
2889 *type = calloc(1, sizeof(struct lysc_type_union));
2890 }
2891 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002892 case LY_TYPE_BOOL:
2893 case LY_TYPE_EMPTY:
2894 case LY_TYPE_UNKNOWN: /* just to complete switch */
2895 break;
2896 }
2897 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2898done:
2899 return ret;
2900}
2901
Radek Krejcia3045382018-11-22 14:30:31 +01002902/**
2903 * @brief Compile information about the leaf/leaf-list's type.
2904 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002905 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2906 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2907 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2908 * @param[in] context_name Name of the context node or referencing typedef for logging.
2909 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002910 * @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 +01002911 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002912 * @param[out] dflt Default value for the type.
2913 * @param[out] dflt_mod Local module for the default value.
Radek Krejcia3045382018-11-22 14:30:31 +01002914 * @return LY_ERR value.
2915 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002916static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002917lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2918 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002919 struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod)
Radek Krejci19a96102018-11-15 13:38:09 +01002920{
2921 LY_ERR ret = LY_SUCCESS;
2922 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002923 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002924 struct type_context {
2925 const struct lysp_tpdf *tpdf;
2926 struct lysp_node *node;
2927 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002928 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002929 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002930 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002931 struct ly_set tpdf_chain = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002932
2933 assert((dflt && dflt_mod) || (!dflt && !dflt_mod));
Radek Krejci19a96102018-11-15 13:38:09 +01002934
2935 (*type) = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002936 if (dflt) {
2937 *dflt = NULL;
2938 *dflt_mod = NULL;
2939 }
Radek Krejci19a96102018-11-15 13:38:09 +01002940
2941 tctx = calloc(1, sizeof *tctx);
2942 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002943 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002944 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2945 ret == LY_SUCCESS;
2946 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2947 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2948 if (basetype) {
2949 break;
2950 }
2951
2952 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002953 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2954 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002955 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2956
Radek Krejcicdfecd92018-11-26 11:27:32 +01002957 if (units && !*units) {
2958 /* inherit units */
2959 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2960 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002961 if (dflt && !*dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002962 /* inherit default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002963 *dflt = tctx->tpdf->dflt;
2964 *dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002965 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002966 if (dummyloops && (!units || *units) && dflt && *dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002967 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2968 break;
2969 }
2970
Radek Krejci19a96102018-11-15 13:38:09 +01002971 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002972 /* it is not necessary to continue, the rest of the chain was already compiled,
2973 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002974 basetype = tctx->tpdf->type.compiled->basetype;
2975 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002976 if ((units && !*units) || (dflt && !*dflt)) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002977 dummyloops = 1;
2978 goto preparenext;
2979 } else {
2980 tctx = NULL;
2981 break;
2982 }
Radek Krejci19a96102018-11-15 13:38:09 +01002983 }
2984
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002985 /* circular typedef reference detection */
2986 for (u = 0; u < tpdf_chain.count; u++) {
2987 /* local part */
2988 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2989 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2990 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2991 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2992 free(tctx);
2993 ret = LY_EVALID;
2994 goto cleanup;
2995 }
2996 }
2997 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2998 /* global part for unions corner case */
2999 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3000 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3002 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3003 free(tctx);
3004 ret = LY_EVALID;
3005 goto cleanup;
3006 }
3007 }
3008
Radek Krejci19a96102018-11-15 13:38:09 +01003009 /* store information for the following processing */
3010 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3011
Radek Krejcicdfecd92018-11-26 11:27:32 +01003012preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003013 /* prepare next loop */
3014 tctx_prev = tctx;
3015 tctx = calloc(1, sizeof *tctx);
3016 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3017 }
3018 free(tctx);
3019
3020 /* allocate type according to the basetype */
3021 switch (basetype) {
3022 case LY_TYPE_BINARY:
3023 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003024 break;
3025 case LY_TYPE_BITS:
3026 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003027 break;
3028 case LY_TYPE_BOOL:
3029 case LY_TYPE_EMPTY:
3030 *type = calloc(1, sizeof(struct lysc_type));
3031 break;
3032 case LY_TYPE_DEC64:
3033 *type = calloc(1, sizeof(struct lysc_type_dec));
3034 break;
3035 case LY_TYPE_ENUM:
3036 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003037 break;
3038 case LY_TYPE_IDENT:
3039 *type = calloc(1, sizeof(struct lysc_type_identityref));
3040 break;
3041 case LY_TYPE_INST:
3042 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3043 break;
3044 case LY_TYPE_LEAFREF:
3045 *type = calloc(1, sizeof(struct lysc_type_leafref));
3046 break;
3047 case LY_TYPE_STRING:
3048 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003049 break;
3050 case LY_TYPE_UNION:
3051 *type = calloc(1, sizeof(struct lysc_type_union));
3052 break;
3053 case LY_TYPE_INT8:
3054 case LY_TYPE_UINT8:
3055 case LY_TYPE_INT16:
3056 case LY_TYPE_UINT16:
3057 case LY_TYPE_INT32:
3058 case LY_TYPE_UINT32:
3059 case LY_TYPE_INT64:
3060 case LY_TYPE_UINT64:
3061 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003062 break;
3063 case LY_TYPE_UNKNOWN:
3064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3065 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3066 ret = LY_EVALID;
3067 goto cleanup;
3068 }
3069 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003070 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003071 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3072 ly_data_type2str[basetype]);
3073 free(*type);
3074 (*type) = NULL;
3075 ret = LY_EVALID;
3076 goto cleanup;
3077 }
3078
3079 /* get restrictions from the referred typedefs */
3080 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3081 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003082
3083 /* remember the typedef context for circular check */
3084 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3085
Radek Krejci43699232018-11-23 14:59:46 +01003086 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003087 base = tctx->tpdf->type.compiled;
3088 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003089 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003090 /* no change, just use the type information from the base */
3091 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3092 ++base->refcount;
3093 continue;
3094 }
3095
3096 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003097 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3099 tctx->tpdf->name, ly_data_type2str[basetype]);
3100 ret = LY_EVALID;
3101 goto cleanup;
3102 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3104 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3105 tctx->tpdf->name, tctx->tpdf->dflt);
3106 ret = LY_EVALID;
3107 goto cleanup;
3108 }
3109
Radek Krejci19a96102018-11-15 13:38:09 +01003110 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003111 /* TODO user type plugins */
3112 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003113 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003114 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 +01003115 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003116 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003117 LY_CHECK_GOTO(ret, cleanup);
3118 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003119 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003120 /* remove the processed typedef contexts from the stack for circular check */
3121 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003122
Radek Krejcic5c27e52018-11-15 14:38:11 +01003123 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003124 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003125 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003126 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003127 /* TODO user type plugins */
3128 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003129 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003130 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 +01003131 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003132 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003133 /* no specific restriction in leaf's type definition, copy from the base */
3134 free(*type);
3135 (*type) = base;
3136 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003137 }
3138
Radek Krejci0935f412019-08-20 16:15:18 +02003139 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003140
3141cleanup:
3142 ly_set_erase(&tpdf_chain, free);
3143 return ret;
3144}
3145
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003146/**
3147 * @brief Compile status information of the given node.
3148 *
3149 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3150 * has the status correctly set during the compilation.
3151 *
3152 * @param[in] ctx Compile context
3153 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3154 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3155 * the compatibility with the parent's status value.
3156 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3157 * @return LY_ERR value.
3158 */
3159static LY_ERR
3160lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3161{
3162 /* status - it is not inherited by specification, but it does not make sense to have
3163 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3164 if (!((*node_flags) & LYS_STATUS_MASK)) {
3165 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3166 if ((parent_flags & 0x3) != 0x3) {
3167 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3168 * combination of bits (0x3) which marks the uses_status value */
3169 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3170 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3171 }
3172 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3173 } else {
3174 (*node_flags) |= LYS_STATUS_CURR;
3175 }
3176 } else if (parent_flags & LYS_STATUS_MASK) {
3177 /* check status compatibility with the parent */
3178 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3179 if ((*node_flags) & LYS_STATUS_CURR) {
3180 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3181 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3182 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3183 } else { /* LYS_STATUS_DEPRC */
3184 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3185 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3186 }
3187 return LY_EVALID;
3188 }
3189 }
3190 return LY_SUCCESS;
3191}
3192
Radek Krejci8cce8532019-03-05 11:27:45 +01003193/**
3194 * @brief Check uniqness of the node/action/notification name.
3195 *
3196 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3197 * structures, but they share the namespace so we need to check their name collisions.
3198 *
3199 * @param[in] ctx Compile context.
3200 * @param[in] children List (linked list) of data nodes to go through.
3201 * @param[in] actions List (sized array) of actions or RPCs to go through.
3202 * @param[in] notifs List (sized array) of Notifications to go through.
3203 * @param[in] name Name of the item to find in the given lists.
3204 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3205 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3206 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3207 */
3208static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003209lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3210 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003211{
3212 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003213 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003214
3215 LY_LIST_FOR(children, iter) {
3216 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3217 goto error;
3218 }
3219 }
3220 LY_ARRAY_FOR(actions, u) {
3221 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3222 goto error;
3223 }
3224 }
3225 LY_ARRAY_FOR(notifs, u) {
3226 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3227 goto error;
3228 }
3229 }
3230 return LY_SUCCESS;
3231error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003233 return LY_EEXIST;
3234}
3235
Radek Krejciec4da802019-05-02 13:02:41 +02003236static 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 +01003237
Radek Krejcia3045382018-11-22 14:30:31 +01003238/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003239 * @brief Compile parsed RPC/action schema node information.
3240 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003241 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003242 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003243 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3244 * @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).
3245 * Zero means no uses, non-zero value with no status bit set mean the default status.
3246 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3247 */
3248static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003249lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003250 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3251{
3252 LY_ERR ret = LY_SUCCESS;
3253 struct lysp_node *child_p;
3254 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003255 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003256
Radek Krejci327de162019-06-14 12:52:07 +02003257 lysc_update_path(ctx, parent, action_p->name);
3258
Radek Krejci8cce8532019-03-05 11:27:45 +01003259 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3260 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3261 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3262 action_p->name, action)) {
3263 return LY_EVALID;
3264 }
3265
Radek Krejciec4da802019-05-02 13:02:41 +02003266 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003268 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003269 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003270 return LY_EVALID;
3271 }
3272
Michal Vasko1bf09392020-03-27 12:38:10 +01003273 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003274 action->module = ctx->mod;
3275 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003276 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003277 action->sp = action_p;
3278 }
3279 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3280
3281 /* status - it is not inherited by specification, but it does not make sense to have
3282 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003283 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003284
3285 DUP_STRING(ctx->ctx, action_p->name, action->name);
3286 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3287 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003288 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003289 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003290
3291 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003292 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003293 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003294 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 +02003295 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003296 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003297 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003298 }
Radek Krejci327de162019-06-14 12:52:07 +02003299 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003300 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003301
3302 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003303 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003304 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003305 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 +02003306 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003307 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003308 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003309 }
Radek Krejci327de162019-06-14 12:52:07 +02003310 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003311 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003312
3313 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3314 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003315 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003316 }
3317
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003318cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003319 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003320 return ret;
3321}
3322
3323/**
Radek Krejci43981a32019-04-12 09:44:11 +02003324 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003325 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003326 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003327 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3328 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3329 * @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 +02003330 * Zero means no uses, non-zero value with no status bit set mean the default status.
3331 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3332 */
3333static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003334lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003335 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3336{
3337 LY_ERR ret = LY_SUCCESS;
3338 struct lysp_node *child_p;
3339 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003340 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003341
Radek Krejci327de162019-06-14 12:52:07 +02003342 lysc_update_path(ctx, parent, notif_p->name);
3343
Radek Krejcifc11bd72019-04-11 16:00:05 +02003344 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3345 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3346 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3347 notif_p->name, notif)) {
3348 return LY_EVALID;
3349 }
3350
Radek Krejciec4da802019-05-02 13:02:41 +02003351 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3353 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003354 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003355 return LY_EVALID;
3356 }
3357
3358 notif->nodetype = LYS_NOTIF;
3359 notif->module = ctx->mod;
3360 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003361 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003362 notif->sp = notif_p;
3363 }
3364 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3365
3366 /* status - it is not inherited by specification, but it does not make sense to have
3367 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3368 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3369
3370 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3371 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3372 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003373 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003374 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003375 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3376 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003377 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003378 }
Radek Krejci0935f412019-08-20 16:15:18 +02003379 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003380
Radek Krejciec4da802019-05-02 13:02:41 +02003381 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003382 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003383 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003384 }
3385
Radek Krejci327de162019-06-14 12:52:07 +02003386 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003387cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003388 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003389 return ret;
3390}
3391
3392/**
Radek Krejcia3045382018-11-22 14:30:31 +01003393 * @brief Compile parsed container node information.
3394 * @param[in] ctx Compile context
3395 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003396 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3397 * is enriched with the container-specific information.
3398 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3399 */
Radek Krejci19a96102018-11-15 13:38:09 +01003400static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003401lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003402{
3403 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3404 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3405 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003406 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003407 LY_ERR ret = LY_SUCCESS;
3408
Radek Krejcife909632019-02-12 15:34:42 +01003409 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003410 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003411 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003412 } else if (cont_p->musts) {
3413 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003414 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3415 cont->flags |= LYS_PRESENCE;
3416 } else if (cont_p->when) {
3417 /* container with a when condition */
3418 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 +02003419 cont->flags |= LYS_PRESENCE;
3420 } else if (cont_p->parent) {
3421 if (cont_p->parent->nodetype == LYS_CHOICE) {
3422 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003423 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 +02003424 cont_p->name, cont_p->parent->name);
3425 cont->flags |= LYS_PRESENCE;
3426 } else if ((cont_p->parent->nodetype == LYS_CASE)
3427 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3428 /* 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 +02003429 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 +02003430 cont_p->name, cont_p->parent->name);
3431 cont->flags |= LYS_PRESENCE;
3432 }
Radek Krejcife909632019-02-12 15:34:42 +01003433 }
3434
Michal Vaskoba417ac2020-08-06 14:48:20 +02003435 /* more cases when the container has meaning but is kept NP for convenience:
3436 * - when condition
3437 * - direct child action/notification
3438 */
3439
Radek Krejci19a96102018-11-15 13:38:09 +01003440 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003441 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003442 }
3443
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003444 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003445 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3446 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003447 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003448 }
Radek Krejciec4da802019-05-02 13:02:41 +02003449 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3450 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003451
3452done:
3453 return ret;
3454}
3455
Radek Krejci33f72892019-02-21 10:36:58 +01003456/*
3457 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3458 * @param[in] ctx Compile context.
3459 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3460 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003461 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3462 * @return LY_ERR value.
3463 */
3464static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003465lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
3466 struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003467{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003468 const char *dflt;
3469 struct lys_module *dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003470
Radek Krejciec4da802019-05-02 13:02:41 +02003471 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 +02003472 leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod));
3473
3474 /* store default value, if any */
3475 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
3476 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, dflt, dflt_mod));
Radek Krejci33f72892019-02-21 10:36:58 +01003477 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003478
Radek Krejci33f72892019-02-21 10:36:58 +01003479 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3480 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Michal Vasko004d3152020-06-11 19:59:22 +02003481 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003482 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003483 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003484 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
3485 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
Radek Krejci33f72892019-02-21 10:36:58 +01003486 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Michal Vasko004d3152020-06-11 19:59:22 +02003487 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003488 }
3489 }
3490 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003491 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3492 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3493 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3494 return LY_EVALID;
3495 }
3496 }
3497
Radek Krejci33f72892019-02-21 10:36:58 +01003498 return LY_SUCCESS;
3499}
3500
Radek Krejcia3045382018-11-22 14:30:31 +01003501/**
3502 * @brief Compile parsed leaf node information.
3503 * @param[in] ctx Compile context
3504 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003505 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3506 * is enriched with the leaf-specific information.
3507 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3508 */
Radek Krejci19a96102018-11-15 13:38:09 +01003509static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003510lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003511{
3512 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3513 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003514 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003515 LY_ERR ret = LY_SUCCESS;
3516
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003517 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003518 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3519 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003520 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003521 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003522 if (leaf_p->units) {
3523 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3524 leaf->flags |= LYS_SET_UNITS;
3525 }
Radek Krejcia1911222019-07-22 17:24:50 +02003526
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003527 /* compile type */
3528 LY_CHECK_RET(lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf));
Radek Krejcia1911222019-07-22 17:24:50 +02003529
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003530 /* store/update default value */
Radek Krejciccd20f12019-02-15 14:12:27 +01003531 if (leaf_p->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003532 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, leaf_p->dflt, ctx->mod_def));
Radek Krejci76b3e962018-12-14 17:01:25 +01003533 leaf->flags |= LYS_SET_DFLT;
3534 }
Radek Krejci43699232018-11-23 14:59:46 +01003535
Radek Krejci19a96102018-11-15 13:38:09 +01003536done:
3537 return ret;
3538}
3539
Radek Krejcia3045382018-11-22 14:30:31 +01003540/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003541 * @brief Compile parsed leaf-list node information.
3542 * @param[in] ctx Compile context
3543 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003544 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3545 * is enriched with the leaf-list-specific information.
3546 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3547 */
3548static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003549lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003550{
3551 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3552 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003553 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003554 LY_ERR ret = LY_SUCCESS;
3555
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003556 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003557 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3558 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003559 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003560 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003561 if (llist_p->units) {
3562 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3563 llist->flags |= LYS_SET_UNITS;
3564 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003565
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003566 /* compile type */
3567 LY_CHECK_RET(lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist));
Michal Vasko6a044b22020-01-15 12:25:39 +01003568
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003569 /* store/update default values */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003570 if (llist_p->dflts) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003571 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, llist_p->dflts, ctx->mod_def), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003572 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003573 }
3574
3575 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003576 if (llist->min) {
3577 llist->flags |= LYS_MAND_TRUE;
3578 }
Radek Krejcib7408632018-11-28 17:12:11 +01003579 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003580
Radek Krejci0e5d8382018-11-28 16:37:53 +01003581done:
3582 return ret;
3583}
3584
3585/**
Radek Krejci7af64242019-02-18 13:07:53 +01003586 * @brief Compile information about list's uniques.
3587 * @param[in] ctx Compile context.
3588 * @param[in] context_module Module where the prefixes are going to be resolved.
3589 * @param[in] uniques Sized array list of unique statements.
3590 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3591 * @return LY_ERR value.
3592 */
3593static LY_ERR
3594lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3595{
3596 LY_ERR ret = LY_SUCCESS;
3597 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003598 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003599 const char *keystr, *delim;
3600 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003601 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003602 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003603 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003604
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003605 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003606 config = -1;
3607 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3608 keystr = uniques[v];
3609 while (keystr) {
3610 delim = strpbrk(keystr, " \t\n");
3611 if (delim) {
3612 len = delim - keystr;
3613 while (isspace(*delim)) {
3614 ++delim;
3615 }
3616 } else {
3617 len = strlen(keystr);
3618 }
3619
3620 /* unique node must be present */
3621 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003622 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3623 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003624 if (ret != LY_SUCCESS) {
3625 if (ret == LY_EDENIED) {
3626 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003627 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003628 len, keystr, lys_nodetype2str((*key)->nodetype));
3629 }
3630 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003631 } else if (flags) {
3632 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3633 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003634 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003635 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003636 }
3637
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003638
Radek Krejci7af64242019-02-18 13:07:53 +01003639 /* all referenced leafs must be of the same config type */
3640 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3641 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003642 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003643 return LY_EVALID;
3644 } else if ((*key)->flags & LYS_CONFIG_W) {
3645 config = 1;
3646 } else { /* LYS_CONFIG_R */
3647 config = 0;
3648 }
3649
Michal Vasko14654712020-02-06 08:35:21 +01003650 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3651 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3652 if (parent->nodetype == LYS_LIST) {
3653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3654 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3655 return LY_EVALID;
3656 }
3657 }
3658
Radek Krejci7af64242019-02-18 13:07:53 +01003659 /* check status */
3660 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3661 (*key)->flags, (*key)->module, (*key)->name));
3662
3663 /* mark leaf as unique */
3664 (*key)->flags |= LYS_UNIQUE;
3665
3666 /* next unique value in line */
3667 keystr = delim;
3668 }
3669 /* next unique definition */
3670 }
3671
3672 return LY_SUCCESS;
3673}
3674
3675/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003676 * @brief Compile parsed list node information.
3677 * @param[in] ctx Compile context
3678 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003679 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3680 * is enriched with the list-specific information.
3681 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3682 */
3683static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003684lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003685{
3686 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3687 struct lysc_node_list *list = (struct lysc_node_list*)node;
3688 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003689 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003690 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003691 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003692 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003693 LY_ERR ret = LY_SUCCESS;
3694
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003695 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003696 if (list->min) {
3697 list->flags |= LYS_MAND_TRUE;
3698 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003699 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3700
3701 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003702 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003703 }
3704
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003705 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003706 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3707 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003708 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003709 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003710
3711 /* keys */
3712 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3714 return LY_EVALID;
3715 }
3716
3717 /* find all the keys (must be direct children) */
3718 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003719 if (!keystr) {
3720 /* keyless list */
3721 list->flags |= LYS_KEYLESS;
3722 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003723 while (keystr) {
3724 delim = strpbrk(keystr, " \t\n");
3725 if (delim) {
3726 len = delim - keystr;
3727 while (isspace(*delim)) {
3728 ++delim;
3729 }
3730 } else {
3731 len = strlen(keystr);
3732 }
3733
3734 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003735 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 +02003736 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3738 "The list's key \"%.*s\" not found.", len, keystr);
3739 return LY_EVALID;
3740 }
3741 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003742 if (key->flags & LYS_KEY) {
3743 /* the node was already marked as a key */
3744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3745 "Duplicated key identifier \"%.*s\".", len, keystr);
3746 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003747 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003748
3749 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003750 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003751 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3753 return LY_EVALID;
3754 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003755 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003756 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003757 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003759 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003760 return LY_EVALID;
3761 }
3762 } else {
3763 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003764 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003766 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003767 return LY_EVALID;
3768 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003769 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003770 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003771 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003772 return LY_EVALID;
3773 }
3774 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003775
3776 /* check status */
3777 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003778 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003779
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003780 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003781 if (key->dflt) {
3782 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3783 lysc_type_free(ctx->ctx, key->dflt->realtype);
3784 free(key->dflt);
3785 key->dflt = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003786 }
3787 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003788 key->flags |= LYS_KEY;
3789
3790 /* move it to the correct position */
3791 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3792 /* fix links in closest previous siblings of the key */
3793 if (key->next) {
3794 key->next->prev = key->prev;
3795 } else {
3796 /* last child */
3797 list->child->prev = key->prev;
3798 }
3799 if (key->prev->next) {
3800 key->prev->next = key->next;
3801 }
3802 /* fix links in the key */
3803 if (prev_key) {
3804 key->prev = (struct lysc_node*)prev_key;
3805 key->next = prev_key->next;
3806 } else {
3807 key->prev = list->child->prev;
3808 key->next = list->child;
3809 }
3810 /* fix links in closes future siblings of the key */
3811 if (prev_key) {
3812 if (prev_key->next) {
3813 prev_key->next->prev = (struct lysc_node*)key;
3814 } else {
3815 list->child->prev = (struct lysc_node*)key;
3816 }
3817 prev_key->next = (struct lysc_node*)key;
3818 } else {
3819 list->child->prev = (struct lysc_node*)key;
3820 }
3821 /* fix links in parent */
3822 if (!key->prev->next) {
3823 list->child = (struct lysc_node*)key;
3824 }
3825 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003826
3827 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003828 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003829 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003830 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003831 }
3832
3833 /* uniques */
3834 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003835 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003836 }
3837
Radek Krejciec4da802019-05-02 13:02:41 +02003838 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3839 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003840
3841done:
3842 return ret;
3843}
3844
Radek Krejcib56c7502019-02-13 14:19:54 +01003845/**
3846 * @brief Do some checks and set the default choice's case.
3847 *
3848 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3849 *
3850 * @param[in] ctx Compile context.
3851 * @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,
3852 * not the reference to the imported module.
3853 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3854 * @return LY_ERR value.
3855 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003856static LY_ERR
3857lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3858{
3859 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3860 const char *prefix = NULL, *name;
3861 size_t prefix_len = 0;
3862
3863 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3864 name = strchr(dflt, ':');
3865 if (name) {
3866 prefix = dflt;
3867 prefix_len = name - prefix;
3868 ++name;
3869 } else {
3870 name = dflt;
3871 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003872 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003873 /* prefixed default case make sense only for the prefix of the schema itself */
3874 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3875 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3876 prefix_len, prefix);
3877 return LY_EVALID;
3878 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003879 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 +01003880 if (!ch->dflt) {
3881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3882 "Default case \"%s\" not found.", dflt);
3883 return LY_EVALID;
3884 }
3885 /* no mandatory nodes directly under the default case */
3886 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003887 if (iter->parent != (struct lysc_node*)ch->dflt) {
3888 break;
3889 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003890 if (iter->flags & LYS_MAND_TRUE) {
3891 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3892 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3893 return LY_EVALID;
3894 }
3895 }
Radek Krejci01342af2019-01-03 15:18:08 +01003896 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003897 return LY_SUCCESS;
3898}
3899
Radek Krejciccd20f12019-02-15 14:12:27 +01003900static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003901lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003902{
3903 struct lys_module *mod;
3904 const char *prefix = NULL, *name;
3905 size_t prefix_len = 0;
3906 struct lysc_node_case *cs;
3907 struct lysc_node *node;
3908
3909 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3910 name = strchr(dflt, ':');
3911 if (name) {
3912 prefix = dflt;
3913 prefix_len = name - prefix;
3914 ++name;
3915 } else {
3916 name = dflt;
3917 }
3918 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3919 if (prefix) {
3920 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003922 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3923 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003924 return LY_EVALID;
3925 }
3926 } else {
3927 mod = ctx->mod;
3928 }
3929 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003930 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 +01003931 if (!cs) {
3932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003933 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003934 return LY_EVALID;
3935 }
3936
3937 /* check that there is no mandatory node */
3938 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003939 if (node->parent != (struct lysc_node*)cs) {
3940 break;
3941 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003942 if (node->flags & LYS_MAND_TRUE) {
3943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003944 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3945 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003946 return LY_EVALID;
3947 }
3948 }
3949
3950 /* set the default case in choice */
3951 ch->dflt = cs;
3952 cs->flags |= LYS_SET_DFLT;
3953
3954 return LY_SUCCESS;
3955}
3956
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003957/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003958 * @brief Compile parsed choice node information.
3959 * @param[in] ctx Compile context
3960 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003961 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003962 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003963 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3964 */
3965static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003966lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003967{
3968 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3969 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3970 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003971 LY_ERR ret = LY_SUCCESS;
3972
Radek Krejci056d0a82018-12-06 16:57:25 +01003973 LY_LIST_FOR(ch_p->child, child_p) {
3974 if (child_p->nodetype == LYS_CASE) {
3975 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003976 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003977 }
3978 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003979 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003980 }
3981 }
3982
3983 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003984 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003985 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003986 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003987
Radek Krejci9800fb82018-12-13 14:26:23 +01003988 return ret;
3989}
3990
3991/**
3992 * @brief Compile parsed anydata or anyxml node information.
3993 * @param[in] ctx Compile context
3994 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01003995 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3996 * is enriched with the any-specific information.
3997 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3998 */
3999static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004000lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004001{
4002 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4003 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4004 unsigned int u;
4005 LY_ERR ret = LY_SUCCESS;
4006
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004007 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004008 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4009 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004010 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004011 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004012
4013 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004014 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4015 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004016 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004017done:
4018 return ret;
4019}
4020
Radek Krejcib56c7502019-02-13 14:19:54 +01004021/**
Michal Vasko795b3752020-07-13 15:24:27 +02004022 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4023 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004024 *
4025 * @param[in] ctx Compile context
4026 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4027 * the choice itself is expected instead of a specific case node.
4028 * @param[in] node Schema node to connect into the list.
4029 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004030 * 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 +01004031 */
4032static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004033lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004034{
Michal Vasko795b3752020-07-13 15:24:27 +02004035 struct lysc_node **children, *start, *end;
4036 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004037
4038 if (node->nodetype == LYS_CASE) {
4039 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4040 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004041 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004042 }
4043 if (children) {
4044 if (!(*children)) {
4045 /* first child */
4046 *children = node;
4047 } else if (*children != node) {
4048 /* by the condition in previous branch we cover the choice/case children
4049 * - the children list is shared by the choice and the the first case, in addition
4050 * the first child of each case must be referenced from the case node. So the node is
4051 * actually always already inserted in case it is the first children - so here such
4052 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004053 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4054 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4055 /* some augments are already connected and we are connecting new ones,
4056 * keep module name order and insert the node into the children list */
4057 end = (*children);
4058 do {
4059 end = end->prev;
4060 mod = end->module;
4061 while (end->prev->module == mod) {
4062 end = end->prev;
4063 }
Radek Krejcif6a11002020-08-21 13:29:07 +02004064 } 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 +02004065
4066 /* we have the last existing node after our node, easily get the first before and connect it */
4067 start = end->prev;
4068 start->next = node;
4069 node->next = end;
4070 end->prev = node;
4071 node->prev = start;
4072 } else {
4073 /* insert at the end of the parent's children list */
4074 (*children)->prev->next = node;
4075 node->prev = (*children)->prev;
4076 (*children)->prev = node;
4077 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004078
4079 /* check the name uniqueness */
4080 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4081 lysc_node_notifs(parent), node->name, node)) {
4082 return LY_EEXIST;
4083 }
4084 }
4085 }
4086 return LY_SUCCESS;
4087}
4088
Radek Krejci95710c92019-02-11 15:49:55 +01004089/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004090 * @brief Prepare the case structure in choice node for the new data node.
4091 *
4092 * 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
4093 * created in the choice when the first child was processed.
4094 *
4095 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004096 * @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 +02004097 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004098 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4099 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4100 * @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,
4101 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004102 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004103static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004104lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child)
Radek Krejci056d0a82018-12-06 16:57:25 +01004105{
4106 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004107 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004108 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004109 unsigned int u;
4110 LY_ERR ret;
4111
Radek Krejci95710c92019-02-11 15:49:55 +01004112#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004113 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004114 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4116 return NULL; \
4117 } \
4118 }
4119
Radek Krejci39424802020-08-12 09:31:00 +02004120 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci95710c92019-02-11 15:49:55 +01004121 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004122
4123 /* we have to add an implicit case node into the parent choice */
4124 cs = calloc(1, sizeof(struct lysc_node_case));
4125 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004126 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004127 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004128 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004129 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4130 /* the case is already present since the child is not its first children */
4131 return (struct lysc_node_case*)ch->cases->prev;
4132 }
Radek Krejci95710c92019-02-11 15:49:55 +01004133 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004134
4135 /* explicit parent case is not present (this is its first child) */
4136 cs = calloc(1, sizeof(struct lysc_node_case));
4137 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004138 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004139 cs->flags = LYS_STATUS_MASK & node_p->flags;
4140 cs->sp = node_p;
4141
Radek Krejcib1b59152019-01-07 13:21:56 +01004142 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
Radek Krejcibae745f2019-04-09 16:28:00 +02004143 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004144
4145 if (node_p->when) {
4146 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004147 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004148 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004149
4150 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4151 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004152 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004153 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004154 }
Radek Krejciec4da802019-05-02 13:02:41 +02004155 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004156 } else {
4157 LOGINT(ctx->ctx);
4158 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004159 }
4160 cs->module = ctx->mod;
4161 cs->prev = (struct lysc_node*)cs;
4162 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004163 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004164 cs->child = child;
4165
4166 return cs;
4167error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004168 if (cs) {
4169 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4170 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004171 return NULL;
4172
4173#undef UNIQUE_CHECK
4174}
4175
Radek Krejcib56c7502019-02-13 14:19:54 +01004176/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004177 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004178 *
4179 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004180 * @param[in] node Target node where the config is supposed to be changed.
4181 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004182 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4183 * 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 +01004184 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4185 * @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 +01004186 * @return LY_ERR value.
4187 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004188static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004189lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004190 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004191{
4192 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004193 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004194
4195 if (config == (node->flags & LYS_CONFIG_MASK)) {
4196 /* nothing to do */
4197 return LY_SUCCESS;
4198 }
4199
4200 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004201 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004202 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4203 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004204 "Invalid %s of config - configuration node cannot be child of any state data node.",
4205 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004206 return LY_EVALID;
4207 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004208 node->flags |= LYS_SET_CONFIG;
4209 } else {
4210 if (node->flags & LYS_SET_CONFIG) {
4211 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4212 /* setting config flags, but have node with explicit config true */
4213 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004214 "Invalid %s of config - configuration node cannot be child of any state data node.",
4215 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004216 return LY_EVALID;
4217 }
4218 /* do not change config on nodes where the config is explicitely set, this does not apply to
4219 * nodes, which are being changed explicitly (targets of refine or deviation) */
4220 return LY_SUCCESS;
4221 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004222 }
4223 node->flags &= ~LYS_CONFIG_MASK;
4224 node->flags |= config;
4225
4226 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004227 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004228 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004229 }
4230
Radek Krejci76b3e962018-12-14 17:01:25 +01004231 return LY_SUCCESS;
4232}
4233
Radek Krejcib56c7502019-02-13 14:19:54 +01004234/**
4235 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4236 *
4237 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4238 * the flag to such parents from a mandatory children.
4239 *
4240 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4241 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4242 * (mandatory children was removed).
4243 */
Radek Krejcife909632019-02-12 15:34:42 +01004244void
4245lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4246{
4247 struct lysc_node *iter;
4248
4249 if (add) { /* set flag */
4250 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4251 parent = parent->parent) {
4252 parent->flags |= LYS_MAND_TRUE;
4253 }
4254 } else { /* unset flag */
4255 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004256 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004257 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004258 /* there is another mandatory node */
4259 return;
4260 }
4261 }
4262 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4263 parent->flags &= ~LYS_MAND_TRUE;
4264 }
4265 }
4266}
4267
Radek Krejci056d0a82018-12-06 16:57:25 +01004268/**
Radek Krejci3641f562019-02-13 15:38:40 +01004269 * @brief Internal sorting process for the lys_compile_augment_sort().
4270 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4271 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4272 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4273 */
4274static void
4275lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4276{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004277 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004278 size_t len;
4279
4280 len = strlen(aug_p->nodeid);
4281 LY_ARRAY_FOR(result, v) {
4282 if (strlen(result[v]->nodeid) <= len) {
4283 continue;
4284 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004285 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004286 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004287 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004288 break;
4289 }
4290 }
4291 result[v] = aug_p;
4292 LY_ARRAY_INCREMENT(result);
4293}
4294
4295/**
4296 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4297 *
4298 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4299 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4300 *
4301 * @param[in] ctx Compile context.
4302 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4303 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4304 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4305 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4306 * @return LY_ERR value.
4307 */
4308LY_ERR
4309lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4310{
4311 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004312 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004313
4314 assert(augments);
4315
4316 /* get count of the augments in module and all its submodules */
4317 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004318 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004319 }
4320 LY_ARRAY_FOR(inc_p, u) {
4321 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004322 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004323 }
4324 }
4325
4326 if (!count) {
4327 *augments = NULL;
4328 return LY_SUCCESS;
4329 }
4330 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4331
4332 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4333 * together, so there can be even /z/y betwwen them. */
4334 LY_ARRAY_FOR(aug_p, u) {
4335 lys_compile_augment_sort_(&aug_p[u], result);
4336 }
4337 LY_ARRAY_FOR(inc_p, u) {
4338 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4339 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4340 }
4341 }
4342
4343 *augments = result;
4344 return LY_SUCCESS;
4345}
4346
4347/**
4348 * @brief Compile the parsed augment connecting it into its target.
4349 *
4350 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4351 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4352 * are already implemented and compiled.
4353 *
4354 * @param[in] ctx Compile context.
4355 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004356 * @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
4357 * children in case of the augmenting uses data.
4358 * @return LY_SUCCESS on success.
4359 * @return LY_EVALID on failure.
4360 */
4361LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004362lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004363{
Michal Vaskoe6143202020-07-03 13:02:08 +02004364 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004365 struct lysp_node *node_p, *case_node_p;
4366 struct lysc_node *target; /* target target of the augment */
4367 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004368 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004369 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004370 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004371 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004372 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejciec4da802019-05-02 13:02:41 +02004373 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004374
Radek Krejci327de162019-06-14 12:52:07 +02004375 lysc_update_path(ctx, NULL, "{augment}");
4376 lysc_update_path(ctx, NULL, aug_p->nodeid);
4377
Radek Krejci7af64242019-02-18 13:07:53 +01004378 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004379 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004380 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004381 if (ret != LY_SUCCESS) {
4382 if (ret == LY_EDENIED) {
4383 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4384 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4385 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4386 }
4387 return LY_EVALID;
4388 }
4389
4390 /* check for mandatory nodes
4391 * - new cases augmenting some choice can have mandatory nodes
4392 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4393 */
Radek Krejci733988a2019-02-15 15:12:44 +01004394 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004395 allow_mandatory = 1;
4396 }
4397
4398 when_shared = NULL;
4399 LY_LIST_FOR(aug_p->child, node_p) {
4400 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4401 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004402 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004403 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4404 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004405 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004406 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4407 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004408 return LY_EVALID;
4409 }
4410
4411 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004412 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004413 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004414 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004415 } else {
4416 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004417 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004418 }
4419 }
Radek Krejciec4da802019-05-02 13:02:41 +02004420 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004421
Radek Krejcife13da42019-02-15 14:51:01 +01004422 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4423 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004424 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004425 /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */
Radek Krejci1e008d22020-08-17 11:37:37 +02004426 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 +01004427 } else if (target->nodetype == LYS_CHOICE) {
4428 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4429 node = ((struct lysc_node_choice*)target)->cases->prev;
4430 } else {
4431 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004432 node = (struct lysc_node*)lysc_node_children(target, flags);
4433 if (!node) {
4434 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4435 break;
4436 }
4437 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004438 }
4439
Radek Krejci733988a2019-02-15 15:12:44 +01004440 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004441 node->flags &= ~LYS_MAND_TRUE;
4442 lys_compile_mandatory_parents(target, 0);
4443 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004444 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004445 return LY_EVALID;
4446 }
4447
4448 /* pass augment's when to all the children */
4449 if (aug_p->when) {
4450 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4451 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004452 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004453 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004454
4455 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4456 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004457 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004458 }
4459
Radek Krejci3641f562019-02-13 15:38:40 +01004460 when_shared = *when;
4461 } else {
4462 ++when_shared->refcount;
4463 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004464
4465 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4466 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004467 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004468 }
Radek Krejci3641f562019-02-13 15:38:40 +01004469 }
4470 }
4471 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004472
Radek Krejciec4da802019-05-02 13:02:41 +02004473 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004474 switch (target->nodetype) {
4475 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004476 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004477 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004478 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004479 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004480 break;
4481 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004482 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004483 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004484 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004485 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004486 break;
4487 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004488 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004489 if (aug_p->actions) {
4490 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004491 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4492 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004493 return LY_EVALID;
4494 }
4495 if (aug_p->notifs) {
4496 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004497 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004498 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004499 return LY_EVALID;
4500 }
4501 }
Radek Krejci3641f562019-02-13 15:38:40 +01004502
Michal Vaskoe6143202020-07-03 13:02:08 +02004503 /* add this module into the target module augmented_by, if not there already */
4504 rc = LY_SUCCESS;
4505 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4506 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4507 rc = LY_EEXIST;
4508 break;
4509 }
4510 }
4511 if (!rc) {
4512 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4513 *aug_mod = ctx->mod;
4514 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004515
Radek Krejci327de162019-06-14 12:52:07 +02004516 lysc_update_path(ctx, NULL, NULL);
4517 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004518
Radek Krejci3641f562019-02-13 15:38:40 +01004519error:
Radek Krejciec4da802019-05-02 13:02:41 +02004520 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004521 return ret;
4522}
4523
4524/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004525 * @brief Apply refined or deviated mandatory flag to the target node.
4526 *
4527 * @param[in] ctx Compile context.
4528 * @param[in] node Target node where the mandatory property is supposed to be changed.
4529 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004530 * @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 +01004531 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4532 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4533 * 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 +01004534 * @return LY_ERR value.
4535 */
4536static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004537lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, int refine_flag)
Radek Krejcif1421c22019-02-19 13:05:20 +01004538{
4539 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4540 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004541 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4542 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004543 return LY_EVALID;
4544 }
4545
4546 if (mandatory_flag & LYS_MAND_TRUE) {
4547 /* check if node has default value */
4548 if (node->nodetype & LYS_LEAF) {
4549 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004550 if (refine_flag) {
4551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004552 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004553 return LY_EVALID;
4554 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004555 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004556 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004557 if (refine_flag) {
4558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004559 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004560 return LY_EVALID;
4561 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004562 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004563 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004564 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 +01004565 return LY_EVALID;
4566 }
4567
4568 node->flags &= ~LYS_MAND_FALSE;
4569 node->flags |= LYS_MAND_TRUE;
4570 lys_compile_mandatory_parents(node->parent, 1);
4571 } else {
4572 /* make mandatory false */
4573 node->flags &= ~LYS_MAND_TRUE;
4574 node->flags |= LYS_MAND_FALSE;
4575 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcif1421c22019-02-19 13:05:20 +01004576 }
4577 return LY_SUCCESS;
4578}
4579
4580/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004581 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4582 * If present, also apply uses's modificators.
4583 *
4584 * @param[in] ctx Compile context
4585 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004586 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4587 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4588 * the compile context.
4589 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4590 */
4591static LY_ERR
Radek Krejcic6b4f442020-08-12 14:45:18 +02004592lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p)
Radek Krejcie86bf772018-12-14 11:39:53 +01004593{
4594 struct lysp_node *node_p;
Radek Krejcic6b4f442020-08-12 14:45:18 +02004595 struct lysc_node *node, *child, *iter;
Radek Krejci12fb9142019-01-08 09:45:30 +01004596 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004597 struct lysc_node_container context_node_fake =
4598 {.nodetype = LYS_CONTAINER,
4599 .module = ctx->mod,
4600 .flags = parent ? parent->flags : 0,
4601 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004602 .prev = (struct lysc_node*)&context_node_fake,
4603 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004604 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004605 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004606 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004607 int found;
4608 const char *id, *name, *prefix;
4609 size_t prefix_len, name_len;
4610 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004611 struct lysp_refine *rfn;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004612 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004613 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004614 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004615 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004616 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004617 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004618 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004619 struct lysc_notif **notifs = NULL;
4620 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004621
4622 /* search for the grouping definition */
4623 found = 0;
4624 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004625 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004626 if (prefix) {
4627 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4628 if (!mod) {
4629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004630 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004631 return LY_EVALID;
4632 }
4633 } else {
4634 mod = ctx->mod_def;
4635 }
4636 if (mod == ctx->mod_def) {
4637 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004638 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004639 LY_ARRAY_FOR(grp, u) {
4640 if (!strcmp(grp[u].name, name)) {
4641 grp = &grp[u];
4642 found = 1;
4643 break;
4644 }
4645 }
4646 }
4647 }
4648 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004649 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004650 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004651 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004652 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004653 if (!strcmp(grp[u].name, name)) {
4654 grp = &grp[u];
4655 found = 1;
4656 }
4657 }
4658 }
4659 if (!found && mod->parsed->includes) {
4660 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004661 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004662 grp = mod->parsed->includes[u].submodule->groupings;
4663 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004664 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004665 if (!strcmp(grp[v].name, name)) {
4666 grp = &grp[v];
4667 found = 1;
4668 }
4669 }
4670 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004671 }
4672 }
4673 }
4674 if (!found) {
4675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4676 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4677 return LY_EVALID;
4678 }
4679
4680 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4681 grp_stack_count = ctx->groupings.count;
4682 ly_set_add(&ctx->groupings, (void*)grp, 0);
4683 if (grp_stack_count == ctx->groupings.count) {
4684 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4686 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4687 return LY_EVALID;
4688 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004689 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4690 /* remember that the grouping is instantiated to avoid its standalone validation */
4691 grp->flags |= LYS_USED_GRP;
4692 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004693
4694 /* switch context's mod_def */
4695 mod_old = ctx->mod_def;
4696 ctx->mod_def = mod;
4697
4698 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004699 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup);
Radek Krejcie86bf772018-12-14 11:39:53 +01004700
Radek Krejcic6b4f442020-08-12 14:45:18 +02004701 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
4702 * applu refine and augment only to the nodes from the uses */
4703 if (parent) {
4704 if (parent->nodetype == LYS_CASE) {
4705 child = (struct lysc_node*)lysc_node_children(parent->parent, ctx->options & LYSC_OPT_RPC_MASK);
4706 } else {
4707 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4708 }
4709 } else if (ctx->mod->compiled->data) {
4710 child = ctx->mod->compiled->data;
4711 } else {
4712 child = NULL;
4713 }
4714 /* remember the last child */
4715 if (child) {
4716 child = child->prev;
4717 }
4718
Radek Krejcifc11bd72019-04-11 16:00:05 +02004719 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004720 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004721 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
Radek Krejciec4da802019-05-02 13:02:41 +02004722 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01004723 }
Radek Krejcic6b4f442020-08-12 14:45:18 +02004724
4725 /* split the children and add the uses's data into the fake context node */
4726 if (child) {
4727 context_node_fake.child = child->next;
4728 } else if (parent) {
4729 context_node_fake.child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4730 } else if (ctx->mod->compiled->data) {
4731 context_node_fake.child = ctx->mod->compiled->data;
4732 }
4733 if (context_node_fake.child) {
4734 /* remember child as the last data node added by grouping to fix the list later */
4735 child = context_node_fake.child->prev;
4736 context_node_fake.child->prev = NULL;
4737 }
4738
Radek Krejci00b874b2019-02-12 10:54:50 +01004739 when_shared = NULL;
Radek Krejcic6b4f442020-08-12 14:45:18 +02004740 LY_LIST_FOR(context_node_fake.child, iter) {
4741 iter->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004742
Radek Krejcifc11bd72019-04-11 16:00:05 +02004743 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004744 if (uses_p->when) {
Radek Krejcic6b4f442020-08-12 14:45:18 +02004745 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004746 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004747 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4748
4749 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4750 /* do not check "when" semantics in a grouping */
Radek Krejcic6b4f442020-08-12 14:45:18 +02004751 ly_set_add(&ctx->xpath, iter, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004752 }
4753
Radek Krejci00b874b2019-02-12 10:54:50 +01004754 when_shared = *when;
4755 } else {
4756 ++when_shared->refcount;
4757 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004758
4759 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4760 /* in this case check "when" again for all children because of dummy node check */
Radek Krejcic6b4f442020-08-12 14:45:18 +02004761 ly_set_add(&ctx->xpath, iter, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004762 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004763 }
4764 }
Radek Krejci01342af2019-01-03 15:18:08 +01004765 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004766
Radek Krejcifc11bd72019-04-11 16:00:05 +02004767 /* compile actions */
4768 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4769 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004770 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004771 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004772 if (*actions && (uses_p->augments || uses_p->refines)) {
4773 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004774 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4775 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4776 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004777 }
4778 }
4779
4780 /* compile notifications */
4781 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4782 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004783 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004784 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004785 if (*notifs && (uses_p->augments || uses_p->refines)) {
4786 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004787 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4788 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4789 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004790 }
4791 }
4792
4793
Radek Krejci3641f562019-02-13 15:38:40 +01004794 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004795 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004796 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004797 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004798 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004799
Radek Krejcif0089082019-01-07 16:42:01 +01004800 /* reload previous context's mod_def */
4801 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004802 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004803
Radek Krejci76b3e962018-12-14 17:01:25 +01004804 /* apply refine */
4805 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004806 lysc_update_path(ctx, NULL, rfn->nodeid);
4807
Radek Krejci7af64242019-02-18 13:07:53 +01004808 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, ctx->mod,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004809 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004810 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004811 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004812
4813 /* default value */
4814 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004815 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004816 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004817 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4818 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004819 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004820 }
4821 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004823 "Invalid refine of default - %s cannot hold default value(s).",
4824 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004825 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004826 }
4827 if (node->nodetype == LYS_LEAF) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004828 /* postpone default compilation when the tree is complete */
4829 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0],
4830 ctx->mod_def), cleanup);
4831 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004832 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004833 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004834 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4835 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004836 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004837 }
Radek Krejcia1911222019-07-22 17:24:50 +02004838
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004839 /* postpone default compilation when the tree is complete */
4840 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts,
4841 ctx->mod_def), cleanup);
4842 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004843 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004844 if (((struct lysc_node_choice*)node)->dflt) {
4845 /* unset LYS_SET_DFLT from the current default case */
4846 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4847 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004848 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004849 }
4850 }
4851
Radek Krejci12fb9142019-01-08 09:45:30 +01004852 /* description */
4853 if (rfn->dsc) {
4854 FREE_STRING(ctx->ctx, node->dsc);
4855 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4856 }
4857
4858 /* reference */
4859 if (rfn->ref) {
4860 FREE_STRING(ctx->ctx, node->ref);
4861 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4862 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004863
4864 /* config */
4865 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004866 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004867 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004868 } else {
4869 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004870 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004871 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004872 }
4873
4874 /* mandatory */
4875 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004876 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004877 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004878
4879 /* presence */
4880 if (rfn->presence) {
4881 if (node->nodetype != LYS_CONTAINER) {
4882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004883 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4884 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004885 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004886 }
4887 node->flags |= LYS_PRESENCE;
4888 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004889
4890 /* must */
4891 if (rfn->musts) {
4892 switch (node->nodetype) {
4893 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004894 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 +01004895 break;
4896 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004897 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 +01004898 break;
4899 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004900 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 +01004901 break;
4902 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004903 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 +01004904 break;
4905 case LYS_ANYXML:
4906 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004907 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 +01004908 break;
4909 default:
4910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004911 "Invalid refine of must statement - %s cannot hold any must statement.",
4912 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004913 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004914 }
Michal Vasko004d3152020-06-11 19:59:22 +02004915 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01004916 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004917
4918 /* min/max-elements */
4919 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4920 switch (node->nodetype) {
4921 case LYS_LEAFLIST:
4922 if (rfn->flags & LYS_SET_MAX) {
4923 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4924 }
4925 if (rfn->flags & LYS_SET_MIN) {
4926 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004927 if (rfn->min) {
4928 node->flags |= LYS_MAND_TRUE;
4929 lys_compile_mandatory_parents(node->parent, 1);
4930 } else {
4931 node->flags &= ~LYS_MAND_TRUE;
4932 lys_compile_mandatory_parents(node->parent, 0);
4933 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004934 }
4935 break;
4936 case LYS_LIST:
4937 if (rfn->flags & LYS_SET_MAX) {
4938 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4939 }
4940 if (rfn->flags & LYS_SET_MIN) {
4941 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004942 if (rfn->min) {
4943 node->flags |= LYS_MAND_TRUE;
4944 lys_compile_mandatory_parents(node->parent, 1);
4945 } else {
4946 node->flags &= ~LYS_MAND_TRUE;
4947 lys_compile_mandatory_parents(node->parent, 0);
4948 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004949 }
4950 break;
4951 default:
4952 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004953 "Invalid refine of %s statement - %s cannot hold this statement.",
4954 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004955 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004956 }
4957 }
Radek Krejcif0089082019-01-07 16:42:01 +01004958
4959 /* if-feature */
4960 if (rfn->iffeatures) {
4961 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004962 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004963 }
Radek Krejci327de162019-06-14 12:52:07 +02004964
4965 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004966 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004967
Radek Krejcif2271f12019-01-07 16:42:23 +01004968 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004969 for (uint32_t i = 0; i < refined.count; ++i) {
4970 node = (struct lysc_node*)refined.objs[i];
4971 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02004972 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004973
4974 /* check possible conflict with default value (default added, mandatory left true) */
4975 if ((node->flags & LYS_MAND_TRUE) &&
4976 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4977 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004979 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004980 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004981 }
4982
4983 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4984 if (node->nodetype == LYS_LIST) {
4985 min = ((struct lysc_node_list*)node)->min;
4986 max = ((struct lysc_node_list*)node)->max;
4987 } else {
4988 min = ((struct lysc_node_leaflist*)node)->min;
4989 max = ((struct lysc_node_leaflist*)node)->max;
4990 }
4991 if (min > max) {
4992 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004993 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
4994 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004995 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004996 }
4997 }
4998 }
4999
Radek Krejcic6b4f442020-08-12 14:45:18 +02005000 if (first_p) {
5001 *first_p = context_node_fake.child;
5002 }
Radek Krejci327de162019-06-14 12:52:07 +02005003 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005004 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005005
5006cleanup:
5007 /* fix connection of the children nodes from fake context node back into the parent */
5008 if (context_node_fake.child) {
5009 context_node_fake.child->prev = child;
5010 }
5011 LY_LIST_FOR(context_node_fake.child, child) {
5012 child->parent = parent;
5013 }
5014
5015 if (uses_p->augments || uses_p->refines) {
5016 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005017 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005018 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005019 LY_ARRAY_FREE(context_node_fake.actions);
5020 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005021 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005022 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005023 LY_ARRAY_FREE(context_node_fake.notifs);
5024 }
5025 }
5026
Radek Krejcie86bf772018-12-14 11:39:53 +01005027 /* reload previous context's mod_def */
5028 ctx->mod_def = mod_old;
5029 /* remove the grouping from the stack for circular groupings dependency check */
5030 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5031 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005032 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005033 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005034
5035 return ret;
5036}
5037
Radek Krejci327de162019-06-14 12:52:07 +02005038static int
5039lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5040{
5041 struct lysp_node *iter;
5042 int len = 0;
5043
5044 *path = NULL;
5045 for (iter = node; iter && len >= 0; iter = iter->parent) {
5046 char *s = *path;
5047 char *id;
5048
5049 switch (iter->nodetype) {
5050 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005051 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005052 break;
5053 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005054 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005055 break;
5056 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005057 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005058 break;
5059 default:
5060 id = strdup(iter->name);
5061 break;
5062 }
5063
5064 if (!iter->parent) {
5065 /* print prefix */
5066 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5067 } else {
5068 /* prefix is the same as in parent */
5069 len = asprintf(path, "/%s%s", id, s ? s : "");
5070 }
5071 free(s);
5072 free(id);
5073 }
5074
5075 if (len < 0) {
5076 free(*path);
5077 *path = NULL;
5078 } else if (len == 0) {
5079 *path = strdup("/");
5080 len = 1;
5081 }
5082 return len;
5083}
5084
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005085/**
5086 * @brief Validate groupings that were defined but not directly used in the schema itself.
5087 *
5088 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5089 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5090 */
5091static LY_ERR
5092lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5093{
5094 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005095 char *path;
5096 int len;
5097
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005098 struct lysp_node_uses fake_uses = {
5099 .parent = node_p,
5100 .nodetype = LYS_USES,
5101 .flags = 0, .next = NULL,
5102 .name = grp->name,
5103 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5104 .refines = NULL, .augments = NULL
5105 };
5106 struct lysc_node_container fake_container = {
5107 .nodetype = LYS_CONTAINER,
5108 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5109 .module = ctx->mod,
5110 .sp = NULL, .parent = NULL, .next = NULL,
5111 .prev = (struct lysc_node*)&fake_container,
5112 .name = "fake",
5113 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5114 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5115 };
5116
5117 if (grp->parent) {
5118 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5119 }
Radek Krejci327de162019-06-14 12:52:07 +02005120
5121 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5122 if (len < 0) {
5123 LOGMEM(ctx->ctx);
5124 return LY_EMEM;
5125 }
5126 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5127 ctx->path_len = (uint16_t)len;
5128 free(path);
5129
5130 lysc_update_path(ctx, NULL, "{grouping}");
5131 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcic6b4f442020-08-12 14:45:18 +02005132 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005133 lysc_update_path(ctx, NULL, NULL);
5134 lysc_update_path(ctx, NULL, NULL);
5135
5136 ctx->path_len = 1;
5137 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005138
5139 /* cleanup */
5140 lysc_node_container_free(ctx->ctx, &fake_container);
5141
5142 return ret;
5143}
Radek Krejcife909632019-02-12 15:34:42 +01005144
Radek Krejcie86bf772018-12-14 11:39:53 +01005145/**
Radek Krejcia3045382018-11-22 14:30:31 +01005146 * @brief Compile parsed schema node information.
5147 * @param[in] ctx Compile context
5148 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005149 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5150 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5151 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005152 * @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).
5153 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005154 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5155 */
Radek Krejci19a96102018-11-15 13:38:09 +01005156static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005157lys_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 +01005158{
Radek Krejci1c54f462020-05-12 17:25:34 +02005159 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005160 struct lysc_node *node = NULL;
5161 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005162 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005163 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005164 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005165
Radek Krejci327de162019-06-14 12:52:07 +02005166 if (node_p->nodetype != LYS_USES) {
5167 lysc_update_path(ctx, parent, node_p->name);
5168 } else {
5169 lysc_update_path(ctx, NULL, "{uses}");
5170 lysc_update_path(ctx, NULL, node_p->name);
5171 }
5172
Radek Krejci19a96102018-11-15 13:38:09 +01005173 switch (node_p->nodetype) {
5174 case LYS_CONTAINER:
5175 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5176 node_compile_spec = lys_compile_node_container;
5177 break;
5178 case LYS_LEAF:
5179 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5180 node_compile_spec = lys_compile_node_leaf;
5181 break;
5182 case LYS_LIST:
5183 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005184 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005185 break;
5186 case LYS_LEAFLIST:
5187 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005188 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005189 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005190 case LYS_CHOICE:
5191 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005192 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005193 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005194 case LYS_ANYXML:
5195 case LYS_ANYDATA:
5196 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005197 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005198 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005199 case LYS_USES:
Radek Krejcic6b4f442020-08-12 14:45:18 +02005200 if (parent && parent->nodetype == LYS_CHOICE) {
5201 assert(node_p->parent->nodetype == LYS_CASE);
5202 lysc_update_path(ctx, parent, node_p->parent->name);
5203 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, NULL);
5204 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
5205 }
5206
5207 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, cs ? (struct lysc_node*)cs : parent, &node);
5208 if (cs) {
5209 cs->child = node;
5210 lysc_update_path(ctx, NULL, NULL);
5211 }
Radek Krejci327de162019-06-14 12:52:07 +02005212 lysc_update_path(ctx, NULL, NULL);
5213 lysc_update_path(ctx, NULL, NULL);
5214 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005215 default:
5216 LOGINT(ctx->ctx);
5217 return LY_EINT;
5218 }
5219 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5220 node->nodetype = node_p->nodetype;
5221 node->module = ctx->mod;
5222 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005223 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005224
5225 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005226 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005227 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005228 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005229 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5230 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005231 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005232 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005233 node->flags |= LYS_CONFIG_R;
5234 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005235 /* config not explicitely set, inherit it from parent */
5236 if (parent) {
5237 node->flags |= parent->flags & LYS_CONFIG_MASK;
5238 } else {
5239 /* default is config true */
5240 node->flags |= LYS_CONFIG_W;
5241 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005242 } else {
5243 /* config set explicitely */
5244 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005245 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005246 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5248 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005249 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005250 goto error;
5251 }
Radek Krejci19a96102018-11-15 13:38:09 +01005252
Radek Krejcia6d57732018-11-29 13:40:37 +01005253 /* *list ordering */
5254 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5255 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005256 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005257 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5258 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005259 node->flags &= ~LYS_ORDBY_MASK;
5260 node->flags |= LYS_ORDBY_SYSTEM;
5261 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5262 /* default ordering is system */
5263 node->flags |= LYS_ORDBY_SYSTEM;
5264 }
5265 }
5266
Radek Krejci19a96102018-11-15 13:38:09 +01005267 /* status - it is not inherited by specification, but it does not make sense to have
5268 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005269 if (!parent || parent->nodetype != LYS_CHOICE) {
5270 /* in case of choice/case's children, postpone the check to the moment we know if
5271 * the parent is choice (parent here) or some case (so we have to get its flags to check) */
Radek Krejci1c54f462020-05-12 17:25:34 +02005272 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 +01005273 }
5274
Radek Krejciec4da802019-05-02 13:02:41 +02005275 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005276 node->sp = node_p;
5277 }
5278 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005279 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5280 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005281 if (node_p->when) {
5282 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005283 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005284
5285 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5286 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005287 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005288 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005289 }
Radek Krejciec4da802019-05-02 13:02:41 +02005290 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005291
5292 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005293 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005294
Radek Krejci0935f412019-08-20 16:15:18 +02005295 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5296
Radek Krejcife909632019-02-12 15:34:42 +01005297 /* inherit LYS_MAND_TRUE in parent containers */
5298 if (node->flags & LYS_MAND_TRUE) {
5299 lys_compile_mandatory_parents(parent, 1);
5300 }
5301
Radek Krejci327de162019-06-14 12:52:07 +02005302 lysc_update_path(ctx, NULL, NULL);
5303
Radek Krejci19a96102018-11-15 13:38:09 +01005304 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005305 if (parent) {
5306 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005307 if (node_p->parent->nodetype == LYS_CASE) {
5308 lysc_update_path(ctx, parent, node_p->parent->name);
5309 } else {
5310 lysc_update_path(ctx, parent, node->name);
5311 }
Radek Krejciec4da802019-05-02 13:02:41 +02005312 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005313 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005314 if (uses_status) {
5315
5316 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005317 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005318 * it directly gets the same status flags as the choice;
5319 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005320 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005321 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005322 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005323 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005324 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005325 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005326 }
Radek Krejciec4da802019-05-02 13:02:41 +02005327 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
Radek Krejci327de162019-06-14 12:52:07 +02005328
5329 if (parent->nodetype == LYS_CHOICE) {
5330 lysc_update_path(ctx, NULL, NULL);
5331 }
Radek Krejci19a96102018-11-15 13:38:09 +01005332 } else {
5333 /* top-level element */
5334 if (!ctx->mod->compiled->data) {
5335 ctx->mod->compiled->data = node;
5336 } else {
5337 /* insert at the end of the module's top-level nodes list */
5338 ctx->mod->compiled->data->prev->next = node;
5339 node->prev = ctx->mod->compiled->data->prev;
5340 ctx->mod->compiled->data->prev = node;
5341 }
Radek Krejci327de162019-06-14 12:52:07 +02005342 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005343 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5344 ctx->mod->compiled->notifs, node->name, node)) {
5345 return LY_EVALID;
5346 }
Radek Krejci19a96102018-11-15 13:38:09 +01005347 }
Radek Krejci327de162019-06-14 12:52:07 +02005348 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005349
5350 return LY_SUCCESS;
5351
5352error:
5353 lysc_node_free(ctx->ctx, node);
5354 return ret;
5355}
5356
Radek Krejciccd20f12019-02-15 14:12:27 +01005357static void
5358lysc_disconnect(struct lysc_node *node)
5359{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005360 struct lysc_node *parent, *child, *prev = NULL, *next;
5361 struct lysc_node_case *cs = NULL;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005362 struct lysc_module *modc = node->module->compiled;
Radek Krejciccd20f12019-02-15 14:12:27 +01005363 int remove_cs = 0;
5364
5365 parent = node->parent;
5366
5367 /* parent's first child */
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005368 if (parent && parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005369 cs = (struct lysc_node_case*)node;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005370 } else if (parent && parent->nodetype == LYS_CASE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005371 /* disconnecting some node in a case */
5372 cs = (struct lysc_node_case*)parent;
5373 parent = cs->parent;
5374 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5375 if (child == node) {
5376 if (cs->child == child) {
5377 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5378 /* case with a single child -> remove also the case */
5379 child->parent = NULL;
5380 remove_cs = 1;
5381 } else {
5382 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005383 }
5384 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005385 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005386 }
5387 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005388 if (!remove_cs) {
5389 cs = NULL;
5390 }
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005391 } else if (parent && lysc_node_children(parent, node->flags) == node) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005392 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005393 } else if (modc->data == node) {
5394 modc->data = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005395 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005396 if (cs) {
5397 if (remove_cs) {
5398 /* cs has only one child which is being also removed */
5399 lysc_disconnect((struct lysc_node*)cs);
5400 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5401 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005402 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5403 /* default case removed */
5404 ((struct lysc_node_choice*)parent)->dflt = NULL;
5405 }
5406 if (((struct lysc_node_choice*)parent)->cases == cs) {
5407 /* first case removed */
5408 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5409 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005410 if (cs->child) {
5411 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5412 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5413 prev = cs->child->prev;
5414 } /* else all the children are under a single case */
5415 LY_LIST_FOR_SAFE(cs->child, next, child) {
5416 if (child->parent != (struct lysc_node*)cs) {
5417 break;
5418 }
5419 lysc_node_free(node->module->ctx, child);
5420 }
5421 if (prev) {
5422 if (prev->next) {
5423 prev->next = child;
5424 }
5425 if (child) {
5426 child->prev = prev;
5427 } else {
5428 /* link from the first child under the cases */
5429 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5430 }
5431 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005432 }
5433 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005434 }
5435
5436 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005437 if (node->prev->next) {
5438 node->prev->next = node->next;
5439 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005440 if (node->next) {
5441 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005442 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005443 if (parent) {
5444 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
5445 } else {
5446 child = modc->data;
5447 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005448 if (child) {
5449 child->prev = node->prev;
5450 }
5451 } else if (((struct lysc_node_choice*)parent)->cases) {
5452 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005453 }
5454}
5455
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005456struct lysc_deviation {
5457 const char *nodeid;
5458 struct lysc_node *target; /* target node of the deviation */
5459 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
5460 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
5461 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5462};
5463
Michal Vaskoccc062a2020-08-13 08:34:50 +02005464/* MACROS for deviates checking */
5465#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5466 if (!(target->nodetype & (NODETYPES))) { \
5467 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5468 goto cleanup; \
5469 }
5470
5471#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5472 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5473 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5474 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5475 goto cleanup; \
5476 }
5477
5478#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5479 if (!((TYPE)target)->MEMBER || COND) { \
5480 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5481 goto cleanup; \
5482 }
5483
5484/**
5485 * @brief Apply deviate add.
5486 *
5487 * @param[in] ctx Compile context.
5488 * @param[in] target Deviation target.
5489 * @param[in] dev_flags Internal deviation flags.
5490 * @param[in] d Deviate add to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02005491 * @return LY_ERR value.
5492 */
5493static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005494lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_add *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02005495{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005496 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005497 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5498 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005499 LY_ARRAY_COUNT_TYPE x;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005500
5501#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5502 if (((TYPE)target)->MEMBER COND) { \
5503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5504 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5505 PROPERTY, ((TYPE)target)->MEMBER); \
5506 goto cleanup; \
5507 }
5508
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005509#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005510 if (((TYPE)target)->MEMBER && (COND)) { \
5511 int dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005512 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005513 ctx->mod_def, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5515 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5516 if (dynamic_) {free((void*)val_);} \
5517 goto cleanup; \
5518 }
5519
5520#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5521 if (((TYPE)target)->MEMBER && (COND)) { \
5522 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5523 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5524 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5525 goto cleanup; \
5526 }
5527
5528 /* [units-stmt] */
5529 if (d->units) {
5530 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5531 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5532
5533 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5534 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units);
5535 }
5536
5537 /* *must-stmt */
5538 if (d->musts) {
5539 switch (target->nodetype) {
5540 case LYS_CONTAINER:
5541 case LYS_LIST:
5542 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5543 x, lys_compile_must, ret, cleanup);
5544 break;
5545 case LYS_LEAF:
5546 case LYS_LEAFLIST:
5547 case LYS_ANYDATA:
5548 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5549 x, lys_compile_must, ret, cleanup);
5550 break;
5551 case LYS_NOTIF:
5552 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5553 x, lys_compile_must, ret, cleanup);
5554 break;
5555 case LYS_RPC:
5556 case LYS_ACTION:
5557 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5558 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5559 x, lys_compile_must, ret, cleanup);
5560 break;
5561 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5562 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5563 x, lys_compile_must, ret, cleanup);
5564 break;
5565 }
5566 /* fall through */
5567 default:
5568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5569 lys_nodetype2str(target->nodetype), "add", "must");
5570 goto cleanup;
5571 }
5572 ly_set_add(&ctx->xpath, target, 0);
5573 }
5574
5575 /* *unique-stmt */
5576 if (d->uniques) {
5577 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5578 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5579 }
5580
5581 /* *default-stmt */
5582 if (d->dflts) {
5583 switch (target->nodetype) {
5584 case LYS_LEAF:
5585 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005586 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005587 if (leaf->dflt) {
5588 /* first, remove the default value taken from the type */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005589 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5590 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005591 free(leaf->dflt);
5592 leaf->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005593 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005594
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005595 /* store the default value in unres */
Michal Vasko31a82d52020-08-21 08:11:48 +02005596 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005597 target->flags |= LYS_SET_DFLT;
5598 break;
5599 case LYS_LEAFLIST:
5600 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5601 /* first, remove the default value taken from the type */
5602 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005603 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5604 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5605 free(llist->dflts[x]);
5606 }
5607 LY_ARRAY_FREE(llist->dflts);
5608 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005609 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005610
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005611 /* store the default values in unres */
5612 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005613 target->flags |= LYS_SET_DFLT;
5614 break;
5615 case LYS_CHOICE:
5616 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5617 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5618 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5619 * to allow making the default case even the augmented case from the deviating module */
5620 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5621 goto cleanup;
5622 }
5623 break;
5624 default:
5625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5626 lys_nodetype2str(target->nodetype), "add", "default");
5627 goto cleanup;
5628 }
5629 }
5630
5631 /* [config-stmt] */
5632 if (d->flags & LYS_CONFIG_MASK) {
5633 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5635 lys_nodetype2str(target->nodetype), "add", "config");
5636 goto cleanup;
5637 }
5638 if (dev_flags) {
5639 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5640 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5641 }
5642 if (target->flags & LYS_SET_CONFIG) {
5643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5644 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5645 target->flags & LYS_CONFIG_W ? "true" : "false");
5646 goto cleanup;
5647 }
5648 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5649 }
5650
5651 /* [mandatory-stmt] */
5652 if (d->flags & LYS_MAND_MASK) {
5653 if (target->flags & LYS_MAND_MASK) {
5654 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5655 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5656 target->flags & LYS_MAND_TRUE ? "true" : "false");
5657 goto cleanup;
5658 }
5659 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5660 }
5661
5662 /* [min-elements-stmt] */
5663 if (d->flags & LYS_SET_MIN) {
5664 if (target->nodetype == LYS_LEAFLIST) {
5665 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5666 /* change value */
5667 ((struct lysc_node_leaflist*)target)->min = d->min;
5668 } else if (target->nodetype == LYS_LIST) {
5669 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5670 /* change value */
5671 ((struct lysc_node_list*)target)->min = d->min;
5672 } else {
5673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5674 lys_nodetype2str(target->nodetype), "add", "min-elements");
5675 goto cleanup;
5676 }
5677 if (d->min) {
5678 target->flags |= LYS_MAND_TRUE;
5679 }
5680 }
5681
5682 /* [max-elements-stmt] */
5683 if (d->flags & LYS_SET_MAX) {
5684 if (target->nodetype == LYS_LEAFLIST) {
5685 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5686 /* change value */
5687 ((struct lysc_node_leaflist*)target)->max = d->max ? d->max : (uint32_t)-1;
5688 } else if (target->nodetype == LYS_LIST) {
5689 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5690 /* change value */
5691 ((struct lysc_node_list*)target)->max = d->max ? d->max : (uint32_t)-1;
5692 } else {
5693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5694 lys_nodetype2str(target->nodetype), "add", "max-elements");
5695 goto cleanup;
5696 }
5697 }
5698
5699 ret = LY_SUCCESS;
5700
5701cleanup:
5702 return ret;
5703}
5704
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005705static LY_ERR
5706lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt)
5707{
5708 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5709 int dyn = 0;
5710 const char *orig_dflt;
5711 uint32_t i;
5712
5713 if (target->module != ctx->mod) {
5714 /* foreign deviation */
5715 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt);
5716
5717 /* check that the value matches */
5718 orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5719 if (strcmp(orig_dflt, dflt)) {
5720 goto error;
5721 }
5722 if (dyn) {
5723 free((char *)orig_dflt);
5724 }
5725
5726 /* remove the default specification */
5727 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5728 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5729 free(leaf->dflt);
5730 leaf->dflt = NULL;
5731 } else {
5732 /* local deviation */
5733 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt);
5734
5735 /* find the incomplete default */
5736 orig_dflt = NULL;
5737 for (i = 0; i < ctx->dflts.count; ++i) {
5738 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
5739 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5740 break;
5741 }
5742 }
5743 LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT);
5744
5745 /* check that the value matches */
5746 if (strcmp(orig_dflt, dflt)) {
5747 goto error;
5748 }
5749
5750 /* update the list of incomplete default values */
5751 lysc_incomplete_dflt_remove(ctx, target);
5752 }
5753
5754 target->flags &= ~LYS_SET_DFLT;
5755 return LY_SUCCESS;
5756
5757error:
5758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5759 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5760 dflt, orig_dflt);
5761 if (dyn) {
5762 free((char *)orig_dflt);
5763 }
5764cleanup:
5765 return LY_EVALID;
5766}
5767
5768static LY_ERR
5769lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts)
5770{
5771 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
5772 int dyn = 0, found;
5773 const char *orig_dflt, **orig_dflts;
5774 uint32_t i;
5775 LY_ARRAY_COUNT_TYPE x, y;
5776
5777 if (target->module != ctx->mod) {
5778 /* foreign deviation */
5779 DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]);
5780 LY_ARRAY_FOR(dflts, x) {
5781 found = 0;
5782 LY_ARRAY_FOR(llist->dflts, y) {
5783 orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5784 if (!strcmp(orig_dflt, dflts[x])) {
5785 if (dyn) {
5786 free((char *)orig_dflt);
5787 }
5788 found = 1;
5789 break;
5790 }
5791 if (dyn) {
5792 free((char *)orig_dflt);
5793 }
5794 }
5795 if (!found) {
5796 goto error;
5797 }
5798
5799 /* update compiled default values */
5800 LY_ARRAY_DECREMENT(llist->dflts);
5801 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
5802 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
5803 free(llist->dflts[y]);
5804 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
5805 }
5806 if (!LY_ARRAY_COUNT(llist->dflts)) {
5807 LY_ARRAY_FREE(llist->dflts);
5808 llist->dflts = NULL;
5809 llist->flags &= ~LYS_SET_DFLT;
5810 }
5811 } else {
5812 /* local deviation */
5813 orig_dflt = NULL;
5814 orig_dflts = NULL;
5815 for (i = 0; i < ctx->dflts.count; ++i) {
5816 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) {
5817 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5818 orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts;
5819 break;
5820 }
5821 }
5822 LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT);
5823
5824 if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) {
5825 /* TODO it is not currently possible to remove just one default value from incomplete defaults array */
5826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5827 "Local deviation deleting leaf-list defaults is not supported.");
5828 return LY_EVALID;
5829 }
5830
5831 LY_ARRAY_FOR(dflts, x) {
5832 found = 0;
5833 if (orig_dflts) {
5834 LY_ARRAY_FOR(orig_dflts, y) {
5835 if (!strcmp(orig_dflts[y], dflts[x])) {
5836 found = 1;
5837 break;
5838 }
5839 }
5840 } else if (!strcmp(orig_dflt, dflts[x])) {
5841 found = 1;
5842 }
5843 if (!found) {
5844 goto error;
5845 }
5846
5847 /* update the list of incomplete default values */
5848 lysc_incomplete_dflt_remove(ctx, target);
5849 }
5850
5851 llist->flags &= ~LYS_SET_DFLT;
5852 }
5853
5854 return LY_SUCCESS;
5855
5856error:
5857 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
5858 "which does not match any of the target's property values.", dflts[x]);
5859cleanup:
5860 return LY_EVALID;
5861}
5862
Michal Vaskoccc062a2020-08-13 08:34:50 +02005863/**
5864 * @brief Apply deviate delete.
5865 *
5866 * @param[in] ctx Compile context.
5867 * @param[in] target Deviation target.
5868 * @param[in] dev_flags Internal deviation flags.
5869 * @param[in] d Deviate delete to apply.
5870 * @return LY_ERR value.
5871 */
5872static LY_ERR
5873lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_del *d)
5874{
5875 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005876 struct lysc_node_list *list = (struct lysc_node_list *)target;
5877 LY_ARRAY_COUNT_TYPE x, y, z;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005878 size_t prefix_len, name_len;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005879 const char *prefix, *name, *nodeid;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005880 struct lys_module *mod;
5881
5882#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5883 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5884 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5885 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5886 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5887 } \
5888 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5889 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5890 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5891 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5892 goto cleanup; \
5893 } \
5894 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5895 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5896 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5897 &((TYPE)target)->ARRAY_TRG[y + 1], \
5898 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5899 } \
5900 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5901 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5902 ((TYPE)target)->ARRAY_TRG = NULL; \
5903 }
5904
5905 /* [units-stmt] */
5906 if (d->units) {
5907 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5908 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d->units);
5909 if (strcmp(((struct lysc_node_leaf*)target)->units, d->units)) {
5910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5911 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5912 d->units, ((struct lysc_node_leaf*)target)->units);
5913 goto cleanup;
5914 }
5915 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)target)->units);
5916 ((struct lysc_node_leaf*)target)->units = NULL;
5917 }
5918
5919 /* *must-stmt */
5920 if (d->musts) {
5921 switch (target->nodetype) {
5922 case LYS_CONTAINER:
5923 case LYS_LIST:
5924 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5925 break;
5926 case LYS_LEAF:
5927 case LYS_LEAFLIST:
5928 case LYS_ANYDATA:
5929 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5930 break;
5931 case LYS_NOTIF:
5932 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5933 break;
5934 case LYS_RPC:
5935 case LYS_ACTION:
5936 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5937 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5938 break;
5939 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5940 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5941 break;
5942 }
5943 /* fall through */
5944 default:
5945 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5946 lys_nodetype2str(target->nodetype), "delete", "must");
5947 goto cleanup;
5948 }
5949 }
5950
5951 /* *unique-stmt */
5952 if (d->uniques) {
5953 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5954 LY_ARRAY_FOR(d->uniques, x) {
5955 LY_ARRAY_FOR(list->uniques, z) {
5956 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
5957 nodeid = strpbrk(name, " \t\n");
5958 if (nodeid) {
5959 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
5960 break;
5961 }
5962 while (isspace(*nodeid)) {
5963 ++nodeid;
5964 }
5965 } else {
5966 if (strcmp(name, list->uniques[z][y]->name)) {
5967 break;
5968 }
5969 }
5970 }
5971 if (!name) {
5972 /* complete match - remove the unique */
5973 LY_ARRAY_DECREMENT(list->uniques);
5974 LY_ARRAY_FREE(list->uniques[z]);
5975 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
5976 --z;
5977 break;
5978 }
5979 }
5980 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
5981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5982 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5983 d->uniques[x]);
5984 goto cleanup;
5985 }
5986 }
5987 if (!LY_ARRAY_COUNT(list->uniques)) {
5988 LY_ARRAY_FREE(list->uniques);
5989 list->uniques = NULL;
5990 }
5991 }
5992
5993 /* *default-stmt */
5994 if (d->dflts) {
5995 switch (target->nodetype) {
5996 case LYS_LEAF:
5997 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005998 LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005999 break;
6000 case LYS_LEAFLIST:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006001 LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006002 break;
6003 case LYS_CHOICE:
6004 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
6005 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d->dflts[0]);
6006 nodeid = d->dflts[0];
6007 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6008 if (prefix) {
6009 /* use module prefixes from the deviation module to match the module of the default case */
6010 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6011 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
6012 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6013 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6014 goto cleanup;
6015 }
6016 if (mod != ((struct lysc_node_choice*)target)->dflt->module) {
6017 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
6018 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6019 "The prefix does not match the default case's module.", d->dflts[0]);
6020 goto cleanup;
6021 }
6022 }
6023 /* else {
6024 * strictly, the default prefix would point to the deviation module, but the value should actually
6025 * match the default string in the original module (usually unprefixed), so in this case we do not check
6026 * the module of the default case, just matching its name */
6027 if (strcmp(name, ((struct lysc_node_choice*)target)->dflt->name)) {
6028 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6029 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6030 d->dflts[0], ((struct lysc_node_choice*)target)->dflt->name);
6031 goto cleanup;
6032 }
6033 ((struct lysc_node_choice*)target)->dflt->flags &= ~LYS_SET_DFLT;
6034 ((struct lysc_node_choice*)target)->dflt = NULL;
6035 break;
6036 default:
6037 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6038 lys_nodetype2str(target->nodetype), "delete", "default");
6039 goto cleanup;
6040 }
6041 }
6042
6043 ret = LY_SUCCESS;
6044
6045cleanup:
6046 return ret;
6047}
6048
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006049static LY_ERR
6050lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target)
6051{
6052 LY_ERR ret;
6053 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6054 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6055 struct ly_err_item *err = NULL;
6056 LY_ARRAY_COUNT_TYPE x;
6057 const char *dflt;
6058 int dyn;
6059
6060 if (target->module != ctx->mod) {
6061 /* foreign deviation */
6062 if (target->nodetype == LYS_LEAF) {
6063 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn);
6064 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6065 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6066
6067 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6068 LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err);
6069 if (dyn) {
6070 free((char *)dflt);
6071 }
6072 if (err) {
6073 ly_err_print(err);
6074 ctx->path[0] = '\0';
6075 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6077 "Invalid default - value does not fit the type (%s).", err->msg);
6078 ly_err_free(err);
6079 }
6080 LY_CHECK_RET(ret);
6081
6082 ++leaf->dflt->realtype->refcount;
6083 } else { /* LY_LEAFLIST */
6084 LY_ARRAY_FOR(llist->dflts, x) {
6085 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn);
6086 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6087 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6088
6089 ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6090 LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err);
6091 if (dyn) {
6092 free((char *)dflt);
6093 }
6094 if (err) {
6095 ly_err_print(err);
6096 ctx->path[0] = '\0';
6097 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6099 "Invalid default - value does not fit the type (%s).", err->msg);
6100 ly_err_free(err);
6101 }
6102 LY_CHECK_RET(ret);
6103
6104 ++llist->dflts[x]->realtype->refcount;
6105 }
6106 }
6107 } else {
6108 /* local deviation */
6109
6110 /* these default were not compiled yet, so they will use the new type automatically */
6111 }
6112
6113 return LY_SUCCESS;
6114}
6115
Michal Vaskoccc062a2020-08-13 08:34:50 +02006116/**
6117 * @brief Apply deviate replace.
6118 *
6119 * @param[in] ctx Compile context.
6120 * @param[in] target Deviation target.
6121 * @param[in] d Deviate replace to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02006122 * @return LY_ERR value.
6123 */
6124static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006125lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02006126{
6127 LY_ERR ret = LY_EVALID;
6128 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6129 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6130 LY_ARRAY_COUNT_TYPE x;
6131
6132#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6133 if (!(((TYPE)target)->MEMBER COND)) { \
6134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6135 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6136 goto cleanup; \
6137 }
6138
6139 /* [type-stmt] */
6140 if (d->type) {
6141 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6142 /* type is mandatory, so checking for its presence is not necessary */
6143 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6144
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006145 /* remove only default value inherited from the type */
6146 if (!(target->flags & LYS_SET_DFLT)) {
6147 if (target->module != ctx->mod) {
6148 /* foreign deviation - the target has default from the previous type, remove it */
6149 if (target->nodetype == LYS_LEAF) {
6150 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6151 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6152 free(leaf->dflt);
6153 leaf->dflt = NULL;
6154 } else { /* LYS_LEAFLIST */
6155 LY_ARRAY_FOR(llist->dflts, x) {
6156 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6157 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6158 free(llist->dflts[x]);
6159 }
6160 LY_ARRAY_FREE(llist->dflts);
6161 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006162 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006163 } else {
6164 /* local deviation */
6165 lysc_incomplete_dflt_remove(ctx, target);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006166 }
6167 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006168
6169 LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf));
6170
6171 if (target->flags & LYS_SET_DFLT) {
6172 /* the term default value(s) needs to be recompiled */
6173 LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006174 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006175 }
6176
6177 /* [units-stmt] */
6178 if (d->units) {
6179 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6180 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6181 units, "replacing", "units", d->units);
6182
6183 lydict_remove(ctx->ctx, leaf->units);
6184 DUP_STRING(ctx->ctx, d->units, leaf->units);
6185 }
6186
6187 /* [default-stmt] */
6188 if (d->dflt) {
6189 switch (target->nodetype) {
6190 case LYS_LEAF:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006191 if (target->module != ctx->mod) {
6192 /* foreign deviation */
6193 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing",
6194 "default", d->dflt);
6195
6196 /* remove the default specification */
6197 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6198 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6199 free(leaf->dflt);
6200 leaf->dflt = NULL;
6201 } else {
6202 /* local deviation */
6203 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing",
6204 "default", d->dflt);
6205 assert(!leaf->dflt);
6206 }
6207
6208 /* store the new default value */
6209 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006210 break;
6211 case LYS_CHOICE:
6212 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6213 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6214 goto cleanup;
6215 }
6216 break;
6217 default:
6218 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6219 lys_nodetype2str(target->nodetype), "replace", "default");
6220 goto cleanup;
6221 }
6222 }
6223
6224 /* [config-stmt] */
6225 if (d->flags & LYS_CONFIG_MASK) {
6226 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6227 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6228 lys_nodetype2str(target->nodetype), "replace", "config");
6229 goto cleanup;
6230 }
6231 if (!(target->flags & LYS_SET_CONFIG)) {
6232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6233 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6234 goto cleanup;
6235 }
6236 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6237 }
6238
6239 /* [mandatory-stmt] */
6240 if (d->flags & LYS_MAND_MASK) {
6241 if (!(target->flags & LYS_MAND_MASK)) {
6242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6243 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6244 goto cleanup;
6245 }
6246 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6247 }
6248
6249 /* [min-elements-stmt] */
6250 if (d->flags & LYS_SET_MIN) {
6251 if (target->nodetype == LYS_LEAFLIST) {
6252 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6253 /* change value */
6254 ((struct lysc_node_leaflist *)target)->min = d->min;
6255 } else if (target->nodetype == LYS_LIST) {
6256 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6257 /* change value */
6258 ((struct lysc_node_list *)target)->min = d->min;
6259 } else {
6260 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6261 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6262 goto cleanup;
6263 }
6264 if (d->min) {
6265 target->flags |= LYS_MAND_TRUE;
6266 }
6267 }
6268
6269 /* [max-elements-stmt] */
6270 if (d->flags & LYS_SET_MAX) {
6271 if (target->nodetype == LYS_LEAFLIST) {
6272 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6273 /* change value */
6274 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6275 } else if (target->nodetype == LYS_LIST) {
6276 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6277 /* change value */
6278 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6279 } else {
6280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6281 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6282 goto cleanup;
6283 }
6284 }
6285
6286 ret = LY_SUCCESS;
6287
6288cleanup:
6289 return ret;
6290}
6291
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006292/**
6293 * @brief Apply all deviations of one target node.
6294 *
6295 * @param[in] ctx Compile context.
6296 * @param[in] dev Deviation structure to apply.
6297 * @return LY_ERR value.
6298 */
6299static LY_ERR
6300lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006301{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006302 LY_ERR ret = LY_EVALID;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006303 struct lysc_node *target = dev->target;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006304 struct lysc_action *rpcs;
6305 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006306 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006307 LY_ARRAY_COUNT_TYPE v, x;
Radek Krejci551b12c2019-02-19 16:11:21 +01006308 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006309
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006310 lysc_update_path(ctx, NULL, dev->nodeid);
6311
6312 /* not-supported */
6313 if (dev->not_supported) {
6314 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
6315 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
6316 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6317 }
6318
6319#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6320 if (target->parent) { \
6321 ARRAY = (TYPE*)GETFUNC(target->parent); \
6322 } else { \
6323 ARRAY = target->module->compiled->ARRAY; \
6324 } \
6325 LY_ARRAY_FOR(ARRAY, x) { \
6326 if (&ARRAY[x] == (TYPE*)target) { break; } \
6327 } \
6328 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6329 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6330 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6331 LY_ARRAY_DECREMENT(ARRAY); \
6332 }
6333
6334 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6335 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6336 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006337 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6338 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6339 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6340 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006341 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6342 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006343 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6344 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6345 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6346 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006347 } else {
6348 /* remove RPC/action */
6349 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6350 }
6351 } else if (target->nodetype == LYS_NOTIF) {
6352 /* remove Notification */
6353 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6354 } else {
6355 /* remove the target node */
6356 lysc_disconnect(target);
6357 lysc_node_free(ctx->ctx, target);
6358 }
6359
6360 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6361 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6362 return LY_SUCCESS;
6363 }
6364
6365 /* list of deviates (not-supported is not present in the list) */
6366 LY_ARRAY_FOR(dev->deviates, v) {
6367 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006368 switch (d->mod) {
6369 case LYS_DEV_ADD:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006370 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006371 break;
6372 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006373 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006374 break;
6375 case LYS_DEV_REPLACE:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006376 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006377 break;
6378 default:
6379 LOGINT(ctx->ctx);
6380 goto cleanup;
6381 }
6382 }
6383
6384 /* final check when all deviations of a single target node are applied */
6385
6386 /* check min-max compatibility */
6387 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006388 min = ((struct lysc_node_leaflist *)target)->min;
6389 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006390 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006391 min = ((struct lysc_node_list *)target)->min;
6392 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006393 } else {
6394 min = max = 0;
6395 }
6396 if (min > max) {
6397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6398 "after deviation: min value %u is bigger than max value %u.", min, max);
6399 goto cleanup;
6400 }
6401
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006402 /* check mandatory - default compatibility */
6403 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6404 && (target->flags & LYS_MAND_TRUE)) {
6405 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6406 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6407 goto cleanup;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006408 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice* )target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006409 && (target->flags & LYS_MAND_TRUE)) {
6410 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6411 goto cleanup;
6412 }
6413 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6414 /* mandatory node under a default case */
6415 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6416 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6417 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6418 goto cleanup;
6419 }
6420
6421 /* success */
6422 ret = LY_SUCCESS;
6423
6424cleanup:
6425 lysc_update_path(ctx, NULL, NULL);
6426 return ret;
6427}
6428
6429LY_ERR
6430lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6431{
6432 LY_ERR ret = LY_EVALID;
6433 struct ly_set devs_p = {0};
6434 struct ly_set targets = {0};
6435 struct lysc_node *target; /* target target of the deviation */
6436 struct lysp_deviation *dev;
6437 struct lysp_deviate *d, **dp_new;
6438 LY_ARRAY_COUNT_TYPE u, v;
6439 struct lysc_deviation **devs = NULL;
6440 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006441 uint16_t flags;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006442 int i;
Radek Krejciccd20f12019-02-15 14:12:27 +01006443
6444 /* get all deviations from the module and all its submodules ... */
6445 LY_ARRAY_FOR(mod_p->deviations, u) {
6446 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
6447 }
6448 LY_ARRAY_FOR(mod_p->includes, v) {
6449 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
6450 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
6451 }
6452 }
6453 if (!devs_p.count) {
6454 /* nothing to do */
6455 return LY_SUCCESS;
6456 }
6457
Radek Krejci327de162019-06-14 12:52:07 +02006458 lysc_update_path(ctx, NULL, "{deviation}");
6459
Radek Krejciccd20f12019-02-15 14:12:27 +01006460 /* ... and group them by the target node */
6461 devs = calloc(devs_p.count, sizeof *devs);
6462 for (u = 0; u < devs_p.count; ++u) {
6463 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006464 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006465
6466 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006467 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
6468 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006469 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006470 /* move the target pointer to input/output to make them different from the action and
6471 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6472 * back to the RPC/action node due to a better compatibility and decision code in this function.
6473 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6474 if (flags & LYSC_OPT_RPC_INPUT) {
6475 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
6476 flags |= LYSC_OPT_INTERNAL;
6477 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
6478 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
6479 flags |= LYSC_OPT_INTERNAL;
6480 }
6481 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006482 /* insert into the set of targets with duplicity detection */
6483 i = ly_set_add(&targets, target, 0);
6484 if (!devs[i]) {
6485 /* new record */
6486 devs[i] = calloc(1, sizeof **devs);
6487 devs[i]->target = target;
6488 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006489 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006490 }
6491 /* add deviates into the deviation's list of deviates */
6492 for (d = dev->deviates; d; d = d->next) {
6493 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
6494 *dp_new = d;
6495 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
6496 devs[i]->not_supported = 1;
6497 }
6498 }
Radek Krejci327de162019-06-14 12:52:07 +02006499
6500 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006501 }
6502
Radek Krejciccd20f12019-02-15 14:12:27 +01006503 /* apply deviations */
6504 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006505 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6506 /* fix the target pointer in case of RPC's/action's input/output */
6507 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006508 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006509 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006510 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006511 }
6512 }
6513
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006514 /* remember target module (the target node may be removed) */
6515 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006516
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006517 /* apply the deviation */
6518 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006519
Michal Vaskoe6143202020-07-03 13:02:08 +02006520 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006521 i = 0;
6522 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6523 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
6524 i = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006525 break;
6526 }
6527 }
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006528 if (!i) {
6529 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006530 *dev_mod = mod_p->mod;
6531 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006532 }
6533
Radek Krejci327de162019-06-14 12:52:07 +02006534 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006535 ret = LY_SUCCESS;
6536
6537cleanup:
6538 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6539 LY_ARRAY_FREE(devs[u]->deviates);
6540 free(devs[u]);
6541 }
6542 free(devs);
6543 ly_set_erase(&targets, NULL);
6544 ly_set_erase(&devs_p, NULL);
6545
6546 return ret;
6547}
6548
Radek Krejcib56c7502019-02-13 14:19:54 +01006549/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006550 * @brief Compile the given YANG submodule into the main module.
6551 * @param[in] ctx Compile context
6552 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006553 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6554 */
6555LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006556lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006557{
6558 unsigned int u;
6559 LY_ERR ret = LY_SUCCESS;
6560 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006561 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006562 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006563 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006564
Michal Vasko33ff9422020-07-03 09:50:39 +02006565 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006566 /* features are compiled directly into the compiled module structure,
6567 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6568 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006569 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6570 LY_CHECK_GOTO(ret, error);
6571 }
Radek Krejci0af46292019-01-11 16:02:31 +01006572
Michal Vasko33ff9422020-07-03 09:50:39 +02006573 if (!mainmod->mod->dis_identities) {
6574 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6575 LY_CHECK_GOTO(ret, error);
6576 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006577
Radek Krejci474f9b82019-07-24 11:36:37 +02006578 /* data nodes */
6579 LY_LIST_FOR(submod->data, node_p) {
6580 ret = lys_compile_node(ctx, node_p, NULL, 0);
6581 LY_CHECK_GOTO(ret, error);
6582 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006583
Radek Krejciec4da802019-05-02 13:02:41 +02006584 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6585 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006586
Radek Krejcid05cbd92018-12-05 14:26:40 +01006587error:
6588 return ret;
6589}
6590
Radek Krejci335332a2019-09-05 13:03:35 +02006591static void *
6592lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6593{
6594 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6595 if (substmts[u].stmt == stmt) {
6596 return substmts[u].storage;
6597 }
6598 }
6599 return NULL;
6600}
6601
6602LY_ERR
6603lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6604{
6605 LY_ERR ret = LY_EVALID, r;
6606 unsigned int u;
6607 struct lysp_stmt *stmt;
6608 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006609
6610 /* check for invalid substatements */
6611 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006612 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6613 continue;
6614 }
Radek Krejci335332a2019-09-05 13:03:35 +02006615 for (u = 0; substmts[u].stmt; ++u) {
6616 if (substmts[u].stmt == stmt->kw) {
6617 break;
6618 }
6619 }
6620 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6622 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006623 goto cleanup;
6624 }
Radek Krejci335332a2019-09-05 13:03:35 +02006625 }
6626
Radek Krejciad5963b2019-09-06 16:03:05 +02006627 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6628
Radek Krejci335332a2019-09-05 13:03:35 +02006629 /* keep order of the processing the same as the order in the defined substmts,
6630 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6631 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006632 int stmt_present = 0;
6633
Radek Krejci335332a2019-09-05 13:03:35 +02006634 for (stmt = ext->child; stmt; stmt = stmt->next) {
6635 if (substmts[u].stmt != stmt->kw) {
6636 continue;
6637 }
6638
Radek Krejciad5963b2019-09-06 16:03:05 +02006639 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006640 if (substmts[u].storage) {
6641 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006642 case LY_STMT_STATUS:
6643 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6644 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6645 break;
6646 case LY_STMT_UNITS: {
6647 const char **units;
6648
6649 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6650 /* single item */
6651 if (*((const char **)substmts[u].storage)) {
6652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6653 goto cleanup;
6654 }
6655 units = (const char **)substmts[u].storage;
6656 } else {
6657 /* sized array */
6658 const char ***units_array = (const char ***)substmts[u].storage;
6659 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6660 }
6661 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6662 break;
6663 }
Radek Krejci335332a2019-09-05 13:03:35 +02006664 case LY_STMT_TYPE: {
6665 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6666 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6667
6668 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6669 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006670 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6672 goto cleanup;
6673 }
6674 compiled = substmts[u].storage;
6675 } else {
6676 /* sized array */
6677 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6678 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6679 compiled = (void*)type;
6680 }
6681
Radek Krejciad5963b2019-09-06 16:03:05 +02006682 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006683 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6684 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006685 units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02006686 lysp_type_free(ctx->ctx, parsed);
6687 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006688 break;
6689 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006690 case LY_STMT_IF_FEATURE: {
6691 struct lysc_iffeature *iff = NULL;
6692
6693 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6694 /* single item */
6695 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6697 goto cleanup;
6698 }
6699 iff = (struct lysc_iffeature*)substmts[u].storage;
6700 } else {
6701 /* sized array */
6702 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6703 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6704 }
6705 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6706 break;
6707 }
6708 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6709 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006710 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006711 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.",
6712 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006713 goto cleanup;
6714 }
6715 }
Radek Krejci335332a2019-09-05 13:03:35 +02006716 }
Radek Krejci335332a2019-09-05 13:03:35 +02006717
Radek Krejciad5963b2019-09-06 16:03:05 +02006718 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6719 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6720 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6721 goto cleanup;
6722 }
Radek Krejci335332a2019-09-05 13:03:35 +02006723 }
6724
6725 ret = LY_SUCCESS;
6726
6727cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006728 return ret;
6729}
6730
Michal Vasko175012e2019-11-06 15:49:14 +01006731/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006732 * @brief Check when for cyclic dependencies.
6733 * @param[in] set Set with all the referenced nodes.
6734 * @param[in] node Node whose "when" referenced nodes are in @p set.
6735 * @return LY_ERR value
6736 */
6737static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006738lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006739{
6740 struct lyxp_set tmp_set;
6741 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006742 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006743 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006744 int idx;
6745 struct lysc_when *when;
6746 LY_ERR ret = LY_SUCCESS;
6747
6748 memset(&tmp_set, 0, sizeof tmp_set);
6749
6750 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006751 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006752 xp_scnode = &set->val.scnodes[i];
6753
Michal Vasko5c4e5892019-11-14 12:31:38 +01006754 if (xp_scnode->in_ctx != -1) {
6755 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006756 xp_scnode->in_ctx = 1;
6757 }
6758 }
6759
6760 for (i = 0; i < set->used; ++i) {
6761 xp_scnode = &set->val.scnodes[i];
6762 if (xp_scnode->in_ctx != 1) {
6763 /* already checked */
6764 continue;
6765 }
6766
Michal Vasko1bf09392020-03-27 12:38:10 +01006767 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006768 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006769 /* no when to check */
6770 xp_scnode->in_ctx = 0;
6771 continue;
6772 }
6773
6774 node = xp_scnode->scnode;
6775 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006776 LY_ARRAY_FOR(node->when, u) {
6777 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006778 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006779 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6780 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006781 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006782 goto cleanup;
6783 }
6784
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006785 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006786 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006787 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006788 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006789 idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006790 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006791 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 +01006792 ret = LY_EVALID;
6793 goto cleanup;
6794 }
6795
6796 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006797 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006798 } else {
6799 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006800 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006801 }
6802 }
6803
6804 /* merge this set into the global when set */
6805 lyxp_set_scnode_merge(set, &tmp_set);
6806 }
6807
6808 /* check when of non-data parents as well */
6809 node = node->parent;
6810 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6811
Michal Vasko251f56e2019-11-14 16:06:47 +01006812 /* this node when was checked (xp_scnode could have been reallocd) */
6813 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006814 }
6815
6816cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006817 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006818 return ret;
6819}
6820
6821/**
Michal Vasko175012e2019-11-06 15:49:14 +01006822 * @brief Check when/must expressions of a node on a compiled schema tree.
6823 * @param[in] ctx Compile context.
6824 * @param[in] node Node to check.
6825 * @return LY_ERR value
6826 */
6827static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006828lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006829{
Michal Vasko175012e2019-11-06 15:49:14 +01006830 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006831 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006832 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006833 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006834 struct lysc_when **when = NULL;
6835 struct lysc_must *musts = NULL;
6836 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006837 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006838
6839 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006840 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006841 if (node->flags & LYS_CONFIG_R) {
6842 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6843 if (op) {
6844 /* we are actually in output */
6845 opts = LYXP_SCNODE_OUTPUT;
6846 }
6847 }
Michal Vasko175012e2019-11-06 15:49:14 +01006848
6849 switch (node->nodetype) {
6850 case LYS_CONTAINER:
6851 when = ((struct lysc_node_container *)node)->when;
6852 musts = ((struct lysc_node_container *)node)->musts;
6853 break;
6854 case LYS_CHOICE:
6855 when = ((struct lysc_node_choice *)node)->when;
6856 break;
6857 case LYS_LEAF:
6858 when = ((struct lysc_node_leaf *)node)->when;
6859 musts = ((struct lysc_node_leaf *)node)->musts;
6860 break;
6861 case LYS_LEAFLIST:
6862 when = ((struct lysc_node_leaflist *)node)->when;
6863 musts = ((struct lysc_node_leaflist *)node)->musts;
6864 break;
6865 case LYS_LIST:
6866 when = ((struct lysc_node_list *)node)->when;
6867 musts = ((struct lysc_node_list *)node)->musts;
6868 break;
6869 case LYS_ANYXML:
6870 case LYS_ANYDATA:
6871 when = ((struct lysc_node_anydata *)node)->when;
6872 musts = ((struct lysc_node_anydata *)node)->musts;
6873 break;
6874 case LYS_CASE:
6875 when = ((struct lysc_node_case *)node)->when;
6876 break;
6877 case LYS_NOTIF:
6878 musts = ((struct lysc_notif *)node)->musts;
6879 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006880 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006881 case LYS_ACTION:
6882 /* first process input musts */
6883 musts = ((struct lysc_action *)node)->input.musts;
6884 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006885 default:
6886 /* nothing to check */
6887 break;
6888 }
6889
Michal Vasko175012e2019-11-06 15:49:14 +01006890 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006891 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006892 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 +02006893 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006894 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006895 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006896 goto cleanup;
6897 }
6898
Michal Vaskodc052f32019-11-07 11:11:38 +01006899 ctx->path[0] = '\0';
6900 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006901 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006902 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006903 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6904 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006905
Michal Vaskoecd62de2019-11-13 12:35:11 +01006906 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006907 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006908 schema->name);
6909 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006910
6911 /* check dummy node accessing */
6912 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006913 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006914 ret = LY_EVALID;
6915 goto cleanup;
6916 }
Michal Vasko175012e2019-11-06 15:49:14 +01006917 }
6918 }
6919
Michal Vaskoecd62de2019-11-13 12:35:11 +01006920 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006921 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006922 LY_CHECK_GOTO(ret, cleanup);
6923
Michal Vaskod3678892020-05-21 10:06:58 +02006924 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006925 }
6926
Michal Vasko5d8756a2019-11-07 15:21:00 +01006927check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006928 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006929 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006930 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 +01006931 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006932 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006933 goto cleanup;
6934 }
6935
Michal Vaskodc052f32019-11-07 11:11:38 +01006936 ctx->path[0] = '\0';
6937 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006938 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006939 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006940 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006941 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006942 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6943 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006944 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006945 }
6946 }
6947
Michal Vaskod3678892020-05-21 10:06:58 +02006948 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006949 }
6950
Michal Vasko1bf09392020-03-27 12:38:10 +01006951 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006952 /* now check output musts */
6953 input_done = 1;
6954 musts = ((struct lysc_action *)node)->output.musts;
6955 opts = LYXP_SCNODE_OUTPUT;
6956 goto check_musts;
6957 }
6958
Michal Vasko175012e2019-11-06 15:49:14 +01006959cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006960 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006961 return ret;
6962}
6963
Michal Vasko8d544252020-03-02 10:19:52 +01006964static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006965lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6966{
Michal Vasko6b26e742020-07-17 15:02:10 +02006967 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02006968 struct ly_path *p;
6969 struct lysc_type *type;
6970
6971 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6972
6973 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006974 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6975 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006976 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006977
6978 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006979 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02006980 ly_path_free(node->module->ctx, p);
6981
6982 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6983 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6984 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6985 lref->path->expr, lys_nodetype2str(target->nodetype));
6986 return LY_EVALID;
6987 }
6988
6989 /* check status */
6990 ctx->path[0] = '\0';
6991 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6992 ctx->path_len = strlen(ctx->path);
6993 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6994 return LY_EVALID;
6995 }
6996 ctx->path_len = 1;
6997 ctx->path[1] = '\0';
6998
6999 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007000 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007001 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007002 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007003 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7004 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7005 return LY_EVALID;
7006 }
7007 }
7008
7009 /* store the target's type and check for circular chain of leafrefs */
7010 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7011 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7012 if (type == (struct lysc_type *)lref) {
7013 /* circular chain detected */
7014 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7015 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7016 return LY_EVALID;
7017 }
7018 }
7019
7020 /* check if leafref and its target are under common if-features */
7021 if (lys_compile_leafref_features_validate(node, target)) {
7022 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7023 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7024 " features applicable to the leafref itself.", lref->path->expr);
7025 return LY_EVALID;
7026 }
7027
7028 return LY_SUCCESS;
7029}
7030
7031static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007032lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7033{
7034 struct lysc_ext_instance *ext;
7035 struct lysp_ext_instance *ext_p = NULL;
7036 struct lysp_stmt *stmt;
7037 const struct lys_module *ext_mod;
7038 LY_ERR ret = LY_SUCCESS;
7039
7040 /* create the parsed extension instance manually */
7041 ext_p = calloc(1, sizeof *ext_p);
7042 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
7043 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
7044 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
7045 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7046 ext_p->insubstmt_index = 0;
7047
7048 stmt = calloc(1, sizeof *ext_p->child);
7049 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
7050 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
7051 stmt->kw = LY_STMT_TYPE;
7052 ext_p->child = stmt;
7053
7054 /* allocate new extension instance */
7055 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7056
7057 /* manually get extension definition module */
7058 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7059
7060 /* compile the extension instance */
7061 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7062
7063cleanup:
7064 lysp_ext_instance_free(ctx->ctx, ext_p);
7065 free(ext_p);
7066 return ret;
7067}
7068
Michal Vasko004d3152020-06-11 19:59:22 +02007069static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007070lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
7071 const struct lys_module *dflt_mod, struct lyd_value *storage)
7072{
7073 LY_ERR ret;
7074 struct ly_err_item *err = NULL;
7075
7076 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7077 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7078 if (err) {
7079 ly_err_print(err);
7080 ctx->path[0] = '\0';
7081 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7082 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7083 "Invalid default - value does not fit the type (%s).", err->msg);
7084 ly_err_free(err);
7085 }
7086 if (!ret) {
7087 ++storage->realtype->refcount;
7088 return LY_SUCCESS;
7089 }
7090 return ret;
7091}
7092
7093static LY_ERR
7094lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
7095 const struct lys_module *dflt_mod)
7096{
7097 LY_ERR ret;
7098
7099 assert(!leaf->dflt);
7100
7101 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7102 /* ignore default values for keys and mandatory leaves */
7103 return LY_SUCCESS;
7104 }
7105
7106 /* allocate the default value */
7107 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7108 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7109
7110 /* store the default value */
7111 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt);
7112 if (ret) {
7113 free(leaf->dflt);
7114 leaf->dflt = NULL;
7115 }
7116
7117 return ret;
7118}
7119
7120static LY_ERR
7121lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char *dflt, const char **dflts,
7122 const struct lys_module *dflt_mod)
7123{
7124 LY_ERR ret;
7125 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7126
7127 assert(dflt || dflts);
7128
7129 if (llist->dflts) {
7130 /* there were already some defaults and we are adding new by deviations */
7131 assert(dflts);
7132 orig_count = LY_ARRAY_COUNT(llist->dflts);
7133 } else {
7134 orig_count = 0;
7135 }
7136
7137 /* allocate new items */
7138 if (dflts) {
7139 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7140 } else {
7141 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7142 }
7143
7144 /* fill each new default value */
7145 if (dflts) {
7146 LY_ARRAY_FOR(dflts, u) {
7147 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
7148 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod,
7149 llist->dflts[orig_count + u]);
7150 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7151 LY_ARRAY_INCREMENT(llist->dflts);
7152 }
7153 } else {
7154 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
7155 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]);
7156 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7157 LY_ARRAY_INCREMENT(llist->dflts);
7158 }
7159
7160 /* check default value uniqueness */
7161 if (llist->flags & LYS_CONFIG_W) {
7162 /* configuration data values must be unique - so check the default values */
7163 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7164 for (v = 0; v < u; ++v) {
7165 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
7166 int dynamic = 0;
7167 const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic);
7168
7169 lysc_update_path(ctx, llist->parent, llist->name);
7170 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7171 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
7172 lysc_update_path(ctx, NULL, NULL);
7173 if (dynamic) {
7174 free((char *)val);
7175 }
7176 return LY_EVALID;
7177 }
7178 }
7179 }
7180 }
7181
7182 return LY_SUCCESS;
7183}
7184
7185static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007186lys_compile_unres(struct lysc_ctx *ctx)
7187{
7188 struct lysc_node *node;
7189 struct lysc_type *type, *typeiter;
7190 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007191 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007192 uint32_t i;
7193
7194 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7195 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7196 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7197 for (i = 0; i < ctx->leafrefs.count; ++i) {
7198 node = ctx->leafrefs.objs[i];
7199 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7200 type = ((struct lysc_node_leaf *)node)->type;
7201 if (type->basetype == LY_TYPE_LEAFREF) {
7202 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7203 } else if (type->basetype == LY_TYPE_UNION) {
7204 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7205 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7206 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7207 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7208 }
7209 }
7210 }
7211 }
7212 for (i = 0; i < ctx->leafrefs.count; ++i) {
7213 /* store pointer to the real type */
7214 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7215 if (type->basetype == LY_TYPE_LEAFREF) {
7216 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7217 typeiter->basetype == LY_TYPE_LEAFREF;
Radek Krejci1e008d22020-08-17 11:37:37 +02007218 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype) {}
Michal Vasko004d3152020-06-11 19:59:22 +02007219 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7220 } else if (type->basetype == LY_TYPE_UNION) {
7221 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7222 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7223 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7224 typeiter->basetype == LY_TYPE_LEAFREF;
Radek Krejci1e008d22020-08-17 11:37:37 +02007225 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype) {}
Michal Vasko004d3152020-06-11 19:59:22 +02007226 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7227 }
7228 }
7229 }
7230 }
7231
7232 /* check xpath */
7233 for (i = 0; i < ctx->xpath.count; ++i) {
7234 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7235 }
7236
7237 /* finish incomplete default values compilation */
7238 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007239 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007240 if (r->leaf->nodetype == LYS_LEAF) {
7241 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod));
7242 } else {
7243 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 +02007244 }
Michal Vasko004d3152020-06-11 19:59:22 +02007245 }
7246
7247 return LY_SUCCESS;
7248}
7249
Radek Krejci19a96102018-11-15 13:38:09 +01007250LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007251lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007252{
7253 struct lysc_ctx ctx = {0};
7254 struct lysc_module *mod_c;
7255 struct lysp_module *sp;
7256 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007257 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007258 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007259 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007260 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007261 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007262 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007263 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007264
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007265 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007266
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007267 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007268 /* just imported modules are not compiled */
7269 return LY_SUCCESS;
7270 }
7271
Radek Krejcia46012b2020-08-12 15:41:04 +02007272 compile_id = ++(*mod)->ctx->module_set_id;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007273 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007274
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007275 ctx.ctx = (*mod)->ctx;
7276 ctx.mod = *mod;
7277 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007278 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007279 ctx.path_len = 1;
7280 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007281
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007282 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7283 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7284 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007285
Michal Vasko7c8439f2020-08-05 13:25:19 +02007286 LY_ARRAY_FOR(sp->imports, u) {
7287 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7288 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01007289 LY_ARRAY_FOR(sp->includes, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02007290 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007291 }
Radek Krejci0935f412019-08-20 16:15:18 +02007292
7293 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007294 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007295 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007296 mod_c->features = (*mod)->dis_features;
7297 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007298 } else {
7299 /* features are compiled directly into the compiled module structure,
7300 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci327de162019-06-14 12:52:07 +02007301 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007302 LY_CHECK_GOTO(ret, error);
7303 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007304
Radek Krejci0af46292019-01-11 16:02:31 +01007305 /* finish feature compilation, not only for the main module, but also for the submodules.
7306 * Due to possible forward references, it must be done when all the features (including submodules)
7307 * are present. */
7308 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007309 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007310 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7311 }
Radek Krejci327de162019-06-14 12:52:07 +02007312 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007313 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007314 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007315 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007316 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007317 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7318 }
Radek Krejci327de162019-06-14 12:52:07 +02007319 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007320 }
Radek Krejci327de162019-06-14 12:52:07 +02007321 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007322
Michal Vasko33ff9422020-07-03 09:50:39 +02007323 /* identities, work similarly to features with the precompilation */
7324 if ((*mod)->dis_identities) {
7325 mod_c->identities = (*mod)->dis_identities;
7326 (*mod)->dis_identities = NULL;
7327 } else {
7328 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7329 LY_CHECK_GOTO(ret, error);
7330 }
Radek Krejci19a96102018-11-15 13:38:09 +01007331 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007332 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007333 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007334 lysc_update_path(&ctx, NULL, "{submodule}");
7335 LY_ARRAY_FOR(sp->includes, v) {
7336 if (sp->includes[v].submodule->identities) {
7337 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7338 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7339 LY_CHECK_GOTO(ret, error);
7340 lysc_update_path(&ctx, NULL, NULL);
7341 }
7342 }
Radek Krejci327de162019-06-14 12:52:07 +02007343 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007344
Radek Krejci95710c92019-02-11 15:49:55 +01007345 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007346 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007347 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007348 }
Radek Krejci95710c92019-02-11 15:49:55 +01007349
Radek Krejciec4da802019-05-02 13:02:41 +02007350 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7351 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007352
Radek Krejci95710c92019-02-11 15:49:55 +01007353 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007354 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007355 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007356 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007357 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007358
Radek Krejci474f9b82019-07-24 11:36:37 +02007359 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007360 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007361
Radek Krejci0935f412019-08-20 16:15:18 +02007362 /* extension instances TODO cover extension instances from submodules */
7363 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007364
Michal Vasko004d3152020-06-11 19:59:22 +02007365 /* finish compilation for all unresolved items in the context */
7366 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007367
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007368 /* validate non-instantiated groupings from the parsed schema,
7369 * without it we would accept even the schemas with invalid grouping specification */
7370 ctx.options |= LYSC_OPT_GROUPING;
7371 LY_ARRAY_FOR(sp->groupings, u) {
7372 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007373 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007374 }
7375 }
7376 LY_LIST_FOR(sp->data, node_p) {
7377 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7378 LY_ARRAY_FOR(grps, u) {
7379 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007380 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007381 }
7382 }
7383 }
7384
Radek Krejci474f9b82019-07-24 11:36:37 +02007385 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7386 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7387 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7388 }
7389
Michal Vasko8d544252020-03-02 10:19:52 +01007390#if 0
7391 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7392 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7393 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7394 * the anotation definitions available in the internal schema structure. */
7395 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7396 if (lyp_add_ietf_netconf_annotations(mod)) {
7397 lys_free(mod, NULL, 1, 1);
7398 return NULL;
7399 }
7400 }
7401#endif
7402
7403 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007404 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7405 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007406 }
7407
Radek Krejci1c0c3442019-07-23 16:08:47 +02007408 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007409 ly_set_erase(&ctx.xpath, NULL);
7410 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007411 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007412 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007413 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007414
Radek Krejciec4da802019-05-02 13:02:41 +02007415 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007416 lysp_module_free((*mod)->parsed);
7417 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007418 }
7419
Radek Krejciec4da802019-05-02 13:02:41 +02007420 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007421 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007422 for (i = 0; i < ctx.ctx->list.count; ++i) {
7423 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007424 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007425 m->implemented = 1;
7426 }
7427 }
7428 }
7429
Radek Krejci19a96102018-11-15 13:38:09 +01007430 return LY_SUCCESS;
7431
7432error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007433 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007434 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007435 ly_set_erase(&ctx.xpath, NULL);
7436 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007437 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007438 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007439 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007440 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007441 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007442
Radek Krejcia46012b2020-08-12 15:41:04 +02007443 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7444 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7445 * 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 +02007446 for (i = 0; i < ctx.ctx->list.count; ++i) {
7447 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007448 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007449 /* revert features list to the precompiled state */
7450 lys_feature_precompile_revert(&ctx, m);
7451 /* mark module as imported-only / not-implemented */
7452 m->implemented = 0;
7453 /* free the compiled version of the module */
7454 lysc_module_free(m->compiled, NULL);
7455 m->compiled = NULL;
7456 }
7457 }
7458
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007459 /* remove the module itself from the context and free it */
7460 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7461 lys_module_free(*mod, NULL);
7462 *mod = NULL;
7463
Radek Krejci19a96102018-11-15 13:38:09 +01007464 return ret;
7465}