blob: cde221b44ef4be6f40303fd47ee603778c2c66a0 [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 }
4064 } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module)
4065 && (strcmp(mod->name, node->module->name) > 0));
4066
4067 /* we have the last existing node after our node, easily get the first before and connect it */
4068 start = end->prev;
4069 start->next = node;
4070 node->next = end;
4071 end->prev = node;
4072 node->prev = start;
4073 } else {
4074 /* insert at the end of the parent's children list */
4075 (*children)->prev->next = node;
4076 node->prev = (*children)->prev;
4077 (*children)->prev = node;
4078 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004079
4080 /* check the name uniqueness */
4081 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4082 lysc_node_notifs(parent), node->name, node)) {
4083 return LY_EEXIST;
4084 }
4085 }
4086 }
4087 return LY_SUCCESS;
4088}
4089
Radek Krejci95710c92019-02-11 15:49:55 +01004090/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004091 * @brief Prepare the case structure in choice node for the new data node.
4092 *
4093 * 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
4094 * created in the choice when the first child was processed.
4095 *
4096 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004097 * @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 +02004098 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004099 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4100 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4101 * @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,
4102 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004103 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004104static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004105lys_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 +01004106{
4107 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004108 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004109 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004110 unsigned int u;
4111 LY_ERR ret;
4112
Radek Krejci95710c92019-02-11 15:49:55 +01004113#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004114 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004115 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4117 return NULL; \
4118 } \
4119 }
4120
Radek Krejci39424802020-08-12 09:31:00 +02004121 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci95710c92019-02-11 15:49:55 +01004122 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004123
4124 /* we have to add an implicit case node into the parent choice */
4125 cs = calloc(1, sizeof(struct lysc_node_case));
4126 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004127 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004128 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004129 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004130 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4131 /* the case is already present since the child is not its first children */
4132 return (struct lysc_node_case*)ch->cases->prev;
4133 }
Radek Krejci95710c92019-02-11 15:49:55 +01004134 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004135
4136 /* explicit parent case is not present (this is its first child) */
4137 cs = calloc(1, sizeof(struct lysc_node_case));
4138 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004139 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004140 cs->flags = LYS_STATUS_MASK & node_p->flags;
4141 cs->sp = node_p;
4142
Radek Krejcib1b59152019-01-07 13:21:56 +01004143 /* 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 +02004144 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004145
4146 if (node_p->when) {
4147 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004148 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004149 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004150
4151 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4152 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004153 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004154 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004155 }
Radek Krejciec4da802019-05-02 13:02:41 +02004156 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004157 } else {
4158 LOGINT(ctx->ctx);
4159 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004160 }
4161 cs->module = ctx->mod;
4162 cs->prev = (struct lysc_node*)cs;
4163 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004164 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004165 cs->child = child;
4166
4167 return cs;
4168error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004169 if (cs) {
4170 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4171 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004172 return NULL;
4173
4174#undef UNIQUE_CHECK
4175}
4176
Radek Krejcib56c7502019-02-13 14:19:54 +01004177/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004178 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004179 *
4180 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004181 * @param[in] node Target node where the config is supposed to be changed.
4182 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004183 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4184 * 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 +01004185 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4186 * @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 +01004187 * @return LY_ERR value.
4188 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004189static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004190lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004191 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004192{
4193 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004194 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004195
4196 if (config == (node->flags & LYS_CONFIG_MASK)) {
4197 /* nothing to do */
4198 return LY_SUCCESS;
4199 }
4200
4201 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004202 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004203 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4204 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004205 "Invalid %s of config - configuration node cannot be child of any state data node.",
4206 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004207 return LY_EVALID;
4208 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004209 node->flags |= LYS_SET_CONFIG;
4210 } else {
4211 if (node->flags & LYS_SET_CONFIG) {
4212 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4213 /* setting config flags, but have node with explicit config true */
4214 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004215 "Invalid %s of config - configuration node cannot be child of any state data node.",
4216 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004217 return LY_EVALID;
4218 }
4219 /* do not change config on nodes where the config is explicitely set, this does not apply to
4220 * nodes, which are being changed explicitly (targets of refine or deviation) */
4221 return LY_SUCCESS;
4222 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004223 }
4224 node->flags &= ~LYS_CONFIG_MASK;
4225 node->flags |= config;
4226
4227 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004228 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004229 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004230 }
4231
Radek Krejci76b3e962018-12-14 17:01:25 +01004232 return LY_SUCCESS;
4233}
4234
Radek Krejcib56c7502019-02-13 14:19:54 +01004235/**
4236 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4237 *
4238 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4239 * the flag to such parents from a mandatory children.
4240 *
4241 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4242 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4243 * (mandatory children was removed).
4244 */
Radek Krejcife909632019-02-12 15:34:42 +01004245void
4246lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4247{
4248 struct lysc_node *iter;
4249
4250 if (add) { /* set flag */
4251 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4252 parent = parent->parent) {
4253 parent->flags |= LYS_MAND_TRUE;
4254 }
4255 } else { /* unset flag */
4256 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004257 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004258 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004259 /* there is another mandatory node */
4260 return;
4261 }
4262 }
4263 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4264 parent->flags &= ~LYS_MAND_TRUE;
4265 }
4266 }
4267}
4268
Radek Krejci056d0a82018-12-06 16:57:25 +01004269/**
Radek Krejci3641f562019-02-13 15:38:40 +01004270 * @brief Internal sorting process for the lys_compile_augment_sort().
4271 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4272 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4273 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4274 */
4275static void
4276lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4277{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004278 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004279 size_t len;
4280
4281 len = strlen(aug_p->nodeid);
4282 LY_ARRAY_FOR(result, v) {
4283 if (strlen(result[v]->nodeid) <= len) {
4284 continue;
4285 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004286 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004287 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004288 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004289 break;
4290 }
4291 }
4292 result[v] = aug_p;
4293 LY_ARRAY_INCREMENT(result);
4294}
4295
4296/**
4297 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4298 *
4299 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4300 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4301 *
4302 * @param[in] ctx Compile context.
4303 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4304 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4305 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4306 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4307 * @return LY_ERR value.
4308 */
4309LY_ERR
4310lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4311{
4312 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004313 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004314
4315 assert(augments);
4316
4317 /* get count of the augments in module and all its submodules */
4318 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004319 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004320 }
4321 LY_ARRAY_FOR(inc_p, u) {
4322 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004323 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004324 }
4325 }
4326
4327 if (!count) {
4328 *augments = NULL;
4329 return LY_SUCCESS;
4330 }
4331 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4332
4333 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4334 * together, so there can be even /z/y betwwen them. */
4335 LY_ARRAY_FOR(aug_p, u) {
4336 lys_compile_augment_sort_(&aug_p[u], result);
4337 }
4338 LY_ARRAY_FOR(inc_p, u) {
4339 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4340 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4341 }
4342 }
4343
4344 *augments = result;
4345 return LY_SUCCESS;
4346}
4347
4348/**
4349 * @brief Compile the parsed augment connecting it into its target.
4350 *
4351 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4352 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4353 * are already implemented and compiled.
4354 *
4355 * @param[in] ctx Compile context.
4356 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004357 * @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
4358 * children in case of the augmenting uses data.
4359 * @return LY_SUCCESS on success.
4360 * @return LY_EVALID on failure.
4361 */
4362LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004363lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004364{
Michal Vaskoe6143202020-07-03 13:02:08 +02004365 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004366 struct lysp_node *node_p, *case_node_p;
4367 struct lysc_node *target; /* target target of the augment */
4368 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004369 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004370 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004371 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004372 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004373 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejciec4da802019-05-02 13:02:41 +02004374 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004375
Radek Krejci327de162019-06-14 12:52:07 +02004376 lysc_update_path(ctx, NULL, "{augment}");
4377 lysc_update_path(ctx, NULL, aug_p->nodeid);
4378
Radek Krejci7af64242019-02-18 13:07:53 +01004379 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004380 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004381 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004382 if (ret != LY_SUCCESS) {
4383 if (ret == LY_EDENIED) {
4384 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4385 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4386 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4387 }
4388 return LY_EVALID;
4389 }
4390
4391 /* check for mandatory nodes
4392 * - new cases augmenting some choice can have mandatory nodes
4393 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4394 */
Radek Krejci733988a2019-02-15 15:12:44 +01004395 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004396 allow_mandatory = 1;
4397 }
4398
4399 when_shared = NULL;
4400 LY_LIST_FOR(aug_p->child, node_p) {
4401 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4402 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004403 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004404 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4405 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004407 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4408 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004409 return LY_EVALID;
4410 }
4411
4412 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004413 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004414 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004415 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004416 } else {
4417 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004418 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004419 }
4420 }
Radek Krejciec4da802019-05-02 13:02:41 +02004421 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004422
Radek Krejcife13da42019-02-15 14:51:01 +01004423 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4424 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004425 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004426 /* 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 +02004427 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 +01004428 } else if (target->nodetype == LYS_CHOICE) {
4429 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4430 node = ((struct lysc_node_choice*)target)->cases->prev;
4431 } else {
4432 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004433 node = (struct lysc_node*)lysc_node_children(target, flags);
4434 if (!node) {
4435 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4436 break;
4437 }
4438 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004439 }
4440
Radek Krejci733988a2019-02-15 15:12:44 +01004441 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004442 node->flags &= ~LYS_MAND_TRUE;
4443 lys_compile_mandatory_parents(target, 0);
4444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004445 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004446 return LY_EVALID;
4447 }
4448
4449 /* pass augment's when to all the children */
4450 if (aug_p->when) {
4451 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4452 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004453 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004454 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004455
4456 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4457 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004458 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004459 }
4460
Radek Krejci3641f562019-02-13 15:38:40 +01004461 when_shared = *when;
4462 } else {
4463 ++when_shared->refcount;
4464 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004465
4466 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4467 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004468 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004469 }
Radek Krejci3641f562019-02-13 15:38:40 +01004470 }
4471 }
4472 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004473
Radek Krejciec4da802019-05-02 13:02:41 +02004474 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004475 switch (target->nodetype) {
4476 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004477 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004478 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004479 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004480 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004481 break;
4482 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004483 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004484 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004485 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004486 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004487 break;
4488 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004489 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004490 if (aug_p->actions) {
4491 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004492 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4493 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004494 return LY_EVALID;
4495 }
4496 if (aug_p->notifs) {
4497 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004498 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004499 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004500 return LY_EVALID;
4501 }
4502 }
Radek Krejci3641f562019-02-13 15:38:40 +01004503
Michal Vaskoe6143202020-07-03 13:02:08 +02004504 /* add this module into the target module augmented_by, if not there already */
4505 rc = LY_SUCCESS;
4506 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4507 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4508 rc = LY_EEXIST;
4509 break;
4510 }
4511 }
4512 if (!rc) {
4513 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4514 *aug_mod = ctx->mod;
4515 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004516
Radek Krejci327de162019-06-14 12:52:07 +02004517 lysc_update_path(ctx, NULL, NULL);
4518 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004519
Radek Krejci3641f562019-02-13 15:38:40 +01004520error:
Radek Krejciec4da802019-05-02 13:02:41 +02004521 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004522 return ret;
4523}
4524
4525/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004526 * @brief Apply refined or deviated mandatory flag to the target node.
4527 *
4528 * @param[in] ctx Compile context.
4529 * @param[in] node Target node where the mandatory property is supposed to be changed.
4530 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004531 * @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 +01004532 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4533 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4534 * 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 +01004535 * @return LY_ERR value.
4536 */
4537static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004538lys_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 +01004539{
4540 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4541 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004542 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4543 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004544 return LY_EVALID;
4545 }
4546
4547 if (mandatory_flag & LYS_MAND_TRUE) {
4548 /* check if node has default value */
4549 if (node->nodetype & LYS_LEAF) {
4550 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004551 if (refine_flag) {
4552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004553 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004554 return LY_EVALID;
4555 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004556 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004557 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004558 if (refine_flag) {
4559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004560 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004561 return LY_EVALID;
4562 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004563 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004564 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004565 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 +01004566 return LY_EVALID;
4567 }
4568
4569 node->flags &= ~LYS_MAND_FALSE;
4570 node->flags |= LYS_MAND_TRUE;
4571 lys_compile_mandatory_parents(node->parent, 1);
4572 } else {
4573 /* make mandatory false */
4574 node->flags &= ~LYS_MAND_TRUE;
4575 node->flags |= LYS_MAND_FALSE;
4576 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcif1421c22019-02-19 13:05:20 +01004577 }
4578 return LY_SUCCESS;
4579}
4580
4581/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004582 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4583 * If present, also apply uses's modificators.
4584 *
4585 * @param[in] ctx Compile context
4586 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004587 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4588 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4589 * the compile context.
4590 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4591 */
4592static LY_ERR
Radek Krejcic6b4f442020-08-12 14:45:18 +02004593lys_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 +01004594{
4595 struct lysp_node *node_p;
Radek Krejcic6b4f442020-08-12 14:45:18 +02004596 struct lysc_node *node, *child, *iter;
Radek Krejci12fb9142019-01-08 09:45:30 +01004597 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004598 struct lysc_node_container context_node_fake =
4599 {.nodetype = LYS_CONTAINER,
4600 .module = ctx->mod,
4601 .flags = parent ? parent->flags : 0,
4602 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004603 .prev = (struct lysc_node*)&context_node_fake,
4604 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004605 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004606 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004607 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004608 int found;
4609 const char *id, *name, *prefix;
4610 size_t prefix_len, name_len;
4611 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004612 struct lysp_refine *rfn;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004613 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004614 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004615 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004616 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004617 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004618 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004619 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004620 struct lysc_notif **notifs = NULL;
4621 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004622
4623 /* search for the grouping definition */
4624 found = 0;
4625 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004626 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004627 if (prefix) {
4628 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4629 if (!mod) {
4630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004631 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004632 return LY_EVALID;
4633 }
4634 } else {
4635 mod = ctx->mod_def;
4636 }
4637 if (mod == ctx->mod_def) {
4638 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004639 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004640 LY_ARRAY_FOR(grp, u) {
4641 if (!strcmp(grp[u].name, name)) {
4642 grp = &grp[u];
4643 found = 1;
4644 break;
4645 }
4646 }
4647 }
4648 }
4649 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004650 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004651 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004652 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004653 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004654 if (!strcmp(grp[u].name, name)) {
4655 grp = &grp[u];
4656 found = 1;
4657 }
4658 }
4659 }
4660 if (!found && mod->parsed->includes) {
4661 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004662 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004663 grp = mod->parsed->includes[u].submodule->groupings;
4664 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004665 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004666 if (!strcmp(grp[v].name, name)) {
4667 grp = &grp[v];
4668 found = 1;
4669 }
4670 }
4671 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004672 }
4673 }
4674 }
4675 if (!found) {
4676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4677 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4678 return LY_EVALID;
4679 }
4680
4681 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4682 grp_stack_count = ctx->groupings.count;
4683 ly_set_add(&ctx->groupings, (void*)grp, 0);
4684 if (grp_stack_count == ctx->groupings.count) {
4685 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4687 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4688 return LY_EVALID;
4689 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004690 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4691 /* remember that the grouping is instantiated to avoid its standalone validation */
4692 grp->flags |= LYS_USED_GRP;
4693 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004694
4695 /* switch context's mod_def */
4696 mod_old = ctx->mod_def;
4697 ctx->mod_def = mod;
4698
4699 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004700 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 +01004701
Radek Krejcic6b4f442020-08-12 14:45:18 +02004702 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
4703 * applu refine and augment only to the nodes from the uses */
4704 if (parent) {
4705 if (parent->nodetype == LYS_CASE) {
4706 child = (struct lysc_node*)lysc_node_children(parent->parent, ctx->options & LYSC_OPT_RPC_MASK);
4707 } else {
4708 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4709 }
4710 } else if (ctx->mod->compiled->data) {
4711 child = ctx->mod->compiled->data;
4712 } else {
4713 child = NULL;
4714 }
4715 /* remember the last child */
4716 if (child) {
4717 child = child->prev;
4718 }
4719
Radek Krejcifc11bd72019-04-11 16:00:05 +02004720 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004721 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004722 /* 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 +02004723 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 +01004724 }
Radek Krejcic6b4f442020-08-12 14:45:18 +02004725
4726 /* split the children and add the uses's data into the fake context node */
4727 if (child) {
4728 context_node_fake.child = child->next;
4729 } else if (parent) {
4730 context_node_fake.child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4731 } else if (ctx->mod->compiled->data) {
4732 context_node_fake.child = ctx->mod->compiled->data;
4733 }
4734 if (context_node_fake.child) {
4735 /* remember child as the last data node added by grouping to fix the list later */
4736 child = context_node_fake.child->prev;
4737 context_node_fake.child->prev = NULL;
4738 }
4739
Radek Krejci00b874b2019-02-12 10:54:50 +01004740 when_shared = NULL;
Radek Krejcic6b4f442020-08-12 14:45:18 +02004741 LY_LIST_FOR(context_node_fake.child, iter) {
4742 iter->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004743
Radek Krejcifc11bd72019-04-11 16:00:05 +02004744 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004745 if (uses_p->when) {
Radek Krejcic6b4f442020-08-12 14:45:18 +02004746 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004747 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004748 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4749
4750 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4751 /* do not check "when" semantics in a grouping */
Radek Krejcic6b4f442020-08-12 14:45:18 +02004752 ly_set_add(&ctx->xpath, iter, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004753 }
4754
Radek Krejci00b874b2019-02-12 10:54:50 +01004755 when_shared = *when;
4756 } else {
4757 ++when_shared->refcount;
4758 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004759
4760 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4761 /* in this case check "when" again for all children because of dummy node check */
Radek Krejcic6b4f442020-08-12 14:45:18 +02004762 ly_set_add(&ctx->xpath, iter, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004763 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004764 }
4765 }
Radek Krejci01342af2019-01-03 15:18:08 +01004766 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004767
Radek Krejcifc11bd72019-04-11 16:00:05 +02004768 /* compile actions */
4769 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4770 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004771 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004772 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004773 if (*actions && (uses_p->augments || uses_p->refines)) {
4774 /* 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 +02004775 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4776 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4777 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004778 }
4779 }
4780
4781 /* compile notifications */
4782 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4783 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004784 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004785 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004786 if (*notifs && (uses_p->augments || uses_p->refines)) {
4787 /* 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 +02004788 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4789 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4790 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004791 }
4792 }
4793
4794
Radek Krejci3641f562019-02-13 15:38:40 +01004795 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004796 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004797 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004798 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004799 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004800
Radek Krejcif0089082019-01-07 16:42:01 +01004801 /* reload previous context's mod_def */
4802 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004803 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004804
Radek Krejci76b3e962018-12-14 17:01:25 +01004805 /* apply refine */
4806 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004807 lysc_update_path(ctx, NULL, rfn->nodeid);
4808
Radek Krejci7af64242019-02-18 13:07:53 +01004809 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 +01004810 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004811 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004812 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004813
4814 /* default value */
4815 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004816 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004817 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004818 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4819 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004820 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004821 }
4822 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4823 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004824 "Invalid refine of default - %s cannot hold default value(s).",
4825 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004826 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004827 }
4828 if (node->nodetype == LYS_LEAF) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004829 /* postpone default compilation when the tree is complete */
4830 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0],
4831 ctx->mod_def), cleanup);
4832 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004833 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004834 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4836 "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 +02004837 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004838 }
Radek Krejcia1911222019-07-22 17:24:50 +02004839
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004840 /* postpone default compilation when the tree is complete */
4841 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts,
4842 ctx->mod_def), cleanup);
4843 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004844 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004845 if (((struct lysc_node_choice*)node)->dflt) {
4846 /* unset LYS_SET_DFLT from the current default case */
4847 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4848 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004849 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 +01004850 }
4851 }
4852
Radek Krejci12fb9142019-01-08 09:45:30 +01004853 /* description */
4854 if (rfn->dsc) {
4855 FREE_STRING(ctx->ctx, node->dsc);
4856 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4857 }
4858
4859 /* reference */
4860 if (rfn->ref) {
4861 FREE_STRING(ctx->ctx, node->ref);
4862 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4863 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004864
4865 /* config */
4866 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004867 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004868 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004869 } else {
4870 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004871 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004872 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004873 }
4874
4875 /* mandatory */
4876 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004877 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004878 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004879
4880 /* presence */
4881 if (rfn->presence) {
4882 if (node->nodetype != LYS_CONTAINER) {
4883 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004884 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4885 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004886 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004887 }
4888 node->flags |= LYS_PRESENCE;
4889 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004890
4891 /* must */
4892 if (rfn->musts) {
4893 switch (node->nodetype) {
4894 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004895 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 +01004896 break;
4897 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004898 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 +01004899 break;
4900 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004901 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 +01004902 break;
4903 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004904 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 +01004905 break;
4906 case LYS_ANYXML:
4907 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004908 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 +01004909 break;
4910 default:
4911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004912 "Invalid refine of must statement - %s cannot hold any must statement.",
4913 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004914 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004915 }
Michal Vasko004d3152020-06-11 19:59:22 +02004916 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01004917 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004918
4919 /* min/max-elements */
4920 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4921 switch (node->nodetype) {
4922 case LYS_LEAFLIST:
4923 if (rfn->flags & LYS_SET_MAX) {
4924 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4925 }
4926 if (rfn->flags & LYS_SET_MIN) {
4927 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004928 if (rfn->min) {
4929 node->flags |= LYS_MAND_TRUE;
4930 lys_compile_mandatory_parents(node->parent, 1);
4931 } else {
4932 node->flags &= ~LYS_MAND_TRUE;
4933 lys_compile_mandatory_parents(node->parent, 0);
4934 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004935 }
4936 break;
4937 case LYS_LIST:
4938 if (rfn->flags & LYS_SET_MAX) {
4939 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4940 }
4941 if (rfn->flags & LYS_SET_MIN) {
4942 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004943 if (rfn->min) {
4944 node->flags |= LYS_MAND_TRUE;
4945 lys_compile_mandatory_parents(node->parent, 1);
4946 } else {
4947 node->flags &= ~LYS_MAND_TRUE;
4948 lys_compile_mandatory_parents(node->parent, 0);
4949 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004950 }
4951 break;
4952 default:
4953 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004954 "Invalid refine of %s statement - %s cannot hold this statement.",
4955 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004956 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004957 }
4958 }
Radek Krejcif0089082019-01-07 16:42:01 +01004959
4960 /* if-feature */
4961 if (rfn->iffeatures) {
4962 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004963 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004964 }
Radek Krejci327de162019-06-14 12:52:07 +02004965
4966 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004967 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004968
Radek Krejcif2271f12019-01-07 16:42:23 +01004969 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004970 for (uint32_t i = 0; i < refined.count; ++i) {
4971 node = (struct lysc_node*)refined.objs[i];
4972 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02004973 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004974
4975 /* check possible conflict with default value (default added, mandatory left true) */
4976 if ((node->flags & LYS_MAND_TRUE) &&
4977 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4978 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004980 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004981 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004982 }
4983
4984 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4985 if (node->nodetype == LYS_LIST) {
4986 min = ((struct lysc_node_list*)node)->min;
4987 max = ((struct lysc_node_list*)node)->max;
4988 } else {
4989 min = ((struct lysc_node_leaflist*)node)->min;
4990 max = ((struct lysc_node_leaflist*)node)->max;
4991 }
4992 if (min > max) {
4993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004994 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
4995 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004996 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004997 }
4998 }
4999 }
5000
Radek Krejcic6b4f442020-08-12 14:45:18 +02005001 if (first_p) {
5002 *first_p = context_node_fake.child;
5003 }
Radek Krejci327de162019-06-14 12:52:07 +02005004 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005005 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005006
5007cleanup:
5008 /* fix connection of the children nodes from fake context node back into the parent */
5009 if (context_node_fake.child) {
5010 context_node_fake.child->prev = child;
5011 }
5012 LY_LIST_FOR(context_node_fake.child, child) {
5013 child->parent = parent;
5014 }
5015
5016 if (uses_p->augments || uses_p->refines) {
5017 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005018 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005019 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005020 LY_ARRAY_FREE(context_node_fake.actions);
5021 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005022 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005023 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005024 LY_ARRAY_FREE(context_node_fake.notifs);
5025 }
5026 }
5027
Radek Krejcie86bf772018-12-14 11:39:53 +01005028 /* reload previous context's mod_def */
5029 ctx->mod_def = mod_old;
5030 /* remove the grouping from the stack for circular groupings dependency check */
5031 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5032 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005033 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005034 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005035
5036 return ret;
5037}
5038
Radek Krejci327de162019-06-14 12:52:07 +02005039static int
5040lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5041{
5042 struct lysp_node *iter;
5043 int len = 0;
5044
5045 *path = NULL;
5046 for (iter = node; iter && len >= 0; iter = iter->parent) {
5047 char *s = *path;
5048 char *id;
5049
5050 switch (iter->nodetype) {
5051 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005052 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005053 break;
5054 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005055 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005056 break;
5057 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005058 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005059 break;
5060 default:
5061 id = strdup(iter->name);
5062 break;
5063 }
5064
5065 if (!iter->parent) {
5066 /* print prefix */
5067 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5068 } else {
5069 /* prefix is the same as in parent */
5070 len = asprintf(path, "/%s%s", id, s ? s : "");
5071 }
5072 free(s);
5073 free(id);
5074 }
5075
5076 if (len < 0) {
5077 free(*path);
5078 *path = NULL;
5079 } else if (len == 0) {
5080 *path = strdup("/");
5081 len = 1;
5082 }
5083 return len;
5084}
5085
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005086/**
5087 * @brief Validate groupings that were defined but not directly used in the schema itself.
5088 *
5089 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5090 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5091 */
5092static LY_ERR
5093lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5094{
5095 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005096 char *path;
5097 int len;
5098
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005099 struct lysp_node_uses fake_uses = {
5100 .parent = node_p,
5101 .nodetype = LYS_USES,
5102 .flags = 0, .next = NULL,
5103 .name = grp->name,
5104 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5105 .refines = NULL, .augments = NULL
5106 };
5107 struct lysc_node_container fake_container = {
5108 .nodetype = LYS_CONTAINER,
5109 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5110 .module = ctx->mod,
5111 .sp = NULL, .parent = NULL, .next = NULL,
5112 .prev = (struct lysc_node*)&fake_container,
5113 .name = "fake",
5114 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5115 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5116 };
5117
5118 if (grp->parent) {
5119 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5120 }
Radek Krejci327de162019-06-14 12:52:07 +02005121
5122 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5123 if (len < 0) {
5124 LOGMEM(ctx->ctx);
5125 return LY_EMEM;
5126 }
5127 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5128 ctx->path_len = (uint16_t)len;
5129 free(path);
5130
5131 lysc_update_path(ctx, NULL, "{grouping}");
5132 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcic6b4f442020-08-12 14:45:18 +02005133 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005134 lysc_update_path(ctx, NULL, NULL);
5135 lysc_update_path(ctx, NULL, NULL);
5136
5137 ctx->path_len = 1;
5138 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005139
5140 /* cleanup */
5141 lysc_node_container_free(ctx->ctx, &fake_container);
5142
5143 return ret;
5144}
Radek Krejcife909632019-02-12 15:34:42 +01005145
Radek Krejcie86bf772018-12-14 11:39:53 +01005146/**
Radek Krejcia3045382018-11-22 14:30:31 +01005147 * @brief Compile parsed schema node information.
5148 * @param[in] ctx Compile context
5149 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005150 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5151 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5152 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005153 * @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).
5154 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005155 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5156 */
Radek Krejci19a96102018-11-15 13:38:09 +01005157static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005158lys_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 +01005159{
Radek Krejci1c54f462020-05-12 17:25:34 +02005160 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005161 struct lysc_node *node = NULL;
5162 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005163 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005164 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005165 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005166
Radek Krejci327de162019-06-14 12:52:07 +02005167 if (node_p->nodetype != LYS_USES) {
5168 lysc_update_path(ctx, parent, node_p->name);
5169 } else {
5170 lysc_update_path(ctx, NULL, "{uses}");
5171 lysc_update_path(ctx, NULL, node_p->name);
5172 }
5173
Radek Krejci19a96102018-11-15 13:38:09 +01005174 switch (node_p->nodetype) {
5175 case LYS_CONTAINER:
5176 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5177 node_compile_spec = lys_compile_node_container;
5178 break;
5179 case LYS_LEAF:
5180 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5181 node_compile_spec = lys_compile_node_leaf;
5182 break;
5183 case LYS_LIST:
5184 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005185 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005186 break;
5187 case LYS_LEAFLIST:
5188 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005189 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005190 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005191 case LYS_CHOICE:
5192 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005193 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005194 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005195 case LYS_ANYXML:
5196 case LYS_ANYDATA:
5197 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005198 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005199 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005200 case LYS_USES:
Radek Krejcic6b4f442020-08-12 14:45:18 +02005201 if (parent && parent->nodetype == LYS_CHOICE) {
5202 assert(node_p->parent->nodetype == LYS_CASE);
5203 lysc_update_path(ctx, parent, node_p->parent->name);
5204 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, NULL);
5205 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
5206 }
5207
5208 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, cs ? (struct lysc_node*)cs : parent, &node);
5209 if (cs) {
5210 cs->child = node;
5211 lysc_update_path(ctx, NULL, NULL);
5212 }
Radek Krejci327de162019-06-14 12:52:07 +02005213 lysc_update_path(ctx, NULL, NULL);
5214 lysc_update_path(ctx, NULL, NULL);
5215 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005216 default:
5217 LOGINT(ctx->ctx);
5218 return LY_EINT;
5219 }
5220 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5221 node->nodetype = node_p->nodetype;
5222 node->module = ctx->mod;
5223 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005224 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005225
5226 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005227 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005228 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005229 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005230 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5231 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005232 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005233 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005234 node->flags |= LYS_CONFIG_R;
5235 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005236 /* config not explicitely set, inherit it from parent */
5237 if (parent) {
5238 node->flags |= parent->flags & LYS_CONFIG_MASK;
5239 } else {
5240 /* default is config true */
5241 node->flags |= LYS_CONFIG_W;
5242 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005243 } else {
5244 /* config set explicitely */
5245 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005246 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005247 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5248 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5249 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005250 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005251 goto error;
5252 }
Radek Krejci19a96102018-11-15 13:38:09 +01005253
Radek Krejcia6d57732018-11-29 13:40:37 +01005254 /* *list ordering */
5255 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5256 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005257 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005258 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5259 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005260 node->flags &= ~LYS_ORDBY_MASK;
5261 node->flags |= LYS_ORDBY_SYSTEM;
5262 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5263 /* default ordering is system */
5264 node->flags |= LYS_ORDBY_SYSTEM;
5265 }
5266 }
5267
Radek Krejci19a96102018-11-15 13:38:09 +01005268 /* status - it is not inherited by specification, but it does not make sense to have
5269 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005270 if (!parent || parent->nodetype != LYS_CHOICE) {
5271 /* in case of choice/case's children, postpone the check to the moment we know if
5272 * 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 +02005273 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 +01005274 }
5275
Radek Krejciec4da802019-05-02 13:02:41 +02005276 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005277 node->sp = node_p;
5278 }
5279 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005280 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5281 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005282 if (node_p->when) {
5283 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005284 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005285
5286 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5287 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005288 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005289 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005290 }
Radek Krejciec4da802019-05-02 13:02:41 +02005291 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005292
5293 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005294 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005295
Radek Krejci0935f412019-08-20 16:15:18 +02005296 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5297
Radek Krejcife909632019-02-12 15:34:42 +01005298 /* inherit LYS_MAND_TRUE in parent containers */
5299 if (node->flags & LYS_MAND_TRUE) {
5300 lys_compile_mandatory_parents(parent, 1);
5301 }
5302
Radek Krejci327de162019-06-14 12:52:07 +02005303 lysc_update_path(ctx, NULL, NULL);
5304
Radek Krejci19a96102018-11-15 13:38:09 +01005305 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005306 if (parent) {
5307 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005308 if (node_p->parent->nodetype == LYS_CASE) {
5309 lysc_update_path(ctx, parent, node_p->parent->name);
5310 } else {
5311 lysc_update_path(ctx, parent, node->name);
5312 }
Radek Krejciec4da802019-05-02 13:02:41 +02005313 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005314 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005315 if (uses_status) {
5316
5317 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005318 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005319 * it directly gets the same status flags as the choice;
5320 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005321 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005322 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005323 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005324 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005325 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005326 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005327 }
Radek Krejciec4da802019-05-02 13:02:41 +02005328 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 +02005329
5330 if (parent->nodetype == LYS_CHOICE) {
5331 lysc_update_path(ctx, NULL, NULL);
5332 }
Radek Krejci19a96102018-11-15 13:38:09 +01005333 } else {
5334 /* top-level element */
5335 if (!ctx->mod->compiled->data) {
5336 ctx->mod->compiled->data = node;
5337 } else {
5338 /* insert at the end of the module's top-level nodes list */
5339 ctx->mod->compiled->data->prev->next = node;
5340 node->prev = ctx->mod->compiled->data->prev;
5341 ctx->mod->compiled->data->prev = node;
5342 }
Radek Krejci327de162019-06-14 12:52:07 +02005343 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005344 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5345 ctx->mod->compiled->notifs, node->name, node)) {
5346 return LY_EVALID;
5347 }
Radek Krejci19a96102018-11-15 13:38:09 +01005348 }
Radek Krejci327de162019-06-14 12:52:07 +02005349 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005350
5351 return LY_SUCCESS;
5352
5353error:
5354 lysc_node_free(ctx->ctx, node);
5355 return ret;
5356}
5357
Radek Krejciccd20f12019-02-15 14:12:27 +01005358static void
5359lysc_disconnect(struct lysc_node *node)
5360{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005361 struct lysc_node *parent, *child, *prev = NULL, *next;
5362 struct lysc_node_case *cs = NULL;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005363 struct lysc_module *modc = node->module->compiled;
Radek Krejciccd20f12019-02-15 14:12:27 +01005364 int remove_cs = 0;
5365
5366 parent = node->parent;
5367
5368 /* parent's first child */
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005369 if (parent && parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005370 cs = (struct lysc_node_case*)node;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005371 } else if (parent && parent->nodetype == LYS_CASE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005372 /* disconnecting some node in a case */
5373 cs = (struct lysc_node_case*)parent;
5374 parent = cs->parent;
5375 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5376 if (child == node) {
5377 if (cs->child == child) {
5378 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5379 /* case with a single child -> remove also the case */
5380 child->parent = NULL;
5381 remove_cs = 1;
5382 } else {
5383 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005384 }
5385 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005386 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005387 }
5388 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005389 if (!remove_cs) {
5390 cs = NULL;
5391 }
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005392 } else if (parent && lysc_node_children(parent, node->flags) == node) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005393 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005394 } else if (modc->data == node) {
5395 modc->data = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005396 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005397 if (cs) {
5398 if (remove_cs) {
5399 /* cs has only one child which is being also removed */
5400 lysc_disconnect((struct lysc_node*)cs);
5401 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5402 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005403 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5404 /* default case removed */
5405 ((struct lysc_node_choice*)parent)->dflt = NULL;
5406 }
5407 if (((struct lysc_node_choice*)parent)->cases == cs) {
5408 /* first case removed */
5409 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5410 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005411 if (cs->child) {
5412 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5413 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5414 prev = cs->child->prev;
5415 } /* else all the children are under a single case */
5416 LY_LIST_FOR_SAFE(cs->child, next, child) {
5417 if (child->parent != (struct lysc_node*)cs) {
5418 break;
5419 }
5420 lysc_node_free(node->module->ctx, child);
5421 }
5422 if (prev) {
5423 if (prev->next) {
5424 prev->next = child;
5425 }
5426 if (child) {
5427 child->prev = prev;
5428 } else {
5429 /* link from the first child under the cases */
5430 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5431 }
5432 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005433 }
5434 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005435 }
5436
5437 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005438 if (node->prev->next) {
5439 node->prev->next = node->next;
5440 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005441 if (node->next) {
5442 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005443 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6b0dbc62020-08-14 23:51:43 +02005444 if (parent) {
5445 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
5446 } else {
5447 child = modc->data;
5448 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005449 if (child) {
5450 child->prev = node->prev;
5451 }
5452 } else if (((struct lysc_node_choice*)parent)->cases) {
5453 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005454 }
5455}
5456
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005457struct lysc_deviation {
5458 const char *nodeid;
5459 struct lysc_node *target; /* target node of the deviation */
5460 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
5461 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
5462 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5463};
5464
Michal Vaskoccc062a2020-08-13 08:34:50 +02005465/* MACROS for deviates checking */
5466#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5467 if (!(target->nodetype & (NODETYPES))) { \
5468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5469 goto cleanup; \
5470 }
5471
5472#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5473 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5474 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5475 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5476 goto cleanup; \
5477 }
5478
5479#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5480 if (!((TYPE)target)->MEMBER || COND) { \
5481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5482 goto cleanup; \
5483 }
5484
5485/**
5486 * @brief Apply deviate add.
5487 *
5488 * @param[in] ctx Compile context.
5489 * @param[in] target Deviation target.
5490 * @param[in] dev_flags Internal deviation flags.
5491 * @param[in] d Deviate add to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02005492 * @return LY_ERR value.
5493 */
5494static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005495lys_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 +02005496{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005497 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005498 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5499 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005500 LY_ARRAY_COUNT_TYPE x;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005501
5502#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5503 if (((TYPE)target)->MEMBER COND) { \
5504 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5505 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5506 PROPERTY, ((TYPE)target)->MEMBER); \
5507 goto cleanup; \
5508 }
5509
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005510#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005511 if (((TYPE)target)->MEMBER && (COND)) { \
5512 int dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005513 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005514 ctx->mod_def, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005515 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5516 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5517 if (dynamic_) {free((void*)val_);} \
5518 goto cleanup; \
5519 }
5520
5521#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5522 if (((TYPE)target)->MEMBER && (COND)) { \
5523 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5524 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5525 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5526 goto cleanup; \
5527 }
5528
5529 /* [units-stmt] */
5530 if (d->units) {
5531 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5532 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5533
5534 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5535 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units);
5536 }
5537
5538 /* *must-stmt */
5539 if (d->musts) {
5540 switch (target->nodetype) {
5541 case LYS_CONTAINER:
5542 case LYS_LIST:
5543 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5544 x, lys_compile_must, ret, cleanup);
5545 break;
5546 case LYS_LEAF:
5547 case LYS_LEAFLIST:
5548 case LYS_ANYDATA:
5549 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5550 x, lys_compile_must, ret, cleanup);
5551 break;
5552 case LYS_NOTIF:
5553 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5554 x, lys_compile_must, ret, cleanup);
5555 break;
5556 case LYS_RPC:
5557 case LYS_ACTION:
5558 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5559 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5560 x, lys_compile_must, ret, cleanup);
5561 break;
5562 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5563 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5564 x, lys_compile_must, ret, cleanup);
5565 break;
5566 }
5567 /* fall through */
5568 default:
5569 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5570 lys_nodetype2str(target->nodetype), "add", "must");
5571 goto cleanup;
5572 }
5573 ly_set_add(&ctx->xpath, target, 0);
5574 }
5575
5576 /* *unique-stmt */
5577 if (d->uniques) {
5578 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5579 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5580 }
5581
5582 /* *default-stmt */
5583 if (d->dflts) {
5584 switch (target->nodetype) {
5585 case LYS_LEAF:
5586 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005587 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005588 if (leaf->dflt) {
5589 /* first, remove the default value taken from the type */
Michal Vaskoccc062a2020-08-13 08:34:50 +02005590 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5591 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005592 free(leaf->dflt);
5593 leaf->dflt = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005594 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005595
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005596 /* store the default value in unres */
Michal Vasko31a82d52020-08-21 08:11:48 +02005597 LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005598 target->flags |= LYS_SET_DFLT;
5599 break;
5600 case LYS_LEAFLIST:
5601 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5602 /* first, remove the default value taken from the type */
5603 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02005604 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5605 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5606 free(llist->dflts[x]);
5607 }
5608 LY_ARRAY_FREE(llist->dflts);
5609 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005610 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02005611
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005612 /* store the default values in unres */
5613 LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005614 target->flags |= LYS_SET_DFLT;
5615 break;
5616 case LYS_CHOICE:
5617 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5618 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5619 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5620 * to allow making the default case even the augmented case from the deviating module */
5621 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5622 goto cleanup;
5623 }
5624 break;
5625 default:
5626 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5627 lys_nodetype2str(target->nodetype), "add", "default");
5628 goto cleanup;
5629 }
5630 }
5631
5632 /* [config-stmt] */
5633 if (d->flags & LYS_CONFIG_MASK) {
5634 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5635 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5636 lys_nodetype2str(target->nodetype), "add", "config");
5637 goto cleanup;
5638 }
5639 if (dev_flags) {
5640 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5641 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5642 }
5643 if (target->flags & LYS_SET_CONFIG) {
5644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5645 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5646 target->flags & LYS_CONFIG_W ? "true" : "false");
5647 goto cleanup;
5648 }
5649 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5650 }
5651
5652 /* [mandatory-stmt] */
5653 if (d->flags & LYS_MAND_MASK) {
5654 if (target->flags & LYS_MAND_MASK) {
5655 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5656 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5657 target->flags & LYS_MAND_TRUE ? "true" : "false");
5658 goto cleanup;
5659 }
5660 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5661 }
5662
5663 /* [min-elements-stmt] */
5664 if (d->flags & LYS_SET_MIN) {
5665 if (target->nodetype == LYS_LEAFLIST) {
5666 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5667 /* change value */
5668 ((struct lysc_node_leaflist*)target)->min = d->min;
5669 } else if (target->nodetype == LYS_LIST) {
5670 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5671 /* change value */
5672 ((struct lysc_node_list*)target)->min = d->min;
5673 } else {
5674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5675 lys_nodetype2str(target->nodetype), "add", "min-elements");
5676 goto cleanup;
5677 }
5678 if (d->min) {
5679 target->flags |= LYS_MAND_TRUE;
5680 }
5681 }
5682
5683 /* [max-elements-stmt] */
5684 if (d->flags & LYS_SET_MAX) {
5685 if (target->nodetype == LYS_LEAFLIST) {
5686 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5687 /* change value */
5688 ((struct lysc_node_leaflist*)target)->max = d->max ? d->max : (uint32_t)-1;
5689 } else if (target->nodetype == LYS_LIST) {
5690 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5691 /* change value */
5692 ((struct lysc_node_list*)target)->max = d->max ? d->max : (uint32_t)-1;
5693 } else {
5694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5695 lys_nodetype2str(target->nodetype), "add", "max-elements");
5696 goto cleanup;
5697 }
5698 }
5699
5700 ret = LY_SUCCESS;
5701
5702cleanup:
5703 return ret;
5704}
5705
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005706static LY_ERR
5707lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt)
5708{
5709 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5710 int dyn = 0;
5711 const char *orig_dflt;
5712 uint32_t i;
5713
5714 if (target->module != ctx->mod) {
5715 /* foreign deviation */
5716 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt);
5717
5718 /* check that the value matches */
5719 orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5720 if (strcmp(orig_dflt, dflt)) {
5721 goto error;
5722 }
5723 if (dyn) {
5724 free((char *)orig_dflt);
5725 }
5726
5727 /* remove the default specification */
5728 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5729 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5730 free(leaf->dflt);
5731 leaf->dflt = NULL;
5732 } else {
5733 /* local deviation */
5734 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt);
5735
5736 /* find the incomplete default */
5737 orig_dflt = NULL;
5738 for (i = 0; i < ctx->dflts.count; ++i) {
5739 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
5740 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5741 break;
5742 }
5743 }
5744 LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT);
5745
5746 /* check that the value matches */
5747 if (strcmp(orig_dflt, dflt)) {
5748 goto error;
5749 }
5750
5751 /* update the list of incomplete default values */
5752 lysc_incomplete_dflt_remove(ctx, target);
5753 }
5754
5755 target->flags &= ~LYS_SET_DFLT;
5756 return LY_SUCCESS;
5757
5758error:
5759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5760 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5761 dflt, orig_dflt);
5762 if (dyn) {
5763 free((char *)orig_dflt);
5764 }
5765cleanup:
5766 return LY_EVALID;
5767}
5768
5769static LY_ERR
5770lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts)
5771{
5772 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
5773 int dyn = 0, found;
5774 const char *orig_dflt, **orig_dflts;
5775 uint32_t i;
5776 LY_ARRAY_COUNT_TYPE x, y;
5777
5778 if (target->module != ctx->mod) {
5779 /* foreign deviation */
5780 DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]);
5781 LY_ARRAY_FOR(dflts, x) {
5782 found = 0;
5783 LY_ARRAY_FOR(llist->dflts, y) {
5784 orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn);
5785 if (!strcmp(orig_dflt, dflts[x])) {
5786 if (dyn) {
5787 free((char *)orig_dflt);
5788 }
5789 found = 1;
5790 break;
5791 }
5792 if (dyn) {
5793 free((char *)orig_dflt);
5794 }
5795 }
5796 if (!found) {
5797 goto error;
5798 }
5799
5800 /* update compiled default values */
5801 LY_ARRAY_DECREMENT(llist->dflts);
5802 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
5803 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
5804 free(llist->dflts[y]);
5805 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
5806 }
5807 if (!LY_ARRAY_COUNT(llist->dflts)) {
5808 LY_ARRAY_FREE(llist->dflts);
5809 llist->dflts = NULL;
5810 llist->flags &= ~LYS_SET_DFLT;
5811 }
5812 } else {
5813 /* local deviation */
5814 orig_dflt = NULL;
5815 orig_dflts = NULL;
5816 for (i = 0; i < ctx->dflts.count; ++i) {
5817 if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) {
5818 orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt;
5819 orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts;
5820 break;
5821 }
5822 }
5823 LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT);
5824
5825 if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) {
5826 /* TODO it is not currently possible to remove just one default value from incomplete defaults array */
5827 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5828 "Local deviation deleting leaf-list defaults is not supported.");
5829 return LY_EVALID;
5830 }
5831
5832 LY_ARRAY_FOR(dflts, x) {
5833 found = 0;
5834 if (orig_dflts) {
5835 LY_ARRAY_FOR(orig_dflts, y) {
5836 if (!strcmp(orig_dflts[y], dflts[x])) {
5837 found = 1;
5838 break;
5839 }
5840 }
5841 } else if (!strcmp(orig_dflt, dflts[x])) {
5842 found = 1;
5843 }
5844 if (!found) {
5845 goto error;
5846 }
5847
5848 /* update the list of incomplete default values */
5849 lysc_incomplete_dflt_remove(ctx, target);
5850 }
5851
5852 llist->flags &= ~LYS_SET_DFLT;
5853 }
5854
5855 return LY_SUCCESS;
5856
5857error:
5858 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
5859 "which does not match any of the target's property values.", dflts[x]);
5860cleanup:
5861 return LY_EVALID;
5862}
5863
Michal Vaskoccc062a2020-08-13 08:34:50 +02005864/**
5865 * @brief Apply deviate delete.
5866 *
5867 * @param[in] ctx Compile context.
5868 * @param[in] target Deviation target.
5869 * @param[in] dev_flags Internal deviation flags.
5870 * @param[in] d Deviate delete to apply.
5871 * @return LY_ERR value.
5872 */
5873static LY_ERR
5874lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_del *d)
5875{
5876 LY_ERR ret = LY_EVALID;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005877 struct lysc_node_list *list = (struct lysc_node_list *)target;
5878 LY_ARRAY_COUNT_TYPE x, y, z;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005879 size_t prefix_len, name_len;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005880 const char *prefix, *name, *nodeid;
Michal Vaskoccc062a2020-08-13 08:34:50 +02005881 struct lys_module *mod;
5882
5883#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5884 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5885 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5886 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5887 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5888 } \
5889 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5891 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5892 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5893 goto cleanup; \
5894 } \
5895 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5896 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5897 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5898 &((TYPE)target)->ARRAY_TRG[y + 1], \
5899 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5900 } \
5901 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5902 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5903 ((TYPE)target)->ARRAY_TRG = NULL; \
5904 }
5905
5906 /* [units-stmt] */
5907 if (d->units) {
5908 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5909 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d->units);
5910 if (strcmp(((struct lysc_node_leaf*)target)->units, d->units)) {
5911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5912 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5913 d->units, ((struct lysc_node_leaf*)target)->units);
5914 goto cleanup;
5915 }
5916 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)target)->units);
5917 ((struct lysc_node_leaf*)target)->units = NULL;
5918 }
5919
5920 /* *must-stmt */
5921 if (d->musts) {
5922 switch (target->nodetype) {
5923 case LYS_CONTAINER:
5924 case LYS_LIST:
5925 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5926 break;
5927 case LYS_LEAF:
5928 case LYS_LEAFLIST:
5929 case LYS_ANYDATA:
5930 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5931 break;
5932 case LYS_NOTIF:
5933 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5934 break;
5935 case LYS_RPC:
5936 case LYS_ACTION:
5937 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5938 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5939 break;
5940 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5941 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5942 break;
5943 }
5944 /* fall through */
5945 default:
5946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5947 lys_nodetype2str(target->nodetype), "delete", "must");
5948 goto cleanup;
5949 }
5950 }
5951
5952 /* *unique-stmt */
5953 if (d->uniques) {
5954 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5955 LY_ARRAY_FOR(d->uniques, x) {
5956 LY_ARRAY_FOR(list->uniques, z) {
5957 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
5958 nodeid = strpbrk(name, " \t\n");
5959 if (nodeid) {
5960 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
5961 break;
5962 }
5963 while (isspace(*nodeid)) {
5964 ++nodeid;
5965 }
5966 } else {
5967 if (strcmp(name, list->uniques[z][y]->name)) {
5968 break;
5969 }
5970 }
5971 }
5972 if (!name) {
5973 /* complete match - remove the unique */
5974 LY_ARRAY_DECREMENT(list->uniques);
5975 LY_ARRAY_FREE(list->uniques[z]);
5976 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
5977 --z;
5978 break;
5979 }
5980 }
5981 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
5982 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5983 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5984 d->uniques[x]);
5985 goto cleanup;
5986 }
5987 }
5988 if (!LY_ARRAY_COUNT(list->uniques)) {
5989 LY_ARRAY_FREE(list->uniques);
5990 list->uniques = NULL;
5991 }
5992 }
5993
5994 /* *default-stmt */
5995 if (d->dflts) {
5996 switch (target->nodetype) {
5997 case LYS_LEAF:
5998 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
Michal Vaskoba99a3e2020-08-18 15:50:05 +02005999 LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006000 break;
6001 case LYS_LEAFLIST:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006002 LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006003 break;
6004 case LYS_CHOICE:
6005 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
6006 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d->dflts[0]);
6007 nodeid = d->dflts[0];
6008 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6009 if (prefix) {
6010 /* use module prefixes from the deviation module to match the module of the default case */
6011 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6012 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
6013 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6014 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6015 goto cleanup;
6016 }
6017 if (mod != ((struct lysc_node_choice*)target)->dflt->module) {
6018 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
6019 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6020 "The prefix does not match the default case's module.", d->dflts[0]);
6021 goto cleanup;
6022 }
6023 }
6024 /* else {
6025 * strictly, the default prefix would point to the deviation module, but the value should actually
6026 * match the default string in the original module (usually unprefixed), so in this case we do not check
6027 * the module of the default case, just matching its name */
6028 if (strcmp(name, ((struct lysc_node_choice*)target)->dflt->name)) {
6029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6030 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6031 d->dflts[0], ((struct lysc_node_choice*)target)->dflt->name);
6032 goto cleanup;
6033 }
6034 ((struct lysc_node_choice*)target)->dflt->flags &= ~LYS_SET_DFLT;
6035 ((struct lysc_node_choice*)target)->dflt = NULL;
6036 break;
6037 default:
6038 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6039 lys_nodetype2str(target->nodetype), "delete", "default");
6040 goto cleanup;
6041 }
6042 }
6043
6044 ret = LY_SUCCESS;
6045
6046cleanup:
6047 return ret;
6048}
6049
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006050static LY_ERR
6051lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target)
6052{
6053 LY_ERR ret;
6054 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6055 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6056 struct ly_err_item *err = NULL;
6057 LY_ARRAY_COUNT_TYPE x;
6058 const char *dflt;
6059 int dyn;
6060
6061 if (target->module != ctx->mod) {
6062 /* foreign deviation */
6063 if (target->nodetype == LYS_LEAF) {
6064 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn);
6065 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6066 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6067
6068 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6069 LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err);
6070 if (dyn) {
6071 free((char *)dflt);
6072 }
6073 if (err) {
6074 ly_err_print(err);
6075 ctx->path[0] = '\0';
6076 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6078 "Invalid default - value does not fit the type (%s).", err->msg);
6079 ly_err_free(err);
6080 }
6081 LY_CHECK_RET(ret);
6082
6083 ++leaf->dflt->realtype->refcount;
6084 } else { /* LY_LEAFLIST */
6085 LY_ARRAY_FOR(llist->dflts, x) {
6086 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn);
6087 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6088 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6089
6090 ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
6091 LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err);
6092 if (dyn) {
6093 free((char *)dflt);
6094 }
6095 if (err) {
6096 ly_err_print(err);
6097 ctx->path[0] = '\0';
6098 lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6099 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6100 "Invalid default - value does not fit the type (%s).", err->msg);
6101 ly_err_free(err);
6102 }
6103 LY_CHECK_RET(ret);
6104
6105 ++llist->dflts[x]->realtype->refcount;
6106 }
6107 }
6108 } else {
6109 /* local deviation */
6110
6111 /* these default were not compiled yet, so they will use the new type automatically */
6112 }
6113
6114 return LY_SUCCESS;
6115}
6116
Michal Vaskoccc062a2020-08-13 08:34:50 +02006117/**
6118 * @brief Apply deviate replace.
6119 *
6120 * @param[in] ctx Compile context.
6121 * @param[in] target Deviation target.
6122 * @param[in] d Deviate replace to apply.
Michal Vaskoccc062a2020-08-13 08:34:50 +02006123 * @return LY_ERR value.
6124 */
6125static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006126lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d)
Michal Vaskoccc062a2020-08-13 08:34:50 +02006127{
6128 LY_ERR ret = LY_EVALID;
6129 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6130 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6131 LY_ARRAY_COUNT_TYPE x;
6132
6133#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6134 if (!(((TYPE)target)->MEMBER COND)) { \
6135 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6136 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6137 goto cleanup; \
6138 }
6139
6140 /* [type-stmt] */
6141 if (d->type) {
6142 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6143 /* type is mandatory, so checking for its presence is not necessary */
6144 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6145
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006146 /* remove only default value inherited from the type */
6147 if (!(target->flags & LYS_SET_DFLT)) {
6148 if (target->module != ctx->mod) {
6149 /* foreign deviation - the target has default from the previous type, remove it */
6150 if (target->nodetype == LYS_LEAF) {
6151 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6152 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6153 free(leaf->dflt);
6154 leaf->dflt = NULL;
6155 } else { /* LYS_LEAFLIST */
6156 LY_ARRAY_FOR(llist->dflts, x) {
6157 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6158 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6159 free(llist->dflts[x]);
6160 }
6161 LY_ARRAY_FREE(llist->dflts);
6162 llist->dflts = NULL;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006163 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006164 } else {
6165 /* local deviation */
6166 lysc_incomplete_dflt_remove(ctx, target);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006167 }
6168 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006169
6170 LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf));
6171
6172 if (target->flags & LYS_SET_DFLT) {
6173 /* the term default value(s) needs to be recompiled */
6174 LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006175 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006176 }
6177
6178 /* [units-stmt] */
6179 if (d->units) {
6180 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6181 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6182 units, "replacing", "units", d->units);
6183
6184 lydict_remove(ctx->ctx, leaf->units);
6185 DUP_STRING(ctx->ctx, d->units, leaf->units);
6186 }
6187
6188 /* [default-stmt] */
6189 if (d->dflt) {
6190 switch (target->nodetype) {
6191 case LYS_LEAF:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006192 if (target->module != ctx->mod) {
6193 /* foreign deviation */
6194 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing",
6195 "default", d->dflt);
6196
6197 /* remove the default specification */
6198 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6199 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6200 free(leaf->dflt);
6201 leaf->dflt = NULL;
6202 } else {
6203 /* local deviation */
6204 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing",
6205 "default", d->dflt);
6206 assert(!leaf->dflt);
6207 }
6208
6209 /* store the new default value */
6210 LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def));
Michal Vaskoccc062a2020-08-13 08:34:50 +02006211 break;
6212 case LYS_CHOICE:
6213 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6214 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6215 goto cleanup;
6216 }
6217 break;
6218 default:
6219 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6220 lys_nodetype2str(target->nodetype), "replace", "default");
6221 goto cleanup;
6222 }
6223 }
6224
6225 /* [config-stmt] */
6226 if (d->flags & LYS_CONFIG_MASK) {
6227 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6228 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6229 lys_nodetype2str(target->nodetype), "replace", "config");
6230 goto cleanup;
6231 }
6232 if (!(target->flags & LYS_SET_CONFIG)) {
6233 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6234 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6235 goto cleanup;
6236 }
6237 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6238 }
6239
6240 /* [mandatory-stmt] */
6241 if (d->flags & LYS_MAND_MASK) {
6242 if (!(target->flags & LYS_MAND_MASK)) {
6243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6244 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6245 goto cleanup;
6246 }
6247 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6248 }
6249
6250 /* [min-elements-stmt] */
6251 if (d->flags & LYS_SET_MIN) {
6252 if (target->nodetype == LYS_LEAFLIST) {
6253 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6254 /* change value */
6255 ((struct lysc_node_leaflist *)target)->min = d->min;
6256 } else if (target->nodetype == LYS_LIST) {
6257 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6258 /* change value */
6259 ((struct lysc_node_list *)target)->min = d->min;
6260 } else {
6261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6262 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6263 goto cleanup;
6264 }
6265 if (d->min) {
6266 target->flags |= LYS_MAND_TRUE;
6267 }
6268 }
6269
6270 /* [max-elements-stmt] */
6271 if (d->flags & LYS_SET_MAX) {
6272 if (target->nodetype == LYS_LEAFLIST) {
6273 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6274 /* change value */
6275 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6276 } else if (target->nodetype == LYS_LIST) {
6277 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6278 /* change value */
6279 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6280 } else {
6281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6282 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6283 goto cleanup;
6284 }
6285 }
6286
6287 ret = LY_SUCCESS;
6288
6289cleanup:
6290 return ret;
6291}
6292
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006293/**
6294 * @brief Apply all deviations of one target node.
6295 *
6296 * @param[in] ctx Compile context.
6297 * @param[in] dev Deviation structure to apply.
6298 * @return LY_ERR value.
6299 */
6300static LY_ERR
6301lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006302{
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006303 LY_ERR ret = LY_EVALID;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006304 struct lysc_node *target = dev->target;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006305 struct lysc_action *rpcs;
6306 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006307 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006308 LY_ARRAY_COUNT_TYPE v, x;
Radek Krejci551b12c2019-02-19 16:11:21 +01006309 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006310
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006311 lysc_update_path(ctx, NULL, dev->nodeid);
6312
6313 /* not-supported */
6314 if (dev->not_supported) {
6315 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
6316 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
6317 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6318 }
6319
6320#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6321 if (target->parent) { \
6322 ARRAY = (TYPE*)GETFUNC(target->parent); \
6323 } else { \
6324 ARRAY = target->module->compiled->ARRAY; \
6325 } \
6326 LY_ARRAY_FOR(ARRAY, x) { \
6327 if (&ARRAY[x] == (TYPE*)target) { break; } \
6328 } \
6329 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6330 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6331 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6332 LY_ARRAY_DECREMENT(ARRAY); \
6333 }
6334
6335 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6336 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6337 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006338 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6339 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6340 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6341 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006342 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6343 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006344 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6345 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6346 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6347 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006348 } else {
6349 /* remove RPC/action */
6350 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6351 }
6352 } else if (target->nodetype == LYS_NOTIF) {
6353 /* remove Notification */
6354 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6355 } else {
6356 /* remove the target node */
6357 lysc_disconnect(target);
6358 lysc_node_free(ctx->ctx, target);
6359 }
6360
6361 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6362 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6363 return LY_SUCCESS;
6364 }
6365
6366 /* list of deviates (not-supported is not present in the list) */
6367 LY_ARRAY_FOR(dev->deviates, v) {
6368 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006369 switch (d->mod) {
6370 case LYS_DEV_ADD:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006371 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006372 break;
6373 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006374 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006375 break;
6376 case LYS_DEV_REPLACE:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006377 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006378 break;
6379 default:
6380 LOGINT(ctx->ctx);
6381 goto cleanup;
6382 }
6383 }
6384
6385 /* final check when all deviations of a single target node are applied */
6386
6387 /* check min-max compatibility */
6388 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006389 min = ((struct lysc_node_leaflist *)target)->min;
6390 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006391 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006392 min = ((struct lysc_node_list *)target)->min;
6393 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006394 } else {
6395 min = max = 0;
6396 }
6397 if (min > max) {
6398 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6399 "after deviation: min value %u is bigger than max value %u.", min, max);
6400 goto cleanup;
6401 }
6402
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006403 /* check mandatory - default compatibility */
6404 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6405 && (target->flags & LYS_MAND_TRUE)) {
6406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6407 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6408 goto cleanup;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006409 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice* )target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006410 && (target->flags & LYS_MAND_TRUE)) {
6411 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6412 goto cleanup;
6413 }
6414 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6415 /* mandatory node under a default case */
6416 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6417 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6418 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6419 goto cleanup;
6420 }
6421
6422 /* success */
6423 ret = LY_SUCCESS;
6424
6425cleanup:
6426 lysc_update_path(ctx, NULL, NULL);
6427 return ret;
6428}
6429
6430LY_ERR
6431lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6432{
6433 LY_ERR ret = LY_EVALID;
6434 struct ly_set devs_p = {0};
6435 struct ly_set targets = {0};
6436 struct lysc_node *target; /* target target of the deviation */
6437 struct lysp_deviation *dev;
6438 struct lysp_deviate *d, **dp_new;
6439 LY_ARRAY_COUNT_TYPE u, v;
6440 struct lysc_deviation **devs = NULL;
6441 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006442 uint16_t flags;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006443 int i;
Radek Krejciccd20f12019-02-15 14:12:27 +01006444
6445 /* get all deviations from the module and all its submodules ... */
6446 LY_ARRAY_FOR(mod_p->deviations, u) {
6447 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
6448 }
6449 LY_ARRAY_FOR(mod_p->includes, v) {
6450 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
6451 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
6452 }
6453 }
6454 if (!devs_p.count) {
6455 /* nothing to do */
6456 return LY_SUCCESS;
6457 }
6458
Radek Krejci327de162019-06-14 12:52:07 +02006459 lysc_update_path(ctx, NULL, "{deviation}");
6460
Radek Krejciccd20f12019-02-15 14:12:27 +01006461 /* ... and group them by the target node */
6462 devs = calloc(devs_p.count, sizeof *devs);
6463 for (u = 0; u < devs_p.count; ++u) {
6464 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006465 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006466
6467 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006468 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
6469 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006470 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006471 /* move the target pointer to input/output to make them different from the action and
6472 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6473 * back to the RPC/action node due to a better compatibility and decision code in this function.
6474 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6475 if (flags & LYSC_OPT_RPC_INPUT) {
6476 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
6477 flags |= LYSC_OPT_INTERNAL;
6478 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
6479 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
6480 flags |= LYSC_OPT_INTERNAL;
6481 }
6482 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006483 /* insert into the set of targets with duplicity detection */
6484 i = ly_set_add(&targets, target, 0);
6485 if (!devs[i]) {
6486 /* new record */
6487 devs[i] = calloc(1, sizeof **devs);
6488 devs[i]->target = target;
6489 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006490 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006491 }
6492 /* add deviates into the deviation's list of deviates */
6493 for (d = dev->deviates; d; d = d->next) {
6494 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
6495 *dp_new = d;
6496 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
6497 devs[i]->not_supported = 1;
6498 }
6499 }
Radek Krejci327de162019-06-14 12:52:07 +02006500
6501 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006502 }
6503
Radek Krejciccd20f12019-02-15 14:12:27 +01006504 /* apply deviations */
6505 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006506 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6507 /* fix the target pointer in case of RPC's/action's input/output */
6508 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006509 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006510 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006511 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006512 }
6513 }
6514
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006515 /* remember target module (the target node may be removed) */
6516 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006517
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006518 /* apply the deviation */
6519 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006520
Michal Vaskoe6143202020-07-03 13:02:08 +02006521 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006522 i = 0;
6523 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6524 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
6525 i = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006526 break;
6527 }
6528 }
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006529 if (!i) {
6530 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006531 *dev_mod = mod_p->mod;
6532 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006533 }
6534
Radek Krejci327de162019-06-14 12:52:07 +02006535 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006536 ret = LY_SUCCESS;
6537
6538cleanup:
6539 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6540 LY_ARRAY_FREE(devs[u]->deviates);
6541 free(devs[u]);
6542 }
6543 free(devs);
6544 ly_set_erase(&targets, NULL);
6545 ly_set_erase(&devs_p, NULL);
6546
6547 return ret;
6548}
6549
Radek Krejcib56c7502019-02-13 14:19:54 +01006550/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006551 * @brief Compile the given YANG submodule into the main module.
6552 * @param[in] ctx Compile context
6553 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006554 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6555 */
6556LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006557lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006558{
6559 unsigned int u;
6560 LY_ERR ret = LY_SUCCESS;
6561 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006562 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006563 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006564 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006565
Michal Vasko33ff9422020-07-03 09:50:39 +02006566 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006567 /* features are compiled directly into the compiled module structure,
6568 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6569 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006570 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6571 LY_CHECK_GOTO(ret, error);
6572 }
Radek Krejci0af46292019-01-11 16:02:31 +01006573
Michal Vasko33ff9422020-07-03 09:50:39 +02006574 if (!mainmod->mod->dis_identities) {
6575 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6576 LY_CHECK_GOTO(ret, error);
6577 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006578
Radek Krejci474f9b82019-07-24 11:36:37 +02006579 /* data nodes */
6580 LY_LIST_FOR(submod->data, node_p) {
6581 ret = lys_compile_node(ctx, node_p, NULL, 0);
6582 LY_CHECK_GOTO(ret, error);
6583 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006584
Radek Krejciec4da802019-05-02 13:02:41 +02006585 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6586 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006587
Radek Krejcid05cbd92018-12-05 14:26:40 +01006588error:
6589 return ret;
6590}
6591
Radek Krejci335332a2019-09-05 13:03:35 +02006592static void *
6593lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6594{
6595 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6596 if (substmts[u].stmt == stmt) {
6597 return substmts[u].storage;
6598 }
6599 }
6600 return NULL;
6601}
6602
6603LY_ERR
6604lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6605{
6606 LY_ERR ret = LY_EVALID, r;
6607 unsigned int u;
6608 struct lysp_stmt *stmt;
6609 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006610
6611 /* check for invalid substatements */
6612 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006613 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6614 continue;
6615 }
Radek Krejci335332a2019-09-05 13:03:35 +02006616 for (u = 0; substmts[u].stmt; ++u) {
6617 if (substmts[u].stmt == stmt->kw) {
6618 break;
6619 }
6620 }
6621 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6623 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006624 goto cleanup;
6625 }
Radek Krejci335332a2019-09-05 13:03:35 +02006626 }
6627
Radek Krejciad5963b2019-09-06 16:03:05 +02006628 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6629
Radek Krejci335332a2019-09-05 13:03:35 +02006630 /* keep order of the processing the same as the order in the defined substmts,
6631 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6632 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006633 int stmt_present = 0;
6634
Radek Krejci335332a2019-09-05 13:03:35 +02006635 for (stmt = ext->child; stmt; stmt = stmt->next) {
6636 if (substmts[u].stmt != stmt->kw) {
6637 continue;
6638 }
6639
Radek Krejciad5963b2019-09-06 16:03:05 +02006640 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006641 if (substmts[u].storage) {
6642 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006643 case LY_STMT_STATUS:
6644 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6645 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6646 break;
6647 case LY_STMT_UNITS: {
6648 const char **units;
6649
6650 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6651 /* single item */
6652 if (*((const char **)substmts[u].storage)) {
6653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6654 goto cleanup;
6655 }
6656 units = (const char **)substmts[u].storage;
6657 } else {
6658 /* sized array */
6659 const char ***units_array = (const char ***)substmts[u].storage;
6660 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6661 }
6662 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6663 break;
6664 }
Radek Krejci335332a2019-09-05 13:03:35 +02006665 case LY_STMT_TYPE: {
6666 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6667 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6668
6669 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6670 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006671 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6673 goto cleanup;
6674 }
6675 compiled = substmts[u].storage;
6676 } else {
6677 /* sized array */
6678 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6679 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6680 compiled = (void*)type;
6681 }
6682
Radek Krejciad5963b2019-09-06 16:03:05 +02006683 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006684 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6685 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02006686 units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02006687 lysp_type_free(ctx->ctx, parsed);
6688 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006689 break;
6690 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006691 case LY_STMT_IF_FEATURE: {
6692 struct lysc_iffeature *iff = NULL;
6693
6694 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6695 /* single item */
6696 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6698 goto cleanup;
6699 }
6700 iff = (struct lysc_iffeature*)substmts[u].storage;
6701 } else {
6702 /* sized array */
6703 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6704 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6705 }
6706 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6707 break;
6708 }
6709 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6710 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006711 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006712 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.",
6713 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006714 goto cleanup;
6715 }
6716 }
Radek Krejci335332a2019-09-05 13:03:35 +02006717 }
Radek Krejci335332a2019-09-05 13:03:35 +02006718
Radek Krejciad5963b2019-09-06 16:03:05 +02006719 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6721 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6722 goto cleanup;
6723 }
Radek Krejci335332a2019-09-05 13:03:35 +02006724 }
6725
6726 ret = LY_SUCCESS;
6727
6728cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006729 return ret;
6730}
6731
Michal Vasko175012e2019-11-06 15:49:14 +01006732/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006733 * @brief Check when for cyclic dependencies.
6734 * @param[in] set Set with all the referenced nodes.
6735 * @param[in] node Node whose "when" referenced nodes are in @p set.
6736 * @return LY_ERR value
6737 */
6738static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006739lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006740{
6741 struct lyxp_set tmp_set;
6742 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006743 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006744 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006745 int idx;
6746 struct lysc_when *when;
6747 LY_ERR ret = LY_SUCCESS;
6748
6749 memset(&tmp_set, 0, sizeof tmp_set);
6750
6751 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006752 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006753 xp_scnode = &set->val.scnodes[i];
6754
Michal Vasko5c4e5892019-11-14 12:31:38 +01006755 if (xp_scnode->in_ctx != -1) {
6756 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006757 xp_scnode->in_ctx = 1;
6758 }
6759 }
6760
6761 for (i = 0; i < set->used; ++i) {
6762 xp_scnode = &set->val.scnodes[i];
6763 if (xp_scnode->in_ctx != 1) {
6764 /* already checked */
6765 continue;
6766 }
6767
Michal Vasko1bf09392020-03-27 12:38:10 +01006768 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006769 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006770 /* no when to check */
6771 xp_scnode->in_ctx = 0;
6772 continue;
6773 }
6774
6775 node = xp_scnode->scnode;
6776 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006777 LY_ARRAY_FOR(node->when, u) {
6778 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006779 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006780 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6781 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006782 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006783 goto cleanup;
6784 }
6785
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006786 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006787 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006788 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006789 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006790 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 +01006791 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006792 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 +01006793 ret = LY_EVALID;
6794 goto cleanup;
6795 }
6796
6797 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006798 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006799 } else {
6800 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006801 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006802 }
6803 }
6804
6805 /* merge this set into the global when set */
6806 lyxp_set_scnode_merge(set, &tmp_set);
6807 }
6808
6809 /* check when of non-data parents as well */
6810 node = node->parent;
6811 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6812
Michal Vasko251f56e2019-11-14 16:06:47 +01006813 /* this node when was checked (xp_scnode could have been reallocd) */
6814 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006815 }
6816
6817cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006818 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006819 return ret;
6820}
6821
6822/**
Michal Vasko175012e2019-11-06 15:49:14 +01006823 * @brief Check when/must expressions of a node on a compiled schema tree.
6824 * @param[in] ctx Compile context.
6825 * @param[in] node Node to check.
6826 * @return LY_ERR value
6827 */
6828static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006829lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006830{
Michal Vasko175012e2019-11-06 15:49:14 +01006831 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006832 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006833 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006834 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006835 struct lysc_when **when = NULL;
6836 struct lysc_must *musts = NULL;
6837 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006838 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006839
6840 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006841 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006842 if (node->flags & LYS_CONFIG_R) {
6843 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6844 if (op) {
6845 /* we are actually in output */
6846 opts = LYXP_SCNODE_OUTPUT;
6847 }
6848 }
Michal Vasko175012e2019-11-06 15:49:14 +01006849
6850 switch (node->nodetype) {
6851 case LYS_CONTAINER:
6852 when = ((struct lysc_node_container *)node)->when;
6853 musts = ((struct lysc_node_container *)node)->musts;
6854 break;
6855 case LYS_CHOICE:
6856 when = ((struct lysc_node_choice *)node)->when;
6857 break;
6858 case LYS_LEAF:
6859 when = ((struct lysc_node_leaf *)node)->when;
6860 musts = ((struct lysc_node_leaf *)node)->musts;
6861 break;
6862 case LYS_LEAFLIST:
6863 when = ((struct lysc_node_leaflist *)node)->when;
6864 musts = ((struct lysc_node_leaflist *)node)->musts;
6865 break;
6866 case LYS_LIST:
6867 when = ((struct lysc_node_list *)node)->when;
6868 musts = ((struct lysc_node_list *)node)->musts;
6869 break;
6870 case LYS_ANYXML:
6871 case LYS_ANYDATA:
6872 when = ((struct lysc_node_anydata *)node)->when;
6873 musts = ((struct lysc_node_anydata *)node)->musts;
6874 break;
6875 case LYS_CASE:
6876 when = ((struct lysc_node_case *)node)->when;
6877 break;
6878 case LYS_NOTIF:
6879 musts = ((struct lysc_notif *)node)->musts;
6880 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006881 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006882 case LYS_ACTION:
6883 /* first process input musts */
6884 musts = ((struct lysc_action *)node)->input.musts;
6885 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006886 default:
6887 /* nothing to check */
6888 break;
6889 }
6890
Michal Vasko175012e2019-11-06 15:49:14 +01006891 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006892 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006893 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 +02006894 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006895 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006896 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006897 goto cleanup;
6898 }
6899
Michal Vaskodc052f32019-11-07 11:11:38 +01006900 ctx->path[0] = '\0';
6901 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006902 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006903 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006904 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6905 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006906
Michal Vaskoecd62de2019-11-13 12:35:11 +01006907 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006908 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006909 schema->name);
6910 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006911
6912 /* check dummy node accessing */
6913 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006914 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006915 ret = LY_EVALID;
6916 goto cleanup;
6917 }
Michal Vasko175012e2019-11-06 15:49:14 +01006918 }
6919 }
6920
Michal Vaskoecd62de2019-11-13 12:35:11 +01006921 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006922 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006923 LY_CHECK_GOTO(ret, cleanup);
6924
Michal Vaskod3678892020-05-21 10:06:58 +02006925 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006926 }
6927
Michal Vasko5d8756a2019-11-07 15:21:00 +01006928check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006929 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006930 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006931 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 +01006932 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006933 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006934 goto cleanup;
6935 }
6936
Michal Vaskodc052f32019-11-07 11:11:38 +01006937 ctx->path[0] = '\0';
6938 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006939 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006940 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006941 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006942 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006943 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6944 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006945 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006946 }
6947 }
6948
Michal Vaskod3678892020-05-21 10:06:58 +02006949 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006950 }
6951
Michal Vasko1bf09392020-03-27 12:38:10 +01006952 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006953 /* now check output musts */
6954 input_done = 1;
6955 musts = ((struct lysc_action *)node)->output.musts;
6956 opts = LYXP_SCNODE_OUTPUT;
6957 goto check_musts;
6958 }
6959
Michal Vasko175012e2019-11-06 15:49:14 +01006960cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006961 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006962 return ret;
6963}
6964
Michal Vasko8d544252020-03-02 10:19:52 +01006965static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006966lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6967{
Michal Vasko6b26e742020-07-17 15:02:10 +02006968 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02006969 struct ly_path *p;
6970 struct lysc_type *type;
6971
6972 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6973
6974 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006975 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6976 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006977 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006978
6979 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006980 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02006981 ly_path_free(node->module->ctx, p);
6982
6983 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6984 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6985 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6986 lref->path->expr, lys_nodetype2str(target->nodetype));
6987 return LY_EVALID;
6988 }
6989
6990 /* check status */
6991 ctx->path[0] = '\0';
6992 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6993 ctx->path_len = strlen(ctx->path);
6994 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6995 return LY_EVALID;
6996 }
6997 ctx->path_len = 1;
6998 ctx->path[1] = '\0';
6999
7000 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007001 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007002 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007003 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007004 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7005 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7006 return LY_EVALID;
7007 }
7008 }
7009
7010 /* store the target's type and check for circular chain of leafrefs */
7011 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7012 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7013 if (type == (struct lysc_type *)lref) {
7014 /* circular chain detected */
7015 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7016 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7017 return LY_EVALID;
7018 }
7019 }
7020
7021 /* check if leafref and its target are under common if-features */
7022 if (lys_compile_leafref_features_validate(node, target)) {
7023 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7024 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7025 " features applicable to the leafref itself.", lref->path->expr);
7026 return LY_EVALID;
7027 }
7028
7029 return LY_SUCCESS;
7030}
7031
7032static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007033lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7034{
7035 struct lysc_ext_instance *ext;
7036 struct lysp_ext_instance *ext_p = NULL;
7037 struct lysp_stmt *stmt;
7038 const struct lys_module *ext_mod;
7039 LY_ERR ret = LY_SUCCESS;
7040
7041 /* create the parsed extension instance manually */
7042 ext_p = calloc(1, sizeof *ext_p);
7043 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
7044 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
7045 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
7046 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7047 ext_p->insubstmt_index = 0;
7048
7049 stmt = calloc(1, sizeof *ext_p->child);
7050 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
7051 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
7052 stmt->kw = LY_STMT_TYPE;
7053 ext_p->child = stmt;
7054
7055 /* allocate new extension instance */
7056 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7057
7058 /* manually get extension definition module */
7059 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7060
7061 /* compile the extension instance */
7062 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7063
7064cleanup:
7065 lysp_ext_instance_free(ctx->ctx, ext_p);
7066 free(ext_p);
7067 return ret;
7068}
7069
Michal Vasko004d3152020-06-11 19:59:22 +02007070static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007071lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
7072 const struct lys_module *dflt_mod, struct lyd_value *storage)
7073{
7074 LY_ERR ret;
7075 struct ly_err_item *err = NULL;
7076
7077 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7078 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7079 if (err) {
7080 ly_err_print(err);
7081 ctx->path[0] = '\0';
7082 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7084 "Invalid default - value does not fit the type (%s).", err->msg);
7085 ly_err_free(err);
7086 }
7087 if (!ret) {
7088 ++storage->realtype->refcount;
7089 return LY_SUCCESS;
7090 }
7091 return ret;
7092}
7093
7094static LY_ERR
7095lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt,
7096 const struct lys_module *dflt_mod)
7097{
7098 LY_ERR ret;
7099
7100 assert(!leaf->dflt);
7101
7102 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7103 /* ignore default values for keys and mandatory leaves */
7104 return LY_SUCCESS;
7105 }
7106
7107 /* allocate the default value */
7108 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7109 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7110
7111 /* store the default value */
7112 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt);
7113 if (ret) {
7114 free(leaf->dflt);
7115 leaf->dflt = NULL;
7116 }
7117
7118 return ret;
7119}
7120
7121static LY_ERR
7122lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char *dflt, const char **dflts,
7123 const struct lys_module *dflt_mod)
7124{
7125 LY_ERR ret;
7126 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7127
7128 assert(dflt || dflts);
7129
7130 if (llist->dflts) {
7131 /* there were already some defaults and we are adding new by deviations */
7132 assert(dflts);
7133 orig_count = LY_ARRAY_COUNT(llist->dflts);
7134 } else {
7135 orig_count = 0;
7136 }
7137
7138 /* allocate new items */
7139 if (dflts) {
7140 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7141 } else {
7142 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7143 }
7144
7145 /* fill each new default value */
7146 if (dflts) {
7147 LY_ARRAY_FOR(dflts, u) {
7148 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
7149 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod,
7150 llist->dflts[orig_count + u]);
7151 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7152 LY_ARRAY_INCREMENT(llist->dflts);
7153 }
7154 } else {
7155 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
7156 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]);
7157 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7158 LY_ARRAY_INCREMENT(llist->dflts);
7159 }
7160
7161 /* check default value uniqueness */
7162 if (llist->flags & LYS_CONFIG_W) {
7163 /* configuration data values must be unique - so check the default values */
7164 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7165 for (v = 0; v < u; ++v) {
7166 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
7167 int dynamic = 0;
7168 const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic);
7169
7170 lysc_update_path(ctx, llist->parent, llist->name);
7171 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7172 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
7173 lysc_update_path(ctx, NULL, NULL);
7174 if (dynamic) {
7175 free((char *)val);
7176 }
7177 return LY_EVALID;
7178 }
7179 }
7180 }
7181 }
7182
7183 return LY_SUCCESS;
7184}
7185
7186static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007187lys_compile_unres(struct lysc_ctx *ctx)
7188{
7189 struct lysc_node *node;
7190 struct lysc_type *type, *typeiter;
7191 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007192 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007193 uint32_t i;
7194
7195 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7196 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7197 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7198 for (i = 0; i < ctx->leafrefs.count; ++i) {
7199 node = ctx->leafrefs.objs[i];
7200 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7201 type = ((struct lysc_node_leaf *)node)->type;
7202 if (type->basetype == LY_TYPE_LEAFREF) {
7203 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7204 } else if (type->basetype == LY_TYPE_UNION) {
7205 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7206 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7207 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7208 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7209 }
7210 }
7211 }
7212 }
7213 for (i = 0; i < ctx->leafrefs.count; ++i) {
7214 /* store pointer to the real type */
7215 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7216 if (type->basetype == LY_TYPE_LEAFREF) {
7217 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7218 typeiter->basetype == LY_TYPE_LEAFREF;
Radek Krejci1e008d22020-08-17 11:37:37 +02007219 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype) {}
Michal Vasko004d3152020-06-11 19:59:22 +02007220 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7221 } else if (type->basetype == LY_TYPE_UNION) {
7222 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7223 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7224 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7225 typeiter->basetype == LY_TYPE_LEAFREF;
Radek Krejci1e008d22020-08-17 11:37:37 +02007226 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype) {}
Michal Vasko004d3152020-06-11 19:59:22 +02007227 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7228 }
7229 }
7230 }
7231 }
7232
7233 /* check xpath */
7234 for (i = 0; i < ctx->xpath.count; ++i) {
7235 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7236 }
7237
7238 /* finish incomplete default values compilation */
7239 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007240 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007241 if (r->leaf->nodetype == LYS_LEAF) {
7242 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod));
7243 } else {
7244 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 +02007245 }
Michal Vasko004d3152020-06-11 19:59:22 +02007246 }
7247
7248 return LY_SUCCESS;
7249}
7250
Radek Krejci19a96102018-11-15 13:38:09 +01007251LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007252lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007253{
7254 struct lysc_ctx ctx = {0};
7255 struct lysc_module *mod_c;
7256 struct lysp_module *sp;
7257 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007258 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007259 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007260 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007261 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007262 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007263 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007264 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007265
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007266 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007267
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007268 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007269 /* just imported modules are not compiled */
7270 return LY_SUCCESS;
7271 }
7272
Radek Krejcia46012b2020-08-12 15:41:04 +02007273 compile_id = ++(*mod)->ctx->module_set_id;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007274 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007275
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007276 ctx.ctx = (*mod)->ctx;
7277 ctx.mod = *mod;
7278 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007279 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007280 ctx.path_len = 1;
7281 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007282
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007283 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7284 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7285 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007286
Michal Vasko7c8439f2020-08-05 13:25:19 +02007287 LY_ARRAY_FOR(sp->imports, u) {
7288 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7289 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01007290 LY_ARRAY_FOR(sp->includes, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02007291 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007292 }
Radek Krejci0935f412019-08-20 16:15:18 +02007293
7294 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007295 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007296 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007297 mod_c->features = (*mod)->dis_features;
7298 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007299 } else {
7300 /* features are compiled directly into the compiled module structure,
7301 * 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 +02007302 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007303 LY_CHECK_GOTO(ret, error);
7304 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007305
Radek Krejci0af46292019-01-11 16:02:31 +01007306 /* finish feature compilation, not only for the main module, but also for the submodules.
7307 * Due to possible forward references, it must be done when all the features (including submodules)
7308 * are present. */
7309 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007310 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007311 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7312 }
Radek Krejci327de162019-06-14 12:52:07 +02007313 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007314 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007315 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007316 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007317 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007318 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7319 }
Radek Krejci327de162019-06-14 12:52:07 +02007320 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007321 }
Radek Krejci327de162019-06-14 12:52:07 +02007322 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007323
Michal Vasko33ff9422020-07-03 09:50:39 +02007324 /* identities, work similarly to features with the precompilation */
7325 if ((*mod)->dis_identities) {
7326 mod_c->identities = (*mod)->dis_identities;
7327 (*mod)->dis_identities = NULL;
7328 } else {
7329 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7330 LY_CHECK_GOTO(ret, error);
7331 }
Radek Krejci19a96102018-11-15 13:38:09 +01007332 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007333 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007334 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007335 lysc_update_path(&ctx, NULL, "{submodule}");
7336 LY_ARRAY_FOR(sp->includes, v) {
7337 if (sp->includes[v].submodule->identities) {
7338 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7339 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7340 LY_CHECK_GOTO(ret, error);
7341 lysc_update_path(&ctx, NULL, NULL);
7342 }
7343 }
Radek Krejci327de162019-06-14 12:52:07 +02007344 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007345
Radek Krejci95710c92019-02-11 15:49:55 +01007346 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007347 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007348 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007349 }
Radek Krejci95710c92019-02-11 15:49:55 +01007350
Radek Krejciec4da802019-05-02 13:02:41 +02007351 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7352 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007353
Radek Krejci95710c92019-02-11 15:49:55 +01007354 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007355 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007356 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007357 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007358 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007359
Radek Krejci474f9b82019-07-24 11:36:37 +02007360 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007361 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007362
Radek Krejci0935f412019-08-20 16:15:18 +02007363 /* extension instances TODO cover extension instances from submodules */
7364 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007365
Michal Vasko004d3152020-06-11 19:59:22 +02007366 /* finish compilation for all unresolved items in the context */
7367 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007368
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007369 /* validate non-instantiated groupings from the parsed schema,
7370 * without it we would accept even the schemas with invalid grouping specification */
7371 ctx.options |= LYSC_OPT_GROUPING;
7372 LY_ARRAY_FOR(sp->groupings, u) {
7373 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007374 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007375 }
7376 }
7377 LY_LIST_FOR(sp->data, node_p) {
7378 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7379 LY_ARRAY_FOR(grps, u) {
7380 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007381 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007382 }
7383 }
7384 }
7385
Radek Krejci474f9b82019-07-24 11:36:37 +02007386 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7387 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7388 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7389 }
7390
Michal Vasko8d544252020-03-02 10:19:52 +01007391#if 0
7392 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7393 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7394 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7395 * the anotation definitions available in the internal schema structure. */
7396 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7397 if (lyp_add_ietf_netconf_annotations(mod)) {
7398 lys_free(mod, NULL, 1, 1);
7399 return NULL;
7400 }
7401 }
7402#endif
7403
7404 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007405 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7406 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007407 }
7408
Radek Krejci1c0c3442019-07-23 16:08:47 +02007409 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007410 ly_set_erase(&ctx.xpath, NULL);
7411 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007412 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007413 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007414 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007415
Radek Krejciec4da802019-05-02 13:02:41 +02007416 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007417 lysp_module_free((*mod)->parsed);
7418 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007419 }
7420
Radek Krejciec4da802019-05-02 13:02:41 +02007421 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007422 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007423 for (i = 0; i < ctx.ctx->list.count; ++i) {
7424 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007425 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007426 m->implemented = 1;
7427 }
7428 }
7429 }
7430
Radek Krejci19a96102018-11-15 13:38:09 +01007431 return LY_SUCCESS;
7432
7433error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007434 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007435 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007436 ly_set_erase(&ctx.xpath, NULL);
7437 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007438 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007439 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007440 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007441 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007442 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007443
Radek Krejcia46012b2020-08-12 15:41:04 +02007444 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7445 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7446 * 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 +02007447 for (i = 0; i < ctx.ctx->list.count; ++i) {
7448 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007449 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007450 /* revert features list to the precompiled state */
7451 lys_feature_precompile_revert(&ctx, m);
7452 /* mark module as imported-only / not-implemented */
7453 m->implemented = 0;
7454 /* free the compiled version of the module */
7455 lysc_module_free(m->compiled, NULL);
7456 m->compiled = NULL;
7457 }
7458 }
7459
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007460 /* remove the module itself from the context and free it */
7461 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7462 lys_module_free(*mod, NULL);
7463 *mod = NULL;
7464
Radek Krejci19a96102018-11-15 13:38:09 +01007465 return ret;
7466}