blob: 076891dce8df1fe4df19abaefea58cb72764b96c [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
Radek Krejcib56c7502019-02-13 14:19:54 +0100151/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200152 * @brief Add record into the compile context's list of incomplete default values.
153 * @param[in] ctx Compile context with the incomplete default values list.
154 * @param[in] context_node Context schema node to store in the record.
155 * @param[in] dflt Incomplete default value to store in the record.
156 * @param[in] dflt_mod Module of the default value definition to store in the record.
157 * @return LY_EMEM in case of memory allocation failure.
158 * @return LY_SUCCESS
159 */
160static LY_ERR
161lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
162{
163 struct lysc_incomplete_dflt *r;
164 r = malloc(sizeof *r);
165 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
166 r->context_node = context_node;
167 r->dflt = dflt;
168 r->dflt_mod = dflt_mod;
169 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
170
171 return LY_SUCCESS;
172}
173
174/**
175 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
176 * @param[in] ctx Compile context with the incomplete default values list.
177 * @param[in] dflt Incomplete default values identifying the record to remove.
178 */
179static void
180lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
181{
182 unsigned int u;
183 struct lysc_incomplete_dflt *r;
184
185 for (u = 0; u < ctx->dflts.count; ++u) {
186 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
187 if (r->dflt == dflt) {
188 free(ctx->dflts.objs[u]);
189 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
190 --ctx->dflts.count;
191 return;
192 }
193 }
194}
195
Radek Krejci0e59c312019-08-15 15:34:15 +0200196void
Radek Krejci327de162019-06-14 12:52:07 +0200197lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
198{
199 int len;
200 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
201
202 if (!name) {
203 /* removing last path segment */
204 if (ctx->path[ctx->path_len - 1] == '}') {
205 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
206 if (ctx->path[ctx->path_len] == '=') {
207 ctx->path[ctx->path_len++] = '}';
208 } else {
209 /* not a top-level special tag, remove also preceiding '/' */
210 goto remove_nodelevel;
211 }
212 } else {
213remove_nodelevel:
214 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
215 if (ctx->path_len == 0) {
216 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200217 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200218 }
219 }
220 /* set new terminating NULL-byte */
221 ctx->path[ctx->path_len] = '\0';
222 } else {
223 if (ctx->path_len > 1) {
224 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
225 /* extension of the special tag */
226 nextlevel = 2;
227 --ctx->path_len;
228 } else {
229 /* there is already some path, so add next level */
230 nextlevel = 1;
231 }
232 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
233
234 if (nextlevel != 2) {
235 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
236 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200237 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200238 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200239 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 +0200240 }
241 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200242 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200243 }
Radek Krejciacc79042019-07-25 14:14:57 +0200244 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
245 /* output truncated */
246 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
247 } else {
248 ctx->path_len += len;
249 }
Radek Krejci327de162019-06-14 12:52:07 +0200250 }
251}
252
253/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100254 * @brief Duplicate the compiled pattern structure.
255 *
256 * Instead of duplicating memory, the reference counter in the @p orig is increased.
257 *
258 * @param[in] orig The pattern structure to duplicate.
259 * @return The duplicated structure to use.
260 */
Radek Krejci19a96102018-11-15 13:38:09 +0100261static struct lysc_pattern*
262lysc_pattern_dup(struct lysc_pattern *orig)
263{
264 ++orig->refcount;
265 return orig;
266}
267
Radek Krejcib56c7502019-02-13 14:19:54 +0100268/**
269 * @brief Duplicate the array of compiled patterns.
270 *
271 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
272 *
273 * @param[in] ctx Libyang context for logging.
274 * @param[in] orig The patterns sized array to duplicate.
275 * @return New sized array as a copy of @p orig.
276 * @return NULL in case of memory allocation error.
277 */
Radek Krejci19a96102018-11-15 13:38:09 +0100278static struct lysc_pattern**
279lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
280{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100281 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200282 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100283
Radek Krejcib56c7502019-02-13 14:19:54 +0100284 assert(orig);
285
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200286 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100287 LY_ARRAY_FOR(orig, u) {
288 dup[u] = lysc_pattern_dup(orig[u]);
289 LY_ARRAY_INCREMENT(dup);
290 }
291 return dup;
292}
293
Radek Krejcib56c7502019-02-13 14:19:54 +0100294/**
295 * @brief Duplicate compiled range structure.
296 *
297 * @param[in] ctx Libyang context for logging.
298 * @param[in] orig The range structure to be duplicated.
299 * @return New compiled range structure as a copy of @p orig.
300 * @return NULL in case of memory allocation error.
301 */
Radek Krejci19a96102018-11-15 13:38:09 +0100302struct lysc_range*
303lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
304{
305 struct lysc_range *dup;
306 LY_ERR ret;
307
Radek Krejcib56c7502019-02-13 14:19:54 +0100308 assert(orig);
309
Radek Krejci19a96102018-11-15 13:38:09 +0100310 dup = calloc(1, sizeof *dup);
311 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
312 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200313 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
314 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
315 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100316 }
317 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
318 DUP_STRING(ctx, orig->emsg, dup->emsg);
319 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
320
321 return dup;
322cleanup:
323 free(dup);
324 (void) ret; /* set but not used due to the return type */
325 return NULL;
326}
327
Radek Krejcib56c7502019-02-13 14:19:54 +0100328/**
329 * @brief Stack for processing if-feature expressions.
330 */
Radek Krejci19a96102018-11-15 13:38:09 +0100331struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100332 int size; /**< number of items in the stack */
333 int index; /**< first empty item */
334 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100335};
336
Radek Krejcib56c7502019-02-13 14:19:54 +0100337/**
338 * @brief Add @ref ifftokens into the stack.
339 * @param[in] stack The if-feature stack to use.
340 * @param[in] value One of the @ref ifftokens to store in the stack.
341 * @return LY_EMEM in case of memory allocation error
342 * @return LY_ESUCCESS if the value successfully stored.
343 */
Radek Krejci19a96102018-11-15 13:38:09 +0100344static LY_ERR
345iff_stack_push(struct iff_stack *stack, uint8_t value)
346{
347 if (stack->index == stack->size) {
348 stack->size += 4;
349 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
350 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
351 }
352 stack->stack[stack->index++] = value;
353 return LY_SUCCESS;
354}
355
Radek Krejcib56c7502019-02-13 14:19:54 +0100356/**
357 * @brief Get (and remove) the last item form the stack.
358 * @param[in] stack The if-feature stack to use.
359 * @return The value from the top of the stack.
360 */
Radek Krejci19a96102018-11-15 13:38:09 +0100361static uint8_t
362iff_stack_pop(struct iff_stack *stack)
363{
Radek Krejcib56c7502019-02-13 14:19:54 +0100364 assert(stack && stack->index);
365
Radek Krejci19a96102018-11-15 13:38:09 +0100366 stack->index--;
367 return stack->stack[stack->index];
368}
369
Radek Krejcib56c7502019-02-13 14:19:54 +0100370/**
371 * @brief Clean up the stack.
372 * @param[in] stack The if-feature stack to use.
373 */
Radek Krejci19a96102018-11-15 13:38:09 +0100374static void
375iff_stack_clean(struct iff_stack *stack)
376{
377 stack->size = 0;
378 free(stack->stack);
379}
380
Radek Krejcib56c7502019-02-13 14:19:54 +0100381/**
382 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
383 * (libyang format of the if-feature expression).
384 * @param[in,out] list The 2bits array to modify.
385 * @param[in] op The operand (@ref ifftokens) to store.
386 * @param[in] pos Position (0-based) where to store the given @p op.
387 */
Radek Krejci19a96102018-11-15 13:38:09 +0100388static void
389iff_setop(uint8_t *list, uint8_t op, int pos)
390{
391 uint8_t *item;
392 uint8_t mask = 3;
393
394 assert(pos >= 0);
395 assert(op <= 3); /* max 2 bits */
396
397 item = &list[pos / 4];
398 mask = mask << 2 * (pos % 4);
399 *item = (*item) & ~mask;
400 *item = (*item) | (op << 2 * (pos % 4));
401}
402
Radek Krejcib56c7502019-02-13 14:19:54 +0100403#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
404#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100405
Radek Krejci0af46292019-01-11 16:02:31 +0100406/**
407 * @brief Find a feature of the given name and referenced in the given module.
408 *
409 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
410 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
411 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
412 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
413 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
414 * assigned till that time will be still valid.
415 *
416 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
417 * @param[in] name Name of the feature including possible prefix.
418 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
419 * @return Pointer to the feature structure if found, NULL otherwise.
420 */
Radek Krejci19a96102018-11-15 13:38:09 +0100421static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100422lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100423{
424 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200425 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100426 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100427
428 for (i = 0; i < len; ++i) {
429 if (name[i] == ':') {
430 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100431 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100432 LY_CHECK_RET(!mod, NULL);
433
434 name = &name[i + 1];
435 len = len - i - 1;
436 }
437 }
438
439 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100440 if (mod->implemented) {
441 /* module is implemented so there is already the compiled schema */
442 flist = mod->compiled->features;
443 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200444 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100445 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200446 LY_ARRAY_FOR(flist, u) {
447 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200448 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100449 return f;
450 }
451 }
452
453 return NULL;
454}
455
Michal Vasko8d544252020-03-02 10:19:52 +0100456/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100457 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
458 */
459static LY_ERR
460lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
461{
462 LY_ERR ret = LY_SUCCESS;
463
464 if (!ext_p->compiled) {
465 lysc_update_path(ctx, NULL, "{extension}");
466 lysc_update_path(ctx, NULL, ext_p->name);
467
468 /* compile the extension definition */
469 ext_p->compiled = calloc(1, sizeof **ext);
470 ext_p->compiled->refcount = 1;
471 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
472 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
473 ext_p->compiled->module = (struct lys_module *)ext_mod;
474 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
475
476 lysc_update_path(ctx, NULL, NULL);
477 lysc_update_path(ctx, NULL, NULL);
478
479 /* find extension definition plugin */
480 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
481 }
482
483 *ext = lysc_ext_dup(ext_p->compiled);
484
485done:
486 return ret;
487}
488
489/**
Michal Vasko8d544252020-03-02 10:19:52 +0100490 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
491 *
492 * @param[in] ctx Compilation context.
493 * @param[in] ext_p Parsed extension instance.
494 * @param[in,out] ext Prepared compiled extension instance.
495 * @param[in] parent Extension instance parent.
496 * @param[in] parent_type Extension instance parent type.
497 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
498 */
Radek Krejci19a96102018-11-15 13:38:09 +0100499static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100500lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
501 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100502{
Radek Krejci7c960162019-09-18 14:16:12 +0200503 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100504 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200505 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200506 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200507 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100508
509 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
510 ext->insubstmt = ext_p->insubstmt;
511 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200512 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200513 ext->parent = parent;
514 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100515
Radek Krejcif56e2a42019-09-09 14:15:25 +0200516 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200517
Radek Krejci19a96102018-11-15 13:38:09 +0100518 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200519 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
520 if (ext_p->yin) {
521 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
522 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
523 * YANG (import) prefix must be done here. */
524 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
525 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
526 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200527 } else {
528 assert(ctx->mod_def->parsed);
529 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
530 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200531 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200532 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 +0200533 ret = LY_EMEM, cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200534 prefixed_name = lydict_insert_zc(ctx->ctx, s);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200535 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200536 break;
537 }
538 }
539 }
540 if (!prefixed_name) {
541 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
542 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
543 goto cleanup;
544 }
545 } else {
546 prefixed_name = ext_p->name;
547 }
548 lysc_update_path(ctx, NULL, prefixed_name);
549
Michal Vasko8d544252020-03-02 10:19:52 +0100550 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200551 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100552 if (!ext_mod) {
553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
554 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
555 goto cleanup;
556 } else if (!ext_mod->parsed->extensions) {
557 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
558 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
559 prefixed_name, ext_mod->name);
560 goto cleanup;
561 }
Radek Krejci7c960162019-09-18 14:16:12 +0200562 }
563 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200564
Michal Vasko5fe75f12020-03-02 13:52:37 +0100565 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200566 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
567 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100568 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200569 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100570 break;
571 }
572 }
Radek Krejci7c960162019-09-18 14:16:12 +0200573 if (!ext->def) {
574 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
575 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
576 goto cleanup;
577 }
Radek Krejci0935f412019-08-20 16:15:18 +0200578
Radek Krejcif56e2a42019-09-09 14:15:25 +0200579 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
580 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
581 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200582 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200583 /* Schema was parsed from YIN and an argument is expected, ... */
584 struct lysp_stmt *stmt = NULL;
585
586 if (ext->def->flags & LYS_YINELEM_TRUE) {
587 /* ... argument was the first XML child element */
588 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
589 /* TODO check namespace of the statement */
590 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
591 stmt = ext_p->child;
592 }
593 }
594 } else {
595 /* ... argument was one of the XML attributes which are represented as child stmt
596 * with LYS_YIN_ATTR flag */
597 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
598 if (!strcmp(stmt->stmt, ext->def->argument)) {
599 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200600 break;
601 }
602 }
603 }
604 if (!stmt) {
605 /* missing extension's argument */
606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200607 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
608 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200609
610 }
611 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
612 stmt->flags |= LYS_YIN_ARGUMENT;
613 }
Radek Krejci7c960162019-09-18 14:16:12 +0200614 if (prefixed_name != ext_p->name) {
615 lydict_remove(ctx->ctx, ext_p->name);
616 ext_p->name = prefixed_name;
617 if (!ext_p->argument && ext->argument) {
618 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
619 }
620 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200621
Radek Krejci0935f412019-08-20 16:15:18 +0200622 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200623 if (ext->argument) {
624 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
625 }
Radek Krejci7c960162019-09-18 14:16:12 +0200626 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200627 if (ext->argument) {
628 lysc_update_path(ctx, NULL, NULL);
629 }
Radek Krejci0935f412019-08-20 16:15:18 +0200630 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200631 ext_p->compiled = ext;
632
Radek Krejci7c960162019-09-18 14:16:12 +0200633 ret = LY_SUCCESS;
634cleanup:
635 if (prefixed_name && prefixed_name != ext_p->name) {
636 lydict_remove(ctx->ctx, prefixed_name);
637 }
638
Radek Krejcif56e2a42019-09-09 14:15:25 +0200639 lysc_update_path(ctx, NULL, NULL);
640 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200641
Radek Krejci7c960162019-09-18 14:16:12 +0200642 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200643}
644
645/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100646 * @brief Compile information from the if-feature statement
647 * @param[in] ctx Compile context.
648 * @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 +0100649 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
650 * @return LY_ERR value.
651 */
Radek Krejci19a96102018-11-15 13:38:09 +0100652static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200653lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100654{
655 const char *c = *value;
656 int r, rc = EXIT_FAILURE;
657 int i, j, last_not, checkversion = 0;
658 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
659 uint8_t op;
660 struct iff_stack stack = {0, 0, NULL};
661 struct lysc_feature *f;
662
663 assert(c);
664
665 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
666 for (i = j = last_not = 0; c[i]; i++) {
667 if (c[i] == '(') {
668 j++;
669 checkversion = 1;
670 continue;
671 } else if (c[i] == ')') {
672 j--;
673 continue;
674 } else if (isspace(c[i])) {
675 checkversion = 1;
676 continue;
677 }
678
679 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 +0200680 int sp;
681 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
682 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
684 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
685 return LY_EVALID;
686 } else if (!isspace(c[i + r])) {
687 /* feature name starting with the not/and/or */
688 last_not = 0;
689 f_size++;
690 } else if (c[i] == 'n') { /* not operation */
691 if (last_not) {
692 /* double not */
693 expr_size = expr_size - 2;
694 last_not = 0;
695 } else {
696 last_not = 1;
697 }
698 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200699 if (f_exp != f_size) {
700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
701 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
702 return LY_EVALID;
703 }
Radek Krejci19a96102018-11-15 13:38:09 +0100704 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200705
Radek Krejci19a96102018-11-15 13:38:09 +0100706 /* not a not operation */
707 last_not = 0;
708 }
709 i += r;
710 } else {
711 f_size++;
712 last_not = 0;
713 }
714 expr_size++;
715
716 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200717 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100718 i--;
719 break;
720 }
721 i++;
722 }
723 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200724 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100725 /* not matching count of ( and ) */
726 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
727 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
728 return LY_EVALID;
729 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200730 if (f_exp != f_size) {
731 /* features do not match the needed arguments for the logical operations */
732 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
733 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
734 "the required number of operands for the operations.", *value);
735 return LY_EVALID;
736 }
Radek Krejci19a96102018-11-15 13:38:09 +0100737
738 if (checkversion || expr_size > 1) {
739 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100740 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
742 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
743 return LY_EVALID;
744 }
745 }
746
747 /* allocate the memory */
748 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
749 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
750 stack.stack = malloc(expr_size * sizeof *stack.stack);
751 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
752
753 stack.size = expr_size;
754 f_size--; expr_size--; /* used as indexes from now */
755
756 for (i--; i >= 0; i--) {
757 if (c[i] == ')') {
758 /* push it on stack */
759 iff_stack_push(&stack, LYS_IFF_RP);
760 continue;
761 } else if (c[i] == '(') {
762 /* pop from the stack into result all operators until ) */
763 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
764 iff_setop(iff->expr, op, expr_size--);
765 }
766 continue;
767 } else if (isspace(c[i])) {
768 continue;
769 }
770
771 /* end of operator or operand -> find beginning and get what is it */
772 j = i + 1;
773 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
774 i--;
775 }
776 i++; /* go back by one step */
777
778 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
779 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
780 /* double not */
781 iff_stack_pop(&stack);
782 } else {
783 /* not has the highest priority, so do not pop from the stack
784 * as in case of AND and OR */
785 iff_stack_push(&stack, LYS_IFF_NOT);
786 }
787 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
788 /* as for OR - pop from the stack all operators with the same or higher
789 * priority and store them to the result, then push the AND to the stack */
790 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
791 op = iff_stack_pop(&stack);
792 iff_setop(iff->expr, op, expr_size--);
793 }
794 iff_stack_push(&stack, LYS_IFF_AND);
795 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
796 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
797 op = iff_stack_pop(&stack);
798 iff_setop(iff->expr, op, expr_size--);
799 }
800 iff_stack_push(&stack, LYS_IFF_OR);
801 } else {
802 /* feature name, length is j - i */
803
804 /* add it to the expression */
805 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
806
807 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100808 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
809 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
810 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
811 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100812 iff->features[f_size] = f;
813 LY_ARRAY_INCREMENT(iff->features);
814 f_size--;
815 }
816 }
817 while (stack.index) {
818 op = iff_stack_pop(&stack);
819 iff_setop(iff->expr, op, expr_size--);
820 }
821
822 if (++expr_size || ++f_size) {
823 /* not all expected operators and operands found */
824 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
825 "Invalid value \"%s\" of if-feature - processing error.", *value);
826 rc = LY_EINT;
827 } else {
828 rc = LY_SUCCESS;
829 }
830
831error:
832 /* cleanup */
833 iff_stack_clean(&stack);
834
835 return rc;
836}
837
Radek Krejcib56c7502019-02-13 14:19:54 +0100838/**
Michal Vasko175012e2019-11-06 15:49:14 +0100839 * @brief Get the XPath context node for the given schema node.
840 * @param[in] start The schema node where the XPath expression appears.
841 * @return The context node to evaluate XPath expression in given schema node.
842 * @return NULL in case the context node is the root node.
843 */
844static struct lysc_node *
845lysc_xpath_context(struct lysc_node *start)
846{
Michal Vasko1bf09392020-03-27 12:38:10 +0100847 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko175012e2019-11-06 15:49:14 +0100848 start = start->parent);
849 return start;
850}
851
852/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100853 * @brief Compile information from the when statement
854 * @param[in] ctx Compile context.
855 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100856 * @param[in] flags Flags of the node with the "when" defiition.
857 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100858 * @param[out] when Pointer where to store pointer to the created compiled when structure.
859 * @return LY_ERR value.
860 */
Radek Krejci19a96102018-11-15 13:38:09 +0100861static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100862lys_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 +0100863{
Radek Krejci19a96102018-11-15 13:38:09 +0100864 LY_ERR ret = LY_SUCCESS;
865
Radek Krejci00b874b2019-02-12 10:54:50 +0100866 *when = calloc(1, sizeof **when);
867 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200868 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200869 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100870 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100871 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
872 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
873 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200874 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100875 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100876
877done:
878 return ret;
879}
880
Radek Krejcib56c7502019-02-13 14:19:54 +0100881/**
882 * @brief Compile information from the must statement
883 * @param[in] ctx Compile context.
884 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100885 * @param[in,out] must Prepared (empty) compiled must structure to fill.
886 * @return LY_ERR value.
887 */
Radek Krejci19a96102018-11-15 13:38:09 +0100888static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200889lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100890{
Radek Krejci19a96102018-11-15 13:38:09 +0100891 LY_ERR ret = LY_SUCCESS;
892
Michal Vasko004d3152020-06-11 19:59:22 +0200893 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100894 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200895 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100896 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
897 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100898 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
899 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200900 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100901
902done:
903 return ret;
904}
905
Radek Krejcib56c7502019-02-13 14:19:54 +0100906/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200907 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100908 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200909 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100910 * @return LY_ERR value.
911 */
Radek Krejci19a96102018-11-15 13:38:09 +0100912static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200913lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100914{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200915 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100916 LY_ERR ret = LY_SUCCESS;
917
Radek Krejci7f2a5362018-11-28 13:05:37 +0100918 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
919 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100920 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200921 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200923 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200924 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200925 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200926 LOGINT(ctx->ctx);
927 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200928 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 +0200929 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200930 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200931 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200932 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200933 mod = NULL;
934 }
Radek Krejci19a96102018-11-15 13:38:09 +0100935 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200936 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100937 }
938 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200939 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 +0100940 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 +0200941 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100942 return LY_ENOTFOUND;
943 }
944 }
Radek Krejci19a96102018-11-15 13:38:09 +0100945 }
946
Radek Krejci19a96102018-11-15 13:38:09 +0100947 return ret;
948}
949
Michal Vasko33ff9422020-07-03 09:50:39 +0200950LY_ERR
951lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
952 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100953{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200954 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200955 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +0100956 LY_ERR ret = LY_SUCCESS;
957
Michal Vasko33ff9422020-07-03 09:50:39 +0200958 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +0200959
Michal Vasko33ff9422020-07-03 09:50:39 +0200960 if (!ctx_sc) {
961 context.ctx = ctx;
962 context.mod = module;
963 context.path_len = 1;
964 context.path[0] = '/';
965 ctx_sc = &context;
966 }
Radek Krejci19a96102018-11-15 13:38:09 +0100967
Michal Vasko33ff9422020-07-03 09:50:39 +0200968 if (!identities_p) {
969 return LY_SUCCESS;
970 }
971 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200972 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +0200973 }
974
975 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200976 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +0200977 LY_ARRAY_FOR(identities_p, u) {
978 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
979
980 LY_ARRAY_INCREMENT(*identities);
981 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
982 DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
983 DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
984 DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
985 (*identities)[offset + u].module = ctx_sc->mod;
986 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
987 lys_compile_iffeature, ret, done);
988 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
989 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
990 LYEXT_PAR_IDENT, ret, done);
991 (*identities)[offset + u].flags = identities_p[u].flags;
992
993 lysc_update_path(ctx_sc, NULL, NULL);
994 }
995 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100996done:
997 return ret;
998}
999
Radek Krejcib56c7502019-02-13 14:19:54 +01001000/**
1001 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1002 *
1003 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1004 *
1005 * @param[in] ctx Compile context for logging.
1006 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1007 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1008 * @return LY_SUCCESS if everything is ok.
1009 * @return LY_EVALID if the identity is derived from itself.
1010 */
Radek Krejci38222632019-02-12 16:55:05 +01001011static LY_ERR
1012lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1013{
1014 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001015 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001016 struct ly_set recursion = {0};
1017 struct lysc_ident *drv;
1018
1019 if (!derived) {
1020 return LY_SUCCESS;
1021 }
1022
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001023 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001024 if (ident == derived[u]) {
1025 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1026 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1027 goto cleanup;
1028 }
1029 ly_set_add(&recursion, derived[u], 0);
1030 }
1031
1032 for (v = 0; v < recursion.count; ++v) {
1033 drv = recursion.objs[v];
1034 if (!drv->derived) {
1035 continue;
1036 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001037 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001038 if (ident == drv->derived[u]) {
1039 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1040 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1041 goto cleanup;
1042 }
1043 ly_set_add(&recursion, drv->derived[u], 0);
1044 }
1045 }
1046 ret = LY_SUCCESS;
1047
1048cleanup:
1049 ly_set_erase(&recursion, NULL);
1050 return ret;
1051}
1052
Radek Krejcia3045382018-11-22 14:30:31 +01001053/**
1054 * @brief Find and process the referenced base identities from another identity or identityref
1055 *
Radek Krejciaca74032019-06-04 08:53:06 +02001056 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001057 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1058 * to distinguish these two use cases.
1059 *
1060 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1061 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001062 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001063 * @param[in] bases Array of bases of identityref to fill in.
1064 * @return LY_ERR value.
1065 */
Radek Krejci19a96102018-11-15 13:38:09 +01001066static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001067lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
1068 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001069{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001070 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001071 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001072 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001073 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001074
1075 assert(ident || bases);
1076
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001077 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001078 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1079 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1080 return LY_EVALID;
1081 }
1082
Michal Vasko33ff9422020-07-03 09:50:39 +02001083 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001084 s = strchr(bases_p[u], ':');
1085 if (s) {
1086 /* prefixed identity */
1087 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001088 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001089 } else {
1090 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001091 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001092 }
1093 if (!mod) {
1094 if (ident) {
1095 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1096 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1097 } else {
1098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1099 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1100 }
1101 return LY_EVALID;
1102 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001103
Radek Krejci555cb5b2018-11-16 14:54:33 +01001104 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001105 if (mod->compiled) {
1106 identities = mod->compiled->identities;
1107 } else {
1108 identities = mod->dis_identities;
1109 }
1110 LY_ARRAY_FOR(identities, v) {
1111 if (!strcmp(name, identities[v].name)) {
1112 if (ident) {
1113 if (ident == &identities[v]) {
1114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1115 "Identity \"%s\" is derived from itself.", ident->name);
1116 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001117 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001118 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1119 /* we have match! store the backlink */
1120 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1121 *idref = ident;
1122 } else {
1123 /* we have match! store the found identity */
1124 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1125 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001126 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001127 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001128 }
1129 }
1130 if (!idref || !(*idref)) {
1131 if (ident) {
1132 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1133 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1134 } else {
1135 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1136 "Unable to find base (%s) of identityref.", bases_p[u]);
1137 }
1138 return LY_EVALID;
1139 }
1140 }
1141 return LY_SUCCESS;
1142}
1143
Radek Krejcia3045382018-11-22 14:30:31 +01001144/**
1145 * @brief For the given array of identities, set the backlinks from all their base identities.
1146 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1147 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1148 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1149 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1150 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001151static LY_ERR
1152lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1153{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001154 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001155
Michal Vasko33ff9422020-07-03 09:50:39 +02001156 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001157 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001158 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001159 continue;
1160 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001161 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001162 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 +02001163 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001164 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001165 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001166 return LY_SUCCESS;
1167}
1168
Radek Krejci0af46292019-01-11 16:02:31 +01001169LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001170lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
1171 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001172{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001173 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001174 struct lysc_ctx context = {0};
1175
Radek Krejci327de162019-06-14 12:52:07 +02001176 assert(ctx_sc || ctx);
1177
1178 if (!ctx_sc) {
1179 context.ctx = ctx;
1180 context.mod = module;
1181 context.path_len = 1;
1182 context.path[0] = '/';
1183 ctx_sc = &context;
1184 }
Radek Krejci0af46292019-01-11 16:02:31 +01001185
1186 if (!features_p) {
1187 return LY_SUCCESS;
1188 }
1189 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001190 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001191 }
1192
Radek Krejci327de162019-06-14 12:52:07 +02001193 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001194 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001195 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001196 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1197
Radek Krejci0af46292019-01-11 16:02:31 +01001198 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001199 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001200 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1201 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1202 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001203 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001204 (*features)[offset + u].module = ctx_sc->mod;
1205
1206 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001207 }
Radek Krejci327de162019-06-14 12:52:07 +02001208 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001209
1210 return LY_SUCCESS;
1211}
1212
Radek Krejcia3045382018-11-22 14:30:31 +01001213/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001214 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001215 *
1216 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1217 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001218 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001219 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1220 * being currently processed).
1221 * @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 +01001222 * @return LY_SUCCESS if everything is ok.
1223 * @return LY_EVALID if the feature references indirectly itself.
1224 */
1225static LY_ERR
1226lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1227{
1228 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001229 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001230 struct ly_set recursion = {0};
1231 struct lysc_feature *drv;
1232
1233 if (!depfeatures) {
1234 return LY_SUCCESS;
1235 }
1236
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001237 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001238 if (feature == depfeatures[u]) {
1239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1240 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1241 goto cleanup;
1242 }
1243 ly_set_add(&recursion, depfeatures[u], 0);
1244 }
1245
1246 for (v = 0; v < recursion.count; ++v) {
1247 drv = recursion.objs[v];
1248 if (!drv->depfeatures) {
1249 continue;
1250 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001251 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001252 if (feature == drv->depfeatures[u]) {
1253 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1254 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1255 goto cleanup;
1256 }
1257 ly_set_add(&recursion, drv->depfeatures[u], 0);
1258 }
1259 }
1260 ret = LY_SUCCESS;
1261
1262cleanup:
1263 ly_set_erase(&recursion, NULL);
1264 return ret;
1265}
1266
1267/**
Radek Krejci0af46292019-01-11 16:02:31 +01001268 * @brief Create pre-compiled features array.
1269 *
1270 * See lys_feature_precompile() for more details.
1271 *
Radek Krejcia3045382018-11-22 14:30:31 +01001272 * @param[in] ctx Compile context.
1273 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001274 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001275 * @return LY_ERR value.
1276 */
Radek Krejci19a96102018-11-15 13:38:09 +01001277static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001278lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001279{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001280 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001281 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001282 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001283
Radek Krejci327de162019-06-14 12:52:07 +02001284
Radek Krejci0af46292019-01-11 16:02:31 +01001285 /* find the preprecompiled feature */
1286 LY_ARRAY_FOR(features, x) {
1287 if (strcmp(features[x].name, feature_p->name)) {
1288 continue;
1289 }
1290 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001291 lysc_update_path(ctx, NULL, "{feature}");
1292 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001293
Radek Krejci0af46292019-01-11 16:02:31 +01001294 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001295 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001296 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001297 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001298 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001299 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001300 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001301 /* check for circular dependency - direct reference first,... */
1302 if (feature == feature->iffeatures[u].features[v]) {
1303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1304 "Feature \"%s\" is referenced from itself.", feature->name);
1305 return LY_EVALID;
1306 }
1307 /* ... and indirect circular reference */
1308 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1309
Radek Krejci0af46292019-01-11 16:02:31 +01001310 /* add itself into the dependants list */
1311 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1312 *df = feature;
1313 }
Radek Krejci19a96102018-11-15 13:38:09 +01001314 }
Radek Krejci19a96102018-11-15 13:38:09 +01001315 }
1316 }
Radek Krejci327de162019-06-14 12:52:07 +02001317 lysc_update_path(ctx, NULL, NULL);
1318 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001319 done:
1320 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001321 }
Radek Krejci0af46292019-01-11 16:02:31 +01001322
1323 LOGINT(ctx->ctx);
1324 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001325}
1326
Radek Krejcib56c7502019-02-13 14:19:54 +01001327/**
1328 * @brief Revert compiled list of features back to the precompiled state.
1329 *
1330 * 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 +02001331 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001332 *
1333 * @param[in] ctx Compilation context.
1334 * @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 +02001335 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001336 */
Radek Krejci95710c92019-02-11 15:49:55 +01001337static void
1338lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1339{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001340 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001341
Michal Vasko33ff9422020-07-03 09:50:39 +02001342 /* keep the dis_features list until the complete lys_module is freed */
1343 mod->dis_features = mod->compiled->features;
Radek Krejci95710c92019-02-11 15:49:55 +01001344 mod->compiled->features = NULL;
1345
Michal Vasko33ff9422020-07-03 09:50:39 +02001346 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001347 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001348 LY_ARRAY_FOR(mod->dis_features, u) {
1349 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1350 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001351 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001352 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1353 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001354
Michal Vasko33ff9422020-07-03 09:50:39 +02001355 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1356 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001357 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001358 LY_ARRAY_FREE(mod->dis_features[u].exts);
1359 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001360 }
1361}
1362
Radek Krejcia3045382018-11-22 14:30:31 +01001363/**
1364 * @brief Validate and normalize numeric value from a range definition.
1365 * @param[in] ctx Compile context.
1366 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1367 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1368 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1369 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1370 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1371 * @param[in] value String value of the range boundary.
1372 * @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.
1373 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1374 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1375 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001376LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001377range_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 +01001378{
Radek Krejci6cba4292018-11-15 17:33:29 +01001379 size_t fraction = 0, size;
1380
Radek Krejci19a96102018-11-15 13:38:09 +01001381 *len = 0;
1382
1383 assert(value);
1384 /* parse value */
1385 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1386 return LY_EVALID;
1387 }
1388
1389 if ((value[*len] == '-') || (value[*len] == '+')) {
1390 ++(*len);
1391 }
1392
1393 while (isdigit(value[*len])) {
1394 ++(*len);
1395 }
1396
1397 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001398 if (basetype == LY_TYPE_DEC64) {
1399 goto decimal;
1400 } else {
1401 *valcopy = strndup(value, *len);
1402 return LY_SUCCESS;
1403 }
Radek Krejci19a96102018-11-15 13:38:09 +01001404 }
1405 fraction = *len;
1406
1407 ++(*len);
1408 while (isdigit(value[*len])) {
1409 ++(*len);
1410 }
1411
Radek Krejci6cba4292018-11-15 17:33:29 +01001412 if (basetype == LY_TYPE_DEC64) {
1413decimal:
1414 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001415 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001416 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1417 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1418 *len, value, frdigits);
1419 return LY_EINVAL;
1420 }
1421 if (fraction) {
1422 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1423 } else {
1424 size = (*len) + frdigits + 1;
1425 }
1426 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001427 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1428
Radek Krejci6cba4292018-11-15 17:33:29 +01001429 (*valcopy)[size - 1] = '\0';
1430 if (fraction) {
1431 memcpy(&(*valcopy)[0], &value[0], fraction);
1432 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1433 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1434 } else {
1435 memcpy(&(*valcopy)[0], &value[0], *len);
1436 memset(&(*valcopy)[*len], '0', frdigits);
1437 }
Radek Krejci19a96102018-11-15 13:38:09 +01001438 }
1439 return LY_SUCCESS;
1440}
1441
Radek Krejcia3045382018-11-22 14:30:31 +01001442/**
1443 * @brief Check that values in range are in ascendant order.
1444 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001445 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1446 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001447 * @param[in] value Current value to check.
1448 * @param[in] prev_value The last seen value.
1449 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1450 */
Radek Krejci19a96102018-11-15 13:38:09 +01001451static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001452range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001453{
1454 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001455 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001456 return LY_EEXIST;
1457 }
1458 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001459 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001460 return LY_EEXIST;
1461 }
1462 }
1463 return LY_SUCCESS;
1464}
1465
Radek Krejcia3045382018-11-22 14:30:31 +01001466/**
1467 * @brief Set min/max value of the range part.
1468 * @param[in] ctx Compile context.
1469 * @param[in] part Range part structure to fill.
1470 * @param[in] max Flag to distinguish if storing min or max value.
1471 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1472 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1473 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1474 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1475 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001476 * @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 +01001477 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1478 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1479 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1480 * frdigits value), LY_EMEM.
1481 */
Radek Krejci19a96102018-11-15 13:38:09 +01001482static LY_ERR
1483range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001484 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001485{
1486 LY_ERR ret = LY_SUCCESS;
1487 char *valcopy = NULL;
1488 size_t len;
1489
1490 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001491 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001492 LY_CHECK_GOTO(ret, finalize);
1493 }
1494 if (!valcopy && base_range) {
1495 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001496 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001497 } else {
1498 part->min_64 = base_range->parts[0].min_64;
1499 }
1500 if (!first) {
1501 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1502 }
1503 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001504 }
1505
1506 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001507 case LY_TYPE_INT8: /* range */
1508 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001509 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 +01001510 } else if (max) {
1511 part->max_64 = INT64_C(127);
1512 } else {
1513 part->min_64 = INT64_C(-128);
1514 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001515 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001516 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001517 }
1518 break;
1519 case LY_TYPE_INT16: /* range */
1520 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001521 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 +01001522 } else if (max) {
1523 part->max_64 = INT64_C(32767);
1524 } else {
1525 part->min_64 = INT64_C(-32768);
1526 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001527 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001528 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001529 }
1530 break;
1531 case LY_TYPE_INT32: /* range */
1532 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001533 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 +01001534 } else if (max) {
1535 part->max_64 = INT64_C(2147483647);
1536 } else {
1537 part->min_64 = INT64_C(-2147483648);
1538 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001539 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001540 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001541 }
1542 break;
1543 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001544 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001545 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001546 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001547 max ? &part->max_64 : &part->min_64);
1548 } else if (max) {
1549 part->max_64 = INT64_C(9223372036854775807);
1550 } else {
1551 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1552 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001553 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001554 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001555 }
1556 break;
1557 case LY_TYPE_UINT8: /* range */
1558 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001559 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 +01001560 } else if (max) {
1561 part->max_u64 = UINT64_C(255);
1562 } else {
1563 part->min_u64 = UINT64_C(0);
1564 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001565 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001566 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001567 }
1568 break;
1569 case LY_TYPE_UINT16: /* range */
1570 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001571 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 +01001572 } else if (max) {
1573 part->max_u64 = UINT64_C(65535);
1574 } else {
1575 part->min_u64 = UINT64_C(0);
1576 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001577 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001578 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001579 }
1580 break;
1581 case LY_TYPE_UINT32: /* range */
1582 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001583 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 +01001584 } else if (max) {
1585 part->max_u64 = UINT64_C(4294967295);
1586 } else {
1587 part->min_u64 = UINT64_C(0);
1588 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001589 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001590 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001591 }
1592 break;
1593 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001594 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001595 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001596 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001597 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 +01001598 } else if (max) {
1599 part->max_u64 = UINT64_C(18446744073709551615);
1600 } else {
1601 part->min_u64 = UINT64_C(0);
1602 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001603 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001604 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001605 }
1606 break;
1607 default:
1608 LOGINT(ctx->ctx);
1609 ret = LY_EINT;
1610 }
1611
Radek Krejci5969f272018-11-23 10:03:58 +01001612finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001613 if (ret == LY_EDENIED) {
1614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1615 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1616 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1617 } else if (ret == LY_EVALID) {
1618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1619 "Invalid %s restriction - invalid value \"%s\".",
1620 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1621 } else if (ret == LY_EEXIST) {
1622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1623 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001624 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001625 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001626 } else if (!ret && value) {
1627 *value = *value + len;
1628 }
1629 free(valcopy);
1630 return ret;
1631}
1632
Radek Krejcia3045382018-11-22 14:30:31 +01001633/**
1634 * @brief Compile the parsed range restriction.
1635 * @param[in] ctx Compile context.
1636 * @param[in] range_p Parsed range structure to compile.
1637 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1638 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1639 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1640 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1641 * range restriction must be more restrictive than the base_range.
1642 * @param[in,out] range Pointer to the created current range structure.
1643 * @return LY_ERR value.
1644 */
Radek Krejci19a96102018-11-15 13:38:09 +01001645static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001646lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001647 struct lysc_range *base_range, struct lysc_range **range)
1648{
1649 LY_ERR ret = LY_EVALID;
1650 const char *expr;
1651 struct lysc_range_part *parts = NULL, *part;
1652 int range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001653 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001654
1655 assert(range);
1656 assert(range_p);
1657
1658 expr = range_p->arg;
1659 while(1) {
1660 if (isspace(*expr)) {
1661 ++expr;
1662 } else if (*expr == '\0') {
1663 if (range_expected) {
1664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1665 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1666 length_restr ? "length" : "range", range_p->arg);
1667 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001668 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1670 "Invalid %s restriction - unexpected end of the expression (%s).",
1671 length_restr ? "length" : "range", range_p->arg);
1672 goto cleanup;
1673 }
1674 parts_done++;
1675 break;
1676 } else if (!strncmp(expr, "min", 3)) {
1677 if (parts) {
1678 /* min cannot be used elsewhere than in the first part */
1679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1680 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1681 expr - range_p->arg, range_p->arg);
1682 goto cleanup;
1683 }
1684 expr += 3;
1685
1686 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001687 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 +01001688 part->max_64 = part->min_64;
1689 } else if (*expr == '|') {
1690 if (!parts || range_expected) {
1691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1692 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1693 goto cleanup;
1694 }
1695 expr++;
1696 parts_done++;
1697 /* process next part of the expression */
1698 } else if (!strncmp(expr, "..", 2)) {
1699 expr += 2;
1700 while (isspace(*expr)) {
1701 expr++;
1702 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001703 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001704 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1705 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1706 goto cleanup;
1707 }
1708 /* continue expecting the upper boundary */
1709 range_expected = 1;
1710 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1711 /* number */
1712 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001713 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001714 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 +01001715 range_expected = 0;
1716 } else {
1717 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001718 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 +01001719 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001720 part->max_64 = part->min_64;
1721 }
1722
1723 /* continue with possible another expression part */
1724 } else if (!strncmp(expr, "max", 3)) {
1725 expr += 3;
1726 while (isspace(*expr)) {
1727 expr++;
1728 }
1729 if (*expr != '\0') {
1730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1731 length_restr ? "length" : "range", expr);
1732 goto cleanup;
1733 }
1734 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001735 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001736 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 +01001737 range_expected = 0;
1738 } else {
1739 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001740 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 +01001741 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001742 part->min_64 = part->max_64;
1743 }
1744 } else {
1745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1746 length_restr ? "length" : "range", expr);
1747 goto cleanup;
1748 }
1749 }
1750
1751 /* check with the previous range/length restriction */
1752 if (base_range) {
1753 switch (basetype) {
1754 case LY_TYPE_BINARY:
1755 case LY_TYPE_UINT8:
1756 case LY_TYPE_UINT16:
1757 case LY_TYPE_UINT32:
1758 case LY_TYPE_UINT64:
1759 case LY_TYPE_STRING:
1760 uns = 1;
1761 break;
1762 case LY_TYPE_DEC64:
1763 case LY_TYPE_INT8:
1764 case LY_TYPE_INT16:
1765 case LY_TYPE_INT32:
1766 case LY_TYPE_INT64:
1767 uns = 0;
1768 break;
1769 default:
1770 LOGINT(ctx->ctx);
1771 ret = LY_EINT;
1772 goto cleanup;
1773 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001774 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001775 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1776 goto baseerror;
1777 }
1778 /* current lower bound is not lower than the base */
1779 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1780 /* base has single value */
1781 if (base_range->parts[v].min_64 == parts[u].min_64) {
1782 /* both lower bounds are the same */
1783 if (parts[u].min_64 != parts[u].max_64) {
1784 /* current continues with a range */
1785 goto baseerror;
1786 } else {
1787 /* equal single values, move both forward */
1788 ++v;
1789 continue;
1790 }
1791 } else {
1792 /* base is single value lower than current range, so the
1793 * value from base range is removed in the current,
1794 * move only base and repeat checking */
1795 ++v;
1796 --u;
1797 continue;
1798 }
1799 } else {
1800 /* base is the range */
1801 if (parts[u].min_64 == parts[u].max_64) {
1802 /* current is a single value */
1803 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1804 /* current is behind the base range, so base range is omitted,
1805 * move the base and keep the current for further check */
1806 ++v;
1807 --u;
1808 } /* else it is within the base range, so move the current, but keep the base */
1809 continue;
1810 } else {
1811 /* both are ranges - check the higher bound, the lower was already checked */
1812 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1813 /* higher bound is higher than the current higher bound */
1814 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1815 /* but the current lower bound is also higher, so the base range is omitted,
1816 * continue with the same current, but move the base */
1817 --u;
1818 ++v;
1819 continue;
1820 }
1821 /* current range starts within the base range but end behind it */
1822 goto baseerror;
1823 } else {
1824 /* current range is smaller than the base,
1825 * move current, but stay with the base */
1826 continue;
1827 }
1828 }
1829 }
1830 }
1831 if (u != parts_done) {
1832baseerror:
1833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1834 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1835 length_restr ? "length" : "range", range_p->arg);
1836 goto cleanup;
1837 }
1838 }
1839
1840 if (!(*range)) {
1841 *range = calloc(1, sizeof **range);
1842 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1843 }
1844
Radek Krejcic8b31002019-01-08 10:24:45 +01001845 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001846 if (range_p->eapptag) {
1847 lydict_remove(ctx->ctx, (*range)->eapptag);
1848 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1849 }
1850 if (range_p->emsg) {
1851 lydict_remove(ctx->ctx, (*range)->emsg);
1852 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1853 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001854 if (range_p->dsc) {
1855 lydict_remove(ctx->ctx, (*range)->dsc);
1856 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1857 }
1858 if (range_p->ref) {
1859 lydict_remove(ctx->ctx, (*range)->ref);
1860 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1861 }
Radek Krejci19a96102018-11-15 13:38:09 +01001862 /* extensions are taken only from the last range by the caller */
1863
1864 (*range)->parts = parts;
1865 parts = NULL;
1866 ret = LY_SUCCESS;
1867cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001868 LY_ARRAY_FREE(parts);
1869
1870 return ret;
1871}
1872
1873/**
1874 * @brief Checks pattern syntax.
1875 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 * @param[in] ctx Context.
1877 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001878 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001879 * @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 +01001880 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001881 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001882LY_ERR
1883lys_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 +01001884{
Michal Vasko40a00082020-05-27 15:20:01 +02001885 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001886 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001887 int err_code;
1888 const char *orig_ptr;
1889 PCRE2_SIZE err_offset;
1890 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001891#define URANGE_LEN 19
1892 char *ublock2urange[][2] = {
1893 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1894 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1895 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1896 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1897 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1898 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1899 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1900 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1901 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1902 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1903 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1904 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1905 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1906 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1907 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1908 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1909 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1910 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1911 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1912 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1913 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1914 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1915 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1916 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1917 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1918 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1919 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1920 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1921 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1922 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1923 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1924 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1925 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1926 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1927 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1928 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1929 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1930 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1931 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1932 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1933 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1934 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1935 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1936 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1937 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1938 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1939 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1940 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1941 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1942 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1943 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1944 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1945 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1946 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1947 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1948 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1949 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1950 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1951 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1952 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1953 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1954 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1955 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1956 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1957 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1958 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1959 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1960 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1961 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1962 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1963 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1964 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1965 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1966 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1967 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1968 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1969 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1970 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1971 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1972 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1973 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1974 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1975 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1976 {NULL, NULL}
1977 };
1978
1979 /* adjust the expression to a Perl equivalent
1980 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1981
Michal Vasko40a00082020-05-27 15:20:01 +02001982 /* allocate space for the transformed pattern */
1983 size = strlen(pattern) + 1;
1984 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001986 perl_regex[0] = '\0';
1987
Michal Vasko40a00082020-05-27 15:20:01 +02001988 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1989 brack = 0;
1990 idx = 0;
1991 orig_ptr = pattern;
1992 while (orig_ptr[0]) {
1993 switch (orig_ptr[0]) {
1994 case '$':
1995 case '^':
1996 if (!brack) {
1997 /* make space for the extra character */
1998 ++size;
1999 perl_regex = ly_realloc(perl_regex, size);
2000 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002001
Michal Vasko40a00082020-05-27 15:20:01 +02002002 /* print escape slash */
2003 perl_regex[idx] = '\\';
2004 ++idx;
2005 }
2006 break;
2007 case '[':
2008 /* must not be escaped */
2009 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2010 ++brack;
2011 }
2012 break;
2013 case ']':
2014 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2015 /* pattern was checked and compiled already */
2016 assert(brack);
2017 --brack;
2018 }
2019 break;
2020 default:
2021 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002022 }
Michal Vasko40a00082020-05-27 15:20:01 +02002023
2024 /* copy char */
2025 perl_regex[idx] = orig_ptr[0];
2026
2027 ++idx;
2028 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002029 }
Michal Vasko40a00082020-05-27 15:20:01 +02002030 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002031
2032 /* substitute Unicode Character Blocks with exact Character Ranges */
2033 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2034 start = ptr - perl_regex;
2035
2036 ptr = strchr(ptr, '}');
2037 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002039 pattern, perl_regex + start + 2, "unterminated character property");
2040 free(perl_regex);
2041 return LY_EVALID;
2042 }
2043 end = (ptr - perl_regex) + 1;
2044
2045 /* need more space */
2046 if (end - start < URANGE_LEN) {
2047 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002048 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002049 }
2050
2051 /* find our range */
2052 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2053 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2054 break;
2055 }
2056 }
2057 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002059 pattern, perl_regex + start + 5, "unknown block name");
2060 free(perl_regex);
2061 return LY_EVALID;
2062 }
2063
2064 /* 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 +02002065 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002066 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002067 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002068 }
2069 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002070 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002071 }
2072 }
Michal Vasko40a00082020-05-27 15:20:01 +02002073 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002074 /* skip brackets */
2075 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2076 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2077 } else {
2078 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2079 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2080 }
2081 }
2082
2083 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002084 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2085 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002086 &err_code, &err_offset, NULL);
2087 if (!code_local) {
2088 PCRE2_UCHAR err_msg[256] = {0};
2089 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002091 free(perl_regex);
2092 return LY_EVALID;
2093 }
2094 free(perl_regex);
2095
Radek Krejci54579462019-04-30 12:47:06 +02002096 if (code) {
2097 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002098 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002099 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002100 }
2101
2102 return LY_SUCCESS;
2103
2104#undef URANGE_LEN
2105}
2106
Radek Krejcia3045382018-11-22 14:30:31 +01002107/**
2108 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2109 * @param[in] ctx Compile context.
2110 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002111 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2112 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2113 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2114 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2115 */
Radek Krejci19a96102018-11-15 13:38:09 +01002116static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002117lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002118 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2119{
2120 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002121 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002122 LY_ERR ret = LY_SUCCESS;
2123
2124 /* first, copy the patterns from the base type */
2125 if (base_patterns) {
2126 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2127 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2128 }
2129
2130 LY_ARRAY_FOR(patterns_p, u) {
2131 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2132 *pattern = calloc(1, sizeof **pattern);
2133 ++(*pattern)->refcount;
2134
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002136 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002137
2138 if (patterns_p[u].arg[0] == 0x15) {
2139 (*pattern)->inverted = 1;
2140 }
Radek Krejci54579462019-04-30 12:47:06 +02002141 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002142 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2143 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002144 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2145 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002146 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002147 }
2148done:
2149 return ret;
2150}
2151
Radek Krejcia3045382018-11-22 14:30:31 +01002152/**
2153 * @brief map of the possible restrictions combination for the specific built-in type.
2154 */
Radek Krejci19a96102018-11-15 13:38:09 +01002155static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2156 0 /* LY_TYPE_UNKNOWN */,
2157 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002158 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2159 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2160 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2161 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2162 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002163 LYS_SET_BIT /* LY_TYPE_BITS */,
2164 0 /* LY_TYPE_BOOL */,
2165 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2166 0 /* LY_TYPE_EMPTY */,
2167 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2168 LYS_SET_BASE /* LY_TYPE_IDENT */,
2169 LYS_SET_REQINST /* LY_TYPE_INST */,
2170 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002171 LYS_SET_TYPE /* LY_TYPE_UNION */,
2172 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002173 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002174 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002175 LYS_SET_RANGE /* LY_TYPE_INT64 */
2176};
2177
2178/**
2179 * @brief stringification of the YANG built-in data types
2180 */
2181const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2182 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2183 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002184};
2185
Radek Krejcia3045382018-11-22 14:30:31 +01002186/**
2187 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2188 * @param[in] ctx Compile context.
2189 * @param[in] enums_p Array of the parsed enum structures to compile.
2190 * @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 +01002191 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2192 * @param[out] enums Newly created array of the compiled enums information for the current type.
2193 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2194 */
Radek Krejci19a96102018-11-15 13:38:09 +01002195static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002196lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002197 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002198{
2199 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002200 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002201 int32_t value = 0;
2202 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002203 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002204
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002205 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002206 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2207 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2208 return LY_EVALID;
2209 }
2210
2211 LY_ARRAY_FOR(enums_p, u) {
2212 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2213 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002214 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2215 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002216 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002217 if (base_enums) {
2218 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2219 LY_ARRAY_FOR(base_enums, v) {
2220 if (!strcmp(e->name, base_enums[v].name)) {
2221 break;
2222 }
2223 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002224 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2226 "Invalid %s - derived type adds new item \"%s\".",
2227 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2228 return LY_EVALID;
2229 }
2230 match = v;
2231 }
2232
2233 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002234 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002235 if (enums_p[u].flags & LYS_SET_VALUE) {
2236 e->value = (int32_t)enums_p[u].value;
2237 if (!u || e->value >= value) {
2238 value = e->value + 1;
2239 }
2240 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002241 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002242 if (e->value == (*enums)[v].value) {
2243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2244 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2245 e->value, e->name, (*enums)[v].name);
2246 return LY_EVALID;
2247 }
2248 }
2249 } else if (base_enums) {
2250 /* inherit the assigned value */
2251 e->value = base_enums[match].value;
2252 if (!u || e->value >= value) {
2253 value = e->value + 1;
2254 }
2255 } else {
2256 /* assign value automatically */
2257 if (u && value == INT32_MIN) {
2258 /* counter overflow */
2259 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2260 "Invalid enumeration - it is not possible to auto-assign enum value for "
2261 "\"%s\" since the highest value is already 2147483647.", e->name);
2262 return LY_EVALID;
2263 }
2264 e->value = value++;
2265 }
2266 } else { /* LY_TYPE_BITS */
2267 if (enums_p[u].flags & LYS_SET_VALUE) {
2268 e->value = (int32_t)enums_p[u].value;
2269 if (!u || (uint32_t)e->value >= position) {
2270 position = (uint32_t)e->value + 1;
2271 }
2272 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002273 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002274 if (e->value == (*enums)[v].value) {
2275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2276 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2277 (uint32_t)e->value, e->name, (*enums)[v].name);
2278 return LY_EVALID;
2279 }
2280 }
2281 } else if (base_enums) {
2282 /* inherit the assigned value */
2283 e->value = base_enums[match].value;
2284 if (!u || (uint32_t)e->value >= position) {
2285 position = (uint32_t)e->value + 1;
2286 }
2287 } else {
2288 /* assign value automatically */
2289 if (u && position == 0) {
2290 /* counter overflow */
2291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2292 "Invalid bits - it is not possible to auto-assign bit position for "
2293 "\"%s\" since the highest value is already 4294967295.", e->name);
2294 return LY_EVALID;
2295 }
2296 e->value = position++;
2297 }
2298 }
2299
2300 if (base_enums) {
2301 /* the assigned values must not change from the derived type */
2302 if (e->value != base_enums[match].value) {
2303 if (basetype == LY_TYPE_ENUM) {
2304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2305 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2306 e->name, base_enums[match].value, e->value);
2307 } else {
2308 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2309 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2310 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2311 }
2312 return LY_EVALID;
2313 }
2314 }
2315
Radek Krejciec4da802019-05-02 13:02:41 +02002316 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002317 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 +01002318
2319 if (basetype == LY_TYPE_BITS) {
2320 /* keep bits ordered by position */
2321 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2322 if (v != u) {
2323 memcpy(&storage, e, sizeof *e);
2324 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2325 memcpy(&(*enums)[v], &storage, sizeof storage);
2326 }
2327 }
2328 }
2329
2330done:
2331 return ret;
2332}
2333
Radek Krejcia3045382018-11-22 14:30:31 +01002334/**
2335 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2336 *
2337 * path-arg = absolute-path / relative-path
2338 * absolute-path = 1*("/" (node-identifier *path-predicate))
2339 * relative-path = 1*(".." "/") descendant-path
2340 *
2341 * @param[in,out] path Path to parse.
2342 * @param[out] prefix Prefix of the token, NULL if there is not any.
2343 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2344 * @param[out] name Name of the token.
2345 * @param[out] nam_len Length of the name.
2346 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2347 * must not be changed between consecutive calls. -1 if the
2348 * path is absolute.
2349 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2350 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2351 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002352LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002353lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2354 int *parent_times, int *has_predicate)
2355{
2356 int par_times = 0;
2357
2358 assert(path && *path);
2359 assert(parent_times);
2360 assert(prefix);
2361 assert(prefix_len);
2362 assert(name);
2363 assert(name_len);
2364 assert(has_predicate);
2365
2366 *prefix = NULL;
2367 *prefix_len = 0;
2368 *name = NULL;
2369 *name_len = 0;
2370 *has_predicate = 0;
2371
2372 if (!*parent_times) {
2373 if (!strncmp(*path, "..", 2)) {
2374 *path += 2;
2375 ++par_times;
2376 while (!strncmp(*path, "/..", 3)) {
2377 *path += 3;
2378 ++par_times;
2379 }
2380 }
2381 if (par_times) {
2382 *parent_times = par_times;
2383 } else {
2384 *parent_times = -1;
2385 }
2386 }
2387
2388 if (**path != '/') {
2389 return LY_EINVAL;
2390 }
2391 /* skip '/' */
2392 ++(*path);
2393
2394 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002395 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002396
2397 if ((**path == '/' && (*path)[1]) || !**path) {
2398 /* path continues by another token or this is the last token */
2399 return LY_SUCCESS;
2400 } else if ((*path)[0] != '[') {
2401 /* unexpected character */
2402 return LY_EINVAL;
2403 } else {
2404 /* predicate starting with [ */
2405 *has_predicate = 1;
2406 return LY_SUCCESS;
2407 }
2408}
2409
2410/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002411 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2412 *
2413 * The set of features used for target must be a subset of features used for the leafref.
2414 * This is not a perfect, we should compare the truth tables but it could require too much resources
2415 * and RFC 7950 does not require it explicitely, so we simplify that.
2416 *
2417 * @param[in] refnode The leafref node.
2418 * @param[in] target Tha target node of the leafref.
2419 * @return LY_SUCCESS or LY_EVALID;
2420 */
2421static LY_ERR
2422lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2423{
2424 LY_ERR ret = LY_EVALID;
2425 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002426 LY_ARRAY_COUNT_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002427 struct ly_set features = {0};
2428
2429 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002430 if (iter->iffeatures) {
2431 LY_ARRAY_FOR(iter->iffeatures, u) {
2432 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2433 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002434 }
2435 }
2436 }
2437 }
2438
2439 /* we should have, in features set, a superset of features applicable to the target node.
2440 * So when adding features applicable to the target into the features set, we should not be
2441 * able to actually add any new feature, otherwise it is not a subset of features applicable
2442 * to the leafref itself. */
2443 count = features.count;
2444 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002445 if (iter->iffeatures) {
2446 LY_ARRAY_FOR(iter->iffeatures, u) {
2447 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2448 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002449 /* new feature was added (or LY_EMEM) */
2450 goto cleanup;
2451 }
2452 }
2453 }
2454 }
2455 }
2456 ret = LY_SUCCESS;
2457
2458cleanup:
2459 ly_set_erase(&features, NULL);
2460 return ret;
2461}
2462
Michal Vasko004d3152020-06-11 19:59:22 +02002463static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2464 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2465 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002466
Radek Krejcia3045382018-11-22 14:30:31 +01002467/**
2468 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2469 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002470 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2471 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2472 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2473 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002474 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002475 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002476 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002477 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2478 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2479 * @param[out] type Newly created type structure with the filled information about the type.
2480 * @return LY_ERR value.
2481 */
Radek Krejci19a96102018-11-15 13:38:09 +01002482static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002483lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2484 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2485 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2486 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002487{
2488 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002489 struct lysc_type_bin *bin;
2490 struct lysc_type_num *num;
2491 struct lysc_type_str *str;
2492 struct lysc_type_bits *bits;
2493 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002494 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002495 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002496 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002497 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002498
2499 switch (basetype) {
2500 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002501 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002502
2503 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002504 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002505 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2506 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002507 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002508 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 +01002509 }
2510 }
2511
2512 if (tpdfname) {
2513 type_p->compiled = *type;
2514 *type = calloc(1, sizeof(struct lysc_type_bin));
2515 }
2516 break;
2517 case LY_TYPE_BITS:
2518 /* RFC 7950 9.7 - bits */
2519 bits = (struct lysc_type_bits*)(*type);
2520 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002521 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002522 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2523 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002524 }
2525
Radek Krejci555cb5b2018-11-16 14:54:33 +01002526 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002527 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002528 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002529 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002530 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002532 free(*type);
2533 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002534 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002535 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002536 }
2537
2538 if (tpdfname) {
2539 type_p->compiled = *type;
2540 *type = calloc(1, sizeof(struct lysc_type_bits));
2541 }
2542 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002543 case LY_TYPE_DEC64:
2544 dec = (struct lysc_type_dec*)(*type);
2545
2546 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002547 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002548 if (!type_p->fraction_digits) {
2549 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002551 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002553 free(*type);
2554 *type = NULL;
2555 }
2556 return LY_EVALID;
2557 }
2558 } else if (type_p->fraction_digits) {
2559 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002560 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002561 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2562 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002563 tpdfname);
2564 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002565 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2566 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002567 free(*type);
2568 *type = NULL;
2569 }
2570 return LY_EVALID;
2571 }
2572 dec->fraction_digits = type_p->fraction_digits;
2573
2574 /* RFC 7950 9.2.4 - range */
2575 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002576 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2577 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002578 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002579 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 +01002580 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002581 }
2582
2583 if (tpdfname) {
2584 type_p->compiled = *type;
2585 *type = calloc(1, sizeof(struct lysc_type_dec));
2586 }
2587 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002588 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002589 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002590
2591 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002592 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002593 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2594 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002595 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002596 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 +01002597 }
2598 } else if (base && ((struct lysc_type_str*)base)->length) {
2599 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2600 }
2601
2602 /* RFC 7950 9.4.5 - pattern */
2603 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002604 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002605 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002606 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2607 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2608 }
2609
2610 if (tpdfname) {
2611 type_p->compiled = *type;
2612 *type = calloc(1, sizeof(struct lysc_type_str));
2613 }
2614 break;
2615 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002616 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002617
2618 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002619 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002620 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002621 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002622 }
2623
Radek Krejci555cb5b2018-11-16 14:54:33 +01002624 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002625 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002626 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002627 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002628 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002630 free(*type);
2631 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002632 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002633 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002634 }
2635
2636 if (tpdfname) {
2637 type_p->compiled = *type;
2638 *type = calloc(1, sizeof(struct lysc_type_enum));
2639 }
2640 break;
2641 case LY_TYPE_INT8:
2642 case LY_TYPE_UINT8:
2643 case LY_TYPE_INT16:
2644 case LY_TYPE_UINT16:
2645 case LY_TYPE_INT32:
2646 case LY_TYPE_UINT32:
2647 case LY_TYPE_INT64:
2648 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002649 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002650
2651 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002652 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002653 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2654 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002655 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002656 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 +01002657 }
2658 }
2659
2660 if (tpdfname) {
2661 type_p->compiled = *type;
2662 *type = calloc(1, sizeof(struct lysc_type_num));
2663 }
2664 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002665 case LY_TYPE_IDENT:
2666 idref = (struct lysc_type_identityref*)(*type);
2667
2668 /* RFC 7950 9.10.2 - base */
2669 if (type_p->bases) {
2670 if (base) {
2671 /* only the directly derived identityrefs can contain base specification */
2672 if (tpdfname) {
2673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002674 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002675 tpdfname);
2676 } else {
2677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002678 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002679 free(*type);
2680 *type = NULL;
2681 }
2682 return LY_EVALID;
2683 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002684 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002685 }
2686
2687 if (!base && !type_p->flags) {
2688 /* type derived from identityref built-in type must contain at least one base */
2689 if (tpdfname) {
2690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2691 } else {
2692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2693 free(*type);
2694 *type = NULL;
2695 }
2696 return LY_EVALID;
2697 }
2698
2699 if (tpdfname) {
2700 type_p->compiled = *type;
2701 *type = calloc(1, sizeof(struct lysc_type_identityref));
2702 }
2703 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002704 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002705 lref = (struct lysc_type_leafref*)*type;
2706
Radek Krejcia3045382018-11-22 14:30:31 +01002707 /* RFC 7950 9.9.3 - require-instance */
2708 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002709 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002710 if (tpdfname) {
2711 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2712 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2713 } else {
2714 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2715 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2716 free(*type);
2717 *type = NULL;
2718 }
2719 return LY_EVALID;
2720 }
Michal Vasko004d3152020-06-11 19:59:22 +02002721 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002722 } else if (base) {
2723 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002724 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002725 } else {
2726 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002727 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002728 }
2729 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002730 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2731 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002732 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002733 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2734 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002735 } else if (tpdfname) {
2736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2737 return LY_EVALID;
2738 } else {
2739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2740 free(*type);
2741 *type = NULL;
2742 return LY_EVALID;
2743 }
2744 if (tpdfname) {
2745 type_p->compiled = *type;
2746 *type = calloc(1, sizeof(struct lysc_type_leafref));
2747 }
2748 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002749 case LY_TYPE_INST:
2750 /* RFC 7950 9.9.3 - require-instance */
2751 if (type_p->flags & LYS_SET_REQINST) {
2752 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2753 } else {
2754 /* default is true */
2755 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2756 }
2757
2758 if (tpdfname) {
2759 type_p->compiled = *type;
2760 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2761 }
2762 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002763 case LY_TYPE_UNION:
2764 un = (struct lysc_type_union*)(*type);
2765
2766 /* RFC 7950 7.4 - type */
2767 if (type_p->types) {
2768 if (base) {
2769 /* only the directly derived union can contain types specification */
2770 if (tpdfname) {
2771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2772 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2773 tpdfname);
2774 } else {
2775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2776 "Invalid type substatement for the type not directly derived from union built-in type.");
2777 free(*type);
2778 *type = NULL;
2779 }
2780 return LY_EVALID;
2781 }
2782 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002783 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2784 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002785 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2786 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002787 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2788 /* add space for additional types from the union subtype */
2789 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002790 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 +02002791 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002792
2793 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002794 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002795 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2796 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2797 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2798 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 +02002799 lref = (struct lysc_type_leafref *)un->types[u + additional];
2800
2801 lref->basetype = LY_TYPE_LEAFREF;
2802 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2803 lref->refcount = 1;
2804 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2805 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002806 /* TODO extensions */
2807
2808 } else {
2809 un->types[u + additional] = un_aux->types[v];
2810 ++un_aux->types[v]->refcount;
2811 }
2812 ++additional;
2813 LY_ARRAY_INCREMENT(un->types);
2814 }
2815 /* compensate u increment in main loop */
2816 --additional;
2817
2818 /* free the replaced union subtype */
2819 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2820 } else {
2821 LY_ARRAY_INCREMENT(un->types);
2822 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002823 }
2824 }
2825
2826 if (!base && !type_p->flags) {
2827 /* type derived from union built-in type must contain at least one type */
2828 if (tpdfname) {
2829 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2830 } else {
2831 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2832 free(*type);
2833 *type = NULL;
2834 }
2835 return LY_EVALID;
2836 }
2837
2838 if (tpdfname) {
2839 type_p->compiled = *type;
2840 *type = calloc(1, sizeof(struct lysc_type_union));
2841 }
2842 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002843 case LY_TYPE_BOOL:
2844 case LY_TYPE_EMPTY:
2845 case LY_TYPE_UNKNOWN: /* just to complete switch */
2846 break;
2847 }
2848 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2849done:
2850 return ret;
2851}
2852
Radek Krejcia3045382018-11-22 14:30:31 +01002853/**
2854 * @brief Compile information about the leaf/leaf-list's type.
2855 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002856 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2857 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2858 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2859 * @param[in] context_name Name of the context node or referencing typedef for logging.
2860 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002861 * @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 +01002862 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002863 * @return LY_ERR value.
2864 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002865static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002866lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2867 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2868 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002869{
2870 LY_ERR ret = LY_SUCCESS;
2871 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002872 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002873 struct type_context {
2874 const struct lysp_tpdf *tpdf;
2875 struct lysp_node *node;
2876 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002877 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002878 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002879 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002880 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002881 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002882 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002883
2884 (*type) = NULL;
2885
2886 tctx = calloc(1, sizeof *tctx);
2887 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002888 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002889 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2890 ret == LY_SUCCESS;
2891 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2892 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2893 if (basetype) {
2894 break;
2895 }
2896
2897 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002898 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2899 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002900 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2901
Radek Krejcicdfecd92018-11-26 11:27:32 +01002902 if (units && !*units) {
2903 /* inherit units */
2904 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2905 }
Radek Krejci01342af2019-01-03 15:18:08 +01002906 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002907 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002908 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002909 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002910 }
Radek Krejci01342af2019-01-03 15:18:08 +01002911 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002912 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2913 break;
2914 }
2915
Radek Krejci19a96102018-11-15 13:38:09 +01002916 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002917 /* it is not necessary to continue, the rest of the chain was already compiled,
2918 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002919 basetype = tctx->tpdf->type.compiled->basetype;
2920 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002921 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002922 dummyloops = 1;
2923 goto preparenext;
2924 } else {
2925 tctx = NULL;
2926 break;
2927 }
Radek Krejci19a96102018-11-15 13:38:09 +01002928 }
2929
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002930 /* circular typedef reference detection */
2931 for (u = 0; u < tpdf_chain.count; u++) {
2932 /* local part */
2933 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2934 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2936 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2937 free(tctx);
2938 ret = LY_EVALID;
2939 goto cleanup;
2940 }
2941 }
2942 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2943 /* global part for unions corner case */
2944 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2945 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2947 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2948 free(tctx);
2949 ret = LY_EVALID;
2950 goto cleanup;
2951 }
2952 }
2953
Radek Krejci19a96102018-11-15 13:38:09 +01002954 /* store information for the following processing */
2955 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2956
Radek Krejcicdfecd92018-11-26 11:27:32 +01002957preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002958 /* prepare next loop */
2959 tctx_prev = tctx;
2960 tctx = calloc(1, sizeof *tctx);
2961 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2962 }
2963 free(tctx);
2964
2965 /* allocate type according to the basetype */
2966 switch (basetype) {
2967 case LY_TYPE_BINARY:
2968 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002969 break;
2970 case LY_TYPE_BITS:
2971 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002972 break;
2973 case LY_TYPE_BOOL:
2974 case LY_TYPE_EMPTY:
2975 *type = calloc(1, sizeof(struct lysc_type));
2976 break;
2977 case LY_TYPE_DEC64:
2978 *type = calloc(1, sizeof(struct lysc_type_dec));
2979 break;
2980 case LY_TYPE_ENUM:
2981 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002982 break;
2983 case LY_TYPE_IDENT:
2984 *type = calloc(1, sizeof(struct lysc_type_identityref));
2985 break;
2986 case LY_TYPE_INST:
2987 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2988 break;
2989 case LY_TYPE_LEAFREF:
2990 *type = calloc(1, sizeof(struct lysc_type_leafref));
2991 break;
2992 case LY_TYPE_STRING:
2993 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002994 break;
2995 case LY_TYPE_UNION:
2996 *type = calloc(1, sizeof(struct lysc_type_union));
2997 break;
2998 case LY_TYPE_INT8:
2999 case LY_TYPE_UINT8:
3000 case LY_TYPE_INT16:
3001 case LY_TYPE_UINT16:
3002 case LY_TYPE_INT32:
3003 case LY_TYPE_UINT32:
3004 case LY_TYPE_INT64:
3005 case LY_TYPE_UINT64:
3006 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003007 break;
3008 case LY_TYPE_UNKNOWN:
3009 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3010 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3011 ret = LY_EVALID;
3012 goto cleanup;
3013 }
3014 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003015 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003016 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3017 ly_data_type2str[basetype]);
3018 free(*type);
3019 (*type) = NULL;
3020 ret = LY_EVALID;
3021 goto cleanup;
3022 }
3023
3024 /* get restrictions from the referred typedefs */
3025 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3026 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003027
3028 /* remember the typedef context for circular check */
3029 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3030
Radek Krejci43699232018-11-23 14:59:46 +01003031 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003032 base = tctx->tpdf->type.compiled;
3033 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003034 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003035 /* no change, just use the type information from the base */
3036 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3037 ++base->refcount;
3038 continue;
3039 }
3040
3041 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003042 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3043 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3044 tctx->tpdf->name, ly_data_type2str[basetype]);
3045 ret = LY_EVALID;
3046 goto cleanup;
3047 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3048 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3049 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3050 tctx->tpdf->name, tctx->tpdf->dflt);
3051 ret = LY_EVALID;
3052 goto cleanup;
3053 }
3054
Radek Krejci19a96102018-11-15 13:38:09 +01003055 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003056 /* TODO user type plugins */
3057 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003058 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003059 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 +01003060 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003061 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003062 LY_CHECK_GOTO(ret, cleanup);
3063 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003064 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003065 /* remove the processed typedef contexts from the stack for circular check */
3066 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003067
Radek Krejcic5c27e52018-11-15 14:38:11 +01003068 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003069 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003070 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003071 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003072 /* TODO user type plugins */
3073 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003074 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003075 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 +01003076 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003077 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003078 /* no specific restriction in leaf's type definition, copy from the base */
3079 free(*type);
3080 (*type) = base;
3081 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003082 }
Radek Krejcia1911222019-07-22 17:24:50 +02003083 if (dflt && !(*type)->dflt) {
3084 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003085 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003086 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3087 (*type)->dflt->realtype = (*type);
3088 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3089 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003090 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003091 if (err) {
3092 ly_err_print(err);
3093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3094 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3095 ly_err_free(err);
3096 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003097 if (ret == LY_EINCOMPLETE) {
3098 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003099 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003100
3101 /* but in general result is so far ok */
3102 ret = LY_SUCCESS;
3103 }
Radek Krejcia1911222019-07-22 17:24:50 +02003104 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003105 }
Radek Krejci19a96102018-11-15 13:38:09 +01003106
Radek Krejci0935f412019-08-20 16:15:18 +02003107 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003108
3109cleanup:
3110 ly_set_erase(&tpdf_chain, free);
3111 return ret;
3112}
3113
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003114/**
3115 * @brief Compile status information of the given node.
3116 *
3117 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3118 * has the status correctly set during the compilation.
3119 *
3120 * @param[in] ctx Compile context
3121 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3122 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3123 * the compatibility with the parent's status value.
3124 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3125 * @return LY_ERR value.
3126 */
3127static LY_ERR
3128lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3129{
3130 /* status - it is not inherited by specification, but it does not make sense to have
3131 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3132 if (!((*node_flags) & LYS_STATUS_MASK)) {
3133 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3134 if ((parent_flags & 0x3) != 0x3) {
3135 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3136 * combination of bits (0x3) which marks the uses_status value */
3137 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3138 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3139 }
3140 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3141 } else {
3142 (*node_flags) |= LYS_STATUS_CURR;
3143 }
3144 } else if (parent_flags & LYS_STATUS_MASK) {
3145 /* check status compatibility with the parent */
3146 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3147 if ((*node_flags) & LYS_STATUS_CURR) {
3148 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3149 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3150 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3151 } else { /* LYS_STATUS_DEPRC */
3152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3153 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3154 }
3155 return LY_EVALID;
3156 }
3157 }
3158 return LY_SUCCESS;
3159}
3160
Radek Krejci8cce8532019-03-05 11:27:45 +01003161/**
3162 * @brief Check uniqness of the node/action/notification name.
3163 *
3164 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3165 * structures, but they share the namespace so we need to check their name collisions.
3166 *
3167 * @param[in] ctx Compile context.
3168 * @param[in] children List (linked list) of data nodes to go through.
3169 * @param[in] actions List (sized array) of actions or RPCs to go through.
3170 * @param[in] notifs List (sized array) of Notifications to go through.
3171 * @param[in] name Name of the item to find in the given lists.
3172 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3173 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3174 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3175 */
3176static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003177lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3178 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003179{
3180 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003181 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003182
3183 LY_LIST_FOR(children, iter) {
3184 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3185 goto error;
3186 }
3187 }
3188 LY_ARRAY_FOR(actions, u) {
3189 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3190 goto error;
3191 }
3192 }
3193 LY_ARRAY_FOR(notifs, u) {
3194 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3195 goto error;
3196 }
3197 }
3198 return LY_SUCCESS;
3199error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003200 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003201 return LY_EEXIST;
3202}
3203
Radek Krejciec4da802019-05-02 13:02:41 +02003204static 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 +01003205
Radek Krejcia3045382018-11-22 14:30:31 +01003206/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003207 * @brief Compile parsed RPC/action schema node information.
3208 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003209 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003210 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003211 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3212 * @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).
3213 * Zero means no uses, non-zero value with no status bit set mean the default status.
3214 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3215 */
3216static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003217lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003218 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3219{
3220 LY_ERR ret = LY_SUCCESS;
3221 struct lysp_node *child_p;
3222 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003223 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003224
Radek Krejci327de162019-06-14 12:52:07 +02003225 lysc_update_path(ctx, parent, action_p->name);
3226
Radek Krejci8cce8532019-03-05 11:27:45 +01003227 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3228 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3229 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3230 action_p->name, action)) {
3231 return LY_EVALID;
3232 }
3233
Radek Krejciec4da802019-05-02 13:02:41 +02003234 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003235 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003236 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003237 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003238 return LY_EVALID;
3239 }
3240
Michal Vasko1bf09392020-03-27 12:38:10 +01003241 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003242 action->module = ctx->mod;
3243 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003244 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003245 action->sp = action_p;
3246 }
3247 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3248
3249 /* status - it is not inherited by specification, but it does not make sense to have
3250 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003251 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003252
3253 DUP_STRING(ctx->ctx, action_p->name, action->name);
3254 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3255 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003256 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003257 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003258
3259 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003260 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003261 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003262 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 +02003263 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003264 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003265 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003266 }
Radek Krejci327de162019-06-14 12:52:07 +02003267 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003268 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003269
3270 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003271 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003272 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003273 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 +02003274 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003275 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003276 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003277 }
Radek Krejci327de162019-06-14 12:52:07 +02003278 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003279 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003280
3281 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3282 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003283 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003284 }
3285
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003286cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003287 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003288 return ret;
3289}
3290
3291/**
Radek Krejci43981a32019-04-12 09:44:11 +02003292 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003293 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003294 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003295 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3296 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3297 * @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 +02003298 * Zero means no uses, non-zero value with no status bit set mean the default status.
3299 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3300 */
3301static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003302lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003303 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3304{
3305 LY_ERR ret = LY_SUCCESS;
3306 struct lysp_node *child_p;
3307 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003308 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003309
Radek Krejci327de162019-06-14 12:52:07 +02003310 lysc_update_path(ctx, parent, notif_p->name);
3311
Radek Krejcifc11bd72019-04-11 16:00:05 +02003312 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3313 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3314 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3315 notif_p->name, notif)) {
3316 return LY_EVALID;
3317 }
3318
Radek Krejciec4da802019-05-02 13:02:41 +02003319 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003320 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3321 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003322 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003323 return LY_EVALID;
3324 }
3325
3326 notif->nodetype = LYS_NOTIF;
3327 notif->module = ctx->mod;
3328 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003329 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003330 notif->sp = notif_p;
3331 }
3332 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3333
3334 /* status - it is not inherited by specification, but it does not make sense to have
3335 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3336 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3337
3338 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3339 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3340 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003341 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003342 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003343 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3344 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003345 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003346 }
Radek Krejci0935f412019-08-20 16:15:18 +02003347 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003348
Radek Krejciec4da802019-05-02 13:02:41 +02003349 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003350 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003351 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003352 }
3353
Radek Krejci327de162019-06-14 12:52:07 +02003354 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003355cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003356 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003357 return ret;
3358}
3359
3360/**
Radek Krejcia3045382018-11-22 14:30:31 +01003361 * @brief Compile parsed container node information.
3362 * @param[in] ctx Compile context
3363 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003364 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3365 * is enriched with the container-specific information.
3366 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3367 */
Radek Krejci19a96102018-11-15 13:38:09 +01003368static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003369lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003370{
3371 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3372 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3373 struct lysp_node *child_p;
3374 unsigned int u;
3375 LY_ERR ret = LY_SUCCESS;
3376
Radek Krejcife909632019-02-12 15:34:42 +01003377 if (cont_p->presence) {
3378 cont->flags |= LYS_PRESENCE;
3379 }
3380
Radek Krejci19a96102018-11-15 13:38:09 +01003381 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003382 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003383 }
3384
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003385 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003386 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3387 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003388 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003389 }
Radek Krejciec4da802019-05-02 13:02:41 +02003390 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3391 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003392
3393done:
3394 return ret;
3395}
3396
Radek Krejci33f72892019-02-21 10:36:58 +01003397/*
3398 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3399 * @param[in] ctx Compile context.
3400 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3401 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003402 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3403 * @return LY_ERR value.
3404 */
3405static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003406lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003407{
Radek Krejci33f72892019-02-21 10:36:58 +01003408 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3409
Radek Krejciec4da802019-05-02 13:02:41 +02003410 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type,
Radek Krejci33f72892019-02-21 10:36:58 +01003411 leaf->units ? NULL : &leaf->units));
3412 if (leaf->nodetype == LYS_LEAFLIST) {
3413 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003414 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3415 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003416 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003417 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3418 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3419 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3420 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003421 LY_ARRAY_INCREMENT(llist->dflts);
3422 }
3423 } else {
3424 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003425 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003426 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3427 leaf->dflt->realtype = leaf->type->dflt->realtype;
3428 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3429 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003430 }
3431 }
3432 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3433 /* 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 +02003434 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003435 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003436 LY_ARRAY_COUNT_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003437 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3438 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3439 /* 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 +02003440 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003441 }
3442 }
3443 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003444 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3445 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3446 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3447 return LY_EVALID;
3448 }
3449 }
3450
Radek Krejci33f72892019-02-21 10:36:58 +01003451 return LY_SUCCESS;
3452}
3453
Radek Krejcia3045382018-11-22 14:30:31 +01003454/**
3455 * @brief Compile parsed leaf node information.
3456 * @param[in] ctx Compile context
3457 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003458 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3459 * is enriched with the leaf-specific information.
3460 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3461 */
Radek Krejci19a96102018-11-15 13:38:09 +01003462static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003463lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003464{
3465 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3466 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003467 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003468 LY_ERR ret = LY_SUCCESS;
3469
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003470 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003471 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3472 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003473 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003474 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003475 if (leaf_p->units) {
3476 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3477 leaf->flags |= LYS_SET_UNITS;
3478 }
Radek Krejcia1911222019-07-22 17:24:50 +02003479
3480 /* the dflt member is just filled to avoid getting the default value from the type */
3481 leaf->dflt = (void*)leaf_p->dflt;
3482 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003483 if (ret) {
3484 leaf->dflt = NULL;
3485 return ret;
3486 }
Radek Krejcia1911222019-07-22 17:24:50 +02003487
Radek Krejciccd20f12019-02-15 14:12:27 +01003488 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003489 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003490 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003491 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3492 leaf->dflt->realtype = leaf->type;
3493 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3494 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003495 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003496 leaf->dflt->realtype->refcount++;
3497 if (err) {
3498 ly_err_print(err);
3499 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3500 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3501 ly_err_free(err);
3502 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003503 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003504 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003505 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003506
3507 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003508 ret = LY_SUCCESS;
3509 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003510 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003511 leaf->flags |= LYS_SET_DFLT;
3512 }
Radek Krejci43699232018-11-23 14:59:46 +01003513
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003514
Radek Krejci19a96102018-11-15 13:38:09 +01003515done:
3516 return ret;
3517}
3518
Radek Krejcia3045382018-11-22 14:30:31 +01003519/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003520 * @brief Compile parsed leaf-list node information.
3521 * @param[in] ctx Compile context
3522 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003523 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3524 * is enriched with the leaf-list-specific information.
3525 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3526 */
3527static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003528lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003529{
3530 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3531 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003532 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003533 LY_ERR ret = LY_SUCCESS;
3534
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003535 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003536 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3537 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003538 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003539 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003540 if (llist_p->units) {
3541 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3542 llist->flags |= LYS_SET_UNITS;
3543 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003544
Radek Krejcia1911222019-07-22 17:24:50 +02003545 /* the dflts member is just filled to avoid getting the default value from the type */
3546 llist->dflts = (void*)llist_p->dflts;
3547 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003548 if (ret != LY_SUCCESS) {
3549 /* make sure the defaults are freed correctly */
3550 if (llist_p->dflts) {
3551 llist->dflts = NULL;
3552 }
3553 return ret;
3554 }
3555
Radek Krejci0e5d8382018-11-28 16:37:53 +01003556 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003557 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003558 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
3559 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003560 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003561 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003562 LY_ARRAY_INCREMENT(llist->dflts_mods);
3563 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003564 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003565 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3566 llist->dflts[u]->realtype = llist->type;
3567 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3568 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003569 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003570 llist->dflts[u]->realtype->refcount++;
3571 if (err) {
3572 ly_err_print(err);
3573 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3574 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3575 ly_err_free(err);
3576 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003577 if (ret == LY_EINCOMPLETE) {
3578 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003579 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003580
3581 /* but in general result is so far ok */
3582 ret = LY_SUCCESS;
3583 }
Radek Krejcia1911222019-07-22 17:24:50 +02003584 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003585 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003586 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003587 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003588 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02003589 /* configuration data values must be unique - so check the default values */
3590 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003591 for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) {
Radek Krejcia1911222019-07-22 17:24:50 +02003592 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3593 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003594 const char *val = llist->type->plugin->print(llist->dflts[v], LYD_XML, lys_get_prefix, llist->dflts_mods[v], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02003595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3596 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3597 if (dynamic) {
3598 free((char*)val);
3599 }
3600 return LY_EVALID;
3601 }
3602 }
3603 }
3604 }
3605
3606 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003607
3608 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003609 if (llist->min) {
3610 llist->flags |= LYS_MAND_TRUE;
3611 }
Radek Krejcib7408632018-11-28 17:12:11 +01003612 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003613
Radek Krejci0e5d8382018-11-28 16:37:53 +01003614done:
3615 return ret;
3616}
3617
3618/**
Radek Krejci7af64242019-02-18 13:07:53 +01003619 * @brief Compile information about list's uniques.
3620 * @param[in] ctx Compile context.
3621 * @param[in] context_module Module where the prefixes are going to be resolved.
3622 * @param[in] uniques Sized array list of unique statements.
3623 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3624 * @return LY_ERR value.
3625 */
3626static LY_ERR
3627lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3628{
3629 LY_ERR ret = LY_SUCCESS;
3630 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003631 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003632 const char *keystr, *delim;
3633 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003634 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003635 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003636 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003637
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003638 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003639 config = -1;
3640 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3641 keystr = uniques[v];
3642 while (keystr) {
3643 delim = strpbrk(keystr, " \t\n");
3644 if (delim) {
3645 len = delim - keystr;
3646 while (isspace(*delim)) {
3647 ++delim;
3648 }
3649 } else {
3650 len = strlen(keystr);
3651 }
3652
3653 /* unique node must be present */
3654 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003655 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3656 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003657 if (ret != LY_SUCCESS) {
3658 if (ret == LY_EDENIED) {
3659 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003660 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003661 len, keystr, lys_nodetype2str((*key)->nodetype));
3662 }
3663 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003664 } else if (flags) {
3665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3666 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003667 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003668 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003669 }
3670
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003671
Radek Krejci7af64242019-02-18 13:07:53 +01003672 /* all referenced leafs must be of the same config type */
3673 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003675 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003676 return LY_EVALID;
3677 } else if ((*key)->flags & LYS_CONFIG_W) {
3678 config = 1;
3679 } else { /* LYS_CONFIG_R */
3680 config = 0;
3681 }
3682
Michal Vasko14654712020-02-06 08:35:21 +01003683 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3684 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3685 if (parent->nodetype == LYS_LIST) {
3686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3687 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3688 return LY_EVALID;
3689 }
3690 }
3691
Radek Krejci7af64242019-02-18 13:07:53 +01003692 /* check status */
3693 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3694 (*key)->flags, (*key)->module, (*key)->name));
3695
3696 /* mark leaf as unique */
3697 (*key)->flags |= LYS_UNIQUE;
3698
3699 /* next unique value in line */
3700 keystr = delim;
3701 }
3702 /* next unique definition */
3703 }
3704
3705 return LY_SUCCESS;
3706}
3707
3708/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003709 * @brief Compile parsed list node information.
3710 * @param[in] ctx Compile context
3711 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003712 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3713 * is enriched with the list-specific information.
3714 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3715 */
3716static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003717lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003718{
3719 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3720 struct lysc_node_list *list = (struct lysc_node_list*)node;
3721 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003722 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003723 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003724 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003725 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003726 LY_ERR ret = LY_SUCCESS;
3727
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003728 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003729 if (list->min) {
3730 list->flags |= LYS_MAND_TRUE;
3731 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003732 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3733
3734 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003735 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003736 }
3737
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003738 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003739 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3740 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003741 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003742 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003743
3744 /* keys */
3745 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3746 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3747 return LY_EVALID;
3748 }
3749
3750 /* find all the keys (must be direct children) */
3751 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003752 if (!keystr) {
3753 /* keyless list */
3754 list->flags |= LYS_KEYLESS;
3755 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003756 while (keystr) {
3757 delim = strpbrk(keystr, " \t\n");
3758 if (delim) {
3759 len = delim - keystr;
3760 while (isspace(*delim)) {
3761 ++delim;
3762 }
3763 } else {
3764 len = strlen(keystr);
3765 }
3766
3767 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003768 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 +02003769 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003770 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3771 "The list's key \"%.*s\" not found.", len, keystr);
3772 return LY_EVALID;
3773 }
3774 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003775 if (key->flags & LYS_KEY) {
3776 /* the node was already marked as a key */
3777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3778 "Duplicated key identifier \"%.*s\".", len, keystr);
3779 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003780 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003781
3782 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003783 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003784 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3786 return LY_EVALID;
3787 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003788 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003789 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003790 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003792 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003793 return LY_EVALID;
3794 }
3795 } else {
3796 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003797 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003798 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003799 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003800 return LY_EVALID;
3801 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003802 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003804 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003805 return LY_EVALID;
3806 }
3807 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003808
3809 /* check status */
3810 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003811 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003812
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003813 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003814 if (key->dflt) {
3815 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3816 lysc_type_free(ctx->ctx, key->dflt->realtype);
3817 free(key->dflt);
3818 key->dflt = NULL;
3819 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003820 }
3821 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003822 key->flags |= LYS_KEY;
3823
3824 /* move it to the correct position */
3825 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3826 /* fix links in closest previous siblings of the key */
3827 if (key->next) {
3828 key->next->prev = key->prev;
3829 } else {
3830 /* last child */
3831 list->child->prev = key->prev;
3832 }
3833 if (key->prev->next) {
3834 key->prev->next = key->next;
3835 }
3836 /* fix links in the key */
3837 if (prev_key) {
3838 key->prev = (struct lysc_node*)prev_key;
3839 key->next = prev_key->next;
3840 } else {
3841 key->prev = list->child->prev;
3842 key->next = list->child;
3843 }
3844 /* fix links in closes future siblings of the key */
3845 if (prev_key) {
3846 if (prev_key->next) {
3847 prev_key->next->prev = (struct lysc_node*)key;
3848 } else {
3849 list->child->prev = (struct lysc_node*)key;
3850 }
3851 prev_key->next = (struct lysc_node*)key;
3852 } else {
3853 list->child->prev = (struct lysc_node*)key;
3854 }
3855 /* fix links in parent */
3856 if (!key->prev->next) {
3857 list->child = (struct lysc_node*)key;
3858 }
3859 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003860
3861 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003862 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003863 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003864 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003865 }
3866
3867 /* uniques */
3868 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003869 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003870 }
3871
Radek Krejciec4da802019-05-02 13:02:41 +02003872 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3873 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003874
3875done:
3876 return ret;
3877}
3878
Radek Krejcib56c7502019-02-13 14:19:54 +01003879/**
3880 * @brief Do some checks and set the default choice's case.
3881 *
3882 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3883 *
3884 * @param[in] ctx Compile context.
3885 * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself,
3886 * not the reference to the imported module.
3887 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3888 * @return LY_ERR value.
3889 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003890static LY_ERR
3891lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3892{
3893 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3894 const char *prefix = NULL, *name;
3895 size_t prefix_len = 0;
3896
3897 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3898 name = strchr(dflt, ':');
3899 if (name) {
3900 prefix = dflt;
3901 prefix_len = name - prefix;
3902 ++name;
3903 } else {
3904 name = dflt;
3905 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003906 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003907 /* prefixed default case make sense only for the prefix of the schema itself */
3908 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3909 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3910 prefix_len, prefix);
3911 return LY_EVALID;
3912 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003913 ch->dflt = (struct lysc_node_case*)lys_find_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejci76b3e962018-12-14 17:01:25 +01003914 if (!ch->dflt) {
3915 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3916 "Default case \"%s\" not found.", dflt);
3917 return LY_EVALID;
3918 }
3919 /* no mandatory nodes directly under the default case */
3920 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003921 if (iter->parent != (struct lysc_node*)ch->dflt) {
3922 break;
3923 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003924 if (iter->flags & LYS_MAND_TRUE) {
3925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3926 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3927 return LY_EVALID;
3928 }
3929 }
Radek Krejci01342af2019-01-03 15:18:08 +01003930 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003931 return LY_SUCCESS;
3932}
3933
Radek Krejciccd20f12019-02-15 14:12:27 +01003934static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003935lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003936{
3937 struct lys_module *mod;
3938 const char *prefix = NULL, *name;
3939 size_t prefix_len = 0;
3940 struct lysc_node_case *cs;
3941 struct lysc_node *node;
3942
3943 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3944 name = strchr(dflt, ':');
3945 if (name) {
3946 prefix = dflt;
3947 prefix_len = name - prefix;
3948 ++name;
3949 } else {
3950 name = dflt;
3951 }
3952 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3953 if (prefix) {
3954 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003956 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3957 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003958 return LY_EVALID;
3959 }
3960 } else {
3961 mod = ctx->mod;
3962 }
3963 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003964 cs = (struct lysc_node_case*)lys_find_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejciccd20f12019-02-15 14:12:27 +01003965 if (!cs) {
3966 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003967 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003968 return LY_EVALID;
3969 }
3970
3971 /* check that there is no mandatory node */
3972 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003973 if (node->parent != (struct lysc_node*)cs) {
3974 break;
3975 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003976 if (node->flags & LYS_MAND_TRUE) {
3977 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003978 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3979 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003980 return LY_EVALID;
3981 }
3982 }
3983
3984 /* set the default case in choice */
3985 ch->dflt = cs;
3986 cs->flags |= LYS_SET_DFLT;
3987
3988 return LY_SUCCESS;
3989}
3990
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003991/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003992 * @brief Compile parsed choice node information.
3993 * @param[in] ctx Compile context
3994 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003995 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003996 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003997 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3998 */
3999static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004000lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004001{
4002 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4003 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4004 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004005 LY_ERR ret = LY_SUCCESS;
4006
Radek Krejci056d0a82018-12-06 16:57:25 +01004007 LY_LIST_FOR(ch_p->child, child_p) {
4008 if (child_p->nodetype == LYS_CASE) {
4009 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004010 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004011 }
4012 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004013 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004014 }
4015 }
4016
4017 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004018 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004019 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004020 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004021
Radek Krejci9800fb82018-12-13 14:26:23 +01004022 return ret;
4023}
4024
4025/**
4026 * @brief Compile parsed anydata or anyxml node information.
4027 * @param[in] ctx Compile context
4028 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004029 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4030 * is enriched with the any-specific information.
4031 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4032 */
4033static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004034lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004035{
4036 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4037 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4038 unsigned int u;
4039 LY_ERR ret = LY_SUCCESS;
4040
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004041 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004042 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4043 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004044 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004045 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004046
4047 if (any->flags & LYS_CONFIG_W) {
4048 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004049 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004050 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004051done:
4052 return ret;
4053}
4054
Radek Krejcib56c7502019-02-13 14:19:54 +01004055/**
Michal Vasko795b3752020-07-13 15:24:27 +02004056 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4057 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004058 *
4059 * @param[in] ctx Compile context
4060 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4061 * the choice itself is expected instead of a specific case node.
4062 * @param[in] node Schema node to connect into the list.
4063 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004064 * 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 +01004065 */
4066static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004067lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004068{
Michal Vasko795b3752020-07-13 15:24:27 +02004069 struct lysc_node **children, *start, *end;
4070 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004071
4072 if (node->nodetype == LYS_CASE) {
4073 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4074 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004075 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004076 }
4077 if (children) {
4078 if (!(*children)) {
4079 /* first child */
4080 *children = node;
4081 } else if (*children != node) {
4082 /* by the condition in previous branch we cover the choice/case children
4083 * - the children list is shared by the choice and the the first case, in addition
4084 * the first child of each case must be referenced from the case node. So the node is
4085 * actually always already inserted in case it is the first children - so here such
4086 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004087 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4088 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4089 /* some augments are already connected and we are connecting new ones,
4090 * keep module name order and insert the node into the children list */
4091 end = (*children);
4092 do {
4093 end = end->prev;
4094 mod = end->module;
4095 while (end->prev->module == mod) {
4096 end = end->prev;
4097 }
4098 } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module)
4099 && (strcmp(mod->name, node->module->name) > 0));
4100
4101 /* we have the last existing node after our node, easily get the first before and connect it */
4102 start = end->prev;
4103 start->next = node;
4104 node->next = end;
4105 end->prev = node;
4106 node->prev = start;
4107 } else {
4108 /* insert at the end of the parent's children list */
4109 (*children)->prev->next = node;
4110 node->prev = (*children)->prev;
4111 (*children)->prev = node;
4112 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004113
4114 /* check the name uniqueness */
4115 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4116 lysc_node_notifs(parent), node->name, node)) {
4117 return LY_EEXIST;
4118 }
4119 }
4120 }
4121 return LY_SUCCESS;
4122}
4123
Radek Krejci95710c92019-02-11 15:49:55 +01004124/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004125 * @brief Prepare the case structure in choice node for the new data node.
4126 *
4127 * 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
4128 * created in the choice when the first child was processed.
4129 *
4130 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004131 * @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,
4132 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004133 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4134 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4135 * @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,
4136 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004137 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004138static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004139lys_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 +01004140{
4141 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004142 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004143 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004144 unsigned int u;
4145 LY_ERR ret;
4146
Radek Krejci95710c92019-02-11 15:49:55 +01004147#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004148 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004149 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4151 return NULL; \
4152 } \
4153 }
4154
Radek Krejci95710c92019-02-11 15:49:55 +01004155 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4156 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004157
4158 /* we have to add an implicit case node into the parent choice */
4159 cs = calloc(1, sizeof(struct lysc_node_case));
4160 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004161 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004162 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004163 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004164 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4165 /* the case is already present since the child is not its first children */
4166 return (struct lysc_node_case*)ch->cases->prev;
4167 }
Radek Krejci95710c92019-02-11 15:49:55 +01004168 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004169
4170 /* explicit parent case is not present (this is its first child) */
4171 cs = calloc(1, sizeof(struct lysc_node_case));
4172 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004173 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004174 cs->flags = LYS_STATUS_MASK & node_p->flags;
4175 cs->sp = node_p;
4176
Radek Krejcib1b59152019-01-07 13:21:56 +01004177 /* 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 +02004178 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004179
4180 if (node_p->when) {
4181 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004182 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004183 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004184
4185 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4186 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004187 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004188 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004189 }
Radek Krejciec4da802019-05-02 13:02:41 +02004190 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004191 } else {
4192 LOGINT(ctx->ctx);
4193 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004194 }
4195 cs->module = ctx->mod;
4196 cs->prev = (struct lysc_node*)cs;
4197 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004198 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004199 cs->child = child;
4200
4201 return cs;
4202error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004203 if (cs) {
4204 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4205 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004206 return NULL;
4207
4208#undef UNIQUE_CHECK
4209}
4210
Radek Krejcib56c7502019-02-13 14:19:54 +01004211/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004212 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004213 *
4214 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004215 * @param[in] node Target node where the config is supposed to be changed.
4216 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004217 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4218 * 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 +01004219 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4220 * @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 +01004221 * @return LY_ERR value.
4222 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004223static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004224lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004225 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004226{
4227 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004228 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004229
4230 if (config == (node->flags & LYS_CONFIG_MASK)) {
4231 /* nothing to do */
4232 return LY_SUCCESS;
4233 }
4234
4235 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004236 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004237 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004239 "Invalid %s of config - configuration node cannot be child of any state data node.",
4240 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004241 return LY_EVALID;
4242 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004243 node->flags |= LYS_SET_CONFIG;
4244 } else {
4245 if (node->flags & LYS_SET_CONFIG) {
4246 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4247 /* setting config flags, but have node with explicit config true */
4248 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004249 "Invalid %s of config - configuration node cannot be child of any state data node.",
4250 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004251 return LY_EVALID;
4252 }
4253 /* do not change config on nodes where the config is explicitely set, this does not apply to
4254 * nodes, which are being changed explicitly (targets of refine or deviation) */
4255 return LY_SUCCESS;
4256 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004257 }
4258 node->flags &= ~LYS_CONFIG_MASK;
4259 node->flags |= config;
4260
4261 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004262 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004263 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004264 }
4265
Radek Krejci76b3e962018-12-14 17:01:25 +01004266 return LY_SUCCESS;
4267}
4268
Radek Krejcib56c7502019-02-13 14:19:54 +01004269/**
4270 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4271 *
4272 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4273 * the flag to such parents from a mandatory children.
4274 *
4275 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4276 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4277 * (mandatory children was removed).
4278 */
Radek Krejcife909632019-02-12 15:34:42 +01004279void
4280lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4281{
4282 struct lysc_node *iter;
4283
4284 if (add) { /* set flag */
4285 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4286 parent = parent->parent) {
4287 parent->flags |= LYS_MAND_TRUE;
4288 }
4289 } else { /* unset flag */
4290 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004291 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004292 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004293 /* there is another mandatory node */
4294 return;
4295 }
4296 }
4297 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4298 parent->flags &= ~LYS_MAND_TRUE;
4299 }
4300 }
4301}
4302
Radek Krejci056d0a82018-12-06 16:57:25 +01004303/**
Radek Krejci3641f562019-02-13 15:38:40 +01004304 * @brief Internal sorting process for the lys_compile_augment_sort().
4305 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4306 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4307 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4308 */
4309static void
4310lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4311{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004312 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004313 size_t len;
4314
4315 len = strlen(aug_p->nodeid);
4316 LY_ARRAY_FOR(result, v) {
4317 if (strlen(result[v]->nodeid) <= len) {
4318 continue;
4319 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004320 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004321 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004322 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004323 break;
4324 }
4325 }
4326 result[v] = aug_p;
4327 LY_ARRAY_INCREMENT(result);
4328}
4329
4330/**
4331 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4332 *
4333 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4334 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4335 *
4336 * @param[in] ctx Compile context.
4337 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4338 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4339 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4340 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4341 * @return LY_ERR value.
4342 */
4343LY_ERR
4344lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4345{
4346 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004347 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004348
4349 assert(augments);
4350
4351 /* get count of the augments in module and all its submodules */
4352 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004353 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004354 }
4355 LY_ARRAY_FOR(inc_p, u) {
4356 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004357 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004358 }
4359 }
4360
4361 if (!count) {
4362 *augments = NULL;
4363 return LY_SUCCESS;
4364 }
4365 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4366
4367 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4368 * together, so there can be even /z/y betwwen them. */
4369 LY_ARRAY_FOR(aug_p, u) {
4370 lys_compile_augment_sort_(&aug_p[u], result);
4371 }
4372 LY_ARRAY_FOR(inc_p, u) {
4373 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4374 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4375 }
4376 }
4377
4378 *augments = result;
4379 return LY_SUCCESS;
4380}
4381
4382/**
4383 * @brief Compile the parsed augment connecting it into its target.
4384 *
4385 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4386 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4387 * are already implemented and compiled.
4388 *
4389 * @param[in] ctx Compile context.
4390 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004391 * @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
4392 * children in case of the augmenting uses data.
4393 * @return LY_SUCCESS on success.
4394 * @return LY_EVALID on failure.
4395 */
4396LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004397lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004398{
Michal Vaskoe6143202020-07-03 13:02:08 +02004399 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004400 struct lysp_node *node_p, *case_node_p;
4401 struct lysc_node *target; /* target target of the augment */
4402 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004403 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004404 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004405 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004406 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004407 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejciec4da802019-05-02 13:02:41 +02004408 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004409
Radek Krejci327de162019-06-14 12:52:07 +02004410 lysc_update_path(ctx, NULL, "{augment}");
4411 lysc_update_path(ctx, NULL, aug_p->nodeid);
4412
Radek Krejci7af64242019-02-18 13:07:53 +01004413 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004414 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004415 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004416 if (ret != LY_SUCCESS) {
4417 if (ret == LY_EDENIED) {
4418 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4419 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4420 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4421 }
4422 return LY_EVALID;
4423 }
4424
4425 /* check for mandatory nodes
4426 * - new cases augmenting some choice can have mandatory nodes
4427 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4428 */
Radek Krejci733988a2019-02-15 15:12:44 +01004429 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004430 allow_mandatory = 1;
4431 }
4432
4433 when_shared = NULL;
4434 LY_LIST_FOR(aug_p->child, node_p) {
4435 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4436 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004437 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004438 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4439 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004440 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004441 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4442 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004443 return LY_EVALID;
4444 }
4445
4446 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004447 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004448 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004449 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004450 } else {
4451 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004452 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004453 }
4454 }
Radek Krejciec4da802019-05-02 13:02:41 +02004455 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004456
Radek Krejcife13da42019-02-15 14:51:01 +01004457 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4458 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004459 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004460 /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004461 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 +01004462 } else if (target->nodetype == LYS_CHOICE) {
4463 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4464 node = ((struct lysc_node_choice*)target)->cases->prev;
4465 } else {
4466 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004467 node = (struct lysc_node*)lysc_node_children(target, flags);
4468 if (!node) {
4469 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4470 break;
4471 }
4472 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004473 }
4474
Radek Krejci733988a2019-02-15 15:12:44 +01004475 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004476 node->flags &= ~LYS_MAND_TRUE;
4477 lys_compile_mandatory_parents(target, 0);
4478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004479 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004480 return LY_EVALID;
4481 }
4482
4483 /* pass augment's when to all the children */
4484 if (aug_p->when) {
4485 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4486 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004487 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004488 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004489
4490 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4491 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004492 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004493 }
4494
Radek Krejci3641f562019-02-13 15:38:40 +01004495 when_shared = *when;
4496 } else {
4497 ++when_shared->refcount;
4498 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004499
4500 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4501 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004502 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004503 }
Radek Krejci3641f562019-02-13 15:38:40 +01004504 }
4505 }
4506 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004507
Radek Krejciec4da802019-05-02 13:02:41 +02004508 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004509 switch (target->nodetype) {
4510 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004511 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004512 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004513 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004514 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004515 break;
4516 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004517 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004518 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004519 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004520 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004521 break;
4522 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004523 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004524 if (aug_p->actions) {
4525 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004526 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4527 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004528 return LY_EVALID;
4529 }
4530 if (aug_p->notifs) {
4531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004532 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004533 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004534 return LY_EVALID;
4535 }
4536 }
Radek Krejci3641f562019-02-13 15:38:40 +01004537
Michal Vaskoe6143202020-07-03 13:02:08 +02004538 /* add this module into the target module augmented_by, if not there already */
4539 rc = LY_SUCCESS;
4540 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4541 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4542 rc = LY_EEXIST;
4543 break;
4544 }
4545 }
4546 if (!rc) {
4547 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4548 *aug_mod = ctx->mod;
4549 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004550
Radek Krejci327de162019-06-14 12:52:07 +02004551 lysc_update_path(ctx, NULL, NULL);
4552 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004553
Radek Krejci3641f562019-02-13 15:38:40 +01004554error:
Radek Krejciec4da802019-05-02 13:02:41 +02004555 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004556 return ret;
4557}
4558
4559/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004560 * @brief Apply refined or deviated mandatory flag to the target node.
4561 *
4562 * @param[in] ctx Compile context.
4563 * @param[in] node Target node where the mandatory property is supposed to be changed.
4564 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004565 * @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 +01004566 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4567 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4568 * 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 +01004569 * @return LY_ERR value.
4570 */
4571static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004572lys_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 +01004573{
4574 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4575 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004576 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4577 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004578 return LY_EVALID;
4579 }
4580
4581 if (mandatory_flag & LYS_MAND_TRUE) {
4582 /* check if node has default value */
4583 if (node->nodetype & LYS_LEAF) {
4584 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004585 if (refine_flag) {
4586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004587 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004588 return LY_EVALID;
4589 }
Radek Krejcia1911222019-07-22 17:24:50 +02004590 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004591 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004592 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004593
4594 /* update the list of incomplete default values if needed */
4595 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4596
Radek Krejcia1911222019-07-22 17:24:50 +02004597 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4598 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4599 free(leaf->dflt);
4600 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004601 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004602 }
4603 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004604 if (refine_flag) {
4605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004606 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004607 return LY_EVALID;
4608 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004609 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004610 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004611 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 +01004612 return LY_EVALID;
4613 }
4614
4615 node->flags &= ~LYS_MAND_FALSE;
4616 node->flags |= LYS_MAND_TRUE;
4617 lys_compile_mandatory_parents(node->parent, 1);
4618 } else {
4619 /* make mandatory false */
4620 node->flags &= ~LYS_MAND_TRUE;
4621 node->flags |= LYS_MAND_FALSE;
4622 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004623 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt && ((struct lysc_node_leaf*)node)->type->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004624 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004625 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004626 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004627 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4628 leaf->dflt->realtype = leaf->type->dflt->realtype;
4629 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4630 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004631 }
4632 }
4633 return LY_SUCCESS;
4634}
4635
4636/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004637 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4638 * If present, also apply uses's modificators.
4639 *
4640 * @param[in] ctx Compile context
4641 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004642 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4643 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4644 * the compile context.
4645 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4646 */
4647static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004648lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004649{
4650 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004651 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004652 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004653 struct lysc_node_container context_node_fake =
4654 {.nodetype = LYS_CONTAINER,
4655 .module = ctx->mod,
4656 .flags = parent ? parent->flags : 0,
4657 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004658 .prev = (struct lysc_node*)&context_node_fake,
4659 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004660 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004661 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004662 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004663 int found;
4664 const char *id, *name, *prefix;
4665 size_t prefix_len, name_len;
4666 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004667 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004668 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004669 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004670 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004671 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004672 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004673 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004674 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004675 struct lysc_notif **notifs = NULL;
4676 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004677
4678 /* search for the grouping definition */
4679 found = 0;
4680 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004681 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004682 if (prefix) {
4683 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4684 if (!mod) {
4685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004686 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004687 return LY_EVALID;
4688 }
4689 } else {
4690 mod = ctx->mod_def;
4691 }
4692 if (mod == ctx->mod_def) {
4693 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004694 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004695 LY_ARRAY_FOR(grp, u) {
4696 if (!strcmp(grp[u].name, name)) {
4697 grp = &grp[u];
4698 found = 1;
4699 break;
4700 }
4701 }
4702 }
4703 }
4704 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004705 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004706 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004707 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004708 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004709 if (!strcmp(grp[u].name, name)) {
4710 grp = &grp[u];
4711 found = 1;
4712 }
4713 }
4714 }
4715 if (!found && mod->parsed->includes) {
4716 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004717 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004718 grp = mod->parsed->includes[u].submodule->groupings;
4719 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004720 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004721 if (!strcmp(grp[v].name, name)) {
4722 grp = &grp[v];
4723 found = 1;
4724 }
4725 }
4726 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004727 }
4728 }
4729 }
4730 if (!found) {
4731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4732 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4733 return LY_EVALID;
4734 }
4735
4736 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4737 grp_stack_count = ctx->groupings.count;
4738 ly_set_add(&ctx->groupings, (void*)grp, 0);
4739 if (grp_stack_count == ctx->groupings.count) {
4740 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4742 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4743 return LY_EVALID;
4744 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004745 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4746 /* remember that the grouping is instantiated to avoid its standalone validation */
4747 grp->flags |= LYS_USED_GRP;
4748 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004749
4750 /* switch context's mod_def */
4751 mod_old = ctx->mod_def;
4752 ctx->mod_def = mod;
4753
4754 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004755 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 +01004756
Radek Krejcifc11bd72019-04-11 16:00:05 +02004757 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004758 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004759 /* 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 +02004760 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004761
4762 /* some preparation for applying refines */
4763 if (grp->data == node_p) {
4764 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004765 if (parent) {
4766 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4767 } else if (ctx->mod->compiled->data) {
4768 child = ctx->mod->compiled->data;
4769 } else {
4770 child = NULL;
4771 }
4772 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004773 }
4774 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004775 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004776 LY_LIST_FOR(context_node_fake.child, child) {
4777 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004778
Radek Krejcifc11bd72019-04-11 16:00:05 +02004779 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004780 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004781 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004782 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004783 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4784
4785 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4786 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004787 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004788 }
4789
Radek Krejci00b874b2019-02-12 10:54:50 +01004790 when_shared = *when;
4791 } else {
4792 ++when_shared->refcount;
4793 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004794
4795 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4796 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004797 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004798 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004799 }
4800 }
Radek Krejci01342af2019-01-03 15:18:08 +01004801 }
4802 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004803 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004804 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004805 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004806 context_node_fake.child->prev = parent ? lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01004807 }
4808
Radek Krejcifc11bd72019-04-11 16:00:05 +02004809 /* compile actions */
4810 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4811 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004812 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004813 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004814 if (*actions && (uses_p->augments || uses_p->refines)) {
4815 /* 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 +02004816 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4817 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4818 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004819 }
4820 }
4821
4822 /* compile notifications */
4823 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4824 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004825 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004826 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004827 if (*notifs && (uses_p->augments || uses_p->refines)) {
4828 /* 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 +02004829 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4830 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4831 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004832 }
4833 }
4834
4835
Radek Krejci3641f562019-02-13 15:38:40 +01004836 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004837 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004838 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004839 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004840 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004841
Radek Krejcif0089082019-01-07 16:42:01 +01004842 /* reload previous context's mod_def */
4843 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004844 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004845
Radek Krejci76b3e962018-12-14 17:01:25 +01004846 /* apply refine */
4847 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004848 lysc_update_path(ctx, NULL, rfn->nodeid);
4849
Radek Krejci7af64242019-02-18 13:07:53 +01004850 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 +01004851 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004852 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004853 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004854
4855 /* default value */
4856 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004857 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004858 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004859 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4860 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004861 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004862 }
4863 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4864 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004865 "Invalid refine of default - %s cannot hold default value(s).",
4866 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004867 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004868 }
4869 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004870 struct ly_err_item *err = NULL;
4871 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4872 if (leaf->dflt) {
4873 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004874 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004875 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4876 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4877 } else {
4878 /* prepare a new one */
4879 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4880 leaf->dflt->realtype = leaf->type;
4881 }
4882 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004883 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004884 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4885 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004886 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004887 leaf->dflt->realtype->refcount++;
4888 if (err) {
4889 ly_err_print(err);
4890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4891 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4892 ly_err_free(err);
4893 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004894 if (rc == LY_EINCOMPLETE) {
4895 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004896 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004897
4898 /* but in general result is so far ok */
4899 rc = LY_SUCCESS;
4900 }
Radek Krejcia1911222019-07-22 17:24:50 +02004901 LY_CHECK_GOTO(rc, cleanup);
4902 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004903 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004904 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4905
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004906 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4908 "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 +02004909 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004910 }
Radek Krejcia1911222019-07-22 17:24:50 +02004911
4912 /* remove previous set of default values */
4913 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004914 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004915 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4916 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4917 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004918 }
Radek Krejcia1911222019-07-22 17:24:50 +02004919 LY_ARRAY_FREE(llist->dflts);
4920 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004921 LY_ARRAY_FREE(llist->dflts_mods);
4922 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004923
4924 /* create the new set of the default values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004925 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
4926 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004927 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004928 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004929 LY_ARRAY_INCREMENT(llist->dflts_mods);
4930 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004931 LY_ARRAY_INCREMENT(llist->dflts);
4932 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4933 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004934 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004935 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004936 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004937 llist->dflts[u]->realtype->refcount++;
4938 if (err) {
4939 ly_err_print(err);
4940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4941 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4942 ly_err_free(err);
4943 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004944 if (rc == LY_EINCOMPLETE) {
4945 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004946 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004947
4948 /* but in general result is so far ok */
4949 rc = LY_SUCCESS;
4950 }
4951 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004952 }
Radek Krejcia1911222019-07-22 17:24:50 +02004953 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004954 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004955 if (((struct lysc_node_choice*)node)->dflt) {
4956 /* unset LYS_SET_DFLT from the current default case */
4957 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4958 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004959 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 +01004960 }
4961 }
4962
Radek Krejci12fb9142019-01-08 09:45:30 +01004963 /* description */
4964 if (rfn->dsc) {
4965 FREE_STRING(ctx->ctx, node->dsc);
4966 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4967 }
4968
4969 /* reference */
4970 if (rfn->ref) {
4971 FREE_STRING(ctx->ctx, node->ref);
4972 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4973 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004974
4975 /* config */
4976 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004977 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004978 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004979 } else {
4980 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004981 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004982 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004983 }
4984
4985 /* mandatory */
4986 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004987 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004988 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004989
4990 /* presence */
4991 if (rfn->presence) {
4992 if (node->nodetype != LYS_CONTAINER) {
4993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004994 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4995 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004996 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004997 }
4998 node->flags |= LYS_PRESENCE;
4999 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005000
5001 /* must */
5002 if (rfn->musts) {
5003 switch (node->nodetype) {
5004 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005005 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 +01005006 break;
5007 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005008 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 +01005009 break;
5010 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005011 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 +01005012 break;
5013 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005014 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 +01005015 break;
5016 case LYS_ANYXML:
5017 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005018 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 +01005019 break;
5020 default:
5021 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005022 "Invalid refine of must statement - %s cannot hold any must statement.",
5023 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005024 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005025 }
Michal Vasko004d3152020-06-11 19:59:22 +02005026 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005027 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005028
5029 /* min/max-elements */
5030 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5031 switch (node->nodetype) {
5032 case LYS_LEAFLIST:
5033 if (rfn->flags & LYS_SET_MAX) {
5034 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5035 }
5036 if (rfn->flags & LYS_SET_MIN) {
5037 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005038 if (rfn->min) {
5039 node->flags |= LYS_MAND_TRUE;
5040 lys_compile_mandatory_parents(node->parent, 1);
5041 } else {
5042 node->flags &= ~LYS_MAND_TRUE;
5043 lys_compile_mandatory_parents(node->parent, 0);
5044 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005045 }
5046 break;
5047 case LYS_LIST:
5048 if (rfn->flags & LYS_SET_MAX) {
5049 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5050 }
5051 if (rfn->flags & LYS_SET_MIN) {
5052 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005053 if (rfn->min) {
5054 node->flags |= LYS_MAND_TRUE;
5055 lys_compile_mandatory_parents(node->parent, 1);
5056 } else {
5057 node->flags &= ~LYS_MAND_TRUE;
5058 lys_compile_mandatory_parents(node->parent, 0);
5059 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005060 }
5061 break;
5062 default:
5063 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005064 "Invalid refine of %s statement - %s cannot hold this statement.",
5065 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005066 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005067 }
5068 }
Radek Krejcif0089082019-01-07 16:42:01 +01005069
5070 /* if-feature */
5071 if (rfn->iffeatures) {
5072 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005073 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005074 }
Radek Krejci327de162019-06-14 12:52:07 +02005075
5076 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005077 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005078
Radek Krejcif2271f12019-01-07 16:42:23 +01005079 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005080 for (uint32_t i = 0; i < refined.count; ++i) {
5081 node = (struct lysc_node*)refined.objs[i];
5082 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005083 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005084
5085 /* check possible conflict with default value (default added, mandatory left true) */
5086 if ((node->flags & LYS_MAND_TRUE) &&
5087 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5088 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005090 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005091 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005092 }
5093
5094 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5095 if (node->nodetype == LYS_LIST) {
5096 min = ((struct lysc_node_list*)node)->min;
5097 max = ((struct lysc_node_list*)node)->max;
5098 } else {
5099 min = ((struct lysc_node_leaflist*)node)->min;
5100 max = ((struct lysc_node_leaflist*)node)->max;
5101 }
5102 if (min > max) {
5103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005104 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5105 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005106 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005107 }
5108 }
5109 }
5110
Radek Krejci327de162019-06-14 12:52:07 +02005111 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005112 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005113
5114cleanup:
5115 /* fix connection of the children nodes from fake context node back into the parent */
5116 if (context_node_fake.child) {
5117 context_node_fake.child->prev = child;
5118 }
5119 LY_LIST_FOR(context_node_fake.child, child) {
5120 child->parent = parent;
5121 }
5122
5123 if (uses_p->augments || uses_p->refines) {
5124 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005125 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005126 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005127 LY_ARRAY_FREE(context_node_fake.actions);
5128 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005129 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005130 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005131 LY_ARRAY_FREE(context_node_fake.notifs);
5132 }
5133 }
5134
Radek Krejcie86bf772018-12-14 11:39:53 +01005135 /* reload previous context's mod_def */
5136 ctx->mod_def = mod_old;
5137 /* remove the grouping from the stack for circular groupings dependency check */
5138 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5139 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005140 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005141 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005142
5143 return ret;
5144}
5145
Radek Krejci327de162019-06-14 12:52:07 +02005146static int
5147lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5148{
5149 struct lysp_node *iter;
5150 int len = 0;
5151
5152 *path = NULL;
5153 for (iter = node; iter && len >= 0; iter = iter->parent) {
5154 char *s = *path;
5155 char *id;
5156
5157 switch (iter->nodetype) {
5158 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005159 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005160 break;
5161 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005162 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005163 break;
5164 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005165 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005166 break;
5167 default:
5168 id = strdup(iter->name);
5169 break;
5170 }
5171
5172 if (!iter->parent) {
5173 /* print prefix */
5174 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5175 } else {
5176 /* prefix is the same as in parent */
5177 len = asprintf(path, "/%s%s", id, s ? s : "");
5178 }
5179 free(s);
5180 free(id);
5181 }
5182
5183 if (len < 0) {
5184 free(*path);
5185 *path = NULL;
5186 } else if (len == 0) {
5187 *path = strdup("/");
5188 len = 1;
5189 }
5190 return len;
5191}
5192
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005193/**
5194 * @brief Validate groupings that were defined but not directly used in the schema itself.
5195 *
5196 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5197 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5198 */
5199static LY_ERR
5200lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5201{
5202 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005203 char *path;
5204 int len;
5205
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005206 struct lysp_node_uses fake_uses = {
5207 .parent = node_p,
5208 .nodetype = LYS_USES,
5209 .flags = 0, .next = NULL,
5210 .name = grp->name,
5211 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5212 .refines = NULL, .augments = NULL
5213 };
5214 struct lysc_node_container fake_container = {
5215 .nodetype = LYS_CONTAINER,
5216 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5217 .module = ctx->mod,
5218 .sp = NULL, .parent = NULL, .next = NULL,
5219 .prev = (struct lysc_node*)&fake_container,
5220 .name = "fake",
5221 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5222 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5223 };
5224
5225 if (grp->parent) {
5226 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5227 }
Radek Krejci327de162019-06-14 12:52:07 +02005228
5229 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5230 if (len < 0) {
5231 LOGMEM(ctx->ctx);
5232 return LY_EMEM;
5233 }
5234 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5235 ctx->path_len = (uint16_t)len;
5236 free(path);
5237
5238 lysc_update_path(ctx, NULL, "{grouping}");
5239 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005240 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005241 lysc_update_path(ctx, NULL, NULL);
5242 lysc_update_path(ctx, NULL, NULL);
5243
5244 ctx->path_len = 1;
5245 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005246
5247 /* cleanup */
5248 lysc_node_container_free(ctx->ctx, &fake_container);
5249
5250 return ret;
5251}
Radek Krejcife909632019-02-12 15:34:42 +01005252
Radek Krejcie86bf772018-12-14 11:39:53 +01005253/**
Radek Krejcia3045382018-11-22 14:30:31 +01005254 * @brief Compile parsed schema node information.
5255 * @param[in] ctx Compile context
5256 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005257 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5258 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5259 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005260 * @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).
5261 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005262 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5263 */
Radek Krejci19a96102018-11-15 13:38:09 +01005264static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005265lys_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 +01005266{
Radek Krejci1c54f462020-05-12 17:25:34 +02005267 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005268 struct lysc_node *node;
5269 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005270 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005271 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005272 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005273
Radek Krejci327de162019-06-14 12:52:07 +02005274 if (node_p->nodetype != LYS_USES) {
5275 lysc_update_path(ctx, parent, node_p->name);
5276 } else {
5277 lysc_update_path(ctx, NULL, "{uses}");
5278 lysc_update_path(ctx, NULL, node_p->name);
5279 }
5280
Radek Krejci19a96102018-11-15 13:38:09 +01005281 switch (node_p->nodetype) {
5282 case LYS_CONTAINER:
5283 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5284 node_compile_spec = lys_compile_node_container;
5285 break;
5286 case LYS_LEAF:
5287 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5288 node_compile_spec = lys_compile_node_leaf;
5289 break;
5290 case LYS_LIST:
5291 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005292 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005293 break;
5294 case LYS_LEAFLIST:
5295 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005296 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005297 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005298 case LYS_CHOICE:
5299 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005300 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005301 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005302 case LYS_ANYXML:
5303 case LYS_ANYDATA:
5304 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005305 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005306 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005307 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005308 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5309 lysc_update_path(ctx, NULL, NULL);
5310 lysc_update_path(ctx, NULL, NULL);
5311 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005312 default:
5313 LOGINT(ctx->ctx);
5314 return LY_EINT;
5315 }
5316 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5317 node->nodetype = node_p->nodetype;
5318 node->module = ctx->mod;
5319 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005320 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005321
5322 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005323 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005324 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005325 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005326 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5327 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005328 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005329 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005330 node->flags |= LYS_CONFIG_R;
5331 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005332 /* config not explicitely set, inherit it from parent */
5333 if (parent) {
5334 node->flags |= parent->flags & LYS_CONFIG_MASK;
5335 } else {
5336 /* default is config true */
5337 node->flags |= LYS_CONFIG_W;
5338 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005339 } else {
5340 /* config set explicitely */
5341 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005342 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005343 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5344 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5345 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005346 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005347 goto error;
5348 }
Radek Krejci19a96102018-11-15 13:38:09 +01005349
Radek Krejcia6d57732018-11-29 13:40:37 +01005350 /* *list ordering */
5351 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5352 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005353 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005354 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5355 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005356 node->flags &= ~LYS_ORDBY_MASK;
5357 node->flags |= LYS_ORDBY_SYSTEM;
5358 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5359 /* default ordering is system */
5360 node->flags |= LYS_ORDBY_SYSTEM;
5361 }
5362 }
5363
Radek Krejci19a96102018-11-15 13:38:09 +01005364 /* status - it is not inherited by specification, but it does not make sense to have
5365 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005366 if (!parent || parent->nodetype != LYS_CHOICE) {
5367 /* in case of choice/case's children, postpone the check to the moment we know if
5368 * 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 +02005369 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 +01005370 }
5371
Radek Krejciec4da802019-05-02 13:02:41 +02005372 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005373 node->sp = node_p;
5374 }
5375 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005376 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5377 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005378 if (node_p->when) {
5379 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005380 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005381
5382 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5383 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005384 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005385 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005386 }
Radek Krejciec4da802019-05-02 13:02:41 +02005387 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005388
5389 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005390 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005391
Radek Krejci0935f412019-08-20 16:15:18 +02005392 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5393
Radek Krejcife909632019-02-12 15:34:42 +01005394 /* inherit LYS_MAND_TRUE in parent containers */
5395 if (node->flags & LYS_MAND_TRUE) {
5396 lys_compile_mandatory_parents(parent, 1);
5397 }
5398
Radek Krejci327de162019-06-14 12:52:07 +02005399 lysc_update_path(ctx, NULL, NULL);
5400
Radek Krejci19a96102018-11-15 13:38:09 +01005401 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005402 if (parent) {
5403 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005404 if (node_p->parent->nodetype == LYS_CASE) {
5405 lysc_update_path(ctx, parent, node_p->parent->name);
5406 } else {
5407 lysc_update_path(ctx, parent, node->name);
5408 }
Radek Krejciec4da802019-05-02 13:02:41 +02005409 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005410 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005411 if (uses_status) {
5412
5413 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005414 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005415 * it directly gets the same status flags as the choice;
5416 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005417 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005418 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005419 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005420 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005421 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005422 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005423 }
Radek Krejciec4da802019-05-02 13:02:41 +02005424 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 +02005425
5426 if (parent->nodetype == LYS_CHOICE) {
5427 lysc_update_path(ctx, NULL, NULL);
5428 }
Radek Krejci19a96102018-11-15 13:38:09 +01005429 } else {
5430 /* top-level element */
5431 if (!ctx->mod->compiled->data) {
5432 ctx->mod->compiled->data = node;
5433 } else {
5434 /* insert at the end of the module's top-level nodes list */
5435 ctx->mod->compiled->data->prev->next = node;
5436 node->prev = ctx->mod->compiled->data->prev;
5437 ctx->mod->compiled->data->prev = node;
5438 }
Radek Krejci327de162019-06-14 12:52:07 +02005439 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005440 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5441 ctx->mod->compiled->notifs, node->name, node)) {
5442 return LY_EVALID;
5443 }
Radek Krejci19a96102018-11-15 13:38:09 +01005444 }
Radek Krejci327de162019-06-14 12:52:07 +02005445 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005446
5447 return LY_SUCCESS;
5448
5449error:
5450 lysc_node_free(ctx->ctx, node);
5451 return ret;
5452}
5453
Radek Krejciccd20f12019-02-15 14:12:27 +01005454static void
5455lysc_disconnect(struct lysc_node *node)
5456{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005457 struct lysc_node *parent, *child, *prev = NULL, *next;
5458 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005459 int remove_cs = 0;
5460
5461 parent = node->parent;
5462
5463 /* parent's first child */
5464 if (!parent) {
5465 return;
5466 }
5467 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005468 cs = (struct lysc_node_case*)node;
5469 } else if (parent->nodetype == LYS_CASE) {
5470 /* disconnecting some node in a case */
5471 cs = (struct lysc_node_case*)parent;
5472 parent = cs->parent;
5473 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5474 if (child == node) {
5475 if (cs->child == child) {
5476 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5477 /* case with a single child -> remove also the case */
5478 child->parent = NULL;
5479 remove_cs = 1;
5480 } else {
5481 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005482 }
5483 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005484 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005485 }
5486 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005487 if (!remove_cs) {
5488 cs = NULL;
5489 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005490 } else if (lysc_node_children(parent, node->flags) == node) {
5491 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005492 }
5493
5494 if (cs) {
5495 if (remove_cs) {
5496 /* cs has only one child which is being also removed */
5497 lysc_disconnect((struct lysc_node*)cs);
5498 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5499 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005500 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5501 /* default case removed */
5502 ((struct lysc_node_choice*)parent)->dflt = NULL;
5503 }
5504 if (((struct lysc_node_choice*)parent)->cases == cs) {
5505 /* first case removed */
5506 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5507 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005508 if (cs->child) {
5509 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5510 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5511 prev = cs->child->prev;
5512 } /* else all the children are under a single case */
5513 LY_LIST_FOR_SAFE(cs->child, next, child) {
5514 if (child->parent != (struct lysc_node*)cs) {
5515 break;
5516 }
5517 lysc_node_free(node->module->ctx, child);
5518 }
5519 if (prev) {
5520 if (prev->next) {
5521 prev->next = child;
5522 }
5523 if (child) {
5524 child->prev = prev;
5525 } else {
5526 /* link from the first child under the cases */
5527 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5528 }
5529 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005530 }
5531 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005532 }
5533
5534 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005535 if (node->prev->next) {
5536 node->prev->next = node->next;
5537 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005538 if (node->next) {
5539 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005540 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005541 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005542 if (child) {
5543 child->prev = node->prev;
5544 }
5545 } else if (((struct lysc_node_choice*)parent)->cases) {
5546 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005547 }
5548}
5549
5550LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005551lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005552{
Radek Krejcia1911222019-07-22 17:24:50 +02005553 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005554 struct ly_set devs_p = {0};
5555 struct ly_set targets = {0};
5556 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005557 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005558 struct lysc_action *rpcs;
5559 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005560 struct lysp_deviation *dev;
5561 struct lysp_deviate *d, **dp_new;
5562 struct lysp_deviate_add *d_add;
5563 struct lysp_deviate_del *d_del;
5564 struct lysp_deviate_rpl *d_rpl;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005565 LY_ARRAY_COUNT_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005566 struct lysc_deviation {
5567 const char *nodeid;
5568 struct lysc_node *target; /* target node of the deviation */
5569 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005570 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005571 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5572 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005573 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005574 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005575 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005576 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005577 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005578 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005579
5580 /* get all deviations from the module and all its submodules ... */
5581 LY_ARRAY_FOR(mod_p->deviations, u) {
5582 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5583 }
5584 LY_ARRAY_FOR(mod_p->includes, v) {
5585 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5586 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5587 }
5588 }
5589 if (!devs_p.count) {
5590 /* nothing to do */
5591 return LY_SUCCESS;
5592 }
5593
Radek Krejci327de162019-06-14 12:52:07 +02005594 lysc_update_path(ctx, NULL, "{deviation}");
5595
Radek Krejciccd20f12019-02-15 14:12:27 +01005596 /* ... and group them by the target node */
5597 devs = calloc(devs_p.count, sizeof *devs);
5598 for (u = 0; u < devs_p.count; ++u) {
5599 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005600 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005601
5602 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005603 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5604 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005605 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005606 /* move the target pointer to input/output to make them different from the action and
5607 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5608 * back to the RPC/action node due to a better compatibility and decision code in this function.
5609 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5610 if (flags & LYSC_OPT_RPC_INPUT) {
5611 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5612 flags |= LYSC_OPT_INTERNAL;
5613 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5614 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5615 flags |= LYSC_OPT_INTERNAL;
5616 }
5617 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005618 /* insert into the set of targets with duplicity detection */
5619 i = ly_set_add(&targets, target, 0);
5620 if (!devs[i]) {
5621 /* new record */
5622 devs[i] = calloc(1, sizeof **devs);
5623 devs[i]->target = target;
5624 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005625 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005626 }
5627 /* add deviates into the deviation's list of deviates */
5628 for (d = dev->deviates; d; d = d->next) {
5629 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5630 *dp_new = d;
5631 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5632 devs[i]->not_supported = 1;
5633 }
5634 }
Radek Krejci327de162019-06-14 12:52:07 +02005635
5636 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005637 }
5638
5639 /* MACROS for deviates checking */
5640#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5641 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
Radek Krejciccd20f12019-02-15 14:12:27 +01005643 goto cleanup; \
5644 }
5645
5646#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005647 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5648 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5649 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005650 goto cleanup; \
5651 }
5652
Radek Krejcia1911222019-07-22 17:24:50 +02005653
Radek Krejciccd20f12019-02-15 14:12:27 +01005654#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005655 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5657 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5658 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5659 goto cleanup; \
5660 }
5661
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005662#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005663 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005664 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005665 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5666 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005668 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5669 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005670 goto cleanup; \
5671 }
5672
Radek Krejci551b12c2019-02-19 16:11:21 +01005673#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5674 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005676 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5677 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005678 goto cleanup; \
5679 }
5680
Radek Krejciccd20f12019-02-15 14:12:27 +01005681#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5682 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005684 goto cleanup; \
5685 }
5686
Radek Krejci551b12c2019-02-19 16:11:21 +01005687#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5688 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005690 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005691 goto cleanup; \
5692 }
5693
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005694#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5695 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5696 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5697 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5698 if (!strcmp(((TYPE)devs[u]->target)->ARRAY_TRG[y]VALMEMBER_CMP, d_del->ARRAY_DEV[x]VALMEMBER)) { break; } \
Radek Krejciccd20f12019-02-15 14:12:27 +01005699 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005700 if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005702 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5703 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005704 goto cleanup; \
5705 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005706 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5707 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5708 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5709 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005710 (LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005711 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005712 if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005713 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5714 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005715 }
5716
5717 /* apply deviations */
5718 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005719 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5720 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5721 struct ly_err_item *err = NULL;
5722
5723 dflt = NULL;
5724 changed_type = 0;
5725
Radek Krejci327de162019-06-14 12:52:07 +02005726 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5727
Radek Krejcif538ce52019-03-05 10:46:14 +01005728 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5729 /* fix the target pointer in case of RPC's/action's input/output */
5730 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5731 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5732 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5733 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5734 }
5735 }
5736
Radek Krejciccd20f12019-02-15 14:12:27 +01005737 /* not-supported */
5738 if (devs[u]->not_supported) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005739 if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) {
5740 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
5741 LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005742 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005743
5744#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5745 if (devs[u]->target->parent) { \
5746 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5747 } else { \
5748 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5749 } \
5750 LY_ARRAY_FOR(ARRAY, x) { \
5751 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5752 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005753 if (x < LY_ARRAY_COUNT(ARRAY)) { \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005754 FREEFUNC(ctx->ctx, &ARRAY[x]); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005755 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005756 LY_ARRAY_DECREMENT(ARRAY); \
5757 }
5758
Michal Vasko1bf09392020-03-27 12:38:10 +01005759 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005760 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5761 /* remove RPC's/action's input */
5762 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5763 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005764 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5765 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005766 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5767 /* remove RPC's/action's output */
5768 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5769 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005770 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5771 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005772 } else {
5773 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005774 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005775 }
5776 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005777 /* remove Notification */
5778 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005779 } else {
5780 /* remove the target node */
5781 lysc_disconnect(devs[u]->target);
5782 lysc_node_free(ctx->ctx, devs[u]->target);
5783 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005784
Radek Krejci474f9b82019-07-24 11:36:37 +02005785 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5786 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005787 continue;
5788 }
5789
5790 /* list of deviates (not-supported is not present in the list) */
5791 LY_ARRAY_FOR(devs[u]->deviates, v) {
5792 d = devs[u]->deviates[v];
5793
5794 switch (d->mod) {
5795 case LYS_DEV_ADD:
5796 d_add = (struct lysp_deviate_add*)d;
5797 /* [units-stmt] */
5798 if (d_add->units) {
5799 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5800 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5801
5802 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5803 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5804 }
5805
5806 /* *must-stmt */
5807 if (d_add->musts) {
5808 switch (devs[u]->target->nodetype) {
5809 case LYS_CONTAINER:
5810 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005811 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5812 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005813 break;
5814 case LYS_LEAF:
5815 case LYS_LEAFLIST:
5816 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005817 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5818 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005819 break;
5820 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005821 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5822 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005823 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005824 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005825 case LYS_ACTION:
5826 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005827 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5828 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005829 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005830 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005831 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5832 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005833 break;
5834 }
5835 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005836 default:
5837 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005838 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005839 goto cleanup;
5840 }
Michal Vasko004d3152020-06-11 19:59:22 +02005841 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005842 }
5843
5844 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005845 if (d_add->uniques) {
5846 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5847 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5848 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005849
5850 /* *default-stmt */
5851 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005852 switch (devs[u]->target->nodetype) {
5853 case LYS_LEAF:
5854 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005855 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod);
Radek Krejcia1911222019-07-22 17:24:50 +02005856 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005857 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005858 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005859 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5860 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5861 } else {
5862 /* prepare new default value storage */
5863 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005864 }
Radek Krejcia1911222019-07-22 17:24:50 +02005865 dflt = d_add->dflts[0];
5866 /* parsing is done at the end after possible replace of the leaf's type */
5867
Radek Krejci551b12c2019-02-19 16:11:21 +01005868 /* mark the new default values as leaf's own */
5869 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005870 break;
5871 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005872 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005873 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005874 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005875 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005876 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5877 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5878 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005879 }
Radek Krejcia1911222019-07-22 17:24:50 +02005880 LY_ARRAY_FREE(llist->dflts);
5881 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005882 LY_ARRAY_FREE(llist->dflts_mods);
5883 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005884 }
5885 /* add new default value(s) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005886 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5887 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5888 for (x = y = LY_ARRAY_COUNT(llist->dflts);
5889 x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005890 LY_ARRAY_INCREMENT(llist->dflts_mods);
5891 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005892 LY_ARRAY_INCREMENT(llist->dflts);
5893 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5894 llist->dflts[x]->realtype = llist->type;
5895 rc = llist->type->plugin->store(ctx->ctx, llist->type, d_add->dflts[x - y], strlen(d_add->dflts[x - y]),
Radek Krejci474f9b82019-07-24 11:36:37 +02005896 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005897 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005898 llist->dflts[x]->realtype->refcount++;
5899 if (err) {
5900 ly_err_print(err);
5901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5902 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5903 d_add->dflts[x - y], err->msg);
5904 ly_err_free(err);
5905 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005906 if (rc == LY_EINCOMPLETE) {
5907 /* postpone default compilation when the tree is complete */
5908 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5909
5910 /* but in general result is so far ok */
5911 rc = LY_SUCCESS;
5912 }
Radek Krejcia1911222019-07-22 17:24:50 +02005913 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005914 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005915 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005916 devs[u]->target->flags |= LYS_SET_DFLT;
5917 break;
5918 case LYS_CHOICE:
5919 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5920 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5921 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5922 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005923 if (lys_compile_deviation_set_choice_dflt(ctx, d_add->dflts[0], (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005924 goto cleanup;
5925 }
5926 break;
5927 default:
5928 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005929 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005930 goto cleanup;
5931 }
5932 }
5933
5934 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005935 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005936 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005938 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5939 goto cleanup;
5940 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005941 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005942 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005943 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005944 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005945 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005947 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5948 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005949 goto cleanup;
5950 }
Radek Krejci327de162019-06-14 12:52:07 +02005951 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005952 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005953
5954 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005955 if (d_add->flags & LYS_MAND_MASK) {
5956 if (devs[u]->target->flags & LYS_MAND_MASK) {
5957 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005958 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5959 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005960 goto cleanup;
5961 }
Radek Krejci327de162019-06-14 12:52:07 +02005962 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005963 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005964
5965 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005966 if (d_add->flags & LYS_SET_MIN) {
5967 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5968 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5969 /* change value */
5970 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5971 } else if (devs[u]->target->nodetype == LYS_LIST) {
5972 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5973 /* change value */
5974 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5975 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005976 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005977 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5978 goto cleanup;
5979 }
5980 if (d_add->min) {
5981 devs[u]->target->flags |= LYS_MAND_TRUE;
5982 }
5983 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005984
5985 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005986 if (d_add->flags & LYS_SET_MAX) {
5987 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5988 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5989 /* change value */
5990 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5991 } else if (devs[u]->target->nodetype == LYS_LIST) {
5992 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5993 /* change value */
5994 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5995 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005997 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5998 goto cleanup;
5999 }
6000 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006001
6002 break;
6003 case LYS_DEV_DELETE:
6004 d_del = (struct lysp_deviate_del*)d;
6005
6006 /* [units-stmt] */
6007 if (d_del->units) {
6008 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006009 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6010 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6012 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6013 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6014 goto cleanup;
6015 }
6016 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6017 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006018 }
6019
6020 /* *must-stmt */
6021 if (d_del->musts) {
6022 switch (devs[u]->target->nodetype) {
6023 case LYS_CONTAINER:
6024 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006025 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006026 break;
6027 case LYS_LEAF:
6028 case LYS_LEAFLIST:
6029 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006030 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006031 break;
6032 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006033 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006034 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006035 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006036 case LYS_ACTION:
6037 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6038 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6039 break;
6040 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6041 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6042 break;
6043 }
6044 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006045 default:
6046 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006047 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006048 goto cleanup;
6049 }
6050 }
6051
6052 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006053 if (d_del->uniques) {
6054 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6055 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6056 LY_ARRAY_FOR(d_del->uniques, x) {
6057 LY_ARRAY_FOR(list->uniques, z) {
6058 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6059 nodeid = strpbrk(name, " \t\n");
6060 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006061 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006062 break;
6063 }
6064 while (isspace(*nodeid)) {
6065 ++nodeid;
6066 }
6067 } else {
6068 if (strcmp(name, list->uniques[z][y]->name)) {
6069 break;
6070 }
6071 }
6072 }
6073 if (!name) {
6074 /* complete match - remove the unique */
6075 LY_ARRAY_DECREMENT(list->uniques);
6076 LY_ARRAY_FREE(list->uniques[z]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006077 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
Radek Krejci7af64242019-02-18 13:07:53 +01006078 --z;
6079 break;
6080 }
6081 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006082 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006084 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6085 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006086 goto cleanup;
6087 }
6088 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006089 if (!LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006090 LY_ARRAY_FREE(list->uniques);
6091 list->uniques = NULL;
6092 }
6093 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006094
6095 /* *default-stmt */
6096 if (d_del->dflts) {
6097 switch (devs[u]->target->nodetype) {
6098 case LYS_LEAF:
6099 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6100 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6101 dflt, "deleting", "default", d_del->dflts[0]);
6102
Radek Krejcia1911222019-07-22 17:24:50 +02006103 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006104 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006105 if (strcmp(dflt, d_del->dflts[0])) {
6106 if (i) {
6107 free((char*)dflt);
6108 }
6109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6110 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6111 d_del->dflts[0], dflt);
6112 goto cleanup;
6113 }
6114 if (i) {
6115 free((char*)dflt);
6116 }
6117 dflt = NULL;
6118
Radek Krejci474f9b82019-07-24 11:36:37 +02006119 /* update the list of incomplete default values if needed */
6120 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6121
Radek Krejcia1911222019-07-22 17:24:50 +02006122 /* remove the default specification */
6123 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6124 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6125 free(leaf->dflt);
6126 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006127 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006128 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006129 break;
6130 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006131 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6132 LY_ARRAY_FOR(d_del->dflts, x) {
6133 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006134 dflt = llist->type->plugin->print(llist->dflts[y], LYD_XML, lys_get_prefix, llist->dflts_mods[y], &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006135 if (!strcmp(dflt, d_del->dflts[x])) {
6136 if (i) {
6137 free((char*)dflt);
6138 }
6139 dflt = NULL;
6140 break;
6141 }
6142 if (i) {
6143 free((char*)dflt);
6144 }
6145 dflt = NULL;
6146 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006147 if (y == LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02006148 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6149 "which does not match any of the target's property values.", d_del->dflts[x]);
6150 goto cleanup;
6151 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006152
6153 /* update the list of incomplete default values if needed */
6154 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6155
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006156 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006157 LY_ARRAY_DECREMENT(llist->dflts);
6158 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6159 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6160 free(llist->dflts[y]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006161 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
6162 memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_COUNT(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods));
Radek Krejcia1911222019-07-22 17:24:50 +02006163 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006164 if (!LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006165 LY_ARRAY_FREE(llist->dflts_mods);
6166 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006167 LY_ARRAY_FREE(llist->dflts);
6168 llist->dflts = NULL;
6169 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006170 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006171 break;
6172 case LYS_CHOICE:
6173 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6174 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6175 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006176 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006177 if (prefix) {
6178 /* use module prefixes from the deviation module to match the module of the default case */
6179 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6180 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006181 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6182 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006183 goto cleanup;
6184 }
6185 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6186 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006187 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6188 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006189 goto cleanup;
6190 }
6191 }
6192 /* else {
6193 * strictly, the default prefix would point to the deviation module, but the value should actually
6194 * match the default string in the original module (usually unprefixed), so in this case we do not check
6195 * the module of the default case, just matching its name */
6196 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6197 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006198 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6199 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006200 goto cleanup;
6201 }
6202 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6203 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6204 break;
6205 default:
6206 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006207 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006208 goto cleanup;
6209 }
6210 }
6211
6212 break;
6213 case LYS_DEV_REPLACE:
6214 d_rpl = (struct lysp_deviate_rpl*)d;
6215
6216 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006217 if (d_rpl->type) {
6218 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6219 /* type is mandatory, so checking for its presence is not necessary */
6220 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006221
6222 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6223 /* the target has default from the previous type - remove it */
6224 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006225 /* update the list of incomplete default values if needed */
6226 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6227
Radek Krejcia1911222019-07-22 17:24:50 +02006228 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6229 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6230 free(leaf->dflt);
6231 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006232 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006233 } else { /* LYS_LEAFLIST */
6234 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006235 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006236 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6237 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6238 free(llist->dflts[x]);
6239 }
6240 LY_ARRAY_FREE(llist->dflts);
6241 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006242 LY_ARRAY_FREE(llist->dflts_mods);
6243 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006244 }
6245 }
6246 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006247 /* there is no default value, do not set changed_type after type compilation
6248 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006249 changed_type = -1;
6250 }
Radek Krejciec4da802019-05-02 13:02:41 +02006251 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006252 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006253 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006254
6255 /* [units-stmt] */
6256 if (d_rpl->units) {
6257 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6258 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6259 units, "replacing", "units", d_rpl->units);
6260
6261 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6262 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6263 }
6264
6265 /* [default-stmt] */
6266 if (d_rpl->dflt) {
6267 switch (devs[u]->target->nodetype) {
6268 case LYS_LEAF:
6269 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6270 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006271 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006272 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006273 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6274 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6275 dflt = d_rpl->dflt;
6276 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006277 break;
6278 case LYS_CHOICE:
6279 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006280 if (lys_compile_deviation_set_choice_dflt(ctx, d_rpl->dflt, (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006281 goto cleanup;
6282 }
6283 break;
6284 default:
6285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006286 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006287 goto cleanup;
6288 }
6289 }
6290
6291 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006292 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006293 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006294 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006295 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6296 goto cleanup;
6297 }
6298 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006299 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006300 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6301 goto cleanup;
6302 }
Radek Krejci327de162019-06-14 12:52:07 +02006303 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006304 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006305
6306 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006307 if (d_rpl->flags & LYS_MAND_MASK) {
6308 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006310 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6311 goto cleanup;
6312 }
Radek Krejci327de162019-06-14 12:52:07 +02006313 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006314 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006315
6316 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006317 if (d_rpl->flags & LYS_SET_MIN) {
6318 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6319 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6320 /* change value */
6321 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6322 } else if (devs[u]->target->nodetype == LYS_LIST) {
6323 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6324 /* change value */
6325 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6326 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006327 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006328 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6329 goto cleanup;
6330 }
6331 if (d_rpl->min) {
6332 devs[u]->target->flags |= LYS_MAND_TRUE;
6333 }
6334 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006335
6336 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006337 if (d_rpl->flags & LYS_SET_MAX) {
6338 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6339 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6340 /* change value */
6341 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6342 } else if (devs[u]->target->nodetype == LYS_LIST) {
6343 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6344 /* change value */
6345 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6346 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006347 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006348 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6349 goto cleanup;
6350 }
6351 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006352
6353 break;
6354 default:
6355 LOGINT(ctx->ctx);
6356 goto cleanup;
6357 }
6358 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006359
Radek Krejci33f72892019-02-21 10:36:58 +01006360 /* final check when all deviations of a single target node are applied */
6361
Radek Krejci551b12c2019-02-19 16:11:21 +01006362 /* check min-max compatibility */
6363 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6364 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6365 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6366 } else if (devs[u]->target->nodetype == LYS_LIST) {
6367 min = ((struct lysc_node_list*)devs[u]->target)->min;
6368 max = ((struct lysc_node_list*)devs[u]->target)->max;
6369 } else {
6370 min = max = 0;
6371 }
6372 if (min > max) {
6373 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
Radek Krejci327de162019-06-14 12:52:07 +02006374 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006375 goto cleanup;
6376 }
6377
Radek Krejcia1911222019-07-22 17:24:50 +02006378 if (dflt) {
6379 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006380 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006381 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006382 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6383 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006384 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006385 leaf->dflt->realtype->refcount++;
6386 if (err) {
6387 ly_err_print(err);
6388 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6389 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6390 ly_err_free(err);
6391 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006392 if (rc == LY_EINCOMPLETE) {
6393 /* postpone default compilation when the tree is complete */
6394 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6395
6396 /* but in general result is so far ok */
6397 rc = LY_SUCCESS;
6398 }
Radek Krejcia1911222019-07-22 17:24:50 +02006399 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006400 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006401 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6402 int dynamic;
6403 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006404 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006405
6406 /* update the list of incomplete default values if needed */
6407 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6408
6409 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006410 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6411 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6412 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006413 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6414 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006415 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006416 leaf->dflt->realtype->refcount++;
6417 if (err) {
6418 ly_err_print(err);
6419 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6420 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6421 ly_err_free(err);
6422 }
6423 if (dynamic) {
6424 free((void*)dflt);
6425 }
Radek Krejcia1911222019-07-22 17:24:50 +02006426 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006427 if (rc == LY_EINCOMPLETE) {
6428 /* postpone default compilation when the tree is complete */
6429 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6430
6431 /* but in general result is so far ok */
6432 rc = LY_SUCCESS;
6433 }
6434 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006435 } else { /* LYS_LEAFLIST */
6436 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006437 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LYD_XML, lys_get_prefix, llist->dflts_mods[x], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02006438 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6439 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6440 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006441 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6442 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006443 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6444 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006445 llist->dflts[x]->realtype->refcount++;
6446 if (err) {
6447 ly_err_print(err);
6448 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6449 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6450 dflt, err->msg);
6451 ly_err_free(err);
6452 }
6453 if (dynamic) {
6454 free((void*)dflt);
6455 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006456 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006457 if (rc == LY_EINCOMPLETE) {
6458 /* postpone default compilation when the tree is complete */
6459 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6460
6461 /* but in general result is so far ok */
6462 rc = LY_SUCCESS;
6463 }
Radek Krejcia1911222019-07-22 17:24:50 +02006464 LY_CHECK_GOTO(rc, cleanup);
6465 }
6466 }
6467 }
6468
Radek Krejci551b12c2019-02-19 16:11:21 +01006469 /* check mandatory - default compatibility */
6470 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6471 && (devs[u]->target->flags & LYS_SET_DFLT)
6472 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6473 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006474 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006475 goto cleanup;
6476 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6477 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6478 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
Radek Krejci551b12c2019-02-19 16:11:21 +01006480 goto cleanup;
6481 }
6482 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6483 /* mandatory node under a default case */
6484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006485 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6486 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006487 goto cleanup;
6488 }
Radek Krejci33f72892019-02-21 10:36:58 +01006489
Michal Vaskoe6143202020-07-03 13:02:08 +02006490 /* add this module into the target module deviated_by, if not there already */
6491 rc = LY_SUCCESS;
6492 LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) {
6493 if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) {
6494 rc = LY_EEXIST;
6495 break;
6496 }
6497 }
6498 if (!rc) {
6499 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6500 *dev_mod = mod_p->mod;
6501 }
Michal Vasko57c10cd2020-05-27 15:57:11 +02006502
Radek Krejci327de162019-06-14 12:52:07 +02006503 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006504 }
6505
Radek Krejci327de162019-06-14 12:52:07 +02006506 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006507 ret = LY_SUCCESS;
6508
6509cleanup:
6510 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6511 LY_ARRAY_FREE(devs[u]->deviates);
6512 free(devs[u]);
6513 }
6514 free(devs);
6515 ly_set_erase(&targets, NULL);
6516 ly_set_erase(&devs_p, NULL);
6517
6518 return ret;
6519}
6520
Radek Krejcib56c7502019-02-13 14:19:54 +01006521/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006522 * @brief Compile the given YANG submodule into the main module.
6523 * @param[in] ctx Compile context
6524 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006525 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6526 */
6527LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006528lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006529{
6530 unsigned int u;
6531 LY_ERR ret = LY_SUCCESS;
6532 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006533 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006534 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006535 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006536
Michal Vasko33ff9422020-07-03 09:50:39 +02006537 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006538 /* features are compiled directly into the compiled module structure,
6539 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6540 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006541 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6542 LY_CHECK_GOTO(ret, error);
6543 }
Radek Krejci0af46292019-01-11 16:02:31 +01006544
Michal Vasko33ff9422020-07-03 09:50:39 +02006545 if (!mainmod->mod->dis_identities) {
6546 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6547 LY_CHECK_GOTO(ret, error);
6548 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006549
Radek Krejci474f9b82019-07-24 11:36:37 +02006550 /* data nodes */
6551 LY_LIST_FOR(submod->data, node_p) {
6552 ret = lys_compile_node(ctx, node_p, NULL, 0);
6553 LY_CHECK_GOTO(ret, error);
6554 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006555
Radek Krejciec4da802019-05-02 13:02:41 +02006556 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6557 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006558
Radek Krejcid05cbd92018-12-05 14:26:40 +01006559error:
6560 return ret;
6561}
6562
Radek Krejci335332a2019-09-05 13:03:35 +02006563static void *
6564lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6565{
6566 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6567 if (substmts[u].stmt == stmt) {
6568 return substmts[u].storage;
6569 }
6570 }
6571 return NULL;
6572}
6573
6574LY_ERR
6575lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6576{
6577 LY_ERR ret = LY_EVALID, r;
6578 unsigned int u;
6579 struct lysp_stmt *stmt;
6580 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006581
6582 /* check for invalid substatements */
6583 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006584 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6585 continue;
6586 }
Radek Krejci335332a2019-09-05 13:03:35 +02006587 for (u = 0; substmts[u].stmt; ++u) {
6588 if (substmts[u].stmt == stmt->kw) {
6589 break;
6590 }
6591 }
6592 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6594 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006595 goto cleanup;
6596 }
Radek Krejci335332a2019-09-05 13:03:35 +02006597 }
6598
Radek Krejciad5963b2019-09-06 16:03:05 +02006599 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6600
Radek Krejci335332a2019-09-05 13:03:35 +02006601 /* keep order of the processing the same as the order in the defined substmts,
6602 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6603 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006604 int stmt_present = 0;
6605
Radek Krejci335332a2019-09-05 13:03:35 +02006606 for (stmt = ext->child; stmt; stmt = stmt->next) {
6607 if (substmts[u].stmt != stmt->kw) {
6608 continue;
6609 }
6610
Radek Krejciad5963b2019-09-06 16:03:05 +02006611 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006612 if (substmts[u].storage) {
6613 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006614 case LY_STMT_STATUS:
6615 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6616 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6617 break;
6618 case LY_STMT_UNITS: {
6619 const char **units;
6620
6621 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6622 /* single item */
6623 if (*((const char **)substmts[u].storage)) {
6624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6625 goto cleanup;
6626 }
6627 units = (const char **)substmts[u].storage;
6628 } else {
6629 /* sized array */
6630 const char ***units_array = (const char ***)substmts[u].storage;
6631 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6632 }
6633 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6634 break;
6635 }
Radek Krejci335332a2019-09-05 13:03:35 +02006636 case LY_STMT_TYPE: {
6637 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6638 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6639
6640 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6641 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006642 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6644 goto cleanup;
6645 }
6646 compiled = substmts[u].storage;
6647 } else {
6648 /* sized array */
6649 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6650 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6651 compiled = (void*)type;
6652 }
6653
Radek Krejciad5963b2019-09-06 16:03:05 +02006654 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006655 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6656 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006657 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6658 lysp_type_free(ctx->ctx, parsed);
6659 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006660 break;
6661 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006662 case LY_STMT_IF_FEATURE: {
6663 struct lysc_iffeature *iff = NULL;
6664
6665 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6666 /* single item */
6667 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6669 goto cleanup;
6670 }
6671 iff = (struct lysc_iffeature*)substmts[u].storage;
6672 } else {
6673 /* sized array */
6674 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6675 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6676 }
6677 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6678 break;
6679 }
6680 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6681 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006682 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006683 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.",
6684 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006685 goto cleanup;
6686 }
6687 }
Radek Krejci335332a2019-09-05 13:03:35 +02006688 }
Radek Krejci335332a2019-09-05 13:03:35 +02006689
Radek Krejciad5963b2019-09-06 16:03:05 +02006690 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6692 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6693 goto cleanup;
6694 }
Radek Krejci335332a2019-09-05 13:03:35 +02006695 }
6696
6697 ret = LY_SUCCESS;
6698
6699cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006700 return ret;
6701}
6702
Michal Vasko175012e2019-11-06 15:49:14 +01006703/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006704 * @brief Check when for cyclic dependencies.
6705 * @param[in] set Set with all the referenced nodes.
6706 * @param[in] node Node whose "when" referenced nodes are in @p set.
6707 * @return LY_ERR value
6708 */
6709static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006710lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006711{
6712 struct lyxp_set tmp_set;
6713 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006714 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006715 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006716 int idx;
6717 struct lysc_when *when;
6718 LY_ERR ret = LY_SUCCESS;
6719
6720 memset(&tmp_set, 0, sizeof tmp_set);
6721
6722 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006723 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006724 xp_scnode = &set->val.scnodes[i];
6725
Michal Vasko5c4e5892019-11-14 12:31:38 +01006726 if (xp_scnode->in_ctx != -1) {
6727 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006728 xp_scnode->in_ctx = 1;
6729 }
6730 }
6731
6732 for (i = 0; i < set->used; ++i) {
6733 xp_scnode = &set->val.scnodes[i];
6734 if (xp_scnode->in_ctx != 1) {
6735 /* already checked */
6736 continue;
6737 }
6738
Michal Vasko1bf09392020-03-27 12:38:10 +01006739 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006740 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006741 /* no when to check */
6742 xp_scnode->in_ctx = 0;
6743 continue;
6744 }
6745
6746 node = xp_scnode->scnode;
6747 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006748 LY_ARRAY_FOR(node->when, u) {
6749 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006750 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006751 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6752 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006753 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006754 goto cleanup;
6755 }
6756
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006757 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006758 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006759 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006760 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006761 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 +01006762 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006763 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 +01006764 ret = LY_EVALID;
6765 goto cleanup;
6766 }
6767
6768 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006769 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006770 } else {
6771 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006772 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006773 }
6774 }
6775
6776 /* merge this set into the global when set */
6777 lyxp_set_scnode_merge(set, &tmp_set);
6778 }
6779
6780 /* check when of non-data parents as well */
6781 node = node->parent;
6782 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6783
Michal Vasko251f56e2019-11-14 16:06:47 +01006784 /* this node when was checked (xp_scnode could have been reallocd) */
6785 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006786 }
6787
6788cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006789 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006790 return ret;
6791}
6792
6793/**
Michal Vasko175012e2019-11-06 15:49:14 +01006794 * @brief Check when/must expressions of a node on a compiled schema tree.
6795 * @param[in] ctx Compile context.
6796 * @param[in] node Node to check.
6797 * @return LY_ERR value
6798 */
6799static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006800lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006801{
Michal Vasko175012e2019-11-06 15:49:14 +01006802 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006803 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006804 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006805 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006806 struct lysc_when **when = NULL;
6807 struct lysc_must *musts = NULL;
6808 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006809 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006810
6811 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006812 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006813 if (node->flags & LYS_CONFIG_R) {
6814 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6815 if (op) {
6816 /* we are actually in output */
6817 opts = LYXP_SCNODE_OUTPUT;
6818 }
6819 }
Michal Vasko175012e2019-11-06 15:49:14 +01006820
6821 switch (node->nodetype) {
6822 case LYS_CONTAINER:
6823 when = ((struct lysc_node_container *)node)->when;
6824 musts = ((struct lysc_node_container *)node)->musts;
6825 break;
6826 case LYS_CHOICE:
6827 when = ((struct lysc_node_choice *)node)->when;
6828 break;
6829 case LYS_LEAF:
6830 when = ((struct lysc_node_leaf *)node)->when;
6831 musts = ((struct lysc_node_leaf *)node)->musts;
6832 break;
6833 case LYS_LEAFLIST:
6834 when = ((struct lysc_node_leaflist *)node)->when;
6835 musts = ((struct lysc_node_leaflist *)node)->musts;
6836 break;
6837 case LYS_LIST:
6838 when = ((struct lysc_node_list *)node)->when;
6839 musts = ((struct lysc_node_list *)node)->musts;
6840 break;
6841 case LYS_ANYXML:
6842 case LYS_ANYDATA:
6843 when = ((struct lysc_node_anydata *)node)->when;
6844 musts = ((struct lysc_node_anydata *)node)->musts;
6845 break;
6846 case LYS_CASE:
6847 when = ((struct lysc_node_case *)node)->when;
6848 break;
6849 case LYS_NOTIF:
6850 musts = ((struct lysc_notif *)node)->musts;
6851 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006852 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006853 case LYS_ACTION:
6854 /* first process input musts */
6855 musts = ((struct lysc_action *)node)->input.musts;
6856 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006857 default:
6858 /* nothing to check */
6859 break;
6860 }
6861
Michal Vasko175012e2019-11-06 15:49:14 +01006862 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006863 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006864 ret = lyxp_atomize(when[u]->cond, LYD_SCHEMA, when[u]->module, when[u]->context ? when[u]->context : node,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006865 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006866 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006867 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006868 goto cleanup;
6869 }
6870
Michal Vaskodc052f32019-11-07 11:11:38 +01006871 ctx->path[0] = '\0';
6872 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006873 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006874 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006875 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6876 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006877
Michal Vaskoecd62de2019-11-13 12:35:11 +01006878 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006879 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006880 schema->name);
6881 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006882
6883 /* check dummy node accessing */
6884 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006885 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006886 ret = LY_EVALID;
6887 goto cleanup;
6888 }
Michal Vasko175012e2019-11-06 15:49:14 +01006889 }
6890 }
6891
Michal Vaskoecd62de2019-11-13 12:35:11 +01006892 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006893 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006894 LY_CHECK_GOTO(ret, cleanup);
6895
Michal Vaskod3678892020-05-21 10:06:58 +02006896 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006897 }
6898
Michal Vasko5d8756a2019-11-07 15:21:00 +01006899check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006900 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006901 LY_ARRAY_FOR(musts, u) {
6902 ret = lyxp_atomize(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006903 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006904 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006905 goto cleanup;
6906 }
6907
Michal Vaskodc052f32019-11-07 11:11:38 +01006908 ctx->path[0] = '\0';
6909 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006910 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006911 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006912 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006913 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006914 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6915 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006916 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006917 }
6918 }
6919
Michal Vaskod3678892020-05-21 10:06:58 +02006920 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006921 }
6922
Michal Vasko1bf09392020-03-27 12:38:10 +01006923 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006924 /* now check output musts */
6925 input_done = 1;
6926 musts = ((struct lysc_action *)node)->output.musts;
6927 opts = LYXP_SCNODE_OUTPUT;
6928 goto check_musts;
6929 }
6930
Michal Vasko175012e2019-11-06 15:49:14 +01006931cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006932 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006933 return ret;
6934}
6935
Michal Vasko8d544252020-03-02 10:19:52 +01006936static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006937lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6938{
Michal Vasko6b26e742020-07-17 15:02:10 +02006939 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02006940 struct ly_path *p;
6941 struct lysc_type *type;
6942
6943 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6944
6945 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006946 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6947 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6948 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006949
6950 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006951 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02006952 ly_path_free(node->module->ctx, p);
6953
6954 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6955 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6956 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6957 lref->path->expr, lys_nodetype2str(target->nodetype));
6958 return LY_EVALID;
6959 }
6960
6961 /* check status */
6962 ctx->path[0] = '\0';
6963 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6964 ctx->path_len = strlen(ctx->path);
6965 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6966 return LY_EVALID;
6967 }
6968 ctx->path_len = 1;
6969 ctx->path[1] = '\0';
6970
6971 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02006972 if (lref->require_instance) {
6973 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent);
6974 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006975 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6976 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6977 return LY_EVALID;
6978 }
6979 }
6980
6981 /* store the target's type and check for circular chain of leafrefs */
6982 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6983 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6984 if (type == (struct lysc_type *)lref) {
6985 /* circular chain detected */
6986 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6987 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6988 return LY_EVALID;
6989 }
6990 }
6991
6992 /* check if leafref and its target are under common if-features */
6993 if (lys_compile_leafref_features_validate(node, target)) {
6994 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6995 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
6996 " features applicable to the leafref itself.", lref->path->expr);
6997 return LY_EVALID;
6998 }
6999
7000 return LY_SUCCESS;
7001}
7002
7003static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007004lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7005{
7006 struct lysc_ext_instance *ext;
7007 struct lysp_ext_instance *ext_p = NULL;
7008 struct lysp_stmt *stmt;
7009 const struct lys_module *ext_mod;
7010 LY_ERR ret = LY_SUCCESS;
7011
7012 /* create the parsed extension instance manually */
7013 ext_p = calloc(1, sizeof *ext_p);
7014 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
7015 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
7016 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
7017 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7018 ext_p->insubstmt_index = 0;
7019
7020 stmt = calloc(1, sizeof *ext_p->child);
7021 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
7022 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
7023 stmt->kw = LY_STMT_TYPE;
7024 ext_p->child = stmt;
7025
7026 /* allocate new extension instance */
7027 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7028
7029 /* manually get extension definition module */
7030 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7031
7032 /* compile the extension instance */
7033 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7034
7035cleanup:
7036 lysp_ext_instance_free(ctx->ctx, ext_p);
7037 free(ext_p);
7038 return ret;
7039}
7040
Michal Vasko004d3152020-06-11 19:59:22 +02007041static LY_ERR
7042lys_compile_unres(struct lysc_ctx *ctx)
7043{
7044 struct lysc_node *node;
7045 struct lysc_type *type, *typeiter;
7046 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007047 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007048 uint32_t i;
7049
7050 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7051 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7052 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7053 for (i = 0; i < ctx->leafrefs.count; ++i) {
7054 node = ctx->leafrefs.objs[i];
7055 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7056 type = ((struct lysc_node_leaf *)node)->type;
7057 if (type->basetype == LY_TYPE_LEAFREF) {
7058 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7059 } else if (type->basetype == LY_TYPE_UNION) {
7060 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7061 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7062 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7063 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7064 }
7065 }
7066 }
7067 }
7068 for (i = 0; i < ctx->leafrefs.count; ++i) {
7069 /* store pointer to the real type */
7070 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7071 if (type->basetype == LY_TYPE_LEAFREF) {
7072 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7073 typeiter->basetype == LY_TYPE_LEAFREF;
7074 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7075 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7076 } else if (type->basetype == LY_TYPE_UNION) {
7077 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7078 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7079 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7080 typeiter->basetype == LY_TYPE_LEAFREF;
7081 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7082 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7083 }
7084 }
7085 }
7086 }
7087
7088 /* check xpath */
7089 for (i = 0; i < ctx->xpath.count; ++i) {
7090 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7091 }
7092
7093 /* finish incomplete default values compilation */
7094 for (i = 0; i < ctx->dflts.count; ++i) {
7095 struct ly_err_item *err = NULL;
7096 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7097 LY_ERR ret;
7098 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7099 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7100 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7101 if (err) {
7102 ly_err_print(err);
7103 ctx->path[0] = '\0';
7104 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7106 "Invalid default - value does not fit the type (%s).", err->msg);
7107 ly_err_free(err);
7108 }
7109 LY_CHECK_RET(ret);
7110 }
7111
7112 return LY_SUCCESS;
7113}
7114
Radek Krejci19a96102018-11-15 13:38:09 +01007115LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007116lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007117{
7118 struct lysc_ctx ctx = {0};
7119 struct lysc_module *mod_c;
7120 struct lysp_module *sp;
7121 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007122 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007123 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007124 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007125 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007126 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007127 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007128
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007129 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007130
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007131 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007132 /* just imported modules are not compiled */
7133 return LY_SUCCESS;
7134 }
7135
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007136 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007137
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007138 ctx.ctx = (*mod)->ctx;
7139 ctx.mod = *mod;
7140 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007141 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007142 ctx.path_len = 1;
7143 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007144
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007145 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7146 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7147 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007148
Michal Vasko7c8439f2020-08-05 13:25:19 +02007149 LY_ARRAY_FOR(sp->imports, u) {
7150 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7151 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01007152 LY_ARRAY_FOR(sp->includes, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02007153 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007154 }
Radek Krejci0935f412019-08-20 16:15:18 +02007155
7156 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007157 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007158 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007159 mod_c->features = (*mod)->dis_features;
7160 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007161 } else {
7162 /* features are compiled directly into the compiled module structure,
7163 * 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 +02007164 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007165 LY_CHECK_GOTO(ret, error);
7166 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007167
Radek Krejci0af46292019-01-11 16:02:31 +01007168 /* finish feature compilation, not only for the main module, but also for the submodules.
7169 * Due to possible forward references, it must be done when all the features (including submodules)
7170 * are present. */
7171 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007172 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007173 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7174 }
Radek Krejci327de162019-06-14 12:52:07 +02007175 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007176 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007177 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007178 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007179 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007180 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7181 }
Radek Krejci327de162019-06-14 12:52:07 +02007182 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007183 }
Radek Krejci327de162019-06-14 12:52:07 +02007184 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007185
Michal Vasko33ff9422020-07-03 09:50:39 +02007186 /* identities, work similarly to features with the precompilation */
7187 if ((*mod)->dis_identities) {
7188 mod_c->identities = (*mod)->dis_identities;
7189 (*mod)->dis_identities = NULL;
7190 } else {
7191 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7192 LY_CHECK_GOTO(ret, error);
7193 }
Radek Krejci19a96102018-11-15 13:38:09 +01007194 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007195 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007196 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007197 lysc_update_path(&ctx, NULL, "{submodule}");
7198 LY_ARRAY_FOR(sp->includes, v) {
7199 if (sp->includes[v].submodule->identities) {
7200 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7201 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7202 LY_CHECK_GOTO(ret, error);
7203 lysc_update_path(&ctx, NULL, NULL);
7204 }
7205 }
Radek Krejci327de162019-06-14 12:52:07 +02007206 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007207
Radek Krejci95710c92019-02-11 15:49:55 +01007208 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007209 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007210 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007211 }
Radek Krejci95710c92019-02-11 15:49:55 +01007212
Radek Krejciec4da802019-05-02 13:02:41 +02007213 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7214 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007215
Radek Krejci95710c92019-02-11 15:49:55 +01007216 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007217 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007218 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007219 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007220 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007221
Radek Krejci474f9b82019-07-24 11:36:37 +02007222 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007223 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007224
Radek Krejci0935f412019-08-20 16:15:18 +02007225 /* extension instances TODO cover extension instances from submodules */
7226 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007227
Michal Vasko004d3152020-06-11 19:59:22 +02007228 /* finish compilation for all unresolved items in the context */
7229 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007230
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007231 /* validate non-instantiated groupings from the parsed schema,
7232 * without it we would accept even the schemas with invalid grouping specification */
7233 ctx.options |= LYSC_OPT_GROUPING;
7234 LY_ARRAY_FOR(sp->groupings, u) {
7235 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007236 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007237 }
7238 }
7239 LY_LIST_FOR(sp->data, node_p) {
7240 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7241 LY_ARRAY_FOR(grps, u) {
7242 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007243 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007244 }
7245 }
7246 }
7247
Radek Krejci474f9b82019-07-24 11:36:37 +02007248 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7249 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7250 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7251 }
7252
Michal Vasko8d544252020-03-02 10:19:52 +01007253#if 0
7254 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7255 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7256 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7257 * the anotation definitions available in the internal schema structure. */
7258 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7259 if (lyp_add_ietf_netconf_annotations(mod)) {
7260 lys_free(mod, NULL, 1, 1);
7261 return NULL;
7262 }
7263 }
7264#endif
7265
7266 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007267 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7268 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007269 }
7270
Radek Krejci1c0c3442019-07-23 16:08:47 +02007271 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007272 ly_set_erase(&ctx.xpath, NULL);
7273 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007274 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007275 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007276 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007277
Radek Krejciec4da802019-05-02 13:02:41 +02007278 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007279 lysp_module_free((*mod)->parsed);
7280 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007281 }
7282
Radek Krejciec4da802019-05-02 13:02:41 +02007283 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007284 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007285 for (i = 0; i < ctx.ctx->list.count; ++i) {
7286 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007287 if (m->implemented == 2) {
7288 m->implemented = 1;
7289 }
7290 }
7291 }
7292
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007293 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007294 return LY_SUCCESS;
7295
7296error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007297 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007298 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007299 ly_set_erase(&ctx.xpath, NULL);
7300 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007301 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007302 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007303 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007304 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007305 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007306
7307 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007308 for (i = 0; i < ctx.ctx->list.count; ++i) {
7309 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007310 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007311 /* revert features list to the precompiled state */
7312 lys_feature_precompile_revert(&ctx, m);
7313 /* mark module as imported-only / not-implemented */
7314 m->implemented = 0;
7315 /* free the compiled version of the module */
7316 lysc_module_free(m->compiled, NULL);
7317 m->compiled = NULL;
7318 }
7319 }
7320
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007321 /* remove the module itself from the context and free it */
7322 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7323 lys_module_free(*mod, NULL);
7324 *mod = NULL;
7325
Radek Krejci19a96102018-11-15 13:38:09 +01007326 return ret;
7327}